cat-file tests: add corrupt loose object test

Fix a blindspot in the tests for "cat-file" (and by proxy, the guts of object-file.c) by testing that when we can't decode a loose object with zlib we'll emit an error from zlib.c. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Oct 1, 2021 at 11:16 UTC 7e7d220d9d0f357388923d0fe4a7e3f898858adb
1 file changed +52
t/t1006-cat-file.sh
+52
@@ -426,6 +426,58 @@ test_expect_success "Size of large broken object is correct when type is large"
426 test_cmp expect actual
427 '
428
429 +test_expect_success 'cat-file -t and -s on corrupt loose object' '
430 + git init --bare corrupt-loose.git &&
431 + (
432 + cd corrupt-loose.git &&
433 +
434 + # Setup and create the empty blob and its path
435 + empty_path=$(git rev-parse --git-path objects/$(test_oid_to_path "$EMPTY_BLOB")) &&
436 + git hash-object -w --stdin </dev/null &&
437 +
438 + # Create another blob and its path
439 + echo other >other.blob &&
440 + other_blob=$(git hash-object -w --stdin <other.blob) &&
441 + other_path=$(git rev-parse --git-path objects/$(test_oid_to_path "$other_blob")) &&
442 +
443 + # Before the swap the size is 0
444 + cat >out.expect <<-EOF &&
445 + 0
446 + EOF
447 + git cat-file -s "$EMPTY_BLOB" >out.actual 2>err.actual &&
448 + test_must_be_empty err.actual &&
449 + test_cmp out.expect out.actual &&
450 +
451 + # Swap the two to corrupt the repository
452 + mv -f "$other_path" "$empty_path" &&
453 + test_must_fail git fsck 2>err.fsck &&
454 + grep "hash mismatch" err.fsck &&
455 +
456 + # confirm that cat-file is reading the new swapped-in
457 + # blob...
458 + cat >out.expect <<-EOF &&
459 + blob
460 + EOF
461 + git cat-file -t "$EMPTY_BLOB" >out.actual 2>err.actual &&
462 + test_must_be_empty err.actual &&
463 + test_cmp out.expect out.actual &&
464 +
465 + # ... since it has a different size now.
466 + cat >out.expect <<-EOF &&
467 + 6
468 + EOF
469 + git cat-file -s "$EMPTY_BLOB" >out.actual 2>err.actual &&
470 + test_must_be_empty err.actual &&
471 + test_cmp out.expect out.actual &&
472 +
473 + # So far "cat-file" has been happy to spew the found
474 + # content out as-is. Try to make it zlib-invalid.
475 + mv -f other.blob "$empty_path" &&
476 + test_must_fail git fsck 2>err.fsck &&
477 + grep "^error: inflate: data stream error (" err.fsck
478 + )
479 +'
480 +
481 # Tests for git cat-file --follow-symlinks
482 test_expect_success 'prep for symlink tests' '
483 echo_without_newline "$hello_content" >morx &&