Skip to content

Commit

Permalink
wire/test/run-tlvstream.c: use generated defs for test vectors.
Browse files Browse the repository at this point in the history
Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell authored and cdecker committed Aug 2, 2019
1 parent 4b5cd5f commit fafd5ed
Showing 1 changed file with 25 additions and 238 deletions.
263 changes: 25 additions & 238 deletions wire/test/run-tlvstream.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,240 +4,22 @@
#include <stdio.h>
#include <wally_core.h>

#include <common/amount.c>
#include <common/bigsize.c>
#include <common/node_id.c>

static const char *reason;
#undef SUPERVERBOSE
#define SUPERVERBOSE(r) do { reason = (r); } while(0)

#include <wire/gen_peer_wire.c>
#include <wire/tlvstream.c>
#include <wire/fromwire.c>
#include <wire/towire.c>

/* AUTOGENERATED MOCKS START */
/* AUTOGENERATED MOCKS END */

/* FIXME: Autogenerate these! */
/* BOLT-EXPERIMENTAL #1:
* 1. tlvs: `n1`
* 2. types:
* 1. type: 1 (`tlv1`)
* 2. data:
* * [`tu64`:`amount_msat`]
* 1. type: 2 (`tlv2`)
* 2. data:
* * [`short_channel_id`:`scid`]
* 1. type: 3 (`tlv3`)
* 2. data:
* * [`point`:`node_id`]
* * [`u64`:`amount_msat_1`]
* * [`u64`:`amount_msat_2`]
* 1. type: 254 (`tlv4`)
* 2. data:
* * [`u16`:`cltv_delta`]
*/
struct tlv_n1_tlv1 {
u64 amount_msat;
};

struct tlv_n1_tlv2 {
struct short_channel_id scid;
};

struct tlv_n1_tlv3 {
struct pubkey node_id;
u64 amount_msat_1;
u64 amount_msat_2;
};

struct tlv_n1_tlv4 {
u16 cltv_delta;
};

struct tlv_n1 {
struct tlv_n1_tlv1 *tlv1;
struct tlv_n1_tlv2 *tlv2;
struct tlv_n1_tlv3 *tlv3;
struct tlv_n1_tlv4 *tlv4;
};

static struct tlv_n1 *tlv_n1_new(const tal_t *ctx)
{
/* Initialize everything to NULL. (Quiet, C pedants!) */
return talz(ctx, struct tlv_n1);
}

static u8 *towire_tlv_n1_tlv1(const tal_t *ctx, const void *vrecord)
{
const struct tlv_n1 *r = vrecord;
u8 *ptr;

if (!r->tlv1)
return NULL;

ptr = tal_arr(ctx, u8, 0);
towire_tu64(&ptr, r->tlv1->amount_msat);
return ptr;
}

static u8 *towire_tlv_n1_tlv2(const tal_t *ctx, const void *vrecord)
{
const struct tlv_n1 *r = vrecord;
u8 *ptr;

if (!r->tlv2)
return NULL;

ptr = tal_arr(ctx, u8, 0);
towire_short_channel_id(&ptr, &r->tlv2->scid);
return ptr;
}

static u8 *towire_tlv_n1_tlv3(const tal_t *ctx, const void *vrecord)
{
const struct tlv_n1 *r = vrecord;
u8 *ptr;

if (!r->tlv3)
return NULL;

ptr = tal_arr(ctx, u8, 0);
towire_pubkey(&ptr, &r->tlv3->node_id);
towire_u64(&ptr, r->tlv3->amount_msat_1);
towire_u64(&ptr, r->tlv3->amount_msat_2);
return ptr;
}

static u8 *towire_tlv_n1_tlv4(const tal_t *ctx, const void *vrecord)
{
const struct tlv_n1 *r = vrecord;
u8 *ptr;

if (!r->tlv4)
return NULL;

ptr = tal_arr(ctx, u8, 0);
towire_u16(&ptr, r->tlv4->cltv_delta);
return ptr;
}

