Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/Authenticator/B8034170.java
38812 views
/*1* Copyright (c) 2014, 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 803417030* @summary Digest authentication interop issue31* @run main/othervm B8034170 unquoted32* @run main/othervm -Dhttp.auth.digest.quoteParameters=true B8034170 quoted33*/3435public class B8034170 {3637static boolean expectQuotes;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: Digest realm=\""+realm+"\", qop=\"auth\"" +51", nonce=\"8989de95ea2402b64d73cecdb15da255\"" +52", opaque=\"bbfb4c9ee92ddccc73521c3e6e841ba2\"\r\n\r\n";5354String OKreply = "HTTP/1.1 200 OK\r\n"+55"Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +56"Server: Apache/1.3.14 (Unix)\r\n" +57"Connection: close\r\n" +58"Content-Type: text/plain; charset=iso-8859-1\r\n" +59"Content-Length: 10\r\n\r\n";6061String ERRreply = "HTTP/1.1 500 Internal server error\r\n"+62"Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +63"Server: Apache/1.3.14 (Unix)\r\n" +64"Connection: close\r\n" +65"Content-Length: 0\r\n\r\n";6667BasicServer (ServerSocket s) {68server = s;69}7071int readAll (Socket s, byte[] buf) throws IOException {72int pos = 0;73InputStream is = s.getInputStream ();74// wait two seconds for request, as client doesn't close75// the connection76s.setSoTimeout(2000);77try {78int n;79while ((n=is.read(buf, pos, buf.length-pos)) > 0)80pos +=n;81} catch (SocketTimeoutException x) { }82return pos;83}8485public void run () {86byte[] buf = new byte[5000];87try {88System.out.println ("Server 1: accept");89s = server.accept ();90System.out.println ("accepted");91os = s.getOutputStream();92os.write (reply1.getBytes());93readAll (s, buf);94s.close ();9596System.out.println ("Server 2: accept");97s = server.accept ();98System.out.println ("accepted");99os = s.getOutputStream();100int count = readAll (s, buf);101String reply = new String(buf, 0, count);102103boolean error;104105if (expectQuotes) {106error = false;107if (!reply.contains("qop=\"auth\"")) {108System.out.println ("Expecting quoted qop. Not found");109error = true;110}111if (!reply.contains("algorithm=\"MD5\"")) {112System.out.println ("Expecting quoted algorithm. Not found");113error = true;114}115} else {116error = false;117if (!reply.contains("qop=auth")) {118System.out.println ("Expecting unquoted qop. Not found");119error = true;120}121if (!reply.contains("algorithm=MD5")) {122System.out.println ("Expecting unquoted algorithm. Not found");123error = true;124}125}126if (error) {127os.write(ERRreply.getBytes());128os.flush();129s.close();130} else {131os.write((OKreply+"HelloWorld").getBytes());132os.flush();133s.close();134}135}136catch (Exception e) {137System.out.println (e);138}139finished ();140}141142public synchronized void finished () {143notifyAll();144}145146}147148static class MyAuthenticator3 extends Authenticator {149PasswordAuthentication pw;150MyAuthenticator3 () {151super ();152pw = new PasswordAuthentication ("user", "passwordNotCheckedAnyway".toCharArray());153}154155public PasswordAuthentication getPasswordAuthentication ()156{157System.out.println ("Auth called");158return pw;159}160}161162163static void read (InputStream is) throws IOException {164int c;165System.out.println ("reading");166while ((c=is.read()) != -1) {167System.out.write (c);168}169System.out.println ("");170System.out.println ("finished reading");171}172173public static void main (String args[]) throws Exception {174expectQuotes = args[0].equals("quoted");175176MyAuthenticator3 auth = new MyAuthenticator3 ();177Authenticator.setDefault (auth);178ServerSocket ss = new ServerSocket (0);179int port = ss.getLocalPort ();180BasicServer server = new BasicServer (ss);181synchronized (server) {182server.start();183System.out.println ("client 1");184URL url = new URL ("http://localhost:"+port+"/d1/d2/d3/foo.html");185URLConnection urlc = url.openConnection ();186InputStream is = urlc.getInputStream ();187read (is);188is.close ();189}190}191}192193194