@matheusbsbs / matheus-bagliano / commits / 800e6a7041
Fork 0

t3404: prepare 'short SHA-1 collision' tests for SHA-256

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>

Johannes Schindelin committed Jul 29, 2020 at 23:13 UTC 800e6a7041961b91fb97ab5dfd440983953fe75c
1 file changed +40 -9
t/t3404-rebase-interactive.sh
+40 -9
@@ -1250,7 +1250,7 @@ test_expect_success 'rebase -i error on commits with \ in message' '
1250 test_expect_code 1 grep " emp" error
1251 '
1252
1253 -test_expect_success SHA1 'short SHA-1 setup' '
1253 +test_expect_success 'short commit ID setup' '
1254 test_when_finished "git checkout master" &&
1255 git checkout --orphan collide &&
1256 git rm -rf . &&
@@ -1262,23 +1262,54 @@ test_expect_success SHA1 'short SHA-1 setup' '
1262 )
1263 '
1264
1265 -test_expect_success SHA1 'short SHA-1 collide' '
1265 +if test -n "$GIT_TEST_FIND_COLLIDER"
1266 +then
1267 + author="$(unset test_tick; test_tick; git var GIT_AUTHOR_IDENT)"
1268 + committer="$(unset test_tick; test_tick; git var GIT_COMMITTER_IDENT)"
1269 + blob="$(git rev-parse collide2:collide)"
1270 + from="$(git rev-parse collide1^0)"
1271 + repl="commit refs/heads/collider-&\\n"
1272 + repl="${repl}author $author\\ncommitter $committer\\n"
1273 + repl="${repl}data <<EOF\\ncollide2 &\\nEOF\\n"
1274 + repl="${repl}from $from\\nM 100644 $blob collide\\n"
1275 + test_seq 1 32768 | sed "s|.*|$repl|" >script &&
1276 + git fast-import <script &&
1277 + git pack-refs &&
1278 + git for-each-ref >refs &&
1279 + grep "^$(test_oid t3404_collision)" <refs >matches &&
1280 + cat matches &&
1281 + test_line_count -gt 2 matches || {
1282 + echo "Could not find a collider" >&2
1283 + exit 1
1284 + }
1285 +fi
1286 +
1287 +test_expect_success 'short commit ID collide' '
1288 + test_oid_cache <<-EOF &&
1289 + # collision-related constants
1290 + t3404_collision sha1:6bcd
1291 + t3404_collision sha256:0161
1292 + t3404_collider sha1:ac4f2ee
1293 + t3404_collider sha256:16697
1294 + EOF
1295 test_when_finished "reset_rebase && git checkout master" &&
1296 git checkout collide &&
1268 - colliding_sha1=6bcda37 &&
1269 - test $colliding_sha1 = "$(git rev-parse HEAD | cut -c 1-7)" &&
1297 + colliding_id=$(test_oid t3404_collision) &&
1298 + hexsz=$(test_oid hexsz) &&
1299 + test $colliding_id = "$(git rev-parse HEAD | cut -c 1-4)" &&
1300 + test_config core.abbrev 4 &&
1301 (
1302 unset test_tick &&
1303 test_tick &&
1304 set_fake_editor &&
1274 - FAKE_COMMIT_MESSAGE="collide2 ac4f2ee" \
1305 + FAKE_COMMIT_MESSAGE="collide2 $(test_oid t3404_collider)" \
1306 FAKE_LINES="reword 1 break 2" git rebase -i HEAD~2 &&
1276 - test $colliding_sha1 = "$(git rev-parse HEAD | cut -c 1-7)" &&
1277 - grep "^pick $colliding_sha1 " \
1307 + test $colliding_id = "$(git rev-parse HEAD | cut -c 1-4)" &&
1308 + grep "^pick $colliding_id " \
1309 .git/rebase-merge/git-rebase-todo.tmp &&
1279 - grep "^pick [0-9a-f]\{40\}" \
1310 + grep "^pick [0-9a-f]\{$hexsz\}" \
1311 .git/rebase-merge/git-rebase-todo &&
1281 - grep "^pick [0-9a-f]\{40\}" \
1312 + grep "^pick [0-9a-f]\{$hexsz\}" \
1313 .git/rebase-merge/git-rebase-todo.backup &&
1314 git rebase --continue
1315 ) &&