View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.net.ssl;
15  
16  import static org.junit.Assert.assertNotNull;
17  import static org.junit.Assert.assertTrue;
18  import static org.junit.Assert.fail;
19  
20  import java.security.NoSuchAlgorithmException;
21  import java.security.NoSuchProviderException;
22  import java.security.SecureRandom;
23  
24  import org.junit.Test;
25  
26  import ch.qos.logback.core.net.ssl.SSL;
27  import ch.qos.logback.core.net.ssl.SecureRandomFactoryBean;
28  
29  /**
30   * Unit tests for {@link SecureRandomFactoryBean}.
31   *
32   * @author Carl Harris
33   */
34  public class SecureRandomFactoryBeanTest {
35  
36      private SecureRandomFactoryBean factoryBean = new SecureRandomFactoryBean();
37  
38      @Test
39      public void testDefaults() throws Exception {
40          assertNotNull(factoryBean.createSecureRandom());
41      }
42  
43      @Test
44      public void testExplicitProvider() throws Exception {
45          SecureRandom secureRandom = SecureRandom.getInstance(SSL.DEFAULT_SECURE_RANDOM_ALGORITHM);
46          factoryBean.setProvider(secureRandom.getProvider().getName());
47          assertNotNull(factoryBean.createSecureRandom());
48      }
49  
50      @Test
51      public void testUnknownProvider() throws Exception {
52          factoryBean.setProvider(SSLTestConstants.FAKE_PROVIDER_NAME);
53          try {
54              factoryBean.createSecureRandom();
55              fail("expected NoSuchProviderException");
56          } catch (NoSuchProviderException ex) {
57              assertTrue(ex.getMessage().contains(SSLTestConstants.FAKE_PROVIDER_NAME));
58          }
59      }
60  
61      @Test
62      public void testUnknownAlgorithm() throws Exception {
63          factoryBean.setAlgorithm(SSLTestConstants.FAKE_ALGORITHM_NAME);
64          try {
65              factoryBean.createSecureRandom();
66              fail("expected NoSuchAlgorithmException");
67          } catch (NoSuchAlgorithmException ex) {
68              assertTrue(ex.getMessage().contains(SSLTestConstants.FAKE_ALGORITHM_NAME));
69          }
70      }
71  
72  }