Skip to content

Commit f32a36f

Browse files
tests: Don't use global context for context tests
1 parent ce4f936 commit f32a36f

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed

src/tests.c

+30-30
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ void run_ec_illegal_argument_tests(void) {
231231
void run_context_tests(int use_prealloc) {
232232
int32_t ecount;
233233
int32_t ecount2;
234-
void *ctx_prealloc = NULL;
234+
secp256k1_context *my_ctx;
235+
void *my_ctx_prealloc = NULL;
235236

236237
secp256k1_gej pubj;
237238
secp256k1_ge pub;
@@ -242,24 +243,24 @@ void run_context_tests(int use_prealloc) {
242243
CHECK(secp256k1_context_no_precomp == secp256k1_context_static);
243244

244245
if (use_prealloc) {
245-
ctx_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_NONE));
246-
CHECK(ctx_prealloc != NULL);
247-
ctx = secp256k1_context_preallocated_create(ctx_prealloc, SECP256K1_CONTEXT_NONE);
246+
my_ctx_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_NONE));
247+
CHECK(my_ctx_prealloc != NULL);
248+
my_ctx = secp256k1_context_preallocated_create(my_ctx_prealloc, SECP256K1_CONTEXT_NONE);
248249
} else {
249-
ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
250+
my_ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
250251
}
251252

252253
ecount = 0;
253254
ecount2 = 10;
254255
secp256k1_context_set_illegal_callback(sttc, counting_illegal_callback_fn, &ecount);
255-
secp256k1_context_set_illegal_callback(ctx, counting_illegal_callback_fn, &ecount2);
256+
secp256k1_context_set_illegal_callback(my_ctx, counting_illegal_callback_fn, &ecount2);
256257
/* set error callback (to a function that still aborts in case malloc() fails in secp256k1_context_clone() below) */
257-
secp256k1_context_set_error_callback(ctx, secp256k1_default_illegal_callback_fn, NULL);
258-
CHECK(ctx->error_callback.fn != sttc->error_callback.fn);
259-
CHECK(ctx->error_callback.fn == secp256k1_default_illegal_callback_fn);
258+
secp256k1_context_set_error_callback(my_ctx, secp256k1_default_illegal_callback_fn, NULL);
259+
CHECK(my_ctx->error_callback.fn != sttc->error_callback.fn);
260+
CHECK(my_ctx->error_callback.fn == secp256k1_default_illegal_callback_fn);
260261

261262
/* check if sizes for cloning are consistent */
262-
CHECK(secp256k1_context_preallocated_clone_size(ctx) == secp256k1_context_preallocated_size(SECP256K1_CONTEXT_NONE));
263+
CHECK(secp256k1_context_preallocated_clone_size(my_ctx) == secp256k1_context_preallocated_size(SECP256K1_CONTEXT_NONE));
263264
CHECK(secp256k1_context_preallocated_clone_size(sttc) >= sizeof(secp256k1_context));
264265

265266
/*** clone and destroy all of them to make sure cloning was complete ***/
@@ -268,50 +269,50 @@ void run_context_tests(int use_prealloc) {
268269

269270
if (use_prealloc) {
270271
/* clone into a non-preallocated context and then again into a new preallocated one. */
271-
ctx_tmp = ctx; ctx = secp256k1_context_clone(ctx); secp256k1_context_preallocated_destroy(ctx_tmp);
272-
free(ctx_prealloc); ctx_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_NONE)); CHECK(ctx_prealloc != NULL);
273-
ctx_tmp = ctx; ctx = secp256k1_context_preallocated_clone(ctx, ctx_prealloc); secp256k1_context_destroy(ctx_tmp);
272+
ctx_tmp = my_ctx; my_ctx = secp256k1_context_clone(my_ctx); secp256k1_context_preallocated_destroy(ctx_tmp);
273+
free(my_ctx_prealloc); my_ctx_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_NONE)); CHECK(my_ctx_prealloc != NULL);
274+
ctx_tmp = my_ctx; my_ctx = secp256k1_context_preallocated_clone(my_ctx, my_ctx_prealloc); secp256k1_context_destroy(ctx_tmp);
274275
} else {
275276
/* clone into a preallocated context and then again into a new non-preallocated one. */
276277
void *prealloc_tmp;
277278

278279
prealloc_tmp = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_NONE)); CHECK(prealloc_tmp != NULL);
279-
ctx_tmp = ctx; ctx = secp256k1_context_preallocated_clone(ctx, prealloc_tmp); secp256k1_context_destroy(ctx_tmp);
280-
ctx_tmp = ctx; ctx = secp256k1_context_clone(ctx); secp256k1_context_preallocated_destroy(ctx_tmp);
280+
ctx_tmp = my_ctx; my_ctx = secp256k1_context_preallocated_clone(my_ctx, prealloc_tmp); secp256k1_context_destroy(ctx_tmp);
281+
ctx_tmp = my_ctx; my_ctx = secp256k1_context_clone(my_ctx); secp256k1_context_preallocated_destroy(ctx_tmp);
281282
free(prealloc_tmp);
282283
}
283284
}
284285

