jdk/test/java/net/Authenticator/B4769350.java
changeset 22976 90ec9fec7c7f
parent 21320 0a56bf0c2390
child 38883 d5de564f8089
equal deleted inserted replaced
22975:1a8002758a3d 22976:90ec9fec7c7f
    64             this.authority = authority;
    64             this.authority = authority;
    65             this.path = path;
    65             this.path = path;
    66             this.allowerror = allowerror;
    66             this.allowerror = allowerror;
    67         }
    67         }
    68 
    68 
       
    69         @Override
    69         public void run () {
    70         public void run () {
    70             try {
    71             try {
    71                 URI u = new URI ("http", authority, path, null, null);
    72                 URI u = new URI ("http", authority, path, null, null);
    72                 URL url = u.toURL();
    73                 URL url = u.toURL();
    73                 URLConnection urlc = url.openConnection();
    74                 URLConnection urlc = url.openConnection();
    74                 InputStream is = urlc.getInputStream();
    75                 try (InputStream is = urlc.getInputStream()) {
    75                 read (is);
    76                     read (is);
    76                 is.close();
    77                 }
    77             } catch (URISyntaxException  e) {
    78             } catch (URISyntaxException  e) {
    78                 System.out.println (e);
    79                 System.out.println (e);
    79                 error = true;
    80                 error = true;
    80             } catch (IOException e) {
    81             } catch (IOException e) {
    81                 if (!allowerror) {
    82                 if (!allowerror) {
    89     }
    90     }
    90 
    91 
    91     class Server implements AutoCloseable {
    92     class Server implements AutoCloseable {
    92         HttpServer server;
    93         HttpServer server;
    93         Executor executor;
    94         Executor executor;
    94         CyclicBarrier t1Cond1;
       
    95         CyclicBarrier t1Cond2;
       
    96 
    95 
    97         public String getAddress() {
    96         public String getAddress() {
    98             return server.getAddress().getHostName();
    97             return server.getAddress().getHostName();
    99         }
    98         }
   100 
    99 
   124                     new AuthenticationHandlerT3a());
   123                     new AuthenticationHandlerT3a());
   125             server.createContext("/test/realm4/t3b",
   124             server.createContext("/test/realm4/t3b",
   126                     new AuthenticationHandlerT3bc());
   125                     new AuthenticationHandlerT3bc());
   127             server.createContext("/test/realm4/t3c",
   126             server.createContext("/test/realm4/t3c",
   128                     new AuthenticationHandlerT3bc());
   127                     new AuthenticationHandlerT3bc());
   129             t1Cond1 = new CyclicBarrier(2);
   128             t1Cond1 = new CyclicBarrier(3);
   130             t1Cond2 = new CyclicBarrier(2);
       
   131             server.start();
   129             server.start();
   132         }
   130         }
   133 
   131 
   134         public int getPort() {
   132         public int getPort() {
   135             return server.getAddress().getPort();
   133             return server.getAddress().getPort();
   136         }
   134         }
   137 
   135 
       
   136         @Override
   138         public void close() {
   137         public void close() {
   139             if (executor != null)
   138             if (executor != null)
   140                 ((ExecutorService)executor).shutdownNow();
   139                 ((ExecutorService)executor).shutdownNow();
   141             if (server != null)
   140             if (server != null)
   142                 server.stop(0);
   141                 server.stop(0);
   161                             AuthenticationHandler.errorReply(exchange,
   160                             AuthenticationHandler.errorReply(exchange,
   162                                     "Basic realm=\"realm1\"");
   161                                     "Basic realm=\"realm1\"");
   163                             break;
   162                             break;
   164                         case 1:
   163                         case 1:
   165                             t1Cond1.await();
   164                             t1Cond1.await();
   166                             t1cond2latch.await();
       
   167                             AuthenticationHandler.okReply(exchange);
   165                             AuthenticationHandler.okReply(exchange);
   168                             break;
   166                             break;
   169                         default:
   167                         default:
   170                             System.out.println ("Unexpected request");
   168                             System.out.println ("Unexpected request");
   171                     }
   169                     }
   190                             AuthenticationHandler.errorReply(exchange,
   188                             AuthenticationHandler.errorReply(exchange,
   191                                     "Basic realm=\"realm2\"");
   189                                     "Basic realm=\"realm2\"");
   192                             break;
   190                             break;
   193                         case 1:
   191                         case 1:
   194                             t1Cond1.await();
   192                             t1Cond1.await();
   195                             t1cond1latch.countDown();
       
   196                             t1cond2latch.await();
       
   197                             AuthenticationHandler.okReply(exchange);
   193                             AuthenticationHandler.okReply(exchange);
   198                             break;
   194                             break;
   199                         default:
   195                         default:
   200                             System.out.println ("Unexpected request");
   196                             System.out.println ("Unexpected request");
   201                     }
   197                     }
   214                 count++;
   210                 count++;
   215                 switch(count) {
   211                 switch(count) {
   216                     case 0:
   212                     case 0:
   217                         AuthenticationHandler.errorReply(exchange,
   213                         AuthenticationHandler.errorReply(exchange,
   218                                 "Basic realm=\"realm1\"");
   214                                 "Basic realm=\"realm1\"");
   219                         try {
       
   220                             t1Cond2.await();
       
   221                         } catch (InterruptedException |
       
   222                                  BrokenBarrierException e)
       
   223                         {
       
   224                             throw new RuntimeException(e);
       
   225                         }
       
   226                         break;
   215                         break;
   227                     case 1:
   216                     case 1:
   228                         AuthenticationHandler.okReply(exchange);
   217                         AuthenticationHandler.okReply(exchange);
   229                         break;
   218                         break;
   230                     default:
   219                     default:
   242                 count++;
   231                 count++;
   243                 switch(count) {
   232                 switch(count) {
   244                     case 0:
   233                     case 0:
   245                         AuthenticationHandler.errorReply(exchange,
   234                         AuthenticationHandler.errorReply(exchange,
   246                                 "Basic realm=\"realm2\"");
   235                                 "Basic realm=\"realm2\"");
   247                         try {
       
   248                             t1Cond2.await();
       
   249                         } catch (InterruptedException |
       
   250                                  BrokenBarrierException e)
       
   251                         {
       
   252                             throw new RuntimeException(e);
       
   253                         }
       
   254                         t1cond2latch.countDown();
       
   255                         break;
   236                         break;
   256                     case 1:
   237                     case 1:
   257                         AuthenticationHandler.okReply(exchange);
   238                         AuthenticationHandler.okReply(exchange);
   258                         break;
   239                         break;
   259                     default:
   240                     default:
   375 
   356 
   376         static void okReply (HttpExchange exchange) throws IOException {
   357         static void okReply (HttpExchange exchange) throws IOException {
   377             exchange.getResponseHeaders().add("Connection", "close");
   358             exchange.getResponseHeaders().add("Connection", "close");
   378             String response = "Hello .";
   359             String response = "Hello .";
   379             exchange.sendResponseHeaders(200, response.getBytes().length);
   360             exchange.sendResponseHeaders(200, response.getBytes().length);
   380             OutputStream os = exchange.getResponseBody();
   361             try (OutputStream os = exchange.getResponseBody()) {
   381             os.write(response.getBytes());
   362                 os.write(response.getBytes());
   382             os.close();
   363             }
   383             exchange.close();
   364             exchange.close();
   384         }
   365         }
   385     }
   366     }
   386 
   367 
   387     static Server server;
   368     static Server server;
   389 
   370 
   390     static int redirects = 4;
   371     static int redirects = 4;
   391 
   372 
   392     static Client c1,c2,c3,c4,c5,c6,c7,c8,c9;
   373     static Client c1,c2,c3,c4,c5,c6,c7,c8,c9;
   393 
   374 
   394     static CountDownLatch t1cond1latch;
       
   395     static CountDownLatch t1cond2latch;
       
   396     static CountDownLatch t2condlatch;
   375     static CountDownLatch t2condlatch;
   397     static CountDownLatch t3cond1;
   376     static CountDownLatch t3cond1;
       
   377     static CyclicBarrier t1Cond1;
   398 
   378 
   399     static void doServerTests (String authority, Server server) throws Exception
   379     static void doServerTests (String authority, Server server) throws Exception
   400     {
   380     {
   401         System.out.println ("Doing Server tests");
   381         System.out.println ("Doing Server tests");
   402         System.out.println ("T1");
   382         System.out.println ("T1");
   403         c1 = new Client (authority, "/test/realm1/t1a", false);
   383         c1 = new Client (authority, "/test/realm1/t1a", false);
   404         c2 = new Client (authority, "/test/realm2/t1b", false);
   384         c2 = new Client (authority, "/test/realm2/t1b", false);
   405         c3 = new Client (authority, "/test/realm1/t1c", false);
   385         c3 = new Client (authority, "/test/realm1/t1c", false);
   406         c4 = new Client (authority, "/test/realm2/t1d", false);
   386         c4 = new Client (authority, "/test/realm2/t1d", false);
   407         t1cond1latch = new CountDownLatch(1);
       
   408         t1cond2latch = new CountDownLatch(1);
       
   409         c1.start(); c2.start();
   387         c1.start(); c2.start();
   410         t1cond1latch.await();
   388         t1Cond1.await();
   411         c3.start(); c4.start();
   389         c3.start(); c4.start();
   412         c1.join(); c2.join(); c3.join(); c4.join();
   390         c1.join(); c2.join(); c3.join(); c4.join();
   413 
   391 
   414         int f = auth.getCount();
   392         int f = auth.getCount();
   415         if (f != 2) {
   393         if (f != 2) {