diff --git a/src/tests.c b/src/tests.c index 22bc042b67..dae379a87b 100644 --- a/src/tests.c +++ b/src/tests.c @@ -44,11 +44,35 @@ static int all_bytes_equal(const void* s, unsigned char value, size_t n) { return 1; } +/* TODO Use CHECK_ILLEGAL(_VOID) everywhere and get rid of the uncounting callback */ +/* For checking functions that use ARG_CHECK */ +#define CHECK_ILLEGAL(ctx, expr) do { \ + /* long descriptive variable name to avoid accidental shadowing and have clear error message */ \ + int32_t count_of_calls_to_illegal_callback = 0; \ + secp256k1_context_set_illegal_callback(ctx, \ + counting_illegal_callback_fn, &count_of_calls_to_illegal_callback); \ + { CHECK((expr) == 0); } \ + secp256k1_context_set_illegal_callback(ctx, NULL, NULL); \ + CHECK(count_of_calls_to_illegal_callback == 1); \ +} while(0); + +/* For checking functions that use ARG_CHECK_VOID */ +#define CHECK_ILLEGAL_VOID(ctx, expr) do { \ + /* long descriptive variable name to avoid accidental shadowing and have clear error message */ \ + int32_t count_of_calls_to_illegal_callback = 0; \ + secp256k1_context_set_illegal_callback(ctx, \ + counting_illegal_callback_fn, &count_of_calls_to_illegal_callback); \ + { (expr); } \ + secp256k1_context_set_illegal_callback(ctx, NULL, NULL); \ + CHECK(count_of_calls_to_illegal_callback == 1); \ +} while(0); + static void counting_illegal_callback_fn(const char* str, void* data) { /* Dummy callback function that just counts. */ int32_t *p; (void)str; p = data; + CHECK(*p != INT32_MAX); (*p)++; } @@ -57,6 +81,7 @@ static void uncounting_illegal_callback_fn(const char* str, void* data) { int32_t *p; (void)str; p = data; + CHECK(*p != INT32_MIN); (*p)--; } @@ -246,39 +271,28 @@ static void run_static_context_tests(int use_prealloc) { CHECK(secp256k1_context_no_precomp == secp256k1_context_static); { - int ecount = 0; unsigned char seed[32] = {0x17}; - secp256k1_context_set_illegal_callback(STATIC_CTX, counting_illegal_callback_fn, &ecount); /* Randomizing secp256k1_context_static is not supported. */ - CHECK(secp256k1_context_randomize(STATIC_CTX, seed) == 0); - CHECK(ecount == 1); - CHECK(secp256k1_context_randomize(STATIC_CTX, NULL) == 0); - CHECK(ecount == 2); - ecount = 0; + CHECK_ILLEGAL(STATIC_CTX, secp256k1_context_randomize(STATIC_CTX, seed)); + CHECK_ILLEGAL(STATIC_CTX, secp256k1_context_randomize(STATIC_CTX, NULL)); /* Destroying or cloning secp256k1_context_static is not supported. */ if (use_prealloc) { - CHECK(secp256k1_context_preallocated_clone_size(STATIC_CTX) == 0); - CHECK(ecount == 1); + CHECK_ILLEGAL(STATIC_CTX, secp256k1_context_preallocated_clone_size(STATIC_CTX)); { secp256k1_context *my_static_ctx = malloc(sizeof(*STATIC_CTX)); CHECK(my_static_ctx != NULL); memset(my_static_ctx, 0x2a, sizeof(*my_static_ctx)); - CHECK(secp256k1_context_preallocated_clone(STATIC_CTX, my_static_ctx) == NULL); + CHECK_ILLEGAL(STATIC_CTX, secp256k1_context_preallocated_clone(STATIC_CTX, my_static_ctx)); CHECK(all_bytes_equal(my_static_ctx, 0x2a, sizeof(*my_static_ctx))); - CHECK(ecount == 2); free(my_static_ctx); } - secp256k1_context_preallocated_destroy(STATIC_CTX); - CHECK(ecount == 3); + CHECK_ILLEGAL_VOID(STATIC_CTX, secp256k1_context_preallocated_destroy(STATIC_CTX)); } else { - CHECK(secp256k1_context_clone(STATIC_CTX) == NULL); - CHECK(ecount == 1); - secp256k1_context_destroy(STATIC_CTX); - CHECK(ecount == 2); + CHECK_ILLEGAL(STATIC_CTX, secp256k1_context_clone(STATIC_CTX)); + CHECK_ILLEGAL_VOID(STATIC_CTX, secp256k1_context_destroy(STATIC_CTX)); } - secp256k1_context_set_illegal_callback(STATIC_CTX, NULL, NULL); } {