Path: blob/master/test/jdk/sun/net/www/protocol/http/TestTransparentNTLM.java
41159 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 /test/lib30* @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.Closeable;50import java.io.IOException;51import java.io.InputStream;52import java.net.HttpURLConnection;53import java.net.InetAddress;54import java.net.InetSocketAddress;55import java.net.ServerSocket;56import java.net.Socket;57import java.net.URL;58import jdk.test.lib.net.URIBuilder;59import org.testng.annotations.AfterTest;60import org.testng.annotations.BeforeTest;61import org.testng.annotations.Test;62import org.testng.SkipException;63import static java.lang.System.out;64import static java.net.Proxy.NO_PROXY;65import static java.nio.charset.StandardCharsets.UTF_8;66import static org.testng.Assert.assertEquals;67import static org.testng.Assert.fail;6869public class TestTransparentNTLM {7071boolean succeed; // true if authentication is expected to succeed72Server server;73URL url;7475@Test76public void testNTLM() throws IOException {77out.println("connecting to url: " + url);78HttpURLConnection uc = (HttpURLConnection)url.openConnection(NO_PROXY);79int respCode = uc.getResponseCode();80out.println("received: " + respCode);8182if (succeed) {83assertEquals(respCode, HttpURLConnection.HTTP_OK);84String body = new String(uc.getInputStream().readAllBytes(), UTF_8);85out.println("received body: " + body);86} else {87assertEquals(respCode, HttpURLConnection.HTTP_UNAUTHORIZED);88}89}9091static class Server extends Thread implements Closeable {9293static final InetAddress LOOPBACK = InetAddress.getLoopbackAddress();94final ServerSocket serverSocket;95final boolean expectAuthToSucceed;9697Server(boolean expectAuthToSucceed) throws IOException {98super("TestTransparentNTLM-Server");99serverSocket = new ServerSocket();100serverSocket.bind(new InetSocketAddress(LOOPBACK, 0));101this.expectAuthToSucceed = expectAuthToSucceed;102}103104int port() {105return serverSocket.getLocalPort();106}107108static final String AUTH_REQUIRED =109"HTTP/1.1 401 Unauthorized\r\n" +110"Content-Length: 0\r\n" +111"Connection: close\r\n" +112"WWW-Authenticate: NTLM\r\n\r\n";113114static final String AUTH_STAGE_TWO =115"HTTP/1.1 401 Unauthorized\r\n" +116"Content-Length: 0\r\n" +117"WWW-Authenticate: NTLM TlRMTVNTUAACAAAAAAAAACgAAAABggAAU3J2Tm9uY2UAAAAAAAAAAA==\r\n\r\n";118119static final String AUTH_SUCCESSFUL =120"HTTP/1.1 200 OK\r\n" +121"Content-Length: 11\r\n\r\n" +122"Hello world";123124@Override125public void run() {126try {127try (Socket s = serverSocket.accept()) {128out.println("Server accepted connection - 1");129readRequestHeaders(s.getInputStream());130s.getOutputStream().write(AUTH_REQUIRED.getBytes(UTF_8));131}132133if (expectAuthToSucceed) {134// await the second follow up connection135try (Socket s = serverSocket.accept()) {136out.println("Server accepted connection - 2");137readRequestHeaders(s.getInputStream());138s.getOutputStream().write(AUTH_STAGE_TWO.getBytes(UTF_8));139readRequestHeaders(s.getInputStream());140s.getOutputStream().write(AUTH_SUCCESSFUL.getBytes(UTF_8));141}142}143} catch (IOException e) {144fail("Unexpected exception", e);145}146}147148@Override149public void close() throws IOException {150serverSocket.close();151}152153static final byte[] REQUEST_END = new byte[] {'\r', '\n', '\r', '\n'};154155// Read until the end of the HTTP request headers156static void readRequestHeaders(InputStream is) throws IOException {157int requestEndCount = 0, r;158while ((r = is.read()) != -1) {159if (r == REQUEST_END[requestEndCount]) {160requestEndCount++;161if (requestEndCount == 4) {162break;163}164} else {165requestEndCount = 0;166}167}168}169}170171@BeforeTest172public void setup() throws Exception {173succeed = System.getProperty("test.auth.succeed").equals("true");174if (succeed)175out.println("Expect client to succeed, with 200 Ok");176else177out.println("Expect client to fail, with 401 Unauthorized");178179server = new Server(succeed);180server.start();181url = URIBuilder.newBuilder()182.scheme("http")183.loopback()184.port(server.port())185.path("/xxyyzz")186.toURL();187}188189@AfterTest190public void teardown() throws Exception {191server.close();192server.join();193}194}195196197