t: make the sha1 test-tool helper generic

Since we're going to have multiple hash algorithms to test, it makes sense to share as much of the test code as possible. Convert the sha1 helper for the test-tool to be generic and move it out into its own module. This will allow us to share most of this code with our NewHash implementation. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

brian m. carlson committed Nov 14, 2018 at 04:09 UTC 50c817e0c02a133a1615ba32971fd5f6649c894f
4 files changed +62 -51
Makefile
+1
@@ -714,6 +714,7 @@ TEST_BUILTINS_OBJS += test-dump-split-index.o
714 TEST_BUILTINS_OBJS += test-dump-untracked-cache.o
715 TEST_BUILTINS_OBJS += test-example-decorate.o
716 TEST_BUILTINS_OBJS += test-genrandom.o
717 +TEST_BUILTINS_OBJS += test-hash.o
718 TEST_BUILTINS_OBJS += test-hashmap.o
719 TEST_BUILTINS_OBJS += test-index-version.o
720 TEST_BUILTINS_OBJS += test-json-writer.o
t/helper/test-hash.c new
+58
@@ -0,0 +1,58 @@
1 +#include "test-tool.h"
2 +#include "cache.h"
3 +
4 +int cmd_hash_impl(int ac, const char **av, int algo)
5 +{
6 + git_hash_ctx ctx;
7 + unsigned char hash[GIT_MAX_HEXSZ];
8 + unsigned bufsz = 8192;
9 + int binary = 0;
10 + char *buffer;
11 + const struct git_hash_algo *algop = &hash_algos[algo];
12 +
13 + if (ac == 2) {
14 + if (!strcmp(av[1], "-b"))
15 + binary = 1;
16 + else
17 + bufsz = strtoul(av[1], NULL, 10) * 1024 * 1024;
18 + }
19 +
20 + if (!bufsz)
21 + bufsz = 8192;
22 +
23 + while ((buffer = malloc(bufsz)) == NULL) {
24 + fprintf(stderr, "bufsz %u is too big, halving...\n", bufsz);
25 + bufsz /= 2;
26 + if (bufsz < 1024)
27 + die("OOPS");
28 + }
29 +
30 + algop->init_fn(&ctx);
31 +
32 + while (1) {
33 + ssize_t sz, this_sz;
34 + char *cp = buffer;
35 + unsigned room = bufsz;
36 + this_sz = 0;
37 + while (room) {
38 + sz = xread(0, cp, room);
39 + if (sz == 0)
40 + break;
41 + if (sz < 0)
42 + die_errno("test-hash");
43 + this_sz += sz;
44 + cp += sz;
45 + room -= sz;
46 + }
47 + if (this_sz == 0)
48 + break;
49 + algop->update_fn(&ctx, buffer, this_sz);
50 + }
51 + algop->final_fn(hash, &ctx);
52 +
53 + if (binary)
54 + fwrite(hash, 1, algop->rawsz, stdout);
55 + else
56 + puts(hash_to_hex_algop(hash, algop));
57 + exit(0);
58 +}
t/helper/test-sha1.c
+1 -51
@@ -3,55 +3,5 @@
3
4 int cmd__sha1(int ac, const char **av)
5 {
6 - git_SHA_CTX ctx;
7 - unsigned char sha1[20];
8 - unsigned bufsz = 8192;
9 - int binary = 0;
10 - char *buffer;
11 -
12 - if (ac == 2) {
13 - if (!strcmp(av[1], "-b"))
14 - binary = 1;
15 - else
16 - bufsz = strtoul(av[1], NULL, 10) * 1024 * 1024;
17 - }
18 -
19 - if (!bufsz)
20 - bufsz = 8192;
21 -
22 - while ((buffer = malloc(bufsz)) == NULL) {
23 - fprintf(stderr, "bufsz %u is too big, halving...\n", bufsz);
24 - bufsz /= 2;
25 - if (bufsz < 1024)
26 - die("OOPS");
27 - }
28 -
29 - git_SHA1_Init(&ctx);
30 -
31 - while (1) {
32 - ssize_t sz, this_sz;
33 - char *cp = buffer;
34 - unsigned room = bufsz;
35 - this_sz = 0;
36 - while (room) {
37 - sz = xread(0, cp, room);
38 - if (sz == 0)
39 - break;
40 - if (sz < 0)
41 - die_errno("test-sha1");
42 - this_sz += sz;
43 - cp += sz;
44 - room -= sz;
45 - }
46 - if (this_sz == 0)
47 - break;
48 - git_SHA1_Update(&ctx, buffer, this_sz);
49 - }
50 - git_SHA1_Final(sha1, &ctx);
51 -
52 - if (binary)
53 - fwrite(sha1, 1, 20, stdout);
54 - else
55 - puts(sha1_to_hex(sha1));
56 - exit(0);
6 + return cmd_hash_impl(ac, av, GIT_HASH_SHA1);
7 }
t/helper/test-tool.h
+2
@@ -50,4 +50,6 @@ int cmd__windows_named_pipe(int argc, const char **argv);
50 #endif
51 int cmd__write_cache(int argc, const char **argv);
52
53 +int cmd_hash_impl(int ac, const char **av, int algo);
54 +
55 #endif