]> git.ipfire.org Git - thirdparty/git.git/commitdiff
t3404: prepare 'short SHA-1 collision' tests for SHA-256
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Wed, 29 Jul 2020 23:13:53 +0000 (23:13 +0000)
committerJunio C Hamano <gitster@pobox.com>
Thu, 30 Jul 2020 16:16:45 +0000 (09:16 -0700)
The idea of the magic value "ac4f2ee" in this test is to make the
reworded commit `collide2` have the same shortened ID as the commit
`collide3`.

To port the same idea to the SHA-256 version of Git, we therefore need
another magic value that causes the same collision, but this time with
the SHA-256 version of the commit IDs.

In this patch, we add code guarded by `GIT_TEST_FIND_COLLIDER` to do
exactly that. Essentially, a large number of integers is appended to the
commit message "collide2" to find such a collision. To make it easier to
find such a collision, we reduce the number of digits to 4.

As the tests are no longer dependent on SHA-1, we also rename their
titles to talk about "commit IDs" instead of "SHA-1s".

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
Reviewed-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
t/t3404-rebase-interactive.sh

index 4a7d21f898f7361ffcdf988b2458e52089b758ba..9744e88760c9b1ba2621c82e2474e44c8f16e9a7 100755 (executable)
@@ -1250,7 +1250,7 @@ test_expect_success 'rebase -i error on commits with \ in message' '
        test_expect_code 1 grep  "      emp" error
 '
 
-test_expect_success SHA1 'short SHA-1 setup' '
+test_expect_success 'short commit ID setup' '
        test_when_finished "git checkout master" &&
        git checkout --orphan collide &&
        git rm -rf . &&
@@ -1262,23 +1262,54 @@ test_expect_success SHA1 'short SHA-1 setup' '
        )
 '
 
-test_expect_success SHA1 'short SHA-1 collide' '
+if test -n "$GIT_TEST_FIND_COLLIDER"
+then
+       author="$(unset test_tick; test_tick; git var GIT_AUTHOR_IDENT)"
+       committer="$(unset test_tick; test_tick; git var GIT_COMMITTER_IDENT)"
+       blob="$(git rev-parse collide2:collide)"
+       from="$(git rev-parse collide1^0)"
+       repl="commit refs/heads/collider-&\\n"
+       repl="${repl}author $author\\ncommitter $committer\\n"
+       repl="${repl}data <<EOF\\ncollide2 &\\nEOF\\n"
+       repl="${repl}from $from\\nM 100644 $blob collide\\n"
+       test_seq 1 32768 | sed "s|.*|$repl|" >script &&
+       git fast-import <script &&
+       git pack-refs &&
+       git for-each-ref >refs &&
+       grep "^$(test_oid t3404_collision)" <refs >matches &&
+       cat matches &&
+       test_line_count -gt 2 matches || {
+               echo "Could not find a collider" >&2
+               exit 1
+       }
+fi
+
+test_expect_success 'short commit ID collide' '
+       test_oid_cache <<-EOF &&
+       # collision-related constants
+       t3404_collision sha1:6bcd
+       t3404_collision sha256:0161
+       t3404_collider  sha1:ac4f2ee
+       t3404_collider  sha256:16697
+       EOF
        test_when_finished "reset_rebase && git checkout master" &&
        git checkout collide &&
-       colliding_sha1=6bcda37 &&
-       test $colliding_sha1 = "$(git rev-parse HEAD | cut -c 1-7)" &&
+       colliding_id=$(test_oid t3404_collision) &&
+       hexsz=$(test_oid hexsz) &&
+       test $colliding_id = "$(git rev-parse HEAD | cut -c 1-4)" &&
+       test_config core.abbrev 4 &&
        (
                unset test_tick &&
                test_tick &&
                set_fake_editor &&
-               FAKE_COMMIT_MESSAGE="collide2 ac4f2ee" \
+               FAKE_COMMIT_MESSAGE="collide2 $(test_oid t3404_collider)" \
                FAKE_LINES="reword 1 break 2" git rebase -i HEAD~2 &&
-               test $colliding_sha1 = "$(git rev-parse HEAD | cut -c 1-7)" &&
-               grep "^pick $colliding_sha1 " \
+               test $colliding_id = "$(git rev-parse HEAD | cut -c 1-4)" &&
+               grep "^pick $colliding_id " \
                        .git/rebase-merge/git-rebase-todo.tmp &&
-               grep "^pick [0-9a-f]\{40\}" \
+               grep "^pick [0-9a-f]\{$hexsz\}" \
                        .git/rebase-merge/git-rebase-todo &&
-               grep "^pick [0-9a-f]\{40\}" \
+               grep "^pick [0-9a-f]\{$hexsz\}" \
                        .git/rebase-merge/git-rebase-todo.backup &&
                git rebase --continue
        ) &&