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