Skip to content

Commit

Permalink
tests: Add CHECK_ILLEGAL(_VOID) macros and use in static ctx tests
Browse files Browse the repository at this point in the history
  • Loading branch information
real-or-random committed Jan 18, 2023
1 parent 61841fc commit 6862279
Showing 1 changed file with 32 additions and 18 deletions.
50 changes: 32 additions & 18 deletions src/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)++;
}

Expand All @@ -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)--;
}

Expand Down Expand Up @@ -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);
}

{
Expand Down

0 comments on commit 6862279

Please sign in to comment.