t/helper/test-tool: implement sha1-unsafe helper
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>
Taylor Blau committed
Jan 23, 2025 at 12:34 UTC
d9213e4716ec8d0ac543d32a52a39c79818cb8ca
6 files changed
+45
-23
t/helper/test-hash.c
+13
-4
@@ -1,7 +1,7 @@
1
#include "test-tool.h"
2
#include "hex.h"
3
4
-int cmd_hash_impl(int ac, const char **av, int algo)
4
+int cmd_hash_impl(int ac, const char **av, int algo, int unsafe)
5
{
6
git_hash_ctx ctx;
7
unsigned char hash[GIT_MAX_HEXSZ];
@@ -27,7 +27,10 @@ int cmd_hash_impl(int ac, const char **av, int algo)
27
die("OOPS");
28
}
29
30
- algop->init_fn(&ctx);
30
+ if (unsafe)
31
+ algop->unsafe_init_fn(&ctx);
32
+ else
33
+ algop->init_fn(&ctx);
34
35
while (1) {
36
ssize_t sz, this_sz;
@@ -46,9 +49,15 @@ int cmd_hash_impl(int ac, const char **av, int algo)
49
}
50
if (this_sz == 0)
51
break;
49
- algop->update_fn(&ctx, buffer, this_sz);
52
+ if (unsafe)
53
+ algop->unsafe_update_fn(&ctx, buffer, this_sz);
54
+ else
55
+ algop->update_fn(&ctx, buffer, this_sz);
56
}
51
- algop->final_fn(hash, &ctx);
57
+ if (unsafe)
58
+ algop->unsafe_final_fn(hash, &ctx);
59
+ else
60
+ algop->final_fn(hash, &ctx);
61
62
if (binary)
63
fwrite(hash, 1, algop->rawsz, stdout);
t/helper/test-sha1.c
+6
-1
@@ -3,7 +3,7 @@
3
4
int cmd__sha1(int ac, const char **av)
5
{
6
- return cmd_hash_impl(ac, av, GIT_HASH_SHA1);
6
+ return cmd_hash_impl(ac, av, GIT_HASH_SHA1, 0);
7
}
8
9
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)
13
#endif
14
return 1;
15
}
16
+
17
+int cmd__sha1_unsafe(int ac, const char **av)
18
+{
19
+ return cmd_hash_impl(ac, av, GIT_HASH_SHA1, 1);
20
+}
t/helper/test-sha1.sh
+22
-16
@@ -3,25 +3,31 @@
3
dd if=/dev/zero bs=1048576 count=100 2>/dev/null |
4
/usr/bin/time t/helper/test-tool sha1 >/dev/null
5
6
+dd if=/dev/zero bs=1048576 count=100 2>/dev/null |
7
+/usr/bin/time t/helper/test-tool sha1-unsafe >/dev/null
8
+
9
while read expect cnt pfx
10
do
11
case "$expect" in '#'*) continue ;; esac
9
- actual=$(
10
- {
11
- test -z "$pfx" || echo "$pfx"
12
- dd if=/dev/zero bs=1048576 count=$cnt 2>/dev/null |
13
- perl -pe 'y/\000/g/'
14
- } | ./t/helper/test-tool sha1 $cnt
15
- )
16
- if test "$expect" = "$actual"
17
- then
18
- echo "OK: $expect $cnt $pfx"
19
- else
20
- echo >&2 "OOPS: $cnt"
21
- echo >&2 "expect: $expect"
22
- echo >&2 "actual: $actual"
23
- exit 1
24
- fi
12
+ for sha1 in sha1 sha1-unsafe
13
+ do
14
+ actual=$(
15
+ {
16
+ test -z "$pfx" || echo "$pfx"
17
+ dd if=/dev/zero bs=1048576 count=$cnt 2>/dev/null |
18
+ perl -pe 'y/\000/g/'
19
+ } | ./t/helper/test-tool $sha1 $cnt
20
+ )
21
+ if test "$expect" = "$actual"
22
+ then
23
+ echo "OK ($sha1): $expect $cnt $pfx"
24
+ else
25
+ echo >&2 "OOPS ($sha1): $cnt"
26
+ echo >&2 "expect ($sha1): $expect"
27
+ echo >&2 "actual ($sha1): $actual"
28
+ exit 1
29
+ fi
30
+ done
31
done <<EOF
32
da39a3ee5e6b4b0d3255bfef95601890afd80709 0
33
3f786850e387550fdab836ed7e6dc881de23001b 0 a
t/helper/test-sha256.c
+1
-1
@@ -3,5 +3,5 @@
3
4
int cmd__sha256(int ac, const char **av)
5
{
6
- return cmd_hash_impl(ac, av, GIT_HASH_SHA256);
6
+ return cmd_hash_impl(ac, av, GIT_HASH_SHA256, 0);
7
}
t/helper/test-tool.c
+1
@@ -70,6 +70,7 @@ static struct test_cmd cmds[] = {
70
{ "serve-v2", cmd__serve_v2 },
71
{ "sha1", cmd__sha1 },
72
{ "sha1-is-sha1dc", cmd__sha1_is_sha1dc },
73
+ { "sha1-unsafe", cmd__sha1_unsafe },
74
{ "sha256", cmd__sha256 },
75
{ "sigchain", cmd__sigchain },
76
{ "simple-ipc", cmd__simple_ipc },
t/helper/test-tool.h
+2
-1
@@ -63,6 +63,7 @@ int cmd__scrap_cache_tree(int argc, const char **argv);
63
int cmd__serve_v2(int argc, const char **argv);
64
int cmd__sha1(int argc, const char **argv);
65
int cmd__sha1_is_sha1dc(int argc, const char **argv);
66
+int cmd__sha1_unsafe(int argc, const char **argv);
67
int cmd__sha256(int argc, const char **argv);
68
int cmd__sigchain(int argc, const char **argv);
69
int cmd__simple_ipc(int argc, const char **argv);
@@ -81,6 +82,6 @@ int cmd__windows_named_pipe(int argc, const char **argv);
82
#endif
83
int cmd__write_cache(int argc, const char **argv);
84
84
-int cmd_hash_impl(int ac, const char **av, int algo);
85
+int cmd_hash_impl(int ac, const char **av, int algo, int unsafe);
86
87
#endif