| 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.util; |
| 12 |
|
| 13 |
import static org.junit.jupiter.api.Assertions.assertEquals; |
| 14 |
|
| 15 |
import java.net.Inet4Address; |
| 16 |
import java.net.Inet6Address; |
| 17 |
|
| 18 |
import org.junit.jupiter.api.Test; |
| 19 |
import org.minidns.dnsname.DnsName; |
| 20 |
|
| 21 |
public class InetAddressUtilTest { |
| 22 |
|
| 23 |
// @formatter:off |
| 24 |
private static final String[] VALID_IPV6 = new String[] { |
| 25 |
"2a03:4000:2:2f5::1", |
| 26 |
"2001:0db8:85a3:08d3:1319:8a2e:0370:7344" |
| 27 |
}; |
| 28 |
// @formatter:on |
| 29 |
|
| 30 |
// @formatter:off |
| 31 |
private static final String[] ZERO_COMPRESSED_IPV6 = new String[] { |
| 32 |
"2001:db8:0:0:0::1", |
| 33 |
"2001:db8:0:0::1", |
| 34 |
"2001:db8:0::1", |
| 35 |
"2001:db8::1" |
| 36 |
}; |
| 37 |
// @formatter:on |
| 38 |
|
| 39 |
// @formatter:off |
| 40 |
private static final String[] VALID_IPV4 = new String[] { |
| 41 |
"192.168.0.1", |
| 42 |
"127.0.0.1" |
| 43 |
}; |
| 44 |
// @formatter:on |
| 45 |
|
| 46 |
// @formatter:off |
| 47 |
private static final String[] INVALID_IP = new String[] { |
| 48 |
"2001:0db8:85a3:08d3:1319:8a2e:0370:7344:3212", |
| 49 |
"2001:db8:0:0:1", |
| 50 |
"0.0.1", |
| 51 |
"1.2.3.4.5", |
| 52 |
"foo.example", |
| 53 |
"foo.bar.baz.example", |
| 54 |
}; |
| 55 |
// @formatter:on |
| 56 |
|
| 57 |
@Test |
| 58 |
public void testValidIpv6() { |
| 59 |
assertAllValidIpv6(VALID_IPV6); |
| 60 |
assertAllValidIpv6(ZERO_COMPRESSED_IPV6); |
| 61 |
} |
| 62 |
|
| 63 |
@Test |
| 64 |
public void testInvalidIpv6() { |
| 65 |
assertAllInvalidIpv6(INVALID_IP); |
| 66 |
} |
| 67 |
|
| 68 |
@Test |
| 69 |
public void testValidIpv4() { |
| 70 |
assertAllValidIpv4(VALID_IPV4); |
| 71 |
} |
| 72 |
|
| 73 |
@Test |
| 74 |
public void testInvalidIpv4() { |
| 75 |
assertAllInvalidIpv4(INVALID_IP); |
| 76 |
} |
| 77 |
|
| 78 |
private static void assertAllValidIpv6(String... addresses) { |
| 79 |
for (String address : addresses) { |
| 80 |
if (!InetAddressUtil.isIpV6Address(address)) { |
| 81 |
throw new AssertionError(address + " is not a valid IPv6 Address"); |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
private static void assertAllValidIpv4(String... addresses) { |
| 87 |
for (String address : addresses) { |
| 88 |
if (!InetAddressUtil.isIpV4Address(address)) { |
| 89 |
throw new AssertionError(address + " is not a valid IPv4 Address"); |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
private static void assertAllInvalidIpv6(String... addresses) { |
| 95 |
for (String address : addresses) { |
| 96 |
if (InetAddressUtil.isIpV6Address(address)) { |
| 97 |
throw new AssertionError( |
| 98 |
address + " is believed to be valid IPv6 Address by isIpv6Address(), although it should not be one."); |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
private static void assertAllInvalidIpv4(String... addresses) { |
| 104 |
for (String address : addresses) { |
| 105 |
if (InetAddressUtil.isIpV4Address(address)) { |
| 106 |
throw new AssertionError( |
| 107 |
address + " is believed to be valid IPv4 Address by isIpv4Address(), although it should not be one"); |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
@Test |
| 113 |
public void testReverseInet6Address() { |
| 114 |
Inet6Address inet6Address = InetAddressUtil.ipv6From(VALID_IPV6[0]); |
| 115 |
DnsName reversedIpv6Address = InetAddressUtil.reverseIpAddressOf(inet6Address); |
| 116 |
assertEquals(DnsName.from("3.0.a.2.0.0.0.4.2.0.0.0.5.f.2.0.0.0.0.0.0.0.0.0.0.0.0.0.1.0.0.0"), reversedIpv6Address); |
| 117 |
} |
| 118 |
|
| 119 |
@Test |
| 120 |
public void testReverseInet4Address() { |
| 121 |
Inet4Address inet4Address = InetAddressUtil.ipv4From(VALID_IPV4[0]); |
| 122 |
DnsName reversedIpv4Address = InetAddressUtil.reverseIpAddressOf(inet4Address); |
| 123 |
assertEquals(DnsName.from("1.0.168.192"), reversedIpv4Address); |
| 124 |
} |
| 125 |
} |