Added test cases for NetUtil methods, if we can`t convertAddress we return null

This commit is contained in:
alexey 2012-11-21 22:53:04 +04:00 committed by Norman Maurer
parent 0fb0037eab
commit 738d382fbc
3 changed files with 334 additions and 89 deletions

View File

@ -56,7 +56,7 @@ public final class SocksCmdRequest extends SocksRequest {
} }
break; break;
case IPv6: case IPv6:
if (!NetUtil.isValidIp6Address(host)) { if (!NetUtil.isValidIpV6Address(host)) {
throw new IllegalArgumentException(host + " is not a valid IPv6 address"); throw new IllegalArgumentException(host + " is not a valid IPv6 address");
} }
break; break;

View File

@ -33,7 +33,7 @@ import java.util.StringTokenizer;
/** /**
* A class that holds a number of network-related constants. * A class that holds a number of network-related constants.
* * <p/>
* This class borrowed some of its methods from a modified fork of the * This class borrowed some of its methods from a modified fork of the
* <a href="http://svn.apache.org/repos/asf/harmony/enhanced/java/branches/java6/classlib/modules/luni/ * <a href="http://svn.apache.org/repos/asf/harmony/enhanced/java/branches/java6/classlib/modules/luni/
* src/main/java/org/apache/harmony/luni/util/Inet6Util.java">Inet6Util class</a> which was part of Apache Harmony. * src/main/java/org/apache/harmony/luni/util/Inet6Util.java">Inet6Util class</a> which was part of Apache Harmony.
@ -42,7 +42,7 @@ public final class NetUtil {
/** /**
* The {@link InetAddress} representing the host machine * The {@link InetAddress} representing the host machine
* * <p/>
* We cache this because some machines take almost forever to return from * We cache this because some machines take almost forever to return from
* {@link InetAddress}.getLocalHost(). This may be due to incorrect * {@link InetAddress}.getLocalHost(). This may be due to incorrect
* configuration of the hosts and DNS client configuration files. * configuration of the hosts and DNS client configuration files.
@ -75,12 +75,12 @@ public final class NetUtil {
} catch (IOException e) { } catch (IOException e) {
// The default local host names did not work. Try hard-coded IPv4 address. // The default local host names did not work. Try hard-coded IPv4 address.
try { try {
localhost = InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 }); localhost = InetAddress.getByAddress(new byte[]{127, 0, 0, 1});
validateHost(localhost); validateHost(localhost);
} catch (IOException e1) { } catch (IOException e1) {
// The hard-coded IPv4 address did not work. Try hard coded IPv6 address. // The hard-coded IPv4 address did not work. Try hard coded IPv6 address.
try { try {
localhost = InetAddress.getByAddress(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }); localhost = InetAddress.getByAddress(new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1});
validateHost(localhost); validateHost(localhost);
} catch (IOException e2) { } catch (IOException e2) {
throw new Error("Failed to resolve localhost - incorrect network configuration?", e2); throw new Error("Failed to resolve localhost - incorrect network configuration?", e2);
@ -204,105 +204,89 @@ public final class NetUtil {
return byteAddress; return byteAddress;
} }
if (ipAddressString.charAt(0) == '[') { if (isValidIpV6Address(ipAddressString)) {
ipAddressString = ipAddressString.substring(1, ipAddressString if (ipAddressString.charAt(0) == '[') {
.length() - 1); ipAddressString = ipAddressString.substring(1, ipAddressString
} .length() - 1);
}
StringTokenizer tokenizer = new StringTokenizer(ipAddressString, ":.", StringTokenizer tokenizer = new StringTokenizer(ipAddressString, ":.",
true); true);
ArrayList<String> hexStrings = new ArrayList<String>(); ArrayList<String> hexStrings = new ArrayList<String>();
ArrayList<String> decStrings = new ArrayList<String>(); ArrayList<String> decStrings = new ArrayList<String>();
String token = ""; String token = "";
String prevToken = ""; String prevToken = "";
int doubleColonIndex = -1; // If a double colon exists, we need to int doubleColonIndex = -1; // If a double colon exists, we need to
// insert 0s. // insert 0s.
// Go through the tokens, including the seperators ':' and '.' // Go through the tokens, including the seperators ':' and '.'
// When we hit a : or . the previous token will be added to either // When we hit a : or . the previous token will be added to either
// the hex list or decimal list. In the case where we hit a :: // the hex list or decimal list. In the case where we hit a ::
// we will save the index of the hexStrings so we can add zeros // we will save the index of the hexStrings so we can add zeros
// in to fill out the string // in to fill out the string
while (tokenizer.hasMoreTokens()) { while (tokenizer.hasMoreTokens()) {
prevToken = token; prevToken = token;
token = tokenizer.nextToken(); token = tokenizer.nextToken();
if (":".equals(token)) { if (":".equals(token)) {
if (":".equals(prevToken)) { if (":".equals(prevToken)) {
doubleColonIndex = hexStrings.size(); doubleColonIndex = hexStrings.size();
} else if (!prevToken.isEmpty()) { } else if (!prevToken.isEmpty()) {
hexStrings.add(prevToken); hexStrings.add(prevToken);
}
} else if (".".equals(token)) {
decStrings.add(prevToken);
} }
} else if (".".equals(token)) {
decStrings.add(prevToken);
} }
}
if (":".equals(prevToken)) { if (":".equals(prevToken)) {
if (":".equals(token)) { if (":".equals(token)) {
doubleColonIndex = hexStrings.size(); doubleColonIndex = hexStrings.size();
} else { } else {
hexStrings.add(token); hexStrings.add(token);
}
} else if (".".equals(prevToken)) {
decStrings.add(token);
} }
} else if (".".equals(prevToken)) {
decStrings.add(token);
}
// figure out how many hexStrings we should have // figure out how many hexStrings we should have
// also check if it is a IPv4 address // also check if it is a IPv4 address
int hexStringsLength = 8; int hexStringsLength = 8;
// If we have an IPv4 address tagged on at the end, subtract // If we have an IPv4 address tagged on at the end, subtract
// 4 bytes, or 2 hex words from the total // 4 bytes, or 2 hex words from the total
if (!decStrings.isEmpty()) { if (!decStrings.isEmpty()) {
hexStringsLength -= 2; hexStringsLength -= 2;
}
// if we hit a double Colon add the appropriate hex strings
if (doubleColonIndex != -1) {
int numberToInsert = hexStringsLength - hexStrings.size();
for (int i = 0; i < numberToInsert; i++) {
hexStrings.add(doubleColonIndex, "0");
} }
}
byte[] ipByteArray = new byte[16]; // if we hit a double Colon add the appropriate hex strings
if (doubleColonIndex != -1) {
// Finally convert these strings to bytes... int numberToInsert = hexStringsLength - hexStrings.size();
for (int i = 0; i < hexStrings.size(); i++) { for (int i = 0; i < numberToInsert; i++) {
convertToBytes(hexStrings.get(i), ipByteArray, i * 2); hexStrings.add(doubleColonIndex, "0");
} }
// Now if there are any decimal values, we know where they go...
for (int i = 0; i < decStrings.size(); i++) {
ipByteArray[i + 12] = (byte) (Integer.parseInt(decStrings
.get(i)) & 255);
}
// now check to see if this guy is actually and IPv4 address
// an ipV4 address is ::FFFF:d.d.d.d
boolean ipV4 = true;
for (int i = 0; i < 10; i++) {
if (ipByteArray[i] != 0) {
ipV4 = false;
break;
} }
}
if (ipByteArray[10] != -1 || ipByteArray[11] != -1) { byte[] ipByteArray = new byte[16];
ipV4 = false;
}
if (ipV4) { // Finally convert these strings to bytes...
byte[] ipv4ByteArray = new byte[4]; for (int i = 0; i < hexStrings.size(); i++) {
System.arraycopy(ipByteArray, 12, ipv4ByteArray, 0, 4); convertToBytes(hexStrings.get(i), ipByteArray, i * 2);
return ipv4ByteArray; }
}
return ipByteArray; // Now if there are any decimal values, we know where they go...
for (int i = 0; i < decStrings.size(); i++) {
ipByteArray[i + 12] = (byte) (Integer.parseInt(decStrings
.get(i)) & 255);
}
return ipByteArray;
}
return null;
} }
/** Converts a 4 character hex word into a 2 byte word equivalent */ /**
* Converts a 4 character hex word into a 2 byte word equivalent
*/
private static void convertToBytes(String hexWord, byte[] ipByteArray, private static void convertToBytes(String hexWord, byte[] ipByteArray,
int byteIndex) { int byteIndex) {
@ -378,7 +362,7 @@ public final class NetUtil {
return 0; return 0;
} }
public static boolean isValidIp6Address(String ipAddress) { public static boolean isValidIpV6Address(String ipAddress) {
int length = ipAddress.length(); int length = ipAddress.length();
boolean doubleColon = false; boolean doubleColon = false;
int numberOfColons = 0; int numberOfColons = 0;
@ -503,7 +487,8 @@ public final class NetUtil {
// Check if we have an IPv4 ending // Check if we have an IPv4 ending
if (numberOfPeriods > 0) { if (numberOfPeriods > 0) {
if (numberOfPeriods != 3 || !isValidIp4Word(word.toString())) { // There is a test case with 7 colons and valid ipv4 this should resolve it
if (numberOfPeriods != 3 || !(isValidIp4Word(word.toString()) && numberOfColons < 7)) {
return false; return false;
} }
} else { } else {

View File

@ -15,11 +15,236 @@
*/ */
package io.netty.util; package io.netty.util;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.*; import static org.junit.Assert.*;
public class NetUtilTest { public class NetUtilTest {
private final static Map<String, byte[]> validIpV4Hosts = new HashMap<String, byte[]>() {
{
put("192.168.1.0", new byte[]{
(byte) 0xc0, (byte) 0xa8, 0x01, 0x00}
);
put("10.255.255.254", new byte[]{
0x0a, (byte) 0xff, (byte) 0xff, (byte) 0xfe
});
put("172.18.5.4", new byte[]{
(byte) 0xac, 0x12, 0x05, 0x04
});
put("0.0.0.0", new byte[]{
0x00, 0x00, 0x00, 0x00
});
put("127.0.0.1", new byte[]{
0x7f, 0x00, 0x00, 0x01
});
}
};
private final static Map<String, byte[]> invalidIpV4Hosts = new HashMap<String, byte[]>() {
{
put("1.256.3.4", null);
put("256.0.0.1", null);
put("1.1.1.1.1", null);
}
};
private final static Map<String, byte[]> validIpV6Hosts = new HashMap<String, byte[]>() {
{
put("::ffff:5.6.7.8", new byte[]{
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, (byte) 0xff, (byte) 0xff,
0x05, 0x06, 0x07, 0x08}
);
put("fdf8:f53b:82e4::53", new byte[]{
(byte) 0xfd, (byte) 0xf8, (byte) 0xf5, 0x3b,
(byte) 0x82, (byte) 0xe4, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x53}
);
put("fe80::200:5aee:feaa:20a2", new byte[]{
(byte) 0xfe, (byte) 0x80, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x02, 0x00, 0x5a, (byte) 0xee,
(byte) 0xfe, (byte) 0xaa, 0x20, (byte) 0xa2}
);
put("2001::1", new byte[]{
0x20, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x01}
);
put("2001:0000:4136:e378:8000:63bf:3fff:fdd2", new byte[]{
0x20, 0x01, 0x00, 0x00,
0x41, 0x36, (byte) 0xe3, 0x78,
(byte) 0x80, 0x00, 0x63, (byte) 0xbf,
0x3f, (byte) 0xff, (byte) 0xfd, (byte) 0xd2}
);
put("2001:0002:6c::430", new byte[]{
0x20, 0x01, 0x00, 0x02,
0x00, 0x6c, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x04, 0x30}
);
put("2001:10:240:ab::a", new byte[]{
0x20, 0x01, 0x00, 0x10,
0x02, 0x40, 0x00, (byte) 0xab,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x0a});
put("2002:cb0a:3cdd:1::1", new byte[]{
0x20, 0x02, (byte) 0xcb, 0x0a,
0x3c, (byte) 0xdd, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x01}
);
put("2001:db8:8:4::2", new byte[]{
0x20, 0x01, 0x0d, (byte) 0xb8,
0x00, 0x08, 0x00, 0x04,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x02}
);
put("ff01:0:0:0:0:0:0:2", new byte[]{
(byte) 0xff, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x02}
);
put("[fdf8:f53b:82e4::53]", new byte[]{
(byte) 0xfd, (byte) 0xf8, (byte) 0xf5, 0x3b,
(byte) 0x82, (byte) 0xe4, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x53}
);
put("[fe80::200:5aee:feaa:20a2]", new byte[]{
(byte) 0xfe, (byte) 0x80, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x02, 0x00, 0x5a, (byte) 0xee,
(byte) 0xfe, (byte) 0xaa, 0x20, (byte) 0xa2}
);
put("[2001::1]", new byte[]{
0x20, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x01}
);
put("[2001:0000:4136:e378:8000:63bf:3fff:fdd2]", new byte[]{
0x20, 0x01, 0x00, 0x00,
0x41, 0x36, (byte) 0xe3, 0x78,
(byte) 0x80, 0x00, 0x63, (byte) 0xbf,
0x3f, (byte) 0xff, (byte) 0xfd, (byte) 0xd2}
);
put("0:1:2:3:4:5:6:789a", new byte[]{
0x00, 0x00, 0x00, 0x01,
0x00, 0x02, 0x00, 0x03,
0x00, 0x04, 0x00, 0x05,
0x00, 0x06, 0x78, (byte) 0x9a}
);
put("0:1:2:3::f", new byte[]{
0x00, 0x00, 0x00, 0x01,
0x00, 0x02, 0x00, 0x03,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x0f}
);
put("0:0:0:0:0:0:10.0.0.1", new byte[]{
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x0a, 0x00, 0x00, 0x01}
);
put("::ffff:192.168.0.1", new byte[]{
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, (byte) 0xff, (byte) 0xff,
(byte) 0xc0, (byte) 0xa8, 0x00, 0x01}
);
}
};
private final static Map<String, byte[]> invalidIpV6Hosts = new HashMap<String, byte[]>() {
{
// Test method with garbage.
put("Obvious Garbage", null);
// Test method with preferred style, too many :
put("0:1:2:3:4:5:6:7:8", null);
// Test method with preferred style, not enough :
put("0:1:2:3:4:5:6", null);
// Test method with preferred style, bad digits.
put("0:1:2:3:4:5:6:x", null);
// Test method with preferred style, adjacent :
put("0:1:2:3:4:5:6::7", null);
// Test method with preferred style, too many digits.
put("0:1:2:3:4:5:6:789abcdef", null);
// Test method with compressed style, bad digits.
put("0:1:2:3::x", null);
// Test method with compressed style, too many adjacent :
put("0:1:2:::3", null);
// Test method with compressed style, too many digits.
put("0:1:2:3::abcde", null);
// Test method with preferred style, too many :
put("0:1:2:3:4:5:6:7:8", null);
// Test method with compressed style, not enough :
put("0:1", null);
// Test method with ipv4 style, bad ipv6 digits.
put("0:0:0:0:0:x:10.0.0.1", null);
// Test method with ipv4 style, bad ipv4 digits.
put("0:0:0:0:0:0:10.0.0.x", null);
// Test method with ipv4 style, adjacent :
put("0:0:0:0:0::0:10.0.0.1", null);
// Test method with ipv4 style, too many ipv6 digits.
put("0:0:0:0:0:00000:10.0.0.1", null);
// Test method with ipv4 style, too many :
put("0:0:0:0:0:0:0:10.0.0.1", null);
// Test method with ipv4 style, not enough :
put("0:0:0:0:0:10.0.0.1", null);
// Test method with ipv4 style, too many .
put("0:0:0:0:0:0:10.0.0.0.1", null);
// Test method with ipv4 style, not enough .
put("0:0:0:0:0:0:10.0.1", null);
// Test method with ipv4 style, adjacent .
put("0:0:0:0:0:0:10..0.0.1", null);
// Test method with compressed ipv4 style, bad ipv6 digits.
put("::fffx:192.168.0.1", null);
// Test method with compressed ipv4 style, bad ipv4 digits.
put("::ffff:192.168.0.x", null);
// Test method with compressed ipv4 style, too many adjacent :
put(":::ffff:192.168.0.1", null);
// Test method with compressed ipv4 style, too many ipv6 digits.
put("::fffff:192.168.0.1", null);
// Test method with compressed ipv4 style, too many ipv4 digits.
put("::ffff:1923.168.0.1", null);
// Test method with compressed ipv4 style, not enough :
put(":ffff:192.168.0.1", null);
// Test method with compressed ipv4 style, too many .
put("::ffff:192.168.0.1.2", null);
// Test method with compressed ipv4 style, not enough .
put("::ffff:192.168.0", null);
// Test method with compressed ipv4 style, adjacent .
put("::ffff:192.168..0.1", null);
// Test method, garbage.
put("absolute, and utter garbage", null);
// Test method, bad ipv6 digits.
put("x:0:0:0:0:0:10.0.0.1", null);
// Test method, bad ipv4 digits.
put("0:0:0:0:0:0:x.0.0.1", null);
// Test method, too many ipv6 digits.
put("00000:0:0:0:0:0:10.0.0.1", null);
// Test method, too many ipv4 digits.
put("0:0:0:0:0:0:10.0.0.1000", null);
// Test method, too many :
put("0:0:0:0:0:0:0:10.0.0.1", null);
// Test method, not enough :
put("0:0:0:0:0:10.0.0.1", null);
// Test method, too many .
put("0:0:0:0:0:0:10.0.0.0.1", null);
// Test method, not enough .
put("0:0:0:0:0:0:10.0.1", null);
// Test method, adjacent .
put("0:0:0:0:0:0:10.0.0..1", null);
}
};
@Test @Test
public void testLocalhost() { public void testLocalhost() {
@ -31,4 +256,39 @@ public class NetUtilTest {
assertNotNull(NetUtil.LOOPBACK_IF); assertNotNull(NetUtil.LOOPBACK_IF);
} }
@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() {
for (String host : validIpV4Hosts.keySet()) {
assertTrue(Arrays.equals(validIpV4Hosts.get(host), NetUtil.createByteArrayFromIpAddressString(host)));
}
for (String host : invalidIpV4Hosts.keySet()) {
assertTrue(Arrays.equals(invalidIpV4Hosts.get(host), NetUtil.createByteArrayFromIpAddressString(host)));
}
for (String host : validIpV6Hosts.keySet()) {
assertTrue(Arrays.equals(validIpV6Hosts.get(host), NetUtil.createByteArrayFromIpAddressString(host)));
}
for (String host : invalidIpV6Hosts.keySet()) {
assertTrue(Arrays.equals(invalidIpV6Hosts.get(host), NetUtil.createByteArrayFromIpAddressString(host)));
}
}
} }