author | msheppar |
Mon, 11 Apr 2016 03:00:50 +0100 | |
changeset 36967 | d041d2e80712 |
parent 30820 | 0d4717a011d3 |
permissions | -rw-r--r-- |
2 | 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 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* @test |
|
25 |
* @bug 6442088 |
|
26 |
* @summary Change default DNS caching behavior for code not running under |
|
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 | 29 |
*/ |
30 |
import java.net.InetAddress; |
|
31 |
import java.net.UnknownHostException; |
|
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 | 36 |
|
7995
8168047d48c2
7012768: InetAddress lookupTable leaks/deadlocks when using unsupported name service spi
chegar
parents:
5506
diff
changeset
|
37 |
public class DefaultCaching { |
2 | 38 |
|
39 |
public static void main(String args[]) throws Exception { |
|
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 | 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 | 46 |
|
47 |
test ("theclub", "129.156.220.219", true); // lk: 1 |
|
48 |
test ("luster", "1.16.20.2", false); // lk: 2 |
|
49 |
||
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 | 52 |
|
53 |
test ("luster", "1.16.20.2", false); // lk: 2 |
|
54 |
sleep (10+1); |
|
55 |
test("luster", "10.5.18.21", true, 3); // lk: 3 |
|
56 |
sleep (5); |
|
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 | 62 |
|
63 |
test ("theclub", "129.156.220.219", true, 3); |
|
64 |
test ("luster", "10.5.18.21", true, 3); |
|
65 |
test ("bar", "10.5.18.22", false, 4); |
|
66 |
test ("foo", "10.5.18.22", true, 5); |
|
67 |
||
68 |
// now delay to see if theclub has expired |
|
69 |
sleep (5); |
|
70 |
||
71 |
test ("foo", "10.5.18.22", true, 5); |
|
72 |
test ("theclub", "129.156.220.1", true, 6); |
|
73 |
||
74 |
sleep (11); |
|
75 |
// now see if luster has expired |
|
76 |
test ("luster", "10.5.18.21", true, 7); |
|
77 |
test ("theclub", "129.156.220.1", true, 7); |
|
78 |
||
79 |
// now delay to see if 3rd has expired |
|
80 |
sleep (10+6); |
|
81 |
||
82 |
test ("theclub", "129.156.220.1", true, 8); |
|
83 |
test ("luster", "10.5.18.21", true, 8); |
|
84 |
test ("foo", "10.5.18.22", true, 9); |
|
85 |
} |
|
86 |
||
87 |
/* throws RuntimeException if it fails */ |
|
88 |
||
89 |
static void test (String host, String address, |
|
90 |
boolean shouldSucceed, int count) { |
|
91 |
test (host, address, shouldSucceed); |
|
92 |
} |
|
93 |
||
94 |
static void sleep (int seconds) { |
|
95 |
try { |
|
96 |
Thread.sleep (seconds * 1000); |
|
97 |
} catch (InterruptedException e) {} |
|
98 |
} |
|
99 |
||
100 |
static void test (String host, String address, boolean shouldSucceed) { |
|
101 |
InetAddress addr = null; |
|
102 |
try { |
|
103 |
addr = InetAddress.getByName (host); |
|
104 |
if (!shouldSucceed) { |
|
105 |
throw new RuntimeException (host+":"+address+": should fail"); |
|
106 |
||
107 |
} |
|
108 |
if (!address.equals(addr.getHostAddress())) { |
|
109 |
throw new RuntimeException(host+":"+address+": compare failed"); |
|
110 |
} |
|
111 |
} catch (UnknownHostException e) { |
|
112 |
if (shouldSucceed) { |
|
113 |
throw new RuntimeException(host+":"+address+": should succeed"); |
|
114 |
} |
|
115 |
} |
|
116 |
} |
|
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 | 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 |
} |