Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/Authenticator/BasicTest3.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 451344030* @summary BasicAuthentication is zeroing out the given password31*/3233public class BasicTest3 {3435static class BasicServer3 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";5455BasicServer3 (ServerSocket s) {56server = s;57}5859void readAll (Socket s) throws IOException {60byte[] buf = new byte [128];61InputStream is = s.getInputStream ();62s.setSoTimeout(1000);63try {64while (is.read(buf) > 0) ;65} catch (SocketTimeoutException x) { }66}6768public void run () {69try {70System.out.println ("Server 1: accept");71s = server.accept ();72System.out.println ("accepted");73os = s.getOutputStream();74os.write (reply1.getBytes());75readAll (s);76s.close ();7778System.out.println ("Server 2: accept");79s = server.accept ();80System.out.println ("accepted");81os = s.getOutputStream();82readAll (s);83os.write ((reply2+"HelloWorld").getBytes());8485}86catch (Exception e) {87System.out.println (e);88}89finished ();90}9192public synchronized void finished () {93notifyAll();94}9596}9798static class MyAuthenticator3 extends Authenticator {99PasswordAuthentication pw;100MyAuthenticator3 () {101super ();102pw = new PasswordAuthentication ("user", "passwordNotCheckedAnyway".toCharArray());103}104105public PasswordAuthentication getPasswordAuthentication ()106{107System.out.println ("Auth called");108return pw;109}110111public void checkPW () {112if (!new String (pw.getPassword()).equals ("passwordNotCheckedAnyway")) {113throw new RuntimeException ("Password was \"" + new String (pw.getPassword()) + "\"");114}115}116}117118119static void read (InputStream is) throws IOException {120int c;121System.out.println ("reading");122while ((c=is.read()) != -1) {123System.out.write (c);124}125System.out.println ("");126System.out.println ("finished reading");127}128129public static void main (String args[]) throws Exception {130MyAuthenticator3 auth = new MyAuthenticator3 ();131Authenticator.setDefault (auth);132ServerSocket ss = new ServerSocket (0);133int port = ss.getLocalPort ();134BasicServer3 server = new BasicServer3 (ss);135synchronized (server) {136server.start();137System.out.println ("client 1");138URL url = new URL ("http://localhost:"+port+"/d1/d2/d3/foo.html");139URLConnection urlc = url.openConnection ();140InputStream is = urlc.getInputStream ();141read (is);142is.close ();143auth.checkPW ();144}145}146}147148149