2012-08-21 02:20:21 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2012 The Netty Project
|
|
|
|
*
|
|
|
|
* The Netty Project licenses this file to you under the Apache License,
|
|
|
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
|
|
|
package io.netty.util;
|
|
|
|
|
2014-11-12 04:11:31 +01:00
|
|
|
import org.junit.Test;
|
2012-08-21 02:20:21 +02:00
|
|
|
|
2014-09-04 12:58:28 +02:00
|
|
|
import java.net.InetAddress;
|
|
|
|
import java.net.UnknownHostException;
|
2012-11-21 19:53:04 +01:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2013-01-10 07:23:58 +01:00
|
|
|
import java.util.Map.Entry;
|
2012-11-21 19:53:04 +01:00
|
|
|
|
2014-11-12 04:11:31 +01:00
|
|
|
import static org.junit.Assert.*;
|
2012-11-10 00:50:21 +01:00
|
|
|
|
2012-11-19 06:07:18 +01:00
|
|
|
public class NetUtilTest {
|
2014-11-12 04:11:31 +01:00
|
|
|
|
|
|
|
private static final class TestMap extends HashMap<String, String> {
|
|
|
|
private static final long serialVersionUID = -298642816998608473L;
|
|
|
|
|
|
|
|
TestMap(String... values) {
|
|
|
|
for (int i = 0; i < values.length; i += 2) {
|
|
|
|
String key = values[i];
|
|
|
|
String value = values[i + 1];
|
|
|
|
put(key, value);
|
|
|
|
}
|
2012-11-21 19:53:04 +01:00
|
|
|
}
|
2014-11-12 04:11:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static final Map<String, String> validIpV4Hosts = new TestMap(
|
|
|
|
"192.168.1.0", "c0a80100",
|
|
|
|
"10.255.255.254", "0afffffe",
|
|
|
|
"172.18.5.4", "ac120504",
|
|
|
|
"0.0.0.0", "00000000",
|
|
|
|
"127.0.0.1", "7f000001");
|
|
|
|
|
|
|
|
private static final Map<String, String> invalidIpV4Hosts = new TestMap(
|
|
|
|
"1.256.3.4", null,
|
|
|
|
"256.0.0.1", null,
|
|
|
|
"1.1.1.1.1", null,
|
|
|
|
"x.255.255.255", null,
|
|
|
|
"0.1:0.0", null,
|
|
|
|
"0.1.0.0:", null,
|
|
|
|
"127.0.0.", null,
|
|
|
|
"1.2..4", null,
|
|
|
|
"192.0.1", null,
|
|
|
|
"192.0.1.1.1", null,
|
|
|
|
"192.0.1.a", null,
|
|
|
|
"19a.0.1.1", null,
|
|
|
|
"a.0.1.1", null,
|
|
|
|
".0.1.1", null,
|
|
|
|
"...", null);
|
2014-11-12 04:28:04 +01:00
|
|
|
|
2014-11-12 04:11:31 +01:00
|
|
|
private static final Map<String, String> validIpV6Hosts = new TestMap(
|
|
|
|
"::ffff:5.6.7.8", "00000000000000000000ffff05060708",
|
|
|
|
"fdf8:f53b:82e4::53", "fdf8f53b82e400000000000000000053",
|
|
|
|
"fe80::200:5aee:feaa:20a2", "fe8000000000000002005aeefeaa20a2",
|
|
|
|
"2001::1", "20010000000000000000000000000001",
|
|
|
|
"2001:0000:4136:e378:8000:63bf:3fff:fdd2", "200100004136e378800063bf3ffffdd2",
|
|
|
|
"2001:0002:6c::430", "20010002006c00000000000000000430",
|
|
|
|
"2001:10:240:ab::a", "20010010024000ab000000000000000a",
|
|
|
|
"2002:cb0a:3cdd:1::1", "2002cb0a3cdd00010000000000000001",
|
|
|
|
"2001:db8:8:4::2", "20010db8000800040000000000000002",
|
|
|
|
"ff01:0:0:0:0:0:0:2", "ff010000000000000000000000000002",
|
|
|
|
"[fdf8:f53b:82e4::53]", "fdf8f53b82e400000000000000000053",
|
|
|
|
"[fe80::200:5aee:feaa:20a2]", "fe8000000000000002005aeefeaa20a2",
|
|
|
|
"[2001::1]", "20010000000000000000000000000001",
|
|
|
|
"[2001:0000:4136:e378:8000:63bf:3fff:fdd2]", "200100004136e378800063bf3ffffdd2",
|
|
|
|
"0:1:2:3:4:5:6:789a", "0000000100020003000400050006789a",
|
|
|
|
"0:1:2:3::f", "0000000100020003000000000000000f",
|
|
|
|
"0:0:0:0:0:0:10.0.0.1", "0000000000000000000000000a000001",
|
|
|
|
"::ffff:192.168.0.1", "00000000000000000000ffffc0a80001",
|
|
|
|
// Test if various interface names after the percent sign are recognized.
|
|
|
|
"[::1%1]", "00000000000000000000000000000001",
|
|
|
|
"[::1%eth0]", "00000000000000000000000000000001",
|
|
|
|
"[::1%%]", "00000000000000000000000000000001",
|
|
|
|
"::1%1", "00000000000000000000000000000001",
|
|
|
|
"::1%eth0", "00000000000000000000000000000001",
|
|
|
|
"::1%%", "00000000000000000000000000000001");
|
|
|
|
|
|
|
|
private static final Map<String, String> invalidIpV6Hosts = new TestMap(
|
2012-11-21 19:53:04 +01:00
|
|
|
// Test method with garbage.
|
2014-11-12 04:11:31 +01:00
|
|
|
"Obvious Garbage", null,
|
2012-11-21 19:53:04 +01:00
|
|
|
// Test method with preferred style, too many :
|
2014-11-12 04:11:31 +01:00
|
|
|
"0:1:2:3:4:5:6:7:8", null,
|
2012-11-21 19:53:04 +01:00
|
|
|
// Test method with preferred style, not enough :
|
2014-11-12 04:11:31 +01:00
|
|
|
"0:1:2:3:4:5:6", null,
|
2012-11-21 19:53:04 +01:00
|
|
|
// Test method with preferred style, bad digits.
|
2014-11-12 04:11:31 +01:00
|
|
|
"0:1:2:3:4:5:6:x", null,
|
2012-11-21 19:53:04 +01:00
|
|
|
// Test method with preferred style, adjacent :
|
2014-11-12 04:11:31 +01:00
|
|
|
"0:1:2:3:4:5:6::7", null,
|
2014-09-04 12:58:28 +02:00
|
|
|
// Too many : separators trailing
|
2014-11-12 04:11:31 +01:00
|
|
|
"0:1:2:3:4:5:6:7::", null,
|
2014-09-04 12:58:28 +02:00
|
|
|
// Too many : separators leading
|
2014-11-12 04:11:31 +01:00
|
|
|
"::0:1:2:3:4:5:6:7", null,
|
2014-09-04 12:58:28 +02:00
|
|
|
// Too many : separators trailing
|
2014-11-12 04:11:31 +01:00
|
|
|
"1:2:3:4:5:6:7:", null,
|
2014-09-04 12:58:28 +02:00
|
|
|
// Too many : separators leading
|
2014-11-12 04:11:31 +01:00
|
|
|
":1:2:3:4:5:6:7", null,
|
2014-09-04 12:58:28 +02:00
|
|
|
// Too many : separators leading 0
|
2014-11-12 04:11:31 +01:00
|
|
|
"0::1:2:3:4:5:6:7", null,
|
2012-11-21 19:53:04 +01:00
|
|
|
// Test method with preferred style, too many digits.
|
2014-11-12 04:11:31 +01:00
|
|
|
"0:1:2:3:4:5:6:789abcdef", null,
|
2012-11-21 19:53:04 +01:00
|
|
|
// Test method with compressed style, bad digits.
|
2014-11-12 04:11:31 +01:00
|
|
|
"0:1:2:3::x", null,
|
2012-11-21 19:53:04 +01:00
|
|
|
// Test method with compressed style, too many adjacent :
|
2014-11-12 04:11:31 +01:00
|
|
|
"0:1:2:::3", null,
|
2012-11-21 19:53:04 +01:00
|
|
|
// Test method with compressed style, too many digits.
|
2014-11-12 04:11:31 +01:00
|
|
|
"0:1:2:3::abcde", null,
|
2012-11-21 19:53:04 +01:00
|
|
|
// Test method with preferred style, too many :
|
2014-11-12 04:11:31 +01:00
|
|
|
"0:1:2:3:4:5:6:7:8", null,
|
2012-11-21 19:53:04 +01:00
|
|
|
// Test method with compressed style, not enough :
|
2014-11-12 04:11:31 +01:00
|
|
|
"0:1", null,
|
2012-11-21 19:53:04 +01:00
|
|
|
// Test method with ipv4 style, bad ipv6 digits.
|
2014-11-12 04:11:31 +01:00
|
|
|
"0:0:0:0:0:x:10.0.0.1", null,
|
2012-11-21 19:53:04 +01:00
|
|
|
// Test method with ipv4 style, bad ipv4 digits.
|
2014-11-12 04:11:31 +01:00
|
|
|
"0:0:0:0:0:0:10.0.0.x", null,
|
2012-11-21 19:53:04 +01:00
|
|
|
// Test method with ipv4 style, adjacent :
|
2014-11-12 04:11:31 +01:00
|
|
|
"0:0:0:0:0::0:10.0.0.1", null,
|
2012-11-21 19:53:04 +01:00
|
|
|
// Test method with ipv4 style, too many ipv6 digits.
|
2014-11-12 04:11:31 +01:00
|
|
|
"0:0:0:0:0:00000:10.0.0.1", null,
|
2012-11-21 19:53:04 +01:00
|
|
|
// Test method with ipv4 style, too many :
|
2014-11-12 04:11:31 +01:00
|
|
|
"0:0:0:0:0:0:0:10.0.0.1", null,
|
2012-11-21 19:53:04 +01:00
|
|
|
// Test method with ipv4 style, not enough :
|
2014-11-12 04:11:31 +01:00
|
|
|
"0:0:0:0:0:10.0.0.1", null,
|
2012-11-21 19:53:04 +01:00
|
|
|
// Test method with ipv4 style, too many .
|
2014-11-12 04:11:31 +01:00
|
|
|
"0:0:0:0:0:0:10.0.0.0.1", null,
|
2012-11-21 19:53:04 +01:00
|
|
|
// Test method with ipv4 style, not enough .
|
2014-11-12 04:11:31 +01:00
|
|
|
"0:0:0:0:0:0:10.0.1", null,
|
2012-11-21 19:53:04 +01:00
|
|
|
// Test method with ipv4 style, adjacent .
|
2014-11-12 04:11:31 +01:00
|
|
|
"0:0:0:0:0:0:10..0.0.1", null,
|
2014-09-04 12:58:28 +02:00
|
|
|
// Test method with ipv4 style, leading .
|
2014-11-12 04:11:31 +01:00
|
|
|
"0:0:0:0:0:0:.0.0.1", null,
|
2014-09-04 12:58:28 +02:00
|
|
|
// Test method with ipv4 style, leading .
|
2014-11-12 04:11:31 +01:00
|
|
|
"0:0:0:0:0:0:.10.0.0.1", null,
|
2014-09-04 12:58:28 +02:00
|
|
|
// Test method with ipv4 style, trailing .
|
2014-11-12 04:11:31 +01:00
|
|
|
"0:0:0:0:0:0:10.0.0.", null,
|
2014-09-04 12:58:28 +02:00
|
|
|
// Test method with ipv4 style, trailing .
|
2014-11-12 04:11:31 +01:00
|
|
|
"0:0:0:0:0:0:10.0.0.1.", null,
|
2012-11-21 19:53:04 +01:00
|
|
|
// Test method with compressed ipv4 style, bad ipv6 digits.
|
2014-11-12 04:11:31 +01:00
|
|
|
"::fffx:192.168.0.1", null,
|
2012-11-21 19:53:04 +01:00
|
|
|
// Test method with compressed ipv4 style, bad ipv4 digits.
|
2014-11-12 04:11:31 +01:00
|
|
|
"::ffff:192.168.0.x", null,
|
2012-11-21 19:53:04 +01:00
|
|
|
// Test method with compressed ipv4 style, too many adjacent :
|
2014-11-12 04:11:31 +01:00
|
|
|
":::ffff:192.168.0.1", null,
|
2012-11-21 19:53:04 +01:00
|
|
|
// Test method with compressed ipv4 style, too many ipv6 digits.
|
2014-11-12 04:11:31 +01:00
|
|
|
"::fffff:192.168.0.1", null,
|
2012-11-21 19:53:04 +01:00
|
|
|
// Test method with compressed ipv4 style, too many ipv4 digits.
|
2014-11-12 04:11:31 +01:00
|
|
|
"::ffff:1923.168.0.1", null,
|
2012-11-21 19:53:04 +01:00
|
|
|
// Test method with compressed ipv4 style, not enough :
|
2014-11-12 04:11:31 +01:00
|
|
|
":ffff:192.168.0.1", null,
|
2012-11-21 19:53:04 +01:00
|
|
|
// Test method with compressed ipv4 style, too many .
|
2014-11-12 04:11:31 +01:00
|
|
|
"::ffff:192.168.0.1.2", null,
|
2012-11-21 19:53:04 +01:00
|
|
|
// Test method with compressed ipv4 style, not enough .
|
2014-11-12 04:11:31 +01:00
|
|
|
"::ffff:192.168.0", null,
|
2012-11-21 19:53:04 +01:00
|
|
|
// Test method with compressed ipv4 style, adjacent .
|
2014-11-12 04:11:31 +01:00
|
|
|
"::ffff:192.168..0.1", null,
|
2012-11-21 19:53:04 +01:00
|
|
|
// Test method, garbage.
|
2014-11-12 04:11:31 +01:00
|
|
|
"absolute, and utter garbage", null,
|
2012-11-21 19:53:04 +01:00
|
|
|
// Test method, bad ipv6 digits.
|
2014-11-12 04:11:31 +01:00
|
|
|
"x:0:0:0:0:0:10.0.0.1", null,
|
2012-11-21 19:53:04 +01:00
|
|
|
// Test method, bad ipv4 digits.
|
2014-11-12 04:11:31 +01:00
|
|
|
"0:0:0:0:0:0:x.0.0.1", null,
|
2012-11-21 19:53:04 +01:00
|
|
|
// Test method, too many ipv6 digits.
|
2014-11-12 04:11:31 +01:00
|
|
|
"00000:0:0:0:0:0:10.0.0.1", null,
|
2012-11-21 19:53:04 +01:00
|
|
|
// Test method, too many ipv4 digits.
|
2014-11-12 04:11:31 +01:00
|
|
|
"0:0:0:0:0:0:10.0.0.1000", null,
|
2012-11-21 19:53:04 +01:00
|
|
|
// Test method, too many :
|
2014-11-12 04:11:31 +01:00
|
|
|
"0:0:0:0:0:0:0:10.0.0.1", null,
|
2012-11-21 19:53:04 +01:00
|
|
|
// Test method, not enough :
|
2014-11-12 04:11:31 +01:00
|
|
|
"0:0:0:0:0:10.0.0.1", null,
|
2014-09-04 12:58:28 +02:00
|
|
|
// Test method, out of order trailing :
|
2014-11-12 04:11:31 +01:00
|
|
|
"0:0:0:0:0:10.0.0.1:", null,
|
2014-09-04 12:58:28 +02:00
|
|
|
// Test method, out of order leading :
|
2014-11-12 04:11:31 +01:00
|
|
|
":0:0:0:0:0:10.0.0.1", null,
|
2014-09-04 12:58:28 +02:00
|
|
|
// Test method, out of order leading :
|
2014-11-12 04:11:31 +01:00
|
|
|
"0:0:0:0::10.0.0.1:", null,
|
2014-09-04 12:58:28 +02:00
|
|
|
// Test method, out of order trailing :
|
2014-11-12 04:11:31 +01:00
|
|
|
":0:0:0:0::10.0.0.1", null,
|
2012-11-21 19:53:04 +01:00
|
|
|
// Test method, too many .
|
2014-11-12 04:11:31 +01:00
|
|
|
"0:0:0:0:0:0:10.0.0.0.1", null,
|
2012-11-21 19:53:04 +01:00
|
|
|
// Test method, not enough .
|
2014-11-12 04:11:31 +01:00
|
|
|
"0:0:0:0:0:0:10.0.1", null,
|
2012-11-21 19:53:04 +01:00
|
|
|
// Test method, adjacent .
|
2014-11-12 04:11:31 +01:00
|
|
|
"0:0:0:0:0:0:10.0.0..1", null,
|
2014-09-04 12:58:28 +02:00
|
|
|
// Double compression symbol
|
2014-11-12 04:11:31 +01:00
|
|
|
"::0::", null,
|
2014-09-04 12:58:28 +02:00
|
|
|
// Empty contents
|
2014-11-12 04:11:31 +01:00
|
|
|
"", null,
|
2014-09-04 12:58:28 +02:00
|
|
|
// Trailing : (max number of : = 8)
|
2014-11-12 04:11:31 +01:00
|
|
|
"2001:0:4136:e378:8000:63bf:3fff:fdd2:", null,
|
2014-09-04 12:58:28 +02:00
|
|
|
// Leading : (max number of : = 8)
|
2014-11-12 04:11:31 +01:00
|
|
|
":aaaa:bbbb:cccc:dddd:eeee:ffff:1111:2222", null,
|
2014-09-04 12:58:28 +02:00
|
|
|
// Invalid character
|
2014-11-12 04:11:31 +01:00
|
|
|
"1234:2345:3456:4567:5678:6789::X890", null,
|
2014-09-04 12:58:28 +02:00
|
|
|
// Trailing . in IPv4
|
2014-11-12 04:11:31 +01:00
|
|
|
"::ffff:255.255.255.255.", null,
|
2014-09-04 12:58:28 +02:00
|
|
|
// To many characters in IPv4
|
2014-11-12 04:11:31 +01:00
|
|
|
"::ffff:0.0.1111.0", null,
|
2014-09-04 12:58:28 +02:00
|
|
|
// Test method, adjacent .
|
2014-11-12 04:11:31 +01:00
|
|
|
"::ffff:0.0..0", null,
|
2014-09-04 12:58:28 +02:00
|
|
|
// Not enough IPv4 entries trailing .
|
2014-11-12 04:11:31 +01:00
|
|
|
"::ffff:127.0.0.", null,
|
2014-09-04 12:58:28 +02:00
|
|
|
// Not enough IPv4 entries no trailing .
|
2014-11-12 04:11:31 +01:00
|
|
|
"::ffff:1.2.4", null,
|
2014-09-04 12:58:28 +02:00
|
|
|
// Extra IPv4 entry
|
2014-11-12 04:11:31 +01:00
|
|
|
"::ffff:192.168.0.1.255", null,
|
2014-09-04 12:58:28 +02:00
|
|
|
// Not enough IPv6 content
|
2014-11-12 04:11:31 +01:00
|
|
|
":ffff:192.168.0.1.255", null,
|
2014-09-04 12:58:28 +02:00
|
|
|
// Intermixed IPv4 and IPv6 symbols
|
2014-11-12 04:11:31 +01:00
|
|
|
"::ffff:255.255:255.255.", null);
|
|
|
|
|
2014-09-04 12:58:28 +02:00
|
|
|
private static final Map<byte[], String> ipv6ToAddressStrings = new HashMap<byte[], String>() {
|
|
|
|
private static final long serialVersionUID = 2999763170377573184L;
|
|
|
|
{
|
|
|
|
// From the RFC 5952 http://tools.ietf.org/html/rfc5952#section-4
|
2014-11-12 04:11:31 +01:00
|
|
|
put(new byte[] {
|
|
|
|
32, 1, 13, -72,
|
|
|
|
0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0,
|
|
|
|
0, 0, 0, 1
|
|
|
|
},
|
|
|
|
"2001:db8::1");
|
|
|
|
put(new byte[] {
|
|
|
|
32, 1, 13, -72,
|
|
|
|
0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0,
|
|
|
|
0, 2, 0, 1
|
|
|
|
},
|
|
|
|
"2001:db8::2:1");
|
|
|
|
put(new byte[] {
|
|
|
|
32, 1, 13, -72,
|
|
|
|
0, 0, 0, 1,
|
|
|
|
0, 1, 0, 1,
|
|
|
|
0, 1, 0, 1
|
|
|
|
},
|
|
|
|
"2001:db8:0:1:1:1:1:1");
|
2014-09-04 12:58:28 +02:00
|
|
|
|
|
|
|
// Other examples
|
2014-11-12 04:11:31 +01:00
|
|
|
put(new byte[] {
|
|
|
|
32, 1, 13, -72,
|
|
|
|
0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0,
|
|
|
|
0, 2, 0, 1
|
|
|
|
},
|
|
|
|
"2001:db8::2:1");
|
|
|
|
put(new byte[] {
|
|
|
|
32, 1, 0, 0,
|
|
|
|
0, 0, 0, 1,
|
|
|
|
0, 0, 0, 0,
|
|
|
|
0, 0, 0, 1
|
|
|
|
},
|
|
|
|
"2001:0:0:1::1");
|
|
|
|
put(new byte[] {
|
|
|
|
32, 1, 13, -72,
|
|
|
|
0, 0, 0, 0,
|
|
|
|
0, 1, 0, 0,
|
|
|
|
0, 0, 0, 1
|
|
|
|
},
|
|
|
|
"2001:db8::1:0:0:1");
|
|
|
|
put(new byte[] {
|
|
|
|
32, 1, 13, -72,
|
|
|
|
0, 0, 0, 0,
|
|
|
|
0, 1, 0, 0,
|
|
|
|
0, 0, 0, 0
|
|
|
|
},
|
|
|
|
"2001:db8:0:0:1::");
|
|
|
|
put(new byte[] {
|
|
|
|
32, 1, 13, -72,
|
|
|
|
0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0,
|
|
|
|
0, 2, 0, 0
|
|
|
|
},
|
|
|
|
"2001:db8::2:0");
|
|
|
|
put(new byte[] {
|
|
|
|
0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0,
|
|
|
|
0, 0, 0, 1
|
|
|
|
},
|
|
|
|
"::1");
|
|
|
|
put(new byte[] {
|
|
|
|
0, 0, 0, 0,
|
|
|
|
0, 0, 0, 1,
|
|
|
|
0, 0, 0, 0,
|
|
|
|
0, 0, 0, 1
|
|
|
|
},
|
|
|
|
"::1:0:0:0:1");
|
|
|
|
put(new byte[] {
|
|
|
|
0, 0, 0, 0,
|
|
|
|
1, 0, 0, 1,
|
|
|
|
0, 0, 0, 0,
|
|
|
|
1, 0, 0, 0
|
|
|
|
},
|
|
|
|
"::100:1:0:0:100:0");
|
|
|
|
put(new byte[] {
|
|
|
|
32, 1, 0, 0,
|
|
|
|
65, 54, -29, 120,
|
|
|
|
-128, 0, 99, -65,
|
|
|
|
63, -1, -3, -46
|
|
|
|
},
|
|
|
|
"2001:0:4136:e378:8000:63bf:3fff:fdd2");
|
|
|
|
put(new byte[] {
|
|
|
|
-86, -86, -69, -69,
|
|
|
|
-52, -52, -35, -35,
|
|
|
|
-18, -18, -1, -1,
|
|
|
|
17, 17, 34, 34
|
|
|
|
},
|
|
|
|
"aaaa:bbbb:cccc:dddd:eeee:ffff:1111:2222");
|
|
|
|
put(new byte[] {
|
|
|
|
0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0
|
|
|
|
},
|
|
|
|
"::");
|
2014-09-04 12:58:28 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-11-12 04:11:31 +01:00
|
|
|
private static final Map<String, String> ipv4MappedToIPv6AddressStrings = new TestMap(
|
2014-09-04 12:58:28 +02:00
|
|
|
// IPv4 addresses
|
2014-11-12 04:11:31 +01:00
|
|
|
"255.255.255.255", "::ffff:255.255.255.255",
|
|
|
|
"0.0.0.0", "::ffff:0.0.0.0",
|
|
|
|
"127.0.0.1", "::ffff:127.0.0.1",
|
|
|
|
"1.2.3.4", "::ffff:1.2.3.4",
|
|
|
|
"192.168.0.1", "::ffff:192.168.0.1",
|
2014-09-04 12:58:28 +02:00
|
|
|
|
|
|
|
// IPv6 addresses
|
|
|
|
// Fully specified
|
2014-11-12 04:11:31 +01:00
|
|
|
"2001:0:4136:e378:8000:63bf:3fff:fdd2", "2001:0:4136:e378:8000:63bf:3fff:fdd2",
|
|
|
|
"aaaa:bbbb:cccc:dddd:eeee:ffff:1111:2222", "aaaa:bbbb:cccc:dddd:eeee:ffff:1111:2222",
|
|
|
|
"0:0:0:0:0:0:0:0", "::",
|
|
|
|
"0:0:0:0:0:0:0:1", "::1",
|
2014-09-04 12:58:28 +02:00
|
|
|
|
|
|
|
// Compressing at the beginning
|
2014-11-12 04:11:31 +01:00
|
|
|
"::1:0:0:0:1", "::1:0:0:0:1",
|
|
|
|
"::1:ffff:ffff", "::1:ffff:ffff",
|
|
|
|
"::", "::",
|
|
|
|
"::1", "::1",
|
|
|
|
"::ffff", "::ffff",
|
|
|
|
"::ffff:0", "::ffff:0",
|
|
|
|
"::ffff:ffff", "::ffff:ffff",
|
|
|
|
"::0987:9876:8765", "::987:9876:8765",
|
|
|
|
"::0987:9876:8765:7654", "::987:9876:8765:7654",
|
|
|
|
"::0987:9876:8765:7654:6543", "::987:9876:8765:7654:6543",
|
|
|
|
"::0987:9876:8765:7654:6543:5432", "::987:9876:8765:7654:6543:5432",
|
2014-09-04 12:58:28 +02:00
|
|
|
// Note the compression is removed (rfc 5952 section 4.2.2)
|
2014-11-12 04:11:31 +01:00
|
|
|
"::0987:9876:8765:7654:6543:5432:3210", "0:987:9876:8765:7654:6543:5432:3210",
|
2014-09-04 12:58:28 +02:00
|
|
|
|
|
|
|
// Compressing at the end
|
|
|
|
// Note the compression is removed (rfc 5952 section 4.2.2)
|
2014-11-12 04:11:31 +01:00
|
|
|
"2001:db8:abcd:bcde:cdef:def1:ef12::", "2001:db8:abcd:bcde:cdef:def1:ef12:0",
|
|
|
|
"2001:db8:abcd:bcde:cdef:def1::", "2001:db8:abcd:bcde:cdef:def1::",
|
|
|
|
"2001:db8:abcd:bcde:cdef::", "2001:db8:abcd:bcde:cdef::",
|
|
|
|
"2001:db8:abcd:bcde::", "2001:db8:abcd:bcde::",
|
|
|
|
"2001:db8:abcd::", "2001:db8:abcd::",
|
|
|
|
"2001:1234::", "2001:1234::",
|
|
|
|
"2001::", "2001::",
|
|
|
|
"0::", "::",
|
2014-09-04 12:58:28 +02:00
|
|
|
|
|
|
|
// Compressing in the middle
|
2014-11-12 04:11:31 +01:00
|
|
|
"1234:2345::7890", "1234:2345::7890",
|
|
|
|
"1234::2345:7890", "1234::2345:7890",
|
|
|
|
"1234:2345:3456::7890", "1234:2345:3456::7890",
|
|
|
|
"1234:2345::3456:7890", "1234:2345::3456:7890",
|
|
|
|
"1234::2345:3456:7890", "1234::2345:3456:7890",
|
|
|
|
"1234:2345:3456:4567::7890", "1234:2345:3456:4567::7890",
|
|
|
|
"1234:2345:3456::4567:7890", "1234:2345:3456::4567:7890",
|
|
|
|
"1234:2345::3456:4567:7890", "1234:2345::3456:4567:7890",
|
|
|
|
"1234::2345:3456:4567:7890", "1234::2345:3456:4567:7890",
|
|
|
|
"1234:2345:3456:4567:5678::7890", "1234:2345:3456:4567:5678::7890",
|
|
|
|
"1234:2345:3456:4567::5678:7890", "1234:2345:3456:4567::5678:7890",
|
|
|
|
"1234:2345:3456::4567:5678:7890", "1234:2345:3456::4567:5678:7890",
|
|
|
|
"1234:2345::3456:4567:5678:7890", "1234:2345::3456:4567:5678:7890",
|
|
|
|
"1234::2345:3456:4567:5678:7890", "1234::2345:3456:4567:5678:7890",
|
2014-09-04 12:58:28 +02:00
|
|
|
// Note the compression is removed (rfc 5952 section 4.2.2)
|
2014-11-12 04:11:31 +01:00
|
|
|
"1234:2345:3456:4567:5678:6789::7890", "1234:2345:3456:4567:5678:6789:0:7890",
|
2014-09-04 12:58:28 +02:00
|
|
|
// Note the compression is removed (rfc 5952 section 4.2.2)
|
2014-11-12 04:11:31 +01:00
|
|
|
"1234:2345:3456:4567:5678::6789:7890", "1234:2345:3456:4567:5678:0:6789:7890",
|
2014-09-04 12:58:28 +02:00
|
|
|
// Note the compression is removed (rfc 5952 section 4.2.2)
|
2014-11-12 04:11:31 +01:00
|
|
|
"1234:2345:3456:4567::5678:6789:7890", "1234:2345:3456:4567:0:5678:6789:7890",
|
2014-09-04 12:58:28 +02:00
|
|
|
// Note the compression is removed (rfc 5952 section 4.2.2)
|
2014-11-12 04:11:31 +01:00
|
|
|
"1234:2345:3456::4567:5678:6789:7890", "1234:2345:3456:0:4567:5678:6789:7890",
|
2014-09-04 12:58:28 +02:00
|
|
|
// Note the compression is removed (rfc 5952 section 4.2.2)
|
2014-11-12 04:11:31 +01:00
|
|
|
"1234:2345::3456:4567:5678:6789:7890", "1234:2345:0:3456:4567:5678:6789:7890",
|
2014-09-04 12:58:28 +02:00
|
|
|
// Note the compression is removed (rfc 5952 section 4.2.2)
|
2014-11-12 04:11:31 +01:00
|
|
|
"1234::2345:3456:4567:5678:6789:7890", "1234:0:2345:3456:4567:5678:6789:7890",
|
2014-09-04 12:58:28 +02:00
|
|
|
|
|
|
|
// IPv4 mapped addresses
|
2014-11-12 04:11:31 +01:00
|
|
|
"::ffff:255.255.255.255", "::ffff:255.255.255.255",
|
|
|
|
"::ffff:0.0.0.0", "::ffff:0.0.0.0",
|
|
|
|
"::ffff:127.0.0.1", "::ffff:127.0.0.1",
|
|
|
|
"::ffff:1.2.3.4", "::ffff:1.2.3.4",
|
|
|
|
"::ffff:192.168.0.1", "::ffff:192.168.0.1");
|
2012-08-21 02:20:21 +02:00
|
|
|
|
|
|
|
@Test
|
2012-11-10 00:50:21 +01:00
|
|
|
public void testLocalhost() {
|
2012-11-19 06:07:18 +01:00
|
|
|
assertNotNull(NetUtil.LOCALHOST);
|
2012-08-21 02:20:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2012-09-01 10:00:24 +02:00
|
|
|
public void testLoopback() {
|
2012-11-19 06:07:18 +01:00
|
|
|
assertNotNull(NetUtil.LOOPBACK_IF);
|
2012-08-21 02:20:21 +02:00
|
|
|
}
|
|
|
|
|
2012-11-21 19:53:04 +01:00
|
|
|
@Test
|
|
|
|
public void testIsValidIpV4Address() {
|
|
|
|
for (String host : validIpV4Hosts.keySet()) {
|
|
|
|
assertTrue(NetUtil.isValidIpV4Address(host));
|
|
|
|
}
|
|
|
|
for (String host : invalidIpV4Hosts.keySet()) {
|
|
|
|
assertFalse(NetUtil.isValidIpV4Address(host));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testIsValidIpV6Address() {
|
|
|
|
for (String host : validIpV6Hosts.keySet()) {
|
|
|
|
assertTrue(NetUtil.isValidIpV6Address(host));
|
|
|
|
}
|
|
|
|
for (String host : invalidIpV6Hosts.keySet()) {
|
|
|
|
assertFalse(NetUtil.isValidIpV6Address(host));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testCreateByteArrayFromIpAddressString() {
|
2014-11-12 04:11:31 +01:00
|
|
|
for (Entry<String, String> e : validIpV4Hosts.entrySet()) {
|
|
|
|
assertHexDumpEquals(e.getValue(), NetUtil.createByteArrayFromIpAddressString(e.getKey()));
|
2012-11-21 19:53:04 +01:00
|
|
|
}
|
2014-11-12 04:11:31 +01:00
|
|
|
for (Entry<String, String> e : invalidIpV4Hosts.entrySet()) {
|
|
|
|
assertHexDumpEquals(e.getValue(), NetUtil.createByteArrayFromIpAddressString(e.getKey()));
|
2012-11-21 19:53:04 +01:00
|
|
|
}
|
2014-11-12 04:11:31 +01:00
|
|
|
for (Entry<String, String> e : validIpV6Hosts.entrySet()) {
|
|
|
|
assertHexDumpEquals(e.getValue(), NetUtil.createByteArrayFromIpAddressString(e.getKey()));
|
2012-11-21 19:53:04 +01:00
|
|
|
}
|
2014-11-12 04:11:31 +01:00
|
|
|
for (Entry<String, String> e : invalidIpV6Hosts.entrySet()) {
|
|
|
|
assertHexDumpEquals(e.getValue(), NetUtil.createByteArrayFromIpAddressString(e.getKey()));
|
2012-11-21 19:53:04 +01:00
|
|
|
}
|
|
|
|
}
|
2014-09-04 12:58:28 +02:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testIp6AddressToString() throws UnknownHostException {
|
|
|
|
for (Entry<byte[], String> testEntry : ipv6ToAddressStrings.entrySet()) {
|
2014-11-12 04:11:31 +01:00
|
|
|
assertEquals(testEntry.getValue(), NetUtil.toAddressString(InetAddress.getByAddress(testEntry.getKey())));
|
2014-09-04 12:58:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testIp4AddressToString() throws UnknownHostException {
|
2014-11-12 04:11:31 +01:00
|
|
|
for (Entry<String, String> e : validIpV4Hosts.entrySet()) {
|
|
|
|
assertEquals(e.getKey(), NetUtil.toAddressString(InetAddress.getByAddress(unhex(e.getValue()))));
|
2014-09-04 12:58:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testIpv4MappedIp6GetByName() {
|
|
|
|
for (Entry<String, String> testEntry : ipv4MappedToIPv6AddressStrings.entrySet()) {
|
2014-11-12 04:11:31 +01:00
|
|
|
assertEquals(
|
|
|
|
testEntry.getValue(),
|
|
|
|
NetUtil.toAddressString(NetUtil.getByName(testEntry.getKey(), true), true));
|
2014-09-04 12:58:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testinvalidIpv4MappedIp6GetByName() {
|
|
|
|
for (String testEntry : invalidIpV4Hosts.keySet()) {
|
|
|
|
assertNull(NetUtil.getByName(testEntry, true));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (String testEntry : invalidIpV6Hosts.keySet()) {
|
|
|
|
assertNull(NetUtil.getByName(testEntry, true));
|
|
|
|
}
|
|
|
|
}
|
2014-11-12 04:11:31 +01:00
|
|
|
|
|
|
|
private static void assertHexDumpEquals(String expected, byte[] actual) {
|
|
|
|
assertEquals(expected, hex(actual));
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String hex(byte[] value) {
|
|
|
|
if (value == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder buf = new StringBuilder(value.length << 1);
|
|
|
|
for (byte b: value) {
|
|
|
|
String hex = Integer.toHexString(b & 0xFF);
|
|
|
|
if (hex.length() == 1) {
|
|
|
|
buf.append('0');
|
|
|
|
}
|
|
|
|
buf.append(hex);
|
|
|
|
}
|
|
|
|
return buf.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static byte[] unhex(String value) {
|
|
|
|
if (value == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
byte[] buf = new byte[value.length() >>> 1];
|
|
|
|
for (int i = 0; i < buf.length; i ++) {
|
|
|
|
buf[i] = (byte) Integer.parseInt(value.substring(i << 1, i + 1 << 1), 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
2012-08-21 02:20:21 +02:00
|
|
|
}
|