xref: /PHP-7.2/ext/pgsql/tests/bug71998.phpt (revision 17ccbeec)
1--TEST--
2Bug #71998 Function pg_insert does not insert when column type = inet
3--SKIPIF--
4<?php include("skipif.inc"); ?>
5--FILE--
6<?php
7// Kudos for the IP regex to
8// http://stackoverflow.com/a/17871737/3358424
9
10include('config.inc');
11
12$db = pg_connect($conn_str);
13
14pg_query("CREATE TABLE tmp_statistics (id integer NOT NULL, remote_addr inet);");
15
16$ips = array(
17	/* IPv4*/
18	"127.0.0.1",
19	"10.0.0.1",
20	"192.168.1.1",
21	"0.0.0.0",
22	"255.255.255.255",
23	"192.168.1.35/24",
24
25	/* IPv6 */
26	"::1",
27	"::10.2.3.4",
28	"::ffff:10.4.3.2",
29	"1:2:3:4:5:6:7:8",
30	"::ffff:10.0.0.1",
31	"::ffff:1.2.3.4",
32	"::ffff:0.0.0.0",
33	"1:2:3:4:5:6:77:88",
34	"::ffff:255.255.255.255",
35	"fe08::7:8",
36	"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
37	"::5:aef1:ffff/128",
38	"2001:4f8:3:ba::/112",
39
40);
41
42$bad = array(
43	/* bad */
44	"256.257.258.259",
45	"fe08::7:8interface",
46	"schnitzel",
47	"10002.3.4",
48	"1.2.3.4.5",
49	"256.0.0.0",
50	"260.0.0.0",
51);
52
53$ips = array_merge($ips, $bad);
54$i = 0;
55$errors = 0;
56foreach ($ips as $ip) {
57	$data = array("id" => ++$i, "remote_addr" => $ip);
58	$r = @pg_insert($db, 'tmp_statistics', $data);
59
60	if (!$r && in_array($ip, $bad)) {
61		$errors++;
62		//echo pg_last_error($db);
63	}
64
65	//pg_query($db, "INSERT INTO tmp_statistics (id, remote_addr) VALUES (2, '127.0.0.1')"); // OK, record inserted
66}
67
68
69$r = pg_query($db, "SELECT * FROM tmp_statistics");
70while (false != ($row = pg_fetch_row($r))) {
71	var_dump($row);
72}
73echo $errors, " errors catched\n";
74
75pg_query($db, "DROP TABLE tmp_statistics");
76pg_close($db);
77
78?>
79==DONE==
80--EXPECT--
81array(2) {
82  [0]=>
83  string(1) "1"
84  [1]=>
85  string(9) "127.0.0.1"
86}
87array(2) {
88  [0]=>
89  string(1) "2"
90  [1]=>
91  string(8) "10.0.0.1"
92}
93array(2) {
94  [0]=>
95  string(1) "3"
96  [1]=>
97  string(11) "192.168.1.1"
98}
99array(2) {
100  [0]=>
101  string(1) "4"
102  [1]=>
103  string(7) "0.0.0.0"
104}
105array(2) {
106  [0]=>
107  string(1) "5"
108  [1]=>
109  string(15) "255.255.255.255"
110}
111array(2) {
112  [0]=>
113  string(1) "6"
114  [1]=>
115  string(15) "192.168.1.35/24"
116}
117array(2) {
118  [0]=>
119  string(1) "7"
120  [1]=>
121  string(3) "::1"
122}
123array(2) {
124  [0]=>
125  string(1) "8"
126  [1]=>
127  string(10) "::10.2.3.4"
128}
129array(2) {
130  [0]=>
131  string(1) "9"
132  [1]=>
133  string(15) "::ffff:10.4.3.2"
134}
135array(2) {
136  [0]=>
137  string(2) "10"
138  [1]=>
139  string(15) "1:2:3:4:5:6:7:8"
140}
141array(2) {
142  [0]=>
143  string(2) "11"
144  [1]=>
145  string(15) "::ffff:10.0.0.1"
146}
147array(2) {
148  [0]=>
149  string(2) "12"
150  [1]=>
151  string(14) "::ffff:1.2.3.4"
152}
153array(2) {
154  [0]=>
155  string(2) "13"
156  [1]=>
157  string(14) "::ffff:0.0.0.0"
158}
159array(2) {
160  [0]=>
161  string(2) "14"
162  [1]=>
163  string(17) "1:2:3:4:5:6:77:88"
164}
165array(2) {
166  [0]=>
167  string(2) "15"
168  [1]=>
169  string(22) "::ffff:255.255.255.255"
170}
171array(2) {
172  [0]=>
173  string(2) "16"
174  [1]=>
175  string(9) "fe08::7:8"
176}
177array(2) {
178  [0]=>
179  string(2) "17"
180  [1]=>
181  string(39) "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
182}
183array(2) {
184  [0]=>
185  string(2) "18"
186  [1]=>
187  string(13) "::5:aef1:ffff"
188}
189array(2) {
190  [0]=>
191  string(2) "19"
192  [1]=>
193  string(19) "2001:4f8:3:ba::/112"
194}
1957 errors catched
196==DONE==
197