Skip to content

Commit 7a6cc4d

Browse files
tests: Add CHECK_ILLEGAL(_VOID) macros and use in static ctx tests
1 parent 7c11f97 commit 7a6cc4d

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

src/tests.c

+32-18
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,35 @@ static int all_bytes_equal(const void* s, unsigned char value, size_t n) {
4444
return 1;
4545
}
4646

47+
/* TODO Use CHECK_ILLEGAL(_VOID) everywhere and get rid of the uncounting callback */
48+
/* For checking functions that use ARG_CHECK */
49+
#define CHECK_ILLEGAL(ctx, expr) do { \
50+
/* long descriptive variable name to avoid accidental shadowing and have clear error message */ \
51+
int32_t count_of_calls_to_illegal_callback = 0; \
52+
secp256k1_context_set_illegal_callback(ctx, \
53+
counting_illegal_callback_fn, &count_of_calls_to_illegal_callback); \
54+
{ CHECK((expr) == 0); } \
55+
secp256k1_context_set_illegal_callback(ctx, NULL, NULL); \
56+
CHECK(count_of_calls_to_illegal_callback == 1); \
57+
} while(0);
58+
59+
/* For checking functions that use ARG_CHECK_VOID */
60+
#define CHECK_ILLEGAL_VOID(ctx, expr) do { \
61+
/* long descriptive variable name to avoid accidental shadowing and have clear error message */ \
62+
int32_t count_of_calls_to_illegal_callback = 0; \
63+
secp256k1_context_set_illegal_callback(ctx, \
64+
counting_illegal_callback_fn, &count_of_calls_to_illegal_callback); \
65+
{ (expr); } \
66+
secp256k1_context_set_illegal_callback(ctx, NULL, NULL); \
67+
CHECK(count_of_calls_to_illegal_callback == 1); \
68+
} while(0);
69+
4770
static void counting_illegal_callback_fn(const char* str, void* data) {
4871
/* Dummy callback function that just counts. */
4972
int32_t *p;
5073
(void)str;
5174
p = data;
75+
CHECK(*p != INT32_MAX);
5276
(*p)++;
5377
}
5478

@@ -57,6 +81,7 @@ static void uncounting_illegal_callback_fn(const char* str, void* data) {
5781
int32_t *p;
5882
(void)str;
5983
p = data;
84+
CHECK(*p != INT32_MIN);
6085
(*p)--;
6186
}
6287

@@ -246,39 +271,28 @@ static void run_static_context_tests(int use_prealloc) {
246271
CHECK(secp256k1_context_no_precomp == secp256k1_context_static);
247272

248273
{
249-
int ecount = 0;
250274
unsigned char seed[32] = {0x17};
251-
secp256k1_context_set_illegal_callback(STATIC_CTX, counting_illegal_callback_fn, &ecount);
252275

253276
/* Randomizing secp256k1_context_static is not supported. */
254-
CHECK(secp256k1_context_randomize(STATIC_CTX, seed) == 0);
255-
CHECK(ecount == 1);
256-
CHECK(secp256k1_context_randomize(STATIC_CTX, NULL) == 0);
257-
CHECK(ecount == 2);
258-
ecount = 0;
277+
CHECK_ILLEGAL(STATIC_CTX, secp256k1_context_randomize(STATIC_CTX, seed));
278+
CHECK_ILLEGAL(STATIC_CTX, secp256k1_context_randomize(STATIC_CTX, NULL));
259279

260280
/* Destroying or cloning secp256k1_context_static is not supported. */
261281
if (use_prealloc) {
262-
CHECK(secp256k1_context_preallocated_clone_size(STATIC_CTX) == 0);
263-
CHECK(ecount == 1);
282+
CHECK_ILLEGAL(STATIC_CTX, secp256k1_context_preallocated_clone_size(STATIC_CTX));
264283
{
265284
secp256k1_context *my_static_ctx = malloc(sizeof(*STATIC_CTX));
266285
CHECK(my_static_ctx != NULL);
267286
memset(my_static_ctx, 0x2a, sizeof(*my_static_ctx));
268-
CHECK(secp256k1_context_preallocated_clone(STATIC_CTX, my_static_ctx) == NULL);
287+
CHECK_ILLEGAL(STATIC_CTX, secp256k1_context_preallocated_clone(STATIC_CTX, my_static_ctx));
269288
CHECK(all_bytes_equal(my_static_ctx, 0x2a, sizeof(*my_static_ctx)));
270-
CHECK(ecount == 2);
271289
free(my_static_ctx);
272290
}
273-
secp256k1_context_preallocated_destroy(STATIC_CTX);
274-
CHECK(ecount == 3);
291+
CHECK_ILLEGAL_VOID(STATIC_CTX, secp256k1_context_preallocated_destroy(STATIC_CTX));
275292
} else {
276-
CHECK(secp256k1_context_clone(STATIC_CTX) == NULL);
277-
CHECK(ecount == 1);
278-
secp256k1_context_destroy(STATIC_CTX);
279-
CHECK(ecount == 2);
293+
CHECK_ILLEGAL(STATIC_CTX, secp256k1_context_clone(STATIC_CTX));
294+
CHECK_ILLEGAL_VOID(STATIC_CTX, secp256k1_context_destroy(STATIC_CTX));
280295
}
281-
secp256k1_context_set_illegal_callback(STATIC_CTX, NULL, NULL);
282296
}
283297

284298
{

0 commit comments

Comments
 (0)