Skip to content

Commit 0c8a5ca

Browse files
committed
tests: Switch to NONE contexts in tests.c
1 parent 86540e9 commit 0c8a5ca

File tree

1 file changed

+38
-39
lines changed

1 file changed

+38
-39
lines changed

src/tests.c

+38-39
Original file line numberDiff line numberDiff line change
@@ -335,81 +335,82 @@ void run_scratch_tests(void) {
335335
int32_t ecount = 0;
336336
size_t checkpoint;
337337
size_t checkpoint_2;
338-
secp256k1_context *none = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
339338
secp256k1_scratch_space *scratch;
340339
secp256k1_scratch_space local_scratch;
341340

341+
ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
342+
342343
/* Test public API */
343-
secp256k1_context_set_illegal_callback(none, counting_illegal_callback_fn, &ecount);
344-
secp256k1_context_set_error_callback(none, counting_illegal_callback_fn, &ecount);
344+
secp256k1_context_set_illegal_callback(ctx, counting_illegal_callback_fn, &ecount);
345+
secp256k1_context_set_error_callback(ctx, counting_illegal_callback_fn, &ecount);
345346

346-
scratch = secp256k1_scratch_space_create(none, 1000);
347+
scratch = secp256k1_scratch_space_create(ctx, 1000);
347348
CHECK(scratch != NULL);
348349
CHECK(ecount == 0);
349350

350351
/* Test internal API */
351-
CHECK(secp256k1_scratch_max_allocation(&none->error_callback, scratch, 0) == 1000);
352-
CHECK(secp256k1_scratch_max_allocation(&none->error_callback, scratch, 1) == 1000 - (ALIGNMENT - 1));
352+
CHECK(secp256k1_scratch_max_allocation(&ctx->error_callback, scratch, 0) == 1000);
353+
CHECK(secp256k1_scratch_max_allocation(&ctx->error_callback, scratch, 1) == 1000 - (ALIGNMENT - 1));
353354
CHECK(scratch->alloc_size == 0);
354355
CHECK(scratch->alloc_size % ALIGNMENT == 0);
355356

356357
/* Allocating 500 bytes succeeds */
357-
checkpoint = secp256k1_scratch_checkpoint(&none->error_callback, scratch);
358-
CHECK(secp256k1_scratch_alloc(&none->error_callback, scratch, 500) != NULL);
359-
CHECK(secp256k1_scratch_max_allocation(&none->error_callback, scratch, 0) == 1000 - adj_alloc);
360-
CHECK(secp256k1_scratch_max_allocation(&none->error_callback, scratch, 1) == 1000 - adj_alloc - (ALIGNMENT - 1));
358+
checkpoint = secp256k1_scratch_checkpoint(&ctx->error_callback, scratch);
359+
CHECK(secp256k1_scratch_alloc(&ctx->error_callback, scratch, 500) != NULL);
360+
CHECK(secp256k1_scratch_max_allocation(&ctx->error_callback, scratch, 0) == 1000 - adj_alloc);
361+
CHECK(secp256k1_scratch_max_allocation(&ctx->error_callback, scratch, 1) == 1000 - adj_alloc - (ALIGNMENT - 1));
361362
CHECK(scratch->alloc_size != 0);
362363
CHECK(scratch->alloc_size % ALIGNMENT == 0);
363364

364365
/* Allocating another 501 bytes fails */
365-
CHECK(secp256k1_scratch_alloc(&none->error_callback, scratch, 501) == NULL);
366-
CHECK(secp256k1_scratch_max_allocation(&none->error_callback, scratch, 0) == 1000 - adj_alloc);
367-
CHECK(secp256k1_scratch_max_allocation(&none->error_callback, scratch, 1) == 1000 - adj_alloc - (ALIGNMENT - 1));
366+
CHECK(secp256k1_scratch_alloc(&ctx->error_callback, scratch, 501) == NULL);
367+
CHECK(secp256k1_scratch_max_allocation(&ctx->error_callback, scratch, 0) == 1000 - adj_alloc);
368+
CHECK(secp256k1_scratch_max_allocation(&ctx->error_callback, scratch, 1) == 1000 - adj_alloc - (ALIGNMENT - 1));
368369
CHECK(scratch->alloc_size != 0);
369370
CHECK(scratch->alloc_size % ALIGNMENT == 0);
370371

371372
/* ...but it succeeds once we apply the checkpoint to undo it */
372-
secp256k1_scratch_apply_checkpoint(&none->error_callback, scratch, checkpoint);
373+
secp256k1_scratch_apply_checkpoint(&ctx->error_callback, scratch, checkpoint);
373374
CHECK(scratch->alloc_size == 0);
374-
CHECK(secp256k1_scratch_max_allocation(&none->error_callback, scratch, 0) == 1000);
375-
CHECK(secp256k1_scratch_alloc(&none->error_callback, scratch, 500) != NULL);
375+
CHECK(secp256k1_scratch_max_allocation(&ctx->error_callback, scratch, 0) == 1000);
376+
CHECK(secp256k1_scratch_alloc(&ctx->error_callback, scratch, 500) != NULL);
376377
CHECK(scratch->alloc_size != 0);
377378

378379
/* try to apply a bad checkpoint */
379-
checkpoint_2 = secp256k1_scratch_checkpoint(&none->error_callback, scratch);
380-
secp256k1_scratch_apply_checkpoint(&none->error_callback, scratch, checkpoint);
380+
checkpoint_2 = secp256k1_scratch_checkpoint(&ctx->error_callback, scratch);
381+
secp256k1_scratch_apply_checkpoint(&ctx->error_callback, scratch, checkpoint);
381382
CHECK(ecount == 0);
382-
secp256k1_scratch_apply_checkpoint(&none->error_callback, scratch, checkpoint_2); /* checkpoint_2 is after checkpoint */
383+
secp256k1_scratch_apply_checkpoint(&ctx->error_callback, scratch, checkpoint_2); /* checkpoint_2 is after checkpoint */
383384
CHECK(ecount == 1);
384-
secp256k1_scratch_apply_checkpoint(&none->error_callback, scratch, (size_t) -1); /* this is just wildly invalid */
385+
secp256k1_scratch_apply_checkpoint(&ctx->error_callback, scratch, (size_t) -1); /* this is just wildly invalid */
385386
CHECK(ecount == 2);
386387

387388
/* try to use badly initialized scratch space */
388-
secp256k1_scratch_space_destroy(none, scratch);
389+
secp256k1_scratch_space_destroy(ctx, scratch);
389390
memset(&local_scratch, 0, sizeof(local_scratch));
390391
scratch = &local_scratch;
391-
CHECK(!secp256k1_scratch_max_allocation(&none->error_callback, scratch, 0));
392+
CHECK(!secp256k1_scratch_max_allocation(&ctx->error_callback, scratch, 0));
392393
CHECK(ecount == 3);
393-
CHECK(secp256k1_scratch_alloc(&none->error_callback, scratch, 500) == NULL);
394+
CHECK(secp256k1_scratch_alloc(&ctx->error_callback, scratch, 500) == NULL);
394395
CHECK(ecount == 4);
395-
secp256k1_scratch_space_destroy(none, scratch);
396+
secp256k1_scratch_space_destroy(ctx, scratch);
396397
CHECK(ecount == 5);
397398

398399
/* Test that large integers do not wrap around in a bad way */
399-
scratch = secp256k1_scratch_space_create(none, 1000);
400+
scratch = secp256k1_scratch_space_create(ctx, 1000);
400401
/* Try max allocation with a large number of objects. Only makes sense if
401402
* ALIGNMENT is greater than 1 because otherwise the objects take no extra
402403
* space. */
403-
CHECK(ALIGNMENT <= 1 || !secp256k1_scratch_max_allocation(&none->error_callback, scratch, (SIZE_MAX / (ALIGNMENT - 1)) + 1));
404+
CHECK(ALIGNMENT <= 1 || !secp256k1_scratch_max_allocation(&ctx->error_callback, scratch, (SIZE_MAX / (ALIGNMENT - 1)) + 1));
404405
/* Try allocating SIZE_MAX to test wrap around which only happens if
405406
* ALIGNMENT > 1, otherwise it returns NULL anyway because the scratch
406407
* space is too small. */
407-
CHECK(secp256k1_scratch_alloc(&none->error_callback, scratch, SIZE_MAX) == NULL);
408-
secp256k1_scratch_space_destroy(none, scratch);
408+
CHECK(secp256k1_scratch_alloc(&ctx->error_callback, scratch, SIZE_MAX) == NULL);
409+
secp256k1_scratch_space_destroy(ctx, scratch);
409410

410411
/* cleanup */
411-
secp256k1_scratch_space_destroy(none, NULL); /* no-op */
412-
secp256k1_context_destroy(none);
412+
secp256k1_scratch_space_destroy(ctx, NULL); /* no-op */
413+
secp256k1_context_destroy(ctx);
413414
}
414415

