diff --git a/bitcoin/test/run-secret_eq_consttime.c b/bitcoin/test/run-secret_eq_consttime.c index f0cea7aae..4ac8ffa53 100644 --- a/bitcoin/test/run-secret_eq_consttime.c +++ b/bitcoin/test/run-secret_eq_consttime.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include @@ -106,17 +107,18 @@ static bool secret_time_test(struct timerel (*test)(struct secret *s1, #define ITERATIONS 1000 -int main(void) +int main(int argc, char *argv[]) { const char *v; int const_success, nonconst_success = ITERATIONS, i; double load; - setup_locale(); + + common_setup(argv[0]); /* no point running this under valgrind. */ v = getenv("VALGRIND"); if (v && atoi(v) == 1) - exit(0); + goto exit; s1 = calloc(RUNS, sizeof(*s1)); s2 = calloc(RUNS, sizeof(*s2)); @@ -142,16 +144,20 @@ int main(void) /* Now, check loadavg: if we weren't alone, that could explain results */ getloadavg(&load, 1); - if (load > 1.0) - errx(0, "Load %.2f: too high, ignoring", load); + if (load > 1.0) { + warnx("Load %.2f: too high, ignoring", load); + } else { + if (const_success < ITERATIONS / 2) + errx(1, "Only const time %u/%u?", const_success, i); - if (const_success < ITERATIONS / 2) - errx(1, "Only const time %u/%u?", const_success, i); - - if (nonconst_success < ITERATIONS / 2) - errx(1, "memcmp seemed const time %u/%u?", nonconst_success, i); + if (nonconst_success < ITERATIONS / 2) + errx(1, "memcmp seemed const time %u/%u?", + nonconst_success, i); + } free(s1); free(s2); +exit: + common_shutdown(); return 0; } diff --git a/channeld/test/run-commit_tx.c b/channeld/test/run-commit_tx.c index 3204a1500..47d18876d 100644 --- a/channeld/test/run-commit_tx.c +++ b/channeld/test/run-commit_tx.c @@ -1057,11 +1057,8 @@ int main(int argc, const char *argv[]) break; } - /* No memory leaks please */ - take_cleanup(); common_shutdown(); - /* FIXME: Do BOLT comparison! */ return 0; } diff --git a/channeld/test/run-full_channel.c b/channeld/test/run-full_channel.c index 429d88a8a..64fdc7aea 100644 --- a/channeld/test/run-full_channel.c +++ b/channeld/test/run-full_channel.c @@ -670,8 +670,6 @@ int main(int argc, const char *argv[]) txs_must_be_eq(txs, txs2); } - /* No memory leaks please */ - take_cleanup(); common_shutdown(); /* FIXME: Do BOLT comparison! */ diff --git a/common/test/run-amount.c b/common/test/run-amount.c index 61110f24d..9080aae45 100644 --- a/common/test/run-amount.c +++ b/common/test/run-amount.c @@ -1,4 +1,5 @@ #include "../amount.c" +#include #include #include #include @@ -82,13 +83,12 @@ void towire_u8_array(u8 **pptr UNNEEDED, const u8 *arr UNNEEDED, size_t num UNNE assert((satp)->satoshis == val); \ } while (0) -int main(void) +int main(int argc, char *argv[]) { struct amount_msat msat; struct amount_sat sat; - setup_locale(); - setup_tmpctx(); + common_setup(argv[0]); /* Grossly malformed */ FAIL_MSAT(&msat, "x"); @@ -233,5 +233,5 @@ int main(void) assert(sat.satoshis == i); } - tal_free(tmpctx); + common_shutdown(); } diff --git a/common/test/run-bigsize.c b/common/test/run-bigsize.c index db9621b84..45f8c9e26 100644 --- a/common/test/run-bigsize.c +++ b/common/test/run-bigsize.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -336,8 +337,7 @@ int main(int argc, char *argv[]) char **lines, *json = NULL; int test_count = 0; - setup_locale(); - setup_tmpctx(); + common_setup(argv[0]); lines = tal_strsplit(tmpctx, grab_file(tmpctx, tal_fmt(tmpctx, "%s.c", argv[0])), @@ -375,5 +375,5 @@ int main(int argc, char *argv[]) tal_append_fmt(&json, "%s", l); } assert(test_count == 2); - tal_free(tmpctx); + common_shutdown(); } diff --git a/common/test/run-bolt11.c b/common/test/run-bolt11.c index c9f1c576c..3ce8aa16b 100644 --- a/common/test/run-bolt11.c +++ b/common/test/run-bolt11.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -134,10 +135,8 @@ static void test_b11(const char *b11str, assert(strlen(reproduce) == strlen(b11str)); } -int main(void) +int main(int argc, char *argv[]) { - setup_locale(); - struct bolt11 *b11; struct node_id node; struct amount_msat msatoshi; @@ -146,9 +145,7 @@ int main(void) char *fail; struct feature_set *fset; - wally_init(0); - secp256k1_ctx = wally_get_secp_context(); - setup_tmpctx(); + common_setup(argv[0]); /* BOLT #11: * @@ -627,8 +624,5 @@ int main(void) assert(streq(fail, "d: invalid utf8")); /* FIXME: Test the others! */ - wally_cleanup(0); - tal_free(tmpctx); - take_cleanup(); - return 0; + common_shutdown(); } diff --git a/common/test/run-cryptomsg.c b/common/test/run-cryptomsg.c index 731b60359..5c2e3bd6a 100644 --- a/common/test/run-cryptomsg.c +++ b/common/test/run-cryptomsg.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -135,16 +136,14 @@ static void check_result(const u8 *msg, const char *hex) assert(streq(hex, tal_hex(tmpctx, msg))); } -int main(void) +int main(int argc, char *argv[]) { - setup_locale(); - struct crypto_state cs_out, cs_in; struct secret sk, rk, ck; const void *msg; size_t i; - setup_tmpctx(); + common_setup(argv[0]); msg = tal_dup_arr(tmpctx, char, "hello", 5, 0); /* BOLT #8: @@ -217,6 +216,6 @@ int main(void) dec = cryptomsg_decrypt_body(enc, &cs_in, enc); assert(memeq(dec, tal_bytelen(dec), msg, tal_bytelen(msg))); } - tal_free(tmpctx); + common_shutdown(); return 0; } diff --git a/common/test/run-derive_basepoints.c b/common/test/run-derive_basepoints.c index 3644581b6..ea4db67e4 100644 --- a/common/test/run-derive_basepoints.c +++ b/common/test/run-derive_basepoints.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -139,15 +140,12 @@ static struct info *new_info(const tal_t *ctx) return info; } -int main(void) +int main(int argc, char *argv[]) { - setup_locale(); - const tal_t *ctx = tal(NULL, char); struct info *baseline, *info; - wally_init(0); - secp256k1_ctx = wally_get_secp_context(); + common_setup(argv[0]); baseline = new_info(ctx); assert(derive_basepoints(&baseline->seed, &baseline->funding_pubkey, &baseline->basepoints, @@ -263,6 +261,6 @@ int main(void) &info->secrets.htlc_basepoint_secret)); tal_free(ctx); - wally_cleanup(0); + common_shutdown(); return 0; } diff --git a/common/test/run-features.c b/common/test/run-features.c index 7f5ff79c2..9ccfae5bc 100644 --- a/common/test/run-features.c +++ b/common/test/run-features.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -248,15 +249,12 @@ static void test_feature_trim(void) } } -int main(void) +int main(int argc, char *argv[]) { u8 *bits; struct feature_set *fset; - setup_locale(); - wally_init(0); - secp256k1_ctx = wally_get_secp_context(); - setup_tmpctx(); + common_setup(argv[0]); /* Just some bits to set. */ fset = feature_set_for_feature(tmpctx, @@ -337,8 +335,6 @@ int main(void) test_feature_trim(); test_feature_set_sub(); - wally_cleanup(0); - tal_free(tmpctx); - take_cleanup(); + common_shutdown(); return 0; } diff --git a/common/test/run-gossip_rcvd_filter.c b/common/test/run-gossip_rcvd_filter.c index 135c4e967..6ed63589f 100644 --- a/common/test/run-gossip_rcvd_filter.c +++ b/common/test/run-gossip_rcvd_filter.c @@ -1,6 +1,7 @@ #include "../gossip_rcvd_filter.c" #include "../pseudorand.c" #include "../../wire/fromwire.c" +#include #include #include @@ -86,13 +87,13 @@ static u8 *mkgossip(const tal_t *ctx, const char *str) return tal_hexdata(ctx, str, strlen(str)); } -int main(void) +int main(int argc, char *argv[]) { const tal_t *ctx = tal(NULL, char); struct gossip_rcvd_filter *f = new_gossip_rcvd_filter(ctx); const u8 *msg[3], *badmsg; - setup_locale(); + common_setup(argv[0]); msg[0] = mkgossip(ctx, "0100231024fcd59aa58ca8e2ed8f71e07843fc576dd6b2872681960ce64f5f3cd3b5386211a103736bf1de2c03a74f5885d50ea30d21a82e4389339ad13149ac7f52942e6ff0778952b7cb001350d1e2edd25cee80c4c64d624a0273be5436923f5524f1f7e4586007203b2f2c47d6863052529321ebb8e0a171ed013c889bbeaa7a462e9826861c608428509804eb4dd5f75dc5e6baa03205933759fd7abcb2e0304b1a895abb7de3d24e92ade99a6a14f51ac9852ef3daf68b8ad40459d8a6124f23e1271537347b6a1bc9bff1f3e6f60e93177b3bf1d53e76771be9c974ba6b1c6d4916762c0867c13f3617e4893f6272c64fa360aaf6c2a94af7739c498bab3600006fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d619000000000008a8090000510000020cf679b34b5819dfbaf68663bdd636c92117b6c04981940e878818b736c0cdb702809e936f0e82dfce13bcc47c77112db068f569e1db29e7bf98bcdd68b838ee8402590349bcd37d04b81022e52bd65d5119a43e0d7b78f526971ede38d2cd1c0d9e02eec6ac38e4acad847cd42b0946977896527b9e1f7dd59525a1a1344a3cea7fa3"); msg[1] = mkgossip(ctx, "0102ccc0a84e4ce09f522f7765db7c30b822ebb346eb17dda92612d03cc8e53ee1454b6c9a918a60ac971e623fd056687f17a01d3c7e805723f7b68be0e8544013546fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d619000000000008a80900005100005d06bacc0102009000000000000003e8000003e8000000010000000005e69ec0"); @@ -192,5 +193,6 @@ int main(void) && tal_next(f->cur) == NULL)); tal_free(ctx); + common_shutdown(); return 0; } diff --git a/common/test/run-ip_port_parsing.c b/common/test/run-ip_port_parsing.c index eaa68e7a1..52a60ffef 100644 --- a/common/test/run-ip_port_parsing.c +++ b/common/test/run-ip_port_parsing.c @@ -5,6 +5,7 @@ #include #include #include +#include /* AUTOGENERATED MOCKS START */ /* Generated stub for amount_asset_is_main */ @@ -109,15 +110,13 @@ void towire_u8_array(u8 **pptr UNNEEDED, const u8 *arr UNNEEDED, size_t num UNNE { fprintf(stderr, "towire_u8_array called!\n"); abort(); } /* AUTOGENERATED MOCKS END */ -int main(void) +int main(int argc, char *argv[]) { - setup_locale(); - struct wireaddr addr; char *ip; u16 port; - setup_tmpctx(); + common_setup(argv[0]); /* Grossly invalid. */ assert(!separate_address_and_port(tmpctx, "[", &ip, &port)); @@ -193,6 +192,6 @@ int main(void) assert(tal_count(wireaddr_from_hostname(tmpctx, "odpzvneidqdf5hdq.onion", 1, NULL, NULL, NULL)) > 0); assert(wireaddr_from_hostname(tmpctx, "aaa.onion", 1, NULL, NULL, NULL) == NULL); - tal_free(tmpctx); + common_shutdown(); return 0; } diff --git a/common/test/run-json.c b/common/test/run-json.c index a932f9f13..4fc7be9cc 100644 --- a/common/test/run-json.c +++ b/common/test/run-json.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -287,17 +288,15 @@ static void test_json_bad_utf8(void) assert(json_parse_simple(tmpctx, buf, strlen(buf))); } -int main(void) +int main(int argc, char *argv[]) { - setup_locale(); - setup_tmpctx(); + common_setup(argv[0]); test_json_tok_size(); test_json_tok_bitcoin_amount(); test_json_tok_millionths(); test_json_delve(); test_json_bad_utf8(); - assert(!taken_any()); - take_cleanup(); - tal_free(tmpctx); + + common_shutdown(); } diff --git a/common/test/run-json_remove.c b/common/test/run-json_remove.c index 04233d074..69b74d7bb 100644 --- a/common/test/run-json_remove.c +++ b/common/test/run-json_remove.c @@ -2,6 +2,7 @@ #include #include #include +#include #include /* AUTOGENERATED MOCKS START */ @@ -264,10 +265,9 @@ static void remove_inside_obj(void) "4", "four", NULL); } -int main(void) +int main(int argc, char *argv[]) { - setup_locale(); - setup_tmpctx(); + common_setup(argv[0]); sanity(); remove_one(); @@ -278,6 +278,6 @@ int main(void) remove_complex(); remove_inside_obj(); - tal_free(tmpctx); printf("run-json_remove ok\n"); + common_shutdown(); } diff --git a/common/test/run-key_derive.c b/common/test/run-key_derive.c index 550bb20d6..1e8400334 100644 --- a/common/test/run-key_derive.c +++ b/common/test/run-key_derive.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -118,17 +119,13 @@ static struct secret secret_from_hex(const char *hex) return s; } -int main(void) +int main(int argc, char *argv[]) { - setup_locale(); - struct privkey privkey; struct secret base_secret, per_commitment_secret; struct pubkey base_point, per_commitment_point, pubkey, pubkey2; - setup_tmpctx(); - secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY - | SECP256K1_CONTEXT_SIGN); + common_setup(argv[0]); base_secret = secret_from_hex("0x000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"); per_commitment_secret = secret_from_hex("0x1f1e1d1c1b1a191817161514131211100f0e0d0c0b0a09080706050403020100"); @@ -178,8 +175,5 @@ int main(void) pubkey_from_privkey(&privkey, &pubkey2); assert(pubkey_eq(&pubkey, &pubkey2)); - /* No memory leaks please */ - secp256k1_context_destroy(secp256k1_ctx); - tal_free(tmpctx); - return 0; + common_shutdown(); } diff --git a/common/test/run-lock.c b/common/test/run-lock.c index 934bd04a6..b0dfa02e6 100644 --- a/common/test/run-lock.c +++ b/common/test/run-lock.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -215,12 +216,11 @@ static bool test_multi_write(const tal_t *ctx) return true; } -int main(void) +int main(int argc, char *argv[]) { bool ok = true; - setup_locale(); - setup_tmpctx(); + common_setup(argv[0]); ok &= test_multi_write(tmpctx); - tal_free(tmpctx); + common_shutdown(); return !ok; } diff --git a/common/test/run-param.c b/common/test/run-param.c index 6f6e22c76..321074571 100644 --- a/common/test/run-param.c +++ b/common/test/run-param.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -590,10 +591,9 @@ static void usage(void) cmd->mode = CMD_NORMAL; } -int main(void) +int main(int argc, char *argv[]) { - setup_locale(); - setup_tmpctx(); + common_setup(argv[0]); cmd = tal(tmpctx, struct command); cmd->mode = CMD_NORMAL; fail_msg = tal_arr(cmd, char, 10000); @@ -615,6 +615,6 @@ int main(void) param_tests(); usage(); - tal_free(tmpctx); printf("run-params ok\n"); + common_shutdown(); } diff --git a/common/test/run-softref.c b/common/test/run-softref.c index 477f1aad6..fcb0ee8ba 100644 --- a/common/test/run-softref.c +++ b/common/test/run-softref.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -109,13 +110,13 @@ struct objtype { char *c_softref, *c_softref2; }; -int main(void) +int main(int argc, char *argv[]) { const void *ctx = tal(NULL, char); struct objtype *o; char *c; - setup_locale(); + common_setup(argv[0]); /* Simple test: freeing obj NULLs softref */ o = tal(ctx, struct objtype); @@ -159,4 +160,5 @@ int main(void) tal_free(o); tal_free(ctx); + common_shutdown(); } diff --git a/common/test/run-sphinx.c b/common/test/run-sphinx.c index 1eac47732..8ef97651e 100644 --- a/common/test/run-sphinx.c +++ b/common/test/run-sphinx.c @@ -3,11 +3,11 @@ #include "../onionreply.c" #include "../sphinx.c" #include -#include #include #include #include #include +#include #include #include #include @@ -208,16 +208,9 @@ static void run_unit_tests(void) int main(int argc, char **argv) { - setup_locale(); - - secp256k1_ctx = secp256k1_context_create( - SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_SIGN); - setup_tmpctx(); - + common_setup(argv[0]); run_unit_tests(); - secp256k1_context_destroy(secp256k1_ctx); - opt_free_table(); - tal_free(tmpctx); + common_shutdown(); return 0; } diff --git a/connectd/test/Makefile b/connectd/test/Makefile index 7b93473b5..06b0faa34 100644 --- a/connectd/test/Makefile +++ b/connectd/test/Makefile @@ -7,6 +7,7 @@ CONNECTD_TEST_PROGRAMS := $(CONNECTD_TEST_OBJS:.o=) CONNECTD_TEST_COMMON_OBJS := \ common/features.o \ common/pseudorand.o \ + common/setup.o \ common/type_to_string.o \ common/utils.o diff --git a/connectd/test/run-initiator-success.c b/connectd/test/run-initiator-success.c index fcbd372b8..80ea2fbb9 100644 --- a/connectd/test/run-initiator-success.c +++ b/connectd/test/run-initiator-success.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -287,8 +288,7 @@ static struct io_plan *success(struct io_conn *conn UNUSED, assert(secret_eq_str(&cs->rk, expect_rk)); /* No memory leaks please */ - secp256k1_context_destroy(secp256k1_ctx); - tal_free(tmpctx); + common_shutdown(); exit(0); } @@ -299,16 +299,11 @@ void ecdh(const struct pubkey *point, struct secret *ss) abort(); } -int main(void) +int main(int argc, char *argv[]) { - setup_locale(); - struct wireaddr_internal dummy; - secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY - | SECP256K1_CONTEXT_SIGN); - setup_tmpctx(); - + common_setup(argv[0]); /* BOLT #8: * diff --git a/connectd/test/run-responder-success.c b/connectd/test/run-responder-success.c index 4eb799445..4b8c52b9a 100644 --- a/connectd/test/run-responder-success.c +++ b/connectd/test/run-responder-success.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -283,9 +284,7 @@ static struct io_plan *success(struct io_conn *conn UNUSED, assert(secret_eq_str(&cs->sk, expect_sk)); assert(secret_eq_str(&cs->rk, expect_rk)); - /* No memory leaks please */ - secp256k1_context_destroy(secp256k1_ctx); - tal_free(tmpctx); + common_shutdown(); exit(0); } @@ -296,16 +295,11 @@ void ecdh(const struct pubkey *point, struct secret *ss) abort(); } -int main(void) +int main(int argc, char *argv[]) { - setup_locale(); - struct wireaddr_internal dummy; - secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY - | SECP256K1_CONTEXT_SIGN); - setup_tmpctx(); - + common_setup(argv[0]); /* BOLT #8: * diff --git a/devtools/Makefile b/devtools/Makefile index 988eb8a06..cf82e83ea 100644 --- a/devtools/Makefile +++ b/devtools/Makefile @@ -38,6 +38,7 @@ DEVTOOLS_COMMON_OBJS := \ common/json.o \ common/json_helpers.o \ common/json_stream.o \ + common/setup.o \ common/type_to_string.o \ common/utils.o \ common/version.o \ diff --git a/devtools/blindedpath.c b/devtools/blindedpath.c index 627d1bb0d..4e47bca47 100644 --- a/devtools/blindedpath.c +++ b/devtools/blindedpath.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -52,10 +53,7 @@ int main(int argc, char **argv) { bool first = false; - setup_locale(); - - secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY | - SECP256K1_CONTEXT_SIGN); + common_setup(argv[0]); opt_set_alloc(opt_allocfn, tal_reallocfn, tal_freefn); opt_register_noarg("--help|-h", opt_usage_and_exit, @@ -69,7 +67,6 @@ int main(int argc, char **argv) opt_register_version(); opt_parse(&argc, argv, opt_log_stderr_exit); - setup_tmpctx(); if (argc < 2) errx(1, "You must specify create or unwrap"); @@ -315,4 +312,6 @@ int main(int argc, char **argv) printf("Next onion: %s\n", tal_hex(tmpctx, serialize_onionpacket(tmpctx, rs->next))); } else errx(1, "Either create or unwrap!"); + + common_shutdown(); } diff --git a/devtools/bolt11-cli.c b/devtools/bolt11-cli.c index 9b6d359ed..7e2d55630 100644 --- a/devtools/bolt11-cli.c +++ b/devtools/bolt11-cli.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -56,8 +57,6 @@ static char *fmt_time(const tal_t *ctx, u64 time) int main(int argc, char *argv[]) { - setup_locale(); - const tal_t *ctx = tal(NULL, char); const char *method; struct bolt11 *b11; @@ -65,9 +64,7 @@ int main(int argc, char *argv[]) size_t i; char *fail, *description = NULL; - err_set_progname(argv[0]); - secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY - | SECP256K1_CONTEXT_SIGN); + common_setup(argv[0]); opt_set_alloc(opt_allocfn, tal_reallocfn, tal_freefn); opt_register_noarg("--help|-h", opt_usage_and_exit, @@ -186,5 +183,6 @@ int main(int argc, char *argv[]) printf("signature: %s\n", type_to_string(ctx, secp256k1_ecdsa_signature, &b11->sig)); tal_free(ctx); + common_shutdown(); return NO_ERROR; } diff --git a/gossipd/test/Makefile b/gossipd/test/Makefile index 2deca9f07..512ff0a0b 100644 --- a/gossipd/test/Makefile +++ b/gossipd/test/Makefile @@ -15,6 +15,7 @@ GOSSIPD_TEST_COMMON_OBJS := \ common/json.o \ common/json_helpers.o \ common/pseudorand.o \ + common/setup.o \ common/type_to_string.o \ common/utils.o \ wire/peer$(EXP)_wiregen.o \ diff --git a/gossipd/test/run-bench-find_route.c b/gossipd/test/run-bench-find_route.c index a05f4a66b..1d1f213fb 100644 --- a/gossipd/test/run-bench-find_route.c +++ b/gossipd/test/run-bench-find_route.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -224,8 +225,6 @@ static void run(const char *name) int main(int argc, char *argv[]) { - setup_locale(); - struct routing_state *rstate; size_t num_nodes = 100, num_runs = 1; struct timemono start, end; @@ -236,9 +235,7 @@ int main(int argc, char *argv[]) const double riskfactor = 0.01 / BLOCKS_PER_YEAR / 10000; struct siphash_seed base_seed; - secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY - | SECP256K1_CONTEXT_SIGN); - setup_tmpctx(); + common_setup(argv[0]); me = nodeid(0); rstate = new_routing_state(tmpctx, &me, NULL, NULL, NULL, @@ -302,8 +299,7 @@ int main(int argc, char *argv[]) if (route_lengths[i]) printf(" Length %zu: %zu\n", i, route_lengths[i]); - tal_free(tmpctx); - secp256k1_context_destroy(secp256k1_ctx); + common_shutdown(); opt_free_table(); return 0; } diff --git a/gossipd/test/run-check_channel_announcement.c b/gossipd/test/run-check_channel_announcement.c index 3d536cf39..57c22933d 100644 --- a/gossipd/test/run-check_channel_announcement.c +++ b/gossipd/test/run-check_channel_announcement.c @@ -30,6 +30,7 @@ In particular, we set feature bit 19. The spec says we should set feature bit 1 #include "../common/wire_error.c" #include "../routing.c" #include +#include #include /* AUTOGENERATED MOCKS START */ @@ -130,7 +131,7 @@ u8 *towire_gossip_store_channel_amount(const tal_t *ctx UNNEEDED, struct amount_ { fprintf(stderr, "towire_gossip_store_channel_amount called!\n"); abort(); } /* AUTOGENERATED MOCKS END */ -int main(void) +int main(int argc, char *argv[]) { struct bitcoin_blkid chain_hash; u8 *features, *err; @@ -141,10 +142,7 @@ int main(void) struct pubkey bitcoin_key_1, bitcoin_key_2; const u8 *cannounce; - setup_locale(); - secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY - | SECP256K1_CONTEXT_SIGN); - setup_tmpctx(); + common_setup(argv[0]); cannounce = tal_hexdata(tmpctx, "010011effc9ed10fceccfae5f9e3fef20d983b06eed030e968fd8d1e6c5905e18f9f2df6a43f00d7c0ddf52e0467ab1e32394051b72ea6343fb008a4117c265f3d7b732bab7df4ee404ac926aef6610f4eb33e31baabfd9afdbf897c8a80057efa1468362b4d2cc0a5482013e1058c8205717f85c3bc82c3ea89f17cfeac21e2cb2ac65b429f79b24fbd51094bee5e080d4c7cfc28a584e279075643054a48b2972f0b72becfd57e03297bf0102b09329982e0ac839dc120959c07456431d3c8fd1430ffe2cc2710e9600e602779c9cf5f91e95874ef4bcf9f0bdda2ce2be97bba562848a2717acdb8dec30bd5073f2f853776cc98f0b6cddc2dcfb57aa69fa7c43400030800006fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d619000000000009984d00063a00010254ff808f53b2f8c45e74b70430f336c6c76ba2f4af289f48d6086ae6e60462d303baa70886d9200af0ffbd3f9e18d96008331c858456b16e3a9b41e735c6208fef03c8731bbac446b7d11b7f5a1861c7d2c87ccf429780c74463de3428dceeb73ad702b3e55c7a1a6cdf17a83a801f7f8f698e4980323e2584f27a643a1b0519ebf8c7", strlen("010011effc9ed10fceccfae5f9e3fef20d983b06eed030e968fd8d1e6c5905e18f9f2df6a43f00d7c0ddf52e0467ab1e32394051b72ea6343fb008a4117c265f3d7b732bab7df4ee404ac926aef6610f4eb33e31baabfd9afdbf897c8a80057efa1468362b4d2cc0a5482013e1058c8205717f85c3bc82c3ea89f17cfeac21e2cb2ac65b429f79b24fbd51094bee5e080d4c7cfc28a584e279075643054a48b2972f0b72becfd57e03297bf0102b09329982e0ac839dc120959c07456431d3c8fd1430ffe2cc2710e9600e602779c9cf5f91e95874ef4bcf9f0bdda2ce2be97bba562848a2717acdb8dec30bd5073f2f853776cc98f0b6cddc2dcfb57aa69fa7c43400030800006fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d619000000000009984d00063a00010254ff808f53b2f8c45e74b70430f336c6c76ba2f4af289f48d6086ae6e60462d303baa70886d9200af0ffbd3f9e18d96008331c858456b16e3a9b41e735c6208fef03c8731bbac446b7d11b7f5a1861c7d2c87ccf429780c74463de3428dceeb73ad702b3e55c7a1a6cdf17a83a801f7f8f698e4980323e2584f27a643a1b0519ebf8c7")); if (!fromwire_channel_announcement(cannounce, cannounce, @@ -196,7 +194,6 @@ int main(void) assert(memmem(err, tal_bytelen(err), "Bad node_signature_2", strlen("Bad node_signature_2"))); - tal_free(tmpctx); - secp256k1_context_destroy(secp256k1_ctx); + common_shutdown(); return 0; } diff --git a/gossipd/test/run-crc32_of_update.c b/gossipd/test/run-crc32_of_update.c index c808bcafe..88f1ae0c5 100644 --- a/gossipd/test/run-crc32_of_update.c +++ b/gossipd/test/run-crc32_of_update.c @@ -134,7 +134,7 @@ bool wire_sync_write(int fd UNNEEDED, const void *msg TAKES UNNEEDED) { fprintf(stderr, "wire_sync_write called!\n"); abort(); } /* AUTOGENERATED MOCKS END */ -int main(void) +int main(int argc, char *argv[]) { u8 *update; setup_locale(); diff --git a/gossipd/test/run-find_route-specific.c b/gossipd/test/run-find_route-specific.c index 110a07a20..e64f60af3 100644 --- a/gossipd/test/run-find_route-specific.c +++ b/gossipd/test/run-find_route-specific.c @@ -6,6 +6,7 @@ * {'channels': [{'active': True, 'short_id': '6990x2x1/1', 'fee_per_kw': 10, 'delay': 5, 'message_flags': 0, 'channel_flags': 1, 'destination': '0230ad0e74ea03976b28fda587bb75bdd357a1938af4424156a18265167f5e40ae', 'source': '02ea622d5c8d6143f15ed3ce1d501dd0d3d09d3b1c83a44d0034949f8a9ab60f06', 'last_update': 1504064344}, {'active': True, 'short_id': '6989x2x1/0', 'fee_per_kw': 10, 'delay': 5, 'message_flags': 0, 'channel_flags': 0, 'destination': '03c173897878996287a8100469f954dd820fcd8941daed91c327f168f3329be0bf', 'source': '0230ad0e74ea03976b28fda587bb75bdd357a1938af4424156a18265167f5e40ae', 'last_update': 1504064344}, {'active': True, 'short_id': '6990x2x1/0', 'fee_per_kw': 10, 'delay': 5, 'message_flags': 0, 'channel_flags': 0, 'destination': '02ea622d5c8d6143f15ed3ce1d501dd0d3d09d3b1c83a44d0034949f8a9ab60f06', 'source': '0230ad0e74ea03976b28fda587bb75bdd357a1938af4424156a18265167f5e40ae', 'last_update': 1504064344}, {'active': True, 'short_id': '6989x2x1/1', 'fee_per_kw': 10, 'delay': 5, 'message_flags': 0, 'channel_flags': 1, 'destination': '0230ad0e74ea03976b28fda587bb75bdd357a1938af4424156a18265167f5e40ae', 'source': '03c173897878996287a8100469f954dd820fcd8941daed91c327f168f3329be0bf', 'last_update': 1504064344}]} */ #include +#include #include #include @@ -166,10 +167,8 @@ static bool channel_is_between(const struct chan *chan, return false; } -int main(void) +int main(int argc, char *argv[]) { - setup_locale(); - struct half_chan *nc; struct routing_state *rstate; struct node_id a, b, c, d; @@ -177,9 +176,7 @@ int main(void) struct chan **route; const double riskfactor = 1.0 / BLOCKS_PER_YEAR / 10000; - secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY - | SECP256K1_CONTEXT_SIGN); - setup_tmpctx(); + common_setup(argv[0]); node_id_from_hexstr("03c173897878996287a8100469f954dd820fcd8941daed91c327f168f3329be0bf", strlen("03c173897878996287a8100469f954dd820fcd8941daed91c327f168f3329be0bf"), @@ -281,7 +278,6 @@ int main(void) ROUTING_MAX_HOPS, &fee); assert(!route); - tal_free(tmpctx); - secp256k1_context_destroy(secp256k1_ctx); + common_shutdown(); return 0; } diff --git a/gossipd/test/run-find_route.c b/gossipd/test/run-find_route.c index d9edf652c..4b60544fb 100644 --- a/gossipd/test/run-find_route.c +++ b/gossipd/test/run-find_route.c @@ -1,6 +1,7 @@ #include "../routing.c" #include "../gossip_store.c" #include +#include #include void status_fmt(enum log_level level UNUSED, @@ -214,10 +215,8 @@ static bool channel_is_between(const struct chan *chan, return false; } -int main(void) +int main(int argc, char *argv[]) { - setup_locale(); - struct routing_state *rstate; struct node_id a, b, c, d; struct privkey tmp; @@ -225,9 +224,7 @@ int main(void) struct chan **route; const double riskfactor = 1.0 / BLOCKS_PER_YEAR / 10000; - secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY - | SECP256K1_CONTEXT_SIGN); - setup_tmpctx(); + common_setup(argv[0]); memset(&tmp, 'a', sizeof(tmp)); node_id_from_privkey(&tmp, &a); @@ -301,7 +298,6 @@ int main(void) assert(channel_is_between(route[1], &d, &c)); assert(amount_msat_eq(fee, AMOUNT_MSAT(0 + 6))); - tal_free(tmpctx); - secp256k1_context_destroy(secp256k1_ctx); + common_shutdown(); return 0; } diff --git a/gossipd/test/run-next_block_range.c b/gossipd/test/run-next_block_range.c index 94eda220b..39d9eb962 100644 --- a/gossipd/test/run-next_block_range.c +++ b/gossipd/test/run-next_block_range.c @@ -109,7 +109,7 @@ static void test_block_range(struct seeker *seeker, va_end(ap); } -int main(void) +int main(int argc, char *argv[]) { struct seeker *seeker = tal(NULL, struct seeker); diff --git a/gossipd/test/run-overlong.c b/gossipd/test/run-overlong.c index 0b2717049..1f1ea365f 100644 --- a/gossipd/test/run-overlong.c +++ b/gossipd/test/run-overlong.c @@ -1,6 +1,7 @@ #include "../routing.c" #include "../gossip_store.c" #include +#include #include void status_fmt(enum log_level level UNUSED, @@ -136,18 +137,14 @@ static void node_id_from_privkey(const struct privkey *p, struct node_id *id) /* We create an arrangement of nodes, each node N connected to N+1 and * to node 1. The cost for each N to N+1 route is 1, for N to 1 is * 2^N. That means it's always cheapest to go the longer route */ -int main(void) +int main(int argc, char *argv[]) { - setup_locale(); - struct routing_state *rstate; struct node_id ids[NUM_NODES]; struct chan **route; struct amount_msat last_fee; - secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY - | SECP256K1_CONTEXT_SIGN); - setup_tmpctx(); + common_setup(argv[0]); for (size_t i = 0; i < NUM_NODES; i++) { struct privkey tmp; @@ -221,7 +218,6 @@ int main(void) last_fee = fee; } - tal_free(tmpctx); - secp256k1_context_destroy(secp256k1_ctx); + common_shutdown(); return 0; } diff --git a/gossipd/test/run-txout_failure.c b/gossipd/test/run-txout_failure.c index 87d06cb8b..793adc7ae 100644 --- a/gossipd/test/run-txout_failure.c +++ b/gossipd/test/run-txout_failure.c @@ -1,6 +1,7 @@ #include "../routing.c" #include "../common/timeout.c" #include +#include #include /* AUTOGENERATED MOCKS START */ @@ -107,15 +108,14 @@ struct gossip_store *gossip_store_new(struct routing_state *rstate UNNEEDED, return NULL; } -int main(void) +int main(int argc, char *argv[]) { struct routing_state *rstate; struct timers timers; struct timer *t; struct short_channel_id scid1, scid2; - setup_locale(); - setup_tmpctx(); + common_setup(argv[0]); timers_init(&timers, time_mono()); /* Random uninitalized node_id, we don't reference it. */ @@ -158,7 +158,7 @@ int main(void) assert(rstate->num_txout_failures == 1); assert(!in_txout_failures(rstate, &scid2)); - tal_free(tmpctx); + common_shutdown(); timers_cleanup(&timers); return 0; } diff --git a/lightningd/test/Makefile b/lightningd/test/Makefile index d43a2a483..da39178dd 100644 --- a/lightningd/test/Makefile +++ b/lightningd/test/Makefile @@ -19,6 +19,7 @@ LIGHTNINGD_TEST_COMMON_OBJS := \ common/random_select.o \ common/memleak.o \ common/msg_queue.o \ + common/setup.o \ common/utils.o \ common/utxo.o \ common/type_to_string.o \ diff --git a/lightningd/test/run-invoice-select-inchan.c b/lightningd/test/run-invoice-select-inchan.c index ce649a7c6..d1931eac6 100644 --- a/lightningd/test/run-invoice-select-inchan.c +++ b/lightningd/test/run-invoice-select-inchan.c @@ -3,6 +3,7 @@ #include "../routehint.c" #include #include +#include bool deprecated_apis = false; @@ -768,17 +769,14 @@ STRUCTEQ_DEF(route_info, fee_base_msat, fee_proportional_millionths); -int main(void) +int main(int argc, char *argv[]) { struct lightningd *ld; struct routehint_candidate *candidates; struct route_info **ret; size_t n; - setup_locale(); - secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY - | SECP256K1_CONTEXT_SIGN); - setup_tmpctx(); + common_setup(argv[0]); ld = tal(tmpctx, struct lightningd); list_head_init(&ld->peers); @@ -856,9 +854,7 @@ int main(void) n, 1000 - n); assert(n > 250 - 50 && n < 250 + 50); - /* No memory leaks please */ - secp256k1_context_destroy(secp256k1_ctx); - tal_free(tmpctx); + common_shutdown(); /* FIXME: Do BOLT comparison! */ return 0; diff --git a/lightningd/test/run-jsonrpc.c b/lightningd/test/run-jsonrpc.c index 57e9c9630..94d76b9d5 100644 --- a/lightningd/test/run-jsonrpc.c +++ b/lightningd/test/run-jsonrpc.c @@ -1,6 +1,7 @@ #include "../../common/json_stream.c" #include "../jsonrpc.c" #include "../json.c" +#include /* AUTOGENERATED MOCKS START */ /* Generated stub for db_begin_transaction_ */ @@ -295,14 +296,14 @@ static void test_json_stream(void) tal_free(toks); } -int main(void) +int main(int argc, char *argv[]) { - setup_locale(); + common_setup(argv[0]); test_json_filter(); test_json_escape(); test_json_partial(); test_json_stream(); - assert(!taken_any()); - take_cleanup(); + + common_shutdown(); } diff --git a/wallet/db_postgres_sqlgen.c b/wallet/db_postgres_sqlgen.c index d99fb238b..3078165f9 100644 --- a/wallet/db_postgres_sqlgen.c +++ b/wallet/db_postgres_sqlgen.c @@ -1690,4 +1690,4 @@ struct db_query db_postgres_queries[] = { #endif /* LIGHTNINGD_WALLET_GEN_DB_POSTGRES */ -// SHA256STAMP:756457b3587828d8b4226c2787cbadc1605669371ac57bb92c0b21e434bd80e1 +// SHA256STAMP:a61b8b6ea86287e22c696530a9b5cd35ccef271280b8ac34d2665dc5aff42fce diff --git a/wallet/db_sqlite3_sqlgen.c b/wallet/db_sqlite3_sqlgen.c index 7d0629c76..ba6043ced 100644 --- a/wallet/db_sqlite3_sqlgen.c +++ b/wallet/db_sqlite3_sqlgen.c @@ -1690,4 +1690,4 @@ struct db_query db_sqlite3_queries[] = { #endif /* LIGHTNINGD_WALLET_GEN_DB_SQLITE3 */ -// SHA256STAMP:756457b3587828d8b4226c2787cbadc1605669371ac57bb92c0b21e434bd80e1 +// SHA256STAMP:a61b8b6ea86287e22c696530a9b5cd35ccef271280b8ac34d2665dc5aff42fce diff --git a/wallet/statements_gettextgen.po b/wallet/statements_gettextgen.po index e944c3a21..65a4da937 100644 --- a/wallet/statements_gettextgen.po +++ b/wallet/statements_gettextgen.po @@ -1102,15 +1102,15 @@ msgstr "" msgid "DELETE FROM penalty_bases WHERE channel_id = ? AND commitnum = ?" msgstr "" -#: wallet/test/run-db.c:119 +#: wallet/test/run-db.c:120 msgid "SELECT name FROM sqlite_master WHERE type='table';" msgstr "" -#: wallet/test/run-db.c:124 +#: wallet/test/run-db.c:125 msgid "not a valid SQL statement" msgstr "" #: wallet/test/run-wallet.c:1376 msgid "INSERT INTO channels (id) VALUES (1);" msgstr "" -# SHA256STAMP:8fd4a9d444c885db744f98790817b0b580f3e39e36be393e578cc05418d25323 +# SHA256STAMP:a63bd31a3977b161d8245ffddf5de3a03c972a60436eb8cbc1bb23f0ffb03405 diff --git a/wallet/test/run-db.c b/wallet/test/run-db.c index 0e82018fb..78bf2c596 100644 --- a/wallet/test/run-db.c +++ b/wallet/test/run-db.c @@ -14,6 +14,7 @@ static void db_log_(struct log *log UNUSED, enum log_level level UNUSED, const s #include #include +#include #include #include @@ -162,14 +163,13 @@ static bool test_vars(struct lightningd *ld) return true; } -int main(void) +int main(int argc, char *argv[]) { - setup_locale(); - setup_tmpctx(); - bool ok = true; /* Dummy for migration hooks */ struct lightningd *ld = tal(NULL, struct lightningd); + + common_setup(argv[0]); ld->config = test_config; ok &= test_empty_db_migrate(ld); @@ -177,6 +177,6 @@ int main(void) ok &= test_primitives(); tal_free(ld); - tal_free(tmpctx); + common_shutdown(); return !ok; } diff --git a/wire/test/Makefile b/wire/test/Makefile index 154f92ea3..ebfc080fd 100644 --- a/wire/test/Makefile +++ b/wire/test/Makefile @@ -10,6 +10,7 @@ ALL_C_SOURCES += $(WIRE_TEST_SRC) ALL_TEST_PROGRAMS += $(WIRE_TEST_PROGRAMS) WIRE_TEST_COMMON_OBJS := \ + common/setup.o \ common/utils.o update-mocks: $(WIRE_TEST_SRC:%=update-mocks/%) diff --git a/wire/test/run-peer-wire.c b/wire/test/run-peer-wire.c index a9acefb93..62b3124c8 100644 --- a/wire/test/run-peer-wire.c +++ b/wire/test/run-peer-wire.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -955,10 +956,8 @@ static bool node_announcement_eq(const struct msg_node_announcement *a, #define test_corruption_tlv(a, b, type) \ test_bitflip_and_short(a, b, type, false) -int main(void) +int main(int argc, char *argv[]) { - setup_locale(); - struct msg_channel_announcement ca, *ca2; struct msg_funding_locked fl, *fl2; struct msg_announcement_signatures as, *as2; @@ -984,8 +983,7 @@ int main(void) u8 *msg; const struct chainparams **chains; - secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY - | SECP256K1_CONTEXT_SIGN); + common_setup(argv[0]); memset(&ca, 2, sizeof(ca)); set_node_id(&ca.node_id_1); @@ -1170,8 +1168,7 @@ int main(void) assert(node_announcement_eq(&na, na2)); test_corruption(&na, na2, node_announcement); - /* No memory leaks please */ - secp256k1_context_destroy(secp256k1_ctx); tal_free(ctx); + common_shutdown(); return 0; } diff --git a/wire/test/run-tlvstream.c b/wire/test/run-tlvstream.c index f7991edb6..51d3cfb09 100644 --- a/wire/test/run-tlvstream.c +++ b/wire/test/run-tlvstream.c @@ -12,6 +12,7 @@ static const char *reason; #include #include #include +#include #if EXPERIMENTAL_FEATURES #include @@ -445,13 +446,9 @@ static bool ignored_fields(const struct tlv_n1 *tlv_n1) && tlv_n1->tlv4 == NULL; } -int main(void) +int main(int argc, char *argv[]) { - setup_locale(); - wally_init(0); - secp256k1_ctx = wally_get_secp_context(); - - setup_tmpctx(); + common_setup(argv[0]); if (!pubkey_from_hexstr("023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb", 66, &tlv3_node_id.node_id)) abort(); @@ -636,6 +633,5 @@ int main(void) p2, tal_count(p2))); } } - tal_free(tmpctx); - wally_cleanup(0); + common_shutdown(); }