diff --git a/Makefile b/Makefile index 302b86354850c3..ca6b38069c002d 100644 --- a/Makefile +++ b/Makefile @@ -1346,6 +1346,7 @@ UNIT_TEST_PROGRAMS += t-basic UNIT_TEST_PROGRAMS += t-mem-pool UNIT_TEST_PROGRAMS += t-strbuf UNIT_TEST_PROGRAMS += t-ctype +UNIT_TEST_PROGRAMS += t-sha256 UNIT_TEST_PROGS = $(patsubst %,$(UNIT_TEST_BIN)/%$X,$(UNIT_TEST_PROGRAMS)) UNIT_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(UNIT_TEST_PROGRAMS)) UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/test-lib.o diff --git a/t/unit-tests/t-sha256.c b/t/unit-tests/t-sha256.c new file mode 100644 index 00000000000000..6c7f70ca19b6a4 --- /dev/null +++ b/t/unit-tests/t-sha256.c @@ -0,0 +1,102 @@ +#include "test-lib.h" +#include "hash-ll.h" +#include "hex.h" + +static const char out_file[] = "./output.txt"; + +static void hash_impl(const char **av, int algo) { + git_hash_ctx ctx; + unsigned char hash[GIT_MAX_HEXSZ]; + unsigned bufsz = 8192; + int binary = 0; + char *buffer; + const struct git_hash_algo *algop = &hash_algos[algo]; + FILE *file; + size_t data_length; + + int ac = 0; + while (av[ac] != NULL) { + ac++; + } + + if (ac > 1) { + if (!strcmp(av[1], "-b")) { + binary = 1; + if (ac > 2) { + bufsz = strtoul(av[2], NULL, 10) * 1024 * 1024; + } + } else { + bufsz = strtoul(av[1], NULL, 10) * 1024 * 1024; + } + } + + if (!bufsz) + bufsz = 8192; + + while ((buffer = malloc(bufsz)) == NULL) { + fprintf(stderr, "bufsz %u is too big, halving...\n", bufsz); + bufsz /= 2; + if (bufsz < 1024) + die("OOPS"); + } + + algop->init_fn(&ctx); + + data_length = strlen(av[0]); + + algop->update_fn(&ctx, av[0], data_length); + + algop->final_fn(hash, &ctx); + + file = freopen(out_file, "wb", stderr); + if (!check(!!file)) { + test_msg("Error opening file %s", out_file); + return; + } + + if (binary) + fwrite(hash, 1, algop->rawsz, file); + else + puts(hash_to_hex_algop(hash, algop)); + + fclose(file); + free(buffer); +} + +static char *grep(const char *pattern, const char *filename) { + char *result; + char line[1000]; + FILE *file = fopen(out_file, "r"); + if (!check(!!file)) { + test_msg("Error opening file %s", out_file); + return NULL; + } + + while (fgets(line, sizeof(line), file)) { + if (strstr(line, pattern)) { + fclose(file); + + result = strdup(line); + if (!result) { + fprintf(stderr, "Memory allocation error\n"); + return NULL; + } + return result; + } + } + + fclose(file); + return NULL; +} + +static void check_sha256(const char **input, const char *expected) { + hash_impl(input, GIT_HASH_SHA256); + check_str(grep(expected, out_file), expected); +} + +int cmd_main(int argc, const char **argv) { + const char *av[] = {"a", NULL}; + TEST(check_sha256(av, "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"), "SHA256 a works"); + + return test_done(); +}