285286
/* Verify that the error callback makes it across the clone. */
286-
CHECK(ctx->error_callback.fn != sttc->error_callback.fn);
287-
CHECK(ctx->error_callback.fn == secp256k1_default_illegal_callback_fn);
287+
CHECK(my_ctx->error_callback.fn != sttc->error_callback.fn);
288+
CHECK(my_ctx->error_callback.fn == secp256k1_default_illegal_callback_fn);
288289
/* And that it resets back to default. */
289-
secp256k1_context_set_error_callback(ctx, NULL, NULL);
290-
CHECK(ctx->error_callback.fn == sttc->error_callback.fn);
290+
secp256k1_context_set_error_callback(my_ctx, NULL, NULL);
291+
CHECK(my_ctx->error_callback.fn == sttc->error_callback.fn);
291292

292293
/*** attempt to use them ***/
293294
random_scalar_order_test(&msg);
294295
random_scalar_order_test(&key);
295-
secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, &pubj, &key);
296+
secp256k1_ecmult_gen(&my_ctx->ecmult_gen_ctx, &pubj, &key);
296297
secp256k1_ge_set_gej(&pub, &pubj);
297298

298299
/* obtain a working nonce */
299300
do {
300301
random_scalar_order_test(&nonce);
301-
} while(!secp256k1_ecdsa_sig_sign(&ctx->ecmult_gen_ctx, &sigr, &sigs, &key, &msg, &nonce, NULL));
302+
} while(!secp256k1_ecdsa_sig_sign(&my_ctx->ecmult_gen_ctx, &sigr, &sigs, &key, &msg, &nonce, NULL));
302303

303304
/* try signing */
304-
CHECK(secp256k1_ecdsa_sig_sign(&ctx->ecmult_gen_ctx, &sigr, &sigs, &key, &msg, &nonce, NULL));
305+
CHECK(secp256k1_ecdsa_sig_sign(&my_ctx->ecmult_gen_ctx, &sigr, &sigs, &key, &msg, &nonce, NULL));
305306

306307
/* try verifying */
307308
CHECK(secp256k1_ecdsa_sig_verify(&sigr, &sigs, &pub, &msg));
308309

309310
/* cleanup */
310311
if (use_prealloc) {
311-
secp256k1_context_preallocated_destroy(ctx);
312-
free(ctx_prealloc);
312+
secp256k1_context_preallocated_destroy(my_ctx);
313+
free(my_ctx_prealloc);
313314
} else {
314-
secp256k1_context_destroy(ctx);
315+
secp256k1_context_destroy(my_ctx);
315316
}
316317
/* Defined as no-op. */
317318
secp256k1_context_destroy(NULL);
@@ -7360,11 +7361,6 @@ int main(int argc, char **argv) {
73607361
memcpy(sttc, secp256k1_context_static, sizeof(secp256k1_context));
73617362
CHECK(!secp256k1_context_is_proper(sttc));
73627363

7363-
run_selftest_tests();
7364-
run_context_tests(0);
7365-
run_context_tests(1);
7366-
run_deprecated_context_flags_test();
7367-
73687364
ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
73697365
/* Randomize the context only with probability 15/16
73707366
to make sure we test without context randomization from time to time.
@@ -7375,6 +7371,10 @@ int main(int argc, char **argv) {
73757371
CHECK(secp256k1_context_randomize(ctx, rand32));
73767372
}
73777373

7374+
run_selftest_tests();
7375+
run_context_tests(0);
7376+
run_context_tests(1);
7377+
run_deprecated_context_flags_test();
73787378
run_scratch_tests();
73797379

73807380
run_rand_bits();

0 commit comments

Comments
 (0)