From 57328fe59ec209fb31bc082cf61462dc83fc95fd Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 15 Nov 2021 04:22:46 +1030 Subject: [PATCH] tests: use common_setup/common_shutdown to avoid leaks. It also does more checks (like taken() checks). Signed-off-by: Rusty Russell --- cli/test/Makefile | 1 + cli/test/run-human-mode.c | 5 +++-- cli/test/run-large-input.c | 6 +++--- cli/test/run-remove-hint.c | 5 +++-- common/test/run-bolt12_decode.c | 10 +++++----- common/test/run-bolt12_merkle-json.c | 5 +++-- common/test/run-gossmap-fp16.c | 7 +++++-- common/test/run-route-specific.c | 13 ++++--------- common/test/run-route.c | 11 ++++------- gossipd/test/run-crc32_of_update.c | 4 +++- gossipd/test/run-extended-info.c | 6 ++++-- gossipd/test/run-next_block_range.c | 4 +++- lightningd/test/run-find_my_abspath.c | 10 +++++----- lightningd/test/run-log-pruning.c | 7 +++++-- onchaind/test/run-grind_feerate-bug.c | 5 +++-- plugins/test/run-route-overlong.c | 12 ++++-------- wallet/test/run-wallet.c | 3 ++- 17 files changed, 60 insertions(+), 54 deletions(-) diff --git a/cli/test/Makefile b/cli/test/Makefile index 14daeb11e..e49837f22 100644 --- a/cli/test/Makefile +++ b/cli/test/Makefile @@ -16,6 +16,7 @@ CLI_TEST_COMMON_OBJS := \ common/pseudorand.o \ common/memleak.o \ common/msg_queue.o \ + common/setup.o \ common/utils.o \ common/type_to_string.o \ common/permute_tx.o diff --git a/cli/test/run-human-mode.c b/cli/test/run-human-mode.c index d0dcea17e..a3a122b1d 100644 --- a/cli/test/run-human-mode.c +++ b/cli/test/run-human-mode.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -162,7 +163,7 @@ ssize_t test_read(int fd UNUSED, void *buf, size_t len) int main(int argc UNUSED, char *argv[]) { - setup_locale(); + common_setup(argv[0]); char *fake_argv[] = { argv[0], "--lightning-dir=/tmp/", "-H", "listconfigs", "-N", "none", NULL }; @@ -170,7 +171,7 @@ int main(int argc UNUSED, char *argv[]) max_read_return = -1; assert(test_main(6, fake_argv) == 0); assert(!taken_any()); - take_cleanup(); + common_shutdown(); return 0; } diff --git a/cli/test/run-large-input.c b/cli/test/run-large-input.c index dec5bc571..7bb642333 100644 --- a/cli/test/run-large-input.c +++ b/cli/test/run-large-input.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -171,7 +172,7 @@ ssize_t test_read(int fd UNUSED, void *buf, size_t len) int main(int argc UNUSED, char *argv[]) { - setup_locale(); + common_setup(argv[0]); char *fake_argv[] = { argv[0], "--lightning-dir=/tmp/", "test", "-N", "none", NULL }; @@ -200,7 +201,6 @@ int main(int argc UNUSED, char *argv[]) max_read_return = -1; assert(test_main(5, fake_argv) == 0); tal_free(response); - assert(!taken_any()); - take_cleanup(); + common_shutdown(); return 0; } diff --git a/cli/test/run-remove-hint.c b/cli/test/run-remove-hint.c index c31bebc8e..3311a9aec 100644 --- a/cli/test/run-remove-hint.c +++ b/cli/test/run-remove-hint.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -168,7 +169,7 @@ int test_chdir(const char *path) int main(int argc UNUSED, char *argv[]) { - setup_locale(); + common_setup(argv[0]); char *fake_argv[] = { argv[0], "--lightning-dir=/tmp/", "test", "-N", "none", NULL }; @@ -190,6 +191,6 @@ int main(int argc UNUSED, char *argv[]) "num_connected=1\n")); tal_free(output); assert(!taken_any()); - take_cleanup(); + common_shutdown(); return 0; } diff --git a/common/test/run-bolt12_decode.c b/common/test/run-bolt12_decode.c index 0cac9920e..a975a253f 100644 --- a/common/test/run-bolt12_decode.c +++ b/common/test/run-bolt12_decode.c @@ -6,6 +6,7 @@ #include #include #include +#include bool deprecated_apis = false; @@ -174,8 +175,7 @@ int main(int argc, char *argv[]) jsmntok_t toks[5000]; const jsmntok_t *t; - setup_locale(); - setup_tmpctx(); + common_setup(argv[0]); if (argv[1]) json = grab_file(tmpctx, argv[1]); @@ -187,8 +187,7 @@ int main(int argc, char *argv[]) "bolt12/format-string-test.json")); if (!json) { printf("test file not found, skipping\n"); - tal_free(tmpctx); - exit(0); + goto out; } } @@ -213,6 +212,7 @@ int main(int argc, char *argv[]) "lno", &dlen, &fail) != NULL); assert(actual == valid); } - tal_free(tmpctx); +out: + common_shutdown(); return 0; } diff --git a/common/test/run-bolt12_merkle-json.c b/common/test/run-bolt12_merkle-json.c index e242ada25..1fb1b9916 100644 --- a/common/test/run-bolt12_merkle-json.c +++ b/common/test/run-bolt12_merkle-json.c @@ -129,8 +129,7 @@ int main(int argc, char *argv[]) "bolt12/merkle-test.json")); if (!json) { printf("test file not found, skipping\n"); - tal_free(tmpctx); - exit(0); + goto out; } } @@ -179,6 +178,8 @@ int main(int argc, char *argv[]) abort(); printf(" - WRAPPED OK\n"); } + +out: common_shutdown(); return 0; } diff --git a/common/test/run-gossmap-fp16.c b/common/test/run-gossmap-fp16.c index 8c3ba923d..5b96d3681 100644 --- a/common/test/run-gossmap-fp16.c +++ b/common/test/run-gossmap-fp16.c @@ -1,4 +1,5 @@ #include "../fp16.c" +#include #include #include #include @@ -106,12 +107,12 @@ 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[]) { /* 5 bit exponent, 11 bit mantissa. */ u32 exponent, mantissa; - setup_locale(); + common_setup(argv[0]); /* These can be represented exactly. */ for (exponent = 0; exponent < (1 << 5); exponent++) { @@ -140,4 +141,6 @@ int main(void) /* Round up works, even if it causes overflow. */ assert(fp16_to_u64(u64_to_fp16(0xffffffff, true)) == (1ULL << 32)); + + common_shutdown(); } diff --git a/common/test/run-route-specific.c b/common/test/run-route-specific.c index 6998ca1ab..b0e80ba30 100644 --- a/common/test/run-route-specific.c +++ b/common/test/run-route-specific.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -180,10 +181,8 @@ static bool route_can_carry_unless_disabled(const struct gossmap *map, return route_can_carry_even_disabled(map, c, dir, amount, arg); } -int main(void) +int main(int argc, char *argv[]) { - setup_locale(); - struct node_id a, b, c, d; struct gossmap_node *a_node, *b_node, *c_node, *d_node; const struct dijkstra *dij; @@ -194,10 +193,7 @@ int main(void) char gossip_version = GOSSIP_STORE_VERSION; char gossipfilename[] = "/tmp/run-route-specific-gossipstore.XXXXXX"; - secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY - | SECP256K1_CONTEXT_SIGN); - setup_tmpctx(); - + common_setup(argv[0]); node_id_from_hexstr("03c173897878996287a8100469f954dd820fcd8941daed91c327f168f3329be0bf", strlen("03c173897878996287a8100469f954dd820fcd8941daed91c327f168f3329be0bf"), &a); @@ -309,7 +305,6 @@ int main(void) AMOUNT_MSAT(499968+1), 0); assert(!route); - tal_free(tmpctx); - secp256k1_context_destroy(secp256k1_ctx); + common_shutdown(); return 0; } diff --git a/common/test/run-route.c b/common/test/run-route.c index fb9d756a4..53dd81a93 100644 --- a/common/test/run-route.c +++ b/common/test/run-route.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -174,9 +175,9 @@ static void node_id_from_privkey(const struct privkey *p, struct node_id *id) node_id_from_pubkey(id, &k); } -int main(void) +int main(int argc, char *argv[]) { - setup_locale(); + common_setup(argv[0]); struct node_id a, b, c, d; struct gossmap_node *a_node, *b_node, *c_node, *d_node; @@ -189,9 +190,6 @@ int main(void) char gossip_version = GOSSIP_STORE_VERSION; char gossipfilename[] = "/tmp/run-route-gossipstore.XXXXXX"; - secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY - | SECP256K1_CONTEXT_SIGN); - setup_tmpctx(); chainparams = chainparams_for_network("regtest"); store_fd = mkstemp(gossipfilename); @@ -313,7 +311,6 @@ int main(void) assert(amount_msat_eq(route[0].amount, AMOUNT_MSAT(3000000 + 6))); assert(route[0].delay == 15); - 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 82033c8d3..2d59a9ca0 100644 --- a/gossipd/test/run-crc32_of_update.c +++ b/gossipd/test/run-crc32_of_update.c @@ -5,6 +5,7 @@ int unused_main(int argc, char *argv[]); #undef main #include #include +#include #include /* AUTOGENERATED MOCKS START */ @@ -148,7 +149,7 @@ bool wire_sync_write(int fd UNNEEDED, const void *msg TAKES UNNEEDED) int main(int argc, char *argv[]) { u8 *update; - setup_locale(); + common_setup(argv[0]); update = tal_hexdata(NULL, "010276df7e70c63cc2b63ef1c062b99c6d934a80ef2fd4dae9e1d86d277f47674af3255a97fa52ade7f129263f591ed784996eba6383135896cc117a438c8029328206226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f00006700000100005d50f933000000900000000000000000000003e80000000a", strlen("010276df7e70c63cc2b63ef1c062b99c6d934a80ef2fd4dae9e1d86d277f47674af3255a97fa52ade7f129263f591ed784996eba6383135896cc117a438c8029328206226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f00006700000100005d50f933000000900000000000000000000003e80000000a")); @@ -159,5 +160,6 @@ int main(int argc, char *argv[]) strlen("010206737e9e18d3e4d0ab4066ccaecdcc10e648c5f1c5413f1610747e0d463fa7fa39c1b02ea2fd694275ecfefe4fe9631f24afd182ab75b805e16cd550941f858c06226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f00006d00000100005d50f935010000300000000000000000000000640000000b00000000000186a0")); assert(crc32_of_update(update) == 0xf32ce968); tal_free(update); + common_shutdown(); return 0; } diff --git a/gossipd/test/run-extended-info.c b/gossipd/test/run-extended-info.c index 38cfc3fbc..276caf655 100644 --- a/gossipd/test/run-extended-info.c +++ b/gossipd/test/run-extended-info.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #ifdef NDEBUG @@ -491,11 +492,11 @@ static u8 *test_query_short_channel_ids(const char *test_vector, return msg; } -int main(void) +int main(int argc, char *argv[]) { jsmntok_t *toks = tal_arr(NULL, jsmntok_t, 1000); - setup_locale(); + common_setup(argv[0]); for (size_t i = 0; i < ARRAY_SIZE(test_vectors); i++) { jsmn_parser parser; @@ -524,5 +525,6 @@ int main(void) tal_free(m); } tal_free(toks); + common_shutdown(); return 0; } diff --git a/gossipd/test/run-next_block_range.c b/gossipd/test/run-next_block_range.c index e193d3f5d..c57081c1f 100644 --- a/gossipd/test/run-next_block_range.c +++ b/gossipd/test/run-next_block_range.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include @@ -117,7 +118,7 @@ int main(int argc, char *argv[]) { struct seeker *seeker = tal(NULL, struct seeker); - setup_locale(); + common_setup(argv[0]); seeker->daemon = tal(seeker, struct daemon); @@ -152,4 +153,5 @@ int main(int argc, char *argv[]) -1); tal_free(seeker); + common_shutdown(); } diff --git a/lightningd/test/run-find_my_abspath.c b/lightningd/test/run-find_my_abspath.c index 411b3add8..02605e39a 100644 --- a/lightningd/test/run-find_my_abspath.c +++ b/lightningd/test/run-find_my_abspath.c @@ -5,6 +5,7 @@ int unused_main(int argc, char *argv[]); #include "../io_loop_with_timers.c" #include "../lightningd.c" #include "../subd.c" +#include /* AUTOGENERATED MOCKS START */ /* Generated stub for activate_peers */ @@ -245,15 +246,14 @@ struct wallet *wallet_new(struct lightningd *ld UNNEEDED, struct timers *timers struct log *crashlog; #undef main -int main(int argc UNUSED, char *argv[] UNUSED) +int main(int argc UNUSED, char *argv[]) { - setup_locale(); + common_setup(argv[0]); char *argv0; /* We're assuming we're run from top build dir. */ const char *answer; - setup_tmpctx(); answer = path_canon(tmpctx, "lightningd/test/run-find_my_abspath"); /* Various different ways we could find ourselves. */ @@ -286,7 +286,7 @@ int main(int argc UNUSED, char *argv[] UNUSED) assert(streq(find_my_abspath(tmpctx, argv0), answer)); assert(!taken_any()); - take_cleanup(); - tal_free(tmpctx); + common_shutdown(); + return 0; } diff --git a/lightningd/test/run-log-pruning.c b/lightningd/test/run-log-pruning.c index f1150f276..c59d9d263 100644 --- a/lightningd/test/run-log-pruning.c +++ b/lightningd/test/run-log-pruning.c @@ -1,4 +1,5 @@ #include "../log.c" +#include /* AUTOGENERATED MOCKS START */ /* Generated stub for command_fail */ @@ -87,12 +88,13 @@ void towire_node_id(u8 **pptr UNNEEDED, const struct node_id *id UNNEEDED) { fprintf(stderr, "towire_node_id called!\n"); abort(); } /* AUTOGENERATED MOCKS END */ -int main(void) +int main(int argc, char *argv[]) { struct log_book *lb; struct log *l; - setup_locale(); + common_setup(argv[0]); + lb = new_log_book(NULL, (sizeof(struct log_entry) + sizeof("test XXXXXX")) *100); @@ -133,4 +135,5 @@ int main(void) /* Freeing (last) log frees logbook */ tal_free(l); + common_shutdown(); } diff --git a/onchaind/test/run-grind_feerate-bug.c b/onchaind/test/run-grind_feerate-bug.c index 17b360244..1798dc456 100644 --- a/onchaind/test/run-grind_feerate-bug.c +++ b/onchaind/test/run-grind_feerate-bug.c @@ -307,8 +307,10 @@ u8 *wire_sync_read(const tal_t *ctx UNNEEDED, int fd UNNEEDED) return (u8 *)ctx; } -bool wire_sync_write(int fd UNNEEDED, const void *msg TAKES UNNEEDED) +bool wire_sync_write(int fd UNNEEDED, const void *msg TAKES) { + if (taken(msg)) + tal_free(msg); return true; } @@ -422,6 +424,5 @@ int main(int argc, char *argv[]) htlc_scripts, false); assert(ret == 2); - take_cleanup(); common_shutdown(); } diff --git a/plugins/test/run-route-overlong.c b/plugins/test/run-route-overlong.c index b11c64736..3b4159ce2 100644 --- a/plugins/test/run-route-overlong.c +++ b/plugins/test/run-route-overlong.c @@ -1,6 +1,7 @@ #include "../libplugin-pay.c" #include #include +#include #include #include @@ -324,10 +325,8 @@ 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 node_id ids[NUM_NODES]; int store_fd; struct payment *p; @@ -335,9 +334,7 @@ int main(void) char gossip_version = GOSSIP_STORE_VERSION; char gossipfilename[] = "/tmp/run-route-overlong.XXXXXX"; - secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY - | SECP256K1_CONTEXT_SIGN); - setup_tmpctx(); + common_setup(argv[0]); chainparams = chainparams_for_network("regtest"); store_fd = mkstemp(gossipfilename); assert(write(store_fd, &gossip_version, sizeof(gossip_version)) @@ -401,7 +398,6 @@ int main(void) assert(tal_count(r) == 2); } - tal_free(tmpctx); - secp256k1_context_destroy(secp256k1_ctx); + common_shutdown(); return 0; } diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 8b927e855..4463d9c29 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -1082,6 +1082,8 @@ static bool test_wallet_outputs(struct lightningd *ld, const tal_t *ctx) 5), "wallet_add_utxo with close_info and csv > 1"); CHECK_MSG(!wallet_err, wallet_err); + /* Normally freed by destroy_channel, but we don't call that */ + tal_free(channel.peer); /* Select everything but 5 csv-locked utxo */ utxos = tal_arr(w, const struct utxo *, 0); @@ -1890,7 +1892,6 @@ int main(int argc, const char *argv[]) /* Do not clean up in the case of an error, we might want to debug the * database. */ if (ok) { - take_cleanup(); common_shutdown(); } return !ok;