1 /*
   2  * Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 4722333
  27  * @modules java.base/sun.net.www
  28  * @library ../../../sun/net/www/httptest/
  29  * @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction
  30  * @run main B4722333
  31  * @summary JRE Proxy Authentication Not Working with ISA2000
  32  */
  33 
  34 import java.io.*;
  35 import java.net.*;
  36 
  37 public class B4722333 implements HttpCallback {
  38 
  39     static int count = 0;
  40 
  41     static String [][] expected = {
  42        /* scheme  realm/prompt */
  43         {"basic", "foo"},
  44         {"basic", "foobar"},
  45         {"digest", "biz"},
  46         {"digest", "bizbar"},
  47         {"digest", "foobiz"}
  48     };
  49 
  50     public void request (HttpTransaction req) {
  51         try {
  52             if (count % 2 == 1 ) {
  53                 req.setResponseEntityBody ("Hello .");
  54                 req.sendResponse (200, "Ok");
  55                 req.orderlyClose();
  56             } else {
  57                 switch (count) {
  58                   case 0:
  59                     req.addResponseHeader ("Connection", "close");
  60                     req.addResponseHeader ("WWW-Authenticate", "Basic realm=\"foo\"");
  61                     req.addResponseHeader ("WWW-Authenticate", "Foo realm=\"bar\"");
  62                     req.sendResponse (401, "Unauthorized");
  63                     req.orderlyClose();
  64                     break;
  65                   case 2:
  66                     req.addResponseHeader ("Connection", "close");
  67                     req.addResponseHeader ("WWW-Authenticate", "Basic realm=\"foobar\" Foo realm=\"bar\"");
  68                     req.sendResponse (401, "Unauthorized");
  69                     break;
  70                   case 4:
  71                     req.addResponseHeader ("Connection", "close");
  72                     req.addResponseHeader ("WWW-Authenticate", "Digest realm=biz domain=/foo nonce=thisisanonce ");
  73                     req.addResponseHeader ("WWW-Authenticate", "Basic realm=bizbar");
  74                     req.sendResponse (401, "Unauthorized");
  75                     req.orderlyClose();
  76                     break;
  77                   case 6:
  78                     req.addResponseHeader ("Connection", "close");
  79                     req.addResponseHeader ("WWW-Authenticate", "Digest realm=\"bizbar\" domain=/biz nonce=\"hereisanonce\" Basic realm=\"foobar\" Foo realm=\"bar\"");
  80                     req.sendResponse (401, "Unauthorized");
  81                     req.orderlyClose();
  82                     break;
  83                   case 8:
  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");
  86                     req.addResponseHeader ("WWW-Authenticate", "Basic realm=bizbar");
  87                     req.sendResponse (401, "Unauthorized");
  88                     req.orderlyClose();
  89                     break;
  90                 }
  91             }
  92             count ++;
  93         } catch (IOException e) {
  94             e.printStackTrace();
  95         }
  96     }
  97 
  98     static void read (InputStream is) throws IOException {
  99         int c;
 100         System.out.println ("reading");
 101         while ((c=is.read()) != -1) {
 102             System.out.write (c);
 103         }
 104         System.out.println ("");
 105         System.out.println ("finished reading");
 106     }
 107 
 108 
 109     static void client (String u) throws Exception {
 110         URL url = new URL (u);
 111         System.out.println ("client opening connection to: " + u);
 112         URLConnection urlc = url.openConnection ();
 113         InputStream is = urlc.getInputStream ();
 114         read (is);
 115         is.close();
 116     }
 117 
 118     static TestHttpServer server;
 119 
 120     public static void main (String[] args) throws Exception {
 121         MyAuthenticator auth = new MyAuthenticator ();
 122         Authenticator.setDefault (auth);
 123         try {
 124             server = new TestHttpServer (new B4722333(), 1, 10, 0);
 125             System.out.println ("Server started: listening on port: " + server.getLocalPort());
 126             client ("http://localhost:"+server.getLocalPort()+"/d1/d2/d3/foo.html");
 127             client ("http://localhost:"+server.getLocalPort()+"/ASD/d3/x.html");
 128             client ("http://localhost:"+server.getLocalPort()+"/biz/d3/x.html");
 129             client ("http://localhost:"+server.getLocalPort()+"/bar/d3/x.html");
 130             client ("http://localhost:"+server.getLocalPort()+"/fuzz/d3/x.html");
 131         } catch (Exception e) {
 132             if (server != null) {
 133                 server.terminate();
 134             }
 135             throw e;
 136         }
 137         int f = auth.getCount();
 138         if (f != expected.length) {
 139             except ("Authenticator was called "+f+" times. Should be " + expected.length);
 140         }
 141         server.terminate();
 142     }
 143 
 144     public static void except (String s) {
 145         server.terminate();
 146         throw new RuntimeException (s);
 147     }
 148 
 149     static class MyAuthenticator extends Authenticator {
 150         MyAuthenticator () {
 151             super ();
 152         }
 153 
 154         int count = 0;
 155 
 156         public PasswordAuthentication getPasswordAuthentication ()
 157             {
 158             System.out.println ("Auth called");
 159             String scheme = getRequestingScheme();
 160             System.out.println ("getRequestingScheme() returns " + scheme);
 161             String prompt = getRequestingPrompt();
 162             System.out.println ("getRequestingPrompt() returns " + prompt);
 163 
 164             if (!scheme.equals (expected [count][0])) {
 165                 B4722333.except ("wrong scheme received, " + scheme + " expected " + expected [count][0]);
 166             }
 167             if (!prompt.equals (expected [count][1])) {
 168                 B4722333.except ("wrong realm received, " + prompt + " expected " + expected [count][1]);
 169             }
 170             count ++;
 171             return (new PasswordAuthentication ("user", "passwordNotCheckedAnyway".toCharArray()));
 172         }
 173 
 174         public int getCount () {
 175             return (count);
 176         }
 177     }
 178 
 179 }