tests: use common_setup/common_shutdown to avoid leaks.

It also does more checks (like taken() checks).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2021-11-15 04:22:46 +10:30
parent 68043c2e8c
commit 57328fe59e
17 changed files with 60 additions and 54 deletions

View file

@ -16,6 +16,7 @@ CLI_TEST_COMMON_OBJS := \
common/pseudorand.o \ common/pseudorand.o \
common/memleak.o \ common/memleak.o \
common/msg_queue.o \ common/msg_queue.o \
common/setup.o \
common/utils.o \ common/utils.o \
common/type_to_string.o \ common/type_to_string.o \
common/permute_tx.o common/permute_tx.o

View file

@ -4,6 +4,7 @@
#include <common/channel_id.h> #include <common/channel_id.h>
#include <common/json_stream.h> #include <common/json_stream.h>
#include <common/node_id.h> #include <common/node_id.h>
#include <common/setup.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/socket.h> #include <sys/socket.h>
@ -162,7 +163,7 @@ ssize_t test_read(int fd UNUSED, void *buf, size_t len)
int main(int argc UNUSED, char *argv[]) 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 }; 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; max_read_return = -1;
assert(test_main(6, fake_argv) == 0); assert(test_main(6, fake_argv) == 0);
assert(!taken_any()); assert(!taken_any());
take_cleanup(); common_shutdown();
return 0; return 0;
} }

View file

@ -4,6 +4,7 @@
#include <common/channel_id.h> #include <common/channel_id.h>
#include <common/json_stream.h> #include <common/json_stream.h>
#include <common/node_id.h> #include <common/node_id.h>
#include <common/setup.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/socket.h> #include <sys/socket.h>
@ -171,7 +172,7 @@ ssize_t test_read(int fd UNUSED, void *buf, size_t len)
int main(int argc UNUSED, char *argv[]) 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 }; 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; max_read_return = -1;
assert(test_main(5, fake_argv) == 0); assert(test_main(5, fake_argv) == 0);
tal_free(response); tal_free(response);
assert(!taken_any()); common_shutdown();
take_cleanup();
return 0; return 0;
} }

View file

@ -4,6 +4,7 @@
#include <common/channel_id.h> #include <common/channel_id.h>
#include <common/json_stream.h> #include <common/json_stream.h>
#include <common/node_id.h> #include <common/node_id.h>
#include <common/setup.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include <sys/socket.h> #include <sys/socket.h>
@ -168,7 +169,7 @@ int test_chdir(const char *path)
int main(int argc UNUSED, char *argv[]) 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 }; 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")); "num_connected=1\n"));
tal_free(output); tal_free(output);
assert(!taken_any()); assert(!taken_any());
take_cleanup(); common_shutdown();
return 0; return 0;
} }

View file

@ -6,6 +6,7 @@
#include <ccan/array_size/array_size.h> #include <ccan/array_size/array_size.h>
#include <ccan/tal/grab_file/grab_file.h> #include <ccan/tal/grab_file/grab_file.h>
#include <ccan/tal/path/path.h> #include <ccan/tal/path/path.h>
#include <common/setup.h>
bool deprecated_apis = false; bool deprecated_apis = false;
@ -174,8 +175,7 @@ int main(int argc, char *argv[])
jsmntok_t toks[5000]; jsmntok_t toks[5000];
const jsmntok_t *t; const jsmntok_t *t;
setup_locale(); common_setup(argv[0]);
setup_tmpctx();
if (argv[1]) if (argv[1])
json = grab_file(tmpctx, argv[1]); json = grab_file(tmpctx, argv[1]);
@ -187,8 +187,7 @@ int main(int argc, char *argv[])
"bolt12/format-string-test.json")); "bolt12/format-string-test.json"));
if (!json) { if (!json) {
printf("test file not found, skipping\n"); printf("test file not found, skipping\n");
tal_free(tmpctx); goto out;
exit(0);
} }
} }
@ -213,6 +212,7 @@ int main(int argc, char *argv[])
"lno", &dlen, &fail) != NULL); "lno", &dlen, &fail) != NULL);
assert(actual == valid); assert(actual == valid);
} }
tal_free(tmpctx); out:
common_shutdown();
return 0; return 0;
} }

View file

@ -129,8 +129,7 @@ int main(int argc, char *argv[])
"bolt12/merkle-test.json")); "bolt12/merkle-test.json"));
if (!json) { if (!json) {
printf("test file not found, skipping\n"); printf("test file not found, skipping\n");
tal_free(tmpctx); goto out;
exit(0);
} }
} }
@ -179,6 +178,8 @@ int main(int argc, char *argv[])
abort(); abort();
printf(" - WRAPPED OK\n"); printf(" - WRAPPED OK\n");
} }
out:
common_shutdown(); common_shutdown();
return 0; return 0;
} }

