File: SANTest.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 (204 lines) | stat: -rw-r--r-- 7,457 bytes parent folder | download | duplicates (11)
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
/*
 * Copyright (c) 2021, 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 8278312
 * @library /test/lib /test/jdk/java/net/httpclient /test/jdk/java/net/httpclient/lib
 * @build jdk.test.lib.net.SimpleSSLContext jdk.httpclient.test.lib.common.HttpServerAdapters
 *        jdk.httpclient.test.lib.http2.Http2TestServer
 *        jdk.test.lib.net.IPSupport
 *
 * @modules java.net.http/jdk.internal.net.http.common
 *          java.net.http/jdk.internal.net.http.frame
 *          java.net.http/jdk.internal.net.http.hpack
 *          java.logging
 *          java.base/sun.net.www.http
 *          java.base/sun.net.www
 *          java.base/sun.net
 *
 * @run main/othervm SANTest
 * @summary Update SimpleSSLContext keystore to use SANs for localhost IP addresses
 */

import com.sun.net.httpserver.*;

import java.util.concurrent.*;
import java.io.*;
import java.net.*;
import java.net.http.*;
import java.nio.charset.StandardCharsets;
import javax.net.ssl.*;
import jdk.test.lib.net.SimpleSSLContext;
import jdk.test.lib.net.URIBuilder;
import jdk.test.lib.net.IPSupport;
import jdk.httpclient.test.lib.common.HttpServerAdapters;
import jdk.httpclient.test.lib.http2.Http2TestServer;

/*
 * Will fail if the testkeys file belonging to SimpleSSLContext
 * does not have SAN entries for 127.0.0.1 or ::1
 */
public class SANTest implements HttpServerAdapters {

    static SSLContext ctx;

    static HttpServer getHttpsServer(InetSocketAddress addr, Executor exec, SSLContext ctx) throws Exception {
        HttpsServer server = HttpsServer.create(addr, 0);
        server.setExecutor(exec);
        server.setHttpsConfigurator(new HttpsConfigurator (ctx));
        return server;
    }

    static final boolean hasIPv4 = IPSupport.hasIPv4();
    static final boolean hasIPv6 = IPSupport.hasIPv6();

    static HttpTestServer initServer(boolean h2, InetAddress addr, SSLContext ctx,
                String sni, ExecutorService e) throws Exception {
        HttpTestServer s = null;
        InetSocketAddress ia = new InetSocketAddress (addr, 0);
        if ((addr instanceof Inet4Address) && !hasIPv4)
                return null;
        if ((addr instanceof Inet6Address) && !hasIPv6)
                return null;

        if (!h2) {
            s = HttpTestServer.of(getHttpsServer(ia, e, ctx));
            HttpTestHandler h = new HttpTestEchoHandler();
            s.addHandler(h, "/test1");
            s.start();
            return s;
        } else {
            s = HttpTestServer.of(new Http2TestServer(addr, sni, true, 0, e,
                        10, null, ctx, false));
            HttpTestHandler h = new HttpTestEchoHandler();
            s.addHandler(h, "/test1");
            s.start();
            return s;
        }
    }

    public static void main (String[] args) throws Exception {
        // Http/1.1 servers
        HttpTestServer h1s1 = null;
        HttpTestServer h1s2 = null;

        // Http/2 servers
        HttpTestServer h2s1 = null;
        HttpTestServer h2s2 = null;

        ExecutorService executor=null;
        try {
            System.out.print ("SANTest: ");
            ctx = new SimpleSSLContext().get();
            executor = Executors.newCachedThreadPool();

            InetAddress l1 = InetAddress.getByName("::1");
            InetAddress l2 = InetAddress.getByName("127.0.0.1");

            h1s1 = initServer(false, l1, ctx, "::1", executor);
            h1s2 = initServer(false, l2, ctx, "127.0.0.1", executor);

            h2s1 = initServer(true, l1, ctx, "::1", executor);
            h2s2 = initServer(true, l2, ctx, "127.0.0.1", executor);

            test("127.0.0.1", h1s2);
            test("::1", h1s1);
            testNew("127.0.0.1", h2s2, executor);
            testNew("::1", h2s1, executor);
            System.out.println ("OK");
        } finally {
            if (h1s1 != null)
                h1s1.stop();
            if (h1s2 != null)
                h1s2.stop();
            if (h2s1 != null)
                h2s1.stop();
            if (h2s2 != null)
                h2s2.stop();
            if (executor != null)
                executor.shutdown ();
        }
    }

    static void test (String host, HttpTestServer server) throws Exception {
        if (server == null)
            return;
        int port = server.getAddress().getPort();
        String body = "Yellow world";
        URL url = URIBuilder.newBuilder()
                 .scheme("https")
                 .host(host)
                 .port(port)
                 .path("/test1/foo.txt")
                 .toURL();
        System.out.println("URL = " + url);
        HttpURLConnection urlc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
        System.out.println("urlc = " + urlc);
        if (urlc instanceof HttpsURLConnection) {
            HttpsURLConnection urlcs = (HttpsURLConnection) urlc;
            urlcs.setSSLSocketFactory (ctx.getSocketFactory());
        }

        urlc.setRequestMethod("POST");
        urlc.setDoOutput(true);

        OutputStream os = urlc.getOutputStream();
        os.write(body.getBytes(StandardCharsets.ISO_8859_1));
        os.close();
        InputStream is = urlc.getInputStream();
        byte[] vv = is.readAllBytes();
        String ff = new String(vv, StandardCharsets.ISO_8859_1);
        System.out.println("resp = " + ff);
        if (!ff.equals(body))
            throw new RuntimeException();
        is.close();
    }

    static void testNew (String host, HttpTestServer server, Executor exec) throws Exception {
        if (server == null)
            return;
        int port = server.getAddress().getPort();
        String body = "Red and Yellow world";
        URI uri = URIBuilder.newBuilder()
                 .scheme("https")
                 .host(host)
                 .port(port)
                 .path("/test1/foo.txt")
                 .build();

        HttpClient client = HttpClient.newBuilder()
                .sslContext(ctx)
                .executor(exec)
                .build();
        HttpRequest req = HttpRequest.newBuilder(uri)
                .version(HttpClient.Version.HTTP_2)
                .POST(HttpRequest.BodyPublishers.ofString(body))
                .build();

        HttpResponse<String> resp = client.send(req, HttpResponse.BodyHandlers.ofString());
        System.out.println("resp = " + resp.body());
        if (!resp.body().equals(body))
            throw new RuntimeException();
    }
}