36131
|
1 |
/*
|
|
2 |
* Copyright (c) 2015, 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. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
*/
|
|
24 |
|
|
25 |
/**
|
|
26 |
* @test
|
|
27 |
* @bug 8087112
|
|
28 |
* @run main/othervm BasicAuthTest
|
|
29 |
* @summary Basic Authentication Test
|
|
30 |
*/
|
|
31 |
|
|
32 |
import com.sun.net.httpserver.BasicAuthenticator;
|
|
33 |
import com.sun.net.httpserver.HttpContext;
|
|
34 |
import com.sun.net.httpserver.HttpExchange;
|
|
35 |
import com.sun.net.httpserver.HttpHandler;
|
|
36 |
import com.sun.net.httpserver.HttpServer;
|
|
37 |
import java.io.IOException;
|
|
38 |
import java.io.InputStream;
|
|
39 |
import java.io.OutputStream;
|
|
40 |
import java.net.InetSocketAddress;
|
|
41 |
import java.net.PasswordAuthentication;
|
|
42 |
import java.net.URI;
|
|
43 |
import java.net.http.*;
|
|
44 |
import java.util.concurrent.ExecutorService;
|
|
45 |
import java.util.concurrent.Executors;
|
|
46 |
import static java.nio.charset.StandardCharsets.US_ASCII;
|
|
47 |
|
|
48 |
public class BasicAuthTest {
|
|
49 |
|
|
50 |
static volatile boolean ok;
|
|
51 |
static final String RESPONSE = "Hello world";
|
|
52 |
static final String POST_BODY = "This is the POST body 123909090909090";
|
|
53 |
|
|
54 |
public static void main(String[] args) throws Exception {
|
|
55 |
HttpServer server = HttpServer.create(new InetSocketAddress(0), 10);
|
|
56 |
ExecutorService e = Executors.newCachedThreadPool();
|
|
57 |
Handler h = new Handler();
|
|
58 |
HttpContext serverContext = server.createContext("/test", h);
|
|
59 |
int port = server.getAddress().getPort();
|
|
60 |
System.out.println("Server port = " + port);
|
|
61 |
|
|
62 |
ClientAuth ca = new ClientAuth();
|
|
63 |
ServerAuth sa = new ServerAuth("foo realm");
|
|
64 |
serverContext.setAuthenticator(sa);
|
|
65 |
server.setExecutor(e);
|
|
66 |
server.start();
|
|
67 |
HttpClient client = HttpClient.create()
|
|
68 |
.authenticator(ca)
|
|
69 |
.build();
|
|
70 |
|
|
71 |
try {
|
|
72 |
URI uri = new URI("http://127.0.0.1:" + Integer.toString(port) + "/test/foo");
|
|
73 |
HttpRequest req = client.request(uri).GET();
|
|
74 |
|
|
75 |
HttpResponse resp = req.response();
|
|
76 |
ok = resp.statusCode() == 200 &&
|
|
77 |
resp.body(HttpResponse.asString()).equals(RESPONSE);
|
|
78 |
|
|
79 |
if (!ok || ca.count != 1)
|
|
80 |
throw new RuntimeException("Test failed");
|
|
81 |
|
|
82 |
// repeat same request, should succeed but no additional authenticator calls
|
|
83 |
|
|
84 |
req = client.request(uri).GET();
|
|
85 |
resp = req.response();
|
|
86 |
ok = resp.statusCode() == 200 &&
|
|
87 |
resp.body(HttpResponse.asString()).equals(RESPONSE);
|
|
88 |
|
|
89 |
if (!ok || ca.count != 1)
|
|
90 |
throw new RuntimeException("Test failed");
|
|
91 |
|
|
92 |
// try a POST
|
|
93 |
|
|
94 |
req = client.request(uri)
|
|
95 |
.body(HttpRequest.fromString(POST_BODY))
|
|
96 |
.POST();
|
|
97 |
resp = req.response();
|
|
98 |
ok = resp.statusCode() == 200;
|
|
99 |
|
|
100 |
if (!ok || ca.count != 1)
|
|
101 |
throw new RuntimeException("Test failed");
|
|
102 |
} finally {
|
|
103 |
client.executorService().shutdownNow();
|
|
104 |
server.stop(0);
|
|
105 |
e.shutdownNow();
|
|
106 |
}
|
|
107 |
System.out.println("OK");
|
|
108 |
}
|
|
109 |
|
|
110 |
static class ServerAuth extends BasicAuthenticator {
|
|
111 |
|
|
112 |
ServerAuth(String realm) {
|
|
113 |
super(realm);
|
|
114 |
}
|
|
115 |
|
|
116 |
@Override
|
|
117 |
public boolean checkCredentials(String username, String password) {
|
|
118 |
if (!"user".equals(username) || !"passwd".equals(password)) {
|
|
119 |
return false;
|
|
120 |
}
|
|
121 |
return true;
|
|
122 |
}
|
|
123 |
|
|
124 |
}
|
|
125 |
|
|
126 |
static class ClientAuth extends java.net.Authenticator {
|
|
127 |
volatile int count = 0;
|
|
128 |
|
|
129 |
@Override
|
|
130 |
protected PasswordAuthentication getPasswordAuthentication() {
|
|
131 |
count++;
|
|
132 |
return new PasswordAuthentication("user", "passwd".toCharArray());
|
|
133 |
}
|
|
134 |
}
|
|
135 |
|
|
136 |
static class Handler implements HttpHandler {
|
|
137 |
static volatile boolean ok;
|
|
138 |
|
|
139 |
@Override
|
|
140 |
public void handle(HttpExchange he) throws IOException {
|
|
141 |
String method = he.getRequestMethod();
|
|
142 |
InputStream is = he.getRequestBody();
|
|
143 |
if (method.equalsIgnoreCase("POST")) {
|
|
144 |
String requestBody = new String(is.readAllBytes(), US_ASCII);
|
|
145 |
if (!requestBody.equals(POST_BODY)) {
|
|
146 |
he.sendResponseHeaders(500, -1);
|
|
147 |
ok = false;
|
|
148 |
} else {
|
|
149 |
he.sendResponseHeaders(200, -1);
|
|
150 |
ok = true;
|
|
151 |
}
|
|
152 |
} else { // GET
|
|
153 |
he.sendResponseHeaders(200, RESPONSE.length());
|
|
154 |
OutputStream os = he.getResponseBody();
|
|
155 |
os.write(RESPONSE.getBytes(US_ASCII));
|
|
156 |
os.close();
|
|
157 |
ok = true;
|
|
158 |
}
|
|
159 |
}
|
|
160 |
|
|
161 |
}
|
|
162 |
}
|