static void fromwire_tlv_n1_tlv1(const u8 **cursor, size_t *max, void *vrecord)
{
struct tlv_n1 *r = vrecord;

r->tlv1 = tal(r, struct tlv_n1_tlv1);
r->tlv1->amount_msat = fromwire_tu64(cursor, max);
}

static void fromwire_tlv_n1_tlv2(const u8 **cursor, size_t *max, void *vrecord)
{
struct tlv_n1 *r = vrecord;

r->tlv2 = tal(r, struct tlv_n1_tlv2);
fromwire_short_channel_id(cursor, max, &r->tlv2->scid);
}

static void fromwire_tlv_n1_tlv3(const u8 **cursor, size_t *max, void *vrecord)
{
struct tlv_n1 *r = vrecord;

r->tlv3 = tal(r, struct tlv_n1_tlv3);

fromwire_pubkey(cursor, max, &r->tlv3->node_id);
r->tlv3->amount_msat_1 = fromwire_u64(cursor, max);
r->tlv3->amount_msat_2 = fromwire_u64(cursor, max);
}

static void fromwire_tlv_n1_tlv4(const u8 **cursor, size_t *max, void *vrecord)
{
struct tlv_n1 *r = vrecord;

r->tlv4 = tal(r, struct tlv_n1_tlv4);
r->tlv4->cltv_delta = fromwire_u16(cursor, max);
}

static const struct tlv_record_type tlvs_n1[] = {
{ 1, towire_tlv_n1_tlv1, fromwire_tlv_n1_tlv1 },
{ 2, towire_tlv_n1_tlv2, fromwire_tlv_n1_tlv2 },
{ 3, towire_tlv_n1_tlv3, fromwire_tlv_n1_tlv3 },
{ 254, towire_tlv_n1_tlv4, fromwire_tlv_n1_tlv4 },
};

/* BOLT-EXPERIMENTAL #1:
* 1. tlvs: `n2`
* 2. types:
* 1. type: 0 (`tlv1`)
* 2. data:
* * [`tu64`:`amount_msat`]
* 1. type: 11 (`tlv2`)
* 2. data:
* * [`tu32`:`cltv_expiry`]
*/
struct tlv_n2_tlv1 {
u64 amount_msat;
};

struct tlv_n2_tlv2 {
u16 cltv_expiry;
};

struct tlv_n2 {
struct tlv_n2_tlv1 *tlv1;
struct tlv_n2_tlv2 *tlv2;
};

static struct tlv_n2 *tlv_n2_new(const tal_t *ctx)
{
/* Initialize everything to NULL. (Quiet, C pedants!) */
return talz(ctx, struct tlv_n2);
}

static u8 *towire_tlv_n2_tlv1(const tal_t *ctx, const void *vrecord)
{
const struct tlv_n2 *r = vrecord;
u8 *ptr;

if (!r->tlv1)
return NULL;

ptr = tal_arr(ctx, u8, 0);
towire_tu64(&ptr, r->tlv1->amount_msat);
return ptr;
}

static u8 *towire_tlv_n2_tlv2(const tal_t *ctx, const void *vrecord)
{
const struct tlv_n2 *r = vrecord;
u8 *ptr;

if (!r->tlv2)
return NULL;

ptr = tal_arr(ctx, u8, 0);
towire_u16(&ptr, r->tlv2->cltv_expiry);
return ptr;
}

static void fromwire_tlv_n2_tlv1(const u8 **cursor, size_t *max, void *vrecord)
{
struct tlv_n2 *r = vrecord;

r->tlv1 = tal(r, struct tlv_n2_tlv1);
r->tlv1->amount_msat = fromwire_tu64(cursor, max);
}

static void fromwire_tlv_n2_tlv2(const u8 **cursor, size_t *max, void *vrecord)
{
struct tlv_n2 *r = vrecord;

r->tlv2 = tal(r, struct tlv_n2_tlv2);
r->tlv2->cltv_expiry = fromwire_u16(cursor, max);
}

static const struct tlv_record_type tlvs_n2[] = {
{ 0, towire_tlv_n2_tlv1, fromwire_tlv_n2_tlv1 },
{ 11, towire_tlv_n2_tlv2, fromwire_tlv_n2_tlv2 },
};

