jdk/test/sun/net/InetAddress/nameservice/simple/DefaultCaching.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) 2006, 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 6442088
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Change default DNS caching behavior for code not running under
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *          security manager.
7995
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 5506
diff changeset
    28
 * @compile -XDignore.symbol.file=true SimpleNameService.java
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 5506
diff changeset
    29
 *                                     SimpleNameServiceDescriptor.java
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 5506
diff changeset
    30
 * @run main/othervm/timeout=200 -Dsun.net.inetaddr.ttl=20 -Dsun.net.spi.nameservice.provider.1=simple,sun DefaultCaching
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.InetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.UnknownHostException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.Security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
7995
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 5506
diff changeset
    36
public class DefaultCaching {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        // name service needs to resolve this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        SimpleNameService.put("theclub", "129.156.220.219");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        test ("theclub", "129.156.220.219", true);      // lk: 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        test ("luster", "1.16.20.2", false);            // lk: 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        // name service now needs to know about luster
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        SimpleNameService.put("luster", "10.5.18.21");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        test ("luster", "1.16.20.2", false);            // lk: 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        sleep (10+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        test("luster", "10.5.18.21", true, 3);          // lk: 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        sleep (5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        SimpleNameService.put("foo", "10.5.18.22");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        SimpleNameService.put("theclub", "129.156.220.1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        test ("theclub", "129.156.220.219", true, 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        test ("luster", "10.5.18.21", true, 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        test ("bar", "10.5.18.22", false, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        test ("foo", "10.5.18.22", true, 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        // now delay to see if theclub has expired
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        sleep (5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        test ("foo", "10.5.18.22", true, 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        test ("theclub", "129.156.220.1", true, 6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        sleep (11);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        // now see if luster has expired
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        test ("luster", "10.5.18.21", true, 7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        test ("theclub", "129.156.220.1", true, 7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        // now delay to see if 3rd has expired
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        sleep (10+6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        test ("theclub", "129.156.220.1", true, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        test ("luster", "10.5.18.21", true, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        test ("foo", "10.5.18.22", true, 9);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /* throws RuntimeException if it fails */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    static void test (String host, String address,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                        boolean shouldSucceed, int count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        test (host, address, shouldSucceed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        int got = SimpleNameService.lookupCalls();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        if (got != count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            throw new RuntimeException ("lookups exp/got: " + count+"/"+got);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    static void sleep (int seconds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            Thread.sleep (seconds * 1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        } catch (InterruptedException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    static void test (String host, String address, boolean shouldSucceed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        InetAddress addr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            addr = InetAddress.getByName (host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            if (!shouldSucceed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                throw new RuntimeException (host+":"+address+": should fail");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            if (!address.equals(addr.getHostAddress())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                throw new RuntimeException(host+":"+address+": compare failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        } catch (UnknownHostException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            if (shouldSucceed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                throw new RuntimeException(host+":"+address+": should succeed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
}