jdk/test/sun/net/InetAddress/nameservice/simple/DefaultCaching.java
author msheppar
Mon, 11 Apr 2016 03:00:50 +0100
changeset 36967 d041d2e80712
parent 30820 0d4717a011d3
permissions -rw-r--r--
8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor Reviewed-by: chegar, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
36967
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 30820
diff changeset
     2
 * Copyright (c) 2006, 2016, 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.
36967
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 30820
diff changeset
    28
 * @run main/othervm/timeout=200 -Dsun.net.inetaddr.ttl=20  DefaultCaching
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.InetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.UnknownHostException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.Security;
36967
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 30820
diff changeset
    33
import java.io.FileWriter;
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 30820
diff changeset
    34
import java.io.PrintWriter;
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 30820
diff changeset
    35
import java.io.BufferedWriter;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
7995
8168047d48c2 7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents: 5506
diff changeset
    37
public class DefaultCaching {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
36967
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 30820
diff changeset
    41
        String hostsFileName = System.getProperty("test.src", ".") + "/DefaultCachingHosts";
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 30820
diff changeset
    42
        System.setProperty("jdk.net.hosts.file", hostsFileName);
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 30820
diff changeset
    43
        // initial mapping
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        // name service needs to resolve this.
36967
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 30820
diff changeset
    45
        addMappingToHostsFile("theclub", "129.156.220.219", hostsFileName, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        test ("theclub", "129.156.220.219", true);      // lk: 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        test ("luster", "1.16.20.2", false);            // lk: 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        // name service now needs to know about luster
36967
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 30820
diff changeset
    51
        addMappingToHostsFile("luster", "10.5.18.21", hostsFileName, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        test ("luster", "1.16.20.2", false);            // lk: 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        sleep (10+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        test("luster", "10.5.18.21", true, 3);          // lk: 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        sleep (5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
36967
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 30820
diff changeset
    58
        // new mapping for theclub and rewrite existing foo and luster mappings
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 30820
diff changeset
    59
        addMappingToHostsFile("theclub", "129.156.220.1", hostsFileName, false);
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 30820
diff changeset
    60
        addMappingToHostsFile("foo", "10.5.18.22", hostsFileName, true);
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 30820
diff changeset
    61
        addMappingToHostsFile("luster", "10.5.18.21", hostsFileName, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        test ("theclub", "129.156.220.219", true, 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        test ("luster", "10.5.18.21", true, 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        test ("bar", "10.5.18.22", false, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        test ("foo", "10.5.18.22", true, 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        // now delay to see if theclub has expired
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        sleep (5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        test ("foo", "10.5.18.22", true, 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        test ("theclub", "129.156.220.1", true, 6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        sleep (11);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        // now see if luster has expired
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        test ("luster", "10.5.18.21", true, 7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        test ("theclub", "129.156.220.1", true, 7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        // now delay to see if 3rd has expired
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        sleep (10+6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        test ("theclub", "129.156.220.1", true, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        test ("luster", "10.5.18.21", true, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        test ("foo", "10.5.18.22", true, 9);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /* throws RuntimeException if it fails */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    static void test (String host, String address,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                        boolean shouldSucceed, int count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        test (host, address, shouldSucceed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    static void sleep (int seconds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            Thread.sleep (seconds * 1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        } catch (InterruptedException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    static void test (String host, String address, boolean shouldSucceed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        InetAddress addr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            addr = InetAddress.getByName (host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            if (!shouldSucceed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                throw new RuntimeException (host+":"+address+": should fail");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            if (!address.equals(addr.getHostAddress())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                throw new RuntimeException(host+":"+address+": compare failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        } catch (UnknownHostException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            if (shouldSucceed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                throw new RuntimeException(host+":"+address+": should succeed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
36967
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 30820
diff changeset
   118
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 30820
diff changeset
   119
    private static void addMappingToHostsFile (String host,
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 30820
diff changeset
   120
                                               String addr,
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 30820
diff changeset
   121
                                               String hostsFileName,
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 30820
diff changeset
   122
                                               boolean append)
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 30820
diff changeset
   123
                                             throws Exception {
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 30820
diff changeset
   124
        String mapping = addr + " " + host;
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 30820
diff changeset
   125
        try (PrintWriter hfPWriter = new PrintWriter(new BufferedWriter(
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 30820
diff changeset
   126
                new FileWriter(hostsFileName, append)))) {
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 30820
diff changeset
   127
            hfPWriter.println(mapping);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
}
36967
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 30820
diff changeset
   129
    }
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 30820
diff changeset
   130
}