author | xuelei |
Wed, 05 Mar 2014 07:24:34 +0000 | |
changeset 23052 | 241885315119 |
parent 22047 | jdk/test/sun/security/ssl/templates/SSLSocketSSLEngineTemplate.java@7980346542b6 |
child 30904 | ec0224270f90 |
permissions | -rw-r--r-- |
10893
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
1 |
/* |
16913 | 2 |
* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. |
10893
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
4 |
* |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
8 |
* |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
13 |
* accompanied this code). |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
14 |
* |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
18 |
* |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
21 |
* questions. |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
22 |
*/ |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
23 |
|
16913 | 24 |
// |
25 |
// SunJSSE does not support dynamic system properties, no way to re-use |
|
26 |
// system properties in samevm/agentvm mode. |
|
27 |
// |
|
28 |
||
10893
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
29 |
/* |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
30 |
* @test |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
31 |
* @bug 7105780 |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
32 |
* @summary Add SSLSocket client/SSLEngine server to templates directory. |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
33 |
* @run main/othervm SSLSocketSSLEngineTemplate |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
34 |
*/ |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
35 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
36 |
/** |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
37 |
* A SSLSocket/SSLEngine interop test case. This is not the way to |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
38 |
* code SSLEngine-based servers, but works for what we need to do here, |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
39 |
* which is to make sure that SSLEngine/SSLSockets can talk to each other. |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
40 |
* SSLEngines can use direct or indirect buffers, and different code |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
41 |
* is used to get at the buffer contents internally, so we test that here. |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
42 |
* |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
43 |
* The test creates one SSLSocket (client) and one SSLEngine (server). |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
44 |
* The SSLSocket talks to a raw ServerSocket, and the server code |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
45 |
* does the translation between byte [] and ByteBuffers that the SSLEngine |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
46 |
* can use. The "transport" layer consists of a Socket Input/OutputStream |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
47 |
* and two byte buffers for the SSLEngines: think of them |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
48 |
* as directly connected pipes. |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
49 |
* |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
50 |
* Again, this is a *very* simple example: real code will be much more |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
51 |
* involved. For example, different threading and I/O models could be |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
52 |
* used, transport mechanisms could close unexpectedly, and so on. |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
53 |
* |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
54 |
* When this application runs, notice that several messages |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
55 |
* (wrap/unwrap) pass before any application data is consumed or |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
56 |
* produced. (For more information, please see the SSL/TLS |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
57 |
* specifications.) There may several steps for a successful handshake, |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
58 |
* so it's typical to see the following series of operations: |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
59 |
* |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
60 |
* client server message |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
61 |
* ====== ====== ======= |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
62 |
* write() ... ClientHello |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
63 |
* ... unwrap() ClientHello |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
64 |
* ... wrap() ServerHello/Certificate |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
65 |
* read() ... ServerHello/Certificate |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
66 |
* write() ... ClientKeyExchange |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
67 |
* write() ... ChangeCipherSpec |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
68 |
* write() ... Finished |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
69 |
* ... unwrap() ClientKeyExchange |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
70 |
* ... unwrap() ChangeCipherSpec |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
71 |
* ... unwrap() Finished |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
72 |
* ... wrap() ChangeCipherSpec |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
73 |
* ... wrap() Finished |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
74 |
* read() ... ChangeCipherSpec |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
75 |
* read() ... Finished |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
76 |
*/ |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
77 |
import javax.net.ssl.*; |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
78 |
import javax.net.ssl.SSLEngineResult.*; |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
79 |
import java.io.*; |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
80 |
import java.net.*; |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
81 |
import java.security.*; |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
82 |
import java.nio.*; |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
83 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
84 |
public class SSLSocketSSLEngineTemplate { |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
85 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
86 |
/* |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
87 |
* Enables logging of the SSL/TLS operations. |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
88 |
*/ |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
89 |
private static boolean logging = true; |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
90 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
91 |
/* |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
92 |
* Enables the JSSE system debugging system property: |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
93 |
* |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
94 |
* -Djavax.net.debug=all |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
95 |
* |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
96 |
* This gives a lot of low-level information about operations underway, |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
97 |
* including specific handshake messages, and might be best examined |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
98 |
* after gaining some familiarity with this application. |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
99 |
*/ |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
100 |
private static boolean debug = false; |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
101 |
private SSLContext sslc; |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
102 |
private SSLEngine serverEngine; // server-side SSLEngine |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
103 |
private SSLSocket sslSocket; // client-side socket |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
104 |
private ServerSocket serverSocket; // server-side Socket, generates the... |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
105 |
private Socket socket; // server-side socket that will read |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
106 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
107 |
private final byte[] serverMsg = |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
108 |
"Hi there Client, I'm a Server.".getBytes(); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
109 |
private final byte[] clientMsg = |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
110 |
"Hello Server, I'm a Client! Pleased to meet you!".getBytes(); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
111 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
112 |
private ByteBuffer serverOut; // write side of serverEngine |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
113 |
private ByteBuffer serverIn; // read side of serverEngine |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
114 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
115 |
private volatile Exception clientException; |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
116 |
private volatile Exception serverException; |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
117 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
118 |
/* |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
119 |
* For data transport, this example uses local ByteBuffers. |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
120 |
*/ |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
121 |
private ByteBuffer cTOs; // "reliable" transport client->server |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
122 |
private ByteBuffer sTOc; // "reliable" transport server->client |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
123 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
124 |
/* |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
125 |
* The following is to set up the keystores/trust material. |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
126 |
*/ |
23052
241885315119
8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents:
22047
diff
changeset
|
127 |
private static final String pathToStores = "../etc"; |
10893
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
128 |
private static final String keyStoreFile = "keystore"; |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
129 |
private static final String trustStoreFile = "truststore"; |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
130 |
private static final String passwd = "passphrase"; |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
131 |
private static String keyFilename = |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
132 |
System.getProperty("test.src", ".") + "/" + pathToStores |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
133 |
+ "/" + keyStoreFile; |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
134 |
private static String trustFilename = |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
135 |
System.getProperty("test.src", ".") + "/" + pathToStores |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
136 |
+ "/" + trustStoreFile; |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
137 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
138 |
/* |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
139 |
* Main entry point for this test. |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
140 |
*/ |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
141 |
public static void main(String args[]) throws Exception { |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
142 |
if (debug) { |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
143 |
System.setProperty("javax.net.debug", "all"); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
144 |
} |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
145 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
146 |
String [] protocols = new String [] { |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
147 |
"SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2" }; |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
148 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
149 |
for (String protocol : protocols) { |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
150 |
log("Testing " + protocol); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
151 |
/* |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
152 |
* Run the tests with direct and indirect buffers. |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
153 |
*/ |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
154 |
SSLSocketSSLEngineTemplate test = |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
155 |
new SSLSocketSSLEngineTemplate(protocol); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
156 |
test.runTest(true); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
157 |
test.runTest(false); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
158 |
} |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
159 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
160 |
System.out.println("Test Passed."); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
161 |
} |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
162 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
163 |
/* |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
164 |
* Create an initialized SSLContext to use for these tests. |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
165 |
*/ |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
166 |
public SSLSocketSSLEngineTemplate(String protocol) throws Exception { |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
167 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
168 |
KeyStore ks = KeyStore.getInstance("JKS"); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
169 |
KeyStore ts = KeyStore.getInstance("JKS"); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
170 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
171 |
char[] passphrase = "passphrase".toCharArray(); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
172 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
173 |
ks.load(new FileInputStream(keyFilename), passphrase); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
174 |
ts.load(new FileInputStream(trustFilename), passphrase); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
175 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
176 |
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
177 |
kmf.init(ks, passphrase); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
178 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
179 |
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
180 |
tmf.init(ts); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
181 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
182 |
SSLContext sslCtx = SSLContext.getInstance(protocol); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
183 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
184 |
sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
185 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
186 |
sslc = sslCtx; |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
187 |
} |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
188 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
189 |
/* |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
190 |
* Run the test. |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
191 |
* |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
192 |
* Sit in a tight loop, with the server engine calling wrap/unwrap |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
193 |
* regardless of whether data is available or not. We do this until |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
194 |
* we get the application data. Then we shutdown and go to the next one. |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
195 |
* |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
196 |
* The main loop handles all of the I/O phases of the SSLEngine's |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
197 |
* lifetime: |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
198 |
* |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
199 |
* initial handshaking |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
200 |
* application data transfer |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
201 |
* engine closing |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
202 |
* |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
203 |
* One could easily separate these phases into separate |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
204 |
* sections of code. |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
205 |
*/ |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
206 |
private void runTest(boolean direct) throws Exception { |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
207 |
boolean serverClose = direct; |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
208 |
|
21943
0fdb9441797f
8021418: Intermittent: SSLSocketSSLEngineTemplate.java test fails with timeout
juh
parents:
18784
diff
changeset
|
209 |
serverSocket = new ServerSocket(); |
0fdb9441797f
8021418: Intermittent: SSLSocketSSLEngineTemplate.java test fails with timeout
juh
parents:
18784
diff
changeset
|
210 |
serverSocket.setReuseAddress(false); |
0fdb9441797f
8021418: Intermittent: SSLSocketSSLEngineTemplate.java test fails with timeout
juh
parents:
18784
diff
changeset
|
211 |
serverSocket.bind(null); |
10893
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
212 |
int port = serverSocket.getLocalPort(); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
213 |
Thread thread = createClientThread(port, serverClose); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
214 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
215 |
socket = serverSocket.accept(); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
216 |
socket.setSoTimeout(500); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
217 |
serverSocket.close(); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
218 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
219 |
createSSLEngine(); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
220 |
createBuffers(direct); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
221 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
222 |
try { |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
223 |
boolean closed = false; |
22047
7980346542b6
8028562: Test SSLSocketSSLEngineTemplate.java intermittent failed with "Data length error"
xuelei
parents:
21943
diff
changeset
|
224 |
// will try to read one more time in case client message |
7980346542b6
8028562: Test SSLSocketSSLEngineTemplate.java intermittent failed with "Data length error"
xuelei
parents:
21943
diff
changeset
|
225 |
// is fragmented to multiple pieces |
7980346542b6
8028562: Test SSLSocketSSLEngineTemplate.java intermittent failed with "Data length error"
xuelei
parents:
21943
diff
changeset
|
226 |
boolean retry = true; |
10893
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
227 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
228 |
InputStream is = socket.getInputStream(); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
229 |
OutputStream os = socket.getOutputStream(); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
230 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
231 |
SSLEngineResult serverResult; // results from last operation |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
232 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
233 |
/* |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
234 |
* Examining the SSLEngineResults could be much more involved, |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
235 |
* and may alter the overall flow of the application. |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
236 |
* |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
237 |
* For example, if we received a BUFFER_OVERFLOW when trying |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
238 |
* to write to the output pipe, we could reallocate a larger |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
239 |
* pipe, but instead we wait for the peer to drain it. |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
240 |
*/ |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
241 |
byte[] inbound = new byte[8192]; |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
242 |
byte[] outbound = new byte[8192]; |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
243 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
244 |
while (!isEngineClosed(serverEngine)) { |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
245 |
int len = 0; |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
246 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
247 |
// Inbound data |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
248 |
log("================"); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
249 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
250 |
// Read from the Client side. |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
251 |
try { |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
252 |
len = is.read(inbound); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
253 |
if (len == -1) { |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
254 |
throw new Exception("Unexpected EOF"); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
255 |
} |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
256 |
cTOs.put(inbound, 0, len); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
257 |
} catch (SocketTimeoutException ste) { |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
258 |
// swallow. Nothing yet, probably waiting on us. |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
259 |
} |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
260 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
261 |
cTOs.flip(); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
262 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
263 |
serverResult = serverEngine.unwrap(cTOs, serverIn); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
264 |
log("server unwrap: ", serverResult); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
265 |
runDelegatedTasks(serverResult, serverEngine); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
266 |
cTOs.compact(); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
267 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
268 |
// Outbound data |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
269 |
log("----"); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
270 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
271 |
serverResult = serverEngine.wrap(serverOut, sTOc); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
272 |
log("server wrap: ", serverResult); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
273 |
runDelegatedTasks(serverResult, serverEngine); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
274 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
275 |
sTOc.flip(); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
276 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
277 |
if ((len = sTOc.remaining()) != 0) { |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
278 |
sTOc.get(outbound, 0, len); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
279 |
os.write(outbound, 0, len); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
280 |
// Give the other side a chance to process |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
281 |
} |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
282 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
283 |
sTOc.compact(); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
284 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
285 |
if (!closed && (serverOut.remaining() == 0)) { |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
286 |
closed = true; |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
287 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
288 |
/* |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
289 |
* We'll alternate initiatating the shutdown. |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
290 |
* When the server initiates, it will take one more |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
291 |
* loop, but tests the orderly shutdown. |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
292 |
*/ |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
293 |
if (serverClose) { |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
294 |
serverEngine.closeOutbound(); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
295 |
} |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
296 |
serverIn.flip(); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
297 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
298 |
/* |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
299 |
* A sanity check to ensure we got what was sent. |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
300 |
*/ |
22047
7980346542b6
8028562: Test SSLSocketSSLEngineTemplate.java intermittent failed with "Data length error"
xuelei
parents:
21943
diff
changeset
|
301 |
if (serverIn.remaining() != clientMsg.length) { |
7980346542b6
8028562: Test SSLSocketSSLEngineTemplate.java intermittent failed with "Data length error"
xuelei
parents:
21943
diff
changeset
|
302 |
if (retry && serverIn.remaining() < clientMsg.length) { |
7980346542b6
8028562: Test SSLSocketSSLEngineTemplate.java intermittent failed with "Data length error"
xuelei
parents:
21943
diff
changeset
|
303 |
log("Need to read more from client"); |
7980346542b6
8028562: Test SSLSocketSSLEngineTemplate.java intermittent failed with "Data length error"
xuelei
parents:
21943
diff
changeset
|
304 |
retry = false; |
7980346542b6
8028562: Test SSLSocketSSLEngineTemplate.java intermittent failed with "Data length error"
xuelei
parents:
21943
diff
changeset
|
305 |
continue; |
7980346542b6
8028562: Test SSLSocketSSLEngineTemplate.java intermittent failed with "Data length error"
xuelei
parents:
21943
diff
changeset
|
306 |
} else { |
7980346542b6
8028562: Test SSLSocketSSLEngineTemplate.java intermittent failed with "Data length error"
xuelei
parents:
21943
diff
changeset
|
307 |
throw new Exception("Client: Data length error"); |
7980346542b6
8028562: Test SSLSocketSSLEngineTemplate.java intermittent failed with "Data length error"
xuelei
parents:
21943
diff
changeset
|
308 |
} |
10893
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
309 |
} |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
310 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
311 |
for (int i = 0; i < clientMsg.length; i++) { |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
312 |
if (clientMsg[i] != serverIn.get()) { |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
313 |
throw new Exception("Client: Data content error"); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
314 |
} |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
315 |
} |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
316 |
serverIn.compact(); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
317 |
} |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
318 |
} |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
319 |
return; |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
320 |
} catch (Exception e) { |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
321 |
serverException = e; |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
322 |
} finally { |
18784
7f517475eeb4
8019341: Update CookieHttpsClientTest to use the newer framework.
wetmore
parents:
16913
diff
changeset
|
323 |
if (socket != null) { |
7f517475eeb4
8019341: Update CookieHttpsClientTest to use the newer framework.
wetmore
parents:
16913
diff
changeset
|
324 |
socket.close(); |
7f517475eeb4
8019341: Update CookieHttpsClientTest to use the newer framework.
wetmore
parents:
16913
diff
changeset
|
325 |
} |
10893
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
326 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
327 |
// Wait for the client to join up with us. |
18784
7f517475eeb4
8019341: Update CookieHttpsClientTest to use the newer framework.
wetmore
parents:
16913
diff
changeset
|
328 |
if (thread != null) { |
7f517475eeb4
8019341: Update CookieHttpsClientTest to use the newer framework.
wetmore
parents:
16913
diff
changeset
|
329 |
thread.join(); |
7f517475eeb4
8019341: Update CookieHttpsClientTest to use the newer framework.
wetmore
parents:
16913
diff
changeset
|
330 |
} |
7f517475eeb4
8019341: Update CookieHttpsClientTest to use the newer framework.
wetmore
parents:
16913
diff
changeset
|
331 |
|
10893
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
332 |
if (serverException != null) { |
18784
7f517475eeb4
8019341: Update CookieHttpsClientTest to use the newer framework.
wetmore
parents:
16913
diff
changeset
|
333 |
if (clientException != null) { |
7f517475eeb4
8019341: Update CookieHttpsClientTest to use the newer framework.
wetmore
parents:
16913
diff
changeset
|
334 |
serverException.initCause(clientException); |
7f517475eeb4
8019341: Update CookieHttpsClientTest to use the newer framework.
wetmore
parents:
16913
diff
changeset
|
335 |
} |
10893
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
336 |
throw serverException; |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
337 |
} |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
338 |
if (clientException != null) { |
18784
7f517475eeb4
8019341: Update CookieHttpsClientTest to use the newer framework.
wetmore
parents:
16913
diff
changeset
|
339 |
if (serverException != null) { |
7f517475eeb4
8019341: Update CookieHttpsClientTest to use the newer framework.
wetmore
parents:
16913
diff
changeset
|
340 |
clientException.initCause(serverException); |
7f517475eeb4
8019341: Update CookieHttpsClientTest to use the newer framework.
wetmore
parents:
16913
diff
changeset
|
341 |
} |
10893
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
342 |
throw clientException; |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
343 |
} |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
344 |
} |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
345 |
} |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
346 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
347 |
/* |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
348 |
* Create a client thread which does simple SSLSocket operations. |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
349 |
* We'll write and read one data packet. |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
350 |
*/ |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
351 |
private Thread createClientThread(final int port, |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
352 |
final boolean serverClose) throws Exception { |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
353 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
354 |
Thread t = new Thread("ClientThread") { |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
355 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
356 |
@Override |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
357 |
public void run() { |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
358 |
try { |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
359 |
Thread.sleep(1000); // Give server time to finish setup. |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
360 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
361 |
sslSocket = (SSLSocket) sslc.getSocketFactory(). |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
362 |
createSocket("localhost", port); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
363 |
OutputStream os = sslSocket.getOutputStream(); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
364 |
InputStream is = sslSocket.getInputStream(); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
365 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
366 |
// write(byte[]) goes in one shot. |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
367 |
os.write(clientMsg); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
368 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
369 |
byte[] inbound = new byte[2048]; |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
370 |
int pos = 0; |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
371 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
372 |
int len; |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
373 |
done: |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
374 |
while ((len = is.read(inbound, pos, 2048 - pos)) != -1) { |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
375 |
pos += len; |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
376 |
// Let the client do the closing. |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
377 |
if ((pos == serverMsg.length) && !serverClose) { |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
378 |
sslSocket.close(); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
379 |
break done; |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
380 |
} |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
381 |
} |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
382 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
383 |
if (pos != serverMsg.length) { |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
384 |
throw new Exception("Client: Data length error"); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
385 |
} |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
386 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
387 |
for (int i = 0; i < serverMsg.length; i++) { |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
388 |
if (inbound[i] != serverMsg[i]) { |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
389 |
throw new Exception("Client: Data content error"); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
390 |
} |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
391 |
} |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
392 |
} catch (Exception e) { |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
393 |
clientException = e; |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
394 |
} |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
395 |
} |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
396 |
}; |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
397 |
t.start(); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
398 |
return t; |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
399 |
} |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
400 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
401 |
/* |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
402 |
* Using the SSLContext created during object creation, |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
403 |
* create/configure the SSLEngines we'll use for this test. |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
404 |
*/ |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
405 |
private void createSSLEngine() throws Exception { |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
406 |
/* |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
407 |
* Configure the serverEngine to act as a server in the SSL/TLS |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
408 |
* handshake. |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
409 |
*/ |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
410 |
serverEngine = sslc.createSSLEngine(); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
411 |
serverEngine.setUseClientMode(false); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
412 |
serverEngine.getNeedClientAuth(); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
413 |
} |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
414 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
415 |
/* |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
416 |
* Create and size the buffers appropriately. |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
417 |
*/ |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
418 |
private void createBuffers(boolean direct) { |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
419 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
420 |
SSLSession session = serverEngine.getSession(); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
421 |
int appBufferMax = session.getApplicationBufferSize(); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
422 |
int netBufferMax = session.getPacketBufferSize(); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
423 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
424 |
/* |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
425 |
* We'll make the input buffers a bit bigger than the max needed |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
426 |
* size, so that unwrap()s following a successful data transfer |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
427 |
* won't generate BUFFER_OVERFLOWS. |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
428 |
* |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
429 |
* We'll use a mix of direct and indirect ByteBuffers for |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
430 |
* tutorial purposes only. In reality, only use direct |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
431 |
* ByteBuffers when they give a clear performance enhancement. |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
432 |
*/ |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
433 |
if (direct) { |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
434 |
serverIn = ByteBuffer.allocateDirect(appBufferMax + 50); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
435 |
cTOs = ByteBuffer.allocateDirect(netBufferMax); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
436 |
sTOc = ByteBuffer.allocateDirect(netBufferMax); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
437 |
} else { |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
438 |
serverIn = ByteBuffer.allocate(appBufferMax + 50); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
439 |
cTOs = ByteBuffer.allocate(netBufferMax); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
440 |
sTOc = ByteBuffer.allocate(netBufferMax); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
441 |
} |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
442 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
443 |
serverOut = ByteBuffer.wrap(serverMsg); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
444 |
} |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
445 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
446 |
/* |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
447 |
* If the result indicates that we have outstanding tasks to do, |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
448 |
* go ahead and run them in this thread. |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
449 |
*/ |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
450 |
private static void runDelegatedTasks(SSLEngineResult result, |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
451 |
SSLEngine engine) throws Exception { |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
452 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
453 |
if (result.getHandshakeStatus() == HandshakeStatus.NEED_TASK) { |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
454 |
Runnable runnable; |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
455 |
while ((runnable = engine.getDelegatedTask()) != null) { |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
456 |
log("\trunning delegated task..."); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
457 |
runnable.run(); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
458 |
} |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
459 |
HandshakeStatus hsStatus = engine.getHandshakeStatus(); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
460 |
if (hsStatus == HandshakeStatus.NEED_TASK) { |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
461 |
throw new Exception( |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
462 |
"handshake shouldn't need additional tasks"); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
463 |
} |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
464 |
log("\tnew HandshakeStatus: " + hsStatus); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
465 |
} |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
466 |
} |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
467 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
468 |
private static boolean isEngineClosed(SSLEngine engine) { |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
469 |
return (engine.isOutboundDone() && engine.isInboundDone()); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
470 |
} |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
471 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
472 |
/* |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
473 |
* Logging code |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
474 |
*/ |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
475 |
private static boolean resultOnce = true; |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
476 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
477 |
private static void log(String str, SSLEngineResult result) { |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
478 |
if (!logging) { |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
479 |
return; |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
480 |
} |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
481 |
if (resultOnce) { |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
482 |
resultOnce = false; |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
483 |
System.out.println("The format of the SSLEngineResult is: \n" |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
484 |
+ "\t\"getStatus() / getHandshakeStatus()\" +\n" |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
485 |
+ "\t\"bytesConsumed() / bytesProduced()\"\n"); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
486 |
} |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
487 |
HandshakeStatus hsStatus = result.getHandshakeStatus(); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
488 |
log(str |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
489 |
+ result.getStatus() + "/" + hsStatus + ", " |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
490 |
+ result.bytesConsumed() + "/" + result.bytesProduced() |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
491 |
+ " bytes"); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
492 |
if (hsStatus == HandshakeStatus.FINISHED) { |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
493 |
log("\t...ready for application data"); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
494 |
} |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
495 |
} |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
496 |
|
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
497 |
private static void log(String str) { |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
498 |
if (logging) { |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
499 |
System.out.println(str); |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
500 |
} |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
501 |
} |
0c145634e745
7105780: Add SSLSocket client/SSLEngine server to templates directory
wetmore
parents:
diff
changeset
|
502 |
} |