Path: blob/master/test/jdk/sun/net/InetAddress/nameservice/simple/CacheTest.java
41161 views
/*1* Copyright (c) 2002, 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 429286725* @summary Check that InetAddress doesn't continue to throw UHE26* after the name service has recovered and the negative ttl27* on the initial lookup has expired.28* @run main/othervm/timeout=200 -Djdk.net.hosts.file=CacheTestHosts29* CacheTest30*/31import java.net.InetAddress;32import java.net.UnknownHostException;33import java.security.Security;34import java.io.PrintWriter;35import java.io.FileWriter;36import java.io.BufferedWriter;3738public class CacheTest {394041public static void main(String args[]) throws Exception {4243/*44* First check the ttl on negative lookups is in the <15 second45* range. If the ttl is <=0 it means we cache forever or always46* consult the name service. For ttl > 15 the test would take47* too long so we skip it (need to coordinate jtreg timeout48* with negative ttl)49*/50String ttlProp = "networkaddress.cache.negative.ttl";51int ttl = 0;52String policy = Security.getProperty(ttlProp);53if (policy != null) {54ttl = Integer.parseInt(policy);55}56if (ttl <= 0 || ttl > 15) {57System.err.println("Security property " + ttlProp + " needs to " +58" in 1-15 second range to execute this test");59return;6061}62String hostsFileName = System.getProperty("jdk.net.hosts.file");6364/*65* The following outlines how the test works :-66*67* 1. Do a lookup via InetAddress.getByName that it guaranteed68* to succeed. This forces at least one entry into the cache69* that will not expire.70*71* 2. Do a lookup via InetAddress.getByName that is guarnateed72* to fail. This results in a negative lookup cached for73* for a short period to time.74*75* 3. Wait for the cache entry to expire.76*77* 4. Do a lookup (which should consult the name service) and78* the lookup should succeed.79*/8081// name service needs to resolve this.82addMappingToHostsFile("theclub", "129.156.220.219", hostsFileName, false);8384// this lookup will succeed85InetAddress.getByName("theclub");8687// lookup "luster" - this should throw UHE as name service88// doesn't know anything about this host.8990try {91InetAddress.getByName("luster");92throw new RuntimeException("Test internal error " +93" - luster is being resolved by name service");94} catch (UnknownHostException x) {95}9697// name service now needs to know about luster98addMappingToHostsFile("luster", "10.5.18.21", hostsFileName, true);99100// wait for the cache entry to expire and lookup should101// succeed.102Thread.currentThread().sleep(ttl*1000 + 1000);103InetAddress.getByName("luster");104}105106private static void addMappingToHostsFile ( String host,107String addr,108String hostsFileName,109boolean append)110throws Exception {111String mapping = addr + " " + host;112try (PrintWriter hfPWriter = new PrintWriter(new BufferedWriter(113new FileWriter(hostsFileName, append)))) {114hfPWriter.println(mapping);115}116}117118}119120121