Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/net/httpserver/Test2.java
38855 views
/*1* Copyright (c) 2005, 2006, 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*/2829import com.sun.net.httpserver.*;3031import java.util.*;32import java.util.concurrent.*;33import java.io.*;34import java.net.*;35import java.security.*;36import javax.security.auth.callback.*;37import javax.net.ssl.*;3839/**40* Test authentication41*/4243public class Test2 extends Test {4445public static void main (String[] args) throws Exception {46Handler handler = new Handler();47InetSocketAddress addr = new InetSocketAddress (0);48HttpServer server = HttpServer.create (addr, 0);49HttpContext ctx = server.createContext ("/test", handler);50BasicAuthenticator a = new BasicAuthenticator ("[email protected]") {51public boolean checkCredentials (String username, String pw) {52return "fred".equals(username) && pw.charAt(0) == 'x';53}54};5556ctx.setAuthenticator (a);57ExecutorService executor = Executors.newCachedThreadPool();58server.setExecutor (executor);59server.start ();60java.net.Authenticator.setDefault (new MyAuthenticator());6162URL url = new URL ("http://localhost:"+server.getAddress().getPort()+"/test/foo.html");63System.out.print ("Test2: " );64HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();65InputStream is = urlc.getInputStream();66int c = 0;67while (is.read()!= -1) {68c ++;69}70server.stop(2);71executor.shutdown();72if (error ) {73throw new RuntimeException ("test failed error");74}75if (c != 0) {76throw new RuntimeException ("test failed c");77}78if (count != 2) {79throw new RuntimeException ("test failed count = " + count);80}81System.out.println ("OK");8283}8485public static boolean error = false;86public static int count = 0;8788static class MyAuthenticator extends java.net.Authenticator {89public PasswordAuthentication getPasswordAuthentication () {90PasswordAuthentication pw;91if (!getRequestingPrompt().equals ("[email protected]")) {92Test2.error = true;93}94if (count == 0) {95pw = new PasswordAuthentication ("bad", "wrong".toCharArray());96} else {97pw = new PasswordAuthentication ("fred", "xyz".toCharArray());98}99count ++;100return pw;101}102}103104static class Handler implements HttpHandler {105int invocation = 1;106public void handle (HttpExchange t)107throws IOException108{109InputStream is = t.getRequestBody();110Headers map = t.getRequestHeaders();111Headers rmap = t.getResponseHeaders();112while (is.read () != -1) ;113is.close();114t.sendResponseHeaders (200, -1);115HttpPrincipal p = t.getPrincipal ();116if (!p.getUsername().equals("fred")) {117error = true;118}119if (!p.getRealm().equals("[email protected]")) {120error = true;121}122t.close();123}124}125}126127128