1 package club.wpia.gigi.dbObjects;
3 import static org.junit.Assert.*;
6 import java.io.FileInputStream;
7 import java.io.FileNotFoundException;
8 import java.security.NoSuchAlgorithmException;
9 import java.security.cert.CertificateEncodingException;
10 import java.security.cert.CertificateException;
11 import java.security.cert.CertificateFactory;
12 import java.security.cert.X509Certificate;
14 import org.junit.Before;
15 import org.junit.Test;
17 import club.wpia.gigi.testUtils.ClientBusinessTest;
19 public class TestCACertificate extends ClientBusinessTest {
21 public CertificateFactory fact;
23 public CACertificate root, orga;
25 public int rootId, orgaId;
27 public X509Certificate configRoot;
30 public void getTestCertificates() throws CertificateException, FileNotFoundException {
31 fact = CertificateFactory.getInstance("X.509");
33 for (CACertificate cert : CACertificate.getAll()) {
34 if ("root".equals(cert.getKeyname())) {
36 rootId = cert.getId();
37 } else if ("orga".equals(cert.getKeyname())) {
39 orgaId = cert.getId();
42 FileInputStream fis = new FileInputStream(new File("config/ca/root.crt"));
43 configRoot = (X509Certificate) fact.generateCertificate(fis);
47 public void testGetParent() {
48 assertEquals(root, orga.getParent());
52 public void testGetCertificate() {
53 assertEquals(configRoot, root.getCertificate());
57 public void testToString() {
58 assertEquals("CACertificate: root", root.toString());
59 assertEquals("CACertificate: orga", orga.toString());
63 public void testGetId() {
64 assertEquals(rootId, root.getId());
65 assertEquals(orgaId, orga.getId());
69 public void testGetKeyname() {
70 assertEquals("root", root.getKeyname());
71 assertEquals("orga", orga.getKeyname());
77 public void testGetById() {
78 assertEquals(root, CACertificate.getById(rootId));
79 assertEquals(orga, CACertificate.getById(orgaId));
83 public void testIsSelfsigned() {
84 assertTrue(root.isSelfsigned());
85 assertFalse(orga.isSelfsigned());
89 public void testGetFingerprint() throws CertificateEncodingException, NoSuchAlgorithmException {
90 assertEquals(Certificate.getFingerprint(configRoot, "sha-1"), root.getFingerprint("sha-1"));
91 assertEquals(Certificate.getFingerprint(configRoot, "sha-256"), root.getFingerprint("sha-256"));
95 public void testGetAll() throws FileNotFoundException, CertificateException {
96 for (CACertificate cert : CACertificate.getAll()) {
97 FileInputStream fis = new FileInputStream(new File(String.format("config/ca/%s.crt", cert.getKeyname())));
98 assertEquals(cert.getCertificate(), (X509Certificate) fact.generateCertificate(fis));