From 1df332b3ee5688a62833f25c108b470f2a4f7dc9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Niels=20M=C3=B6ller?= Date: Fri, 27 Jun 2025 20:54:27 +0200 Subject: [PATCH] New testutils function open_srcdir_file. --- ChangeLog | 6 ++++++ testsuite/testutils.c | 27 +++++++++++++++++++++++++++ testsuite/testutils.h | 3 +++ testsuite/yarrow-test.c | 34 +--------------------------------- 4 files changed, 37 insertions(+), 33 deletions(-) diff --git a/ChangeLog b/ChangeLog index 5c4898eb..509144ae 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2025-06-27 Niels Möller + + * testsuite/testutils.c (open_srcdir_file): New function. + * testsuite/yarrow-test.c (open_file): Deleted function. + (test_main): Use open_srcdir_file instead. + 2025-06-26 Niels Möller Cherry-picked from branch nettle-3.10-fixes: diff --git a/testsuite/testutils.c b/testsuite/testutils.c index 9ab354a3..613bffd1 100644 --- a/testsuite/testutils.c +++ b/testsuite/testutils.c @@ -122,6 +122,33 @@ print_hex(size_t length, const uint8_t *data) printf("\n"); } +/* Tries opening the file in $srcdir, if set, otherwise the current + * working directory */ +FILE * +open_srcdir_file (const char *name) +{ + const char *srcdir = getenv("srcdir"); + FILE *f; + if (srcdir && srcdir[0]) + { + size_t size = strlen(name) + strlen(srcdir) + 2; + char *buf = xalloc(size); + snprintf(buf, size, "%s/%s", srcdir, name); + + f = fopen(buf, "r"); + free(buf); + } + else + f = fopen(name, "r"); + + if (!f) + { + fprintf(stderr, "Failed to open '%s': %s\n", name, strerror(errno)); + FAIL(); + } + return f; +} + int verbose = 0; int test_side_channel = 0; diff --git a/testsuite/testutils.h b/testsuite/testutils.h index 49b813ab..34090aa7 100644 --- a/testsuite/testutils.h +++ b/testsuite/testutils.h @@ -68,6 +68,9 @@ tstring_print_hex(const struct tstring *s); void print_hex(size_t length, const uint8_t *data); +FILE * +open_srcdir_file (const char *name); + /* If side-channel tests are requested, attach valgrind annotations on given memory area. */ void diff --git a/testsuite/yarrow-test.c b/testsuite/yarrow-test.c index 2310dbb9..dfb9ef1d 100644 --- a/testsuite/yarrow-test.c +++ b/testsuite/yarrow-test.c @@ -4,9 +4,6 @@ #include "macros.h" -#include -#include -#include #include #include @@ -36,28 +33,6 @@ get_event(FILE *f, struct sha256_ctx *hash, return 1; } -static FILE * -open_file(const char *name) -{ - /* Tries opening the file in $srcdir, if set, otherwise the current - * working directory */ - - const char *srcdir = getenv("srcdir"); - if (srcdir && srcdir[0]) - { - FILE *f; - char *buf = xalloc(strlen(name) + strlen(srcdir) + 10); - sprintf(buf, "%s/%s", srcdir, name); - - f = fopen(buf, "r"); - free(buf); - return f; - } - - /* Opens the file in text mode. */ - return fopen(name, "r"); -} - void test_main(void) { @@ -112,15 +87,8 @@ test_main(void) ASSERT(!yarrow256_is_seeded(&yarrow)); - input = open_file("gold-bug.txt"); + input = open_srcdir_file("gold-bug.txt"); - if (!input) - { - fprintf(stderr, "Couldn't open `gold-bug.txt', errno = %d\n", - errno); - FAIL(); - } - while (get_event(input, &input_hash, &c, &t)) { uint8_t buf[8]; -- 2.47.2