test/jdk/java/net/Authenticator/B4722333.java
changeset 55031 37a077319427
parent 54681 edd709e64ea1
equal deleted inserted replaced
55030:703b2c04fc2c 55031:37a077319427
    25  * @test
    25  * @test
    26  * @bug 4722333
    26  * @bug 4722333
    27  * @modules java.base/sun.net.www
    27  * @modules java.base/sun.net.www
    28  * @library ../../../sun/net/www/httptest/
    28  * @library ../../../sun/net/www/httptest/
    29  * @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction
    29  * @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction
    30  * @run main B4722333
    30  * @run main/othervm B4722333
    31  * @summary JRE Proxy Authentication Not Working with ISA2000
    31  * @summary JRE Proxy Authentication Not Working with ISA2000
    32  */
    32  */
    33 
    33 
    34 import java.io.*;
    34 import java.io.*;
    35 import java.net.*;
    35 import java.net.*;
    36 
    36 
    37 public class B4722333 implements HttpCallback {
    37 public class B4722333 implements HttpCallback {
    38 
    38 
    39     static int count = 0;
    39     static int count = 0;
    40 
    40 
    41     static String [][] expected = {
    41     static String[][] expected = {
    42        /* scheme  realm/prompt */
    42        /* scheme  realm/prompt */
    43         {"basic", "foo"},
    43         {"basic", "foo"},
    44         {"basic", "foobar"},
    44         {"basic", "foobar"},
    45         {"digest", "biz"},
    45         {"digest", "biz"},
    46         {"digest", "bizbar"},
    46         {"digest", "bizbar"},
    47         {"digest", "foobiz"}
    47         {"digest", "foobiz"}
    48     };
    48     };
    49 
    49 
    50     public void request (HttpTransaction req) {
    50     public void request(HttpTransaction req) {
    51         try {
    51         try {
    52             if (count % 2 == 1 ) {
    52             if (count % 2 == 1) {
    53                 req.setResponseEntityBody ("Hello .");
    53                 req.setResponseEntityBody("Hello .");
    54                 req.sendResponse (200, "Ok");
    54                 req.sendResponse(200, "Ok");
    55                 req.orderlyClose();
    55                 req.orderlyClose();
    56             } else {
    56             } else {
    57                 switch (count) {
    57                 switch (count) {
    58                   case 0:
    58                   case 0:
    59                     req.addResponseHeader ("Connection", "close");
    59                     req.addResponseHeader("Connection", "close");
    60                     req.addResponseHeader ("WWW-Authenticate", "Basic realm=\"foo\"");
    60                     req.addResponseHeader("WWW-Authenticate", "Basic realm=\"foo\"");
    61                     req.addResponseHeader ("WWW-Authenticate", "Foo realm=\"bar\"");
    61                     req.addResponseHeader("WWW-Authenticate", "Foo realm=\"bar\"");
    62                     req.sendResponse (401, "Unauthorized");
    62                     req.sendResponse(401, "Unauthorized");
    63                     req.orderlyClose();
    63                     req.orderlyClose();
    64                     break;
    64                     break;
    65                   case 2:
    65                   case 2:
    66                     req.addResponseHeader ("Connection", "close");
    66                     req.addResponseHeader("Connection", "close");
    67                     req.addResponseHeader ("WWW-Authenticate", "Basic realm=\"foobar\" Foo realm=\"bar\"");
    67                     req.addResponseHeader("WWW-Authenticate", "Basic realm=\"foobar\" Foo realm=\"bar\"");
    68                     req.sendResponse (401, "Unauthorized");
    68                     req.sendResponse(401, "Unauthorized");
    69                     break;
    69                     break;
    70                   case 4:
    70                   case 4:
    71                     req.addResponseHeader ("Connection", "close");
    71                     req.addResponseHeader("Connection", "close");
    72                     req.addResponseHeader ("WWW-Authenticate", "Digest realm=biz domain=/foo nonce=thisisanonce ");
    72                     req.addResponseHeader("WWW-Authenticate", "Digest realm=biz domain=/foo nonce=thisisanonce ");
    73                     req.addResponseHeader ("WWW-Authenticate", "Basic realm=bizbar");
    73                     req.addResponseHeader("WWW-Authenticate", "Basic realm=bizbar");
    74                     req.sendResponse (401, "Unauthorized");
    74                     req.sendResponse(401, "Unauthorized");
    75                     req.orderlyClose();
    75                     req.orderlyClose();
    76                     break;
    76                     break;
    77                   case 6:
    77                   case 6:
    78                     req.addResponseHeader ("Connection", "close");
    78                     req.addResponseHeader("Connection", "close");
    79                     req.addResponseHeader ("WWW-Authenticate", "Digest realm=\"bizbar\" domain=/biz nonce=\"hereisanonce\" Basic realm=\"foobar\" Foo realm=\"bar\"");
    79                     req.addResponseHeader("WWW-Authenticate", "Digest realm=\"bizbar\" domain=/biz nonce=\"hereisanonce\" Basic realm=\"foobar\" Foo realm=\"bar\"");
    80                     req.sendResponse (401, "Unauthorized");
    80                     req.sendResponse(401, "Unauthorized");
    81                     req.orderlyClose();
    81                     req.orderlyClose();
    82                     break;
    82                     break;
    83                   case 8:
    83                   case 8:
    84                     req.addResponseHeader ("Connection", "close");
    84                     req.addResponseHeader("Connection", "close");
    85                     req.addResponseHeader ("WWW-Authenticate", "Foo p1=1 p2=2 p3=3 p4=4 p5=5 p6=6 p7=7 p8=8 p9=10 Digest realm=foobiz domain=/foobiz nonce=newnonce");
    85                     req.addResponseHeader("WWW-Authenticate", "Foo p1=1 p2=2 p3=3 p4=4 p5=5 p6=6 p7=7 p8=8 p9=10 Digest realm=foobiz domain=/foobiz nonce=newnonce");
    86                     req.addResponseHeader ("WWW-Authenticate", "Basic realm=bizbar");
    86                     req.addResponseHeader("WWW-Authenticate", "Basic realm=bizbar");
    87                     req.sendResponse (401, "Unauthorized");
    87                     req.sendResponse(401, "Unauthorized");
    88                     req.orderlyClose();
    88                     req.orderlyClose();
    89                     break;
    89                     break;
    90                 }
    90                 }
    91             }
    91             }
    92             count ++;
    92             count ++;
    93         } catch (IOException e) {
    93         } catch (IOException e) {
    94             e.printStackTrace();
    94             e.printStackTrace();
    95         }
    95         }
    96     }
    96     }
    97 
    97 
    98     static void read (InputStream is) throws IOException {
    98     static void read(InputStream is) throws IOException {
    99         int c;
    99         int c;
   100         System.out.println ("reading");
   100         System.out.println("reading");
   101         while ((c=is.read()) != -1) {
   101         while ((c=is.read()) != -1) {
   102             System.out.write (c);
   102             System.out.write(c);
   103         }
   103         }
   104         System.out.println ("");
   104         System.out.println("");
   105         System.out.println ("finished reading");
   105         System.out.println("finished reading");
   106     }
   106     }
   107 
   107 
   108 
   108 
   109     static void client (String u) throws Exception {
   109     static void client(String u) throws Exception {
   110         URL url = new URL (u);
   110         URL url = new URL (u);
   111         System.out.println ("client opening connection to: " + u);
   111         System.out.println("client opening connection to: " + u);
   112         URLConnection urlc = url.openConnection ();
   112         URLConnection urlc = url.openConnection ();
   113         InputStream is = urlc.getInputStream ();
   113         InputStream is = urlc.getInputStream ();
   114         read (is);
   114         read(is);
   115         is.close();
   115         is.close();
   116     }
   116     }
   117 
   117 
   118     static TestHttpServer server;
   118     static TestHttpServer server;
   119 
   119 
   120     public static void main (String[] args) throws Exception {
   120     public static void main(String[] args) throws Exception {
   121         MyAuthenticator auth = new MyAuthenticator ();
   121         MyAuthenticator auth = new MyAuthenticator();
   122         Authenticator.setDefault (auth);
   122         Authenticator.setDefault(auth);
   123         try {
   123         try {
   124             InetAddress loopback = InetAddress.getLoopbackAddress();
   124             InetAddress loopback = InetAddress.getLoopbackAddress();
   125             server = new TestHttpServer (new B4722333(), 1, 10, loopback, 0);
   125             server = new TestHttpServer(new B4722333(), 1, 10, loopback, 0);
   126             System.out.println ("Server started: listening on port: " + server.getLocalPort());
   126             System.out.println("Server started: listening on port: " + server.getLocalPort());
   127             client ("http://" + server.getAuthority() + "/d1/d2/d3/foo.html");
   127             client("http://" + server.getAuthority() + "/d1/d2/d3/foo.html");
   128             client ("http://" + server.getAuthority() + "/ASD/d3/x.html");
   128             client("http://" + server.getAuthority() + "/ASD/d3/x.html");
   129             client ("http://" + server.getAuthority() + "/biz/d3/x.html");
   129             client("http://" + server.getAuthority() + "/biz/d3/x.html");
   130             client ("http://" + server.getAuthority() + "/bar/d3/x.html");
   130             client("http://" + server.getAuthority() + "/bar/d3/x.html");
   131             client ("http://" + server.getAuthority() + "/fuzz/d3/x.html");
   131             client("http://" + server.getAuthority() + "/fuzz/d3/x.html");
   132         } catch (Exception e) {
   132         } catch (Exception e) {
   133             if (server != null) {
   133             if (server != null) {
   134                 server.terminate();
   134                 server.terminate();
   135             }
   135             }
   136             throw e;
   136             throw e;
   137         }
   137         }
   138         int f = auth.getCount();
   138         int f = auth.getCount();
   139         if (f != expected.length) {
   139         if (f != expected.length) {
   140             except ("Authenticator was called "+f+" times. Should be " + expected.length);
   140             except("Authenticator was called "+f+" times. Should be " + expected.length);
   141         }
   141         }
   142         server.terminate();
   142         server.terminate();
   143     }
   143     }
   144 
   144 
   145     public static void except (String s) {
   145     public static void except(String s) {
   146         server.terminate();
   146         server.terminate();
   147         throw new RuntimeException (s);
   147         throw new RuntimeException(s);
   148     }
   148     }
   149 
   149 
   150     static class MyAuthenticator extends Authenticator {
   150     static class MyAuthenticator extends Authenticator {
   151         MyAuthenticator () {
   151         MyAuthenticator() {
   152             super ();
   152             super();
   153         }
   153         }
   154 
   154 
   155         int count = 0;
   155         int count = 0;
   156 
   156 
   157         public PasswordAuthentication getPasswordAuthentication ()
   157         public PasswordAuthentication getPasswordAuthentication() {
   158             {
   158             System.out.println("Auth called");
   159             System.out.println ("Auth called");
       
   160             String scheme = getRequestingScheme();
   159             String scheme = getRequestingScheme();
   161             System.out.println ("getRequestingScheme() returns " + scheme);
   160             System.out.println("getRequestingScheme() returns " + scheme);
   162             String prompt = getRequestingPrompt();
   161             String prompt = getRequestingPrompt();
   163             System.out.println ("getRequestingPrompt() returns " + prompt);
   162             System.out.println("getRequestingPrompt() returns " + prompt);
   164 
   163 
   165             if (!scheme.equals (expected [count][0])) {
   164             if (!scheme.equals(expected [count][0])) {
   166                 B4722333.except ("wrong scheme received, " + scheme + " expected " + expected [count][0]);
   165                 B4722333.except("wrong scheme received, " + scheme + " expected " + expected [count][0]);
   167             }
   166             }
   168             if (!prompt.equals (expected [count][1])) {
   167             if (!prompt.equals(expected [count][1])) {
   169                 B4722333.except ("wrong realm received, " + prompt + " expected " + expected [count][1]);
   168                 B4722333.except("wrong realm received, " + prompt + " expected " + expected [count][1]);
   170             }
   169             }
   171             count ++;
   170             count ++;
   172             return (new PasswordAuthentication ("user", "passwordNotCheckedAnyway".toCharArray()));
   171             return (new PasswordAuthentication("user", "passwordNotCheckedAnyway".toCharArray()));
   173         }
   172         }
   174 
   173 
   175         public int getCount () {
   174         public int getCount () {
   176             return (count);
   175             return count;
   177         }
   176         }
   178     }
   177     }
   179 
   178 
   180 }
   179 }