16
16
17
17
package io .grpc .netty ;
18
18
19
+ import static com .google .common .truth .Truth .assertThat ;
19
20
import static org .junit .Assert .assertArrayEquals ;
20
21
import static org .junit .Assert .assertEquals ;
22
+ import static org .junit .Assert .assertThrows ;
21
23
import static org .junit .Assert .fail ;
22
24
23
25
import com .google .common .util .concurrent .MoreExecutors ;
56
58
import javax .net .ssl .SSLEngine ;
57
59
import org .junit .After ;
58
60
import org .junit .Before ;
59
- import org .junit .Rule ;
60
61
import org .junit .Test ;
61
- import org .junit .rules .ExpectedException ;
62
62
import org .junit .runner .RunWith ;
63
63
import org .junit .runners .JUnit4 ;
64
64
@@ -89,9 +89,6 @@ public class AdvancedTlsTest {
89
89
private PrivateKey serverKeyBad ;
90
90
private X509Certificate [] serverCertBad ;
91
91
92
- @ Rule
93
- public ExpectedException exceptionRule = ExpectedException .none ();
94
-
95
92
@ Before
96
93
public void setUp ()
97
94
throws NoSuchAlgorithmException , IOException , CertificateException , InvalidKeySpecException {
@@ -427,24 +424,22 @@ public void onFileLoadingKeyManagerTrustManagerTest() throws Exception {
427
424
428
425
@ Test
429
426
public void onFileReloadingKeyManagerBadInitialContentTest () throws Exception {
430
- exceptionRule .expect (GeneralSecurityException .class );
431
427
AdvancedTlsX509KeyManager keyManager = new AdvancedTlsX509KeyManager ();
432
428
// We swap the order of key and certificates to intentionally create an exception.
433
- Closeable keyShutdown = keyManager . updateIdentityCredentialsFromFile ( serverCert0File ,
434
- serverKey0File , 100 , TimeUnit . MILLISECONDS , executor );
435
- keyShutdown . close ( );
429
+ assertThrows ( GeneralSecurityException . class ,
430
+ () -> keyManager . updateIdentityCredentialsFromFile ( serverCert0File ,
431
+ serverKey0File , 100 , TimeUnit . MILLISECONDS , executor ) );
436
432
}
437
433
438
434
@ Test
439
435
public void onFileReloadingTrustManagerBadInitialContentTest () throws Exception {
440
- exceptionRule .expect (GeneralSecurityException .class );
441
436
AdvancedTlsX509TrustManager trustManager = AdvancedTlsX509TrustManager .newBuilder ()
442
437
.setVerification (Verification .CERTIFICATE_ONLY_VERIFICATION )
443
438
.build ();
444
439
// We pass in a key as the trust certificates to intentionally create an exception.
445
- Closeable trustShutdown = trustManager . updateTrustCredentialsFromFile ( serverKey0File ,
446
- 100 , TimeUnit . MILLISECONDS , executor );
447
- trustShutdown . close ( );
440
+ assertThrows ( GeneralSecurityException . class ,
441
+ () -> trustManager . updateTrustCredentialsFromFile ( serverKey0File ,
442
+ 100 , TimeUnit . MILLISECONDS , executor ) );
448
443
}
449
444
450
445
@ Test
@@ -472,40 +467,38 @@ public void trustManagerCheckTrustedWithSocketTest() throws Exception {
472
467
473
468
@ Test
474
469
public void trustManagerCheckClientTrustedWithoutParameterTest () throws Exception {
475
- exceptionRule .expect (CertificateException .class );
476
- exceptionRule .expectMessage (
477
- "Not enough information to validate peer. SSLEngine or Socket required." );
478
470
AdvancedTlsX509TrustManager tm = AdvancedTlsX509TrustManager .newBuilder ()
479
471
.setVerification (Verification .INSECURELY_SKIP_ALL_VERIFICATION ).build ();
480
- tm .checkClientTrusted (serverCert0 , "RSA" );
472
+ CertificateException ex =
473
+ assertThrows (CertificateException .class , () -> tm .checkClientTrusted (serverCert0 , "RSA" ));
474
+ assertThat (ex ).hasMessageThat ()
475
+ .isEqualTo ("Not enough information to validate peer. SSLEngine or Socket required." );
481
476
}
482
477
483
478
@ Test
484
479
public void trustManagerCheckServerTrustedWithoutParameterTest () throws Exception {
485
- exceptionRule .expect (CertificateException .class );
486
- exceptionRule .expectMessage (
487
- "Not enough information to validate peer. SSLEngine or Socket required." );
488
480
AdvancedTlsX509TrustManager tm = AdvancedTlsX509TrustManager .newBuilder ()
489
481
.setVerification (Verification .INSECURELY_SKIP_ALL_VERIFICATION ).build ();
490
- tm .checkServerTrusted (serverCert0 , "RSA" );
482
+ CertificateException ex =
483
+ assertThrows (CertificateException .class , () -> tm .checkServerTrusted (serverCert0 , "RSA" ));
484
+ assertThat (ex ).hasMessageThat ()
485
+ .isEqualTo ("Not enough information to validate peer. SSLEngine or Socket required." );
491
486
}
492
487
493
488
@ Test
494
489
public void trustManagerEmptyChainTest () throws Exception {
495
- exceptionRule .expect (IllegalArgumentException .class );
496
- exceptionRule .expectMessage (
497
- "Want certificate verification but got null or empty certificates" );
498
490
AdvancedTlsX509TrustManager tm = AdvancedTlsX509TrustManager .newBuilder ()
499
491
.setVerification (Verification .CERTIFICATE_ONLY_VERIFICATION )
500
492
.build ();
501
493
tm .updateTrustCredentials (caCert );
502
- tm .checkClientTrusted (null , "RSA" , (SSLEngine ) null );
494
+ IllegalArgumentException ex = assertThrows (IllegalArgumentException .class ,
495
+ () -> tm .checkClientTrusted (null , "RSA" , (SSLEngine ) null ));
496
+ assertThat (ex ).hasMessageThat ()
497
+ .isEqualTo ("Want certificate verification but got null or empty certificates" );
503
498
}
504
499
505
500
@ Test
506
501
public void trustManagerBadCustomVerificationTest () throws Exception {
507
- exceptionRule .expect (CertificateException .class );
508
- exceptionRule .expectMessage ("Bad Custom Verification" );
509
502
AdvancedTlsX509TrustManager tm = AdvancedTlsX509TrustManager .newBuilder ()
510
503
.setVerification (Verification .CERTIFICATE_ONLY_VERIFICATION )
511
504
.setSslSocketAndEnginePeerVerifier (
@@ -523,7 +516,10 @@ public void verifyPeerCertificate(X509Certificate[] peerCertChain, String authTy
523
516
}
524
517
}).build ();
525
518
tm .updateTrustCredentials (caCert );
526
- tm .checkClientTrusted (serverCert0 , "RSA" , new Socket ());
519
+ CertificateException ex = assertThrows (
520
+ CertificateException .class ,
521
+ () -> tm .checkClientTrusted (serverCert0 , "RSA" , new Socket ()));
522
+ assertThat (ex ).hasMessageThat ().isEqualTo ("Bad Custom Verification" );
527
523
}
528
524
529
525
private static class SimpleServiceImpl extends SimpleServiceGrpc .SimpleServiceImplBase {
0 commit comments