fix missing bold for your account item
[brisk.git] / webtest / test_db.php
1 <?php
2 /*
3  *  brisk - test_db.php
4  *
5  *  Copyright (C) 2014-2015 Matteo Nastasi
6  *                          mailto: nastasi@alternativeoutput.it
7  *                                  matteo.nastasi@milug.org
8  *                          web: http://www.alternativeoutput.it
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * General Public License for more details. You should have received a
19  * copy of the GNU General Public License along with this program; if
20  * not, write to the Free Software Foundation, Inc, 59 Temple Place -
21  * Suite 330, Boston, MA 02111-1307, USA.
22  *
23  */
24
25 $G_base = "./";
26
27 require_once($G_base."Obj/brisk.phh");
28 require_once($G_base."Obj/dbase_${G_dbasetype}.phh");
29
30 function succ($s)
31 {
32     printf("%s ... SUCCESS<br>\n", $s);
33 }
34
35 function fail($s)
36 {
37     printf("%s ... FAILED<br>\n", $s);
38 }
39
40 function main() {
41     do {
42         if (($bdb = BriskDB::create()) == FALSE) {
43             fail("DB Creation");
44             break;
45         }
46         succ("DB Creation");
47         if ($bdb->transaction("BEGIN") == FALSE) {
48             fail("BEGIN");
49             break;
50         }
51         succ("BEGIN");
52
53         if ($bdb->transaction("COMMIT") == FALSE) {
54             fail("COMMIT");
55             break;
56         }
57         succ("COMMIT");
58
59         $arr_ip = array("1.1.1.1", "127.0.0.1", "255.255.255.255", "255.255.0.0", "201.102.123.111", "5.9.11.13" );
60
61         if ($bdb->query("DROP TABLE IF EXISTS test_ip;") == FALSE) {
62             fail("DROP TABLE test_ip");
63             break;
64         }
65         succ("DROP TABLE test_ip");
66
67         if ($bdb->query("CREATE TABLE test_ip (
68        code       integer,                           -- insertion code
69        ip         integer,                           -- ip v4 address
70        atime      timestamp DEFAULT to_timestamp(0)  -- access time
71        ); ") == FALSE) {
72             fail("CREATE TABLE test_ip");
73             break;
74         }
75         succ("CREATE TABLE test_ip");
76
77         foreach ($arr_ip as $i) {
78             $v = ip2four($i);
79
80             $msg = sprintf("&nbsp;&nbsp;INSERT INTO test_ip [%s]  val: [%d (%x)]", $i, $v, $v);
81             if ($bdb->query(sprintf("INSERT INTO test_ip (ip, atime) VALUES (%d, '1999-01-08 04:05:06');", $v)) == FALSE) {
82                 printf("%s<br>\n", $bdb->last_error());
83                 fail($msg);
84                 break;
85             }
86             succ($msg);
87         }
88         printf("<br>\n");
89         foreach ($arr_ip as $i) {
90             $cmp = int2four(ip2int($i) & 0xffffff00);
91             $msk = int2four(0xffffff00);
92             $cmp_que = sprintf("SELECT * FROM test_ip WHERE (ip & %d = %d);", $msk, $cmp);
93             if (($cmp_pg = $bdb->query($cmp_que)) == FALSE) {
94                 printf("%s<br>\n", $bdb->last_error());
95                 fail("SELECT * FROM test_ip");
96                 break;
97             }
98             succ($cmp_que);
99
100             for ($r = 0 ; $r < pg_numrows($cmp_pg) ; $r++) {
101                 $cmp_obj = pg_fetch_object($cmp_pg, $r);
102
103                 if ($ip_obj->ip & $msk != $cmp) {
104                     fail(sprintf("&nbsp;&nbsp;Expected: %s, retrieved: %s", int2ip($cmp), int2ip($ip_obj->ip & $msk)));
105                 }
106                 else {
107                     succ(sprintf("&nbsp;&nbsp;Expected: %s (%s)", int2ip($cmp), int2ip($cmp_obj->ip)));
108                 }
109                 // printf("RET IP: %d  IP: %s<br>\n", $ip_obj->ip, $v));
110             }
111         }
112         printf("<br>\n");
113
114         if (($ip_pg = $bdb->query(sprintf("SELECT * FROM test_ip ORDER BY code;"))) == FALSE) {
115             printf("%s<br>\n", $bdb->last_error());
116             fail("SELECT * FROM test_ip");
117             break;
118         }
119         succ("SELECT * FROM test_ip");
120
121         for ($r = 0 ; $r < pg_numrows($ip_pg) ; $r++) {
122             $ip_obj = pg_fetch_object($ip_pg, $r);
123
124             $v = int2ip($ip_obj->ip);
125
126             if ($arr_ip[$r] != $v) {
127                 fail(sprintf("&nbsp;&nbsp;Expected: %s, retrieved: %s", $arr_ip[$r], $v));
128             }
129             else {
130                 succ(sprintf("&nbsp;&nbsp;Expected: %s", $arr_ip[$r]));
131             }
132             // printf("RET IP: %d  IP: %s<br>\n", $ip_obj->ip, $v));
133
134         }
135
136         if ($bdb->query("DROP TABLE IF EXISTS test_ip;") == FALSE) {
137             fail("DROP TABLE test_ip");
138             break;
139         }
140         succ("DROP TABLE test_ip");
141
142
143         if ($bdb->transaction("BEgIN") != FALSE) {
144             fail("BEgIN missed fail");
145             break;
146         }
147         succ("BEgIN missed fail");
148     } while (FALSE);
149 }
150
151 main();
152 ?>