test/jdk/java/net/HttpURLConnection/SetAuthenticator/HTTPTest.java
branchdatagramsocketimpl-branch
changeset 58678 9cf78a70fa4f
parent 52387 8c0b1894d524
child 58679 9c3209ff7550
equal deleted inserted replaced
58677:13588c901957 58678:9cf78a70fa4f
     1 /*
     1 /*
     2  * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2016, 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.
    38 import javax.net.ssl.HostnameVerifier;
    38 import javax.net.ssl.HostnameVerifier;
    39 import javax.net.ssl.HttpsURLConnection;
    39 import javax.net.ssl.HttpsURLConnection;
    40 import javax.net.ssl.SSLContext;
    40 import javax.net.ssl.SSLContext;
    41 import javax.net.ssl.SSLSession;
    41 import javax.net.ssl.SSLSession;
    42 import jdk.test.lib.net.SimpleSSLContext;
    42 import jdk.test.lib.net.SimpleSSLContext;
       
    43 import static java.net.Proxy.NO_PROXY;
    43 
    44 
    44 /*
    45 /*
    45  * @test
    46  * @test
    46  * @bug 8169415
    47  * @bug 8169415
    47  * @library /test/lib
    48  * @library /test/lib
    87         // authenticator from the server side.
    88         // authenticator from the server side.
    88         private final ThreadLocal<Boolean> skipCount = new ThreadLocal<>();
    89         private final ThreadLocal<Boolean> skipCount = new ThreadLocal<>();
    89         // count will be incremented every time getPasswordAuthentication()
    90         // count will be incremented every time getPasswordAuthentication()
    90         // is called from the client side.
    91         // is called from the client side.
    91         final AtomicInteger count = new AtomicInteger();
    92         final AtomicInteger count = new AtomicInteger();
    92 
    93         private final String name;
    93         public HttpTestAuthenticator(String realm, String username) {
    94 
       
    95         public HttpTestAuthenticator(String name, String realm, String username) {
       
    96             this.name = name;
    94             this.realm = realm;
    97             this.realm = realm;
    95             this.username = username;
    98             this.username = username;
    96         }
    99         }
    97 
   100 
    98         @Override
   101         @Override
    99         protected PasswordAuthentication getPasswordAuthentication() {
   102         protected PasswordAuthentication getPasswordAuthentication() {
   100             if (skipCount.get() == null || skipCount.get().booleanValue() == false) {
   103             if (skipCount.get() == null || skipCount.get().booleanValue() == false) {
   101                 System.out.println("Authenticator called: " + count.incrementAndGet());
   104                 System.out.println("Authenticator " + name + " called: " + count.incrementAndGet());
   102             }
   105             }
   103             return new PasswordAuthentication(getUserName(),
   106             return new PasswordAuthentication(getUserName(),
   104                     new char[] {'b','a','r'});
   107                     new char[] {'b','a','r'});
   105         }
   108         }
   106 
   109 
   116                 }
   119                 }
   117             }
   120             }
   118             throw new SecurityException("User unknown: " + user);
   121             throw new SecurityException("User unknown: " + user);
   119         }
   122         }
   120 
   123 
       
   124         @Override
       
   125         public String toString() {
       
   126             return super.toString() + "[name=\"" + name + "\"]";
       
   127         }
       
   128 
   121         public final String getUserName() {
   129         public final String getUserName() {
   122             return username;
   130             return username;
   123         }
   131         }
   124         public final String getRealm() {
   132         public final String getRealm() {
   125             return realm;
   133             return realm;
   126         }
   134         }
   127 
   135 
   128     }
   136     }
   129     public static final HttpTestAuthenticator AUTHENTICATOR;
   137     public static final HttpTestAuthenticator AUTHENTICATOR;
   130     static {
   138     static {
   131         AUTHENTICATOR = new HttpTestAuthenticator("dublin", "foox");
   139         AUTHENTICATOR = new HttpTestAuthenticator("AUTHENTICATOR","dublin", "foox");
   132         Authenticator.setDefault(AUTHENTICATOR);
   140         Authenticator.setDefault(AUTHENTICATOR);
   133     }
   141     }
   134 
   142 
   135     static {
   143     static {
   136         try {
   144         try {
   256     }
   264     }
   257 
   265 
   258     public static URL url(HttpProtocolType protocol, InetSocketAddress address,
   266     public static URL url(HttpProtocolType protocol, InetSocketAddress address,
   259                           String path) throws MalformedURLException {
   267                           String path) throws MalformedURLException {
   260         return new URL(protocol(protocol),
   268         return new URL(protocol(protocol),
   261                        address.getHostString(),
   269                        address.getAddress().getHostAddress(),
   262                        address.getPort(), path);
   270                        address.getPort(), path);
   263     }
   271     }
   264 
   272 
   265     public static Proxy proxy(HTTPTestServer server, HttpAuthType authType) {
   273     public static Proxy proxy(HTTPTestServer server, HttpAuthType authType) {
   266         return (authType == HttpAuthType.PROXY)
   274         if (authType != HttpAuthType.PROXY) return null;
   267                ? new Proxy(Proxy.Type.HTTP, server.getAddress())
   275 
   268                : null;
   276         InetSocketAddress proxyAddress = server.getProxyAddress();
       
   277         if (!proxyAddress.isUnresolved()) {
       
   278             // Forces the proxy to use an unresolved address created
       
   279             // from the actual IP address to avoid using the proxy
       
   280             // address hostname which would result in resolving to
       
   281             // a posibly different address. For instance we want to
       
   282             // avoid cases such as:
       
   283             //    ::1 => "localhost" => 127.0.0.1
       
   284             proxyAddress = InetSocketAddress.
       
   285                 createUnresolved(proxyAddress.getAddress().getHostAddress(),
       
   286                                  proxyAddress.getPort());
       
   287         }
       
   288 
       
   289         return new Proxy(Proxy.Type.HTTP, proxyAddress);
   269     }
   290     }
   270 
   291 
   271     public static HttpURLConnection openConnection(URL url,
   292     public static HttpURLConnection openConnection(URL url,
   272                                                    HttpAuthType authType,
   293                                                    HttpAuthType authType,
   273                                                    Proxy proxy)
   294                                                    Proxy proxy)
   274                                     throws IOException {
   295                                     throws IOException {
   275 
   296 
   276         HttpURLConnection conn = (HttpURLConnection)
   297         HttpURLConnection conn = (HttpURLConnection)
   277                 (authType == HttpAuthType.PROXY
   298                 (authType == HttpAuthType.PROXY
   278                     ? url.openConnection(proxy)
   299                     ? url.openConnection(proxy)
   279                     : url.openConnection());
   300                     : url.openConnection(NO_PROXY));
   280         return conn;
   301         return conn;
   281     }
   302     }
   282 }
   303 }