author | xuelei |
Wed, 05 Mar 2014 07:24:34 +0000 | |
changeset 23052 | 241885315119 |
parent 5506 | jdk/test/sun/security/ssl/com/sun/net/ssl/internal/ssl/SSLSocketImpl/NotifyHandshakeTest.java@202f599c92aa |
child 23352 | 0b3d4e01b3c4 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2002, 2005, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 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 |
||
23052
241885315119
8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents:
5506
diff
changeset
|
46 |
static String pathToStores = "../../../../javax/net/ssl/etc"; |
2 | 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 |
serverPort = sslss.getLocalPort(); |
|
92 |
Server server = new Server(sslss); |
|
93 |
server.start(); |
|
94 |
||
95 |
System.out.println("Server started..."); |
|
96 |
||
97 |
/* |
|
98 |
* Create the socket. |
|
99 |
*/ |
|
100 |
SSLSocket socket = |
|
101 |
(SSLSocket)sslsf.createSocket("localhost", serverPort); |
|
102 |
||
103 |
/* |
|
104 |
* Create a second thread also interested in this socket |
|
105 |
*/ |
|
106 |
edu.NotifyHandshakeTestHeyYou heyYou = |
|
107 |
new edu.NotifyHandshakeTestHeyYou(socket); |
|
108 |
heyYou.start(); |
|
109 |
while (triggerState < 1) { |
|
110 |
Thread.sleep(500); |
|
111 |
} |
|
112 |
System.out.println("HeyYou thread ready..."); |
|
113 |
||
114 |
NotifyHandshakeTest listener = new NotifyHandshakeTest(); |
|
115 |
socket.addHandshakeCompletedListener(listener); |
|
116 |
||
117 |
System.out.println("Client starting handshake..."); |
|
118 |
socket.startHandshake(); |
|
119 |
System.out.println("Client done handshaking..."); |
|
120 |
||
121 |
InputStream is = socket.getInputStream(); |
|
122 |
if ((byte)is.read() != (byte)0x77) { |
|
123 |
throw new Exception("problem reading byte"); |
|
124 |
} |
|
125 |
||
126 |
/* |
|
127 |
* Wait for HeyYou and the client to get a slice, so |
|
128 |
* they can receive their SSLSessions. |
|
129 |
*/ |
|
130 |
while (triggerState < 3) { |
|
131 |
Thread.sleep(500); |
|
132 |
} |
|
133 |
||
134 |
/* |
|
135 |
* Grab the variables before reaping the thread. |
|
136 |
*/ |
|
137 |
boolean heyYouSet = heyYou.set; |
|
138 |
AccessControlContext heyYouACC = heyYou.acc; |
|
139 |
SSLSession heyYouSess = heyYou.ssls; |
|
140 |
||
141 |
heyYou.interrupt(); |
|
142 |
heyYou.join(); |
|
143 |
server.join(); |
|
144 |
||
145 |
socket.close(); |
|
146 |
||
147 |
if (!heyYouSet) { |
|
148 |
throw new Exception("HeyYou's wasn't set"); |
|
149 |
} |
|
150 |
if (!listener.set) { |
|
151 |
throw new Exception("This' wasn't set"); |
|
152 |
} |
|
153 |
||
154 |
if (heyYouACC.equals(AccessController.getContext())) { |
|
155 |
throw new Exception("Access Control Contexts were the same"); |
|
156 |
} |
|
157 |
||
158 |
if (!heyYouSess.equals(listener.sess)) { |
|
159 |
throw new Exception("SSLSessions were not equal"); |
|
160 |
} |
|
161 |
||
162 |
System.out.println("Everything Passed"); |
|
163 |
} |
|
164 |
||
165 |
static class Server extends Thread { |
|
166 |
||
167 |
SSLServerSocket ss; |
|
168 |
||
169 |
Server(SSLServerSocket ss) { |
|
170 |
this.ss = ss; |
|
171 |
} |
|
172 |
||
173 |
public void run() { |
|
174 |
try { |
|
175 |
System.out.println("Server accepting socket..."); |
|
176 |
SSLSocket s = (SSLSocket) ss.accept(); |
|
177 |
System.out.println( |
|
178 |
"Server accepted socket...starting handshake"); |
|
179 |
s.startHandshake(); |
|
180 |
System.out.println("Server done handshaking"); |
|
181 |
OutputStream os = s.getOutputStream(); |
|
182 |
os.write(0x77); |
|
183 |
os.flush(); |
|
184 |
System.out.println("Server returning"); |
|
185 |
} catch (Exception e) { |
|
186 |
System.out.println("Server died"); |
|
187 |
e.printStackTrace(); |
|
188 |
} |
|
189 |
} |
|
190 |
} |
|
191 |
} |