Path: blob/master/test/jdk/java/net/Authenticator/Deadlock.java
41152 views
/*1* Copyright (c) 2010, 2019, 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 664800126* @modules jdk.httpserver27* @library /test/lib28* @run main/othervm/timeout=20 -ea:sun.net.www.protocol.http.AuthenticationInfo -Dhttp.auth.serializeRequests=true Deadlock29* @run main/othervm/timeout=20 -Djava.net.preferIPv6Addresses=true30* -ea:sun.net.www.protocol.http.AuthenticationInfo -Dhttp.auth.serializeRequests=true Deadlock31* @summary cancelling HTTP authentication causes deadlock32*/3334import java.util.concurrent.Executors;35import java.util.concurrent.ExecutorService;36import java.io.InputStream;37import java.io.IOException;38import java.net.HttpURLConnection;39import java.net.InetAddress;40import java.net.InetSocketAddress;41import java.net.PasswordAuthentication;42import java.net.Proxy;43import java.net.URL;44import com.sun.net.httpserver.BasicAuthenticator;45import com.sun.net.httpserver.Headers;46import com.sun.net.httpserver.HttpContext;47import com.sun.net.httpserver.HttpExchange;48import com.sun.net.httpserver.HttpHandler;49import com.sun.net.httpserver.HttpPrincipal;50import com.sun.net.httpserver.HttpServer;51import jdk.test.lib.net.URIBuilder;5253public class Deadlock {5455public static void main (String[] args) throws Exception {56Handler handler = new Handler();57InetAddress loopback = InetAddress.getLoopbackAddress();58InetSocketAddress addr = new InetSocketAddress (loopback, 0);59HttpServer server = HttpServer.create(addr, 0);60HttpContext ctx = server.createContext("/test", handler);61BasicAuthenticator a = new BasicAuthenticator("[email protected]") {62@Override63public boolean checkCredentials (String username, String pw) {64return "fred".equals(username) && pw.charAt(0) == 'x';65}66};6768ctx.setAuthenticator(a);69ExecutorService executor = Executors.newCachedThreadPool();70server.setExecutor(executor);71server.start ();72java.net.Authenticator.setDefault(new MyAuthenticator());7374System.out.print("Deadlock: " );75for (int i=0; i<2; i++) {76Runner t = new Runner(server, i);77t.start();78t.join();79}80server.stop(2);81executor.shutdown();82if (error) {83throw new RuntimeException("test failed error");84}8586if (count != 2) {87throw new RuntimeException("test failed count = " + count);88}89System.out.println("OK");9091}9293static class Runner extends Thread {94HttpServer server;95int i;96Runner(HttpServer s, int i) {97server = s;98this.i = i;99}100101@Override102public void run() {103URL url;104HttpURLConnection urlc;105try {106url = URIBuilder.newBuilder()107.scheme("http")108.loopback()109.port(server.getAddress().getPort())110.path("/test/foo.html")111.toURLUnchecked();112urlc = (HttpURLConnection)url.openConnection (Proxy.NO_PROXY);113} catch (IOException e) {114error = true;115return;116}117InputStream is = null;118try {119is = urlc.getInputStream();120while (is.read()!= -1) {}121} catch (IOException e) {122if (i == 1) error = true;123} finally {124if (is != null) try { is.close(); } catch (IOException e) {}125}126}127}128129public static boolean error = false;130public static int count = 0;131132static class MyAuthenticator extends java.net.Authenticator {133@Override134public PasswordAuthentication getPasswordAuthentication() {135PasswordAuthentication pw;136if (!getRequestingPrompt().equals("[email protected]")) {137Deadlock.error = true;138}139if (count == 0) {140pw = null;141} else {142pw = new PasswordAuthentication("fred", "xyz".toCharArray());143}144count++;145return pw;146}147}148149static class Handler implements HttpHandler {150int invocation = 1;151152@Override153public void handle (HttpExchange t)154throws IOException155{156InputStream is = t.getRequestBody();157Headers map = t.getRequestHeaders();158Headers rmap = t.getResponseHeaders();159while (is.read() != -1);160is.close();161t.sendResponseHeaders(200, -1);162HttpPrincipal p = t.getPrincipal();163if (!p.getUsername().equals("fred")) {164error = true;165}166if (!p.getRealm().equals("[email protected]")) {167error = true;168}169t.close();170}171}172}173174175