8315767: InetAddress: constructing objects from BSD literal addresses
Reviewed-by: dfuchs, aefimov, michaelm, jpai
This commit is contained in:
parent
2a11e0da02
commit
c2180d141c
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2000, 2023, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -70,9 +70,9 @@ import java.util.Objects;
|
|||||||
* <p> When only one part is given, the value is stored directly in
|
* <p> When only one part is given, the value is stored directly in
|
||||||
* the network address without any byte rearrangement.
|
* the network address without any byte rearrangement.
|
||||||
*
|
*
|
||||||
* <p> These forms support parts specified in decimal format only.
|
* <p> For example, the following (decimal) forms are supported by the methods
|
||||||
* For example, the following forms are supported by methods capable
|
* {@link Inet4Address#ofLiteral(String)} and {@link InetAddress#getByName(String)}
|
||||||
* of parsing textual representations of IPv4 addresses:
|
* which are capable of parsing textual representations of IPv4 addresses:
|
||||||
* {@snippet :
|
* {@snippet :
|
||||||
* // Dotted-decimal 'd.d.d.d' form with four part address literal
|
* // Dotted-decimal 'd.d.d.d' form with four part address literal
|
||||||
* InetAddress.getByName("007.008.009.010"); // ==> /7.8.9.10
|
* InetAddress.getByName("007.008.009.010"); // ==> /7.8.9.10
|
||||||
@ -93,8 +93,16 @@ import java.util.Objects;
|
|||||||
* Inet4Address.ofLiteral("02130706689"); // ==> /127.0.1.1
|
* Inet4Address.ofLiteral("02130706689"); // ==> /127.0.1.1
|
||||||
* }
|
* }
|
||||||
*
|
*
|
||||||
|
* <p> The above forms adhere to "strict" decimal-only syntax.
|
||||||
|
* Additionally, the {@link Inet4Address#ofPosixLiteral(String)}
|
||||||
|
* method implements a POSIX {@code inet_addr} compatible "loose"
|
||||||
|
* parsing algorithm, allowing octal and hexadecimal address segments.
|
||||||
|
* Please refer to <a href="https://www.ietf.org/rfc/rfc6943.html#section-3.1.1">
|
||||||
|
* <i>RFC 6943: Issues in Identifier Comparison for Security
|
||||||
|
* Purposes</i></a>. Aside from {@code Inet4Address.ofPosixLiteral(String)}, all methods only
|
||||||
|
* support strict decimal parsing.
|
||||||
* <p> For methods that return a textual representation as output
|
* <p> For methods that return a textual representation as output
|
||||||
* value, the first form, i.e. a dotted-quad string, is used.
|
* value, the first form, i.e. a dotted-quad string in strict decimal notation, is used.
|
||||||
*
|
*
|
||||||
* <h3> The Scope of a Multicast Address </h3>
|
* <h3> The Scope of a Multicast Address </h3>
|
||||||
*
|
*
|
||||||
@ -112,6 +120,8 @@ import java.util.Objects;
|
|||||||
* RFC 2365: Administratively Scoped IP Multicast
|
* RFC 2365: Administratively Scoped IP Multicast
|
||||||
* @spec https://www.rfc-editor.org/info/rfc790
|
* @spec https://www.rfc-editor.org/info/rfc790
|
||||||
* RFC 790: Assigned numbers
|
* RFC 790: Assigned numbers
|
||||||
|
* @spec https://www.rfc-editor.org/rfc/rfc6943.html#section-3.1.1
|
||||||
|
* RFC 6943: Issues in Identifier Comparison for Security Purposes
|
||||||
* @since 1.4
|
* @since 1.4
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -180,6 +190,72 @@ class Inet4Address extends InetAddress {
|
|||||||
return parseAddressString(ipv4AddressLiteral, true);
|
return parseAddressString(ipv4AddressLiteral, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an {@code Inet4Address} based on the provided {@linkplain
|
||||||
|
* Inet4Address##format-posix textual representation of an IPv4 address in
|
||||||
|
* POSIX {@code inet_addr} compatible form}.
|
||||||
|
* <p> <a id="format-posix"></a> The method {@code ofPosixLiteral}
|
||||||
|
* implements <a href="https://pubs.opengroup.org/onlinepubs/9699919799/functions/inet_addr.html">
|
||||||
|
* POSIX {@code inet_addr}</a> compatible parsing algorithm, allowing
|
||||||
|
* octal and hexadecimal address segments. {@code "0"} is the prefix
|
||||||
|
* for octal numbers, {@code "0x"} and {@code "0X"} are the prefixes
|
||||||
|
* for hexadecimal numbers. Non-zero address segments that start from
|
||||||
|
* non-zero digits are parsed as decimal numbers. The following
|
||||||
|
* (non-decimal) forms are supported by this method:
|
||||||
|
* {@snippet :
|
||||||
|
* // Dotted-quad 'x.x.x.x' form with four part address literal
|
||||||
|
* Inet4Address.ofPosixLiteral("0177.0.0.1"); // ==> /127.0.0.1
|
||||||
|
* Inet4Address.ofPosixLiteral("0x7F.0.0.1"); // ==> /127.0.0.1
|
||||||
|
*
|
||||||
|
* // Dotted-triple 'x.x.x' form with three part address literal,
|
||||||
|
* // the last part is placed in the rightmost two bytes
|
||||||
|
* // of the constructed address
|
||||||
|
* Inet4Address.ofPosixLiteral("0177.0.0402"); // ==> /127.0.1.2
|
||||||
|
* Inet4Address.ofPosixLiteral("0x7F.0.0x102"); // ==> /127.0.1.2
|
||||||
|
*
|
||||||
|
* // Dotted-double 'x.x' form with two part address literal,
|
||||||
|
* // the last part is placed in the rightmost three bytes
|
||||||
|
* // of the constructed address
|
||||||
|
* Inet4Address.ofPosixLiteral("0177.0201003"); // ==> /127.1.2.3
|
||||||
|
* Inet4Address.ofPosixLiteral("0x7F.0x10203"); // ==> /127.1.2.3
|
||||||
|
* Inet4Address.ofPosixLiteral("127.66051"); // ==> /127.1.2.3
|
||||||
|
*
|
||||||
|
* // Dotless 'x' form with one value that is stored directly in
|
||||||
|
* // the constructed address bytes without any rearrangement
|
||||||
|
* Inet4Address.ofPosixLiteral("0100401404"); // ==> /1.2.3.4
|
||||||
|
* Inet4Address.ofPosixLiteral("0x1020304"); // ==> /1.2.3.4
|
||||||
|
* Inet4Address.ofPosixLiteral("16909060"); // ==> /1.2.3.4
|
||||||
|
* }
|
||||||
|
* <p> If the provided IPv4 address literal cannot represent a
|
||||||
|
* valid IPv4 address in {@linkplain Inet4Address##format-posix
|
||||||
|
* POSIX form} an {@code IllegalArgumentException} is thrown.
|
||||||
|
* <p> This method doesn't block, i.e. no hostname lookup is performed.
|
||||||
|
*
|
||||||
|
* @apiNote
|
||||||
|
* This method produces different results compared to {@linkplain Inet4Address#ofLiteral}
|
||||||
|
* when {@code posixIPAddressLiteral} parameter contains address segments with
|
||||||
|
* leading zeroes. An address segment with a leading zero is always parsed as an octal
|
||||||
|
* number by this method, therefore {@code 0255} (octal) will be parsed as
|
||||||
|
* {@code 173} (decimal). On the other hand, {@link Inet4Address#ofLiteral
|
||||||
|
* Inet4Address.ofLiteral} ignores leading zeros, parses all numbers as decimal and produces
|
||||||
|
* {@code 255}. Where this method would parse {@code 0256.0256.0256.0256} (octal) and
|
||||||
|
* produce {@code 174.174.174.174} (decimal) in four dotted quad notation,
|
||||||
|
* {@link Inet4Address#ofLiteral Inet4Address.ofLiteral} will throw
|
||||||
|
* {@code IllegalArgumentException}.
|
||||||
|
*
|
||||||
|
* @param posixIPAddressLiteral a textual representation of an IPv4 address.
|
||||||
|
* @return an {@link Inet4Address} object with no hostname set, and constructed
|
||||||
|
* from the provided IPv4 address literal.
|
||||||
|
* @throws IllegalArgumentException if the {@code posixIPAddressLiteral} cannot be
|
||||||
|
* parsed as an IPv4 address literal.
|
||||||
|
* @throws NullPointerException if the {@code posixIPAddressLiteral} is {@code null}.
|
||||||
|
* @since 23
|
||||||
|
*/
|
||||||
|
public static Inet4Address ofPosixLiteral(String posixIPAddressLiteral) {
|
||||||
|
Objects.requireNonNull(posixIPAddressLiteral);
|
||||||
|
return parseAddressStringPosix(posixIPAddressLiteral);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses the given string as an IPv4 address literal.
|
* Parses the given string as an IPv4 address literal.
|
||||||
* If the given {@code addressLiteral} string cannot be parsed as an IPv4 address literal
|
* If the given {@code addressLiteral} string cannot be parsed as an IPv4 address literal
|
||||||
@ -212,6 +288,45 @@ class Inet4Address extends InetAddress {
|
|||||||
return new Inet4Address(null, addrBytes);
|
return new Inet4Address(null, addrBytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses the given string as an IPv4 address literal in
|
||||||
|
* {@linkplain Inet4Address##format-posix POSIX form.}
|
||||||
|
*
|
||||||
|
* <p> If the given {@code addressLiteral} string cannot be parsed as an IPv4 address literal
|
||||||
|
* in POSIX form and {@code throwIAE} is {@code false}, {@code null} is returned.
|
||||||
|
* If the given {@code addressLiteral} string cannot be parsed as an IPv4 address literal
|
||||||
|
* and {@code throwIAE} is {@code true}, an {@code IllegalArgumentException}
|
||||||
|
* is thrown.
|
||||||
|
*
|
||||||
|
* @apiNote
|
||||||
|
* This method produces different results compared to {@linkplain Inet4Address#parseAddressString}
|
||||||
|
* when {@code addressLiteral} parameter contains address segments with leading
|
||||||
|
* zeroes. An address segment with a leading zero is always parsed as an octal
|
||||||
|
* number by this method, therefore {@code 0255} (octal) will be parsed as
|
||||||
|
* {@code 173} (decimal). On the other hand, {@link Inet4Address#parseAddressString}
|
||||||
|
* ignores leading zeros, parses all numbers as decimal and produces {@code 255}.
|
||||||
|
* Where this method would parse {@code 0256.0256.0256.0256} (octal) and produce
|
||||||
|
* {@code 174.174.174.174} (decimal) in four dotted quad notation, {@linkplain
|
||||||
|
* Inet4Address#parseAddressString} will either throw {@code IllegalArgumentException}
|
||||||
|
* or return {@code null}, depending on the value of {@code throwIAE}.
|
||||||
|
*
|
||||||
|
* @param addressLiteral IPv4 address literal to parse
|
||||||
|
* @param throwIAE whether to throw {@code IllegalArgumentException} if the
|
||||||
|
* given {@code addressLiteral} string cannot be parsed as
|
||||||
|
* an IPv4 address literal.
|
||||||
|
* @return {@code Inet4Address} object constructed from the address literal;
|
||||||
|
* or {@code null} if the literal cannot be parsed as an IPv4 address
|
||||||
|
* @throws IllegalArgumentException if the given {@code addressLiteral} string
|
||||||
|
* cannot be parsed as an IPv4 address literal and {@code throwIAE} is {@code true}.
|
||||||
|
*/
|
||||||
|
private static Inet4Address parseAddressStringPosix(String addressLiteral) {
|
||||||
|
byte [] parsedBytes = IPAddressUtil.parseBsdLiteralV4(addressLiteral);
|
||||||
|
if (parsedBytes == null) {
|
||||||
|
throw IPAddressUtil.invalidIpAddressLiteral(addressLiteral);
|
||||||
|
}
|
||||||
|
return new Inet4Address(null, parsedBytes);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Replaces the object to be serialized with an InetAddress object.
|
* Replaces the object to be serialized with an InetAddress object.
|
||||||
*
|
*
|
||||||
|
@ -1722,6 +1722,7 @@ public sealed class InetAddress implements Serializable permits Inet4Address, In
|
|||||||
* @throws NullPointerException if the {@code ipAddressLiteral} is {@code null}.
|
* @throws NullPointerException if the {@code ipAddressLiteral} is {@code null}.
|
||||||
* @see Inet4Address#ofLiteral(String)
|
* @see Inet4Address#ofLiteral(String)
|
||||||
* @see Inet6Address#ofLiteral(String)
|
* @see Inet6Address#ofLiteral(String)
|
||||||
|
* @see Inet4Address#ofPosixLiteral(String)
|
||||||
* @since 22
|
* @since 22
|
||||||
*/
|
*/
|
||||||
public static InetAddress ofLiteral(String ipAddressLiteral) {
|
public static InetAddress ofLiteral(String ipAddressLiteral) {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2004, 2023, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2004, 2024, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -666,40 +666,81 @@ public class IPAddressUtil {
|
|||||||
* {@code false} otherwise.
|
* {@code false} otherwise.
|
||||||
*/
|
*/
|
||||||
public static boolean isBsdParsableV4(String input) {
|
public static boolean isBsdParsableV4(String input) {
|
||||||
|
return parseBsdLiteralV4(input) != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse String as IPv4 address literal by following
|
||||||
|
* POSIX-style formatting rules.
|
||||||
|
*
|
||||||
|
* @param input a String representing an IPv4 address in POSIX format
|
||||||
|
* @return a byte array representing the IPv4 numeric address
|
||||||
|
* if input string is a parsable POSIX formatted IPv4 address literal,
|
||||||
|
* {@code null} otherwise.
|
||||||
|
*/
|
||||||
|
public static byte[] parseBsdLiteralV4(String input) {
|
||||||
|
|
||||||
|
byte[] res = new byte[]{0,0,0,0};
|
||||||
|
|
||||||
|
int len = input.length();
|
||||||
|
if (len == 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
char firstSymbol = input.charAt(0);
|
char firstSymbol = input.charAt(0);
|
||||||
// Check if first digit is not a decimal digit
|
// Check if first digit is not a decimal digit
|
||||||
if (parseAsciiDigit(firstSymbol, DECIMAL) == -1) {
|
if (parseAsciiDigit(firstSymbol, DECIMAL) == -1) {
|
||||||
return false;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Last character is dot OR is not a supported digit: [0-9,A-F,a-f]
|
// Last character is dot OR is not a supported digit: [0-9,A-F,a-f]
|
||||||
char lastSymbol = input.charAt(input.length() - 1);
|
char lastSymbol = input.charAt(len - 1);
|
||||||
if (lastSymbol == '.' || parseAsciiHexDigit(lastSymbol) == -1) {
|
if (lastSymbol == '.' || parseAsciiHexDigit(lastSymbol) == -1) {
|
||||||
return false;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse IP address fields
|
// Parse IP address fields
|
||||||
CharBuffer charBuffer = CharBuffer.wrap(input);
|
CharBuffer charBuffer = CharBuffer.wrap(input);
|
||||||
int fieldNumber = 0;
|
int fieldNumber = 0;
|
||||||
|
long fieldValue = -1L;
|
||||||
while (charBuffer.hasRemaining()) {
|
while (charBuffer.hasRemaining()) {
|
||||||
long fieldValue = -1L;
|
fieldValue = -1L;
|
||||||
// Try to parse fields in all supported radixes
|
// Try to parse fields in all supported radixes
|
||||||
for (int radix : SUPPORTED_RADIXES) {
|
for (int radix : SUPPORTED_RADIXES) {
|
||||||
fieldValue = parseV4FieldBsd(radix, charBuffer, fieldNumber);
|
fieldValue = parseV4FieldBsd(radix, charBuffer, fieldNumber);
|
||||||
if (fieldValue >= 0) {
|
if (fieldValue >= 0) {
|
||||||
|
if (fieldValue < 256) {
|
||||||
|
// Store the parsed field in the byte buffer.
|
||||||
|
// If the field value is greater than 255, it can only be the last field.
|
||||||
|
// If it is not the last one, parseV4FieldBsd enforces this limit
|
||||||
|
// and returns TERMINAL_PARSE_ERROR.
|
||||||
|
res[fieldNumber] = (byte) fieldValue;
|
||||||
|
}
|
||||||
fieldNumber++;
|
fieldNumber++;
|
||||||
break;
|
break;
|
||||||
} else if (fieldValue == TERMINAL_PARSE_ERROR) {
|
} else if (fieldValue == TERMINAL_PARSE_ERROR) {
|
||||||
return false;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// If field can't be parsed as one of supported radixes stop
|
// If field can't be parsed as one of supported radixes stop
|
||||||
// parsing
|
// parsing
|
||||||
if (fieldValue < 0) {
|
if (fieldValue < 0) {
|
||||||
return false;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
// The last field value must be non-negative
|
||||||
|
if (fieldValue < 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
// If the last fieldValue is greater than 255 (fieldNumber < 4),
|
||||||
|
// it is written to the last (4 - (fieldNumber - 1)) octets
|
||||||
|
// in the network order
|
||||||
|
if (fieldNumber < 4) {
|
||||||
|
for (int i = 3; i >= fieldNumber - 1; --i) {
|
||||||
|
res[i] = (byte) (fieldValue & 255);
|
||||||
|
fieldValue >>= 8;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -22,8 +22,8 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/* @test
|
/* @test
|
||||||
* @bug 8272215
|
* @bug 8272215 8315767
|
||||||
* @summary Test for ofLiteral API in InetAddress classes
|
* @summary Test for ofLiteral, ofPosixLiteral APIs in InetAddress classes
|
||||||
* @run junit/othervm -Djdk.net.hosts.file=nonExistingHostsFile.txt
|
* @run junit/othervm -Djdk.net.hosts.file=nonExistingHostsFile.txt
|
||||||
* OfLiteralTest
|
* OfLiteralTest
|
||||||
* @run junit/othervm -Djdk.net.hosts.file=nonExistingHostsFile.txt
|
* @run junit/othervm -Djdk.net.hosts.file=nonExistingHostsFile.txt
|
||||||
@ -67,11 +67,15 @@ public class OfLiteralTest {
|
|||||||
InetAddress ofLiteralResult = switch (inetAddressClass) {
|
InetAddress ofLiteralResult = switch (inetAddressClass) {
|
||||||
case INET_ADDRESS -> InetAddress.ofLiteral(addressLiteral);
|
case INET_ADDRESS -> InetAddress.ofLiteral(addressLiteral);
|
||||||
case INET4_ADDRESS -> Inet4Address.ofLiteral(addressLiteral);
|
case INET4_ADDRESS -> Inet4Address.ofLiteral(addressLiteral);
|
||||||
|
case INET4_ADDRESS_POSIX -> Inet4Address.ofPosixLiteral(addressLiteral);
|
||||||
case INET6_ADDRESS -> Inet6Address.ofLiteral(addressLiteral);
|
case INET6_ADDRESS -> Inet6Address.ofLiteral(addressLiteral);
|
||||||
};
|
};
|
||||||
InetAddress getByNameResult = InetAddress.getByName(addressLiteral);
|
|
||||||
Assert.assertArrayEquals(expectedAddressBytes, ofLiteralResult.getAddress());
|
Assert.assertArrayEquals(expectedAddressBytes, ofLiteralResult.getAddress());
|
||||||
Assert.assertEquals(getByNameResult, ofLiteralResult);
|
// POSIX literals are not compatible with InetAddress.getByName()
|
||||||
|
if (inetAddressClass != InetAddressClass.INET4_ADDRESS_POSIX) {
|
||||||
|
InetAddress getByNameResult = InetAddress.getByName(addressLiteral);
|
||||||
|
Assert.assertEquals(getByNameResult, ofLiteralResult);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Stream<Arguments> validLiteralArguments() throws Exception {
|
private static Stream<Arguments> validLiteralArguments() throws Exception {
|
||||||
@ -101,6 +105,33 @@ public class OfLiteralTest {
|
|||||||
byte[] ipv6Ipv4MappedAddressExpBytes = new byte[]{
|
byte[] ipv6Ipv4MappedAddressExpBytes = new byte[]{
|
||||||
(byte) 129, (byte) 144, 52, 38};
|
(byte) 129, (byte) 144, 52, 38};
|
||||||
|
|
||||||
|
// 87.0.0.1 address bytes
|
||||||
|
byte[] ipv4_87_0_0_1 = new byte[]{87, 0, 0, 1};
|
||||||
|
|
||||||
|
// 127.0.0.1 address bytes
|
||||||
|
byte[] ipv4_127_0_0_1 = new byte[]{127, 0, 0, 1};
|
||||||
|
|
||||||
|
// 17.99.141.27 address bytes
|
||||||
|
byte[] ipv4_17_99_141_27 = new byte[]{17, 99, (byte)141, 27};
|
||||||
|
|
||||||
|
// 127.8.0.1 address bytes
|
||||||
|
byte[] ipv4_127_8_0_1 = new byte[]{127, 8, 0, 1};
|
||||||
|
|
||||||
|
// 0.0.0.42 address bytes
|
||||||
|
byte[] ipv4_0_0_0_42 = new byte[]{0, 0, 0, 42};
|
||||||
|
|
||||||
|
// 0.0.0.34 address bytes
|
||||||
|
byte[] ipv4_0_0_0_34 = new byte[]{0, 0, 0, 34};
|
||||||
|
|
||||||
|
// 127.0.1.2 address bytes
|
||||||
|
byte[] ipv4_127_0_1_2 = new byte[]{127, 0, 1, 2};
|
||||||
|
|
||||||
|
// 127.1.2.3 address bytes
|
||||||
|
byte[] ipv4_127_1_2_3 = new byte[]{127, 1, 2, 3};
|
||||||
|
|
||||||
|
// 255.255.255.255 address bytes
|
||||||
|
byte[] ipv4_255_255_255_255 = new byte[]{(byte)255, (byte)255, (byte)255, (byte)255};
|
||||||
|
|
||||||
Stream<Arguments> validLiterals = Stream.of(
|
Stream<Arguments> validLiterals = Stream.of(
|
||||||
// IPv6 address literals are parsable by Inet6Address.ofLiteral
|
// IPv6 address literals are parsable by Inet6Address.ofLiteral
|
||||||
// and InetAddress.ofLiteral methods
|
// and InetAddress.ofLiteral methods
|
||||||
@ -170,7 +201,87 @@ public class OfLiteralTest {
|
|||||||
// with leading 0 that is discarded and address
|
// with leading 0 that is discarded and address
|
||||||
// parsed as decimal
|
// parsed as decimal
|
||||||
Arguments.of(InetAddressClass.INET_ADDRESS,
|
Arguments.of(InetAddressClass.INET_ADDRESS,
|
||||||
"03735928559", ipv4ExpBytes)
|
"03735928559", ipv4ExpBytes),
|
||||||
|
// form:'x.x.x.x' method:InetAddress.ofPosixLiteral -
|
||||||
|
// with leading 0 treated as octal segment prefix
|
||||||
|
Arguments.of(InetAddressClass.INET4_ADDRESS_POSIX,
|
||||||
|
"0127.0.0.1", ipv4_87_0_0_1),
|
||||||
|
// form:'x.x.x.x' method:InetAddress.ofPosixLiteral -
|
||||||
|
// with leading 0 treated as octal segment prefix
|
||||||
|
Arguments.of(InetAddressClass.INET4_ADDRESS_POSIX,
|
||||||
|
"0177.0.0.1", ipv4_127_0_0_1),
|
||||||
|
// form:'x.x.x.x' method:InetAddress.ofPosixLiteral -
|
||||||
|
// with leading 0s treated as octal segment prefixes
|
||||||
|
Arguments.of(InetAddressClass.INET4_ADDRESS_POSIX,
|
||||||
|
"0177.0000.0000.0001", ipv4_127_0_0_1),
|
||||||
|
// form:'x.x.x.x' method:InetAddress.ofPosixLiteral -
|
||||||
|
// with leading 0 treated as octal segment prefix
|
||||||
|
Arguments.of(InetAddressClass.INET4_ADDRESS_POSIX,
|
||||||
|
"127.010.0.1", ipv4_127_8_0_1),
|
||||||
|
// form:'x.x.x.x' method:InetAddress.ofPosixLiteral -
|
||||||
|
// with leading 0 treated as octal segment prefix
|
||||||
|
Arguments.of(InetAddressClass.INET4_ADDRESS_POSIX,
|
||||||
|
"0377.0377.0377.0377", ipv4_255_255_255_255),
|
||||||
|
// form:'x.x.x' method:InetAddress.ofPosixLiteral -
|
||||||
|
// with leading 0s treated as octal segment prefixes
|
||||||
|
Arguments.of(InetAddressClass.INET4_ADDRESS_POSIX,
|
||||||
|
"0177.0.0402", ipv4_127_0_1_2),
|
||||||
|
// form:'x.x.x' method:InetAddress.ofPosixLiteral -
|
||||||
|
// with leading 0x treated as hexadecimal segment prefixes
|
||||||
|
Arguments.of(InetAddressClass.INET4_ADDRESS_POSIX,
|
||||||
|
"0x7F.0.0x102", ipv4_127_0_1_2),
|
||||||
|
// form:'x.x' method:InetAddress.ofPosixLiteral -
|
||||||
|
// with leading 0s treated as octal segment prefixes
|
||||||
|
Arguments.of(InetAddressClass.INET4_ADDRESS_POSIX,
|
||||||
|
"0177.0201003", ipv4_127_1_2_3),
|
||||||
|
// form:'x.x' method:InetAddress.ofPosixLiteral -
|
||||||
|
// with leading 0x treated as hexadecimal prefixes
|
||||||
|
Arguments.of(InetAddressClass.INET4_ADDRESS_POSIX,
|
||||||
|
"0x7F.0x10203", ipv4_127_1_2_3),
|
||||||
|
// form:'x.x' method:InetAddress.ofPosixLiteral -
|
||||||
|
// without prefixes treated as decimal
|
||||||
|
Arguments.of(InetAddressClass.INET4_ADDRESS_POSIX,
|
||||||
|
"127.66051", ipv4_127_1_2_3),
|
||||||
|
// form:'x' method:InetAddress.ofPosixLiteral -
|
||||||
|
// with leading 0 treated as octal segment prefix
|
||||||
|
Arguments.of(InetAddressClass.INET4_ADDRESS_POSIX,
|
||||||
|
"017700000001", ipv4_127_0_0_1),
|
||||||
|
// form:'x' method:InetAddress.ofPosixLiteral -
|
||||||
|
// with leading 0 treated as octal segment prefix
|
||||||
|
Arguments.of(InetAddressClass.INET4_ADDRESS_POSIX,
|
||||||
|
"02130706433", ipv4_17_99_141_27),
|
||||||
|
// form:'x' method:InetAddress.ofPosixLiteral -
|
||||||
|
// with leading 0 treated as octal segment prefix
|
||||||
|
Arguments.of(InetAddressClass.INET4_ADDRESS_POSIX,
|
||||||
|
"037777777777", ipv4_255_255_255_255),
|
||||||
|
// form:'x' method:InetAddress.ofPosixLiteral -
|
||||||
|
// with leading 0x treated as hex prefix
|
||||||
|
Arguments.of(InetAddressClass.INET4_ADDRESS_POSIX,
|
||||||
|
"0x1020304", oneToFourAddressExpBytes),
|
||||||
|
// form:'x' method:InetAddress.ofPosixLiteral -
|
||||||
|
// with leading 0x treated as hex prefix
|
||||||
|
Arguments.of(InetAddressClass.INET4_ADDRESS_POSIX,
|
||||||
|
"0xFFFFFFFF", ipv4_255_255_255_255),
|
||||||
|
// form:'x' method:InetAddress.ofPosixLiteral -
|
||||||
|
// without leading 0 treated as decimal
|
||||||
|
Arguments.of(InetAddressClass.INET4_ADDRESS_POSIX,
|
||||||
|
"2130706433", ipv4_127_0_0_1),
|
||||||
|
// form:'x' method:InetAddress.ofPosixLiteral -
|
||||||
|
// without leading 0 treated as decimal
|
||||||
|
Arguments.of(InetAddressClass.INET4_ADDRESS_POSIX,
|
||||||
|
"42", ipv4_0_0_0_42),
|
||||||
|
// form:'x' method:InetAddress.ofPosixLiteral -
|
||||||
|
// with leading 0 treated as octal segment prefix
|
||||||
|
Arguments.of(InetAddressClass.INET4_ADDRESS_POSIX,
|
||||||
|
"0100401404", oneToFourAddressExpBytes),
|
||||||
|
// form:'x' method:InetAddress.ofPosixLiteral -
|
||||||
|
// without prefixes treated as decimal
|
||||||
|
Arguments.of(InetAddressClass.INET4_ADDRESS_POSIX,
|
||||||
|
"16909060", oneToFourAddressExpBytes),
|
||||||
|
// form:'x' method:InetAddress.ofPosixLiteral -
|
||||||
|
// with leading 0 treated as octal segment prefix
|
||||||
|
Arguments.of(InetAddressClass.INET4_ADDRESS_POSIX,
|
||||||
|
"042", ipv4_0_0_0_34)
|
||||||
);
|
);
|
||||||
|
|
||||||
// Generate addresses for loopback and wildcard address test cases
|
// Generate addresses for loopback and wildcard address test cases
|
||||||
@ -251,7 +362,18 @@ public class OfLiteralTest {
|
|||||||
Arguments.of(InetAddressClass.INET_ADDRESS, "0x1.2.3.4"),
|
Arguments.of(InetAddressClass.INET_ADDRESS, "0x1.2.3.4"),
|
||||||
Arguments.of(InetAddressClass.INET4_ADDRESS, "1.2.0x3.4"),
|
Arguments.of(InetAddressClass.INET4_ADDRESS, "1.2.0x3.4"),
|
||||||
Arguments.of(InetAddressClass.INET_ADDRESS, "0xFFFFFFFF"),
|
Arguments.of(InetAddressClass.INET_ADDRESS, "0xFFFFFFFF"),
|
||||||
Arguments.of(InetAddressClass.INET4_ADDRESS, "0xFFFFFFFF")
|
Arguments.of(InetAddressClass.INET4_ADDRESS, "0xFFFFFFFF"),
|
||||||
|
|
||||||
|
// invalid IPv4 literals in POSIX/BSD form
|
||||||
|
Arguments.of(InetAddressClass.INET4_ADDRESS_POSIX, "0x100.1.2.3"), // 0x100 is too large
|
||||||
|
Arguments.of(InetAddressClass.INET4_ADDRESS_POSIX, "1.2.3.0x100"),
|
||||||
|
Arguments.of(InetAddressClass.INET4_ADDRESS_POSIX, "127.08.9.1"), // 8, 9 are invalid octals
|
||||||
|
Arguments.of(InetAddressClass.INET4_ADDRESS_POSIX, "127.8.09.1"),
|
||||||
|
Arguments.of(InetAddressClass.INET4_ADDRESS_POSIX, "048"),
|
||||||
|
Arguments.of(InetAddressClass.INET4_ADDRESS_POSIX, ""), // empty
|
||||||
|
Arguments.of(InetAddressClass.INET4_ADDRESS_POSIX, "0x1FFFFFFFF"), // 2^33 - 1 is too large
|
||||||
|
Arguments.of(InetAddressClass.INET4_ADDRESS_POSIX, "0x100000000"), // 2^32 is too large
|
||||||
|
Arguments.of(InetAddressClass.INET4_ADDRESS_POSIX, "040000000000")
|
||||||
);
|
);
|
||||||
// Construct arguments for a test case with IPv6-scoped address with scope-id
|
// Construct arguments for a test case with IPv6-scoped address with scope-id
|
||||||
// specified as a string with non-existing network interface name
|
// specified as a string with non-existing network interface name
|
||||||
@ -297,6 +419,7 @@ public class OfLiteralTest {
|
|||||||
return switch (inetAddressClass) {
|
return switch (inetAddressClass) {
|
||||||
case INET_ADDRESS -> () -> InetAddress.ofLiteral(input);
|
case INET_ADDRESS -> () -> InetAddress.ofLiteral(input);
|
||||||
case INET4_ADDRESS -> () -> Inet4Address.ofLiteral(input);
|
case INET4_ADDRESS -> () -> Inet4Address.ofLiteral(input);
|
||||||
|
case INET4_ADDRESS_POSIX -> () -> Inet4Address.ofPosixLiteral(input);
|
||||||
case INET6_ADDRESS -> () -> Inet6Address.ofLiteral(input);
|
case INET6_ADDRESS -> () -> Inet6Address.ofLiteral(input);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -304,6 +427,7 @@ public class OfLiteralTest {
|
|||||||
enum InetAddressClass {
|
enum InetAddressClass {
|
||||||
INET_ADDRESS,
|
INET_ADDRESS,
|
||||||
INET4_ADDRESS,
|
INET4_ADDRESS,
|
||||||
|
INET4_ADDRESS_POSIX,
|
||||||
INET6_ADDRESS
|
INET6_ADDRESS
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user