1/* 2 * Copyright (c) 2002, 2016, 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 4292867 26 * @summary Check that InetAddress doesn't continue to throw UHE 27 * after the name service has recovered and the negative ttl 28 * on the initial lookup has expired. 29 * @run main/othervm/timeout=200 CacheTest 30 */ 31import java.net.InetAddress; 32import java.net.UnknownHostException; 33import java.security.Security; 34import java.io.PrintWriter; 35import java.io.FileWriter; 36import java.io.BufferedWriter; 37 38public class CacheTest { 39 40 41 public static void main(String args[]) throws Exception { 42 43 /* 44 * First check the ttl on negative lookups is in the <15 second 45 * range. If the ttl is <=0 it means we cache forever or always 46 * consult the name service. For ttl > 15 the test would take 47 * too long so we skip it (need to coordinate jtreg timeout 48 * with negative ttl) 49 */ 50 String ttlProp = "networkaddress.cache.negative.ttl"; 51 int ttl = 0; 52 String policy = Security.getProperty(ttlProp); 53 if (policy != null) { 54 ttl = Integer.parseInt(policy); 55 } 56 if (ttl <= 0 || ttl > 15) { 57 System.err.println("Security property " + ttlProp + " needs to " + 58 " in 1-15 second range to execute this test"); 59 return; 60 61 } 62 String hostsFileName = System.getProperty("test.src", ".") + "/CacheTestHosts"; 63 System.setProperty("jdk.net.hosts.file", hostsFileName); 64 65 /* 66 * The following outlines how the test works :- 67 * 68 * 1. Do a lookup via InetAddress.getByName that it guaranteed 69 * to succeed. This forces at least one entry into the cache 70 * that will not expire. 71 * 72 * 2. Do a lookup via InetAddress.getByName that is guarnateed 73 * to fail. This results in a negative lookup cached for 74 * for a short period to time. 75 * 76 * 3. Wait for the cache entry to expire. 77 * 78 * 4. Do a lookup (which should consult the name service) and 79 * the lookup should succeed. 80 */ 81 82 // name service needs to resolve this. 83 addMappingToHostsFile("theclub", "129.156.220.219", hostsFileName, false); 84 85 // this lookup will succeed 86 InetAddress.getByName("theclub"); 87 88 // lookup "luster" - this should throw UHE as name service 89 // doesn't know anything about this host. 90 91 try { 92 InetAddress.getByName("luster"); 93 throw new RuntimeException("Test internal error " + 94 " - luster is being resolved by name service"); 95 } catch (UnknownHostException x) { 96 } 97 98 // name service now needs to know about luster 99 addMappingToHostsFile("luster", "10.5.18.21", hostsFileName, true); 100 101 // wait for the cache entry to expire and lookup should 102 // succeed. 103 Thread.currentThread().sleep(ttl*1000 + 1000); 104 InetAddress.getByName("luster"); 105 } 106 107 private static void addMappingToHostsFile ( String host, 108 String addr, 109 String hostsFileName, 110 boolean append) 111 throws Exception { 112 String mapping = addr + " " + host; 113 try (PrintWriter hfPWriter = new PrintWriter(new BufferedWriter( 114 new FileWriter(hostsFileName, append)))) { 115 hfPWriter.println(mapping); 116} 117 } 118 119} 120