author | mikael |
Fri, 04 Dec 2015 15:08:49 -0800 | |
changeset 35090 | 1f5b6aa795d0 |
parent 30820 | 0d4717a011d3 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14342
8435a30053c1
7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents:
13788
diff
changeset
|
2 |
* Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/** |
|
25 |
* @test |
|
26 |
* @bug 4678055 |
|
30820 | 27 |
* @modules java.base/sun.net.www |
2 | 28 |
* @library ../../../sun/net/www/httptest/ |
13788 | 29 |
* @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction |
2 | 30 |
* @run main B4678055 |
31 |
* @summary Basic Authentication fails with multiple realms |
|
32 |
*/ |
|
33 |
||
34 |
import java.io.*; |
|
35 |
import java.net.*; |
|
36 |
||
37 |
public class B4678055 implements HttpCallback { |
|
38 |
||
39 |
static int count = 0; |
|
40 |
static String authstring; |
|
41 |
||
42 |
void errorReply (HttpTransaction req, String reply) throws IOException { |
|
43 |
req.addResponseHeader ("Connection", "close"); |
|
44 |
req.addResponseHeader ("WWW-Authenticate", reply); |
|
45 |
req.sendResponse (401, "Unauthorized"); |
|
46 |
req.orderlyClose(); |
|
47 |
} |
|
48 |
||
49 |
void okReply (HttpTransaction req) throws IOException { |
|
50 |
req.setResponseEntityBody ("Hello ."); |
|
51 |
req.sendResponse (200, "Ok"); |
|
52 |
req.orderlyClose(); |
|
53 |
} |
|
54 |
||
55 |
public void request (HttpTransaction req) { |
|
56 |
try { |
|
57 |
authstring = req.getRequestHeader ("Authorization"); |
|
58 |
System.out.println (authstring); |
|
59 |
switch (count) { |
|
60 |
case 0: |
|
61 |
errorReply (req, "Basic realm=\"wallyworld\""); |
|
62 |
break; |
|
63 |
case 1: |
|
64 |
/* client stores a username/pw for wallyworld |
|
65 |
*/ |
|
66 |
okReply (req); |
|
67 |
break; |
|
68 |
case 2: |
|
69 |
/* emulates a server that has configured a second |
|
70 |
* realm, but by misconfiguration uses the same |
|
71 |
* realm string as the previous one. |
|
72 |
* |
|
73 |
* An alternative (more likely) scenario that shows this behavior is |
|
74 |
* the case where the password in the original realm has changed |
|
75 |
*/ |
|
76 |
errorReply (req, "Basic realm=\"wallyworld\""); |
|
77 |
break; |
|
78 |
case 3: |
|
79 |
/* The client replies with the username/password |
|
80 |
* from the first realm, which is wrong (unexpectedly) |
|
81 |
*/ |
|
82 |
errorReply (req, "Basic realm=\"wallyworld\""); |
|
83 |
break; |
|
84 |
case 4: |
|
85 |
/* The client re-prompts for a password and |
|
86 |
* we now reply with an OK. The client with the bug |
|
87 |
* will throw NPE at this point. |
|
88 |
*/ |
|
89 |
case 5: |
|
90 |
/* Repeat the OK, to make sure the same new auth string is sent */ |
|
91 |
okReply (req); |
|
92 |
break; |
|
93 |
} |
|
94 |
count ++; |
|
95 |
} catch (IOException e) { |
|
96 |
e.printStackTrace(); |
|
97 |
} |
|
98 |
} |
|
99 |
||
100 |
static void read (InputStream is) throws IOException { |
|
101 |
int c; |
|
102 |
System.out.println ("reading"); |
|
103 |
while ((c=is.read()) != -1) { |
|
104 |
System.out.write (c); |
|
105 |
} |
|
106 |
System.out.println (""); |
|
107 |
System.out.println ("finished reading"); |
|
108 |
} |
|
109 |
||
110 |
static boolean checkFinalAuth () { |
|
111 |
return authstring.equals ("Basic dXNlcjpwYXNzMg=="); |
|
112 |
} |
|
113 |
||
114 |
static void client (String u) throws Exception { |
|
115 |
URL url = new URL (u); |
|
116 |
System.out.println ("client opening connection to: " + u); |
|
117 |
URLConnection urlc = url.openConnection (); |
|
118 |
InputStream is = urlc.getInputStream (); |
|
119 |
read (is); |
|
120 |
is.close(); |
|
121 |
} |
|
122 |
||
13788 | 123 |
static TestHttpServer server; |
2 | 124 |
|
125 |
public static void main (String[] args) throws Exception { |
|
126 |
MyAuthenticator auth = new MyAuthenticator (); |
|
127 |
Authenticator.setDefault (auth); |
|
128 |
try { |
|
13788 | 129 |
server = new TestHttpServer (new B4678055(), 1, 10, 0); |
2 | 130 |
System.out.println ("Server: listening on port: " + server.getLocalPort()); |
131 |
client ("http://localhost:"+server.getLocalPort()+"/d1/foo.html"); |
|
132 |
client ("http://localhost:"+server.getLocalPort()+"/d2/foo.html"); |
|
133 |
client ("http://localhost:"+server.getLocalPort()+"/d2/foo.html"); |
|
134 |
} catch (Exception e) { |
|
135 |
if (server != null) { |
|
136 |
server.terminate(); |
|
137 |
} |
|
138 |
throw e; |
|
139 |
} |
|
140 |
int f = auth.getCount(); |
|
141 |
if (f != 2) { |
|
142 |
except ("Authenticator was called "+f+" times. Should be 2"); |
|
143 |
} |
|
144 |
/* this checks the authorization string corresponding to second password "pass2"*/ |
|
145 |
if (!checkFinalAuth()) { |
|
146 |
except ("Wrong authorization string received from client"); |
|
147 |
} |
|
148 |
server.terminate(); |
|
149 |
} |
|
150 |
||
151 |
public static void except (String s) { |
|
152 |
server.terminate(); |
|
153 |
throw new RuntimeException (s); |
|
154 |
} |
|
155 |
||
156 |
static class MyAuthenticator extends Authenticator { |
|
157 |
MyAuthenticator () { |
|
158 |
super (); |
|
159 |
} |
|
160 |
||
161 |
int count = 0; |
|
162 |
||
163 |
public PasswordAuthentication getPasswordAuthentication () { |
|
164 |
PasswordAuthentication pw; |
|
165 |
if (count == 0) { |
|
166 |
pw = new PasswordAuthentication ("user", "pass1".toCharArray()); |
|
167 |
} else { |
|
168 |
pw = new PasswordAuthentication ("user", "pass2".toCharArray()); |
|
169 |
} |
|
170 |
count ++; |
|
171 |
return pw; |
|
172 |
} |
|
173 |
||
174 |
public int getCount () { |
|
175 |
return (count); |
|
176 |
} |
|
177 |
} |
|
178 |
} |