SSLUtil { public static SSLContext getSSLContextInstance(SSLConfig sslConfig) throws NoSuchAlgorithmException { String[] protocols = sslConfig.getProtocolsAsStringArray(); return findSSLContextForProtocols(protocols, DEFAULT_ALGORITMS); }  static SSLContext getSSLContextInstance(SSLConfig sslConfig); static String[] readArray(String text); static SSLContext createAndConfigureSSLContext(SSLConfig sslConfig,
      boolean skipSslVerification);  }
@Test(expected = NoSuchAlgorithmException.class) public void failWhenNothingIsRequested() throws Exception { SSLConfig sslConfig = mock(SSLConfig.class); when(sslConfig.getProtocolsAsStringArray()) .thenReturn(new String[0]); SSLUtil.getSSLContextInstance(sslConfig); }
@Test(expected = NoSuchAlgorithmException.class) public void failWithAnUnknownProtocol() throws Exception { SSLConfig sslConfig = mock(SSLConfig.class); when(sslConfig.getProtocolsAsStringArray()) .thenReturn(new String[] {"boulevard of broken dreams"}); SSLUtil.getSSLContextInstance(sslConfig); }
@Test public void getASpecificProtocol() throws Exception { SSLConfig sslConfig = mock(SSLConfig.class); when(sslConfig.getProtocolsAsStringArray()).thenReturn(new String[] {"TLSv1.2"}); final SSLContext sslContextInstance = SSLUtil.getSSLContextInstance(sslConfig); assertThat(sslContextInstance.getProtocol().equalsIgnoreCase("TLSv1.2")).isTrue(); }
@Test public void getAnyProtocolWithAnUnknownInTheList() throws Exception { SSLConfig sslConfig = mock(SSLConfig.class); when(sslConfig.getProtocolsAsStringArray()) .thenReturn(new String[] {"the dream of the blue turtles", "any", "SSL"}); final SSLContext sslContextInstance = SSLUtil.getSSLContextInstance(sslConfig); assertThat(sslContextInstance.getProtocol().equalsIgnoreCase("SSL")).isFalse(); String selectedProtocol = sslContextInstance.getProtocol(); String matchedProtocol = null; for (String algorithm : SSLUtil.DEFAULT_ALGORITMS) { if (algorithm.equalsIgnoreCase(selectedProtocol)) { matchedProtocol = algorithm; } } assertThat(matchedProtocol).isNotNull().withFailMessage("selected protocol (" + selectedProtocol + ") is not in the list of default algorithms, " + "indicating that the \"any\" setting did not work correctly"); }