View file

@ -1,4 +1,5 @@
#include "../fp16.c" #include "../fp16.c"
#include <common/setup.h>
#include <common/type_to_string.h> #include <common/type_to_string.h>
#include <stdio.h> #include <stdio.h>
#include <wire/wire.h> #include <wire/wire.h>
@ -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(); } { fprintf(stderr, "towire_u8_array called!\n"); abort(); }
/* AUTOGENERATED MOCKS END */ /* AUTOGENERATED MOCKS END */
int main(void) int main(int argc, char *argv[])
{ {
/* 5 bit exponent, 11 bit mantissa. */ /* 5 bit exponent, 11 bit mantissa. */
u32 exponent, mantissa; u32 exponent, mantissa;
setup_locale(); common_setup(argv[0]);
/* These can be represented exactly. */ /* These can be represented exactly. */
for (exponent = 0; exponent < (1 << 5); exponent++) { for (exponent = 0; exponent < (1 << 5); exponent++) {
@ -140,4 +141,6 @@ int main(void)
/* Round up works, even if it causes overflow. */ /* Round up works, even if it causes overflow. */
assert(fp16_to_u64(u64_to_fp16(0xffffffff, true)) == (1ULL << 32)); assert(fp16_to_u64(u64_to_fp16(0xffffffff, true)) == (1ULL << 32));
common_shutdown();
} }

View file

@ -11,6 +11,7 @@
#include <common/gossmap.h> #include <common/gossmap.h>
#include <common/gossip_store.h> #include <common/gossip_store.h>
#include <common/route.h> #include <common/route.h>
#include <common/setup.h>
#include <common/type_to_string.h> #include <common/type_to_string.h>
#include <bitcoin/chainparams.h> #include <bitcoin/chainparams.h>
#include <stdio.h> #include <stdio.h>
@ -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); 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 node_id a, b, c, d;
struct gossmap_node *a_node, *b_node, *c_node, *d_node; struct gossmap_node *a_node, *b_node, *c_node, *d_node;
const struct dijkstra *dij; const struct dijkstra *dij;
@ -194,10 +193,7 @@ int main(void)
char gossip_version = GOSSIP_STORE_VERSION; char gossip_version = GOSSIP_STORE_VERSION;
char gossipfilename[] = "/tmp/run-route-specific-gossipstore.XXXXXX"; char gossipfilename[] = "/tmp/run-route-specific-gossipstore.XXXXXX";
secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY common_setup(argv[0]);
| SECP256K1_CONTEXT_SIGN);
setup_tmpctx();
node_id_from_hexstr("03c173897878996287a8100469f954dd820fcd8941daed91c327f168f3329be0bf", node_id_from_hexstr("03c173897878996287a8100469f954dd820fcd8941daed91c327f168f3329be0bf",
strlen("03c173897878996287a8100469f954dd820fcd8941daed91c327f168f3329be0bf"), strlen("03c173897878996287a8100469f954dd820fcd8941daed91c327f168f3329be0bf"),
&a); &a);
@ -309,7 +305,6 @@ int main(void)
AMOUNT_MSAT(499968+1), 0); AMOUNT_MSAT(499968+1), 0);
assert(!route); assert(!route);
tal_free(tmpctx); common_shutdown();
secp256k1_context_destroy(secp256k1_ctx);
return 0; return 0;
} }

View file

@ -4,6 +4,7 @@
#include <common/gossmap.h> #include <common/gossmap.h>
#include <common/gossip_store.h> #include <common/gossip_store.h>
#include <common/route.h> #include <common/route.h>
#include <common/setup.h>
#include <common/type_to_string.h> #include <common/type_to_string.h>
#include <bitcoin/chainparams.h> #include <bitcoin/chainparams.h>
#include <stdio.h> #include <stdio.h>
@ -174,9 +175,9 @@ static void node_id_from_privkey(const struct privkey *p, struct node_id *id)
node_id_from_pubkey(id, &k); 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 node_id a, b, c, d;
struct gossmap_node *a_node, *b_node, *c_node, *d_node; 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 gossip_version = GOSSIP_STORE_VERSION;
char gossipfilename[] = "/tmp/run-route-gossipstore.XXXXXX"; 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"); chainparams = chainparams_for_network("regtest");
store_fd = mkstemp(gossipfilename); store_fd = mkstemp(gossipfilename);
@ -313,7 +311,6 @@ int main(void)
assert(amount_msat_eq(route[0].amount, AMOUNT_MSAT(3000000 + 6))); assert(amount_msat_eq(route[0].amount, AMOUNT_MSAT(3000000 + 6)));
assert(route[0].delay == 15); assert(route[0].delay == 15);
tal_free(tmpctx); common_shutdown();
secp256k1_context_destroy(secp256k1_ctx);
return 0; return 0;
} }

