Skip to content

Commit 94d4b81

Browse files
committed
tests: add CHECK_ERROR_VOID and use it in scratch tests
1 parent 4309074 commit 94d4b81

File tree

2 files changed

+26
-34
lines changed

2 files changed

+26
-34
lines changed

src/modules/extrakeys/tests_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ static void test_xonly_pubkey_comparison(void) {
132132
CHECK_ILLEGAL_VOID(CTX, CHECK(secp256k1_xonly_pubkey_cmp(CTX, &pk1, &pk2) < 0));
133133
{
134134
int32_t ecount = 0;
135-
secp256k1_context_set_illegal_callback(CTX, counting_illegal_callback_fn, &ecount);
135+
secp256k1_context_set_illegal_callback(CTX, counting_callback_fn, &ecount);
136136
CHECK(secp256k1_xonly_pubkey_cmp(CTX, &pk1, &pk1) == 0);
137137
CHECK(ecount == 2);
138138
secp256k1_context_set_illegal_callback(CTX, NULL, NULL);

src/tests.c

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -52,25 +52,29 @@ static int all_bytes_equal(const void* s, unsigned char value, size_t n) {
5252
return 1;
5353
}
5454

55-
/* CHECK that expr_or_stmt calls the illegal callback of ctx exactly once
56-
*
57-
* For checking functions that use ARG_CHECK_VOID */
58-
#define CHECK_ILLEGAL_VOID(ctx, expr_or_stmt) do { \
59-
int32_t _calls_to_illegal_callback = 0; \
60-
secp256k1_callback _saved_illegal_cb = ctx->illegal_callback; \
61-
secp256k1_context_set_illegal_callback(ctx, \
62-
counting_illegal_callback_fn, &_calls_to_illegal_callback); \
55+
#define CHECK_COUNTING_CALLBACK_VOID(ctx, expr_or_stmt, callback, callback_setter) do { \
56+
int32_t _calls_to_callback = 0; \
57+
secp256k1_callback _saved_callback = ctx->callback; \
58+
callback_setter(ctx, counting_callback_fn, &_calls_to_callback); \
6359
{ expr_or_stmt; } \
64-
ctx->illegal_callback = _saved_illegal_cb; \
65-
CHECK(_calls_to_illegal_callback == 1); \
60+
ctx->callback = _saved_callback; \
61+
CHECK(_calls_to_callback == 1); \
6662
} while(0);
6763

64+
/* CHECK that expr_or_stmt calls the error or illegal callback of ctx exactly once
65+
*
66+
* For checking functions that use ARG_CHECK_VOID */
67+
#define CHECK_ERROR_VOID(ctx, expr_or_stmt) \
68+
CHECK_COUNTING_CALLBACK_VOID(ctx, expr_or_stmt, error_callback, secp256k1_context_set_error_callback)
69+
#define CHECK_ILLEGAL_VOID(ctx, expr_or_stmt) \
70+
CHECK_COUNTING_CALLBACK_VOID(ctx, expr_or_stmt, illegal_callback, secp256k1_context_set_illegal_callback)
71+
6872
/* CHECK that expr calls the illegal callback of ctx exactly once and that expr == 0
6973
*
7074
* For checking functions that use ARG_CHECK */
7175
#define CHECK_ILLEGAL(ctx, expr) CHECK_ILLEGAL_VOID(ctx, CHECK((expr) == 0))
7276

73-
static void counting_illegal_callback_fn(const char* str, void* data) {
77+
static void counting_callback_fn(const char* str, void* data) {
7478
/* Dummy callback function that just counts. */
7579
int32_t *p;
7680
(void)str;
@@ -301,8 +305,8 @@ static void run_static_context_tests(int use_prealloc) {
301305
{
302306
/* Verify that setting and resetting illegal callback works */
303307
int32_t dummy = 0;
304-
secp256k1_context_set_illegal_callback(STATIC_CTX, counting_illegal_callback_fn, &dummy);
305-
CHECK(STATIC_CTX->illegal_callback.fn == counting_illegal_callback_fn);
308+
secp256k1_context_set_illegal_callback(STATIC_CTX, counting_callback_fn, &dummy);
309+
CHECK(STATIC_CTX->illegal_callback.fn == counting_callback_fn);
306310
CHECK(STATIC_CTX->illegal_callback.data == &dummy);
307311
secp256k1_context_set_illegal_callback(STATIC_CTX, NULL, NULL);
308312
CHECK(STATIC_CTX->illegal_callback.fn == secp256k1_default_illegal_callback_fn);
@@ -393,8 +397,8 @@ static void run_proper_context_tests(int use_prealloc) {
393397
CHECK(context_eq(my_ctx, my_ctx_fresh));
394398

395399
/* Verify that setting and resetting illegal callback works */
396-
secp256k1_context_set_illegal_callback(my_ctx, counting_illegal_callback_fn, &dummy);
397-
CHECK(my_ctx->illegal_callback.fn == counting_illegal_callback_fn);
400+
secp256k1_context_set_illegal_callback(my_ctx, counting_callback_fn, &dummy);
401+
CHECK(my_ctx->illegal_callback.fn == counting_callback_fn);
398402
CHECK(my_ctx->illegal_callback.data == &dummy);
399403
secp256k1_context_set_illegal_callback(my_ctx, NULL, NULL);
400404
CHECK(my_ctx->illegal_callback.fn == secp256k1_default_illegal_callback_fn);
@@ -435,18 +439,14 @@ static void run_proper_context_tests(int use_prealloc) {
435439
static void run_scratch_tests(void) {
436440
const size_t adj_alloc = ((500 + ALIGNMENT - 1) / ALIGNMENT) * ALIGNMENT;
437441

438-
int32_t ecount = 0;
439442
size_t checkpoint;
440443
size_t checkpoint_2;
441444
secp256k1_scratch_space *scratch;
442445
secp256k1_scratch_space local_scratch;
443446

444-
secp256k1_context_set_error_callback(CTX, counting_illegal_callback_fn, &ecount);
445-
446447
/* Test public API */
447448
scratch = secp256k1_scratch_space_create(CTX, 1000);
448449
CHECK(scratch != NULL);
449-
CHECK(ecount == 0);
450450

451451
/* Test internal API */
452452
CHECK(secp256k1_scratch_max_allocation(&CTX->error_callback, scratch, 0) == 1000);
@@ -479,22 +479,16 @@ static void run_scratch_tests(void) {
479479
/* try to apply a bad checkpoint */
480480
checkpoint_2 = secp256k1_scratch_checkpoint(&CTX->error_callback, scratch);
481481
secp256k1_scratch_apply_checkpoint(&CTX->error_callback, scratch, checkpoint);
482-
CHECK(ecount == 0);
483-
secp256k1_scratch_apply_checkpoint(&CTX->error_callback, scratch, checkpoint_2); /* checkpoint_2 is after checkpoint */
484-
CHECK(ecount == 1);
485-
secp256k1_scratch_apply_checkpoint(&CTX->error_callback, scratch, (size_t) -1); /* this is just wildly invalid */
486-
CHECK(ecount == 2);
482+
CHECK_ERROR_VOID(CTX, secp256k1_scratch_apply_checkpoint(&CTX->error_callback, scratch, checkpoint_2)); /* checkpoint_2 is after checkpoint */
483+
CHECK_ERROR_VOID(CTX, secp256k1_scratch_apply_checkpoint(&CTX->error_callback, scratch, (size_t) -1)); /* this is just wildly invalid */
487484

488485
/* try to use badly initialized scratch space */
489486
secp256k1_scratch_space_destroy(CTX, scratch);
490487
memset(&local_scratch, 0, sizeof(local_scratch));
491488
scratch = &local_scratch;
492-
CHECK(!secp256k1_scratch_max_allocation(&CTX->error_callback, scratch, 0));
493-
CHECK(ecount == 3);
494-
CHECK(secp256k1_scratch_alloc(&CTX->error_callback, scratch, 500) == NULL);
495-
CHECK(ecount == 4);
496-
secp256k1_scratch_space_destroy(CTX, scratch);
497-
CHECK(ecount == 5);
489+
CHECK_ERROR_VOID(CTX, CHECK(!secp256k1_scratch_max_allocation(&CTX->error_callback, scratch, 0)));
490+
CHECK_ERROR_VOID(CTX, CHECK(secp256k1_scratch_alloc(&CTX->error_callback, scratch, 500) == NULL));
491+
CHECK_ERROR_VOID(CTX, secp256k1_scratch_space_destroy(CTX, scratch));
498492

499493
/* Test that large integers do not wrap around in a bad way */
500494
scratch = secp256k1_scratch_space_create(CTX, 1000);
@@ -510,8 +504,6 @@ static void run_scratch_tests(void) {
510504

511505
/* cleanup */
512506
secp256k1_scratch_space_destroy(CTX, NULL); /* no-op */
513-
514-
secp256k1_context_set_error_callback(CTX, NULL, NULL);
515507
}
516508

517509
static void run_ctz_tests(void) {
@@ -6606,7 +6598,7 @@ static void run_pubkey_comparison(void) {
66066598
CHECK_ILLEGAL_VOID(CTX, CHECK(secp256k1_ec_pubkey_cmp(CTX, &pk_tmp, &pk2) < 0));
66076599
{
66086600
int32_t ecount = 0;
6609-
secp256k1_context_set_illegal_callback(CTX, counting_illegal_callback_fn, &ecount);
6601+
secp256k1_context_set_illegal_callback(CTX, counting_callback_fn, &ecount);
66106602
CHECK(secp256k1_ec_pubkey_cmp(CTX, &pk_tmp, &pk_tmp) == 0);
66116603
CHECK(ecount == 2);
66126604
secp256k1_context_set_illegal_callback(CTX, NULL, NULL);

0 commit comments

Comments
 (0)