2021-12-04 12:23:56 +01:00
|
|
|
#include "config.h"
|
2020-01-20 10:23:55 +01:00
|
|
|
#include "../../common/json_stream.c"
|
2018-10-19 03:17:48 +02:00
|
|
|
#include "../jsonrpc.c"
|
2022-07-04 05:52:34 +02:00
|
|
|
#include "../feerate.c"
|
2020-12-02 01:10:04 +01:00
|
|
|
#include <common/setup.h>
|
2021-09-17 00:31:38 +02:00
|
|
|
#include <stdio.h>
|
2018-10-19 03:17:48 +02:00
|
|
|
|
|
|
|
/* AUTOGENERATED MOCKS START */
|
2020-05-05 03:12:19 +02:00
|
|
|
/* Generated stub for db_begin_transaction_ */
|
|
|
|
void db_begin_transaction_(struct db *db UNNEEDED, const char *location UNNEEDED)
|
|
|
|
{ fprintf(stderr, "db_begin_transaction_ called!\n"); abort(); }
|
|
|
|
/* Generated stub for db_commit_transaction */
|
|
|
|
void db_commit_transaction(struct db *db UNNEEDED)
|
|
|
|
{ fprintf(stderr, "db_commit_transaction called!\n"); abort(); }
|
2022-07-08 11:52:11 +02:00
|
|
|
/* Generated stub for deprecated_apis */
|
|
|
|
bool deprecated_apis;
|
2018-10-19 03:17:48 +02:00
|
|
|
/* Generated stub for fatal */
|
|
|
|
void fatal(const char *fmt UNNEEDED, ...)
|
|
|
|
{ fprintf(stderr, "fatal called!\n"); abort(); }
|
2020-05-16 03:29:05 +02:00
|
|
|
/* Generated stub for fromwire_bigsize */
|
|
|
|
bigsize_t fromwire_bigsize(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
|
|
|
|
{ fprintf(stderr, "fromwire_bigsize called!\n"); abort(); }
|
2020-05-15 12:29:53 +02:00
|
|
|
/* Generated stub for fromwire_channel_id */
|
2021-12-28 00:24:09 +01:00
|
|
|
bool fromwire_channel_id(const u8 **cursor UNNEEDED, size_t *max UNNEEDED,
|
2020-05-15 12:29:53 +02:00
|
|
|
struct channel_id *channel_id UNNEEDED)
|
|
|
|
{ fprintf(stderr, "fromwire_channel_id called!\n"); abort(); }
|
2020-05-16 03:29:05 +02:00
|
|
|
/* Generated stub for fromwire_node_id */
|
|
|
|
void fromwire_node_id(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct node_id *id UNNEEDED)
|
|
|
|
{ fprintf(stderr, "fromwire_node_id called!\n"); abort(); }
|
2022-07-04 05:49:38 +02:00
|
|
|
/* Generated stub for json_to_errcode */
|
|
|
|
bool json_to_errcode(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, errcode_t *errcode UNNEEDED)
|
|
|
|
{ fprintf(stderr, "json_to_errcode called!\n"); abort(); }
|
2022-07-04 05:52:34 +02:00
|
|
|
/* Generated stub for json_to_number */
|
|
|
|
bool json_to_number(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
|
|
|
|
unsigned int *num UNNEEDED)
|
|
|
|
{ fprintf(stderr, "json_to_number called!\n"); abort(); }
|
2018-10-19 03:17:48 +02:00
|
|
|
/* Generated stub for log_ */
|
2019-11-17 12:41:33 +01:00
|
|
|
void log_(struct log *log UNNEEDED, enum log_level level UNNEEDED,
|
|
|
|
const struct node_id *node_id UNNEEDED,
|
|
|
|
bool call_notifier UNNEEDED,
|
|
|
|
const char *fmt UNNEEDED, ...)
|
2018-10-19 03:17:48 +02:00
|
|
|
|
|
|
|
{ fprintf(stderr, "log_ called!\n"); abort(); }
|
|
|
|
/* Generated stub for log_io */
|
2019-11-17 12:41:33 +01:00
|
|
|
void log_io(struct log *log UNNEEDED, enum log_level dir UNNEEDED,
|
|
|
|
const struct node_id *node_id UNNEEDED,
|
|
|
|
const char *comment UNNEEDED,
|
2018-10-19 03:17:48 +02:00
|
|
|
const void *data UNNEEDED, size_t len UNNEEDED)
|
|
|
|
{ fprintf(stderr, "log_io called!\n"); abort(); }
|
2020-10-12 07:33:51 +02:00
|
|
|
/* Generated stub for log_level_name */
|
|
|
|
const char *log_level_name(enum log_level level UNNEEDED)
|
|
|
|
{ fprintf(stderr, "log_level_name called!\n"); abort(); }
|
2018-10-19 03:17:48 +02:00
|
|
|
/* Generated stub for new_log */
|
2019-11-17 12:41:33 +01:00
|
|
|
struct log *new_log(const tal_t *ctx UNNEEDED, struct log_book *record UNNEEDED,
|
|
|
|
const struct node_id *default_node_id UNNEEDED,
|
|
|
|
const char *fmt UNNEEDED, ...)
|
2018-10-19 03:17:48 +02:00
|
|
|
{ fprintf(stderr, "new_log called!\n"); abort(); }
|
2018-11-20 02:46:32 +01:00
|
|
|
/* Generated stub for new_reltimer_ */
|
|
|
|
struct oneshot *new_reltimer_(struct timers *timers UNNEEDED,
|
|
|
|
const tal_t *ctx UNNEEDED,
|
|
|
|
struct timerel expire UNNEEDED,
|
|
|
|
void (*cb)(void *) UNNEEDED, void *arg UNNEEDED)
|
|
|
|
{ fprintf(stderr, "new_reltimer_ called!\n"); abort(); }
|
2018-10-19 03:17:48 +02:00
|
|
|
/* Generated stub for param */
|
|
|
|
bool param(struct command *cmd UNNEEDED, const char *buffer UNNEEDED,
|
|
|
|
const jsmntok_t params[] UNNEEDED, ...)
|
|
|
|
{ fprintf(stderr, "param called!\n"); abort(); }
|
2020-10-12 07:33:50 +02:00
|
|
|
/* Generated stub for param_bool */
|
|
|
|
struct command_result *param_bool(struct command *cmd UNNEEDED, const char *name UNNEEDED,
|
|
|
|
const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
|
|
|
|
bool **b UNNEEDED)
|
|
|
|
{ fprintf(stderr, "param_bool called!\n"); abort(); }
|
2019-07-16 03:41:51 +02:00
|
|
|
/* Generated stub for param_ignore */
|
|
|
|
struct command_result *param_ignore(struct command *cmd UNNEEDED, const char *name UNNEEDED,
|
|
|
|
const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
|
|
|
|
const void *unused UNNEEDED)
|
|
|
|
{ fprintf(stderr, "param_ignore called!\n"); abort(); }
|
2018-12-16 05:50:06 +01:00
|
|
|
/* Generated stub for param_number */
|
|
|
|
struct command_result *param_number(struct command *cmd UNNEEDED, const char *name UNNEEDED,
|
|
|
|
const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
|
|
|
|
unsigned int **num UNNEEDED)
|
|
|
|
{ fprintf(stderr, "param_number called!\n"); abort(); }
|
|
|
|
/* Generated stub for param_sha256 */
|
|
|
|
struct command_result *param_sha256(struct command *cmd UNNEEDED, const char *name UNNEEDED,
|
|
|
|
const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
|
|
|
|
struct sha256 **hash UNNEEDED)
|
|
|
|
{ fprintf(stderr, "param_sha256 called!\n"); abort(); }
|
2022-07-04 03:26:36 +02:00
|
|
|
/* Generated stub for param_string */
|
|
|
|
struct command_result *param_string(struct command *cmd UNNEEDED, const char *name UNNEEDED,
|
|
|
|
const char * buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
|
|
|
|
const char **str UNNEEDED)
|
|
|
|
{ fprintf(stderr, "param_string called!\n"); abort(); }
|
2019-07-16 03:41:51 +02:00
|
|
|
/* Generated stub for param_subcommand */
|
|
|
|
const char *param_subcommand(struct command *cmd UNNEEDED, const char *buffer UNNEEDED,
|
|
|
|
const jsmntok_t tokens[] UNNEEDED,
|
|
|
|
const char *name UNNEEDED, ...)
|
|
|
|
{ fprintf(stderr, "param_subcommand called!\n"); abort(); }
|
2019-09-27 09:50:50 +02:00
|
|
|
/* Generated stub for plugin_hook_call_ */
|
2020-05-05 03:11:59 +02:00
|
|
|
bool plugin_hook_call_(struct lightningd *ld UNNEEDED, const struct plugin_hook *hook UNNEEDED,
|
2020-05-04 09:19:53 +02:00
|
|
|
tal_t *cb_arg STEALS UNNEEDED)
|
2019-09-27 09:50:50 +02:00
|
|
|
{ fprintf(stderr, "plugin_hook_call_ called!\n"); abort(); }
|
2020-05-16 03:29:05 +02:00
|
|
|
/* Generated stub for towire_bigsize */
|
|
|
|
void towire_bigsize(u8 **pptr UNNEEDED, const bigsize_t val UNNEEDED)
|
|
|
|
{ fprintf(stderr, "towire_bigsize called!\n"); abort(); }
|
2020-05-15 12:29:53 +02:00
|
|
|
/* Generated stub for towire_channel_id */
|
|
|
|
void towire_channel_id(u8 **pptr UNNEEDED, const struct channel_id *channel_id UNNEEDED)
|
|
|
|
{ fprintf(stderr, "towire_channel_id called!\n"); abort(); }
|
2020-05-16 03:29:05 +02:00
|
|
|
/* Generated stub for towire_node_id */
|
|
|
|
void towire_node_id(u8 **pptr UNNEEDED, const struct node_id *id UNNEEDED)
|
|
|
|
{ fprintf(stderr, "towire_node_id called!\n"); abort(); }
|
2022-07-04 05:52:34 +02:00
|
|
|
/* Generated stub for try_get_feerate */
|
|
|
|
u32 try_get_feerate(const struct chain_topology *topo UNNEEDED, enum feerate feerate UNNEEDED)
|
|
|
|
{ fprintf(stderr, "try_get_feerate called!\n"); abort(); }
|
2018-10-19 03:17:48 +02:00
|
|
|
/* AUTOGENERATED MOCKS END */
|
|
|
|
|
|
|
|
static int test_json_filter(void)
|
|
|
|
{
|
2019-02-18 03:44:29 +01:00
|
|
|
struct json_stream *result = new_json_stream(NULL, NULL, NULL);
|
2018-10-19 03:17:48 +02:00
|
|
|
jsmntok_t *toks;
|
|
|
|
const jsmntok_t *x;
|
2020-08-20 15:40:32 +02:00
|
|
|
bool valid, complete;
|
2018-10-19 03:17:48 +02:00
|
|
|
int i;
|
|
|
|
char *badstr = tal_arr(result, char, 256);
|
|
|
|
const char *str;
|
2019-06-12 02:38:55 +02:00
|
|
|
size_t len;
|
2020-08-20 15:40:32 +02:00
|
|
|
jsmn_parser parser;
|
2018-10-19 03:17:48 +02:00
|
|
|
|
|
|
|
/* Fill with junk, and nul-terminate (256 -> 0) */
|
|
|
|
for (i = 1; i < 257; i++)
|
|
|
|
badstr[i-1] = i;
|
|
|
|
|
|
|
|
json_object_start(result, NULL);
|
|
|
|
json_add_string(result, "x", badstr);
|
|
|
|
json_object_end(result);
|
|
|
|
|
|
|
|
/* Parse back in, make sure nothing crazy. */
|
2019-06-12 02:38:55 +02:00
|
|
|
str = json_out_contents(result->jout, &len);
|
|
|
|
str = tal_strndup(result, str, len);
|
2018-10-19 03:17:48 +02:00
|
|
|
|
2020-08-20 15:40:32 +02:00
|
|
|
toks = toks_alloc(str);
|
|
|
|
jsmn_init(&parser);
|
|
|
|
valid = json_parse_input(&parser, &toks, str, strlen(str), &complete);
|
2020-11-25 01:17:44 +01:00
|
|
|
/* Fails to parse because it's not valid UTF8 */
|
|
|
|
assert(!valid);
|
2018-10-19 03:17:48 +02:00
|
|
|
|
|
|
|
assert(toks[0].type == JSMN_OBJECT);
|
|
|
|
x = json_get_member(str, toks, "x");
|
|
|
|
assert(x);
|
|
|
|
assert(x->type == JSMN_STRING);
|
|
|
|
|
|
|
|
/* There are 7 one-letter escapes, and (32-5) \uXXXX escapes. */
|
|
|
|
assert((x->end - x->start) == 255 + 7*1 + (32-5)*5);
|
|
|
|
/* No control characters. */
|
|
|
|
for (i = x->start; i < x->end; i++) {
|
|
|
|
assert((unsigned)str[i] >= ' ');
|
|
|
|
assert((unsigned)str[i] != 127);
|
|
|
|
}
|
2018-11-20 02:46:32 +01:00
|
|
|
tal_free(result);
|
2018-10-19 03:17:48 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_json_escape(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 1; i < 256; i++) {
|
|
|
|
char badstr[2];
|
2019-02-18 03:44:29 +01:00
|
|
|
struct json_stream *result = new_json_stream(NULL, NULL, NULL);
|
2019-06-12 02:38:54 +02:00
|
|
|
struct json_escape *esc;
|
2018-10-19 03:17:48 +02:00
|
|
|
|
|
|
|
badstr[0] = i;
|
|
|
|
badstr[1] = 0;
|
|
|
|
|
|
|
|
json_object_start(result, NULL);
|
|
|
|
esc = json_escape(NULL, badstr);
|
|
|
|
json_add_escaped_string(result, "x", take(esc));
|
|
|
|
json_object_end(result);
|
|
|
|
|
2019-06-12 02:38:55 +02:00
|
|
|
size_t len;
|
|
|
|
const char *str = json_out_contents(result->jout, &len);
|
|
|
|
str = tal_strndup(result, str, len);
|
2018-10-19 03:17:48 +02:00
|
|
|
if (i == '\\' || i == '"'
|
|
|
|
|| i == '\n' || i == '\r' || i == '\b'
|
|
|
|
|| i == '\t' || i == '\f')
|
2019-04-08 11:58:44 +02:00
|
|
|
assert(strstarts(str, "{\"x\":\"\\"));
|
2018-10-19 03:17:48 +02:00
|
|
|
else if (i < 32 || i == 127) {
|
2019-04-08 11:58:44 +02:00
|
|
|
assert(strstarts(str, "{\"x\":\"\\u00"));
|
2018-10-19 03:17:48 +02:00
|
|
|
} else {
|
2019-04-08 11:58:44 +02:00
|
|
|
char expect[] = "{\"x\":\"?\"}";
|
|
|
|
expect[6] = i;
|
2018-10-19 03:17:48 +02:00
|
|
|
assert(streq(str, expect));
|
|
|
|
}
|
2018-11-20 02:46:32 +01:00
|
|
|
tal_free(result);
|
2018-10-19 03:17:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_json_partial(void)
|
|
|
|
{
|
|
|
|
const tal_t *ctx = tal(NULL, char);
|
|
|
|
|
|
|
|
assert(streq(json_partial_escape(ctx, "\\")->s, "\\\\"));
|
|
|
|
assert(streq(json_partial_escape(ctx, "\\\\")->s, "\\\\"));
|
|
|
|
assert(streq(json_partial_escape(ctx, "\\\\\\")->s, "\\\\\\\\"));
|
|
|
|
assert(streq(json_partial_escape(ctx, "\\\\\\\\")->s, "\\\\\\\\"));
|
|
|
|
assert(streq(json_partial_escape(ctx, "\\n")->s, "\\n"));
|
|
|
|
assert(streq(json_partial_escape(ctx, "\n")->s, "\\n"));
|
|
|
|
assert(streq(json_partial_escape(ctx, "\\\"")->s, "\\\""));
|
|
|
|
assert(streq(json_partial_escape(ctx, "\"")->s, "\\\""));
|
|
|
|
assert(streq(json_partial_escape(ctx, "\\t")->s, "\\t"));
|
|
|
|
assert(streq(json_partial_escape(ctx, "\t")->s, "\\t"));
|
|
|
|
assert(streq(json_partial_escape(ctx, "\\b")->s, "\\b"));
|
|
|
|
assert(streq(json_partial_escape(ctx, "\b")->s, "\\b"));
|
|
|
|
assert(streq(json_partial_escape(ctx, "\\r")->s, "\\r"));
|
|
|
|
assert(streq(json_partial_escape(ctx, "\r")->s, "\\r"));
|
|
|
|
assert(streq(json_partial_escape(ctx, "\\f")->s, "\\f"));
|
|
|
|
assert(streq(json_partial_escape(ctx, "\f")->s, "\\f"));
|
|
|
|
/* You're allowed to escape / according to json.org. */
|
|
|
|
assert(streq(json_partial_escape(ctx, "\\/")->s, "\\/"));
|
|
|
|
assert(streq(json_partial_escape(ctx, "/")->s, "/"));
|
|
|
|
|
|
|
|
assert(streq(json_partial_escape(ctx, "\\u0FFF")->s, "\\u0FFF"));
|
|
|
|
assert(streq(json_partial_escape(ctx, "\\u0FFFx")->s, "\\u0FFFx"));
|
|
|
|
|
|
|
|
/* Unknown escapes should be escaped. */
|
|
|
|
assert(streq(json_partial_escape(ctx, "\\x")->s, "\\\\x"));
|
|
|
|
tal_free(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Test that we can segment and parse a stream of json objects correctly. */
|
|
|
|
static void test_json_stream(void)
|
|
|
|
{
|
2020-08-20 15:40:32 +02:00
|
|
|
bool valid, complete;
|
2018-10-19 03:17:48 +02:00
|
|
|
char *input, *talstr;
|
|
|
|
jsmntok_t *toks;
|
2020-08-20 15:40:32 +02:00
|
|
|
jsmn_parser parser;
|
2018-10-19 03:17:48 +02:00
|
|
|
|
|
|
|
/* Multiple full messages in a single buffer (happens when buffer
|
|
|
|
* boundary coincides with message boundary, or read returned after
|
|
|
|
* timeout. */
|
|
|
|
input = "{\"x\":\"x\"}{\"y\":\"y\"}";
|
|
|
|
talstr = tal_strndup(NULL, input, strlen(input));
|
2020-08-20 15:40:32 +02:00
|
|
|
toks = toks_alloc(NULL);
|
|
|
|
jsmn_init(&parser);
|
|
|
|
valid = json_parse_input(&parser, &toks, talstr, strlen(talstr), &complete);
|
2018-10-19 03:17:48 +02:00
|
|
|
assert(tal_count(toks) == 4);
|
|
|
|
assert(toks[0].start == 0 && toks[0].end == 9);
|
|
|
|
assert(valid);
|
2020-08-20 15:40:32 +02:00
|
|
|
assert(complete);
|
2018-10-19 03:17:48 +02:00
|
|
|
tal_free(talstr);
|
|
|
|
|
|
|
|
/* Multiple messages, and the last one is partial, far more likely than
|
|
|
|
* accidentally getting the boundaries to match. */
|
|
|
|
input = "{\"x\":\"x\"}{\"y\":\"y\"}{\"z\":\"z";
|
|
|
|
talstr = tal_strndup(NULL, input, strlen(input));
|
2020-08-20 15:40:32 +02:00
|
|
|
toks_reset(toks);
|
|
|
|
jsmn_init(&parser);
|
|
|
|
valid = json_parse_input(&parser, &toks, talstr, strlen(talstr), &complete);
|
2018-10-19 03:17:48 +02:00
|
|
|
assert(toks);
|
|
|
|
assert(tal_count(toks) == 4);
|
|
|
|
assert(toks[0].start == 0 && toks[0].end == 9);
|
|
|
|
assert(valid);
|
2020-08-20 15:40:32 +02:00
|
|
|
assert(complete);
|
2018-10-19 03:17:48 +02:00
|
|
|
tal_free(talstr);
|
2020-08-20 15:40:32 +02:00
|
|
|
|
|
|
|
/* We can do this incrementally, too. */
|
|
|
|
toks_reset(toks);
|
|
|
|
input = "{\"x\":\"x\"}";
|
|
|
|
jsmn_init(&parser);
|
|
|
|
for (size_t i = 0; i <= strlen(input); i++) {
|
|
|
|
valid = json_parse_input(&parser, &toks, input, i, &complete);
|
|
|
|
assert(valid);
|
|
|
|
if (i == strlen(input))
|
|
|
|
assert(complete);
|
|
|
|
else
|
|
|
|
assert(!complete);
|
|
|
|
}
|
|
|
|
assert(tal_count(toks) == 4);
|
|
|
|
assert(toks[0].start == 0 && toks[0].end == 9);
|
|
|
|
tal_free(toks);
|
2018-10-19 03:17:48 +02:00
|
|
|
}
|
|
|
|
|
2020-12-02 01:10:04 +01:00
|
|
|
int main(int argc, char *argv[])
|
2018-10-19 03:17:48 +02:00
|
|
|
{
|
2020-12-02 01:10:04 +01:00
|
|
|
common_setup(argv[0]);
|
2018-10-19 03:17:48 +02:00
|
|
|
|
|
|
|
test_json_filter();
|
|
|
|
test_json_escape();
|
|
|
|
test_json_partial();
|
|
|
|
test_json_stream();
|
2020-12-02 01:10:04 +01:00
|
|
|
|
|
|
|
common_shutdown();
|
2018-10-19 03:17:48 +02:00
|
|
|
}
|