Path: blob/master/test/jdk/com/sun/net/httpserver/Test2.java
41152 views
/*1* Copyright (c) 2005, 2019, 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 627001526* @summary Light weight HTTP server27* @library /test/lib28* @run main Test229* @run main/othervm -Djava.net.preferIPv6Addresses=true Test230*/3132import com.sun.net.httpserver.*;3334import java.util.*;35import java.util.concurrent.*;36import java.io.*;37import java.net.*;38import java.security.*;39import javax.security.auth.callback.*;40import javax.net.ssl.*;41import jdk.test.lib.net.URIBuilder;4243/**44* Test authentication45*/4647public class Test2 extends Test {4849public static void main (String[] args) throws Exception {50Handler handler = new Handler();51InetAddress loopback = InetAddress.getLoopbackAddress();52InetSocketAddress addr = new InetSocketAddress(loopback, 0);53HttpServer server = HttpServer.create (addr, 0);54HttpContext ctx = server.createContext ("/test", handler);55BasicAuthenticator a = new BasicAuthenticator ("[email protected]") {56public boolean checkCredentials (String username, String pw) {57return "fred".equals(username) && pw.charAt(0) == 'x';58}59};6061ctx.setAuthenticator (a);62ExecutorService executor = Executors.newCachedThreadPool();63server.setExecutor (executor);64server.start ();65java.net.Authenticator.setDefault (new MyAuthenticator());6667URL url = URIBuilder.newBuilder()68.scheme("http")69.loopback()70.port(server.getAddress().getPort())71.path("/test/foo.html")72.toURL();7374System.out.print ("Test2: " );75HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);76InputStream is = urlc.getInputStream();77int c = 0;78while (is.read()!= -1) {79c ++;80}81server.stop(2);82executor.shutdown();83if (error ) {84throw new RuntimeException ("test failed error");85}86if (c != 0) {87throw new RuntimeException ("test failed c");88}89if (count != 2) {90throw new RuntimeException ("test failed count = " + count);91}92System.out.println ("OK");9394}9596public static boolean error = false;97public static int count = 0;9899static class MyAuthenticator extends java.net.Authenticator {100public PasswordAuthentication getPasswordAuthentication () {101PasswordAuthentication pw;102if (!getRequestingPrompt().equals ("[email protected]")) {103Test2.error = true;104}105if (count == 0) {106pw = new PasswordAuthentication ("bad", "wrong".toCharArray());107} else {108pw = new PasswordAuthentication ("fred", "xyz".toCharArray());109}110count ++;111return pw;112}113}114115static class Handler implements HttpHandler {116int invocation = 1;117public void handle (HttpExchange t)118throws IOException119{120InputStream is = t.getRequestBody();121Headers map = t.getRequestHeaders();122Headers rmap = t.getResponseHeaders();123while (is.read () != -1) ;124is.close();125t.sendResponseHeaders (200, -1);126HttpPrincipal p = t.getPrincipal ();127if (!p.getUsername().equals("fred")) {128error = true;129}130if (!p.getRealm().equals("[email protected]")) {131error = true;132}133t.close();134}135}136}137138139