|
4 | 4 | package com.microsoft.aad.msal4j; |
5 | 5 |
|
6 | 6 | import com.nimbusds.oauth2.sdk.util.URLUtils; |
| 7 | +import org.junit.jupiter.api.BeforeEach; |
7 | 8 | import org.junit.jupiter.api.Nested; |
8 | 9 | import org.junit.jupiter.api.Test; |
9 | 10 | import org.junit.jupiter.api.TestInstance; |
10 | 11 | import org.junit.jupiter.api.extension.ExtendWith; |
| 12 | +import org.junit.jupiter.api.io.TempDir; |
11 | 13 | import org.junit.jupiter.params.ParameterizedTest; |
12 | 14 | import org.junit.jupiter.params.provider.MethodSource; |
13 | 15 | import org.junit.jupiter.params.provider.ValueSource; |
14 | 16 | import org.mockito.junit.jupiter.MockitoExtension; |
15 | 17 |
|
| 18 | +import java.io.File; |
| 19 | +import java.io.IOException; |
16 | 20 | import java.net.SocketException; |
17 | 21 | import java.nio.file.Path; |
18 | 22 | import java.nio.file.Paths; |
|
32 | 36 | import static java.util.Collections.*; |
33 | 37 | import static org.apache.http.HttpStatus.*; |
34 | 38 | import static org.junit.jupiter.api.Assertions.*; |
| 39 | +import static org.junit.jupiter.api.Assumptions.assumeTrue; |
35 | 40 | import static org.mockito.ArgumentMatchers.any; |
36 | 41 | import static org.mockito.Mockito.*; |
37 | 42 |
|
@@ -961,4 +966,127 @@ private void assertMsalServiceException(String errorCode, String message) throws |
961 | 966 | assertTrue(ex.getMessage().contains(message)); |
962 | 967 | } |
963 | 968 | } |
| 969 | + |
| 970 | + |
| 971 | + @Nested |
| 972 | + class OSTests { |
| 973 | + |
| 974 | + @TempDir |
| 975 | + Path tempDir; |
| 976 | + |
| 977 | + private ManagedIdentityClient client = new ManagedIdentityClient(); |
| 978 | + private EnvironmentVariablesHelper envVars; |
| 979 | + |
| 980 | + @BeforeEach |
| 981 | + void setUp() { |
| 982 | + envVars = new EnvironmentVariablesHelper(AZURE_ARC, azureArcEndpoint); |
| 983 | + ManagedIdentityApplication.setEnvironmentVariables(envVars); |
| 984 | + } |
| 985 | + |
| 986 | + @Test |
| 987 | + void validateAzureArc_WithCorrectEnvironmentVariables() { |
| 988 | + // Set environment variables for Azure Arc |
| 989 | + envVars.setEnvironmentVariable(Constants.IDENTITY_ENDPOINT, "https://example.com"); |
| 990 | + envVars.setEnvironmentVariable(Constants.IMDS_ENDPOINT, "https://example2.com"); |
| 991 | + |
| 992 | + // Test validation |
| 993 | + boolean result = ManagedIdentityClient.validateAzureArcEnvironment(envVars); |
| 994 | + |
| 995 | + assertTrue(result, "Azure Arc should be validated with correct environment variables"); |
| 996 | + } |
| 997 | + |
| 998 | + @Test |
| 999 | + void validateAzureArc_WithMissingEnvironmentVariables() { |
| 1000 | + // Only set one environment variable |
| 1001 | + envVars.setEnvironmentVariable(Constants.IDENTITY_ENDPOINT, "https://example.com"); |
| 1002 | + envVars.setEnvironmentVariable(Constants.IMDS_ENDPOINT, null); |
| 1003 | + |
| 1004 | + boolean result = ManagedIdentityClient.validateAzureArcEnvironment(envVars); |
| 1005 | + |
| 1006 | + assertFalse(result, "Azure Arc validation should fail with missing environment variables"); |
| 1007 | + } |
| 1008 | + |
| 1009 | + @Test |
| 1010 | + void validateAzureArc_WindowsFileExists() throws IOException { |
| 1011 | + // Determine OS and skip if not Windows |
| 1012 | + String osName = System.getProperty("os.name").toLowerCase(); |
| 1013 | + assumeTrue(osName.contains("windows"), "Test only runs on Windows"); |
| 1014 | + |
| 1015 | + // Create temp file to simulate Azure Arc file on Windows |
| 1016 | + File windowsFile = tempDir.resolve("himds.key").toFile(); |
| 1017 | + assertTrue(windowsFile.createNewFile(), "Failed to create test file"); |
| 1018 | + |
| 1019 | + // Set custom file path for testing |
| 1020 | + client.setWindowsFilePath(windowsFile.getAbsolutePath()); |
| 1021 | + |
| 1022 | + boolean result = ManagedIdentityClient.validateAzureArcEnvironment(envVars); |
| 1023 | + |
| 1024 | + assertTrue(result, "Azure Arc should be validated when Windows file exists"); |
| 1025 | + } |
| 1026 | + |
| 1027 | + @Test |
| 1028 | + void validateAzureArc_LinuxFileExists() throws IOException { |
| 1029 | + // Determine OS and skip if not Linux |
| 1030 | + String osName = System.getProperty("os.name").toLowerCase(); |
| 1031 | + assumeTrue(osName.contains("linux"), "Test only runs on Linux"); |
| 1032 | + |
| 1033 | + // Create temp file to simulate Azure Arc file on Linux |
| 1034 | + File linuxFile = tempDir.resolve("himds.sock").toFile(); |
| 1035 | + assertTrue(linuxFile.createNewFile(), "Failed to create test file"); |
| 1036 | + |
| 1037 | + // Set custom file path for testing |
| 1038 | + client.setLinuxFilePath(linuxFile.getAbsolutePath()); |
| 1039 | + |
| 1040 | + boolean result = ManagedIdentityClient.validateAzureArcEnvironment(envVars); |
| 1041 | + |
| 1042 | + assertTrue(result, "Azure Arc should be validated when Linux file exists"); |
| 1043 | + } |
| 1044 | + |
| 1045 | + @Test |
| 1046 | + void validateAzureArc_FilesNotExist() { |
| 1047 | + envVars.setEnvironmentVariable(Constants.IMDS_ENDPOINT, null); |
| 1048 | + |
| 1049 | + // Set non-existent file paths |
| 1050 | + client.setWindowsFilePath(tempDir.resolve("nonexistent-himds.key").toString()); |
| 1051 | + client.setLinuxFilePath(tempDir.resolve("nonexistent-himds.sock").toString()); |
| 1052 | + |
| 1053 | + boolean result = ManagedIdentityClient.validateAzureArcEnvironment(envVars); |
| 1054 | + |
| 1055 | + assertFalse(result, "Azure Arc validation should fail when files don't exist"); |
| 1056 | + } |
| 1057 | + |
| 1058 | + @Test |
| 1059 | + void validateAzureArc_CrossPlatformTest() throws IOException { |
| 1060 | + // This test creates both Windows and Linux files to test the method |
| 1061 | + // independent of platform in unit tests |
| 1062 | + envVars.setEnvironmentVariable(Constants.IMDS_ENDPOINT, null); |
| 1063 | + // Create both temp files |
| 1064 | + File windowsFile = tempDir.resolve("himds.key").toFile(); |
| 1065 | + File linuxFile = tempDir.resolve("himds.sock").toFile(); |
| 1066 | + |
| 1067 | + // Set custom file paths for testing |
| 1068 | + client.setWindowsFilePath(windowsFile.getAbsolutePath()); |
| 1069 | + client.setLinuxFilePath(linuxFile.getAbsolutePath()); |
| 1070 | + |
| 1071 | + // Test with no files |
| 1072 | + boolean noFilesResult = ManagedIdentityClient.validateAzureArcEnvironment(envVars); |
| 1073 | + assertFalse(noFilesResult, "Validation should fail when no files exist"); |
| 1074 | + |
| 1075 | + // Create Windows file |
| 1076 | + assertTrue(windowsFile.createNewFile(), "Failed to create Windows test file"); |
| 1077 | + |
| 1078 | + // The result depends on OS - but at least one path should be checked |
| 1079 | + boolean windowsFileResult = ManagedIdentityClient.validateAzureArcEnvironment(envVars); |
| 1080 | + |
| 1081 | + // Create Linux file |
| 1082 | + assertTrue(linuxFile.createNewFile(), "Failed to create Linux test file"); |
| 1083 | + |
| 1084 | + // Now with both files existing, the result should depend on OS |
| 1085 | + boolean bothFilesResult = ManagedIdentityClient.validateAzureArcEnvironment(envVars); |
| 1086 | + |
| 1087 | + // At least one of the tests with files should pass |
| 1088 | + assertTrue(windowsFileResult || bothFilesResult, |
| 1089 | + "At least one validation should succeed when test files exist"); |
| 1090 | + } |
| 1091 | + } |
964 | 1092 | } |
0 commit comments