Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/Authenticator/AuthNPETest.java
38811 views
/*1* Copyright (c) 2002, 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.*;2627/**28* @test29* @bug 466224630* @summary REGRESSION: plugin 14x client authentication dialog returns NullPointerException31*/3233public class AuthNPETest {3435static class BasicServer extends Thread {3637ServerSocket server;3839Socket s;40InputStream is;41OutputStream os;4243static final String realm = "wallyworld";4445String reply1 = "HTTP/1.1 401 Unauthorized\r\n"+46"WWW-Authenticate: Basic realm=\""+realm+"\"\r\n\r\n";4748String reply2 = "HTTP/1.1 200 OK\r\n"+49"Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +50"Server: Apache/1.3.14 (Unix)\r\n" +51"Connection: close\r\n" +52"Content-Type: text/html; charset=iso-8859-1\r\n" +53"Content-Length: 10\r\n\r\n";5455BasicServer (ServerSocket s) {56server = s;57}5859void readAll (Socket s) throws IOException {60byte[] buf = new byte [128];61InputStream is = s.getInputStream ();62s.setSoTimeout(1000);63try {64while (is.read(buf) > 0) ;65} catch (SocketTimeoutException x) { }66}6768public void run () {69try {70System.out.println ("Server 1: accept");71s = server.accept ();72System.out.println ("accepted");73os = s.getOutputStream();74os.write (reply1.getBytes());75readAll (s);76s.close ();7778System.out.println ("Server 2: accept");79s = server.accept ();80System.out.println ("accepted");81os = s.getOutputStream();82os.write ((reply2+"HelloWorld").getBytes());83readAll (s);84s.close ();8586}87catch (Exception e) {88System.out.println (e);89}90finished ();91}9293public synchronized void finished () {94notifyAll();95}9697}9899static class MyAuthenticator extends Authenticator {100101MyAuthenticator () {102super ();103}104105int count = 0;106107public PasswordAuthentication getPasswordAuthentication ()108{109count ++;110System.out.println ("Auth called");111return (new PasswordAuthentication ("user", "passwordNotCheckedAnyway".toCharArray()));112}113114public int getCount () {115return (count);116}117}118119120static void read (InputStream is) throws IOException {121int c;122System.out.println ("reading");123while ((c=is.read()) != -1) {124System.out.write (c);125}126System.out.println ("");127System.out.println ("finished reading");128}129130public static void main (String args[]) throws Exception {131MyAuthenticator auth = new MyAuthenticator ();132Authenticator.setDefault (auth);133ServerSocket ss = new ServerSocket (0);134int port = ss.getLocalPort ();135BasicServer server = new BasicServer (ss);136synchronized (server) {137server.start();138System.out.println ("client 1");139URL url = new URL ("http://localhost:"+port);140URLConnection urlc = url.openConnection ();141InputStream is = urlc.getInputStream ();142read (is);143is.close();144}145}146}147148149