NetworkChannelTests.java revision 524:343253d05123
1/*
2 * Copyright 2007-2008 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 4640544
26 * @summary Unit test for channels that implement NetworkChannel
27 */
28
29import java.nio.*;
30import java.nio.channels.*;
31import java.net.*;
32import java.io.IOException;
33import java.util.*;
34
35public class NetworkChannelTests {
36
37    static interface ChannelFactory {
38        NetworkChannel open() throws IOException;
39    }
40
41    static class BogusSocketAddress extends SocketAddress {
42    }
43
44    /**
45     * Exercise bind method.
46     */
47    static void bindTests(ChannelFactory factory) throws IOException {
48        NetworkChannel ch;
49
50        // AlreadyBoundException
51        ch = factory.open().bind(new InetSocketAddress(0));
52        try {
53            ch.bind(new InetSocketAddress(0));
54            throw new RuntimeException("AlreadyBoundException not thrown");
55        } catch (AlreadyBoundException x) {
56        }
57        ch.close();
58
59        // bind(null)
60        ch = factory.open().bind(null);
61        if (ch.getLocalAddress() == null)
62            throw new RuntimeException("socket not found");
63        ch.close();
64
65        // UnsupportedAddressTypeException
66        ch = factory.open();
67        try {
68            ch.bind(new BogusSocketAddress());
69            throw new RuntimeException("UnsupportedAddressTypeException not thrown");
70        } catch (UnsupportedAddressTypeException x) {
71        }
72        ch.close();
73
74        // ClosedChannelException
75        try {
76            ch.bind(new InetSocketAddress(0));
77            throw new RuntimeException("ClosedChannelException not thrown");
78        } catch (ClosedChannelException x) {
79        }
80    }
81
82    /**
83     * Exercise getLocalAddress method.
84     */
85    static void localAddressTests(ChannelFactory factory) throws IOException {
86        NetworkChannel ch;
87
88        // not bound
89        ch = factory.open();
90        if (ch.getLocalAddress() != null) {
91            throw new RuntimeException("Local address returned when not bound");
92        }
93
94        // bound
95        InetSocketAddress local =
96            (InetSocketAddress)(ch.bind(new InetSocketAddress(0)).getLocalAddress());
97        if (!local.getAddress().isAnyLocalAddress()) {
98            if (NetworkInterface.getByInetAddress(local.getAddress()) == null)
99                throw new RuntimeException("not bound to local address");
100        }
101        if (local.getPort() <= 0)
102            throw new RuntimeException("not bound to local port");
103
104        // closed
105        ch.close();
106        if (ch.getLocalAddress() != null) {
107            throw new RuntimeException("Local address return when closed");
108        }
109    }
110
111    /**
112     * Exercise getConnectedAddress method (SocketChannel only)
113     */
114    static void connectedAddressTests() throws IOException {
115        ServerSocketChannel ssc = ServerSocketChannel.open()
116            .bind(new InetSocketAddress(0));
117        InetSocketAddress local = (InetSocketAddress)(ssc.getLocalAddress());
118        int port = local.getPort();
119        InetSocketAddress server = new InetSocketAddress(InetAddress.getLocalHost(), port);
120
121        SocketChannel sc = SocketChannel.open();
122
123        // not connected
124        if (sc.getConnectedAddress() != null)
125            throw new RuntimeException("getConnectedAddress returned address when not connected");
126
127        // connected
128        sc.connect(server);
129        SocketAddress remote = sc.getConnectedAddress();
130        if (!remote.equals(server))
131            throw new RuntimeException("getConnectedAddress returned incorrect address");
132
133        // closed
134        sc.close();
135        if (sc.getConnectedAddress() != null)
136            throw new RuntimeException("getConnectedAddress returned address when closed");
137
138        ssc.close();
139    }
140
141    public static void main(String[] args) throws IOException {
142        ChannelFactory factory;
143
144        // -- SocketChannel --
145
146        factory = new ChannelFactory() {
147            public NetworkChannel open() throws IOException {
148                return SocketChannel.open();
149            }
150        };
151
152        bindTests(factory);
153        localAddressTests(factory);
154        connectedAddressTests();
155
156        // -- ServerSocketChannel --
157
158        factory = new ChannelFactory() {
159            public NetworkChannel open() throws IOException {
160                return ServerSocketChannel.open();
161            }
162        };
163
164        bindTests(factory);
165        localAddressTests(factory);
166
167        // backlog values
168        ServerSocketChannel.open()
169            .bind(new InetSocketAddress(0), 100).close();
170        ServerSocketChannel.open()
171            .bind(new InetSocketAddress(0), 0).close();
172        ServerSocketChannel.open()
173            .bind(new InetSocketAddress(0), -1).close();
174
175        // -- DatagramChannel --
176
177        factory = new ChannelFactory() {
178            public NetworkChannel open() throws IOException {
179                return DatagramChannel.open();
180            }
181        };
182
183        bindTests(factory);
184        localAddressTests(factory);
185    }
186
187}
188