BasicLongCredentials.java revision 14606:bc3775e25b52
1/*
2 * Copyright (c) 2010, 2016, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/**
25 * @test
26 * @bug 6947917
27 * @modules jdk.httpserver
28 * @summary  Error in basic authentication when user name and password are long
29 */
30
31import com.sun.net.httpserver.BasicAuthenticator;
32import com.sun.net.httpserver.HttpContext;
33import com.sun.net.httpserver.HttpExchange;
34import com.sun.net.httpserver.HttpHandler;
35import com.sun.net.httpserver.HttpPrincipal;
36import com.sun.net.httpserver.HttpServer;
37import java.io.InputStream;
38import java.io.IOException;
39import java.net.Authenticator;
40import java.net.InetSocketAddress;
41import java.net.PasswordAuthentication;
42import java.net.HttpURLConnection;
43import java.net.URL;
44
45public class BasicLongCredentials {
46
47    static final String USERNAME = "ThisIsMyReallyReallyReallyReallyReallyReally" +
48                                   "LongFirstNameDotLastNameAtCompanyEmailAddress";
49    static final String PASSWORD = "AndThisIsALongLongLongLongLongLongLongLongLong" +
50                                   "LongLongLongLongLongLongLongLongLongPassword";
51    static final String REALM = "foobar@test.realm";
52
53    public static void main (String[] args) throws Exception {
54        HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
55        try {
56            Handler handler = new Handler();
57            HttpContext ctx = server.createContext("/test", handler);
58
59            BasicAuthenticator a = new BasicAuthenticator(REALM) {
60                public boolean checkCredentials (String username, String pw) {
61                    return USERNAME.equals(username) && PASSWORD.equals(pw);
62                }
63            };
64            ctx.setAuthenticator(a);
65            server.start();
66
67            Authenticator.setDefault(new MyAuthenticator());
68
69            URL url = new URL("http://localhost:"+server.getAddress().getPort()+"/test/");
70            HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
71            InputStream is = urlc.getInputStream();
72            int c = 0;
73            while (is.read()!= -1) { c ++; }
74
75            if (c != 0) { throw new RuntimeException("Test failed c = " + c); }
76            if (error) { throw new RuntimeException("Test failed: error"); }
77
78            System.out.println ("OK");
79        } finally {
80            server.stop(0);
81        }
82    }
83
84    public static boolean error = false;
85
86    static class MyAuthenticator extends java.net.Authenticator {
87        @Override
88        public PasswordAuthentication getPasswordAuthentication () {
89            if (!getRequestingPrompt().equals(REALM)) {
90                BasicLongCredentials.error = true;
91            }
92            return new PasswordAuthentication (USERNAME, PASSWORD.toCharArray());
93        }
94    }
95
96    static class Handler implements HttpHandler {
97        public void handle (HttpExchange t) throws IOException {
98            InputStream is = t.getRequestBody();
99            while (is.read () != -1) ;
100            is.close();
101            t.sendResponseHeaders(200, -1);
102            HttpPrincipal p = t.getPrincipal();
103            if (!p.getUsername().equals(USERNAME)) {
104                error = true;
105            }
106            if (!p.getRealm().equals(REALM)) {
107                error = true;
108            }
109            t.close();
110        }
111    }
112}
113