Path: blob/master/test/jdk/java/net/Authenticator/BasicTest.java
41149 views
/*1* Copyright (c) 2001, 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 447494731* @summary fix for bug #4244472 is incomplete - HTTP authorization still needs work32* @library /test/lib33* @run main/othervm BasicTest34* @run main/othervm -Djava.net.preferIPv6Addresses=true BasicTest35*/3637/*38* Note, this is not a general purpose test for Basic Authentication because39* it does not check the correctness of the data, only whether the user40* authenticator gets called once as expected41*/4243public class BasicTest {4445static class BasicServer extends Thread {4647ServerSocket server;4849Socket s;50InputStream is;51OutputStream os;5253static final String realm = "wallyworld";5455String reply1 = "HTTP/1.1 401 Unauthorized\r\n"+56"WWW-Authenticate: Basic realm=\""+realm+"\"\r\n\r\n";5758String reply2 = "HTTP/1.1 200 OK\r\n"+59"Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +60"Server: Apache/1.3.14 (Unix)\r\n" +61"Connection: close\r\n" +62"Content-Type: text/html; charset=iso-8859-1\r\n" +63"Content-Length: 10\r\n\r\n";6465BasicServer (ServerSocket s) {66server = s;67}6869void readAll (Socket s) throws IOException {70byte[] buf = new byte [128];71InputStream is = s.getInputStream ();72while (is.available() > 0) {73is.read (buf);74}75}7677public void run () {78try {79System.out.println ("Server 1: accept");80s = server.accept ();81readAll (s);82System.out.println ("accepted");83os = s.getOutputStream();84os.write (reply1.getBytes());85Thread.sleep (500);8687System.out.println ("Server 2: accept");88s = server.accept ();89readAll (s);90System.out.println ("accepted");91os = s.getOutputStream();92os.write ((reply2+"HelloWorld").getBytes());9394/* Second request now */9596System.out.println ("Server 3: accept");97s = server.accept ();98readAll (s);99System.out.println ("accepted");100os = s.getOutputStream();101os.write (reply1.getBytes());102Thread.sleep (500);103s.close ();104105System.out.println ("Server 4: accept");106s = server.accept ();107readAll (s);108System.out.println ("accepted");109os = s.getOutputStream();110os.write ((reply2+"HelloAgain").getBytes());111}112catch (Exception e) {113System.out.println (e);114}115finished ();116}117118public synchronized void finished () {119notifyAll();120}121122}123124static class MyAuthenticator extends Authenticator {125MyAuthenticator () {126super ();127}128129int count = 0;130131public PasswordAuthentication getPasswordAuthentication ()132{133count ++;134System.out.println ("Auth called");135return (new PasswordAuthentication ("user", "passwordNotCheckedAnyway".toCharArray()));136}137138public int getCount () {139return (count);140}141}142143144static void read (InputStream is) throws IOException {145int c;146System.out.println ("reading");147while ((c=is.read()) != -1) {148System.out.write (c);149}150System.out.println ("");151System.out.println ("finished reading");152}153154public static void main (String args[]) throws Exception {155MyAuthenticator auth = new MyAuthenticator ();156Authenticator.setDefault (auth);157InetAddress loopback = InetAddress.getLoopbackAddress();158ServerSocket ss = new ServerSocket();159ss.bind(new InetSocketAddress(loopback, 0));160int port = ss.getLocalPort ();161BasicServer server = new BasicServer (ss);162synchronized (server) {163server.start();164System.out.println ("client 1");165String base = URIBuilder.newBuilder()166.scheme("http")167.loopback()168.port(port)169.path("/")170.build()171.toString();172URL url = new URL(base + "d1/d2/d3/foo.html");173URLConnection urlc = url.openConnection(Proxy.NO_PROXY);174InputStream is = urlc.getInputStream ();175read (is);176System.out.println ("client 2");177url = new URL(base + "d1/foo.html");178urlc = url.openConnection(Proxy.NO_PROXY);179is = urlc.getInputStream ();180read (is);181server.wait ();182// check if authenticator was called once (ok) or twice (not)183int f = auth.getCount();184if (f != 1) {185throw new RuntimeException ("Authenticator was called "+f+" times. Should be 1");186}187}188}189}190191192