Skip to content

Commit

Permalink
8314517: some tests fail in case ipv6 is disabled on the machine
Browse files Browse the repository at this point in the history
Reviewed-by: mdoerr
Backport-of: 703817d21f6fd8b24cc670695625dfdb09d3592c
  • Loading branch information
MBaesken committed Aug 29, 2023
1 parent 1d65a31 commit d82de1c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 26 deletions.
7 changes: 7 additions & 0 deletions test/jdk/java/net/InetAddress/HostsFileOrderingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,17 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import jdk.test.lib.net.IPSupport;

import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.testng.Assert;


/* @test
* @bug 8244958
* @library /test/lib
* @build jdk.test.lib.Platform jdk.test.lib.net.IPSupport
* @summary Test that "jdk.net.hosts.file" NameService implementation returns addresses
* with respect to "java.net.preferIPv4Stack" and "java.net.preferIPv6Addresses" system
* property values
Expand Down Expand Up @@ -106,6 +110,9 @@ static ExpectedOrder getExpectedOrderFromSystemProperties() {
PREFER_IPV4_STACK_VALUE.equalsIgnoreCase("true")) {
return ExpectedOrder.IPV4_ONLY;
}
if (!IPSupport.hasIPv6()) {
return ExpectedOrder.IPV4_ONLY;
}

if (PREFER_IPV6_ADDRESSES_VALUE != null) {
return switch(PREFER_IPV6_ADDRESSES_VALUE.toLowerCase()) {
Expand Down
25 changes: 14 additions & 11 deletions test/jdk/java/net/InetAddress/InternalNameServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
* the system property jdk.net.hosts.file. This property specifies
* a file name that contains address host mappings, similar to those in
* /etc/hosts file.
* @library /test/lib
* @build jdk.test.lib.net.IPSupport
* @run main/othervm -Djdk.net.hosts.file=TestHosts -Dsun.net.inetaddr.ttl=0
* InternalNameServiceTest
*/
Expand All @@ -38,6 +40,8 @@
import java.net.UnknownHostException;
import java.util.Arrays;

import jdk.test.lib.net.IPSupport;

public class InternalNameServiceTest {

static final String HOSTS_FILE_NAME = System.getProperty("jdk.net.hosts.file");
Expand Down Expand Up @@ -128,17 +132,16 @@ private static void testHostToIPAddressMappings(String hostsFileName)
}

// IPV6 tests

// IPV6 tests
addMappingToHostsFile("host-ipv6.sample-domain", "::1", hostsFileName,
true);
testAddress = InetAddress.getByName("host-ipv6.sample-domain");
retrievedIpAddr = testAddress.getAddress();
if (!Arrays.equals(retrievedIpAddr, expectedIpAddrIpv6_1)) {
System.out.println("retrieved ipv6 addr == " + Arrays.toString(retrievedIpAddr));
System.out.println("expected ipv6 addr == " + Arrays.toString(expectedIpAddrIpv6_1));
throw new RuntimeException(
"retrieved IPV6 Addr not equal to expected IPV6 Addr");
if (IPSupport.hasIPv6()) {
addMappingToHostsFile("host-ipv6.sample-domain", "::1", hostsFileName, true);
testAddress = InetAddress.getByName("host-ipv6.sample-domain");
retrievedIpAddr = testAddress.getAddress();
if (!Arrays.equals(retrievedIpAddr, expectedIpAddrIpv6_1)) {
System.out.println("retrieved ipv6 addr == " + Arrays.toString(retrievedIpAddr));
System.out.println("expected ipv6 addr == " + Arrays.toString(expectedIpAddrIpv6_1));
throw new RuntimeException(
"retrieved IPV6 Addr not equal to expected IPV6 Addr");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
* a file name that contains address host mappings, similar to those in
* /etc/hosts file. TestHosts-III file exist, with a set of ipv4 and ipv6
* mappings
* @library /test/lib
* @build jdk.test.lib.net.IPSupport
* @run main/othervm -Djdk.net.hosts.file=${test.src}/TestHosts-III -Dsun.net.inetaddr.ttl=0
* InternalNameServiceWithHostsFileTest
*/
Expand All @@ -36,6 +38,8 @@
import java.net.UnknownHostException;
import java.util.Arrays;

import jdk.test.lib.net.IPSupport;

public class InternalNameServiceWithHostsFileTest {
public static void main(String args[]) throws Exception {
// fe80::1
Expand All @@ -52,26 +56,31 @@ public static void main(String args[]) throws Exception {

// 10.2.3.4 testHost.testDomain
testHostsMapping(expectedIpv4Address, "testHost.testDomain");
// ::1 ip6-localhost ip6-loopback
testHostsMapping(expectedIpv6LocalhostAddress, "ip6-localhost");
// fe00::0 ip6-localnet
testHostsMapping(expectedIpv6LocalAddress, "ip6-localnet");
// fe80::1 link-local-host
testHostsMapping(expectedIpv6Address, "link-local-host");

if (IPSupport.hasIPv6()) {
// ::1 ip6-localhost ip6-loopback
testHostsMapping(expectedIpv6LocalhostAddress, "ip6-localhost");
// fe00::0 ip6-localnet
testHostsMapping(expectedIpv6LocalAddress, "ip6-localnet");
// fe80::1 link-local-host
testHostsMapping(expectedIpv6Address, "link-local-host");
}

testReverseLookup("10.2.3.4", "testHost.testDomain");

testReverseLookup("::1", "ip6-localhost");
testReverseLookup("0:0:0:0:0:0:0:1", "ip6-localhost");
testReverseLookup("0000:0000:0000:0000:0000:0000:0000:0001", "ip6-localhost");
if (IPSupport.hasIPv6()) {
testReverseLookup("::1", "ip6-localhost");
testReverseLookup("0:0:0:0:0:0:0:1", "ip6-localhost");
testReverseLookup("0000:0000:0000:0000:0000:0000:0000:0001", "ip6-localhost");

testReverseLookup("fe00::0", "ip6-localnet");
testReverseLookup("fe00:0:0:0:0:0:0:0", "ip6-localnet");
testReverseLookup("fe00:0000:0000:0000:0000:0000:0000:0000", "ip6-localnet");
testReverseLookup("fe00::0", "ip6-localnet");
testReverseLookup("fe00:0:0:0:0:0:0:0", "ip6-localnet");
testReverseLookup("fe00:0000:0000:0000:0000:0000:0000:0000", "ip6-localnet");

testReverseLookup("fe80::1", "link-local-host");
testReverseLookup("fe80:000:0:00:0:000:00:1", "link-local-host");
testReverseLookup("fe80:0000:0000:0000:0000:0000:0000:0001", "link-local-host");
testReverseLookup("fe80::1", "link-local-host");
testReverseLookup("fe80:000:0:00:0:000:00:1", "link-local-host");
testReverseLookup("fe80:0000:0000:0000:0000:0000:0000:0001", "link-local-host");
}
}

private static void testHostsMapping(byte[] expectedIpAddress, String hostName)
Expand Down

1 comment on commit d82de1c

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.