Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/Authenticator/B4722333.java
38812 views
/*1* Copyright (c) 2002, 2012, 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 ../../../sun/net/www/httptest/27* @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction28* @run main B472233329* @summary JRE Proxy Authentication Not Working with ISA200030*/3132import java.io.*;33import java.net.*;3435public class B4722333 implements HttpCallback {3637static int count = 0;3839static String [][] expected = {40/* scheme realm/prompt */41{"basic", "foo"},42{"basic", "foobar"},43{"digest", "biz"},44{"digest", "bizbar"},45{"digest", "foobiz"}46};4748public void request (HttpTransaction req) {49try {50if (count % 2 == 1 ) {51req.setResponseEntityBody ("Hello .");52req.sendResponse (200, "Ok");53req.orderlyClose();54} else {55switch (count) {56case 0:57req.addResponseHeader ("Connection", "close");58req.addResponseHeader ("WWW-Authenticate", "Basic realm=\"foo\"");59req.addResponseHeader ("WWW-Authenticate", "Foo realm=\"bar\"");60req.sendResponse (401, "Unauthorized");61req.orderlyClose();62break;63case 2:64req.addResponseHeader ("Connection", "close");65req.addResponseHeader ("WWW-Authenticate", "Basic realm=\"foobar\" Foo realm=\"bar\"");66req.sendResponse (401, "Unauthorized");67break;68case 4:69req.addResponseHeader ("Connection", "close");70req.addResponseHeader ("WWW-Authenticate", "Digest realm=biz domain=/foo nonce=thisisanonce ");71req.addResponseHeader ("WWW-Authenticate", "Basic realm=bizbar");72req.sendResponse (401, "Unauthorized");73req.orderlyClose();74break;75case 6:76req.addResponseHeader ("Connection", "close");77req.addResponseHeader ("WWW-Authenticate", "Digest realm=\"bizbar\" domain=/biz nonce=\"hereisanonce\" Basic realm=\"foobar\" Foo realm=\"bar\"");78req.sendResponse (401, "Unauthorized");79req.orderlyClose();80break;81case 8:82req.addResponseHeader ("Connection", "close");83req.addResponseHeader ("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");84req.addResponseHeader ("WWW-Authenticate", "Basic realm=bizbar");85req.sendResponse (401, "Unauthorized");86req.orderlyClose();87break;88}89}90count ++;91} catch (IOException e) {92e.printStackTrace();93}94}9596static void read (InputStream is) throws IOException {97int c;98System.out.println ("reading");99while ((c=is.read()) != -1) {100System.out.write (c);101}102System.out.println ("");103System.out.println ("finished reading");104}105106107static void client (String u) throws Exception {108URL url = new URL (u);109System.out.println ("client opening connection to: " + u);110URLConnection urlc = url.openConnection ();111InputStream is = urlc.getInputStream ();112read (is);113is.close();114}115116static TestHttpServer server;117118public static void main (String[] args) throws Exception {119MyAuthenticator auth = new MyAuthenticator ();120Authenticator.setDefault (auth);121try {122server = new TestHttpServer (new B4722333(), 1, 10, 0);123System.out.println ("Server started: listening on port: " + server.getLocalPort());124client ("http://localhost:"+server.getLocalPort()+"/d1/d2/d3/foo.html");125client ("http://localhost:"+server.getLocalPort()+"/ASD/d3/x.html");126client ("http://localhost:"+server.getLocalPort()+"/biz/d3/x.html");127client ("http://localhost:"+server.getLocalPort()+"/bar/d3/x.html");128client ("http://localhost:"+server.getLocalPort()+"/fuzz/d3/x.html");129} catch (Exception e) {130if (server != null) {131server.terminate();132}133throw e;134}135int f = auth.getCount();136if (f != expected.length) {137except ("Authenticator was called "+f+" times. Should be " + expected.length);138}139server.terminate();140}141142public static void except (String s) {143server.terminate();144throw new RuntimeException (s);145}146147static class MyAuthenticator extends Authenticator {148MyAuthenticator () {149super ();150}151152int count = 0;153154public PasswordAuthentication getPasswordAuthentication ()155{156System.out.println ("Auth called");157String scheme = getRequestingScheme();158System.out.println ("getRequestingScheme() returns " + scheme);159String prompt = getRequestingPrompt();160System.out.println ("getRequestingPrompt() returns " + prompt);161162if (!scheme.equals (expected [count][0])) {163B4722333.except ("wrong scheme received, " + scheme + " expected " + expected [count][0]);164}165if (!prompt.equals (expected [count][1])) {166B4722333.except ("wrong realm received, " + prompt + " expected " + expected [count][1]);167}168count ++;169return (new PasswordAuthentication ("user", "passwordNotCheckedAnyway".toCharArray()));170}171172public int getCount () {173return (count);174}175}176177}178179180