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