1 package org.conscrypt; 2 3 import java.io.IOException; 4 import java.security.Provider; 5 import javax.net.ssl.SSLContext; 6 import javax.net.ssl.SSLServerSocketFactory; 7 import javax.net.ssl.SSLSocketFactory; 8 9 /** 10 * Utility for creating test client and server instances. 11 */ 12 @SuppressWarnings("ImmutableEnumChecker") 13 public enum OpenJdkEndpointFactory implements EndpointFactory { 14 @SuppressWarnings("unused") 15 JDK(newJdkFactories()), 16 @SuppressWarnings("unused") 17 CONSCRYPT(newConscryptFactories(false)), 18 CONSCRYPT_ENGINE(newConscryptFactories(true)); 19 20 private final Factories factories; 21 22 OpenJdkEndpointFactory(Factories factories) { 23 this.factories = factories; 24 } 25 26 @Override 27 public ClientEndpoint newClient(ChannelType channelType, int port, String[] protocols, 28 String[] ciphers) throws IOException { 29 return new ClientEndpoint( 30 factories.clientFactory, channelType, port, protocols, ciphers); 31 } 32 33 @Override 34 public ServerEndpoint newServer(ChannelType channelType, int messageSize, 35 String[] protocols, String[] ciphers) throws IOException { 36 return new ServerEndpoint(factories.serverFactory, factories.serverSocketFactory, 37 channelType, messageSize, protocols, ciphers); 38 } 39 40 private static final class Factories { 41 final SSLSocketFactory clientFactory; 42 final SSLSocketFactory serverFactory; 43 final SSLServerSocketFactory serverSocketFactory; 44 45 private Factories(SSLSocketFactory clientFactory, SSLSocketFactory serverFactory, 46 SSLServerSocketFactory serverSocketFactory) { 47 this.clientFactory = clientFactory; 48 this.serverFactory = serverFactory; 49 this.serverSocketFactory = serverSocketFactory; 50 } 51 } 52 53 private static Factories newJdkFactories() { 54 Provider provider = TestUtils.getJdkProvider(); 55 SSLContext clientContext = TestUtils.newClientSslContext(provider); 56 SSLContext serverContext = TestUtils.newServerSslContext(provider); 57 final SSLSocketFactory clientFactory = clientContext.getSocketFactory(); 58 final SSLSocketFactory serverFactory = serverContext.getSocketFactory(); 59 final SSLServerSocketFactory serverSocketFactory = serverContext.getServerSocketFactory(); 60 return new Factories(clientFactory, serverFactory, serverSocketFactory); 61 } 62 63 private static Factories newConscryptFactories(boolean useEngineSocket) { 64 Provider provider = TestUtils.getConscryptProvider(); 65 SSLContext clientContext = TestUtils.newClientSslContext(provider); 66 SSLContext serverContext = TestUtils.newServerSslContext(provider); 67 final SSLSocketFactory clientFactory = clientContext.getSocketFactory(); 68 final SSLSocketFactory serverFactory = serverContext.getSocketFactory(); 69 final SSLServerSocketFactory serverSocketFactory = serverContext.getServerSocketFactory(); 70 TestUtils.setUseEngineSocket(clientFactory, useEngineSocket); 71 TestUtils.setUseEngineSocket(serverFactory, useEngineSocket); 72 TestUtils.setUseEngineSocket(serverSocketFactory, useEngineSocket); 73 return new Factories(clientFactory, serverFactory, serverSocketFactory); 74 } 75 } 76