Skip to content

Commit 3c54c4a

Browse files
tests: Don't use global context for context tests
1 parent fc16b69 commit 3c54c4a

File tree

1 file changed

+35
-32
lines changed

1 file changed

+35
-32
lines changed

src/tests.c

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -162,16 +162,18 @@ void run_deprecated_context_flags_test(void) {
162162
unsigned int flags[] = { SECP256K1_CONTEXT_SIGN,
163163
SECP256K1_CONTEXT_VERIFY,
164164
SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY };
165+
secp256k1_context *none_ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
165166
int i;
166167
/* Check that a context created with any of the flags in the flags array is
167168
* identical to the NONE context. */
168169
for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++) {
169170
secp256k1_context *tmp_ctx;
170171
CHECK(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_NONE) == secp256k1_context_preallocated_size(flags[i]));
171172
tmp_ctx = secp256k1_context_create(flags[i]);
172-
CHECK(context_eq(ctx, tmp_ctx));
173+
CHECK(context_eq(none_ctx, tmp_ctx));
173174
secp256k1_context_destroy(tmp_ctx);
174175
}
176+
secp256k1_context_destroy(none_ctx);
175177
}
176178

177179
void run_ec_illegal_argument_tests(void) {
@@ -229,7 +231,8 @@ void run_ec_illegal_argument_tests(void) {
229231
void run_context_tests(int use_prealloc) {
230232
int32_t ecount;
231233
int32_t ecount2;
232-
void *ctx_prealloc = NULL;
234+
secp256k1_context *my_ctx;
235+
void *my_ctx_prealloc = NULL;
233236

234237
secp256k1_gej pubj;
235238
secp256k1_ge pub;
@@ -240,24 +243,24 @@ void run_context_tests(int use_prealloc) {
240243
CHECK(secp256k1_context_no_precomp == secp256k1_context_static);
241244

242245
if (use_prealloc) {
243-
ctx_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_NONE));
244-
CHECK(ctx_prealloc != NULL);
245-
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);
246249
} else {
247-
ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
250+
my_ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
248251
}
249252

250253
ecount = 0;
251254
ecount2 = 10;
252255
secp256k1_context_set_illegal_callback(sttc, counting_illegal_callback_fn, &ecount);
253-
secp256k1_context_set_illegal_callback(ctx, counting_illegal_callback_fn, &ecount2);
256+
secp256k1_context_set_illegal_callback(my_ctx, counting_illegal_callback_fn, &ecount2);
254257
/* set error callback (to a function that still aborts in case malloc() fails in secp256k1_context_clone() below) */
255-
secp256k1_context_set_error_callback(ctx, secp256k1_default_illegal_callback_fn, NULL);
256-
CHECK(ctx->error_callback.fn != sttc->error_callback.fn);
257-
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);
258261

259262
/* check if sizes for cloning are consistent */
260-
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));
261264
CHECK(secp256k1_context_preallocated_clone_size(sttc) >= sizeof(secp256k1_context));
262265

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

267270
if (use_prealloc) {
268271
/* clone into a non-preallocated context and then again into a new preallocated one. */
269-
ctx_tmp = ctx; ctx = secp256k1_context_clone(ctx); secp256k1_context_preallocated_destroy(ctx_tmp);
270-
free(ctx_prealloc); ctx_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_NONE)); CHECK(ctx_prealloc != NULL);
271-
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);
272275
} else {
273276
/* clone into a preallocated context and then again into a new non-preallocated one. */
274277
void *prealloc_tmp;
275278

276279
prealloc_tmp = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_NONE)); CHECK(prealloc_tmp != NULL);
277-
ctx_tmp = ctx; ctx = secp256k1_context_preallocated_clone(ctx, prealloc_tmp); secp256k1_context_destroy(ctx_tmp);
278-
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);
279282
free(prealloc_tmp);
280283
}
281284
}
282285

283286
/* Verify that the error callback makes it across the clone. */
284-
CHECK(ctx->error_callback.fn != sttc->error_callback.fn);
285-
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);
286289
/* And that it resets back to default. */
287-
secp256k1_context_set_error_callback(ctx, NULL, NULL);
288-
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);
289292

290293
/*** attempt to use them ***/
291294
random_scalar_order_test(&msg);
292295
random_scalar_order_test(&key);
293-
secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, &pubj, &key);
296+
secp256k1_ecmult_gen(&my_ctx->ecmult_gen_ctx, &pubj, &key);
294297
secp256k1_ge_set_gej(&pub, &pubj);
295298

296299
/* obtain a working nonce */
297300
do {
298301
random_scalar_order_test(&nonce);
299-
} 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));
300303

301304
/* try signing */
302-
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));
303306

304307
/* try verifying */
305308
CHECK(secp256k1_ecdsa_sig_verify(&sigr, &sigs, &pub, &msg));
306309

307310
/* cleanup */
308311
if (use_prealloc) {
309-
secp256k1_context_preallocated_destroy(ctx);
310-
free(ctx_prealloc);
312+
secp256k1_context_preallocated_destroy(my_ctx);
313+
free(my_ctx_prealloc);
311314
} else {
312-
secp256k1_context_destroy(ctx);
315+
secp256k1_context_destroy(my_ctx);
313316
}
314317
/* Defined as no-op. */
315318
secp256k1_context_destroy(NULL);
@@ -7358,12 +7361,6 @@ int main(int argc, char **argv) {
73587361
memcpy(sttc, secp256k1_context_static, sizeof(secp256k1_context));
73597362
CHECK(!secp256k1_context_is_proper(sttc));
73607363

7361-
run_selftest_tests();
7362-
run_context_tests(0);
7363-
run_context_tests(1);
7364-
run_deprecated_context_flags_test();
7365-
run_scratch_tests();
7366-
73677364
ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
73687365
/* Randomize the context only with probability 15/16
73697366
to make sure we test without context randomization from time to time.
@@ -7374,6 +7371,12 @@ int main(int argc, char **argv) {
73747371
CHECK(secp256k1_context_randomize(ctx, rand32));
73757372
}
73767373

7374+
run_selftest_tests();
7375+
run_context_tests(0);
7376+
run_context_tests(1);
7377+
run_deprecated_context_flags_test();
7378+
run_scratch_tests();
7379+
73777380
run_rand_bits();
73787381
run_rand_int();
73797382

0 commit comments

Comments
 (0)