diff --git a/jargyle-client/src/main/java/com/github/jh3nd3rs0n/jargyle/client/internal/client/ConnectClientSocketBuilder.java b/jargyle-client/src/main/java/com/github/jh3nd3rs0n/jargyle/client/internal/client/ConnectClientSocketBuilder.java index 88fbc6918..8e0f13581 100644 --- a/jargyle-client/src/main/java/com/github/jh3nd3rs0n/jargyle/client/internal/client/ConnectClientSocketBuilder.java +++ b/jargyle-client/src/main/java/com/github/jh3nd3rs0n/jargyle/client/internal/client/ConnectClientSocketBuilder.java @@ -29,7 +29,7 @@ public final class ConnectClientSocketBuilder { final NetObjectFactory netObjFactory, final SslSocketFactory sslSockFactory) { this.clientSocket = clientSock; - this.localAddress = HostIpv4Address.getAllZerosInetAddress(); + this.localAddress = HostIpv4Address.getAllZerosInet4Address(); this.localPortRanges = PortRanges.getDefault(); this.mustBindBeforeConnect = false; this.netObjectFactory = netObjFactory; diff --git a/jargyle-client/src/main/java/com/github/jh3nd3rs0n/jargyle/client/socks5/Socks5ServerSocket.java b/jargyle-client/src/main/java/com/github/jh3nd3rs0n/jargyle/client/socks5/Socks5ServerSocket.java index 1d63efb37..84d98cfb3 100644 --- a/jargyle-client/src/main/java/com/github/jh3nd3rs0n/jargyle/client/socks5/Socks5ServerSocket.java +++ b/jargyle-client/src/main/java/com/github/jh3nd3rs0n/jargyle/client/socks5/Socks5ServerSocket.java @@ -64,7 +64,7 @@ public void bind(SocketAddress bindpoint) throws IOException { @Override public synchronized void close() throws IOException { - this.localInetAddress = HostIpv4Address.getAllZerosInetAddress(); + this.localInetAddress = HostIpv4Address.getAllZerosInet4Address(); this.localPort = -1; this.localSocketAddress = null; this.remoteInetAddress = null; @@ -447,7 +447,7 @@ public void socks5Bind( } InetAddress bAddr = bindAddr; if (bAddr == null) { - bAddr = HostIpv4Address.getAllZerosInetAddress(); + bAddr = HostIpv4Address.getAllZerosInet4Address(); } String address = bAddr.getHostAddress(); Request req = Request.newInstance( diff --git a/jargyle-common/src/main/java/com/github/jh3nd3rs0n/jargyle/common/net/HostIpv4Address.java b/jargyle-common/src/main/java/com/github/jh3nd3rs0n/jargyle/common/net/HostIpv4Address.java index 486010e38..da8bab23b 100644 --- a/jargyle-common/src/main/java/com/github/jh3nd3rs0n/jargyle/common/net/HostIpv4Address.java +++ b/jargyle-common/src/main/java/com/github/jh3nd3rs0n/jargyle/common/net/HostIpv4Address.java @@ -1,7 +1,10 @@ package com.github.jh3nd3rs0n.jargyle.common.net; +import java.net.Inet4Address; import java.net.InetAddress; import java.net.UnknownHostException; +import java.nio.ByteBuffer; +import java.util.Arrays; /** * An IPv4 address of a node of a network. @@ -14,56 +17,60 @@ public final class HostIpv4Address extends HostAddress { public static final String ALL_ZEROS_IPV4_ADDRESS = "0.0.0.0"; /** - * The regular expression for an all zeros IPv4 address as 1 part. + * The regular expression for a blank {@code String}. */ - private static final String ALL_ZEROS_IPV4_ADDRESS_AS_1_PART_REGEX = - "\\A0{1,10}\\z"; + private static final String BLANK_STRING_REGEX = "\\A\\s+\\z"; /** - * The regular expression for an all zeros IPv4 address as 2 parts. + * The maximum value for part 4 (or for any of the first parts) in an IPv4 + * address. */ - private static final String ALL_ZEROS_IPV4_ADDRESS_AS_2_PARTS_REGEX = - "\\A0{1,3}\\.0{1,8}\\z"; + private static final long MAX_PART_4_VALUE = 255; // (256 ^ 1) - 1 /** - * The regular expression for an all zeros IPv4 address as 3 parts. + * The maximum value for part 3 in an IPv4 address as 3 parts. */ - private static final String ALL_ZEROS_IPV4_ADDRESS_AS_3_PARTS_REGEX = - "\\A0{1,3}\\.0{1,3}\\.0{1,5}\\z"; + private static final long MAX_PART_3_VALUE = 65535; // (256 ^ 2) - 1 /** - * The regular expression for an all zeros IPv4 address as 4 parts. + * The maximum value for part 2 in an IPv4 address as 2 parts. */ - private static final String ALL_ZEROS_IPV4_ADDRESS_AS_4_PARTS_REGEX = - "\\A0{1,3}(\\.0{1,3}){3}+\\z"; + private static final long MAX_PART_2_VALUE = 16777215; // (256 ^ 3) - 1 /** - * The regular expression for an IPv4 address as 1 part. + * The maximum value for part 1 in an IPv4 address as 1 part. */ - private static final String IPV4_ADDRESS_AS_1_PART_REGEX = "\\A\\d{1,10}\\z"; + private static final long MAX_PART_1_VALUE = 4294967295L; // (256 ^ 4) - 1 /** - * The regular expression for an IPv4 address as 2 parts. + * The maximum number of parts in an IPv4 address. */ - private static final String IPV4_ADDRESS_AS_2_PARTS_REGEX = - "\\A\\d{1,3}\\.\\d{1,8}\\z"; + private static final int MAX_PART_COUNT = 4; /** - * The regular expression for an IPv4 address as 3 parts. + * The index of part 1 of the raw IPv4 address in network byte order. */ - private static final String IPV4_ADDRESS_AS_3_PARTS_REGEX = - "\\A\\d{1,3}\\.\\d{1,3}\\.\\d{1,5}\\z"; + private static final int PART_1_INDEX = 0; /** - * The regular expression for an IPv4 address as 4 parts. + * The index of part 2 of the raw IPv4 address in network byte order. */ - private static final String IPV4_ADDRESS_AS_4_PARTS_REGEX = - "\\A\\d{1,3}(\\.\\d{1,3}){3}+\\z"; + private static final int PART_2_INDEX = 1; /** - * The {@code InetAddress} of an all zeros IPv4 address. + * The index of part 3 of the raw IPv4 address in network byte order. */ - private static InetAddress allZerosInetAddress; + private static final int PART_3_INDEX = 2; + + /** + * The index of part 4 of the raw IPv4 address in network byte order. + */ + private static final int PART_4_INDEX = 3; + + /** + * The {@code Inet4Address} of an all zeros IPv4 address. + */ + private static Inet4Address allZerosInet4Address; /** * The {@code HostIpv4Address} of an all zeros IPv4 address. @@ -72,13 +79,12 @@ public final class HostIpv4Address extends HostAddress { /** * Constructs a {@code HostIpv4Address} of the provided IPv4 address - * and the provided {@code InetAddress} of the provided IPv4 address. + * and the provided {@code Inet4Address}. * * @param str the provided IPv4 address - * @param inetAddr the provided {@code InetAddress} of the provided - * IPv4 address + * @param inetAddr the provided {@code Inet4Address} */ - HostIpv4Address(final String str, final InetAddress inetAddr) { + HostIpv4Address(final String str, final Inet4Address inetAddr) { super(str, inetAddr); } @@ -90,26 +96,28 @@ public final class HostIpv4Address extends HostAddress { public static HostIpv4Address getAllZerosInstance() { if (allZerosInstance == null) { allZerosInstance = new HostIpv4Address( - ALL_ZEROS_IPV4_ADDRESS, getAllZerosInetAddress()); + ALL_ZEROS_IPV4_ADDRESS, getAllZerosInet4Address()); } return allZerosInstance; } /** - * Returns an {@code InetAddress} of an all zeros IPv4 address. + * Returns an {@code Inet4Address} of an all zeros IPv4 address. * - * @return an {@code InetAddress} of an all zeros IPv4 address + * @return an {@code Inet4Address} of an all zeros IPv4 address */ - public static InetAddress getAllZerosInetAddress() { - if (allZerosInetAddress == null) { + public static Inet4Address getAllZerosInet4Address() { + if (allZerosInet4Address == null) { + byte[] address = new byte[MAX_PART_COUNT]; + Arrays.fill(address, (byte) 0x00); try { - allZerosInetAddress = InetAddress.getByName( - ALL_ZEROS_IPV4_ADDRESS); + allZerosInet4Address = (Inet4Address) InetAddress.getByAddress( + address); } catch (UnknownHostException e) { throw new AssertionError(e); } } - return allZerosInetAddress; + return allZerosInet4Address; } /** @@ -121,10 +129,172 @@ public static InetAddress getAllZerosInetAddress() { * IPv4 address is an IPv4 address of all zeros */ public static boolean isAllZerosIpv4Address(final String string) { - return string.matches(ALL_ZEROS_IPV4_ADDRESS_AS_4_PARTS_REGEX) - || string.matches(ALL_ZEROS_IPV4_ADDRESS_AS_3_PARTS_REGEX) - || string.matches(ALL_ZEROS_IPV4_ADDRESS_AS_2_PARTS_REGEX) - || string.matches(ALL_ZEROS_IPV4_ADDRESS_AS_1_PART_REGEX); + InetAddress inetAddress; + try { + inetAddress = newInet4AddressFrom(string); + } catch (IllegalArgumentException e) { + return false; + } + return inetAddress.isAnyLocalAddress(); + } + + /** + * Returns a new raw IPv4 address in network byte order from the provided + * {@code String}. An {@code IllegalArgumentException} is thrown if the + * provided {@code String} is invalid. + * + * @param string the provided {@code String} + * @return a new raw IPv4 address in network byte order from the provided + * {@code String} + */ + static byte[] newAddressFrom(final String string) { + if (string.isEmpty()) { + throw new IllegalArgumentException( + "IPv6 address must not be empty"); + } + if (string.matches(BLANK_STRING_REGEX)) { + throw new IllegalArgumentException( + "IPv6 address must not be blank"); + } + String[] parts = string.split("\\.", -1); + if (parts.length > MAX_PART_COUNT) { + throw new IllegalArgumentException(String.format( + "number of parts must be no more than %s. " + + "actual number of parts is %s", + MAX_PART_COUNT, + parts.length)); + } + if (parts.length == 1) { + return newAddressFrom1Part(parts); + } + if (parts.length == 2) { + return newAddressFrom2Parts(parts); + } + if (parts.length == 3) { + return newAddressFrom3Parts(parts); + } + return newAddressFrom4Parts(parts); + } + + /** + * Returns a new raw IPv4 address in network byte order from the provided + * {@code String} array of an IPv4 address of 1 part. An + * {@code IllegalArgumentException} is thrown if the provided + * {@code String} array of an IPv4 address of 1 part is invalid. + * + * @param parts the provided {@code String} array of an IPv4 address of 1 + * part + * @return a new raw IPv4 address in network byte order from the provided + * {@code String} array of an IPv4 address of 1 part + */ + private static byte[] newAddressFrom1Part(final String[] parts) { + long partValue = newPartValueFrom( + parts[PART_1_INDEX], MAX_PART_1_VALUE); + ByteBuffer byteBuffer = ByteBuffer.allocate(Integer.BYTES); + byteBuffer.putInt((int) partValue); + return byteBuffer.array(); + } + + /** + * Returns a new raw IPv4 address in network byte order from the provided + * {@code String} array of an IPv4 address of 2 parts. An + * {@code IllegalArgumentException} is thrown if the provided + * {@code String} array of an IPv4 address of 2 parts is invalid. + * + * @param parts the provided {@code String} array of an IPv4 address of 2 + * parts + * @return a new raw IPv4 address in network byte order from the provided + * {@code String} array of an IPv4 address of 2 parts + */ + private static byte[] newAddressFrom2Parts(final String[] parts) { + long part1Value = newPartValueFrom( + parts[PART_1_INDEX], MAX_PART_4_VALUE); + long part2Value = newPartValueFrom( + parts[PART_2_INDEX], MAX_PART_2_VALUE); + ByteBuffer byteBuffer1 = ByteBuffer.allocate(Integer.BYTES); + byteBuffer1.putInt((int) part1Value); + byte[] array1 = byteBuffer1.array(); + ByteBuffer byteBuffer2 = ByteBuffer.allocate(Integer.BYTES); + byteBuffer2.putInt((int) part2Value); + byte[] array2 = byteBuffer2.array(); + byte[] address = new byte[MAX_PART_COUNT]; + address[PART_1_INDEX] = array1[Integer.BYTES - 1]; + System.arraycopy(array2, 1, address, PART_2_INDEX, 3); + return address; + } + + /** + * Returns a new raw IPv4 address in network byte order from the provided + * {@code String} array of an IPv4 address of 3 parts. An + * {@code IllegalArgumentException} is thrown if the provided + * {@code String} array of an IPv4 address of 3 parts is invalid. + * + * @param parts the provided {@code String} array of an IPv4 address of 3 + * parts + * @return a new raw IPv4 address in network byte order from the provided + * {@code String} array of an IPv4 address of 3 parts + */ + private static byte[] newAddressFrom3Parts(final String[] parts) { + long part1Value = newPartValueFrom( + parts[PART_1_INDEX], MAX_PART_4_VALUE); + long part2Value = newPartValueFrom( + parts[PART_2_INDEX], MAX_PART_4_VALUE); + long part3Value = newPartValueFrom( + parts[PART_3_INDEX], MAX_PART_3_VALUE); + ByteBuffer byteBuffer1 = ByteBuffer.allocate(Integer.BYTES); + byteBuffer1.putInt((int) part1Value); + byte[] array1 = byteBuffer1.array(); + ByteBuffer byteBuffer2 = ByteBuffer.allocate(Integer.BYTES); + byteBuffer2.putInt((int) part2Value); + byte[] array2 = byteBuffer2.array(); + ByteBuffer byteBuffer3 = ByteBuffer.allocate(Integer.BYTES); + byteBuffer3.putInt((int) part3Value); + byte[] array3 = byteBuffer3.array(); + byte[] address = new byte[MAX_PART_COUNT]; + address[PART_1_INDEX] = array1[Integer.BYTES - 1]; + address[PART_2_INDEX] = array2[Integer.BYTES - 1]; + System.arraycopy(array3, 2, address, PART_3_INDEX, 2); + return address; + } + + /** + * Returns a new raw IPv4 address in network byte order from the provided + * {@code String} array of an IPv4 address of 4 parts. An + * {@code IllegalArgumentException} is thrown if the provided + * {@code String} array of an IPv4 address of 4 parts is invalid. + * + * @param parts the provided {@code String} array of an IPv4 address of 4 + * parts + * @return a new raw IPv4 address in network byte order from the provided + * {@code String} array of an IPv4 address of 4 parts + */ + private static byte[] newAddressFrom4Parts(final String[] parts) { + long part1Value = newPartValueFrom( + parts[PART_1_INDEX], MAX_PART_4_VALUE); + long part2Value = newPartValueFrom( + parts[PART_2_INDEX], MAX_PART_4_VALUE); + long part3Value = newPartValueFrom( + parts[PART_3_INDEX], MAX_PART_4_VALUE); + long part4Value = newPartValueFrom( + parts[PART_4_INDEX], MAX_PART_4_VALUE); + ByteBuffer byteBuffer1 = ByteBuffer.allocate(Integer.BYTES); + byteBuffer1.putInt((int) part1Value); + byte[] array1 = byteBuffer1.array(); + ByteBuffer byteBuffer2 = ByteBuffer.allocate(Integer.BYTES); + byteBuffer2.putInt((int) part2Value); + byte[] array2 = byteBuffer2.array(); + ByteBuffer byteBuffer3 = ByteBuffer.allocate(Integer.BYTES); + byteBuffer3.putInt((int) part3Value); + byte[] array3 = byteBuffer3.array(); + ByteBuffer byteBuffer4 = ByteBuffer.allocate(Integer.BYTES); + byteBuffer4.putInt((int) part4Value); + byte[] array4 = byteBuffer4.array(); + byte[] address = new byte[MAX_PART_COUNT]; + address[PART_1_INDEX] = array1[Integer.BYTES - 1]; + address[PART_2_INDEX] = array2[Integer.BYTES - 1]; + address[PART_3_INDEX] = array3[Integer.BYTES - 1]; + address[PART_4_INDEX] = array4[Integer.BYTES - 1]; + return address; } /** @@ -136,22 +306,68 @@ public static boolean isAllZerosIpv4Address(final String string) { * @return a new {@code HostIpv4Address} of the provided IPv4 address */ public static HostIpv4Address newHostIpv4Address(final String string) { + return new HostIpv4Address(string, newInet4AddressFrom(string)); + } + + /** + * Returns a new {@code Inet4Address} from the provided {@code String}. An + * {@code IllegalArgumentException} is thrown if the provided + * {@code String} is invalid. + * + * @param string the provided {@code String} + * @return a new {@code Inet4Address} from the provided {@code String} + */ + private static Inet4Address newInet4AddressFrom(final String string) { String message = String.format( - "invalid IPv4 address: %s", + "invalid IPv4 address: '%s'", string); - if (!(string.matches(IPV4_ADDRESS_AS_4_PARTS_REGEX) - || string.matches(IPV4_ADDRESS_AS_3_PARTS_REGEX) - || string.matches(IPV4_ADDRESS_AS_2_PARTS_REGEX) - || string.matches(IPV4_ADDRESS_AS_1_PART_REGEX))) { - throw new IllegalArgumentException(message); + byte[] address; + try { + address = newAddressFrom(string); + } catch (IllegalArgumentException e) { + throw new IllegalArgumentException(message, e); } - InetAddress inetAddress; try { - inetAddress = InetAddress.getByName(string); + return (Inet4Address) InetAddress.getByAddress(address); } catch (UnknownHostException e) { throw new IllegalArgumentException(message, e); } - return new HostIpv4Address(string, inetAddress); + } + + /** + * Returns a new {@code long} value of the part of the IPv4 address from + * the provided {@code String} of the part of the IPv4 address. An + * {@code IllegalArgumentException} is thrown if the provided + * {@code String} of the part of the IPv4 address is invalid or if the + * new {@code long} value of the part of the IPv4 address is greater than + * the provided maximum {@code long} value of the part of the IPv4 address + * + * @param part the provided {@code String} of the part of the IPv4 + * address + * @param maxPartValue the provided maximum {@code long} value of the part + * of the IPv4 address + * @return a new {@code long} value of the part of the IPv4 address from + * the provided {@code String} of the part of the IPv4 address + */ + private static long newPartValueFrom( + final String part, final long maxPartValue) { + long partValue; + try { + partValue = Long.parseLong(part); + } catch (IllegalArgumentException e) { + throw new IllegalArgumentException(String.format( + "part must be an integer no more than %s. " + + "actual part is '%s'", + maxPartValue, + part)); + } + if (partValue > maxPartValue) { + throw new IllegalArgumentException(String.format( + "part must be no more than %s. actual part is %s", + maxPartValue, + partValue)); + } + return partValue; } } diff --git a/jargyle-common/src/main/java/com/github/jh3nd3rs0n/jargyle/common/net/HostIpv6Address.java b/jargyle-common/src/main/java/com/github/jh3nd3rs0n/jargyle/common/net/HostIpv6Address.java index dd253f884..0a6949902 100644 --- a/jargyle-common/src/main/java/com/github/jh3nd3rs0n/jargyle/common/net/HostIpv6Address.java +++ b/jargyle-common/src/main/java/com/github/jh3nd3rs0n/jargyle/common/net/HostIpv6Address.java @@ -1,7 +1,9 @@ package com.github.jh3nd3rs0n.jargyle.common.net; -import java.net.InetAddress; -import java.net.UnknownHostException; +import java.io.UncheckedIOException; +import java.net.*; +import java.nio.ByteBuffer; +import java.util.Arrays; /** * An IPv6 address of a node of a network. @@ -9,41 +11,110 @@ public final class HostIpv6Address extends HostAddress { /** - * The regular expression for an all zeros IPv6 address in compressed form. + * The length of a raw IPv6 address in network byte order. */ - private static final String ALL_ZEROS_IPV6_ADDRESS_IN_COMPRESSED_FORM_REGEX = - "\\A(0{0,4}:){1,7}(:0{0,4}){1,7}\\z"; + private static final int ADDRESS_LENGTH = 16; /** - * The regular expression for an all zeros IPv6 address in full form. + * The regular expression for a blank {@code String}. */ - private static final String ALL_ZEROS_IPV6_ADDRESS_IN_FULL_FORM_REGEX = - "\\A0{1,4}(:0{1,4}){7}+\\z"; + private static final String BLANK_STRING_REGEX = "\\A\\s+\\z"; /** - * The regular expression for an IPv6 address in compressed form. + * The regular expression for a hexadecimal value of a 16-bit piece in a + * IPv6 address. */ - private static final String IPV6_ADDRESS_IN_COMPRESSED_FORM_REGEX = - "\\A([a-fA-F0-9]{0,4}:){1,7}(:[a-fA-F0-9]{0,4}){1,7}\\z"; + private static final String HEX_VALUE_16_BIT_PIECE_REGEX = + "\\A[a-fA-F0-9]{1,4}\\z"; /** - * The regular expression for an IPv6 address in full form. + * The regular expression for an IPv4 address in 4 parts. */ - private static final String IPV6_ADDRESS_IN_FULL_FORM_REGEX = - "\\A[a-fA-F0-9]{1,4}(:[a-fA-F0-9]{1,4}){7}+\\z"; + private static final String IP_V4_ADDRESS_PIECE_REGEX = + "\\A[0-9]{1,3}(\\.[0-9]{1,3}){3}\\z"; + + /** + * The start index for the raw IPv4 address in a raw IPv6 address in + * network byte order. + */ + private static final int IP_V4_ADDRESS_START_INDEX = 12; + + /** + * The maximum number of pieces in a IPv6 address without the use of + * {@code ::} and without the IPv4 address. + */ + private static final int MAX_PIECE_COUNT = 8; + + /** + * The regular expression for a non-negative integer. + */ + private static final String NON_NEGATIVE_INTEGER_REGEX = "\\A\\d+\\z"; /** * Constructs a {@code HostIpv6Address} of the provided IPv6 address - * and the provided {@code InetAddress} of the provided IPv6 address. + * and the provided {@code Inet6Address}. * * @param str the provided IPv6 address - * @param inetAddr the provided {@code InetAddress} of the provided - * IPv6 address + * @param inetAddr the provided {@code Inet6Address} */ - HostIpv6Address(final String str, final InetAddress inetAddr) { + HostIpv6Address(final String str, final Inet6Address inetAddr) { super(str, inetAddr); } + /** + * Returns the scope ID as an {@code int} from the provided zone + * identifier. The provided zone identifier must be non-negative integer. + * An {@code IllegalArgumentException} is thrown if the provided zone + * identifier is not a non-negative integer. + * + * @param zoneIdentifier the provided zone identifier (a non-negative + * integer) + * @return the scope ID as an {@code int} from the provided zone + * identifier + */ + private static int getScopeIdAsIntFrom(final String zoneIdentifier) { + int scopeId; + try { + scopeId = Integer.parseInt(zoneIdentifier); + } catch (IllegalArgumentException e) { + throw new IllegalArgumentException(String.format( + "invalid scope id: scope id must be an " + + "integer between 0 and %s " + + "(inclusive). actual scope id is %s", + Integer.MAX_VALUE, + zoneIdentifier)); + } + return scopeId; + } + + /** + * Returns the scope ID as a {@code NetworkInterface} from the provided + * zone identifier. An {@code IllegalArgumentException} is thrown if + * there is no {@code NetworkInterface} of the provided zone identifier. + * An {@code UncheckedIOException} is thrown if I/O occurs in searching + * for the {@code NetworkInterface} of the provided zone identifier. + * + * @param zoneIdentifier the provided zone identifier + * @return the scope ID as a {@code NetworkInterface} from the provided + * zone identifier + */ + private static NetworkInterface getScopeIdAsNetworkInterfaceFrom( + final String zoneIdentifier) { + NetworkInterface networkInterface; + try { + networkInterface = NetworkInterface.getByName( + zoneIdentifier); + } catch (SocketException e) { + throw new UncheckedIOException(e); + } + if (networkInterface == null) { + throw new IllegalArgumentException(String.format( + "unknown scope id: '%s'", + zoneIdentifier)); + } + return networkInterface; + } + /** * Returns a {@code boolean} value to indicate if the provided * IPv6 address is an IPv6 address of all zeros. @@ -53,38 +124,195 @@ public final class HostIpv6Address extends HostAddress { * IPv6 address is an IPv6 address of all zeros */ public static boolean isAllZerosIpv6Address(final String string) { - return string.matches(ALL_ZEROS_IPV6_ADDRESS_IN_FULL_FORM_REGEX) - || (string.matches( - ALL_ZEROS_IPV6_ADDRESS_IN_COMPRESSED_FORM_REGEX) - && string.split("::").length <= 2 - && string.split(":").length <= 8); + InetAddress inetAddress; + try { + inetAddress = newInet6AddressFrom(string); + } catch (IllegalArgumentException e) { + return false; + } + return inetAddress.isAnyLocalAddress(); + } + + /** + * Returns a new raw IPv6 address in network byte order from the provided + * {@code String}. An {@code IllegalArgumentException} is thrown if the + * provided {@code String} is invalid. + * + * @param string the provided {@code String} + * @return a new raw IPv6 address in network byte order from the provided + * {@code String} + */ + private static byte[] newAddressFrom(final String string) { + if (string.isEmpty()) { + throw new IllegalArgumentException( + "IPv6 address must not be empty"); + } + if (string.matches(BLANK_STRING_REGEX)) { + throw new IllegalArgumentException( + "IPv6 address must not be blank"); + } + String[] pieces = string.split(":", -1); + if (pieces.length > MAX_PIECE_COUNT) { + throw new IllegalArgumentException(String.format( + "number of pieces delimited by ':' must no more than %s. " + + "actual number of pieces is %s", + MAX_PIECE_COUNT, + pieces.length)); + } + byte[] address = new byte[ADDRESS_LENGTH]; + Arrays.fill(address, (byte) 0x00); + int offset = 0; + boolean compressedPresent = false; + boolean ipv4AddressPresent = false; + for (int i = 0; i < pieces.length; i++) { + String piece = pieces[i]; + if (piece.isEmpty()) { + if (i == 0 && !pieces[i + 1].isEmpty()) { + throw new IllegalArgumentException( + "first piece cannot be empty if the second piece " + + "is not empty"); + } + if (i == pieces.length - 1 && !pieces[i - 1].isEmpty()) { + throw new IllegalArgumentException( + "last piece cannot be empty if the second to last " + + "piece is not empty"); + } + if (i > 0 && i < pieces.length - 1) { + if (compressedPresent) { + throw new IllegalArgumentException( + "use of '::' can only appear once"); + } + offset = MAX_PIECE_COUNT - pieces.length; + compressedPresent = true; + } + continue; + } + if (piece.matches(HEX_VALUE_16_BIT_PIECE_REGEX)) { + int hexValue16BitPiece = Integer.parseInt(piece, 16); + ByteBuffer byteBuffer = ByteBuffer.allocate(Short.BYTES); + byteBuffer.putShort((short) hexValue16BitPiece); + byte[] array = byteBuffer.array(); + System.arraycopy( + array, + 0, + address, + (i + offset) * Short.BYTES, + array.length); + continue; + } + if (piece.matches(IP_V4_ADDRESS_PIECE_REGEX)) { + if (i < pieces.length - 1 + || pieces.length == MAX_PIECE_COUNT) { + throw new IllegalArgumentException( + "piece cannot be an IPv4 address if the piece is " + + "not the last and at most the seventh"); + } + byte[] ipv4Address; + try { + ipv4Address = HostIpv4Address.newAddressFrom(piece); + } catch (IllegalArgumentException e) { + throw new IllegalArgumentException(String.format( + "invalid IPv4 address: %s", + piece), + e); + } + System.arraycopy( + ipv4Address, + 0, + address, + IP_V4_ADDRESS_START_INDEX, + ipv4Address.length); + ipv4AddressPresent = true; + continue; + } + throw new IllegalArgumentException(String.format( + "invalid piece: '%s'", + piece)); + } + if (!compressedPresent) { + if (!ipv4AddressPresent) { + if (pieces.length < MAX_PIECE_COUNT) { + throw new IllegalArgumentException(String.format( + "number of pieces with no use of '::' present and " + + "with no IPv4 address present must be " + + "%s. actual number of pieces is %s", + MAX_PIECE_COUNT, + pieces.length)); + } + } else { + if (pieces.length < MAX_PIECE_COUNT - 1) { + throw new IllegalArgumentException(String.format( + "number of pieces with no use of '::' present and " + + "with the IPv4 address present must be " + + "%s. actual number of pieces is %s", + MAX_PIECE_COUNT - 1, + pieces.length)); + } + } + } + return address; } /** * Returns a new {@code HostIpv6Address} of the provided IPv6 address. * An {@code IllegalArgumentException} is thrown if the provided - * IPv6 address is invalid. + * IPv6 address is invalid. An {@code UncheckedIOException} is thrown if + * I/O occurs in searching for the network interface from the zone + * identifier of the IPv6 address of the provided {@code String}. * * @param string the provided IPv6 address * @return a new {@code HostIpv6Address} of the provided IPv6 address */ public static HostIpv6Address newHostIpv6Address(final String string) { + return new HostIpv6Address(string, newInet6AddressFrom(string)); + } + + /** + * Returns a new {@code Inet6Address} from the provided {@code String}. + * An {@code IllegalArgumentException} is thrown if the provided + * {@code String} is invalid. An {@code UncheckedIOException} is thrown if + * I/O occurs in searching for the network interface from the zone + * identifier of the IPv6 address of the provided {@code String}. + * + * @param string the provided {@code String} + * @return a new {@code Inet6Address} from the provided {@code String} + */ + private static Inet6Address newInet6AddressFrom(final String string) { String message = String.format( - "invalid IPv6 address: %s", + "invalid IPv6 address: '%s'", string); - if (!(string.matches(IPV6_ADDRESS_IN_FULL_FORM_REGEX) - || (string.matches(IPV6_ADDRESS_IN_COMPRESSED_FORM_REGEX) - && string.split("::").length <= 2 - && string.split(":").length <= 8))) { - throw new IllegalArgumentException(message); + String ipv6Address = string; + String zoneIdentifier = null; + int zoneIdentifierDelimiterIndex = string.indexOf('%'); + if (zoneIdentifierDelimiterIndex > -1) { + ipv6Address = string.substring(0, zoneIdentifierDelimiterIndex); + zoneIdentifier = string.substring( + zoneIdentifierDelimiterIndex + 1); + } + byte[] address; + try { + address = newAddressFrom(ipv6Address); + } catch (IllegalArgumentException e) { + throw new IllegalArgumentException(message, e); } - InetAddress inetAddress; try { - inetAddress = InetAddress.getByName(string); - } catch (UnknownHostException e) { + if (zoneIdentifier == null) { + return (Inet6Address) InetAddress.getByAddress(address); + } else { + if (zoneIdentifier.matches(NON_NEGATIVE_INTEGER_REGEX)) { + return Inet6Address.getByAddress( + string, + address, + getScopeIdAsIntFrom(zoneIdentifier)); + } + return Inet6Address.getByAddress( + string, + address, + getScopeIdAsNetworkInterfaceFrom(zoneIdentifier)); + } + } catch (IllegalArgumentException | UnknownHostException e) { throw new IllegalArgumentException(message, e); } - return new HostIpv6Address(string, inetAddress); } } diff --git a/jargyle-common/src/main/java/com/github/jh3nd3rs0n/jargyle/common/net/HostName.java b/jargyle-common/src/main/java/com/github/jh3nd3rs0n/jargyle/common/net/HostName.java index abe330297..403f7ae03 100644 --- a/jargyle-common/src/main/java/com/github/jh3nd3rs0n/jargyle/common/net/HostName.java +++ b/jargyle-common/src/main/java/com/github/jh3nd3rs0n/jargyle/common/net/HostName.java @@ -9,11 +9,11 @@ public final class HostName extends Host { /** - * The regular expression for a host name. + * The regular expression for a host name defined by RFC 2396. */ private static final String HOST_NAME_REGEX = - "\\A([a-z0-9]|[a-z0-9][a-z0-9-_]*[a-z0-9])" - + "(\\.([a-z0-9]|[a-z0-9][a-z0-9-_]*[a-z0-9]))*\\z"; + "\\A(([a-z0-9]|[a-z0-9][a-z0-9\\-]*[a-z0-9])\\.)*" + + "([a-z]|[a-z][a-z0-9\\-]*[a-z0-9])\\.?\\z"; /** * Constructs a {@code HostName} of the provided host name. @@ -35,7 +35,7 @@ public final class HostName extends Host { public static HostName newHostName(final String string) { if (!string.matches(HOST_NAME_REGEX)) { throw new IllegalArgumentException(String.format( - "invalid host name: %s", + "invalid host name: '%s'", string)); } return new HostName(string); diff --git a/jargyle-common/src/test/java/com/github/jh3nd3rs0n/jargyle/common/net/HostIpv4AddressTest.java b/jargyle-common/src/test/java/com/github/jh3nd3rs0n/jargyle/common/net/HostIpv4AddressTest.java index 3d773d494..4746e1a7e 100644 --- a/jargyle-common/src/test/java/com/github/jh3nd3rs0n/jargyle/common/net/HostIpv4AddressTest.java +++ b/jargyle-common/src/test/java/com/github/jh3nd3rs0n/jargyle/common/net/HostIpv4AddressTest.java @@ -66,10 +66,10 @@ public void testGetAllZerosInstance() { } @Test - public void testGetAllZerosInetAddress() throws UnknownHostException { + public void testGetAllZerosInet4Address() throws UnknownHostException { InetAddress inetAddress = InetAddress.getByName("0.0.0.0"); Assert.assertEquals( - inetAddress, HostIpv4Address.getAllZerosInetAddress()); + inetAddress, HostIpv4Address.getAllZerosInet4Address()); } @Test @@ -141,6 +141,21 @@ public void testNewHostIpv4AddressString01() { Assert.assertNotNull(HostIpv4Address.newHostIpv4Address("127.0.0.1")); } + @Test + public void testNewHostIpv4AddressString02() { + Assert.assertNotNull(HostIpv4Address.newHostIpv4Address("127.0.0")); + } + + @Test + public void testNewHostIpv4AddressString03() { + Assert.assertNotNull(HostIpv4Address.newHostIpv4Address("127.0")); + } + + @Test + public void testNewHostIpv4AddressString04() { + Assert.assertNotNull(HostIpv4Address.newHostIpv4Address("127")); + } + @Test(expected = IllegalArgumentException.class) public void testNewHostIpv4AddressStringForIllegalArgumentException01() { HostIpv4Address.newHostIpv4Address("localhost"); @@ -151,4 +166,126 @@ public void testNewHostIpv4AddressStringForIllegalArgumentException02() { HostIpv4Address.newHostIpv4Address("::1"); } + @Test(expected = IllegalArgumentException.class) + public void testNewHostIpv4AddressStringForIllegalArgumentException03() { + HostIpv4Address.newHostIpv4Address("127.127.127.127.127"); + } + + @Test(expected = IllegalArgumentException.class) + public void testNewHostIpv4AddressStringForIllegalArgumentException04() { + HostIpv4Address.newHostIpv4Address(""); + } + + @Test(expected = IllegalArgumentException.class) + public void testNewHostIpv4AddressStringForIllegalArgumentException05() { + HostIpv4Address.newHostIpv4Address(" "); + } + + @Test(expected = IllegalArgumentException.class) + public void testNewHostIpv4AddressStringForIllegalArgumentException06() { + HostIpv4Address.newHostIpv4Address("."); + } + + @Test(expected = IllegalArgumentException.class) + public void testNewHostIpv4AddressStringForIllegalArgumentException07() { + HostIpv4Address.newHostIpv4Address(".255.255.255"); + } + + @Test(expected = IllegalArgumentException.class) + public void testNewHostIpv4AddressStringForIllegalArgumentException08() { + HostIpv4Address.newHostIpv4Address("255.255.255."); + } + + @Test(expected = IllegalArgumentException.class) + public void testNewHostIpv4AddressStringForIllegalArgumentException09() { + HostIpv4Address.newHostIpv4Address("255.255.255.256"); + } + + @Test(expected = IllegalArgumentException.class) + public void testNewHostIpv4AddressStringForIllegalArgumentException10() { + HostIpv4Address.newHostIpv4Address("255.255.65536"); + } + + @Test(expected = IllegalArgumentException.class) + public void testNewHostIpv4AddressStringForIllegalArgumentException11() { + HostIpv4Address.newHostIpv4Address("255.16777216"); + } + + @Test(expected = IllegalArgumentException.class) + public void testNewHostIpv4AddressStringForIllegalArgumentException12() { + HostIpv4Address.newHostIpv4Address("4294967296"); + } + + @Test + public void testToInetAddress01() throws UnknownHostException { + HostIpv4Address hostIpv4Address = HostIpv4Address.newHostIpv4Address( + "255.255.255.255"); + InetAddress inetAddress = InetAddress.getByName("255.255.255.255"); + Assert.assertEquals(inetAddress, hostIpv4Address.toInetAddress()); + } + + @Test + public void testToInetAddress02() throws UnknownHostException { + HostIpv4Address hostIpv4Address = HostIpv4Address.newHostIpv4Address( + "255.255.65535"); + InetAddress inetAddress = InetAddress.getByName("255.255.65535"); + Assert.assertEquals(inetAddress, hostIpv4Address.toInetAddress()); + } + + @Test + public void testToInetAddress03() throws UnknownHostException { + HostIpv4Address hostIpv4Address = HostIpv4Address.newHostIpv4Address( + "255.16777215"); + InetAddress inetAddress = InetAddress.getByName("255.16777215"); + Assert.assertEquals(inetAddress, hostIpv4Address.toInetAddress()); + } + + @Test + public void testToInetAddress04() throws UnknownHostException { + HostIpv4Address hostIpv4Address = HostIpv4Address.newHostIpv4Address( + "4294967295"); + InetAddress inetAddress = InetAddress.getByName("4294967295"); + Assert.assertEquals(inetAddress, hostIpv4Address.toInetAddress()); + } + + @Test + public void testToInetAddress05() throws UnknownHostException { + HostIpv4Address hostIpv4Address = HostIpv4Address.newHostIpv4Address( + "127.0.0.1"); + InetAddress inetAddress = InetAddress.getByName("127.0.0.1"); + Assert.assertEquals(inetAddress, hostIpv4Address.toInetAddress()); + } + + @Test + public void testToInetAddress06() throws UnknownHostException { + HostIpv4Address hostIpv4Address = HostIpv4Address.newHostIpv4Address( + "127.0.0"); + InetAddress inetAddress = InetAddress.getByName("127.0.0"); + Assert.assertEquals(inetAddress, hostIpv4Address.toInetAddress()); + } + + @Test + public void testToInetAddress07() throws UnknownHostException { + HostIpv4Address hostIpv4Address = HostIpv4Address.newHostIpv4Address( + "127.0"); + InetAddress inetAddress = InetAddress.getByName("127.0"); + Assert.assertEquals(inetAddress, hostIpv4Address.toInetAddress()); + } + + @Test + public void testToInetAddress08() throws UnknownHostException { + HostIpv4Address hostIpv4Address = HostIpv4Address.newHostIpv4Address( + "127"); + InetAddress inetAddress = InetAddress.getByName("127"); + Assert.assertEquals(inetAddress, hostIpv4Address.toInetAddress()); + } + + @Test + public void testToInetAddress09() throws UnknownHostException { + HostIpv4Address hostIpv4Address = HostIpv4Address.newHostIpv4Address( + "255.255.0.6"); + InetAddress inetAddress = InetAddress.getByName("255.255.0.6"); + Assert.assertEquals(inetAddress, hostIpv4Address.toInetAddress()); + } + } \ No newline at end of file diff --git a/jargyle-common/src/test/java/com/github/jh3nd3rs0n/jargyle/common/net/HostIpv6AddressTest.java b/jargyle-common/src/test/java/com/github/jh3nd3rs0n/jargyle/common/net/HostIpv6AddressTest.java index 6399c9878..27d92d2aa 100644 --- a/jargyle-common/src/test/java/com/github/jh3nd3rs0n/jargyle/common/net/HostIpv6AddressTest.java +++ b/jargyle-common/src/test/java/com/github/jh3nd3rs0n/jargyle/common/net/HostIpv6AddressTest.java @@ -4,6 +4,8 @@ import org.junit.Test; import java.net.InetAddress; +import java.net.NetworkInterface; +import java.net.SocketException; import java.net.UnknownHostException; public class HostIpv6AddressTest { @@ -146,12 +148,156 @@ public void testNewHostIpv6AddressStringForIllegalArgumentException07() { HostIpv6Address.newHostIpv6Address(":::::::"); } + @Test(expected = IllegalArgumentException.class) + public void testNewHostIpv6AddressStringForIllegalArgumentException08() { + HostIpv6Address.newHostIpv6Address(":0000:0000:0000:0000:0000:0000:0000"); + } + + @Test(expected = IllegalArgumentException.class) + public void testNewHostIpv6AddressStringForIllegalArgumentException09() { + HostIpv6Address.newHostIpv6Address("0000:0000:0000:0000:0000:0000:0000:"); + } + + @Test(expected = IllegalArgumentException.class) + public void testNewHostIpv6AddressStringForIllegalArgumentException10() { + HostIpv6Address.newHostIpv6Address("0.0.0.0:0000:0000:0000:0000:0000:0000"); + } + + @Test(expected = IllegalArgumentException.class) + public void testNewHostIpv6AddressStringForIllegalArgumentException11() { + HostIpv6Address.newHostIpv6Address("0000:0000:0000:0000:0000:0.0.0.0:0000"); + } + + @Test(expected = IllegalArgumentException.class) + public void testNewHostIpv6AddressStringForIllegalArgumentException12() { + HostIpv6Address.newHostIpv6Address("0000:0000:0000:0000:0000:0000:0000:0.0.0.0"); + } + + @Test(expected = IllegalArgumentException.class) + public void testNewHostIpv6AddressStringForIllegalArgumentException13() { + HostIpv6Address.newHostIpv6Address("0000:0000:0000:0000:0000:0000:999.999.999.999"); + } + + @Test(expected = IllegalArgumentException.class) + public void testNewHostIpv6AddressStringForIllegalArgumentException14() { + HostIpv6Address.newHostIpv6Address("0000:0000:0000:0000:0000:0000:0.0.0"); + } + + @Test(expected = IllegalArgumentException.class) + public void testNewHostIpv6AddressStringForIllegalArgumentException15() { + HostIpv6Address.newHostIpv6Address("0000:0000:0000:0000:0000:0000:0.0"); + } + + @Test(expected = IllegalArgumentException.class) + public void testNewHostIpv6AddressStringForIllegalArgumentException16() { + HostIpv6Address.newHostIpv6Address("zero:zero:zero:zero:zero:zero:zero:zero"); + } + + @Test(expected = IllegalArgumentException.class) + public void testNewHostIpv6AddressStringForIllegalArgumentException17() { + HostIpv6Address.newHostIpv6Address("::1%10000000000000000000000000000"); + } + + @Test(expected = IllegalArgumentException.class) + public void testNewHostIpv6AddressStringForIllegalArgumentException18() { + HostIpv6Address.newHostIpv6Address("::1%bogus"); + } + + @Test(expected = IllegalArgumentException.class) + public void testNewHostIpv6AddressStringForIllegalArgumentException19() { + HostIpv6Address.newHostIpv6Address(" "); + } + + @Test(expected = IllegalArgumentException.class) + public void testNewHostIpv6AddressStringForIllegalArgumentException20() { + HostIpv6Address.newHostIpv6Address(""); + } + + @Test(expected = IllegalArgumentException.class) + public void testNewHostIpv6AddressStringForIllegalArgumentException21() { + HostIpv6Address.newHostIpv6Address("0000:0000:0000"); + } + + @Test(expected = IllegalArgumentException.class) + public void testNewHostIpv6AddressStringForIllegalArgumentException22() { + HostIpv6Address.newHostIpv6Address("0000: :0000"); + } + @Test - public void testToInetAddress() throws UnknownHostException { + public void testToInetAddress01() throws UnknownHostException { InetAddress inetAddress1 = HostIpv6Address.newHostIpv6Address( "::1").toInetAddress(); InetAddress inetAddress2 = InetAddress.getByName("0:0:0:0:0:0:0:1"); Assert.assertEquals(inetAddress1, inetAddress2); } + @Test + public void testToInetAddress02() throws UnknownHostException { + InetAddress inetAddress1 = HostIpv6Address.newHostIpv6Address( + "1080::8:800:200C:417A").toInetAddress(); + InetAddress inetAddress2 = InetAddress.getByName("1080:0:0:0:8:800:200C:417A"); + Assert.assertEquals(inetAddress1, inetAddress2); + } + + @Test + public void testToInetAddress03() throws UnknownHostException { + InetAddress inetAddress1 = HostIpv6Address.newHostIpv6Address( + "1080:8:800:200C::417A").toInetAddress(); + InetAddress inetAddress2 = InetAddress.getByName("1080:8:800:200C:0:0:0:417A"); + Assert.assertEquals(inetAddress1, inetAddress2); + } + + @Test + public void testToInetAddress04() throws UnknownHostException { + InetAddress inetAddress1 = HostIpv6Address.newHostIpv6Address( + "::1080:8:800:200C:417A").toInetAddress(); + InetAddress inetAddress2 = InetAddress.getByName("0:0:0:1080:8:800:200C:417A"); + Assert.assertEquals(inetAddress1, inetAddress2); + } + + @Test + public void testToInetAddress05() throws UnknownHostException { + InetAddress inetAddress1 = HostIpv6Address.newHostIpv6Address( + "1080:8:800:200C:417A::").toInetAddress(); + InetAddress inetAddress2 = InetAddress.getByName("1080:8:800:200C:417A:0:0:0"); + Assert.assertEquals(inetAddress1, inetAddress2); + } + + @Test + public void testToInetAddress06() throws UnknownHostException { + InetAddress inetAddress1 = HostIpv6Address.newHostIpv6Address( + "::255.255.0.6").toInetAddress(); + InetAddress inetAddress2 = InetAddress.getByName("::FFFF:6"); + Assert.assertEquals(inetAddress1, inetAddress2); + } + + @Test + public void testToInetAddress07() throws UnknownHostException { + InetAddress inetAddress1 = HostIpv6Address.newHostIpv6Address( + "::1%0").toInetAddress(); + InetAddress inetAddress2 = InetAddress.getByName("::1%0"); + Assert.assertEquals(inetAddress1, inetAddress2); + } + + @Test + public void testToInetAddress08() throws UnknownHostException, SocketException { + NetworkInterface networkInterface = NetworkInterface.getByInetAddress( + InetAddress.getByName("::1")); + String networkInterfaceName = networkInterface.getName(); + InetAddress inetAddress1 = HostIpv6Address.newHostIpv6Address( + "::1%".concat(networkInterfaceName)).toInetAddress(); + InetAddress inetAddress2 = InetAddress.getByName("::1%".concat( + networkInterfaceName)); + Assert.assertEquals(inetAddress1, inetAddress2); + } + + @Test + public void testToInetAddress09() throws UnknownHostException { + InetAddress inetAddress1 = HostIpv6Address.newHostIpv6Address( + "0000:0000:0000:0000:0000:0000:0.0.0.0").toInetAddress(); + InetAddress inetAddress2 = InetAddress.getByName( + "0000:0000:0000:0000:0000:0000:0.0.0.0"); + Assert.assertEquals(inetAddress1, inetAddress2); + } + } \ No newline at end of file diff --git a/jargyle-common/src/test/java/com/github/jh3nd3rs0n/jargyle/common/net/HostNameTest.java b/jargyle-common/src/test/java/com/github/jh3nd3rs0n/jargyle/common/net/HostNameTest.java index a9f583e7b..9bb61b295 100644 --- a/jargyle-common/src/test/java/com/github/jh3nd3rs0n/jargyle/common/net/HostNameTest.java +++ b/jargyle-common/src/test/java/com/github/jh3nd3rs0n/jargyle/common/net/HostNameTest.java @@ -70,6 +70,61 @@ public void testNewHostNameString03() { Assert.assertNotNull(HostName.newHostName("jh3nd3rs0n.github.io")); } + @Test + public void testNewHostNameString04() { + Assert.assertNotNull(HostName.newHostName("a")); + } + + @Test + public void testNewHostNameString05() { + Assert.assertNotNull(HostName.newHostName("a.a")); + } + + @Test + public void testNewHostNameString06() { + Assert.assertNotNull(HostName.newHostName("a.a.a")); + } + + @Test + public void testNewHostNameString07() { + Assert.assertNotNull(HostName.newHostName("a.a.a.")); + } + + @Test + public void testNewHostNameString08() { + Assert.assertNotNull(HostName.newHostName("0.a")); + } + + @Test + public void testNewHostNameString09() { + Assert.assertNotNull(HostName.newHostName("0.0.a")); + } + + @Test + public void testNewHostNameString10() { + Assert.assertNotNull(HostName.newHostName("0.0.a.")); + } + + @Test + public void testNewHostNameString11() { + Assert.assertNotNull(HostName.newHostName("a-a.a-a")); + } + + @Test + public void testNewHostNameString12() { + Assert.assertNotNull(HostName.newHostName("0-0.a-0")); + } + + @Test + public void testNewHostNameString13() { + Assert.assertNotNull(HostName.newHostName("127.0.0.1.a")); + } + + @Test + public void testNewHostNameString14() { + Assert.assertNotNull(HostName.newHostName("127-0-0-1.a")); + } + @Test(expected = IllegalArgumentException.class) public void testNewHostNameStringForIllegalArgumentException01() { HostName.newHostName("LOCALHOST"); @@ -80,11 +135,41 @@ public void testNewHostNameStringForIllegalArgumentException02() { HostName.newHostName("@#$%^&"); } + @Test(expected = IllegalArgumentException.class) + public void testNewHostNameStringForIllegalArgumentException03() { + HostName.newHostName("0"); + } + + @Test(expected = IllegalArgumentException.class) + public void testNewHostNameStringForIllegalArgumentException04() { + HostName.newHostName("0.0"); + } + + @Test(expected = IllegalArgumentException.class) + public void testNewHostNameStringForIllegalArgumentException05() { + HostName.newHostName("0.0.0"); + } + + @Test(expected = IllegalArgumentException.class) + public void testNewHostNameStringForIllegalArgumentException06() { + HostName.newHostName("-"); + } + + @Test(expected = IllegalArgumentException.class) + public void testNewHostNameStringForIllegalArgumentException07() { + HostName.newHostName("0.-"); + } + + @Test(expected = IllegalArgumentException.class) + public void testNewHostNameStringForIllegalArgumentException08() { + HostName.newHostName("0-0.-0-"); + } + @Test public void testToInetAddress() throws UnknownHostException { InetAddress inetAddress1 = HostName.newHostName( "localhost").toInetAddress(); - InetAddress inetAddress2 = InetAddress.getByName("127.0.0.1"); + InetAddress inetAddress2 = InetAddress.getByName("localhost"); Assert.assertEquals(inetAddress1, inetAddress2); } diff --git a/jargyle-protocolbase/src/test/java/com/github/jh3nd3rs0n/jargyle/protocolbase/socks5/AddressTest.java b/jargyle-protocolbase/src/test/java/com/github/jh3nd3rs0n/jargyle/protocolbase/socks5/AddressTest.java index d4c609757..fd4107172 100644 --- a/jargyle-protocolbase/src/test/java/com/github/jh3nd3rs0n/jargyle/protocolbase/socks5/AddressTest.java +++ b/jargyle-protocolbase/src/test/java/com/github/jh3nd3rs0n/jargyle/protocolbase/socks5/AddressTest.java @@ -175,7 +175,7 @@ public void testNewInstanceFromStringStringForIllegalArgumentException03() { @Test(expected = IllegalArgumentException.class) public void testNewInstanceFromStringStringForIllegalArgumentException04() { - Address.newInstanceFrom("google."); + Address.newInstanceFrom("2google."); } @Test(expected = IllegalArgumentException.class)