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