18
18
*/
19
19
package org .neo4j .driver .internal ;
20
20
21
+ import org .junit .jupiter .api .Nested ;
22
+ import org .junit .jupiter .api .Test ;
21
23
import org .junit .jupiter .params .ParameterizedTest ;
22
24
import org .junit .jupiter .params .provider .MethodSource ;
25
+ import org .junit .platform .commons .support .ReflectionSupport ;
23
26
27
+ import java .io .File ;
28
+ import java .io .IOException ;
29
+ import java .lang .reflect .InvocationTargetException ;
30
+ import java .lang .reflect .Method ;
24
31
import java .util .stream .Stream ;
25
32
26
33
import org .neo4j .driver .Config ;
27
34
import org .neo4j .driver .exceptions .ClientException ;
28
35
import org .neo4j .driver .internal .security .SecurityPlan ;
36
+ import org .neo4j .driver .util .TestUtil ;
29
37
30
38
import static org .junit .jupiter .api .Assertions .assertEquals ;
31
39
import static org .junit .jupiter .api .Assertions .assertFalse ;
32
40
import static org .junit .jupiter .api .Assertions .assertThrows ;
33
41
import static org .junit .jupiter .api .Assertions .assertTrue ;
34
- import static org .neo4j .driver .internal .RevocationStrategy .STRICT ;
35
42
import static org .neo4j .driver .internal .RevocationStrategy .NO_CHECKS ;
43
+ import static org .neo4j .driver .internal .RevocationStrategy .STRICT ;
36
44
import static org .neo4j .driver .internal .RevocationStrategy .VERIFY_IF_PRESENT ;
37
45
38
46
class SecuritySettingsTest
@@ -98,7 +106,7 @@ void testSelfSignedCertConfigDisablesHostnameVerification( String scheme ) throw
98
106
void testThrowsOnUserCustomizedEncryption ( String scheme )
99
107
{
100
108
SecuritySettings securitySettings = new SecuritySettings .SecuritySettingsBuilder ()
101
- .withoutEncryption ()
109
+ .withEncryption ()
102
110
.build ();
103
111
104
112
ClientException ex =
@@ -113,7 +121,7 @@ void testThrowsOnUserCustomizedEncryption( String scheme )
113
121
void testThrowsOnUserCustomizedTrustConfiguration ( String scheme )
114
122
{
115
123
SecuritySettings securitySettings = new SecuritySettings .SecuritySettingsBuilder ()
116
- .withTrustStrategy ( Config .TrustStrategy .trustSystemCertificates () )
124
+ .withTrustStrategy ( Config .TrustStrategy .trustAllCertificates () )
117
125
.build ();
118
126
119
127
ClientException ex =
@@ -218,4 +226,102 @@ void testRevocationCheckingDisabledByDefault( String scheme )
218
226
assertEquals ( NO_CHECKS , securityPlan .revocationStrategy () );
219
227
}
220
228
229
+ @ Nested
230
+ class SerializationTests
231
+ {
232
+ Method isCustomized = ReflectionSupport .findMethod ( SecuritySettings .class , "isCustomized" ).orElseThrow (
233
+ () -> new RuntimeException ( "This test requires isCustomized to be present." ) );
234
+
235
+ boolean isCustomized ( SecuritySettings securitySettings )
236
+ {
237
+ isCustomized .setAccessible ( true );
238
+ try
239
+ {
240
+ return (boolean ) isCustomized .invoke ( securitySettings );
241
+ }
242
+ catch ( IllegalAccessException | InvocationTargetException e )
243
+ {
244
+ throw new RuntimeException ( e );
245
+ }
246
+ }
247
+
248
+ @ Test
249
+ void defaultSettingsShouldNotBeCustomizedWhenReadBack () throws IOException , ClassNotFoundException
250
+ {
251
+ SecuritySettings securitySettings = new SecuritySettings .SecuritySettingsBuilder ().build ();
252
+
253
+ assertFalse ( isCustomized ( securitySettings ) );
254
+
255
+ SecuritySettings verify = TestUtil .serializeAndReadBack ( securitySettings , SecuritySettings .class );
256
+
257
+ assertFalse ( isCustomized ( verify ) );
258
+ }
259
+
260
+ @ Test
261
+ void defaultsShouldBeCheckCorrect () throws IOException , ClassNotFoundException
262
+ {
263
+ SecuritySettings securitySettings = new SecuritySettings .SecuritySettingsBuilder ().withoutEncryption ().withTrustStrategy (
264
+ Config .TrustStrategy .trustSystemCertificates () ).build ();
265
+
266
+ // The settings are still equivalent to the defaults, even if the builder has been used. It is not customized.
267
+ assertFalse ( isCustomized ( securitySettings ) );
268
+
269
+ SecuritySettings verify = TestUtil .serializeAndReadBack ( securitySettings , SecuritySettings .class );
270
+
271
+ assertFalse ( isCustomized ( verify ) );
272
+ }
273
+
274
+ @ Test
275
+ void shouldReadBackChangedEncryption () throws IOException , ClassNotFoundException
276
+ {
277
+ SecuritySettings securitySettings =
278
+ new SecuritySettings .SecuritySettingsBuilder ().withEncryption ().withTrustStrategy ( Config .TrustStrategy .trustSystemCertificates () ).build ();
279
+
280
+ assertTrue ( isCustomized ( securitySettings ) );
281
+ assertTrue ( securitySettings .encrypted () );
282
+
283
+ SecuritySettings verify = TestUtil .serializeAndReadBack ( securitySettings , SecuritySettings .class );
284
+
285
+ assertTrue ( isCustomized ( verify ) );
286
+ assertTrue ( securitySettings .encrypted () );
287
+ }
288
+
289
+ @ Test
290
+ void shouldReadBackChangedStrategey () throws IOException , ClassNotFoundException
291
+ {
292
+ SecuritySettings securitySettings =
293
+ new SecuritySettings .SecuritySettingsBuilder ().withoutEncryption ().withTrustStrategy ( Config .TrustStrategy .trustAllCertificates () ).build ();
294
+
295
+ // The settings are still equivalent to the defaults, even if the builder has been used. It is not customized.
296
+ assertTrue ( isCustomized ( securitySettings ) );
297
+ assertFalse ( securitySettings .encrypted () );
298
+ assertEquals ( Config .TrustStrategy .trustAllCertificates ().strategy (), securitySettings .trustStrategy ().strategy () );
299
+
300
+ SecuritySettings verify = TestUtil .serializeAndReadBack ( securitySettings , SecuritySettings .class );
301
+
302
+ assertTrue ( isCustomized ( verify ) );
303
+ assertFalse ( securitySettings .encrypted () );
304
+ assertEquals ( Config .TrustStrategy .trustAllCertificates ().strategy (), securitySettings .trustStrategy ().strategy () );
305
+ }
306
+
307
+ @ Test
308
+ void shouldReadBackChangedCertFile () throws IOException , ClassNotFoundException
309
+ {
310
+ SecuritySettings securitySettings = new SecuritySettings .SecuritySettingsBuilder ().withoutEncryption ().withTrustStrategy (
311
+ Config .TrustStrategy .trustCustomCertificateSignedBy ( new File ( "some.cert" ) ) ).build ();
312
+
313
+ // The settings are still equivalent to the defaults, even if the builder has been used. It is not customized.
314
+ assertTrue ( isCustomized ( securitySettings ) );
315
+ assertFalse ( securitySettings .encrypted () );
316
+ assertEquals ( Config .TrustStrategy .trustCustomCertificateSignedBy ( new File ( "some.cert" ) ).strategy (),
317
+ securitySettings .trustStrategy ().strategy () );
318
+
319
+ SecuritySettings verify = TestUtil .serializeAndReadBack ( securitySettings , SecuritySettings .class );
320
+
321
+ assertTrue ( isCustomized ( verify ) );
322
+ assertFalse ( securitySettings .encrypted () );
323
+ assertEquals ( Config .TrustStrategy .trustCustomCertificateSignedBy ( new File ( "some.cert" ) ).strategy (),
324
+ securitySettings .trustStrategy ().strategy () );
325
+ }
326
+ }
221
327
}
0 commit comments