1 package org.cacert.gigi;
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;
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;
15 import static org.junit.Assert.*;
17 public class TestCertificate extends ManagedTest {
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));
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]);
35 testFails(CertificateStatus.DRAFT, c);
36 c.issue().waitFor(60000);
38 testFails(CertificateStatus.ISSUED, c);
39 X509Certificate cert = c.cert();
40 assertNotNull(login(pk, cert));
41 c.revoke().waitFor(60000);
43 testFails(CertificateStatus.REVOKED, c);
44 assertNull(login(pk, cert));
48 private void testFails(CertificateStatus status, Certificate c) throws IOException, GeneralSecurityException,
50 assertEquals(status, c.getStatus());
51 if (status != CertificateStatus.ISSUED) {
54 fail(status + " is in invalid state");
55 } catch (IllegalStateException ise) {
59 if (status != CertificateStatus.DRAFT) {
62 fail(status + " is in invalid state");
63 } catch (IllegalStateException ise) {
67 if (status != CertificateStatus.ISSUED) {
70 fail(status + " is in invalid state");
71 } catch (IllegalStateException ise) {