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