Skip to content

Commit d9213e4

Browse files
ttaylorrgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fbe8d30 commit d9213e4

File tree

6 files changed

+45
-23
lines changed

6 files changed

+45
-23
lines changed

t/helper/test-hash.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "test-tool.h"
22
#include "hex.h"
33

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)
55
{
66
git_hash_ctx ctx;
77
unsigned char hash[GIT_MAX_HEXSZ];
@@ -27,7 +27,10 @@ int cmd_hash_impl(int ac, const char **av, int algo)
2727
die("OOPS");
2828
}
2929

30-
algop->init_fn(&ctx);
30+
if (unsafe)
31+
algop->unsafe_init_fn(&ctx);
32+
else
33+
algop->init_fn(&ctx);
3134

3235
while (1) {
3336
ssize_t sz, this_sz;
@@ -46,9 +49,15 @@ int cmd_hash_impl(int ac, const char **av, int algo)
4649
}
4750
if (this_sz == 0)
4851
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);
5056
}
51-
algop->final_fn(hash, &ctx);
57+
if (unsafe)
58+
algop->unsafe_final_fn(hash, &ctx);
59+
else
60+
algop->final_fn(hash, &ctx);
5261

5362
if (binary)
5463
fwrite(hash, 1, algop->rawsz, stdout);

t/helper/test-sha1.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
int cmd__sha1(int ac, const char **av)
55
{
6-
return cmd_hash_impl(ac, av, GIT_HASH_SHA1);
6+
return cmd_hash_impl(ac, av, GIT_HASH_SHA1, 0);
77
}
88

99
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)
1313
#endif
1414
return 1;
1515
}
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

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,31 @@
33
dd if=/dev/zero bs=1048576 count=100 2>/dev/null |
44
/usr/bin/time t/helper/test-tool sha1 >/dev/null
55

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+
69
while read expect cnt pfx
710
do
811
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
2531
done <<EOF
2632
da39a3ee5e6b4b0d3255bfef95601890afd80709 0
2733
3f786850e387550fdab836ed7e6dc881de23001b 0 a

t/helper/test-sha256.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33

44
int cmd__sha256(int ac, const char **av)
55
{
6-
return cmd_hash_impl(ac, av, GIT_HASH_SHA256);
6+
return cmd_hash_impl(ac, av, GIT_HASH_SHA256, 0);
77
}

t/helper/test-tool.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ static struct test_cmd cmds[] = {
7070
{ "serve-v2", cmd__serve_v2 },
7171
{ "sha1", cmd__sha1 },
7272
{ "sha1-is-sha1dc", cmd__sha1_is_sha1dc },
73+
{ "sha1-unsafe", cmd__sha1_unsafe },
7374
{ "sha256", cmd__sha256 },
7475
{ "sigchain", cmd__sigchain },
7576
{ "simple-ipc", cmd__simple_ipc },

t/helper/test-tool.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ int cmd__scrap_cache_tree(int argc, const char **argv);
6363
int cmd__serve_v2(int argc, const char **argv);
6464
int cmd__sha1(int argc, const char **argv);
6565
int cmd__sha1_is_sha1dc(int argc, const char **argv);
66+
int cmd__sha1_unsafe(int argc, const char **argv);
6667
int cmd__sha256(int argc, const char **argv);
6768
int cmd__sigchain(int argc, const char **argv);
6869
int cmd__simple_ipc(int argc, const char **argv);
@@ -81,6 +82,6 @@ int cmd__windows_named_pipe(int argc, const char **argv);
8182
#endif
8283
int cmd__write_cache(int argc, const char **argv);
8384

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);
8586

8687
#endif

0 commit comments

Comments
 (0)