55
66import java .nio .charset .StandardCharsets ;
77import java .security .KeyStore ;
8+ import java .security .KeyStoreException ;
89import java .security .PrivateKey ;
910import java .security .cert .CertStore ;
1011import java .security .cert .Certificate ;
1516
1617import org .bouncycastle .cms .jcajce .JcaSignerId ;
1718import org .hamcrest .Matchers ;
19+ import org .junit .AfterClass ;
20+ import org .junit .BeforeClass ;
1821import org .junit .Test ;
1922
2023public class CmsReaderTest {
2124
25+ private static final String PASSWORD = "pass" ;
26+ private static final String ALICE_ALIAS = "alice" ;
27+ private static final String BOB_ALIAS = "bob" ;
28+ private static KeyStore aliceKs ;
29+ private static KeyStore bobKs ;
30+ private static X509Certificate aliceCert ;
31+ private static X509Certificate bobCert ;
32+
33+ @ BeforeClass
34+ public static void createCertificates () throws KeyStoreException {
35+ aliceKs = X509CertBuilder .createTemporaryCert ("cn1" , ALICE_ALIAS , PASSWORD );
36+ bobKs = X509CertBuilder .createTemporaryCert ("cn2" , BOB_ALIAS , PASSWORD );
37+ aliceCert = (X509Certificate ) aliceKs .getCertificate (ALICE_ALIAS );
38+ bobCert = (X509Certificate ) bobKs .getCertificate (BOB_ALIAS );
39+ }
40+
41+ @ AfterClass
42+ public static void cleanup () {
43+ aliceKs = null ;
44+ bobKs = null ;
45+ aliceCert = null ;
46+ bobCert = null ;
47+ }
48+
2249 @ Test
2350 public void testCreateCertStore () throws Exception {
2451
25- X509Certificate c1 = (X509Certificate ) X509CertBuilder .createTemporaryCert ("cn1" , "a1" , "pass" ).getCertificate ("a1" );
26- X509Certificate c2 = (X509Certificate ) X509CertBuilder .createTemporaryCert ("cn2" , "a2" , "pass" ).getCertificate ("a2" );
27-
28- CertStore certStore = CmsReader .createCertStore (Arrays .asList (c1 , c2 ));
52+ CertStore certStore = CmsReader .createCertStore (Arrays .asList (aliceCert , bobCert ));
2953
3054 Collection <? extends Certificate > certs = certStore .getCertificates (new X509CertSelector () {
3155
@@ -34,40 +58,38 @@ public boolean match(Certificate cert) {
3458 return true ;
3559 }
3660 });
37- assertThat (certs , Matchers .containsInAnyOrder (c1 , c2 ));
61+ assertThat (certs , Matchers .containsInAnyOrder (aliceCert , bobCert ));
3862
3963 }
4064
4165 @ Test
4266 public void testGetCertificates () throws Exception {
43- X509Certificate c1 = (X509Certificate ) X509CertBuilder .createTemporaryCert ("cn1" , "a1" , "pass" ).getCertificate ("a1" );
44- X509Certificate c2 = (X509Certificate ) X509CertBuilder .createTemporaryCert ("cn2" , "a2" , "pass" ).getCertificate ("a2" );
4567
46- CertStore certStore = CmsReader .createCertStore (Arrays .asList (c1 , c2 ));
68+ CertStore certStore = CmsReader .createCertStore (Arrays .asList (aliceCert , bobCert ));
4769
48- assertThat (CmsReader .getCertificates (certStore , new JcaSignerId (c1 )), Matchers .contains (c1 ));
49- assertThat (CmsReader .getCertificates (certStore , new JcaSignerId (c2 )), Matchers .contains (c2 ));
70+ assertThat (CmsReader .getCertificates (certStore , new JcaSignerId (aliceCert )), Matchers .contains (aliceCert ));
71+ assertThat (CmsReader .getCertificates (certStore , new JcaSignerId (bobCert )), Matchers .contains (bobCert ));
5072 }
5173
5274 @ Test
5375 public void testDecryptAndVerify () throws Exception {
54- KeyStore alice = X509CertBuilder . createTemporaryCert ( "cn1" , "alice" , "pass" );
55- KeyStore bob = X509CertBuilder . createTemporaryCert ( "cn2" , "bob" , "pass" ) ;
76+
77+ final String helloWorld = "Hello World!" ;
5678
5779 byte [] signedAndEncrypted = CmsCreator .signAndEncrypt (
58- "Hello World!" .getBytes (StandardCharsets .UTF_8 ),
59- (X509Certificate ) alice .getCertificate ("alice" ),
60- (PrivateKey ) alice .getKey ("alice" , "pass" .toCharArray ()),
61- (X509Certificate ) bob .getCertificate ("bob" ),
80+ helloWorld .getBytes (StandardCharsets .UTF_8 ),
81+ (X509Certificate ) aliceKs .getCertificate (ALICE_ALIAS ),
82+ (PrivateKey ) aliceKs .getKey (ALICE_ALIAS , PASSWORD .toCharArray ()),
83+ (X509Certificate ) bobKs .getCertificate (BOB_ALIAS ),
6284 true );
6385
6486 byte [] plain = CmsReader .decryptAndVerify (
6587 signedAndEncrypted ,
66- Arrays .asList ((X509Certificate ) alice .getCertificate ("alice" )),
67- (X509Certificate ) bob .getCertificate ("bob" ),
68- (PrivateKey ) bob .getKey ("bob" , "pass" .toCharArray ()));
88+ Arrays .asList ((X509Certificate ) aliceKs .getCertificate (ALICE_ALIAS )),
89+ (X509Certificate ) bobKs .getCertificate (BOB_ALIAS ),
90+ (PrivateKey ) bobKs .getKey (BOB_ALIAS , PASSWORD .toCharArray ()));
6991
70- assertEquals (new String (plain , StandardCharsets .UTF_8 ), "Hello World!" );
92+ assertEquals (new String (plain , StandardCharsets .UTF_8 ), helloWorld );
7193
7294 }
7395
0 commit comments