Path: blob/master/test/jdk/java/net/Authenticator/B4759514.java
41149 views
/*1* Copyright (c) 2002, 2021, 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*/2223/**24* @test25* @bug 475951426* @library /test/lib27* @run main/othervm B475951428* @run main/othervm -Djava.net.preferIPv6Addresses=true B475951429* @summary Digest Authentication is erroniously quoting the nc value, contrary to RFC 261730*/3132import java.io.IOException;33import java.io.InputStream;34import java.io.PrintWriter;35import java.net.Authenticator;36import java.net.InetAddress;37import java.net.InetSocketAddress;38import java.net.PasswordAuthentication;39import java.net.ProxySelector;40import java.net.URL;41import java.net.URLConnection;42import java.util.concurrent.Executors;4344import com.sun.net.httpserver.HttpExchange;45import com.sun.net.httpserver.HttpHandler;46import com.sun.net.httpserver.HttpServer;47import jdk.test.lib.net.URIBuilder;4849public class B4759514 implements HttpHandler {5051static int count = 0;52static String authstring;5354void errorReply (HttpExchange req, String reply) throws IOException {55req.getResponseHeaders().set("Connection", "close");56req.getResponseHeaders().set("WWW-Authenticate", reply);57req.sendResponseHeaders(401, -1);58}5960void okReply (HttpExchange req) throws IOException {61req.sendResponseHeaders(200, 0);62try(PrintWriter pw = new PrintWriter(req.getResponseBody())) {63pw.print("Hello .");64}65}6667public void handle (HttpExchange req) {68try {69if(req.getRequestHeaders().get("Authorization") != null) {70authstring = req.getRequestHeaders().get("Authorization").get(0);71System.out.println(authstring);72}73switch (count) {74case 0:75errorReply (req, "Digest realm=\"wallyworld\", nonce=\"1234\", domain=\"/\"");76break;77case 1:78int n = authstring.indexOf ("nc=");79if (n != -1) {80if (authstring.charAt (n+3) == '\"') {81req.sendResponseHeaders(400, -1);82break;83}84}85okReply (req);86break;87}88count ++;89} catch (IOException e) {90e.printStackTrace();91}92}9394static void read (InputStream is) throws IOException {95int c;96while ((c=is.read()) != -1) {97System.out.write (c);98}99}100101static void client (String u) throws Exception {102URL url = new URL (u);103System.out.println ("client opening connection to: " + u);104URLConnection urlc = url.openConnection ();105InputStream is = urlc.getInputStream ();106read (is);107is.close();108}109110static HttpServer server;111112public static void main (String[] args) throws Exception {113B4759514 b4759514 = new B4759514();114MyAuthenticator auth = new MyAuthenticator ();115Authenticator.setDefault (auth);116ProxySelector.setDefault(ProxySelector.of(null)); // no proxy117try {118InetAddress loopback = InetAddress.getLoopbackAddress();119server = HttpServer.create(new InetSocketAddress(loopback, 0), 10);120server.createContext("/", b4759514);121server.setExecutor(Executors.newSingleThreadExecutor());122server.start();123String serverURL = URIBuilder.newBuilder()124.scheme("http")125.loopback()126.port(server.getAddress().getPort())127.path("/")128.build()129.toString();130System.out.println("Server: listening at: " + serverURL);131client(serverURL + "d1/foo.html");132} catch (Exception e) {133if (server != null) {134server.stop(1);135}136throw e;137}138int f = auth.getCount();139if (f != 1) {140except ("Authenticator was called "+f+" times. Should be 1");141}142server.stop(1);143}144145public static void except (String s) {146server.stop(1);147throw new RuntimeException (s);148}149150static class MyAuthenticator extends Authenticator {151MyAuthenticator () {152super ();153}154155int count = 0;156157public PasswordAuthentication getPasswordAuthentication () {158PasswordAuthentication pw;159pw = new PasswordAuthentication ("user", "pass1".toCharArray());160count ++;161return pw;162}163164public int getCount () {165return (count);166}167}168}169170171