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 7c11f97 commit abcd6e9
Showing 1 changed file with 34 additions and 18 deletions.
52 changes: 34 additions & 18 deletions src/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,33 @@ static int all_bytes_equal(const void* s, unsigned char value, size_t n) {
return 1;
}

static unsigned int ILLEGAL;
static void check_illegal_helper_callback_fn(const char* str, void* data) {
(void)data;
(void)str;
ILLEGAL++;
CHECK(ILLEGAL != 0);
}

/* For checking functions that use ARG_CHECK */
#define CHECK_ILLEGAL(ctx, expr) do { \
ILLEGAL = 0; \
secp256k1_context_set_illegal_callback(ctx, check_illegal_helper_callback_fn, NULL); \
{ CHECK((expr) == 0); } \
secp256k1_context_set_illegal_callback(ctx, NULL, NULL); \
CHECK(ILLEGAL == 1); \
} while(0);

/* For checking functions that use ARG_CHECK_VOID */
#define CHECK_ILLEGAL_VOID(ctx, expr) do { \
ILLEGAL = 0; \
secp256k1_context_set_illegal_callback(ctx, check_illegal_helper_callback_fn, NULL); \
{ (expr); } \
secp256k1_context_set_illegal_callback(ctx, NULL, NULL); \
CHECK(ILLEGAL == 1); \
} while(0);

/* TODO Use CHECK_ILLEGAL(_VOID) everywhere and get rid of these counting callbacks */
static void counting_illegal_callback_fn(const char* str, void* data) {
/* Dummy callback function that just counts. */
int32_t *p;
Expand Down Expand Up @@ -246,39 +273,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 abcd6e9

Please sign in to comment.