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