|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, 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 | +/* |
| 25 | + * @test |
| 26 | + * @bug 8373677 |
| 27 | + * @summary Tests for verifying that a non-SSL server can detect |
| 28 | + * when a client attempts to use SSL. |
| 29 | + * @library /test/lib |
| 30 | + * @run junit/othervm ${test.main.class} |
| 31 | + */ |
| 32 | + |
| 33 | +import com.sun.net.httpserver.HttpExchange; |
| 34 | +import com.sun.net.httpserver.HttpHandler; |
| 35 | +import com.sun.net.httpserver.HttpServer; |
| 36 | +import jdk.test.lib.net.SimpleSSLContext; |
| 37 | +import jdk.test.lib.net.URIBuilder; |
| 38 | +import org.junit.jupiter.api.Assertions; |
| 39 | +import org.junit.jupiter.api.BeforeAll; |
| 40 | +import org.junit.jupiter.api.Test; |
| 41 | + |
| 42 | +import java.io.IOException; |
| 43 | +import java.io.InputStreamReader; |
| 44 | +import java.net.InetAddress; |
| 45 | +import java.net.InetSocketAddress; |
| 46 | +import java.net.Socket; |
| 47 | +import java.net.URI; |
| 48 | +import java.net.URISyntaxException; |
| 49 | +import java.net.http.HttpClient; |
| 50 | +import java.net.http.HttpRequest; |
| 51 | +import java.net.http.HttpResponse; |
| 52 | +import java.util.logging.ConsoleHandler; |
| 53 | +import java.util.logging.Level; |
| 54 | +import java.util.logging.Logger; |
| 55 | + |
| 56 | +import javax.net.ssl.SSLException; |
| 57 | + |
| 58 | +import static com.sun.net.httpserver.HttpExchange.RSPBODY_EMPTY; |
| 59 | +import static java.net.http.HttpClient.Builder.NO_PROXY; |
| 60 | +import static org.junit.jupiter.api.Assertions.*; |
| 61 | + |
| 62 | +public class ClearTextServerSSL { |
| 63 | + |
| 64 | + static final InetAddress LOOPBACK_ADDR = InetAddress.getLoopbackAddress(); |
| 65 | + static final boolean ENABLE_LOGGING = true; |
| 66 | + static final Logger logger = Logger.getLogger("com.sun.net.httpserver"); |
| 67 | + |
| 68 | + static final String CTXT_PATH = "/ClearTextServerSSL"; |
| 69 | + |
| 70 | + @BeforeAll |
| 71 | + public static void setup() { |
| 72 | + if (ENABLE_LOGGING) { |
| 73 | + ConsoleHandler ch = new ConsoleHandler(); |
| 74 | + logger.setLevel(Level.ALL); |
| 75 | + ch.setLevel(Level.ALL); |
| 76 | + logger.addHandler(ch); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void test() throws Exception { |
| 82 | + var sslContext = new SimpleSSLContext().get(); |
| 83 | + var handler = new TestHandler(); |
| 84 | + var server = HttpServer.create(new InetSocketAddress(LOOPBACK_ADDR, 0), 0); |
| 85 | + server.createContext(path(""), handler); |
| 86 | + server.start(); |
| 87 | + try (var client = HttpClient.newBuilder() |
| 88 | + .sslContext(sslContext) |
| 89 | + .proxy(NO_PROXY) |
| 90 | + .build()) { |
| 91 | + var request = HttpRequest.newBuilder() |
| 92 | + .uri(uri("http", server, path("/clear"))) |
| 93 | + .build(); |
| 94 | + var response = client.send(request, HttpResponse.BodyHandlers.ofString()); |
| 95 | + assertEquals(200, response.statusCode()); |
| 96 | + var sslRequest = HttpRequest.newBuilder() |
| 97 | + .uri(uri("https", server, path("/ssl"))) |
| 98 | + .build(); |
| 99 | + Assertions.assertThrows(SSLException.class, () -> { |
| 100 | + client.send(sslRequest, HttpResponse.BodyHandlers.ofString()); |
| 101 | + }); |
| 102 | + try (var socket = new Socket()) { |
| 103 | + socket.connect(server.getAddress()); |
| 104 | + byte[] badRequest = { |
| 105 | + 22, 'B', 'A', 'D', ' ', |
| 106 | + '/', ' ' , |
| 107 | + 'H', 'T', 'T', 'P', '/', '1', '.', '1' }; |
| 108 | + socket.getOutputStream().write(badRequest); |
| 109 | + socket.getOutputStream().flush(); |
| 110 | + var reader = new InputStreamReader(socket.getInputStream()); |
| 111 | + var line = reader.readAllLines(); |
| 112 | + Assertions.assertEquals("HTTP/1.1 400 Bad Request", line.get(0)); |
| 113 | + System.out.println("Got expected response:"); |
| 114 | + line.stream().map(l -> "\t" + l).forEach(System.out::println); |
| 115 | + } |
| 116 | + |
| 117 | + } finally { |
| 118 | + server.stop(0); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + // --- infra --- |
| 123 | + |
| 124 | + static String path(String path) { |
| 125 | + assert CTXT_PATH.startsWith("/"); |
| 126 | + assert !CTXT_PATH.endsWith("/"); |
| 127 | + if (path.startsWith("/")) { |
| 128 | + return CTXT_PATH + path; |
| 129 | + } else { |
| 130 | + return CTXT_PATH + "/" + path; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + static URI uri(String scheme, HttpServer server, String path) throws URISyntaxException { |
| 135 | + return URIBuilder.newBuilder() |
| 136 | + .scheme(scheme) |
| 137 | + .loopback() |
| 138 | + .port(server.getAddress().getPort()) |
| 139 | + .path(path) |
| 140 | + .build(); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * A test handler that reads any request bytes and sends |
| 145 | + * an empty 200 response |
| 146 | + */ |
| 147 | + static class TestHandler implements HttpHandler { |
| 148 | + @java.lang.Override |
| 149 | + public void handle(HttpExchange exchange) throws IOException { |
| 150 | + try (var reqBody = exchange.getRequestBody()) { |
| 151 | + reqBody.readAllBytes(); |
| 152 | + exchange.sendResponseHeaders(200, RSPBODY_EMPTY); |
| 153 | + } catch (Throwable t) { |
| 154 | + t.printStackTrace(); |
| 155 | + exchange.sendResponseHeaders(500, RSPBODY_EMPTY); |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments