author | ohair |
Tue, 25 May 2010 15:58:33 -0700 | |
changeset 5506 | 202f599c92aa |
parent 3424 | 8f6c0145dc8f |
child 9679 | d98ae8bc45fc |
permissions | -rw-r--r-- |
1152 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2007, 2009, Oracle and/or its affiliates. All rights reserved. |
1152 | 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. |
|
1152 | 22 |
*/ |
23 |
||
24 |
/* @test |
|
25 |
* @bug 4527345 |
|
26 |
* @summary Unit test for DatagramChannel's multicast support |
|
27 |
* @build BasicMulticastTests NetworkConfiguration |
|
3424 | 28 |
* @run main BasicMulticastTests |
1152 | 29 |
*/ |
30 |
||
31 |
import java.nio.ByteBuffer; |
|
32 |
import java.nio.channels.*; |
|
33 |
import java.net.*; |
|
34 |
import java.util.*; |
|
35 |
import java.io.IOException; |
|
36 |
||
37 |
public class BasicMulticastTests { |
|
38 |
||
39 |
/** |
|
40 |
* Tests that existing membership key is returned by join methods and that |
|
41 |
* membership key methods return the expected results |
|
42 |
*/ |
|
43 |
static void membershipKeyTests(NetworkInterface nif, |
|
44 |
InetAddress group, |
|
45 |
InetAddress source) |
|
46 |
throws IOException |
|
47 |
{ |
|
48 |
System.out.format("MembershipKey test using %s @ %s\n", |
|
49 |
group.getHostAddress(), nif.getName()); |
|
50 |
||
51 |
ProtocolFamily family = (group instanceof Inet4Address) ? |
|
52 |
StandardProtocolFamily.INET : StandardProtocolFamily.INET6; |
|
53 |
||
54 |
DatagramChannel dc = DatagramChannel.open(family) |
|
55 |
.setOption(StandardSocketOption.SO_REUSEADDR, true) |
|
56 |
.bind(new InetSocketAddress(source, 0)); |
|
57 |
||
58 |
// check existing key is returned |
|
59 |
MembershipKey key = dc.join(group, nif); |
|
60 |
MembershipKey other = dc.join(group, nif); |
|
61 |
if (other != key) { |
|
62 |
throw new RuntimeException("existing key not returned"); |
|
63 |
} |
|
64 |
||
65 |
// check key |
|
66 |
if (!key.isValid()) |
|
67 |
throw new RuntimeException("key is not valid"); |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1152
diff
changeset
|
68 |
if (!key.group().equals(group)) |
1152 | 69 |
throw new RuntimeException("group is incorrect"); |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1152
diff
changeset
|
70 |
if (!key.networkInterface().equals(nif)) |
1152 | 71 |
throw new RuntimeException("network interface is incorrect"); |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1152
diff
changeset
|
72 |
if (key.sourceAddress() != null) |
1152 | 73 |
throw new RuntimeException("key is source specific"); |
74 |
||
75 |
// drop membership |
|
76 |
key.drop(); |
|
77 |
if (key.isValid()) { |
|
78 |
throw new RuntimeException("key is still valid"); |
|
79 |
} |
|
80 |
||
81 |
// source-specific |
|
82 |
try { |
|
83 |
key = dc.join(group, nif, source); |
|
84 |
other = dc.join(group, nif, source); |
|
85 |
if (other != key) { |
|
86 |
throw new RuntimeException("existing key not returned"); |
|
87 |
} |
|
88 |
if (!key.isValid()) |
|
89 |
throw new RuntimeException("key is not valid"); |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1152
diff
changeset
|
90 |
if (!key.group().equals(group)) |
1152 | 91 |
throw new RuntimeException("group is incorrect"); |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1152
diff
changeset
|
92 |
if (!key.networkInterface().equals(nif)) |
1152 | 93 |
throw new RuntimeException("network interface is incorrect"); |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1152
diff
changeset
|
94 |
if (!key.sourceAddress().equals(source)) |
1152 | 95 |
throw new RuntimeException("key's source address incorrect"); |
96 |
||
97 |
// drop membership |
|
98 |
key.drop(); |
|
99 |
if (key.isValid()) { |
|
100 |
throw new RuntimeException("key is still valid"); |
|
101 |
} |
|
102 |
} catch (UnsupportedOperationException x) { |
|
103 |
} |
|
104 |
||
105 |
// done |
|
106 |
dc.close(); |
|
107 |
} |
|
108 |
||
109 |
/** |
|
110 |
* Tests exceptions for invalid arguments or scenarios |
|
111 |
*/ |
|
112 |
static void exceptionTests(NetworkInterface nif) |
|
113 |
throws IOException |
|
114 |
{ |
|
115 |
System.out.println("Exception Tests"); |
|
116 |
||
117 |
DatagramChannel dc = DatagramChannel.open(StandardProtocolFamily.INET) |
|
118 |
.setOption(StandardSocketOption.SO_REUSEADDR, true) |
|
119 |
.bind(new InetSocketAddress(0)); |
|
120 |
||
121 |
InetAddress group = InetAddress.getByName("225.4.5.6"); |
|
122 |
InetAddress notGroup = InetAddress.getByName("1.2.3.4"); |
|
123 |
InetAddress thisHost = InetAddress.getLocalHost(); |
|
124 |
||
125 |
// IllegalStateException |
|
126 |
MembershipKey key; |
|
127 |
key = dc.join(group, nif); |
|
128 |
try { |
|
129 |
dc.join(group, nif, thisHost); |
|
130 |
throw new RuntimeException("IllegalStateException not thrown"); |
|
131 |
} catch (IllegalStateException x) { |
|
132 |
} catch (UnsupportedOperationException x) { |
|
133 |
} |
|
134 |
key.drop(); |
|
135 |
try { |
|
136 |
key = dc.join(group, nif, thisHost); |
|
137 |
try { |
|
138 |
dc.join(group, nif); |
|
139 |
throw new RuntimeException("IllegalStateException not thrown"); |
|
140 |
} catch (IllegalStateException x) { |
|
141 |
} |
|
142 |
key.drop(); |
|
143 |
} catch (UnsupportedOperationException x) { |
|
144 |
} |
|
145 |
||
146 |
// IllegalArgumentException |
|
147 |
try { |
|
148 |
dc.join(notGroup, nif); |
|
149 |
throw new RuntimeException("IllegalArgumentException not thrown"); |
|
150 |
} catch (IllegalArgumentException x) { |
|
151 |
} |
|
152 |
try { |
|
153 |
dc.join(notGroup, nif, thisHost); |
|
154 |
throw new RuntimeException("IllegalArgumentException not thrown"); |
|
155 |
} catch (IllegalArgumentException x) { |
|
156 |
} catch (UnsupportedOperationException x) { |
|
157 |
} |
|
158 |
||
159 |
// NullPointerException |
|
160 |
try { |
|
161 |
dc.join(null, nif); |
|
162 |
throw new RuntimeException("NullPointerException not thrown"); |
|
163 |
} catch (NullPointerException x) { |
|
164 |
} |
|
165 |
try { |
|
166 |
dc.join(group, null); |
|
167 |
throw new RuntimeException("NullPointerException not thrown"); |
|
168 |
} catch (NullPointerException x) { |
|
169 |
} |
|
170 |
try { |
|
171 |
dc.join(group, nif, null); |
|
172 |
throw new RuntimeException("NullPointerException not thrown"); |
|
173 |
} catch (NullPointerException x) { |
|
174 |
} catch (UnsupportedOperationException x) { |
|
175 |
} |
|
176 |
||
177 |
dc.close(); |
|
178 |
||
179 |
// ClosedChannelException |
|
180 |
try { |
|
181 |
dc.join(group, nif); |
|
182 |
throw new RuntimeException("ClosedChannelException not thrown"); |
|
183 |
} catch (ClosedChannelException x) { |
|
184 |
} |
|
185 |
try { |
|
186 |
dc.join(group, nif, thisHost); |
|
187 |
throw new RuntimeException("ClosedChannelException not thrown"); |
|
188 |
} catch (ClosedChannelException x) { |
|
189 |
} catch (UnsupportedOperationException x) { |
|
190 |
} |
|
191 |
} |
|
192 |
||
193 |
||
194 |
/** |
|
195 |
* Probe interfaces to get interfaces that support IPv4 or IPv6 multicasting |
|
196 |
* and invoke tests. |
|
197 |
*/ |
|
198 |
public static void main(String[] args) throws IOException { |
|
199 |
||
200 |
// multicast groups used for the test |
|
201 |
InetAddress ip4Group = InetAddress.getByName("225.4.5.6"); |
|
202 |
InetAddress ip6Group = InetAddress.getByName("ff02::a"); |
|
203 |
||
204 |
||
205 |
NetworkConfiguration config = NetworkConfiguration.probe(); |
|
206 |
||
207 |
NetworkInterface nif = config.ip4Interfaces().iterator().next(); |
|
208 |
InetAddress anySource = config.ip4Addresses(nif).iterator().next(); |
|
209 |
membershipKeyTests(nif, ip4Group, anySource); |
|
210 |
exceptionTests(nif); |
|
211 |
||
212 |
// re-run the membership key tests with IPv6 if available |
|
213 |
||
214 |
Iterator<NetworkInterface> iter = config.ip6Interfaces().iterator(); |
|
215 |
if (iter.hasNext()) { |
|
216 |
nif = iter.next(); |
|
217 |
anySource = config.ip6Addresses(nif).iterator().next(); |
|
218 |
membershipKeyTests(nif, ip6Group, anySource); |
|
219 |
} |
|
220 |
} |
|
221 |
} |