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.CertificateStatus;
10 import org.cacert.gigi.testUtils.ManagedTest;
11 import org.cacert.gigi.testUtils.PemKey;
12 import org.junit.Test;
14 import static org.junit.Assert.*;
16 public class TestCertificate extends ManagedTest {
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));
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]);
34 testFails(CertificateStatus.DRAFT, c);
35 c.issue().waitFor(60000);
37 testFails(CertificateStatus.ISSUED, c);
38 X509Certificate cert = c.cert();
39 assertNotNull(login(pk, cert));
40 c.revoke().waitFor(60000);
42 testFails(CertificateStatus.REVOKED, c);
43 assertNull(login(pk, cert));
47 private void testFails(CertificateStatus status, Certificate c) throws IOException, GeneralSecurityException,
49 assertEquals(status, c.getStatus());
50 if (status != CertificateStatus.ISSUED) {
53 fail(status + " is in invalid state");
54 } catch (IllegalStateException ise) {
58 if (status != CertificateStatus.DRAFT) {
61 fail(status + " is in invalid state");
62 } catch (IllegalStateException ise) {
66 if (status != CertificateStatus.ISSUED) {
69 fail(status + " is in invalid state");
70 } catch (IllegalStateException ise) {