Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/InetAddress/nameservice/simple/CacheTest.java
38862 views
/*1* Copyright (c) 2002, 2011, 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* @compile -XDignore.symbol.file=true SimpleNameService.java29* SimpleNameServiceDescriptor.java30* @run main/othervm/timeout=200 -Dsun.net.spi.nameservice.provider.1=simple,sun CacheTest31*/32import java.net.InetAddress;33import java.net.UnknownHostException;34import java.security.Security;3536public class CacheTest {373839public static void main(String args[]) throws Exception {4041/*42* First check the ttl on negative lookups is in the <15 second43* range. If the ttl is <=0 it means we cache forever or always44* consult the name service. For ttl > 15 the test would take45* too long so we skip it (need to coordinate jtreg timeout46* with negative ttl)47*/48String ttlProp = "networkaddress.cache.negative.ttl";49int ttl = 0;50String policy = Security.getProperty(ttlProp);51if (policy != null) {52ttl = Integer.parseInt(policy);53}54if (ttl <= 0 || ttl > 15) {55System.err.println("Security property " + ttlProp + " needs to " +56" in 1-15 second range to execute this test");57return;5859}6061/*62* The following outlines how the test works :-63*64* 1. Do a lookup via InetAddress.getByName that it guaranteed65* to succeed. This forces at least one entry into the cache66* that will not expire.67*68* 2. Do a lookup via InetAddress.getByName that is guarnateed69* to fail. This results in a negative lookup cached for70* for a short period to time.71*72* 3. Wait for the cache entry to expire.73*74* 4. Do a lookup (which should consult the name service) and75* the lookup should succeed.76*/7778// name service needs to resolve this.79SimpleNameService.put("theclub", "129.156.220.219");8081// this lookup will succeed82InetAddress.getByName("theclub");8384// lookup "luster" - this should throw UHE as name service85// doesn't know anything about this host.8687try {88InetAddress.getByName("luster");89throw new RuntimeException("Test internal error " +90" - luster is bring resolved by name service");91} catch (UnknownHostException x) {92}9394// name service now needs to know about luster95SimpleNameService.put("luster", "10.5.18.21");9697// wait for the cache entry to expire and lookup should98// succeed.99Thread.currentThread().sleep(ttl*1000 + 1000);100InetAddress.getByName("luster");101}102103}104105106