Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/Authenticator/Deadlock.java
38821 views
/*1* Copyright (c) 2010, 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* @run main/othervm/timeout=20 -ea:sun.net.www.protocol.http.AuthenticationInfo -Dhttp.auth.serializeRequests=true Deadlock27* @summary cancelling HTTP authentication causes deadlock28*/2930import java.util.concurrent.Executors;31import java.util.concurrent.ExecutorService;32import java.io.InputStream;33import java.io.IOException;34import java.net.HttpURLConnection;35import java.net.InetSocketAddress;36import java.net.PasswordAuthentication;37import java.net.URL;38import com.sun.net.httpserver.BasicAuthenticator;39import com.sun.net.httpserver.Headers;40import com.sun.net.httpserver.HttpContext;41import com.sun.net.httpserver.HttpExchange;42import com.sun.net.httpserver.HttpHandler;43import com.sun.net.httpserver.HttpPrincipal;44import com.sun.net.httpserver.HttpServer;4546public class Deadlock {4748public static void main (String[] args) throws Exception {49Handler handler = new Handler();50InetSocketAddress addr = new InetSocketAddress (0);51HttpServer server = HttpServer.create(addr, 0);52HttpContext ctx = server.createContext("/test", handler);53BasicAuthenticator a = new BasicAuthenticator("[email protected]") {54@Override55public boolean checkCredentials (String username, String pw) {56return "fred".equals(username) && pw.charAt(0) == 'x';57}58};5960ctx.setAuthenticator(a);61ExecutorService executor = Executors.newCachedThreadPool();62server.setExecutor(executor);63server.start ();64java.net.Authenticator.setDefault(new MyAuthenticator());6566System.out.print("Deadlock: " );67for (int i=0; i<2; i++) {68Runner t = new Runner(server, i);69t.start();70t.join();71}72server.stop(2);73executor.shutdown();74if (error) {75throw new RuntimeException("test failed error");76}7778if (count != 2) {79throw new RuntimeException("test failed count = " + count);80}81System.out.println("OK");8283}8485static class Runner extends Thread {86HttpServer server;87int i;88Runner(HttpServer s, int i) {89server = s;90this.i = i;91}9293@Override94public void run() {95URL url;96HttpURLConnection urlc;97try {98url = new URL("http://localhost:"+server.getAddress().getPort()+"/test/foo.html");99urlc = (HttpURLConnection)url.openConnection ();100} catch (IOException e) {101error = true;102return;103}104InputStream is = null;105try {106is = urlc.getInputStream();107while (is.read()!= -1) {}108} catch (IOException e) {109if (i == 1) error = true;110} finally {111if (is != null) try { is.close(); } catch (IOException e) {}112}113}114}115116public static boolean error = false;117public static int count = 0;118119static class MyAuthenticator extends java.net.Authenticator {120@Override121public PasswordAuthentication getPasswordAuthentication() {122PasswordAuthentication pw;123if (!getRequestingPrompt().equals("[email protected]")) {124Deadlock.error = true;125}126if (count == 0) {127pw = null;128} else {129pw = new PasswordAuthentication("fred", "xyz".toCharArray());130}131count++;132return pw;133}134}135136static class Handler implements HttpHandler {137int invocation = 1;138139@Override140public void handle (HttpExchange t)141throws IOException142{143InputStream is = t.getRequestBody();144Headers map = t.getRequestHeaders();145Headers rmap = t.getResponseHeaders();146while (is.read() != -1);147is.close();148t.sendResponseHeaders(200, -1);149HttpPrincipal p = t.getPrincipal();150if (!p.getUsername().equals("fred")) {151error = true;152}153if (!p.getRealm().equals("[email protected]")) {154error = true;155}156t.close();157}158}159}160161162