@@ -32,6 +32,18 @@ static int COUNT = 64;
32
32
static secp256k1_context * CTX = NULL ;
33
33
static secp256k1_context * STATIC_CTX = NULL ;
34
34
35
+ static int all_bytes_equal (const void * s , unsigned char value , size_t n ) {
36
+ const unsigned char * p = s ;
37
+ size_t i ;
38
+
39
+ for (i = 0 ; i < n ; i ++ ) {
40
+ if (p [i ] != value ) {
41
+ return 0 ;
42
+ }
43
+ }
44
+ return 1 ;
45
+ }
46
+
35
47
static void counting_illegal_callback_fn (const char * str , void * data ) {
36
48
/* Dummy callback function that just counts. */
37
49
int32_t * p ;
@@ -229,20 +241,45 @@ static void run_ec_illegal_argument_tests(void) {
229
241
secp256k1_context_set_illegal_callback (CTX , NULL , NULL );
230
242
}
231
243
232
- static void run_static_context_tests (void ) {
233
- int32_t dummy = 0 ;
234
-
244
+ static void run_static_context_tests (int use_prealloc ) {
235
245
/* Check that deprecated secp256k1_context_no_precomp is an alias to secp256k1_context_static. */
236
246
CHECK (secp256k1_context_no_precomp == secp256k1_context_static );
237
247
238
- /* check if sizes for cloning are consistent */
239
- CHECK (secp256k1_context_preallocated_clone_size (STATIC_CTX ) >= sizeof (secp256k1_context ));
248
+ {
249
+ int ecount = 0 ;
250
+ secp256k1_context_set_illegal_callback (STATIC_CTX , counting_illegal_callback_fn , & ecount );
251
+ /* Destroying or cloning secp256k1_context_static is not supported. */
252
+ if (use_prealloc ) {
253
+ CHECK (secp256k1_context_preallocated_clone_size (STATIC_CTX ) == 0 );
254
+ CHECK (ecount == 1 );
255
+ {
256
+ secp256k1_context * my_static_ctx = malloc (sizeof (* STATIC_CTX ));
257
+ CHECK (my_static_ctx != NULL );
258
+ memset (my_static_ctx , 0x2a , sizeof (* my_static_ctx ));
259
+ CHECK (secp256k1_context_preallocated_clone (STATIC_CTX , my_static_ctx ) == NULL );
260
+ CHECK (all_bytes_equal (my_static_ctx , 0x2a , sizeof (* my_static_ctx )));
261
+ CHECK (ecount == 2 );
262
+ free (my_static_ctx );
263
+ }
264
+ secp256k1_context_preallocated_destroy (STATIC_CTX );
265
+ CHECK (ecount == 3 );
266
+ } else {
267
+ CHECK (secp256k1_context_clone (STATIC_CTX ) == NULL );
268
+ CHECK (ecount == 1 );
269
+ secp256k1_context_destroy (STATIC_CTX );
270
+ CHECK (ecount == 2 );
271
+ }
272
+ secp256k1_context_set_illegal_callback (STATIC_CTX , NULL , NULL );
273
+ }
240
274
241
- /* Verify that setting and resetting illegal callback works */
242
- secp256k1_context_set_illegal_callback (STATIC_CTX , counting_illegal_callback_fn , & dummy );
243
- CHECK (STATIC_CTX -> illegal_callback .fn == counting_illegal_callback_fn );
244
- secp256k1_context_set_illegal_callback (STATIC_CTX , NULL , NULL );
245
- CHECK (STATIC_CTX -> illegal_callback .fn == secp256k1_default_illegal_callback_fn );
275
+ {
276
+ /* Verify that setting and resetting illegal callback works */
277
+ int32_t dummy = 0 ;
278
+ secp256k1_context_set_illegal_callback (STATIC_CTX , counting_illegal_callback_fn , & dummy );
279
+ CHECK (STATIC_CTX -> illegal_callback .fn == counting_illegal_callback_fn );
280
+ secp256k1_context_set_illegal_callback (STATIC_CTX , NULL , NULL );
281
+ CHECK (STATIC_CTX -> illegal_callback .fn == secp256k1_default_illegal_callback_fn );
282
+ }
246
283
}
247
284
248
285
static void run_proper_context_tests (int use_prealloc ) {
@@ -327,6 +364,7 @@ static void run_proper_context_tests(int use_prealloc) {
327
364
} else {
328
365
secp256k1_context_destroy (my_ctx );
329
366
}
367
+
330
368
/* Defined as no-op. */
331
369
secp256k1_context_destroy (NULL );
332
370
secp256k1_context_preallocated_destroy (NULL );
@@ -7389,9 +7427,8 @@ int main(int argc, char **argv) {
7389
7427
run_selftest_tests ();
7390
7428
7391
7429
/* context tests */
7392
- run_proper_context_tests (0 );
7393
- run_proper_context_tests (1 );
7394
- run_static_context_tests ();
7430
+ run_proper_context_tests (0 ); run_proper_context_tests (1 );
7431
+ run_static_context_tests (0 ); run_static_context_tests (1 );
7395
7432
run_deprecated_context_flags_test ();
7396
7433
7397
7434
/* scratch tests */
0 commit comments