0N/A/*
2362N/A * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.net.*;
0N/Aimport java.util.*;
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 4513440
0N/A * @summary BasicAuthentication is zeroing out the given password
0N/A */
0N/A
0N/Apublic class BasicTest3 {
0N/A
0N/A static class BasicServer3 extends Thread {
0N/A
0N/A ServerSocket server;
0N/A
0N/A Socket s;
0N/A InputStream is;
0N/A OutputStream os;
0N/A
0N/A static final String realm = "wallyworld";
0N/A
0N/A String reply1 = "HTTP/1.1 401 Unauthorized\r\n"+
0N/A "WWW-Authenticate: Basic realm=\""+realm+"\"\r\n\r\n";
0N/A
0N/A String reply2 = "HTTP/1.1 200 OK\r\n"+
0N/A "Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +
0N/A "Server: Apache/1.3.14 (Unix)\r\n" +
0N/A "Connection: close\r\n" +
0N/A "Content-Type: text/html; charset=iso-8859-1\r\n" +
0N/A "Content-Length: 10\r\n\r\n";
0N/A
0N/A BasicServer3 (ServerSocket s) {
0N/A server = s;
0N/A }
0N/A
0N/A void readAll (Socket s) throws IOException {
0N/A byte[] buf = new byte [128];
0N/A InputStream is = s.getInputStream ();
0N/A s.setSoTimeout(1000);
0N/A try {
0N/A while (is.read(buf) > 0) ;
0N/A } catch (SocketTimeoutException x) { }
0N/A }
0N/A
0N/A public void run () {
0N/A try {
0N/A System.out.println ("Server 1: accept");
0N/A s = server.accept ();
0N/A System.out.println ("accepted");
0N/A os = s.getOutputStream();
0N/A os.write (reply1.getBytes());
0N/A readAll (s);
0N/A s.close ();
0N/A
0N/A System.out.println ("Server 2: accept");
0N/A s = server.accept ();
0N/A System.out.println ("accepted");
0N/A os = s.getOutputStream();
0N/A readAll (s);
0N/A os.write ((reply2+"HelloWorld").getBytes());
0N/A
0N/A }
0N/A catch (Exception e) {
0N/A System.out.println (e);
0N/A }
0N/A finished ();
0N/A }
0N/A
0N/A public synchronized void finished () {
0N/A notifyAll();
0N/A }
0N/A
0N/A }
0N/A
0N/A static class MyAuthenticator3 extends Authenticator {
0N/A PasswordAuthentication pw;
0N/A MyAuthenticator3 () {
0N/A super ();
0N/A pw = new PasswordAuthentication ("user", "passwordNotCheckedAnyway".toCharArray());
0N/A }
0N/A
0N/A public PasswordAuthentication getPasswordAuthentication ()
0N/A {
0N/A System.out.println ("Auth called");
0N/A return pw;
0N/A }
0N/A
0N/A public void checkPW () {
0N/A if (!new String (pw.getPassword()).equals ("passwordNotCheckedAnyway")) {
0N/A throw new RuntimeException ("Password was \"" + new String (pw.getPassword()) + "\"");
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A static void read (InputStream is) throws IOException {
0N/A int c;
0N/A System.out.println ("reading");
0N/A while ((c=is.read()) != -1) {
0N/A System.out.write (c);
0N/A }
0N/A System.out.println ("");
0N/A System.out.println ("finished reading");
0N/A }
0N/A
0N/A public static void main (String args[]) throws Exception {
0N/A MyAuthenticator3 auth = new MyAuthenticator3 ();
0N/A Authenticator.setDefault (auth);
0N/A ServerSocket ss = new ServerSocket (0);
0N/A int port = ss.getLocalPort ();
0N/A BasicServer3 server = new BasicServer3 (ss);
0N/A synchronized (server) {
0N/A server.start();
0N/A System.out.println ("client 1");
0N/A URL url = new URL ("http://localhost:"+port+"/d1/d2/d3/foo.html");
0N/A URLConnection urlc = url.openConnection ();
0N/A InputStream is = urlc.getInputStream ();
0N/A read (is);
0N/A is.close ();
0N/A auth.checkPW ();
0N/A }
0N/A }
0N/A}