View file

@ -5,6 +5,7 @@ int unused_main(int argc, char *argv[]);
#undef main #undef main
#include <common/channel_type.h> #include <common/channel_type.h>
#include <common/json_stream.h> #include <common/json_stream.h>
#include <common/setup.h>
#include <stdio.h> #include <stdio.h>
/* AUTOGENERATED MOCKS START */ /* 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[]) int main(int argc, char *argv[])
{ {
u8 *update; u8 *update;
setup_locale(); common_setup(argv[0]);
update = tal_hexdata(NULL, "010276df7e70c63cc2b63ef1c062b99c6d934a80ef2fd4dae9e1d86d277f47674af3255a97fa52ade7f129263f591ed784996eba6383135896cc117a438c8029328206226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f00006700000100005d50f933000000900000000000000000000003e80000000a", update = tal_hexdata(NULL, "010276df7e70c63cc2b63ef1c062b99c6d934a80ef2fd4dae9e1d86d277f47674af3255a97fa52ade7f129263f591ed784996eba6383135896cc117a438c8029328206226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f00006700000100005d50f933000000900000000000000000000003e80000000a",
strlen("010276df7e70c63cc2b63ef1c062b99c6d934a80ef2fd4dae9e1d86d277f47674af3255a97fa52ade7f129263f591ed784996eba6383135896cc117a438c8029328206226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f00006700000100005d50f933000000900000000000000000000003e80000000a")); strlen("010276df7e70c63cc2b63ef1c062b99c6d934a80ef2fd4dae9e1d86d277f47674af3255a97fa52ade7f129263f591ed784996eba6383135896cc117a438c8029328206226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f00006700000100005d50f933000000900000000000000000000003e80000000a"));
@ -159,5 +160,6 @@ int main(int argc, char *argv[])
strlen("010206737e9e18d3e4d0ab4066ccaecdcc10e648c5f1c5413f1610747e0d463fa7fa39c1b02ea2fd694275ecfefe4fe9631f24afd182ab75b805e16cd550941f858c06226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f00006d00000100005d50f935010000300000000000000000000000640000000b00000000000186a0")); strlen("010206737e9e18d3e4d0ab4066ccaecdcc10e648c5f1c5413f1610747e0d463fa7fa39c1b02ea2fd694275ecfefe4fe9631f24afd182ab75b805e16cd550941f858c06226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f00006d00000100005d50f935010000300000000000000000000000640000000b00000000000186a0"));
assert(crc32_of_update(update) == 0xf32ce968); assert(crc32_of_update(update) == 0xf32ce968);
tal_free(update); tal_free(update);
common_shutdown();
return 0; return 0;
} }

View file

@ -7,6 +7,7 @@
#include <common/channel_type.h> #include <common/channel_type.h>
#include <common/json_helpers.h> #include <common/json_helpers.h>
#include <common/json_stream.h> #include <common/json_stream.h>
#include <common/setup.h>
#include <stdio.h> #include <stdio.h>
#ifdef NDEBUG #ifdef NDEBUG
@ -491,11 +492,11 @@ static u8 *test_query_short_channel_ids(const char *test_vector,
return msg; return msg;
} }
int main(void) int main(int argc, char *argv[])
{ {
jsmntok_t *toks = tal_arr(NULL, jsmntok_t, 1000); 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++) { for (size_t i = 0; i < ARRAY_SIZE(test_vectors); i++) {
jsmn_parser parser; jsmn_parser parser;
@ -524,5 +525,6 @@ int main(void)
tal_free(m); tal_free(m);
} }
tal_free(toks); tal_free(toks);
common_shutdown();
return 0; return 0;
} }

View file

@ -2,6 +2,7 @@
#include <ccan/err/err.h> #include <ccan/err/err.h>
#include <common/channel_type.h> #include <common/channel_type.h>
#include <common/json_stream.h> #include <common/json_stream.h>
#include <common/setup.h>
#include <common/wireaddr.h> #include <common/wireaddr.h>
#include <stdio.h> #include <stdio.h>
@ -117,7 +118,7 @@ int main(int argc, char *argv[])
{ {
struct seeker *seeker = tal(NULL, struct seeker); struct seeker *seeker = tal(NULL, struct seeker);
setup_locale(); common_setup(argv[0]);
seeker->daemon = tal(seeker, struct daemon); seeker->daemon = tal(seeker, struct daemon);
@ -152,4 +153,5 @@ int main(int argc, char *argv[])
-1); -1);
tal_free(seeker); tal_free(seeker);
common_shutdown();
} }

View file

@ -5,6 +5,7 @@ int unused_main(int argc, char *argv[]);
#include "../io_loop_with_timers.c" #include "../io_loop_with_timers.c"
#include "../lightningd.c" #include "../lightningd.c"
#include "../subd.c" #include "../subd.c"
#include <common/setup.h>
/* AUTOGENERATED MOCKS START */ /* AUTOGENERATED MOCKS START */
/* Generated stub for activate_peers */ /* Generated stub for activate_peers */
@ -245,15 +246,14 @@ struct wallet *wallet_new(struct lightningd *ld UNNEEDED, struct timers *timers
struct log *crashlog; struct log *crashlog;
#undef main #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; char *argv0;
/* We're assuming we're run from top build dir. */ /* We're assuming we're run from top build dir. */
const char *answer; const char *answer;
setup_tmpctx();
answer = path_canon(tmpctx, "lightningd/test/run-find_my_abspath"); answer = path_canon(tmpctx, "lightningd/test/run-find_my_abspath");
/* Various different ways we could find ourselves. */ /* 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(streq(find_my_abspath(tmpctx, argv0), answer));
assert(!taken_any()); assert(!taken_any());
take_cleanup(); common_shutdown();
tal_free(tmpctx);
return 0; return 0;
} }

View file

@ -1,4 +1,5 @@
#include "../log.c" #include "../log.c"
#include <common/setup.h>
/* AUTOGENERATED MOCKS START */ /* AUTOGENERATED MOCKS START */
/* Generated stub for command_fail */ /* 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(); } { fprintf(stderr, "towire_node_id called!\n"); abort(); }
/* AUTOGENERATED MOCKS END */ /* AUTOGENERATED MOCKS END */
int main(void) int main(int argc, char *argv[])
{ {
struct log_book *lb; struct log_book *lb;
struct log *l; struct log *l;
setup_locale(); common_setup(argv[0]);
lb = new_log_book(NULL, lb = new_log_book(NULL,
(sizeof(struct log_entry) + sizeof("test XXXXXX")) (sizeof(struct log_entry) + sizeof("test XXXXXX"))
*100); *100);
@ -133,4 +135,5 @@ int main(void)
/* Freeing (last) log frees logbook */ /* Freeing (last) log frees logbook */
tal_free(l); tal_free(l);
common_shutdown();
} }

View file

@ -307,8 +307,10 @@ u8 *wire_sync_read(const tal_t *ctx UNNEEDED, int fd UNNEEDED)
return (u8 *)ctx; 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; return true;
} }
@ -422,6 +424,5 @@ int main(int argc, char *argv[])
htlc_scripts, htlc_scripts,
false); false);
assert(ret == 2); assert(ret == 2);
take_cleanup();
common_shutdown(); common_shutdown();
} }

