]> git.ipfire.org Git - thirdparty/git.git/commitdiff
t/helper/test-tool: implement sha1-unsafe helper
authorTaylor Blau <me@ttaylorr.com>
Thu, 23 Jan 2025 17:34:19 +0000 (12:34 -0500)
committerJunio C Hamano <gitster@pobox.com>
Thu, 23 Jan 2025 18:28:16 +0000 (10:28 -0800)
With the new "unsafe" SHA-1 build knob, it is convenient to have a
test-tool that can exercise Git's unsafe SHA-1 wrappers for testing,
similar to 't/helper/test-tool sha1'.

Implement that helper by altering the implementation of that test-tool
(in cmd_hash_impl(), which is generic and parameterized over different
hash functions) to conditionally run the unsafe variants of the chosen
hash function, and expose the new behavior via a new 'sha1-unsafe' test
helper.

Signed-off-by: Taylor Blau <me@ttaylorr.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
t/helper/test-hash.c
t/helper/test-sha1.c
t/helper/test-sha1.sh
t/helper/test-sha256.c
t/helper/test-tool.c
t/helper/test-tool.h

index 45d829c908fec95ded535ebcfe502c3bb4aa5441..d0ee668df9574cc3be890f2f340d43c5e7254232 100644 (file)
@@ -1,7 +1,7 @@
 #include "test-tool.h"
 #include "hex.h"
 
-int cmd_hash_impl(int ac, const char **av, int algo)
+int cmd_hash_impl(int ac, const char **av, int algo, int unsafe)
 {
        git_hash_ctx ctx;
        unsigned char hash[GIT_MAX_HEXSZ];
@@ -27,7 +27,10 @@ int cmd_hash_impl(int ac, const char **av, int algo)
                        die("OOPS");
        }
 
-       algop->init_fn(&ctx);
+       if (unsafe)
+               algop->unsafe_init_fn(&ctx);
+       else
+               algop->init_fn(&ctx);
 
        while (1) {
                ssize_t sz, this_sz;
@@ -46,9 +49,15 @@ int cmd_hash_impl(int ac, const char **av, int algo)
                }
                if (this_sz == 0)
                        break;
-               algop->update_fn(&ctx, buffer, this_sz);
+               if (unsafe)
+                       algop->unsafe_update_fn(&ctx, buffer, this_sz);
+               else
+                       algop->update_fn(&ctx, buffer, this_sz);
        }
-       algop->final_fn(hash, &ctx);
+       if (unsafe)
+               algop->unsafe_final_fn(hash, &ctx);
+       else
+               algop->final_fn(hash, &ctx);
 
        if (binary)
                fwrite(hash, 1, algop->rawsz, stdout);
index e60d000c0393cbbb48e43193399f54d2186463f1..349540c4df8b6aa9f41881b1ee7d400257b68682 100644 (file)
@@ -3,7 +3,7 @@
 
 int cmd__sha1(int ac, const char **av)
 {
-       return cmd_hash_impl(ac, av, GIT_HASH_SHA1);
+       return cmd_hash_impl(ac, av, GIT_HASH_SHA1, 0);
 }
 
 int cmd__sha1_is_sha1dc(int argc UNUSED, const char **argv UNUSED)
@@ -13,3 +13,8 @@ int cmd__sha1_is_sha1dc(int argc UNUSED, const char **argv UNUSED)
 #endif
        return 1;
 }
+
+int cmd__sha1_unsafe(int ac, const char **av)
+{
+       return cmd_hash_impl(ac, av, GIT_HASH_SHA1, 1);
+}
index 84594885c703887c3b09aceb51583b9895ac3b45..bf387d3db14d8bb66c4fcaad7d077068dc93b942 100755 (executable)
@@ -3,25 +3,31 @@
 dd if=/dev/zero bs=1048576 count=100 2>/dev/null |
 /usr/bin/time t/helper/test-tool sha1 >/dev/null
 
+dd if=/dev/zero bs=1048576 count=100 2>/dev/null |
+/usr/bin/time t/helper/test-tool sha1-unsafe >/dev/null
+
 while read expect cnt pfx
 do
        case "$expect" in '#'*) continue ;; esac
-       actual=$(
-               {
-                       test -z "$pfx" || echo "$pfx"
-                       dd if=/dev/zero bs=1048576 count=$cnt 2>/dev/null |
-                       perl -pe 'y/\000/g/'
-               } | ./t/helper/test-tool sha1 $cnt
-       )
-       if test "$expect" = "$actual"
-       then
-               echo "OK: $expect $cnt $pfx"
-       else
-               echo >&2 "OOPS: $cnt"
-               echo >&2 "expect: $expect"
-               echo >&2 "actual: $actual"
-               exit 1
-       fi
+       for sha1 in sha1 sha1-unsafe
+       do
+               actual=$(
+                       {
+                               test -z "$pfx" || echo "$pfx"
+                               dd if=/dev/zero bs=1048576 count=$cnt 2>/dev/null |
+                               perl -pe 'y/\000/g/'
+                       } | ./t/helper/test-tool $sha1 $cnt
+               )
+               if test "$expect" = "$actual"
+               then
+                       echo "OK ($sha1): $expect $cnt $pfx"
+               else
+                       echo >&2 "OOPS ($sha1): $cnt"
+                       echo >&2 "expect ($sha1): $expect"
+                       echo >&2 "actual ($sha1): $actual"
+                       exit 1
+               fi
+       done
 done <<EOF
 da39a3ee5e6b4b0d3255bfef95601890afd80709 0
 3f786850e387550fdab836ed7e6dc881de23001b 0 a
index 2fb20438f3c382d56f16299afece2787d59c9d01..7fd0aa1fcd36135760667233b3f3cac1aca33771 100644 (file)
@@ -3,5 +3,5 @@
 
 int cmd__sha256(int ac, const char **av)
 {
-       return cmd_hash_impl(ac, av, GIT_HASH_SHA256);
+       return cmd_hash_impl(ac, av, GIT_HASH_SHA256, 0);
 }
index 4a7aa993ba99e2791718e0d7206158aad207dc56..958452ef12e33dc3c91aaecb2607f2ca4475e079 100644 (file)
@@ -70,6 +70,7 @@ static struct test_cmd cmds[] = {
        { "serve-v2", cmd__serve_v2 },
        { "sha1", cmd__sha1 },
        { "sha1-is-sha1dc", cmd__sha1_is_sha1dc },
+       { "sha1-unsafe", cmd__sha1_unsafe },
        { "sha256", cmd__sha256 },
        { "sigchain", cmd__sigchain },
        { "simple-ipc", cmd__simple_ipc },
index 21802ac27da37f8730c2f68cdd8d82932e2dcc92..24149edd414ee9de87f8236b3b92532a4e15d246 100644 (file)
@@ -63,6 +63,7 @@ int cmd__scrap_cache_tree(int argc, const char **argv);
 int cmd__serve_v2(int argc, const char **argv);
 int cmd__sha1(int argc, const char **argv);
 int cmd__sha1_is_sha1dc(int argc, const char **argv);
+int cmd__sha1_unsafe(int argc, const char **argv);
 int cmd__sha256(int argc, const char **argv);
 int cmd__sigchain(int argc, const char **argv);
 int cmd__simple_ipc(int argc, const char **argv);
@@ -81,6 +82,6 @@ int cmd__windows_named_pipe(int argc, const char **argv);
 #endif
 int cmd__write_cache(int argc, const char **argv);
 
-int cmd_hash_impl(int ac, const char **av, int algo);
+int cmd_hash_impl(int ac, const char **av, int algo, int unsafe);
 
 #endif