Path: blob/master/test/jdk/java/net/Authenticator/AuthNPETest.java
41149 views
/*1* Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.*;24import java.net.*;25import java.util.*;26import jdk.test.lib.net.URIBuilder;2728/**29* @test30* @bug 466224631* @summary REGRESSION: plugin 14x client authentication dialog returns NullPointerException32* @library /test/lib33* @run main/othervm AuthNPETest34* @run main/othervm -Djava.net.preferIPv6Addresses=true AuthNPETest35*/3637public class AuthNPETest {3839static class BasicServer extends Thread {4041ServerSocket server;4243Socket s;44InputStream is;45OutputStream os;4647static final String realm = "wallyworld";4849String reply1 = "HTTP/1.1 401 Unauthorized\r\n"+50"WWW-Authenticate: Basic realm=\""+realm+"\"\r\n\r\n";5152String reply2 = "HTTP/1.1 200 OK\r\n"+53"Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +54"Server: Apache/1.3.14 (Unix)\r\n" +55"Connection: close\r\n" +56"Content-Type: text/html; charset=iso-8859-1\r\n" +57"Content-Length: 10\r\n\r\n";5859BasicServer(ServerSocket s) {60server = s;61}6263void readAll(Socket s) throws IOException {64byte[] buf = new byte [128];65InputStream is = s.getInputStream();66s.setSoTimeout(1000);67try {68while (is.read(buf) > 0) ;69} catch (SocketTimeoutException x) { }70}7172public void run() {73try {74System.out.println("Server 1: accept");75s = server.accept();76System.out.println("accepted");77os = s.getOutputStream();78os.write(reply1.getBytes());79readAll(s);80s.close();8182System.out.println("Server 2: accept");83s = server.accept();84System.out.println("accepted");85os = s.getOutputStream();86os.write((reply2+"HelloWorld").getBytes());87readAll(s);88s.close();8990}91catch (Exception e) {92System.out.println (e);93}94finished();95}9697public synchronized void finished() {98notifyAll();99}100101}102103static class MyAuthenticator extends Authenticator {104105MyAuthenticator() {106super();107}108109int count = 0;110111public PasswordAuthentication getPasswordAuthentication()112{113count++;114System.out.println("Auth called");115return (new PasswordAuthentication("user", "passwordNotCheckedAnyway".toCharArray()));116}117118public int getCount() {119return count;120}121}122123124static void read(InputStream is) throws IOException {125int c;126System.out.println("reading");127while ((c=is.read()) != -1) {128System.out.write(c);129}130System.out.println("");131System.out.println("finished reading");132}133134public static void main(String args[]) throws Exception {135MyAuthenticator auth = new MyAuthenticator();136Authenticator.setDefault(auth);137InetAddress loopback = InetAddress.getLoopbackAddress();138ServerSocket ss = new ServerSocket();139ss.bind(new InetSocketAddress(loopback, 0));140int port = ss.getLocalPort();141BasicServer server = new BasicServer(ss);142synchronized (server) {143server.start();144System.out.println ("client 1");145URL url = URIBuilder.newBuilder()146.scheme("http")147.loopback()148.port(port)149.toURL();150URLConnection urlc = url.openConnection(Proxy.NO_PROXY);151InputStream is = urlc.getInputStream();152read(is);153is.close();154}155}156}157158159