2278N/A/*
2362N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2278N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2278N/A *
2278N/A * This code is free software; you can redistribute it and/or modify it
2278N/A * under the terms of the GNU General Public License version 2 only, as
2278N/A * published by the Free Software Foundation.
2278N/A *
2278N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2278N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2278N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2278N/A * version 2 for more details (a copy is included in the LICENSE file that
2278N/A * accompanied this code).
2278N/A *
2278N/A * You should have received a copy of the GNU General Public License version
2278N/A * 2 along with this work; if not, write to the Free Software Foundation,
2278N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2278N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
2278N/A */
2278N/A
2278N/A/**
2278N/A * @test
2278N/A * @bug 6648001
2278N/A * @run main/othervm/timeout=20 -ea:sun.net.www.protocol.http.AuthenticationInfo -Dhttp.auth.serializeRequests=true Deadlock
2278N/A * @summary cancelling HTTP authentication causes deadlock
2278N/A */
2278N/A
2278N/Aimport java.util.concurrent.Executors;
2278N/Aimport java.util.concurrent.ExecutorService;
2278N/Aimport java.io.InputStream;
2278N/Aimport java.io.IOException;
2278N/Aimport java.net.HttpURLConnection;
2278N/Aimport java.net.InetSocketAddress;
2278N/Aimport java.net.PasswordAuthentication;
2278N/Aimport java.net.URL;
2278N/Aimport com.sun.net.httpserver.BasicAuthenticator;
2278N/Aimport com.sun.net.httpserver.Headers;
2278N/Aimport com.sun.net.httpserver.HttpContext;
2278N/Aimport com.sun.net.httpserver.HttpExchange;
2278N/Aimport com.sun.net.httpserver.HttpHandler;
2278N/Aimport com.sun.net.httpserver.HttpPrincipal;
2278N/Aimport com.sun.net.httpserver.HttpServer;
2278N/A
2278N/Apublic class Deadlock {
2278N/A
2278N/A public static void main (String[] args) throws Exception {
2278N/A Handler handler = new Handler();
2278N/A InetSocketAddress addr = new InetSocketAddress (0);
2278N/A HttpServer server = HttpServer.create(addr, 0);
2278N/A HttpContext ctx = server.createContext("/test", handler);
2278N/A BasicAuthenticator a = new BasicAuthenticator("foobar@test.realm") {
2278N/A @Override
2278N/A public boolean checkCredentials (String username, String pw) {
2278N/A return "fred".equals(username) && pw.charAt(0) == 'x';
2278N/A }
2278N/A };
2278N/A
2278N/A ctx.setAuthenticator(a);
2278N/A ExecutorService executor = Executors.newCachedThreadPool();
2278N/A server.setExecutor(executor);
2278N/A server.start ();
2278N/A java.net.Authenticator.setDefault(new MyAuthenticator());
2278N/A
2278N/A System.out.print("Deadlock: " );
2278N/A for (int i=0; i<2; i++) {
2278N/A Runner t = new Runner(server, i);
2278N/A t.start();
2278N/A t.join();
2278N/A }
2278N/A server.stop(2);
2278N/A executor.shutdown();
2278N/A if (error) {
2278N/A throw new RuntimeException("test failed error");
2278N/A }
2278N/A
2278N/A if (count != 2) {
2278N/A throw new RuntimeException("test failed count = " + count);
2278N/A }
2278N/A System.out.println("OK");
2278N/A
2278N/A }
2278N/A
2278N/A static class Runner extends Thread {
2278N/A HttpServer server;
2278N/A int i;
2278N/A Runner(HttpServer s, int i) {
2278N/A server = s;
2278N/A this.i = i;
2278N/A }
2278N/A
2278N/A @Override
2278N/A public void run() {
2278N/A URL url;
2278N/A HttpURLConnection urlc;
2278N/A try {
2278N/A url = new URL("http://localhost:"+server.getAddress().getPort()+"/test/foo.html");
2278N/A urlc = (HttpURLConnection)url.openConnection ();
2278N/A } catch (IOException e) {
2278N/A error = true;
2278N/A return;
2278N/A }
2278N/A InputStream is = null;
2278N/A try {
2278N/A is = urlc.getInputStream();
2278N/A while (is.read()!= -1) {}
2278N/A } catch (IOException e) {
2278N/A if (i == 1) error = true;
2278N/A } finally {
2278N/A if (is != null) try { is.close(); } catch (IOException e) {}
2278N/A }
2278N/A }
2278N/A }
2278N/A
2278N/A public static boolean error = false;
2278N/A public static int count = 0;
2278N/A
2278N/A static class MyAuthenticator extends java.net.Authenticator {
2278N/A @Override
2278N/A public PasswordAuthentication getPasswordAuthentication() {
2278N/A PasswordAuthentication pw;
2278N/A if (!getRequestingPrompt().equals("foobar@test.realm")) {
2278N/A Deadlock.error = true;
2278N/A }
2278N/A if (count == 0) {
2278N/A pw = null;
2278N/A } else {
2278N/A pw = new PasswordAuthentication("fred", "xyz".toCharArray());
2278N/A }
2278N/A count++;
2278N/A return pw;
2278N/A }
2278N/A }
2278N/A
2278N/A static class Handler implements HttpHandler {
2278N/A int invocation = 1;
2278N/A
2278N/A @Override
2278N/A public void handle (HttpExchange t)
2278N/A throws IOException
2278N/A {
2278N/A InputStream is = t.getRequestBody();
2278N/A Headers map = t.getRequestHeaders();
2278N/A Headers rmap = t.getResponseHeaders();
2278N/A while (is.read() != -1);
2278N/A is.close();
2278N/A t.sendResponseHeaders(200, -1);
2278N/A HttpPrincipal p = t.getPrincipal();
2278N/A if (!p.getUsername().equals("fred")) {
2278N/A error = true;
2278N/A }
2278N/A if (!p.getRealm().equals("foobar@test.realm")) {
2278N/A error = true;
2278N/A }
2278N/A t.close();
2278N/A }
2278N/A }
2278N/A}