| 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.dnslabel; |
| 12 |
|
| 13 |
import static org.junit.jupiter.api.Assertions.assertEquals; |
| 14 |
import static org.junit.jupiter.api.Assertions.assertTrue; |
| 15 |
|
| 16 |
import org.junit.jupiter.api.Test; |
| 17 |
|
| 18 |
import org.minidns.idna.MiniDnsIdna; |
| 19 |
|
| 20 |
public class DnsLabelTest { |
| 21 |
|
| 22 |
@Test |
| 23 |
public void simpleNonReservedLdhLabelTest() { |
| 24 |
final String nonReservedLdhLabelString = "test"; |
| 25 |
final DnsLabel label = DnsLabel.from(nonReservedLdhLabelString); |
| 26 |
|
| 27 |
assertEquals(nonReservedLdhLabelString, label.label); |
| 28 |
assertTrue(label instanceof NonReservedLdhLabel); |
| 29 |
assertEquals("NonReservedLdhLabel", label.getLabelType()); |
| 30 |
} |
| 31 |
|
| 32 |
@Test |
| 33 |
public void aLabelTest() { |
| 34 |
final String uLabelString = "müller"; |
| 35 |
final String aLabelString = MiniDnsIdna.toASCII(uLabelString); |
| 36 |
final DnsLabel label = DnsLabel.from(aLabelString); |
| 37 |
|
| 38 |
assertEquals(aLabelString, label.label); |
| 39 |
assertTrue(label instanceof ALabel); |
| 40 |
assertEquals(uLabelString, label.getInternationalizedRepresentation()); |
| 41 |
assertEquals("ALabel", label.getLabelType()); |
| 42 |
} |
| 43 |
|
| 44 |
@Test |
| 45 |
public void fakeALabelTest() { |
| 46 |
final String fakeALabelString = "xn--mller-va"; |
| 47 |
final DnsLabel label = DnsLabel.from(fakeALabelString); |
| 48 |
|
| 49 |
assertEquals(fakeALabelString, label.label); |
| 50 |
assertTrue(label instanceof FakeALabel); |
| 51 |
assertEquals("FakeALabel", label.getLabelType()); |
| 52 |
} |
| 53 |
|
| 54 |
@Test |
| 55 |
public void underscoreLabelTest() { |
| 56 |
final String underscoreLabelString = "_tcp"; |
| 57 |
final DnsLabel label = DnsLabel.from(underscoreLabelString); |
| 58 |
|
| 59 |
assertEquals(underscoreLabelString, label.label); |
| 60 |
assertTrue(label instanceof UnderscoreLabel); |
| 61 |
assertEquals("UnderscoreLabel", label.getLabelType()); |
| 62 |
} |
| 63 |
|
| 64 |
@Test |
| 65 |
public void leadingHyphenLabelTest() { |
| 66 |
final String leadingHyphenLabelString = "-foo"; |
| 67 |
final DnsLabel label = DnsLabel.from(leadingHyphenLabelString); |
| 68 |
|
| 69 |
assertEquals(leadingHyphenLabelString, label.label); |
| 70 |
assertTrue(label instanceof LeadingOrTrailingHyphenLabel); |
| 71 |
assertEquals("LeadingOrTrailingHyphenLabel", label.getLabelType()); |
| 72 |
} |
| 73 |
|
| 74 |
@Test |
| 75 |
public void trailingHyphenLabelTest() { |
| 76 |
final String trailingHyphenLabelString = "bar-"; |
| 77 |
final DnsLabel label = DnsLabel.from(trailingHyphenLabelString); |
| 78 |
|
| 79 |
assertEquals(trailingHyphenLabelString, label.label); |
| 80 |
assertTrue(label instanceof LeadingOrTrailingHyphenLabel); |
| 81 |
assertEquals("LeadingOrTrailingHyphenLabel", label.getLabelType()); |
| 82 |
} |
| 83 |
|
| 84 |
@Test |
| 85 |
public void otherNonLdhLabelTest() { |
| 86 |
final String otherNonLdhLabelString = "w@$abi"; |
| 87 |
final DnsLabel label = DnsLabel.from(otherNonLdhLabelString); |
| 88 |
|
| 89 |
assertEquals(otherNonLdhLabelString, label.label); |
| 90 |
assertTrue(label instanceof OtherNonLdhLabel); |
| 91 |
assertEquals("OtherNonLdhLabel", label.getLabelType()); |
| 92 |
} |
| 93 |
|
| 94 |
@Test |
| 95 |
public void dnsLabelWildcardStringTest() { |
| 96 |
assertEquals("*", DnsLabel.WILDCARD_LABEL.toString()); |
| 97 |
} |
| 98 |
} |