Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/Authenticator/BasicTest.java
41149 views
1
/*
2
* Copyright (c) 2001, 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 4474947
32
* @summary fix for bug #4244472 is incomplete - HTTP authorization still needs work
33
* @library /test/lib
34
* @run main/othervm BasicTest
35
* @run main/othervm -Djava.net.preferIPv6Addresses=true BasicTest
36
*/
37
38
/*
39
* Note, this is not a general purpose test for Basic Authentication because
40
* it does not check the correctness of the data, only whether the user
41
* authenticator gets called once as expected
42
*/
43
44
public class BasicTest {
45
46
static class BasicServer extends Thread {
47
48
ServerSocket server;
49
50
Socket s;
51
InputStream is;
52
OutputStream os;
53
54
static final String realm = "wallyworld";
55
56
String reply1 = "HTTP/1.1 401 Unauthorized\r\n"+
57
"WWW-Authenticate: Basic realm=\""+realm+"\"\r\n\r\n";
58
59
String reply2 = "HTTP/1.1 200 OK\r\n"+
60
"Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +
61
"Server: Apache/1.3.14 (Unix)\r\n" +
62
"Connection: close\r\n" +
63
"Content-Type: text/html; charset=iso-8859-1\r\n" +
64
"Content-Length: 10\r\n\r\n";
65
66
BasicServer (ServerSocket s) {
67
server = s;
68
}
69
70
void readAll (Socket s) throws IOException {
71
byte[] buf = new byte [128];
72
InputStream is = s.getInputStream ();
73
while (is.available() > 0) {
74
is.read (buf);
75
}
76
}
77
78
public void run () {
79
try {
80
System.out.println ("Server 1: accept");
81
s = server.accept ();
82
readAll (s);
83
System.out.println ("accepted");
84
os = s.getOutputStream();
85
os.write (reply1.getBytes());
86
Thread.sleep (500);
87
88
System.out.println ("Server 2: accept");
89
s = server.accept ();
90
readAll (s);
91
System.out.println ("accepted");
92
os = s.getOutputStream();
93
os.write ((reply2+"HelloWorld").getBytes());
94
95
/* Second request now */
96
97
System.out.println ("Server 3: accept");
98
s = server.accept ();
99
readAll (s);
100
System.out.println ("accepted");
101
os = s.getOutputStream();
102
os.write (reply1.getBytes());
103
Thread.sleep (500);
104
s.close ();
105
106
System.out.println ("Server 4: accept");
107
s = server.accept ();
108
readAll (s);
109
System.out.println ("accepted");
110
os = s.getOutputStream();
111
os.write ((reply2+"HelloAgain").getBytes());
112
}
113
catch (Exception e) {
114
System.out.println (e);
115
}
116
finished ();
117
}
118
119
public synchronized void finished () {
120
notifyAll();
121
}
122
123
}
124
125
static class MyAuthenticator extends Authenticator {
126
MyAuthenticator () {
127
super ();
128
}
129
130
int count = 0;
131
132
public PasswordAuthentication getPasswordAuthentication ()
133
{
134
count ++;
135
System.out.println ("Auth called");
136
return (new PasswordAuthentication ("user", "passwordNotCheckedAnyway".toCharArray()));
137
}
138
139
public int getCount () {
140
return (count);
141
}
142
}
143
144
145
static void read (InputStream is) throws IOException {
146
int c;
147
System.out.println ("reading");
148
while ((c=is.read()) != -1) {
149
System.out.write (c);
150
}
151
System.out.println ("");
152
System.out.println ("finished reading");
153
}
154
155
public static void main (String args[]) throws Exception {
156
MyAuthenticator auth = new MyAuthenticator ();
157
Authenticator.setDefault (auth);
158
InetAddress loopback = InetAddress.getLoopbackAddress();
159
ServerSocket ss = new ServerSocket();
160
ss.bind(new InetSocketAddress(loopback, 0));
161
int port = ss.getLocalPort ();
162
BasicServer server = new BasicServer (ss);
163
synchronized (server) {
164
server.start();
165
System.out.println ("client 1");
166
String base = URIBuilder.newBuilder()
167
.scheme("http")
168
.loopback()
169
.port(port)
170
.path("/")
171
.build()
172
.toString();
173
URL url = new URL(base + "d1/d2/d3/foo.html");
174
URLConnection urlc = url.openConnection(Proxy.NO_PROXY);
175
InputStream is = urlc.getInputStream ();
176
read (is);
177
System.out.println ("client 2");
178
url = new URL(base + "d1/foo.html");
179
urlc = url.openConnection(Proxy.NO_PROXY);
180
is = urlc.getInputStream ();
181
read (is);
182
server.wait ();
183
// check if authenticator was called once (ok) or twice (not)
184
int f = auth.getCount();
185
if (f != 1) {
186
throw new RuntimeException ("Authenticator was called "+f+" times. Should be 1");
187
}
188
}
189
}
190
}
191
192