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