Path: blob/master/test/jdk/sun/net/www/protocol/http/BasicLongCredentials.java
41159 views
/*1* Copyright (c) 2010, 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*/2223/**24* @test25* @bug 694791726* @modules jdk.httpserver27* @summary Error in basic authentication when user name and password are long28* @library /test/lib29* @run main BasicLongCredentials30* @run main/othervm -Djava.net.preferIPv6Addresses=true BasicLongCredentials31*/3233import com.sun.net.httpserver.BasicAuthenticator;34import com.sun.net.httpserver.HttpContext;35import com.sun.net.httpserver.HttpExchange;36import com.sun.net.httpserver.HttpHandler;37import com.sun.net.httpserver.HttpPrincipal;38import com.sun.net.httpserver.HttpServer;39import java.io.InputStream;40import java.io.IOException;41import java.net.Authenticator;42import java.net.InetAddress;43import java.net.InetSocketAddress;44import java.net.PasswordAuthentication;45import java.net.Proxy;46import java.net.HttpURLConnection;47import java.net.URL;4849import jdk.test.lib.net.URIBuilder;5051public class BasicLongCredentials {5253static final String USERNAME = "ThisIsMyReallyReallyReallyReallyReallyReally" +54"LongFirstNameDotLastNameAtCompanyEmailAddress";55static final String PASSWORD = "AndThisIsALongLongLongLongLongLongLongLongLong" +56"LongLongLongLongLongLongLongLongLongPassword";57static final String REALM = "[email protected]";5859public static void main (String[] args) throws Exception {60InetAddress loopback = InetAddress.getLoopbackAddress();61HttpServer server = HttpServer.create(new InetSocketAddress(loopback, 0), 0);62try {63Handler handler = new Handler();64HttpContext ctx = server.createContext("/test", handler);6566BasicAuthenticator a = new BasicAuthenticator(REALM) {67public boolean checkCredentials (String username, String pw) {68return USERNAME.equals(username) && PASSWORD.equals(pw);69}70};71ctx.setAuthenticator(a);72server.start();7374Authenticator.setDefault(new MyAuthenticator());7576URL url = URIBuilder.newBuilder()77.scheme("http")78.host(server.getAddress().getAddress())79.port(server.getAddress().getPort())80.path("/test/")81.toURL();82HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);83InputStream is = urlc.getInputStream();84int c = 0;85while (is.read()!= -1) { c ++; }8687if (c != 0) { throw new RuntimeException("Test failed c = " + c); }88if (error) { throw new RuntimeException("Test failed: error"); }8990System.out.println ("OK");91} finally {92server.stop(0);93}94}9596public static boolean error = false;9798static class MyAuthenticator extends java.net.Authenticator {99@Override100public PasswordAuthentication getPasswordAuthentication () {101if (!getRequestingPrompt().equals(REALM)) {102BasicLongCredentials.error = true;103}104return new PasswordAuthentication (USERNAME, PASSWORD.toCharArray());105}106}107108static class Handler implements HttpHandler {109public void handle (HttpExchange t) throws IOException {110InputStream is = t.getRequestBody();111while (is.read () != -1) ;112is.close();113t.sendResponseHeaders(200, -1);114HttpPrincipal p = t.getPrincipal();115if (!p.getUsername().equals(USERNAME)) {116error = true;117}118if (!p.getRealm().equals(REALM)) {119error = true;120}121t.close();122}123}124}125126127