Skip to content

Commit

Permalink
Upgrade JUnit to 4.13.2
Browse files Browse the repository at this point in the history
ExpectedException is deprecated, so I fixed the new warnings. However,
we are still using ExpectedException many places and had previously
supressed the warning. See
#7467 . I did not fix those
existing instances that had suppressed the warning, since it is
unrelated to the upgrade and we have been free to fix them at any time
since we dropped Java 7.
  • Loading branch information
ejona86 authored Jan 20, 2023
1 parent 5a2c94b commit 44847bf
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 44 deletions.
9 changes: 2 additions & 7 deletions core/src/test/java/io/grpc/internal/ReadableBuffersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.common.base.Charsets.UTF_8;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock;
Expand All @@ -32,9 +33,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.InvalidMarkException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

Expand All @@ -46,9 +45,6 @@
public class ReadableBuffersTest {
private static final byte[] MSG_BYTES = "hello".getBytes(UTF_8);

@Rule
public final ExpectedException thrown = ExpectedException.none();

@Test
public void empty_returnsEmptyBuffer() {
ReadableBuffer buffer = ReadableBuffers.empty();
Expand Down Expand Up @@ -216,8 +212,7 @@ public void bufferInputStream_markDiscardedAfterDetached() throws IOException {
InputStream inputStream = ReadableBuffers.openStream(buffer, true);
inputStream.mark(5);
((Detachable) inputStream).detach();
thrown.expect(InvalidMarkException.class);
inputStream.reset();
assertThrows(InvalidMarkException.class, () -> inputStream.reset());
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion examples/android/clientcache/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ dependencies {
implementation 'io.grpc:grpc-stub:1.53.0-SNAPSHOT' // CURRENT_GRPC_VERSION
implementation 'org.apache.tomcat:annotations-api:6.0.53'

testImplementation 'junit:junit:4.12'
testImplementation 'junit:junit:4.13.2'
testImplementation 'com.google.truth:truth:1.0.1'
testImplementation 'io.grpc:grpc-testing:1.53.0-SNAPSHOT' // CURRENT_GRPC_VERSION
}
2 changes: 1 addition & 1 deletion examples/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ dependencies {
runtimeOnly "io.grpc:grpc-netty-shaded:${grpcVersion}"

testImplementation "io.grpc:grpc-testing:${grpcVersion}"
testImplementation "junit:junit:4.12"
testImplementation "junit:junit:4.13.2"
testImplementation "org.mockito:mockito-core:3.4.0"
}

Expand Down
2 changes: 1 addition & 1 deletion examples/example-gauth/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
2 changes: 1 addition & 1 deletion examples/example-hostname/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ dependencies {
compileOnly "org.apache.tomcat:annotations-api:6.0.53"
runtimeOnly "io.grpc:grpc-netty-shaded:${grpcVersion}"

testImplementation 'junit:junit:4.12'
testImplementation 'junit:junit:4.13.2'
testImplementation "io.grpc:grpc-testing:${grpcVersion}"
}

Expand Down
2 changes: 1 addition & 1 deletion examples/example-hostname/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
2 changes: 1 addition & 1 deletion examples/example-jwt-auth/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ dependencies {
runtimeOnly "io.grpc:grpc-netty-shaded:${grpcVersion}"

testImplementation "io.grpc:grpc-testing:${grpcVersion}"
testImplementation "junit:junit:4.12"
testImplementation "junit:junit:4.13.2"
testImplementation "org.mockito:mockito-core:3.4.0"
}

Expand Down
2 changes: 1 addition & 1 deletion examples/example-jwt-auth/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
2 changes: 1 addition & 1 deletion examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ hdrhistogram = "org.hdrhistogram:HdrHistogram:2.1.12"
javax-annotation = "org.apache.tomcat:annotations-api:6.0.53"
jetty-alpn-agent = "org.mortbay.jetty.alpn:jetty-alpn-agent:2.0.10"
jsr305 = "com.google.code.findbugs:jsr305:3.0.2"
junit = "junit:junit:4.12"
junit = "junit:junit:4.13.2"
mockito-android = "org.mockito:mockito-android:3.8.0"
mockito-core = "org.mockito:mockito-core:3.3.3"
netty-codec-http2 = { module = "io.netty:netty-codec-http2", version.ref = "netty" }
Expand Down
52 changes: 24 additions & 28 deletions netty/src/test/java/io/grpc/netty/AdvancedTlsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

package io.grpc.netty;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;

import com.google.common.util.concurrent.MoreExecutors;
Expand Down Expand Up @@ -56,9 +58,7 @@
import javax.net.ssl.SSLEngine;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

Expand Down Expand Up @@ -89,9 +89,6 @@ public class AdvancedTlsTest {
private PrivateKey serverKeyBad;
private X509Certificate[] serverCertBad;

@Rule
public ExpectedException exceptionRule = ExpectedException.none();

@Before
public void setUp()
throws NoSuchAlgorithmException, IOException, CertificateException, InvalidKeySpecException {
Expand Down Expand Up @@ -427,24 +424,22 @@ public void onFileLoadingKeyManagerTrustManagerTest() throws Exception {

@Test
public void onFileReloadingKeyManagerBadInitialContentTest() throws Exception {
exceptionRule.expect(GeneralSecurityException.class);
AdvancedTlsX509KeyManager keyManager = new AdvancedTlsX509KeyManager();
// We swap the order of key and certificates to intentionally create an exception.
Closeable keyShutdown = keyManager.updateIdentityCredentialsFromFile(serverCert0File,
serverKey0File, 100, TimeUnit.MILLISECONDS, executor);
keyShutdown.close();
assertThrows(GeneralSecurityException.class,
() -> keyManager.updateIdentityCredentialsFromFile(serverCert0File,
serverKey0File, 100, TimeUnit.MILLISECONDS, executor));
}

@Test
public void onFileReloadingTrustManagerBadInitialContentTest() throws Exception {
exceptionRule.expect(GeneralSecurityException.class);
AdvancedTlsX509TrustManager trustManager = AdvancedTlsX509TrustManager.newBuilder()
.setVerification(Verification.CERTIFICATE_ONLY_VERIFICATION)
.build();
// We pass in a key as the trust certificates to intentionally create an exception.
Closeable trustShutdown = trustManager.updateTrustCredentialsFromFile(serverKey0File,
100, TimeUnit.MILLISECONDS, executor);
trustShutdown.close();
assertThrows(GeneralSecurityException.class,
() -> trustManager.updateTrustCredentialsFromFile(serverKey0File,
100, TimeUnit.MILLISECONDS, executor));
}

@Test
Expand Down Expand Up @@ -472,40 +467,38 @@ public void trustManagerCheckTrustedWithSocketTest() throws Exception {

@Test
public void trustManagerCheckClientTrustedWithoutParameterTest() throws Exception {
exceptionRule.expect(CertificateException.class);
exceptionRule.expectMessage(
"Not enough information to validate peer. SSLEngine or Socket required.");
AdvancedTlsX509TrustManager tm = AdvancedTlsX509TrustManager.newBuilder()
.setVerification(Verification.INSECURELY_SKIP_ALL_VERIFICATION).build();
tm.checkClientTrusted(serverCert0, "RSA");
CertificateException ex =
assertThrows(CertificateException.class, () -> tm.checkClientTrusted(serverCert0, "RSA"));
assertThat(ex).hasMessageThat()
.isEqualTo("Not enough information to validate peer. SSLEngine or Socket required.");
}

@Test
public void trustManagerCheckServerTrustedWithoutParameterTest() throws Exception {
exceptionRule.expect(CertificateException.class);
exceptionRule.expectMessage(
"Not enough information to validate peer. SSLEngine or Socket required.");
AdvancedTlsX509TrustManager tm = AdvancedTlsX509TrustManager.newBuilder()
.setVerification(Verification.INSECURELY_SKIP_ALL_VERIFICATION).build();
tm.checkServerTrusted(serverCert0, "RSA");
CertificateException ex =
assertThrows(CertificateException.class, () -> tm.checkServerTrusted(serverCert0, "RSA"));
assertThat(ex).hasMessageThat()
.isEqualTo("Not enough information to validate peer. SSLEngine or Socket required.");
}

@Test
public void trustManagerEmptyChainTest() throws Exception {
exceptionRule.expect(IllegalArgumentException.class);
exceptionRule.expectMessage(
"Want certificate verification but got null or empty certificates");
AdvancedTlsX509TrustManager tm = AdvancedTlsX509TrustManager.newBuilder()
.setVerification(Verification.CERTIFICATE_ONLY_VERIFICATION)
.build();
tm.updateTrustCredentials(caCert);
tm.checkClientTrusted(null, "RSA", (SSLEngine) null);
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
() -> tm.checkClientTrusted(null, "RSA", (SSLEngine) null));
assertThat(ex).hasMessageThat()
.isEqualTo("Want certificate verification but got null or empty certificates");
}

@Test
public void trustManagerBadCustomVerificationTest() throws Exception {
exceptionRule.expect(CertificateException.class);
exceptionRule.expectMessage("Bad Custom Verification");
AdvancedTlsX509TrustManager tm = AdvancedTlsX509TrustManager.newBuilder()
.setVerification(Verification.CERTIFICATE_ONLY_VERIFICATION)
.setSslSocketAndEnginePeerVerifier(
Expand All @@ -523,7 +516,10 @@ public void verifyPeerCertificate(X509Certificate[] peerCertChain, String authTy
}
}).build();
tm.updateTrustCredentials(caCert);
tm.checkClientTrusted(serverCert0, "RSA", new Socket());
CertificateException ex = assertThrows(
CertificateException.class,
() -> tm.checkClientTrusted(serverCert0, "RSA", new Socket()));
assertThat(ex).hasMessageThat().isEqualTo("Bad Custom Verification");
}

private static class SimpleServiceImpl extends SimpleServiceGrpc.SimpleServiceImplBase {
Expand Down

0 comments on commit 44847bf

Please sign in to comment.