Skip to content

Commit 9ec3bfe

Browse files
committed
test: adapt modules to the new test infrastructure
This not only provides a structural improvement but also allows us to (1) specify individual tests to run and (2) execute each of them concurrently.
1 parent 48789da commit 9ec3bfe

File tree

8 files changed

+93
-106
lines changed

8 files changed

+93
-106
lines changed

src/modules/ecdh/tests_impl.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#ifndef SECP256K1_MODULE_ECDH_TESTS_H
88
#define SECP256K1_MODULE_ECDH_TESTS_H
99

10+
#include "../../unit_test.h"
11+
1012
static int ecdh_hash_function_test_xpassthru(unsigned char *output, const unsigned char *x, const unsigned char *y, void *data) {
1113
(void)y;
1214
(void)data;
@@ -182,12 +184,13 @@ static void test_ecdh_wycheproof(void) {
182184
}
183185
}
184186

185-
static void run_ecdh_tests(void) {
186-
test_ecdh_api();
187-
test_ecdh_generator_basepoint();
188-
test_bad_scalar();
189-
test_result_basepoint();
190-
test_ecdh_wycheproof();
191-
}
187+
/* --- Test registry --- */
188+
static const struct tf_test_entry tests_ecdh[] = {
189+
CASE1(test_ecdh_api),
190+
CASE1(test_ecdh_generator_basepoint),
191+
CASE1(test_bad_scalar),
192+
CASE1(test_result_basepoint),
193+
CASE1(test_ecdh_wycheproof),
194+
};
192195

193196
#endif /* SECP256K1_MODULE_ECDH_TESTS_H */

src/modules/ellswift/tests_impl.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#define SECP256K1_MODULE_ELLSWIFT_TESTS_H
88

99
#include "../../../include/secp256k1_ellswift.h"
10+
#include "../../unit_test.h"
1011

1112
struct ellswift_xswiftec_inv_test {
1213
int enc_bitmap;
@@ -433,4 +434,10 @@ void run_ellswift_tests(void) {
433434
}
434435
}
435436

437+
/* --- Test registry --- */
438+
/* TODO: subdivide test in cases */
439+
static const struct tf_test_entry tests_ellswift[] = {
440+
CASE(ellswift_tests),
441+
};
442+
436443
#endif

src/modules/extrakeys/tests_impl.h

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define SECP256K1_MODULE_EXTRAKEYS_TESTS_H
99

1010
#include "../../../include/secp256k1_extrakeys.h"
11+
#include "../../unit_test.h"
1112

1213
static void test_xonly_pubkey(void) {
1314
secp256k1_pubkey pk;
@@ -467,17 +468,17 @@ static void test_keypair_add(void) {
467468
}
468469
}
469470

470-
static void run_extrakeys_tests(void) {
471+
/* --- Test registry --- */
472+
static const struct tf_test_entry tests_extrakeys[] = {
471473
/* xonly key test cases */
472-
test_xonly_pubkey();
473-
test_xonly_pubkey_tweak();
474-
test_xonly_pubkey_tweak_check();
475-
test_xonly_pubkey_tweak_recursive();
476-
test_xonly_pubkey_comparison();
477-
474+
CASE1(test_xonly_pubkey),
475+
CASE1(test_xonly_pubkey_tweak),
476+
CASE1(test_xonly_pubkey_tweak_check),
477+
CASE1(test_xonly_pubkey_tweak_recursive),
478+
CASE1(test_xonly_pubkey_comparison),
478479
/* keypair tests */
479-
test_keypair();
480-
test_keypair_add();
481-
}
480+
CASE1(test_keypair),
481+
CASE1(test_keypair_add),
482+
};
482483

483484
#endif

src/modules/musig/tests_impl.h

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "../../group.h"
2121
#include "../../hash.h"
2222
#include "../../util.h"
23+
#include "../../unit_test.h"
2324

2425
#include "vectors.h"
2526

@@ -36,7 +37,7 @@ static int create_keypair_and_pk(secp256k1_keypair *keypair, secp256k1_pubkey *p
3637

3738
/* Just a simple (non-tweaked) 2-of-2 MuSig aggregate, sign, verify
3839
* test. */
39-
static void musig_simple_test(void) {
40+
static void musig_simple_test_internal(void) {
4041
unsigned char sk[2][32];
4142
secp256k1_keypair keypair[2];
4243
secp256k1_musig_pubnonce pubnonce[2];
@@ -629,7 +630,7 @@ static void musig_tweak_test_helper(const secp256k1_xonly_pubkey* agg_pk, const
629630

630631
/* Create aggregate public key P[0], tweak multiple times (using xonly and
631632
* plain tweaking) and test signing. */
632-
static void musig_tweak_test(void) {
633+
static void musig_tweak_test_internal(void) {
633634
unsigned char sk[2][32];
634635
secp256k1_pubkey pk[2];
635636
const secp256k1_pubkey *pk_ptr[2];
@@ -1114,28 +1115,24 @@ static void musig_test_static_nonce_gen_counter(void) {
11141115
CHECK(secp256k1_memcmp_var(pubnonce66, expected_pubnonce, sizeof(pubnonce66)) == 0);
11151116
}
11161117

1117-
static void run_musig_tests(void) {
1118-
int i;
1119-
1120-
for (i = 0; i < COUNT; i++) {
1121-
musig_simple_test();
1122-
}
1123-
musig_api_tests();
1124-
musig_nonce_test();
1125-
for (i = 0; i < COUNT; i++) {
1126-
/* Run multiple times to ensure that pk and nonce have different y
1127-
* parities */
1128-
musig_tweak_test();
1129-
}
1130-
sha256_tag_test();
1131-
musig_test_vectors_keyagg();
1132-
musig_test_vectors_noncegen();
1133-
musig_test_vectors_nonceagg();
1134-
musig_test_vectors_signverify();
1135-
musig_test_vectors_tweak();
1136-
musig_test_vectors_sigagg();
1137-
1138-
musig_test_static_nonce_gen_counter();
1139-
}
1118+
/* --- Test registry --- */
1119+
REPEAT_TEST(musig_simple_test)
1120+
/* Run multiple times to ensure that pk and nonce have different y parities */
1121+
REPEAT_TEST(musig_tweak_test)
1122+
1123+
static const struct tf_test_entry tests_musig[] = {
1124+
CASE1(musig_simple_test),
1125+
CASE1(musig_api_tests),
1126+
CASE1(musig_nonce_test),
1127+
CASE1(musig_tweak_test),
1128+
CASE1(sha256_tag_test),
1129+
CASE1(musig_test_vectors_keyagg),
1130+
CASE1(musig_test_vectors_noncegen),
1131+
CASE1(musig_test_vectors_nonceagg),
1132+
CASE1(musig_test_vectors_signverify),
1133+
CASE1(musig_test_vectors_tweak),
1134+
CASE1(musig_test_vectors_sigagg),
1135+
CASE1(musig_test_static_nonce_gen_counter),
1136+
};
11401137

11411138
#endif

src/modules/recovery/tests_impl.h

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#ifndef SECP256K1_MODULE_RECOVERY_TESTS_H
88
#define SECP256K1_MODULE_RECOVERY_TESTS_H
99

10+
#include "../../unit_test.h"
11+
1012
static int recovery_test_nonce_function(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, void *data, unsigned int counter) {
1113
(void) msg32;
1214
(void) key32;
@@ -28,7 +30,7 @@ static int recovery_test_nonce_function(unsigned char *nonce32, const unsigned c
2830
return testrand_bits(1);
2931
}
3032

31-
static void test_ecdsa_recovery_api(void) {
33+
static void test_ecdsa_recovery_api_internal(void) {
3234
/* Setup contexts that just count errors */
3335
secp256k1_pubkey pubkey;
3436
secp256k1_pubkey recpubkey;
@@ -92,7 +94,7 @@ static void test_ecdsa_recovery_api(void) {
9294
CHECK(secp256k1_ecdsa_recoverable_signature_parse_compact(CTX, &recsig, sig, recid) == 0);
9395
}
9496

95-
static void test_ecdsa_recovery_end_to_end(void) {
97+
static void test_ecdsa_recovery_end_to_end_internal(void) {
9698
unsigned char extra[32] = {0x00};
9799
unsigned char privkey[32];
98100
unsigned char message[32];
@@ -324,15 +326,14 @@ static void test_ecdsa_recovery_edge_cases(void) {
324326
}
325327
}
326328

327-
static void run_recovery_tests(void) {
328-
int i;
329-
for (i = 0; i < COUNT; i++) {
330-
test_ecdsa_recovery_api();
331-
}
332-
for (i = 0; i < 64*COUNT; i++) {
333-
test_ecdsa_recovery_end_to_end();
334-
}
335-
test_ecdsa_recovery_edge_cases();
336-
}
329+
/* --- Test registry --- */
330+
REPEAT_TEST(test_ecdsa_recovery_api)
331+
REPEAT_TEST_MULT(test_ecdsa_recovery_end_to_end, 64)
332+
333+
static const struct tf_test_entry tests_recovery[] = {
334+
CASE1(test_ecdsa_recovery_api),
335+
CASE1(test_ecdsa_recovery_end_to_end),
336+
CASE1(test_ecdsa_recovery_edge_cases)
337+
};
337338

338339
#endif /* SECP256K1_MODULE_RECOVERY_TESTS_H */

src/modules/schnorrsig/tests_impl.h

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define SECP256K1_MODULE_SCHNORRSIG_TESTS_H
99

1010
#include "../../../include/secp256k1_schnorrsig.h"
11+
#include "../../unit_test.h"
1112

1213
/* Checks that a bit flip in the n_flip-th argument (that has n_bytes many
1314
* bytes) changes the hash function
@@ -802,7 +803,7 @@ static int nonce_function_overflowing(unsigned char *nonce32, const unsigned cha
802803
return 1;
803804
}
804805

805-
static void test_schnorrsig_sign(void) {
806+
static void test_schnorrsig_sign_internal(void) {
806807
unsigned char sk[32];
807808
secp256k1_xonly_pubkey pk;
808809
secp256k1_keypair keypair;
@@ -852,7 +853,7 @@ static void test_schnorrsig_sign(void) {
852853
/* Creates N_SIGS valid signatures and verifies them with verify and
853854
* verify_batch (TODO). Then flips some bits and checks that verification now
854855
* fails. */
855-
static void test_schnorrsig_sign_verify(void) {
856+
static void test_schnorrsig_sign_verify_internal(void) {
856857
unsigned char sk[32];
857858
unsigned char msg[N_SIGS][32];
858859
unsigned char sig[N_SIGS][64];
@@ -965,18 +966,18 @@ static void test_schnorrsig_taproot(void) {
965966
CHECK(secp256k1_xonly_pubkey_tweak_add_check(CTX, output_pk_bytes, pk_parity, &internal_pk, tweak) == 1);
966967
}
967968

968-
static void run_schnorrsig_tests(void) {
969-
int i;
970-
run_nonce_function_bip340_tests();
971-
972-
test_schnorrsig_api();
973-
test_schnorrsig_sha256_tagged();
974-
test_schnorrsig_bip_vectors();
975-
for (i = 0; i < COUNT; i++) {
976-
test_schnorrsig_sign();
977-
test_schnorrsig_sign_verify();
978-
}
979-
test_schnorrsig_taproot();
980-
}
969+
/* --- Test registry --- */
970+
REPEAT_TEST(test_schnorrsig_sign)
971+
REPEAT_TEST(test_schnorrsig_sign_verify)
972+
973+
static const struct tf_test_entry tests_schnorrsig[] = {
974+
CASE(nonce_function_bip340_tests),
975+
CASE1(test_schnorrsig_api),
976+
CASE1(test_schnorrsig_sha256_tagged),
977+
CASE1(test_schnorrsig_bip_vectors),
978+
CASE1(test_schnorrsig_sign),
979+
CASE1(test_schnorrsig_sign_verify),
980+
CASE1(test_schnorrsig_taproot),
981+
};
981982

982983
#endif

src/tests.c

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7755,12 +7755,6 @@ static const struct tf_test_entry tests_ec[] = {
77557755
CASE(eckey_negate_test),
77567756
};
77577757

7758-
#ifdef ENABLE_MODULE_ECDH
7759-
static const struct tf_test_entry tests_ecdh[] = {
7760-
CASE(ecdh_tests),
7761-
};
7762-
#endif
7763-
77647758
static const struct tf_test_entry tests_ecdsa[] = {
77657759
CASE(ec_illegal_argument_tests),
77667760
CASE(pubkey_comparison),
@@ -7773,37 +7767,6 @@ static const struct tf_test_entry tests_ecdsa[] = {
77737767
CASE(ecdsa_wycheproof),
77747768
};
77757769

7776-
#ifdef ENABLE_MODULE_RECOVERY
7777-
static const struct tf_test_entry tests_recovery[] = {
7778-
/* ECDSA pubkey recovery tests */
7779-
CASE(recovery_tests),
7780-
};
7781-
#endif
7782-
7783-
#ifdef ENABLE_MODULE_EXTRAKEYS
7784-
static const struct tf_test_entry tests_extrakeys[] = {
7785-
CASE(extrakeys_tests),
7786-
};
7787-
#endif
7788-
7789-
#ifdef ENABLE_MODULE_SCHNORRSIG
7790-
static const struct tf_test_entry tests_schnorrsig[] = {
7791-
CASE(schnorrsig_tests),
7792-
};
7793-
#endif
7794-
7795-
#ifdef ENABLE_MODULE_MUSIG
7796-
static const struct tf_test_entry tests_musig[] = {
7797-
CASE(musig_tests),
7798-
};
7799-
#endif
7800-
7801-
#ifdef ENABLE_MODULE_ELLSWIFT
7802-
static const struct tf_test_entry tests_ellswift[] = {
7803-
CASE(ellswift_tests),
7804-
};
7805-
#endif
7806-
78077770
static const struct tf_test_entry tests_utils[] = {
78087771
CASE(hsort_tests),
78097772
CASE(secp256k1_memczero_test),
@@ -7827,6 +7790,7 @@ static const struct tf_test_module registry_modules[] = {
78277790
#endif
78287791
MAKE_TEST_MODULE(ecdsa),
78297792
#ifdef ENABLE_MODULE_RECOVERY
7793+
/* ECDSA pubkey recovery tests */
78307794
MAKE_TEST_MODULE(recovery),
78317795
#endif
78327796
#ifdef ENABLE_MODULE_EXTRAKEYS

src/unit_test.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,26 @@
2222
/* --------------------------------------------------------- */
2323

2424
#define CASE(name) { #name, run_##name }
25+
#define CASE1(name) { #name, name }
2526

2627
#define MAKE_TEST_MODULE(name) { \
2728
#name, \
2829
tests_##name, \
2930
sizeof(tests_##name) / sizeof(tests_##name[0]) \
3031
}
3132

33+
/* Macro to wrap a test internal function with a COUNT loop (iterations number) */
34+
#define REPEAT_TEST(fn) REPEAT_TEST_MULT(fn, 1)
35+
#define REPEAT_TEST_MULT(fn, multiplier) \
36+
static void fn(void) { \
37+
int i; \
38+
int repeat = COUNT * (multiplier); \
39+
for (i = 0; i < repeat; i++) \
40+
fn##_internal(); \
41+
}
42+
43+
44+
3245
/* --------------------------------------------------------- */
3346
/* Test Framework API */
3447
/* --------------------------------------------------------- */

0 commit comments

Comments
 (0)