1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package nl.altindag.ssl.util;
18
19 import com.sun.net.httpserver.HttpsServer;
20 import nl.altindag.ssl.SSLFactory;
21 import nl.altindag.ssl.ServerUtils;
22 import org.junit.jupiter.api.Test;
23
24 import java.io.IOException;
25 import java.security.cert.Certificate;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.concurrent.ExecutorService;
29 import java.util.concurrent.Executors;
30
31 import static org.assertj.core.api.Assertions.assertThat;
32
33
34
35
36 class CertificateUtilsIT {
37
38 @Test
39 void getRemoteCertificates() {
40 Map<String, List<Certificate>> certificatesFromRemote = CertificateUtils.getCertificate(
41 "https://stackoverflow.com/",
42 "https://github.com/",
43 "https://www.linkedin.com/"
44 );
45
46 assertThat(certificatesFromRemote).containsKeys(
47 "https://stackoverflow.com/",
48 "https://github.com/",
49 "https://www.linkedin.com/"
50 );
51
52 assertThat(certificatesFromRemote.get("https://stackoverflow.com/")).hasSizeGreaterThan(0);
53 assertThat(certificatesFromRemote.get("https://github.com/")).hasSizeGreaterThan(0);
54 assertThat(certificatesFromRemote.get("https://www.linkedin.com/")).hasSizeGreaterThan(0);
55 }
56
57 @Test
58 void getRemoteCertificatesAsPem() {
59 Map<String, List<String>> certificatesFromRemote = CertificateUtils.getCertificateAsPem(
60 "https://stackoverflow.com/",
61 "https://github.com/",
62 "https://www.linkedin.com/"
63 );
64
65 assertThat(certificatesFromRemote).containsKeys(
66 "https://stackoverflow.com/",
67 "https://github.com/",
68 "https://www.linkedin.com/"
69 );
70
71 assertThat(certificatesFromRemote.get("https://stackoverflow.com/")).hasSizeGreaterThan(0);
72 assertThat(certificatesFromRemote.get("https://github.com/")).hasSizeGreaterThan(0);
73 assertThat(certificatesFromRemote.get("https://www.linkedin.com/")).hasSizeGreaterThan(0);
74 }
75
76 @Test
77 void getRemoteSelfSignedCertificate() throws IOException {
78 ExecutorService executorService = Executors.newSingleThreadExecutor();
79
80 char[] keyStorePassword = "secret".toCharArray();
81 SSLFactory sslFactoryForServerOne = SSLFactory.builder()
82 .withIdentityMaterial("keystores-for-unit-tests/client-server/server-one/identity.jks", keyStorePassword)
83 .withTrustMaterial("keystores-for-unit-tests/client-server/server-one/truststore.jks", keyStorePassword)
84 .withProtocols("TLSv1.2")
85 .build();
86
87 HttpsServer server = ServerUtils.createServer(8443, sslFactoryForServerOne, executorService, "");
88 server.start();
89
90 Map<String, List<Certificate>> certificatesFromRemote = CertificateUtils.getCertificate("https://localhost:8443");
91
92 server.stop(0);
93 executorService.shutdownNow();
94
95 assertThat(certificatesFromRemote).containsKeys("https://localhost:8443");
96 assertThat(certificatesFromRemote.get("https://localhost:8443")).hasSizeGreaterThan(0);
97 }
98
99 @Test
100 void getRemoteCustomRootCaSignedCertificate() throws IOException {
101 ExecutorService executorService = Executors.newSingleThreadExecutor();
102
103 char[] keyStorePassword = "secret".toCharArray();
104 SSLFactory sslFactoryForServerOne = SSLFactory.builder()
105 .withIdentityMaterial("keystores-for-unit-tests/client-server/server-three/identity.jks", keyStorePassword)
106 .withTrustMaterial("keystores-for-unit-tests/client-server/server-three/truststore.jks", keyStorePassword)
107 .withProtocols("TLSv1.2")
108 .build();
109
110 HttpsServer server = ServerUtils.createServer(8443, sslFactoryForServerOne, executorService, "");
111 server.start();
112
113 Map<String, List<Certificate>> certificatesFromRemote = CertificateUtils.getCertificate("https://localhost:8443");
114
115 server.stop(0);
116 executorService.shutdownNow();
117
118 assertThat(certificatesFromRemote).containsKeys("https://localhost:8443");
119 assertThat(certificatesFromRemote.get("https://localhost:8443")).hasSizeGreaterThan(0);
120 }
121
122 }