Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/InetAddress/nameservice/simple/DefaultCaching.java
38862 views
/*1* Copyright (c) 2006, 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 644208825* @summary Change default DNS caching behavior for code not running under26* security manager.27* @compile -XDignore.symbol.file=true SimpleNameService.java28* SimpleNameServiceDescriptor.java29* @run main/othervm/timeout=200 -Dsun.net.inetaddr.ttl=20 -Dsun.net.spi.nameservice.provider.1=simple,sun DefaultCaching30*/31import java.net.InetAddress;32import java.net.UnknownHostException;33import java.security.Security;3435public class DefaultCaching {3637public static void main(String args[]) throws Exception {3839// name service needs to resolve this.40SimpleNameService.put("theclub", "129.156.220.219");4142test ("theclub", "129.156.220.219", true); // lk: 143test ("luster", "1.16.20.2", false); // lk: 24445// name service now needs to know about luster46SimpleNameService.put("luster", "10.5.18.21");4748test ("luster", "1.16.20.2", false); // lk: 249sleep (10+1);50test("luster", "10.5.18.21", true, 3); // lk: 351sleep (5);5253SimpleNameService.put("foo", "10.5.18.22");54SimpleNameService.put("theclub", "129.156.220.1");5556test ("theclub", "129.156.220.219", true, 3);57test ("luster", "10.5.18.21", true, 3);58test ("bar", "10.5.18.22", false, 4);59test ("foo", "10.5.18.22", true, 5);6061// now delay to see if theclub has expired62sleep (5);6364test ("foo", "10.5.18.22", true, 5);65test ("theclub", "129.156.220.1", true, 6);6667sleep (11);68// now see if luster has expired69test ("luster", "10.5.18.21", true, 7);70test ("theclub", "129.156.220.1", true, 7);7172// now delay to see if 3rd has expired73sleep (10+6);7475test ("theclub", "129.156.220.1", true, 8);76test ("luster", "10.5.18.21", true, 8);77test ("foo", "10.5.18.22", true, 9);78}7980/* throws RuntimeException if it fails */8182static void test (String host, String address,83boolean shouldSucceed, int count) {84test (host, address, shouldSucceed);85int got = SimpleNameService.lookupCalls();86if (got != count) {87throw new RuntimeException ("lookups exp/got: " + count+"/"+got);88}89}9091static void sleep (int seconds) {92try {93Thread.sleep (seconds * 1000);94} catch (InterruptedException e) {}95}9697static void test (String host, String address, boolean shouldSucceed) {98InetAddress addr = null;99try {100addr = InetAddress.getByName (host);101if (!shouldSucceed) {102throw new RuntimeException (host+":"+address+": should fail");103104}105if (!address.equals(addr.getHostAddress())) {106throw new RuntimeException(host+":"+address+": compare failed");107}108} catch (UnknownHostException e) {109if (shouldSucceed) {110throw new RuntimeException(host+":"+address+": should succeed");111}112}113}114115}116117118