Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/Authenticator/BasicTest4.java
38811 views
/*1* Copyright (c) 2002, 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.*;2627/**28* @test29* @bug 462372230* @summary performance hit for Basic Authentication31*/3233public class BasicTest4 {3435static class BasicServer extends Thread {3637ServerSocket server;3839Socket s;40InputStream is;41OutputStream os;4243static final String realm = "wallyworld";4445String reply1 = "HTTP/1.1 401 Unauthorized\r\n"+46"WWW-Authenticate: Basic realm=\""+realm+"\"\r\n\r\n";4748String reply2 = "HTTP/1.1 200 OK\r\n"+49"Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +50"Server: Apache/1.3.14 (Unix)\r\n" +51"Connection: close\r\n" +52"Content-Type: text/html; charset=iso-8859-1\r\n" +53"Content-Length: 10\r\n\r\n";5455BasicServer (ServerSocket s) {56server = s;57}5859static boolean checkFor (InputStream in, char[] seq) throws IOException {60System.out.println ("checkfor");61try {62int i=0, count=0;63while (true) {64int c = in.read();65if (c == -1)66return false;67count++;68if (c == seq[i]) {69i++;70if (i == seq.length)71return true;72continue;73} else {74i = 0;75}76}77}78catch (SocketTimeoutException e) {79return false;80}81}8283boolean success = false;8485void readAll (Socket s) throws IOException {86byte[] buf = new byte [128];87InputStream is = s.getInputStream ();88s.setSoTimeout(1000);89try {90while (is.read(buf) > 0) ;91} catch (SocketTimeoutException x) { }92}9394public void run () {95try {96System.out.println ("Server 1: accept");97s = server.accept ();98readAll (s);99System.out.println ("accepted");100os = s.getOutputStream();101os.write (reply1.getBytes());102s.close ();103104System.out.println ("Server 2: accept");105s = server.accept ();106readAll (s);107System.out.println ("accepted");108os = s.getOutputStream();109os.write ((reply2+"HelloWorld").getBytes());110s.close ();111112/* Second request now */113114System.out.println ("Server 3: accept");115s = server.accept ();116readAll (s);117System.out.println ("accepted");118os = s.getOutputStream();119os.write (reply1.getBytes());120s.close ();121122System.out.println ("Server 4: accept");123s = server.accept ();124readAll (s);125System.out.println ("accepted");126os = s.getOutputStream();127os.write ((reply2+"HelloAgain").getBytes());128s.close ();129130/* Third request now */131132/* This should include pre-emptive authorization */133134System.out.println ("Server 5: accept");135s = server.accept ();136s.setSoTimeout (1000);137System.out.println ("accepted");138InputStream is = s.getInputStream ();139success = checkFor (is, "Authorization".toCharArray());140System.out.println ("checkfor returned " + success);141readAll (s);142os = s.getOutputStream();143os.write (reply2.getBytes());144s.close ();145146if (success)147return;148149System.out.println ("Server 6: accept");150s = server.accept ();151System.out.println ("accepted");152os = s.getOutputStream();153readAll (s);154os.write ((reply2+"HelloAgain").getBytes());155s.close ();156}157catch (Exception e) {158System.out.println (e);159}160finished ();161}162163public synchronized void finished () {164notifyAll();165}166167}168169static class MyAuthenticator extends Authenticator {170MyAuthenticator () {171super ();172}173174public PasswordAuthentication getPasswordAuthentication ()175{176System.out.println ("Auth called");177return (new PasswordAuthentication ("user", "passwordNotCheckedAnyway".toCharArray()));178}179180}181182183static void read (InputStream is) throws IOException {184int c;185System.out.println ("reading");186while ((c=is.read()) != -1) {187System.out.write (c);188}189System.out.println ("");190System.out.println ("finished reading");191}192193public static void main (String args[]) throws Exception {194MyAuthenticator auth = new MyAuthenticator ();195Authenticator.setDefault (auth);196ServerSocket ss = new ServerSocket (0);197int port = ss.getLocalPort ();198BasicServer server = new BasicServer (ss);199synchronized (server) {200server.start();201System.out.println ("client 1");202URL url = new URL ("http://localhost:"+port+"/d1/d3/foo.html");203URLConnection urlc = url.openConnection ();204InputStream is = urlc.getInputStream ();205read (is);206System.out.println ("client 2");207url = new URL ("http://localhost:"+port+"/d1/d2/bar.html");208urlc = url.openConnection ();209is = urlc.getInputStream ();210System.out.println ("client 3");211url = new URL ("http://localhost:"+port+"/d1/d4/foobar.html");212urlc = url.openConnection ();213is = urlc.getInputStream ();214read (is);215server.wait ();216if (!server.success) {217throw new RuntimeException ("3rd request did not use pre-emptive authorization");218}219}220}221}222223224