SocketTest.java revision a9a57f2581773484d13e160fd3407692e825971a
1a9a57f2581773484d13e160fd3407692e825971aElliott Hughes/*
2a9a57f2581773484d13e160fd3407692e825971aElliott Hughes * Copyright (C) 2009 The Android Open Source Project
3a9a57f2581773484d13e160fd3407692e825971aElliott Hughes *
4a9a57f2581773484d13e160fd3407692e825971aElliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
5a9a57f2581773484d13e160fd3407692e825971aElliott Hughes * you may not use this file except in compliance with the License.
6a9a57f2581773484d13e160fd3407692e825971aElliott Hughes * You may obtain a copy of the License at
7a9a57f2581773484d13e160fd3407692e825971aElliott Hughes *
8a9a57f2581773484d13e160fd3407692e825971aElliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
9a9a57f2581773484d13e160fd3407692e825971aElliott Hughes *
10a9a57f2581773484d13e160fd3407692e825971aElliott Hughes * Unless required by applicable law or agreed to in writing, software
11a9a57f2581773484d13e160fd3407692e825971aElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
12a9a57f2581773484d13e160fd3407692e825971aElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a9a57f2581773484d13e160fd3407692e825971aElliott Hughes * See the License for the specific language governing permissions and
14a9a57f2581773484d13e160fd3407692e825971aElliott Hughes * limitations under the License.
15a9a57f2581773484d13e160fd3407692e825971aElliott Hughes */
16a9a57f2581773484d13e160fd3407692e825971aElliott Hughes
17a9a57f2581773484d13e160fd3407692e825971aElliott Hughespackage java.net;
18a9a57f2581773484d13e160fd3407692e825971aElliott Hughes
19a9a57f2581773484d13e160fd3407692e825971aElliott Hughesimport junit.framework.Test;
20a9a57f2581773484d13e160fd3407692e825971aElliott Hughesimport junit.framework.TestSuite;
21a9a57f2581773484d13e160fd3407692e825971aElliott Hughes
22a9a57f2581773484d13e160fd3407692e825971aElliott Hughespublic class SocketTest extends junit.framework.TestCase {
23a9a57f2581773484d13e160fd3407692e825971aElliott Hughes    /**
24a9a57f2581773484d13e160fd3407692e825971aElliott Hughes     * Our getLocalAddress and getLocalPort currently use getsockname(3).
25a9a57f2581773484d13e160fd3407692e825971aElliott Hughes     * This means they give incorrect results on closed sockets (as well
26a9a57f2581773484d13e160fd3407692e825971aElliott Hughes     * as requiring an unnecessary call into native code).
27a9a57f2581773484d13e160fd3407692e825971aElliott Hughes     */
28a9a57f2581773484d13e160fd3407692e825971aElliott Hughes    public void test_getLocalAddress_after_close() throws Exception {
29a9a57f2581773484d13e160fd3407692e825971aElliott Hughes        Socket s = new Socket();
30a9a57f2581773484d13e160fd3407692e825971aElliott Hughes        try {
31a9a57f2581773484d13e160fd3407692e825971aElliott Hughes            // Bind to an ephemeral local port.
32a9a57f2581773484d13e160fd3407692e825971aElliott Hughes            s.bind(new InetSocketAddress("localhost", 0));
33a9a57f2581773484d13e160fd3407692e825971aElliott Hughes            assertTrue(s.getLocalAddress().isLoopbackAddress());
34a9a57f2581773484d13e160fd3407692e825971aElliott Hughes            // What local port did we get?
35a9a57f2581773484d13e160fd3407692e825971aElliott Hughes            int localPort = s.getLocalPort();
36a9a57f2581773484d13e160fd3407692e825971aElliott Hughes            assertTrue(localPort > 0);
37a9a57f2581773484d13e160fd3407692e825971aElliott Hughes            // Now close the socket...
38a9a57f2581773484d13e160fd3407692e825971aElliott Hughes            s.close();
39a9a57f2581773484d13e160fd3407692e825971aElliott Hughes            // The RI returns the ANY address but the original local port after close.
40a9a57f2581773484d13e160fd3407692e825971aElliott Hughes            assertTrue(s.getLocalAddress().isAnyLocalAddress());
41a9a57f2581773484d13e160fd3407692e825971aElliott Hughes            assertEquals(localPort, s.getLocalPort());
42a9a57f2581773484d13e160fd3407692e825971aElliott Hughes        } finally {
43a9a57f2581773484d13e160fd3407692e825971aElliott Hughes            s.close();
44a9a57f2581773484d13e160fd3407692e825971aElliott Hughes        }
45a9a57f2581773484d13e160fd3407692e825971aElliott Hughes    }
46a9a57f2581773484d13e160fd3407692e825971aElliott Hughes}
47