Skip to content

Commit

Permalink
unittest: use common_setup / common_shutdown almost everywhere.
Browse files Browse the repository at this point in the history
Avoids much cut & paste.  Some tests don't need any of it, but most
want at least some of this infrastructure.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Dec 2, 2020
1 parent 39f4ca9 commit ca2bd98
Show file tree
Hide file tree
Showing 43 changed files with 148 additions and 209 deletions.
28 changes: 17 additions & 11 deletions bitcoin/test/run-secret_eq_consttime.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <bitcoin/privkey.c>
#include <ccan/err/err.h>
#include <ccan/time/time.h>
#include <common/setup.h>
#include <stdio.h>
#include <unistd.h>

Expand Down Expand Up @@ -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));
Expand All @@ -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 (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 (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 (nonconst_success < ITERATIONS / 2)
errx(1, "memcmp seemed const time %u/%u?",
nonconst_success, i);
}
free(s1);
free(s2);

exit:
common_shutdown();
return 0;
}
3 changes: 0 additions & 3 deletions channeld/test/run-commit_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 0 additions & 2 deletions channeld/test/run-full_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -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! */
Expand Down
8 changes: 4 additions & 4 deletions common/test/run-amount.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "../amount.c"
#include <common/setup.h>
#include <common/utils.h>
#include <stdio.h>
#include <wire/wire.h>
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -233,5 +233,5 @@ int main(void)
assert(sat.satoshis == i);
}

tal_free(tmpctx);
common_shutdown();
}
6 changes: 3 additions & 3 deletions common/test/run-bigsize.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <ccan/tal/str/str.h>
#include <common/amount.h>
#include <common/json.c>
#include <common/setup.h>
#include <common/utils.h>
#include <wire/wire.h>

Expand Down Expand Up @@ -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])),
Expand Down Expand Up @@ -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();
}
14 changes: 4 additions & 10 deletions common/test/run-bolt11.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <ccan/err/err.h>
#include <ccan/mem/mem.h>
#include <ccan/str/hex/hex.h>
#include <common/setup.h>
#include <stdio.h>
#include <wally_core.h>

Expand Down Expand Up @@ -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;
Expand All @@ -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:
*
Expand Down Expand Up @@ -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();
}
9 changes: 4 additions & 5 deletions common/test/run-cryptomsg.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <ccan/str/hex/hex.h>
#include <ccan/tal/str/str.h>
#include <common/dev_disconnect.h>
#include <common/setup.h>
#include <common/status.h>
#include <stdio.h>
#include <wire/peer_wire.h>
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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;
}
10 changes: 4 additions & 6 deletions common/test/run-derive_basepoints.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <ccan/str/hex/hex.h>
#include <ccan/structeq/structeq.h>
#include <common/amount.h>
#include <common/setup.h>
#include <common/utils.h>
#include <stdio.h>
#include <wally_core.h>
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -263,6 +261,6 @@ int main(void)
&info->secrets.htlc_basepoint_secret));

tal_free(ctx);
wally_cleanup(0);
common_shutdown();
return 0;
}
12 changes: 4 additions & 8 deletions common/test/run-features.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <ccan/err/err.h>
#include <ccan/mem/mem.h>
#include <ccan/str/hex/hex.h>
#include <common/setup.h>
#include <common/utils.h>
#include <stdio.h>
#include <wally_core.h>
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
6 changes: 4 additions & 2 deletions common/test/run-gossip_rcvd_filter.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "../gossip_rcvd_filter.c"
#include "../pseudorand.c"
#include "../../wire/fromwire.c"
#include <common/setup.h>
#include <common/utils.h>
#include <stdio.h>

Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -192,5 +193,6 @@ int main(void)
&& tal_next(f->cur) == NULL));

tal_free(ctx);
common_shutdown();
return 0;
}
9 changes: 4 additions & 5 deletions common/test/run-ip_port_parsing.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <assert.h>
#include <ccan/mem/mem.h>
#include <common/amount.h>
#include <common/setup.h>

/* AUTOGENERATED MOCKS START */
/* Generated stub for amount_asset_is_main */
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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;
}
11 changes: 5 additions & 6 deletions common/test/run-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <ccan/mem/mem.h>
#include <ccan/tal/str/str.h>
#include <common/json.h>
#include <common/setup.h>
#include <common/utils.h>
#include <inttypes.h>
#include <stdio.h>
Expand Down Expand Up @@ -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();
}
Loading

0 comments on commit ca2bd98

Please sign in to comment.