Path: blob/master/test/jdk/javax/net/ssl/HttpsURLConnection/Equals.java
41152 views
/*1* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 805529926* @library /test/lib27* @modules jdk.httpserver28* @build jdk.test.lib.net.SimpleSSLContext29* @run main/othervm -Djavax.net.debug=ssl,handshake,record Equals30*/31import com.sun.net.httpserver.*;32import java.net.*;33import java.io.*;34import javax.net.ssl.*;35import java.util.concurrent.*;36import jdk.test.lib.net.SimpleSSLContext;3738public class Equals {3940static SSLContext ctx;4142public static void main(String[] args) throws Exception {43HttpsServer s2 = null;44ExecutorService executor = null;45try {46InetSocketAddress addr = new InetSocketAddress(0);47s2 = HttpsServer.create(addr, 0);48HttpHandler h = new Handler();49HttpContext c2 = s2.createContext("/test1", h);50executor = Executors.newCachedThreadPool();51s2.setExecutor(executor);52ctx = new SimpleSSLContext().get();53s2.setHttpsConfigurator(new HttpsConfigurator(ctx));54s2.start();55int httpsport = s2.getAddress().getPort();56System.out.printf("%nServer address: %s%n", s2.getAddress());57test(httpsport);58System.out.println("OK");59} finally {60if (s2 != null) {61s2.stop(2);62}63if (executor != null) {64executor.shutdown();65}66}67}6869static class Handler implements HttpHandler {7071int invocation = 1;7273public void handle(HttpExchange t)74throws IOException {75InputStream is = t.getRequestBody();76while (is.read() != -1) {77}78is.close();79t.sendResponseHeaders(200, 0);80t.close();81}82}8384static void test(int port) throws Exception {85System.out.printf("%nClient using port number: %s%n", port);86String spec = String.format("https://localhost:%s/test1/", port);87URL url = new URL(spec);88HttpsURLConnection urlcs = (HttpsURLConnection) url.openConnection();89urlcs.setHostnameVerifier(new HostnameVerifier() {90public boolean verify(String s, SSLSession s1) {91return true;92}93});94urlcs.setSSLSocketFactory(ctx.getSocketFactory());9596InputStream is = urlcs.getInputStream();97while (is.read() != -1) {98}99is.close();100if (!urlcs.equals(urlcs)) {101throw new RuntimeException("Test failed");102}103}104}105106107