]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/TestSeparateSessionScope.java
fix: ResultSet.getDate is often wrong as it fetches day-precision times
[gigi.git] / tests / org / cacert / gigi / TestSeparateSessionScope.java
1 package org.cacert.gigi;
2
3 import static org.junit.Assert.*;
4
5 import java.io.IOException;
6 import java.net.HttpURLConnection;
7 import java.net.URL;
8 import java.security.GeneralSecurityException;
9 import java.security.KeyManagementException;
10 import java.security.KeyPair;
11 import java.security.NoSuchAlgorithmException;
12 import java.security.PrivateKey;
13 import java.security.cert.X509Certificate;
14 import java.sql.SQLException;
15
16 import org.cacert.gigi.dbObjects.Certificate;
17 import org.cacert.gigi.dbObjects.Certificate.CSRType;
18 import org.cacert.gigi.dbObjects.Digest;
19 import org.cacert.gigi.dbObjects.Job;
20 import org.cacert.gigi.dbObjects.User;
21 import org.cacert.gigi.testUtils.ManagedTest;
22 import org.junit.Test;
23
24 public class TestSeparateSessionScope extends ManagedTest {
25
26     @Test
27     public void testSeparateScope() throws IOException, GeneralSecurityException, SQLException, InterruptedException, GigiApiException {
28         String mail = "thisgo" + createUniqueName() + "@example.com";
29         int user = createAssuranceUser("test", "tugo", mail, TEST_PASSWORD);
30         String cookie = login(mail, TEST_PASSWORD);
31         KeyPair kp = generateKeypair();
32         String csr = generatePEMCSR(kp, "CN=hans");
33         User u = User.getById(user);
34         Certificate c = new Certificate(u, u, Certificate.buildDN("CN", "hans"), Digest.SHA256, csr, CSRType.CSR, getClientProfile());
35         final PrivateKey pk = kp.getPrivate();
36         await(c.issue(null, "2y", u));
37         final X509Certificate ce = c.cert();
38         c.setLoginEnabled(true);
39         String scookie = login(pk, ce);
40
41         assertTrue(isLoggedin(cookie));
42         assertFalse(isLoggedin(scookie));
43
44         checkCertLogin(c, pk, scookie, 200);
45         checkCertLogin(c, pk, cookie, 302);
46     }
47
48     @Test
49     public void testSerialSteal() throws IOException, GeneralSecurityException, SQLException, InterruptedException, GigiApiException {
50         String mail = "thisgo" + createUniqueName() + "@example.com";
51         int user = createAssuranceUser("test", "tugo", mail, TEST_PASSWORD);
52         KeyPair kp = generateKeypair();
53         String csr = generatePEMCSR(kp, "CN=hans");
54         User u = User.getById(user);
55         Certificate c = new Certificate(u, u, Certificate.buildDN("CN", "hans"), Digest.SHA256, csr, CSRType.CSR, getClientProfile());
56         Certificate c2 = new Certificate(u, u, Certificate.buildDN("CN", "hans"), Digest.SHA256, csr, CSRType.CSR, getClientProfile());
57         final PrivateKey pk = kp.getPrivate();
58         Job j1 = c.issue(null, "2y", u);
59         await(c2.issue(null, "2y", u));
60         await(j1);
61         final X509Certificate ce = c.cert();
62         c.setLoginEnabled(true);
63         String scookie = login(pk, ce);
64
65         checkCertLogin(c, pk, scookie, 200);
66         checkCertLogin(c2, pk, scookie, 403);
67         checkCertLogin(c, pk, scookie, 302);
68
69     }
70
71     private void checkCertLogin(Certificate c2, final PrivateKey pk, String scookie, int expected) throws IOException, NoSuchAlgorithmException, KeyManagementException, GeneralSecurityException {
72         URL u = new URL("https://" + getSecureServerName() + SECURE_REFERENCE);
73         HttpURLConnection huc = (HttpURLConnection) u.openConnection();
74         authenticateClientCert(pk, c2.cert(), huc);
75         huc.setRequestProperty("Cookie", scookie);
76         assertEquals(expected, huc.getResponseCode());
77     }
78 }