Skip to content

Commit

Permalink
tests: add "help" command line flag and output
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasnick committed Feb 9, 2023
1 parent 5fbff5d commit f46a31e
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 13 deletions.
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ noinst_HEADERS += src/precomputed_ecmult_gen.h
noinst_HEADERS += src/assumptions.h
noinst_HEADERS += src/checkmem.h
noinst_HEADERS += src/util.h
noinst_HEADERS += src/cli_util.h
noinst_HEADERS += src/int128.h
noinst_HEADERS += src/int128_impl.h
noinst_HEADERS += src/int128_native.h
Expand Down
1 change: 1 addition & 0 deletions src/bench.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "../include/secp256k1.h"
#include "util.h"
#include "cli_util.h"
#include "bench.h"

static void help(int default_iters) {
Expand Down
12 changes: 0 additions & 12 deletions src/bench.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,18 +129,6 @@ static void run_benchmark(char *name, void (*benchmark)(void*, int), void (*setu
printf("\n");
}

static int have_flag(int argc, char** argv, char *flag) {
char** argm = argv + argc;
argv++;
while (argv != argm) {
if (strcmp(*argv, flag) == 0) {
return 1;
}
argv++;
}
return 0;
}

/* takes an array containing the arguments that the user is allowed to enter on the command-line
returns:
- 1 if the user entered an invalid argument
Expand Down
1 change: 1 addition & 0 deletions src/bench_ecmult.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "../include/secp256k1.h"

#include "util.h"
#include "cli_util.h"
#include "hash_impl.h"
#include "field_impl.h"
#include "group_impl.h"
Expand Down
1 change: 1 addition & 0 deletions src/bench_internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "assumptions.h"
#include "util.h"
#include "cli_util.h"
#include "hash_impl.h"
#include "field_impl.h"
#include "group_impl.h"
Expand Down
24 changes: 24 additions & 0 deletions src/cli_util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/***********************************************************************
* Copyright (c) 2023 Jonas Nick *
* Distributed under the MIT software license, see the accompanying *
* file COPYING or https://www.opensource.org/licenses/mit-license.php.*
***********************************************************************/

#ifndef SECP256K1_CLI_UTIL_H
#define SECP256K1_CLI_UTIL_H

#include <string.h>

static int have_flag(int argc, char** argv, char *flag) {
char** argm = argv + argc;
argv++;
while (argv != argm) {
if (strcmp(*argv, flag) == 0) {
return 1;
}
argv++;
}
return 0;
}

#endif /* SECP256K1_CLI_UTIL_H */
28 changes: 27 additions & 1 deletion src/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "testrand_impl.h"
#include "checkmem.h"
#include "util.h"
#include "cli_util.h"

#include "../contrib/lax_der_parsing.c"
#include "../contrib/lax_der_privatekey_parsing.c"
Expand All @@ -28,7 +29,8 @@

#define CONDITIONAL_TEST(cnt, nam) if (COUNT < (cnt)) { printf("Skipping %s (iteration count too low)\n", nam); } else

static int COUNT = 64;
static const int DEFAULT_COUNT = 64;
static int COUNT = DEFAULT_COUNT;
static secp256k1_context *CTX = NULL;
static secp256k1_context *STATIC_CTX = NULL;

Expand Down Expand Up @@ -7428,6 +7430,21 @@ static void run_cmov_tests(void) {
ge_storage_cmov_test();
}

static void help(void) {
printf("The command ./tests runs a test suite on the code base.\n");
printf("\n");
printf("Some randomized tests are run for a certain number of iterations,\n");
printf("which is set to %d by default. This number can be altered by either\n", DEFAULT_COUNT);
printf("setting the environment variable SECP256K1_TEST_ITERS or by providing\n");
printf("the iteration count as a command line argument.\n");
printf("\n");
printf("Usage: ./tests [args]\n");
printf("Available arguments:\n");
printf(" help : display this help message and exit\n");
printf(" <count> : set the iteration count to <count>\n");
printf("\n");
}

int main(int argc, char **argv) {
/* Disable buffering for stdout to improve reliability of getting
* diagnostic information. Happens right at the start of main because
Expand All @@ -7437,6 +7454,15 @@ int main(int argc, char **argv) {
* unbuffered on all systems. */
setbuf(stderr, NULL);

if (argc > 1) {
if (have_flag(argc, argv, "-h")
|| have_flag(argc, argv, "--help")
|| have_flag(argc, argv, "help")) {
help();
return 0;
}
}

/* find iteration count */
if (argc > 1) {
COUNT = strtol(argv[1], NULL, 0);
Expand Down

0 comments on commit f46a31e

Please sign in to comment.