]> WPIA git - gigi.git/blob - tests/club/wpia/gigi/dbObjects/TestCertificate.java
694bc491c8f514550664419d54528cecf9273b72
[gigi.git] / tests / club / wpia / gigi / dbObjects / TestCertificate.java
1 package club.wpia.gigi.dbObjects;
2
3 import static org.junit.Assert.*;
4
5 import java.io.IOException;
6 import java.security.GeneralSecurityException;
7 import java.security.KeyPair;
8
9 import org.junit.Test;
10
11 import club.wpia.gigi.GigiApiException;
12 import club.wpia.gigi.dbObjects.Certificate.AttachmentType;
13 import club.wpia.gigi.dbObjects.Certificate.CSRType;
14 import club.wpia.gigi.testUtils.ClientBusinessTest;
15
16 public class TestCertificate extends ClientBusinessTest {
17
18     @Test
19     public void testSetLoginEnabled() throws GeneralSecurityException, IOException, GigiApiException {
20         KeyPair kp = generateKeypair();
21         String key = generatePEMCSR(kp, "CN=testmail@example.com");
22         Certificate c = new Certificate(u, u, Certificate.buildDN("CN", "testmail@example.com"), Digest.SHA256, key, CSRType.CSR, getClientProfile());
23
24         assertFalse(c.isLoginEnabled());
25         c.setLoginEnabled(true);
26         assertTrue(c.isLoginEnabled());
27         c.setLoginEnabled(true);
28         assertTrue(c.isLoginEnabled());
29         c.setLoginEnabled(false);
30         assertFalse(c.isLoginEnabled());
31         c.setLoginEnabled(false);
32         assertFalse(c.isLoginEnabled());
33     }
34
35     @Test
36     public void testAttachment() throws GeneralSecurityException, IOException, GigiApiException {
37         KeyPair kp = generateKeypair();
38         String key = generatePEMCSR(kp, "CN=testmail@example.com");
39         Certificate c = new Certificate(u, u, Certificate.buildDN("CN", "testmail@example.com"), Digest.SHA256, key, CSRType.CSR, getClientProfile());
40         assertNull(c.getAttachment(AttachmentType.CRT));
41         assertNull(c.getAttachment(AttachmentType.CSR));
42         c.addAttachment(AttachmentType.CSR, "a");
43         assertNull(c.getAttachment(AttachmentType.CRT));
44         assertEquals("a", c.getAttachment(AttachmentType.CSR));
45         try {
46             c.addAttachment(AttachmentType.CSR, "different CSR");
47             fail("double add attachment must fail");
48         } catch (GigiApiException e) {
49             // expected
50         }
51         assertNull(c.getAttachment(AttachmentType.CRT));
52         assertEquals("a", c.getAttachment(AttachmentType.CSR));
53         try {
54             c.addAttachment(AttachmentType.CRT, null);
55             fail("attachment must not be null");
56         } catch (GigiApiException e) {
57             // expected
58         }
59         assertNull(c.getAttachment(AttachmentType.CRT));
60         assertEquals("a", c.getAttachment(AttachmentType.CSR));
61         c.addAttachment(AttachmentType.CRT, "b");
62         assertEquals("a", c.getAttachment(AttachmentType.CSR));
63         assertEquals("b", c.getAttachment(AttachmentType.CRT));
64         try {
65             c.addAttachment(AttachmentType.CRT, "different CRT");
66             fail("double add attachment must fail");
67         } catch (GigiApiException e) {
68             // expected
69         }
70         assertEquals("a", c.getAttachment(AttachmentType.CSR));
71         assertEquals("b", c.getAttachment(AttachmentType.CRT));
72     }
73 }