📄 jadsignaturetest.java
字号:
/*
********************************************************************
*
* File : JadSignatureTest.java
* Package : eclipseme.core.internal.signing
* System : eclipseme-unittest
* Author : Kevin Hunter
* Description : This class runs unit tests on the
* eclipseme.core.internal.signing.JadSignature 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/JadSignatureTest.java,v $$
* $$Author: kdhunter $$
* $$Date: 2004/12/07 02:43:24 $$
* $$Revision: 1.2 $$
*
********************************************************************
*/
package eclipseme.core.internal.signing;
import java.io.InputStream;
import java.util.Properties;
import junit.framework.TestCase;
import org.eclipse.core.runtime.CoreException;
import eclipseme.core.EclipseMECoreErrors;
/**
* JadSignatureTest
*
*/
public class JadSignatureTest 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 InputJarFile = "eclipseme/core/internal/signing/MyMidlet.jar";
public static final String CorrectJadFile = "eclipseme/core/internal/signing/UnitTest.jad";
protected InputStream m_jarStream;
/**
* Standard test case constructor
*/
public JadSignatureTest()
{
super();
}
/**
* Standard test case constructor
*/
public JadSignatureTest(String arg0)
{
super(arg0);
}
protected void setUp() throws Exception
{
m_jarStream = getClass().getClassLoader().getResourceAsStream(InputJarFile);
assertNotNull(m_jarStream);
}
protected void tearDown() throws Exception
{
if (m_jarStream != null)
{
m_jarStream.close();
}
}
protected KeyChainSet loadKeyChainSet(String strKeyAlias, String strKeyPass) throws Exception
{
InputStream is = getClass().getClassLoader().getResourceAsStream(KeyStorePath);
assertNotNull(is);
KeyChainSet kcs = KeyChainSet.getInstance(is, KeyStorePass, strKeyAlias, strKeyPass);
is.close();
assertNotNull(kcs.getKey());
return(kcs);
}
public void testGoodSign() throws Exception
{
/*
* Perform the signing operation
*/
JadSignature signature = new JadSignature(loadKeyChainSet(RSAKeyAlias, RSAKeyPass));
signature.computeSignature(m_jarStream);
/*
* Load the JAD file that has the correct values (put there by JadTool)
* to check the result.
*/
InputStream is = getClass().getClassLoader().getResourceAsStream(CorrectJadFile);
assertNotNull(is);
Properties props = new Properties();
props.load(is);
is.close();
String correctSignature = props.getProperty("MIDlet-Jar-RSA-SHA1");
String mySignature = signature.getJarSignatureString();
assertEquals(correctSignature, mySignature);
String correctCertificate = props.getProperty("MIDlet-Certificate-1-1");
String[] myCertificates = signature.getCertificateStrings();
assertEquals(1, myCertificates.length);
assertEquals(correctCertificate, myCertificates[0]);
}
public void testMissingKeyChainSet() throws Exception
{
JadSignature signature = new JadSignature();
try
{
signature.computeSignature(m_jarStream);
fail("failed to throw in testMissingKeyChainSet()");
}
catch(CoreException ce)
{
assertEquals(EclipseMECoreErrors.SIGNING_INTERNAL_MISSING_KEYCHAINSET, ce.getStatus().getCode());
}
}
public void testMissingCertificates() throws Exception
{
KeyChainSet set = loadKeyChainSet(RSAKeyAlias, RSAKeyPass);
set.setCertificateChain(null);
JadSignature signature = new JadSignature(set);
try
{
signature.computeSignature(m_jarStream);
fail("failed to throw in testMissingCertificates()");
}
catch(CoreException ce)
{
assertEquals(EclipseMECoreErrors.SIGNING_MISSING_CERTIFICATES, ce.getStatus().getCode());
}
}
public void testInvalidKeyType() throws Exception
{
JadSignature signature = new JadSignature(loadKeyChainSet(DSAKeyAlias, DSAKeyPass));
try
{
signature.computeSignature(m_jarStream);
fail("failed to throw in testInvalidKeyType()");
}
catch(CoreException ce)
{
assertEquals(EclipseMECoreErrors.SIGNING_BAD_KEY_TYPE, ce.getStatus().getCode());
}
}
public void testProviderNotConfigured() throws Exception
{
KeyChainSet set = loadKeyChainSet(RSAKeyAlias, RSAKeyPass);
set.setProvider("foo");
JadSignature signature = new JadSignature(set);
try
{
signature.computeSignature(m_jarStream);
fail("failed to throw in testProviderNotConfigured()");
}
catch(CoreException ce)
{
assertEquals(EclipseMECoreErrors.SIGNING_PROVIDER_NOT_CONFIGURED, ce.getStatus().getCode());
}
}
}
/*
********************************************************************
* CVS History:
* $$Log: JadSignatureTest.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 + -