23
23
import java .nio .file .Path ;
24
24
import java .time .Duration ;
25
25
import java .util .List ;
26
+ import java .util .Set ;
26
27
import java .util .stream .Collectors ;
27
28
28
29
import org .junit .jupiter .api .Test ;
29
30
import org .junit .jupiter .api .io .TempDir ;
31
+ import org .junit .jupiter .params .ParameterizedTest ;
32
+ import org .junit .jupiter .params .provider .EnumSource ;
30
33
31
34
import org .springframework .boot .info .SslInfo .BundleInfo ;
32
35
import org .springframework .boot .info .SslInfo .CertificateChainInfo ;
46
49
* Tests for {@link SslInfo}.
47
50
*
48
51
* @author Jonatan Ivanov
52
+ * @author Joshua Chen
49
53
*/
50
54
class SslInfoTests {
51
55
52
- @ Test
56
+ @ ParameterizedTest
57
+ @ EnumSource (StoreType .class )
53
58
@ WithPackageResources ("test.p12" )
54
- void validCertificatesShouldProvideSslInfo () {
55
- SslInfo sslInfo = createSslInfo ("classpath:test.p12" );
59
+ void validCertificatesShouldProvideSslInfo (StoreType storeType ) {
60
+ SslInfo sslInfo = createSslInfo (storeType , "classpath:test.p12" );
56
61
assertThat (sslInfo .getBundles ()).hasSize (1 );
57
62
BundleInfo bundle = sslInfo .getBundles ().get (0 );
58
63
assertThat (bundle .getName ()).isEqualTo ("test-0" );
@@ -62,10 +67,10 @@ void validCertificatesShouldProvideSslInfo() {
62
67
assertThat (bundle .getCertificateChains ().get (1 ).getAlias ()).isEqualTo ("test-alias" );
63
68
assertThat (bundle .getCertificateChains ().get (1 ).getCertificates ()).hasSize (1 );
64
69
assertThat (bundle .getCertificateChains ().get (2 ).getAlias ()).isEqualTo ("spring-boot-cert" );
65
- assertThat (bundle .getCertificateChains ().get (2 ).getCertificates ()).isEmpty ( );
70
+ assertThat (bundle .getCertificateChains ().get (2 ).getCertificates ()).hasSize ( 1 );
66
71
assertThat (bundle .getCertificateChains ().get (3 ).getAlias ()).isEqualTo ("test-alias-cert" );
67
- assertThat (bundle .getCertificateChains ().get (3 ).getCertificates ()).isEmpty ( );
68
- CertificateInfo cert1 = bundle .getCertificateChains ().get (0 ).getCertificates ().get ( 0 );
72
+ assertThat (bundle .getCertificateChains ().get (3 ).getCertificates ()).hasSize ( 1 );
73
+ CertificateInfo cert1 = bundle .getCertificateChains ().get (0 ).getCertificates ().iterator (). next ( );
69
74
assertThat (cert1 .getSubject ()).isEqualTo ("CN=localhost,OU=Spring,O=VMware,L=Palo Alto,ST=California,C=US" );
70
75
assertThat (cert1 .getIssuer ()).isEqualTo (cert1 .getSubject ());
71
76
assertThat (cert1 .getSerialNumber ()).isNotEmpty ();
@@ -76,7 +81,7 @@ void validCertificatesShouldProvideSslInfo() {
76
81
assertThat (cert1 .getValidity ()).isNotNull ();
77
82
assertThat (cert1 .getValidity ().getStatus ()).isSameAs (Status .VALID );
78
83
assertThat (cert1 .getValidity ().getMessage ()).isNull ();
79
- CertificateInfo cert2 = bundle .getCertificateChains ().get (1 ).getCertificates ().get ( 0 );
84
+ CertificateInfo cert2 = bundle .getCertificateChains ().get (1 ).getCertificates ().iterator (). next ( );
80
85
assertThat (cert2 .getSubject ()).isEqualTo ("CN=localhost,OU=Spring,O=VMware,L=Palo Alto,ST=California,C=US" );
81
86
assertThat (cert2 .getIssuer ()).isEqualTo (cert2 .getSubject ());
82
87
assertThat (cert2 .getSerialNumber ()).isNotEmpty ();
@@ -89,19 +94,20 @@ void validCertificatesShouldProvideSslInfo() {
89
94
assertThat (cert2 .getValidity ().getMessage ()).isNull ();
90
95
}
91
96
92
- @ Test
97
+ @ ParameterizedTest
98
+ @ EnumSource (StoreType .class )
93
99
@ WithPackageResources ("test-not-yet-valid.p12" )
94
- void notYetValidCertificateShouldProvideSslInfo () {
95
- SslInfo sslInfo = createSslInfo ("classpath:test-not-yet-valid.p12" );
100
+ void notYetValidCertificateShouldProvideSslInfo (StoreType storeType ) {
101
+ SslInfo sslInfo = createSslInfo (storeType , "classpath:test-not-yet-valid.p12" );
96
102
assertThat (sslInfo .getBundles ()).hasSize (1 );
97
103
BundleInfo bundle = sslInfo .getBundles ().get (0 );
98
104
assertThat (bundle .getName ()).isEqualTo ("test-0" );
99
105
assertThat (bundle .getCertificateChains ()).hasSize (1 );
100
106
CertificateChainInfo certificateChain = bundle .getCertificateChains ().get (0 );
101
107
assertThat (certificateChain .getAlias ()).isEqualTo ("spring-boot" );
102
- List <CertificateInfo > certs = certificateChain .getCertificates ();
108
+ Set <CertificateInfo > certs = certificateChain .getCertificates ();
103
109
assertThat (certs ).hasSize (1 );
104
- CertificateInfo cert = certs .get ( 0 );
110
+ CertificateInfo cert = certs .iterator (). next ( );
105
111
assertThat (cert .getSubject ()).isEqualTo ("CN=localhost,OU=Spring,O=VMware,L=Palo Alto,ST=California,C=US" );
106
112
assertThat (cert .getIssuer ()).isEqualTo (cert .getSubject ());
107
113
assertThat (cert .getSerialNumber ()).isNotEmpty ();
@@ -124,9 +130,9 @@ void expiredCertificateShouldProvideSslInfo() {
124
130
assertThat (bundle .getCertificateChains ()).hasSize (1 );
125
131
CertificateChainInfo certificateChain = bundle .getCertificateChains ().get (0 );
126
132
assertThat (certificateChain .getAlias ()).isEqualTo ("spring-boot" );
127
- List <CertificateInfo > certs = certificateChain .getCertificates ();
133
+ Set <CertificateInfo > certs = certificateChain .getCertificates ();
128
134
assertThat (certs ).hasSize (1 );
129
- CertificateInfo cert = certs .get ( 0 );
135
+ CertificateInfo cert = certs .iterator (). next ( );
130
136
assertThat (cert .getSubject ()).isEqualTo ("CN=localhost,OU=Spring,O=VMware,L=Palo Alto,ST=California,C=US" );
131
137
assertThat (cert .getIssuer ()).isEqualTo (cert .getSubject ());
132
138
assertThat (cert .getSerialNumber ()).isNotEmpty ();
@@ -150,9 +156,9 @@ void soonToBeExpiredCertificateShouldProvideSslInfo(@TempDir Path tempDir)
150
156
assertThat (bundle .getCertificateChains ()).hasSize (1 );
151
157
CertificateChainInfo certificateChain = bundle .getCertificateChains ().get (0 );
152
158
assertThat (certificateChain .getAlias ()).isEqualTo ("spring-boot" );
153
- List <CertificateInfo > certs = certificateChain .getCertificates ();
159
+ Set <CertificateInfo > certs = certificateChain .getCertificates ();
154
160
assertThat (certs ).hasSize (1 );
155
- CertificateInfo cert = certs .get ( 0 );
161
+ CertificateInfo cert = certs .iterator (). next ( );
156
162
assertThat (cert .getSubject ()).isEqualTo ("CN=localhost,OU=Spring,O=VMware,L=Palo Alto,ST=California,C=US" );
157
163
assertThat (cert .getIssuer ()).isEqualTo (cert .getSubject ());
158
164
assertThat (cert .getSerialNumber ()).isNotEmpty ();
@@ -178,7 +184,7 @@ void multipleBundlesShouldProvideSslInfo(@TempDir Path tempDir) throws IOExcepti
178
184
.flatMap ((bundle ) -> bundle .getCertificateChains ().stream ())
179
185
.flatMap ((certificateChain ) -> certificateChain .getCertificates ().stream ())
180
186
.toList ();
181
- assertThat (certs ).hasSize (5 );
187
+ assertThat (certs ).hasSize (7 );
182
188
assertThat (certs ).allSatisfy ((cert ) -> {
183
189
assertThat (cert .getSubject ()).isEqualTo ("CN=localhost,OU=Spring,O=VMware,L=Palo Alto,ST=California,C=US" );
184
190
assertThat (cert .getIssuer ()).isEqualTo (cert .getSubject ());
@@ -227,10 +233,20 @@ void nullKeyStore() {
227
233
}
228
234
229
235
private SslInfo createSslInfo (String ... locations ) {
236
+ return createSslInfo (StoreType .KEYSTORE , locations );
237
+ }
238
+
239
+ private SslInfo createSslInfo (StoreType storeType , String ... locations ) {
230
240
DefaultSslBundleRegistry sslBundleRegistry = new DefaultSslBundleRegistry ();
231
241
for (int i = 0 ; i < locations .length ; i ++) {
232
- JksSslStoreDetails keyStoreDetails = JksSslStoreDetails .forLocation (locations [i ]).withPassword ("secret" );
233
- SslStoreBundle sslStoreBundle = new JksSslStoreBundle (keyStoreDetails , null );
242
+ JksSslStoreDetails storeDetails = JksSslStoreDetails .forLocation (locations [i ]).withPassword ("secret" );
243
+ SslStoreBundle sslStoreBundle ;
244
+ if (storeType == StoreType .TRUSTSTORE ) {
245
+ sslStoreBundle = new JksSslStoreBundle (null , storeDetails );
246
+ }
247
+ else {
248
+ sslStoreBundle = new JksSslStoreBundle (storeDetails , null );
249
+ }
234
250
sslBundleRegistry .registerBundle ("test-%d" .formatted (i ), SslBundle .of (sslStoreBundle ));
235
251
}
236
252
return new SslInfo (sslBundleRegistry , Duration .ofDays (7 ));
@@ -270,4 +286,10 @@ private ProcessBuilder createProcessBuilder(Path keystore) {
270
286
return processBuilder ;
271
287
}
272
288
289
+ private enum StoreType {
290
+
291
+ KEYSTORE , TRUSTSTORE
292
+
293
+ }
294
+
273
295
}
0 commit comments