1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package libcore.javax.net.ssl;
18
19import java.io.Closeable;
20import java.security.InvalidAlgorithmParameterException;
21import java.security.KeyManagementException;
22import java.security.KeyStore;
23import java.security.KeyStoreException;
24import java.security.NoSuchAlgorithmException;
25import java.security.Provider;
26import java.security.Security;
27import java.security.UnrecoverableKeyException;
28import java.util.ArrayList;
29import java.util.Arrays;
30import java.util.List;
31import java.util.concurrent.Callable;
32import libcore.java.security.StandardNames;
33import javax.net.ServerSocketFactory;
34import javax.net.SocketFactory;
35import javax.net.ssl.KeyManager;
36import javax.net.ssl.KeyManagerFactory;
37import javax.net.ssl.KeyManagerFactorySpi;
38import javax.net.ssl.ManagerFactoryParameters;
39import javax.net.ssl.SSLContext;
40import javax.net.ssl.SSLEngine;
41import javax.net.ssl.SSLServerSocket;
42import javax.net.ssl.SSLServerSocketFactory;
43import javax.net.ssl.SSLSessionContext;
44import javax.net.ssl.SSLSocket;
45import javax.net.ssl.SSLSocketFactory;
46import javax.net.ssl.TrustManager;
47import javax.net.ssl.TrustManagerFactory;
48import javax.net.ssl.TrustManagerFactorySpi;
49import javax.net.ssl.X509KeyManager;
50import junit.framework.AssertionFailedError;
51import junit.framework.TestCase;
52
53public class SSLContextTest extends TestCase {
54
55    public void test_SSLContext_getDefault() throws Exception {
56        SSLContext sslContext = SSLContext.getDefault();
57        assertNotNull(sslContext);
58        try {
59            sslContext.init(null, null, null);
60            fail();
61        } catch (KeyManagementException expected) {
62        }
63    }
64
65    public void test_SSLContext_setDefault() throws Exception {
66        try {
67            SSLContext.setDefault(null);
68            fail();
69        } catch (NullPointerException expected) {
70        }
71
72        SSLContext defaultContext = SSLContext.getDefault();
73        for (String protocol : StandardNames.SSL_CONTEXT_PROTOCOLS) {
74            SSLContext oldContext = SSLContext.getDefault();
75            assertNotNull(oldContext);
76            SSLContext newContext = SSLContext.getInstance(protocol);
77            assertNotNull(newContext);
78            assertNotSame(oldContext, newContext);
79            SSLContext.setDefault(newContext);
80            assertSame(newContext, SSLContext.getDefault());
81        }
82        SSLContext.setDefault(defaultContext);
83    }
84
85    public void test_SSLContext_defaultConfiguration() throws Exception {
86        SSLConfigurationAsserts.assertSSLContextDefaultConfiguration(SSLContext.getDefault());
87
88        for (String protocol : StandardNames.SSL_CONTEXT_PROTOCOLS) {
89            SSLContext sslContext = SSLContext.getInstance(protocol);
90            if (!protocol.equals(StandardNames.SSL_CONTEXT_PROTOCOLS_DEFAULT)) {
91                sslContext.init(null, null, null);
92            }
93            SSLConfigurationAsserts.assertSSLContextDefaultConfiguration(sslContext);
94        }
95    }
96
97    public void test_SSLContext_pskOnlyConfiguration_defaultProviderOnly() throws Exception {
98        // Test the scenario where only a PSKKeyManager is provided and no TrustManagers are
99        // provided.
100        SSLContext sslContext = SSLContext.getInstance("TLS");
101        sslContext.init(
102                new KeyManager[] {
103                        PSKKeyManagerProxy.getConscryptPSKKeyManager(new PSKKeyManagerProxy())
104                },
105                new TrustManager[0],
106                null);
107        List<String> expectedCipherSuites =
108                new ArrayList<String>(StandardNames.CIPHER_SUITES_DEFAULT_PSK);
109        expectedCipherSuites.add(StandardNames.CIPHER_SUITE_SECURE_RENEGOTIATION);
110        assertEnabledCipherSuites(expectedCipherSuites, sslContext);
111    }
112
113    public void test_SSLContext_x509AndPskConfiguration_defaultProviderOnly() throws Exception {
114        // Test the scenario where an X509TrustManager and PSKKeyManager are provided.
115        SSLContext sslContext = SSLContext.getInstance("TLS");
116        sslContext.init(
117                new KeyManager[] {
118                        PSKKeyManagerProxy.getConscryptPSKKeyManager(new PSKKeyManagerProxy())
119                },
120                null, // Use default trust managers, one of which is an X.509 one.
121                null);
122        List<String> expectedCipherSuites =
123                new ArrayList<String>(StandardNames.CIPHER_SUITES_DEFAULT_PSK);
124        expectedCipherSuites.addAll(StandardNames.CIPHER_SUITES_DEFAULT);
125        assertEnabledCipherSuites(expectedCipherSuites, sslContext);
126
127        // Test the scenario where an X509KeyManager and PSKKeyManager are provided.
128        sslContext = SSLContext.getInstance("TLS");
129        // Just an arbitrary X509KeyManager -- it won't be invoked in this test.
130        X509KeyManager x509KeyManager = new RandomPrivateKeyX509ExtendedKeyManager(null);
131        sslContext.init(
132                new KeyManager[] {
133                        x509KeyManager,
134                        PSKKeyManagerProxy.getConscryptPSKKeyManager(new PSKKeyManagerProxy())
135                },
136                new TrustManager[0],
137                null);
138        assertEnabledCipherSuites(expectedCipherSuites, sslContext);
139    }
140
141    public void test_SSLContext_emptyConfiguration_defaultProviderOnly() throws Exception {
142        // Test the scenario where neither X.509 nor PSK KeyManagers or TrustManagers are provided.
143        SSLContext sslContext = SSLContext.getInstance("TLS");
144        sslContext.init(
145                new KeyManager[0],
146                new TrustManager[0],
147                null);
148        assertEnabledCipherSuites(
149                Arrays.asList(StandardNames.CIPHER_SUITE_SECURE_RENEGOTIATION),
150                sslContext);
151    }
152
153    public void test_SSLContext_init_correctProtocolVersionsEnabled() throws Exception {
154        for (String tlsVersion : StandardNames.SSL_CONTEXT_PROTOCOLS) {
155            // Don't test the "Default" instance.
156            if (StandardNames.SSL_CONTEXT_PROTOCOLS_DEFAULT.equals(tlsVersion)) {
157                continue;
158            }
159
160            SSLContext context = SSLContext.getInstance(tlsVersion);
161            context.init(null, null, null);
162
163            StandardNames.assertSSLContextEnabledProtocols(tlsVersion, ((SSLSocket) (context.getSocketFactory()
164                    .createSocket())).getEnabledProtocols());
165            StandardNames.assertSSLContextEnabledProtocols(tlsVersion, ((SSLServerSocket) (context
166                    .getServerSocketFactory().createServerSocket())).getEnabledProtocols());
167            StandardNames.assertSSLContextEnabledProtocols(tlsVersion, context.getDefaultSSLParameters()
168                    .getProtocols());
169            StandardNames.assertSSLContextEnabledProtocols(tlsVersion, context.createSSLEngine()
170                    .getEnabledProtocols());
171        }
172    }
173
174    private static void assertEnabledCipherSuites(
175            List<String> expectedCipherSuites, SSLContext sslContext) throws Exception {
176        assertContentsInOrder(
177                expectedCipherSuites, sslContext.createSSLEngine().getEnabledCipherSuites());
178        assertContentsInOrder(
179                expectedCipherSuites,
180                sslContext.createSSLEngine().getSSLParameters().getCipherSuites());
181        assertContentsInOrder(
182                expectedCipherSuites, sslContext.getSocketFactory().getDefaultCipherSuites());
183        assertContentsInOrder(
184                expectedCipherSuites, sslContext.getServerSocketFactory().getDefaultCipherSuites());
185
186        SSLSocket sslSocket = (SSLSocket) sslContext.getSocketFactory().createSocket();
187        try {
188            assertContentsInOrder(
189                    expectedCipherSuites, sslSocket.getEnabledCipherSuites());
190            assertContentsInOrder(
191                    expectedCipherSuites, sslSocket.getSSLParameters().getCipherSuites());
192        } finally {
193            closeQuietly(sslSocket);
194        }
195
196        SSLServerSocket sslServerSocket =
197                (SSLServerSocket) sslContext.getServerSocketFactory().createServerSocket();
198        try {
199            assertContentsInOrder(
200                    expectedCipherSuites, sslServerSocket.getEnabledCipherSuites());
201        } finally {
202            closeQuietly(sslSocket);
203        }
204    }
205
206    public void test_SSLContext_getInstance() throws Exception {
207        try {
208            SSLContext.getInstance(null);
209            fail();
210        } catch (NullPointerException expected) {
211        }
212        for (String protocol : StandardNames.SSL_CONTEXT_PROTOCOLS) {
213            assertNotNull(SSLContext.getInstance(protocol));
214            assertNotSame(SSLContext.getInstance(protocol),
215                          SSLContext.getInstance(protocol));
216        }
217
218        try {
219            SSLContext.getInstance(null, (String) null);
220            fail();
221        } catch (IllegalArgumentException expected) {
222        }
223        try {
224            SSLContext.getInstance(null, "");
225            fail();
226        } catch (IllegalArgumentException expected) {
227        }
228        for (String protocol : StandardNames.SSL_CONTEXT_PROTOCOLS) {
229            try {
230                SSLContext.getInstance(protocol, (String) null);
231                fail();
232            } catch (IllegalArgumentException expected) {
233            }
234        }
235        try {
236            SSLContext.getInstance(null, StandardNames.JSSE_PROVIDER_NAME);
237            fail();
238        } catch (NullPointerException expected) {
239        }
240    }
241
242    public void test_SSLContext_getProtocol() throws Exception {
243        for (String protocol : StandardNames.SSL_CONTEXT_PROTOCOLS) {
244            String protocolName = SSLContext.getInstance(protocol).getProtocol();
245            assertNotNull(protocolName);
246            assertTrue(protocol.startsWith(protocolName));
247        }
248    }
249
250    public void test_SSLContext_getProvider() throws Exception {
251        Provider provider = SSLContext.getDefault().getProvider();
252        assertNotNull(provider);
253        assertEquals(StandardNames.JSSE_PROVIDER_NAME, provider.getName());
254    }
255
256    public void test_SSLContext_init_Default() throws Exception {
257        // Assert that initializing a default SSLContext fails because it's supposed to be
258        // initialized already.
259        SSLContext sslContext = SSLContext.getInstance(StandardNames.SSL_CONTEXT_PROTOCOLS_DEFAULT);
260        try {
261            sslContext.init(null, null, null);
262            fail();
263        } catch (KeyManagementException expected) {}
264        try {
265            sslContext.init(new KeyManager[0], new TrustManager[0], null);
266            fail();
267        } catch (KeyManagementException expected) {}
268        try {
269            sslContext.init(
270                    new KeyManager[] {new KeyManager() {}},
271                    new TrustManager[] {new TrustManager() {}},
272                    null);
273            fail();
274        } catch (KeyManagementException expected) {}
275    }
276
277    public void test_SSLContext_init_withNullManagerArrays() throws Exception {
278        // Assert that SSLContext.init works fine even when provided with null arrays of
279        // KeyManagers and TrustManagers.
280        // The contract of SSLContext.init is that it will for default X.509 KeyManager and
281        // TrustManager from the highest priority KeyManagerFactory and TrustManagerFactory.
282        for (String protocol : StandardNames.SSL_CONTEXT_PROTOCOLS) {
283            if (protocol.equals(StandardNames.SSL_CONTEXT_PROTOCOLS_DEFAULT)) {
284                // Default SSLContext is provided in an already initialized state
285                continue;
286            }
287            SSLContext sslContext = SSLContext.getInstance(protocol);
288            sslContext.init(null, null, null);
289        }
290    }
291
292    public void test_SSLContext_init_withEmptyManagerArrays() throws Exception {
293        // Assert that SSLContext.init works fine even when provided with empty arrays of
294        // KeyManagers and TrustManagers.
295        // The contract of SSLContext.init is that it will not look for default X.509 KeyManager and
296        // TrustManager.
297        // This test thus installs a Provider of KeyManagerFactory and TrustManagerFactory whose
298        // factories throw exceptions which will make this test fail if the factories are used.
299        Provider provider = new ThrowExceptionKeyAndTrustManagerFactoryProvider();
300        invokeWithHighestPrioritySecurityProvider(provider, new Callable<Void>() {
301            @Override
302            public Void call() throws Exception {
303                assertEquals(
304                        ThrowExceptionKeyAndTrustManagerFactoryProvider.class,
305                        TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm())
306                                .getProvider().getClass());
307                assertEquals(
308                        ThrowExceptionKeyAndTrustManagerFactoryProvider.class,
309                        KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm())
310                                .getProvider().getClass());
311
312                KeyManager[] keyManagers = new KeyManager[0];
313                TrustManager[] trustManagers = new TrustManager[0];
314                for (String protocol : StandardNames.SSL_CONTEXT_PROTOCOLS) {
315                    if (protocol.equals(StandardNames.SSL_CONTEXT_PROTOCOLS_DEFAULT)) {
316                        // Default SSLContext is provided in an already initialized state
317                        continue;
318                    }
319                    SSLContext sslContext = SSLContext.getInstance(protocol);
320                    sslContext.init(keyManagers, trustManagers, null);
321                }
322
323                return null;
324            }
325        });
326    }
327
328    public void test_SSLContext_init_withoutX509() throws Exception {
329        // Assert that SSLContext.init works fine even when provided with KeyManagers and
330        // TrustManagers which don't include the X.509 ones.
331        // The contract of SSLContext.init is that it will not look for default X.509 KeyManager and
332        // TrustManager.
333        // This test thus installs a Provider of KeyManagerFactory and TrustManagerFactory whose
334        // factories throw exceptions which will make this test fail if the factories are used.
335        Provider provider = new ThrowExceptionKeyAndTrustManagerFactoryProvider();
336        invokeWithHighestPrioritySecurityProvider(provider, new Callable<Void>() {
337            @Override
338            public Void call() throws Exception {
339                assertEquals(
340                        ThrowExceptionKeyAndTrustManagerFactoryProvider.class,
341                        TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm())
342                                .getProvider().getClass());
343                assertEquals(
344                        ThrowExceptionKeyAndTrustManagerFactoryProvider.class,
345                        KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm())
346                                .getProvider().getClass());
347
348                KeyManager[] keyManagers = new KeyManager[] {new KeyManager() {}};
349                TrustManager[] trustManagers = new TrustManager[] {new TrustManager() {}};
350                for (String protocol : StandardNames.SSL_CONTEXT_PROTOCOLS) {
351                    if (protocol.equals(StandardNames.SSL_CONTEXT_PROTOCOLS_DEFAULT)) {
352                        // Default SSLContext is provided in an already initialized state
353                        continue;
354                    }
355                    SSLContext sslContext = SSLContext.getInstance(protocol);
356                    sslContext.init(keyManagers, trustManagers, null);
357                }
358
359                return null;
360            }
361        });
362    }
363
364    public static class ThrowExceptionKeyAndTrustManagerFactoryProvider extends Provider {
365        public ThrowExceptionKeyAndTrustManagerFactoryProvider() {
366            super("ThrowExceptionKeyAndTrustManagerProvider",
367                    1.0,
368                    "SSLContextTest fake KeyManagerFactory  and TrustManagerFactory provider");
369
370            put("TrustManagerFactory." + TrustManagerFactory.getDefaultAlgorithm(),
371                    ThrowExceptionTrustManagagerFactorySpi.class.getName());
372            put("TrustManagerFactory.PKIX", ThrowExceptionTrustManagagerFactorySpi.class.getName());
373
374            put("KeyManagerFactory." + KeyManagerFactory.getDefaultAlgorithm(),
375                    ThrowExceptionKeyManagagerFactorySpi.class.getName());
376            put("KeyManagerFactory.PKIX", ThrowExceptionKeyManagagerFactorySpi.class.getName());
377        }
378    }
379
380    public static class ThrowExceptionTrustManagagerFactorySpi extends TrustManagerFactorySpi {
381        @Override
382        protected void engineInit(KeyStore ks) throws KeyStoreException {
383            fail();
384        }
385
386        @Override
387        protected void engineInit(ManagerFactoryParameters spec)
388                throws InvalidAlgorithmParameterException {
389            fail();
390        }
391
392        @Override
393        protected TrustManager[] engineGetTrustManagers() {
394            throw new AssertionFailedError();
395        }
396    }
397
398    public static class ThrowExceptionKeyManagagerFactorySpi extends KeyManagerFactorySpi {
399        @Override
400        protected void engineInit(KeyStore ks, char[] password) throws KeyStoreException,
401                NoSuchAlgorithmException, UnrecoverableKeyException {
402            fail();
403        }
404
405        @Override
406        protected void engineInit(ManagerFactoryParameters spec)
407                throws InvalidAlgorithmParameterException {
408            fail();
409        }
410
411        @Override
412        protected KeyManager[] engineGetKeyManagers() {
413            throw new AssertionFailedError();
414        }
415    }
416
417    /**
418     * Installs the specified security provider as the highest provider, invokes the provided
419     * {@link Callable}, and removes the provider.
420     *
421     * @return result returned by the {@code callable}.
422     */
423    private static <T> T invokeWithHighestPrioritySecurityProvider(
424            Provider provider, Callable<T> callable) throws Exception {
425        int providerPosition = -1;
426        try {
427            providerPosition = Security.insertProviderAt(provider, 1);
428            assertEquals(1, providerPosition);
429            return callable.call();
430        } finally {
431            if (providerPosition != -1) {
432                Security.removeProvider(provider.getName());
433            }
434        }
435    }
436
437    public void test_SSLContext_getSocketFactory() throws Exception {
438        for (String protocol : StandardNames.SSL_CONTEXT_PROTOCOLS) {
439            if (protocol.equals(StandardNames.SSL_CONTEXT_PROTOCOLS_DEFAULT)) {
440                SSLContext.getInstance(protocol).getSocketFactory();
441            } else {
442                try {
443                    SSLContext.getInstance(protocol).getSocketFactory();
444                    fail();
445                } catch (IllegalStateException expected) {
446                }
447            }
448
449            SSLContext sslContext = SSLContext.getInstance(protocol);
450            if (!protocol.equals(StandardNames.SSL_CONTEXT_PROTOCOLS_DEFAULT)) {
451                sslContext.init(null, null, null);
452            }
453            SocketFactory sf = sslContext.getSocketFactory();
454            assertNotNull(sf);
455            assertTrue(SSLSocketFactory.class.isAssignableFrom(sf.getClass()));
456        }
457    }
458
459    public void test_SSLContext_getServerSocketFactory() throws Exception {
460        for (String protocol : StandardNames.SSL_CONTEXT_PROTOCOLS) {
461            if (protocol.equals(StandardNames.SSL_CONTEXT_PROTOCOLS_DEFAULT)) {
462                SSLContext.getInstance(protocol).getServerSocketFactory();
463            } else {
464                try {
465                    SSLContext.getInstance(protocol).getServerSocketFactory();
466                    fail();
467                } catch (IllegalStateException expected) {
468                }
469            }
470
471            SSLContext sslContext = SSLContext.getInstance(protocol);
472            if (!protocol.equals(StandardNames.SSL_CONTEXT_PROTOCOLS_DEFAULT)) {
473                sslContext.init(null, null, null);
474            }
475            ServerSocketFactory ssf = sslContext.getServerSocketFactory();
476            assertNotNull(ssf);
477            assertTrue(SSLServerSocketFactory.class.isAssignableFrom(ssf.getClass()));
478        }
479    }
480
481    public void test_SSLContext_createSSLEngine() throws Exception {
482        for (String protocol : StandardNames.SSL_CONTEXT_PROTOCOLS) {
483
484            if (protocol.equals(StandardNames.SSL_CONTEXT_PROTOCOLS_DEFAULT)) {
485                SSLContext.getInstance(protocol).createSSLEngine();
486            } else {
487                try {
488                    SSLContext.getInstance(protocol).createSSLEngine();
489                    fail();
490                } catch (IllegalStateException expected) {
491                }
492            }
493
494            if (protocol.equals(StandardNames.SSL_CONTEXT_PROTOCOLS_DEFAULT)) {
495                SSLContext.getInstance(protocol).createSSLEngine(null, -1);
496            } else {
497                try {
498                    SSLContext.getInstance(protocol).createSSLEngine(null, -1);
499                    fail();
500                } catch (IllegalStateException expected) {
501                }
502            }
503
504            {
505                SSLContext sslContext = SSLContext.getInstance(protocol);
506                if (!protocol.equals(StandardNames.SSL_CONTEXT_PROTOCOLS_DEFAULT)) {
507                    sslContext.init(null, null, null);
508                }
509                SSLEngine se = sslContext.createSSLEngine();
510                assertNotNull(se);
511            }
512
513            {
514                SSLContext sslContext = SSLContext.getInstance(protocol);
515                if (!protocol.equals(StandardNames.SSL_CONTEXT_PROTOCOLS_DEFAULT)) {
516                    sslContext.init(null, null, null);
517                }
518                SSLEngine se = sslContext.createSSLEngine(null, -1);
519                assertNotNull(se);
520            }
521        }
522    }
523
524    public void test_SSLContext_getServerSessionContext() throws Exception {
525        for (String protocol : StandardNames.SSL_CONTEXT_PROTOCOLS) {
526            SSLContext sslContext = SSLContext.getInstance(protocol);
527            SSLSessionContext sessionContext = sslContext.getServerSessionContext();
528            assertNotNull(sessionContext);
529
530            if (!StandardNames.IS_RI &&
531                    protocol.equals(StandardNames.SSL_CONTEXT_PROTOCOLS_DEFAULT)) {
532                assertSame(SSLContext.getInstance(protocol).getServerSessionContext(),
533                           sessionContext);
534            } else {
535                assertNotSame(SSLContext.getInstance(protocol).getServerSessionContext(),
536                              sessionContext);
537            }
538        }
539    }
540
541    public void test_SSLContext_getClientSessionContext() throws Exception {
542        for (String protocol : StandardNames.SSL_CONTEXT_PROTOCOLS) {
543            SSLContext sslContext = SSLContext.getInstance(protocol);
544            SSLSessionContext sessionContext = sslContext.getClientSessionContext();
545            assertNotNull(sessionContext);
546
547            if (!StandardNames.IS_RI &&
548                    protocol.equals(StandardNames.SSL_CONTEXT_PROTOCOLS_DEFAULT)) {
549                assertSame(SSLContext.getInstance(protocol).getClientSessionContext(),
550                           sessionContext);
551            } else {
552                assertNotSame(SSLContext.getInstance(protocol).getClientSessionContext(),
553                              sessionContext);
554            }
555        }
556    }
557
558    public void test_SSLContextTest_TestSSLContext_create() {
559        TestSSLContext testContext = TestSSLContext.create();
560        assertNotNull(testContext);
561        assertNotNull(testContext.clientKeyStore);
562        assertNull(testContext.clientStorePassword);
563        assertNotNull(testContext.serverKeyStore);
564        assertEquals(StandardNames.IS_RI, testContext.serverStorePassword != null);
565        assertNotNull(testContext.clientKeyManagers);
566        assertNotNull(testContext.serverKeyManagers);
567        if (testContext.clientKeyManagers.length == 0) {
568          fail("No client KeyManagers");
569        }
570        if (testContext.serverKeyManagers.length == 0) {
571          fail("No server KeyManagers");
572        }
573        assertNotNull(testContext.clientKeyManagers[0]);
574        assertNotNull(testContext.serverKeyManagers[0]);
575        assertNotNull(testContext.clientTrustManager);
576        assertNotNull(testContext.serverTrustManager);
577        assertNotNull(testContext.clientContext);
578        assertNotNull(testContext.serverContext);
579        assertNotNull(testContext.serverSocket);
580        assertNotNull(testContext.host);
581        assertTrue(testContext.port != 0);
582        testContext.close();
583    }
584
585    public void test_SSLContext_SSLv3Unsupported() throws Exception {
586        try {
587            SSLContext context = SSLContext.getInstance("SSLv3");
588            fail("SSLv3 should not be supported");
589        } catch (NoSuchAlgorithmException expected) {
590        }
591    }
592
593    private static void assertContentsInOrder(List<String> expected, String... actual) {
594        if (expected.size() != actual.length) {
595            fail("Unexpected length. Expected len <" + expected.size()
596                    + ">, actual len <" + actual.length + ">, expected <" + expected
597                    + ">, actual <" + Arrays.asList(actual) + ">");
598        }
599        if (!expected.equals(Arrays.asList(actual))) {
600            fail("Unexpected element(s). Expected <" + expected
601                    + ">, actual <" + Arrays.asList(actual) + ">" );
602        }
603    }
604
605    private static final void closeQuietly(Closeable socket) {
606        if (socket != null) {
607            try {
608                socket.close();
609            } catch (Exception ignored) {
610            }
611        }
612    }
613}
614