|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package com.azure.identity.implementation; |
| 5 | + |
| 6 | +import com.azure.core.exception.ClientAuthenticationException; |
| 7 | +import com.azure.core.util.logging.ClientLogger; |
| 8 | +import com.azure.identity.implementation.util.ValidationUtil; |
| 9 | +import org.junit.jupiter.api.BeforeAll; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | +import org.junit.jupiter.api.condition.DisabledOnOs; |
| 12 | +import org.junit.jupiter.api.condition.OS; |
| 13 | + |
| 14 | +import java.io.File; |
| 15 | +import java.nio.file.Path; |
| 16 | +import java.nio.file.Paths; |
| 17 | + |
| 18 | +import static com.azure.identity.implementation.util.IdentityUtil.isLinuxPlatform; |
| 19 | +import static com.azure.identity.implementation.util.IdentityUtil.isWindowsPlatform; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 23 | + |
| 24 | +@DisabledOnOs({OS.MAC}) |
| 25 | +public class ValidationUtilTests { |
| 26 | + private static final ClientLogger LOGGER = new ClientLogger(ValidationUtilTests.class); |
| 27 | + |
| 28 | + private static File good; |
| 29 | + private static File fileTooLong; |
| 30 | + private static File wrongPrefix; |
| 31 | + private static File wrongExtension; |
| 32 | + private static File fileWithRelativeSegments; |
| 33 | + |
| 34 | + |
| 35 | + @BeforeAll |
| 36 | + public static void setupClass() { |
| 37 | + Path beginning = null; |
| 38 | + if (isWindowsPlatform()) { |
| 39 | + beginning = Paths.get(System.getenv("ProgramData"), "AzureConnectedMachineAgent", "Tokens"); |
| 40 | + } else if (isLinuxPlatform()) { |
| 41 | + |
| 42 | + beginning = Paths.get("/", "var", "opt", "azcmagent", "tokens"); |
| 43 | + } |
| 44 | + |
| 45 | + good = new TestFile(Paths.get(beginning.toString(), "good.key").toString()); |
| 46 | + fileTooLong = new TestFile(Paths.get(beginning.toString(), "fileTooLong.key").toString(), 4097); |
| 47 | + wrongPrefix = new TestFile(Paths.get("wrongPrefix", ".key").toString()); |
| 48 | + wrongExtension = new TestFile(Paths.get(beginning.toString(), "wrongExtension.txt").toString()); |
| 49 | + fileWithRelativeSegments = new TestFile(Paths.get(beginning.toString(), "..", "file.key").toString()); |
| 50 | + |
| 51 | + } |
| 52 | + @Test |
| 53 | + public void testValidPath() { |
| 54 | + assertDoesNotThrow(() -> ValidationUtil.validateSecretFile(good, LOGGER)); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testInvalidTooLong() { |
| 59 | + Throwable thrown = assertThrows(ClientAuthenticationException.class, () -> ValidationUtil.validateSecretFile(fileTooLong, LOGGER)); |
| 60 | + assertTrue(thrown.getMessage().contains("The secret key file is too large")); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testInvalidWrongPrefix() { |
| 65 | + Throwable thrown = assertThrows(ClientAuthenticationException.class, () -> ValidationUtil.validateSecretFile(wrongPrefix, LOGGER)); |
| 66 | + assertTrue(thrown.getMessage().contains("The secret key file is not located in the expected directory")); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testInvalidWrongExtension() { |
| 71 | + Throwable thrown = assertThrows(ClientAuthenticationException.class, () -> ValidationUtil.validateSecretFile(wrongExtension, LOGGER)); |
| 72 | + assertTrue(thrown.getMessage().contains("The secret key file does not have the expected file extension")); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void testInvalidRelativeSegments() { |
| 77 | + Throwable thrown = assertThrows(ClientAuthenticationException.class, () -> ValidationUtil.validateSecretFile(fileWithRelativeSegments, LOGGER)); |
| 78 | + assertTrue(thrown.getMessage().contains("The secret key file is not located in the expected directory")); |
| 79 | + } |
| 80 | + |
| 81 | + static class TestFile extends File { |
| 82 | + long length = 4096; |
| 83 | + TestFile(String pathname) { |
| 84 | + super(pathname); |
| 85 | + } |
| 86 | + |
| 87 | + TestFile(String pathName, long length) { |
| 88 | + super(pathName); |
| 89 | + this.length = length; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public long length() { |
| 94 | + return length; |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments