From c048c3f1aac1c32c04e09a98043c20b92c602caa Mon Sep 17 00:00:00 2001 From: Arun Gopalpuri Date: Wed, 11 Dec 2024 12:09:29 -0800 Subject: [PATCH] Ensure Files.createTempDirectory honors java.io.tmpdir system property Signed-off-by: Arun Gopalpuri --- distribution/tools/launchers/build.gradle | 3 + .../opensearch/tools/launchers/Launchers.java | 11 +- .../tools/launchers/TempDirectory.java | 2 +- .../tools/launchers/LaunchersTests.java | 66 +++++++++++ .../tools/launchers/TempDirectoryTests.java | 107 ++++++++++++++++++ 5 files changed, 182 insertions(+), 7 deletions(-) create mode 100644 distribution/tools/launchers/src/test/java/org/opensearch/tools/launchers/LaunchersTests.java create mode 100644 distribution/tools/launchers/src/test/java/org/opensearch/tools/launchers/TempDirectoryTests.java diff --git a/distribution/tools/launchers/build.gradle b/distribution/tools/launchers/build.gradle index aee205a24dea3..c9a2699fc2137 100644 --- a/distribution/tools/launchers/build.gradle +++ b/distribution/tools/launchers/build.gradle @@ -35,7 +35,10 @@ dependencies { compileOnly project(':distribution:tools:java-version-checker') testImplementation "com.carrotsearch.randomizedtesting:randomizedtesting-runner:${versions.randomizedrunner}" testImplementation "junit:junit:${versions.junit}" + testImplementation "org.mockito:mockito-core:${versions.mockito}" testImplementation "org.hamcrest:hamcrest:${versions.hamcrest}" + testImplementation "net.bytebuddy:byte-buddy:1.15.4" + testImplementation "net.bytebuddy:byte-buddy-agent:1.15.4" } base { diff --git a/distribution/tools/launchers/src/main/java/org/opensearch/tools/launchers/Launchers.java b/distribution/tools/launchers/src/main/java/org/opensearch/tools/launchers/Launchers.java index 815615cd936b9..6aebe7bbadee8 100644 --- a/distribution/tools/launchers/src/main/java/org/opensearch/tools/launchers/Launchers.java +++ b/distribution/tools/launchers/src/main/java/org/opensearch/tools/launchers/Launchers.java @@ -50,7 +50,7 @@ final class Launchers { * @param message the message to print */ @SuppressForbidden(reason = "System#out") - static void outPrintln(final String message) { + public static void outPrintln(final String message) { System.out.println(message); } @@ -60,7 +60,7 @@ static void outPrintln(final String message) { * @param message the message to print */ @SuppressForbidden(reason = "System#err") - static void errPrintln(final String message) { + public static void errPrintln(final String message) { System.err.println(message); } @@ -70,13 +70,12 @@ static void errPrintln(final String message) { * @param status the status */ @SuppressForbidden(reason = "System#exit") - static void exit(final int status) { + public static void exit(final int status) { System.exit(status); } @SuppressForbidden(reason = "Files#createTempDirectory(String, FileAttribute...)") - static Path createTempDirectory(final String prefix, final FileAttribute... attrs) throws IOException { - return Files.createTempDirectory(prefix, attrs); + static Path createTempDirectory(final Path rootDir, final String prefix, final FileAttribute... attrs) throws IOException { + return Files.createTempDirectory(rootDir, prefix, attrs); } - } diff --git a/distribution/tools/launchers/src/main/java/org/opensearch/tools/launchers/TempDirectory.java b/distribution/tools/launchers/src/main/java/org/opensearch/tools/launchers/TempDirectory.java index b282e27bc0340..1728ada007fc0 100644 --- a/distribution/tools/launchers/src/main/java/org/opensearch/tools/launchers/TempDirectory.java +++ b/distribution/tools/launchers/src/main/java/org/opensearch/tools/launchers/TempDirectory.java @@ -64,7 +64,7 @@ public static void main(final String[] args) throws IOException { path = Paths.get(System.getProperty("java.io.tmpdir"), "opensearch"); Files.createDirectories(path); } else { - path = Launchers.createTempDirectory("opensearch-"); + path = Launchers.createTempDirectory(Paths.get(System.getProperty("java.io.tmpdir")), "opensearch-"); } Launchers.outPrintln(path.toString()); } diff --git a/distribution/tools/launchers/src/test/java/org/opensearch/tools/launchers/LaunchersTests.java b/distribution/tools/launchers/src/test/java/org/opensearch/tools/launchers/LaunchersTests.java new file mode 100644 index 0000000000000..5013f61939978 --- /dev/null +++ b/distribution/tools/launchers/src/test/java/org/opensearch/tools/launchers/LaunchersTests.java @@ -0,0 +1,66 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* + * Modifications Copyright OpenSearch Contributors. See + * GitHub history for details. + */ +package org.opensearch.tools.launchers; + +import com.carrotsearch.randomizedtesting.RandomizedTest; + +import org.junit.After; +import org.junit.Test; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +public class LaunchersTests extends LaunchersTestCase { + + private Path tempDirectory; + + @After + public void tearDown() throws IOException { + if (tempDirectory != null && Files.exists(tempDirectory)) { + Files.deleteIfExists(tempDirectory); + } + } + + @Test + public void testCreateTempDirectory() throws IOException { + String tempDirPrefix = "opensearch-test-" + RandomizedTest.randomAsciiLettersOfLength(7); + tempDirectory = Launchers.createTempDirectory(Paths.get(System.getProperty("java.io.tmpdir")), tempDirPrefix); + + assertNotNull("Temporary directory should not be null", tempDirectory); + assertTrue("Temporary directory should exist", Files.exists(tempDirectory)); + } +} diff --git a/distribution/tools/launchers/src/test/java/org/opensearch/tools/launchers/TempDirectoryTests.java b/distribution/tools/launchers/src/test/java/org/opensearch/tools/launchers/TempDirectoryTests.java new file mode 100644 index 0000000000000..045e125034be1 --- /dev/null +++ b/distribution/tools/launchers/src/test/java/org/opensearch/tools/launchers/TempDirectoryTests.java @@ -0,0 +1,107 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* + * Modifications Copyright OpenSearch Contributors. See + * GitHub history for details. + */ + +package org.opensearch.tools.launchers; + +import com.carrotsearch.randomizedtesting.RandomizedTest; + +import org.opensearch.tools.java_version_checker.SuppressForbidden; +import org.junit.After; +import org.junit.Test; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +import org.mockito.MockedStatic; + +import static org.junit.Assert.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.eq; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.times; + +@SuppressForbidden(reason = "modifies system properties to test different O.S") +public class TempDirectoryTests extends LaunchersTestCase { + + private Path createdTempDir; + + @After + public void tearDown() throws IOException { + if (createdTempDir != null && Files.exists(createdTempDir)) { + Files.delete(createdTempDir); + } + } + + @Test + public void testMainWithArguments() { + String[] args = { RandomizedTest.randomAsciiLettersOfLengthBetween(1, 10) }; + assertThrows(IllegalArgumentException.class, () -> TempDirectory.main(args)); + } + + @Test + public void testMainOnWindows() throws IOException { + System.setProperty("os.name", "Windows " + RandomizedTest.randomIntBetween(7, 10)); + + try (MockedStatic mockedLaunchers = mockStatic(Launchers.class)) { + mockedLaunchers.when(() -> Launchers.outPrintln(any(String.class))).thenAnswer(invocation -> null); + + TempDirectory.main(new String[] {}); + + createdTempDir = Paths.get(System.getProperty("java.io.tmpdir"), "opensearch"); + mockedLaunchers.verify(() -> Launchers.outPrintln(createdTempDir.toString()), times(1)); + assert Files.exists(createdTempDir); + } + } + + @Test + public void testMainOnNonWindows() throws IOException { + String[] osNames = { "Linux", "Mac OS X", "Unix" }; + String osName = RandomizedTest.randomFrom(osNames); + System.setProperty("os.name", osName); + + try (MockedStatic mockedLaunchers = mockStatic(Launchers.class)) { + Path mockedTempDir = Paths.get( + System.getProperty("java.io.tmpdir"), + "opensearch-" + RandomizedTest.randomAsciiLettersOfLength(7) + ); + mockedLaunchers.when(() -> Launchers.createTempDirectory(any(Path.class), eq("opensearch-"))).thenReturn(mockedTempDir); + mockedLaunchers.when(() -> Launchers.outPrintln(any(String.class))).thenAnswer(invocation -> null); + + TempDirectory.main(new String[] {}); + + mockedLaunchers.verify(() -> Launchers.createTempDirectory(any(Path.class), eq("opensearch-")), times(1)); + mockedLaunchers.verify(() -> Launchers.outPrintln(mockedTempDir.toString()), times(1)); + } + } +}