author | coleenp |
Wed, 17 Jun 2015 21:44:48 +0000 | |
changeset 31362 | 8957ccbb5821 |
parent 7668 | d4a77089c587 |
child 44671 | d1d011d4654d |
permissions | -rw-r--r-- |
2 | 1 |
/* |
7668 | 2 |
* Copyright (c) 2001, 2010, 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 |
|
27 |
* @summary Connecting to a link-local IPv6 address should not |
|
28 |
* causes a SocketException to be thrown. |
|
29 |
*/ |
|
30 |
import java.net.*; |
|
31 |
import java.util.Enumeration; |
|
32 |
||
33 |
public class LinkLocal { |
|
34 |
||
35 |
static int testCount = 0; |
|
36 |
static int failed = 0; |
|
37 |
||
38 |
static void TcpTest(InetAddress ia) throws Exception { |
|
39 |
System.out.println("**************************************"); |
|
40 |
testCount++; |
|
41 |
System.out.println("Test " + testCount + ": TCP connect to " + ia); |
|
42 |
||
43 |
/* |
|
44 |
* Create ServerSocket on wildcard address and then |
|
45 |
* try to connect Socket to link-local address. |
|
46 |
*/ |
|
47 |
ServerSocket ss = new ServerSocket(0); |
|
48 |
||
49 |
Socket s = new Socket(); |
|
50 |
try { |
|
51 |
s.connect(new InetSocketAddress(ia, ss.getLocalPort())); |
|
52 |
||
53 |
System.out.println("Test passed - connection established."); |
|
54 |
||
55 |
// connection was established so accept it |
|
56 |
Socket s2 = ss.accept(); |
|
57 |
s2.close(); |
|
58 |
} catch (SocketException e) { |
|
59 |
failed++; |
|
60 |
System.out.println("Test failed: " + e); |
|
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
61 |
} finally { |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
62 |
s.close(); |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
63 |
ss.close(); |
2 | 64 |
} |
65 |
} |
|
66 |
||
67 |
static void UdpTest(InetAddress ia, boolean connected) throws Exception { |
|
68 |
||
69 |
System.out.println("**************************************"); |
|
70 |
testCount++; |
|
71 |
||
72 |
if (connected) { |
|
73 |
System.out.println("Test " + testCount + ": UDP connect to " + ia); |
|
74 |
} else { |
|
75 |
System.out.println("Test " + testCount + ": UDP send to " + ia); |
|
76 |
} |
|
77 |
||
78 |
DatagramSocket ds1 = new DatagramSocket(); |
|
79 |
DatagramSocket ds2 = new DatagramSocket(); |
|
80 |
||
81 |
try { |
|
82 |
byte b[] = "Hello".getBytes(); |
|
83 |
DatagramPacket p = new DatagramPacket(b, b.length); |
|
84 |
||
85 |
if (connected) { |
|
86 |
ds1.connect( new InetSocketAddress(ia, ds2.getLocalPort()) ); |
|
87 |
System.out.println("DatagramSocket connected."); |
|
88 |
} else { |
|
89 |
p.setAddress(ia); |
|
90 |
p.setPort(ds2.getLocalPort()); |
|
91 |
} |
|
92 |
ds1.send(p); |
|
93 |
System.out.println("Packet has been sent."); |
|
94 |
||
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
95 |
ds2.setSoTimeout(5000); |
2 | 96 |
ds2.receive(p); |
97 |
System.out.println("Test passed - packet received."); |
|
98 |
} catch (SocketException e) { |
|
99 |
failed++; |
|
100 |
System.out.println("Test failed: " + e); |
|
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
101 |
} finally { |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
102 |
ds1.close(); |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
103 |
ds2.close(); |
2 | 104 |
} |
105 |
} |
|
106 |
||
107 |
static void TestAddress(InetAddress ia) throws Exception { |
|
108 |
TcpTest(ia); |
|
109 |
UdpTest(ia, true); /* unconnected */ |
|
110 |
UdpTest(ia, false); /* connected */ |
|
111 |
} |
|
112 |
||
113 |
public static void main(String args[]) throws Exception { |
|
114 |
||
115 |
/* |
|
116 |
* If an argument is provided ensure that it's |
|
117 |
* a link-local IPv6 address. |
|
118 |
*/ |
|
119 |
if (args.length > 0) { |
|
120 |
InetAddress ia = InetAddress.getByName(args[0]); |
|
121 |
||
122 |
if ( !(ia instanceof Inet6Address) || |
|
123 |
!ia.isLinkLocalAddress()) { |
|
124 |
throw new Exception(ia + |
|
125 |
" is not a link-local IPv6 address"); |
|
126 |
} |
|
127 |
||
128 |
TestAddress(ia); |
|
129 |
} |
|
130 |
||
131 |
/* |
|
132 |
* If no argument is provided then enumerate the |
|
133 |
* local addresses and run the test on each link-local |
|
134 |
* IPv6 address. |
|
135 |
*/ |
|
136 |
if (args.length == 0) { |
|
137 |
Enumeration nifs = NetworkInterface.getNetworkInterfaces(); |
|
138 |
while (nifs.hasMoreElements()) { |
|
139 |
NetworkInterface ni = (NetworkInterface)nifs.nextElement(); |
|
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
140 |
if (!ni.isUp()) |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
141 |
continue; |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
142 |
|
2 | 143 |
Enumeration addrs = ni.getInetAddresses(); |
144 |
while (addrs.hasMoreElements()) { |
|
145 |
InetAddress addr = (InetAddress)addrs.nextElement(); |
|
146 |
||
147 |
if (addr instanceof Inet6Address && |
|
148 |
addr.isLinkLocalAddress()) { |
|
149 |
||
150 |
TestAddress(addr); |
|
151 |
} |
|
152 |
} |
|
153 |
} |
|
154 |
} |
|
155 |
||
156 |
/* |
|
157 |
* Print results |
|
158 |
*/ |
|
159 |
if (testCount == 0) { |
|
160 |
System.out.println("No link-local IPv6 addresses - test skipped!"); |
|
161 |
} else { |
|
162 |
System.out.println("**************************************"); |
|
163 |
System.out.println(testCount + " test(s) executed, " + |
|
164 |
failed + " failed."); |
|
165 |
if (failed > 0) { |
|
166 |
throw new Exception( failed + " test(s) failed."); |
|
167 |
} |
|
168 |
} |
|
169 |
} |
|
170 |
} |