Debian Codegraph @

src/interfaces/libpq/t/001_uri.pl

raw
1 # Copyright (c) 2021-2025, PostgreSQL Global Development Group
2 use strict;
3 use warnings FATAL => 'all';
4
5 use PostgreSQL::Test::Utils;
6 use Test::More;
7 use IPC::Run;
8
9
10 # List of URIs tests. For each test the first element is the input string, the
11 # second the expected stdout and the third the expected stderr. Optionally,
12 # additional arguments may specify key/value pairs which will override
13 # environment variables for the duration of the test.
14 my @tests = (
15 [
16 q{postgresql://uri-user:secret@host:12345/db},
17 q{user='uri-user' password='secret' dbname='db' host='host' port='12345' (inet)},
18 q{},
19 ],
20 [
21 q{postgresql://uri-user@host:12345/db},
22 q{user='uri-user' dbname='db' host='host' port='12345' (inet)}, q{},
23 ],
24 [
25 q{postgresql://uri-user@host/db},
26 q{user='uri-user' dbname='db' host='host' (inet)}, q{},
27 ],
28 [
29 q{postgresql://host:12345/db},
30 q{dbname='db' host='host' port='12345' (inet)}, q{},
31 ],
32 [ q{postgresql://host/db}, q{dbname='db' host='host' (inet)}, q{}, ],
33 [
34 q{postgresql://uri-user@host:12345/},
35 q{user='uri-user' host='host' port='12345' (inet)},
36 q{},
37 ],
38 [
39 q{postgresql://uri-user@host/},
40 q{user='uri-user' host='host' (inet)},
41 q{},
42 ],
43 [ q{postgresql://uri-user@}, q{user='uri-user' (local)}, q{}, ],
44 [ q{postgresql://host:12345/}, q{host='host' port='12345' (inet)}, q{}, ],
45 [ q{postgresql://host:12345}, q{host='host' port='12345' (inet)}, q{}, ],
46 [ q{postgresql://host/db}, q{dbname='db' host='host' (inet)}, q{}, ],
47 [ q{postgresql://host/}, q{host='host' (inet)}, q{}, ],
48 [ q{postgresql://host}, q{host='host' (inet)}, q{}, ],
49 [ q{postgresql://}, q{(local)}, q{}, ],
50 [
51 q{postgresql://?hostaddr=127.0.0.1}, q{hostaddr='127.0.0.1' (inet)},
52 q{},
53 ],
54 [
55 q{postgresql://example.com?hostaddr=63.1.2.4},
56 q{host='example.com' hostaddr='63.1.2.4' (inet)},
57 q{},
58 ],
59 [ q{postgresql://%68ost/}, q{host='host' (inet)}, q{}, ],
60 [
61 q{postgresql://host/db?user=uri-user},
62 q{user='uri-user' dbname='db' host='host' (inet)},
63 q{},
64 ],
65 [
66 q{postgresql://host/db?user=uri-user&port=12345},
67 q{user='uri-user' dbname='db' host='host' port='12345' (inet)},
68 q{},
69 ],
70 [
71 q{postgresql://host/db?u%73er=someotheruser&port=12345},
72 q{user='someotheruser' dbname='db' host='host' port='12345' (inet)},
73 q{},
74 ],
75 [
76 q{postgresql://host/db?u%7aer=someotheruser&port=12345}, q{},
77 q{libpq_uri_regress: invalid URI query parameter: "uzer"},
78 ],
79 [
80 q{postgresql://host:12345?user=uri-user},
81 q{user='uri-user' host='host' port='12345' (inet)},
82 q{},
83 ],
84 [
85 q{postgresql://host?user=uri-user},
86 q{user='uri-user' host='host' (inet)},
87 q{},
88 ],
89 [
90 # Leading and trailing spaces, works.
91 q{postgresql://host? user = uri-user & port = 12345 },
92 q{user='uri-user' host='host' port='12345' (inet)},
93 q{},
94 ],
95 [
96 # Trailing data in parameter.
97 q{postgresql://host? user user = uri & port = 12345 12 },
98 q{},
99 q{libpq_uri_regress: unexpected spaces found in " user user ", use percent-encoded spaces (%20) instead},
100 ],
101 [
102 # Trailing data in value.
103 q{postgresql://host? user = uri-user & port = 12345 12 },
104 q{},
105 q{libpq_uri_regress: unexpected spaces found in " 12345 12 ", use percent-encoded spaces (%20) instead},
106 ],
107 [ q{postgresql://host?}, q{host='host' (inet)}, q{}, ],
108 [
109 q{postgresql://[::1]:12345/db},
110 q{dbname='db' host='::1' port='12345' (inet)},
111 q{},
112 ],
113 [ q{postgresql://[::1]/db}, q{dbname='db' host='::1' (inet)}, q{}, ],
114 [
115 q{postgresql://[2001:db8::1234]/}, q{host='2001:db8::1234' (inet)},
116 q{},
117 ],
118 [
119 q{postgresql://[200z:db8::1234]/}, q{host='200z:db8::1234' (inet)},
120 q{},
121 ],
122 [ q{postgresql://[::1]}, q{host='::1' (inet)}, q{}, ],
123 [ q{postgres://}, q{(local)}, q{}, ],
124 [ q{postgres:///}, q{(local)}, q{}, ],
125 [ q{postgres:///db}, q{dbname='db' (local)}, q{}, ],
126 [
127 q{postgres://uri-user@/db}, q{user='uri-user' dbname='db' (local)},
128 q{},
129 ],
130 [
131 q{postgres://?host=/path/to/socket/dir},
132 q{host='/path/to/socket/dir' (local)},
133 q{},
134 ],
135 [
136 q{postgresql://host?uzer=}, q{},
137 q{libpq_uri_regress: invalid URI query parameter: "uzer"},
138 ],
139 [
140 q{postgre://},
141 q{},
142 q{libpq_uri_regress: missing "=" after "postgre://" in connection info string},
143 ],
144 [
145 q{postgres://[::1},
146 q{},
147 q{libpq_uri_regress: end of string reached when looking for matching "]" in IPv6 host address in URI: "postgres://[::1"},
148 ],
149 [
150 q{postgres://[]},
151 q{},
152 q{libpq_uri_regress: IPv6 host address may not be empty in URI: "postgres://[]"},
153 ],
154 [
155 q{postgres://[::1]z},
156 q{},
157 q{libpq_uri_regress: unexpected character "z" at position 17 in URI (expected ":" or "/"): "postgres://[::1]z"},
158 ],
159 [
160 q{postgresql://host?zzz},
161 q{},
162 q{libpq_uri_regress: missing key/value separator "=" in URI query parameter: "zzz"},
163 ],
164 [
165 q{postgresql://host?value1&value2},
166 q{},
167 q{libpq_uri_regress: missing key/value separator "=" in URI query parameter: "value1"},
168 ],
169 [
170 q{postgresql://host?key=key=value},
171 q{},
172 q{libpq_uri_regress: extra key/value separator "=" in URI query parameter: "key"},
173 ],
174 [
175 q{postgres://host?dbname=%XXfoo}, q{},
176 q{libpq_uri_regress: invalid percent-encoded token: "%XXfoo"},
177 ],
178 [
179 q{postgresql://a%00b},
180 q{},
181 q{libpq_uri_regress: forbidden value %00 in percent-encoded value: "a%00b"},
182 ],
183 [
184 q{postgresql://%zz}, q{},
185 q{libpq_uri_regress: invalid percent-encoded token: "%zz"},
186 ],
187 [
188 q{postgresql://%1}, q{},
189 q{libpq_uri_regress: invalid percent-encoded token: "%1"},
190 ],
191 [
192 q{postgresql://%}, q{},
193 q{libpq_uri_regress: invalid percent-encoded token: "%"},
194 ],
195 [ q{postgres://@host}, q{host='host' (inet)}, q{}, ],
196 [ q{postgres://host:/}, q{host='host' (inet)}, q{}, ],
197 [ q{postgres://:12345/}, q{port='12345' (local)}, q{}, ],
198 [
199 q{postgres://otheruser@?host=/no/such/directory},
200 q{user='otheruser' host='/no/such/directory' (local)},
201 q{},
202 ],
203 [
204 q{postgres://otheruser@/?host=/no/such/directory},
205 q{user='otheruser' host='/no/such/directory' (local)},
206 q{},
207 ],
208 [
209 q{postgres://otheruser@:12345?host=/no/such/socket/path},
210 q{user='otheruser' host='/no/such/socket/path' port='12345' (local)},
211 q{},
212 ],
213 [
214 q{postgres://otheruser@:12345/db?host=/path/to/socket},
215 q{user='otheruser' dbname='db' host='/path/to/socket' port='12345' (local)},
216 q{},
217 ],
218 [
219 q{postgres://:12345/db?host=/path/to/socket},
220 q{dbname='db' host='/path/to/socket' port='12345' (local)},
221 q{},
222 ],
223 [
224 q{postgres://:12345?host=/path/to/socket},
225 q{host='/path/to/socket' port='12345' (local)},
226 q{},
227 ],
228 [
229 q{postgres://%2Fvar%2Flib%2Fpostgresql/dbname},
230 q{dbname='dbname' host='/var/lib/postgresql' (local)},
231 q{},
232 ],
233 # Usually the default sslmode is 'prefer' (for libraries with SSL) or
234 # 'disable' (for those without). This default changes to 'verify-full' if
235 # the system CA store is in use.
236 [
237 q{postgresql://host?sslmode=disable},
238 q{host='host' sslmode='disable' (inet)},
239 q{},
240 PGSSLROOTCERT => "system",
241 ],
242 [
243 q{postgresql://host?sslmode=prefer},
244 q{host='host' sslmode='prefer' (inet)},
245 q{},
246 PGSSLROOTCERT => "system",
247 ],
248 [
249 q{postgresql://host?sslmode=verify-full},
250 q{host='host' (inet)},
251 q{}, PGSSLROOTCERT => "system",
252 ]);
253
254 # test to run for each of the above test definitions
255 sub test_uri
256 {
257 local $Test::Builder::Level = $Test::Builder::Level + 1;
258 local %ENV = %ENV;
259
260 my $uri;
261 my %expect;
262 my %envvars;
263 my %result;
264
265 ($uri, $expect{stdout}, $expect{stderr}, %envvars) = @$_;
266
267 $expect{'exit'} = $expect{stderr} eq '';
268 %ENV = (%ENV, %envvars);
269
270 my $cmd = [ 'libpq_uri_regress', $uri ];
271 $result{exit} = IPC::Run::run $cmd,
272 '>' => \$result{stdout},
273 '2>' => \$result{stderr};
274
275 chomp($result{stdout});
276 chomp($result{stderr});
277
278 # use is_deeply so there's one test result for each test above, without
279 # losing the information whether stdout/stderr mismatched.
280 is_deeply(\%result, \%expect, $uri);
281 }
282
283 foreach (@tests)
284 {
285 test_uri($_);
286 }
287
288 done_testing();