add test_ip checks
[brisk.git] / webtest / test_db.php
1 <?php
2 /*
3  *  brisk - test_db.php
4  *
5  *  Copyright (C) 2014 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" );
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 = $v_or = ip2long($i);
79             if (PHP_INT_SIZE == 8 && $v & 0x80000000)
80                 $v = 0xffffffff00000000 | $v_or;
81
82             if ($bdb->query(sprintf("INSERT INTO test_ip (ip, atime) VALUES (CAST(%d AS integer), '1999-01-08 04:05:06');",  $v)) == FALSE) {
83                 printf("%s<br>\n", $bdb->last_error());
84                 fail("INSERT INTO test_ip ".$i."  V: ".$v."  V_or: ".$v_or);
85                 break;
86             }
87             succ("INSERT INTO test_ip ".$i."  V: ".$v."  V_or: ".$v_or);
88         }
89
90         if (($ip_pg = $bdb->query(sprintf("SELECT * FROM test_ip ORDER BY code;"))) == FALSE) {
91             printf("%s<br>\n", $bdb->last_error());
92             fail("SELECT * FROM test_ip");
93             break;
94         }
95         succ("SELECT * FROM test_ip");
96
97         for ($r = 0 ; $r < pg_numrows($ip_pg) ; $r++) {
98             $ip_obj = pg_fetch_object($ip_pg, $r);
99
100             if (PHP_INT_SIZE == 8)
101                 $v =  $ip_obj->ip & 0x00000000ffffffff;
102             else
103                 $v = $ip_obj->ip;
104
105             if ($arr_ip[$r] != long2ip($v)) {
106                 fail(sprintf("  Expected: %s, retrieved: %s", $arr_ip[$r], long2ip($v)));
107             }
108             else {
109                 succ(sprintf("  Expected: %s", $arr_ip[$r]));
110             }
111             // printf("RET IP: %d V: %d  IP: %s<br>\n", $ip_obj->ip, $v, long2ip($v));
112
113         }
114
115         if ($bdb->query("DROP TABLE IF EXISTS test_ip;") == FALSE) {
116             fail("DROP TABLE test_ip");
117             break;
118         }
119         succ("DROP TABLE test_ip");
120
121
122         if ($bdb->transaction("BEgIN") != FALSE) {
123             fail("BEgIN missed fail");
124             break;
125         }
126         succ("BEgIN missed fail");
127     } while (FALSE);
128 }
129
130 main();
131 ?>