1111import java .nio .channels .SocketChannel ;
1212import java .security .KeyStore ;
1313import java .util .Arrays ;
14+ import java .util .concurrent .ExecutionException ;
1415
1516import javax .net .ssl .KeyManagerFactory ;
1617import javax .net .ssl .SSLContext ;
1718import javax .net .ssl .SSLEngine ;
19+ import javax .net .ssl .SSLSession ;
1820import javax .net .ssl .TrustManager ;
1921
2022import org .junit .After ;
2729import org .threadly .litesockets .utils .SSLProcessor ;
2830import org .threadly .litesockets .utils .SSLProcessor .EncryptionException ;
2931import org .threadly .litesockets .utils .SSLUtils ;
32+ import org .threadly .test .concurrent .TestCondition ;
3033
3134public class SSLProcessorTests {
3235 static final String STRING = "hello" ;
@@ -48,7 +51,7 @@ public class SSLProcessorTests {
4851 KeyStore KS ;
4952 KeyManagerFactory kmf ;
5053 SSLContext sslCtx ;
51-
54+
5255 @ Before
5356 public void start () throws Exception {
5457 SE = new NoThreadSocketExecuter ();
@@ -65,15 +68,15 @@ public void start() throws Exception {
6568 sslCtx .init (kmf .getKeyManagers (), myTMs , null );
6669 System .out .println (Arrays .toString (sslCtx .createSSLEngine ().getSupportedCipherSuites ()));
6770 }
68-
71+
6972 @ After
7073 public void stop () {
7174 SE .stopIfRunning ();
7275 System .gc ();
7376 System .out .println ("Used Memory:"
7477 + (Runtime .getRuntime ().totalMemory () - Runtime .getRuntime ().freeMemory ()) / (1024 *1024 ));
7578 }
76-
79+
7780 @ Test
7881 public void notEncrypted () throws EncryptionException {
7982 FakeClient fc = new FakeClient (SE );
@@ -83,45 +86,52 @@ public void notEncrypted() throws EncryptionException {
8386 MergedByteBuffers mbb2 = sp .decrypt (mbb );
8487 assertEquals (STRING , mbb2 .duplicate ().getAsString (mbb2 .remaining ()));
8588 }
86-
89+
8790 @ Test
88- public void encrypted () throws IOException , EncryptionException {
91+ public void encrypted () throws Exception {
8992 FakeClient fc = new FakeClient (SE );
9093 SSLEngine see = sslCtx .createSSLEngine ();
91- see .setEnabledCipherSuites (SIMPLE_ENCRYPT );
9294 see .setUseClientMode (true );
9395 SSLProcessor sp = new SSLProcessor (fc , see );
9496 fc .setSSLProcessor (sp );
9597 FakeClient fc2 = new FakeClient (SE );
9698 SSLEngine see2 = sslCtx .createSSLEngine ();
97- see2 .setEnabledCipherSuites (SIMPLE_ENCRYPT );
9899 see2 .setUseClientMode (false );
99- SSLProcessor sp2 = new SSLProcessor (fc2 , see2 );
100+ final SSLProcessor sp2 = new SSLProcessor (fc2 , see2 );
100101 fc2 .setSSLProcessor (sp2 );
101102 assertFalse (sp2 .isEncrypted ());
102103 assertFalse (sp .isEncrypted ());
103104 assertFalse (sp .handShakeStarted ());
104- sp .doHandShake ();
105+ ListenableFuture < SSLSession > x = sp .doHandShake ();
105106 assertTrue (sp .handShakeStarted ());
106107 assertFalse (sp2 .handShakeStarted ());
107108 sp2 .doHandShake ();
108109 assertTrue (sp2 .handShakeStarted ());
109-
110+
110111 assertFalse (sp2 .isEncrypted ());
111112 assertFalse (sp .isEncrypted ());
112- while (true ) {
113- if (fc .canWrite ()) {
114- ByteBuffer bb = fc .getWriteBuffer ();
115- fc2 .addReadBuffer (bb );
116- sp2 .decrypt (fc2 .getRead ());
117- } else if (fc2 .canWrite ()) {
118- ByteBuffer bb = fc2 .getWriteBuffer ();
119- fc .addReadBuffer (bb );
120- sp .decrypt (fc .getRead ());
121- } else {
122- break ;
113+
114+ new TestCondition (){
115+ @ Override
116+ public boolean get () {
117+ try {
118+ if (fc .canWrite ()) {
119+ ByteBuffer bb = fc .getWriteBuffer ();
120+ fc2 .addReadBuffer (bb );
121+ sp2 .decrypt (fc2 .getRead ());
122+ } else if (fc2 .canWrite ()) {
123+ ByteBuffer bb = fc2 .getWriteBuffer ();
124+ fc .addReadBuffer (bb );
125+ sp .decrypt (fc .getRead ());
126+ } else {
127+ return sp2 .isEncrypted () && sp .isEncrypted ();
128+ }
129+ } catch (Exception e ) {
130+ return false ;
131+ }
132+ return false ;
123133 }
124- }
134+ }. blockTillTrue ( 5000 );
125135 assertTrue (sp2 .isEncrypted ());
126136 assertTrue (sp .isEncrypted ());
127137 MergedByteBuffers mbb = sp .encrypt (STRINGBB .duplicate ());
@@ -155,7 +165,7 @@ public void noCommonCipher() throws IOException, EncryptionException {
155165 assertFalse (sp2 .handShakeStarted ());
156166 sp2 .doHandShake ();
157167 assertTrue (sp2 .handShakeStarted ());
158-
168+
159169 assertFalse (sp2 .isEncrypted ());
160170 assertFalse (sp .isEncrypted ());
161171 while (true ) {
@@ -173,20 +183,18 @@ public void noCommonCipher() throws IOException, EncryptionException {
173183 }
174184 assertFalse (sp2 .isEncrypted ());
175185 assertFalse (sp .isEncrypted ());
176-
186+
177187 }
178188
179189 @ Test
180190 public void largeEncrypted () throws IOException , EncryptionException {
181191 FakeClient fc = new FakeClient (SE );
182192 SSLEngine see = sslCtx .createSSLEngine ();
183- see .setEnabledCipherSuites (SIMPLE_ENCRYPT );
184193 see .setUseClientMode (true );
185194 SSLProcessor sp = new SSLProcessor (fc , see );
186195 fc .setSSLProcessor (sp );
187196 FakeClient fc2 = new FakeClient (SE );
188197 SSLEngine see2 = sslCtx .createSSLEngine ();
189- see2 .setEnabledCipherSuites (SIMPLE_ENCRYPT );
190198 see2 .setUseClientMode (false );
191199 SSLProcessor sp2 = new SSLProcessor (fc2 , see2 );
192200 fc2 .setSSLProcessor (sp2 );
@@ -217,19 +225,19 @@ public void largeEncrypted() throws IOException, EncryptionException {
217225 }
218226 assertEquals (LARGE_STRING , dmbb .getAsString (dmbb .remaining ()));
219227 }
220-
228+
221229 public static class FakeClient extends Client {
222230 MergedByteBuffers writeBuffers = new ReuseableMergedByteBuffers (false );
223231 SSLProcessor sp ;
224232
225233 public FakeClient (SocketExecuterCommonBase se ) {
226234 super (se );
227235 }
228-
236+
229237 public void setSSLProcessor (SSLProcessor sp ) {
230238 this .sp = sp ;
231239 }
232-
240+
233241 @ Override
234242 public void addReadBuffer (ByteBuffer bb ) {
235243 super .addReadBuffer (bb );
@@ -252,12 +260,12 @@ public ListenableFuture<Boolean> connect() {
252260
253261 @ Override
254262 protected void setConnectionStatus (Throwable t ) {
255-
263+
256264 }
257265
258266 @ Override
259267 public void setConnectionTimeout (int timeout ) {
260-
268+
261269 }
262270
263271 @ Override
@@ -277,7 +285,7 @@ protected ByteBuffer getWriteBuffer() {
277285
278286 @ Override
279287 protected void reduceWrite (int size ) {
280-
288+
281289 }
282290
283291 @ Override
@@ -292,7 +300,7 @@ public WireProtocol getProtocol() {
292300
293301 @ Override
294302 public void close (Throwable error ) {
295-
303+
296304 }
297305
298306 @ Override
@@ -304,7 +312,7 @@ public SocketAddress getRemoteSocketAddress() {
304312 public SocketAddress getLocalSocketAddress () {
305313 return null ;
306314 }
307-
315+
308316 @ Override
309317 public ListenableFuture <?> write (final ByteBuffer bb ) {
310318 try {
@@ -322,12 +330,12 @@ public ClientOptions clientOptions() {
322330
323331 @ Override
324332 protected void doSocketRead (boolean doLocal ) {
325-
333+
326334 }
327335
328336 @ Override
329337 protected void doSocketWrite (boolean doLocal ) {
330-
338+
331339 }
332340
333341 @ Override
0 commit comments