jdk/test/sun/net/www/protocol/http/BasicLongCredentials.java
changeset 5464 8c4deb1b4a45
child 5506 202f599c92aa
equal deleted inserted replaced
5463:ad2d35ddcf37 5464:8c4deb1b4a45
       
     1 /*
       
     2  * Copyright 2010 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /**
       
    25  * @test
       
    26  * @bug 6947917
       
    27  * @summary  Error in basic authentication when user name and password are long
       
    28  */
       
    29 
       
    30 import com.sun.net.httpserver.BasicAuthenticator;
       
    31 import com.sun.net.httpserver.HttpContext;
       
    32 import com.sun.net.httpserver.HttpExchange;
       
    33 import com.sun.net.httpserver.HttpHandler;
       
    34 import com.sun.net.httpserver.HttpPrincipal;
       
    35 import com.sun.net.httpserver.HttpServer;
       
    36 import java.io.InputStream;
       
    37 import java.io.IOException;
       
    38 import java.net.Authenticator;
       
    39 import java.net.InetSocketAddress;
       
    40 import java.net.PasswordAuthentication;
       
    41 import java.net.HttpURLConnection;
       
    42 import java.net.URL;
       
    43 
       
    44 public class BasicLongCredentials {
       
    45 
       
    46     static final String USERNAME = "ThisIsMyReallyReallyReallyReallyReallyReally" +
       
    47                                    "LongFirstNameDotLastNameAtCompanyEmailAddress";
       
    48     static final String PASSWORD = "AndThisIsALongLongLongLongLongLongLongLongLong" +
       
    49                                    "LongLongLongLongLongLongLongLongLongPassword";
       
    50     static final String REALM = "foobar@test.realm";
       
    51 
       
    52     public static void main (String[] args) throws Exception {
       
    53         HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
       
    54         try {
       
    55             Handler handler = new Handler();
       
    56             HttpContext ctx = server.createContext("/test", handler);
       
    57 
       
    58             BasicAuthenticator a = new BasicAuthenticator(REALM) {
       
    59                 public boolean checkCredentials (String username, String pw) {
       
    60                     return USERNAME.equals(username) && PASSWORD.equals(pw);
       
    61                 }
       
    62             };
       
    63             ctx.setAuthenticator(a);
       
    64             server.start();
       
    65 
       
    66             Authenticator.setDefault(new MyAuthenticator());
       
    67 
       
    68             URL url = new URL("http://localhost:"+server.getAddress().getPort()+"/test/");
       
    69             HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
       
    70             InputStream is = urlc.getInputStream();
       
    71             int c = 0;
       
    72             while (is.read()!= -1) { c ++; }
       
    73 
       
    74             if (c != 0) { throw new RuntimeException("Test failed c = " + c); }
       
    75             if (error) { throw new RuntimeException("Test failed: error"); }
       
    76 
       
    77             System.out.println ("OK");
       
    78         } finally {
       
    79             server.stop(0);
       
    80         }
       
    81     }
       
    82 
       
    83     public static boolean error = false;
       
    84 
       
    85     static class MyAuthenticator extends java.net.Authenticator {
       
    86         @Override
       
    87         public PasswordAuthentication getPasswordAuthentication () {
       
    88             if (!getRequestingPrompt().equals(REALM)) {
       
    89                 BasicLongCredentials.error = true;
       
    90             }
       
    91             return new PasswordAuthentication (USERNAME, PASSWORD.toCharArray());
       
    92         }
       
    93     }
       
    94 
       
    95     static class Handler implements HttpHandler {
       
    96         public void handle (HttpExchange t) throws IOException {
       
    97             InputStream is = t.getRequestBody();
       
    98             while (is.read () != -1) ;
       
    99             is.close();
       
   100             t.sendResponseHeaders(200, -1);
       
   101             HttpPrincipal p = t.getPrincipal();
       
   102             if (!p.getUsername().equals(USERNAME)) {
       
   103                 error = true;
       
   104             }
       
   105             if (!p.getRealm().equals(REALM)) {
       
   106                 error = true;
       
   107             }
       
   108             t.close();
       
   109         }
       
   110     }
       
   111 }