1 /* 2 * Copyright (C) 2018 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 17 package android.net.ssl; 18 19 import static org.junit.Assert.assertFalse; 20 import static org.junit.Assert.assertNotNull; 21 import static org.junit.Assert.assertNull; 22 import static org.junit.Assert.assertTrue; 23 import static org.junit.Assert.fail; 24 25 import com.android.org.conscrypt.tlswire.TlsTester; 26 import com.android.org.conscrypt.tlswire.handshake.ClientHello; 27 import com.android.org.conscrypt.tlswire.handshake.HelloExtension; 28 import java.util.concurrent.ExecutorService; 29 import java.util.concurrent.Executors; 30 import java.util.concurrent.TimeUnit; 31 import javax.net.ssl.HandshakeCompletedListener; 32 import javax.net.ssl.SSLSession; 33 import javax.net.ssl.SSLSocket; 34 import javax.net.ssl.SSLSocketFactory; 35 import org.junit.After; 36 import org.junit.Before; 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 import org.junit.runners.JUnit4; 40 import tests.net.DelegatingSSLSocketFactory; 41 42 @RunWith(JUnit4.class) 43 public class SSLSocketsTest { 44 45 private static class BrokenSSLSocket extends SSLSocket { getSupportedCipherSuites()46 @Override public String[] getSupportedCipherSuites() { throw new AssertionError(); } getEnabledCipherSuites()47 @Override public String[] getEnabledCipherSuites() { throw new AssertionError(); } setEnabledCipherSuites(String[] strings)48 @Override public void setEnabledCipherSuites(String[] strings) { throw new AssertionError(); } getSupportedProtocols()49 @Override public String[] getSupportedProtocols() { throw new AssertionError(); } getEnabledProtocols()50 @Override public String[] getEnabledProtocols() { throw new AssertionError(); } setEnabledProtocols(String[] strings)51 @Override public void setEnabledProtocols(String[] strings) { throw new AssertionError(); } getSession()52 @Override public SSLSession getSession() { throw new AssertionError(); } addHandshakeCompletedListener( HandshakeCompletedListener handshakeCompletedListener)53 @Override public void addHandshakeCompletedListener( 54 HandshakeCompletedListener handshakeCompletedListener) { throw new AssertionError(); } removeHandshakeCompletedListener( HandshakeCompletedListener handshakeCompletedListener)55 @Override public void removeHandshakeCompletedListener( 56 HandshakeCompletedListener handshakeCompletedListener) { throw new AssertionError(); } startHandshake()57 @Override public void startHandshake() { throw new AssertionError(); } setUseClientMode(boolean b)58 @Override public void setUseClientMode(boolean b) { throw new AssertionError(); } getUseClientMode()59 @Override public boolean getUseClientMode() { throw new AssertionError(); } setNeedClientAuth(boolean b)60 @Override public void setNeedClientAuth(boolean b) { throw new AssertionError(); } getNeedClientAuth()61 @Override public boolean getNeedClientAuth() { throw new AssertionError(); } setWantClientAuth(boolean b)62 @Override public void setWantClientAuth(boolean b) { throw new AssertionError(); } getWantClientAuth()63 @Override public boolean getWantClientAuth() { throw new AssertionError(); } setEnableSessionCreation(boolean b)64 @Override public void setEnableSessionCreation(boolean b) { throw new AssertionError(); } getEnableSessionCreation()65 @Override public boolean getEnableSessionCreation() { throw new AssertionError(); } 66 } 67 68 private ExecutorService executor; 69 70 @Before setUp()71 public void setUp() { 72 executor = Executors.newCachedThreadPool(); 73 } 74 75 @After tearDown()76 public void tearDown() throws InterruptedException { 77 executor.shutdown(); 78 executor.awaitTermination(1, TimeUnit.SECONDS); 79 } 80 81 @Test testIsSupported()82 public void testIsSupported() throws Exception { 83 SSLSocket s = (SSLSocket) SSLSocketFactory.getDefault().createSocket(); 84 assertTrue(SSLSockets.isSupportedSocket(s)); 85 86 s = new BrokenSSLSocket(); 87 assertFalse(SSLSockets.isSupportedSocket(s)); 88 } 89 90 @Test testUseSessionTickets()91 public void testUseSessionTickets() throws Exception { 92 try { 93 SSLSockets.setUseSessionTickets(new BrokenSSLSocket(), true); 94 fail(); 95 } catch (IllegalArgumentException expected) { 96 } 97 98 SSLSocket s = (SSLSocket) SSLSocketFactory.getDefault().createSocket(); 99 SSLSockets.setUseSessionTickets(s, true); 100 101 ClientHello hello = TlsTester.captureTlsHandshakeClientHello(executor, 102 new DelegatingSSLSocketFactory((SSLSocketFactory) SSLSocketFactory.getDefault()) { 103 @Override public SSLSocket configureSocket(SSLSocket socket) { 104 SSLSockets.setUseSessionTickets(socket, true); 105 return socket; 106 } 107 }); 108 assertNotNull(hello.findExtensionByType(HelloExtension.TYPE_SESSION_TICKET)); 109 110 hello = TlsTester.captureTlsHandshakeClientHello(executor, 111 new DelegatingSSLSocketFactory((SSLSocketFactory) SSLSocketFactory.getDefault()) { 112 @Override public SSLSocket configureSocket(SSLSocket socket) { 113 SSLSockets.setUseSessionTickets(socket, false); 114 return socket; 115 } 116 }); 117 assertNull(hello.findExtensionByType(HelloExtension.TYPE_SESSION_TICKET)); 118 } 119 } 120