Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/ssl/SSLSocketImpl/NotifyHandshakeTest.java
41152 views
1
/*
2
* Copyright (c) 2002, 2014, 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
* This class may have some race conditions that I haven't
26
* accounted for. I've tried to put in sufficient sleeps and triggers
27
* that should cause everything to run correctly.
28
*
29
* This was hackish, but to make sure that there were no problems
30
* with permissions.
31
*
32
* Create a client, server, and interested party thread. The
33
* client and interested threads should receive the same
34
* session notification.
35
*/
36
package com;
37
38
import java.net.*;
39
import java.io.*;
40
import javax.net.ssl.*;
41
import java.security.cert.*;
42
import java.security.*;
43
44
public class NotifyHandshakeTest implements HandshakeCompletedListener {
45
46
static String pathToStores = "../../../../javax/net/ssl/etc";
47
static String keyStoreFile = "keystore";
48
static String trustStoreFile = "truststore";
49
static String passwd = "passphrase";
50
volatile static int serverPort = 0;
51
52
public boolean set;
53
SSLSession sess;
54
55
static public int triggerState = 0;
56
57
static public void trigger() {
58
triggerState++;
59
}
60
61
public void handshakeCompleted(HandshakeCompletedEvent event) {
62
set = true;
63
sess = event.getSession();
64
trigger();
65
}
66
67
public static void main(String[] args) throws Exception {
68
69
String keyFilename =
70
System.getProperty("test.src", "./") + "/" + pathToStores +
71
"/" + keyStoreFile;
72
String trustFilename =
73
System.getProperty("test.src", "./") + "/" + pathToStores +
74
"/" + trustStoreFile;
75
76
System.setProperty("javax.net.ssl.keyStore", keyFilename);
77
System.setProperty("javax.net.ssl.keyStorePassword", passwd);
78
System.setProperty("javax.net.ssl.trustStore", trustFilename);
79
System.setProperty("javax.net.ssl.trustStorePassword", passwd);
80
81
SSLSocketFactory sslsf =
82
(SSLSocketFactory)SSLSocketFactory.getDefault();
83
SSLServerSocketFactory sslssf =
84
(SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
85
86
/*
87
* Start off the Server, give time to initialize.
88
*/
89
SSLServerSocket sslss =
90
(SSLServerSocket)sslssf.createServerSocket(serverPort);
91
sslss.setSoTimeout(30000); // 30 seconds
92
serverPort = sslss.getLocalPort();
93
Server server = new Server(sslss);
94
server.start();
95
96
System.out.println("Server started...");
97
98
/*
99
* Create the socket.
100
*/
101
SSLSocket socket =
102
(SSLSocket)sslsf.createSocket("localhost", serverPort);
103
104
/*
105
* Create a second thread also interested in this socket
106
*/
107
edu.NotifyHandshakeTestHeyYou heyYou =
108
new edu.NotifyHandshakeTestHeyYou(socket);
109
heyYou.start();
110
while (triggerState < 1) {
111
Thread.sleep(500);
112
}
113
System.out.println("HeyYou thread ready...");
114
115
NotifyHandshakeTest listener = new NotifyHandshakeTest();
116
socket.addHandshakeCompletedListener(listener);
117
118
System.out.println("Client starting handshake...");
119
socket.startHandshake();
120
System.out.println("Client done handshaking...");
121
122
InputStream is = socket.getInputStream();
123
if ((byte)is.read() != (byte)0x77) {
124
throw new Exception("problem reading byte");
125
}
126
127
/*
128
* Wait for HeyYou and the client to get a slice, so
129
* they can receive their SSLSessions.
130
*/
131
while (triggerState < 3) {
132
Thread.sleep(500);
133
}
134
135
/*
136
* Grab the variables before reaping the thread.
137
*/
138
boolean heyYouSet = heyYou.set;
139
AccessControlContext heyYouACC = heyYou.acc;
140
SSLSession heyYouSess = heyYou.ssls;
141
142
heyYou.interrupt();
143
heyYou.join();
144
server.join();
145
146
socket.close();
147
148
if (!heyYouSet) {
149
throw new Exception("HeyYou's wasn't set");
150
}
151
if (!listener.set) {
152
throw new Exception("This' wasn't set");
153
}
154
155
if (heyYouACC.equals(AccessController.getContext())) {
156
throw new Exception("Access Control Contexts were the same");
157
}
158
159
if (!heyYouSess.equals(listener.sess)) {
160
throw new Exception("SSLSessions were not equal");
161
}
162
163
System.out.println("Everything Passed");
164
}
165
166
static class Server extends Thread {
167
168
SSLServerSocket ss;
169
170
Server(SSLServerSocket ss) {
171
this.ss = ss;
172
}
173
174
public void run() {
175
try {
176
System.out.println("Server accepting socket...");
177
SSLSocket s = (SSLSocket) ss.accept();
178
System.out.println(
179
"Server accepted socket...starting handshake");
180
s.startHandshake();
181
System.out.println("Server done handshaking");
182
OutputStream os = s.getOutputStream();
183
os.write(0x77);
184
os.flush();
185
System.out.println("Server returning");
186
} catch (Exception e) {
187
System.out.println("Server died");
188
e.printStackTrace();
189
}
190
}
191
}
192
}
193
194