1
+ // Copyright (c) Microsoft Corporation. All rights reserved.
2
+ // Licensed under the MIT License.
3
+
4
+ package com .microsoft .aad .msal4j ;
5
+
6
+ import org .easymock .EasyMock ;
7
+ import org .testng .annotations .BeforeMethod ;
8
+ import org .testng .annotations .Test ;
9
+
10
+ import java .security .KeyStore ;
11
+ import java .security .KeyStoreSpi ;
12
+ import java .util .Arrays ;
13
+ import java .util .Collections ;
14
+
15
+ import static org .testng .AssertJUnit .assertEquals ;
16
+
17
+ @ Test
18
+ public class ClientCertificatePkcs12Test extends AbstractMsalTests {
19
+
20
+ private KeyStoreSpi keyStoreSpi ;
21
+ private KeyStore keystore ;
22
+
23
+ @ BeforeMethod
24
+ public void setUp () throws Exception {
25
+ keyStoreSpi = EasyMock .createMock (KeyStoreSpi .class );
26
+ keystore = new KeyStore (keyStoreSpi , null , "PKCS12" ) {};
27
+ keystore .load (null );
28
+ }
29
+
30
+ @ Test (expectedExceptions = IllegalArgumentException .class , expectedExceptionsMessageRegExp = "certificate not loaded from input stream" )
31
+ public void testNoEntries () throws Exception {
32
+ EasyMock .expect (keyStoreSpi .engineAliases ())
33
+ .andReturn (Collections .enumeration (Collections .emptyList ())).times (1 );
34
+ EasyMock .replay (keyStoreSpi );
35
+
36
+ ClientCertificate .getPrivateKeyAlias (keystore );
37
+ }
38
+
39
+ @ Test (expectedExceptions = IllegalArgumentException .class , expectedExceptionsMessageRegExp = "certificate not loaded from input stream" )
40
+ public void testNoPrivateKey () throws Exception {
41
+ EasyMock .expect (keyStoreSpi .engineAliases ())
42
+ .andReturn (Collections .enumeration (Arrays .asList ("CA_cert1" , "CA_cert2" ))).times (1 );
43
+ EasyMock .expect (keyStoreSpi .engineEntryInstanceOf ("CA_cert1" , KeyStore .PrivateKeyEntry .class )).andReturn (false ).times (1 );
44
+ EasyMock .expect (keyStoreSpi .engineEntryInstanceOf ("CA_cert2" , KeyStore .PrivateKeyEntry .class )).andReturn (false ).times (1 );
45
+ EasyMock .replay (keyStoreSpi );
46
+
47
+ ClientCertificate .getPrivateKeyAlias (keystore );
48
+ }
49
+
50
+ @ Test (expectedExceptions = IllegalArgumentException .class , expectedExceptionsMessageRegExp = "more than one certificate alias found in input stream" )
51
+ public void testMultiplePrivateKeyAliases () throws Exception {
52
+ EasyMock .expect (keyStoreSpi .engineAliases ())
53
+ .andReturn (Collections .enumeration (Arrays .asList ("private_key1" , "private_key2" , "CA_cert" ))).times (1 );
54
+ EasyMock .expect (keyStoreSpi .engineEntryInstanceOf ("private_key1" , KeyStore .PrivateKeyEntry .class )).andReturn (true ).times (1 );
55
+ EasyMock .expect (keyStoreSpi .engineEntryInstanceOf ("private_key2" , KeyStore .PrivateKeyEntry .class )).andReturn (true ).times (1 );
56
+ EasyMock .expect (keyStoreSpi .engineEntryInstanceOf ("CA_cert" , KeyStore .PrivateKeyEntry .class )).andReturn (false ).times (1 );
57
+ EasyMock .replay (keyStoreSpi );
58
+
59
+ ClientCertificate .getPrivateKeyAlias (keystore );
60
+ }
61
+
62
+ @ Test
63
+ public void testMultipleEntriesButOnlyOnePrivateKey () throws Exception {
64
+ EasyMock .expect (keyStoreSpi .engineAliases ())
65
+ .andReturn (Collections .enumeration (Arrays .asList ("CA_cert1" , "private_key" , "CA_cert2" ))).times (1 );
66
+ EasyMock .expect (keyStoreSpi .engineEntryInstanceOf ("CA_cert1" , KeyStore .PrivateKeyEntry .class )).andReturn (false ).times (1 );
67
+ EasyMock .expect (keyStoreSpi .engineEntryInstanceOf ("private_key" , KeyStore .PrivateKeyEntry .class )).andReturn (true ).times (1 );
68
+ EasyMock .expect (keyStoreSpi .engineEntryInstanceOf ("CA_cert2" , KeyStore .PrivateKeyEntry .class )).andReturn (false ).times (1 );
69
+ EasyMock .replay (keyStoreSpi );
70
+
71
+ String privateKeyAlias = ClientCertificate .getPrivateKeyAlias (keystore );
72
+ assertEquals ("private_key" , privateKeyAlias );
73
+ }
74
+
75
+ }
0 commit comments