test/jdk/java/net/Authenticator/BasicTest4.java
changeset 55399 46049b8a5658
parent 47216 71c04702a3d5
equal deleted inserted replaced
55398:e53ec3b362f4 55399:46049b8a5658
     1 /*
     1 /*
     2  * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    22  */
    22  */
    23 
    23 
    24 import java.io.*;
    24 import java.io.*;
    25 import java.net.*;
    25 import java.net.*;
    26 import java.util.*;
    26 import java.util.*;
       
    27 import jdk.test.lib.net.URIBuilder;
    27 
    28 
    28 /**
    29 /**
    29  * @test
    30  * @test
    30  * @bug 4623722
    31  * @bug 4623722
    31  * @summary  performance hit for Basic Authentication
    32  * @summary  performance hit for Basic Authentication
       
    33  * @library /test/lib
       
    34  * @run main/othervm BasicTest4
       
    35  * @run main/othervm -Djava.net.preferIPv6Addresses=true BasicTest4
    32  */
    36  */
    33 
    37 
    34 public class BasicTest4 {
    38 public class BasicTest4 {
    35 
    39 
    36     static class BasicServer extends Thread {
    40     static class BasicServer extends Thread {
    57             server = s;
    61             server = s;
    58         }
    62         }
    59 
    63 
    60         static boolean checkFor (InputStream in, char[] seq) throws IOException {
    64         static boolean checkFor (InputStream in, char[] seq) throws IOException {
    61             System.out.println ("checkfor");
    65             System.out.println ("checkfor");
       
    66             StringBuilder message = new StringBuilder();
    62             try {
    67             try {
    63                 int i=0, count=0;
    68                 int i=0, count=0;
    64                 while (true) {
    69                 while (true) {
    65                     int c = in.read();
    70                     int c = in.read();
    66                     if (c == -1)
    71                     if (c == -1) {
       
    72                         System.out.println(new String(seq) + " not found in \n<<"
       
    73                                            + message + ">>");
    67                         return false;
    74                         return false;
       
    75                     }
       
    76                     message.append((char)c);
    68                     count++;
    77                     count++;
    69                     if (c == seq[i]) {
    78                     if (c == seq[i]) {
    70                         i++;
    79                         i++;
    71                         if (i == seq.length)
    80                         if (i == seq.length)
    72                             return true;
    81                             return true;
    75                         i = 0;
    84                         i = 0;
    76                     }
    85                     }
    77                 }
    86                 }
    78             }
    87             }
    79             catch (SocketTimeoutException e) {
    88             catch (SocketTimeoutException e) {
       
    89                 System.out.println("checkFor: " + e);
    80                 return false;
    90                 return false;
    81             }
    91             }
    82         }
    92         }
    83 
    93 
    84         boolean success = false;
    94         boolean success = false;
   192     }
   202     }
   193 
   203 
   194     public static void main (String args[]) throws Exception {
   204     public static void main (String args[]) throws Exception {
   195         MyAuthenticator auth = new MyAuthenticator ();
   205         MyAuthenticator auth = new MyAuthenticator ();
   196         Authenticator.setDefault (auth);
   206         Authenticator.setDefault (auth);
   197         ServerSocket ss = new ServerSocket (0);
   207         InetAddress loopback = InetAddress.getLoopbackAddress();
       
   208         ServerSocket ss = new ServerSocket();
       
   209         ss.bind(new InetSocketAddress(loopback, 0));
   198         int port = ss.getLocalPort ();
   210         int port = ss.getLocalPort ();
   199         BasicServer server = new BasicServer (ss);
   211         BasicServer server = new BasicServer (ss);
   200         synchronized (server) {
   212         synchronized (server) {
   201             server.start();
   213             server.start();
   202             System.out.println ("client 1");
   214             System.out.println ("client 1");
   203             URL url = new URL ("http://localhost:"+port+"/d1/d3/foo.html");
   215             String base = URIBuilder.newBuilder()
   204             URLConnection urlc = url.openConnection ();
   216                 .scheme("http")
       
   217                 .loopback()
       
   218                 .port(port)
       
   219                 .path("/d1/")
       
   220                 .build()
       
   221                 .toString();
       
   222             System.out.println("Base URL: " + base);
       
   223             URL url = new URL (base + "d3/foo.html");
       
   224             URLConnection urlc = url.openConnection(Proxy.NO_PROXY);
   205             InputStream is = urlc.getInputStream ();
   225             InputStream is = urlc.getInputStream ();
   206             read (is);
   226             read (is);
   207             System.out.println ("client 2");
   227             System.out.println ("client 2");
   208             url = new URL ("http://localhost:"+port+"/d1/d2/bar.html");
   228             url = new URL (base + "d2/bar.html");
   209             urlc = url.openConnection ();
   229             urlc = url.openConnection(Proxy.NO_PROXY);
   210             is = urlc.getInputStream ();
   230             is = urlc.getInputStream ();
   211             System.out.println ("client 3");
   231             System.out.println ("client 3");
   212             url = new URL ("http://localhost:"+port+"/d1/d4/foobar.html");
   232             url = new URL (base + "d4/foobar.html");
   213             urlc = url.openConnection ();
   233             urlc = url.openConnection(Proxy.NO_PROXY);
   214             is = urlc.getInputStream ();
   234             is = urlc.getInputStream ();
   215             read (is);
   235             read (is);
   216             server.wait ();
   236             server.wait ();
   217             if (!server.success) {
   237             if (!server.success) {
   218                 throw new RuntimeException ("3rd request did not use pre-emptive authorization");
   238                 throw new RuntimeException ("3rd request did not use pre-emptive authorization");