aboutsummaryrefslogtreecommitdiff
path: root/src/interfaces/libpq/t/001_uri.pl
blob: ee4944ed18ff18f110d9a4416d9496d962a4c853 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# Copyright (c) 2021-2025, PostgreSQL Global Development Group
use strict;
use warnings FATAL => 'all';

use PostgreSQL::Test::Utils;
use Test::More;
use IPC::Run;


# List of URIs tests. For each test the first element is the input string, the
# second the expected stdout and the third the expected stderr. Optionally,
# additional arguments may specify key/value pairs which will override
# environment variables for the duration of the test.
my @tests = (
	[
		q{postgresql://uri-user:secret@host:12345/db},
		q{user='uri-user' password='secret' dbname='db' host='host' port='12345' (inet)},
		q{},
	],
	[
		q{postgresql://uri-user@host:12345/db},
		q{user='uri-user' dbname='db' host='host' port='12345' (inet)}, q{},
	],
	[
		q{postgresql://uri-user@host/db},
		q{user='uri-user' dbname='db' host='host' (inet)}, q{},
	],
	[
		q{postgresql://host:12345/db},
		q{dbname='db' host='host' port='12345' (inet)}, q{},
	],
	[ q{postgresql://host/db}, q{dbname='db' host='host' (inet)}, q{}, ],
	[
		q{postgresql://uri-user@host:12345/},
		q{user='uri-user' host='host' port='12345' (inet)},
		q{},
	],
	[
		q{postgresql://uri-user@host/},
		q{user='uri-user' host='host' (inet)},
		q{},
	],
	[ q{postgresql://uri-user@}, q{user='uri-user' (local)}, q{}, ],
	[ q{postgresql://host:12345/}, q{host='host' port='12345' (inet)}, q{}, ],
	[ q{postgresql://host:12345}, q{host='host' port='12345' (inet)}, q{}, ],
	[ q{postgresql://host/db}, q{dbname='db' host='host' (inet)}, q{}, ],
	[ q{postgresql://host/}, q{host='host' (inet)}, q{}, ],
	[ q{postgresql://host}, q{host='host' (inet)}, q{}, ],
	[ q{postgresql://}, q{(local)}, q{}, ],
	[
		q{postgresql://?hostaddr=127.0.0.1}, q{hostaddr='127.0.0.1' (inet)},
		q{},
	],
	[
		q{postgresql://example.com?hostaddr=63.1.2.4},
		q{host='example.com' hostaddr='63.1.2.4' (inet)},
		q{},
	],
	[ q{postgresql://%68ost/}, q{host='host' (inet)}, q{}, ],
	[
		q{postgresql://host/db?user=uri-user},
		q{user='uri-user' dbname='db' host='host' (inet)},
		q{},
	],
	[
		q{postgresql://host/db?user=uri-user&port=12345},
		q{user='uri-user' dbname='db' host='host' port='12345' (inet)},
		q{},
	],
	[
		q{postgresql://host/db?u%73er=someotheruser&port=12345},
		q{user='someotheruser' dbname='db' host='host' port='12345' (inet)},
		q{},
	],
	[
		q{postgresql://host/db?u%7aer=someotheruser&port=12345}, q{},
		q{libpq_uri_regress: invalid URI query parameter: "uzer"},
	],
	[
		q{postgresql://host:12345?user=uri-user},
		q{user='uri-user' host='host' port='12345' (inet)},
		q{},
	],
	[
		q{postgresql://host?user=uri-user},
		q{user='uri-user' host='host' (inet)},
		q{},
	],
	[
		# Leading and trailing spaces, works.
		q{postgresql://host?  user = uri-user & port  = 12345 },
		q{user='uri-user' host='host' port='12345' (inet)},
		q{},
	],
	[
		# Trailing data in parameter.
		q{postgresql://host?  user user  =  uri  & port = 12345 12 },
		q{},
		q{libpq_uri_regress: unexpected spaces found in "  user user  ", use percent-encoded spaces (%20) instead},
	],
	[
		# Trailing data in value.
		q{postgresql://host?  user  =  uri-user  & port = 12345 12 },
		q{},
		q{libpq_uri_regress: unexpected spaces found in " 12345 12 ", use percent-encoded spaces (%20) instead},
	],
	[ q{postgresql://host?}, q{host='host' (inet)}, q{}, ],
	[
		q{postgresql://[::1]:12345/db},
		q{dbname='db' host='::1' port='12345' (inet)},
		q{},
	],
	[ q{postgresql://[::1]/db}, q{dbname='db' host='::1' (inet)}, q{}, ],
	[
		q{postgresql://[2001:db8::1234]/}, q{host='2001:db8::1234' (inet)},
		q{},
	],
	[
		q{postgresql://[200z:db8::1234]/}, q{host='200z:db8::1234' (inet)},
		q{},
	],
	[ q{postgresql://[::1]}, q{host='::1' (inet)}, q{}, ],
	[ q{postgres://}, q{(local)}, q{}, ],
	[ q{postgres:///}, q{(local)}, q{}, ],
	[ q{postgres:///db}, q{dbname='db' (local)}, q{}, ],
	[
		q{postgres://uri-user@/db}, q{user='uri-user' dbname='db' (local)},
		q{},
	],
	[
		q{postgres://?host=/path/to/socket/dir},
		q{host='/path/to/socket/dir' (local)},
		q{},
	],
	[
		q{postgresql://host?uzer=}, q{},
		q{libpq_uri_regress: invalid URI query parameter: "uzer"},
	],
	[
		q{postgre://},
		q{},
		q{libpq_uri_regress: missing "=" after "postgre://" in connection info string},
	],
	[
		q{postgres://[::1},
		q{},
		q{libpq_uri_regress: end of string reached when looking for matching "]" in IPv6 host address in URI: "postgres://[::1"},
	],
	[
		q{postgres://[]},
		q{},
		q{libpq_uri_regress: IPv6 host address may not be empty in URI: "postgres://[]"},
	],
	[
		q{postgres://[::1]z},
		q{},
		q{libpq_uri_regress: unexpected character "z" at position 17 in URI (expected ":" or "/"): "postgres://[::1]z"},
	],
	[
		q{postgresql://host?zzz},
		q{},
		q{libpq_uri_regress: missing key/value separator "=" in URI query parameter: "zzz"},
	],
	[
		q{postgresql://host?value1&value2},
		q{},
		q{libpq_uri_regress: missing key/value separator "=" in URI query parameter: "value1"},
	],
	[
		q{postgresql://host?key=key=value},
		q{},
		q{libpq_uri_regress: extra key/value separator "=" in URI query parameter: "key"},
	],
	[
		q{postgres://host?dbname=%XXfoo}, q{},
		q{libpq_uri_regress: invalid percent-encoded token: "%XXfoo"},
	],
	[
		q{postgresql://a%00b},
		q{},
		q{libpq_uri_regress: forbidden value %00 in percent-encoded value: "a%00b"},
	],
	[
		q{postgresql://%zz}, q{},
		q{libpq_uri_regress: invalid percent-encoded token: "%zz"},
	],
	[
		q{postgresql://%1}, q{},
		q{libpq_uri_regress: invalid percent-encoded token: "%1"},
	],
	[
		q{postgresql://%}, q{},
		q{libpq_uri_regress: invalid percent-encoded token: "%"},
	],
	[ q{postgres://@host}, q{host='host' (inet)}, q{}, ],
	[ q{postgres://host:/}, q{host='host' (inet)}, q{}, ],
	[ q{postgres://:12345/}, q{port='12345' (local)}, q{}, ],
	[
		q{postgres://otheruser@?host=/no/such/directory},
		q{user='otheruser' host='/no/such/directory' (local)},
		q{},
	],
	[
		q{postgres://otheruser@/?host=/no/such/directory},
		q{user='otheruser' host='/no/such/directory' (local)},
		q{},
	],
	[
		q{postgres://otheruser@:12345?host=/no/such/socket/path},
		q{user='otheruser' host='/no/such/socket/path' port='12345' (local)},
		q{},
	],
	[
		q{postgres://otheruser@:12345/db?host=/path/to/socket},
		q{user='otheruser' dbname='db' host='/path/to/socket' port='12345' (local)},
		q{},
	],
	[
		q{postgres://:12345/db?host=/path/to/socket},
		q{dbname='db' host='/path/to/socket' port='12345' (local)},
		q{},
	],
	[
		q{postgres://:12345?host=/path/to/socket},
		q{host='/path/to/socket' port='12345' (local)},
		q{},
	],
	[
		q{postgres://%2Fvar%2Flib%2Fpostgresql/dbname},
		q{dbname='dbname' host='/var/lib/postgresql' (local)},
		q{},
	],
	# Usually the default sslmode is 'prefer' (for libraries with SSL) or
	# 'disable' (for those without). This default changes to 'verify-full' if
	# the system CA store is in use.
	[
		q{postgresql://host?sslmode=disable},
		q{host='host' sslmode='disable' (inet)},
		q{},
		PGSSLROOTCERT => "system",
	],
	[
		q{postgresql://host?sslmode=prefer},
		q{host='host' sslmode='prefer' (inet)},
		q{},
		PGSSLROOTCERT => "system",
	],
	[
		q{postgresql://host?sslmode=verify-full},
		q{host='host' (inet)},
		q{}, PGSSLROOTCERT => "system",
	]);

# test to run for each of the above test definitions
sub test_uri
{
	local $Test::Builder::Level = $Test::Builder::Level + 1;
	local %ENV = %ENV;

	my $uri;
	my %expect;
	my %envvars;
	my %result;

	($uri, $expect{stdout}, $expect{stderr}, %envvars) = @$_;

	$expect{'exit'} = $expect{stderr} eq '';
	%ENV = (%ENV, %envvars);

	my $cmd = [ 'libpq_uri_regress', $uri ];
	$result{exit} = IPC::Run::run $cmd, '>', \$result{stdout}, '2>',
	  \$result{stderr};

	chomp($result{stdout});
	chomp($result{stderr});

	# use is_deeply so there's one test result for each test above, without
	# losing the information whether stdout/stderr mismatched.
	is_deeply(\%result, \%expect, $uri);
}

foreach (@tests)
{
	test_uri($_);
}

done_testing();