Path: blob/master/test/jdk/java/net/Authenticator/BasicTest3.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 451344031* @summary BasicAuthentication is zeroing out the given password32* @library /test/lib33* @run main/othervm BasicTest334* @run main/othervm -Djava.net.preferIPv6Addresses=true BasicTest335*/3637public class BasicTest3 {3839static class BasicServer3 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";5859BasicServer3 (ServerSocket s) {60server = s;61}6263void readAll (Socket s) throws IOException {64byte[] buf = new byte [128];65InputStream is = s.getInputStream ();66s.setSoTimeout(1000);67try {68while (is.read(buf) > 0) ;69} catch (SocketTimeoutException x) { }70}7172public void run () {73try {74System.out.println ("Server 1: accept");75s = server.accept ();76System.out.println ("accepted");77os = s.getOutputStream();78os.write (reply1.getBytes());79readAll (s);80s.close ();8182System.out.println ("Server 2: accept");83s = server.accept ();84System.out.println ("accepted");85os = s.getOutputStream();86readAll (s);87os.write ((reply2+"HelloWorld").getBytes());8889}90catch (Exception e) {91System.out.println (e);92}93finished ();94}9596public synchronized void finished () {97notifyAll();98}99100}101102static class MyAuthenticator3 extends Authenticator {103PasswordAuthentication pw;104MyAuthenticator3 () {105super ();106pw = new PasswordAuthentication ("user", "passwordNotCheckedAnyway".toCharArray());107}108109public PasswordAuthentication getPasswordAuthentication ()110{111System.out.println ("Auth called");112return pw;113}114115public void checkPW () {116if (!new String (pw.getPassword()).equals ("passwordNotCheckedAnyway")) {117throw new RuntimeException ("Password was \"" + new String (pw.getPassword()) + "\"");118}119}120}121122123static void read (InputStream is) throws IOException {124int c;125System.out.println ("reading");126while ((c=is.read()) != -1) {127System.out.write (c);128}129System.out.println ("");130System.out.println ("finished reading");131}132133public static void main (String args[]) throws Exception {134MyAuthenticator3 auth = new MyAuthenticator3 ();135Authenticator.setDefault (auth);136InetAddress loopback = InetAddress.getLoopbackAddress();137ServerSocket ss = new ServerSocket();138ss.bind(new InetSocketAddress(loopback, 0));139int port = ss.getLocalPort ();140BasicServer3 server = new BasicServer3 (ss);141synchronized (server) {142server.start();143System.out.println ("client 1");144URL url = URIBuilder.newBuilder()145.scheme("http")146.loopback()147.port(port)148.path("/d1/d2/d3/foo.html")149.toURL();150URLConnection urlc = url.openConnection(Proxy.NO_PROXY);151InputStream is = urlc.getInputStream ();152read (is);153is.close ();154auth.checkPW ();155}156}157}158159160