]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/TestCertificate.java
Format code according do BenBE's formatter.
[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
19     @Test
20     public void testClientCertLoginStates() throws IOException, GeneralSecurityException, SQLException, 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, 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 }