/* BOLT #1
### TLV Decoding Failures
Expand Down Expand Up @@ -523,8 +305,8 @@ static struct tlv_n1_tlv1 tlv1_1099511627776 = { .amount_msat = 1099511627776ULL
static struct tlv_n1_tlv1 tlv1_281474976710656 = { .amount_msat = 281474976710656ULL };
static struct tlv_n1_tlv1 tlv1_72057594037927936 = { .amount_msat = 72057594037927936ULL };
static struct tlv_n1_tlv2 tlv2_0x0x550 = { .scid.u64 = 0x000000000226 };
/* node_id filled in at runtime. */
static struct tlv_n1_tlv3 tlv3_node_id = { { { { 0 } } }, .amount_msat_1 = 1, .amount_msat_2 = 2 };
/* filled in at runtime. */
static struct tlv_n1_tlv3 tlv3_node_id;
static struct tlv_n1_tlv4 tlv4_550 = { .cltv_delta = 550 };

static struct valid_stream valid_streams[] = {
Expand Down Expand Up @@ -575,9 +357,11 @@ static bool tlv_n1_eq(const struct tlv_n1 *a, const struct tlv_n1 *b)
return false;
if (!pubkey_eq(&a->tlv3->node_id, &b->tlv3->node_id))
return false;
if (a->tlv3->amount_msat_1 != b->tlv3->amount_msat_1)
if (!amount_msat_eq(a->tlv3->amount_msat_1,
b->tlv3->amount_msat_1))
return false;
if (a->tlv3->amount_msat_2 != b->tlv3->amount_msat_2)
if (!amount_msat_eq(a->tlv3->amount_msat_2,
b->tlv3->amount_msat_2))
return false;
} else if (b->tlv3)
return false;
Expand Down Expand Up @@ -660,6 +444,9 @@ int main(void)

if (!pubkey_from_hexstr("023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb", 66, &tlv3_node_id.node_id))
abort();
/* gcc 4.8 doesn't like this initialized statically. */
tlv3_node_id.amount_msat_1 = AMOUNT_MSAT(1);
tlv3_node_id.amount_msat_2 = AMOUNT_MSAT(2);

