Path: blob/master/test/jdk/java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java
41152 views
/*1* Copyright (c) 2016, 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*/2223import java.io.IOException;24import java.net.Authenticator;25import java.net.HttpURLConnection;26import java.net.Proxy;27import java.net.URL;28import java.util.Arrays;29import java.util.stream.Collectors;30import java.util.stream.Stream;3132/*33* @test34* @bug 816941535* @library /test/lib36* @modules java.logging37* java.base/sun.net.www38* java.base/sun.net.www.protocol.http39* jdk.httpserver/sun.net.httpserver40* @build jdk.test.lib.net.SimpleSSLContext HTTPTest HTTPTestServer HTTPTestClient HTTPSetAuthenticatorTest41* @summary A simple HTTP test that starts an echo server supporting the given42* authentication scheme, then starts a regular HTTP client to invoke it.43* The client first does a GET request on "/", then follows on44* with a POST request that sends "Hello World!" to the server.45* The client expects to receive "Hello World!" in return.46* The test supports several execution modes:47* SERVER: The server performs Server authentication;48* PROXY: The server pretends to be a proxy and performs49* Proxy authentication;50* SERVER307: The server redirects the client (307) to another51* server that perform Server authentication;52* PROXY305: The server attempts to redirect53* the client to a proxy using 305 code;54* This test runs the client several times, providing different55* authenticators to the HttpURLConnection and verifies that56* the authenticator is invoked as expected - validating that57* connections with different authenticators do not share each58* other's socket channel and authentication info.59* Note: BASICSERVER means that the server will let the underlying60* com.sun.net.httpserver.HttpServer perform BASIC61* authentication when in Server mode. There should be62* no real difference between BASICSERVER and BASIC - it should63* be transparent on the client side.64* @run main/othervm HTTPSetAuthenticatorTest NONE SERVER PROXY SERVER307 PROXY30565* @run main/othervm HTTPSetAuthenticatorTest DIGEST SERVER66* @run main/othervm HTTPSetAuthenticatorTest DIGEST PROXY67* @run main/othervm HTTPSetAuthenticatorTest DIGEST PROXY30568* @run main/othervm HTTPSetAuthenticatorTest DIGEST SERVER30769* @run main/othervm HTTPSetAuthenticatorTest BASIC SERVER70* @run main/othervm HTTPSetAuthenticatorTest BASIC PROXY71* @run main/othervm HTTPSetAuthenticatorTest BASIC PROXY30572* @run main/othervm HTTPSetAuthenticatorTest BASIC SERVER30773* @run main/othervm HTTPSetAuthenticatorTest BASICSERVER SERVER74* @run main/othervm HTTPSetAuthenticatorTest BASICSERVER SERVER30775*76* @author danielfuchs77*/78public class HTTPSetAuthenticatorTest extends HTTPTest {7980public static void main(String[] args) throws Exception {81String[] schemes;82String[] params;83if (args == null || args.length == 0) {84schemes = Stream.of(HttpSchemeType.values())85.map(HttpSchemeType::name)86.collect(Collectors.toList())87.toArray(new String[0]);88params = new String[0];89} else {90schemes = new String[] { args[0] };91params = Arrays.copyOfRange(args, 1, args.length);92}93for (String scheme : schemes) {94System.out.println("==== Testing with scheme=" + scheme + " ====\n");95new HTTPSetAuthenticatorTest(HttpSchemeType.valueOf(scheme))96.execute(params);97System.out.println();98}99}100101final HttpSchemeType scheme;102public HTTPSetAuthenticatorTest(HttpSchemeType scheme) {103this.scheme = scheme;104}105106@Override107public HttpSchemeType getHttpSchemeType() {108return scheme;109}110111@Override112public int run(HTTPTestServer server,113HttpProtocolType protocol,114HttpAuthType mode)115throws IOException116{117HttpTestAuthenticator authOne = new HttpTestAuthenticator("authOne", "dublin", "foox");118HttpTestAuthenticator authTwo = new HttpTestAuthenticator("authTwo", "dublin", "foox");119int expectedIncrement = scheme == HttpSchemeType.NONE120? 0 : EXPECTED_AUTH_CALLS_PER_TEST;121int count;122int defaultCount = AUTHENTICATOR.count.get();123124// Connect to the server with a GET request, then with a125// POST that contains "Hello World!"126// Uses authenticator #1127System.out.println("\nClient: Using authenticator #1: "128+ toString(authOne));129HTTPTestClient.connect(protocol, server, mode, authOne);130count = authOne.count.get();131if (count != expectedIncrement) {132throw new AssertionError("Authenticator #1 called " + count(count)133+ " expected it to be called " + expected(expectedIncrement));134}135136// Connect to the server with a GET request, then with a137// POST that contains "Hello World!"138// Uses authenticator #2139System.out.println("\nClient: Using authenticator #2: "140+ toString(authTwo));141HTTPTestClient.connect(protocol, server, mode, authTwo);142count = authTwo.count.get();143if (count != expectedIncrement) {144throw new AssertionError("Authenticator #2 called " + count(count)145+ " expected it to be called " + expected(expectedIncrement));146}147148// Connect to the server with a GET request, then with a149// POST that contains "Hello World!"150// Uses authenticator #1151System.out.println("\nClient: Using authenticator #1 again: "152+ toString(authOne));153HTTPTestClient.connect(protocol, server, mode, authOne);154count = authOne.count.get();155if (count != expectedIncrement) {156throw new AssertionError("Authenticator #1 called " + count(count)157+ " expected it to be called " + expected(expectedIncrement));158}159count = authTwo.count.get();160if (count != expectedIncrement) {161throw new AssertionError("Authenticator #2 called " + count(count)162+ " expected it to be called " + expected(expectedIncrement));163}164count = AUTHENTICATOR.count.get();165if (count != defaultCount) {166throw new AssertionError("Default Authenticator called " + count(count)167+ " expected it to be called " + expected(defaultCount));168}169170// Now tries with the default authenticator: it should be invoked.171System.out.println("\nClient: Using the default authenticator: "172+ toString(null));173HTTPTestClient.connect(protocol, server, mode, null);174count = authOne.count.get();175if (count != expectedIncrement) {176throw new AssertionError("Authenticator #1 called " + count(count)177+ " expected it to be called " + expected(expectedIncrement));178}179count = authTwo.count.get();180if (count != expectedIncrement) {181throw new AssertionError("Authenticator #2 called " + count(count)182+ " expected it to be called " + expected(expectedIncrement));183}184count = AUTHENTICATOR.count.get();185if (count != defaultCount + expectedIncrement) {186throw new AssertionError("Default Authenticator called " + count(count)187+ " expected it to be called " + expected(defaultCount + expectedIncrement));188}189190// Now tries with explicitly setting the default authenticator: it should191// be invoked again.192// Uncomment the code below when 8169068 is available.193// System.out.println("\nClient: Explicitly setting the default authenticator: "194// + toString(Authenticator.getDefault()));195// HTTPTestClient.connect(protocol, server, mode, Authenticator.getDefault());196// count = authOne.count.get();197// if (count != expectedIncrement) {198// throw new AssertionError("Authenticator #1 called " + count(count)199// + " expected it to be called " + expected(expectedIncrement));200// }201// count = authTwo.count.get();202// if (count != expectedIncrement) {203// throw new AssertionError("Authenticator #2 called " + count(count)204// + " expected it to be called " + expected(expectedIncrement));205// }206// count = AUTHENTICATOR.count.get();207// if (count != defaultCount + 2 * expectedIncrement) {208// throw new AssertionError("Default Authenticator called " + count(count)209// + " expected it to be called "210// + expected(defaultCount + 2 * expectedIncrement));211// }212213// Now tries to set an authenticator on a connected connection.214URL url = url(protocol, server.getAddress(), "/");215Proxy proxy = proxy(server, mode);216HttpURLConnection conn = openConnection(url, mode, proxy);217try {218conn.setAuthenticator(null);219throw new RuntimeException("Expected NullPointerException"220+ " trying to set a null authenticator"221+ " not raised.");222} catch (NullPointerException npe) {223System.out.println("Client: caught expected NPE"224+ " trying to set a null authenticator: "225+ npe);226}227conn.connect();228try {229try {230conn.setAuthenticator(authOne);231throw new RuntimeException("Expected IllegalStateException"232+ " trying to set an authenticator after connect"233+ " not raised.");234} catch (IllegalStateException ise) {235System.out.println("Client: caught expected ISE"236+ " trying to set an authenticator after connect: "237+ ise);238}239// Uncomment the code below when 8169068 is available.240// try {241// conn.setAuthenticator(Authenticator.getDefault());242// throw new RuntimeException("Expected IllegalStateException"243// + " trying to set an authenticator after connect"244// + " not raised.");245// } catch (IllegalStateException ise) {246// System.out.println("Client: caught expected ISE"247// + " trying to set an authenticator after connect: "248// + ise);249// }250try {251conn.setAuthenticator(null);252throw new RuntimeException("Expected"253+ " IllegalStateException or NullPointerException"254+ " trying to set a null authenticator after connect"255+ " not raised.");256} catch (IllegalStateException | NullPointerException xxe) {257System.out.println("Client: caught expected "258+ xxe.getClass().getSimpleName()259+ " trying to set a null authenticator after connect: "260+ xxe);261}262} finally {263conn.disconnect();264}265266// double check that authOne and authTwo haven't been invoked.267count = authOne.count.get();268if (count != expectedIncrement) {269throw new AssertionError("Authenticator #1 called " + count(count)270+ " expected it to be called " + expected(expectedIncrement));271}272count = authTwo.count.get();273if (count != expectedIncrement) {274throw new AssertionError("Authenticator #2 called " + count(count)275+ " expected it to be called " + expected(expectedIncrement));276}277278// All good!279// return the number of times the default authenticator is supposed280// to have been called.281return scheme == HttpSchemeType.NONE ? 0 : 1 * EXPECTED_AUTH_CALLS_PER_TEST;282}283284static String toString(Authenticator a) {285return sun.net.www.protocol.http.AuthenticatorKeys.getKey(a);286}287288}289290291