From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS31976 209.132.180.0/23 X-Spam-Status: No, score=-4.0 required=3.0 tests=AWL,BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI shortcircuit=no autolearn=ham autolearn_force=no version=3.4.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by dcvr.yhbt.net (Postfix) with ESMTP id 591B71F453 for ; Sun, 4 Nov 2018 23:45:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730492AbeKEJB7 (ORCPT ); Mon, 5 Nov 2018 04:01:59 -0500 Received: from injection.crustytoothpaste.net ([192.241.140.119]:53226 "EHLO injection.crustytoothpaste.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729686AbeKEJB6 (ORCPT ); Mon, 5 Nov 2018 04:01:58 -0500 Received: from genre.crustytoothpaste.net (unknown [IPv6:2001:470:b978:101:941b:b2ff:ecfe:7f28]) (using TLSv1.2 with cipher ECDHE-RSA-CHACHA20-POLY1305 (256/256 bits)) (No client certificate requested) by injection.crustytoothpaste.net (Postfix) with ESMTPSA id A7F2061B77; Sun, 4 Nov 2018 23:45:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=crustytoothpaste.net; s=default; t=1541375113; bh=yRvgtxvGTSzl/+jKMYcv4Wpdaj1EDryNpqg57rkcLq8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From:Reply-To: Subject:Date:To:CC:Resent-Date:Resent-From:Resent-To:Resent-Cc: In-Reply-To:References:Content-Type:Content-Disposition; b=yKUc0VR+LIZh0BofNde+7Pr2Dne8TAfQN4j2aqUuWLl7U96XmYSxukHeim/bcqi46 G7eCZ2mTQeuhQ296CLzL1C2Ipf5EiYA9s48lvyRhkT+oJLQOcgiDPyQJyzFdOGlTHI qN1FU2cZH8GbvtQQyAkRdMo2C9jPWMCi8tLiBQgxF1kwpIGdn4rldpR8+UB9zz2NQ9 BNexodrSpYo/JEPGm3tv78CfuzEAP4XoLy+bxcaNke5KjPSb+mn0576iRpG/i641Ye UCRA4KgeKo+mt0nD+CRw2Lm/uyabf5HHx630eDDcpRah5swJFECpY5Wao0YyGR+iL6 Lb8+KWaI57fjAKNvesIijW9AIKKJU+UNhQW0uFXh7G+/dnThX0V8jyZl98sq7n5TvZ kgE1fcgtBm3EfKlH9aMPsE6FwuysbPO97zEdM+iVTIHfk8gnE8QPGDqM0Kvy5JXdxn W6sPXluOpshxYfmBugEIhUOd+mi0wULjsbeiSYkpfmh5OallbqE From: "brian m. carlson" To: git@vger.kernel.org Cc: Derrick Stolee , =?UTF-8?q?=C3=86var=20Arnfj=C3=B6r=C3=B0=20Bjarmason?= , Duy Nguyen , =?UTF-8?q?SZEDER=20G=C3=A1bor?= , Jakub Narebski , Christian Couder Subject: [PATCH v5 06/12] t: make the sha1 test-tool helper generic Date: Sun, 4 Nov 2018 23:44:52 +0000 Message-Id: <20181104234458.139223-7-sandals@crustytoothpaste.net> X-Mailer: git-send-email 2.19.1.930.g4563a0d9d0 In-Reply-To: <20181104234458.139223-1-sandals@crustytoothpaste.net> References: <20181025024005.154208-1-sandals@crustytoothpaste.net> <20181104234458.139223-1-sandals@crustytoothpaste.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 127.0.1.1 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org 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 --- Makefile | 1 + t/helper/{test-sha1.c => test-hash.c} | 19 +++++----- t/helper/test-sha1.c | 52 +-------------------------- t/helper/test-tool.h | 2 ++ 4 files changed, 14 insertions(+), 60 deletions(-) copy t/helper/{test-sha1.c => test-hash.c} (65%) diff --git a/Makefile b/Makefile index d18ab0fe78..81dc9ac819 100644 --- a/Makefile +++ b/Makefile @@ -714,6 +714,7 @@ TEST_BUILTINS_OBJS += test-dump-split-index.o TEST_BUILTINS_OBJS += test-dump-untracked-cache.o TEST_BUILTINS_OBJS += test-example-decorate.o TEST_BUILTINS_OBJS += test-genrandom.o +TEST_BUILTINS_OBJS += test-hash.o TEST_BUILTINS_OBJS += test-hashmap.o TEST_BUILTINS_OBJS += test-index-version.o TEST_BUILTINS_OBJS += test-json-writer.o diff --git a/t/helper/test-sha1.c b/t/helper/test-hash.c similarity index 65% copy from t/helper/test-sha1.c copy to t/helper/test-hash.c index 1ba0675c75..0a31de66f3 100644 --- a/t/helper/test-sha1.c +++ b/t/helper/test-hash.c @@ -1,13 +1,14 @@ #include "test-tool.h" #include "cache.h" -int cmd__sha1(int ac, const char **av) +int cmd_hash_impl(int ac, const char **av, int algo) { - git_SHA_CTX ctx; - unsigned char sha1[20]; + 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]; if (ac == 2) { if (!strcmp(av[1], "-b")) @@ -26,7 +27,7 @@ int cmd__sha1(int ac, const char **av) die("OOPS"); } - git_SHA1_Init(&ctx); + algop->init_fn(&ctx); while (1) { ssize_t sz, this_sz; @@ -38,20 +39,20 @@ int cmd__sha1(int ac, const char **av) if (sz == 0) break; if (sz < 0) - die_errno("test-sha1"); + die_errno("test-hash"); this_sz += sz; cp += sz; room -= sz; } if (this_sz == 0) break; - git_SHA1_Update(&ctx, buffer, this_sz); + algop->update_fn(&ctx, buffer, this_sz); } - git_SHA1_Final(sha1, &ctx); + algop->final_fn(hash, &ctx); if (binary) - fwrite(sha1, 1, 20, stdout); + fwrite(hash, 1, algop->rawsz, stdout); else - puts(sha1_to_hex(sha1)); + puts(hash_to_hex_algop(hash, algop)); exit(0); } diff --git a/t/helper/test-sha1.c b/t/helper/test-sha1.c index 1ba0675c75..d860c387c3 100644 --- a/t/helper/test-sha1.c +++ b/t/helper/test-sha1.c @@ -3,55 +3,5 @@ int cmd__sha1(int ac, const char **av) { - git_SHA_CTX ctx; - unsigned char sha1[20]; - unsigned bufsz = 8192; - int binary = 0; - char *buffer; - - if (ac == 2) { - if (!strcmp(av[1], "-b")) - binary = 1; - 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"); - } - - git_SHA1_Init(&ctx); - - while (1) { - ssize_t sz, this_sz; - char *cp = buffer; - unsigned room = bufsz; - this_sz = 0; - while (room) { - sz = xread(0, cp, room); - if (sz == 0) - break; - if (sz < 0) - die_errno("test-sha1"); - this_sz += sz; - cp += sz; - room -= sz; - } - if (this_sz == 0) - break; - git_SHA1_Update(&ctx, buffer, this_sz); - } - git_SHA1_Final(sha1, &ctx); - - if (binary) - fwrite(sha1, 1, 20, stdout); - else - puts(sha1_to_hex(sha1)); - exit(0); + return cmd_hash_impl(ac, av, GIT_HASH_SHA1); } diff --git a/t/helper/test-tool.h b/t/helper/test-tool.h index e4890566da..29ac7b0b0d 100644 --- a/t/helper/test-tool.h +++ b/t/helper/test-tool.h @@ -50,4 +50,6 @@ int cmd__windows_named_pipe(int argc, const char **argv); #endif int cmd__write_cache(int argc, const char **argv); +int cmd_hash_impl(int ac, const char **av, int algo); + #endif