Skip to content

Commit 68a6c72

Browse files
Sangamesh1997AgraVator
authored andcommitted
util: Fix misleading exception in AdvancedTlsX509TrustManager when cert file is missing (grpc#12353)
1 parent e6eecea commit 68a6c72

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-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
@@ -339,6 +339,10 @@ public void run() {
339339
private long readAndUpdate(File trustCertFile, long oldTime)
340340
throws IOException, GeneralSecurityException {
341341
long newTime = checkNotNull(trustCertFile, "trustCertFile").lastModified();
342+
if (newTime == 0) {
343+
throw new IOException(
344+
"Certificate file not found or not readable: " + trustCertFile.getAbsolutePath());
345+
}
342346
if (newTime == oldTime) {
343347
return oldTime;
344348
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,17 @@ record -> record.getMessage().contains("Default value of "));
142142
}
143143
}
144144

145+
@Test
146+
public void missingFile_throwsFileNotFoundException() throws Exception {
147+
AdvancedTlsX509TrustManager trustManager = AdvancedTlsX509TrustManager.newBuilder().build();
148+
File nonExistentFile = new File("missing_cert.pem");
149+
Exception thrown =
150+
assertThrows(Exception.class, () -> trustManager.updateTrustCredentials(nonExistentFile));
151+
assertNotNull(thrown);
152+
assertEquals(thrown.getMessage(),
153+
"Certificate file not found or not readable: " + nonExistentFile.getAbsolutePath());
154+
}
155+
145156
@Test
146157
public void clientTrustedWithSocketTest() throws Exception {
147158
AdvancedTlsX509TrustManager trustManager = AdvancedTlsX509TrustManager.newBuilder()

0 commit comments

Comments
 (0)