Skip to content
Permalink
Browse files
8262195: Harden tests that use the HostsFileNameService (jdk.net.host…
…s.file property)

Reviewed-by: michaelm, aefimov, dfuchs, chegar
  • Loading branch information
c-cleary authored and AlekseiEfimov committed Mar 1, 2021
1 parent 20c93b3 commit 8bc8542e3f1be9e4c153057c3ada45585c5dabdc
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 64 deletions.

This file was deleted.

@@ -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 static void main(String[] args) throws UnknownHostException {
"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 static void main(String[] args) throws UnknownHostException {

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]);
@@ -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 @@

public class InternalNameServiceTest {

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");
static final String HOSTS_FILE_NAME = System.getProperty("jdk.net.hosts.file");

testHostToIPAddressMappings(hostsFileName);
testIpAddressToHostNameMappings(hostsFileName);
public static void main(String args[]) throws Exception {
testHostToIPAddressMappings(HOSTS_FILE_NAME);
testIpAddressToHostNameMappings(HOSTS_FILE_NAME);
}

private static void testHostToIPAddressMappings(String hostsFileName)
@@ -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 @@

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 };
@@ -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 @@

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 {

This file was deleted.

@@ -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 @@ static void test(String url,
}
}

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
@@ -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 static void main(String args[]) throws Exception {
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 :-

This file was deleted.

@@ -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

This file was deleted.

0 comments on commit 8bc8542

Please sign in to comment.