author | dsamersoff |
Tue, 13 Jul 2010 15:32:36 +0400 | |
changeset 6108 | d5bfa0bc6123 |
parent 5506 | 202f599c92aa |
child 7668 | d4a77089c587 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2007, 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 4742177 |
|
27 |
* @summary Re-test IPv6 (and specifically MulticastSocket) with latest Linux & USAGI code |
|
28 |
*/ |
|
29 |
import java.util.*; |
|
30 |
import java.net.*; |
|
31 |
||
32 |
public class NoLoopbackPackets { |
|
33 |
private static String osname; |
|
34 |
||
35 |
static boolean isWindows() { |
|
36 |
if (osname == null) |
|
37 |
osname = System.getProperty("os.name"); |
|
38 |
return osname.contains("Windows"); |
|
39 |
} |
|
40 |
||
41 |
private static boolean hasIPv6() throws Exception { |
|
42 |
List<NetworkInterface> nics = Collections.list( |
|
43 |
NetworkInterface.getNetworkInterfaces()); |
|
44 |
for (NetworkInterface nic : nics) { |
|
45 |
if (!nic.isLoopback()) { |
|
46 |
List<InetAddress> addrs = Collections.list(nic.getInetAddresses()); |
|
47 |
for (InetAddress addr : addrs) { |
|
48 |
if (addr instanceof Inet6Address) { |
|
49 |
return true; |
|
50 |
} |
|
51 |
} |
|
52 |
} |
|
53 |
} |
|
54 |
||
55 |
return false; |
|
56 |
} |
|
57 |
||
58 |
public static void main(String[] args) throws Exception { |
|
59 |
if (isWindows()) { |
|
60 |
System.out.println("The test only run on non-Windows OS. Bye."); |
|
61 |
return; |
|
62 |
} |
|
63 |
||
64 |
if (!hasIPv6()) { |
|
65 |
System.out.println("No IPv6 available. Bye."); |
|
66 |
return; |
|
67 |
} |
|
68 |
||
5143
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
69 |
MulticastSocket msock = null; |
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
70 |
List<SocketAddress> failedGroups = new ArrayList<SocketAddress>(); |
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
71 |
try { |
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
72 |
msock = new MulticastSocket(); |
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
73 |
int port = msock.getLocalPort(); |
2 | 74 |
|
5143
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
75 |
// we will send packets to three multicast groups :- |
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
76 |
// 224.1.1.1, ::ffff:224.1.1.2, and ff02::1:1 |
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
77 |
// |
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
78 |
List<SocketAddress> groups = new ArrayList<SocketAddress>(); |
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
79 |
groups.add(new InetSocketAddress(InetAddress.getByName("224.1.1.1"), port)); |
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
80 |
groups.add(new InetSocketAddress(InetAddress.getByName("::ffff:224.1.1.2"), port)); |
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
81 |
groups.add(new InetSocketAddress(InetAddress.getByName("ff02::1:1"), port)); |
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
82 |
|
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
83 |
Thread sender = new Thread(new Sender(groups)); |
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
84 |
sender.setDaemon(true); // we want sender to stop when main thread exits |
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
85 |
sender.start(); |
2 | 86 |
|
5143
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
87 |
// Now try to receive multicast packets. we should not see any of them |
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
88 |
// since we disable loopback mode. |
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
89 |
// |
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
90 |
msock.setSoTimeout(5000); // 5 seconds |
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
91 |
|
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
92 |
byte[] buf = new byte[1024]; |
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
93 |
DatagramPacket packet = new DatagramPacket(buf, 0, buf.length); |
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
94 |
for (SocketAddress group : groups) { |
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
95 |
msock.joinGroup(group, null); |
2 | 96 |
|
5143
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
97 |
try { |
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
98 |
msock.receive(packet); |
2 | 99 |
|
5143
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
100 |
// it is an error if we receive something |
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
101 |
failedGroups.add(group); |
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
102 |
} catch (SocketTimeoutException e) { |
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
103 |
// we expect this |
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
104 |
} |
2 | 105 |
|
5143
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
106 |
msock.leaveGroup(group, null); |
2 | 107 |
} |
5143
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
108 |
} finally { |
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
109 |
if (msock != null) try { msock.close(); } catch (Exception e) {} |
2 | 110 |
} |
111 |
||
112 |
if (failedGroups.size() > 0) { |
|
113 |
System.out.println("We should not receive anything from following groups, but we did:"); |
|
114 |
for (SocketAddress group : failedGroups) |
|
115 |
System.out.println(group); |
|
116 |
throw new RuntimeException("test failed."); |
|
117 |
} |
|
118 |
} |
|
5143
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
119 |
|
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
120 |
static class Sender implements Runnable { |
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
121 |
private List<SocketAddress> sendToGroups; |
2 | 122 |
|
5143
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
123 |
public Sender(List<SocketAddress> groups) { |
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
124 |
sendToGroups = groups; |
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
125 |
} |
2 | 126 |
|
5143
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
127 |
public void run() { |
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
128 |
byte[] buf = "hello world".getBytes(); |
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
129 |
List<DatagramPacket> packets = new ArrayList<DatagramPacket>(); |
2 | 130 |
|
5143
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
131 |
try { |
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
132 |
for (SocketAddress group : sendToGroups) { |
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
133 |
DatagramPacket packet = new DatagramPacket(buf, buf.length, group); |
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
134 |
packets.add(packet); |
2 | 135 |
} |
136 |
||
5143
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
137 |
MulticastSocket msock = new MulticastSocket(); |
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
138 |
msock.setLoopbackMode(true); // disable loopback mode |
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
139 |
for (;;) { |
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
140 |
for (DatagramPacket packet : packets) { |
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
141 |
msock.send(packet); |
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
142 |
} |
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
143 |
|
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
144 |
Thread.sleep(1000); // 1 second |
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
145 |
} |
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
146 |
} catch (Exception e) { |
a1682a41e70f
6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun
chegar
parents:
2
diff
changeset
|
147 |
throw new RuntimeException(e); |
2 | 148 |
} |
149 |
} |
|
150 |
} |
|
151 |
} |