author | prr |
Thu, 18 Dec 2014 10:45:45 -0800 | |
changeset 29908 | 83e2c403fefd |
parent 23052 | 241885315119 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2004, 2007, 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 |
* @test |
|
26 |
* @bug 5019096 |
|
27 |
* @summary Add scatter/gather APIs for SSLEngine |
|
28 |
* |
|
29 |
*/ |
|
30 |
||
31 |
import javax.net.ssl.*; |
|
32 |
import javax.net.ssl.SSLEngineResult.*; |
|
33 |
import java.io.*; |
|
34 |
import java.security.*; |
|
35 |
import java.nio.*; |
|
36 |
||
37 |
public class Arrays { |
|
38 |
||
39 |
private static boolean debug = false; |
|
40 |
||
41 |
private SSLContext sslc; |
|
42 |
private SSLEngine ssle1; // client |
|
43 |
private SSLEngine ssle2; // server |
|
44 |
||
23052
241885315119
8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents:
5506
diff
changeset
|
45 |
private static String pathToStores = "../etc"; |
2 | 46 |
private static String keyStoreFile = "keystore"; |
47 |
private static String trustStoreFile = "truststore"; |
|
48 |
private static String passwd = "passphrase"; |
|
49 |
||
50 |
private static String keyFilename = |
|
51 |
System.getProperty("test.src", "./") + "/" + pathToStores + |
|
52 |
"/" + keyStoreFile; |
|
53 |
private static String trustFilename = |
|
54 |
System.getProperty("test.src", "./") + "/" + pathToStores + |
|
55 |
"/" + trustStoreFile; |
|
56 |
||
57 |
private ByteBuffer [] appOutArray1; |
|
58 |
private ByteBuffer [] appInArray1; |
|
59 |
||
60 |
private ByteBuffer appOut2; // write side of ssle2 |
|
61 |
private ByteBuffer appIn2; // read side of ssle2 |
|
62 |
||
63 |
private ByteBuffer oneToTwo; // "reliable" transport ssle1->ssle2 |
|
64 |
private ByteBuffer twoToOne; // "reliable" transport ssle2->ssle1 |
|
65 |
||
66 |
/* |
|
67 |
* Majority of the test case is here, setup is done below. |
|
68 |
*/ |
|
69 |
private void createSSLEngines() throws Exception { |
|
70 |
ssle1 = sslc.createSSLEngine("client", 1); |
|
71 |
ssle1.setUseClientMode(true); |
|
72 |
||
73 |
ssle2 = sslc.createSSLEngine(); |
|
74 |
ssle2.setUseClientMode(false); |
|
75 |
ssle2.setNeedClientAuth(true); |
|
76 |
} |
|
77 |
||
78 |
private void runTest() throws Exception { |
|
79 |
boolean dataDone = false; |
|
80 |
||
81 |
createSSLEngines(); |
|
82 |
createBuffers(); |
|
83 |
||
84 |
SSLEngineResult result1; // ssle1's results from last operation |
|
85 |
SSLEngineResult result2; // ssle2's results from last operation |
|
86 |
||
87 |
while (!isEngineClosed(ssle1) || !isEngineClosed(ssle2)) { |
|
88 |
||
89 |
log("================"); |
|
90 |
||
91 |
result1 = ssle1.wrap(appOutArray1, oneToTwo); |
|
92 |
result2 = ssle2.wrap(appOut2, twoToOne); |
|
93 |
||
94 |
log("wrap1: " + result1); |
|
95 |
log("oneToTwo = " + oneToTwo); |
|
96 |
log(""); |
|
97 |
||
98 |
log("wrap2: " + result2); |
|
99 |
log("twoToOne = " + twoToOne); |
|
100 |
||
101 |
runDelegatedTasks(result1, ssle1); |
|
102 |
runDelegatedTasks(result2, ssle2); |
|
103 |
||
104 |
oneToTwo.flip(); |
|
105 |
twoToOne.flip(); |
|
106 |
||
107 |
log("----"); |
|
108 |
||
109 |
result1 = ssle1.unwrap(twoToOne, appInArray1); |
|
110 |
result2 = ssle2.unwrap(oneToTwo, appIn2); |
|
111 |
||
112 |
log("unwrap1: " + result1); |
|
113 |
log("twoToOne = " + twoToOne); |
|
114 |
log(""); |
|
115 |
||
116 |
log("unwrap2: " + result2); |
|
117 |
log("oneToTwo = " + oneToTwo); |
|
118 |
||
119 |
runDelegatedTasks(result1, ssle1); |
|
120 |
runDelegatedTasks(result2, ssle2); |
|
121 |
||
122 |
oneToTwo.compact(); |
|
123 |
twoToOne.compact(); |
|
124 |
||
125 |
/* |
|
126 |
* If we've transfered all the data between app1 and app2, |
|
127 |
* we try to close and see what that gets us. |
|
128 |
*/ |
|
129 |
if (!dataDone) { |
|
130 |
boolean done = true; |
|
131 |
||
132 |
for (int i = 0; i < appOutArray1.length; i++) { |
|
133 |
if (appOutArray1[i].remaining() != 0) { |
|
134 |
done = false; |
|
135 |
} |
|
136 |
} |
|
137 |
||
138 |
if (appOut2.remaining() != 0) { |
|
139 |
done = false; |
|
140 |
} |
|
141 |
||
142 |
if (done) { |
|
143 |
log("Closing ssle1's *OUTBOUND*..."); |
|
144 |
for (int i = 0; i < appOutArray1.length; i++) { |
|
145 |
appOutArray1[i].rewind(); |
|
146 |
} |
|
147 |
ssle1.closeOutbound(); |
|
148 |
dataDone = true; |
|
149 |
} |
|
150 |
} |
|
151 |
} |
|
152 |
checkTransfer(appOutArray1, appIn2); |
|
153 |
appInArray1[appInArray1.length - 1].limit( |
|
154 |
appInArray1[appInArray1.length - 1].position()); |
|
155 |
checkTransfer(appInArray1, appOut2); |
|
156 |
} |
|
157 |
||
158 |
public static void main(String args[]) throws Exception { |
|
159 |
||
160 |
Arrays test; |
|
161 |
||
162 |
test = new Arrays(); |
|
163 |
||
164 |
test.createSSLEngines(); |
|
165 |
||
166 |
test.runTest(); |
|
167 |
||
168 |
System.out.println("Test Passed."); |
|
169 |
} |
|
170 |
||
171 |
/* |
|
172 |
* ********************************************************** |
|
173 |
* Majority of the test case is above, below is just setup stuff |
|
174 |
* ********************************************************** |
|
175 |
*/ |
|
176 |
||
177 |
public Arrays() throws Exception { |
|
178 |
sslc = getSSLContext(keyFilename, trustFilename); |
|
179 |
} |
|
180 |
||
181 |
/* |
|
182 |
* Create an initialized SSLContext to use for this test. |
|
183 |
*/ |
|
184 |
private SSLContext getSSLContext(String keyFile, String trustFile) |
|
185 |
throws Exception { |
|
186 |
||
187 |
KeyStore ks = KeyStore.getInstance("JKS"); |
|
188 |
KeyStore ts = KeyStore.getInstance("JKS"); |
|
189 |
||
190 |
char[] passphrase = "passphrase".toCharArray(); |
|
191 |
||
192 |
ks.load(new FileInputStream(keyFile), passphrase); |
|
193 |
ts.load(new FileInputStream(trustFile), passphrase); |
|
194 |
||
195 |
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); |
|
196 |
kmf.init(ks, passphrase); |
|
197 |
||
198 |
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); |
|
199 |
tmf.init(ts); |
|
200 |
||
201 |
SSLContext sslCtx = SSLContext.getInstance("TLS"); |
|
202 |
||
203 |
sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); |
|
204 |
||
205 |
return sslCtx; |
|
206 |
} |
|
207 |
||
208 |
private void createBuffers() { |
|
209 |
// Size the buffers as appropriate. |
|
210 |
||
211 |
SSLSession session = ssle1.getSession(); |
|
212 |
int appBufferMax = session.getApplicationBufferSize(); |
|
213 |
int netBufferMax = session.getPacketBufferSize(); |
|
214 |
||
215 |
appIn2 = ByteBuffer.allocateDirect(appBufferMax + 50); |
|
216 |
||
217 |
oneToTwo = ByteBuffer.allocateDirect(netBufferMax); |
|
218 |
twoToOne = ByteBuffer.allocateDirect(netBufferMax); |
|
219 |
||
220 |
ByteBuffer strBB = ByteBuffer.wrap( |
|
221 |
"Hi Engine2, I'm SSLEngine1, So Be it" .getBytes()); |
|
222 |
||
223 |
strBB.position(0); |
|
224 |
strBB.limit(5); |
|
225 |
ByteBuffer appOut1a = strBB.slice(); |
|
226 |
||
227 |
strBB.position(5); |
|
228 |
strBB.limit(15); |
|
229 |
ByteBuffer appOut1b = strBB.slice(); |
|
230 |
||
231 |
strBB.position(15); |
|
232 |
strBB.limit(strBB.capacity()); |
|
233 |
ByteBuffer appOut1c = strBB.slice(); |
|
234 |
||
235 |
strBB.rewind(); |
|
236 |
||
237 |
appOutArray1 = new ByteBuffer [] { appOut1a, appOut1b, appOut1c }; |
|
238 |
||
239 |
appOut2 = ByteBuffer.wrap("Hello Engine1, I'm SSLEngine2".getBytes()); |
|
240 |
||
241 |
ByteBuffer appIn1a = ByteBuffer.allocateDirect(5); |
|
242 |
ByteBuffer appIn1b = ByteBuffer.allocateDirect(10); |
|
243 |
ByteBuffer appIn1c = ByteBuffer.allocateDirect(appBufferMax + 50); |
|
244 |
appInArray1 = new ByteBuffer [] { appIn1a, appIn1b, appIn1c }; |
|
245 |
||
246 |
log("AppOut1a = " + appOut1a); |
|
247 |
log("AppOut1a = " + appOut1b); |
|
248 |
log("AppOut1a = " + appOut1c); |
|
249 |
log("AppOut2 = " + appOut2); |
|
250 |
log(""); |
|
251 |
} |
|
252 |
||
253 |
private static void runDelegatedTasks(SSLEngineResult result, |
|
254 |
SSLEngine engine) throws Exception { |
|
255 |
||
256 |
if (result.getHandshakeStatus() == HandshakeStatus.NEED_TASK) { |
|
257 |
Runnable runnable; |
|
258 |
while ((runnable = engine.getDelegatedTask()) != null) { |
|
259 |
log("running delegated task..."); |
|
260 |
runnable.run(); |
|
261 |
} |
|
262 |
} |
|
263 |
} |
|
264 |
||
265 |
private static boolean isEngineClosed(SSLEngine engine) { |
|
266 |
return (engine.isOutboundDone() && engine.isInboundDone()); |
|
267 |
} |
|
268 |
||
269 |
private static void checkTransfer(ByteBuffer [] a, ByteBuffer b) |
|
270 |
throws Exception { |
|
271 |
||
272 |
b.flip(); |
|
273 |
||
274 |
for (int i = 0; i < a.length; i++) { |
|
275 |
a[i].rewind(); |
|
276 |
||
277 |
b.limit(b.position() + a[i].remaining()); |
|
278 |
||
279 |
if (!a[i].equals(b)) { |
|
280 |
throw new Exception("Data didn't transfer cleanly"); |
|
281 |
} |
|
282 |
||
283 |
b.position(b.limit()); |
|
284 |
} |
|
285 |
||
286 |
log("Data transferred cleanly"); |
|
287 |
} |
|
288 |
||
289 |
private static void log(String str) { |
|
290 |
if (debug) { |
|
291 |
System.out.println(str); |
|
292 |
} |
|
293 |
} |
|
294 |
} |