-
-
Notifications
You must be signed in to change notification settings - Fork 829
Expand file tree
/
Copy pathcheck_cmd_test.py
More file actions
528 lines (431 loc) · 22.1 KB
/
check_cmd_test.py
File metadata and controls
528 lines (431 loc) · 22.1 KB
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
from datetime import datetime, timezone, timedelta
from pathlib import Path
import shutil
from unittest.mock import patch
import pytest
from ...archive import ChunkBuffer
from ...constants import * # NOQA
from ...helpers import bin_to_hex, msgpack
from ...manifest import Manifest
from ...remote import RemoteRepository
from ...repository import Repository
from ..repository_test import fchunk
from . import cmd, src_file, create_src_archive, open_archive, generate_archiver_tests, RK_ENCRYPTION
pytest_generate_tests = lambda metafunc: generate_archiver_tests(metafunc, kinds="local,remote,binary") # NOQA
def check_cmd_setup(archiver):
with patch.object(ChunkBuffer, "BUFFER_SIZE", 10):
cmd(archiver, "repo-create", RK_ENCRYPTION)
create_src_archive(archiver, "archive1")
create_src_archive(archiver, "archive2")
def test_check_usage(archivers, request):
archiver = request.getfixturevalue(archivers)
check_cmd_setup(archiver)
output = cmd(archiver, "check", "-v", "--progress", exit_code=0)
assert "Starting full repository check" in output
assert "Starting archive consistency check" in output
output = cmd(archiver, "check", "-v", "--repository-only", exit_code=0)
assert "Starting full repository check" in output
assert "Starting archive consistency check" not in output
output = cmd(archiver, "check", "-v", "--archives-only", exit_code=0)
assert "Starting full repository check" not in output
assert "Starting archive consistency check" in output
output = cmd(archiver, "check", "-v", "--archives-only", "--match-archives=archive2", exit_code=0)
assert "archive1" not in output
output = cmd(archiver, "check", "-v", "--archives-only", "--first=1", exit_code=0)
assert "archive1" in output
assert "archive2" not in output
output = cmd(archiver, "check", "-v", "--archives-only", "--last=1", exit_code=0)
assert "archive1" not in output
assert "archive2" in output
def test_date_matching(archivers, request):
archiver = request.getfixturevalue(archivers)
check_cmd_setup(archiver)
shutil.rmtree(archiver.repository_path)
cmd(archiver, "repo-create", RK_ENCRYPTION)
create_src_archive(archiver, "archive-2022-11-20", ts="2022-11-20T23:59:59")
create_src_archive(archiver, "archive-2022-12-18", ts="2022-12-18T23:59:59")
create_src_archive(archiver, "archive-now")
cmd(archiver, "check", "-v", "--archives-only", "--oldest=23e", exit_code=2)
output = cmd(archiver, "check", "-v", "--archives-only", "--oldest=1y", exit_code=0)
assert "archive-2022-11-20" in output
assert "archive-2022-12-18" in output
assert "archive-now" not in output
output = cmd(archiver, "check", "-v", "--archives-only", "--newest=1y", exit_code=0)
assert "archive-2022-11-20" not in output
assert "archive-2022-12-18" not in output
assert "archive-now" in output
output = cmd(archiver, "check", "-v", "--archives-only", "--oldest=1m", exit_code=0)
assert "archive-2022-11-20" in output
assert "archive-2022-12-18" in output
assert "archive-now" not in output
output = cmd(archiver, "check", "-v", "--archives-only", "--newest=1m", exit_code=0)
assert "archive-2022-11-20" not in output
assert "archive-2022-12-18" not in output
assert "archive-now" in output
output = cmd(archiver, "check", "-v", "--archives-only", "--oldest=4w", exit_code=0)
assert "archive-2022-11-20" in output
assert "archive-2022-12-18" in output
assert "archive-now" not in output
output = cmd(archiver, "check", "-v", "--archives-only", "--newest=4w", exit_code=0)
assert "archive-2022-11-20" not in output
assert "archive-2022-12-18" not in output
assert "archive-now" in output
output = cmd(archiver, "check", "-v", "--archives-only", "--newer=1d", exit_code=0)
assert "archive-2022-11-20" not in output
assert "archive-2022-12-18" not in output
assert "archive-now" in output
output = cmd(archiver, "check", "-v", "--archives-only", "--older=1d", exit_code=0)
assert "archive-2022-11-20" in output
assert "archive-2022-12-18" in output
assert "archive-now" not in output
output = cmd(archiver, "check", "-v", "--archives-only", "--newer=24H", exit_code=0)
assert "archive-2022-11-20" not in output
assert "archive-2022-12-18" not in output
assert "archive-now" in output
output = cmd(archiver, "check", "-v", "--archives-only", "--older=24H", exit_code=0)
assert "archive-2022-11-20" in output
assert "archive-2022-12-18" in output
assert "archive-now" not in output
output = cmd(archiver, "check", "-v", "--archives-only", "--newer=1440M", exit_code=0)
assert "archive-2022-11-20" not in output
assert "archive-2022-12-18" not in output
assert "archive-now" in output
output = cmd(archiver, "check", "-v", "--archives-only", "--older=1440M", exit_code=0)
assert "archive-2022-11-20" in output
assert "archive-2022-12-18" in output
assert "archive-now" not in output
output = cmd(archiver, "check", "-v", "--archives-only", "--newer=86400S", exit_code=0)
assert "archive-2022-11-20" not in output
assert "archive-2022-12-18" not in output
assert "archive-now" in output
output = cmd(archiver, "check", "-v", "--archives-only", "--older=86400S", exit_code=0)
assert "archive-2022-11-20" in output
assert "archive-2022-12-18" in output
assert "archive-now" not in output
# Check for output when a time span older than the earliest archive is given. Issue #1711
output = cmd(archiver, "check", "-v", "--archives-only", "--older=9999m", exit_code=0)
for archive in ("archive1", "archive2", "archive3"):
assert archive not in output
def test_missing_file_chunk(archivers, request):
archiver = request.getfixturevalue(archivers)
check_cmd_setup(archiver)
archive, repository = open_archive(archiver.repository_path, "archive1")
with repository:
for item in archive.iter_items():
if item.path.endswith(src_file):
valid_chunks = item.chunks
killed_chunk = valid_chunks[-1]
repository.delete(killed_chunk.id)
break
else:
pytest.fail("should not happen") # convert 'fail'
output = cmd(archiver, "check", exit_code=1)
assert "Missing file chunk detected" in output
output = cmd(archiver, "check", "--repair", exit_code=0)
assert "Missing file chunk detected" in output # repair is not changing anything, just reporting.
# check does not modify the chunks list.
for archive_name in ("archive1", "archive2"):
archive, repository = open_archive(archiver.repository_path, archive_name)
with repository:
for item in archive.iter_items():
if item.path.endswith(src_file):
assert len(valid_chunks) == len(item.chunks)
assert valid_chunks == item.chunks
break
else:
pytest.fail("should not happen") # convert 'fail'
# do a fresh backup (that will include the killed chunk)
with patch.object(ChunkBuffer, "BUFFER_SIZE", 10):
create_src_archive(archiver, "archive3")
# check should not complain anymore about missing chunks:
output = cmd(archiver, "check", "-v", "--repair", exit_code=0)
assert "Missing file chunk detected" not in output
def test_missing_archive_item_chunk(archivers, request):
archiver = request.getfixturevalue(archivers)
check_cmd_setup(archiver)
archive, repository = open_archive(archiver.repository_path, "archive1")
with repository:
repository.delete(archive.metadata.items[0])
cmd(archiver, "check", exit_code=1)
cmd(archiver, "check", "--repair", exit_code=0)
cmd(archiver, "check", exit_code=0)
def test_missing_archive_metadata(archivers, request):
archiver = request.getfixturevalue(archivers)
check_cmd_setup(archiver)
archive, repository = open_archive(archiver.repository_path, "archive1")
with repository:
repository.delete(archive.id)
cmd(archiver, "check", exit_code=1)
cmd(archiver, "check", "--repair", exit_code=0)
cmd(archiver, "check", exit_code=0)
def test_missing_manifest(archivers, request):
archiver = request.getfixturevalue(archivers)
check_cmd_setup(archiver)
archive, repository = open_archive(archiver.repository_path, "archive1")
with repository:
if isinstance(repository, (Repository, RemoteRepository)):
repository.store_delete("config/manifest")
else:
repository.delete(Manifest.MANIFEST_ID)
cmd(archiver, "check", exit_code=1)
output = cmd(archiver, "check", "-v", "--repair", exit_code=0)
assert "archive1" in output
assert "archive2" in output
cmd(archiver, "check", exit_code=0)
def test_corrupted_manifest(archivers, request):
archiver = request.getfixturevalue(archivers)
check_cmd_setup(archiver)
archive, repository = open_archive(archiver.repository_path, "archive1")
with repository:
manifest = repository.get_manifest()
corrupted_manifest = manifest[:123] + b"corrupted!" + manifest[123:]
repository.put_manifest(corrupted_manifest)
cmd(archiver, "check", exit_code=1)
output = cmd(archiver, "check", "-v", "--repair", exit_code=0)
assert "archive1" in output
assert "archive2" in output
cmd(archiver, "check", exit_code=0)
def test_spoofed_manifest(archivers, request):
archiver = request.getfixturevalue(archivers)
check_cmd_setup(archiver)
archive, repository = open_archive(archiver.repository_path, "archive1")
with repository:
manifest = Manifest.load(repository, Manifest.NO_OPERATION_CHECK)
cdata = manifest.repo_objs.format(
Manifest.MANIFEST_ID,
{},
msgpack.packb(
{
"version": 1,
"archives": {},
"config": {},
"timestamp": (datetime.now(tz=timezone.utc) + timedelta(days=1)).isoformat(timespec="microseconds"),
}
),
# we assume that an attacker can put a file into backup src files that contains a fake manifest.
# but, the attacker can not influence the ro_type borg will use to store user file data:
ro_type=ROBJ_FILE_STREAM, # a real manifest is stored with ROBJ_MANIFEST
)
# maybe a repo-side attacker could manage to move the fake manifest file chunk over to the manifest ID.
# we simulate this here by directly writing the fake manifest data to the manifest ID.
repository.put_manifest(cdata)
# borg should notice that the manifest has the wrong ro_type.
cmd(archiver, "check", exit_code=1)
# borg check --repair should remove the corrupted manifest and rebuild a new one.
output = cmd(archiver, "check", "-v", "--repair", exit_code=0)
assert "archive1" in output
assert "archive2" in output
cmd(archiver, "check", exit_code=0)
def test_manifest_rebuild_corrupted_chunk(archivers, request):
archiver = request.getfixturevalue(archivers)
check_cmd_setup(archiver)
archive, repository = open_archive(archiver.repository_path, "archive1")
with repository:
manifest = repository.get_manifest()
corrupted_manifest = manifest[:123] + b"corrupted!" + manifest[123:]
repository.put_manifest(corrupted_manifest)
chunk = repository.get(archive.id)
corrupted_chunk = chunk + b"corrupted!"
repository.put(archive.id, corrupted_chunk)
cmd(archiver, "check", exit_code=1)
output = cmd(archiver, "check", "-v", "--repair", exit_code=0)
assert "archive2" in output
cmd(archiver, "check", exit_code=0)
def test_check_undelete_archives(archivers, request):
archiver = request.getfixturevalue(archivers)
check_cmd_setup(archiver) # creates archive1 and archive2
existing_archive_ids = set(cmd(archiver, "repo-list", "--short").splitlines())
create_src_archive(archiver, "archive3")
archive_ids = set(cmd(archiver, "repo-list", "--short").splitlines())
new_archive_id_hex = (archive_ids - existing_archive_ids).pop()
(Path(archiver.repository_path) / "archives" / new_archive_id_hex).unlink() # lose the entry for archive3
output = cmd(archiver, "repo-list")
assert "archive1" in output
assert "archive2" in output
assert "archive3" not in output
# borg check will re-discover archive3 and create a new archives directory entry.
cmd(archiver, "check", "--repair", "--find-lost-archives", exit_code=0)
output = cmd(archiver, "repo-list")
assert "archive1" in output
assert "archive2" in output
assert "archive3" in output
def test_spoofed_archive(archivers, request):
archiver = request.getfixturevalue(archivers)
check_cmd_setup(archiver)
archive, repository = open_archive(archiver.repository_path, "archive1")
repo_objs = archive.repo_objs
with repository:
# attacker would corrupt or delete the manifest to trigger a rebuild of it:
manifest = repository.get_manifest()
corrupted_manifest = manifest[:123] + b"corrupted!" + manifest[123:]
repository.put_manifest(corrupted_manifest)
archive_dict = {
"command_line": "",
"item_ptrs": [],
"hostname": "foo",
"username": "bar",
"name": "archive_spoofed",
"time": "2016-12-15T18:49:51.849711",
"version": 2,
}
archive = repo_objs.key.pack_metadata(archive_dict)
archive_id = repo_objs.id_hash(archive)
repository.put(
archive_id,
repo_objs.format(
archive_id,
{},
archive,
# we assume that an attacker can put a file into backup src files that contains a fake archive.
# but, the attacker can not influence the ro_type borg will use to store user file data:
ro_type=ROBJ_FILE_STREAM, # a real archive is stored with ROBJ_ARCHIVE_META
),
)
cmd(archiver, "check", exit_code=1)
cmd(archiver, "check", "--repair", "--debug", exit_code=0)
output = cmd(archiver, "repo-list")
assert "archive1" in output
assert "archive2" in output
assert "archive_spoofed" not in output
def test_extra_chunks(archivers, request):
archiver = request.getfixturevalue(archivers)
if archiver.get_kind() == "remote":
pytest.skip("only works locally")
check_cmd_setup(archiver)
cmd(archiver, "check", exit_code=0)
with Repository(archiver.repository_location, exclusive=True) as repository:
chunk = fchunk(b"xxxx")
repository.put(b"01234567890123456789012345678901", chunk)
cmd(archiver, "check", "-v", exit_code=0) # check does not deal with orphans anymore
@pytest.mark.parametrize("init_args", [["--encryption=repokey-aes-ocb"], ["--encryption", "none"]])
def test_verify_data(archivers, request, init_args):
archiver = request.getfixturevalue(archivers)
if archiver.get_kind() != "local":
pytest.skip("only works locally, patches objects")
# it's tricky to test the cryptographic data verification, because usually already the
# repository-level xxh64 hash fails to verify. So we use a fake one that doesn't.
# note: it only works like tested here for a highly engineered data corruption attack,
# because with accidental corruption, usually already the xxh64 low-level check fails.
def fake_xxh64(data, seed=0):
return b"fakefake"
import borg.repoobj
import borg.repository
with patch.object(borg.repoobj, "xxh64", fake_xxh64), patch.object(borg.repository, "xxh64", fake_xxh64):
check_cmd_setup(archiver)
shutil.rmtree(archiver.repository_path)
cmd(archiver, "repo-create", *init_args)
create_src_archive(archiver, "archive1")
archive, repository = open_archive(archiver.repository_path, "archive1")
with repository:
for item in archive.iter_items():
if item.path.endswith(src_file):
chunk = item.chunks[-1]
data = repository.get(chunk.id)
data = data[0:123] + b"x" + data[123:]
repository.put(chunk.id, data)
break
# the normal archives check does not read file content data.
cmd(archiver, "check", "--archives-only", exit_code=0)
# but with --verify-data, it does and notices the issue.
output = cmd(archiver, "check", "--archives-only", "--verify-data", exit_code=1)
assert f"{bin_to_hex(chunk.id)}, integrity error" in output
# repair will find the defect chunk and remove it
output = cmd(archiver, "check", "--repair", "--verify-data", exit_code=0)
assert f"{bin_to_hex(chunk.id)}, integrity error" in output
assert f"{src_file}: Missing file chunk detected" in output
# run with --verify-data again, it will notice the missing chunk.
output = cmd(archiver, "check", "--archives-only", "--verify-data", exit_code=1)
assert f"{src_file}: Missing file chunk detected" in output
@pytest.mark.parametrize("init_args", [["--encryption=repokey-aes-ocb"], ["--encryption", "none"]])
def test_corrupted_file_chunk(archivers, request, init_args):
## similar to test_verify_data, but here we let the low level repository-only checks discover the issue.
archiver = request.getfixturevalue(archivers)
check_cmd_setup(archiver)
shutil.rmtree(archiver.repository_path)
cmd(archiver, "repo-create", *init_args)
create_src_archive(archiver, "archive1")
archive, repository = open_archive(archiver.repository_path, "archive1")
with repository:
for item in archive.iter_items():
if item.path.endswith(src_file):
chunk = item.chunks[-1]
data = repository.get(chunk.id)
data = data[0:123] + b"x" + data[123:]
repository.put(chunk.id, data)
break
# the normal check checks all repository objects and the xxh64 checksum fails.
output = cmd(archiver, "check", "--repository-only", exit_code=1)
assert f"{bin_to_hex(chunk.id)} is corrupted: data does not match checksum." in output
# repair: the defect chunk will be removed by repair.
output = cmd(archiver, "check", "--repair", exit_code=0)
assert f"{bin_to_hex(chunk.id)} is corrupted: data does not match checksum." in output
assert f"{src_file}: Missing file chunk detected" in output
# run normal check again
cmd(archiver, "check", "--repository-only", exit_code=0)
output = cmd(archiver, "check", "--archives-only", exit_code=1)
assert f"{src_file}: Missing file chunk detected" in output
def test_empty_repository(archivers, request):
archiver = request.getfixturevalue(archivers)
if archiver.get_kind() == "remote":
pytest.skip("only works locally")
check_cmd_setup(archiver)
with Repository(archiver.repository_location, exclusive=True) as repository:
for id, _ in repository.list():
repository.delete(id)
cmd(archiver, "check", exit_code=1)
def test_check_repair_user_cancellation(archivers, request, monkeypatch):
"""Test that check --repair is cancelled if user doesn't confirm with YES."""
from ...helpers import CancelledByUser
archiver = request.getfixturevalue(archivers)
check_cmd_setup(archiver)
# Simulate user entering "no" instead of "YES"
monkeypatch.setenv("BORG_CHECK_I_KNOW_WHAT_I_AM_DOING", "no")
with pytest.raises(CancelledByUser):
cmd(archiver, "check", "--repair")
def test_check_repository_only_conflicts(archivers, request):
"""Test that --repository-only conflicts with archive-related options."""
from ...helpers import CommandError
archiver = request.getfixturevalue(archivers)
check_cmd_setup(archiver)
# --repository-only conflicts with --verify-data
with pytest.raises(CommandError) as exc_info:
cmd(archiver, "check", "--repository-only", "--verify-data")
assert "contradicts" in str(exc_info.value)
# --repository-only conflicts with --match-archives
with pytest.raises(CommandError) as exc_info:
cmd(archiver, "check", "--repository-only", "--match-archives=*")
assert "contradicts" in str(exc_info.value)
# --repository-only conflicts with --first
with pytest.raises(CommandError) as exc_info:
cmd(archiver, "check", "--repository-only", "--first=1")
assert "contradicts" in str(exc_info.value)
# --repository-only conflicts with --last
with pytest.raises(CommandError) as exc_info:
cmd(archiver, "check", "--repository-only", "--last=1")
assert "contradicts" in str(exc_info.value)
def test_check_repository_only_find_lost_archives_conflict(archivers, request):
"""Test that --repository-only conflicts with --find-lost-archives."""
from ...helpers import CommandError
archiver = request.getfixturevalue(archivers)
check_cmd_setup(archiver)
with pytest.raises(CommandError) as exc_info:
cmd(archiver, "check", "--repository-only", "--find-lost-archives")
assert "contradicts" in str(exc_info.value)
assert "--find-lost-archives" in str(exc_info.value)
def test_check_repair_max_duration_conflict(archivers, request):
"""Test that --repair conflicts with --max-duration."""
from ...helpers import CommandError
archiver = request.getfixturevalue(archivers)
check_cmd_setup(archiver)
with pytest.raises(CommandError) as exc_info:
cmd(archiver, "check", "--repair", "--max-duration=60")
assert "--repair does not allow --max-duration" in str(exc_info.value)
def test_check_max_duration_requires_repository_only(archivers, request):
"""Test that --max-duration requires --repository-only."""
from ...helpers import CommandError
archiver = request.getfixturevalue(archivers)
check_cmd_setup(archiver)
# --max-duration without --repository-only should fail
with pytest.raises(CommandError) as exc_info:
cmd(archiver, "check", "--max-duration=60")
assert "--repository-only is required for --max-duration" in str(exc_info.value)