View file

@ -1,6 +1,7 @@
#include "../libplugin-pay.c" #include "../libplugin-pay.c"
#include <bitcoin/chainparams.h> #include <bitcoin/chainparams.h>
#include <common/gossip_store.h> #include <common/gossip_store.h>
#include <common/setup.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
@ -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 /* 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 * 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 */ * 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]; struct node_id ids[NUM_NODES];
int store_fd; int store_fd;
struct payment *p; struct payment *p;
@ -335,9 +334,7 @@ int main(void)
char gossip_version = GOSSIP_STORE_VERSION; char gossip_version = GOSSIP_STORE_VERSION;
char gossipfilename[] = "/tmp/run-route-overlong.XXXXXX"; char gossipfilename[] = "/tmp/run-route-overlong.XXXXXX";
secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY common_setup(argv[0]);
| SECP256K1_CONTEXT_SIGN);
setup_tmpctx();
chainparams = chainparams_for_network("regtest"); chainparams = chainparams_for_network("regtest");
store_fd = mkstemp(gossipfilename); store_fd = mkstemp(gossipfilename);
assert(write(store_fd, &gossip_version, sizeof(gossip_version)) assert(write(store_fd, &gossip_version, sizeof(gossip_version))
@ -401,7 +398,6 @@ int main(void)
assert(tal_count(r) == 2); assert(tal_count(r) == 2);
} }
tal_free(tmpctx); common_shutdown();
secp256k1_context_destroy(secp256k1_ctx);
return 0; return 0;
} }

View file

@ -1082,6 +1082,8 @@ static bool test_wallet_outputs(struct lightningd *ld, const tal_t *ctx)
5), 5),
"wallet_add_utxo with close_info and csv > 1"); "wallet_add_utxo with close_info and csv > 1");
CHECK_MSG(!wallet_err, wallet_err); 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 */ /* Select everything but 5 csv-locked utxo */
utxos = tal_arr(w, const struct utxo *, 0); 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 /* Do not clean up in the case of an error, we might want to debug the
* database. */ * database. */
if (ok) { if (ok) {
take_cleanup();
common_shutdown(); common_shutdown();
} }
return !ok; return !ok;