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 java.security.NoSuchAlgorithmException;
17  import java.security.NoSuchProviderException;
18  import java.security.SecureRandom;
19  
20  import org.junit.jupiter.api.Assertions;
21  import org.junit.jupiter.api.Test;
22  
23  /**
24   * Unit tests for {@link SecureRandomFactoryBean}.
25   *
26   * @author Carl Harris
27   */
28  public class SecureRandomFactoryBeanTest {
29  
30      private SecureRandomFactoryBean factoryBean = new SecureRandomFactoryBean();
31  
32      @Test
33      public void testDefaults() throws Exception {
34          Assertions.assertNotNull(factoryBean.createSecureRandom());
35      }
36  
37      @Test
38      public void testExplicitProvider() throws Exception {
39          SecureRandom secureRandom = SecureRandom.getInstance(SSL.DEFAULT_SECURE_RANDOM_ALGORITHM);
40          factoryBean.setProvider(secureRandom.getProvider().getName());
41          Assertions.assertNotNull(factoryBean.createSecureRandom());
42      }
43  
44      @Test
45      public void testUnknownProvider() throws Exception {
46          factoryBean.setProvider(SSLTestConstants.FAKE_PROVIDER_NAME);
47          try {
48              factoryBean.createSecureRandom();
49              Assertions.fail("expected NoSuchProviderException");
50          } catch (NoSuchProviderException ex) {
51              Assertions.assertTrue(ex.getMessage().contains(SSLTestConstants.FAKE_PROVIDER_NAME));
52          }
53      }
54  
55      @Test
56      public void testUnknownAlgorithm() throws Exception {
57          factoryBean.setAlgorithm(SSLTestConstants.FAKE_ALGORITHM_NAME);
58          try {
59              factoryBean.createSecureRandom();
60              Assertions.fail("expected NoSuchAlgorithmException");
61          } catch (NoSuchAlgorithmException ex) {
62              Assertions.assertTrue(ex.getMessage().contains(SSLTestConstants.FAKE_ALGORITHM_NAME));
63          }
64      }
65  
66  }