1 /* 2 * Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* @test 25 * @bug 6442088 26 * @summary Change default DNS caching behavior for code not running under 27 * security manager. 28 * @modules java.base/sun.net.spi.nameservice 29 * @compile -XDignore.symbol.file=true SimpleNameService.java 30 * SimpleNameServiceDescriptor.java 31 * @run main/othervm/timeout=200 -Dsun.net.inetaddr.ttl=20 -Dsun.net.spi.nameservice.provider.1=simple,sun DefaultCaching 32 */ 33 import java.net.InetAddress; 34 import java.net.UnknownHostException; 35 import java.security.Security; 36 37 public class DefaultCaching { 38 39 public static void main(String args[]) throws Exception { 40 41 // name service needs to resolve this. 42 SimpleNameService.put("theclub", "129.156.220.219"); 43 44 test ("theclub", "129.156.220.219", true); // lk: 1 45 test ("luster", "1.16.20.2", false); // lk: 2 46 47 // name service now needs to know about luster 48 SimpleNameService.put("luster", "10.5.18.21"); 49 50 test ("luster", "1.16.20.2", false); // lk: 2 51 sleep (10+1); 52 test("luster", "10.5.18.21", true, 3); // lk: 3 53 sleep (5); 54 55 SimpleNameService.put("foo", "10.5.18.22"); 56 SimpleNameService.put("theclub", "129.156.220.1"); 57 58 test ("theclub", "129.156.220.219", true, 3); 59 test ("luster", "10.5.18.21", true, 3); 60 test ("bar", "10.5.18.22", false, 4); 61 test ("foo", "10.5.18.22", true, 5); 62 63 // now delay to see if theclub has expired 64 sleep (5); 65 66 test ("foo", "10.5.18.22", true, 5); 67 test ("theclub", "129.156.220.1", true, 6); 68 69 sleep (11); 70 // now see if luster has expired 71 test ("luster", "10.5.18.21", true, 7); 72 test ("theclub", "129.156.220.1", true, 7); 73 74 // now delay to see if 3rd has expired 75 sleep (10+6); 76 77 test ("theclub", "129.156.220.1", true, 8); 78 test ("luster", "10.5.18.21", true, 8); 79 test ("foo", "10.5.18.22", true, 9); 80 } 81 82 /* throws RuntimeException if it fails */ 83 84 static void test (String host, String address, 85 boolean shouldSucceed, int count) { 86 test (host, address, shouldSucceed); 87 int got = SimpleNameService.lookupCalls(); 88 if (got != count) { 89 throw new RuntimeException ("lookups exp/got: " + count+"/"+got); 90 } 91 } 92 93 static void sleep (int seconds) { 94 try { 95 Thread.sleep (seconds * 1000); 96 } catch (InterruptedException e) {} 97 } 98 99 static void test (String host, String address, boolean shouldSucceed) { 100 InetAddress addr = null; 101 try { 102 addr = InetAddress.getByName (host); 103 if (!shouldSucceed) { 104 throw new RuntimeException (host+":"+address+": should fail"); 105 106 } 107 if (!address.equals(addr.getHostAddress())) { 108 throw new RuntimeException(host+":"+address+": compare failed"); 109 } 110 } catch (UnknownHostException e) { 111 if (shouldSucceed) { 112 throw new RuntimeException(host+":"+address+": should succeed"); 113 } 114 } 115 } 116 117 }