author | tschatzl |
Wed, 24 Jul 2019 11:49:39 +0200 | |
changeset 57508 | 28ab01c06755 |
parent 55649 | ad8e3b295615 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
55649
ad8e3b295615
8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents:
54811
diff
changeset
|
2 |
* Copyright (c) 2001, 2019, 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 |
/* |
|
25 |
* @test |
|
26 |
* @bug 4469866 |
|
54770
62b6e7587b1f
8220673: Add test library support for determining platform IP support
aeubanks
parents:
49431
diff
changeset
|
27 |
* @library /test/lib |
2 | 28 |
* @summary Connecting to a link-local IPv6 address should not |
29 |
* causes a SocketException to be thrown. |
|
45186
387a39577f09
8180644: move jdk.testlibrary.NetworkConfiguration to the top level test library
iignatyev
parents:
44671
diff
changeset
|
30 |
* @library /test/lib |
45467
99c87a16a8e4
8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents:
45186
diff
changeset
|
31 |
* @build jdk.test.lib.NetworkConfiguration |
99c87a16a8e4
8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents:
45186
diff
changeset
|
32 |
* jdk.test.lib.Platform |
44671
d1d011d4654d
8177536: Avoid Apple Peer-to-Peer interfaces in networking tests
chegar
parents:
7668
diff
changeset
|
33 |
* @run main LinkLocal |
49431
5812849b5027
8198358: Align organization of TwoStacksPlainSocketImp with DualStackPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
34 |
* @run main/othervm -Djava.net.preferIPv4Stack=true LinkLocal |
2 | 35 |
*/ |
45186
387a39577f09
8180644: move jdk.testlibrary.NetworkConfiguration to the top level test library
iignatyev
parents:
44671
diff
changeset
|
36 |
|
2 | 37 |
import java.net.*; |
44671
d1d011d4654d
8177536: Avoid Apple Peer-to-Peer interfaces in networking tests
chegar
parents:
7668
diff
changeset
|
38 |
import java.util.List; |
d1d011d4654d
8177536: Avoid Apple Peer-to-Peer interfaces in networking tests
chegar
parents:
7668
diff
changeset
|
39 |
import java.util.stream.Collectors; |
2 | 40 |
|
54770
62b6e7587b1f
8220673: Add test library support for determining platform IP support
aeubanks
parents:
49431
diff
changeset
|
41 |
import jdk.test.lib.NetworkConfiguration; |
62b6e7587b1f
8220673: Add test library support for determining platform IP support
aeubanks
parents:
49431
diff
changeset
|
42 |
import jdk.test.lib.net.IPSupport; |
62b6e7587b1f
8220673: Add test library support for determining platform IP support
aeubanks
parents:
49431
diff
changeset
|
43 |
|
2 | 44 |
public class LinkLocal { |
45 |
||
46 |
static int testCount = 0; |
|
47 |
static int failed = 0; |
|
48 |
||
49 |
static void TcpTest(InetAddress ia) throws Exception { |
|
50 |
System.out.println("**************************************"); |
|
51 |
testCount++; |
|
52 |
System.out.println("Test " + testCount + ": TCP connect to " + ia); |
|
53 |
||
54 |
/* |
|
55 |
* Create ServerSocket on wildcard address and then |
|
56 |
* try to connect Socket to link-local address. |
|
57 |
*/ |
|
55649
ad8e3b295615
8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents:
54811
diff
changeset
|
58 |
ServerSocket ss = new ServerSocket(); |
ad8e3b295615
8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents:
54811
diff
changeset
|
59 |
ss.bind(new InetSocketAddress(ia, 0)); |
2 | 60 |
|
61 |
Socket s = new Socket(); |
|
62 |
try { |
|
63 |
s.connect(new InetSocketAddress(ia, ss.getLocalPort())); |
|
64 |
||
65 |
System.out.println("Test passed - connection established."); |
|
66 |
||
67 |
// connection was established so accept it |
|
68 |
Socket s2 = ss.accept(); |
|
69 |
s2.close(); |
|
70 |
} catch (SocketException e) { |
|
71 |
failed++; |
|
72 |
System.out.println("Test failed: " + e); |
|
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
73 |
} finally { |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
74 |
s.close(); |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
75 |
ss.close(); |
2 | 76 |
} |
77 |
} |
|
78 |
||
79 |
static void UdpTest(InetAddress ia, boolean connected) throws Exception { |
|
80 |
||
81 |
System.out.println("**************************************"); |
|
82 |
testCount++; |
|
83 |
||
84 |
if (connected) { |
|
85 |
System.out.println("Test " + testCount + ": UDP connect to " + ia); |
|
86 |
} else { |
|
87 |
System.out.println("Test " + testCount + ": UDP send to " + ia); |
|
88 |
} |
|
89 |
||
90 |
DatagramSocket ds1 = new DatagramSocket(); |
|
55649
ad8e3b295615
8227539: Replace wildcard address with loopback or local host in tests - part 20
dfuchs
parents:
54811
diff
changeset
|
91 |
DatagramSocket ds2 = new DatagramSocket(0, ia); |
2 | 92 |
|
93 |
try { |
|
94 |
byte b[] = "Hello".getBytes(); |
|
95 |
DatagramPacket p = new DatagramPacket(b, b.length); |
|
96 |
||
97 |
if (connected) { |
|
98 |
ds1.connect( new InetSocketAddress(ia, ds2.getLocalPort()) ); |
|
99 |
System.out.println("DatagramSocket connected."); |
|
100 |
} else { |
|
101 |
p.setAddress(ia); |
|
102 |
p.setPort(ds2.getLocalPort()); |
|
103 |
} |
|
104 |
ds1.send(p); |
|
105 |
System.out.println("Packet has been sent."); |
|
106 |
||
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
107 |
ds2.setSoTimeout(5000); |
2 | 108 |
ds2.receive(p); |
109 |
System.out.println("Test passed - packet received."); |
|
110 |
} catch (SocketException e) { |
|
111 |
failed++; |
|
112 |
System.out.println("Test failed: " + e); |
|
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
113 |
} finally { |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
114 |
ds1.close(); |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
115 |
ds2.close(); |
2 | 116 |
} |
117 |
} |
|
118 |
||
119 |
static void TestAddress(InetAddress ia) throws Exception { |
|
120 |
TcpTest(ia); |
|
121 |
UdpTest(ia, true); /* unconnected */ |
|
122 |
UdpTest(ia, false); /* connected */ |
|
123 |
} |
|
124 |
||
125 |
public static void main(String args[]) throws Exception { |
|
54811
9db7c0f561a6
8223652: Rename IPSupport.skipIfCurrentConfigurationIsInvalid() to IPSupport.throwSkippedExceptionIfNonOperational()
aeubanks
parents:
54770
diff
changeset
|
126 |
IPSupport.throwSkippedExceptionIfNonOperational(); |
2 | 127 |
|
128 |
/* |
|
129 |
* If an argument is provided ensure that it's |
|
130 |
* a link-local IPv6 address. |
|
131 |
*/ |
|
132 |
if (args.length > 0) { |
|
133 |
InetAddress ia = InetAddress.getByName(args[0]); |
|
134 |
||
135 |
if ( !(ia instanceof Inet6Address) || |
|
136 |
!ia.isLinkLocalAddress()) { |
|
137 |
throw new Exception(ia + |
|
138 |
" is not a link-local IPv6 address"); |
|
139 |
} |
|
140 |
||
141 |
TestAddress(ia); |
|
142 |
} |
|
143 |
||
144 |
/* |
|
145 |
* If no argument is provided then enumerate the |
|
146 |
* local addresses and run the test on each link-local |
|
147 |
* IPv6 address. |
|
148 |
*/ |
|
149 |
if (args.length == 0) { |
|
44671
d1d011d4654d
8177536: Avoid Apple Peer-to-Peer interfaces in networking tests
chegar
parents:
7668
diff
changeset
|
150 |
List<Inet6Address> addrs = NetworkConfiguration.probe() |
d1d011d4654d
8177536: Avoid Apple Peer-to-Peer interfaces in networking tests
chegar
parents:
7668
diff
changeset
|
151 |
.ip6Addresses() |
d1d011d4654d
8177536: Avoid Apple Peer-to-Peer interfaces in networking tests
chegar
parents:
7668
diff
changeset
|
152 |
.filter(Inet6Address::isLinkLocalAddress) |
d1d011d4654d
8177536: Avoid Apple Peer-to-Peer interfaces in networking tests
chegar
parents:
7668
diff
changeset
|
153 |
.collect(Collectors.toList()); |
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
154 |
|
44671
d1d011d4654d
8177536: Avoid Apple Peer-to-Peer interfaces in networking tests
chegar
parents:
7668
diff
changeset
|
155 |
for (Inet6Address addr : addrs) { |
d1d011d4654d
8177536: Avoid Apple Peer-to-Peer interfaces in networking tests
chegar
parents:
7668
diff
changeset
|
156 |
TestAddress(addr); |
2 | 157 |
} |
158 |
} |
|
159 |
||
160 |
/* |
|
161 |
* Print results |
|
162 |
*/ |
|
163 |
if (testCount == 0) { |
|
164 |
System.out.println("No link-local IPv6 addresses - test skipped!"); |
|
165 |
} else { |
|
166 |
System.out.println("**************************************"); |
|
167 |
System.out.println(testCount + " test(s) executed, " + |
|
168 |
failed + " failed."); |
|
169 |
if (failed > 0) { |
|
170 |
throw new Exception( failed + " test(s) failed."); |
|
171 |
} |
|
172 |
} |
|
173 |
} |
|
174 |
} |