Debian Codegraph @

src/bin/pg_dump/t/010_dump_connstr.pl

raw
1
2 # Copyright (c) 2021-2025, PostgreSQL Global Development Group
3
4 use strict;
5 use warnings FATAL => 'all';
6
7 use PostgreSQL::Test::Cluster;
8 use PostgreSQL::Test::Utils;
9 use Test::More;
10
11 if ($PostgreSQL::Test::Utils::is_msys2)
12 {
13 plan skip_all => 'High bit name tests fail on Msys2';
14 }
15
16 # We're going to use byte sequences that aren't valid UTF-8 strings. Use
17 # LATIN1, which accepts any byte and has a conversion from each byte to UTF-8.
18 $ENV{LC_ALL} = 'C';
19 $ENV{PGCLIENTENCODING} = 'LATIN1';
20
21 # Create database and user names covering the range of LATIN1
22 # characters, for use in a connection string by pg_dumpall. Skip ','
23 # because of pg_regress --create-role, skip [\n\r] because pg_dumpall
24 # does not allow them. We also skip many ASCII letters, to keep the
25 # total number of tested characters to what will fit in four names.
26 # The odds of finding something interesting by testing all ASCII letters
27 # seem too small to justify the cycles of testing a fifth name.
28 my $dbname1 =
29 'regression'
30 . generate_ascii_string(1, 9)
31 . generate_ascii_string(11, 12)
32 . generate_ascii_string(14, 33)
33 . (
34 $PostgreSQL::Test::Utils::windows_os
35 ? ''
36 : '"x"') # IPC::Run mishandles '"' on Windows
37 . generate_ascii_string(35, 43) # skip ','
38 . generate_ascii_string(45, 54);
39 my $dbname2 = 'regression' . generate_ascii_string(55, 65) # skip 'B'-'W'
40 . generate_ascii_string(88, 99) # skip 'd'-'w'
41 . generate_ascii_string(120, 149);
42 my $dbname3 = 'regression' . generate_ascii_string(150, 202);
43 my $dbname4 = 'regression' . generate_ascii_string(203, 255);
44
45 (my $username1 = $dbname1) =~ s/^regression/regress_/;
46 (my $username2 = $dbname2) =~ s/^regression/regress_/;
47 (my $username3 = $dbname3) =~ s/^regression/regress_/;
48 (my $username4 = $dbname4) =~ s/^regression/regress_/;
49
50 my $src_bootstrap_super = 'regress_postgres';
51 my $dst_bootstrap_super = 'boot';
52
53 my $node = PostgreSQL::Test::Cluster->new('main');
54 $node->init(
55 extra => [
56 '--username' => $src_bootstrap_super,
57 '--locale' => 'C',
58 '--encoding' => 'LATIN1',
59 ]);
60
61 # prep pg_hba.conf and pg_ident.conf
62 $node->run_log(
63 [
64 $ENV{PG_REGRESS},
65 '--config-auth' => $node->data_dir,
66 '--user' => $src_bootstrap_super,
67 '--create-role' => "$username1,$username2,$username3,$username4",
68 ]);
69 $node->start;
70
71 my $backupdir = $node->backup_dir;
72 my $discard = "$backupdir/discard.sql";
73 my $plain = "$backupdir/plain.sql";
74 my $dirfmt = "$backupdir/dirfmt";
75
76 $node->run_log(
77 [ 'createdb', '--username' => $src_bootstrap_super, $dbname1 ]);
78 $node->run_log(
79 [
80 'createuser',
81 '--username' => $src_bootstrap_super,
82 '--superuser',
83 $username1,
84 ]);
85 $node->run_log(
86 [ 'createdb', '--username' => $src_bootstrap_super, $dbname2 ]);
87 $node->run_log(
88 [
89 'createuser',
90 '--username' => $src_bootstrap_super,
91 '--superuser',
92 $username2,
93 ]);
94 $node->run_log(
95 [ 'createdb', '--username' => $src_bootstrap_super, $dbname3 ]);
96 $node->run_log(
97 [
98 'createuser',
99 '--username' => $src_bootstrap_super,
100 '--superuser',
101 $username3,
102 ]);
103 $node->run_log(
104 [ 'createdb', '--username' => $src_bootstrap_super, $dbname4 ]);
105 $node->run_log(
106 [
107 'createuser',
108 '--username' => $src_bootstrap_super,
109 '--superuser',
110 $username4,
111 ]);
112
113
114 # For these tests, pg_dumpall --roles-only is used because it produces
115 # a short dump.
116 $node->command_ok(
117 [
118 'pg_dumpall', '--roles-only',
119 '--file' => $discard,
120 '--dbname' => $node->connstr($dbname1),
121 '--username' => $username4,
122 ],
123 'pg_dumpall with long ASCII name 1');
124 $node->command_ok(
125 [
126 'pg_dumpall', '--no-sync', '--roles-only',
127 '--file' => $discard,
128 '--dbname' => $node->connstr($dbname2),
129 '--username' => $username3,
130 ],
131 'pg_dumpall with long ASCII name 2');
132 $node->command_ok(
133 [
134 'pg_dumpall', '--no-sync', '--roles-only',
135 '--file' => $discard,
136 '--dbname' => $node->connstr($dbname3),
137 '--username' => $username2,
138 ],
139 'pg_dumpall with long ASCII name 3');
140 $node->command_ok(
141 [
142 'pg_dumpall', '--no-sync', '--roles-only',
143 '--file' => $discard,
144 '--dbname' => $node->connstr($dbname4),
145 '--username' => $username1,
146 ],
147 'pg_dumpall with long ASCII name 4');
148 $node->command_ok(
149 [
150 'pg_dumpall', '--no-sync', '--roles-only',
151 '--username' => $src_bootstrap_super,
152 '--dbname' => 'dbname=template1',
153 ],
154 'pg_dumpall --dbname accepts connection string');
155
156 $node->run_log(
157 [ 'createdb', '--username' => $src_bootstrap_super, "foo\n\rbar" ]);
158
159 # not sufficient to use --roles-only here
160 $node->command_fails(
161 [
162 'pg_dumpall', '--no-sync',
163 '--username' => $src_bootstrap_super,
164 '--file' => $discard,
165 ],
166 'pg_dumpall with \n\r in database name');
167 $node->run_log(
168 [ 'dropdb', '--username' => $src_bootstrap_super, "foo\n\rbar" ]);
169
170
171 # make a table, so the parallel worker has something to dump
172 $node->safe_psql(
173 $dbname1,
174 'CREATE TABLE t0()',
175 extra_params => [ '--username' => $src_bootstrap_super ]);
176
177 # XXX no printed message when this fails, just SIGPIPE termination
178 $node->command_ok(
179 [
180 'pg_dump',
181 '--format' => 'directory',
182 '--no-sync',
183 '--jobs' => 2,
184 '--file' => $dirfmt,
185 '--username' => $username1,
186 $node->connstr($dbname1),
187 ],
188 'parallel dump');
189
190 # recreate $dbname1 for restore test
191 $node->run_log([ 'dropdb', '--username' => $src_bootstrap_super, $dbname1 ]);
192 $node->run_log(
193 [ 'createdb', '--username' => $src_bootstrap_super, $dbname1 ]);
194
195 $node->command_ok(
196 [
197 'pg_restore',
198 '--verbose',
199 '--dbname' => 'template1',
200 '--jobs' => 2,
201 '--username' => $username1,
202 $dirfmt,
203 ],
204 'parallel restore');
205
206 $node->run_log([ 'dropdb', '--username' => $src_bootstrap_super, $dbname1 ]);
207
208 $node->command_ok(
209 [
210 'pg_restore',
211 '--create',
212 '--verbose',
213 '--dbname' => 'template1',
214 '--jobs' => 2,
215 '--username' => $username1,
216 $dirfmt,
217 ],
218 'parallel restore with create');
219
220
221 $node->command_ok(
222 [
223 'pg_dumpall',
224 '--no-sync',
225 '--file' => $plain,
226 '--username' => $username1,
227 ],
228 'take full dump');
229 system_log('cat', $plain);
230 my ($stderr, $result);
231 my $restore_super = qq{regress_a'b\\c=d\\ne"f};
232 $restore_super =~ s/"//g
233 if
234 $PostgreSQL::Test::Utils::windows_os; # IPC::Run mishandles '"' on Windows
235
236
237 # Restore full dump through psql using environment variables for
238 # dbname/user connection parameters
239
240 my $envar_node = PostgreSQL::Test::Cluster->new('destination_envar');
241 $envar_node->init(
242 extra => [
243 '--username' => $dst_bootstrap_super,
244 '--locale' => 'C',
245 '--encoding' => 'LATIN1',
246 ],
247 auth_extra =>
248 [ '--user' => $dst_bootstrap_super, '--create-role' => $restore_super ],
249 );
250 $envar_node->start;
251
252 # make superuser for restore
253 $envar_node->run_log(
254 [
255 'createuser',
256 '--username' => $dst_bootstrap_super,
257 '--superuser', $restore_super,
258 ]);
259
260 {
261 local $ENV{PGPORT} = $envar_node->port;
262 local $ENV{PGUSER} = $restore_super;
263 $result = run_log([ 'psql', '--no-psqlrc', '--file' => $plain ],
264 '2>' => \$stderr);
265 }
266 ok($result,
267 'restore full dump using environment variables for connection parameters'
268 );
269 is($stderr, '', 'no dump errors');
270
271
272 # Restore full dump through psql using command-line options for
273 # dbname/user connection parameters. "\connect dbname=" forgets
274 # user/port from command line.
275
276 my $cmdline_node = PostgreSQL::Test::Cluster->new('destination_cmdline');
277 $cmdline_node->init(
278 extra => [
279 '--username' => $dst_bootstrap_super,
280 '--locale' => 'C',
281 '--encoding' => 'LATIN1',
282 ],
283 auth_extra =>
284 [ '--user' => $dst_bootstrap_super, '--create-role' => $restore_super ],
285 );
286 $cmdline_node->start;
287 $cmdline_node->run_log(
288 [
289 'createuser',
290 '--username' => $dst_bootstrap_super,
291 '--superuser',
292 $restore_super,
293 ]);
294 {
295 $result = run_log(
296 [
297 'psql',
298 '--port' => $cmdline_node->port,
299 '--username' => $restore_super,
300 '--no-psqlrc',
301 '--file' => $plain,
302 ],
303 '2>' => \$stderr);
304 }
305 ok($result,
306 'restore full dump with command-line options for connection parameters');
307 is($stderr, '', 'no dump errors');
308
309 done_testing();