Skip to content

Commit e39d954

Browse files
tests: Add CHECK_ILLEGAL(_VOID) macros and use in static ctx tests
1 parent 61841fc commit e39d954

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

src/tests.c

+28-18
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,31 @@ 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+
/* CHECK that expr_or_stmt calls the illegal callback of ctx exactly once
49+
*
50+
* For checking functions that use ARG_CHECK_VOID */
51+
#define CHECK_ILLEGAL_VOID(ctx, expr_or_stmt) do { \
52+
int32_t _calls_to_illegal_callback = 0; \
53+
secp256k1_callback _saved_illegal_cb = ctx->illegal_callback; \
54+
secp256k1_context_set_illegal_callback(ctx, \
55+
counting_illegal_callback_fn, &_calls_to_illegal_callback); \
56+
{ expr_or_stmt; } \
57+
ctx->illegal_callback = _saved_illegal_cb; \
58+
CHECK(_calls_to_illegal_callback == 1); \
59+
} while(0);
60+
61+
/* CHECK that expr calls the illegal callback of ctx exactly once and that expr == 0
62+
*
63+
* For checking functions that use ARG_CHECK */
64+
#define CHECK_ILLEGAL(ctx, expr) CHECK_ILLEGAL_VOID(ctx, CHECK((expr) == 0))
65+
4766
static void counting_illegal_callback_fn(const char* str, void* data) {
4867
/* Dummy callback function that just counts. */
4968
int32_t *p;
5069
(void)str;
5170
p = data;
71+
CHECK(*p != INT32_MAX);
5272
(*p)++;
5373
}
5474

@@ -57,6 +77,7 @@ static void uncounting_illegal_callback_fn(const char* str, void* data) {
5777
int32_t *p;
5878
(void)str;
5979
p = data;
80+
CHECK(*p != INT32_MIN);
6081
(*p)--;
6182
}
6283

@@ -246,39 +267,28 @@ static void run_static_context_tests(int use_prealloc) {
246267
CHECK(secp256k1_context_no_precomp == secp256k1_context_static);
247268

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

253272
/* 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;
273+
CHECK_ILLEGAL(STATIC_CTX, secp256k1_context_randomize(STATIC_CTX, seed));
274+
CHECK_ILLEGAL(STATIC_CTX, secp256k1_context_randomize(STATIC_CTX, NULL));
259275

260276
/* Destroying or cloning secp256k1_context_static is not supported. */
261277
if (use_prealloc) {
262-
CHECK(secp256k1_context_preallocated_clone_size(STATIC_CTX) == 0);
263-
CHECK(ecount == 1);
278+
CHECK_ILLEGAL(STATIC_CTX, secp256k1_context_preallocated_clone_size(STATIC_CTX));
264279
{
265280
secp256k1_context *my_static_ctx = malloc(sizeof(*STATIC_CTX));
266281
CHECK(my_static_ctx != NULL);
267282
memset(my_static_ctx, 0x2a, sizeof(*my_static_ctx));
268-
CHECK(secp256k1_context_preallocated_clone(STATIC_CTX, my_static_ctx) == NULL);
283+
CHECK_ILLEGAL(STATIC_CTX, secp256k1_context_preallocated_clone(STATIC_CTX, my_static_ctx));
269284
CHECK(all_bytes_equal(my_static_ctx, 0x2a, sizeof(*my_static_ctx)));
270-
CHECK(ecount == 2);
271285
free(my_static_ctx);
272286
}
273-
secp256k1_context_preallocated_destroy(STATIC_CTX);
274-
CHECK(ecount == 3);
287+
CHECK_ILLEGAL_VOID(STATIC_CTX, secp256k1_context_preallocated_destroy(STATIC_CTX));
275288
} else {
276-
CHECK(secp256k1_context_clone(STATIC_CTX) == NULL);
277-
CHECK(ecount == 1);
278-
secp256k1_context_destroy(STATIC_CTX);
279-
CHECK(ecount == 2);
289+
CHECK_ILLEGAL(STATIC_CTX, secp256k1_context_clone(STATIC_CTX));
290+
CHECK_ILLEGAL_VOID(STATIC_CTX, secp256k1_context_destroy(STATIC_CTX));
280291
}
281-
secp256k1_context_set_illegal_callback(STATIC_CTX, NULL, NULL);
282292
}
283293

284294
{

0 commit comments

Comments
 (0)