Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/Authenticator/AuthNPETest.java
41149 views
1
/*
2
* Copyright (c) 2002, 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 4662246
32
* @summary REGRESSION: plugin 14x client authentication dialog returns NullPointerException
33
* @library /test/lib
34
* @run main/othervm AuthNPETest
35
* @run main/othervm -Djava.net.preferIPv6Addresses=true AuthNPETest
36
*/
37
38
public class AuthNPETest {
39
40
static class BasicServer extends Thread {
41
42
ServerSocket server;
43
44
Socket s;
45
InputStream is;
46
OutputStream os;
47
48
static final String realm = "wallyworld";
49
50
String reply1 = "HTTP/1.1 401 Unauthorized\r\n"+
51
"WWW-Authenticate: Basic realm=\""+realm+"\"\r\n\r\n";
52
53
String reply2 = "HTTP/1.1 200 OK\r\n"+
54
"Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +
55
"Server: Apache/1.3.14 (Unix)\r\n" +
56
"Connection: close\r\n" +
57
"Content-Type: text/html; charset=iso-8859-1\r\n" +
58
"Content-Length: 10\r\n\r\n";
59
60
BasicServer(ServerSocket s) {
61
server = s;
62
}
63
64
void readAll(Socket s) throws IOException {
65
byte[] buf = new byte [128];
66
InputStream is = s.getInputStream();
67
s.setSoTimeout(1000);
68
try {
69
while (is.read(buf) > 0) ;
70
} catch (SocketTimeoutException x) { }
71
}
72
73
public void run() {
74
try {
75
System.out.println("Server 1: accept");
76
s = server.accept();
77
System.out.println("accepted");
78
os = s.getOutputStream();
79
os.write(reply1.getBytes());
80
readAll(s);
81
s.close();
82
83
System.out.println("Server 2: accept");
84
s = server.accept();
85
System.out.println("accepted");
86
os = s.getOutputStream();
87
os.write((reply2+"HelloWorld").getBytes());
88
readAll(s);
89
s.close();
90
91
}
92
catch (Exception e) {
93
System.out.println (e);
94
}
95
finished();
96
}
97
98
public synchronized void finished() {
99
notifyAll();
100
}
101
102
}
103
104
static class MyAuthenticator extends Authenticator {
105
106
MyAuthenticator() {
107
super();
108
}
109
110
int count = 0;
111
112
public PasswordAuthentication getPasswordAuthentication()
113
{
114
count++;
115
System.out.println("Auth called");
116
return (new PasswordAuthentication("user", "passwordNotCheckedAnyway".toCharArray()));
117
}
118
119
public int getCount() {
120
return count;
121
}
122
}
123
124
125
static void read(InputStream is) throws IOException {
126
int c;
127
System.out.println("reading");
128
while ((c=is.read()) != -1) {
129
System.out.write(c);
130
}
131
System.out.println("");
132
System.out.println("finished reading");
133
}
134
135
public static void main(String args[]) throws Exception {
136
MyAuthenticator auth = new MyAuthenticator();
137
Authenticator.setDefault(auth);
138
InetAddress loopback = InetAddress.getLoopbackAddress();
139
ServerSocket ss = new ServerSocket();
140
ss.bind(new InetSocketAddress(loopback, 0));
141
int port = ss.getLocalPort();
142
BasicServer server = new BasicServer(ss);
143
synchronized (server) {
144
server.start();
145
System.out.println ("client 1");
146
URL url = URIBuilder.newBuilder()
147
.scheme("http")
148
.loopback()
149
.port(port)
150
.toURL();
151
URLConnection urlc = url.openConnection(Proxy.NO_PROXY);
152
InputStream is = urlc.getInputStream();
153
read(is);
154
is.close();
155
}
156
}
157
}
158
159