]> git.ipfire.org Git - thirdparty/git.git/commitdiff
cat-file tests: add corrupt loose object test
authorÆvar Arnfjörð Bjarmason <avarab@gmail.com>
Fri, 1 Oct 2021 09:16:43 +0000 (11:16 +0200)
committerJunio C Hamano <gitster@pobox.com>
Fri, 1 Oct 2021 22:05:59 +0000 (15:05 -0700)
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>
t/t1006-cat-file.sh

index dee3582fc1adb261820f545b9a7632f8a376b9e8..2e83c8cacfba092f1e47a7cc072417f41a239813 100755 (executable)
@@ -426,6 +426,58 @@ test_expect_success "Size of large broken object is correct when type is large"
        test_cmp expect actual
 '
 
+test_expect_success 'cat-file -t and -s on corrupt loose object' '
+       git init --bare corrupt-loose.git &&
+       (
+               cd corrupt-loose.git &&
+
+               # Setup and create the empty blob and its path
+               empty_path=$(git rev-parse --git-path objects/$(test_oid_to_path "$EMPTY_BLOB")) &&
+               git hash-object -w --stdin </dev/null &&
+
+               # Create another blob and its path
+               echo other >other.blob &&
+               other_blob=$(git hash-object -w --stdin <other.blob) &&
+               other_path=$(git rev-parse --git-path objects/$(test_oid_to_path "$other_blob")) &&
+
+               # Before the swap the size is 0
+               cat >out.expect <<-EOF &&
+               0
+               EOF
+               git cat-file -s "$EMPTY_BLOB" >out.actual 2>err.actual &&
+               test_must_be_empty err.actual &&
+               test_cmp out.expect out.actual &&
+
+               # Swap the two to corrupt the repository
+               mv -f "$other_path" "$empty_path" &&
+               test_must_fail git fsck 2>err.fsck &&
+               grep "hash mismatch" err.fsck &&
+
+               # confirm that cat-file is reading the new swapped-in
+               # blob...
+               cat >out.expect <<-EOF &&
+               blob
+               EOF
+               git cat-file -t "$EMPTY_BLOB" >out.actual 2>err.actual &&
+               test_must_be_empty err.actual &&
+               test_cmp out.expect out.actual &&
+
+               # ... since it has a different size now.
+               cat >out.expect <<-EOF &&
+               6
+               EOF
+               git cat-file -s "$EMPTY_BLOB" >out.actual 2>err.actual &&
+               test_must_be_empty err.actual &&
+               test_cmp out.expect out.actual &&
+
+               # So far "cat-file" has been happy to spew the found
+               # content out as-is. Try to make it zlib-invalid.
+               mv -f other.blob "$empty_path" &&
+               test_must_fail git fsck 2>err.fsck &&
+               grep "^error: inflate: data stream error (" err.fsck
+       )
+'
+
 # Tests for git cat-file --follow-symlinks
 test_expect_success 'prep for symlink tests' '
        echo_without_newline "$hello_content" >morx &&