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 4769350
27 * @library ../../../sun/net/www/httptest/
28 * @build HttpCallback HttpServer ClosedChannelList HttpTransaction AbstractCallback
29 * @run main/othervm B4769350 server
30 * @run main/othervm B4769350 proxy
31 * @summary proxy authentication username and password caching only works in serial case
32 * Run in othervm since the test sets system properties that are read by the
33 * networking stack and cached when the HTTP handler is invoked, and previous
34 * tests may already have invoked the HTTP handler.
35 */
36
37 import java.io.*;
38 import java.net.*;
39
40 public class B4769350 {
41
42 static int count = 0;
43 static boolean error = false;
44
45 static void read (InputStream is) throws IOException {
46 int c;
47 while ((c=is.read()) != -1) {
48 //System.out.write (c);
125 } else if (path.endsWith ("/t3c")) {
126 doT3bc (req, count);
127 } else {
128 System.out.println ("unexpected request URI");
129 }
130 } catch (IOException e) {
131 e.printStackTrace();
132 }
133 }
134
135 /* T1 tests the client by sending 4 requests to 2 different realms
136 * in parallel. The client should recognise two pairs of dependent requests
137 * and execute the first of each pair in parallel. When they both succeed
138 * the second requests should be executed without calling the authenticator.
139 * The test succeeds if the authenticator was only called twice.
140 */
141 void doT1a (HttpTransaction req, int count) throws IOException {
142 switch (count) {
143 case 0:
144 errorReply (req, "Basic realm=\"realm1\"");
145 HttpServer.rendezvous ("one", 2);
146 break;
147 case 1:
148 HttpServer.waitForCondition ("cond2");
149 okReply (req);
150 break;
151 default:
152 System.out.println ("Unexpected request");
153 }
154 }
155
156
157 void doT1b (HttpTransaction req, int count) throws IOException {
158 switch (count) {
159 case 0:
160 errorReply (req, "Basic realm=\"realm2\"");
161 HttpServer.rendezvous ("one", 2);
162 HttpServer.setCondition ("cond1");
163 break;
164 case 1:
165 HttpServer.waitForCondition ("cond2");
166 okReply (req);
167 break;
168 default:
169 System.out.println ("Unexpected request");
170 }
171 }
172
173 void doT1c (HttpTransaction req, int count) throws IOException {
174 switch (count) {
175 case 0:
176 errorReply (req, "Basic realm=\"realm1\"");
177 HttpServer.rendezvous ("two", 2);
178 break;
179 case 1:
180 okReply (req);
181 break;
182 default:
183 System.out.println ("Unexpected request");
184 }
185 }
186
187 void doT1d (HttpTransaction req, int count) throws IOException {
188 switch (count) {
189 case 0:
190 errorReply (req, "Basic realm=\"realm2\"");
191 HttpServer.rendezvous ("two", 2);
192 HttpServer.setCondition ("cond2");
193 break;
194 case 1:
195 okReply (req);
196 break;
197 default:
198 System.out.println ("Unexpected request");
199 }
200 }
201
202
203 /* T2 tests to check that if initial authentication fails, the second will
204 * succeed, and the authenticator is called twice
205 */
206
207 void doT2a (HttpTransaction req, int count) throws IOException {
208 /* This will be called several times */
209 if (count == 1) {
210 HttpServer.setCondition ("T2cond1");
211 }
212 errorReply (req, "Basic realm=\"realm3\"");
213 }
214
215 void doT2b (HttpTransaction req, int count) throws IOException {
216 switch (count) {
217 case 0:
218 errorReply (req, "Basic realm=\"realm3\"");
219 break;
220 case 1:
221 okReply (req);
222 break;
223 default:
224 System.out.println ("Unexpected request");
225 }
226 }
227
228 /* T3 tests proxy and server authentication. three threads request same
229 * resource at same time. Authenticator should be called once for server
230 * and once for proxy
231 */
232 void doT3a (HttpTransaction req, int count) throws IOException {
233 switch (count) {
234 case 0:
235 proxyReply (req, "Basic realm=\"proxy\"");
236 HttpServer.setCondition ("T3cond1");
237 break;
238 case 1:
239 errorReply (req, "Basic realm=\"realm4\"");
240 break;
241 case 2:
242 okReply (req);
243 break;
244 default:
245 System.out.println ("Unexpected request");
246 }
247 }
248
249 void doT3bc (HttpTransaction req, int count) throws IOException {
250 switch (count) {
251 case 0:
252 proxyReply (req, "Basic realm=\"proxy\"");
253 break;
254 case 1:
255 okReply (req);
256 break;
257 default:
258 System.out.println ("Unexpected request");
259 }
260 }
261 };
262
263 static HttpServer server;
264 static MyAuthenticator auth = new MyAuthenticator ();
265
266 static int redirects = 4;
267
268 static Client c1,c2,c3,c4,c5,c6,c7,c8,c9;
269
270 static void doServerTests (String authority) throws Exception {
271 System.out.println ("Doing Server tests");
272 System.out.println ("T1");
273 c1 = new Client (authority, "/test/realm1/t1a", false);
274 c2 = new Client (authority, "/test/realm2/t1b", false);
275 c3 = new Client (authority, "/test/realm1/t1c", false);
276 c4 = new Client (authority, "/test/realm2/t1d", false);
277
278 c1.start(); c2.start();
279 HttpServer.waitForCondition ("cond1");
280 c3.start(); c4.start();
281 c1.join(); c2.join(); c3.join(); c4.join();
282
283 int f = auth.getCount();
284 if (f != 2) {
285 except ("Authenticator was called "+f+" times. Should be 2");
286 }
287 if (error) {
288 except ("error occurred");
289 }
290
291 auth.resetCount();
292 System.out.println ("T2");
293
294 c5 = new Client (authority, "/test/realm3/t2a", true);
295 c6 = new Client (authority, "/test/realm3/t2b", false);
296 c5.start ();
297 HttpServer.waitForCondition ("T2cond1");
298 c6.start ();
299 c5.join(); c6.join();
300
301 f = auth.getCount();
302 if (f != redirects+1) {
303 except ("Authenticator was called "+f+" times. Should be: " + redirects+1);
304 }
305 if (error) {
306 except ("error occurred");
307 }
308 }
309
310 static void doProxyTests (String authority) throws Exception {
311 System.out.println ("Doing Proxy tests");
312 c7 = new Client (authority, "/test/realm4/t3a", false);
313 c8 = new Client (authority, "/test/realm4/t3b", false);
314 c9 = new Client (authority, "/test/realm4/t3c", false);
315 c7.start ();
316 HttpServer.waitForCondition ("T3cond1");
317 c8.start ();
318 c9.start ();
319 c7.join(); c8.join(); c9.join();
320
321 int f = auth.getCount();
322 if (f != 2) {
323 except ("Authenticator was called "+f+" times. Should be: " + 2);
324 }
325 if (error) {
326 except ("error occurred");
327 }
328 }
329
330 public static void main (String[] args) throws Exception {
331 System.setProperty ("http.maxRedirects", Integer.toString (redirects));
332 System.setProperty ("http.auth.serializeRequests", "true");
333 Authenticator.setDefault (auth);
334 boolean proxy = args[0].equals ("proxy");
335 try {
336 server = new HttpServer (new CallBack(), 10, 1, 0);
337 System.out.println ("Server: listening on port: " + server.getLocalPort());
338 if (proxy) {
339 System.setProperty ("http.proxyHost", "localhost");
340 System.setProperty ("http.proxyPort",Integer.toString(server.getLocalPort()));
341 doProxyTests ("www.foo.com");
342 } else {
343 doServerTests ("localhost:"+server.getLocalPort());
344 }
345 server.terminate();
346
347 } catch (Exception e) {
348 if (server != null) {
349 server.terminate();
350 }
351 throw e;
352 }
353 }
354
355 static void pause (int millis) {
356 try {
|
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 4769350
27 * @library ../../../sun/net/www/httptest/
28 * @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction AbstractCallback
29 * @run main/othervm B4769350 server
30 * @run main/othervm B4769350 proxy
31 * @summary proxy authentication username and password caching only works in serial case
32 * Run in othervm since the test sets system properties that are read by the
33 * networking stack and cached when the HTTP handler is invoked, and previous
34 * tests may already have invoked the HTTP handler.
35 */
36
37 import java.io.*;
38 import java.net.*;
39
40 public class B4769350 {
41
42 static int count = 0;
43 static boolean error = false;
44
45 static void read (InputStream is) throws IOException {
46 int c;
47 while ((c=is.read()) != -1) {
48 //System.out.write (c);
125 } else if (path.endsWith ("/t3c")) {
126 doT3bc (req, count);
127 } else {
128 System.out.println ("unexpected request URI");
129 }
130 } catch (IOException e) {
131 e.printStackTrace();
132 }
133 }
134
135 /* T1 tests the client by sending 4 requests to 2 different realms
136 * in parallel. The client should recognise two pairs of dependent requests
137 * and execute the first of each pair in parallel. When they both succeed
138 * the second requests should be executed without calling the authenticator.
139 * The test succeeds if the authenticator was only called twice.
140 */
141 void doT1a (HttpTransaction req, int count) throws IOException {
142 switch (count) {
143 case 0:
144 errorReply (req, "Basic realm=\"realm1\"");
145 TestHttpServer.rendezvous ("one", 2);
146 break;
147 case 1:
148 TestHttpServer.waitForCondition ("cond2");
149 okReply (req);
150 break;
151 default:
152 System.out.println ("Unexpected request");
153 }
154 }
155
156
157 void doT1b (HttpTransaction req, int count) throws IOException {
158 switch (count) {
159 case 0:
160 errorReply (req, "Basic realm=\"realm2\"");
161 TestHttpServer.rendezvous ("one", 2);
162 TestHttpServer.setCondition ("cond1");
163 break;
164 case 1:
165 TestHttpServer.waitForCondition ("cond2");
166 okReply (req);
167 break;
168 default:
169 System.out.println ("Unexpected request");
170 }
171 }
172
173 void doT1c (HttpTransaction req, int count) throws IOException {
174 switch (count) {
175 case 0:
176 errorReply (req, "Basic realm=\"realm1\"");
177 TestHttpServer.rendezvous ("two", 2);
178 break;
179 case 1:
180 okReply (req);
181 break;
182 default:
183 System.out.println ("Unexpected request");
184 }
185 }
186
187 void doT1d (HttpTransaction req, int count) throws IOException {
188 switch (count) {
189 case 0:
190 errorReply (req, "Basic realm=\"realm2\"");
191 TestHttpServer.rendezvous ("two", 2);
192 TestHttpServer.setCondition ("cond2");
193 break;
194 case 1:
195 okReply (req);
196 break;
197 default:
198 System.out.println ("Unexpected request");
199 }
200 }
201
202
203 /* T2 tests to check that if initial authentication fails, the second will
204 * succeed, and the authenticator is called twice
205 */
206
207 void doT2a (HttpTransaction req, int count) throws IOException {
208 /* This will be called several times */
209 if (count == 1) {
210 TestHttpServer.setCondition ("T2cond1");
211 }
212 errorReply (req, "Basic realm=\"realm3\"");
213 }
214
215 void doT2b (HttpTransaction req, int count) throws IOException {
216 switch (count) {
217 case 0:
218 errorReply (req, "Basic realm=\"realm3\"");
219 break;
220 case 1:
221 okReply (req);
222 break;
223 default:
224 System.out.println ("Unexpected request");
225 }
226 }
227
228 /* T3 tests proxy and server authentication. three threads request same
229 * resource at same time. Authenticator should be called once for server
230 * and once for proxy
231 */
232 void doT3a (HttpTransaction req, int count) throws IOException {
233 switch (count) {
234 case 0:
235 proxyReply (req, "Basic realm=\"proxy\"");
236 TestHttpServer.setCondition ("T3cond1");
237 break;
238 case 1:
239 errorReply (req, "Basic realm=\"realm4\"");
240 break;
241 case 2:
242 okReply (req);
243 break;
244 default:
245 System.out.println ("Unexpected request");
246 }
247 }
248
249 void doT3bc (HttpTransaction req, int count) throws IOException {
250 switch (count) {
251 case 0:
252 proxyReply (req, "Basic realm=\"proxy\"");
253 break;
254 case 1:
255 okReply (req);
256 break;
257 default:
258 System.out.println ("Unexpected request");
259 }
260 }
261 };
262
263 static TestHttpServer server;
264 static MyAuthenticator auth = new MyAuthenticator ();
265
266 static int redirects = 4;
267
268 static Client c1,c2,c3,c4,c5,c6,c7,c8,c9;
269
270 static void doServerTests (String authority) throws Exception {
271 System.out.println ("Doing Server tests");
272 System.out.println ("T1");
273 c1 = new Client (authority, "/test/realm1/t1a", false);
274 c2 = new Client (authority, "/test/realm2/t1b", false);
275 c3 = new Client (authority, "/test/realm1/t1c", false);
276 c4 = new Client (authority, "/test/realm2/t1d", false);
277
278 c1.start(); c2.start();
279 TestHttpServer.waitForCondition ("cond1");
280 c3.start(); c4.start();
281 c1.join(); c2.join(); c3.join(); c4.join();
282
283 int f = auth.getCount();
284 if (f != 2) {
285 except ("Authenticator was called "+f+" times. Should be 2");
286 }
287 if (error) {
288 except ("error occurred");
289 }
290
291 auth.resetCount();
292 System.out.println ("T2");
293
294 c5 = new Client (authority, "/test/realm3/t2a", true);
295 c6 = new Client (authority, "/test/realm3/t2b", false);
296 c5.start ();
297 TestHttpServer.waitForCondition ("T2cond1");
298 c6.start ();
299 c5.join(); c6.join();
300
301 f = auth.getCount();
302 if (f != redirects+1) {
303 except ("Authenticator was called "+f+" times. Should be: " + redirects+1);
304 }
305 if (error) {
306 except ("error occurred");
307 }
308 }
309
310 static void doProxyTests (String authority) throws Exception {
311 System.out.println ("Doing Proxy tests");
312 c7 = new Client (authority, "/test/realm4/t3a", false);
313 c8 = new Client (authority, "/test/realm4/t3b", false);
314 c9 = new Client (authority, "/test/realm4/t3c", false);
315 c7.start ();
316 TestHttpServer.waitForCondition ("T3cond1");
317 c8.start ();
318 c9.start ();
319 c7.join(); c8.join(); c9.join();
320
321 int f = auth.getCount();
322 if (f != 2) {
323 except ("Authenticator was called "+f+" times. Should be: " + 2);
324 }
325 if (error) {
326 except ("error occurred");
327 }
328 }
329
330 public static void main (String[] args) throws Exception {
331 System.setProperty ("http.maxRedirects", Integer.toString (redirects));
332 System.setProperty ("http.auth.serializeRequests", "true");
333 Authenticator.setDefault (auth);
334 boolean proxy = args[0].equals ("proxy");
335 try {
336 server = new TestHttpServer (new CallBack(), 10, 1, 0);
337 System.out.println ("Server: listening on port: " + server.getLocalPort());
338 if (proxy) {
339 System.setProperty ("http.proxyHost", "localhost");
340 System.setProperty ("http.proxyPort",Integer.toString(server.getLocalPort()));
341 doProxyTests ("www.foo.com");
342 } else {
343 doServerTests ("localhost:"+server.getLocalPort());
344 }
345 server.terminate();
346
347 } catch (Exception e) {
348 if (server != null) {
349 server.terminate();
350 }
351 throw e;
352 }
353 }
354
355 static void pause (int millis) {
356 try {
|