View Javadoc

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