|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +import jdk.test.lib.Utils; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.junit.jupiter.params.ParameterizedTest; |
| 27 | +import org.junit.jupiter.params.provider.MethodSource; |
| 28 | + |
| 29 | +import java.io.IOException; |
| 30 | +import java.net.InetAddress; |
| 31 | +import java.net.InetSocketAddress; |
| 32 | +import java.net.ServerSocket; |
| 33 | +import java.net.Socket; |
| 34 | +import java.net.SocketException; |
| 35 | +import java.net.UnknownHostException; |
| 36 | +import java.nio.channels.SocketChannel; |
| 37 | +import java.util.List; |
| 38 | + |
| 39 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 40 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 41 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 42 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 43 | + |
| 44 | +/* |
| 45 | + * @test |
| 46 | + * @bug 8343791 |
| 47 | + * @summary verifies that `connect()` failures throw the expected exception and leave socket in the expected state |
| 48 | + * @library /test/lib |
| 49 | + * @run junit ConnectFailTest |
| 50 | + */ |
| 51 | +class ConnectFailTest { |
| 52 | + |
| 53 | + private static final int DEAD_SERVER_PORT = 0xDEAD; |
| 54 | + |
| 55 | + private static final InetSocketAddress REFUSING_SOCKET_ADDRESS = Utils.refusingEndpoint(); |
| 56 | + |
| 57 | + private static final InetSocketAddress UNRESOLVED_ADDRESS = |
| 58 | + InetSocketAddress.createUnresolved("no.such.host", DEAD_SERVER_PORT); |
| 59 | + |
| 60 | + @Test |
| 61 | + void testUnresolvedAddress() { |
| 62 | + assertTrue(UNRESOLVED_ADDRESS.isUnresolved()); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Verifies that an unbound socket is closed when {@code connect()} fails. |
| 67 | + */ |
| 68 | + @ParameterizedTest |
| 69 | + @MethodSource("sockets") |
| 70 | + void testUnboundSocket(Socket socket) throws IOException { |
| 71 | + try (socket) { |
| 72 | + assertFalse(socket.isBound()); |
| 73 | + assertFalse(socket.isConnected()); |
| 74 | + assertThrows(IOException.class, () -> socket.connect(REFUSING_SOCKET_ADDRESS)); |
| 75 | + assertTrue(socket.isClosed()); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Verifies that a bound socket is closed when {@code connect()} fails. |
| 81 | + */ |
| 82 | + @ParameterizedTest |
| 83 | + @MethodSource("sockets") |
| 84 | + void testBoundSocket(Socket socket) throws IOException { |
| 85 | + try (socket) { |
| 86 | + socket.bind(new InetSocketAddress(0)); |
| 87 | + assertTrue(socket.isBound()); |
| 88 | + assertFalse(socket.isConnected()); |
| 89 | + assertThrows(IOException.class, () -> socket.connect(REFUSING_SOCKET_ADDRESS)); |
| 90 | + assertTrue(socket.isClosed()); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Verifies that a connected socket is not closed when {@code connect()} fails. |
| 96 | + */ |
| 97 | + @ParameterizedTest |
| 98 | + @MethodSource("sockets") |
| 99 | + void testConnectedSocket(Socket socket) throws Throwable { |
| 100 | + try (socket; ServerSocket serverSocket = createEphemeralServerSocket()) { |
| 101 | + socket.connect(serverSocket.getLocalSocketAddress()); |
| 102 | + try (Socket _ = serverSocket.accept()) { |
| 103 | + assertTrue(socket.isBound()); |
| 104 | + assertTrue(socket.isConnected()); |
| 105 | + SocketException exception = assertThrows( |
| 106 | + SocketException.class, |
| 107 | + () -> socket.connect(REFUSING_SOCKET_ADDRESS)); |
| 108 | + assertEquals("Already connected", exception.getMessage()); |
| 109 | + assertFalse(socket.isClosed()); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Verifies that an unbound socket is closed when {@code connect()} is invoked using an unresolved address. |
| 116 | + */ |
| 117 | + @ParameterizedTest |
| 118 | + @MethodSource("sockets") |
| 119 | + void testUnboundSocketWithUnresolvedAddress(Socket socket) throws IOException { |
| 120 | + try (socket) { |
| 121 | + assertFalse(socket.isBound()); |
| 122 | + assertFalse(socket.isConnected()); |
| 123 | + assertThrows(UnknownHostException.class, () -> socket.connect(UNRESOLVED_ADDRESS)); |
| 124 | + assertTrue(socket.isClosed()); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Verifies that a bound socket is closed when {@code connect()} is invoked using an unresolved address. |
| 130 | + */ |
| 131 | + @ParameterizedTest |
| 132 | + @MethodSource("sockets") |
| 133 | + void testBoundSocketWithUnresolvedAddress(Socket socket) throws IOException { |
| 134 | + try (socket) { |
| 135 | + socket.bind(new InetSocketAddress(0)); |
| 136 | + assertTrue(socket.isBound()); |
| 137 | + assertFalse(socket.isConnected()); |
| 138 | + assertThrows(UnknownHostException.class, () -> socket.connect(UNRESOLVED_ADDRESS)); |
| 139 | + assertTrue(socket.isClosed()); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Verifies that a connected socket is not closed when {@code connect()} is invoked using an unresolved address. |
| 145 | + */ |
| 146 | + @ParameterizedTest |
| 147 | + @MethodSource("sockets") |
| 148 | + void testConnectedSocketWithUnresolvedAddress(Socket socket) throws Throwable { |
| 149 | + try (socket; ServerSocket serverSocket = createEphemeralServerSocket()) { |
| 150 | + socket.connect(serverSocket.getLocalSocketAddress()); |
| 151 | + try (Socket _ = serverSocket.accept()) { |
| 152 | + assertTrue(socket.isBound()); |
| 153 | + assertTrue(socket.isConnected()); |
| 154 | + assertThrows(IOException.class, () -> socket.connect(UNRESOLVED_ADDRESS)); |
| 155 | + assertFalse(socket.isClosed()); |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + static List<Socket> sockets() throws Exception { |
| 161 | + Socket socket = new Socket(); |
| 162 | + @SuppressWarnings("resource") |
| 163 | + Socket channelSocket = SocketChannel.open().socket(); |
| 164 | + return List.of(socket, channelSocket); |
| 165 | + } |
| 166 | + |
| 167 | + private static ServerSocket createEphemeralServerSocket() throws IOException { |
| 168 | + return new ServerSocket(0, 0, InetAddress.getLoopbackAddress()); |
| 169 | + } |
| 170 | + |
| 171 | +} |
0 commit comments