1 #include "config.h"
2 #include <common/amount.h>
3 #include <common/bigsize.h>
4 #include <common/channel_id.h>
5 #include <common/json_stream.h>
6 #include <common/node_id.h>
7 #include <fcntl.h>
8 #include <stdio.h>
9 #include <sys/socket.h>
10
11 int test_main(int argc, char *argv[]);
12 ssize_t test_read(int fd, void *buf, size_t len);
13 int test_socket(int domain, int type, int protocol);
14 int test_connect(int sockfd, const struct sockaddr *addr,
15 socklen_t addrlen);
16 int test_getpid(void);
17 int test_printf(const char *format, ...);
18 int test_fputc(int c, FILE *stream);
19 int test_chdir(const char *path);
20
21 #define main test_main
22 #define read test_read
23 #define socket test_socket
24 #define connect test_connect
25 #define getpid test_getpid
26 #define printf test_printf
27 #define fputc test_fputc
28 #define chdir test_chdir
29
30 #include "../lightning-cli.c"
31 #undef main
32
33 /* AUTOGENERATED MOCKS START */
34 /* Generated stub for amount_asset_is_main */
amount_asset_is_main(struct amount_asset * asset UNNEEDED)35 bool amount_asset_is_main(struct amount_asset *asset UNNEEDED)
36 { fprintf(stderr, "amount_asset_is_main called!\n"); abort(); }
37 /* Generated stub for amount_asset_to_sat */
amount_asset_to_sat(struct amount_asset * asset UNNEEDED)38 struct amount_sat amount_asset_to_sat(struct amount_asset *asset UNNEEDED)
39 { fprintf(stderr, "amount_asset_to_sat called!\n"); abort(); }
40 /* Generated stub for amount_sat */
amount_sat(u64 satoshis UNNEEDED)41 struct amount_sat amount_sat(u64 satoshis UNNEEDED)
42 { fprintf(stderr, "amount_sat called!\n"); abort(); }
43 /* Generated stub for amount_sat_add */
amount_sat_add(struct amount_sat * val UNNEEDED,struct amount_sat a UNNEEDED,struct amount_sat b UNNEEDED)44 bool amount_sat_add(struct amount_sat *val UNNEEDED,
45 struct amount_sat a UNNEEDED,
46 struct amount_sat b UNNEEDED)
47 { fprintf(stderr, "amount_sat_add called!\n"); abort(); }
48 /* Generated stub for amount_sat_eq */
amount_sat_eq(struct amount_sat a UNNEEDED,struct amount_sat b UNNEEDED)49 bool amount_sat_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED)
50 { fprintf(stderr, "amount_sat_eq called!\n"); abort(); }
51 /* Generated stub for amount_sat_greater_eq */
amount_sat_greater_eq(struct amount_sat a UNNEEDED,struct amount_sat b UNNEEDED)52 bool amount_sat_greater_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED)
53 { fprintf(stderr, "amount_sat_greater_eq called!\n"); abort(); }
54 /* Generated stub for amount_sat_sub */
amount_sat_sub(struct amount_sat * val UNNEEDED,struct amount_sat a UNNEEDED,struct amount_sat b UNNEEDED)55 bool amount_sat_sub(struct amount_sat *val UNNEEDED,
56 struct amount_sat a UNNEEDED,
57 struct amount_sat b UNNEEDED)
58 { fprintf(stderr, "amount_sat_sub called!\n"); abort(); }
59 /* Generated stub for amount_sat_to_asset */
amount_sat_to_asset(struct amount_sat * sat UNNEEDED,const u8 * asset UNNEEDED)60 struct amount_asset amount_sat_to_asset(struct amount_sat *sat UNNEEDED, const u8 *asset UNNEEDED)
61 { fprintf(stderr, "amount_sat_to_asset called!\n"); abort(); }
62 /* Generated stub for amount_tx_fee */
amount_tx_fee(u32 fee_per_kw UNNEEDED,size_t weight UNNEEDED)63 struct amount_sat amount_tx_fee(u32 fee_per_kw UNNEEDED, size_t weight UNNEEDED)
64 { fprintf(stderr, "amount_tx_fee called!\n"); abort(); }
65 /* Generated stub for fromwire_amount_msat */
fromwire_amount_msat(const u8 ** cursor UNNEEDED,size_t * max UNNEEDED)66 struct amount_msat fromwire_amount_msat(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
67 { fprintf(stderr, "fromwire_amount_msat called!\n"); abort(); }
68 /* Generated stub for fromwire_amount_sat */
fromwire_amount_sat(const u8 ** cursor UNNEEDED,size_t * max UNNEEDED)69 struct amount_sat fromwire_amount_sat(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
70 { fprintf(stderr, "fromwire_amount_sat called!\n"); abort(); }
71 /* Generated stub for fromwire_bigsize */
fromwire_bigsize(const u8 ** cursor UNNEEDED,size_t * max UNNEEDED)72 bigsize_t fromwire_bigsize(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
73 { fprintf(stderr, "fromwire_bigsize called!\n"); abort(); }
74 /* Generated stub for fromwire_channel_id */
fromwire_channel_id(const u8 ** cursor UNNEEDED,size_t * max UNNEEDED,struct channel_id * channel_id UNNEEDED)75 void fromwire_channel_id(const u8 **cursor UNNEEDED, size_t *max UNNEEDED,
76 struct channel_id *channel_id UNNEEDED)
77 { fprintf(stderr, "fromwire_channel_id called!\n"); abort(); }
78 /* Generated stub for fromwire_node_id */
fromwire_node_id(const u8 ** cursor UNNEEDED,size_t * max UNNEEDED,struct node_id * id UNNEEDED)79 void fromwire_node_id(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct node_id *id UNNEEDED)
80 { fprintf(stderr, "fromwire_node_id called!\n"); abort(); }
81 /* Generated stub for json_add_member */
json_add_member(struct json_stream * js UNNEEDED,const char * fieldname UNNEEDED,bool quote UNNEEDED,const char * fmt UNNEEDED,...)82 void json_add_member(struct json_stream *js UNNEEDED,
83 const char *fieldname UNNEEDED,
84 bool quote UNNEEDED,
85 const char *fmt UNNEEDED, ...)
86 { fprintf(stderr, "json_add_member called!\n"); abort(); }
87 /* Generated stub for json_member_direct */
json_member_direct(struct json_stream * js UNNEEDED,const char * fieldname UNNEEDED,size_t extra UNNEEDED)88 char *json_member_direct(struct json_stream *js UNNEEDED,
89 const char *fieldname UNNEEDED, size_t extra UNNEEDED)
90 { fprintf(stderr, "json_member_direct called!\n"); abort(); }
91 /* Generated stub for log_level_name */
log_level_name(enum log_level level UNNEEDED)92 const char *log_level_name(enum log_level level UNNEEDED)
93 { fprintf(stderr, "log_level_name called!\n"); abort(); }
94 /* Generated stub for log_level_parse */
log_level_parse(const char * levelstr UNNEEDED,size_t len UNNEEDED,enum log_level * level UNNEEDED)95 bool log_level_parse(const char *levelstr UNNEEDED, size_t len UNNEEDED,
96 enum log_level *level UNNEEDED)
97 { fprintf(stderr, "log_level_parse called!\n"); abort(); }
98 /* Generated stub for towire_amount_msat */
towire_amount_msat(u8 ** pptr UNNEEDED,const struct amount_msat msat UNNEEDED)99 void towire_amount_msat(u8 **pptr UNNEEDED, const struct amount_msat msat UNNEEDED)
100 { fprintf(stderr, "towire_amount_msat called!\n"); abort(); }
101 /* Generated stub for towire_amount_sat */
towire_amount_sat(u8 ** pptr UNNEEDED,const struct amount_sat sat UNNEEDED)102 void towire_amount_sat(u8 **pptr UNNEEDED, const struct amount_sat sat UNNEEDED)
103 { fprintf(stderr, "towire_amount_sat called!\n"); abort(); }
104 /* Generated stub for towire_bigsize */
towire_bigsize(u8 ** pptr UNNEEDED,const bigsize_t val UNNEEDED)105 void towire_bigsize(u8 **pptr UNNEEDED, const bigsize_t val UNNEEDED)
106 { fprintf(stderr, "towire_bigsize called!\n"); abort(); }
107 /* Generated stub for towire_channel_id */
towire_channel_id(u8 ** pptr UNNEEDED,const struct channel_id * channel_id UNNEEDED)108 void towire_channel_id(u8 **pptr UNNEEDED, const struct channel_id *channel_id UNNEEDED)
109 { fprintf(stderr, "towire_channel_id called!\n"); abort(); }
110 /* Generated stub for towire_node_id */
towire_node_id(u8 ** pptr UNNEEDED,const struct node_id * id UNNEEDED)111 void towire_node_id(u8 **pptr UNNEEDED, const struct node_id *id UNNEEDED)
112 { fprintf(stderr, "towire_node_id called!\n"); abort(); }
113 /* Generated stub for version_and_exit */
version_and_exit(const void * unused UNNEEDED)114 char *version_and_exit(const void *unused UNNEEDED)
115 { fprintf(stderr, "version_and_exit called!\n"); abort(); }
116 /* AUTOGENERATED MOCKS END */
117
test_socket(int domain UNUSED,int type UNUSED,int protocol UNUSED)118 int test_socket(int domain UNUSED, int type UNUSED, int protocol UNUSED)
119 {
120 /* We give a real fd, as it writes to it */
121 return open("/dev/null", O_WRONLY);
122 }
123
test_connect(int sockfd UNUSED,const struct sockaddr * addr UNUSED,socklen_t addrlen UNUSED)124 int test_connect(int sockfd UNUSED, const struct sockaddr *addr UNUSED,
125 socklen_t addrlen UNUSED)
126 {
127 return 0;
128 }
129
test_getpid(void)130 int test_getpid(void)
131 {
132 return 9999;
133 }
134
135 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";
136 static size_t response_off;
137
test_read(int fd UNUSED,void * buf,size_t len)138 ssize_t test_read(int fd UNUSED, void *buf, size_t len)
139 {
140 if (len > strlen(response + response_off))
141 len = strlen(response + response_off);
142 memcpy(buf, response + response_off, len);
143 return len;
144 }
145
146 static char *output;
147
test_printf(const char * fmt,...)148 int test_printf(const char *fmt, ...)
149 {
150 va_list ap;
151
152 va_start(ap, fmt);
153 tal_append_vfmt(&output, fmt, ap);
154 va_end(ap);
155 return 1;
156 }
157
test_fputc(int c,FILE * stream)158 int test_fputc(int c, FILE *stream)
159 {
160 tal_append_fmt(&output, "%c", c);
161 return (unsigned)c;
162 }
163
test_chdir(const char * path)164 int test_chdir(const char *path)
165 {
166 return 0;
167 }
168
main(int argc UNUSED,char * argv[])169 int main(int argc UNUSED, char *argv[])
170 {
171 setup_locale();
172
173 char *fake_argv[] = { argv[0], "--lightning-dir=/tmp/", "test", "-N", "none", NULL };
174
175 output = tal_strdup(NULL, "");
176 assert(test_main(5, fake_argv) == 0);
177
178 assert(streq(output, "channels=\n"
179 "\n"
180 "├477308sat OUT/OURS ┼ IN/THEIRS 477308sat┤ SCID FLAG ALIAS\n"
181 "├──────────────────────┼──────────────────────┤ 580612x1826x0 [__] BLUEIRON-v0.7.2rc1\n"
182 "my_address=02b78caed0f45120acc48efe867aa506e8ea60f0712a23303178471da0ca2213f5@hdco6sxkbisc7s5t.onion\n"
183 "avail_in=0.00477308500btc (USD $54.37)\n"
184 "num_utxos=0\n"
185 "num_gossipers=1\n"
186 "channels_flags=P:private O:offline\n"
187 "avail_out=0.00477308500btc (USD $54.37)\n"
188 "utxo_amount=0.00000000btc (USD $0.00)\n"
189 "num_channels=1\n"
190 "num_connected=1\n"));
191 tal_free(output);
192 assert(!taken_any());
193 take_cleanup();
194 return 0;
195 }
196