Skip to content

Commit cd44f7d

Browse files
committed
Util: AdvancedTlsX509TrustManager code changes for handling fine not found
1 parent b2a95ca commit cd44f7d

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

util/src/main/java/io/grpc/util/AdvancedTlsX509TrustManager.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import io.grpc.ExperimentalApi;
2323
import java.io.File;
2424
import java.io.FileInputStream;
25+
import java.io.FileNotFoundException;
2526
import java.io.IOException;
2627
import java.net.Socket;
2728
import java.security.GeneralSecurityException;
@@ -339,6 +340,9 @@ public void run() {
339340
private long readAndUpdate(File trustCertFile, long oldTime)
340341
throws IOException, GeneralSecurityException {
341342
long newTime = checkNotNull(trustCertFile, "trustCertFile").lastModified();
343+
if (newTime == 0 && !trustCertFile.exists()) {
344+
throw new FileNotFoundException("Certificate not found: " + trustCertFile.getAbsolutePath());
345+
}
342346
if (newTime == oldTime) {
343347
return oldTime;
344348
}

util/src/test/java/io/grpc/util/AdvancedTlsX509TrustManagerTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static org.junit.Assert.assertEquals;
2121
import static org.junit.Assert.assertNotNull;
2222
import static org.junit.Assert.assertThrows;
23+
import static org.junit.Assert.assertTrue;
2324
import static org.mockito.Mockito.mock;
2425
import static org.mockito.Mockito.when;
2526

@@ -30,7 +31,9 @@
3031
import io.grpc.testing.TlsTesting;
3132
import io.grpc.util.AdvancedTlsX509TrustManager.Verification;
3233
import java.io.File;
34+
import java.io.FileNotFoundException;
3335
import java.io.IOException;
36+
import java.lang.reflect.Method;
3437
import java.net.Socket;
3538
import java.security.GeneralSecurityException;
3639
import java.security.cert.CertificateException;
@@ -142,6 +145,21 @@ record -> record.getMessage().contains("Default value of "));
142145
}
143146
}
144147

148+
@Test
149+
public void missingFile_throwsFileNotFoundException() throws Exception {
150+
AdvancedTlsX509TrustManager trustManager = AdvancedTlsX509TrustManager.newBuilder().build();
151+
Method readAndUpdateMethod =
152+
AdvancedTlsX509TrustManager.class.getDeclaredMethod(
153+
"readAndUpdate", File.class, long.class);
154+
readAndUpdateMethod.setAccessible(true);
155+
File nonExistentFile = new File("missing_cert.pem");
156+
Exception thrown = assertThrows(
157+
Exception.class, () -> readAndUpdateMethod.invoke(trustManager, nonExistentFile, 0L));
158+
159+
assertTrue("Should throw FileNotFoundException, but got: " + thrown.getCause(),
160+
thrown.getCause() instanceof FileNotFoundException);
161+
}
162+
145163
@Test
146164
public void clientTrustedWithSocketTest() throws Exception {
147165
AdvancedTlsX509TrustManager trustManager = AdvancedTlsX509TrustManager.newBuilder()

0 commit comments

Comments
 (0)