* Run an onion encoding/decoding unit-test from a file */
| 191 | * Run an onion encoding/decoding unit-test from a file |
| 192 | */ |
| 193 | static void runtest(const char *filename) |
| 194 | { |
| 195 | const tal_t *ctx = tal(NULL, u8); |
| 196 | char *buffer = grab_file_str(ctx, filename); |
| 197 | const jsmntok_t *toks, *session_key_tok, *associated_data_tok, *gentok, |
| 198 | *hopstok, *hop, *payloadtok, *pubkeytok, *typetok, *oniontok, *decodetok; |
| 199 | const u8 *associated_data, *session_key_raw, *payload, *serialized, *onion; |
| 200 | struct secret session_key, *shared_secrets; |
| 201 | struct pubkey pubkey; |
| 202 | struct sphinx_path *path; |
| 203 | size_t i; |
| 204 | struct onionpacket *res; |
| 205 | struct route_step *step; |
| 206 | char *hexprivkey; |
| 207 | |
| 208 | toks = json_parse_simple(ctx, buffer, strlen(buffer)); |
| 209 | if (!toks) |
| 210 | errx(1, "File is not a valid JSON file."); |
| 211 | |
| 212 | gentok = json_get_member(buffer, toks, "generate"); |
| 213 | if (!gentok) |
| 214 | errx(1, "JSON object does not contain a 'generate' key"); |
| 215 | |
| 216 | /* Unpack the common parts */ |
| 217 | associated_data_tok = json_get_member(buffer, gentok, "associated_data"); |
| 218 | session_key_tok = json_get_member(buffer, gentok, "session_key"); |
| 219 | associated_data = json_tok_bin_from_hex(ctx, buffer, associated_data_tok); |
| 220 | session_key_raw = json_tok_bin_from_hex(ctx, buffer, session_key_tok); |
| 221 | memcpy(&session_key, session_key_raw, sizeof(session_key)); |
| 222 | path = sphinx_path_new_with_key(ctx, associated_data, tal_bytelen(associated_data), |
| 223 | &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 (!tal_arr_eq(onion, serialized)) |
| 247 | errx(1, |
| 248 | "Generated does not match the expected onion: \n" |
| 249 | "generated: %s\n" |
| 250 | "expected : %s\n", |
no test coverage detected