xref: /PHP-8.0/ext/pgsql/tests/bug71998.phpt (revision f8d79582)
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 caught\n";
74
75pg_query($db, "DROP TABLE tmp_statistics");
76pg_close($db);
77
78?>
79--EXPECT--
80array(2) {
81  [0]=>
82  string(1) "1"
83  [1]=>
84  string(9) "127.0.0.1"
85}
86array(2) {
87  [0]=>
88  string(1) "2"
89  [1]=>
90  string(8) "10.0.0.1"
91}
92array(2) {
93  [0]=>
94  string(1) "3"
95  [1]=>
96  string(11) "192.168.1.1"
97}
98array(2) {
99  [0]=>
100  string(1) "4"
101  [1]=>
102  string(7) "0.0.0.0"
103}
104array(2) {
105  [0]=>
106  string(1) "5"
107  [1]=>
108  string(15) "255.255.255.255"
109}
110array(2) {
111  [0]=>
112  string(1) "6"
113  [1]=>
114  string(15) "192.168.1.35/24"
115}
116array(2) {
117  [0]=>
118  string(1) "7"
119  [1]=>
120  string(3) "::1"
121}
122array(2) {
123  [0]=>
124  string(1) "8"
125  [1]=>
126  string(10) "::10.2.3.4"
127}
128array(2) {
129  [0]=>
130  string(1) "9"
131  [1]=>
132  string(15) "::ffff:10.4.3.2"
133}
134array(2) {
135  [0]=>
136  string(2) "10"
137  [1]=>
138  string(15) "1:2:3:4:5:6:7:8"
139}
140array(2) {
141  [0]=>
142  string(2) "11"
143  [1]=>
144  string(15) "::ffff:10.0.0.1"
145}
146array(2) {
147  [0]=>
148  string(2) "12"
149  [1]=>
150  string(14) "::ffff:1.2.3.4"
151}
152array(2) {
153  [0]=>
154  string(2) "13"
155  [1]=>
156  string(14) "::ffff:0.0.0.0"
157}
158array(2) {
159  [0]=>
160  string(2) "14"
161  [1]=>
162  string(17) "1:2:3:4:5:6:77:88"
163}
164array(2) {
165  [0]=>
166  string(2) "15"
167  [1]=>
168  string(22) "::ffff:255.255.255.255"
169}
170array(2) {
171  [0]=>
172  string(2) "16"
173  [1]=>
174  string(9) "fe08::7:8"
175}
176array(2) {
177  [0]=>
178  string(2) "17"
179  [1]=>
180  string(39) "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
181}
182array(2) {
183  [0]=>
184  string(2) "18"
185  [1]=>
186  string(13) "::5:aef1:ffff"
187}
188array(2) {
189  [0]=>
190  string(2) "19"
191  [1]=>
192  string(19) "2001:4f8:3:ba::/112"
193}
1947 errors caught
195