J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005-2006 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 6270015 |
| 27 | * @summary Light weight HTTP server |
| 28 | */ |
| 29 | |
| 30 | import com.sun.net.httpserver.*; |
| 31 | |
| 32 | import java.util.*; |
| 33 | import java.util.concurrent.*; |
| 34 | import java.io.*; |
| 35 | import java.net.*; |
| 36 | import java.security.*; |
| 37 | import javax.security.auth.callback.*; |
| 38 | import javax.net.ssl.*; |
| 39 | |
| 40 | /** |
| 41 | * Test authentication |
| 42 | */ |
| 43 | |
| 44 | public class Test2 extends Test { |
| 45 | |
| 46 | public static void main (String[] args) throws Exception { |
| 47 | Handler handler = new Handler(); |
| 48 | InetSocketAddress addr = new InetSocketAddress (0); |
| 49 | HttpServer server = HttpServer.create (addr, 0); |
| 50 | HttpContext ctx = server.createContext ("/test", handler); |
| 51 | BasicAuthenticator a = new BasicAuthenticator ("foobar@test.realm") { |
| 52 | public boolean checkCredentials (String username, String pw) { |
| 53 | return "fred".equals(username) && pw.charAt(0) == 'x'; |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | ctx.setAuthenticator (a); |
| 58 | ExecutorService executor = Executors.newCachedThreadPool(); |
| 59 | server.setExecutor (executor); |
| 60 | server.start (); |
| 61 | java.net.Authenticator.setDefault (new MyAuthenticator()); |
| 62 | |
| 63 | URL url = new URL ("http://localhost:"+server.getAddress().getPort()+"/test/foo.html"); |
| 64 | System.out.print ("Test2: " ); |
| 65 | HttpURLConnection urlc = (HttpURLConnection)url.openConnection (); |
| 66 | InputStream is = urlc.getInputStream(); |
| 67 | int c = 0; |
| 68 | while (is.read()!= -1) { |
| 69 | c ++; |
| 70 | } |
| 71 | server.stop(2); |
| 72 | executor.shutdown(); |
| 73 | if (error ) { |
| 74 | throw new RuntimeException ("test failed error"); |
| 75 | } |
| 76 | if (c != 0) { |
| 77 | throw new RuntimeException ("test failed c"); |
| 78 | } |
| 79 | if (count != 2) { |
| 80 | throw new RuntimeException ("test failed count = " + count); |
| 81 | } |
| 82 | System.out.println ("OK"); |
| 83 | |
| 84 | } |
| 85 | |
| 86 | public static boolean error = false; |
| 87 | public static int count = 0; |
| 88 | |
| 89 | static class MyAuthenticator extends java.net.Authenticator { |
| 90 | public PasswordAuthentication getPasswordAuthentication () { |
| 91 | PasswordAuthentication pw; |
| 92 | if (!getRequestingPrompt().equals ("foobar@test.realm")) { |
| 93 | Test2.error = true; |
| 94 | } |
| 95 | if (count == 0) { |
| 96 | pw = new PasswordAuthentication ("bad", "wrong".toCharArray()); |
| 97 | } else { |
| 98 | pw = new PasswordAuthentication ("fred", "xyz".toCharArray()); |
| 99 | } |
| 100 | count ++; |
| 101 | return pw; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | static class Handler implements HttpHandler { |
| 106 | int invocation = 1; |
| 107 | public void handle (HttpExchange t) |
| 108 | throws IOException |
| 109 | { |
| 110 | InputStream is = t.getRequestBody(); |
| 111 | Headers map = t.getRequestHeaders(); |
| 112 | Headers rmap = t.getResponseHeaders(); |
| 113 | while (is.read () != -1) ; |
| 114 | is.close(); |
| 115 | t.sendResponseHeaders (200, -1); |
| 116 | HttpPrincipal p = t.getPrincipal (); |
| 117 | if (!p.getUsername().equals("fred")) { |
| 118 | error = true; |
| 119 | } |
| 120 | if (!p.getRealm().equals("foobar@test.realm")) { |
| 121 | error = true; |
| 122 | } |
| 123 | t.close(); |
| 124 | } |
| 125 | } |
| 126 | } |