jdk/test/java/net/Authenticator/BasicTest3.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @bug 4513440
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @summary BasicAuthentication is zeroing out the given password
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
public class BasicTest3 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    static class BasicServer3 extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        ServerSocket server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        Socket s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        InputStream is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        OutputStream os;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        static final String realm = "wallyworld";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        String reply1 = "HTTP/1.1 401 Unauthorized\r\n"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            "WWW-Authenticate: Basic realm=\""+realm+"\"\r\n\r\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        String reply2 = "HTTP/1.1 200 OK\r\n"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            "Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            "Server: Apache/1.3.14 (Unix)\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            "Connection: close\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            "Content-Type: text/html; charset=iso-8859-1\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            "Content-Length: 10\r\n\r\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        BasicServer3 (ServerSocket s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            server = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        void readAll (Socket s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            byte[] buf = new byte [128];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            InputStream is = s.getInputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            s.setSoTimeout(1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                while (is.read(buf) > 0) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            } catch (SocketTimeoutException x) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        public void run () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                System.out.println ("Server 1: accept");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                s = server.accept ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                System.out.println ("accepted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                os = s.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                os.write (reply1.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                readAll (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                s.close ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                System.out.println ("Server 2: accept");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                s = server.accept ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                System.out.println ("accepted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                os = s.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                readAll (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                os.write ((reply2+"HelloWorld").getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                System.out.println (e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            finished ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        public synchronized void finished () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    static class MyAuthenticator3 extends Authenticator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        PasswordAuthentication pw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        MyAuthenticator3 () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            super ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            pw = new PasswordAuthentication ("user", "passwordNotCheckedAnyway".toCharArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        public PasswordAuthentication getPasswordAuthentication ()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            System.out.println ("Auth called");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            return pw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        public void checkPW () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            if (!new String (pw.getPassword()).equals ("passwordNotCheckedAnyway")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                throw new RuntimeException ("Password was \"" + new String (pw.getPassword()) + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    static void read (InputStream is) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        System.out.println ("reading");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        while ((c=is.read()) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            System.out.write (c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        System.out.println ("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        System.out.println ("finished reading");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public static void main (String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        MyAuthenticator3 auth = new MyAuthenticator3 ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        Authenticator.setDefault (auth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        ServerSocket ss = new ServerSocket (0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        int port = ss.getLocalPort ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        BasicServer3 server = new BasicServer3 (ss);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        synchronized (server) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            server.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            System.out.println ("client 1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            URL url = new URL ("http://localhost:"+port+"/d1/d2/d3/foo.html");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            URLConnection urlc = url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            InputStream is = urlc.getInputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            read (is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            is.close ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            auth.checkPW ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
}