|
|
@ -4,14 +4,17 @@ |
|
|
|
#include <ccan/tal/str/str.h> |
|
|
|
#include <common/json.c> |
|
|
|
#include <common/utils.h> |
|
|
|
#include <wire/wire.h> |
|
|
|
|
|
|
|
static const char *reason; |
|
|
|
#define SUPERVERBOSE(r) do { if (!reason) reason = (r); } while(0) |
|
|
|
|
|
|
|
#include <wire/fromwire.c> |
|
|
|
#include <wire/towire.c> |
|
|
|
#include <common/bigsize.c> |
|
|
|
|
|
|
|
/* AUTOGENERATED MOCKS START */ |
|
|
|
/* Generated stub for fromwire_fail */ |
|
|
|
const void *fromwire_fail(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) |
|
|
|
{ fprintf(stderr, "fromwire_fail called!\n"); abort(); } |
|
|
|
/* AUTOGENERATED MOCKS END */ |
|
|
|
|
|
|
|
/* BOLT-EXPERIMENTAL #1:
|
|
|
@ -132,7 +135,7 @@ static void test_decode(const char *json, const jsmntok_t toks[]) |
|
|
|
const jsmntok_t *bytes = json_get_member(json, t, "bytes"); |
|
|
|
u64 num, expect; |
|
|
|
const u8 *b; |
|
|
|
size_t max; |
|
|
|
size_t len; |
|
|
|
|
|
|
|
if (!json_to_u64(json, json_get_member(json, t, "value"), |
|
|
|
&expect)) |
|
|
@ -140,25 +143,14 @@ static void test_decode(const char *json, const jsmntok_t toks[]) |
|
|
|
b = tal_hexdata(tmpctx, json + bytes->start, |
|
|
|
bytes->end - bytes->start); |
|
|
|
|
|
|
|
max = tal_bytelen(b); |
|
|
|
reason = NULL; |
|
|
|
num = fromwire_bigsize(&b, &max); |
|
|
|
len = bigsize_get(b, tal_bytelen(b), &num); |
|
|
|
if (err) { |
|
|
|
assert(b == NULL); |
|
|
|
assert(max == 0); |
|
|
|
/* Map our errors to the test strings. */ |
|
|
|
if (json_tok_streq(json, err, |
|
|
|
"decoded varint is not canonical")) |
|
|
|
assert(streq(reason, "not minimal encoded")); |
|
|
|
else if (json_tok_streq(json, err, "unexpected EOF") |
|
|
|
|| json_tok_streq(json, err, "EOF")) |
|
|
|
assert(streq(reason, "less than encoding length")); |
|
|
|
else |
|
|
|
abort(); |
|
|
|
assert(len == 0); |
|
|
|
assert(json_tok_streq(json, err, reason)); |
|
|
|
} else { |
|
|
|
assert(b != NULL); |
|
|
|
assert(len == tal_bytelen(b)); |
|
|
|
assert(num == expect); |
|
|
|
assert(max == 0); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -215,12 +207,13 @@ static void test_encode(const char *json, const jsmntok_t toks[]) |
|
|
|
{ |
|
|
|
size_t i; |
|
|
|
const jsmntok_t *t; |
|
|
|
u8 buf[BIGSIZE_MAX_LEN]; |
|
|
|
|
|
|
|
json_for_each_arr(i, t, toks) { |
|
|
|
const jsmntok_t *bytes = json_get_member(json, t, "bytes"); |
|
|
|
u64 num; |
|
|
|
const u8 *expect; |
|
|
|
u8 *b; |
|
|
|
size_t len; |
|
|
|
|
|
|
|
if (!json_to_u64(json, json_get_member(json, t, "value"), |
|
|
|
&num)) |
|
|
@ -228,9 +221,8 @@ static void test_encode(const char *json, const jsmntok_t toks[]) |
|
|
|
expect = tal_hexdata(tmpctx, json + bytes->start, |
|
|
|
bytes->end - bytes->start); |
|
|
|
|
|
|
|
b = tal_arr(tmpctx, u8, 0); |
|
|
|
towire_bigsize(&b, num); |
|
|
|
assert(memeq(b, tal_bytelen(b), expect, tal_bytelen(expect))); |
|
|
|
len = bigsize_put(buf, num); |
|
|
|
assert(memeq(buf, len, expect, tal_bytelen(expect))); |
|
|
|
} |
|
|
|
} |
|
|
|
|