jdk/test/java/net/Authenticator/BasicTest.java
author jjg
Wed, 04 Jan 2017 18:33:20 -0800
changeset 43029 1cd1c816581e
parent 5506 202f599c92aa
permissions -rw-r--r--
8172260: remove tests from ProblemList Reviewed-by: rfield
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) 2001, 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 4474947
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @summary  fix for bug #4244472 is incomplete - HTTP authorization still needs work
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Note, this is not a general purpose test for Basic Authentication because
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * it does not check the correctness of the data, only whether the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * authenticator gets called once as expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public class BasicTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    static class BasicServer extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        ServerSocket server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        Socket s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        InputStream is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        OutputStream os;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        static final String realm = "wallyworld";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        String reply1 = "HTTP/1.1 401 Unauthorized\r\n"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            "WWW-Authenticate: Basic realm=\""+realm+"\"\r\n\r\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        String reply2 = "HTTP/1.1 200 OK\r\n"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            "Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            "Server: Apache/1.3.14 (Unix)\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            "Connection: close\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            "Content-Type: text/html; charset=iso-8859-1\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            "Content-Length: 10\r\n\r\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        BasicServer (ServerSocket s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            server = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        void readAll (Socket s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            byte[] buf = new byte [128];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            InputStream is = s.getInputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            while (is.available() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                is.read (buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        public void run () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                System.out.println ("Server 1: accept");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                s = server.accept ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                readAll (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                System.out.println ("accepted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                os = s.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                os.write (reply1.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                Thread.sleep (500);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                System.out.println ("Server 2: accept");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                s = server.accept ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                readAll (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                System.out.println ("accepted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                os = s.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                os.write ((reply2+"HelloWorld").getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                /* Second request now */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                System.out.println ("Server 3: accept");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                s = server.accept ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                readAll (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                System.out.println ("accepted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                os = s.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                os.write (reply1.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                Thread.sleep (500);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                s.close ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                System.out.println ("Server 4: accept");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                s = server.accept ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                readAll (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                System.out.println ("accepted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                os = s.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                os.write ((reply2+"HelloAgain").getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                System.out.println (e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            finished ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        public synchronized void finished () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            notifyAll();
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    static class MyAuthenticator extends Authenticator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        MyAuthenticator () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            super ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        public PasswordAuthentication getPasswordAuthentication ()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            count ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            System.out.println ("Auth called");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            return (new PasswordAuthentication ("user", "passwordNotCheckedAnyway".toCharArray()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        public int getCount () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            return (count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    static void read (InputStream is) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        System.out.println ("reading");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        while ((c=is.read()) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            System.out.write (c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        System.out.println ("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        System.out.println ("finished reading");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public static void main (String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        MyAuthenticator auth = new MyAuthenticator ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        Authenticator.setDefault (auth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        ServerSocket ss = new ServerSocket (0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        int port = ss.getLocalPort ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        BasicServer server = new BasicServer (ss);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        synchronized (server) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            server.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            System.out.println ("client 1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            URL url = new URL ("http://localhost:"+port+"/d1/d2/d3/foo.html");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            URLConnection urlc = url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            InputStream is = urlc.getInputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            read (is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            System.out.println ("client 2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            url = new URL ("http://localhost:"+port+"/d1/foo.html");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            urlc = url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            is = urlc.getInputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            read (is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            server.wait ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            // check if authenticator was called once (ok) or twice (not)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                int f = auth.getCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            if (f != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                throw new RuntimeException ("Authenticator was called "+f+" times. Should be 1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
}