]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/TestCertificate.java
Merge branch 'spkac'
[gigi.git] / tests / org / cacert / gigi / TestCertificate.java
1 package org.cacert.gigi;
2
3 import java.io.IOException;
4 import java.security.GeneralSecurityException;
5 import java.security.PrivateKey;
6 import java.security.cert.X509Certificate;
7 import java.sql.SQLException;
8
9 import org.cacert.gigi.Certificate.CSRType;
10 import org.cacert.gigi.Certificate.CertificateStatus;
11 import org.cacert.gigi.testUtils.ManagedTest;
12 import org.cacert.gigi.testUtils.PemKey;
13 import org.junit.Test;
14
15 import static org.junit.Assert.*;
16
17 public class TestCertificate extends ManagedTest {
18         @Test
19         public void testClientCertLoginStates() throws IOException, GeneralSecurityException, SQLException,
20                 InterruptedException {
21                 String[] key1 = generateCSR("/CN=testmail@example.com");
22                 Certificate c = new Certificate(1, "/CN=testmail@example.com", "sha256", key1[1], CSRType.CSR);
23                 final PrivateKey pk = PemKey.parsePEMPrivateKey(key1[0]);
24                 c.issue().waitFor(60000);
25                 final X509Certificate ce = c.cert();
26                 assertNotNull(login(pk, ce));
27         }
28
29         @Test
30         public void testCertLifeCycle() throws IOException, GeneralSecurityException, SQLException, InterruptedException {
31                 String[] key1 = generateCSR("/CN=testmail@example.com");
32                 Certificate c = new Certificate(1, "/CN=testmail@example.com", "sha256", key1[1], CSRType.CSR);
33                 final PrivateKey pk = PemKey.parsePEMPrivateKey(key1[0]);
34
35                 testFails(CertificateStatus.DRAFT, c);
36                 c.issue().waitFor(60000);
37
38                 testFails(CertificateStatus.ISSUED, c);
39                 X509Certificate cert = c.cert();
40                 assertNotNull(login(pk, cert));
41                 c.revoke().waitFor(60000);
42
43                 testFails(CertificateStatus.REVOKED, c);
44                 assertNull(login(pk, cert));
45
46         }
47
48         private void testFails(CertificateStatus status, Certificate c) throws IOException, GeneralSecurityException,
49                 SQLException {
50                 assertEquals(status, c.getStatus());
51                 if (status != CertificateStatus.ISSUED) {
52                         try {
53                                 c.revoke();
54                                 fail(status + " is in invalid state");
55                         } catch (IllegalStateException ise) {
56
57                         }
58                 }
59                 if (status != CertificateStatus.DRAFT) {
60                         try {
61                                 c.issue();
62                                 fail(status + " is in invalid state");
63                         } catch (IllegalStateException ise) {
64
65                         }
66                 }
67                 if (status != CertificateStatus.ISSUED) {
68                         try {
69                                 c.cert();
70                                 fail(status + " is in invalid state");
71                         } catch (IllegalStateException ise) {
72
73                         }
74                 }
75         }
76 }