xref: /PHP-8.3/ext/pgsql/tests/bug71998.phpt (revision 16a63d7b)
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 = true;
61    try {
62    	@pg_insert($db, 'tmp_statistics', $data);
63    } catch (\ValueError $e) {
64        echo $e->getMessage() . PHP_EOL;
65        $r = false;
66    }
67
68    if (!$r && in_array($ip, $bad)) {
69        $errors++;
70        //echo pg_last_error($db);
71    }
72
73    //pg_query($db, "INSERT INTO tmp_statistics (id, remote_addr) VALUES (2, '127.0.0.1')"); // OK, record inserted
74}
75
76
77$r = pg_query($db, "SELECT * FROM tmp_statistics");
78while (false != ($row = pg_fetch_row($r))) {
79    var_dump($row);
80}
81echo $errors, " errors caught\n";
82
83pg_query($db, "DROP TABLE tmp_statistics");
84pg_close($db);
85
86?>
87--EXPECT--
88pg_insert(): Field "remote_addr" must be a valid IPv4 or IPv6 address string, "256.257.258.259" given
89pg_insert(): Field "remote_addr" must be a valid IPv4 or IPv6 address string, "fe08::7:8interface" given
90pg_insert(): Field "remote_addr" must be a valid IPv4 or IPv6 address string, "schnitzel" given
91pg_insert(): Field "remote_addr" must be a valid IPv4 or IPv6 address string, "10002.3.4" given
92pg_insert(): Field "remote_addr" must be a valid IPv4 or IPv6 address string, "1.2.3.4.5" given
93pg_insert(): Field "remote_addr" must be a valid IPv4 or IPv6 address string, "256.0.0.0" given
94pg_insert(): Field "remote_addr" must be a valid IPv4 or IPv6 address string, "260.0.0.0" given
95array(2) {
96  [0]=>
97  string(1) "1"
98  [1]=>
99  string(9) "127.0.0.1"
100}
101array(2) {
102  [0]=>
103  string(1) "2"
104  [1]=>
105  string(8) "10.0.0.1"
106}
107array(2) {
108  [0]=>
109  string(1) "3"
110  [1]=>
111  string(11) "192.168.1.1"
112}
113array(2) {
114  [0]=>
115  string(1) "4"
116  [1]=>
117  string(7) "0.0.0.0"
118}
119array(2) {
120  [0]=>
121  string(1) "5"
122  [1]=>
123  string(15) "255.255.255.255"
124}
125array(2) {
126  [0]=>
127  string(1) "6"
128  [1]=>
129  string(15) "192.168.1.35/24"
130}
131array(2) {
132  [0]=>
133  string(1) "7"
134  [1]=>
135  string(3) "::1"
136}
137array(2) {
138  [0]=>
139  string(1) "8"
140  [1]=>
141  string(10) "::10.2.3.4"
142}
143array(2) {
144  [0]=>
145  string(1) "9"
146  [1]=>
147  string(15) "::ffff:10.4.3.2"
148}
149array(2) {
150  [0]=>
151  string(2) "10"
152  [1]=>
153  string(15) "1:2:3:4:5:6:7:8"
154}
155array(2) {
156  [0]=>
157  string(2) "11"
158  [1]=>
159  string(15) "::ffff:10.0.0.1"
160}
161array(2) {
162  [0]=>
163  string(2) "12"
164  [1]=>
165  string(14) "::ffff:1.2.3.4"
166}
167array(2) {
168  [0]=>
169  string(2) "13"
170  [1]=>
171  string(14) "::ffff:0.0.0.0"
172}
173array(2) {
174  [0]=>
175  string(2) "14"
176  [1]=>
177  string(17) "1:2:3:4:5:6:77:88"
178}
179array(2) {
180  [0]=>
181  string(2) "15"
182  [1]=>
183  string(22) "::ffff:255.255.255.255"
184}
185array(2) {
186  [0]=>
187  string(2) "16"
188  [1]=>
189  string(9) "fe08::7:8"
190}
191array(2) {
192  [0]=>
193  string(2) "17"
194  [1]=>
195  string(39) "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
196}
197array(2) {
198  [0]=>
199  string(2) "18"
200  [1]=>
201  string(13) "::5:aef1:ffff"
202}
203array(2) {
204  [0]=>
205  string(2) "19"
206  [1]=>
207  string(19) "2001:4f8:3:ba::/112"
208}
2097 errors caught
210