|
| 1 | +use strict; |
| 2 | +use warnings; |
| 3 | + |
| 4 | +use PostgresNode; |
| 5 | +use TestLib; |
| 6 | +use Test::More tests => 14; |
| 7 | + |
| 8 | +# In a SQL_ASCII database, pgwin32_message_to_UTF16() needs to |
| 9 | +# interpret everything as UTF8. We're going to use byte sequences |
| 10 | +# that aren't valid UTF-8 strings, so that would fail. Use LATIN1, |
| 11 | +# which accepts any byte and has a conversion from each byte to UTF-8. |
| 12 | +$ENV{LC_ALL} = 'C'; |
| 13 | +$ENV{PGCLIENTENCODING} = 'LATIN1'; |
| 14 | + |
| 15 | +# Create database and user names covering the range of LATIN1 |
| 16 | +# characters, for use in a connection string by pg_dumpall. Skip ',' |
| 17 | +# because of pg_regress --create-role, skip [\n\r] because pg_dumpall |
| 18 | +# does not allow them. |
| 19 | +my $dbname1 = generate_ascii_string(1, 9) . |
| 20 | + generate_ascii_string(11, 12) . |
| 21 | + generate_ascii_string(14, 33) . |
| 22 | + ($TestLib::windows_os ? '' : '"x"') . # IPC::Run mishandles '"' on Windows |
| 23 | + generate_ascii_string(35, 43) . |
| 24 | + generate_ascii_string(45, 63); # contains '=' |
| 25 | +my $dbname2 = generate_ascii_string(67, 129); # skip 64-66 to keep length to 62 |
| 26 | +my $dbname3 = generate_ascii_string(130, 192); |
| 27 | +my $dbname4 = generate_ascii_string(193, 255); |
| 28 | + |
| 29 | +my $node = get_new_node('main'); |
| 30 | +$node->init(extra => ['--locale=C', '--encoding=LATIN1']); |
| 31 | +# prep pg_hba.conf and pg_ident.conf |
| 32 | +$node->run_log([$ENV{PG_REGRESS}, '--config-auth', $node->data_dir, |
| 33 | + '--create-role', "$dbname1,$dbname2,$dbname3,$dbname4"]); |
| 34 | +$node->start; |
| 35 | + |
| 36 | +my $backupdir = $node->backup_dir; |
| 37 | +my $discard = "$backupdir/discard.sql"; |
| 38 | +my $plain = "$backupdir/plain.sql"; |
| 39 | +my $dirfmt = "$backupdir/dirfmt"; |
| 40 | + |
| 41 | +foreach my $dbname ($dbname1, $dbname2, $dbname3, $dbname4, 'CamelCase') |
| 42 | +{ |
| 43 | + $node->run_log(['createdb', $dbname]); |
| 44 | + $node->run_log(['createuser', '-s', $dbname]); |
| 45 | +} |
| 46 | + |
| 47 | + |
| 48 | +# For these tests, pg_dumpall -r is used because it produces a short |
| 49 | +# dump. |
| 50 | +$node->command_ok(['pg_dumpall', '-r', '-f', $discard, '--dbname', |
| 51 | + $node->connstr($dbname1), '-U', $dbname4], |
| 52 | + 'pg_dumpall with long ASCII name 1'); |
| 53 | +$node->command_ok(['pg_dumpall', '-r', '-f', $discard, '--dbname', |
| 54 | + $node->connstr($dbname2), '-U', $dbname3], |
| 55 | + 'pg_dumpall with long ASCII name 2'); |
| 56 | +$node->command_ok(['pg_dumpall', '-r', '-f', $discard, '--dbname', |
| 57 | + $node->connstr($dbname3), '-U', $dbname2], |
| 58 | + 'pg_dumpall with long ASCII name 3'); |
| 59 | +$node->command_ok(['pg_dumpall', '-r', '-f', $discard, '--dbname', |
| 60 | + $node->connstr($dbname4), '-U', $dbname1], |
| 61 | + 'pg_dumpall with long ASCII name 4'); |
| 62 | +$node->command_ok(['pg_dumpall', '-r', '-l', 'dbname=template1'], |
| 63 | + 'pg_dumpall -l accepts connection string'); |
| 64 | + |
| 65 | +$node->run_log(['createdb', "foo\n\rbar"]); |
| 66 | +# not sufficient to use -r here |
| 67 | +$node->command_fails(['pg_dumpall', '-f', $discard], |
| 68 | + 'pg_dumpall with \n\r in database name'); |
| 69 | +$node->run_log(['dropdb', "foo\n\rbar"]); |
| 70 | + |
| 71 | + |
| 72 | +# make a table, so the parallel worker has something to dump |
| 73 | +$node->safe_psql($dbname1, 'CREATE TABLE t0()'); |
| 74 | +# XXX no printed message when this fails, just SIGPIPE termination |
| 75 | +$node->command_ok(['pg_dump', '-Fd', '-j2', '-f', $dirfmt, |
| 76 | + '-U', $dbname1, $node->connstr($dbname1)], |
| 77 | + 'parallel dump'); |
| 78 | + |
| 79 | +# recreate $dbname1 for restore test |
| 80 | +$node->run_log(['dropdb', $dbname1]); |
| 81 | +$node->run_log(['createdb', $dbname1]); |
| 82 | + |
| 83 | +$node->command_ok(['pg_restore', '-v', '-d', 'template1', '-j2', |
| 84 | + '-U', $dbname1, $dirfmt], |
| 85 | + 'parallel restore'); |
| 86 | + |
| 87 | +$node->run_log(['dropdb', $dbname1]); |
| 88 | + |
| 89 | +$node->command_ok(['pg_restore', '-C', '-v', '-d', 'template1', '-j2', |
| 90 | + '-U', $dbname1, $dirfmt], |
| 91 | + 'parallel restore with create'); |
| 92 | + |
| 93 | + |
| 94 | +$node->command_ok(['pg_dumpall', '-f', $plain, '-U', $dbname1], |
| 95 | + 'take full dump'); |
| 96 | +system_log('cat', $plain); |
| 97 | +my($stderr, $result); |
| 98 | +my $bootstrap_super = 'boot'; |
| 99 | +my $restore_super = qq{a'b\\c=d\\ne"f}; |
| 100 | + |
| 101 | + |
| 102 | +# Restore full dump through psql using environment variables for |
| 103 | +# dbname/user connection parameters |
| 104 | + |
| 105 | +my $envar_node = get_new_node('destination_envar'); |
| 106 | +$envar_node->init(extra => ['-U', $bootstrap_super, |
| 107 | + '--locale=C', '--encoding=LATIN1']); |
| 108 | +$envar_node->run_log([$ENV{PG_REGRESS}, |
| 109 | + '--config-auth', $envar_node->data_dir, |
| 110 | + '--create-role', "$bootstrap_super,$restore_super"]); |
| 111 | +$envar_node->start; |
| 112 | + |
| 113 | +# make superuser for restore |
| 114 | +$envar_node->run_log(['createuser', '-U', $bootstrap_super, '-s', $restore_super]); |
| 115 | + |
| 116 | +{ |
| 117 | + local $ENV{PGPORT} = $envar_node->port; |
| 118 | + local $ENV{PGUSER} = $restore_super; |
| 119 | + $result = run_log(['psql', '-X', '-f', $plain], '2>', \$stderr); |
| 120 | +} |
| 121 | +ok($result, 'restore full dump using environment variables for connection parameters'); |
| 122 | +is($stderr, '', 'no dump errors'); |
| 123 | + |
| 124 | + |
| 125 | +# Restore full dump through psql using command-line options for |
| 126 | +# dbname/user connection parameters. "\connect dbname=" forgets |
| 127 | +# user/port from command line. |
| 128 | + |
| 129 | +$restore_super =~ s/"//g if $TestLib::windows_os; # IPC::Run mishandles '"' on Windows |
| 130 | +my $cmdline_node = get_new_node('destination_cmdline'); |
| 131 | +$cmdline_node->init(extra => ['-U', $bootstrap_super, |
| 132 | + '--locale=C', '--encoding=LATIN1']); |
| 133 | +$cmdline_node->run_log([$ENV{PG_REGRESS}, |
| 134 | + '--config-auth', $cmdline_node->data_dir, |
| 135 | + '--create-role', "$bootstrap_super,$restore_super"]); |
| 136 | +$cmdline_node->start; |
| 137 | +$cmdline_node->run_log(['createuser', '-U', $bootstrap_super, '-s', $restore_super]); |
| 138 | +{ |
| 139 | + $result = run_log(['psql', '-p', $cmdline_node->port, '-U', $restore_super, '-X', '-f', $plain], '2>', \$stderr); |
| 140 | +} |
| 141 | +ok($result, 'restore full dump with command-line options for connection parameters'); |
| 142 | +is($stderr, '', 'no dump errors'); |
0 commit comments