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..b97f3c05c0fa 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 @@ -122,6 +122,18 @@ void fromStream_unknownType_throws() throws IOException { } } + @Test + void fromStream_invalidJson_throws() throws IOException { + // Other client libraries (like Python, Go, and Rust) strictly validate JSON syntax and reject + // malformed payload structures immediately. This test ensures Java maintains parity by + // successfully throwing an IOException when ADC JSON parsing fails, preventing silent fallbacks. + MockHttpTransportFactory transportFactory = new MockHttpTransportFactory(); + try (InputStream stream = new ByteArrayInputStream("invalid-json{".getBytes(java.nio.charset.StandardCharsets.UTF_8))) { + assertThrows( + IOException.class, () -> GoogleCredentials.fromStream(stream, transportFactory)); + } + } + @Test void fromStream_nullTransport_throws() { InputStream stream = new ByteArrayInputStream("foo".getBytes());