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