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