Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/http/TestTransparentNTLM.java
38867 views
/*1* Copyright (c) 2018, 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*/2223/*24* @test25* @bug 822542526* @summary Verifies that transparent NTLM (on Windows) is not used by default,27* and is used only when the relevant property is set.28* @requires os.family == "windows"29* @library ../../../../../lib/testlibrary30* @run testng/othervm31* -Dtest.auth.succeed=false32* TestTransparentNTLM33* @run testng/othervm34* -Djdk.http.ntlm.transparentAuth=allHosts35* -Dtest.auth.succeed=true36* TestTransparentNTLM37* @run testng/othervm38* -Djdk.http.ntlm.transparentAuth=blahblah39* -Dtest.auth.succeed=false40* TestTransparentNTLM41* @run testng/othervm42* -Djdk.http.ntlm.transparentAuth=trustedHosts43* -Dtest.auth.succeed=false44* TestTransparentNTLM45*/4647// Run with `trustedHosts` to exercise the native code, nothing more.4849import java.io.BufferedReader;50import java.io.Closeable;51import java.io.IOException;52import java.io.InputStream;53import java.io.InputStreamReader;54import java.net.HttpURLConnection;55import java.net.InetAddress;56import java.net.InetSocketAddress;57import java.net.ServerSocket;58import java.net.Socket;59import java.net.URL;60import jdk.testlibrary.net.URIBuilder;61import org.testng.annotations.AfterTest;62import org.testng.annotations.BeforeTest;63import org.testng.annotations.Test;64import org.testng.SkipException;65import static java.lang.System.out;66import static java.net.Proxy.NO_PROXY;67import static java.nio.charset.StandardCharsets.UTF_8;68import static org.testng.Assert.assertEquals;69import static org.testng.Assert.fail;7071public class TestTransparentNTLM {7273boolean succeed; // true if authentication is expected to succeed74Server server;75URL url;7677@Test78public void testNTLM() throws IOException {79out.println("connecting to url: " + url);80HttpURLConnection uc = (HttpURLConnection)url.openConnection(NO_PROXY);81int respCode = uc.getResponseCode();82out.println("received: " + respCode);8384if (succeed) {85assertEquals(respCode, HttpURLConnection.HTTP_OK);86InputStream is = uc.getInputStream();87BufferedReader r = new BufferedReader(new InputStreamReader(is, UTF_8));88String body = r.readLine();89out.print("received body: ");90do {91out.print(body);92body = r.readLine();93} while (body != null);94} else {95assertEquals(respCode, HttpURLConnection.HTTP_UNAUTHORIZED);96}97}9899static class Server extends Thread implements Closeable {100101static final InetAddress LOOPBACK = InetAddress.getLoopbackAddress();102final ServerSocket serverSocket;103final boolean expectAuthToSucceed;104105Server(boolean expectAuthToSucceed) throws IOException {106super("TestTransparentNTLM-Server");107serverSocket = new ServerSocket();108serverSocket.bind(new InetSocketAddress(LOOPBACK, 0));109this.expectAuthToSucceed = expectAuthToSucceed;110}111112int port() {113return serverSocket.getLocalPort();114}115116static final String AUTH_REQUIRED =117"HTTP/1.1 401 Unauthorized\r\n" +118"Content-Length: 0\r\n" +119"Connection: close\r\n" +120"WWW-Authenticate: NTLM\r\n\r\n";121122static final String AUTH_STAGE_TWO =123"HTTP/1.1 401 Unauthorized\r\n" +124"Content-Length: 0\r\n" +125"WWW-Authenticate: NTLM TlRMTVNTUAACAAAAAAAAACgAAAABggAAU3J2Tm9uY2UAAAAAAAAAAA==\r\n\r\n";126127static final String AUTH_SUCCESSFUL =128"HTTP/1.1 200 OK\r\n" +129"Content-Length: 11\r\n\r\n" +130"Hello world";131132@Override133public void run() {134try {135try (Socket s = serverSocket.accept()) {136out.println("Server accepted connection - 1");137readRequestHeaders(s.getInputStream());138s.getOutputStream().write(AUTH_REQUIRED.getBytes(UTF_8));139}140141if (expectAuthToSucceed) {142// await the second follow up connection143try (Socket s = serverSocket.accept()) {144out.println("Server accepted connection - 2");145readRequestHeaders(s.getInputStream());146s.getOutputStream().write(AUTH_STAGE_TWO.getBytes(UTF_8));147readRequestHeaders(s.getInputStream());148s.getOutputStream().write(AUTH_SUCCESSFUL.getBytes(UTF_8));149}150}151} catch (IOException e) {152fail("Unexpected exception", e);153}154}155156@Override157public void close() throws IOException {158serverSocket.close();159}160161static final byte[] REQUEST_END = new byte[] {'\r', '\n', '\r', '\n'};162163// Read until the end of the HTTP request headers164static void readRequestHeaders(InputStream is) throws IOException {165int requestEndCount = 0, r;166while ((r = is.read()) != -1) {167if (r == REQUEST_END[requestEndCount]) {168requestEndCount++;169if (requestEndCount == 4) {170break;171}172} else {173requestEndCount = 0;174}175}176}177}178179@BeforeTest180public void setup() throws Exception {181succeed = System.getProperty("test.auth.succeed").equals("true");182if (succeed)183out.println("Expect client to succeed, with 200 Ok");184else185out.println("Expect client to fail, with 401 Unauthorized");186187server = new Server(succeed);188server.start();189url = URIBuilder.newBuilder()190.scheme("http")191.loopback()192.port(server.port())193.path("/xxyyzz")194.toURL();195}196197@AfterTest198public void teardown() throws Exception {199server.close();200server.join();201}202}203204205