Skip to content

Commit 3ceba0c

Browse files
tests: Fix tests by adding a global copy of secp256k1_context_static
1 parent 3487278 commit 3ceba0c

File tree

4 files changed

+11
-4
lines changed

4 files changed

+11
-4
lines changed

src/modules/extrakeys/tests_impl.h

-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,6 @@ void test_keypair(void) {
336336
secp256k1_xonly_pubkey xonly_pk, xonly_pk_tmp;
337337
int pk_parity, pk_parity_tmp;
338338
int ecount;
339-
secp256k1_context *sttc = secp256k1_context_clone(secp256k1_context_static);
340339

341340
set_counting_callbacks(ctx, &ecount);
342341
set_counting_callbacks(sttc, &ecount);

src/modules/recovery/tests_impl.h

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ static int recovery_test_nonce_function(unsigned char *nonce32, const unsigned c
3030

3131
void test_ecdsa_recovery_api(void) {
3232
/* Setup contexts that just count errors */
33-
secp256k1_context *sttc = secp256k1_context_clone(secp256k1_context_static);
3433
secp256k1_pubkey pubkey;
3534
secp256k1_pubkey recpubkey;
3635
secp256k1_ecdsa_signature normal_sig;

src/modules/schnorrsig/tests_impl.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,7 @@ void test_schnorrsig_api(void) {
128128
secp256k1_schnorrsig_extraparams invalid_extraparams = {{ 0 }, NULL, NULL};
129129

130130
/** setup **/
131-
secp256k1_context *sttc = secp256k1_context_clone(secp256k1_context_static);
132-
int ecount;
131+
int ecount = 0;
133132

134133
secp256k1_context_set_error_callback(ctx, counting_illegal_callback_fn, &ecount);
135134
secp256k1_context_set_illegal_callback(ctx, counting_illegal_callback_fn, &ecount);

src/tests.c

+10
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
static int count = 64;
3131
static secp256k1_context *ctx = NULL;
32+
static secp256k1_context *sttc = NULL;
3233

3334
static void counting_illegal_callback_fn(const char* str, void* data) {
3435
/* Dummy callback function that just counts. */
@@ -7390,6 +7391,15 @@ int main(int argc, char **argv) {
73907391
CHECK(secp256k1_context_randomize(ctx, rand32));
73917392
}
73927393

7394+
/* Make a writable copy of secp256k1_context_static in order to test the effect of API functions
7395+
that write to the context. The API does not support cloning the static context, so we use
7396+
memcpy instead. The user is not supposed to copy a context but we should still ensure that
7397+
the API functions handle copies of the static context gracefully. */
7398+
sttc = malloc(sizeof(*secp256k1_context_static));
7399+
CHECK(sttc != NULL);
7400+
memcpy(sttc, secp256k1_context_static, sizeof(secp256k1_context));
7401+
CHECK(!secp256k1_context_is_proper(sttc));
7402+
73937403
run_rand_bits();
73947404
run_rand_int();
73957405

0 commit comments

Comments
 (0)