jdk/test/sun/net/InetAddress/nameservice/simple/CacheTest.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 7995 8168047d48c2
child 30820 0d4717a011d3
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7995
diff changeset
     2
 * Copyright (c) 2002, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 4292867
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Check that InetAddress doesn't continue to throw UHE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *          after the name service has recovered and the negative ttl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          on the initial lookup has expired.
7995
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 5506
diff changeset
    29
 * @compile -XDignore.symbol.file=true SimpleNameService.java
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 5506
diff changeset
    30
 *                                     SimpleNameServiceDescriptor.java
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run main/othervm/timeout=200 -Dsun.net.spi.nameservice.provider.1=simple,sun CacheTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.InetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.net.UnknownHostException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.Security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class CacheTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
         * First check the ttl on negative lookups is in the <15 second
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
         * range. If the ttl is <=0 it means we cache forever or always
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
         * consult the name service. For ttl > 15 the test would take
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
         * too long so we skip it (need to coordinate jtreg timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
         * with negative ttl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        String ttlProp = "networkaddress.cache.negative.ttl";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        int ttl = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        String policy = Security.getProperty(ttlProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        if (policy != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            ttl = Integer.parseInt(policy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        if (ttl <= 0  || ttl > 15) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            System.err.println("Security property " + ttlProp + " needs to " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                " in 1-15 second range to execute this test");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
         * The following outlines how the test works :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
         * 1. Do a lookup via InetAddress.getByName that it guaranteed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
         *    to succeed. This forces at least one entry into the cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
         *    that will not expire.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
         * 2. Do a lookup via InetAddress.getByName that is guarnateed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
         *    to fail. This results in a negative lookup cached for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
         *    for a short period to time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
         * 3. Wait for the cache entry to expire.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
         * 4. Do a lookup (which should consult the name service) and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
         *    the lookup should succeed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        // name service needs to resolve this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        SimpleNameService.put("theclub", "129.156.220.219");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        // this lookup will succeed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        InetAddress.getByName("theclub");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        // lookup "luster" - this should throw UHE as name service
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        // doesn't know anything about this host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            InetAddress.getByName("luster");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            throw new RuntimeException("Test internal error " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                " - luster is bring resolved by name service");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        } catch (UnknownHostException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        // name service now needs to know about luster
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        SimpleNameService.put("luster", "10.5.18.21");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        // wait for the cache entry to expire and lookup should
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        // succeed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        Thread.currentThread().sleep(ttl*1000 + 1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        InetAddress.getByName("luster");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
}