Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/Authenticator/BasicTest.java
38811 views
/*1* Copyright (c) 2001, 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 447494730* @summary fix for bug #4244472 is incomplete - HTTP authorization still needs work31*/3233/*34* Note, this is not a general purpose test for Basic Authentication because35* it does not check the correctness of the data, only whether the user36* authenticator gets called once as expected37*/3839public class BasicTest {4041static class BasicServer extends Thread {4243ServerSocket server;4445Socket s;46InputStream is;47OutputStream os;4849static final String realm = "wallyworld";5051String reply1 = "HTTP/1.1 401 Unauthorized\r\n"+52"WWW-Authenticate: Basic realm=\""+realm+"\"\r\n\r\n";5354String reply2 = "HTTP/1.1 200 OK\r\n"+55"Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +56"Server: Apache/1.3.14 (Unix)\r\n" +57"Connection: close\r\n" +58"Content-Type: text/html; charset=iso-8859-1\r\n" +59"Content-Length: 10\r\n\r\n";6061BasicServer (ServerSocket s) {62server = s;63}6465void readAll (Socket s) throws IOException {66byte[] buf = new byte [128];67InputStream is = s.getInputStream ();68while (is.available() > 0) {69is.read (buf);70}71}7273public void run () {74try {75System.out.println ("Server 1: accept");76s = server.accept ();77readAll (s);78System.out.println ("accepted");79os = s.getOutputStream();80os.write (reply1.getBytes());81Thread.sleep (500);8283System.out.println ("Server 2: accept");84s = server.accept ();85readAll (s);86System.out.println ("accepted");87os = s.getOutputStream();88os.write ((reply2+"HelloWorld").getBytes());8990/* Second request now */9192System.out.println ("Server 3: accept");93s = server.accept ();94readAll (s);95System.out.println ("accepted");96os = s.getOutputStream();97os.write (reply1.getBytes());98Thread.sleep (500);99s.close ();100101System.out.println ("Server 4: accept");102s = server.accept ();103readAll (s);104System.out.println ("accepted");105os = s.getOutputStream();106os.write ((reply2+"HelloAgain").getBytes());107}108catch (Exception e) {109System.out.println (e);110}111finished ();112}113114public synchronized void finished () {115notifyAll();116}117118}119120static class MyAuthenticator extends Authenticator {121MyAuthenticator () {122super ();123}124125int count = 0;126127public PasswordAuthentication getPasswordAuthentication ()128{129count ++;130System.out.println ("Auth called");131return (new PasswordAuthentication ("user", "passwordNotCheckedAnyway".toCharArray()));132}133134public int getCount () {135return (count);136}137}138139140static void read (InputStream is) throws IOException {141int c;142System.out.println ("reading");143while ((c=is.read()) != -1) {144System.out.write (c);145}146System.out.println ("");147System.out.println ("finished reading");148}149150public static void main (String args[]) throws Exception {151MyAuthenticator auth = new MyAuthenticator ();152Authenticator.setDefault (auth);153ServerSocket ss = new ServerSocket (0);154int port = ss.getLocalPort ();155BasicServer server = new BasicServer (ss);156synchronized (server) {157server.start();158System.out.println ("client 1");159URL url = new URL ("http://localhost:"+port+"/d1/d2/d3/foo.html");160URLConnection urlc = url.openConnection ();161InputStream is = urlc.getInputStream ();162read (is);163System.out.println ("client 2");164url = new URL ("http://localhost:"+port+"/d1/foo.html");165urlc = url.openConnection ();166is = urlc.getInputStream ();167read (is);168server.wait ();169// check if authenticator was called once (ok) or twice (not)170int f = auth.getCount();171if (f != 1) {172throw new RuntimeException ("Authenticator was called "+f+" times. Should be 1");173}174}175}176}177178179