Skip to content

Commit b57cbbf

Browse files
author
Junio C Hamano
committed
test-sha1: test hashing large buffer
test to hash a large buffer in one go is more important than hashing large amount of data in small fixed chunks. Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent b65bc21 commit b57cbbf

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

test-sha1.c

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,44 @@ int main(int ac, char **av)
44
{
55
SHA_CTX ctx;
66
unsigned char sha1[20];
7+
unsigned bufsz = 8192;
8+
char *buffer;
9+
10+
if (ac == 2)
11+
bufsz = strtoul(av[1], NULL, 10) * 1024 * 1024;
12+
13+
if (!bufsz)
14+
bufsz = 8192;
15+
16+
while ((buffer = malloc(bufsz)) == NULL) {
17+
fprintf(stderr, "bufsz %u is too big, halving...\n", bufsz);
18+
bufsz /= 2;
19+
if (bufsz < 1024)
20+
die("OOPS");
21+
}
722

823
SHA1_Init(&ctx);
924

1025
while (1) {
11-
ssize_t sz;
12-
char buffer[8192];
13-
sz = xread(0, buffer, sizeof(buffer));
14-
if (sz == 0)
26+
ssize_t sz, this_sz;
27+
char *cp = buffer;
28+
unsigned room = bufsz;
29+
this_sz = 0;
30+
while (room) {
31+
sz = xread(0, cp, room);
32+
if (sz == 0)
33+
break;
34+
if (sz < 0)
35+
die("test-sha1: %s", strerror(errno));
36+
this_sz += sz;
37+
cp += sz;
38+
room -= sz;
39+
}
40+
if (this_sz == 0)
1541
break;
16-
if (sz < 0)
17-
die("test-sha1: %s", strerror(errno));
18-
SHA1_Update(&ctx, buffer, sz);
42+
SHA1_Update(&ctx, buffer, this_sz);
1943
}
2044
SHA1_Final(sha1, &ctx);
2145
puts(sha1_to_hex(sha1));
2246
exit(0);
2347
}
24-

test-sha1.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ do
1111
test -z "$pfx" || echo "$pfx"
1212
dd if=/dev/zero bs=1048576 count=$cnt 2>/dev/null |
1313
tr '[\0]' '[g]'
14-
} | ./test-sha1
14+
} | ./test-sha1 $cnt
1515
`
1616
if test "$expect" = "$actual"
1717
then

0 commit comments

Comments
 (0)