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 *
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.
22 */
23
24 /* @test
25 * @bug 4527345
26 * @summary Unit test for DatagramChannel's multicast support
27 * @build BasicMulticastTests NetworkConfiguration
28 * @run main BasicMulticastTests
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(StandardSocketOptions.SO_REUSEADDR, true)
56 .bind(new InetSocketAddress(source, 0));
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 }
|
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 *
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.
22 */
23
24 /* @test
25 * @bug 4527345
26 * @summary Unit test for DatagramChannel's multicast support
27 * @library /test/lib
28 * @build BasicMulticastTests
29 * @run main BasicMulticastTests
30 */
31
32 import java.nio.ByteBuffer;
33 import java.nio.channels.*;
34 import java.net.*;
35 import java.util.*;
36 import java.io.IOException;
37
38 import jdk.test.lib.NetworkConfiguration;
39
40 public class BasicMulticastTests {
41
42 /**
43 * Tests that existing membership key is returned by join methods and that
44 * membership key methods return the expected results
45 */
46 static void membershipKeyTests(NetworkInterface nif,
47 InetAddress group,
48 InetAddress source)
49 throws IOException
50 {
51 System.out.format("MembershipKey test using %s @ %s\n",
52 group.getHostAddress(), nif.getName());
53
54 ProtocolFamily family = (group instanceof Inet4Address) ?
55 StandardProtocolFamily.INET : StandardProtocolFamily.INET6;
56
57 DatagramChannel dc = DatagramChannel.open(family)
58 .setOption(StandardSocketOptions.SO_REUSEADDR, true)
59 .bind(new InetSocketAddress(source, 0));
190 throw new RuntimeException("ClosedChannelException not thrown");
191 } catch (ClosedChannelException x) {
192 } catch (UnsupportedOperationException x) {
193 }
194 }
195
196
197 /**
198 * Probe interfaces to get interfaces that support IPv4 or IPv6 multicasting
199 * and invoke tests.
200 */
201 public static void main(String[] args) throws IOException {
202
203 // multicast groups used for the test
204 InetAddress ip4Group = InetAddress.getByName("225.4.5.6");
205 InetAddress ip6Group = InetAddress.getByName("ff02::a");
206
207
208 NetworkConfiguration config = NetworkConfiguration.probe();
209
210 NetworkInterface nif = config.ip4MulticastInterfaces().iterator().next();
211 InetAddress anySource = config.ip4Addresses(nif).iterator().next();
212 membershipKeyTests(nif, ip4Group, anySource);
213 exceptionTests(nif);
214
215 // re-run the membership key tests with IPv6 if available
216
217 Iterator<NetworkInterface> iter = config.ip6MulticastInterfaces().iterator();
218 if (iter.hasNext()) {
219 nif = iter.next();
220 anySource = config.ip6Addresses(nif).iterator().next();
221 membershipKeyTests(nif, ip6Group, anySource);
222 }
223 }
224 }
|