MCPcopy Create free account
hub / github.com/ElementsProject/lightning / runtest

Function runtest

devtools/onion.c:194–273  ·  view source on GitHub ↗

* Run an onion encoding/decoding unit-test from a file */

Source from the content-addressed store, hash-verified

192 * Run an onion encoding/decoding unit-test from a file
193 */
194static void runtest(const char *filename)
195{
196 const tal_t *ctx = tal(NULL, u8);
197 char *buffer = grab_file(ctx, filename);
198 const jsmntok_t *toks, *session_key_tok, *associated_data_tok, *gentok,
199 *hopstok, *hop, *payloadtok, *pubkeytok, *typetok, *oniontok, *decodetok;
200 const u8 *associated_data, *session_key_raw, *payload, *serialized, *onion;
201 struct secret session_key, *shared_secrets;
202 struct pubkey pubkey;
203 struct sphinx_path *path;
204 size_t i;
205 struct onionpacket *res;
206 struct route_step *step;
207 char *hexprivkey;
208
209 toks = json_parse_simple(ctx, buffer, strlen(buffer));
210 if (!toks)
211 errx(1, "File is not a valid JSON file.");
212
213 gentok = json_get_member(buffer, toks, "generate");
214 if (!gentok)
215 errx(1, "JSON object does not contain a 'generate' key");
216
217 /* Unpack the common parts */
218 associated_data_tok = json_get_member(buffer, gentok, "associated_data");
219 session_key_tok = json_get_member(buffer, gentok, "session_key");
220 associated_data = json_tok_bin_from_hex(ctx, buffer, associated_data_tok);
221 session_key_raw = json_tok_bin_from_hex(ctx, buffer, session_key_tok);
222 memcpy(&session_key, session_key_raw, sizeof(session_key));
223 path = sphinx_path_new_with_key(ctx, associated_data, &session_key);
224
225 /* Unpack the hops and build up the path */
226 hopstok = json_get_member(buffer, gentok, "hops");
227 json_for_each_arr(i, hop, hopstok) {
228 payloadtok = json_get_member(buffer, hop, "payload");
229 typetok = json_get_member(buffer, hop, "type");
230 pubkeytok = json_get_member(buffer, hop, "pubkey");
231 payload = json_tok_bin_from_hex(ctx, buffer, payloadtok);
232 json_to_pubkey(buffer, pubkeytok, &pubkey);
233 assert(json_tok_streq(buffer, typetok, "tlv"));
234 sphinx_add_hop(path, &pubkey, take(payload));
235 }
236 res = create_onionpacket(ctx, path, ROUTING_INFO_SIZE, &shared_secrets);
237 serialized = serialize_onionpacket(ctx, res);
238
239 if (!serialized)
240 errx(1, "Error serializing message.");
241
242 oniontok = json_get_member(buffer, toks, "onion");
243
244 if (oniontok) {
245 onion = json_tok_bin_from_hex(ctx, buffer, oniontok);
246 if (!memeq(onion, tal_bytelen(onion), serialized,
247 tal_bytelen(serialized)))
248 errx(1,
249 "Generated does not match the expected onion: \n"
250 "generated: %s\n"
251 "expected : %s\n",

Callers 1

mainFunction · 0.85

Calls 15

grab_fileFunction · 0.85
errxFunction · 0.85
sphinx_path_new_with_keyFunction · 0.85
sphinx_add_hopFunction · 0.85
create_onionpacketFunction · 0.85
memeqFunction · 0.85
tal_bytelenFunction · 0.85
tal_hexFunction · 0.85
decode_with_privkeyFunction · 0.85
tal_hexstrFunction · 0.85
tal_freeFunction · 0.85
json_parse_simpleFunction · 0.50

Tested by

no test coverage detected