2348N/A/*
2362N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2348N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2348N/A *
2348N/A * This code is free software; you can redistribute it and/or modify it
2348N/A * under the terms of the GNU General Public License version 2 only, as
2348N/A * published by the Free Software Foundation.
2348N/A *
2348N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2348N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2348N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2348N/A * version 2 for more details (a copy is included in the LICENSE file that
2348N/A * accompanied this code).
2348N/A *
2348N/A * You should have received a copy of the GNU General Public License version
2348N/A * 2 along with this work; if not, write to the Free Software Foundation,
2348N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2348N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
2348N/A */
2348N/A
2348N/A/**
2348N/A * @test
2348N/A * @bug 6947917
2348N/A * @summary Error in basic authentication when user name and password are long
2348N/A */
2348N/A
2348N/Aimport com.sun.net.httpserver.BasicAuthenticator;
2348N/Aimport com.sun.net.httpserver.HttpContext;
2348N/Aimport com.sun.net.httpserver.HttpExchange;
2348N/Aimport com.sun.net.httpserver.HttpHandler;
2348N/Aimport com.sun.net.httpserver.HttpPrincipal;
2348N/Aimport com.sun.net.httpserver.HttpServer;
2348N/Aimport java.io.InputStream;
2348N/Aimport java.io.IOException;
2348N/Aimport java.net.Authenticator;
2348N/Aimport java.net.InetSocketAddress;
2348N/Aimport java.net.PasswordAuthentication;
2348N/Aimport java.net.HttpURLConnection;
2348N/Aimport java.net.URL;
2348N/A
2348N/Apublic class BasicLongCredentials {
2348N/A
2348N/A static final String USERNAME = "ThisIsMyReallyReallyReallyReallyReallyReally" +
2348N/A "LongFirstNameDotLastNameAtCompanyEmailAddress";
2348N/A static final String PASSWORD = "AndThisIsALongLongLongLongLongLongLongLongLong" +
2348N/A "LongLongLongLongLongLongLongLongLongPassword";
2348N/A static final String REALM = "foobar@test.realm";
2348N/A
2348N/A public static void main (String[] args) throws Exception {
2348N/A HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
2348N/A try {
2348N/A Handler handler = new Handler();
2348N/A HttpContext ctx = server.createContext("/test", handler);
2348N/A
2348N/A BasicAuthenticator a = new BasicAuthenticator(REALM) {
2348N/A public boolean checkCredentials (String username, String pw) {
2348N/A return USERNAME.equals(username) && PASSWORD.equals(pw);
2348N/A }
2348N/A };
2348N/A ctx.setAuthenticator(a);
2348N/A server.start();
2348N/A
2348N/A Authenticator.setDefault(new MyAuthenticator());
2348N/A
2348N/A URL url = new URL("http://localhost:"+server.getAddress().getPort()+"/test/");
2348N/A HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
2348N/A InputStream is = urlc.getInputStream();
2348N/A int c = 0;
2348N/A while (is.read()!= -1) { c ++; }
2348N/A
2348N/A if (c != 0) { throw new RuntimeException("Test failed c = " + c); }
2348N/A if (error) { throw new RuntimeException("Test failed: error"); }
2348N/A
2348N/A System.out.println ("OK");
2348N/A } finally {
2348N/A server.stop(0);
2348N/A }
2348N/A }
2348N/A
2348N/A public static boolean error = false;
2348N/A
2348N/A static class MyAuthenticator extends java.net.Authenticator {
2348N/A @Override
2348N/A public PasswordAuthentication getPasswordAuthentication () {
2348N/A if (!getRequestingPrompt().equals(REALM)) {
2348N/A BasicLongCredentials.error = true;
2348N/A }
2348N/A return new PasswordAuthentication (USERNAME, PASSWORD.toCharArray());
2348N/A }
2348N/A }
2348N/A
2348N/A static class Handler implements HttpHandler {
2348N/A public void handle (HttpExchange t) throws IOException {
2348N/A InputStream is = t.getRequestBody();
2348N/A while (is.read () != -1) ;
2348N/A is.close();
2348N/A t.sendResponseHeaders(200, -1);
2348N/A HttpPrincipal p = t.getPrincipal();
2348N/A if (!p.getUsername().equals(USERNAME)) {
2348N/A error = true;
2348N/A }
2348N/A if (!p.getRealm().equals(REALM)) {
2348N/A error = true;
2348N/A }
2348N/A t.close();
2348N/A }
2348N/A }
2348N/A}