-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Fix edge case response parsing for pwsh credential #46572
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
dcd5dcb
fix pwsh credential response parsing
g2vinay e49239d
code refactor
g2vinay 207cf79
add unreleased tag
g2vinay 516dcc5
test old command
g2vinay 86a81b6
test variant new command
g2vinay 238c77d
update setup
g2vinay 7954741
fix tenant ID flow
g2vinay 94c5a57
code refactor
g2vinay c4be20c
cleanup
g2vinay c768e67
Merge remote-tracking branch 'upstream/main' into fix-pwsh-credential
g2vinay 0e3b22c
add pwsh util tests
g2vinay 18f7d8b
code refactor
g2vinay 19a8d4e
fix linting and tests
g2vinay b89243b
update changelog
g2vinay 60b7a8b
Merge remote-tracking branch 'upstream/main' into fix-pwsh-credential
g2vinay File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
186 changes: 186 additions & 0 deletions
186
...re-identity/src/test/java/com/azure/identity/implementation/util/PowerShellUtilTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.azure.identity.implementation.util; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.MethodSource; | ||
| import org.junit.jupiter.params.provider.ValueSource; | ||
|
|
||
| import java.time.Instant; | ||
| import java.time.OffsetDateTime; | ||
| import java.time.ZoneOffset; | ||
| import java.util.stream.Stream; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| public class PowerShellUtilTests { | ||
|
|
||
| // Test data for valid ISO timestamps | ||
| static Stream<String> validISOTimestamps() { | ||
| return Stream.of("2023-05-15T10:30:00Z", "2023-05-15T10:30:00.123Z", "2023-05-15T10:30:00+00:00", | ||
| "2023-05-15T10:30:00-05:00", "2023-05-15T10:30:00.123456+02:00", "2023-12-31T23:59:59Z", | ||
| "2000-01-01T00:00:00Z"); | ||
| } | ||
|
|
||
| // Test data for valid .NET Date format | ||
| static Stream<String> validDotNetDates() { | ||
| return Stream.of("/Date(1684145400000)/", // 2023-05-15 10:30:00 UTC | ||
| "/Date(0)/", // Unix epoch | ||
| "/Date(1640995200000)/", // 2022-01-01 00:00:00 UTC | ||
| "/Date(253402300799000)/" // Very far future date | ||
| ); | ||
| } | ||
|
|
||
| // Test data for invalid inputs | ||
| static Stream<String> invalidInputs() { | ||
| return Stream.of("", " ", "invalid-date", "/Date(/", "/Date())/", "/Date(abc)/", "/Date(123abc)/", | ||
| "/Date(123.456)/", "/Date(-123)/", "/Date(123", "Date(123)/", "/Date(123)//", "2023-13-01T10:30:00Z", // Invalid month | ||
| "2023-05-32T10:30:00Z", // Invalid day | ||
| "not-a-date-at-all"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testParseExpiresOnWithNull() { | ||
| assertNull(PowerShellUtil.parseExpiresOn(null)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testParseExpiresOnWithEmptyString() { | ||
| assertNull(PowerShellUtil.parseExpiresOn("")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testParseExpiresOnWithWhitespace() { | ||
| assertNull(PowerShellUtil.parseExpiresOn(" ")); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @MethodSource("validISOTimestamps") | ||
| public void testParseExpiresOnWithValidISOTimestamps(String isoTimestamp) { | ||
| OffsetDateTime result = PowerShellUtil.parseExpiresOn(isoTimestamp); | ||
|
|
||
| assertNotNull(result, "Should parse valid ISO timestamp: " + isoTimestamp); | ||
| assertEquals(ZoneOffset.UTC, result.getOffset(), "Result should be converted to UTC"); | ||
|
|
||
| // Verify it's a valid timestamp by converting back | ||
| OffsetDateTime original = OffsetDateTime.parse(isoTimestamp); | ||
| assertEquals(original.withOffsetSameInstant(ZoneOffset.UTC), result); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @MethodSource("validDotNetDates") | ||
| public void testParseExpiresOnWithValidDotNetDates(String dotNetDate) { | ||
| OffsetDateTime result = PowerShellUtil.parseExpiresOn(dotNetDate); | ||
|
|
||
| assertNotNull(result, "Should parse valid .NET date: " + dotNetDate); | ||
| assertEquals(ZoneOffset.UTC, result.getOffset(), "Result should be in UTC"); | ||
|
|
||
| // Extract the epoch milliseconds and verify | ||
| String digits = dotNetDate.substring(6, dotNetDate.length() - 2); | ||
| long expectedEpochMs = Long.parseLong(digits); | ||
| OffsetDateTime expected = OffsetDateTime.ofInstant(Instant.ofEpochMilli(expectedEpochMs), ZoneOffset.UTC); | ||
|
|
||
| assertEquals(expected, result); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @MethodSource("invalidInputs") | ||
| public void testParseExpiresOnWithInvalidInputs(String invalidInput) { | ||
| OffsetDateTime result = PowerShellUtil.parseExpiresOn(invalidInput); | ||
| assertNull(result, "Should return null for invalid input: " + invalidInput); | ||
| } | ||
|
|
||
| @Test | ||
| public void testParseExpiresOnWithSpecificDotNetDate() { | ||
| // Test a specific known date: 2023-05-15 10:10:00 UTC = 1684145400000 ms | ||
g2vinay marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| String dotNetDate = "/Date(1684145400000)/"; | ||
| OffsetDateTime result = PowerShellUtil.parseExpiresOn(dotNetDate); | ||
|
|
||
| assertNotNull(result); | ||
| assertEquals(2023, result.getYear()); | ||
| assertEquals(5, result.getMonthValue()); | ||
| assertEquals(15, result.getDayOfMonth()); | ||
| assertEquals(10, result.getHour()); | ||
| assertEquals(10, result.getMinute()); | ||
| assertEquals(0, result.getSecond()); | ||
| assertEquals(ZoneOffset.UTC, result.getOffset()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testParseExpiresOnWithSpecificISODate() { | ||
| // Test a specific ISO date with timezone conversion | ||
| String isoDate = "2023-05-15T10:30:00-05:00"; // Eastern time | ||
| OffsetDateTime result = PowerShellUtil.parseExpiresOn(isoDate); | ||
|
|
||
| assertNotNull(result); | ||
| assertEquals(ZoneOffset.UTC, result.getOffset()); | ||
|
|
||
| // Should be converted to 15:30 UTC (10:30 - 5 hours = 15:30 UTC) | ||
| assertEquals(15, result.getHour()); | ||
| assertEquals(30, result.getMinute()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testParseExpiresOnTriesISOFirst() { | ||
| // Test that ISO parsing is attempted first by using a string that could be ambiguous | ||
| String timestamp = "2023-05-15T10:30:00Z"; | ||
| OffsetDateTime result = PowerShellUtil.parseExpiresOn(timestamp); | ||
|
|
||
| assertNotNull(result); | ||
| // Verify it was parsed as ISO (not as .NET date format) | ||
| assertEquals(2023, result.getYear()); | ||
| assertEquals(5, result.getMonthValue()); | ||
| assertEquals(15, result.getDayOfMonth()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testParseExpiresOnWithDotNetDateContainingNonDigits() { | ||
| // Test .NET date format with non-digit characters in the number part | ||
| String invalidDotNetDate = "/Date(123abc456)/"; | ||
| OffsetDateTime result = PowerShellUtil.parseExpiresOn(invalidDotNetDate); | ||
|
|
||
| assertNull(result, "Should return null when .NET date contains non-digits"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testParseExpiresOnWithDotNetDateNumberFormatException() { | ||
| // Test a .NET date that would cause NumberFormatException (too large number) | ||
| String largeDotNetDate = "/Date(999999999999999999999)/"; | ||
| OffsetDateTime result = PowerShellUtil.parseExpiresOn(largeDotNetDate); | ||
|
|
||
| assertNull(result, "Should return null when .NET date number is too large"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testParseExpiresOnWithEpochZero() { | ||
| // Test Unix epoch (January 1, 1970, 00:00:00 UTC) | ||
| String epochDate = "/Date(0)/"; | ||
| OffsetDateTime result = PowerShellUtil.parseExpiresOn(epochDate); | ||
|
|
||
| assertNotNull(result); | ||
| assertEquals(1970, result.getYear()); | ||
| assertEquals(1, result.getMonthValue()); | ||
| assertEquals(1, result.getDayOfMonth()); | ||
| assertEquals(0, result.getHour()); | ||
| assertEquals(0, result.getMinute()); | ||
| assertEquals(0, result.getSecond()); | ||
| assertEquals(ZoneOffset.UTC, result.getOffset()); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @ValueSource( | ||
| strings = { | ||
| "/Date(/", | ||
| "/Date()/", | ||
| "Date(123)/", | ||
| "/Date(123", | ||
| "/Date(123)//", | ||
| "/Date(123)/extra", | ||
| "prefix/Date(123)/" }) | ||
| public void testParseExpiresOnWithMalformedDotNetDates(String malformedDate) { | ||
| OffsetDateTime result = PowerShellUtil.parseExpiresOn(malformedDate); | ||
| assertNull(result, "Should return null for malformed .NET date: " + malformedDate); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.