1 #include "../bolt12.c"
2 #include "../json.c"
3 #include <ccan/array_size/array_size.h>
4 #include <ccan/tal/grab_file/grab_file.h>
5 #include <ccan/tal/path/path.h>
6 #include <common/setup.h>
7 
8 bool deprecated_apis = false;
9 
10 /* AUTOGENERATED MOCKS START */
11 /* Generated stub for amount_asset_is_main */
amount_asset_is_main(struct amount_asset * asset UNNEEDED)12 bool amount_asset_is_main(struct amount_asset *asset UNNEEDED)
13 { fprintf(stderr, "amount_asset_is_main called!\n"); abort(); }
14 /* Generated stub for amount_asset_to_sat */
amount_asset_to_sat(struct amount_asset * asset UNNEEDED)15 struct amount_sat amount_asset_to_sat(struct amount_asset *asset UNNEEDED)
16 { fprintf(stderr, "amount_asset_to_sat called!\n"); abort(); }
17 /* Generated stub for amount_sat */
amount_sat(u64 satoshis UNNEEDED)18 struct amount_sat amount_sat(u64 satoshis UNNEEDED)
19 { fprintf(stderr, "amount_sat called!\n"); abort(); }
20 /* Generated stub for amount_sat_add */
amount_sat_add(struct amount_sat * val UNNEEDED,struct amount_sat a UNNEEDED,struct amount_sat b UNNEEDED)21  bool amount_sat_add(struct amount_sat *val UNNEEDED,
22 				       struct amount_sat a UNNEEDED,
23 				       struct amount_sat b UNNEEDED)
24 { fprintf(stderr, "amount_sat_add called!\n"); abort(); }
25 /* Generated stub for amount_sat_eq */
amount_sat_eq(struct amount_sat a UNNEEDED,struct amount_sat b UNNEEDED)26 bool amount_sat_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED)
27 { fprintf(stderr, "amount_sat_eq called!\n"); abort(); }
28 /* Generated stub for amount_sat_greater_eq */
amount_sat_greater_eq(struct amount_sat a UNNEEDED,struct amount_sat b UNNEEDED)29 bool amount_sat_greater_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED)
30 { fprintf(stderr, "amount_sat_greater_eq called!\n"); abort(); }
31 /* Generated stub for amount_sat_sub */
amount_sat_sub(struct amount_sat * val UNNEEDED,struct amount_sat a UNNEEDED,struct amount_sat b UNNEEDED)32  bool amount_sat_sub(struct amount_sat *val UNNEEDED,
33 				       struct amount_sat a UNNEEDED,
34 				       struct amount_sat b UNNEEDED)
35 { fprintf(stderr, "amount_sat_sub called!\n"); abort(); }
36 /* Generated stub for amount_sat_to_asset */
amount_sat_to_asset(struct amount_sat * sat UNNEEDED,const u8 * asset UNNEEDED)37 struct amount_asset amount_sat_to_asset(struct amount_sat *sat UNNEEDED, const u8 *asset UNNEEDED)
38 { fprintf(stderr, "amount_sat_to_asset called!\n"); abort(); }
39 /* Generated stub for amount_tx_fee */
amount_tx_fee(u32 fee_per_kw UNNEEDED,size_t weight UNNEEDED)40 struct amount_sat amount_tx_fee(u32 fee_per_kw UNNEEDED, size_t weight UNNEEDED)
41 { fprintf(stderr, "amount_tx_fee called!\n"); abort(); }
42 /* Generated stub for features_unsupported */
features_unsupported(const struct feature_set * our_features UNNEEDED,const u8 * their_features UNNEEDED,enum feature_place p UNNEEDED)43 int features_unsupported(const struct feature_set *our_features UNNEEDED,
44 			 const u8 *their_features UNNEEDED,
45 			 enum feature_place p UNNEEDED)
46 { fprintf(stderr, "features_unsupported called!\n"); abort(); }
47 /* Generated stub for from_bech32_charset */
from_bech32_charset(const tal_t * ctx UNNEEDED,const char * bech32 UNNEEDED,size_t bech32_len UNNEEDED,char ** hrp UNNEEDED,u8 ** data UNNEEDED)48 bool from_bech32_charset(const tal_t *ctx UNNEEDED,
49 			 const char *bech32 UNNEEDED, size_t bech32_len UNNEEDED,
50 			 char **hrp UNNEEDED, u8 **data UNNEEDED)
51 { fprintf(stderr, "from_bech32_charset called!\n"); abort(); }
52 /* Generated stub for fromwire */
fromwire(const u8 ** cursor UNNEEDED,size_t * max UNNEEDED,void * copy UNNEEDED,size_t n UNNEEDED)53 const u8 *fromwire(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, void *copy UNNEEDED, size_t n UNNEEDED)
54 { fprintf(stderr, "fromwire called!\n"); abort(); }
55 /* Generated stub for fromwire_amount_sat */
fromwire_amount_sat(const u8 ** cursor UNNEEDED,size_t * max UNNEEDED)56 struct amount_sat fromwire_amount_sat(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
57 { fprintf(stderr, "fromwire_amount_sat called!\n"); abort(); }
58 /* Generated stub for fromwire_bool */
fromwire_bool(const u8 ** cursor UNNEEDED,size_t * max UNNEEDED)59 bool fromwire_bool(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
60 { fprintf(stderr, "fromwire_bool called!\n"); abort(); }
61 /* Generated stub for fromwire_fail */
fromwire_fail(const u8 ** cursor UNNEEDED,size_t * max UNNEEDED)62 void *fromwire_fail(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
63 { fprintf(stderr, "fromwire_fail called!\n"); abort(); }
64 /* Generated stub for fromwire_invoice */
fromwire_invoice(const u8 ** cursor UNNEEDED,size_t * max UNNEEDED,struct tlv_invoice * record UNNEEDED)65 bool fromwire_invoice(const u8 **cursor UNNEEDED, size_t *max UNNEEDED,
66 			  struct tlv_invoice * record UNNEEDED)
67 { fprintf(stderr, "fromwire_invoice called!\n"); abort(); }
68 /* Generated stub for fromwire_invoice_request */
fromwire_invoice_request(const u8 ** cursor UNNEEDED,size_t * max UNNEEDED,struct tlv_invoice_request * record UNNEEDED)69 bool fromwire_invoice_request(const u8 **cursor UNNEEDED, size_t *max UNNEEDED,
70 			  struct tlv_invoice_request * record UNNEEDED)
71 { fprintf(stderr, "fromwire_invoice_request called!\n"); abort(); }
72 /* Generated stub for fromwire_offer */
fromwire_offer(const u8 ** cursor UNNEEDED,size_t * max UNNEEDED,struct tlv_offer * record UNNEEDED)73 bool fromwire_offer(const u8 **cursor UNNEEDED, size_t *max UNNEEDED,
74 			  struct tlv_offer * record UNNEEDED)
75 { fprintf(stderr, "fromwire_offer called!\n"); abort(); }
76 /* Generated stub for fromwire_secp256k1_ecdsa_signature */
fromwire_secp256k1_ecdsa_signature(const u8 ** cursor UNNEEDED,size_t * max UNNEEDED,secp256k1_ecdsa_signature * signature UNNEEDED)77 void fromwire_secp256k1_ecdsa_signature(const u8 **cursor UNNEEDED, size_t *max UNNEEDED,
78 					secp256k1_ecdsa_signature *signature UNNEEDED)
79 { fprintf(stderr, "fromwire_secp256k1_ecdsa_signature called!\n"); abort(); }
80 /* Generated stub for fromwire_sha256 */
fromwire_sha256(const u8 ** cursor UNNEEDED,size_t * max UNNEEDED,struct sha256 * sha256 UNNEEDED)81 void fromwire_sha256(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct sha256 *sha256 UNNEEDED)
82 { fprintf(stderr, "fromwire_sha256 called!\n"); abort(); }
83 /* Generated stub for fromwire_tal_arrn */
fromwire_tal_arrn(const tal_t * ctx UNNEEDED,const u8 ** cursor UNNEEDED,size_t * max UNNEEDED,size_t num UNNEEDED)84 u8 *fromwire_tal_arrn(const tal_t *ctx UNNEEDED,
85 		       const u8 **cursor UNNEEDED, size_t *max UNNEEDED, size_t num UNNEEDED)
86 { fprintf(stderr, "fromwire_tal_arrn called!\n"); abort(); }
87 /* Generated stub for fromwire_u16 */
fromwire_u16(const u8 ** cursor UNNEEDED,size_t * max UNNEEDED)88 u16 fromwire_u16(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
89 { fprintf(stderr, "fromwire_u16 called!\n"); abort(); }
90 /* Generated stub for fromwire_u32 */
fromwire_u32(const u8 ** cursor UNNEEDED,size_t * max UNNEEDED)91 u32 fromwire_u32(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
92 { fprintf(stderr, "fromwire_u32 called!\n"); abort(); }
93 /* Generated stub for fromwire_u64 */
fromwire_u64(const u8 ** cursor UNNEEDED,size_t * max UNNEEDED)94 u64 fromwire_u64(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
95 { fprintf(stderr, "fromwire_u64 called!\n"); abort(); }
96 /* Generated stub for fromwire_u8 */
fromwire_u8(const u8 ** cursor UNNEEDED,size_t * max UNNEEDED)97 u8 fromwire_u8(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
98 { fprintf(stderr, "fromwire_u8 called!\n"); abort(); }
99 /* Generated stub for fromwire_u8_array */
fromwire_u8_array(const u8 ** cursor UNNEEDED,size_t * max UNNEEDED,u8 * arr UNNEEDED,size_t num UNNEEDED)100 void fromwire_u8_array(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, u8 *arr UNNEEDED, size_t num UNNEEDED)
101 { fprintf(stderr, "fromwire_u8_array called!\n"); abort(); }
102 /* 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,...)103 void json_add_member(struct json_stream *js UNNEEDED,
104 		     const char *fieldname UNNEEDED,
105 		     bool quote UNNEEDED,
106 		     const char *fmt UNNEEDED, ...)
107 { fprintf(stderr, "json_add_member called!\n"); abort(); }
108 /* Generated stub for json_member_direct */
json_member_direct(struct json_stream * js UNNEEDED,const char * fieldname UNNEEDED,size_t extra UNNEEDED)109 char *json_member_direct(struct json_stream *js UNNEEDED,
110 			 const char *fieldname UNNEEDED, size_t extra UNNEEDED)
111 { fprintf(stderr, "json_member_direct called!\n"); abort(); }
112 /* Generated stub for merkle_tlv */
merkle_tlv(const struct tlv_field * fields UNNEEDED,struct sha256 * merkle UNNEEDED)113 void merkle_tlv(const struct tlv_field *fields UNNEEDED, struct sha256 *merkle UNNEEDED)
114 { fprintf(stderr, "merkle_tlv called!\n"); abort(); }
115 /* Generated stub for sighash_from_merkle */
sighash_from_merkle(const char * messagename UNNEEDED,const char * fieldname UNNEEDED,const struct sha256 * merkle UNNEEDED,struct sha256 * sighash UNNEEDED)116 void sighash_from_merkle(const char *messagename UNNEEDED,
117 			 const char *fieldname UNNEEDED,
118 			 const struct sha256 *merkle UNNEEDED,
119 			 struct sha256 *sighash UNNEEDED)
120 { fprintf(stderr, "sighash_from_merkle called!\n"); abort(); }
121 /* Generated stub for tlv_invoice_new */
tlv_invoice_new(const tal_t * ctx UNNEEDED)122 struct tlv_invoice *tlv_invoice_new(const tal_t *ctx UNNEEDED)
123 { fprintf(stderr, "tlv_invoice_new called!\n"); abort(); }
124 /* Generated stub for tlv_invoice_request_new */
tlv_invoice_request_new(const tal_t * ctx UNNEEDED)125 struct tlv_invoice_request *tlv_invoice_request_new(const tal_t *ctx UNNEEDED)
126 { fprintf(stderr, "tlv_invoice_request_new called!\n"); abort(); }
127 /* Generated stub for tlv_offer_new */
tlv_offer_new(const tal_t * ctx UNNEEDED)128 struct tlv_offer *tlv_offer_new(const tal_t *ctx UNNEEDED)
129 { fprintf(stderr, "tlv_offer_new called!\n"); abort(); }
130 /* Generated stub for to_bech32_charset */
to_bech32_charset(const tal_t * ctx UNNEEDED,const char * hrp UNNEEDED,const u8 * data UNNEEDED)131 char *to_bech32_charset(const tal_t *ctx UNNEEDED,
132 			const char *hrp UNNEEDED, const u8 *data UNNEEDED)
133 { fprintf(stderr, "to_bech32_charset called!\n"); abort(); }
134 /* Generated stub for towire */
towire(u8 ** pptr UNNEEDED,const void * data UNNEEDED,size_t len UNNEEDED)135 void towire(u8 **pptr UNNEEDED, const void *data UNNEEDED, size_t len UNNEEDED)
136 { fprintf(stderr, "towire called!\n"); abort(); }
137 /* Generated stub for towire_amount_sat */
towire_amount_sat(u8 ** pptr UNNEEDED,const struct amount_sat sat UNNEEDED)138 void towire_amount_sat(u8 **pptr UNNEEDED, const struct amount_sat sat UNNEEDED)
139 { fprintf(stderr, "towire_amount_sat called!\n"); abort(); }
140 /* Generated stub for towire_bool */
towire_bool(u8 ** pptr UNNEEDED,bool v UNNEEDED)141 void towire_bool(u8 **pptr UNNEEDED, bool v UNNEEDED)
142 { fprintf(stderr, "towire_bool called!\n"); abort(); }
143 /* Generated stub for towire_invoice */
towire_invoice(u8 ** pptr UNNEEDED,const struct tlv_invoice * record UNNEEDED)144 void towire_invoice(u8 **pptr UNNEEDED, const struct tlv_invoice *record UNNEEDED)
145 { fprintf(stderr, "towire_invoice called!\n"); abort(); }
146 /* Generated stub for towire_invoice_request */
towire_invoice_request(u8 ** pptr UNNEEDED,const struct tlv_invoice_request * record UNNEEDED)147 void towire_invoice_request(u8 **pptr UNNEEDED, const struct tlv_invoice_request *record UNNEEDED)
148 { fprintf(stderr, "towire_invoice_request called!\n"); abort(); }
149 /* Generated stub for towire_offer */
towire_offer(u8 ** pptr UNNEEDED,const struct tlv_offer * record UNNEEDED)150 void towire_offer(u8 **pptr UNNEEDED, const struct tlv_offer *record UNNEEDED)
151 { fprintf(stderr, "towire_offer called!\n"); abort(); }
152 /* Generated stub for towire_secp256k1_ecdsa_signature */
towire_secp256k1_ecdsa_signature(u8 ** pptr UNNEEDED,const secp256k1_ecdsa_signature * signature UNNEEDED)153 void towire_secp256k1_ecdsa_signature(u8 **pptr UNNEEDED,
154 			      const secp256k1_ecdsa_signature *signature UNNEEDED)
155 { fprintf(stderr, "towire_secp256k1_ecdsa_signature called!\n"); abort(); }
156 /* Generated stub for towire_sha256 */
towire_sha256(u8 ** pptr UNNEEDED,const struct sha256 * sha256 UNNEEDED)157 void towire_sha256(u8 **pptr UNNEEDED, const struct sha256 *sha256 UNNEEDED)
158 { fprintf(stderr, "towire_sha256 called!\n"); abort(); }
159 /* Generated stub for towire_u16 */
towire_u16(u8 ** pptr UNNEEDED,u16 v UNNEEDED)160 void towire_u16(u8 **pptr UNNEEDED, u16 v UNNEEDED)
161 { fprintf(stderr, "towire_u16 called!\n"); abort(); }
162 /* Generated stub for towire_u32 */
towire_u32(u8 ** pptr UNNEEDED,u32 v UNNEEDED)163 void towire_u32(u8 **pptr UNNEEDED, u32 v UNNEEDED)
164 { fprintf(stderr, "towire_u32 called!\n"); abort(); }
165 /* Generated stub for towire_u64 */
towire_u64(u8 ** pptr UNNEEDED,u64 v UNNEEDED)166 void towire_u64(u8 **pptr UNNEEDED, u64 v UNNEEDED)
167 { fprintf(stderr, "towire_u64 called!\n"); abort(); }
168 /* Generated stub for towire_u8 */
towire_u8(u8 ** pptr UNNEEDED,u8 v UNNEEDED)169 void towire_u8(u8 **pptr UNNEEDED, u8 v UNNEEDED)
170 { fprintf(stderr, "towire_u8 called!\n"); abort(); }
171 /* Generated stub for towire_u8_array */
towire_u8_array(u8 ** pptr UNNEEDED,const u8 * arr UNNEEDED,size_t num UNNEEDED)172 void towire_u8_array(u8 **pptr UNNEEDED, const u8 *arr UNNEEDED, size_t num UNNEEDED)
173 { fprintf(stderr, "towire_u8_array called!\n"); abort(); }
174 /* AUTOGENERATED MOCKS END */
175 
main(int argc,char * argv[])176 int main(int argc, char *argv[])
177 {
178 	char *json;
179 	size_t i;
180 	jsmn_parser parser;
181 	jsmntok_t toks[5000];
182 	const jsmntok_t *t;
183 
184 	common_setup(argv[0]);
185 
186 	if (argv[1])
187 		json = grab_file(tmpctx, argv[1]);
188 	else {
189 		char *dir = getenv("BOLTDIR");
190 		json = grab_file(tmpctx,
191 				 path_join(tmpctx,
192 					   dir ? dir : "../lightning-rfc",
193 					   "bolt12/offer-period-test.json"));
194 		if (!json) {
195 			printf("test file not found, skipping\n");
196 			common_shutdown();
197 			exit(0);
198 		}
199 	}
200 
201 	jsmn_init(&parser);
202 	if (jsmn_parse(&parser, json, strlen(json), toks, ARRAY_SIZE(toks)) < 0)
203 		abort();
204 
205 	/* We deal in UTC; mktime() uses local time */
206 	setenv("TZ", "", 1);
207 	json_for_each_arr(i, t, toks) {
208 		struct tlv_offer_recurrence recurrence;
209 		int unit;
210 		const jsmntok_t *exp;
211 		u64 base, secs, n;
212 		time_t time;
213 		char *timestring, *tstr, **tparts;
214 
215 		assert(json_to_u64(json, json_get_member(json, t, "basetime"),
216 				   &base));
217 		assert(json_to_int(json, json_get_member(json, t, "time_unit"),
218 				   &unit));
219 		recurrence.time_unit = unit;
220 		json_to_u32(json, json_get_member(json, t, "period"),
221 			    &recurrence.period);
222 		assert(json_to_u64(json, json_get_member(json, t, "n"), &n));
223 		exp = json_get_member(json, t, "expect");
224 		assert(json_to_u64(json, json_get_member(json, exp, "seconds_since_epoch"),
225 				   &secs));
226 		timestring = json_strdup(tmpctx, json,
227 					 json_get_member(json, exp, "timestring"));
228 		time = secs;
229 		/* Form is 'Thu Dec 31 10:00:01 1970\n' */
230 		tstr = asctime(gmtime(&time));
231 		/* Remove \n */
232 		tstr[strlen(tstr)-1] = '\0';
233 		tparts = tal_strsplit(tmpctx, tstr, " ", STR_EMPTY_OK);
234 		/* Replace day */
235 		tparts[0] = "UTC";
236 		/* Now each part should appear. */
237 		for (size_t i = 0; tparts[i]; i++)
238 			assert(strstr(timestring, tparts[i]));
239 		assert(offer_period_start(base, n, &recurrence) == secs);
240 	}
241 	common_shutdown();
242 	return 0;
243 }
244