Skip to content

Commit

Permalink
t/helper/test-tool: implement sha1-unsafe helper
Browse files Browse the repository at this point in the history
Add a new helper similar to 't/helper/test-tool sha1' called instead
"sha1-unsafe" which uses the unsafe variant of Git's SHA-1 wrappers.

While we're at it, modify the test-sha1.sh script to exercise both
the sha1 and sha1-unsafe test tools to ensure that both produce the
expected hash values.

Signed-off-by: Taylor Blau <[email protected]>
  • Loading branch information
ttaylorr committed Nov 5, 2024
1 parent 0e2fcee commit d8c1fc7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 16 deletions.
5 changes: 5 additions & 0 deletions t/helper/test-sha1.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
38 changes: 22 additions & 16 deletions t/helper/test-sha1.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions t/helper/test-tool.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
1 change: 1 addition & 0 deletions t/helper/test-tool.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit d8c1fc7

Please sign in to comment.