8262195: Harden tests that use the HostsFileNameService (jdk.net.hosts.file property)

Reviewed-by: michaelm, aefimov, dfuchs, chegar
This commit is contained in:
Conor Cleary 2021-03-01 10:18:12 +00:00 committed by Aleksei Efimov
parent 20c93b3b90
commit 8bc8542e3f
11 changed files with 30 additions and 64 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -25,7 +25,7 @@
* @test
* @bug 4749938 8087190
* @summary Bug in the parsing IPv4 literal addresses
* @run main/othervm textToNumericFormat
* @run main/othervm -Djdk.net.hosts.file=HostFileDoesNotExist textToNumericFormat
*/
/**
@ -66,11 +66,10 @@ public class textToNumericFormat {
"1..1.1",
"1.1.1.",
"..." };
String hostsFileName = System.getProperty("test.src", ".") + "/TestToNumericFormatHosts";
System.setProperty("jdk.net.hosts.file", hostsFileName);
for (int i=0; i<goodAddrs.length; i++) {
try {
// Value is an IP Address literal, Name Service will not be called
InetAddress ia = InetAddress.getByName(goodAddrs[i]);
} catch (UnknownHostException e) {
// shouldn't have come here
@ -81,6 +80,7 @@ public class textToNumericFormat {
for (int i=0; i<badAddrs.length; i++) {
try {
// Value is not an IP Address literal, Name Service will be called
InetAddress ia = InetAddress.getByName(badAddrs[i]);
// shouldn't have come here
badList.add(badAddrs[i]);

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -27,7 +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.
* @run main/othervm InternalNameServiceTest
* @run main/othervm -Djdk.net.hosts.file=TestHosts -Dsun.net.inetaddr.ttl=0
* InternalNameServiceTest
*/
import java.io.BufferedWriter;
@ -39,14 +40,11 @@ import java.util.Arrays;
public class InternalNameServiceTest {
static final String HOSTS_FILE_NAME = System.getProperty("jdk.net.hosts.file");
public static void main(String args[]) throws Exception {
String hostsFileName = System.getProperty("test.src", ".") + "/TestHosts";
System.setProperty("jdk.net.hosts.file", hostsFileName);
System.setProperty("sun.net.inetaddr.ttl", "0");
testHostToIPAddressMappings(hostsFileName);
testIpAddressToHostNameMappings(hostsFileName);
testHostToIPAddressMappings(HOSTS_FILE_NAME);
testIpAddressToHostNameMappings(HOSTS_FILE_NAME);
}
private static void testHostToIPAddressMappings(String hostsFileName)

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -28,7 +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
* @run main/othervm -Dsun.net.inetaddr.ttl=0 InternalNameServiceWithHostsFileTest
* @run main/othervm -Djdk.net.hosts.file=${test.src}/TestHosts-III -Dsun.net.inetaddr.ttl=0
* InternalNameServiceWithHostsFileTest
*/
import java.net.InetAddress;
@ -37,13 +38,6 @@ import java.util.Arrays;
public class InternalNameServiceWithHostsFileTest {
public static void main(String args[]) throws Exception {
// System.getProperty("test.src", ".");
String hostsFileName = System.getProperty("test.src", ".")
+ "/TestHosts-III";
System.setProperty("jdk.net.hosts.file", hostsFileName);
System.setProperty("sun.net.inetaddr.ttl", "0");
// fe80::1
byte[] expectedIpv6Address = { (byte) 0xfe, (byte) 0x80, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1 };

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -28,7 +28,8 @@
* a file name that contains address host mappings, similar to those in
* /etc/hosts file. TestHosts-II file doesn't exist, so a UHE should be
* thrown
* @run main/othervm -Dsun.net.inetaddr.ttl=0 InternalNameServiceWithNoHostsFileTest
* @run main/othervm -Djdk.net.hosts.file=TestHosts-II -Dsun.net.inetaddr.ttl=0
* InternalNameServiceWithNoHostsFileTest
*/
@ -39,10 +40,6 @@ import java.net.UnknownHostException;
public class InternalNameServiceWithNoHostsFileTest {
public static void main(String args[]) throws Exception {
String hostsFileName = System.getProperty("test.src", ".") + "/TestHosts-II";
System.setProperty("jdk.net.hosts.file", hostsFileName);
System.setProperty("sun.net.inetaddr.ttl", "0");
InetAddress testAddress = null;
try {

View File

@ -1,4 +0,0 @@
# test hosts file for internal NameService
10.2.3.4 testHost.testDomain
10.5.6.7 testHost.testDomain
10.8.9.10

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -26,7 +26,7 @@
* @summary A simple smoke test of the HttpURLPermission mechanism, which checks
* for either IOException (due to unknown host) or SecurityException
* due to lack of permission to connect
* @run main/othervm LookupTest
* @run main/othervm -Djdk.net.hosts.file=LookupTestHosts LookupTest
*/
import java.io.BufferedWriter;
@ -100,18 +100,16 @@ public class LookupTest {
}
}
static final String CWD = System.getProperty("user.dir", ".");
static final String HOSTS_FILE_NAME = System.getProperty("jdk.net.hosts.file");
public static void main(String args[]) throws Exception {
String hostsFileName = CWD + "/LookupTestHosts";
System.setProperty("jdk.net.hosts.file", hostsFileName);
addMappingToHostsFile("allowedAndFound.com",
InetAddress.getLoopbackAddress().getHostAddress(),
hostsFileName,
HOSTS_FILE_NAME,
false);
addMappingToHostsFile("notAllowedButFound.com",
"99.99.99.99",
hostsFileName,
HOSTS_FILE_NAME,
true);
// name "notAllowedAndNotFound.com" is not in map
// name "allowedButNotfound.com" is not in map

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -26,7 +26,8 @@
* @summary Check that InetAddress doesn't continue to throw UHE
* after the name service has recovered and the negative ttl
* on the initial lookup has expired.
* @run main/othervm/timeout=200 CacheTest
* @run main/othervm/timeout=200 -Djdk.net.hosts.file=CacheTestHosts
* CacheTest
*/
import java.net.InetAddress;
import java.net.UnknownHostException;
@ -59,8 +60,7 @@ public class CacheTest {
return;
}
String hostsFileName = System.getProperty("test.src", ".") + "/CacheTestHosts";
System.setProperty("jdk.net.hosts.file", hostsFileName);
String hostsFileName = System.getProperty("jdk.net.hosts.file");
/*
* The following outlines how the test works :-

View File

@ -1,2 +0,0 @@
129.156.220.219 theclub
10.5.18.21 luster

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2006, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2006, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -25,31 +25,20 @@
* @bug 6442088
* @summary Change default DNS caching behavior for code not running under
* security manager.
* @run main/othervm/timeout=200 -Dsun.net.inetaddr.ttl=20 DefaultCaching
* @run main/othervm/timeout=200 -Djdk.net.hosts.file=DefaultCachingHosts
* -Dsun.net.inetaddr.ttl=20 DefaultCaching
*/
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.Security;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
public class DefaultCaching {
public static void main(String args[]) throws Exception {
String hostsFileNameSrc = System.getProperty("test.src", ".") + "/DefaultCachingHosts";
String hostsFileName = System.getProperty("user.dir", ".") + "/DefaultCachingHosts";
if (!hostsFileNameSrc.equals(hostsFileName)) {
Files.copy(Path.of(hostsFileNameSrc), Path.of(hostsFileName), REPLACE_EXISTING);
System.out.println("Host file created: " + hostsFileName);
}
System.setProperty("jdk.net.hosts.file", hostsFileName);
// initial mapping
// name service needs to resolve this.
String hostsFileName = System.getProperty("jdk.net.hosts.file");
addMappingToHostsFile("theclub", "129.156.220.219", hostsFileName, false);
test("theclub", "129.156.220.219", true); // lk: 1

View File

@ -1,3 +0,0 @@
129.156.220.1 theclub
10.5.18.22 foo
10.5.18.21 luster