1 package org.cacert.gigi;
3 import java.io.IOException;
4 import java.security.GeneralSecurityException;
5 import java.security.KeyPair;
6 import java.security.PrivateKey;
7 import java.security.cert.X509Certificate;
8 import java.sql.SQLException;
10 import org.cacert.gigi.Certificate.CSRType;
11 import org.cacert.gigi.Certificate.CertificateStatus;
12 import org.cacert.gigi.testUtils.ManagedTest;
13 import org.junit.Test;
15 import static org.junit.Assert.*;
17 public class TestCertificate extends ManagedTest {
20 public void testClientCertLoginStates() throws IOException, GeneralSecurityException, SQLException, InterruptedException {
21 KeyPair kp = generateKeypair();
22 String key1 = generatePEMCSR(kp, "CN=testmail@example.com");
23 Certificate c = new Certificate(1, "/CN=testmail@example.com", "sha256", key1, CSRType.CSR);
24 final PrivateKey pk = kp.getPrivate();
25 c.issue().waitFor(60000);
26 final X509Certificate ce = c.cert();
27 assertNotNull(login(pk, ce));
31 public void testCertLifeCycle() throws IOException, GeneralSecurityException, SQLException, InterruptedException {
32 KeyPair kp = generateKeypair();
33 String key = generatePEMCSR(kp, "CN=testmail@example.com");
34 Certificate c = new Certificate(1, "/CN=testmail@example.com", "sha256", key, CSRType.CSR);
35 final PrivateKey pk = kp.getPrivate();
37 testFails(CertificateStatus.DRAFT, c);
38 c.issue().waitFor(60000);
40 testFails(CertificateStatus.ISSUED, c);
41 X509Certificate cert = c.cert();
42 assertNotNull(login(pk, cert));
43 c.revoke().waitFor(60000);
45 testFails(CertificateStatus.REVOKED, c);
46 assertNull(login(pk, cert));
50 private void testFails(CertificateStatus status, Certificate c) throws IOException, GeneralSecurityException, SQLException {
51 assertEquals(status, c.getStatus());
52 if (status != CertificateStatus.ISSUED) {
55 fail(status + " is in invalid state");
56 } catch (IllegalStateException ise) {
60 if (status != CertificateStatus.DRAFT) {
63 fail(status + " is in invalid state");
64 } catch (IllegalStateException ise) {
68 if (status != CertificateStatus.ISSUED) {
71 fail(status + " is in invalid state");
72 } catch (IllegalStateException ise) {