Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/http/BasicLongCredentials.java
38867 views
/*1* Copyright (c) 2010, 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* @summary Error in basic authentication when user name and password are long27*/2829import com.sun.net.httpserver.BasicAuthenticator;30import com.sun.net.httpserver.HttpContext;31import com.sun.net.httpserver.HttpExchange;32import com.sun.net.httpserver.HttpHandler;33import com.sun.net.httpserver.HttpPrincipal;34import com.sun.net.httpserver.HttpServer;35import java.io.InputStream;36import java.io.IOException;37import java.net.Authenticator;38import java.net.InetSocketAddress;39import java.net.PasswordAuthentication;40import java.net.HttpURLConnection;41import java.net.URL;4243public class BasicLongCredentials {4445static final String USERNAME = "ThisIsMyReallyReallyReallyReallyReallyReally" +46"LongFirstNameDotLastNameAtCompanyEmailAddress";47static final String PASSWORD = "AndThisIsALongLongLongLongLongLongLongLongLong" +48"LongLongLongLongLongLongLongLongLongPassword";49static final String REALM = "[email protected]";5051public static void main (String[] args) throws Exception {52HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);53try {54Handler handler = new Handler();55HttpContext ctx = server.createContext("/test", handler);5657BasicAuthenticator a = new BasicAuthenticator(REALM) {58public boolean checkCredentials (String username, String pw) {59return USERNAME.equals(username) && PASSWORD.equals(pw);60}61};62ctx.setAuthenticator(a);63server.start();6465Authenticator.setDefault(new MyAuthenticator());6667URL url = new URL("http://localhost:"+server.getAddress().getPort()+"/test/");68HttpURLConnection urlc = (HttpURLConnection)url.openConnection();69InputStream is = urlc.getInputStream();70int c = 0;71while (is.read()!= -1) { c ++; }7273if (c != 0) { throw new RuntimeException("Test failed c = " + c); }74if (error) { throw new RuntimeException("Test failed: error"); }7576System.out.println ("OK");77} finally {78server.stop(0);79}80}8182public static boolean error = false;8384static class MyAuthenticator extends java.net.Authenticator {85@Override86public PasswordAuthentication getPasswordAuthentication () {87if (!getRequestingPrompt().equals(REALM)) {88BasicLongCredentials.error = true;89}90return new PasswordAuthentication (USERNAME, PASSWORD.toCharArray());91}92}9394static class Handler implements HttpHandler {95public void handle (HttpExchange t) throws IOException {96InputStream is = t.getRequestBody();97while (is.read () != -1) ;98is.close();99t.sendResponseHeaders(200, -1);100HttpPrincipal p = t.getPrincipal();101if (!p.getUsername().equals(USERNAME)) {102error = true;103}104if (!p.getRealm().equals(REALM)) {105error = true;106}107t.close();108}109}110}111112113