mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-24 01:24:26 +01:00
Move json and param core functionality into common, for plugins.
json_escaped.[ch], param.[ch] and jsonrpc_errors.h move from lightningd/ to common/. Tests moved too. We add a new 'common/json_tok.[ch]' for the common parameter parsing routines which a plugin might want, taking them out of lightningd/json.c (which now only contains the lightningd-specific ones). The rest is mainly fixing up includes. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
@@ -25,10 +25,13 @@ COMMON_SRC_NOGEN := \
|
||||
common/initial_commit_tx.c \
|
||||
common/io_lock.c \
|
||||
common/json.c \
|
||||
common/json_escaped.c \
|
||||
common/json_tok.c \
|
||||
common/key_derive.c \
|
||||
common/keyset.c \
|
||||
common/memleak.c \
|
||||
common/msg_queue.c \
|
||||
common/param.c \
|
||||
common/peer_billboard.c \
|
||||
common/peer_failed.c \
|
||||
common/permute_tx.c \
|
||||
@@ -52,7 +55,7 @@ COMMON_SRC_NOGEN := \
|
||||
|
||||
COMMON_SRC_GEN := common/gen_status_wire.c common/gen_peer_status_wire.c
|
||||
|
||||
COMMON_HEADERS_NOGEN := $(COMMON_SRC_NOGEN:.c=.h) common/overflows.h common/htlc.h common/status_levels.h common/json_command.h
|
||||
COMMON_HEADERS_NOGEN := $(COMMON_SRC_NOGEN:.c=.h) common/overflows.h common/htlc.h common/status_levels.h common/json_command.h common/jsonrpc_errors.h
|
||||
COMMON_HEADERS_GEN := common/gen_htlc_state_names.h common/gen_status_wire.h common/gen_peer_status_wire.h
|
||||
|
||||
COMMON_HEADERS := $(COMMON_HEADERS_GEN) $(COMMON_HEADERS_NOGEN)
|
||||
|
||||
162
common/json_escaped.c
Normal file
162
common/json_escaped.c
Normal file
@@ -0,0 +1,162 @@
|
||||
#include <common/json_escaped.h>
|
||||
#include <stdio.h>
|
||||
|
||||
struct json_escaped *json_escaped_string_(const tal_t *ctx,
|
||||
const void *bytes, size_t len)
|
||||
{
|
||||
struct json_escaped *esc;
|
||||
|
||||
esc = (void *)tal_arr_label(ctx, char, len + 1,
|
||||
TAL_LABEL(struct json_escaped, ""));
|
||||
memcpy(esc->s, bytes, len);
|
||||
esc->s[len] = '\0';
|
||||
return esc;
|
||||
}
|
||||
|
||||
struct json_escaped *json_to_escaped_string(const tal_t *ctx,
|
||||
const char *buffer,
|
||||
const jsmntok_t *tok)
|
||||
{
|
||||
if (tok->type != JSMN_STRING)
|
||||
return NULL;
|
||||
/* jsmn always gives us ~ well-formed strings. */
|
||||
return json_escaped_string_(ctx, buffer + tok->start,
|
||||
tok->end - tok->start);
|
||||
}
|
||||
|
||||
bool json_escaped_eq(const struct json_escaped *a,
|
||||
const struct json_escaped *b)
|
||||
{
|
||||
return streq(a->s, b->s);
|
||||
}
|
||||
|
||||
static struct json_escaped *escape(const tal_t *ctx,
|
||||
const char *str TAKES,
|
||||
bool partial)
|
||||
{
|
||||
struct json_escaped *esc;
|
||||
size_t i, n;
|
||||
|
||||
/* Worst case: all \uXXXX */
|
||||
esc = (struct json_escaped *)tal_arr(ctx, char, strlen(str) * 6 + 1);
|
||||
|
||||
for (i = n = 0; str[i]; i++, n++) {
|
||||
char escape = 0;
|
||||
switch (str[i]) {
|
||||
case '\n':
|
||||
escape = 'n';
|
||||
break;
|
||||
case '\b':
|
||||
escape = 'b';
|
||||
break;
|
||||
case '\f':
|
||||
escape = 'f';
|
||||
break;
|
||||
case '\t':
|
||||
escape = 't';
|
||||
break;
|
||||
case '\r':
|
||||
escape = 'r';
|
||||
break;
|
||||
case '\\':
|
||||
if (partial) {
|
||||
/* Don't double-escape standard escapes. */
|
||||
if (str[i+1] == 'n'
|
||||
|| str[i+1] == 'b'
|
||||
|| str[i+1] == 'f'
|
||||
|| str[i+1] == 't'
|
||||
|| str[i+1] == 'r'
|
||||
|| str[i+1] == '/'
|
||||
|| str[i+1] == '\\'
|
||||
|| str[i+1] == '"') {
|
||||
escape = str[i+1];
|
||||
i++;
|
||||
break;
|
||||
}
|
||||
if (str[i+1] == 'u'
|
||||
&& cisxdigit(str[i+2])
|
||||
&& cisxdigit(str[i+3])
|
||||
&& cisxdigit(str[i+4])
|
||||
&& cisxdigit(str[i+5])) {
|
||||
memcpy(esc->s + n, str + i, 6);
|
||||
n += 5;
|
||||
i += 5;
|
||||
continue;
|
||||
}
|
||||
} /* fall thru */
|
||||
case '"':
|
||||
escape = str[i];
|
||||
break;
|
||||
default:
|
||||
if ((unsigned)str[i] < ' ' || str[i] == 127) {
|
||||
snprintf(esc->s + n, 7, "\\u%04X", str[i]);
|
||||
n += 5;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (escape) {
|
||||
esc->s[n++] = '\\';
|
||||
esc->s[n] = escape;
|
||||
} else
|
||||
esc->s[n] = str[i];
|
||||
}
|
||||
|
||||
esc->s[n] = '\0';
|
||||
if (taken(str))
|
||||
tal_free(str);
|
||||
return esc;
|
||||
}
|
||||
|
||||
struct json_escaped *json_partial_escape(const tal_t *ctx, const char *str TAKES)
|
||||
{
|
||||
return escape(ctx, str, true);
|
||||
}
|
||||
|
||||
struct json_escaped *json_escape(const tal_t *ctx, const char *str TAKES)
|
||||
{
|
||||
return escape(ctx, str, false);
|
||||
}
|
||||
|
||||
/* By policy, we don't handle \u. Use UTF-8. */
|
||||
const char *json_escaped_unescape(const tal_t *ctx,
|
||||
const struct json_escaped *esc)
|
||||
{
|
||||
char *unesc = tal_arr(ctx, char, strlen(esc->s) + 1);
|
||||
size_t i, n;
|
||||
|
||||
for (i = n = 0; esc->s[i]; i++, n++) {
|
||||
if (esc->s[i] != '\\') {
|
||||
unesc[n] = esc->s[i];
|
||||
continue;
|
||||
}
|
||||
|
||||
i++;
|
||||
switch (esc->s[i]) {
|
||||
case 'n':
|
||||
unesc[n] = '\n';
|
||||
break;
|
||||
case 'b':
|
||||
unesc[n] = '\b';
|
||||
break;
|
||||
case 'f':
|
||||
unesc[n] = '\f';
|
||||
break;
|
||||
case 't':
|
||||
unesc[n] = '\t';
|
||||
break;
|
||||
case 'r':
|
||||
unesc[n] = '\r';
|
||||
break;
|
||||
case '/':
|
||||
case '\\':
|
||||
case '"':
|
||||
unesc[n] = esc->s[i];
|
||||
break;
|
||||
default:
|
||||
return tal_free(unesc);
|
||||
}
|
||||
}
|
||||
|
||||
unesc[n] = '\0';
|
||||
return unesc;
|
||||
}
|
||||
35
common/json_escaped.h
Normal file
35
common/json_escaped.h
Normal file
@@ -0,0 +1,35 @@
|
||||
#ifndef LIGHTNING_COMMON_JSON_ESCAPED_H
|
||||
#define LIGHTNING_COMMON_JSON_ESCAPED_H
|
||||
#include "config.h"
|
||||
#include <common/json.h>
|
||||
|
||||
/* Type differentiation for a correctly-escaped JSON string */
|
||||
struct json_escaped {
|
||||
/* NUL terminated string. */
|
||||
char s[1];
|
||||
};
|
||||
|
||||
/* @str be a valid UTF-8 string */
|
||||
struct json_escaped *json_escape(const tal_t *ctx, const char *str TAKES);
|
||||
|
||||
/* @str is a valid UTF-8 string which may already contain escapes. */
|
||||
struct json_escaped *json_partial_escape(const tal_t *ctx,
|
||||
const char *str TAKES);
|
||||
|
||||
/* Extract a JSON-escaped string. */
|
||||
struct json_escaped *json_to_escaped_string(const tal_t *ctx,
|
||||
const char *buffer,
|
||||
const jsmntok_t *tok);
|
||||
|
||||
/* Are two escaped json strings identical? */
|
||||
bool json_escaped_eq(const struct json_escaped *a,
|
||||
const struct json_escaped *b);
|
||||
|
||||
/* Internal routine for creating json_escaped from bytes. */
|
||||
struct json_escaped *json_escaped_string_(const tal_t *ctx,
|
||||
const void *bytes, size_t len);
|
||||
|
||||
/* Be very careful here! Can fail! Doesn't handle \u: use UTF-8 please. */
|
||||
const char *json_escaped_unescape(const tal_t *ctx,
|
||||
const struct json_escaped *esc);
|
||||
#endif /* LIGHTNING_COMMON_JSON_ESCAPED_H */
|
||||
184
common/json_tok.c
Normal file
184
common/json_tok.c
Normal file
@@ -0,0 +1,184 @@
|
||||
#include <ccan/mem/mem.h>
|
||||
#include <ccan/str/hex/hex.h>
|
||||
#include <ccan/tal/str/str.h>
|
||||
#include <common/json_command.h>
|
||||
#include <common/json_escaped.h>
|
||||
#include <common/json_tok.h>
|
||||
#include <common/jsonrpc_errors.h>
|
||||
#include <common/param.h>
|
||||
|
||||
bool json_tok_array(struct command *cmd, const char *name,
|
||||
const char *buffer, const jsmntok_t *tok,
|
||||
const jsmntok_t **arr)
|
||||
{
|
||||
if (tok->type == JSMN_ARRAY)
|
||||
return (*arr = tok);
|
||||
|
||||
command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
||||
"'%s' should be an array, not '%.*s'",
|
||||
name, tok->end - tok->start, buffer + tok->start);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool json_tok_bool(struct command *cmd, const char *name,
|
||||
const char *buffer, const jsmntok_t *tok,
|
||||
bool **b)
|
||||
{
|
||||
*b = tal(cmd, bool);
|
||||
if (tok->type == JSMN_PRIMITIVE) {
|
||||
if (memeqstr(buffer + tok->start, tok->end - tok->start, "true")) {
|
||||
**b = true;
|
||||
return true;
|
||||
}
|
||||
if (memeqstr(buffer + tok->start, tok->end - tok->start, "false")) {
|
||||
**b = false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
||||
"'%s' should be 'true' or 'false', not '%.*s'",
|
||||
name, tok->end - tok->start, buffer + tok->start);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool json_tok_double(struct command *cmd, const char *name,
|
||||
const char *buffer, const jsmntok_t *tok,
|
||||
double **num)
|
||||
{
|
||||
*num = tal(cmd, double);
|
||||
if (json_to_double(buffer, tok, *num))
|
||||
return true;
|
||||
|
||||
command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
||||
"'%s' should be a double, not '%.*s'",
|
||||
name, tok->end - tok->start, buffer + tok->start);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool json_tok_escaped_string(struct command *cmd, const char *name,
|
||||
const char * buffer, const jsmntok_t *tok,
|
||||
const char **str)
|
||||
{
|
||||
struct json_escaped *esc = json_to_escaped_string(cmd, buffer, tok);
|
||||
if (esc) {
|
||||
*str = json_escaped_unescape(cmd, esc);
|
||||
if (*str)
|
||||
return true;
|
||||
}
|
||||
command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
||||
"'%s' should be a string, not '%.*s'"
|
||||
" (note, we don't allow \\u)",
|
||||
name,
|
||||
tok->end - tok->start, buffer + tok->start);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool json_tok_string(struct command *cmd, const char *name,
|
||||
const char * buffer, const jsmntok_t *tok,
|
||||
const char **str)
|
||||
{
|
||||
*str = tal_strndup(cmd, buffer + tok->start,
|
||||
tok->end - tok->start);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool json_tok_label(struct command *cmd, const char *name,
|
||||
const char * buffer, const jsmntok_t *tok,
|
||||
struct json_escaped **label)
|
||||
{
|
||||
/* We accept both strings and number literals here. */
|
||||
*label = json_escaped_string_(cmd, buffer + tok->start, tok->end - tok->start);
|
||||
if (*label && (tok->type == JSMN_STRING || json_tok_is_num(buffer, tok)))
|
||||
return true;
|
||||
|
||||
command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
||||
"'%s' should be a string or number, not '%.*s'",
|
||||
name, tok->end - tok->start, buffer + tok->start);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool json_tok_number(struct command *cmd, const char *name,
|
||||
const char *buffer, const jsmntok_t *tok,
|
||||
unsigned int **num)
|
||||
{
|
||||
*num = tal(cmd, unsigned int);
|
||||
if (json_to_number(buffer, tok, *num))
|
||||
return true;
|
||||
|
||||
command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
||||
"'%s' should be an integer, not '%.*s'",
|
||||
name, tok->end - tok->start, buffer + tok->start);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool json_tok_sha256(struct command *cmd, const char *name,
|
||||
const char *buffer, const jsmntok_t *tok,
|
||||
struct sha256 **hash)
|
||||
{
|
||||
*hash = tal(cmd, struct sha256);
|
||||
if (hex_decode(buffer + tok->start,
|
||||
tok->end - tok->start,
|
||||
*hash, sizeof(**hash)))
|
||||
return true;
|
||||
|
||||
command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
||||
"'%s' should be a 32 byte hex value, not '%.*s'",
|
||||
name, tok->end - tok->start, buffer + tok->start);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool json_tok_msat(struct command *cmd, const char *name,
|
||||
const char *buffer, const jsmntok_t * tok,
|
||||
u64 **msatoshi_val)
|
||||
{
|
||||
if (json_tok_streq(buffer, tok, "any")) {
|
||||
*msatoshi_val = NULL;
|
||||
return true;
|
||||
}
|
||||
*msatoshi_val = tal(cmd, u64);
|
||||
|
||||
if (json_to_u64(buffer, tok, *msatoshi_val) && *msatoshi_val != 0)
|
||||
return true;
|
||||
|
||||
command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
||||
"'%s' should be a positive number or 'any', not '%.*s'",
|
||||
name,
|
||||
tok->end - tok->start,
|
||||
buffer + tok->start);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool json_tok_percent(struct command *cmd, const char *name,
|
||||
const char *buffer, const jsmntok_t *tok,
|
||||
double **num)
|
||||
{
|
||||
*num = tal(cmd, double);
|
||||
if (json_to_double(buffer, tok, *num) && **num >= 0.0)
|
||||
return true;
|
||||
|
||||
command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
||||
"'%s' should be a positive double, not '%.*s'",
|
||||
name, tok->end - tok->start, buffer + tok->start);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool json_tok_u64(struct command *cmd, const char *name,
|
||||
const char *buffer, const jsmntok_t *tok,
|
||||
uint64_t **num)
|
||||
{
|
||||
*num = tal(cmd, uint64_t);
|
||||
if (json_to_u64(buffer, tok, *num))
|
||||
return true;
|
||||
|
||||
command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
||||
"'%s' should be an unsigned 64 bit integer, not '%.*s'",
|
||||
name, tok->end - tok->start, buffer + tok->start);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool json_tok_tok(struct command *cmd, const char *name,
|
||||
const char *buffer, const jsmntok_t * tok,
|
||||
const jsmntok_t **out)
|
||||
{
|
||||
return (*out = tok);
|
||||
}
|
||||
72
common/json_tok.h
Normal file
72
common/json_tok.h
Normal file
@@ -0,0 +1,72 @@
|
||||
/* Helpers for use with param parsing. */
|
||||
#ifndef LIGHTNING_COMMON_JSON_TOK_H
|
||||
#define LIGHTNING_COMMON_JSON_TOK_H
|
||||
#include "config.h"
|
||||
#include <common/json.h>
|
||||
|
||||
struct command;
|
||||
|
||||
/* Extract json array token */
|
||||
bool json_tok_array(struct command *cmd, const char *name,
|
||||
const char *buffer, const jsmntok_t *tok,
|
||||
const jsmntok_t **arr);
|
||||
|
||||
/* Extract boolean this (must be a true or false) */
|
||||
bool json_tok_bool(struct command *cmd, const char *name,
|
||||
const char *buffer, const jsmntok_t *tok,
|
||||
bool **b);
|
||||
|
||||
/* Extract double from this (must be a number literal) */
|
||||
bool json_tok_double(struct command *cmd, const char *name,
|
||||
const char *buffer, const jsmntok_t *tok,
|
||||
double **num);
|
||||
|
||||
/* Extract an escaped string (and unescape it) */
|
||||
bool json_tok_escaped_string(struct command *cmd, const char *name,
|
||||
const char * buffer, const jsmntok_t *tok,
|
||||
const char **str);
|
||||
|
||||
/* Extract a string */
|
||||
bool json_tok_string(struct command *cmd, const char *name,
|
||||
const char * buffer, const jsmntok_t *tok,
|
||||
const char **str);
|
||||
|
||||
/* Extract a label. It is either an escaped string or a number. */
|
||||
bool json_tok_label(struct command *cmd, const char *name,
|
||||
const char * buffer, const jsmntok_t *tok,
|
||||
struct json_escaped **label);
|
||||
|
||||
/* Extract number from this (may be a string, or a number literal) */
|
||||
bool json_tok_number(struct command *cmd, const char *name,
|
||||
const char *buffer, const jsmntok_t *tok,
|
||||
unsigned int **num);
|
||||
|
||||
/* Extract sha256 hash */
|
||||
bool json_tok_sha256(struct command *cmd, const char *name,
|
||||
const char *buffer, const jsmntok_t *tok,
|
||||
struct sha256 **hash);
|
||||
|
||||
/* Extract positive integer, or NULL if tok is 'any'. */
|
||||
bool json_tok_msat(struct command *cmd, const char *name,
|
||||
const char *buffer, const jsmntok_t * tok,
|
||||
u64 **msatoshi_val);
|
||||
|
||||
/* Extract double in range [0.0, 100.0] */
|
||||
bool json_tok_percent(struct command *cmd, const char *name,
|
||||
const char *buffer, const jsmntok_t *tok,
|
||||
double **num);
|
||||
|
||||
/* Extract number from this (may be a string, or a number literal) */
|
||||
bool json_tok_u64(struct command *cmd, const char *name,
|
||||
const char *buffer, const jsmntok_t *tok,
|
||||
uint64_t **num);
|
||||
|
||||
/*
|
||||
* Set the address of @out to @tok. Used as a callback by handlers that
|
||||
* want to unmarshal @tok themselves.
|
||||
*/
|
||||
bool json_tok_tok(struct command *cmd, const char *name,
|
||||
const char *buffer, const jsmntok_t * tok,
|
||||
const jsmntok_t **out);
|
||||
|
||||
#endif /* LIGHTNING_COMMON_JSON_TOK_H */
|
||||
47
common/jsonrpc_errors.h
Normal file
47
common/jsonrpc_errors.h
Normal file
@@ -0,0 +1,47 @@
|
||||
/* common/jsonrpc_errors.h
|
||||
* Lists error codes for JSON-RPC.
|
||||
*/
|
||||
#ifndef LIGHTNING_COMMON_JSONRPC_ERRORS_H
|
||||
#define LIGHTNING_COMMON_JSONRPC_ERRORS_H
|
||||
#include "config.h"
|
||||
|
||||
/* Standard errors defined by JSON-RPC 2.0 standard */
|
||||
#define JSONRPC2_INVALID_REQUEST -32600
|
||||
#define JSONRPC2_METHOD_NOT_FOUND -32601
|
||||
#define JSONRPC2_INVALID_PARAMS -32602
|
||||
|
||||
/* Uncategorized error.
|
||||
* FIXME: This should be replaced in all places
|
||||
* with a specific error code, and then removed.
|
||||
*/
|
||||
#define LIGHTNINGD -1
|
||||
|
||||
/* Developer error in the parameters to param() call */
|
||||
#define PARAM_DEV_ERROR -2
|
||||
|
||||
/* Plugin returned an error */
|
||||
#define PLUGIN_ERROR -3
|
||||
|
||||
/* Errors from `pay`, `sendpay`, or `waitsendpay` commands */
|
||||
#define PAY_IN_PROGRESS 200
|
||||
#define PAY_RHASH_ALREADY_USED 201
|
||||
#define PAY_UNPARSEABLE_ONION 202
|
||||
#define PAY_DESTINATION_PERM_FAIL 203
|
||||
#define PAY_TRY_OTHER_ROUTE 204
|
||||
#define PAY_ROUTE_NOT_FOUND 205
|
||||
#define PAY_ROUTE_TOO_EXPENSIVE 206
|
||||
#define PAY_INVOICE_EXPIRED 207
|
||||
#define PAY_NO_SUCH_PAYMENT 208
|
||||
#define PAY_UNSPECIFIED_ERROR 209
|
||||
#define PAY_STOPPED_RETRYING 210
|
||||
|
||||
/* `fundchannel` or `withdraw` errors */
|
||||
#define FUND_MAX_EXCEEDED 300
|
||||
#define FUND_CANNOT_AFFORD 301
|
||||
#define FUND_OUTPUT_IS_DUST 302
|
||||
|
||||
/* Errors from `invoice` command */
|
||||
#define INVOICE_LABEL_ALREADY_EXISTS 900
|
||||
#define INVOICE_PREIMAGE_ALREADY_EXISTS 901
|
||||
|
||||
#endif /* LIGHTNING_COMMON_JSONRPC_ERRORS_H */
|
||||
296
common/param.c
Normal file
296
common/param.c
Normal file
@@ -0,0 +1,296 @@
|
||||
#include <ccan/asort/asort.h>
|
||||
#include <ccan/tal/str/str.h>
|
||||
#include <common/json_command.h>
|
||||
#include <common/jsonrpc_errors.h>
|
||||
#include <common/param.h>
|
||||
#include <common/utils.h>
|
||||
|
||||
struct param {
|
||||
const char *name;
|
||||
bool is_set;
|
||||
bool required;
|
||||
param_cbx cbx;
|
||||
void *arg;
|
||||
};
|
||||
|
||||
static bool param_add(struct param **params,
|
||||
const char *name, bool required,
|
||||
param_cbx cbx, void *arg)
|
||||
{
|
||||
#if DEVELOPER
|
||||
if (!(name && cbx && arg))
|
||||
return false;
|
||||
#endif
|
||||
struct param *last = tal_arr_expand(params);
|
||||
|
||||
last->is_set = false;
|
||||
last->name = name;
|
||||
last->required = required;
|
||||
last->cbx = cbx;
|
||||
last->arg = arg;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool make_callback(struct command *cmd,
|
||||
struct param *def,
|
||||
const char *buffer, const jsmntok_t *tok)
|
||||
{
|
||||
def->is_set = true;
|
||||
|
||||
return def->cbx(cmd, def->name, buffer, tok, def->arg);
|
||||
}
|
||||
|
||||
static bool post_check(struct command *cmd, struct param *params)
|
||||
{
|
||||
struct param *first = params;
|
||||
struct param *last = first + tal_count(params);
|
||||
|
||||
/* Make sure required params were provided. */
|
||||
while (first != last && first->required) {
|
||||
if (!first->is_set) {
|
||||
command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
||||
"missing required parameter: '%s'",
|
||||
first->name);
|
||||
return false;
|
||||
}
|
||||
first++;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool parse_by_position(struct command *cmd,
|
||||
struct param *params,
|
||||
const char *buffer,
|
||||
const jsmntok_t tokens[],
|
||||
bool allow_extra)
|
||||
{
|
||||
const jsmntok_t *tok = tokens + 1;
|
||||
const jsmntok_t *end = json_next(tokens);
|
||||
struct param *first = params;
|
||||
struct param *last = first + tal_count(params);
|
||||
|
||||
while (first != last && tok != end) {
|
||||
if (!json_tok_is_null(buffer, tok))
|
||||
if (!make_callback(cmd, first, buffer, tok))
|
||||
return NULL;
|
||||
tok = json_next(tok);
|
||||
first++;
|
||||
}
|
||||
|
||||
/* check for unexpected trailing params */
|
||||
if (!allow_extra && tok != end) {
|
||||
command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
||||
"too many parameters:"
|
||||
" got %u, expected %zu",
|
||||
tokens->size, tal_count(params));
|
||||
return false;
|
||||
}
|
||||
|
||||
return post_check(cmd, params);
|
||||
}
|
||||
|
||||
static struct param *find_param(struct param *params, const char *start,
|
||||
size_t n)
|
||||
{
|
||||
struct param *first = params;
|
||||
struct param *last = first + tal_count(params);
|
||||
|
||||
while (first != last) {
|
||||
if (strncmp(first->name, start, n) == 0)
|
||||
if (strlen(first->name) == n)
|
||||
return first;
|
||||
first++;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static bool parse_by_name(struct command *cmd,
|
||||
struct param *params,
|
||||
const char *buffer,
|
||||
const jsmntok_t tokens[],
|
||||
bool allow_extra)
|
||||
{
|
||||
const jsmntok_t *first = tokens + 1;
|
||||
const jsmntok_t *last = json_next(tokens);
|
||||
|
||||
while (first != last) {
|
||||
struct param *p = find_param(params, buffer + first->start,
|
||||
first->end - first->start);
|
||||
if (!p) {
|
||||
if (!allow_extra) {
|
||||
command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
||||
"unknown parameter: '%.*s'",
|
||||
first->end - first->start,
|
||||
buffer + first->start);
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if (p->is_set) {
|
||||
command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
||||
"duplicate json names: '%s'", p->name);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!make_callback(cmd, p, buffer, first + 1))
|
||||
return false;
|
||||
}
|
||||
first = json_next(first + 1);
|
||||
}
|
||||
return post_check(cmd, params);
|
||||
}
|
||||
|
||||
#if DEVELOPER
|
||||
static int comp_by_name(const struct param *a, const struct param *b,
|
||||
void *unused)
|
||||
{
|
||||
return strcmp(a->name, b->name);
|
||||
}
|
||||
|
||||
static int comp_by_arg(const struct param *a, const struct param *b,
|
||||
void *unused)
|
||||
{
|
||||
/* size_t could be larger than int: don't turn a 4bn difference into 0 */
|
||||
if (a->arg > b->arg)
|
||||
return 1;
|
||||
else if (a->arg < b->arg)
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* This comparator is a bit different, but works well.
|
||||
* Return 0 if @a is optional and @b is required. Otherwise return 1.
|
||||
*/
|
||||
static int comp_req_order(const struct param *a, const struct param *b,
|
||||
void *unused)
|
||||
{
|
||||
if (!a->required && b->required)
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Make sure 2 sequential items in @params are not equal (based on
|
||||
* provided comparator).
|
||||
*/
|
||||
static bool check_distinct(struct param *params,
|
||||
int (*compar) (const struct param *a,
|
||||
const struct param *b, void *unused))
|
||||
{
|
||||
struct param *first = params;
|
||||
struct param *last = first + tal_count(params);
|
||||
first++;
|
||||
while (first != last) {
|
||||
if (compar(first - 1, first, NULL) == 0)
|
||||
return false;
|
||||
first++;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool check_unique(struct param *copy,
|
||||
int (*compar) (const struct param *a,
|
||||
const struct param *b, void *unused))
|
||||
{
|
||||
asort(copy, tal_count(copy), compar, NULL);
|
||||
return check_distinct(copy, compar);
|
||||
}
|
||||
|
||||
/*
|
||||
* Verify consistent internal state.
|
||||
*/
|
||||
static bool check_params(struct param *params)
|
||||
{
|
||||
if (tal_count(params) < 2)
|
||||
return true;
|
||||
|
||||
/* make sure there are no required params following optional */
|
||||
if (!check_distinct(params, comp_req_order))
|
||||
return false;
|
||||
|
||||
/* duplicate so we can sort */
|
||||
struct param *copy = tal_dup_arr(params, struct param,
|
||||
params, tal_count(params), 0);
|
||||
|
||||
/* check for repeated names and args */
|
||||
if (!check_unique(copy, comp_by_name))
|
||||
return false;
|
||||
if (!check_unique(copy, comp_by_arg))
|
||||
return false;
|
||||
|
||||
tal_free(copy);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
static char *param_usage(const tal_t *ctx,
|
||||
const struct param *params)
|
||||
{
|
||||
char *usage = tal_strdup(ctx, "");
|
||||
for (size_t i = 0; i < tal_count(params); i++) {
|
||||
if (i != 0)
|
||||
tal_append_fmt(&usage, " ");
|
||||
if (params[i].required)
|
||||
tal_append_fmt(&usage, "%s", params[i].name);
|
||||
else
|
||||
tal_append_fmt(&usage, "[%s]", params[i].name);
|
||||
}
|
||||
return usage;
|
||||
}
|
||||
|
||||
static bool param_arr(struct command *cmd, const char *buffer,
|
||||
const jsmntok_t tokens[],
|
||||
struct param *params,
|
||||
bool allow_extra)
|
||||
{
|
||||
#if DEVELOPER
|
||||
if (!check_params(params)) {
|
||||
command_fail(cmd, PARAM_DEV_ERROR, "developer error: check_params");
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
if (tokens->type == JSMN_ARRAY)
|
||||
return parse_by_position(cmd, params, buffer, tokens, allow_extra);
|
||||
else if (tokens->type == JSMN_OBJECT)
|
||||
return parse_by_name(cmd, params, buffer, tokens, allow_extra);
|
||||
|
||||
command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
||||
"Expected array or object for params");
|
||||
return false;
|
||||
}
|
||||
|
||||
bool param(struct command *cmd, const char *buffer,
|
||||
const jsmntok_t tokens[], ...)
|
||||
{
|
||||
struct param *params = tal_arr(cmd, struct param, 0);
|
||||
const char *name;
|
||||
va_list ap;
|
||||
bool allow_extra = false;
|
||||
|
||||
va_start(ap, tokens);
|
||||
while ((name = va_arg(ap, const char *)) != NULL) {
|
||||
bool required = va_arg(ap, int);
|
||||
param_cbx cbx = va_arg(ap, param_cbx);
|
||||
void *arg = va_arg(ap, void *);
|
||||
if (streq(name, "")) {
|
||||
allow_extra = true;
|
||||
continue;
|
||||
}
|
||||
if (!param_add(¶ms, name, required, cbx, arg)) {
|
||||
command_fail(cmd, PARAM_DEV_ERROR,
|
||||
"developer error: param_add %s", name);
|
||||
va_end(ap);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
va_end(ap);
|
||||
|
||||
if (command_usage_only(cmd)) {
|
||||
command_set_usage(cmd, param_usage(cmd, params));
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Always return false if we're simply checking command parameters;
|
||||
* normally this returns true if all parameters are valid. */
|
||||
return param_arr(cmd, buffer, tokens, params, allow_extra)
|
||||
&& !command_check_only(cmd);
|
||||
}
|
||||
99
common/param.h
Normal file
99
common/param.h
Normal file
@@ -0,0 +1,99 @@
|
||||
#ifndef LIGHTNING_COMMON_PARAM_H
|
||||
#define LIGHTNING_COMMON_PARAM_H
|
||||
#include "config.h"
|
||||
#include <common/json.h>
|
||||
#include <common/json_tok.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
/*~ Greetings adventurer!
|
||||
*
|
||||
* Do you want to automatically validate json input and unmarshall it into
|
||||
* local variables, all using typesafe callbacks? And on error,
|
||||
* call command_fail with a proper error message? Then you've come to the
|
||||
* right place!
|
||||
*
|
||||
* Here is a simple example of using the system:
|
||||
*
|
||||
* unsigned *cltv;
|
||||
* u64 *msatoshi;
|
||||
* const jsmntok_t *note;
|
||||
* u64 *expiry;
|
||||
*
|
||||
* if (!param(cmd, buffer, params,
|
||||
* p_req("cltv", json_tok_number, &cltv),
|
||||
* p_opt("msatoshi", json_tok_u64, &msatoshi),
|
||||
* p_opt("note", json_tok_tok, ¬e),
|
||||
* p_opt_def("expiry", json_tok_u64, &expiry, 3600),
|
||||
* NULL))
|
||||
* return;
|
||||
*
|
||||
* If param() returns true then you're good to go.
|
||||
*
|
||||
* All the command handlers throughout the code use this system.
|
||||
* json_invoice() is a great example. The common callbacks can be found in
|
||||
* common/json_tok.c. Use them directly or feel free to write your own.
|
||||
*/
|
||||
struct command;
|
||||
|
||||
/*
|
||||
* Parse the json tokens. @params can be an array of values or an object
|
||||
* of named values.
|
||||
*/
|
||||
bool param(struct command *cmd, const char *buffer,
|
||||
const jsmntok_t params[], ...);
|
||||
|
||||
/*
|
||||
* The callback signature. Callbacks must return true on success. On failure they
|
||||
* must call comand_fail and return false.
|
||||
*/
|
||||
typedef bool(*param_cbx)(struct command *cmd,
|
||||
const char *name,
|
||||
const char *buffer,
|
||||
const jsmntok_t *tok,
|
||||
void **arg);
|
||||
|
||||
/*
|
||||
* Add a required parameter.
|
||||
*/
|
||||
#define p_req(name, cbx, arg) \
|
||||
name"", \
|
||||
true, \
|
||||
(cbx), \
|
||||
(arg) + 0*sizeof((cbx)((struct command *)NULL, \
|
||||
(const char *)NULL, \
|
||||
(const char *)NULL, \
|
||||
(const jsmntok_t *)NULL, \
|
||||
(arg)) == true)
|
||||
|
||||
/*
|
||||
* Add an optional parameter. *arg is set to NULL if it isn't found.
|
||||
*/
|
||||
#define p_opt(name, cbx, arg) \
|
||||
name"", \
|
||||
false, \
|
||||
(cbx), \
|
||||
({ *arg = NULL; \
|
||||
(arg) + 0*sizeof((cbx)((struct command *)NULL, \
|
||||
(const char *)NULL, \
|
||||
(const char *)NULL, \
|
||||
(const jsmntok_t *)NULL, \
|
||||
(arg)) == true); })
|
||||
|
||||
/*
|
||||
* Add an optional parameter. *arg is set to @def if it isn't found.
|
||||
*/
|
||||
#define p_opt_def(name, cbx, arg, def) \
|
||||
name"", \
|
||||
false, \
|
||||
(cbx), \
|
||||
({ (*arg) = tal((cmd), typeof(**arg)); \
|
||||
(**arg) = (def); \
|
||||
(arg) + 0*sizeof((cbx)((struct command *)NULL, \
|
||||
(const char *)NULL, \
|
||||
(const char *)NULL, \
|
||||
(const jsmntok_t *)NULL, \
|
||||
(arg)) == true); })
|
||||
|
||||
/* Special flag for 'check' which allows any parameters. */
|
||||
#define p_opt_any() "", false, NULL, NULL
|
||||
#endif /* LIGHTNING_COMMON_PARAM_H */
|
||||
46
common/test/run-json_escaped.c
Normal file
46
common/test/run-json_escaped.c
Normal file
@@ -0,0 +1,46 @@
|
||||
#include "../json_escaped.c"
|
||||
#include <assert.h>
|
||||
#include <common/utils.h>
|
||||
#include <stdio.h>
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
int main(void)
|
||||
{
|
||||
setup_locale();
|
||||
|
||||
test_json_partial();
|
||||
assert(!taken_any());
|
||||
take_cleanup();
|
||||
}
|
||||
589
common/test/run-param.c
Normal file
589
common/test/run-param.c
Normal file
@@ -0,0 +1,589 @@
|
||||
#include "config.h"
|
||||
#include "../json.c"
|
||||
#include "../json_escaped.c"
|
||||
#include "../json_tok.c"
|
||||
#include "../param.c"
|
||||
#include <ccan/array_size/array_size.h>
|
||||
#include <ccan/err/err.h>
|
||||
#include <common/json.h>
|
||||
#include <setjmp.h>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
|
||||
char *fail_msg = NULL;
|
||||
bool failed = false;
|
||||
|
||||
static bool check_fail(void) {
|
||||
if (!failed)
|
||||
return false;
|
||||
failed = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
struct command *cmd;
|
||||
|
||||
void command_fail(struct command *cmd, int code, const char *fmt, ...)
|
||||
{
|
||||
failed = true;
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
fail_msg = tal_vfmt(cmd, fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
/* AUTOGENERATED MOCKS START */
|
||||
/* AUTOGENERATED MOCKS END */
|
||||
|
||||
/* We do this lightningd-style: */
|
||||
enum command_mode {
|
||||
CMD_NORMAL,
|
||||
CMD_USAGE,
|
||||
CMD_CHECK
|
||||
};
|
||||
|
||||
struct command {
|
||||
enum command_mode mode;
|
||||
const char *usage;
|
||||
};
|
||||
|
||||
void command_set_usage(struct command *cmd, const char *usage)
|
||||
{
|
||||
cmd->usage = usage;
|
||||
}
|
||||
|
||||
bool command_usage_only(const struct command *cmd)
|
||||
{
|
||||
return cmd->mode == CMD_USAGE;
|
||||
}
|
||||
|
||||
bool command_check_only(const struct command *cmd)
|
||||
{
|
||||
return cmd->mode == CMD_CHECK;
|
||||
}
|
||||
|
||||
struct json {
|
||||
jsmntok_t *toks;
|
||||
char *buffer;
|
||||
};
|
||||
|
||||
static void convert_quotes(char *first)
|
||||
{
|
||||
while (*first != '\0') {
|
||||
if (*first == '\'')
|
||||
*first = '"';
|
||||
first++;
|
||||
}
|
||||
}
|
||||
|
||||
static struct json *json_parse(const tal_t * ctx, const char *str)
|
||||
{
|
||||
struct json *j = tal(ctx, struct json);
|
||||
j->buffer = tal_strdup(j, str);
|
||||
convert_quotes(j->buffer);
|
||||
|
||||
j->toks = tal_arr(j, jsmntok_t, 50);
|
||||
assert(j->toks);
|
||||
jsmn_parser parser;
|
||||
|
||||
again:
|
||||
jsmn_init(&parser);
|
||||
int ret = jsmn_parse(&parser, j->buffer, strlen(j->buffer), j->toks,
|
||||
tal_count(j->toks));
|
||||
if (ret == JSMN_ERROR_NOMEM) {
|
||||
tal_resize(&j->toks, tal_count(j->toks) * 2);
|
||||
goto again;
|
||||
}
|
||||
|
||||
if (ret <= 0) {
|
||||
assert(0);
|
||||
}
|
||||
return j;
|
||||
}
|
||||
|
||||
static void zero_params(void)
|
||||
{
|
||||
struct json *j = json_parse(cmd, "{}");
|
||||
assert(param(cmd, j->buffer, j->toks, NULL));
|
||||
|
||||
j = json_parse(cmd, "[]");
|
||||
assert(param(cmd, j->buffer, j->toks, NULL));
|
||||
}
|
||||
|
||||
struct sanity {
|
||||
char *str;
|
||||
bool failed;
|
||||
int ival;
|
||||
double dval;
|
||||
char *fail_str;
|
||||
};
|
||||
|
||||
struct sanity buffers[] = {
|
||||
// pass
|
||||
{"['42', '3.15']", false, 42, 3.15, NULL},
|
||||
{"{ 'u64' : '42', 'double' : '3.15' }", false, 42, 3.15, NULL},
|
||||
|
||||
// fail
|
||||
{"{'u64':'42', 'double':'3.15', 'extra':'stuff'}", true, 0, 0,
|
||||
"unknown parameter"},
|
||||
{"['42', '3.15', 'stuff']", true, 0, 0, "too many"},
|
||||
{"['42', '3.15', 'null']", true, 0, 0, "too many"},
|
||||
|
||||
// not enough
|
||||
{"{'u64':'42'}", true, 0, 0, "missing required"},
|
||||
{"['42']", true, 0, 0, "missing required"},
|
||||
|
||||
// fail wrong type
|
||||
{"{'u64':'hello', 'double':'3.15'}", true, 0, 0, "be an unsigned 64"},
|
||||
{"['3.15', '3.15', 'stuff']", true, 0, 0, "integer"},
|
||||
};
|
||||
|
||||
static void stest(const struct json *j, struct sanity *b)
|
||||
{
|
||||
u64 *ival;
|
||||
double *dval;
|
||||
if (!param(cmd, j->buffer, j->toks,
|
||||
p_req("u64", json_tok_u64, &ival),
|
||||
p_req("double", json_tok_double, &dval), NULL)) {
|
||||
assert(check_fail());
|
||||
assert(b->failed == true);
|
||||
if (!strstr(fail_msg, b->fail_str)) {
|
||||
printf("%s != %s\n", fail_msg, b->fail_str);
|
||||
assert(false);
|
||||
}
|
||||
} else {
|
||||
assert(!check_fail());
|
||||
assert(b->failed == false);
|
||||
assert(*ival == 42);
|
||||
assert(*dval > 3.1499 && b->dval < 3.1501);
|
||||
}
|
||||
}
|
||||
|
||||
static void sanity(void)
|
||||
{
|
||||
for (int i = 0; i < ARRAY_SIZE(buffers); ++i) {
|
||||
struct json *j = json_parse(cmd, buffers[i].str);
|
||||
assert(j->toks->type == JSMN_OBJECT
|
||||
|| j->toks->type == JSMN_ARRAY);
|
||||
stest(j, &buffers[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Make sure toks are passed through correctly, and also make sure
|
||||
* optional missing toks are set to NULL.
|
||||
*/
|
||||
static void tok_tok(void)
|
||||
{
|
||||
{
|
||||
unsigned int n;
|
||||
const jsmntok_t *tok = NULL;
|
||||
struct json *j = json_parse(cmd, "{ 'satoshi', '546' }");
|
||||
|
||||
assert(param(cmd, j->buffer, j->toks,
|
||||
p_req("satoshi", json_tok_tok, &tok), NULL));
|
||||
assert(tok);
|
||||
assert(json_to_number(j->buffer, tok, &n));
|
||||
assert(n == 546);
|
||||
}
|
||||
// again with missing optional parameter
|
||||
{
|
||||
/* make sure it is *not* NULL */
|
||||
const jsmntok_t *tok = (const jsmntok_t *) 65535;
|
||||
|
||||
struct json *j = json_parse(cmd, "{}");
|
||||
assert(param(cmd, j->buffer, j->toks,
|
||||
p_opt("satoshi", json_tok_tok, &tok), NULL));
|
||||
|
||||
/* make sure it *is* NULL */
|
||||
assert(tok == NULL);
|
||||
}
|
||||
}
|
||||
|
||||
/* check for valid but duplicate json name-value pairs */
|
||||
static void dup_names(void)
|
||||
{
|
||||
struct json *j =
|
||||
json_parse(cmd,
|
||||
"{ 'u64' : '42', 'u64' : '43', 'double' : '3.15' }");
|
||||
|
||||
u64 *i;
|
||||
double *d;
|
||||
assert(!param(cmd, j->buffer, j->toks,
|
||||
p_req("u64", json_tok_u64, &i),
|
||||
p_req("double", json_tok_double, &d), NULL));
|
||||
}
|
||||
|
||||
static void null_params(void)
|
||||
{
|
||||
uint64_t **intptrs = tal_arr(cmd, uint64_t *, 7);
|
||||
/* no null params */
|
||||
struct json *j =
|
||||
json_parse(cmd, "[ '10', '11', '12', '13', '14', '15', '16']");
|
||||
|
||||
assert(param(cmd, j->buffer, j->toks,
|
||||
p_req("0", json_tok_u64, &intptrs[0]),
|
||||
p_req("1", json_tok_u64, &intptrs[1]),
|
||||
p_req("2", json_tok_u64, &intptrs[2]),
|
||||
p_req("3", json_tok_u64, &intptrs[3]),
|
||||
p_opt_def("4", json_tok_u64, &intptrs[4], 999),
|
||||
p_opt("5", json_tok_u64, &intptrs[5]),
|
||||
p_opt("6", json_tok_u64, &intptrs[6]),
|
||||
NULL));
|
||||
for (int i = 0; i < tal_count(intptrs); ++i) {
|
||||
assert(intptrs[i]);
|
||||
assert(*intptrs[i] == i + 10);
|
||||
}
|
||||
|
||||
/* missing at end */
|
||||
j = json_parse(cmd, "[ '10', '11', '12', '13', '14']");
|
||||
assert(param(cmd, j->buffer, j->toks,
|
||||
p_req("0", json_tok_u64, &intptrs[0]),
|
||||
p_req("1", json_tok_u64, &intptrs[1]),
|
||||
p_req("2", json_tok_u64, &intptrs[2]),
|
||||
p_req("3", json_tok_u64, &intptrs[3]),
|
||||
p_opt("4", json_tok_u64, &intptrs[4]),
|
||||
p_opt("5", json_tok_u64, &intptrs[5]),
|
||||
p_opt_def("6", json_tok_u64, &intptrs[6], 888),
|
||||
NULL));
|
||||
assert(*intptrs[0] == 10);
|
||||
assert(*intptrs[1] == 11);
|
||||
assert(*intptrs[2] == 12);
|
||||
assert(*intptrs[3] == 13);
|
||||
assert(*intptrs[4] == 14);
|
||||
assert(!intptrs[5]);
|
||||
assert(*intptrs[6] == 888);
|
||||
}
|
||||
|
||||
static void no_params(void)
|
||||
{
|
||||
struct json *j = json_parse(cmd, "[]");
|
||||
assert(param(cmd, j->buffer, j->toks, NULL));
|
||||
|
||||
j = json_parse(cmd, "[ 'unexpected' ]");
|
||||
assert(!param(cmd, j->buffer, j->toks, NULL));
|
||||
}
|
||||
|
||||
|
||||
#if DEVELOPER
|
||||
/*
|
||||
* Check to make sure there are no programming mistakes.
|
||||
*/
|
||||
static void bad_programmer(void)
|
||||
{
|
||||
u64 *ival;
|
||||
u64 *ival2;
|
||||
double *dval;
|
||||
struct json *j = json_parse(cmd, "[ '25', '546', '26' ]");
|
||||
|
||||
/* check for repeated names */
|
||||
assert(!param(cmd, j->buffer, j->toks,
|
||||
p_req("repeat", json_tok_u64, &ival),
|
||||
p_req("double", json_tok_double, &dval),
|
||||
p_req("repeat", json_tok_u64, &ival2), NULL));
|
||||
assert(check_fail());
|
||||
assert(strstr(fail_msg, "developer error"));
|
||||
|
||||
assert(!param(cmd, j->buffer, j->toks,
|
||||
p_req("repeat", json_tok_u64, &ival),
|
||||
p_req("double", json_tok_double, &dval),
|
||||
p_req("repeat", json_tok_u64, &ival), NULL));
|
||||
assert(check_fail());
|
||||
assert(strstr(fail_msg, "developer error"));
|
||||
|
||||
assert(!param(cmd, j->buffer, j->toks,
|
||||
p_req("u64", json_tok_u64, &ival),
|
||||
p_req("repeat", json_tok_double, &dval),
|
||||
p_req("repeat", json_tok_double, &dval), NULL));
|
||||
assert(check_fail());
|
||||
assert(strstr(fail_msg, "developer error"));
|
||||
|
||||
/* check for repeated arguments */
|
||||
assert(!param(cmd, j->buffer, j->toks,
|
||||
p_req("u64", json_tok_u64, &ival),
|
||||
p_req("repeated-arg", json_tok_u64, &ival), NULL));
|
||||
assert(check_fail());
|
||||
assert(strstr(fail_msg, "developer error"));
|
||||
|
||||
assert(!param(cmd, j->buffer, j->toks,
|
||||
p_req("u64", (param_cbx) NULL, NULL), NULL));
|
||||
assert(check_fail());
|
||||
assert(strstr(fail_msg, "developer error"));
|
||||
|
||||
/* Add required param after optional */
|
||||
j = json_parse(cmd, "[ '25', '546', '26', '1.1' ]");
|
||||
unsigned int *msatoshi;
|
||||
double *riskfactor;
|
||||
assert(!param(cmd, j->buffer, j->toks,
|
||||
p_req("u64", json_tok_u64, &ival),
|
||||
p_req("double", json_tok_double, &dval),
|
||||
p_opt_def("msatoshi", json_tok_number, &msatoshi, 100),
|
||||
p_req("riskfactor", json_tok_double, &riskfactor), NULL));
|
||||
assert(*msatoshi);
|
||||
assert(*msatoshi == 100);
|
||||
assert(check_fail());
|
||||
assert(strstr(fail_msg, "developer error"));
|
||||
}
|
||||
#endif
|
||||
|
||||
static void add_members(struct param **params,
|
||||
char **obj,
|
||||
char **arr, unsigned int **ints)
|
||||
{
|
||||
for (int i = 0; i < tal_count(ints); ++i) {
|
||||
const char *name = tal_fmt(*params, "%i", i);
|
||||
if (i != 0) {
|
||||
tal_append_fmt(obj, ", ");
|
||||
tal_append_fmt(arr, ", ");
|
||||
}
|
||||
tal_append_fmt(obj, "\"%i\" : %i", i, i);
|
||||
tal_append_fmt(arr, "%i", i);
|
||||
param_add(params, name, true,
|
||||
typesafe_cb_preargs(bool, void **,
|
||||
json_tok_number,
|
||||
&ints[i],
|
||||
struct command *,
|
||||
const char *,
|
||||
const char *,
|
||||
const jsmntok_t *),
|
||||
&ints[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* A roundabout way of initializing an array of ints to:
|
||||
* ints[0] = 0, ints[1] = 1, ... ints[499] = 499
|
||||
*/
|
||||
static void five_hundred_params(void)
|
||||
{
|
||||
struct param *params = tal_arr(NULL, struct param, 0);
|
||||
|
||||
unsigned int **ints = tal_arr(params, unsigned int*, 500);
|
||||
char *obj = tal_fmt(params, "{ ");
|
||||
char *arr = tal_fmt(params, "[ ");
|
||||
add_members(¶ms, &obj, &arr, ints);
|
||||
tal_append_fmt(&obj, "}");
|
||||
tal_append_fmt(&arr, "]");
|
||||
|
||||
/* first test object version */
|
||||
struct json *j = json_parse(params, obj);
|
||||
assert(param_arr(cmd, j->buffer, j->toks, params, false));
|
||||
for (int i = 0; i < tal_count(ints); ++i) {
|
||||
assert(ints[i]);
|
||||
assert(*ints[i] == i);
|
||||
*ints[i] = 65535;
|
||||
}
|
||||
|
||||
/* now test array */
|
||||
j = json_parse(params, arr);
|
||||
assert(param_arr(cmd, j->buffer, j->toks, params, false));
|
||||
for (int i = 0; i < tal_count(ints); ++i) {
|
||||
assert(*ints[i] == i);
|
||||
}
|
||||
|
||||
tal_free(params);
|
||||
}
|
||||
|
||||
static void sendpay(void)
|
||||
{
|
||||
struct json *j = json_parse(cmd, "[ 'A', '123', 'hello there' '547']");
|
||||
|
||||
const jsmntok_t *routetok, *note;
|
||||
u64 *msatoshi;
|
||||
unsigned *cltv;
|
||||
|
||||
if (!param(cmd, j->buffer, j->toks,
|
||||
p_req("route", json_tok_tok, &routetok),
|
||||
p_req("cltv", json_tok_number, &cltv),
|
||||
p_opt("note", json_tok_tok, ¬e),
|
||||
p_opt("msatoshi", json_tok_u64, &msatoshi),
|
||||
NULL))
|
||||
assert(false);
|
||||
|
||||
assert(note);
|
||||
assert(!strncmp("hello there", j->buffer + note->start,
|
||||
note->end - note->start));
|
||||
assert(msatoshi);
|
||||
assert(*msatoshi == 547);
|
||||
}
|
||||
|
||||
static void sendpay_nulltok(void)
|
||||
{
|
||||
struct json *j = json_parse(cmd, "[ 'A', '123']");
|
||||
|
||||
const jsmntok_t *routetok, *note = (void *) 65535;
|
||||
u64 *msatoshi;
|
||||
unsigned *cltv;
|
||||
|
||||
if (!param(cmd, j->buffer, j->toks,
|
||||
p_req("route", json_tok_tok, &routetok),
|
||||
p_req("cltv", json_tok_number, &cltv),
|
||||
p_opt("note", json_tok_tok, ¬e),
|
||||
p_opt("msatoshi", json_tok_u64, &msatoshi),
|
||||
NULL))
|
||||
assert(false);
|
||||
|
||||
assert(note == NULL);
|
||||
assert(msatoshi == NULL);
|
||||
}
|
||||
|
||||
static void advanced(void)
|
||||
{
|
||||
{
|
||||
struct json *j = json_parse(cmd, "[ 'lightning', 24, 'tok', 543 ]");
|
||||
|
||||
struct json_escaped *label;
|
||||
u64 *msat;
|
||||
u64 *msat_opt1, *msat_opt2;
|
||||
const jsmntok_t *tok;
|
||||
|
||||
assert(param(cmd, j->buffer, j->toks,
|
||||
p_req("description", json_tok_label, &label),
|
||||
p_req("msat", json_tok_msat, &msat),
|
||||
p_req("tok", json_tok_tok, &tok),
|
||||
p_opt("msat_opt1", json_tok_msat, &msat_opt1),
|
||||
p_opt("msat_opt2", json_tok_msat, &msat_opt2),
|
||||
NULL));
|
||||
assert(label != NULL);
|
||||
assert(streq(label->s, "lightning"));
|
||||
assert(*msat == 24);
|
||||
assert(tok);
|
||||
assert(msat_opt1);
|
||||
assert(*msat_opt1 == 543);
|
||||
assert(msat_opt2 == NULL);
|
||||
}
|
||||
{
|
||||
struct json *j = json_parse(cmd, "[ 3 'foo' ]");
|
||||
struct json_escaped *label, *foo;
|
||||
assert(param(cmd, j->buffer, j->toks,
|
||||
p_req("label", json_tok_label, &label),
|
||||
p_opt("foo", json_tok_label, &foo),
|
||||
NULL));
|
||||
assert(streq(label->s, "3"));
|
||||
assert(streq(foo->s, "foo"));
|
||||
}
|
||||
{
|
||||
u64 *msat;
|
||||
u64 *msat2;
|
||||
struct json *j = json_parse(cmd, "[ 3 ]");
|
||||
assert(param(cmd, j->buffer, j->toks,
|
||||
p_opt_def("msat", json_tok_msat, &msat, 23),
|
||||
p_opt_def("msat2", json_tok_msat, &msat2, 53),
|
||||
NULL));
|
||||
assert(*msat == 3);
|
||||
assert(msat2);
|
||||
assert(*msat2 == 53);
|
||||
}
|
||||
}
|
||||
|
||||
static void advanced_fail(void)
|
||||
{
|
||||
{
|
||||
struct json *j = json_parse(cmd, "[ 'anyx' ]");
|
||||
u64 *msat;
|
||||
assert(!param(cmd, j->buffer, j->toks,
|
||||
p_req("msat", json_tok_msat, &msat),
|
||||
NULL));
|
||||
assert(check_fail());
|
||||
assert(strstr(fail_msg, "'msat' should be a positive"
|
||||
" number or 'any', not 'anyx'"));
|
||||
}
|
||||
}
|
||||
|
||||
#define test_cb(cb, T, json_, value, pass) \
|
||||
{ \
|
||||
struct json *j = json_parse(cmd, json_); \
|
||||
T *v; \
|
||||
bool ret = cb(cmd, "name", j->buffer, j->toks + 1, &v); \
|
||||
assert(ret == pass); \
|
||||
if (ret) { \
|
||||
assert(v); \
|
||||
assert(*v == value); \
|
||||
} \
|
||||
}
|
||||
|
||||
static void json_tok_tests(void)
|
||||
{
|
||||
test_cb(json_tok_bool, bool, "[ true ]", true, true);
|
||||
test_cb(json_tok_bool, bool, "[ false ]", false, true);
|
||||
test_cb(json_tok_bool, bool, "[ tru ]", false, false);
|
||||
test_cb(json_tok_bool, bool, "[ 1 ]", false, false);
|
||||
|
||||
test_cb(json_tok_percent, double, "[ -0.01 ]", 0, false);
|
||||
test_cb(json_tok_percent, double, "[ 0.00 ]", 0, true);
|
||||
test_cb(json_tok_percent, double, "[ 1 ]", 1, true);
|
||||
test_cb(json_tok_percent, double, "[ 1.1 ]", 1.1, true);
|
||||
test_cb(json_tok_percent, double, "[ 1.01 ]", 1.01, true);
|
||||
test_cb(json_tok_percent, double, "[ 99.99 ]", 99.99, true);
|
||||
test_cb(json_tok_percent, double, "[ 100.0 ]", 100, true);
|
||||
test_cb(json_tok_percent, double, "[ 100.001 ]", 100.001, true);
|
||||
test_cb(json_tok_percent, double, "[ 1000 ]", 1000, true);
|
||||
test_cb(json_tok_percent, double, "[ 'wow' ]", 0, false);
|
||||
}
|
||||
|
||||
static void test_invoice(struct command *cmd,
|
||||
const char *buffer,
|
||||
const jsmntok_t *obj UNNEEDED,
|
||||
const jsmntok_t *params)
|
||||
{
|
||||
u64 *msatoshi_val;
|
||||
struct json_escaped *label_val;
|
||||
const char *desc_val;
|
||||
u64 *expiry;
|
||||
const jsmntok_t *fallbacks;
|
||||
const jsmntok_t *preimagetok;
|
||||
|
||||
assert(cmd->mode == CMD_USAGE);
|
||||
if(!param(cmd, buffer, params,
|
||||
p_req("msatoshi", json_tok_msat, &msatoshi_val),
|
||||
p_req("label", json_tok_label, &label_val),
|
||||
p_req("description", json_tok_escaped_string, &desc_val),
|
||||
p_opt("expiry", json_tok_u64, &expiry),
|
||||
p_opt("fallbacks", json_tok_array, &fallbacks),
|
||||
p_opt("preimage", json_tok_tok, &preimagetok), NULL))
|
||||
return;
|
||||
|
||||
/* should not be here since we are in the mode of CMD_USAGE
|
||||
* and it always returns false. */
|
||||
abort();
|
||||
}
|
||||
|
||||
static void usage(void)
|
||||
{
|
||||
cmd->mode = CMD_USAGE;
|
||||
|
||||
test_invoice(cmd, NULL, NULL, NULL);
|
||||
assert(streq(cmd->usage,
|
||||
"msatoshi label description "
|
||||
"[expiry] [fallbacks] [preimage]"));
|
||||
|
||||
cmd->mode = CMD_NORMAL;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
setup_locale();
|
||||
setup_tmpctx();
|
||||
cmd = tal(tmpctx, struct command);
|
||||
cmd->mode = CMD_NORMAL;
|
||||
fail_msg = tal_arr(cmd, char, 10000);
|
||||
|
||||
zero_params();
|
||||
sanity();
|
||||
tok_tok();
|
||||
null_params();
|
||||
no_params();
|
||||
#if DEVELOPER
|
||||
bad_programmer();
|
||||
#endif
|
||||
dup_names();
|
||||
five_hundred_params();
|
||||
sendpay();
|
||||
sendpay_nulltok();
|
||||
advanced();
|
||||
advanced_fail();
|
||||
json_tok_tests();
|
||||
usage();
|
||||
|
||||
tal_free(tmpctx);
|
||||
printf("run-params ok\n");
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <common/json_command.h>
|
||||
#include <common/jsonrpc_errors.h>
|
||||
#include <common/wallet_tx.h>
|
||||
#include <inttypes.h>
|
||||
#include <lightningd/jsonrpc_errors.h>
|
||||
#include <wallet/wallet.h>
|
||||
|
||||
void wtx_init(struct command *cmd, struct wallet_tx * wtx)
|
||||
|
||||
Reference in New Issue
Block a user