Path: blob/master/test/jdk/sun/net/InetAddress/nameservice/simple/DefaultCaching.java
41161 views
/*1* Copyright (c) 2006, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 644208825* @summary Change default DNS caching behavior for code not running under26* security manager.27* @run main/othervm/timeout=200 -Djdk.net.hosts.file=DefaultCachingHosts28* -Dsun.net.inetaddr.ttl=20 DefaultCaching29*/30import java.net.InetAddress;31import java.net.UnknownHostException;32import java.io.FileWriter;33import java.io.PrintWriter;34import java.io.BufferedWriter;3536public class DefaultCaching {3738public static void main(String args[]) throws Exception {3940String hostsFileName = System.getProperty("jdk.net.hosts.file");41addMappingToHostsFile("theclub", "129.156.220.219", hostsFileName, false);4243test("theclub", "129.156.220.219", true); // lk: 144test("luster", "1.16.20.2", false); // lk: 24546// name service now needs to know about luster47addMappingToHostsFile("luster", "10.5.18.21", hostsFileName, true);4849test("luster", "1.16.20.2", false); // lk: 250sleep(10+1);51test("luster", "10.5.18.21", true, 3); // lk: 352sleep(5);5354// new mapping for theclub and rewrite existing foo and luster mappings55addMappingToHostsFile("theclub", "129.156.220.1", hostsFileName, false);56addMappingToHostsFile("foo", "10.5.18.22", hostsFileName, true);57addMappingToHostsFile("luster", "10.5.18.21", hostsFileName, true);5859test("theclub", "129.156.220.219", true, 3);60test("luster", "10.5.18.21", true, 3);61test("bar", "10.5.18.22", false, 4);62test("foo", "10.5.18.22", true, 5);6364// now delay to see if theclub has expired65sleep(5);6667test("foo", "10.5.18.22", true, 5);68test("theclub", "129.156.220.1", true, 6);6970sleep(11);71// now see if luster has expired72test("luster", "10.5.18.21", true, 7);73test("theclub", "129.156.220.1", true, 7);7475// now delay to see if 3rd has expired76sleep(10+6);7778test("theclub", "129.156.220.1", true, 8);79test("luster", "10.5.18.21", true, 8);80test("foo", "10.5.18.22", true, 9);81}8283/* throws RuntimeException if it fails */8485static void test(String host, String address,86boolean shouldSucceed, int count) {87test(host, address, shouldSucceed);88}8990static void sleep(int seconds) {91try {92sleepms(seconds * 1000);93} catch (InterruptedException e) {94e.printStackTrace();95}96}9798static long sleepms(long millis) throws InterruptedException {99long start = System.nanoTime();100long ms = millis;101while (ms > 0) {102assert ms < Long.MAX_VALUE/1000_000L;103Thread.sleep(ms);104long elapsedms = (System.nanoTime() - start)/1000_000L;105ms = millis - elapsedms;106}107return millis - ms;108}109110static void test(String host, String address, boolean shouldSucceed) {111InetAddress addr = null;112try {113addr = InetAddress.getByName(host);114if (!shouldSucceed) {115throw new RuntimeException(host+":"+address+": should fail (got "116+ addr + ")");117}118if (!address.equals(addr.getHostAddress())) {119throw new RuntimeException(host+"/"+address+": compare failed (found "120+ addr + ")");121}122System.out.println("test: " + host + "/" + address123+ " succeeded - got " + addr);124} catch (UnknownHostException e) {125if (shouldSucceed) {126throw new RuntimeException(host+":"+address+": should succeed");127} else {128System.out.println("test: " + host + "/" + address129+ " succeeded - got expected " + e);130}131}132}133134135private static void addMappingToHostsFile(String host,136String addr,137String hostsFileName,138boolean append)139throws Exception {140String mapping = addr + " " + host;141try (PrintWriter hfPWriter = new PrintWriter(new BufferedWriter(142new FileWriter(hostsFileName, append)))) {143hfPWriter.println(mapping);144}145}146}147148149