From 328c2ef36287d366e4ebfe4bca666ca66568a278 Mon Sep 17 00:00:00 2001 From: Wes Tarle Date: Mon, 15 Jun 2026 12:26:29 +0000 Subject: [PATCH] test(auth): verify GoogleCredentials.fromStream throws IOException on invalid JSON Other client libraries (such as Python, Go, and Rust) strictly validate JSON syntax and reject malformed payload structures immediately. This test ensures Java maintains parity by asserting that an IOException is explicitly thrown when ADC JSON parsing fails, preventing silent fallbacks. This fills an untested gap in the Java ADC resolution suite. --- .../google/auth/oauth2/GoogleCredentialsTest.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/google-auth-library-java/oauth2_http/javatests/com/google/auth/oauth2/GoogleCredentialsTest.java b/google-auth-library-java/oauth2_http/javatests/com/google/auth/oauth2/GoogleCredentialsTest.java index 74aa9fae9ccd..343eca7fbcd2 100644 --- a/google-auth-library-java/oauth2_http/javatests/com/google/auth/oauth2/GoogleCredentialsTest.java +++ b/google-auth-library-java/oauth2_http/javatests/com/google/auth/oauth2/GoogleCredentialsTest.java @@ -53,6 +53,7 @@ import java.io.IOException; import java.io.InputStream; import java.net.URI; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Collection; import java.util.Collections; @@ -122,9 +123,20 @@ void fromStream_unknownType_throws() throws IOException { } } + @Test + void fromStream_invalidJson_throws() throws IOException { + // This test ensures Java successfully throws an IOException when ADC JSON parsing + // fails, preventing silent fallbacks. + MockHttpTransportFactory transportFactory = new MockHttpTransportFactory(); + try (InputStream stream = + new ByteArrayInputStream("invalid-json{".getBytes(StandardCharsets.UTF_8))) { + assertThrows(IOException.class, () -> GoogleCredentials.fromStream(stream, transportFactory)); + } + } + @Test void fromStream_nullTransport_throws() { - InputStream stream = new ByteArrayInputStream("foo".getBytes()); + InputStream stream = new ByteArrayInputStream("foo".getBytes(StandardCharsets.UTF_8)); assertThrows(NullPointerException.class, () -> GoogleCredentials.fromStream(stream, null)); }