Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/Authenticator/B4759514.java
38812 views
/*1* Copyright (c) 2002, 2012, 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 ../../../sun/net/www/httptest/27* @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction28* @run main B475951429* @summary Digest Authentication is erroniously quoting the nc value, contrary to RFC 261730*/3132import java.io.*;33import java.net.*;3435public class B4759514 implements HttpCallback {3637static int count = 0;38static String authstring;3940void errorReply (HttpTransaction req, String reply) throws IOException {41req.addResponseHeader ("Connection", "close");42req.addResponseHeader ("WWW-Authenticate", reply);43req.sendResponse (401, "Unauthorized");44req.orderlyClose();45}4647void okReply (HttpTransaction req) throws IOException {48req.setResponseEntityBody ("Hello .");49req.sendResponse (200, "Ok");50req.orderlyClose();51}5253public void request (HttpTransaction req) {54try {55authstring = req.getRequestHeader ("Authorization");56switch (count) {57case 0:58errorReply (req, "Digest realm=\"wallyworld\", nonce=\"1234\", domain=\"/\"");59break;60case 1:61int n = authstring.indexOf ("nc=");62if (n != -1) {63if (authstring.charAt (n+3) == '\"') {64req.sendResponse (400, "Bad Request");65break;66}67}68okReply (req);69break;70}71count ++;72} catch (IOException e) {73e.printStackTrace();74}75}7677static void read (InputStream is) throws IOException {78int c;79while ((c=is.read()) != -1) {80System.out.write (c);81}82}8384static void client (String u) throws Exception {85URL url = new URL (u);86System.out.println ("client opening connection to: " + u);87URLConnection urlc = url.openConnection ();88InputStream is = urlc.getInputStream ();89read (is);90is.close();91}9293static TestHttpServer server;9495public static void main (String[] args) throws Exception {96MyAuthenticator auth = new MyAuthenticator ();97Authenticator.setDefault (auth);98try {99server = new TestHttpServer (new B4759514(), 1, 10, 0);100System.out.println ("Server: listening on port: " + server.getLocalPort());101client ("http://localhost:"+server.getLocalPort()+"/d1/foo.html");102} catch (Exception e) {103if (server != null) {104server.terminate();105}106throw e;107}108int f = auth.getCount();109if (f != 1) {110except ("Authenticator was called "+f+" times. Should be 1");111}112server.terminate();113}114115public static void except (String s) {116server.terminate();117throw new RuntimeException (s);118}119120static class MyAuthenticator extends Authenticator {121MyAuthenticator () {122super ();123}124125int count = 0;126127public PasswordAuthentication getPasswordAuthentication () {128PasswordAuthentication pw;129pw = new PasswordAuthentication ("user", "pass1".toCharArray());130count ++;131return pw;132}133134public int getCount () {135return (count);136}137}138}139140141