Skip to content

Commit

Permalink
tests: use common_setup/common_shutdown to avoid leaks.
Browse files Browse the repository at this point in the history
It also does more checks (like taken() checks).

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Nov 17, 2021
1 parent 68043c2 commit 57328fe
Show file tree
Hide file tree
Showing 17 changed files with 60 additions and 54 deletions.
1 change: 1 addition & 0 deletions cli/test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions cli/test/run-human-mode.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <common/channel_id.h>
#include <common/json_stream.h>
#include <common/node_id.h>
#include <common/setup.h>
#include <fcntl.h>
#include <sys/socket.h>

Expand Down Expand Up @@ -162,15 +163,15 @@ 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 };

response_off = 0;
max_read_return = -1;
assert(test_main(6, fake_argv) == 0);
assert(!taken_any());
take_cleanup();
common_shutdown();
return 0;
}

6 changes: 3 additions & 3 deletions cli/test/run-large-input.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <common/channel_id.h>
#include <common/json_stream.h>
#include <common/node_id.h>
#include <common/setup.h>
#include <fcntl.h>
#include <sys/socket.h>

Expand Down Expand Up @@ -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 };

Expand Down Expand Up @@ -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;
}
5 changes: 3 additions & 2 deletions cli/test/run-remove-hint.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <common/channel_id.h>
#include <common/json_stream.h>
#include <common/node_id.h>
#include <common/setup.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/socket.h>
Expand Down Expand Up @@ -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 };

Expand All @@ -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;
}
10 changes: 5 additions & 5 deletions common/test/run-bolt12_decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <ccan/array_size/array_size.h>
#include <ccan/tal/grab_file/grab_file.h>
#include <ccan/tal/path/path.h>
#include <common/setup.h>

bool deprecated_apis = false;

Expand Down Expand Up @@ -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]);
Expand All @@ -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;
}
}

Expand All @@ -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;
}
5 changes: 3 additions & 2 deletions common/test/run-bolt12_merkle-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -179,6 +178,8 @@ int main(int argc, char *argv[])
abort();
printf(" - WRAPPED OK\n");
}

out:
common_shutdown();
return 0;
}
7 changes: 5 additions & 2 deletions common/test/run-gossmap-fp16.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "../fp16.c"
#include <common/setup.h>
#include <common/type_to_string.h>
#include <stdio.h>
#include <wire/wire.h>
Expand Down Expand Up @@ -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++) {
Expand Down Expand Up @@ -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();
}
13 changes: 4 additions & 9 deletions common/test/run-route-specific.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <common/gossmap.h>
#include <common/gossip_store.h>
#include <common/route.h>
#include <common/setup.h>
#include <common/type_to_string.h>
#include <bitcoin/chainparams.h>
#include <stdio.h>
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
11 changes: 4 additions & 7 deletions common/test/run-route.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <common/gossmap.h>
#include <common/gossip_store.h>
#include <common/route.h>
#include <common/setup.h>
#include <common/type_to_string.h>
#include <bitcoin/chainparams.h>
#include <stdio.h>
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
4 changes: 3 additions & 1 deletion gossipd/test/run-crc32_of_update.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ int unused_main(int argc, char *argv[]);
#undef main
#include <common/channel_type.h>
#include <common/json_stream.h>
#include <common/setup.h>
#include <stdio.h>

/* AUTOGENERATED MOCKS START */
Expand Down Expand Up @@ -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"));
Expand All @@ -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;
}
6 changes: 4 additions & 2 deletions gossipd/test/run-extended-info.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <common/channel_type.h>
#include <common/json_helpers.h>
#include <common/json_stream.h>
#include <common/setup.h>
#include <stdio.h>

#ifdef NDEBUG
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -524,5 +525,6 @@ int main(void)
tal_free(m);
}
tal_free(toks);
common_shutdown();
return 0;
}
4 changes: 3 additions & 1 deletion gossipd/test/run-next_block_range.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <ccan/err/err.h>
#include <common/channel_type.h>
#include <common/json_stream.h>
#include <common/setup.h>
#include <common/wireaddr.h>
#include <stdio.h>

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -152,4 +153,5 @@ int main(int argc, char *argv[])
-1);

tal_free(seeker);
common_shutdown();
}
10 changes: 5 additions & 5 deletions lightningd/test/run-find_my_abspath.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ int unused_main(int argc, char *argv[]);
#include "../io_loop_with_timers.c"
#include "../lightningd.c"
#include "../subd.c"
#include <common/setup.h>

/* AUTOGENERATED MOCKS START */
/* Generated stub for activate_peers */
Expand Down Expand Up @@ -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. */
Expand Down Expand Up @@ -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;
}
7 changes: 5 additions & 2 deletions lightningd/test/run-log-pruning.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "../log.c"
#include <common/setup.h>

/* AUTOGENERATED MOCKS START */
/* Generated stub for command_fail */
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -133,4 +135,5 @@ int main(void)

/* Freeing (last) log frees logbook */
tal_free(l);
common_shutdown();
}
Loading

0 comments on commit 57328fe

Please sign in to comment.