|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package com.azure.identity.implementation.util; |
| 5 | + |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | +import org.junit.jupiter.params.ParameterizedTest; |
| 8 | +import org.junit.jupiter.params.provider.MethodSource; |
| 9 | +import org.junit.jupiter.params.provider.ValueSource; |
| 10 | + |
| 11 | +import java.time.Instant; |
| 12 | +import java.time.OffsetDateTime; |
| 13 | +import java.time.ZoneOffset; |
| 14 | +import java.util.stream.Stream; |
| 15 | + |
| 16 | +import static org.junit.jupiter.api.Assertions.*; |
| 17 | + |
| 18 | +public class PowerShellUtilTests { |
| 19 | + |
| 20 | + // Test data for valid ISO timestamps |
| 21 | + static Stream<String> validISOTimestamps() { |
| 22 | + return Stream.of("2023-05-15T10:30:00Z", "2023-05-15T10:30:00.123Z", "2023-05-15T10:30:00+00:00", |
| 23 | + "2023-05-15T10:30:00-05:00", "2023-05-15T10:30:00.123456+02:00", "2023-12-31T23:59:59Z", |
| 24 | + "2000-01-01T00:00:00Z"); |
| 25 | + } |
| 26 | + |
| 27 | + // Test data for valid .NET Date format |
| 28 | + static Stream<String> validDotNetDates() { |
| 29 | + return Stream.of("/Date(1684145400000)/", // 2023-05-15 10:30:00 UTC |
| 30 | + "/Date(0)/", // Unix epoch |
| 31 | + "/Date(1640995200000)/", // 2022-01-01 00:00:00 UTC |
| 32 | + "/Date(253402300799000)/" // Very far future date |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + // Test data for invalid inputs |
| 37 | + static Stream<String> invalidInputs() { |
| 38 | + return Stream.of("", " ", "invalid-date", "/Date(/", "/Date())/", "/Date(abc)/", "/Date(123abc)/", |
| 39 | + "/Date(123.456)/", "/Date(-123)/", "/Date(123", "Date(123)/", "/Date(123)//", "2023-13-01T10:30:00Z", // Invalid month |
| 40 | + "2023-05-32T10:30:00Z", // Invalid day |
| 41 | + "not-a-date-at-all"); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void testParseExpiresOnWithNull() { |
| 46 | + assertNull(PowerShellUtil.parseExpiresOn(null)); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void testParseExpiresOnWithEmptyString() { |
| 51 | + assertNull(PowerShellUtil.parseExpiresOn("")); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testParseExpiresOnWithWhitespace() { |
| 56 | + assertNull(PowerShellUtil.parseExpiresOn(" ")); |
| 57 | + } |
| 58 | + |
| 59 | + @ParameterizedTest |
| 60 | + @MethodSource("validISOTimestamps") |
| 61 | + public void testParseExpiresOnWithValidISOTimestamps(String isoTimestamp) { |
| 62 | + OffsetDateTime result = PowerShellUtil.parseExpiresOn(isoTimestamp); |
| 63 | + |
| 64 | + assertNotNull(result, "Should parse valid ISO timestamp: " + isoTimestamp); |
| 65 | + assertEquals(ZoneOffset.UTC, result.getOffset(), "Result should be converted to UTC"); |
| 66 | + |
| 67 | + // Verify it's a valid timestamp by converting back |
| 68 | + OffsetDateTime original = OffsetDateTime.parse(isoTimestamp); |
| 69 | + assertEquals(original.withOffsetSameInstant(ZoneOffset.UTC), result); |
| 70 | + } |
| 71 | + |
| 72 | + @ParameterizedTest |
| 73 | + @MethodSource("validDotNetDates") |
| 74 | + public void testParseExpiresOnWithValidDotNetDates(String dotNetDate) { |
| 75 | + OffsetDateTime result = PowerShellUtil.parseExpiresOn(dotNetDate); |
| 76 | + |
| 77 | + assertNotNull(result, "Should parse valid .NET date: " + dotNetDate); |
| 78 | + assertEquals(ZoneOffset.UTC, result.getOffset(), "Result should be in UTC"); |
| 79 | + |
| 80 | + // Extract the epoch milliseconds and verify |
| 81 | + String digits = dotNetDate.substring(6, dotNetDate.length() - 2); |
| 82 | + long expectedEpochMs = Long.parseLong(digits); |
| 83 | + OffsetDateTime expected = OffsetDateTime.ofInstant(Instant.ofEpochMilli(expectedEpochMs), ZoneOffset.UTC); |
| 84 | + |
| 85 | + assertEquals(expected, result); |
| 86 | + } |
| 87 | + |
| 88 | + @ParameterizedTest |
| 89 | + @MethodSource("invalidInputs") |
| 90 | + public void testParseExpiresOnWithInvalidInputs(String invalidInput) { |
| 91 | + OffsetDateTime result = PowerShellUtil.parseExpiresOn(invalidInput); |
| 92 | + assertNull(result, "Should return null for invalid input: " + invalidInput); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testParseExpiresOnWithSpecificDotNetDate() { |
| 97 | + // Test a specific known date: 2023-05-15 10:10:00 UTC = 1684145400000 ms |
| 98 | + String dotNetDate = "/Date(1684145400000)/"; |
| 99 | + OffsetDateTime result = PowerShellUtil.parseExpiresOn(dotNetDate); |
| 100 | + |
| 101 | + assertNotNull(result); |
| 102 | + assertEquals(2023, result.getYear()); |
| 103 | + assertEquals(5, result.getMonthValue()); |
| 104 | + assertEquals(15, result.getDayOfMonth()); |
| 105 | + assertEquals(10, result.getHour()); |
| 106 | + assertEquals(10, result.getMinute()); |
| 107 | + assertEquals(0, result.getSecond()); |
| 108 | + assertEquals(ZoneOffset.UTC, result.getOffset()); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void testParseExpiresOnWithSpecificISODate() { |
| 113 | + // Test a specific ISO date with timezone conversion |
| 114 | + String isoDate = "2023-05-15T10:30:00-05:00"; // Eastern time |
| 115 | + OffsetDateTime result = PowerShellUtil.parseExpiresOn(isoDate); |
| 116 | + |
| 117 | + assertNotNull(result); |
| 118 | + assertEquals(ZoneOffset.UTC, result.getOffset()); |
| 119 | + |
| 120 | + // Should be converted to 15:30 UTC (10:30 - 5 hours = 15:30 UTC) |
| 121 | + assertEquals(15, result.getHour()); |
| 122 | + assertEquals(30, result.getMinute()); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void testParseExpiresOnTriesISOFirst() { |
| 127 | + // Test that ISO parsing is attempted first by using a string that could be ambiguous |
| 128 | + String timestamp = "2023-05-15T10:30:00Z"; |
| 129 | + OffsetDateTime result = PowerShellUtil.parseExpiresOn(timestamp); |
| 130 | + |
| 131 | + assertNotNull(result); |
| 132 | + // Verify it was parsed as ISO (not as .NET date format) |
| 133 | + assertEquals(2023, result.getYear()); |
| 134 | + assertEquals(5, result.getMonthValue()); |
| 135 | + assertEquals(15, result.getDayOfMonth()); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + public void testParseExpiresOnWithDotNetDateContainingNonDigits() { |
| 140 | + // Test .NET date format with non-digit characters in the number part |
| 141 | + String invalidDotNetDate = "/Date(123abc456)/"; |
| 142 | + OffsetDateTime result = PowerShellUtil.parseExpiresOn(invalidDotNetDate); |
| 143 | + |
| 144 | + assertNull(result, "Should return null when .NET date contains non-digits"); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + public void testParseExpiresOnWithDotNetDateNumberFormatException() { |
| 149 | + // Test a .NET date that would cause NumberFormatException (too large number) |
| 150 | + String largeDotNetDate = "/Date(999999999999999999999)/"; |
| 151 | + OffsetDateTime result = PowerShellUtil.parseExpiresOn(largeDotNetDate); |
| 152 | + |
| 153 | + assertNull(result, "Should return null when .NET date number is too large"); |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + public void testParseExpiresOnWithEpochZero() { |
| 158 | + // Test Unix epoch (January 1, 1970, 00:00:00 UTC) |
| 159 | + String epochDate = "/Date(0)/"; |
| 160 | + OffsetDateTime result = PowerShellUtil.parseExpiresOn(epochDate); |
| 161 | + |
| 162 | + assertNotNull(result); |
| 163 | + assertEquals(1970, result.getYear()); |
| 164 | + assertEquals(1, result.getMonthValue()); |
| 165 | + assertEquals(1, result.getDayOfMonth()); |
| 166 | + assertEquals(0, result.getHour()); |
| 167 | + assertEquals(0, result.getMinute()); |
| 168 | + assertEquals(0, result.getSecond()); |
| 169 | + assertEquals(ZoneOffset.UTC, result.getOffset()); |
| 170 | + } |
| 171 | + |
| 172 | + @ParameterizedTest |
| 173 | + @ValueSource( |
| 174 | + strings = { |
| 175 | + "/Date(/", |
| 176 | + "/Date()/", |
| 177 | + "Date(123)/", |
| 178 | + "/Date(123", |
| 179 | + "/Date(123)//", |
| 180 | + "/Date(123)/extra", |
| 181 | + "prefix/Date(123)/" }) |
| 182 | + public void testParseExpiresOnWithMalformedDotNetDates(String malformedDate) { |
| 183 | + OffsetDateTime result = PowerShellUtil.parseExpiresOn(malformedDate); |
| 184 | + assertNull(result, "Should return null for malformed .NET date: " + malformedDate); |
| 185 | + } |
| 186 | +} |
0 commit comments