object-name tests: add tests for ambiguous object blind spots

Extend the tests for ambiguous objects to check how we handle objects where we return OBJ_BAD when trying to parse them. As noted in [1] we have a blindspot when it comes to this behavior. Since we need to add new test data here let's extend these tests to be tested under SHA-256, in d7a2fc82491 (t1512: skip test if not using SHA-1, 2018-05-13) all of the existing tests were skipped, as they rely on specific SHA-1 object IDs. For these tests it only matters that the first 4 characters of the OID prefix are the same for both SHA-1 and SHA-256. This uses strings that I mined, and have the same prefix when hashed with both. We "test_cmp" the full output to guard against any future regressions, and because a subsequent commit will tweak it. Showing a diff of how the output changes is helpful to explain those subsequent commits. The "sed" invocation in test_cmp_failed_rev_parse() doesn't need a "/g" because under both SHA-1 and SHA-256 we'll wildcard match any trailing part of the OID after our known starting prefix. We'd like to convert all of that to just "..." for the "test_cmp" which follows. 1. https://lore.kernel.org/git/YZwbphPpfGk78w2f@coredump.intra.peff.net/ Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Jan 27, 2022 at 06:26 UTC 8d56136d038a611ee69cfd6531a5b86028ef147b
1 file changed +82
t/t1512-rev-parse-disambiguation.sh
+82
@@ -25,6 +25,88 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
25
26 . ./test-lib.sh
27
28 +test_cmp_failed_rev_parse () {
29 + dir=$1
30 + rev=$2
31 +
32 + cat >expect &&
33 + test_must_fail git -C "$dir" rev-parse "$rev" 2>actual.raw &&
34 + sed "s/\($rev\)[0-9a-f]*/\1.../" <actual.raw >actual &&
35 + test_cmp expect actual
36 +}
37 +
38 +test_expect_success 'ambiguous blob output' '
39 + git init --bare blob.prefix &&
40 + (
41 + cd blob.prefix &&
42 +
43 + # Both start with "dead..", under both SHA-1 and SHA-256
44 + echo brocdnra | git hash-object -w --stdin &&
45 + echo brigddsv | git hash-object -w --stdin &&
46 +
47 + # Both start with "beef.."
48 + echo 1agllotbh | git hash-object -w --stdin &&
49 + echo 1bbfctrkc | git hash-object -w --stdin
50 + ) &&
51 +
52 + test_must_fail git -C blob.prefix rev-parse dead &&
53 + test_cmp_failed_rev_parse blob.prefix beef <<-\EOF
54 + error: short object ID beef... is ambiguous
55 + hint: The candidates are:
56 + hint: beef... blob
57 + hint: beef... blob
58 + fatal: ambiguous argument '\''beef...'\'': unknown revision or path not in the working tree.
59 + Use '\''--'\'' to separate paths from revisions, like this:
60 + '\''git <command> [<revision>...] -- [<file>...]'\''
61 + EOF
62 +'
63 +
64 +test_expect_success 'ambiguous loose bad object parsed as OBJ_BAD' '
65 + git init --bare blob.bad &&
66 + (
67 + cd blob.bad &&
68 +
69 + # Both have the prefix "bad0"
70 + echo xyzfaowcoh | git hash-object -t bad -w --stdin --literally &&
71 + echo xyzhjpyvwl | git hash-object -t bad -w --stdin --literally
72 + ) &&
73 +
74 + test_cmp_failed_rev_parse blob.bad bad0 <<-\EOF
75 + error: short object ID bad0... is ambiguous
76 + hint: The candidates are:
77 + fatal: invalid object type
78 + EOF
79 +'
80 +
81 +test_expect_success POSIXPERM 'ambigous zlib corrupt loose blob' '
82 + git init --bare blob.corrupt &&
83 + (
84 + cd blob.corrupt &&
85 +
86 + # Both have the prefix "cafe"
87 + echo bnkxmdwz | git hash-object -w --stdin &&
88 + oid=$(echo bmwsjxzi | git hash-object -w --stdin) &&
89 +
90 + oidf=objects/$(test_oid_to_path "$oid") &&
91 + chmod 755 $oidf &&
92 + echo broken >$oidf
93 + ) &&
94 +
95 + test_cmp_failed_rev_parse blob.corrupt cafe <<-\EOF
96 + error: short object ID cafe... is ambiguous
97 + hint: The candidates are:
98 + error: inflate: data stream error (incorrect header check)
99 + error: unable to unpack cafe... header
100 + error: inflate: data stream error (incorrect header check)
101 + error: unable to unpack cafe... header
102 + hint: cafe... unknown type
103 + hint: cafe... blob
104 + fatal: ambiguous argument '\''cafe...'\'': unknown revision or path not in the working tree.
105 + Use '\''--'\'' to separate paths from revisions, like this:
106 + '\''git <command> [<revision>...] -- [<file>...]'\''
107 + EOF
108 +'
109 +
110 if ! test_have_prereq SHA1
111 then
112 skip_all='not using SHA-1 for objects'