0N/A/*
3909N/A * Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test
0N/A * @bug 6442088
0N/A * @summary Change default DNS caching behavior for code not running under
0N/A * security manager.
3406N/A * @compile -XDignore.symbol.file=true SimpleNameService.java
3406N/A * SimpleNameServiceDescriptor.java
3406N/A * @run main/othervm/timeout=200 -Dsun.net.inetaddr.ttl=20 -Dsun.net.spi.nameservice.provider.1=simple,sun DefaultCaching
0N/A */
0N/Aimport java.net.InetAddress;
0N/Aimport java.net.UnknownHostException;
0N/Aimport java.security.Security;
0N/A
3406N/Apublic class DefaultCaching {
0N/A
0N/A public static void main(String args[]) throws Exception {
0N/A
0N/A // name service needs to resolve this.
0N/A SimpleNameService.put("theclub", "129.156.220.219");
0N/A
0N/A test ("theclub", "129.156.220.219", true); // lk: 1
0N/A test ("luster", "1.16.20.2", false); // lk: 2
0N/A
0N/A // name service now needs to know about luster
0N/A SimpleNameService.put("luster", "10.5.18.21");
0N/A
0N/A test ("luster", "1.16.20.2", false); // lk: 2
0N/A sleep (10+1);
0N/A test("luster", "10.5.18.21", true, 3); // lk: 3
0N/A sleep (5);
0N/A
0N/A SimpleNameService.put("foo", "10.5.18.22");
0N/A SimpleNameService.put("theclub", "129.156.220.1");
0N/A
0N/A test ("theclub", "129.156.220.219", true, 3);
0N/A test ("luster", "10.5.18.21", true, 3);
0N/A test ("bar", "10.5.18.22", false, 4);
0N/A test ("foo", "10.5.18.22", true, 5);
0N/A
0N/A // now delay to see if theclub has expired
0N/A sleep (5);
0N/A
0N/A test ("foo", "10.5.18.22", true, 5);
0N/A test ("theclub", "129.156.220.1", true, 6);
0N/A
0N/A sleep (11);
0N/A // now see if luster has expired
0N/A test ("luster", "10.5.18.21", true, 7);
0N/A test ("theclub", "129.156.220.1", true, 7);
0N/A
0N/A // now delay to see if 3rd has expired
0N/A sleep (10+6);
0N/A
0N/A test ("theclub", "129.156.220.1", true, 8);
0N/A test ("luster", "10.5.18.21", true, 8);
0N/A test ("foo", "10.5.18.22", true, 9);
0N/A }
0N/A
0N/A /* throws RuntimeException if it fails */
0N/A
0N/A static void test (String host, String address,
0N/A boolean shouldSucceed, int count) {
0N/A test (host, address, shouldSucceed);
0N/A int got = SimpleNameService.lookupCalls();
0N/A if (got != count) {
0N/A throw new RuntimeException ("lookups exp/got: " + count+"/"+got);
0N/A }
0N/A }
0N/A
0N/A static void sleep (int seconds) {
0N/A try {
0N/A Thread.sleep (seconds * 1000);
0N/A } catch (InterruptedException e) {}
0N/A }
0N/A
0N/A static void test (String host, String address, boolean shouldSucceed) {
0N/A InetAddress addr = null;
0N/A try {
0N/A addr = InetAddress.getByName (host);
0N/A if (!shouldSucceed) {
0N/A throw new RuntimeException (host+":"+address+": should fail");
0N/A
0N/A }
0N/A if (!address.equals(addr.getHostAddress())) {
0N/A throw new RuntimeException(host+":"+address+": compare failed");
0N/A }
0N/A } catch (UnknownHostException e) {
0N/A if (shouldSucceed) {
0N/A throw new RuntimeException(host+":"+address+": should succeed");
0N/A }
0N/A }
0N/A }
0N/A
0N/A}