001/** 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved. 004 * 005 * This program and the accompanying materials are dual-licensed under 006 * either the terms of the Eclipse Public License v1.0 as published by 007 * the Eclipse Foundation 008 * 009 * or (per the licensee's choosing) 010 * 011 * under the terms of the GNU Lesser General Public License version 2.1 012 * as published by the Free Software Foundation. 013 */ 014package ch.qos.logback.core.net.ssl; 015 016import static org.junit.Assert.assertNotNull; 017import static org.junit.Assert.assertTrue; 018import static org.junit.Assert.fail; 019 020import java.security.NoSuchAlgorithmException; 021import java.security.NoSuchProviderException; 022import java.security.SecureRandom; 023 024import org.junit.Test; 025 026import ch.qos.logback.core.net.ssl.SSL; 027import ch.qos.logback.core.net.ssl.SecureRandomFactoryBean; 028 029/** 030 * Unit tests for {@link SecureRandomFactoryBean}. 031 * 032 * @author Carl Harris 033 */ 034public class SecureRandomFactoryBeanTest { 035 036 private SecureRandomFactoryBean factoryBean = new SecureRandomFactoryBean(); 037 038 @Test 039 public void testDefaults() throws Exception { 040 assertNotNull(factoryBean.createSecureRandom()); 041 } 042 043 @Test 044 public void testExplicitProvider() throws Exception { 045 SecureRandom secureRandom = SecureRandom.getInstance(SSL.DEFAULT_SECURE_RANDOM_ALGORITHM); 046 factoryBean.setProvider(secureRandom.getProvider().getName()); 047 assertNotNull(factoryBean.createSecureRandom()); 048 } 049 050 @Test 051 public void testUnknownProvider() throws Exception { 052 factoryBean.setProvider(SSLTestConstants.FAKE_PROVIDER_NAME); 053 try { 054 factoryBean.createSecureRandom(); 055 fail("expected NoSuchProviderException"); 056 } catch (NoSuchProviderException ex) { 057 assertTrue(ex.getMessage().contains(SSLTestConstants.FAKE_PROVIDER_NAME)); 058 } 059 } 060 061 @Test 062 public void testUnknownAlgorithm() throws Exception { 063 factoryBean.setAlgorithm(SSLTestConstants.FAKE_ALGORITHM_NAME); 064 try { 065 factoryBean.createSecureRandom(); 066 fail("expected NoSuchAlgorithmException"); 067 } catch (NoSuchAlgorithmException ex) { 068 assertTrue(ex.getMessage().contains(SSLTestConstants.FAKE_ALGORITHM_NAME)); 069 } 070 } 071 072}