373c55c23df1d31d53e12d02d130c79208f8eaf4
[gigi.git] / tests / club / wpia / gigi / pages / main / CertStatusTest.java
1 package club.wpia.gigi.pages.main;
2
3 import static org.hamcrest.MatcherAssert.assertThat;
4 import static org.junit.Assert.*;
5
6 import java.io.IOException;
7 import java.io.OutputStream;
8 import java.net.HttpURLConnection;
9 import java.net.URL;
10 import java.net.URLConnection;
11 import java.net.URLEncoder;
12 import java.security.GeneralSecurityException;
13 import java.security.KeyPair;
14
15 import org.hamcrest.CoreMatchers;
16 import org.junit.Test;
17
18 import club.wpia.gigi.GigiApiException;
19 import club.wpia.gigi.dbObjects.Certificate;
20 import club.wpia.gigi.dbObjects.Certificate.CertificateStatus;
21 import club.wpia.gigi.dbObjects.Certificate.RevocationType;
22 import club.wpia.gigi.dbObjects.Digest;
23 import club.wpia.gigi.dbObjects.Job;
24 import club.wpia.gigi.pages.account.certs.CertificateRequest;
25 import club.wpia.gigi.testUtils.ClientTest;
26 import club.wpia.gigi.testUtils.IOUtils;
27 import club.wpia.gigi.util.AuthorizationContext;
28 import club.wpia.gigi.util.PEM;
29
30 public class CertStatusTest extends ClientTest {
31
32     private Certificate cert;
33
34     private String serial;
35
36     private String foreignPEM = "-----BEGIN CERTIFICATE-----\n" + "MIIGvjCCBKagAwIBAgIVEQAAAAfLkxaJ4wATnrSBUbEr3UsxMA0GCSqGSIb3DQEB\n" + "DQUAMHExFzAVBgNVBAMMDkFzc3VyZWQgMjAxNy0yMSowKAYDVQQKDCFUZXN0IEVu\n" + "dmlyb25tZW50IENBIEx0ZC4tMTctMDMtMDQxHTAbBgNVBAsMFFRlc3QgRW52aXJv\n" + "bm1lbnQgQ0FzMQswCQYDVQQGEwJBVTAeFw0xNzA4MTUxMDI5NTdaFw0xNzA4MTYw\n" + "MDAwMDBaMDkxETAPBgNVBAMMCE1hcmN1cyBNMSQwIgYJKoZIhvcNAQkBFhVtLm1h\n" + "ZW5nZWxAaW5vcGlhZS5jb20wggIiMA0GCSqGSIb3DQEBAQ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xodHRwOi8vZzIuY3J0LnRlc3QxLmJhY2t1cC5kb2dj\n" + "cmFmdC5kZS9nMi8yMDE3L2Fzc3VyZWQtMi5jcnQwTQYDVR0fBEYwRDBCoECgPoY8\n" + "aHR0cDovL2cyLmNybC50ZXN0MS5iYWNrdXAuZG9nY3JhZnQuZGUvZzIvMjAxNy9h\n" + "c3N1cmVkLTIuY3JsMCAGA1UdEQQZMBeBFW0ubWFlbmdlbEBpbm9waWFlLmNvbTAN\n" + "BgkqhkiG9w0BAQ0FAAOCAgEATRC7wwfFNExFk6LGcAbYSJViVs8ZgFuaTEzlBrik\n" + "mf9f8QA7Aj2bH2hqCdjbem1ElXhbADcJopS46P7yfH57zUj3qvD9znK0DdqWpvsO\n" + "nCB7/kdA0KysxTZ+D5gFgk/MpDfNP8ALB1SHGEOv/l4gQs0Zn6ORxt+4zrLzqExO\n" + "dMYdxcVQCl0ft5tQRUSxg1k2y8crgplR02TvhJCrb+RNCS0SQMkEA11bZKEpLBYk\n" + "bJMJYMr+SMN/wtC/vjXm9hrPGqnfqpJC7IqHUfzcBt10dGPqzvO/6xnEZn4YSgjr\n" + "MyoVUnOmcgolFrToYbXr3CNoQFO5Dgz7hbXH59/6ph35g7Q3hllTV+DGV753Baaa\n" + "bMgAsUeJqdMcJSAorLKjibinF/odbJ/kghAg7LBLQUmCvfYWzKhnfETXQ/qXbOk7\n" + "fufEB0z1AnzOB032Cde+FZg1NofjyF8N0UuK4l8fS+hSX6bcJaIuvUSNm5Mj2laZ\n" + "cskPgOu2Gng1JteLbotEKnruKshfKgo64Fq/mPASHfrSdAeQ/shlL6JG3QQeiw9k\n" + "Yu7lu7neRduthxwEdZ8EYrQ0fnHWrmnGsDCpvNIv1coaPc0ghi2pfGjEBAXGQoQ3\n" + "7Bia1anze/wG/9viZyuH1Ms10Ya9E8bPfB1D7B26tB6IZUNLaMnoYbCd+EN7Zjx/\n" + "Yac=\n" + "-----END CERTIFICATE-----";
37
38     public CertStatusTest() throws GeneralSecurityException, IOException, GigiApiException, InterruptedException {
39
40         KeyPair kp = generateKeypair();
41         String csr = generatePEMCSR(kp, "CN=test");
42         CertificateRequest cr = new CertificateRequest(new AuthorizationContext(u, u), csr);
43         cr.update(CertificateRequest.DEFAULT_CN, Digest.SHA512.toString(), "client", null, null, "email:" + email + "\n");
44         cert = cr.draft();
45         Job j = cert.issue(null, "2y", u);
46         await(j);
47         serial = cert.getSerial().toLowerCase();
48
49     }
50
51     @Test
52     public void testCertStatus() throws IOException, InterruptedException, GigiApiException, GeneralSecurityException {
53         testExecution("serial=" + URLEncoder.encode(serial, "UTF-8"), null, false);// serial
54         testExecution("serial=0000" + URLEncoder.encode(serial, "UTF-8"), null, false);// leading
55                                                                                        // Zeros
56         testExecution("serial=0000" + URLEncoder.encode(serial.toUpperCase(), "UTF-8"), null, false);// upper
57                                                                                                      // case
58
59         testExecution("serial=0000", "Malformed serial", false);
60         testExecution("serial=0lkd", "Malformed serial", false);
61
62         testExecution("cert=" + URLEncoder.encode(PEM.encode("CERTIFICATE", cert.cert().getEncoded()), "UTF-8"), null, false);
63         testExecution("cert=" + URLEncoder.encode(foreignPEM, "UTF-8"), "Certificate to check not found.", false);
64         testExecution("cert=sometext", "Certificate could not be parsed", false);
65
66         await(cert.revoke(RevocationType.USER));
67
68         testExecution("serial=" + URLEncoder.encode(serial, "UTF-8"), "Certificate has been revoked on ", true);// serial
69         testExecution("cert=" + URLEncoder.encode(PEM.encode("CERTIFICATE", cert.cert().getEncoded()), "UTF-8"), "Certificate has been revoked on ", true);
70
71     }
72
73     public void testExecution(String query, String error, boolean revoked) throws IOException, InterruptedException, GigiApiException, GeneralSecurityException {
74         URLConnection uc = new URL("https://" + getServerName() + CertStatusRequestPage.PATH).openConnection();
75         uc.addRequestProperty("Cookie", cookie);
76         String content = IOUtils.readURL(uc);
77         String csrf = getCSRF(0, content);
78
79         uc = new URL("https://" + getServerName() + CertStatusRequestPage.PATH).openConnection();
80         uc.addRequestProperty("Cookie", cookie);
81         uc.setDoOutput(true);
82         OutputStream os = uc.getOutputStream();
83         os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" + query).getBytes("UTF-8"));
84         os.flush();
85         HttpURLConnection huc = (HttpURLConnection) uc;
86
87         String result = IOUtils.readURL(huc);
88
89         if (error == null) {
90             assertThat(result, hasNoError());
91             assertThat(result, CoreMatchers.containsString("Certificate is valid."));
92         } else {
93             assertThat(fetchStartErrorMessage(result), CoreMatchers.containsString(error));
94             if (revoked == false) {
95                 assertNotEquals(CertificateStatus.REVOKED, cert.getStatus());
96             } else {
97                 assertEquals(CertificateStatus.REVOKED, cert.getStatus());
98             }
99         }
100     }
101 }