5 * Copyright (C) 2014-2015 Matteo Nastasi
6 * mailto: nastasi@alternativeoutput.it
7 * matteo.nastasi@milug.org
8 * web: http://www.alternativeoutput.it
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.
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.
27 require_once($G_base."Obj/brisk.phh");
28 require_once($G_base."Obj/dbase_${G_dbasetype}.phh");
32 printf("%s ... SUCCESS<br>\n", $s);
37 printf("%s ... FAILED<br>\n", $s);
42 if (($bdb = BriskDB::create()) == FALSE) {
47 if ($bdb->transaction("BEGIN") == FALSE) {
53 if ($bdb->transaction("COMMIT") == FALSE) {
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" );
61 if ($bdb->query("DROP TABLE IF EXISTS test_ip;") == FALSE) {
62 fail("DROP TABLE test_ip");
65 succ("DROP TABLE test_ip");
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
72 fail("CREATE TABLE test_ip");
75 succ("CREATE TABLE test_ip");
77 foreach ($arr_ip as $i) {
80 $msg = sprintf(" 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());
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");
100 for ($r = 0 ; $r < pg_numrows($cmp_pg) ; $r++) {
101 $cmp_obj = pg_fetch_object($cmp_pg, $r);
103 if ($ip_obj->ip & $msk != $cmp) {
104 fail(sprintf(" Expected: %s, retrieved: %s", int2ip($cmp), int2ip($ip_obj->ip & $msk)));
107 succ(sprintf(" Expected: %s (%s)", int2ip($cmp), int2ip($cmp_obj->ip)));
109 // printf("RET IP: %d IP: %s<br>\n", $ip_obj->ip, $v));
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");
119 succ("SELECT * FROM test_ip");
121 for ($r = 0 ; $r < pg_numrows($ip_pg) ; $r++) {
122 $ip_obj = pg_fetch_object($ip_pg, $r);
124 $v = int2ip($ip_obj->ip);
126 if ($arr_ip[$r] != $v) {
127 fail(sprintf(" Expected: %s, retrieved: %s", $arr_ip[$r], $v));
130 succ(sprintf(" Expected: %s", $arr_ip[$r]));
132 // printf("RET IP: %d IP: %s<br>\n", $ip_obj->ip, $v));
136 if ($bdb->query("DROP TABLE IF EXISTS test_ip;") == FALSE) {
137 fail("DROP TABLE test_ip");
140 succ("DROP TABLE test_ip");
143 if ($bdb->transaction("BEgIN") != FALSE) {
144 fail("BEgIN missed fail");
147 succ("BEgIN missed fail");