Skip to content

test(auth): Assert IOException on invalid or malformed private keys#13486

Draft
westarle wants to merge 1 commit into
googleapis:mainfrom
westarle:fix-test-gap-key-parsing-errors
Draft

test(auth): Assert IOException on invalid or malformed private keys#13486
westarle wants to merge 1 commit into
googleapis:mainfrom
westarle:fix-test-gap-key-parsing-errors

Conversation

@westarle

Copy link
Copy Markdown
Contributor

This commit adds edge-case parsing tests in OAuth2UtilsTest.java to verify that providing an invalid private key or a structurally malformed PEM string safely triggers an IOException (e.g., 'Invalid PKCS#8 data.') rather than causing unexpected crashes.

This brings the Java library's test suite into alignment with the expected auth specification. Other Google Cloud client libraries like Go, Node.js, and Python natively assert that invalid cryptographic key formats are gracefully caught and surfaced as validation errors.

This commit adds edge-case parsing tests in OAuth2UtilsTest.java to verify that providing an invalid private key or a structurally malformed PEM string safely triggers an IOException (e.g., 'Invalid PKCS#8 data.') rather than causing unexpected crashes.

This brings the Java library's test suite into alignment with the expected auth specification. Other Google Cloud client libraries like Go, Node.js, and Python natively assert that invalid cryptographic key formats are gracefully caught and surfaced as validation errors.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds unit tests to OAuth2UtilsTest to verify that OAuth2Utils.privateKeyFromPkcs8 correctly throws an exception when handled with invalid or malformed keys. The review feedback suggests a minor improvement to import java.io.IOException and use IOException.class directly in the assertions rather than using the fully qualified class name, which will make the test code cleaner and more idiomatic.

String invalidKey = "-----BEGIN PRIVATE KEY-----\n" +
"INVALID_KEY_DATA\n" +
"-----END PRIVATE KEY-----\n";
assertThrows(java.io.IOException.class, () -> OAuth2Utils.privateKeyFromPkcs8(invalidKey));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Avoid using the fully qualified class name java.io.IOException.class. It is cleaner and more idiomatic to import java.io.IOException and use IOException.class directly.

Suggested change
assertThrows(java.io.IOException.class, () -> OAuth2Utils.privateKeyFromPkcs8(invalidKey));
assertThrows(IOException.class, () -> OAuth2Utils.privateKeyFromPkcs8(invalidKey));

@Test
void testPrivateKeyFromPkcs8_malformedPem() {
String malformedKey = "just some random string";
assertThrows(java.io.IOException.class, () -> OAuth2Utils.privateKeyFromPkcs8(malformedKey));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Avoid using the fully qualified class name java.io.IOException.class. It is cleaner and more idiomatic to import java.io.IOException and use IOException.class directly.

Suggested change
assertThrows(java.io.IOException.class, () -> OAuth2Utils.privateKeyFromPkcs8(malformedKey));
assertThrows(IOException.class, () -> OAuth2Utils.privateKeyFromPkcs8(malformedKey));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant