Path: blob/master/test/jdk/java/net/Authenticator/B4722333.java
41149 views
/*1* Copyright (c) 2002, 2021, 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 472233326* @library /test/lib27* @run main/othervm B472233328* @summary JRE Proxy Authentication Not Working with ISA200029*/3031import java.io.IOException;32import java.io.InputStream;33import java.io.PrintWriter;34import java.net.Authenticator;35import java.net.InetAddress;36import java.net.InetSocketAddress;37import java.net.PasswordAuthentication;38import java.net.URL;39import java.net.URLConnection;40import java.util.concurrent.Executors;4142import com.sun.net.httpserver.HttpExchange;43import com.sun.net.httpserver.HttpHandler;44import com.sun.net.httpserver.HttpServer;45import jdk.test.lib.net.URIBuilder;4647public class B4722333 implements HttpHandler {4849static int count = 0;5051static String[][] expected = {52/* scheme realm/prompt */53{"basic", "foo"},54{"basic", "foobar"},55{"digest", "biz"},56{"digest", "bizbar"},57{"digest", "foobiz"}58};5960public void handle(HttpExchange req) {61try {62if (count % 2 == 1) {63req.sendResponseHeaders(200, 0);64try(PrintWriter pw = new PrintWriter(req.getResponseBody())) {65pw.print("Hello .");66}67} else {68switch (count) {69case 0:70req.getResponseHeaders().set("Connection", "close");71req.getResponseHeaders().set("WWW-Authenticate", "Basic realm=\"foo\"");72req.getResponseHeaders().add("WWW-Authenticate", "Foo realm=\"bar\"");73req.sendResponseHeaders(401, -1);74break;75case 2:76req.getResponseHeaders().set("Connection", "close");77req.getResponseHeaders().set("WWW-Authenticate", "Basic realm=\"foobar\" Foo realm=\"bar\"");78req.sendResponseHeaders(401, -1);79break;80case 4:81req.getResponseHeaders().set("Connection", "close");82req.getResponseHeaders().set("WWW-Authenticate", "Digest realm=biz domain=/foo nonce=thisisanonce ");83req.getResponseHeaders().add("WWW-Authenticate", "Basic realm=bizbar");84req.sendResponseHeaders(401, -1);85break;86case 6:87req.getResponseHeaders().set("Connection", "close");88req.getResponseHeaders().set("WWW-Authenticate", "Digest realm=\"bizbar\" domain=/biz nonce=\"hereisanonce\" Basic realm=\"foobar\" Foo realm=\"bar\"");89req.sendResponseHeaders(401, -1);90break;91case 8:92req.getResponseHeaders().set("Connection", "close");93req.getResponseHeaders().set("WWW-Authenticate", "Foo p1=1 p2=2 p3=3 p4=4 p5=5 p6=6 p7=7 p8=8 p9=10 Digest realm=foobiz domain=/foobiz nonce=newnonce");94req.getResponseHeaders().add("WWW-Authenticate", "Basic realm=bizbar");95req.sendResponseHeaders(401, -1);96break;97}98}99count ++;100} catch (IOException e) {101e.printStackTrace();102}103}104105static void read(InputStream is) throws IOException {106int c;107System.out.println("reading");108while ((c=is.read()) != -1) {109System.out.write(c);110}111System.out.println("");112System.out.println("finished reading");113}114115116static void client(String u) throws Exception {117URL url = new URL (u);118System.out.println("client opening connection to: " + u);119URLConnection urlc = url.openConnection ();120InputStream is = urlc.getInputStream ();121read(is);122is.close();123}124125static HttpServer server;126127public static void main(String[] args) throws Exception {128B4722333 b4722333 = new B4722333();129MyAuthenticator auth = new MyAuthenticator();130Authenticator.setDefault(auth);131try {132InetAddress loopback = InetAddress.getLoopbackAddress();133server = HttpServer.create(new InetSocketAddress(loopback, 0), 10);134server.createContext("/", b4722333);135server.setExecutor(Executors.newSingleThreadExecutor());136server.start();137System.out.println("Server started: listening on port: " + server.getAddress().getPort());138String serverURL = URIBuilder.newBuilder()139.scheme("http")140.loopback()141.port(server.getAddress().getPort())142.path("/")143.build()144.toString();145client(serverURL + "d1/d2/d3/foo.html");146client(serverURL + "ASD/d3/x.html");147client(serverURL + "biz/d3/x.html");148client(serverURL + "bar/d3/x.html");149client(serverURL + "fuzz/d3/x.html");150} catch (Exception e) {151if (server != null) {152server.stop(1);153}154throw e;155}156int f = auth.getCount();157if (f != expected.length) {158except("Authenticator was called "+f+" times. Should be " + expected.length);159}160server.stop(1);161}162163public static void except(String s) {164server.stop(1);165throw new RuntimeException(s);166}167168static class MyAuthenticator extends Authenticator {169MyAuthenticator() {170super();171}172173int count = 0;174175public PasswordAuthentication getPasswordAuthentication() {176System.out.println("Auth called");177String scheme = getRequestingScheme();178System.out.println("getRequestingScheme() returns " + scheme);179String prompt = getRequestingPrompt();180System.out.println("getRequestingPrompt() returns " + prompt);181182if (!scheme.equals(expected [count][0])) {183B4722333.except("wrong scheme received, " + scheme + " expected " + expected [count][0]);184}185if (!prompt.equals(expected [count][1])) {186B4722333.except("wrong realm received, " + prompt + " expected " + expected [count][1]);187}188count ++;189return (new PasswordAuthentication("user", "passwordNotCheckedAnyway".toCharArray()));190}191192public int getCount () {193return count;194}195}196197}198199200