Skip to content

Commit b65bc21

Browse files
author
Junio C Hamano
committed
Makefile: add framework to verify and bench sha1 implementations.
Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent 86378b3 commit b65bc21

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,12 @@ test-delta$X: test-delta.c diff-delta.o patch-delta.o
636636
test-dump-cache-tree$X: dump-cache-tree.o $(GITLIBS)
637637
$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(LIBS)
638638

639+
test-sha1$X: test-sha1.o $(GITLIBS)
640+
$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(LIBS)
641+
642+
check-sha1:: test-sha1$X
643+
./test-sha1.sh
644+
639645
check:
640646
for i in *.c; do sparse $(ALL_CFLAGS) $(SPARSE_FLAGS) $$i || exit; done
641647

test-sha1.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "cache.h"
2+
3+
int main(int ac, char **av)
4+
{
5+
SHA_CTX ctx;
6+
unsigned char sha1[20];
7+
8+
SHA1_Init(&ctx);
9+
10+
while (1) {
11+
ssize_t sz;
12+
char buffer[8192];
13+
sz = xread(0, buffer, sizeof(buffer));
14+
if (sz == 0)
15+
break;
16+
if (sz < 0)
17+
die("test-sha1: %s", strerror(errno));
18+
SHA1_Update(&ctx, buffer, sz);
19+
}
20+
SHA1_Final(sha1, &ctx);
21+
puts(sha1_to_hex(sha1));
22+
exit(0);
23+
}
24+

test-sha1.sh

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/bin/sh
2+
3+
dd if=/dev/zero bs=1048576 count=100 2>/dev/null |
4+
/usr/bin/time ./test-sha1 >/dev/null
5+
6+
while read expect cnt pfx
7+
do
8+
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+
tr '[\0]' '[g]'
14+
} | ./test-sha1
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
25+
done <<EOF
26+
da39a3ee5e6b4b0d3255bfef95601890afd80709 0
27+
3f786850e387550fdab836ed7e6dc881de23001b 0 a
28+
5277cbb45a15902137d332d97e89cf8136545485 0 ab
29+
03cfd743661f07975fa2f1220c5194cbaff48451 0 abc
30+
3330b4373640f9e4604991e73c7e86bfd8da2dc3 0 abcd
31+
ec11312386ad561674f724b8cca7cf1796e26d1d 0 abcde
32+
bdc37c074ec4ee6050d68bc133c6b912f36474df 0 abcdef
33+
69bca99b923859f2dc486b55b87f49689b7358c7 0 abcdefg
34+
e414af7161c9554089f4106d6f1797ef14a73666 0 abcdefgh
35+
0707f2970043f9f7c22029482db27733deaec029 0 abcdefghi
36+
a4dd8aa74a5636728fe52451636e2e17726033aa 1
37+
9986b45e2f4d7086372533bb6953a8652fa3644a 1 frotz
38+
23d8d4f788e8526b4877548a32577543cbaaf51f 10
39+
8cd23f822ab44c7f481b8c92d591f6d1fcad431c 10 frotz
40+
f3b5604a4e604899c1233edb3bf1cc0ede4d8c32 512
41+
b095bd837a371593048136e429e9ac4b476e1bb3 512 frotz
42+
08fa81d6190948de5ccca3966340cc48c10cceac 1200 xyzzy
43+
e33a291f42c30a159733dd98b8b3e4ff34158ca0 4090 4G
44+
#a3bf783bc20caa958f6cb24dd140a7b21984838d 9999 nitfol
45+
EOF
46+
47+
exit
48+
49+
# generating test vectors
50+
# inputs are number of megabytes followed by some random string to prefix.
51+
52+
while read cnt pfx
53+
do
54+
actual=`
55+
{
56+
test -z "$pfx" || echo "$pfx"
57+
dd if=/dev/zero bs=1048576 count=$cnt 2>/dev/null |
58+
tr '[\0]' '[g]'
59+
} | sha1sum |
60+
sed -e 's/ .*//'
61+
`
62+
echo "$actual $cnt $pfx"
63+
done <<EOF
64+
0
65+
0 a
66+
0 ab
67+
0 abc
68+
0 abcd
69+
0 abcde
70+
0 abcdef
71+
0 abcdefg
72+
0 abcdefgh
73+
0 abcdefghi
74+
1
75+
1 frotz
76+
10
77+
10 frotz
78+
512
79+
512 frotz
80+
1200 xyzzy
81+
4090 4G
82+
9999 nitfol
83+
EOF

0 commit comments

Comments
 (0)