2424import com .cloud .certificate .dao .CrlDao ;
2525import com .cloud .host .Host ;
2626import com .cloud .host .dao .HostDao ;
27+ import com .cloud .utils .exception .CloudRuntimeException ;
2728import org .apache .cloudstack .api .ServerApiException ;
2829import org .apache .cloudstack .framework .ca .CAProvider ;
2930import org .apache .cloudstack .framework .ca .Certificate ;
3334import org .junit .Before ;
3435import org .junit .Test ;
3536import org .junit .runner .RunWith ;
37+ import org .mockito .InjectMocks ;
3638import org .mockito .Mock ;
3739import org .mockito .Mockito ;
40+ import org .mockito .Spy ;
3841import org .mockito .junit .MockitoJUnitRunner ;
3942
4043import java .lang .reflect .Field ;
@@ -63,7 +66,9 @@ public class CAManagerImplTest {
6366 @ Mock
6467 private CAProvider caProvider ;
6568
66- private CAManagerImpl caManager ;
69+ @ InjectMocks
70+ @ Spy
71+ private CAManagerImpl caManager = new CAManagerImpl ();
6772
6873 private void addField (final CAManagerImpl provider , final String name , final Object o ) throws IllegalAccessException , NoSuchFieldException {
6974 Field f = CAManagerImpl .class .getDeclaredField (name );
@@ -73,10 +78,6 @@ private void addField(final CAManagerImpl provider, final String name, final Obj
7378
7479 @ Before
7580 public void setUp () throws Exception {
76- caManager = new CAManagerImpl ();
77- addField (caManager , "crlDao" , crlDao );
78- addField (caManager , "hostDao" , hostDao );
79- addField (caManager , "agentManager" , agentManager );
8081 addField (caManager , "configuredCaProvider" , caProvider );
8182
8283 Mockito .when (caProvider .getProviderName ()).thenReturn ("root" );
@@ -91,19 +92,19 @@ public void tearDown() throws Exception {
9192 }
9293
9394 @ Test (expected = ServerApiException .class )
94- public void testIssueCertificateThrowsException () throws Exception {
95+ public void testIssueCertificateThrowsException () {
9596 caManager .issueCertificate (null , null , null , 1 , null );
9697 }
9798
9899 @ Test
99- public void testIssueCertificate () throws Exception {
100+ public void testIssueCertificate () {
100101 caManager .issueCertificate (null , Collections .singletonList ("domain.example" ), null , 1 , null );
101102 Mockito .verify (caProvider , Mockito .times (1 )).issueCertificate (anyList (), nullable (List .class ), anyInt ());
102103 Mockito .verify (caProvider , Mockito .times (0 )).issueCertificate (anyString (), anyList (), anyList (), anyInt ());
103104 }
104105
105106 @ Test
106- public void testRevokeCertificate () throws Exception {
107+ public void testRevokeCertificate () {
107108 final CrlVO crl = new CrlVO (CertUtils .generateRandomBigInt (), "some.domain" , "some-uuid" );
108109 Mockito .when (crlDao .revokeCertificate (Mockito .any (BigInteger .class ), anyString ())).thenReturn (crl );
109110 Mockito .when (caProvider .revokeCertificate (Mockito .any (BigInteger .class ), anyString ())).thenReturn (true );
@@ -126,4 +127,93 @@ public void testProvisionCertificate() throws Exception {
126127 Mockito .verify (agentManager , Mockito .times (1 )).send (Mockito .anyLong (), any (SetupCertificateCommand .class ));
127128 Mockito .verify (agentManager , Mockito .times (1 )).reconnect (Mockito .anyLong ());
128129 }
130+
131+
132+ @ Test
133+ public void testProvisionCertificateForced () throws Exception {
134+ final Host host = Mockito .mock (Host .class );
135+ Mockito .doReturn (true ).when (caManager ).provisionCertificateForced (host , true , null );
136+ Assert .assertTrue (caManager .provisionCertificate (host , true , null , true ));
137+ Mockito .verify (caManager , Mockito .times (1 )).provisionCertificateForced (host , true , null );
138+ Mockito .verify (agentManager , Mockito .never ()).send (Mockito .anyLong (), any (SetupKeyStoreCommand .class ));
139+ Mockito .verify (agentManager , Mockito .never ()).send (Mockito .anyLong (), any (SetupCertificateCommand .class ));
140+ }
141+
142+ @ Test
143+ public void testIssueCertificateWithCsr () throws Exception {
144+ final KeyPair keyPair = CertUtils .generateRandomKeyPair (1024 );
145+ final X509Certificate x509 = CertUtils .generateV3Certificate (null , keyPair , keyPair .getPublic (), "CN=ca" , "SHA256withRSA" , 365 , null , null );
146+ Mockito .when (caProvider .issueCertificate (anyString (), anyList (), anyList (), anyInt ()))
147+ .thenReturn (new Certificate (x509 , null , Collections .singletonList (x509 )));
148+ final Certificate result = caManager .issueCertificate ("someCsr" , Collections .singletonList ("domain.example" ), Collections .singletonList ("1.2.3.4" ), 365 , null );
149+ Assert .assertNotNull (result );
150+ Mockito .verify (caProvider , Mockito .times (1 )).issueCertificate (anyString (), anyList (), anyList (), anyInt ());
151+ Mockito .verify (caProvider , Mockito .never ()).issueCertificate (anyList (), nullable (List .class ), anyInt ());
152+ }
153+
154+ @ Test (expected = CloudRuntimeException .class )
155+ public void testProvisionCertificateNullHost () {
156+ caManager .provisionCertificate (null , true , null , false );
157+ }
158+
159+ @ Test
160+ public void testProvisionCertificateForSystemVm () throws Exception {
161+ final Host host = Mockito .mock (Host .class );
162+ Mockito .when (host .getType ()).thenReturn (Host .Type .ConsoleProxy );
163+ Mockito .when (host .getPrivateIpAddress ()).thenReturn ("1.2.3.4" );
164+ final KeyPair keyPair = CertUtils .generateRandomKeyPair (1024 );
165+ final X509Certificate x509 = CertUtils .generateV3Certificate (null , keyPair , keyPair .getPublic (), "CN=ca" , "SHA256withRSA" , 365 , null , null );
166+ Mockito .when (caProvider .issueCertificate (anyList (), anyList (), anyInt ()))
167+ .thenReturn (new Certificate (x509 , null , Collections .singletonList (x509 )));
168+ Mockito .when (agentManager .send (anyLong (), any (SetupCertificateCommand .class ))).thenReturn (new SetupCertificateAnswer (true ));
169+ Assert .assertTrue (caManager .provisionCertificate (host , false , null , false ));
170+ Mockito .verify (agentManager , Mockito .never ()).send (Mockito .anyLong (), any (SetupKeyStoreCommand .class ));
171+ Mockito .verify (agentManager , Mockito .times (1 )).send (Mockito .anyLong (), any (SetupCertificateCommand .class ));
172+ Mockito .verify (agentManager , Mockito .never ()).reconnect (Mockito .anyLong ());
173+ }
174+
175+ @ Test
176+ public void testProvisionCertificateWithoutReconnect () throws Exception {
177+ final Host host = Mockito .mock (Host .class );
178+ Mockito .when (host .getPrivateIpAddress ()).thenReturn ("1.2.3.4" );
179+ final KeyPair keyPair = CertUtils .generateRandomKeyPair (1024 );
180+ final X509Certificate x509 = CertUtils .generateV3Certificate (null , keyPair , keyPair .getPublic (), "CN=ca" , "SHA256withRSA" , 365 , null , null );
181+ Mockito .when (caProvider .issueCertificate (anyString (), anyList (), anyList (), anyInt ()))
182+ .thenReturn (new Certificate (x509 , null , Collections .singletonList (x509 )));
183+ Mockito .when (agentManager .send (anyLong (), any (SetupCertificateCommand .class ))).thenReturn (new SetupCertificateAnswer (true ));
184+ Mockito .when (agentManager .send (anyLong (), any (SetupKeyStoreCommand .class ))).thenReturn (new SetupKeystoreAnswer ("someCsr" ));
185+ Assert .assertTrue (caManager .provisionCertificate (host , false , null , false ));
186+ Mockito .verify (agentManager , Mockito .never ()).reconnect (Mockito .anyLong ());
187+ }
188+
189+ @ Test
190+ public void testRevokeCertificateReturnsFalseWhenCrlIsNull () {
191+ Mockito .when (crlDao .revokeCertificate (Mockito .any (BigInteger .class ), anyString ())).thenReturn (null );
192+ Assert .assertFalse (caManager .revokeCertificate (BigInteger .ONE , "some.domain" , null ));
193+ Mockito .verify (caProvider , Mockito .never ()).revokeCertificate (Mockito .any (BigInteger .class ), anyString ());
194+ }
195+
196+ @ Test
197+ public void testRevokeCertificateReturnsFalseWhenSerialMismatch () {
198+ final CrlVO crl = new CrlVO (BigInteger .ONE , "some.domain" , "some-uuid" );
199+ Mockito .when (crlDao .revokeCertificate (Mockito .any (BigInteger .class ), anyString ())).thenReturn (crl );
200+ Assert .assertFalse (caManager .revokeCertificate (BigInteger .TWO , "some.domain" , null ));
201+ Mockito .verify (caProvider , Mockito .never ()).revokeCertificate (Mockito .any (BigInteger .class ), anyString ());
202+ }
203+
204+ @ Test
205+ public void testPurgeHostCertificate () throws Exception {
206+ final Host host = Mockito .mock (Host .class );
207+ Mockito .when (host .getPrivateIpAddress ()).thenReturn ("10.0.0.1" );
208+ Mockito .when (host .getPublicIpAddress ()).thenReturn ("192.168.0.1" );
209+ final KeyPair keyPair = CertUtils .generateRandomKeyPair (1024 );
210+ final X509Certificate x509 = CertUtils .generateV3Certificate (null , keyPair ,
211+ keyPair .getPublic (), "CN=ca" , "SHA256withRSA" ,
212+ 365 , null , null );
213+ caManager .getActiveCertificatesMap ().put ("10.0.0.1" , x509 );
214+ caManager .getActiveCertificatesMap ().put ("192.168.0.1" , x509 );
215+ caManager .purgeHostCertificate (host );
216+ Assert .assertFalse (caManager .getActiveCertificatesMap ().containsKey ("10.0.0.1" ));
217+ Assert .assertFalse (caManager .getActiveCertificatesMap ().containsKey ("192.168.0.1" ));
218+ }
129219}
0 commit comments