Skip to content

Commit 3e404f2

Browse files
committed
tests: allow user to select tests via command line args
1 parent 38dee2e commit 3e404f2

File tree

3 files changed

+159
-70
lines changed

3 files changed

+159
-70
lines changed

src/bench.h

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include <stdlib.h>
1111
#include <stdint.h>
1212
#include <stdio.h>
13-
#include <string.h>
1413

1514
#if (defined(_MSC_VER) && _MSC_VER >= 1900)
1615
# include <time.h>
@@ -79,7 +78,7 @@ static void print_number(const int64_t x) {
7978
y /= 10;
8079
}
8180
} else if (c == 0) { /* fractional part is 0 */
82-
buffer[--ptr] = '0';
81+
buffer[--ptr] = '0';
8382
}
8483
buffer[--ptr] = '.';
8584
do {
@@ -129,32 +128,6 @@ static void run_benchmark(char *name, void (*benchmark)(void*, int), void (*setu
129128
printf("\n");
130129
}
131130

132-
/* takes an array containing the arguments that the user is allowed to enter on the command-line
133-
returns:
134-
- 1 if the user entered an invalid argument
135-
- 0 if all the user entered arguments are valid */
136-
static int have_invalid_args(int argc, char** argv, char** valid_args, size_t n) {
137-
size_t i;
138-
int found_valid;
139-
char** argm = argv + argc;
140-
argv++;
141-
142-
while (argv != argm) {
143-
found_valid = 0;
144-
for (i = 0; i < n; i++) {
145-
if (strcmp(*argv, valid_args[i]) == 0) {
146-
found_valid = 1; /* user entered a valid arg from the list */
147-
break;
148-
}
149-
}
150-
if (found_valid == 0) {
151-
return 1; /* invalid arg found */
152-
}
153-
argv++;
154-
}
155-
return 0;
156-
}
157-
158131
static int get_iters(int default_iters) {
159132
char* env = getenv("SECP256K1_BENCH_ITERS");
160133
if (env) {

src/cli_util.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,30 @@ static int have_flag(int argc, char** argv, char *flag) {
2121
return 0;
2222
}
2323

24+
/* takes an array containing the arguments that the user is allowed to enter on the command-line
25+
returns:
26+
- 1 if the user entered an invalid argument
27+
- 0 if all the user entered arguments are valid */
28+
static int have_invalid_args(int argc, char** argv, char** valid_args, size_t n) {
29+
size_t i;
30+
int found_valid;
31+
char** argm = argv + argc;
32+
argv++;
33+
34+
while (argv != argm) {
35+
found_valid = 0;
36+
for (i = 0; i < n; i++) {
37+
if (strcmp(*argv, valid_args[i]) == 0) {
38+
found_valid = 1; /* user entered a valid arg from the list */
39+
break;
40+
}
41+
}
42+
if (found_valid == 0) {
43+
return 1; /* invalid arg found */
44+
}
45+
argv++;
46+
}
47+
return 0;
48+
}
49+
2450
#endif /* SECP256K1_CLI_UTIL_H */

src/tests.c

Lines changed: 132 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7430,9 +7430,13 @@ static void run_cmov_tests(void) {
74307430
ge_storage_cmov_test();
74317431
}
74327432

7433-
static int process_args(int argc, char **argv) {
7433+
static int process_args(int *all_enabled, int argc, char **argv) {
74347434
int is_count_arg_set = 0;
74357435
const char* env = getenv("SECP256K1_TEST_ITERS");
7436+
char* valid_args[] = {"integer", "hash", "scalar", "field", "group",
7437+
"ecmult", "ecdh", "ecdsa", "recovery", "extrakeys",
7438+
"schnorrsig"};
7439+
size_t valid_args_size = sizeof(valid_args)/sizeof(valid_args[0]);
74367440

74377441
if (argc > 1) {
74387442
int count_arg = strtol(argv[1], NULL, 0);
@@ -7453,9 +7457,31 @@ static int process_args(int argc, char **argv) {
74537457
return 0;
74547458
}
74557459
}
7460+
7461+
*all_enabled = argc == 1 || (argc == 2 && is_count_arg_set);
7462+
7463+
if (is_count_arg_set) {
7464+
argc--;
7465+
argv++;
7466+
}
7467+
if (have_invalid_args(argc, argv, valid_args, valid_args_size)) {
7468+
fprintf(stderr, "./tests: unrecognized argument.\n\n");
7469+
return 0;
7470+
}
7471+
74567472
return 1;
74577473
}
74587474

7475+
static int module_unavailable(int argc, char **argv, char *module) {
7476+
if (have_flag(argc, argv, module)) {
7477+
fprintf(stderr, "./tests: %s module not enabled.\n", module);
7478+
fprintf(stderr, "Use ./configure --enable-module-%s.\n\n", module);
7479+
return 1;
7480+
}
7481+
return 0;
7482+
}
7483+
7484+
74597485
static void help(void) {
74607486
printf("The command ./tests runs a test suite on the code base.\n");
74617487
printf("\n");
@@ -7464,14 +7490,36 @@ static void help(void) {
74647490
printf("setting the environment variable SECP256K1_TEST_ITERS or by providing\n");
74657491
printf("the iteration count as a command line argument.\n");
74667492
printf("\n");
7493+
printf("By default, all tests are enabled.\n");
74677494
printf("Usage: ./tests [args]\n");
74687495
printf("Available arguments:\n");
74697496
printf(" help : display this help message and exit\n");
74707497
printf(" <count> : set the iteration count to <count>\n");
7498+
printf(" integer : enable integer tests\n");
7499+
printf(" hash : enable hash tests\n");
7500+
printf(" scalar : enable scalar tests\n");
7501+
printf(" field : enable field tests\n");
7502+
printf(" group : enable group tests\n");
7503+
printf(" ecmult : enable ecmult tests\n");
7504+
#ifdef ENABLE_MODULE_ECDH
7505+
printf(" ecdh : enable ecdh tests\n");
7506+
#endif
7507+
printf(" ecdsa : enable ecdsa tests\n");
7508+
#ifdef ENABLE_MODULE_RECOVERY
7509+
printf(" recovery : enable recovery tests\n");
7510+
#endif
7511+
#ifdef ENABLE_MODULE_EXTRAKEYS
7512+
printf(" extrakeys : enable extrakeys tests\n");
7513+
#endif
7514+
#ifdef ENABLE_MODULE_SCHNORRSIG
7515+
printf(" schnorrsig : enable schnorrsig tests\n");
7516+
#endif
74717517
printf("\n");
74727518
}
74737519

74747520
int main(int argc, char **argv) {
7521+
/* This variable is set to true if all tests are enabled by the user */
7522+
int all_enabled;
74757523
/* Disable buffering for stdout to improve reliability of getting
74767524
* diagnostic information. Happens right at the start of main because
74777525
* setbuf must be used before any other operation on the stream. */
@@ -7489,7 +7537,7 @@ int main(int argc, char **argv) {
74897537
}
74907538
}
74917539

7492-
if (!process_args(argc, argv)) {
7540+
if (!process_args(&all_enabled, argc, argv)) {
74937541
help();
74947542
return EXIT_FAILURE;
74957543
}
@@ -7537,47 +7585,59 @@ int main(int argc, char **argv) {
75377585
run_rand_int();
75387586

75397587
/* integer arithmetic tests */
7588+
if (all_enabled || have_flag(argc, argv, "integer")) {
75407589
#ifdef SECP256K1_WIDEMUL_INT128
7541-
run_int128_tests();
7590+
run_int128_tests();
75427591
#endif
7543-
run_ctz_tests();
7544-
run_modinv_tests();
7545-
run_inverse_tests();
7592+
run_ctz_tests();
7593+
run_modinv_tests();
7594+
run_inverse_tests();
7595+
}
75467596

75477597
/* hash tests */
7548-
run_sha256_known_output_tests();
7549-
run_sha256_counter_tests();
7550-
run_hmac_sha256_tests();
7551-
run_rfc6979_hmac_sha256_tests();
7552-
run_tagged_sha256_tests();
7598+
if (all_enabled || have_flag(argc, argv, "hash")) {
7599+
run_sha256_known_output_tests();
7600+
run_sha256_counter_tests();
7601+
run_hmac_sha256_tests();
7602+
run_rfc6979_hmac_sha256_tests();
7603+
run_tagged_sha256_tests();
7604+
}
75537605

75547606
/* scalar tests */
7555-
run_scalar_tests();
7607+
if (all_enabled || have_flag(argc, argv, "scalar")) {
7608+
run_scalar_tests();
7609+
}
75567610

75577611
/* field tests */
7558-
run_field_half();
7559-
run_field_misc();
7560-
run_field_convert();
7561-
run_fe_mul();
7562-
run_sqr();
7563-
run_sqrt();
7612+
if (all_enabled || have_flag(argc, argv, "field")) {
7613+
run_field_half();
7614+
run_field_misc();
7615+
run_field_convert();
7616+
run_fe_mul();
7617+
run_sqr();
7618+
run_sqrt();
7619+
}
75647620

75657621
/* group tests */
7566-
run_ge();
7567-
run_gej();
7568-
run_group_decompress();
7622+
if (all_enabled || have_flag(argc, argv, "group")) {
7623+
run_ge();
7624+
run_gej();
7625+
run_group_decompress();
7626+
}
75697627

75707628
/* ecmult tests */
7571-
run_ecmult_pre_g();
7572-
run_wnaf();
7573-
run_point_times_order();
7574-
run_ecmult_near_split_bound();
7575-
run_ecmult_chain();
7576-
run_ecmult_constants();
7577-
run_ecmult_gen_blind();
7578-
run_ecmult_const_tests();
7579-
run_ecmult_multi_tests();
7580-
run_ec_combine();
7629+
if (all_enabled || have_flag(argc, argv, "ecmult")) {
7630+
run_ecmult_pre_g();
7631+
run_wnaf();
7632+
run_point_times_order();
7633+
run_ecmult_near_split_bound();
7634+
run_ecmult_chain();
7635+
run_ecmult_constants();
7636+
run_ecmult_gen_blind();
7637+
run_ecmult_const_tests();
7638+
run_ecmult_multi_tests();
7639+
run_ec_combine();
7640+
}
75817641

75827642
/* endomorphism tests */
75837643
run_endomorphism_tests();
@@ -7593,29 +7653,59 @@ int main(int argc, char **argv) {
75937653

75947654
#ifdef ENABLE_MODULE_ECDH
75957655
/* ecdh tests */
7596-
run_ecdh_tests();
7656+
if (all_enabled || have_flag(argc, argv, "ecdh")) {
7657+
run_ecdh_tests();
7658+
}
7659+
#endif
7660+
#ifndef ENABLE_MODULE_ECDH
7661+
if (module_unavailable(argc, argv, "ecdh")) {
7662+
return EXIT_FAILURE;
7663+
}
75977664
#endif
75987665

75997666
/* ecdsa tests */
7600-
run_ec_illegal_argument_tests();
7601-
run_pubkey_comparison();
7602-
run_random_pubkeys();
7603-
run_ecdsa_der_parse();
7604-
run_ecdsa_sign_verify();
7605-
run_ecdsa_end_to_end();
7606-
run_ecdsa_edge_cases();
7667+
if (all_enabled || have_flag(argc, argv, "ecdsa")) {
7668+
run_ec_illegal_argument_tests();
7669+
run_pubkey_comparison();
7670+
run_random_pubkeys();
7671+
run_ecdsa_der_parse();
7672+
run_ecdsa_sign_verify();
7673+
run_ecdsa_end_to_end();
7674+
run_ecdsa_edge_cases();
7675+
}
76077676

76087677
#ifdef ENABLE_MODULE_RECOVERY
76097678
/* ECDSA pubkey recovery tests */
7610-
run_recovery_tests();
7679+
if (all_enabled || have_flag(argc, argv, "recovery")) {
7680+
run_recovery_tests();
7681+
}
7682+
#endif
7683+
#ifndef ENABLE_MODULE_RECOVERY
7684+
if (module_unavailable(argc, argv, "recovery")) {
7685+
return EXIT_FAILURE;
7686+
}
76117687
#endif
76127688

76137689
#ifdef ENABLE_MODULE_EXTRAKEYS
7614-
run_extrakeys_tests();
7690+
if (all_enabled || have_flag(argc, argv, "extrakeys")) {
7691+
run_extrakeys_tests();
7692+
}
7693+
#endif
7694+
#ifndef ENABLE_MODULE_EXTRAKEYS
7695+
if (module_unavailable(argc, argv, "extrakeys")) {
7696+
return EXIT_FAILURE;
7697+
}
76157698
#endif
76167699

76177700
#ifdef ENABLE_MODULE_SCHNORRSIG
7618-
run_schnorrsig_tests();
7701+
if (all_enabled || have_flag(argc, argv, "schnorrsig")) {
7702+
run_schnorrsig_tests();
7703+
}
7704+
#endif
7705+
#ifndef ENABLE_MODULE_SCHNORRSIG
7706+
if (module_unavailable(argc, argv, "schnorrsig")) {
7707+
return EXIT_FAILURE;
7708+
}
76197709
#endif
76207710

76217711
/* util tests */

0 commit comments

Comments
 (0)