Path: blob/master/test/jdk/java/net/Authenticator/B8034170.java
41149 views
/*1* Copyright (c) 2014, 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 803417031* @summary Digest authentication interop issue32* @library /test/lib33* @run main/othervm B8034170 unquoted34* @run main/othervm -Dhttp.auth.digest.quoteParameters=true B8034170 quoted35* @run main/othervm -Djava.net.preferIPv6Addresses=true B8034170 unquoted36*/3738public class B8034170 {3940static boolean expectQuotes;4142static class BasicServer extends Thread {4344ServerSocket server;4546Socket s;47InputStream is;48OutputStream os;4950static final String realm = "wallyworld";5152String reply1 = "HTTP/1.1 401 Unauthorized\r\n"+53"WWW-Authenticate: Digest realm=\""+realm+"\", qop=\"auth\"" +54", nonce=\"8989de95ea2402b64d73cecdb15da255\"" +55", opaque=\"bbfb4c9ee92ddccc73521c3e6e841ba2\"\r\n\r\n";5657String OKreply = "HTTP/1.1 200 OK\r\n"+58"Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +59"Server: Apache/1.3.14 (Unix)\r\n" +60"Connection: close\r\n" +61"Content-Type: text/plain; charset=iso-8859-1\r\n" +62"Content-Length: 10\r\n\r\n";6364String ERRreply = "HTTP/1.1 500 Internal server error\r\n"+65"Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +66"Server: Apache/1.3.14 (Unix)\r\n" +67"Connection: close\r\n" +68"Content-Length: 0\r\n\r\n";6970BasicServer (ServerSocket s) {71server = s;72}7374int readAll (Socket s, byte[] buf) throws IOException {75int pos = 0;76InputStream is = s.getInputStream ();77// wait two seconds for request, as client doesn't close78// the connection79s.setSoTimeout(2000);80try {81int n;82while ((n=is.read(buf, pos, buf.length-pos)) > 0)83pos +=n;84} catch (SocketTimeoutException x) { }85return pos;86}8788public void run () {89byte[] buf = new byte[5000];90try {91System.out.println ("Server 1: accept");92s = server.accept ();93System.out.println ("accepted");94os = s.getOutputStream();95os.write (reply1.getBytes());96readAll (s, buf);97s.close ();9899System.out.println ("Server 2: accept");100s = server.accept ();101System.out.println ("accepted");102os = s.getOutputStream();103int count = readAll (s, buf);104String reply = new String(buf, 0, count);105106boolean error;107108if (expectQuotes) {109error = false;110if (!reply.contains("qop=\"auth\"")) {111System.out.println ("Expecting quoted qop. Not found");112error = true;113}114if (!reply.contains("algorithm=\"MD5\"")) {115System.out.println ("Expecting quoted algorithm. Not found");116error = true;117}118} else {119error = false;120if (!reply.contains("qop=auth")) {121System.out.println ("Expecting unquoted qop. Not found");122error = true;123}124if (!reply.contains("algorithm=MD5")) {125System.out.println ("Expecting unquoted algorithm. Not found");126error = true;127}128}129if (error) {130os.write(ERRreply.getBytes());131os.flush();132s.close();133} else {134os.write((OKreply+"HelloWorld").getBytes());135os.flush();136s.close();137}138}139catch (Exception e) {140System.out.println (e);141}142finished ();143}144145public synchronized void finished () {146notifyAll();147}148149}150151static class MyAuthenticator3 extends Authenticator {152PasswordAuthentication pw;153MyAuthenticator3 () {154super ();155pw = new PasswordAuthentication ("user", "passwordNotCheckedAnyway".toCharArray());156}157158public PasswordAuthentication getPasswordAuthentication ()159{160System.out.println ("Auth called");161return pw;162}163}164165166static void read (InputStream is) throws IOException {167int c;168System.out.println ("reading");169while ((c=is.read()) != -1) {170System.out.write (c);171}172System.out.println ("");173System.out.println ("finished reading");174}175176public static void main (String args[]) throws Exception {177expectQuotes = args[0].equals("quoted");178179MyAuthenticator3 auth = new MyAuthenticator3 ();180Authenticator.setDefault (auth);181InetAddress loopback = InetAddress.getLoopbackAddress();182ServerSocket ss = new ServerSocket();183ss.bind(new InetSocketAddress(loopback, 0));184int port = ss.getLocalPort ();185BasicServer server = new BasicServer (ss);186synchronized (server) {187server.start();188System.out.println ("client 1");189URL url = URIBuilder.newBuilder()190.scheme("http")191.loopback()192.port(port)193.path("/d1/d2/d3/foo.html")194.toURL();195URLConnection urlc = url.openConnection(Proxy.NO_PROXY);196InputStream is = urlc.getInputStream ();197read (is);198is.close ();199}200}201}202203204