Debian Codegraph @

minidns-core/src/test/java/org/minidns/dnsname/DnsNameTest.java

raw
1 /*
2 * Copyright 2015-2020 the original author or authors
3 *
4 * This software is licensed under the Apache License, Version 2.0,
5 * the GNU Lesser General Public License version 2 or later ("LGPL")
6 * and the WTFPL.
7 * You may choose either license to govern your use of this software only
8 * upon the condition that you accept all of the terms of either
9 * the Apache License 2.0, the LGPL 2.1+ or the WTFPL.
10 */
11 package org.minidns.dnsname;
12
13 import static org.minidns.Assert.assertCsEquals;
14 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
15 import static org.junit.jupiter.api.Assertions.assertEquals;
16 import static org.junit.jupiter.api.Assertions.assertThrows;
17
18 import java.io.ByteArrayInputStream;
19 import java.io.DataInputStream;
20 import java.io.IOException;
21
22 import org.junit.jupiter.api.Test;
23
24 import org.minidns.dnslabel.DnsLabel;
25
26 public class DnsNameTest {
27
28 @Test
29 public void sizeTest() {
30 assertEquals(1, DnsName.from("").size());
31 assertEquals(13, DnsName.from("example.com").size());
32 assertEquals(16, DnsName.from("dömäin").size());
33 assertEquals(24, DnsName.from("dömäin.example").size());
34 }
35
36 @Test
37 public void toByteArrayTest() {
38 assertArrayEquals(new byte[] {0}, DnsName.from("").getBytes());
39 assertArrayEquals(new byte[] {7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 0}, DnsName.from("example").getBytes());
40 assertArrayEquals(new byte[] {7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 3, 'c', 'o', 'm', 0}, DnsName.from("example.com").getBytes());
41 assertArrayEquals(new byte[] {14, 'x', 'n', '-', '-', 'd', 'm', 'i', 'n', '-', 'm', 'o', 'a', '0', 'i', 0}, DnsName.from("dömäin").getBytes());
42 }
43
44 @Test
45 public void parseTest() throws IOException {
46 byte[] test = new byte[] {7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 0};
47 assertCsEquals("example", DnsName.parse(new DataInputStream(new ByteArrayInputStream(test)), test));
48 test = new byte[] {7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 3, 'c', 'o', 'm', 0};
49 assertCsEquals("example.com", DnsName.parse(new DataInputStream(new ByteArrayInputStream(test)), test));
50 }
51
52 @Test
53 public void equalsTest() {
54 assertEquals(DnsName.from(""), DnsName.from("."));
55 }
56
57 @Test
58 public void testStripToParts() {
59 assertCsEquals(DnsName.from("www.example.com"), DnsName.from("www.example.com").stripToLabels(3));
60 assertCsEquals(DnsName.from("example.com"), DnsName.from("www.example.com").stripToLabels(2));
61 assertCsEquals(DnsName.from("com"), DnsName.from("www.example.com").stripToLabels(1));
62 assertCsEquals(DnsName.from("."), DnsName.from("www.example.com").stripToLabels(0));
63 }
64
65 @Test
66 public void testStripToPartsIllegal() {
67 assertThrows(IllegalArgumentException.class, () ->
68 DnsName.from("").stripToLabels(1)
69 );
70 }
71
72 @Test
73 public void testStripToPartsIllegalLong() {
74 assertThrows(IllegalArgumentException.class, () ->
75 DnsName.from("example.com").stripToLabels(3)
76 );
77 }
78
79 @Test
80 public void testConcact() {
81 String leftString = "foo.bar.de";
82 String rightString = "example.org";
83 DnsName left = DnsName.from(leftString);
84 DnsName right = DnsName.from(rightString);
85
86 DnsName concated = DnsName.from(left, right);
87 DnsName expected = DnsName.from(leftString + '.' + rightString);
88 assertEquals(expected, concated);
89 }
90
91 @Test
92 public void testFromVarargs() {
93 String leftString = "leftmost.left";
94 String middleString = "leftMiddle.middle.rightMiddle";
95 String rightString = "right.rightMost";
96 DnsName left = DnsName.from(leftString);
97 DnsName middle = DnsName.from(middleString);
98 DnsName right = DnsName.from(rightString);
99
100 DnsName name = DnsName.from(left, middle, right);
101
102 String completeString = leftString + '.' + middleString + '.' + rightString;
103 assertEquals(name.getRawAce(), completeString);
104
105 DnsName expected = DnsName.from(completeString);
106 assertEquals(name, expected);
107 }
108
109 @Test
110 public void caseInsenstiveCompare() {
111 DnsName lowercase = DnsName.from("cs.fau.de");
112 DnsName uppercase = DnsName.from("CS.fau.de");
113
114 assertEquals(lowercase, uppercase);
115 }
116
117 @Test
118 public void rawFieldsKeepCase() {
119 String mixedCaseDnsName = "UP.low.UP.low.UP";
120 DnsName mixedCase = DnsName.from(mixedCaseDnsName);
121
122 assertEquals(mixedCaseDnsName, mixedCase.getRawAce());
123 }
124
125 @Test
126 public void getLabelsTest() {
127 final String tldLabelString = "tld";
128 final String secondLevelString = "second-level-domain";
129 final String thirdLevelString = "third-level-domain";
130 final String dnsNameString = thirdLevelString + '.' + secondLevelString + '.' + tldLabelString;
131 final DnsName dnsName = DnsName.from(dnsNameString);
132
133 DnsLabel[] labels = dnsName.getLabels();
134 assertEquals(tldLabelString, labels[0].label);
135 assertEquals(secondLevelString, labels[1].label);
136 assertEquals(thirdLevelString, labels[2].label);
137 }
138
139 @Test
140 public void trailingDotDnsNameFromTest() {
141 final String trailingDotDnsName = "foo.bar.";
142 DnsName dnsName = DnsName.from(trailingDotDnsName);
143 assertEquals("foo.bar", dnsName.ace);
144 }
145
146 }