for (size_t i = 0; i < ARRAY_SIZE(invalid_streams_either); i++) {
struct tlv_n1 *tlv_n1 = tlv_n1_new(tmpctx);
Expand All @@ -670,13 +457,13 @@ int main(void)
orig_p = stream(tmpctx, invalid_streams_either[i].hex);
max = tal_count(orig_p);
p = orig_p;
assert(!fromwire_tlvs(&p, &max, tlvs_n1, ARRAY_SIZE(tlvs_n1),
assert(!fromwire_tlvs(&p, &max, tlvs_n1, TLVS_N1_ARRAY_SIZE,
tlv_n1));
assert(!p);
assert(strstr(invalid_streams_either[i].reason, reason));
max = tal_count(orig_p);
p = orig_p;
assert(!fromwire_tlvs(&p, &max, tlvs_n2, ARRAY_SIZE(tlvs_n2),
assert(!fromwire_tlvs(&p, &max, tlvs_n2, TLVS_N2_ARRAY_SIZE,
tlv_n2));
assert(!p);
assert(strstr(invalid_streams_either[i].reason, reason));
Expand All @@ -689,7 +476,7 @@ int main(void)

p = stream(tmpctx, invalid_streams_n1[i].hex);
max = tal_count(p);
assert(!fromwire_tlvs(&p, &max, tlvs_n1, ARRAY_SIZE(tlvs_n1),
assert(!fromwire_tlvs(&p, &max, tlvs_n1, TLVS_N1_ARRAY_SIZE,
tlv_n1));
assert(!p);
assert(strstr(invalid_streams_n1[i].reason, reason));
Expand All @@ -702,7 +489,7 @@ int main(void)

p = stream(tmpctx, invalid_streams_n1_combo[i].hex);
max = tal_count(p);
assert(!fromwire_tlvs(&p, &max, tlvs_n1, ARRAY_SIZE(tlvs_n1),
assert(!fromwire_tlvs(&p, &max, tlvs_n1, TLVS_N1_ARRAY_SIZE,
tlv_n1));
assert(!p);
assert(strstr(invalid_streams_n1_combo[i].reason, reason));
Expand All @@ -715,7 +502,7 @@ int main(void)

p = stream(tmpctx, invalid_streams_n2_combo[i].hex);
max = tal_count(p);
assert(!fromwire_tlvs(&p, &max, tlvs_n2, ARRAY_SIZE(tlvs_n2),
assert(!fromwire_tlvs(&p, &max, tlvs_n2, TLVS_N2_ARRAY_SIZE,
tlv_n2));
assert(!p);
assert(strstr(invalid_streams_n2_combo[i].reason, reason));
Expand All @@ -731,7 +518,7 @@ int main(void)

max = tal_count(orig_p);
p = orig_p;
assert(fromwire_tlvs(&p, &max, tlvs_n1, ARRAY_SIZE(tlvs_n1),
assert(fromwire_tlvs(&p, &max, tlvs_n1, TLVS_N1_ARRAY_SIZE,
tlv_n1));
assert(p);
assert(max == 0);
Expand All @@ -743,7 +530,7 @@ int main(void)
continue;

p2 = tal_arr(tmpctx, u8, 0);
towire_tlvs(&p2, tlvs_n1, ARRAY_SIZE(tlvs_n1), tlv_n1);
towire_tlvs(&p2, tlvs_n1, TLVS_N1_ARRAY_SIZE, tlv_n1);
assert(memeq(p2, tal_count(p2), orig_p, tal_count(orig_p)));
}

Expand All @@ -765,13 +552,13 @@ int main(void)
max = tal_count(orig_p);
p = orig_p;
assert(!fromwire_tlvs(&p, &max,
tlvs_n1, ARRAY_SIZE(tlvs_n1),
tlvs_n1, TLVS_N1_ARRAY_SIZE,
tlv_n1));
assert(!p);
max = tal_count(orig_p);
p = orig_p;
assert(!fromwire_tlvs(&p, &max,
tlvs_n2, ARRAY_SIZE(tlvs_n2),
tlvs_n2, TLVS_N2_ARRAY_SIZE,
tlv_n2));
assert(!p);
}
Expand All @@ -787,7 +574,7 @@ int main(void)
invalid_streams_n1[i].hex);
max = tal_count(p);
assert(!fromwire_tlvs(&p, &max,
tlvs_n1, ARRAY_SIZE(tlvs_n1),
tlvs_n1, TLVS_N1_ARRAY_SIZE,
tlv_n1));
assert(!p);
}
Expand All @@ -803,7 +590,7 @@ int main(void)
invalid_streams_n1_combo[i].hex);
max = tal_count(p);
assert(!fromwire_tlvs(&p, &max,
tlvs_n1, ARRAY_SIZE(tlvs_n1),
tlvs_n1, TLVS_N1_ARRAY_SIZE,
tlv_n1));
assert(!p);
}
Expand Down Expand Up @@ -835,7 +622,7 @@ int main(void)
< pull_type(valid_streams[j].hex);

assert(fromwire_tlvs(&p, &max,
tlvs_n1, ARRAY_SIZE(tlvs_n1),
tlvs_n1, TLVS_N1_ARRAY_SIZE,
tlv_n1) == expect_success);

if (!expect_success)
Expand All @@ -848,7 +635,7 @@ int main(void)
continue;

u8 *p2 = tal_arr(tmpctx, u8, 0);
towire_tlvs(&p2, tlvs_n1, ARRAY_SIZE(tlvs_n1), tlv_n1);
towire_tlvs(&p2, tlvs_n1, TLVS_N1_ARRAY_SIZE, tlv_n1);
assert(memeq(orig_p, tal_count(orig_p),
p2, tal_count(p2)));
}
Expand Down

0 comments on commit fafd5ed

Please sign in to comment.