File: ImplAccept.java

package info (click to toggle)
openjdk-21 21.0.8%2B9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 823,976 kB
  • sloc: java: 5,613,338; xml: 1,643,607; cpp: 1,296,296; ansic: 420,291; asm: 404,850; objc: 20,994; sh: 15,271; javascript: 11,245; python: 6,895; makefile: 2,362; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (293 lines) | stat: -rw-r--r-- 10,025 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
 * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 8278326
 * @modules java.base/java.net:+open
 * @run junit ImplAccept
 * @summary Test ServerSocket.implAccept with a Socket in different states.
 */

import java.io.FileDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketImpl;
import java.net.SocketOption;
import java.net.StandardSocketOptions;
import java.io.IOException;

import org.junit.*;
import static org.junit.jupiter.api.Assertions.*;

public class ImplAccept {

    /**
     * Test ServerSocket.implAccept with an unbound Socket.
     */
    @Test
    public void testUnbound() throws Exception {
        try (Socket socket = new Socket()) {

            // Socket.impl -> DelegatingSocketImpl
            SocketImpl si = getSocketImpl(socket);
            assertTrue(isDelegatingSocketImpl(si));

            try (ServerSocket ss = serverSocketToAccept(socket);
                 Socket peer = new Socket(ss.getInetAddress(), ss.getLocalPort())) {

                Socket s = ss.accept();
                assertTrue(s == socket);

                // Socket.impl should be replaced with a new PlatformSocketImpl
                SocketImpl psi = getSocketImpl(socket);
                assertTrue(isPlatformSocketImpl(psi));

                // socket and peer should be connected to each other
                pingPong(socket, peer);
            }
        }
    }

    /**
     * Test ServerSocket.implAccept with a bound Socket. The usage is nonsensical
     * but we can test that the accepted Socket is connected and the underlying
     * socket from the original SocketImpl is closed.
     */
    @Test
    public void testBound() throws Exception {
        try (Socket socket = new Socket()) {

            // Socket.impl -> DelegatingSocketImpl -> PlatformSocketImpl
            SocketImpl si = getSocketImpl(socket);
            SocketImpl psi1 = getDelegate(si);
            assertTrue(isPlatformSocketImpl(psi1));

            // bind to local address
            socket.bind(loopbackSocketAddress());
            assertTrue(isSocketOpen(psi1));

            try (ServerSocket ss = serverSocketToAccept(socket);
                 Socket peer = new Socket(ss.getInetAddress(), ss.getLocalPort())) {

                Socket s = ss.accept();
                assertTrue(s == socket);

                // Socket.impl should be replaced with a new PlatformSocketImpl
                SocketImpl psi2 = getSocketImpl(socket);
                assertTrue(isPlatformSocketImpl(psi2));

                // psi1 should be closed
                assertFalse(isSocketOpen(psi1));

                // socket and peer should be connected to each other
                pingPong(socket, peer);
            }
        }
    }

    /**
     * Test ServerSocket.implAccept with a connected Socket. The usage is nonsensical
     * but we can test that the accepted Socket is connected and the underlying
     * socket from the original SocketImpl is closed.
     */
    @Test
    public void testConnected() throws Exception {
        Socket socket;
        Socket peer1;
        try (ServerSocket ss = new ServerSocket()) {
            ss.bind(loopbackSocketAddress());
            socket = new Socket(ss.getInetAddress(), ss.getLocalPort());
            peer1 = ss.accept();
        }

        try {
            // Socket.impl -> DelegatingSocketImpl -> PlatformSocketImpl
            SocketImpl si = getSocketImpl(socket);
            SocketImpl psi1 = getDelegate(si);
            assertTrue(isPlatformSocketImpl(psi1));

            try (ServerSocket ss = serverSocketToAccept(socket);
                 Socket peer2 = new Socket(ss.getInetAddress(), ss.getLocalPort())) {

                Socket s = ss.accept();
                assertTrue(s == socket);

                // Socket.impl should be replaced with a new PlatformSocketImpl
                SocketImpl psi2 = getSocketImpl(socket);
                assertTrue(isPlatformSocketImpl(psi2));

                // psi1 should be closed and peer1 should read EOF
                assertFalse(isSocketOpen(psi1));
                assertTrue(peer1.getInputStream().read() == -1);

                // socket and peer2 should be connected to each other
                pingPong(socket, peer2);
            }
        } finally {
            socket.close();
            peer1.close();
        }
    }

