Skip to content

Commit fc16b69

Browse files
refactor: Tidy run_context_tests() by extracting functions
1 parent 18e0db3 commit fc16b69

File tree

1 file changed

+52
-50
lines changed

1 file changed

+52
-50
lines changed

src/tests.c

Lines changed: 52 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ int context_eq(const secp256k1_context *a, const secp256k1_context *b) {
158158
&& a->error_callback.data == b->error_callback.data;
159159
}
160160

161-
void test_deprecated_flags(void) {
161+
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 };
@@ -174,11 +174,59 @@ void test_deprecated_flags(void) {
174174
}
175175
}
176176

177-
void run_context_tests(int use_prealloc) {
177+
void run_ec_illegal_argument_tests(void) {
178+
int ecount = 0;
179+
int ecount2 = 10;
178180
secp256k1_pubkey pubkey;
179181
secp256k1_pubkey zero_pubkey;
180182
secp256k1_ecdsa_signature sig;
181183
unsigned char ctmp[32];
184+
185+
/* Setup */
186+
secp256k1_context_set_illegal_callback(sttc, counting_illegal_callback_fn, &ecount);
187+
secp256k1_context_set_illegal_callback(ctx, counting_illegal_callback_fn, &ecount2);
188+
memset(ctmp, 1, 32);
189+
memset(&zero_pubkey, 0, sizeof(zero_pubkey));
190+
191+
/* Verify context-type checking illegal-argument errors. */
192+
CHECK(secp256k1_ec_pubkey_create(sttc, &pubkey, ctmp) == 0);
193+
CHECK(ecount == 1);
194+
VG_UNDEF(&pubkey, sizeof(pubkey));
195+
CHECK(secp256k1_ec_pubkey_create(ctx, &pubkey, ctmp) == 1);
196+
VG_CHECK(&pubkey, sizeof(pubkey));
197+
CHECK(secp256k1_ecdsa_sign(sttc, &sig, ctmp, ctmp, NULL, NULL) == 0);
198+
CHECK(ecount == 2);
199+
VG_UNDEF(&sig, sizeof(sig));
200+
CHECK(secp256k1_ecdsa_sign(ctx, &sig, ctmp, ctmp, NULL, NULL) == 1);
201+
VG_CHECK(&sig, sizeof(sig));
202+
CHECK(ecount2 == 10);
203+
CHECK(secp256k1_ecdsa_verify(ctx, &sig, ctmp, &pubkey) == 1);
204+
CHECK(ecount2 == 10);
205+
CHECK(secp256k1_ecdsa_verify(sttc, &sig, ctmp, &pubkey) == 1);
206+
CHECK(ecount == 2);
207+
CHECK(secp256k1_ec_pubkey_tweak_add(ctx, &pubkey, ctmp) == 1);
208+
CHECK(ecount2 == 10);
209+
CHECK(secp256k1_ec_pubkey_tweak_add(sttc, &pubkey, ctmp) == 1);
210+
CHECK(ecount == 2);
211+
CHECK(secp256k1_ec_pubkey_tweak_mul(ctx, &pubkey, ctmp) == 1);
212+
CHECK(ecount2 == 10);
213+
CHECK(secp256k1_ec_pubkey_negate(sttc, &pubkey) == 1);
214+
CHECK(ecount == 2);
215+
CHECK(secp256k1_ec_pubkey_negate(ctx, &pubkey) == 1);
216+
CHECK(ecount == 2);
217+
CHECK(secp256k1_ec_pubkey_negate(sttc, &zero_pubkey) == 0);
218+
CHECK(ecount == 3);
219+
CHECK(secp256k1_ec_pubkey_negate(ctx, NULL) == 0);
220+
CHECK(ecount2 == 11);
221+
CHECK(secp256k1_ec_pubkey_tweak_mul(sttc, &pubkey, ctmp) == 1);
222+
CHECK(ecount == 3);
223+
224+
/* Clean up */
225+
secp256k1_context_set_illegal_callback(sttc, NULL, NULL);
226+
secp256k1_context_set_illegal_callback(ctx, NULL, NULL);
227+
}
228+
229+
void run_context_tests(int use_prealloc) {
182230
int32_t ecount;
183231
int32_t ecount2;
184232
void *ctx_prealloc = NULL;
@@ -199,10 +247,6 @@ void run_context_tests(int use_prealloc) {
199247
ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
200248
}
201249

202-
test_deprecated_flags();
203-
204-
memset(&zero_pubkey, 0, sizeof(zero_pubkey));
205-
206250
ecount = 0;
207251
ecount2 = 10;
208252
secp256k1_context_set_illegal_callback(sttc, counting_illegal_callback_fn, &ecount);
@@ -249,50 +293,6 @@ void run_context_tests(int use_prealloc) {
249293
secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, &pubj, &key);
250294
secp256k1_ge_set_gej(&pub, &pubj);
251295

252-
/* Verify context-type checking illegal-argument errors. */
253-
memset(ctmp, 1, 32);
254-
CHECK(secp256k1_ec_pubkey_create(sttc, &pubkey, ctmp) == 0);
255-
CHECK(ecount == 1);
256-
VG_UNDEF(&pubkey, sizeof(pubkey));
257-
CHECK(secp256k1_ec_pubkey_create(ctx, &pubkey, ctmp) == 1);
258-
VG_CHECK(&pubkey, sizeof(pubkey));
259-
CHECK(secp256k1_ecdsa_sign(sttc, &sig, ctmp, ctmp, NULL, NULL) == 0);
260-
CHECK(ecount == 2);
261-
VG_UNDEF(&sig, sizeof(sig));
262-
CHECK(secp256k1_ecdsa_sign(ctx, &sig, ctmp, ctmp, NULL, NULL) == 1);
263-
VG_CHECK(&sig, sizeof(sig));
264-
CHECK(ecount2 == 10);
265-
CHECK(secp256k1_ecdsa_verify(ctx, &sig, ctmp, &pubkey) == 1);
266-
CHECK(ecount2 == 10);
267-
CHECK(secp256k1_ecdsa_verify(sttc, &sig, ctmp, &pubkey) == 1);
268-
CHECK(ecount == 2);
269-
CHECK(secp256k1_ec_pubkey_tweak_add(ctx, &pubkey, ctmp) == 1);
270-
CHECK(ecount2 == 10);
271-
CHECK(secp256k1_ec_pubkey_tweak_add(sttc, &pubkey, ctmp) == 1);
272-
CHECK(ecount == 2);
273-
CHECK(secp256k1_ec_pubkey_tweak_mul(ctx, &pubkey, ctmp) == 1);
274-
CHECK(ecount2 == 10);
275-
CHECK(secp256k1_ec_pubkey_negate(sttc, &pubkey) == 1);
276-
CHECK(ecount == 2);
277-
CHECK(secp256k1_ec_pubkey_negate(ctx, &pubkey) == 1);
278-
CHECK(ecount == 2);
279-
CHECK(secp256k1_ec_pubkey_negate(ctx, NULL) == 0);
280-
CHECK(ecount2 == 11);
281-
CHECK(secp256k1_ec_pubkey_negate(sttc, &zero_pubkey) == 0);
282-
CHECK(ecount == 3);
283-
CHECK(secp256k1_ec_pubkey_tweak_mul(sttc, &pubkey, ctmp) == 1);
284-
CHECK(ecount == 3);
285-
CHECK(secp256k1_context_randomize(sttc, ctmp) == 1);
286-
CHECK(ecount == 3);
287-
CHECK(secp256k1_context_randomize(sttc, NULL) == 1);
288-
CHECK(ecount == 3);
289-
CHECK(secp256k1_context_randomize(ctx, ctmp) == 1);
290-
CHECK(ecount2 == 11);
291-
CHECK(secp256k1_context_randomize(ctx, NULL) == 1);
292-
CHECK(ecount2 == 11);
293-
secp256k1_context_set_illegal_callback(sttc, NULL, NULL);
294-
secp256k1_context_set_illegal_callback(ctx, NULL, NULL);
295-
296296
/* obtain a working nonce */
297297
do {
298298
random_scalar_order_test(&nonce);
@@ -7361,6 +7361,7 @@ int main(int argc, char **argv) {
73617361
run_selftest_tests();
73627362
run_context_tests(0);
73637363
run_context_tests(1);
7364+
run_deprecated_context_flags_test();
73647365
run_scratch_tests();
73657366

73667367
ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
@@ -7435,6 +7436,7 @@ int main(int argc, char **argv) {
74357436
#endif
74367437

74377438
/* ecdsa tests */
7439+
run_ec_illegal_argument_tests();
74387440
run_pubkey_comparison();
74397441
run_random_pubkeys();
74407442
run_ecdsa_der_parse();

0 commit comments

Comments
 (0)