From 0f5036d866ff08500e7458455b228c8d65386e06 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 13 Aug 2019 13:30:22 +0930 Subject: [PATCH] cli: remove formatting hint correclty when it's not the last element. I noticed that the 'lightning-cli summary' output was wrong. Signed-off-by: Rusty Russell --- cli/lightning-cli.c | 2 +- cli/test/run-remove-hint.c | 114 +++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 cli/test/run-remove-hint.c diff --git a/cli/lightning-cli.c b/cli/lightning-cli.c index 31ffabed6..bb120f1b2 100644 --- a/cli/lightning-cli.c +++ b/cli/lightning-cli.c @@ -408,7 +408,7 @@ static enum format delete_format_hint(const char *resp, format = HUMAN; /* Don't let hint appear in the output! */ - json_tok_remove(toks, result, hint, 1); + json_tok_remove(toks, result, hint-1, 1); return format; } diff --git a/cli/test/run-remove-hint.c b/cli/test/run-remove-hint.c new file mode 100644 index 000000000..2f8bea63d --- /dev/null +++ b/cli/test/run-remove-hint.c @@ -0,0 +1,114 @@ +#include "config.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int test_main(int argc, char *argv[]); +ssize_t test_read(int fd, void *buf, size_t len); +int test_socket(int domain, int type, int protocol); +int test_connect(int sockfd, const struct sockaddr *addr, + socklen_t addrlen); +int test_getpid(void); +int test_printf(const char *format, ...); +int test_fputc(int c, FILE *stream); + +#define main test_main +#define read test_read +#define socket test_socket +#define connect test_connect +#define getpid test_getpid +#define printf test_printf +#define fputc test_fputc + + #include "../lightning-cli.c" +#undef main + +/* AUTOGENERATED MOCKS START */ +/* Generated stub for bigsize_get */ +size_t bigsize_get(const u8 *p UNNEEDED, size_t max UNNEEDED, bigsize_t *val UNNEEDED) +{ fprintf(stderr, "bigsize_get called!\n"); abort(); } +/* Generated stub for bigsize_put */ +size_t bigsize_put(u8 buf[BIGSIZE_MAX_LEN] UNNEEDED, bigsize_t v UNNEEDED) +{ fprintf(stderr, "bigsize_put called!\n"); abort(); } +/* Generated stub for version_and_exit */ +char *version_and_exit(const void *unused UNNEEDED) +{ fprintf(stderr, "version_and_exit called!\n"); abort(); } +/* AUTOGENERATED MOCKS END */ + +int test_socket(int domain UNUSED, int type UNUSED, int protocol UNUSED) +{ + /* We give a real fd, as it writes to it */ + return open("/dev/null", O_WRONLY); +} + +int test_connect(int sockfd UNUSED, const struct sockaddr *addr UNUSED, + socklen_t addrlen UNUSED) +{ + return 0; +} + +int test_getpid(void) +{ + return 9999; +} + +static char *response = "{\"id\": \"lightning-cli-9999\", \"jsonrpc\": \"2.0\", \"result\": {\"channels\": [\"\n\", \"├477308sat OUT/OURS ┼ IN/THEIRS 477308sat┤ SCID FLAG ALIAS\", \"├──────────────────────┼──────────────────────┤ 580612x1826x0 [__] BLUEIRON-v0.7.2rc1\"], \"my_address\": \"02b78caed0f45120acc48efe867aa506e8ea60f0712a23303178471da0ca2213f5@hdco6sxkbisc7s5t.onion\", \"format-hint\": \"simple\", \"avail_in\": \"0.00477308500btc (USD $54.37)\", \"num_utxos\": 0, \"num_gossipers\": 1, \"channels_flags\": \"P:private O:offline\", \"avail_out\": \"0.00477308500btc (USD $54.37)\", \"utxo_amount\": \"0.00000000btc (USD $0.00)\", \"num_channels\": 1, \"num_connected\": 1}}\n\n"; +static size_t response_off; + +ssize_t test_read(int fd UNUSED, void *buf, size_t len) +{ + if (len > strlen(response + response_off)) + len = strlen(response + response_off); + memcpy(buf, response + response_off, len); + return len; +} + +static char *output; + +int test_printf(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + tal_append_vfmt(&output, fmt, ap); + va_end(ap); + return 1; +} + +int test_fputc(int c, FILE *stream) +{ + tal_append_fmt(&output, "%c", c); + return (unsigned)c; +} + +int main(int argc UNUSED, char *argv[]) +{ + setup_locale(); + + char *fake_argv[] = { argv[0], "--lightning-dir=/tmp/", "test", NULL }; + + output = tal_strdup(NULL, ""); + assert(test_main(3, fake_argv) == 0); + + assert(streq(output, "channels=\n" + "\n" + "├477308sat OUT/OURS ┼ IN/THEIRS 477308sat┤ SCID FLAG ALIAS\n" + "├──────────────────────┼──────────────────────┤ 580612x1826x0 [__] BLUEIRON-v0.7.2rc1\n" + "my_address=02b78caed0f45120acc48efe867aa506e8ea60f0712a23303178471da0ca2213f5@hdco6sxkbisc7s5t.onion\n" + "avail_in=0.00477308500btc (USD $54.37)\n" + "num_utxos=0\n" + "num_gossipers=1\n" + "channels_flags=P:private O:offline\n" + "avail_out=0.00477308500btc (USD $54.37)\n" + "utxo_amount=0.00000000btc (USD $0.00)\n" + "num_channels=1\n" + "num_connected=1\n")); + tal_free(output); + return 0; +}