View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.api;
8   
9   import org.mule.api.lifecycle.CreateException;
10  import org.mule.api.security.tls.TlsConfiguration;
11  import org.mule.tck.junit4.AbstractMuleTestCase;
12  
13  import java.io.File;
14  import java.net.URL;
15  
16  import javax.net.ssl.SSLSocketFactory;
17  
18  import org.junit.Test;
19  
20  import static org.junit.Assert.assertNotNull;
21  import static org.junit.Assert.assertTrue;
22  import static org.junit.Assert.fail;
23  
24  public class TlsConfigurationTestCase extends AbstractMuleTestCase
25  {
26      @Test
27      public void testEmptyConfiguration() throws Exception
28      {
29          TlsConfiguration configuration = new TlsConfiguration(TlsConfiguration.DEFAULT_KEYSTORE);
30          try
31          {
32              configuration.initialise(false, TlsConfiguration.JSSE_NAMESPACE);
33              fail("no key password");
34          }
35          catch (IllegalArgumentException e)
36          {
37              assertNotNull("expected", e);
38          }
39          configuration.setKeyPassword("mulepassword");
40          try
41          {
42              configuration.initialise(false, TlsConfiguration.JSSE_NAMESPACE);
43              fail("no store password");
44          }
45          catch (IllegalArgumentException e)
46          {
47              assertNotNull("expected", e);
48          }
49          configuration.setKeyStorePassword("mulepassword");
50          configuration.setKeyStore(""); // guaranteed to not exist
51          try
52          {
53              configuration.initialise(false, TlsConfiguration.JSSE_NAMESPACE);
54              fail("no keystore");
55          }
56          catch (Exception e)
57          {
58              assertNotNull("expected", e);
59          }
60      }
61  
62      @Test
63      public void testSimpleSocket() throws Exception
64      {
65          TlsConfiguration configuration = new TlsConfiguration(TlsConfiguration.DEFAULT_KEYSTORE);
66          configuration.setKeyPassword("mulepassword");
67          configuration.setKeyStorePassword("mulepassword");
68          configuration.setKeyStore("clientKeystore");
69          configuration.initialise(false, TlsConfiguration.JSSE_NAMESPACE);
70          SSLSocketFactory socketFactory = configuration.getSocketFactory();
71          assertTrue("socket is useless", socketFactory.getSupportedCipherSuites().length > 0);
72      }
73  
74      @Test
75      public void testExceptionOnInvalidKeyAlias() throws Exception
76      {
77          URL keystoreUrl = getClass().getClassLoader().getResource("serverKeystore");
78          File keystoreFile = new File(keystoreUrl.toURI());
79  
80          TlsConfiguration config = new TlsConfiguration(keystoreFile.getAbsolutePath());
81          config.setKeyStorePassword("mulepassword");
82          config.setKeyPassword("mulepassword");
83          config.setKeyAlias("this_key_does_not_exist_in_the_keystore");
84  
85          try
86          {
87              config.initialise(false, TlsConfiguration.JSSE_NAMESPACE);
88          }
89          catch (CreateException ce)
90          {
91              assertTrue(ce.getCause() instanceof IllegalStateException);
92          }
93      }
94  }