Path: blob/master/test/jdk/java/net/Authenticator/BasicTest4.java
41149 views
/*1* Copyright (c) 2002, 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.*;24import java.net.*;25import java.util.*;26import jdk.test.lib.net.URIBuilder;2728/**29* @test30* @bug 462372231* @summary performance hit for Basic Authentication32* @library /test/lib33* @run main/othervm BasicTest434* @run main/othervm -Djava.net.preferIPv6Addresses=true BasicTest435*/3637public class BasicTest4 {3839static class BasicServer extends Thread {4041ServerSocket server;4243Socket s;44InputStream is;45OutputStream os;4647static final String realm = "wallyworld";4849String reply1 = "HTTP/1.1 401 Unauthorized\r\n"+50"WWW-Authenticate: Basic realm=\""+realm+"\"\r\n\r\n";5152String reply2 = "HTTP/1.1 200 OK\r\n"+53"Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +54"Server: Apache/1.3.14 (Unix)\r\n" +55"Connection: close\r\n" +56"Content-Type: text/html; charset=iso-8859-1\r\n" +57"Content-Length: 10\r\n\r\n";5859BasicServer (ServerSocket s) {60server = s;61}6263static boolean checkFor (InputStream in, char[] seq) throws IOException {64System.out.println ("checkfor");65StringBuilder message = new StringBuilder();66try {67int i=0, count=0;68while (true) {69int c = in.read();70if (c == -1) {71System.out.println(new String(seq) + " not found in \n<<"72+ message + ">>");73return false;74}75message.append((char)c);76count++;77if (c == seq[i]) {78i++;79if (i == seq.length)80return true;81continue;82} else {83i = 0;84}85}86}87catch (SocketTimeoutException e) {88System.out.println("checkFor: " + e);89return false;90}91}9293boolean success = false;9495void readAll (Socket s) throws IOException {96byte[] buf = new byte [128];97InputStream is = s.getInputStream ();98s.setSoTimeout(1000);99try {100while (is.read(buf) > 0) ;101} catch (SocketTimeoutException x) { }102}103104public void run () {105try {106System.out.println ("Server 1: accept");107s = server.accept ();108readAll (s);109System.out.println ("accepted");110os = s.getOutputStream();111os.write (reply1.getBytes());112s.close ();113114System.out.println ("Server 2: accept");115s = server.accept ();116readAll (s);117System.out.println ("accepted");118os = s.getOutputStream();119os.write ((reply2+"HelloWorld").getBytes());120s.close ();121122/* Second request now */123124System.out.println ("Server 3: accept");125s = server.accept ();126readAll (s);127System.out.println ("accepted");128os = s.getOutputStream();129os.write (reply1.getBytes());130s.close ();131132System.out.println ("Server 4: accept");133s = server.accept ();134readAll (s);135System.out.println ("accepted");136os = s.getOutputStream();137os.write ((reply2+"HelloAgain").getBytes());138s.close ();139140/* Third request now */141142/* This should include pre-emptive authorization */143144System.out.println ("Server 5: accept");145s = server.accept ();146s.setSoTimeout (1000);147System.out.println ("accepted");148InputStream is = s.getInputStream ();149success = checkFor (is, "Authorization".toCharArray());150System.out.println ("checkfor returned " + success);151readAll (s);152os = s.getOutputStream();153os.write (reply2.getBytes());154s.close ();155156if (success)157return;158159System.out.println ("Server 6: accept");160s = server.accept ();161System.out.println ("accepted");162os = s.getOutputStream();163readAll (s);164os.write ((reply2+"HelloAgain").getBytes());165s.close ();166}167catch (Exception e) {168System.out.println (e);169}170finished ();171}172173public synchronized void finished () {174notifyAll();175}176177}178179static class MyAuthenticator extends Authenticator {180MyAuthenticator () {181super ();182}183184public PasswordAuthentication getPasswordAuthentication ()185{186System.out.println ("Auth called");187return (new PasswordAuthentication ("user", "passwordNotCheckedAnyway".toCharArray()));188}189190}191192193static void read (InputStream is) throws IOException {194int c;195System.out.println ("reading");196while ((c=is.read()) != -1) {197System.out.write (c);198}199System.out.println ("");200System.out.println ("finished reading");201}202203public static void main (String args[]) throws Exception {204MyAuthenticator auth = new MyAuthenticator ();205Authenticator.setDefault (auth);206InetAddress loopback = InetAddress.getLoopbackAddress();207ServerSocket ss = new ServerSocket();208ss.bind(new InetSocketAddress(loopback, 0));209int port = ss.getLocalPort ();210BasicServer server = new BasicServer (ss);211synchronized (server) {212server.start();213System.out.println ("client 1");214String base = URIBuilder.newBuilder()215.scheme("http")216.loopback()217.port(port)218.path("/d1/")219.build()220.toString();221System.out.println("Base URL: " + base);222URL url = new URL (base + "d3/foo.html");223URLConnection urlc = url.openConnection(Proxy.NO_PROXY);224InputStream is = urlc.getInputStream ();225read (is);226System.out.println ("client 2");227url = new URL (base + "d2/bar.html");228urlc = url.openConnection(Proxy.NO_PROXY);229is = urlc.getInputStream ();230System.out.println ("client 3");231url = new URL (base + "d4/foobar.html");232urlc = url.openConnection(Proxy.NO_PROXY);233is = urlc.getInputStream ();234read (is);235server.wait ();236if (!server.success) {237throw new RuntimeException ("3rd request did not use pre-emptive authorization");238}239}240}241}242243244