    /**
     * Test ServerSocket.implAccept with a closed Socket. The usage is nonsensical
     * but we can test ServerSocket.accept throws and that it closes the connection
     * to the peer.
     */
    @Test
    public void testClosed() throws Exception {
        Socket socket = new Socket();
        socket.close();

        try (ServerSocket ss = serverSocketToAccept(socket);
             Socket peer = new Socket(ss.getInetAddress(), ss.getLocalPort())) {

            SocketImpl si = getSocketImpl(socket);

            // accept should throw and peer should read EOF
            assertThrows(IOException.class, ss::accept);
            assertTrue(peer.getInputStream().read() == -1);

            // the SocketImpl should have not changed
            assertTrue(getSocketImpl(socket) == si);
        }
    }

    /**
     * Returns the socket's SocketImpl.
     */
    private static SocketImpl getSocketImpl(Socket s) {
        try {
            Field f = Socket.class.getDeclaredField("impl");
            f.setAccessible(true);
            return (SocketImpl) f.get(s);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Returns the SocketImpl that the given SocketImpl delegates to.
     */
    private static SocketImpl getDelegate(SocketImpl si) {
        try {
            Class<?> clazz = Class.forName("java.net.DelegatingSocketImpl");
            Field f = clazz.getDeclaredField("delegate");
            f.setAccessible(true);
            return (SocketImpl) f.get(si);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Returns true if the SocketImpl is a DelegatingSocketImpl.
     */
    private static boolean isDelegatingSocketImpl(SocketImpl si) {
        try {
            Class<?> clazz = Class.forName("java.net.DelegatingSocketImpl");
            return clazz.isInstance(si);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Returns true if the SocketImpl is a PlatformSocketImpl.
     */
    private static boolean isPlatformSocketImpl(SocketImpl si) {
        try {
            Class<?> clazz = Class.forName("sun.net.PlatformSocketImpl");
            return clazz.isInstance(si);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Returns true if the SocketImpl has an open socket.
     */
    private static boolean isSocketOpen(SocketImpl si) throws Exception {
        assertTrue(isPlatformSocketImpl(si));

        // check if SocketImpl.fd is set
        Field f = SocketImpl.class.getDeclaredField("fd");
        f.setAccessible(true);
        FileDescriptor fd = (FileDescriptor) f.get(si);
        if (fd == null) {
            return false;  // not created
        }

        // call getOption to get the value of the SO_REUSEADDR socket option
        Method m = SocketImpl.class.getDeclaredMethod("getOption", SocketOption.class);
        m.setAccessible(true);
        try {
            m.invoke(si, StandardSocketOptions.SO_REUSEADDR);
            return true; // socket is open
        } catch (InvocationTargetException e) {
            if (e.getCause() instanceof IOException) {
                return false; // assume socket is closed
            }
            throw e;
        }
    }

    /**
     * Test that two sockets are connected to each other.
     */
    private static void pingPong(Socket s1, Socket s2) throws Exception {
        s1.getOutputStream().write(11);
        s2.getOutputStream().write(22);
        assertTrue(s1.getInputStream().read() == 22);
        assertTrue(s2.getInputStream().read() == 11);
    }

    /**
     * Creates a ServerSocket that returns the given Socket from accept.
     */
    private static ServerSocket serverSocketToAccept(Socket s) throws IOException {
        ServerSocket ss = new ServerSocket() {
            @Override
            public Socket accept() throws IOException {
                implAccept(s);
                return s;
            }
        };
        ss.bind(loopbackSocketAddress());
        return ss;
    }

    /**
     * Returns a new InetSocketAddress with the loopback interface and port 0.
     */
    private static InetSocketAddress loopbackSocketAddress() {
        InetAddress loopback = InetAddress.getLoopbackAddress();
        return new InetSocketAddress(loopback, 0);
    }
}