Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/Authenticator/B8034170.java
41149 views
1
/*
2
* Copyright (c) 2014, 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
import java.io.*;
25
import java.net.*;
26
import java.util.*;
27
import jdk.test.lib.net.URIBuilder;
28
29
/**
30
* @test
31
* @bug 8034170
32
* @summary Digest authentication interop issue
33
* @library /test/lib
34
* @run main/othervm B8034170 unquoted
35
* @run main/othervm -Dhttp.auth.digest.quoteParameters=true B8034170 quoted
36
* @run main/othervm -Djava.net.preferIPv6Addresses=true B8034170 unquoted
37
*/
38
39
public class B8034170 {
40
41
static boolean expectQuotes;
42
43
static class BasicServer extends Thread {
44
45
ServerSocket server;
46
47
Socket s;
48
InputStream is;
49
OutputStream os;
50
51
static final String realm = "wallyworld";
52
53
String reply1 = "HTTP/1.1 401 Unauthorized\r\n"+
54
"WWW-Authenticate: Digest realm=\""+realm+"\", qop=\"auth\"" +
55
", nonce=\"8989de95ea2402b64d73cecdb15da255\"" +
56
", opaque=\"bbfb4c9ee92ddccc73521c3e6e841ba2\"\r\n\r\n";
57
58
String OKreply = "HTTP/1.1 200 OK\r\n"+
59
"Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +
60
"Server: Apache/1.3.14 (Unix)\r\n" +
61
"Connection: close\r\n" +
62
"Content-Type: text/plain; charset=iso-8859-1\r\n" +
63
"Content-Length: 10\r\n\r\n";
64
65
String ERRreply = "HTTP/1.1 500 Internal server error\r\n"+
66
"Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +
67
"Server: Apache/1.3.14 (Unix)\r\n" +
68
"Connection: close\r\n" +
69
"Content-Length: 0\r\n\r\n";
70
71
BasicServer (ServerSocket s) {
72
server = s;
73
}
74
75
int readAll (Socket s, byte[] buf) throws IOException {
76
int pos = 0;
77
InputStream is = s.getInputStream ();
78
// wait two seconds for request, as client doesn't close
79
// the connection
80
s.setSoTimeout(2000);
81
try {
82
int n;
83
while ((n=is.read(buf, pos, buf.length-pos)) > 0)
84
pos +=n;
85
} catch (SocketTimeoutException x) { }
86
return pos;
87
}
88
89
public void run () {
90
byte[] buf = new byte[5000];
91
try {
92
System.out.println ("Server 1: accept");
93
s = server.accept ();
94
System.out.println ("accepted");
95
os = s.getOutputStream();
96
os.write (reply1.getBytes());
97
readAll (s, buf);
98
s.close ();
99
100
System.out.println ("Server 2: accept");
101
s = server.accept ();
102
System.out.println ("accepted");
103
os = s.getOutputStream();
104
int count = readAll (s, buf);
105
String reply = new String(buf, 0, count);
106
107
boolean error;
108
109
if (expectQuotes) {
110
error = false;
111
if (!reply.contains("qop=\"auth\"")) {
112
System.out.println ("Expecting quoted qop. Not found");
113
error = true;
114
}
115
if (!reply.contains("algorithm=\"MD5\"")) {
116
System.out.println ("Expecting quoted algorithm. Not found");
117
error = true;
118
}
119
} else {
120
error = false;
121
if (!reply.contains("qop=auth")) {
122
System.out.println ("Expecting unquoted qop. Not found");
123
error = true;
124
}
125
if (!reply.contains("algorithm=MD5")) {
126
System.out.println ("Expecting unquoted algorithm. Not found");
127
error = true;
128
}
129
}
130
if (error) {
131
os.write(ERRreply.getBytes());
132
os.flush();
133
s.close();
134
} else {
135
os.write((OKreply+"HelloWorld").getBytes());
136
os.flush();
137
s.close();
138
}
139
}
140
catch (Exception e) {
141
System.out.println (e);
142
}
143
finished ();
144
}
145
146
public synchronized void finished () {
147
notifyAll();
148
}
149
150
}
151
152
static class MyAuthenticator3 extends Authenticator {
153
PasswordAuthentication pw;
154
MyAuthenticator3 () {
155
super ();
156
pw = new PasswordAuthentication ("user", "passwordNotCheckedAnyway".toCharArray());
157
}
158
159
public PasswordAuthentication getPasswordAuthentication ()
160
{
161
System.out.println ("Auth called");
162
return pw;
163
}
164
}
165
166
167
static void read (InputStream is) throws IOException {
168
int c;
169
System.out.println ("reading");
170
while ((c=is.read()) != -1) {
171
System.out.write (c);
172
}
173
System.out.println ("");
174
System.out.println ("finished reading");
175
}
176
177
public static void main (String args[]) throws Exception {
178
expectQuotes = args[0].equals("quoted");
179
180
MyAuthenticator3 auth = new MyAuthenticator3 ();
181
Authenticator.setDefault (auth);
182
InetAddress loopback = InetAddress.getLoopbackAddress();
183
ServerSocket ss = new ServerSocket();
184
ss.bind(new InetSocketAddress(loopback, 0));
185
int port = ss.getLocalPort ();
186
BasicServer server = new BasicServer (ss);
187
synchronized (server) {
188
server.start();
189
System.out.println ("client 1");
190
URL url = URIBuilder.newBuilder()
191
.scheme("http")
192
.loopback()
193
.port(port)
194
.path("/d1/d2/d3/foo.html")
195
.toURL();
196
URLConnection urlc = url.openConnection(Proxy.NO_PROXY);
197
InputStream is = urlc.getInputStream ();
198
read (is);
199
is.close ();
200
}
201
}
202
}
203
204