From 5f3e18722e863a3caa27ddcbb2380440da5d1d61 Mon Sep 17 00:00:00 2001 From: "Matteo Nastasi (mop)" Date: Thu, 3 Sep 2015 08:01:35 +0200 Subject: [PATCH] add test_ip checks --- webtest/test_db.php | 89 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 81 insertions(+), 8 deletions(-) diff --git a/webtest/test_db.php b/webtest/test_db.php index 1c0332b..a656631 100644 --- a/webtest/test_db.php +++ b/webtest/test_db.php @@ -27,30 +27,103 @@ $G_base = "./"; require_once($G_base."Obj/brisk.phh"); require_once($G_base."Obj/dbase_${G_dbasetype}.phh"); +function succ($s) +{ + printf("%s ... SUCCESS
\n", $s); +} + +function fail($s) +{ + printf("%s ... FAILED
\n", $s); +} + function main() { do { if (($bdb = BriskDB::create()) == FALSE) { - printf("DB creation failed
\n"); + fail("DB Creation"); break; } - printf("DB creation success
\n"); + succ("DB Creation"); if ($bdb->transaction("BEGIN") == FALSE) { - printf("BEGIN failed
\n"); + fail("BEGIN"); break; } - printf("BEGIN success
\n"); + succ("BEGIN"); if ($bdb->transaction("COMMIT") == FALSE) { - printf("COMMIT failed
\n"); + fail("COMMIT"); + break; + } + succ("COMMIT"); + + $arr_ip = array("1.1.1.1", "127.0.0.1", "255.255.255.255", "255.255.0.0" ); + + if ($bdb->query("DROP TABLE IF EXISTS test_ip;") == FALSE) { + fail("DROP TABLE test_ip"); + break; + } + succ("DROP TABLE test_ip"); + + if ($bdb->query("CREATE TABLE test_ip ( + code integer, -- insertion code + ip integer, -- ip v4 address + atime timestamp DEFAULT to_timestamp(0) -- access time + ); ") == FALSE) { + fail("CREATE TABLE test_ip"); + break; + } + succ("CREATE TABLE test_ip"); + + foreach ($arr_ip as $i) { + $v = $v_or = ip2long($i); + if (PHP_INT_SIZE == 8 && $v & 0x80000000) + $v = 0xffffffff00000000 | $v_or; + + if ($bdb->query(sprintf("INSERT INTO test_ip (ip, atime) VALUES (CAST(%d AS integer), '1999-01-08 04:05:06');", $v)) == FALSE) { + printf("%s
\n", $bdb->last_error()); + fail("INSERT INTO test_ip ".$i." V: ".$v." V_or: ".$v_or); + break; + } + succ("INSERT INTO test_ip ".$i." V: ".$v." V_or: ".$v_or); + } + + if (($ip_pg = $bdb->query(sprintf("SELECT * FROM test_ip ORDER BY code;"))) == FALSE) { + printf("%s
\n", $bdb->last_error()); + fail("SELECT * FROM test_ip"); break; } - printf("COMMIT success
\n"); + succ("SELECT * FROM test_ip"); + + for ($r = 0 ; $r < pg_numrows($ip_pg) ; $r++) { + $ip_obj = pg_fetch_object($ip_pg, $r); + + if (PHP_INT_SIZE == 8) + $v = $ip_obj->ip & 0x00000000ffffffff; + else + $v = $ip_obj->ip; + + if ($arr_ip[$r] != long2ip($v)) { + fail(sprintf(" Expected: %s, retrieved: %s", $arr_ip[$r], long2ip($v))); + } + else { + succ(sprintf(" Expected: %s", $arr_ip[$r])); + } + // printf("RET IP: %d V: %d IP: %s
\n", $ip_obj->ip, $v, long2ip($v)); + + } + + if ($bdb->query("DROP TABLE IF EXISTS test_ip;") == FALSE) { + fail("DROP TABLE test_ip"); + break; + } + succ("DROP TABLE test_ip"); + if ($bdb->transaction("BEgIN") != FALSE) { - printf("BEgIN missed fail
\n"); + fail("BEgIN missed fail"); break; } - printf("BEgIN fail correctly
\n"); + succ("BEgIN missed fail"); } while (FALSE); } -- 2.17.1