-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathSVCBRecordTest.java
More file actions
602 lines (516 loc) · 18.8 KB
/
Copy pathSVCBRecordTest.java
File metadata and controls
602 lines (516 loc) · 18.8 KB
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
// SPDX-License-Identifier: BSD-3-Clause
package org.xbill.DNS;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.Test;
public class SVCBRecordTest {
@Test
@SuppressWarnings("deprecation")
void createParams() throws UnknownHostException, TextParseException {
List<Integer> mandatoryList = Arrays.asList(SVCBRecord.ALPN, SVCBRecord.IPV4HINT);
SVCBRecord.ParameterMandatory mandatory = new SVCBBase.ParameterMandatory(mandatoryList);
assertEquals(SVCBRecord.MANDATORY, mandatory.getKey());
assertEquals(mandatoryList, mandatory.getValues());
List<String> alpnList = Arrays.asList("h2", "h3");
SVCBRecord.ParameterAlpn alpn = new SVCBRecord.ParameterAlpn(alpnList);
assertEquals(SVCBRecord.ALPN, alpn.getKey());
assertEquals(alpnList, alpn.getValues());
SVCBRecord.ParameterPort port = new SVCBBase.ParameterPort(8443);
assertEquals(SVCBRecord.PORT, port.getKey());
assertEquals(8443, port.getPort());
List<Inet4Address> ipv4List =
Collections.singletonList((Inet4Address) InetAddress.getByName("1.2.3.4"));
SVCBRecord.ParameterIpv4Hint ipv4hint = new SVCBRecord.ParameterIpv4Hint(ipv4List);
assertEquals(SVCBRecord.IPV4HINT, ipv4hint.getKey());
assertEquals(ipv4List, ipv4hint.getAddresses());
byte[] data = {'a', 'b', 'c'};
SVCBBase.ParameterEch ech = new SVCBBase.ParameterEch(data);
assertEquals(SVCBRecord.ECH, ech.getKey());
assertEquals(data, ech.getData());
SVCBRecord.ParameterEchConfig echconfig = new SVCBRecord.ParameterEchConfig(data);
assertEquals(SVCBRecord.ECHCONFIG, echconfig.getKey());
assertEquals(data, echconfig.getData());
List<Inet6Address> ipv6List =
Collections.singletonList((Inet6Address) InetAddress.getByName("2001:db8::1"));
SVCBRecord.ParameterIpv6Hint ipv6hint = new SVCBRecord.ParameterIpv6Hint(ipv6List);
assertEquals(SVCBRecord.IPV6HINT, ipv6hint.getKey());
assertEquals(ipv6List, ipv6hint.getAddresses());
byte[] value = {0, 1, 2, 3};
SVCBRecord.ParameterUnknown unknown = new SVCBRecord.ParameterUnknown(33, value);
assertEquals(33, unknown.getKey());
assertEquals(value, unknown.getValue());
}
@Test
void createRecord() throws IOException {
Name label = Name.fromString("test.com.");
int svcPriority = 5;
Name svcDomain = Name.fromString("svc.test.com.");
SVCBRecord.ParameterMandatory mandatory = new SVCBRecord.ParameterMandatory();
mandatory.fromString("alpn");
SVCBRecord.ParameterAlpn alpn = new SVCBRecord.ParameterAlpn();
alpn.fromString("h1,h2");
SVCBRecord.ParameterIpv4Hint ipv4 = new SVCBRecord.ParameterIpv4Hint();
ipv4.fromString("1.2.3.4,5.6.7.8");
List<SVCBRecord.ParameterBase> params = Arrays.asList(mandatory, ipv4, alpn);
SVCBRecord svcbRecord = new SVCBRecord(label, DClass.IN, 300, svcPriority, svcDomain, params);
assertEquals(Type.SVCB, svcbRecord.getType());
assertEquals(label, svcbRecord.getName());
assertEquals(svcPriority, svcbRecord.getSvcPriority());
assertEquals(svcDomain, svcbRecord.getTargetName());
assertEquals(
Arrays.asList(SVCBRecord.MANDATORY, SVCBRecord.ALPN, SVCBRecord.IPV4HINT).toString(),
svcbRecord.getSvcParamKeys().toString());
assertEquals("alpn", svcbRecord.getSvcParamValue(SVCBRecord.MANDATORY).toString());
assertEquals("h1,h2", svcbRecord.getSvcParamValue(SVCBRecord.ALPN).toString());
assertEquals("h1,h2", svcbRecord.getSvcParamValue(SVCBRecord.ALPN).toString());
assertNull(svcbRecord.getSvcParamValue(1234));
Options.unset("BINDTTL");
Options.unset("noPrintIN");
assertEquals(
"test.com.\t\t300\tIN\tSVCB\t5 svc.test.com. mandatory=alpn alpn=h1,h2 ipv4hint=1.2.3.4,5.6.7.8",
svcbRecord.toString());
}
@Test
void createRecordDuplicateParam() throws IOException {
Name label = Name.fromString("test.com.");
Name svcDomain = Name.fromString("svc.test.com.");
SVCBRecord.ParameterAlpn alpn = new SVCBRecord.ParameterAlpn();
alpn.fromString("h1,h2");
SVCBRecord.ParameterIpv4Hint ipv4 = new SVCBRecord.ParameterIpv4Hint();
ipv4.fromString("1.2.3.4,5.6.7.8");
List<SVCBRecord.ParameterBase> params = Arrays.asList(alpn, ipv4, alpn);
assertThrows(
IllegalArgumentException.class,
() -> new SVCBRecord(label, DClass.IN, 300, 5, svcDomain, params));
}
@Test
void aliasMode() throws IOException {
String str = "0 a.b.c.";
byte[] bytes = stringToWire(str);
byte[] expected = new byte[] {0, 0, 1, 'a', 1, 'b', 1, 'c', 0};
assertArrayEquals(expected, bytes);
assertEquals(str, wireToString(bytes));
}
@Test
void serviceModeWithoutParameters() throws IOException {
String str = "1 .";
byte[] bytes = stringToWire(str);
byte[] expected = new byte[] {0, 1, 0};
assertArrayEquals(expected, bytes);
assertEquals(str, wireToString(bytes));
}
@Test
void serviceModePort() throws IOException {
String str = "1 . port=8443";
byte[] bytes = stringToWire(str);
byte[] expected = new byte[] {0, 1, 0, 0, 3, 0, 2, 0x20, (byte) 0xFB};
assertArrayEquals(expected, bytes);
assertEquals(str, wireToString(bytes));
}
@Test
void serviceModeAlpn() throws IOException {
String str = "1 . alpn=h3";
assertEquals(str, stringToWireToString(str));
}
@Test
void serviceModeNoDefaultAlpn() throws IOException {
String str = "1 . no-default-alpn";
assertEquals(str, stringToWireToString(str));
}
@Test
void serviceModeMultiKey() throws IOException {
String str = "1 . alpn=h3 no-default-alpn";
assertEquals(str, stringToWireToString(str));
}
@Test
void serviceModeIntKey() throws IOException {
String str = "1 . 1=h3";
assertEquals("1 . alpn=h3", stringToWireToString(str));
}
@Test
void serviceModeMultiValue() throws IOException {
String str = "1 . alpn=h2,h3";
byte[] bytes = stringToWire(str);
byte[] expected = new byte[] {0, 1, 0, 0, 1, 0, 6, 2, 'h', '2', 2, 'h', '3'};
assertArrayEquals(expected, bytes);
assertEquals(str, wireToString(bytes));
}
@Test
void serviceModeQuotedValue() throws IOException {
String str = "1 . alpn=\"h2,h3\"";
assertEquals("1 . alpn=h2,h3", stringToWireToString(str));
}
@Test
void serviceModeQuotedEscapedValue() throws IOException {
String str = "1 . alpn=\"h2\\,h3,h\\\\4\"";
String expectedStr = "1 . alpn=h2\\,h3,h\\\\4";
byte[] bytes = stringToWire(str);
byte[] expectedBytes =
new byte[] {0, 1, 0, 0, 1, 0, 10, 5, 104, 50, 44, 104, 51, 3, 104, '\\', 52};
assertArrayEquals(bytes, expectedBytes);
assertEquals(expectedStr, wireToString(bytes));
}
@Test
void serviceModeAlpnEscapedBytes() throws IOException {
String str = "1 . alpn=http/1.1,\\001aa\\003\\b,h2";
String expectedStr = "1 . alpn=http/1.1,\\001aa\\003b,h2";
byte[] bytes = stringToWire(str);
byte[] expectedBytes =
new byte[] {
0, 1, 0, 0, 1, 0, 18, 8, 104, 116, 116, 112, 47, 49, 46, 49, 5, 1, 97, 97, 3, 98, 2, 104,
50
};
assertArrayEquals(bytes, expectedBytes);
assertEquals(expectedStr, wireToString(bytes));
}
@Test
void serviceModeMandatoryAndOutOfOrder() throws IOException {
String str = "1 . alpn=h3 no-default-alpn mandatory=alpn";
assertEquals("1 . mandatory=alpn alpn=h3 no-default-alpn", stringToWireToString(str));
}
@Test
void serviceModeEscapedDomain() throws IOException {
String str = "1 dotty\\.lotty.example.com. no-default-alpn";
assertEquals(str, stringToWireToString(str));
}
@Test
void serviceModeEch() throws IOException {
String str = "1 h3pool. ech=1234";
assertEquals(str, stringToWireToString(str));
}
@Test
void serviceModeEchMulti() throws IOException {
String str = "1 h3pool. alpn=h2,h3 ech=1234";
assertEquals(str, stringToWireToString(str));
}
@Test
void serviceModeEchOutOfOrder() throws IOException {
String str = "1 h3pool. ech=1234 alpn=h2,h3";
assertEquals("1 h3pool. alpn=h2,h3 ech=1234", stringToWireToString(str));
}
@Test
void serviceModeEchQuoted() throws IOException {
String str = "1 h3pool. alpn=h2,h3 ech=\"1234\"";
assertEquals("1 h3pool. alpn=h2,h3 ech=1234", stringToWireToString(str));
}
@Test
void serviceModeObsoleteEchConfigName() throws IOException {
String str = "1 . echconfig=1234";
assertEquals("1 . ech=1234", stringToWireToString(str));
}
@Test
void serviceModeIpv4Hint() throws IOException {
String str = "3 . ipv4hint=4.5.6.7";
assertEquals(str, stringToWireToString(str));
}
@Test
void serviceModeIpv4HintList() throws IOException {
String str = "5 . ipv4hint=4.5.6.7,8.9.1.2";
byte[] bytes = stringToWire(str);
byte[] expected = new byte[] {0, 5, 0, 0, 4, 0, 8, 4, 5, 6, 7, 8, 9, 1, 2};
assertArrayEquals(expected, bytes);
assertEquals(str, wireToString(bytes));
}
@Test
void serviceModeIpv4HintQuoted() throws IOException {
String str = "5 . ipv4hint=\"4.5.6.7,8.9.1.2\"";
assertEquals("5 . ipv4hint=4.5.6.7,8.9.1.2", stringToWireToString(str));
}
@Test
void serviceModeIpv4HintMultiKey() throws IOException {
String str = "7 . alpn=h2 ipv4hint=4.5.6.7";
assertEquals(str, stringToWireToString(str));
}
@Test
void serviceModeIpv6Hint() throws IOException {
String str = "9 . ipv6hint=2001:db8::1";
assertEquals("9 . ipv6hint=2001:db8:0:0:0:0:0:1", stringToWireToString(str));
}
@Test
void serviceModeIpv6HintMulti() throws IOException {
String str = "2 . alpn=h2 ipv6hint=2001:db8::1,2001:db8::2";
assertEquals(
"2 . alpn=h2 ipv6hint=2001:db8:0:0:0:0:0:1,2001:db8:0:0:0:0:0:2",
stringToWireToString(str));
}
@Test
void serviceModeUnknownKey() throws IOException {
String str = "6 . key12345=abcdefg\\012";
assertEquals(str, stringToWireToString(str));
}
@Test
void serviceModeUnknownKeyBytes() throws IOException {
String str = "8 . key23456=\\000\\001\\002\\003";
byte[] bytes = stringToWire(str);
byte[] expected = new byte[] {0, 8, 0, 0x5B, (byte) 0xA0, 0, 4, 0, 1, 2, 3};
assertArrayEquals(expected, bytes);
assertEquals(str, wireToString(bytes));
}
@Test
void serviceModeUnknownKeyEscapedChars() throws IOException {
String str = "1 . key29=a\\b\\c";
assertEquals("1 . key29=abc", stringToWireToString(str));
}
@Test
void serviceModeUnknownKeyEscapedSlash() throws IOException {
String str = "65535 . key29=a\\\\b\\\\c";
assertEquals(str, stringToWireToString(str));
}
@Test
void serviceModeUnknownHighKey() throws IOException {
String str = "65535 . key65535=abcdefg";
assertEquals(str, stringToWireToString(str));
}
@Test
void serviceModeUnknownKeyNoValue() throws IOException {
String str = "65535 . key65535";
assertEquals(str, stringToWireToString(str));
}
@Test
void masterFormatParsing() throws IOException {
String str =
"test.net. 86400 IN SOA test.net. test.net. 2020100900 3600 600 604800 300\n"
+ "test.net. 86400 IN NS ns1.test.net.\n"
+ "test.net. 300 IN HTTPS 0 www.test.net.\n"
+ "test.net. 300 IN SVCB 1 . alpn=h2\n"
+ "test.net. 300 IN HTTPS 1 .\n"
+ "www.test.net. 300 IN A 1.2.3.4\n";
Master m = new Master(new ByteArrayInputStream(str.getBytes()));
Record r = m.nextRecord();
assertEquals(Type.SOA, r.getType());
r = m.nextRecord();
assertEquals(Type.NS, r.getType());
r = m.nextRecord();
assertEquals(Type.HTTPS, r.getType());
assertEquals("0 www.test.net.", r.rdataToString());
r = m.nextRecord();
assertEquals(Type.SVCB, r.getType());
assertEquals("1 . alpn=h2", r.rdataToString());
r = m.nextRecord();
assertEquals(Type.HTTPS, r.getType());
assertEquals("1 .", r.rdataToString());
r = m.nextRecord();
assertEquals(Type.A, r.getType());
assertEquals("1.2.3.4", r.rdataToString());
r = m.nextRecord();
assertNull(r);
}
@Test
void invalidText() {
String str = "these are all garbage strings that should fail";
assertThrows(TextParseException.class, () -> stringToWire(str));
}
@Test
void extraQuotesInParamValues() {
String str = "5 . ipv4hint=\"4.5.6.7\",\"8.9.1.2\"";
assertThrows(TextParseException.class, () -> stringToWire(str));
}
@Test
void aliasModeWithParameters() {
String str = "0 . alpn=h3";
assertThrows(TextParseException.class, () -> stringToWire(str));
}
@Test
void zeroLengthMandatory() {
String str = "1 . mandatory";
assertThrows(TextParseException.class, () -> stringToWire(str));
}
@Test
void zeroLengthAlpnValue() {
String str = "1 . alpn";
assertThrows(TextParseException.class, () -> stringToWire(str));
}
@Test
void zeroLengthPortValue() {
String str = "1 . port";
assertThrows(TextParseException.class, () -> stringToWire(str));
}
@Test
void zeroLengthIpv4Hint() {
String str = "1 . ipv4hint";
assertThrows(TextParseException.class, () -> stringToWire(str));
}
@Test
void zeroLengthEch() {
String str = "1 . ech";
assertThrows(TextParseException.class, () -> stringToWire(str));
}
@Test
void zeroLengthIpv6Hint() {
String str = "1 . ipv6hint";
assertThrows(TextParseException.class, () -> stringToWire(str));
}
@Test
void emptyKey() {
String str = "1 . =1234";
assertThrows(TextParseException.class, () -> stringToWire(str));
}
@Test
void emptyValue() {
String str = "1 . alpn=";
assertThrows(TextParseException.class, () -> stringToWire(str));
}
@Test
void emptyKeyAndValue() {
String str = "1 . =";
assertThrows(TextParseException.class, () -> stringToWire(str));
}
@Test
void unknownKey() {
String str = "1 . sport=8443";
assertThrows(TextParseException.class, () -> stringToWire(str));
}
@Test
void mandatoryListWithSelf() {
String str = "1 . mandatory=alpn,mandatory alpn=h1";
assertThrows(TextParseException.class, () -> stringToWire(str));
}
@Test
void mandatoryListWithDuplicate() {
String str = "1 . mandatory=alpn,ipv4hint,alpn alpn=h1 ipv4hint=1.2.3.4";
assertThrows(TextParseException.class, () -> stringToWire(str));
}
@Test
void mandatoryListWithMissingParam() {
String str = "1 . mandatory=alpn,ipv4hint alpn=h1";
assertThrows(TextParseException.class, () -> stringToWire(str));
}
@Test
void portValueTooLarge() {
String str = "1 . port=84438";
assertThrows(IllegalArgumentException.class, () -> stringToWire(str));
}
@Test
void portValueCharAfterInt() {
String str = "1 . port=443a";
assertThrows(IllegalArgumentException.class, () -> stringToWire(str));
}
@Test
void noDefaultAlpnWithValue() {
String str = "1 . no-default-alpn=true";
assertThrows(TextParseException.class, () -> stringToWire(str));
}
@Test
void emptyString() {
String str = "";
assertThrows(TextParseException.class, () -> stringToWire(str));
}
@Test
void svcPriorityTooHigh() {
String str = "65536 . port=443";
assertThrows(TextParseException.class, () -> stringToWire(str));
}
@Test
void invalidPortKey() {
String str = "1 . port<5";
assertThrows(TextParseException.class, () -> stringToWire(str));
}
@Test
void invalidSvcDomain() {
String str = "1 fred..harvey port=80";
assertThrows(TextParseException.class, () -> stringToWire(str));
}
@Test
void duplicateParamKey() {
String str = "1 . alpn=h2 alpn=h3";
assertThrows(TextParseException.class, () -> stringToWire(str));
}
@Test
void invalidIpv4Hint() {
String str = "1 . ipv4hint=2001:db8::1";
assertThrows(TextParseException.class, () -> stringToWire(str));
}
@Test
void invalidIpv6Hint() {
String str = "1 . ipv6hint=1.2.3.4";
assertThrows(TextParseException.class, () -> stringToWire(str));
}
@Test
void negativeSvcPriority() {
String str = "-1 . port=80";
assertThrows(TextParseException.class, () -> stringToWire(str));
}
@Test
void svcParamUnknownKeyTooHigh() {
String str = "65535 . key65536=abcdefg";
assertThrows(TextParseException.class, () -> stringToWire(str));
}
@Test
void svcParamUnknownKeyCharAfterInt() {
String str = "65535 . key123a=abcdefg";
assertThrows(TextParseException.class, () -> stringToWire(str));
}
@Test
void invalidSvcParamKey() {
String str = "65535 . keyBlooie=abcdefg";
assertThrows(TextParseException.class, () -> stringToWire(str));
}
@Test
void wireFormatTooShort() {
byte[] wire = new byte[] {0, 1, 0, 0, 1, 0, 10};
assertThrows(WireParseException.class, () -> wireToString(wire));
}
@Test
void wireFormatTooLong() {
byte[] wire = new byte[] {0, 0, 0, 1};
assertThrows(WireParseException.class, () -> wireToString(wire));
}
@Test
void wireFormatMandatoryTooLong() {
byte[] wire = new byte[] {0, 1, 0, 0, 0, 0, 3, 0, 1, 55};
assertThrows(WireParseException.class, () -> wireToString(wire));
}
@Test
void wireFormatAlpnTooShort() {
byte[] wire = new byte[] {0, 1, 0, 0, 1, 0, 3, 10, 1, 55};
assertThrows(WireParseException.class, () -> wireToString(wire));
}
@Test
void wireFormatNoDefaultAlpnTooLong() {
byte[] wire = new byte[] {0, 1, 0, 0, 2, 0, 1, 0};
assertThrows(WireParseException.class, () -> wireToString(wire));
}
@Test
void wireFormatPortTooLong() {
byte[] wire = new byte[] {0, 1, 0, 0, 3, 0, 4, 0, 0, 0, 0};
assertThrows(WireParseException.class, () -> wireToString(wire));
}
@Test
void wireFormatIpv4HintTooLong() {
byte[] wire = new byte[] {0, 1, 0, 0, 4, 0, 5, 1, 2, 3, 4, 5};
assertThrows(WireParseException.class, () -> wireToString(wire));
}
@Test
void wireFormatIpv6HintTooShort() {
byte[] wire = new byte[] {0, 1, 0, 0, 6, 0, 2, 1, 2};
assertThrows(WireParseException.class, () -> wireToString(wire));
}
public static byte[] stringToWire(String str) throws IOException {
Tokenizer t = new Tokenizer(str);
SVCBRecord svcbRecord = new SVCBRecord();
svcbRecord.rdataFromString(t, null);
DNSOutput out = new DNSOutput();
svcbRecord.rrToWire(out, null, true);
return out.toByteArray();
}
public static String wireToString(byte[] bytes) throws IOException {
DNSInput in = new DNSInput(bytes);
SVCBRecord svcbRecord = new SVCBRecord();
svcbRecord.rrFromWire(in);
return svcbRecord.rdataToString();
}
public static String stringToWireToString(String str) throws IOException {
return wireToString(stringToWire(str));
}
}