test improvements
* use logger instead of System.err for test output * refactor KeyEntryData into separate file
This commit is contained in:
parent
a1245cf4ae
commit
8800020856
3 changed files with 73 additions and 53 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1,5 +1,7 @@
|
|||
target/
|
||||
.idea/
|
||||
.settings/
|
||||
.checkstyle
|
||||
.project
|
||||
.classpath
|
||||
*.iml
|
|
@ -23,17 +23,7 @@
|
|||
package info.dittberner.bcsmime_demo;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.bouncycastle.asn1.x500.X500Name;
|
||||
import org.bouncycastle.asn1.x509.*;
|
||||
import org.bouncycastle.cert.CertIOException;
|
||||
import org.bouncycastle.cert.X509CertificateHolder;
|
||||
import org.bouncycastle.cert.X509v3CertificateBuilder;
|
||||
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
|
||||
import org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder;
|
||||
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
||||
import org.bouncycastle.operator.ContentSigner;
|
||||
import org.bouncycastle.operator.OperatorCreationException;
|
||||
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
|
||||
|
||||
import javax.mail.BodyPart;
|
||||
import javax.mail.Message.RecipientType;
|
||||
|
@ -44,15 +34,13 @@ import javax.mail.internet.InternetAddress;
|
|||
import javax.mail.internet.MimeBodyPart;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
import javax.mail.internet.MimeMultipart;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.math.BigInteger;
|
||||
import java.security.KeyPair;
|
||||
import java.security.KeyPairGenerator;
|
||||
import java.security.KeyStore;
|
||||
import java.security.Security;
|
||||
import java.security.cert.Certificate;
|
||||
import java.security.cert.CertificateException;
|
||||
import java.util.Date;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* Test Encryption and Decryption.
|
||||
|
@ -67,43 +55,7 @@ public class EncryptDecryptTest extends TestCase {
|
|||
new String[]{"test2", "testrecpt2@example.org", "Test Recipient 2"}
|
||||
};
|
||||
private KeyStore keystore;
|
||||
|
||||
private class KeyEntryData {
|
||||
private final X509CertificateHolder certificateHolder;
|
||||
KeyPair keyPair;
|
||||
|
||||
private KeyEntryData(KeyPairGenerator kpg, String address) throws CertIOException, OperatorCreationException {
|
||||
this.keyPair = kpg.generateKeyPair();
|
||||
|
||||
X500Name issuer = new X500Name(
|
||||
String.format("CN=Test Recipient,emailAddress=%s", address));
|
||||
//noinspection UnnecessaryLocalVariable
|
||||
X500Name subject = issuer;
|
||||
X509v3CertificateBuilder certificateBuilder = new JcaX509v3CertificateBuilder(
|
||||
issuer, BigInteger.valueOf(System.currentTimeMillis()),
|
||||
new Date(System.currentTimeMillis() - 50000), new Date(
|
||||
System.currentTimeMillis() + 50000), subject,
|
||||
keyPair.getPublic());
|
||||
certificateBuilder.addExtension(Extension.basicConstraints, true,
|
||||
new BasicConstraints(true));
|
||||
certificateBuilder.addExtension(Extension.keyUsage, true,
|
||||
new KeyUsage(KeyUsage.digitalSignature
|
||||
| KeyUsage.keyEncipherment));
|
||||
certificateBuilder.addExtension(Extension.extendedKeyUsage, true,
|
||||
new ExtendedKeyUsage(KeyPurposeId.id_kp_emailProtection));
|
||||
certificateBuilder.addExtension(Extension.subjectAlternativeName,
|
||||
false, new GeneralNames(new GeneralName(
|
||||
GeneralName.rfc822Name, address)));
|
||||
|
||||
ContentSigner signer = new JcaContentSignerBuilder("SHA256WithRSA")
|
||||
.build(keyPair.getPrivate());
|
||||
this.certificateHolder = certificateBuilder.build(signer);
|
||||
}
|
||||
|
||||
private Certificate getCertificate() throws CertificateException, CertIOException, OperatorCreationException {
|
||||
return (new JcaX509CertificateConverter()).getCertificate(certificateHolder);
|
||||
}
|
||||
}
|
||||
private Logger logger = Logger.getLogger(EncryptDecryptTest.class.getName());
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
|
@ -131,6 +83,12 @@ public class EncryptDecryptTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
private String messageAsString(MimeMessage message) throws IOException, MessagingException {
|
||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
message.writeTo(byteArrayOutputStream);
|
||||
return byteArrayOutputStream.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of {@link SMIMEEncrypt} and {@link SMIMEDecrypt}.
|
||||
*/
|
||||
|
@ -141,12 +99,14 @@ public class EncryptDecryptTest extends TestCase {
|
|||
SMIMEEncrypt encrypt = new SMIMEEncrypt(keystore);
|
||||
MimeMessage encrypted = encrypt.encryptMessage(message);
|
||||
assertNotNull(encrypted);
|
||||
encrypted.writeTo(System.err);
|
||||
|
||||
logger.info(messageAsString(encrypted));
|
||||
|
||||
SMIMEDecrypt decrypt = new SMIMEDecrypt(keystore);
|
||||
MimeMessage decrypted = decrypt.decryptMessage(encrypted);
|
||||
assertNotNull(decrypted);
|
||||
decrypted.writeTo(System.err);
|
||||
|
||||
logger.info(messageAsString(decrypted));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
58
src/test/java/info/dittberner/bcsmime_demo/KeyEntryData.java
Normal file
58
src/test/java/info/dittberner/bcsmime_demo/KeyEntryData.java
Normal file
|
@ -0,0 +1,58 @@
|
|||
package info.dittberner.bcsmime_demo;
|
||||
|
||||
import org.bouncycastle.asn1.x500.X500Name;
|
||||
import org.bouncycastle.asn1.x509.*;
|
||||
import org.bouncycastle.cert.CertIOException;
|
||||
import org.bouncycastle.cert.X509CertificateHolder;
|
||||
import org.bouncycastle.cert.X509v3CertificateBuilder;
|
||||
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
|
||||
import org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder;
|
||||
import org.bouncycastle.operator.ContentSigner;
|
||||
import org.bouncycastle.operator.OperatorCreationException;
|
||||
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.security.KeyPair;
|
||||
import java.security.KeyPairGenerator;
|
||||
import java.security.cert.CertificateException;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Created by jan on 12.10.14.
|
||||
*/
|
||||
public class KeyEntryData {
|
||||
private final X509CertificateHolder certificateHolder;
|
||||
KeyPair keyPair;
|
||||
|
||||
public KeyEntryData(KeyPairGenerator kpg, String address) throws CertIOException, OperatorCreationException {
|
||||
this.keyPair = kpg.generateKeyPair();
|
||||
|
||||
X500Name issuer = new X500Name(
|
||||
String.format("CN=Test Recipient,emailAddress=%s", address));
|
||||
//noinspection UnnecessaryLocalVariable
|
||||
X500Name subject = issuer;
|
||||
X509v3CertificateBuilder certificateBuilder = new JcaX509v3CertificateBuilder(
|
||||
issuer, BigInteger.valueOf(System.currentTimeMillis()),
|
||||
new Date(System.currentTimeMillis() - 50000), new Date(
|
||||
System.currentTimeMillis() + 50000), subject,
|
||||
keyPair.getPublic());
|
||||
certificateBuilder.addExtension(Extension.basicConstraints, true,
|
||||
new BasicConstraints(true));
|
||||
certificateBuilder.addExtension(Extension.keyUsage, true,
|
||||
new KeyUsage(KeyUsage.digitalSignature
|
||||
| KeyUsage.keyEncipherment));
|
||||
certificateBuilder.addExtension(Extension.extendedKeyUsage, true,
|
||||
new ExtendedKeyUsage(KeyPurposeId.id_kp_emailProtection));
|
||||
certificateBuilder.addExtension(Extension.subjectAlternativeName,
|
||||
false, new GeneralNames(new GeneralName(
|
||||
GeneralName.rfc822Name, address)));
|
||||
|
||||
ContentSigner signer = new JcaContentSignerBuilder("SHA256WithRSA")
|
||||
.build(keyPair.getPrivate());
|
||||
this.certificateHolder = certificateBuilder.build(signer);
|
||||
}
|
||||
|
||||
public java.security.cert.Certificate getCertificate() throws CertificateException, CertIOException, OperatorCreationException {
|
||||
return (new JcaX509CertificateConverter()).getCertificate(certificateHolder);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue