forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-tlvstream.c
638 lines (515 loc) · 20.2 KB
/
run-tlvstream.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
#include <ccan/array_size/array_size.h>
#include <ccan/str/hex/hex.h>
#include <common/utils.h>
#include <stdio.h>
#include <wally_core.h>
static const char *reason;
#undef SUPERVERBOSE
#define SUPERVERBOSE(r) do { reason = (r); } while(0)
#include <bitcoin/pubkey.c>
#include <common/amount.c>
#include <common/bigsize.c>
#include <common/node_id.c>
#include <common/setup.h>
#if EXPERIMENTAL_FEATURES
#include <wire/peer_exp_wiregen.c>
#else
#include <wire/peer_wiregen.c>
#endif
#include <wire/fromwire.c>
#include <wire/towire.c>
#include <wire/tlvstream.c>
/* AUTOGENERATED MOCKS START */
/* Generated stub for fromwire_channel_id */
void fromwire_channel_id(const u8 **cursor UNNEEDED, size_t *max UNNEEDED,
struct channel_id *channel_id UNNEEDED)
{ fprintf(stderr, "fromwire_channel_id called!\n"); abort(); }
/* Generated stub for towire_channel_id */
void towire_channel_id(u8 **pptr UNNEEDED, const struct channel_id *channel_id UNNEEDED)
{ fprintf(stderr, "towire_channel_id called!\n"); abort(); }
/* AUTOGENERATED MOCKS END */
/* BOLT #1
### TLV Decoding Failures
The following TLV streams in any namespace should trigger a decoding failure:
1. Invalid stream: 0xfd
2. Reason: type truncated
1. Invalid stream: 0xfd01
2. Reason: type truncated
1. Invalid stream: 0xfd0001 00
2. Reason: not minimally encoded type
1. Invalid stream: 0xfd0101
2. Reason: missing length
1. Invalid stream: 0x0f fd
2. Reason: (length truncated)
1. Invalid stream: 0x0f fd26
2. Reason: (length truncated)
1. Invalid stream: 0x0f fd2602
2. Reason: missing value
1. Invalid stream: 0x0f fd0001 00
2. Reason: not minimally encoded length
1. Invalid stream: 0x0f fd0201 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
2. Reason: value truncated
The following TLV streams in either namespace should trigger a
decoding failure:
1. Invalid stream: 0x12 00
2. Reason: unknown even type.
1. Invalid stream: 0xfd0102 00
2. Reason: unknown even type.
1. Invalid stream: 0xfe01000002 00
2. Reason: unknown even type.
1. Invalid stream: 0xff0100000000000002 00
2. Reason: unknown even type.
*/
struct invalid_stream {
const char *hex;
const char *reason;
};
static struct invalid_stream invalid_streams_either[] = {
{ "fd", "type truncated" },
{ "fd01", "type truncated" },
{ "fd0001 00", "not minimally encoded type" },
{ "fd0101", "missing length" },
{ "0f fd", "(length truncated)" },
{ "0f fd26", "(length truncated)" },
{ "0f fd2602", "missing value" },
{ "0f fd0001 00", "not minimally encoded length" },
{ "0f fd0201 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "value truncated" },
{ "12 00", "unknown even type." },
{ "fd0102 00", "unknown even type." },
{ "fe01000002 00", "unknown even type." },
{ "ff0100000000000002 00", "unknown even type." },
};
/* BOLT #1:
*
* The following TLV streams in namespace `n1` should trigger a decoding
failure:
1. Invalid stream: 0x01 09 ffffffffffffffffff
2. Reason: greater than encoding length for `n1`s `tlv1`.
1. Invalid stream: 0x01 01 00
2. Reason: encoding for `n1`s `tlv1`s `amount_msat` is not minimal
1. Invalid stream: 0x01 02 0001
2. Reason: encoding for `n1`s `tlv1`s `amount_msat` is not minimal
1. Invalid stream: 0x01 03 000100
2. Reason: encoding for `n1`s `tlv1`s `amount_msat` is not minimal
1. Invalid stream: 0x01 04 00010000
2. Reason: encoding for `n1`s `tlv1`s `amount_msat` is not minimal
1. Invalid stream: 0x01 05 0001000000
2. Reason: encoding for `n1`s `tlv1`s `amount_msat` is not minimal
1. Invalid stream: 0x01 06 000100000000
2. Reason: encoding for `n1`s `tlv1`s `amount_msat` is not minimal
1. Invalid stream: 0x01 07 00010000000000
2. Reason: encoding for `n1`s `tlv1`s `amount_msat` is not minimal
1. Invalid stream: 0x01 08 0001000000000000
2. Reason: encoding for `n1`s `tlv1`s `amount_msat` is not minimal
1. Invalid stream: 0x02 07 01010101010101
2. Reason: less than encoding length for `n1`s `tlv2`.
1. Invalid stream: 0x02 09 010101010101010101
2. Reason: greater than encoding length for `n1`s `tlv2`.
1. Invalid stream: 0x03 21 023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb
2. Reason: less than encoding length for `n1`s `tlv3`.
1. Invalid stream: 0x03 29 023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb0000000000000001
2. Reason: less than encoding length for `n1`s `tlv3`.
1. Invalid stream: 0x03 30 023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb000000000000000100000000000001
2. Reason: less than encoding length for `n1`s `tlv3`.
1. Invalid stream: 0x03 31 043da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb00000000000000010000000000000002
2. Reason: `n1`s `node_id` is not a valid point.
1. Invalid stream: 0x03 32 023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb0000000000000001000000000000000001
2. Reason: greater than encoding length for `n1`s `tlv3`.
1. Invalid stream: 0xfd00fe 00
2. Reason: less than encoding length for `n1`s `tlv4`.
1. Invalid stream: 0xfd00fe 01 01
2. Reason: less than encoding length for `n1`s `tlv4`.
1. Invalid stream: 0xfd00fe 03 010101
2. Reason: greater than encoding length for `n1`s `tlv4`.
1. Invalid stream: 0x00 00
2. Reason: unknown even field for `n1`s namespace.
*/
static struct invalid_stream invalid_streams_n1[] = {
{ "01 09 ffffffffffffffffff", "greater than encoding length for `n1`s `tlv1`." },
{ "01 01 00", "encoding for `n1`s `tlv1`s `amount_msat` is not minimal" },
{ "01 02 0001", "encoding for `n1`s `tlv1`s `amount_msat` is not minimal" },
{ "01 03 000100", "encoding for `n1`s `tlv1`s `amount_msat` is not minimal" },
{ "01 04 00010000", "encoding for `n1`s `tlv1`s `amount_msat` is not minimal" },
{ "01 05 0001000000", "encoding for `n1`s `tlv1`s `amount_msat` is not minimal" },
{ "01 06 000100000000", "encoding for `n1`s `tlv1`s `amount_msat` is not minimal" },
{ "01 07 00010000000000", "encoding for `n1`s `tlv1`s `amount_msat` is not minimal" },
{ "01 08 0001000000000000", "encoding for `n1`s `tlv1`s `amount_msat` is not minimal" },
{ "02 07 01010101010101", "less than encoding length for `n1`s `tlv2`." },
{ "02 09 010101010101010101", "greater than encoding length for `n1`s `tlv2`." },
{ "03 21 023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb", "less than encoding length for `n1`s `tlv3`." },
{ "03 29 023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb0000000000000001", "less than encoding length for `n1`s `tlv3`." },
{ "03 30 023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb000000000000000100000000000001", "less than encoding length for `n1`s `tlv3`." },
{ "03 31 043da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb00000000000000010000000000000002", "`n1`s `node_id` is not a valid point." },
{ "03 32 023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb0000000000000001000000000000000001", "greater than encoding length for `n1`s `tlv3`." },
{ "fd00fe 00", "less than encoding length for `n1`s `tlv4`." },
{ "fd00fe 01 01", "less than encoding length for `n1`s `tlv4`." },
{ "fd00fe 03 010101", "greater than encoding length for `n1`s `tlv4`." },
{ "00 00", "unknown even field for `n1`s namespace." },
};
/* BOLT #1:
### TLV Stream Decoding Failure
Any appending of an invalid stream to a valid stream should trigger
a decoding failure.
Any appending of a higher-numbered valid stream to a lower-numbered
valid stream should not trigger a decoding failure.
In addition, the following TLV streams in namespace `n1` should
trigger a decoding failure:
1. Invalid stream: 0x02 08 0000000000000226 01 01 2a
2. Reason: valid TLV records but invalid ordering
1. Invalid stream: 0x02 08 0000000000000231 02 08 0000000000000451
2. Reason: duplicate TLV type
1. Invalid stream: 0x1f 00 0f 01 2a
2. Reason: valid (ignored) TLV records but invalid ordering
1. Invalid stream: 0x1f 00 1f 01 2a
2. Reason: duplicate TLV type (ignored)
*/
static struct invalid_stream invalid_streams_n1_combo[] = {
{ "02 08 0000000000000226 01 01 2a", "valid tlv records but invalid ordering" },
{ "02 08 0000000000000231 02 08 0000000000000451", "duplicate tlv type" },
{ "1f 00 0f 01 2a", "valid (ignored) tlv records but invalid ordering" },
{ "1f 00 1f 01 2a", "duplicate tlv type (ignored)" }
};
/* BOLT #1:
The following TLV stream in namespace `n2` should trigger a decoding
failure:
1. Invalid stream: 0xffffffffffffffffff 00 00 00
2. Reason: valid TLV records but invalid ordering
*/
static struct invalid_stream invalid_streams_n2_combo[] = {
{ "ffffffffffffffffff 00 00 00", "valid TLV records but invalid ordering" },
};
/* BOLT #1:
*
### TLV Decoding Successes
The following TLV streams in either namespace should correctly decode,
and be ignored:
1. Valid stream: 0x
2. Explanation: empty message
1. Valid stream: 0x21 00
2. Explanation: Unknown odd type.
1. Valid stream: 0xfd0201 00
2. Explanation: Unknown odd type.
1. Valid stream: 0xfd00fd 00
2. Explanation: Unknown odd type.
1. Valid stream: 0xfd00ff 00
2. Explanation: Unknown odd type.
1. Valid stream: 0xfe02000001 00
2. Explanation: Unknown odd type.
1. Valid stream: 0xff0200000000000001 00
2. Explanation: Unknown odd type.
The following TLV streams in `n1` namespace should correctly decode,
with the values given here:
1. Valid stream: 0x01 00
2. Values: `tlv1` `amount_msat`=0
1. Valid stream: 0x01 01 01
2. Values: `tlv1` `amount_msat`=1
1. Valid stream: 0x01 02 0100
2. Values: `tlv1` `amount_msat`=256
1. Valid stream: 0x01 03 010000
2. Values: `tlv1` `amount_msat`=65536
1. Valid stream: 0x01 04 01000000
2. Values: `tlv1` `amount_msat`=16777216
1. Valid stream: 0x01 05 0100000000
2. Values: `tlv1` `amount_msat`=4294967296
1. Valid stream: 0x01 06 010000000000
2. Values: `tlv1` `amount_msat`=1099511627776
1. Valid stream: 0x01 07 01000000000000
2. Values: `tlv1` `amount_msat`=281474976710656
1. Valid stream: 0x01 08 0100000000000000
2. Values: `tlv1` `amount_msat`=72057594037927936
1. Valid stream: 0x02 08 0000000000000226
2. Values: `tlv2` `scid`=0x0x550
1. Valid stream: 0x03 31 023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb00000000000000010000000000000002
2. Values: `tlv3` `node_id`=023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb `amount_msat_1`=1 `amount_msat_2`=2
1. Valid stream: 0xfd00fe 02 0226
2. Values: `tlv4` `cltv_delta`=550
*/
struct valid_stream {
const char *hex;
const struct tlv_n1 expect;
};
static u64 tlv1_0 = 0;
static u64 tlv1_1 = 1;
static u64 tlv1_256 = 256;
static u64 tlv1_65536 = 65536;
static u64 tlv1_16777216 = 16777216;
static u64 tlv1_4294967296 = 4294967296ULL;
static u64 tlv1_1099511627776 = 1099511627776UL;
static u64 tlv1_281474976710656 = 281474976710656ULL;
static u64 tlv1_72057594037927936 = 72057594037927936ULL;
static struct short_channel_id tlv2_0x0x550 = { .u64 = 0x000000000226 };
/* filled in at runtime. */
static struct tlv_n1_tlv3 tlv3_node_id;
static u16 tlv4_550 = 550;
static struct valid_stream valid_streams[] = {
/* Valid but no (known) content. */
{ "", {NULL, NULL, NULL, NULL} },
{ "21 00", {NULL, NULL, NULL, NULL} },
{ "fd0201 00", {NULL, NULL, NULL, NULL} },
{ "fd00fd 00", {NULL, NULL, NULL, NULL} },
{ "fd00ff 00", {NULL, NULL, NULL, NULL} },
{ "fe02000001 00", {NULL, NULL, NULL, NULL} },
{ "ff0200000000000001 00", {NULL, NULL, NULL, NULL} },
/* TLV1 */
{ "01 00", { .tlv1 = &tlv1_0 } },
{ "01 01 01", { .tlv1 = &tlv1_1 } },
{ "01 02 0100", { .tlv1 = &tlv1_256 } },
{ "01 03 010000", { .tlv1 = &tlv1_65536 } },
{ "01 04 01000000", { .tlv1 = &tlv1_16777216 } },
{ "01 05 0100000000", { .tlv1 = &tlv1_4294967296 } },
{ "01 06 010000000000", { .tlv1 = &tlv1_1099511627776 } },
{ "01 07 01000000000000", { .tlv1 = &tlv1_281474976710656 } },
{ "01 08 0100000000000000", { .tlv1 = &tlv1_72057594037927936 } },
{ "02 08 0000000000000226", { .tlv2 = &tlv2_0x0x550 } },
{ "03 31 023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb00000000000000010000000000000002", { .tlv3 = &tlv3_node_id } },
{ "fd00fe 02 0226", { .tlv4 = &tlv4_550 } },
};
static bool tlv_n1_eq(const struct tlv_n1 *a, const struct tlv_n1 *b)
{
if (a->tlv1) {
if (!b->tlv1)
return false;
if (*a->tlv1 != *b->tlv1)
return false;
} else if (b->tlv1)
return false;
if (a->tlv2) {
if (!b->tlv2)
return false;
if (!short_channel_id_eq(a->tlv2, b->tlv2))
return false;
} else if (b->tlv2)
return false;
if (a->tlv3) {
if (!b->tlv3)
return false;
if (!pubkey_eq(&a->tlv3->node_id, &b->tlv3->node_id))
return false;
if (!amount_msat_eq(a->tlv3->amount_msat_1,
b->tlv3->amount_msat_1))
return false;
if (!amount_msat_eq(a->tlv3->amount_msat_2,
b->tlv3->amount_msat_2))
return false;
} else if (b->tlv3)
return false;
if (a->tlv4) {
if (!b->tlv4)
return false;
if (*a->tlv4 != *b->tlv4)
return false;
} else if (b->tlv4)
return false;
return true;
}
/* hexstr is valid, so pull out first field */
static u64 pull_type(const char *hexstr)
{
u8 v;
hex_decode(hexstr, 2, &v, sizeof(v));
switch (v) {
case 0xfd: {
u16 d;
hex_decode(hexstr + 2, 2*sizeof(d), &d, sizeof(d));
return be16_to_cpu(d);
}
case 0xfe: {
u32 d;
hex_decode(hexstr + 2, 2*sizeof(d), &d, sizeof(d));
return be32_to_cpu(d);
}
case 0xff: {
u64 d;
hex_decode(hexstr + 2, 2*sizeof(d), &d, sizeof(d));
return be64_to_cpu(d);
}
default:
return v;
}
}
static u8 *stream(const tal_t *ctx, const char *hex)
{
size_t i, j;
char *str = tal_arr(tmpctx, char, strlen(hex) + 1);
/* Remove spaces and use normal helper */
for (i = j = 0; i <= strlen(hex); i++) {
if (hex[i] != ' ')
str[j++] = hex[i];
}
return tal_hexdata(ctx, str, strlen(str));
}
static u8 *stream2(const tal_t *ctx, const char *hex1, const char *hex2)
{
u8 *a = stream(tmpctx, hex1), *b = stream(tmpctx, hex2), *ret;
ret = tal_dup_arr(ctx, u8, a, tal_count(a), tal_count(b));
memcpy(ret + tal_count(a), b, tal_count(b));
return ret;
}
static bool ignored_fields(const struct tlv_n1 *tlv_n1)
{
return tlv_n1->tlv1 == NULL
&& tlv_n1->tlv2 == NULL
&& tlv_n1->tlv3 == NULL
&& tlv_n1->tlv4 == NULL;
}
int main(int argc, char *argv[])
{
common_setup(argv[0]);
if (!pubkey_from_hexstr("023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb", 66, &tlv3_node_id.node_id))
abort();
/* gcc 4.8 doesn't like this initialized statically. */
tlv3_node_id.amount_msat_1 = AMOUNT_MSAT(1);
tlv3_node_id.amount_msat_2 = AMOUNT_MSAT(2);
for (size_t i = 0; i < ARRAY_SIZE(invalid_streams_either); i++) {
struct tlv_n1 *tlv_n1 = tlv_n1_new(tmpctx);
struct tlv_n2 *tlv_n2 = tlv_n2_new(tmpctx);
const u8 *p, *orig_p;
size_t max;
orig_p = stream(tmpctx, invalid_streams_either[i].hex);
max = tal_count(orig_p);
p = orig_p;
assert((!fromwire_n1(&p, &max, tlv_n1) && !p) ||
!n1_is_valid(tlv_n1, NULL));
assert(strstr(invalid_streams_either[i].reason, reason));
max = tal_count(orig_p);
p = orig_p;
assert((!fromwire_n2(&p, &max, tlv_n2) && !p) ||
!n2_is_valid(tlv_n2, NULL));
assert(strstr(invalid_streams_either[i].reason, reason));
}
for (size_t i = 0; i < ARRAY_SIZE(invalid_streams_n1); i++) {
struct tlv_n1 *tlv_n1 = tlv_n1_new(tmpctx);
const u8 *p;
size_t max;
p = stream(tmpctx, invalid_streams_n1[i].hex);
max = tal_count(p);
assert((!fromwire_n1(&p, &max, tlv_n1) && !p) ||
!n1_is_valid(tlv_n1, NULL));
assert(strstr(invalid_streams_n1[i].reason, reason));
}
for (size_t i = 0; i < ARRAY_SIZE(invalid_streams_n1_combo); i++) {
struct tlv_n1 *tlv_n1 = tlv_n1_new(tmpctx);
const u8 *p;
size_t max;
p = stream(tmpctx, invalid_streams_n1_combo[i].hex);
max = tal_count(p);
assert((!fromwire_n1(&p, &max, tlv_n1) && !p) ||
!n1_is_valid(tlv_n1, NULL));
assert(strstr(invalid_streams_n1_combo[i].reason, reason));
}
for (size_t i = 0; i < ARRAY_SIZE(invalid_streams_n2_combo); i++) {
struct tlv_n2 *tlv_n2 = tlv_n2_new(tmpctx);
const u8 *p;
size_t max;
p = stream(tmpctx, invalid_streams_n2_combo[i].hex);
max = tal_count(p);
assert((!fromwire_n2(&p, &max, tlv_n2) && !p) ||
!n2_is_valid(tlv_n2, NULL));
assert(strstr(invalid_streams_n2_combo[i].reason, reason));
}
for (size_t i = 0; i < ARRAY_SIZE(valid_streams); i++) {
struct tlv_n1 *tlv_n1 = tlv_n1_new(tmpctx);
const u8 *orig_p, *p;
u8 *p2;
size_t max;
orig_p = stream(tmpctx, valid_streams[i].hex);
max = tal_count(orig_p);
p = orig_p;
assert(fromwire_n1(&p, &max, tlv_n1) &&
n1_is_valid(tlv_n1, NULL));
assert(max == 0);
assert(tlv_n1_eq(tlv_n1, &valid_streams[i].expect));
/* Re-encoding should give the same results (except
* ignored fields tests!) */
if (ignored_fields(tlv_n1))
continue;
p2 = tal_arr(tmpctx, u8, 0);
towire_n1(&p2, tlv_n1);
assert(memeq(p2, tal_count(p2), orig_p, tal_count(orig_p)));
}
/* BOLT #1:
*
* Any appending of an invalid stream to a valid stream should trigger
* a decoding failure.
*/
for (size_t i = 0; i < ARRAY_SIZE(invalid_streams_either); i++) {
for (size_t j = 0; j < ARRAY_SIZE(valid_streams); j++) {
struct tlv_n1 *tlv_n1 = tlv_n1_new(tmpctx);
struct tlv_n2 *tlv_n2 = tlv_n2_new(tmpctx);
const u8 *orig_p, *p;
size_t max;
/* Append */
orig_p = stream2(tmpctx, valid_streams[j].hex,
invalid_streams_either[i].hex);
max = tal_count(orig_p);
p = orig_p;
assert((!fromwire_n1(&p, &max, tlv_n1) && !p) ||
!n1_is_valid(tlv_n1, NULL));
max = tal_count(orig_p);
p = orig_p;
assert((!fromwire_n2(&p, &max, tlv_n2) && !p) ||
!n2_is_valid(tlv_n2, NULL));
}
}
for (size_t i = 0; i < ARRAY_SIZE(invalid_streams_n1); i++) {
for (size_t j = 0; j < ARRAY_SIZE(valid_streams); j++) {
struct tlv_n1 *tlv_n1 = tlv_n1_new(tmpctx);
const u8 *p;
size_t max;
p = stream2(tmpctx, valid_streams[j].hex,
invalid_streams_n1[i].hex);
max = tal_count(p);
assert((!fromwire_n1(&p, &max, tlv_n1) && !p) ||
!n1_is_valid(tlv_n1, NULL));
}
}
for (size_t i = 0; i < ARRAY_SIZE(invalid_streams_n1_combo); i++) {
for (size_t j = 0; j < ARRAY_SIZE(valid_streams); j++) {
struct tlv_n1 *tlv_n1 = tlv_n1_new(tmpctx);
const u8 *p;
size_t max;
p = stream2(tmpctx, valid_streams[j].hex,
invalid_streams_n1_combo[i].hex);
max = tal_count(p);
assert((!fromwire_n1(&p, &max, tlv_n1) && !p) ||
!n1_is_valid(tlv_n1, NULL));
}
}
/* BOLT #1:
*
* Any appending of a higher-numbered valid stream to a lower-numbered
* valid stream should not trigger a decoding failure.
*/
for (size_t i = 0; i < ARRAY_SIZE(valid_streams); i++) {
for (size_t j = i+1; j < ARRAY_SIZE(valid_streams); j++) {
struct tlv_n1 *tlv_n1 = tlv_n1_new(tmpctx);
const u8 *orig_p, *p;
size_t max;
bool expect_success;
if (strlen(valid_streams[i].hex) == 0
|| strlen(valid_streams[j].hex) == 0)
continue;
orig_p = stream2(tmpctx, valid_streams[i].hex,
valid_streams[j].hex);
max = tal_count(orig_p);
p = orig_p;
/* This comparison works for our simple cases. */
expect_success = pull_type(valid_streams[i].hex)
< pull_type(valid_streams[j].hex);
assert(fromwire_n1(&p, &max, tlv_n1) &&
n1_is_valid(tlv_n1, NULL) == expect_success);
if (!expect_success)
continue;
/* Re-encoding should give the same results (except
* ignored fields tests!) */
if (ignored_fields(&valid_streams[i].expect)
|| ignored_fields(&valid_streams[j].expect))
continue;
u8 *p2 = tal_arr(tmpctx, u8, 0);
towire_n1(&p2, tlv_n1);
assert(memeq(orig_p, tal_count(orig_p),
p2, tal_count(p2)));
}
}
common_shutdown();
}