Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/protocol/http/BasicLongCredentials.java
41159 views
1
/*
2
* Copyright (c) 2010, 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 6947917
27
* @modules jdk.httpserver
28
* @summary Error in basic authentication when user name and password are long
29
* @library /test/lib
30
* @run main BasicLongCredentials
31
* @run main/othervm -Djava.net.preferIPv6Addresses=true BasicLongCredentials
32
*/
33
34
import com.sun.net.httpserver.BasicAuthenticator;
35
import com.sun.net.httpserver.HttpContext;
36
import com.sun.net.httpserver.HttpExchange;
37
import com.sun.net.httpserver.HttpHandler;
38
import com.sun.net.httpserver.HttpPrincipal;
39
import com.sun.net.httpserver.HttpServer;
40
import java.io.InputStream;
41
import java.io.IOException;
42
import java.net.Authenticator;
43
import java.net.InetAddress;
44
import java.net.InetSocketAddress;
45
import java.net.PasswordAuthentication;
46
import java.net.Proxy;
47
import java.net.HttpURLConnection;
48
import java.net.URL;
49
50
import jdk.test.lib.net.URIBuilder;
51
52
public class BasicLongCredentials {
53
54
static final String USERNAME = "ThisIsMyReallyReallyReallyReallyReallyReally" +
55
"LongFirstNameDotLastNameAtCompanyEmailAddress";
56
static final String PASSWORD = "AndThisIsALongLongLongLongLongLongLongLongLong" +
57
"LongLongLongLongLongLongLongLongLongPassword";
58
static final String REALM = "[email protected]";
59
60
public static void main (String[] args) throws Exception {
61
InetAddress loopback = InetAddress.getLoopbackAddress();
62
HttpServer server = HttpServer.create(new InetSocketAddress(loopback, 0), 0);
63
try {
64
Handler handler = new Handler();
65
HttpContext ctx = server.createContext("/test", handler);
66
67
BasicAuthenticator a = new BasicAuthenticator(REALM) {
68
public boolean checkCredentials (String username, String pw) {
69
return USERNAME.equals(username) && PASSWORD.equals(pw);
70
}
71
};
72
ctx.setAuthenticator(a);
73
server.start();
74
75
Authenticator.setDefault(new MyAuthenticator());
76
77
URL url = URIBuilder.newBuilder()
78
.scheme("http")
79
.host(server.getAddress().getAddress())
80
.port(server.getAddress().getPort())
81
.path("/test/")
82
.toURL();
83
HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
84
InputStream is = urlc.getInputStream();
85
int c = 0;
86
while (is.read()!= -1) { c ++; }
87
88
if (c != 0) { throw new RuntimeException("Test failed c = " + c); }
89
if (error) { throw new RuntimeException("Test failed: error"); }
90
91
System.out.println ("OK");
92
} finally {
93
server.stop(0);
94
}
95
}
96
97
public static boolean error = false;
98
99
static class MyAuthenticator extends java.net.Authenticator {
100
@Override
101
public PasswordAuthentication getPasswordAuthentication () {
102
if (!getRequestingPrompt().equals(REALM)) {
103
BasicLongCredentials.error = true;
104
}
105
return new PasswordAuthentication (USERNAME, PASSWORD.toCharArray());
106
}
107
}
108
109
static class Handler implements HttpHandler {
110
public void handle (HttpExchange t) throws IOException {
111
InputStream is = t.getRequestBody();
112
while (is.read () != -1) ;
113
is.close();
114
t.sendResponseHeaders(200, -1);
115
HttpPrincipal p = t.getPrincipal();
116
if (!p.getUsername().equals(USERNAME)) {
117
error = true;
118
}
119
if (!p.getRealm().equals(REALM)) {
120
error = true;
121
}
122
t.close();
123
}
124
}
125
}
126
127