@@ -44,6 +44,32 @@ static int all_bytes_equal(const void* s, unsigned char value, size_t n) {
44
44
return 1 ;
45
45
}
46
46
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 */
47
73
static void counting_illegal_callback_fn (const char * str , void * data ) {
48
74
/* Dummy callback function that just counts. */
49
75
int32_t * p ;
@@ -246,39 +272,28 @@ static void run_static_context_tests(int use_prealloc) {
246
272
CHECK (secp256k1_context_no_precomp == secp256k1_context_static );
247
273
248
274
{
249
- int ecount = 0 ;
250
275
unsigned char seed [32 ] = {0x17 };
251
- secp256k1_context_set_illegal_callback (STATIC_CTX , counting_illegal_callback_fn , & ecount );
252
276
253
277
/* 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 ));
259
280
260
281
/* Destroying or cloning secp256k1_context_static is not supported. */
261
282
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 ));
264
284
{
265
285
secp256k1_context * my_static_ctx = malloc (sizeof (* STATIC_CTX ));
266
286
CHECK (my_static_ctx != NULL );
267
287
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 ));
269
289
CHECK (all_bytes_equal (my_static_ctx , 0x2a , sizeof (* my_static_ctx )));
270
- CHECK (ecount == 2 );
271
290
free (my_static_ctx );
272
291
}
273
- secp256k1_context_preallocated_destroy (STATIC_CTX );
274
- CHECK (ecount == 3 );
292
+ CHECK_ILLEGAL_VOID (STATIC_CTX , secp256k1_context_preallocated_destroy (STATIC_CTX ));
275
293
} 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 ));
280
296
}
281
- secp256k1_context_set_illegal_callback (STATIC_CTX , NULL , NULL );
282
297
}
283
298
284
299
{
0 commit comments