⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 keychainsettest.java

📁 eclipseme的最新版本的source,欢迎j2me程序员使用
💻 JAVA
字号:
/*  
 ********************************************************************
 *
 *	File    	:   KeyChainSetTest.java
 *  Package     :   eclipseme.core.internal.signing
 *	System      :   eclipseme-unittest
 *	Author      :   Kevin Hunter
 *	Description :   This class runs unit tests on the 
 *					eclipseme.core.internal.signing.KeyChainSet class.
 *	                
 * Copyright (c) 2004 Kevin Hunter
 * All Rights Reserved.
 * Licensed under the Eclipse Public License - v 1.0
 * For more information see http://www.eclipse.org/legal/epl-v10.html
 *
 *
 *  CVS
 *	$$Source: /cvsroot/eclipseme/eclipseme.core/test-src/eclipseme/core/internal/signing/KeyChainSetTest.java,v $$
 *	$$Author: kdhunter $$
 *	$$Date: 2004/12/07 02:43:24 $$
 *	$$Revision: 1.2 $$
 *
 ********************************************************************
 */

package eclipseme.core.internal.signing;

import java.io.InputStream;

import junit.framework.TestCase;

import org.eclipse.core.runtime.CoreException;

import eclipseme.core.EclipseMECoreErrors;

/**
 * KeyChainSetTest
 * 
 */

public class KeyChainSetTest extends TestCase
{
	public static final String KeyStorePath = "eclipseme/core/internal/signing/UnitTestKeyStore.ks";
	public static final String KeyStorePass = "UnitTestKeyStorePass";
	
	public static final String RSAKeyAlias = "TestKeyRSA";
	public static final String RSAKeyPass = "TestKeyRSAPass";
	
	public static final String DSAKeyAlias = "TestKeyDSA";
	public static final String DSAKeyPass = "TestKeyDSAPass";
	
	public static final String BadAlias = "BadAlias";
	public static final String BadPass = "BadPass";
	
	private InputStream	m_is;

	/**
	 * Standard test case constructor
	 */
	public KeyChainSetTest()
	{
		super();
	}

	/**
	 * Standard test case constructor
	 */
	public KeyChainSetTest(String arg0)
	{
		super(arg0);
	}

	/**
	 * Set up the key store input stream for each test case
	 */
	protected void setUp() throws Exception
	{
		m_is = getClass().getClassLoader().getResourceAsStream(KeyStorePath);
		assertNotNull(m_is);
	}
	
	/**
	 * Tear down the key store input stream for each test case
	 */
	protected void tearDown() throws Exception
	{
		if (m_is != null)
		{
			m_is.close();
		}
	}
	
	/**
	 * Test that we can load an RSA key using the default keystore type and provider.
	 * @throws Exception
	 */
	public void testGoodLoad1() throws Exception
	{
		KeyChainSet set = KeyChainSet.getInstance(m_is, KeyStorePass, RSAKeyAlias, RSAKeyPass);
		
		assertNotNull(set.getKey());
		assertNotNull(set.getCertificateChain());
		assertNull(set.getProvider());
	}
	
	/**
	 * Test that we can load a DSA key using the default keystore type and provider.
	 * (Note that the JadSignature doesn't support DSA keys, but this makes sure that
	 * the KeyChainSet behaves itself).
	 * @throws Exception
	 */
	public void testGoodLoad2() throws Exception
	{
		KeyChainSet set = KeyChainSet.getInstance(m_is, KeyStorePass, DSAKeyAlias, DSAKeyPass);
		
		assertNotNull(set.getKey());
		assertNotNull(set.getCertificateChain());
		assertNull(set.getProvider());
	}
	
	/**
	 * Test that we can load an RSA key using explicit keystore type and provider.
	 * @throws Exception
	 */
	public void testGoodLoad3() throws Exception
	{
		KeyChainSet set = KeyChainSet.getInstance(m_is, "jks", "SUN", KeyStorePass, RSAKeyAlias, RSAKeyPass);
		
		assertNotNull(set.getKey());
		assertNotNull(set.getCertificateChain());
		assertEquals("SUN", set.getProvider());
	}
	
	public void testBadStoreType() throws Exception
	{
		try
		{
			KeyChainSet set = KeyChainSet.getInstance(m_is, "FOO", null, KeyStorePass, RSAKeyAlias, RSAKeyPass);
			
			fail("Failed to throw in testBadStoreType()");
		}
		catch(CoreException ce)
		{
			assertEquals(EclipseMECoreErrors.SIGNING_KEYSTORE_TYPE_NOT_AVAILABLE, ce.getStatus().getCode());
		}
	}
	
	public void testBadProvider() throws Exception
	{
		try
		{
			KeyChainSet set = KeyChainSet.getInstance(m_is, null, "BAR", KeyStorePass, RSAKeyAlias, RSAKeyPass);
			
			fail("Failed to throw in testBadProvider()");
		}
		catch(CoreException ce)
		{
			assertEquals(EclipseMECoreErrors.SIGNING_PROVIDER_NOT_CONFIGURED, ce.getStatus().getCode());
		}
	}
	
	public void testBadStoreFormat() throws Exception
	{
		try
		{
			// read a byte in off the stream so that the file looks bad
			m_is.read();
			
			KeyChainSet set = KeyChainSet.getInstance(m_is, KeyStorePass, RSAKeyAlias, RSAKeyPass);
			
			fail("Failed to throw in testBadStoreFormat()");
		}
		catch(CoreException ce)
		{
			assertEquals(EclipseMECoreErrors.SIGNING_BAD_KEYSTORE_OR_PASSWORD, ce.getStatus().getCode());
		}
	}
	
	public void testBadStorePassword() throws Exception
	{
		try
		{
			KeyChainSet set = KeyChainSet.getInstance(m_is, BadPass, RSAKeyAlias, RSAKeyPass);
			
			fail("Failed to throw in testBadStorePassword()");
		}
		catch(CoreException ce)
		{
			assertEquals(EclipseMECoreErrors.SIGNING_BAD_KEYSTORE_OR_PASSWORD, ce.getStatus().getCode());
		}
	}
	
	public void testBadKeyPassword() throws Exception
	{
		try
		{
			KeyChainSet set = KeyChainSet.getInstance(m_is, KeyStorePass, RSAKeyAlias, BadPass);
			
			fail("Failed to throw in testBadKeyPassword()");
		}
		catch(CoreException ce)
		{
			assertEquals(EclipseMECoreErrors.SIGNING_INVALID_KEY_PASSWORD, ce.getStatus().getCode());
		}
	}
	
	public void testKeyNotPresent() throws Exception
	{
		try
		{
			KeyChainSet set = KeyChainSet.getInstance(m_is, KeyStorePass, BadAlias, BadPass);
			
			fail("Failed to throw in testKeyNotPresent()");
		}
		catch(CoreException ce)
		{
			assertEquals(EclipseMECoreErrors.SIGNING_KEY_NOT_FOUND, ce.getStatus().getCode());
		}
	}
}


/*
 ********************************************************************
 *	CVS History:
 *	$$Log: KeyChainSetTest.java,v $
 *	$Revision 1.2  2004/12/07 02:43:24  kdhunter
 *	$Switched from custom exception classes to CoreException
 *	$in signing routines.
 *	$Set up basic error code and error message handling, including
 *	$prep for internationalization
 *	$
 *	$Revision 1.1  2004/11/26 14:58:39  kdhunter
 *	$Moved here from original separate unit test project
 *	$$
 *
 ********************************************************************
 */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -