2019-01-15 04:54:27 +01:00
|
|
|
#include "../json_helpers.c"
|
2020-01-26 13:07:50 +01:00
|
|
|
#include <assert.h>
|
2018-12-16 05:44:06 +01:00
|
|
|
#include <ccan/mem/mem.h>
|
2020-01-26 13:07:50 +01:00
|
|
|
#include <common/json.h>
|
2018-04-25 12:55:34 +02:00
|
|
|
#include <common/utils.h>
|
2020-01-26 13:07:50 +01:00
|
|
|
#include <inttypes.h>
|
2017-11-01 02:58:44 +01:00
|
|
|
#include <stdio.h>
|
2019-06-21 02:11:41 +02:00
|
|
|
#include <wire/wire.h>
|
2017-11-01 02:58:44 +01:00
|
|
|
|
|
|
|
/* AUTOGENERATED MOCKS START */
|
common/node_id: new type.
Node ids are pubkeys, but we only use them as pubkeys for routing and checking
gossip messages. So we're packing and unpacking them constantly, and wasting
some space and time.
This introduces a new type, explicitly the SEC1 compressed encoding
(33 bytes). We ensure its validity when we load from the db, or get it
from JSON. We still use 'struct pubkey' for peer messages, which checks
validity.
Results from 5 runs, min-max(mean +/- stddev):
store_load_msec,vsz_kb,store_rewrite_sec,listnodes_sec,listchannels_sec,routing_sec,peer_write_all_sec
39475-39572(39518+/-36),2880732,41.150000-41.390000(41.298+/-0.085),2.260000-2.550000(2.336+/-0.11),44.390000-65.150000(58.648+/-7.5),32.740000-33.020000(32.89+/-0.093),44.130000-45.090000(44.566+/-0.32)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-04-08 08:34:06 +02:00
|
|
|
/* Generated stub for node_id_from_hexstr */
|
|
|
|
bool node_id_from_hexstr(const char *str UNNEEDED, size_t slen UNNEEDED, struct node_id *id UNNEEDED)
|
|
|
|
{ fprintf(stderr, "node_id_from_hexstr called!\n"); abort(); }
|
2017-11-01 02:58:44 +01:00
|
|
|
/* AUTOGENERATED MOCKS END */
|
|
|
|
|
2018-01-12 17:46:12 +01:00
|
|
|
|
|
|
|
// issue #577
|
|
|
|
|
|
|
|
static void do_json_tok_bitcoin_amount(const char* val, uint64_t expected)
|
|
|
|
{
|
|
|
|
uint64_t amount;
|
|
|
|
jsmntok_t tok;
|
|
|
|
|
|
|
|
tok.start = 0;
|
|
|
|
tok.end = strlen(val);
|
|
|
|
|
|
|
|
fprintf(stderr, "do_json_tok_bitcoin_amount(\"%s\", %"PRIu64"): ", val, expected);
|
|
|
|
|
2018-12-08 01:38:56 +01:00
|
|
|
assert(json_to_bitcoin_amount(val, &tok, &amount) == true);
|
2018-01-12 17:46:12 +01:00
|
|
|
assert(amount == expected);
|
|
|
|
|
|
|
|
fprintf(stderr, "ok\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int test_json_tok_bitcoin_amount(void)
|
|
|
|
{
|
|
|
|
do_json_tok_bitcoin_amount("0.00000001", 1);
|
|
|
|
do_json_tok_bitcoin_amount("0.00000007", 7);
|
|
|
|
do_json_tok_bitcoin_amount("0.00000008", 8);
|
|
|
|
do_json_tok_bitcoin_amount("0.00000010", 10);
|
|
|
|
do_json_tok_bitcoin_amount("0.12345678", 12345678);
|
|
|
|
do_json_tok_bitcoin_amount("0.01234567", 1234567);
|
|
|
|
do_json_tok_bitcoin_amount("123.45678900", 12345678900);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-02-19 11:11:36 +01:00
|
|
|
static void do_json_tok_millionths(const char *val, bool ok, uint64_t expected)
|
|
|
|
{
|
|
|
|
uint64_t amount;
|
|
|
|
jsmntok_t tok;
|
|
|
|
|
|
|
|
tok.start = 0;
|
|
|
|
tok.end = strlen(val);
|
|
|
|
|
|
|
|
assert(json_to_millionths(val, &tok, &amount) == ok);
|
|
|
|
if (ok)
|
|
|
|
assert(amount == expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int test_json_tok_millionths(void)
|
|
|
|
{
|
|
|
|
do_json_tok_millionths("", false, 0);
|
|
|
|
do_json_tok_millionths("0..0", false, 0);
|
|
|
|
do_json_tok_millionths("0.0.", false, 0);
|
|
|
|
do_json_tok_millionths(".", false, 0);
|
|
|
|
do_json_tok_millionths("..", false, 0);
|
|
|
|
|
|
|
|
do_json_tok_millionths("0", true, 0);
|
|
|
|
do_json_tok_millionths(".0", true, 0);
|
|
|
|
do_json_tok_millionths("0.", true, 0);
|
|
|
|
do_json_tok_millionths("100", true, 100 * 1000000);
|
|
|
|
do_json_tok_millionths("100.0", true, 100 * 1000000);
|
|
|
|
do_json_tok_millionths("100.", true, 100 * 1000000);
|
|
|
|
do_json_tok_millionths("100.000001", true, 100 * 1000000 + 1);
|
|
|
|
do_json_tok_millionths("100.0000001", true, 100 * 1000000);
|
|
|
|
do_json_tok_millionths(".000009", true, 9);
|
|
|
|
do_json_tok_millionths(".0000099", true, 9);
|
|
|
|
do_json_tok_millionths("18446744073709.551615", true,
|
|
|
|
18446744073709551615ULL);
|
|
|
|
do_json_tok_millionths("18446744073709.551616", false, 0);
|
|
|
|
do_json_tok_millionths("18446744073709.551625", false, 0);
|
|
|
|
do_json_tok_millionths("18446744073709.551715", false, 0);
|
|
|
|
do_json_tok_millionths("18446744073709.552615", false, 0);
|
|
|
|
do_json_tok_millionths("18446744073709.561615", false, 0);
|
|
|
|
do_json_tok_millionths("18446744073709.651615", false, 0);
|
|
|
|
do_json_tok_millionths("18446744073710.551615", false, 0);
|
|
|
|
do_json_tok_millionths("18446744073809.551615", false, 0);
|
|
|
|
do_json_tok_millionths("18446744074709.551615", false, 0);
|
|
|
|
do_json_tok_millionths("18446744083709.551615", false, 0);
|
|
|
|
do_json_tok_millionths("18446744173709.551615", false, 0);
|
|
|
|
do_json_tok_millionths("18446745073709.551615", false, 0);
|
|
|
|
do_json_tok_millionths("18446754073709.551615", false, 0);
|
|
|
|
do_json_tok_millionths("18446844073709.551615", false, 0);
|
|
|
|
do_json_tok_millionths("18447744073709.551615", false, 0);
|
|
|
|
do_json_tok_millionths("18456744073709.551615", false, 0);
|
|
|
|
do_json_tok_millionths("18546744073709.551615", false, 0);
|
|
|
|
do_json_tok_millionths("19446744073709.551615", false, 0);
|
|
|
|
do_json_tok_millionths("28446744073709.551615", false, 0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-17 03:05:26 +01:00
|
|
|
static void test_json_tok_size(void)
|
|
|
|
{
|
2020-08-20 15:40:32 +02:00
|
|
|
jsmntok_t *toks;
|
2019-01-17 03:05:26 +01:00
|
|
|
char *buf;
|
2020-08-20 15:40:32 +02:00
|
|
|
bool ok, complete;
|
|
|
|
jsmn_parser parser;
|
2019-01-17 03:05:26 +01:00
|
|
|
|
|
|
|
buf = "[\"e1\", [\"e2\", \"e3\"]]";
|
2020-08-20 15:40:32 +02:00
|
|
|
toks = toks_alloc(tmpctx);
|
|
|
|
jsmn_init(&parser);
|
|
|
|
ok = json_parse_input(&parser, &toks, buf, strlen(buf), &complete);
|
2019-01-17 03:05:26 +01:00
|
|
|
assert(ok);
|
2020-08-20 15:40:32 +02:00
|
|
|
assert(complete);
|
2019-01-17 03:05:26 +01:00
|
|
|
/* size only counts *direct* children */
|
|
|
|
assert(toks[0].size == 2);
|
|
|
|
assert(toks[2].size == 2);
|
|
|
|
|
|
|
|
buf = "[[\"e1\", \"e2\"], \"e3\"]";
|
2020-08-20 15:40:32 +02:00
|
|
|
toks_reset(toks);
|
|
|
|
jsmn_init(&parser);
|
|
|
|
ok = json_parse_input(&parser, &toks, buf, strlen(buf), &complete);
|
2019-01-17 03:05:26 +01:00
|
|
|
assert(ok);
|
2020-08-20 15:40:32 +02:00
|
|
|
assert(complete);
|
2019-01-17 03:05:26 +01:00
|
|
|
/* size only counts *direct* children */
|
|
|
|
assert(toks[0].size == 2);
|
|
|
|
assert(toks[1].size == 2);
|
|
|
|
|
2020-06-08 19:23:54 +02:00
|
|
|
buf = "{\"e1\" : {\"e2\": 2, \"e3\": 3}}";
|
2020-08-20 15:40:32 +02:00
|
|
|
toks_reset(toks);
|
|
|
|
jsmn_init(&parser);
|
|
|
|
ok = json_parse_input(&parser, &toks, buf, strlen(buf), &complete);
|
2019-01-17 03:05:26 +01:00
|
|
|
assert(ok);
|
2020-08-20 15:40:32 +02:00
|
|
|
assert(complete);
|
2019-01-17 03:05:26 +01:00
|
|
|
/* size only counts *direct* children */
|
|
|
|
assert(toks[0].size == 1);
|
|
|
|
assert(toks[2].size == 2);
|
|
|
|
|
2020-06-08 19:23:54 +02:00
|
|
|
buf = "{\"e1\" : {\"e2\": 2, \"e3\": 3}, \"e4\" : {\"e5\": 5, \"e6\": 6}}";
|
2020-08-20 15:40:32 +02:00
|
|
|
toks_reset(toks);
|
|
|
|
jsmn_init(&parser);
|
|
|
|
ok = json_parse_input(&parser, &toks, buf, strlen(buf), &complete);
|
2019-01-17 03:05:26 +01:00
|
|
|
assert(ok);
|
2020-08-20 15:40:32 +02:00
|
|
|
assert(complete);
|
2019-01-17 03:05:26 +01:00
|
|
|
/* size only counts *direct* children */
|
|
|
|
assert(toks[0].size == 2);
|
|
|
|
assert(toks[2].size == 2);
|
2020-06-08 19:23:54 +02:00
|
|
|
assert(toks[8].size == 2);
|
|
|
|
|
|
|
|
/* This should *not* parse! (used to give toks[0]->size == 3!) */
|
|
|
|
buf = "{ \"\" \"\" \"\" }";
|
2020-08-20 15:40:32 +02:00
|
|
|
toks_reset(toks);
|
|
|
|
jsmn_init(&parser);
|
|
|
|
ok = json_parse_input(&parser, &toks, buf, strlen(buf), &complete);
|
2020-06-08 19:23:54 +02:00
|
|
|
assert(!ok);
|
2019-01-17 03:09:45 +01:00
|
|
|
|
|
|
|
/* This should *not* parse! (used to give toks[0]->size == 2!) */
|
|
|
|
buf = "{ 'satoshi', '546' }";
|
2020-08-20 02:16:55 +02:00
|
|
|
toks = json_parse_simple(tmpctx, buf, strlen(buf));
|
|
|
|
assert(!toks);
|
2019-01-17 03:05:26 +01:00
|
|
|
}
|
|
|
|
|
2018-12-16 05:44:06 +01:00
|
|
|
static void test_json_delve(void)
|
|
|
|
{
|
|
|
|
const jsmntok_t *toks, *t;
|
|
|
|
char *buf;
|
|
|
|
|
|
|
|
buf = "{\"1\":\"one\", \"2\":\"two\", \"3\":[\"three\", {\"deeper\": 17}]}";
|
2020-08-20 02:16:55 +02:00
|
|
|
toks = json_parse_simple(tmpctx, buf, strlen(buf));
|
|
|
|
assert(toks);
|
2019-01-17 03:05:26 +01:00
|
|
|
assert(toks->size == 3);
|
2018-12-16 05:44:06 +01:00
|
|
|
|
|
|
|
t = json_delve(buf, toks, ".1");
|
|
|
|
assert(t);
|
|
|
|
assert(t->type == JSMN_STRING);
|
|
|
|
assert(json_tok_streq(buf, t, "one"));
|
|
|
|
assert(t == toks+2);
|
|
|
|
|
|
|
|
t = json_delve(buf, toks, ".2");
|
|
|
|
assert(t);
|
|
|
|
assert(t->type == JSMN_STRING);
|
|
|
|
assert(json_tok_streq(buf, t, "two"));
|
|
|
|
assert(t == toks+4);
|
|
|
|
|
|
|
|
t = json_delve(buf, toks, ".3");
|
|
|
|
assert(t);
|
|
|
|
assert(t->type == JSMN_ARRAY);
|
|
|
|
assert(t == toks+6);
|
2019-01-17 03:05:26 +01:00
|
|
|
assert(t->size == 2);
|
2018-12-16 05:44:06 +01:00
|
|
|
|
|
|
|
t = json_delve(buf, toks, ".3[0]");
|
|
|
|
assert(t);
|
|
|
|
assert(t->type == JSMN_STRING);
|
|
|
|
assert(json_tok_streq(buf, t, "three"));
|
|
|
|
assert(t == toks+7);
|
|
|
|
|
|
|
|
t = json_delve(buf, toks, ".3[1]");
|
|
|
|
assert(t);
|
|
|
|
assert(t->type == JSMN_OBJECT);
|
|
|
|
assert(t == toks+8);
|
2019-01-17 03:05:26 +01:00
|
|
|
assert(t->size == 1);
|
2018-12-16 05:44:06 +01:00
|
|
|
|
|
|
|
t = json_delve(buf, toks, ".3[1].deeper");
|
|
|
|
assert(t);
|
|
|
|
assert(t->type == JSMN_PRIMITIVE);
|
|
|
|
assert(memeq(buf + t->start, t->end - t->start, "17", strlen("17")));
|
|
|
|
assert(t == toks+10);
|
|
|
|
|
|
|
|
t = json_delve(buf, toks, ".4");
|
|
|
|
assert(!t);
|
|
|
|
t = json_delve(buf, toks, "[0]");
|
|
|
|
assert(!t);
|
|
|
|
t = json_delve(buf, toks, ".deeper");
|
|
|
|
assert(!t);
|
|
|
|
t = json_delve(buf, toks, ".3[2]");
|
|
|
|
assert(!t);
|
|
|
|
t = json_delve(buf, toks, ".3[2].deeper");
|
|
|
|
assert(!t);
|
|
|
|
t = json_delve(buf, toks, ".3[0].deeper");
|
|
|
|
assert(!t);
|
|
|
|
t = json_delve(buf, toks, ".3[1].deeper[0]");
|
|
|
|
assert(!t);
|
|
|
|
t = json_delve(buf, toks, ".3[1][0]");
|
|
|
|
assert(!t);
|
|
|
|
|
|
|
|
/* Now a real example. */
|
|
|
|
buf = "{\n"
|
|
|
|
" \"jsonrpc\": \"2.0\", \n"
|
|
|
|
" \"method\": \"init\", \n"
|
|
|
|
" \"id\": 1, \n"
|
|
|
|
" \"params\": {\n"
|
|
|
|
" \"options\": {\n"
|
|
|
|
" }, \n"
|
|
|
|
" \"configuration\": {\n"
|
|
|
|
" \"lightning-dir\": \"/tmp/ltests-n2hyd543/test_pay_plugin_1/lightning-2/\", \n"
|
|
|
|
" \"rpc-file\": \"lightning-rpc\"\n"
|
|
|
|
" }\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n"
|
|
|
|
"\n";
|
2020-08-20 02:16:55 +02:00
|
|
|
toks = json_parse_simple(tmpctx, buf, strlen(buf));
|
|
|
|
assert(toks);
|
2019-01-17 03:05:26 +01:00
|
|
|
assert(toks->size == 4);
|
2018-12-16 05:44:06 +01:00
|
|
|
|
|
|
|
t = json_delve(buf, toks, ".rpcfile");
|
|
|
|
assert(!t);
|
|
|
|
t = json_delve(buf, toks, ".configuration.rpc-file");
|
|
|
|
assert(!t);
|
2019-01-17 03:05:26 +01:00
|
|
|
t = json_delve(buf, toks, ".params.configuration");
|
|
|
|
assert(t);
|
|
|
|
assert(t->size == 2);
|
2018-12-16 05:44:06 +01:00
|
|
|
t = json_delve(buf, toks, ".params.configuration.rpc-file");
|
|
|
|
assert(t);
|
|
|
|
assert(t->type == JSMN_STRING);
|
|
|
|
assert(json_tok_streq(buf, t, "lightning-rpc"));
|
|
|
|
}
|
|
|
|
|
2018-01-12 17:46:12 +01:00
|
|
|
int main(void)
|
|
|
|
{
|
2018-04-25 12:55:34 +02:00
|
|
|
setup_locale();
|
2018-12-16 05:44:06 +01:00
|
|
|
setup_tmpctx();
|
2018-04-25 12:55:34 +02:00
|
|
|
|
2019-01-17 03:05:26 +01:00
|
|
|
test_json_tok_size();
|
2018-01-12 17:46:12 +01:00
|
|
|
test_json_tok_bitcoin_amount();
|
2020-02-19 11:11:36 +01:00
|
|
|
test_json_tok_millionths();
|
2018-12-16 05:44:06 +01:00
|
|
|
test_json_delve();
|
2018-03-26 02:08:15 +02:00
|
|
|
assert(!taken_any());
|
|
|
|
take_cleanup();
|
2018-12-16 05:44:06 +01:00
|
|
|
tal_free(tmpctx);
|
2018-01-12 17:46:12 +01:00
|
|
|
}
|