author | iignatyev |
Mon, 12 Jun 2017 12:43:26 -0700 | |
changeset 45467 | 99c87a16a8e4 |
parent 45186 | 387a39577f09 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2001, 2002, 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 4686717 |
|
27 |
* @summary Test MulticastSocket.setLoopbackMode |
|
45186
387a39577f09
8180644: move jdk.testlibrary.NetworkConfiguration to the top level test library
iignatyev
parents:
44927
diff
changeset
|
28 |
* @library /test/lib |
45467
99c87a16a8e4
8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents:
45186
diff
changeset
|
29 |
* @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
|
30 |
* jdk.test.lib.Platform |
44927 | 31 |
* @run main/othervm SetLoopbackMode |
2 | 32 |
*/ |
45186
387a39577f09
8180644: move jdk.testlibrary.NetworkConfiguration to the top level test library
iignatyev
parents:
44927
diff
changeset
|
33 |
|
2 | 34 |
import java.net.*; |
35 |
import java.io.IOException; |
|
44927 | 36 |
import java.util.Enumeration; |
45186
387a39577f09
8180644: move jdk.testlibrary.NetworkConfiguration to the top level test library
iignatyev
parents:
44927
diff
changeset
|
37 |
import jdk.test.lib.NetworkConfiguration; |
2 | 38 |
|
39 |
public class SetLoopbackMode { |
|
40 |
||
41 |
static final boolean FAILED = true; |
|
42 |
static final boolean PASSED = false; |
|
43 |
||
44 |
static boolean test(MulticastSocket mc, InetAddress grp) throws IOException { |
|
45 |
||
46 |
boolean disabled = mc.getLoopbackMode(); |
|
47 |
||
48 |
if (disabled) { |
|
49 |
System.out.println("Loopback mode is disabled."); |
|
50 |
} else { |
|
51 |
System.out.println("Loopback mode is enabled."); |
|
52 |
} |
|
53 |
||
54 |
byte b[] = "hello".getBytes(); |
|
55 |
DatagramPacket p = new DatagramPacket(b, b.length, grp, |
|
56 |
mc.getLocalPort()); |
|
57 |
mc.send(p); |
|
58 |
||
59 |
boolean gotPacket = false; |
|
60 |
||
61 |
mc.setSoTimeout(1000); |
|
62 |
try { |
|
63 |
b = new byte[16]; |
|
64 |
p = new DatagramPacket(b, b.length); |
|
65 |
mc.receive(p); |
|
66 |
gotPacket = true; |
|
67 |
||
68 |
/* purge any additional copies of the packet */ |
|
69 |
for (;;) { |
|
70 |
mc.receive(p); |
|
71 |
} |
|
72 |
||
73 |
} catch (SocketTimeoutException x) { |
|
74 |
} |
|
75 |
||
76 |
if (gotPacket && disabled) { |
|
77 |
System.out.println("Packet received (unexpected as loopback is disabled)"); |
|
78 |
return FAILED; |
|
79 |
} |
|
80 |
if (!gotPacket && !disabled) { |
|
81 |
System.out.println |
|
82 |
("Packet not received (packet excepted as loopback is enabled)"); |
|
83 |
return FAILED; |
|
84 |
} |
|
85 |
||
86 |
if (gotPacket && !disabled) { |
|
87 |
System.out.println("Packet received - correct."); |
|
88 |
} else { |
|
89 |
System.out.println("Packet not received - correct."); |
|
90 |
} |
|
91 |
||
92 |
return PASSED; |
|
93 |
} |
|
94 |
||
44927 | 95 |
private static boolean canUseIPv6(NetworkConfiguration nc) { |
96 |
return nc.ip6MulticastInterfaces().toArray().length > 0; |
|
97 |
} |
|
98 |
||
2 | 99 |
public static void main (String args[]) throws Exception { |
100 |
int failures = 0; |
|
44927 | 101 |
NetworkConfiguration nc = NetworkConfiguration.probe(); |
2 | 102 |
|
103 |
MulticastSocket mc = new MulticastSocket(); |
|
104 |
InetAddress grp = InetAddress.getByName("224.80.80.80"); |
|
105 |
||
106 |
||
107 |
/* |
|
108 |
* If IPv6 is available then use IPv6 multicast group - needed |
|
109 |
* to workaround Linux IPv6 bug whereby !IPV6_MULTICAST_LOOP |
|
110 |
* doesn't prevent loopback of IPv4 multicast packets. |
|
111 |
*/ |
|
44927 | 112 |
|
113 |
if (canUseIPv6(nc)) { |
|
2 | 114 |
grp = InetAddress.getByName("ff01::1"); |
115 |
} |
|
116 |
||
44927 | 117 |
//mc.setNetworkInterface(NetworkInterface.getByInetAddress(lb)); |
2 | 118 |
System.out.println("\nTest will use multicast group: " + grp); |
119 |
mc.joinGroup(grp); |
|
120 |
||
121 |
System.out.println("\n******************\n"); |
|
122 |
||
123 |
mc.setLoopbackMode(true); |
|
124 |
if (test(mc, grp) == FAILED) failures++; |
|
125 |
||
126 |
System.out.println("\n******************\n"); |
|
127 |
||
128 |
mc.setLoopbackMode(false); |
|
129 |
if (test(mc, grp) == FAILED) failures++; |
|
130 |
||
131 |
System.out.println("\n******************\n"); |
|
132 |
||
133 |
if (failures > 0) { |
|
134 |
throw new RuntimeException("Test failed"); |
|
135 |
} |
|
136 |
} |
|
137 |
} |