Skip to content

Commit 71caa81

Browse files
committedJan 17, 2023
tests: Add CHECK_ILLEGAL(_VOID) macros and use in static ctx tests
1 parent 3dbbf11 commit 71caa81

File tree

1 file changed

+33
-18
lines changed

1 file changed

+33
-18
lines changed
 

‎src/tests.c

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

47+
static int ILLEGAL;
48+
static void check_illegal_helper_callback_fn(const char* str, void* data) {
49+
(void)data;
50+
(void)str;
51+
ILLEGAL = 1;
52+
}
53+
54+
/* For checking functions that use ARG_CHECK */
55+
#define CHECK_ILLEGAL(ctx, expr) do { \
56+
ILLEGAL = 0; \
57+
secp256k1_context_set_illegal_callback(ctx, check_illegal_helper_callback_fn, NULL); \
58+
{ CHECK((expr) == 0); } \
59+
secp256k1_context_set_illegal_callback(ctx, NULL, NULL); \
60+
CHECK(ILLEGAL == 1); \
61+
} while(0);
62+
63+
/* For checking functions that use ARG_CHECK_VOID */
64+
#define CHECK_ILLEGAL_VOID(ctx, expr) do { \
65+
ILLEGAL = 0; \
66+
secp256k1_context_set_illegal_callback(ctx, check_illegal_helper_callback_fn, NULL); \
67+
{ (expr); } \
68+
secp256k1_context_set_illegal_callback(ctx, NULL, NULL); \
69+
CHECK(ILLEGAL == 1); \
70+
} while(0);
71+
72+
/* TODO Use CHECK_ILLEGAL(_VOID) everywhere and get rid of these counting callbacks */
4773
static void counting_illegal_callback_fn(const char* str, void* data) {
4874
/* Dummy callback function that just counts. */
4975
int32_t *p;
@@ -246,39 +272,28 @@ static void run_static_context_tests(int use_prealloc) {
246272
CHECK(secp256k1_context_no_precomp == secp256k1_context_static);
247273

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

253277
/* 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;
278+
CHECK_ILLEGAL(STATIC_CTX, secp256k1_context_randomize(STATIC_CTX, seed));
279+
CHECK_ILLEGAL(STATIC_CTX, secp256k1_context_randomize(STATIC_CTX, NULL));
259280

260281
/* Destroying or cloning secp256k1_context_static is not supported. */
261282
if (use_prealloc) {
262-
CHECK(secp256k1_context_preallocated_clone_size(STATIC_CTX) == 0);
263-
CHECK(ecount == 1);
283+
CHECK_ILLEGAL(STATIC_CTX, secp256k1_context_preallocated_clone_size(STATIC_CTX));
264284
{
265285
secp256k1_context *my_static_ctx = malloc(sizeof(*STATIC_CTX));
266286
CHECK(my_static_ctx != NULL);
267287
memset(my_static_ctx, 0x2a, sizeof(*my_static_ctx));
268-
CHECK(secp256k1_context_preallocated_clone(STATIC_CTX, my_static_ctx) == NULL);
288+
CHECK_ILLEGAL(STATIC_CTX, secp256k1_context_preallocated_clone(STATIC_CTX, my_static_ctx));
269289
CHECK(all_bytes_equal(my_static_ctx, 0x2a, sizeof(*my_static_ctx)));
270-
CHECK(ecount == 2);
271290
free(my_static_ctx);
272291
}
273-
secp256k1_context_preallocated_destroy(STATIC_CTX);
274-
CHECK(ecount == 3);
292+
CHECK_ILLEGAL_VOID(STATIC_CTX, secp256k1_context_preallocated_destroy(STATIC_CTX));
275293
} else {
276-
CHECK(secp256k1_context_clone(STATIC_CTX) == NULL);
277-
CHECK(ecount == 1);
278-
secp256k1_context_destroy(STATIC_CTX);
279-
CHECK(ecount == 2);
294+
CHECK_ILLEGAL(STATIC_CTX, secp256k1_context_clone(STATIC_CTX));
295+
CHECK_ILLEGAL_VOID(STATIC_CTX, secp256k1_context_destroy(STATIC_CTX));
280296
}
281-
secp256k1_context_set_illegal_callback(STATIC_CTX, NULL, NULL);
282297
}
283298

284299
{

0 commit comments

Comments
 (0)