test/jdk/java/net/Authenticator/AuthNPETest.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 4662246
    31  * @bug 4662246
    31  * @summary  REGRESSION: plugin 14x client authentication dialog returns NullPointerException
    32  * @summary  REGRESSION: plugin 14x client authentication dialog returns NullPointerException
       
    33  * @library /test/lib
       
    34  * @run main/othervm AuthNPETest
       
    35  * @run main/othervm -Djava.net.preferIPv6Addresses=true AuthNPETest
    32  */
    36  */
    33 
    37 
    34 public class AuthNPETest {
    38 public class AuthNPETest {
    35 
    39 
    36     static class BasicServer extends Thread {
    40     static class BasicServer extends Thread {
    51             "Server: Apache/1.3.14 (Unix)\r\n" +
    55             "Server: Apache/1.3.14 (Unix)\r\n" +
    52             "Connection: close\r\n" +
    56             "Connection: close\r\n" +
    53             "Content-Type: text/html; charset=iso-8859-1\r\n" +
    57             "Content-Type: text/html; charset=iso-8859-1\r\n" +
    54             "Content-Length: 10\r\n\r\n";
    58             "Content-Length: 10\r\n\r\n";
    55 
    59 
    56         BasicServer (ServerSocket s) {
    60         BasicServer(ServerSocket s) {
    57             server = s;
    61             server = s;
    58         }
    62         }
    59 
    63 
    60         void readAll (Socket s) throws IOException {
    64         void readAll(Socket s) throws IOException {
    61             byte[] buf = new byte [128];
    65             byte[] buf = new byte [128];
    62             InputStream is = s.getInputStream ();
    66             InputStream is = s.getInputStream();
    63             s.setSoTimeout(1000);
    67             s.setSoTimeout(1000);
    64             try {
    68             try {
    65                 while (is.read(buf) > 0) ;
    69                 while (is.read(buf) > 0) ;
    66             } catch (SocketTimeoutException x) { }
    70             } catch (SocketTimeoutException x) { }
    67         }
    71         }
    68 
    72 
    69         public void run () {
    73         public void run() {
    70             try {
    74             try {
    71                 System.out.println ("Server 1: accept");
    75                 System.out.println("Server 1: accept");
    72                 s = server.accept ();
    76                 s = server.accept();
    73                 System.out.println ("accepted");
    77                 System.out.println("accepted");
    74                 os = s.getOutputStream();
    78                 os = s.getOutputStream();
    75                 os.write (reply1.getBytes());
    79                 os.write(reply1.getBytes());
    76                 readAll (s);
    80                 readAll(s);
    77                 s.close ();
    81                 s.close();
    78 
    82 
    79                 System.out.println ("Server 2: accept");
    83                 System.out.println("Server 2: accept");
    80                 s = server.accept ();
    84                 s = server.accept();
    81                 System.out.println ("accepted");
    85                 System.out.println("accepted");
    82                 os = s.getOutputStream();
    86                 os = s.getOutputStream();
    83                 os.write ((reply2+"HelloWorld").getBytes());
    87                 os.write((reply2+"HelloWorld").getBytes());
    84                 readAll (s);
    88                 readAll(s);
    85                 s.close ();
    89                 s.close();
    86 
    90 
    87             }
    91             }
    88             catch (Exception e) {
    92             catch (Exception e) {
    89                 System.out.println (e);
    93                 System.out.println (e);
    90             }
    94             }
    91             finished ();
    95             finished();
    92         }
    96         }
    93 
    97 
    94         public synchronized void finished () {
    98         public synchronized void finished() {
    95             notifyAll();
    99             notifyAll();
    96         }
   100         }
    97 
   101 
    98     }
   102     }
    99 
   103 
   100     static class MyAuthenticator extends Authenticator {
   104     static class MyAuthenticator extends Authenticator {
   101 
   105 
   102         MyAuthenticator () {
   106         MyAuthenticator() {
   103             super ();
   107             super();
   104         }
   108         }
   105 
   109 
   106         int count = 0;
   110         int count = 0;
   107 
   111 
   108         public PasswordAuthentication getPasswordAuthentication ()
   112         public PasswordAuthentication getPasswordAuthentication()
   109             {
   113             {
   110             count ++;
   114             count++;
   111             System.out.println ("Auth called");
   115             System.out.println("Auth called");
   112             return (new PasswordAuthentication ("user", "passwordNotCheckedAnyway".toCharArray()));
   116             return (new PasswordAuthentication("user", "passwordNotCheckedAnyway".toCharArray()));
   113         }
   117         }
   114 
   118 
   115         public int getCount () {
   119         public int getCount() {
   116             return (count);
   120             return count;
   117         }
   121         }
   118     }
   122     }
   119 
   123 
   120 
   124 
   121     static void read (InputStream is) throws IOException {
   125     static void read(InputStream is) throws IOException {
   122         int c;
   126         int c;
   123         System.out.println ("reading");
   127         System.out.println("reading");
   124         while ((c=is.read()) != -1) {
   128         while ((c=is.read()) != -1) {
   125             System.out.write (c);
   129             System.out.write(c);
   126         }
   130         }
   127         System.out.println ("");
   131         System.out.println("");
   128         System.out.println ("finished reading");
   132         System.out.println("finished reading");
   129     }
   133     }
   130 
   134 
   131     public static void main (String args[]) throws Exception {
   135     public static void main(String args[]) throws Exception {
   132         MyAuthenticator auth = new MyAuthenticator ();
   136         MyAuthenticator auth = new MyAuthenticator();
   133         Authenticator.setDefault (auth);
   137         Authenticator.setDefault(auth);
   134         ServerSocket ss = new ServerSocket (0);
   138         InetAddress loopback = InetAddress.getLoopbackAddress();
   135         int port = ss.getLocalPort ();
   139         ServerSocket ss = new ServerSocket();
   136         BasicServer server = new BasicServer (ss);
   140         ss.bind(new InetSocketAddress(loopback, 0));
       
   141         int port = ss.getLocalPort();
       
   142         BasicServer server = new BasicServer(ss);
   137         synchronized (server) {
   143         synchronized (server) {
   138             server.start();
   144             server.start();
   139             System.out.println ("client 1");
   145             System.out.println ("client 1");
   140             URL url = new URL ("http://localhost:"+port);
   146             URL url = URIBuilder.newBuilder()
   141             URLConnection urlc = url.openConnection ();
   147                 .scheme("http")
   142             InputStream is = urlc.getInputStream ();
   148                 .loopback()
   143             read (is);
   149                 .port(port)
       
   150                 .toURL();
       
   151             URLConnection urlc = url.openConnection(Proxy.NO_PROXY);
       
   152             InputStream is = urlc.getInputStream();
       
   153             read(is);
   144             is.close();
   154             is.close();
   145         }
   155         }
   146     }
   156     }
   147 }
   157 }