415416

@@ -680,7 +681,6 @@ void run_rfc6979_hmac_sha256_tests(void) {
680681

681682
void run_tagged_sha256_tests(void) {
682683
int ecount = 0;
683-
secp256k1_context *none = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
684684
unsigned char tag[32] = { 0 };
685685
unsigned char msg[32] = { 0 };
686686
unsigned char hash32[32];
@@ -691,23 +691,22 @@ void run_tagged_sha256_tests(void) {
691691
0xE2, 0x76, 0x55, 0x9A, 0x3B, 0xDE, 0x55, 0xB3
692692
};
693693

694-
secp256k1_context_set_illegal_callback(none, counting_illegal_callback_fn, &ecount);
694+
secp256k1_context_set_illegal_callback(ctx, counting_illegal_callback_fn, &ecount);
695695

696696
/* API test */
697-
CHECK(secp256k1_tagged_sha256(none, hash32, tag, sizeof(tag), msg, sizeof(msg)) == 1);
698-
CHECK(secp256k1_tagged_sha256(none, NULL, tag, sizeof(tag), msg, sizeof(msg)) == 0);
697+
CHECK(secp256k1_tagged_sha256(ctx, hash32, tag, sizeof(tag), msg, sizeof(msg)) == 1);
698+
CHECK(secp256k1_tagged_sha256(ctx, NULL, tag, sizeof(tag), msg, sizeof(msg)) == 0);
699699
CHECK(ecount == 1);
700-
CHECK(secp256k1_tagged_sha256(none, hash32, NULL, 0, msg, sizeof(msg)) == 0);
700+
CHECK(secp256k1_tagged_sha256(ctx, hash32, NULL, 0, msg, sizeof(msg)) == 0);
701701
CHECK(ecount == 2);
702-
CHECK(secp256k1_tagged_sha256(none, hash32, tag, sizeof(tag), NULL, 0) == 0);
702+
CHECK(secp256k1_tagged_sha256(ctx, hash32, tag, sizeof(tag), NULL, 0) == 0);
703703
CHECK(ecount == 3);
704704

705705
/* Static test vector */
706706
memcpy(tag, "tag", 3);
707707
memcpy(msg, "msg", 3);
708-
CHECK(secp256k1_tagged_sha256(none, hash32, tag, 3, msg, 3) == 1);
708+
CHECK(secp256k1_tagged_sha256(ctx, hash32, tag, 3, msg, 3) == 1);
709709
CHECK(secp256k1_memcmp_var(hash32, hash_expected, sizeof(hash32)) == 0);
710-
secp256k1_context_destroy(none);
711710
}
712711

713712
/***** RANDOM TESTS *****/
@@ -7366,7 +7365,7 @@ int main(int argc, char **argv) {
73667365
run_context_tests(1);
73677366
run_scratch_tests();
73687367

7369-
ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
7368+
ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
73707369
/* Randomize the context only with probability 15/16
73717370
to make sure we test without context randomization from time to time.
73727371
TODO Reconsider this when recalibrating the tests. */

0 commit comments

Comments
 (0)