Debian Codegraph @

src/bin/pg_verifybackup/t/005_bad_manifest.pl

raw
1
2 # Copyright (c) 2021-2025, PostgreSQL Global Development Group
3
4 # Test the behavior of pg_verifybackup when the backup manifest has
5 # problems.
6
7 use strict;
8 use warnings FATAL => 'all';
9 use PostgreSQL::Test::Cluster;
10 use PostgreSQL::Test::Utils;
11 use Test::More;
12
13 my $tempdir = PostgreSQL::Test::Utils::tempdir;
14
15 test_bad_manifest(
16 'input string ended unexpectedly',
17 qr/could not parse backup manifest: The input string ended unexpectedly/,
18 <<EOM);
19 {
20 EOM
21
22 test_parse_error('unexpected object end', <<EOM);
23 {}
24 EOM
25
26 test_parse_error('unexpected array start', <<EOM);
27 []
28 EOM
29
30 test_parse_error('expected version indicator', <<EOM);
31 {"not-expected": 1}
32 EOM
33
34 test_parse_error('manifest version not an integer', <<EOM);
35 {"PostgreSQL-Backup-Manifest-Version": "phooey"}
36 EOM
37
38 test_parse_error('unexpected manifest version', <<EOM);
39 {"PostgreSQL-Backup-Manifest-Version": 9876599}
40 EOM
41
42 test_parse_error('unexpected scalar', <<EOM);
43 {"PostgreSQL-Backup-Manifest-Version": 1, "Files": true}
44 EOM
45
46 test_parse_error('unrecognized top-level field', <<EOM);
47 {"PostgreSQL-Backup-Manifest-Version": 1, "Oops": 1}
48 EOM
49
50 test_parse_error('unexpected object start', <<EOM);
51 {"PostgreSQL-Backup-Manifest-Version": 1, "Files": {}}
52 EOM
53
54 test_parse_error('missing path name', <<EOM);
55 {"PostgreSQL-Backup-Manifest-Version": 1, "Files": [{}]}
56 EOM
57
58 test_parse_error('both path name and encoded path name', <<EOM);
59 {"PostgreSQL-Backup-Manifest-Version": 1, "Files": [
60 {"Path": "x", "Encoded-Path": "1234"}
61 ]}
62 EOM
63
64 test_parse_error('unexpected file field', <<EOM);
65 {"PostgreSQL-Backup-Manifest-Version": 1, "Files": [
66 {"Oops": 1}
67 ]}
68 EOM
69
70 test_parse_error('missing size', <<EOM);
71 {"PostgreSQL-Backup-Manifest-Version": 1, "Files": [
72 {"Path": "x"}
73 ]}
74 EOM
75
76 test_parse_error('file size is not an integer', <<EOM);
77 {"PostgreSQL-Backup-Manifest-Version": 1, "Files": [
78 {"Path": "x", "Size": "Oops"}
79 ]}
80 EOM
81
82 test_parse_error('could not decode file name', <<EOM);
83 {"PostgreSQL-Backup-Manifest-Version": 1, "Files": [
84 {"Encoded-Path": "123", "Size": 0}
85 ]}
86 EOM
87
88 test_fatal_error('duplicate path name in backup manifest', <<EOM);
89 {"PostgreSQL-Backup-Manifest-Version": 1, "Files": [
90 {"Path": "x", "Size": 0},
91 {"Path": "x", "Size": 0}
92 ]}
93 EOM
94
95 test_parse_error('checksum without algorithm', <<EOM);
96 {"PostgreSQL-Backup-Manifest-Version": 1, "Files": [
97 {"Path": "x", "Size": 100, "Checksum": "Oops"}
98 ]}
99 EOM
100
101 test_fatal_error('unrecognized checksum algorithm', <<EOM);
102 {"PostgreSQL-Backup-Manifest-Version": 1, "Files": [
103 {"Path": "x", "Size": 100, "Checksum-Algorithm": "Oops", "Checksum": "00"}
104 ]}
105 EOM
106
107 test_fatal_error('invalid checksum for file', <<EOM);
108 {"PostgreSQL-Backup-Manifest-Version": 1, "Files": [
109 {"Path": "x", "Size": 100, "Checksum-Algorithm": "CRC32C", "Checksum": "0"}
110 ]}
111 EOM
112
113 test_parse_error('missing start LSN', <<EOM);
114 {"PostgreSQL-Backup-Manifest-Version": 1, "WAL-Ranges": [
115 {"Timeline": 1}
116 ]}
117 EOM
118
119 test_parse_error('missing end LSN', <<EOM);
120 {"PostgreSQL-Backup-Manifest-Version": 1, "WAL-Ranges": [
121 {"Timeline": 1, "Start-LSN": "0/0"}
122 ]}
123 EOM
124
125 test_parse_error('unexpected WAL range field', <<EOM);
126 {"PostgreSQL-Backup-Manifest-Version": 1, "WAL-Ranges": [
127 {"Oops": 1}
128 ]}
129 EOM
130
131 test_parse_error('missing timeline', <<EOM);
132 {"PostgreSQL-Backup-Manifest-Version": 1, "WAL-Ranges": [
133 {}
134 ]}
135 EOM
136
137 test_parse_error('unexpected object end', <<EOM);
138 {"PostgreSQL-Backup-Manifest-Version": 1, "WAL-Ranges": [
139 {"Timeline": 1, "Start-LSN": "0/0", "End-LSN": "0/0"}
140 ]}
141 EOM
142
143 test_parse_error('timeline is not an integer', <<EOM);
144 {"PostgreSQL-Backup-Manifest-Version": 1, "WAL-Ranges": [
145 {"Timeline": true, "Start-LSN": "0/0", "End-LSN": "0/0"}
146 ]}
147 EOM
148
149 test_parse_error('could not parse start LSN', <<EOM);
150 {"PostgreSQL-Backup-Manifest-Version": 1, "WAL-Ranges": [
151 {"Timeline": 1, "Start-LSN": "oops", "End-LSN": "0/0"}
152 ]}
153 EOM
154
155 test_parse_error('could not parse end LSN', <<EOM);
156 {"PostgreSQL-Backup-Manifest-Version": 1, "WAL-Ranges": [
157 {"Timeline": 1, "Start-LSN": "0/0", "End-LSN": "oops"}
158 ]}
159 EOM
160
161 test_parse_error('expected at least 2 lines', <<EOM);
162 {"PostgreSQL-Backup-Manifest-Version": 1, "Files": [], "Manifest-Checksum": null}
163 EOM
164
165 my $manifest_without_newline = <<EOM;
166 {"PostgreSQL-Backup-Manifest-Version": 1,
167 "Files": [],
168 "Manifest-Checksum": null}
169 EOM
170 chomp($manifest_without_newline);
171 test_parse_error('last line not newline-terminated',
172 $manifest_without_newline);
173
174 test_fatal_error('invalid manifest checksum', <<EOM);
175 {"PostgreSQL-Backup-Manifest-Version": 1, "Files": [],
176 "Manifest-Checksum": "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890-"}
177 EOM
178
179 sub test_parse_error
180 {
181 local $Test::Builder::Level = $Test::Builder::Level + 1;
182
183 my ($test_name, $manifest_contents) = @_;
184
185 test_bad_manifest($test_name,
186 qr/could not parse backup manifest: $test_name/,
187 $manifest_contents);
188 return;
189 }
190
191 sub test_fatal_error
192 {
193 local $Test::Builder::Level = $Test::Builder::Level + 1;
194
195 my ($test_name, $manifest_contents) = @_;
196
197 test_bad_manifest($test_name, qr/error: $test_name/, $manifest_contents);
198 return;
199 }
200
201 sub test_bad_manifest
202 {
203 local $Test::Builder::Level = $Test::Builder::Level + 1;
204
205 my ($test_name, $regexp, $manifest_contents) = @_;
206
207 open(my $fh, '>', "$tempdir/backup_manifest") || die "open: $!";
208 print $fh $manifest_contents;
209 close($fh);
210
211 command_fails_like([ 'pg_verifybackup', $tempdir ], $regexp, $test_name);
212 return;
213 }
214
215 done_testing();