From 6c7a94ab8f4e2057a448aade72cb0f7611b7d21a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Sawicz?= Date: Thu, 18 Jul 2024 15:42:43 +0200 Subject: [PATCH] tests: replace `tmpnam` use Fixes #2675 --- .../server_configuration_options.cpp | 13 ++---- tests/include/mir/test/file_utils.h | 29 ++++++++++++ tests/mir_test/CMakeLists.txt | 1 + tests/mir_test/file_utils.cpp | 44 +++++++++++++++++++ tests/miral/external_client.cpp | 4 +- tests/miral/static_display_config.cpp | 5 ++- .../test_desktop_file_manager.cpp | 5 ++- .../test_g_desktop_file_cache.cpp | 5 +-- 8 files changed, 89 insertions(+), 17 deletions(-) create mode 100644 tests/include/mir/test/file_utils.h create mode 100644 tests/mir_test/file_utils.cpp diff --git a/tests/acceptance-tests/server_configuration_options.cpp b/tests/acceptance-tests/server_configuration_options.cpp index dfa01a8f713..6b34d1f7bce 100644 --- a/tests/acceptance-tests/server_configuration_options.cpp +++ b/tests/acceptance-tests/server_configuration_options.cpp @@ -18,6 +18,8 @@ #include "mir/options/option.h" +#include "mir/test/file_utils.h" + #include #include @@ -29,15 +31,6 @@ using namespace testing; namespace { -std::string create_temp_dir() -{ - char temp_dir[] = "temp_dir_XXXXXX"; - if (mkdtemp(temp_dir) == nullptr) - throw std::system_error(errno, std::system_category(), "Failed to create temp dir"); - - return temp_dir; -} - struct ServerConfigurationOptions : mir_test_framework::HeadlessTest { MOCK_METHOD(void, command_line_handler, (std::vector const&)); @@ -102,7 +95,7 @@ struct ServerConfigurationOptions : mir_test_framework::HeadlessTest remove(temp_dir.c_str()); } - std::string const temp_dir = create_temp_dir(); + std::string const temp_dir = mir::test::create_temp_dir(); std::string const fake_xdg_config_home = temp_dir + "/fake_xdg_config_home"; std::string const fake_home = temp_dir + "/fake_home"; std::string const fake_home_config = temp_dir + "/fake_home/.config"; diff --git a/tests/include/mir/test/file_utils.h b/tests/include/mir/test/file_utils.h new file mode 100644 index 00000000000..6d500eaa548 --- /dev/null +++ b/tests/include/mir/test/file_utils.h @@ -0,0 +1,29 @@ +/* + * Copyright © Canonical Ltd. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 or 3 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef MIR_TEST_FILE_UTILS_H_ +#define MIR_TEST_FILE_UTILS_H_ + +namespace mir +{ +namespace test +{ +std::string create_temp_file(); +std::string create_temp_dir(); +} +} + +#endif // MIR_TEST_FILE_UTILS_H_ diff --git a/tests/mir_test/CMakeLists.txt b/tests/mir_test/CMakeLists.txt index c0c89187f77..04380945ea4 100644 --- a/tests/mir_test/CMakeLists.txt +++ b/tests/mir_test/CMakeLists.txt @@ -9,6 +9,7 @@ add_library(mir-public-test OBJECT display_config_matchers.cpp event_factory.cpp event_matchers.cpp + file_utils.cpp pipe.cpp popen.cpp signal.cpp diff --git a/tests/mir_test/file_utils.cpp b/tests/mir_test/file_utils.cpp new file mode 100644 index 00000000000..d463b8b8627 --- /dev/null +++ b/tests/mir_test/file_utils.cpp @@ -0,0 +1,44 @@ +/* + * Copyright © Canonical Ltd. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 or 3 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include + +#include "mir/test/file_utils.h" + +std::string mir::test::create_temp_file() +{ + char temp_file[] = "temp_XXXXXX"; + if (int fd = mkstemp(temp_file) == -1) + { + throw std::system_error(errno, std::system_category(), "Failed to create temp file"); + } + else + { + close(fd); + } + + return temp_file; +} + +std::string mir::test::create_temp_dir() +{ + char temp_dir[] = "temp_dir_XXXXXX"; + if (mkdtemp(temp_dir) == nullptr) + throw std::system_error(errno, std::system_category(), "Failed to create temp dir"); + + return temp_dir; +} diff --git a/tests/miral/external_client.cpp b/tests/miral/external_client.cpp index c182803b6ba..54029bc4609 100644 --- a/tests/miral/external_client.cpp +++ b/tests/miral/external_client.cpp @@ -18,6 +18,8 @@ #include #include +#include "mir/test/file_utils.h" + #include #include #include @@ -43,7 +45,7 @@ struct ExternalClient : miral::TestServer miral::X11Support x11_disabled_by_default{}; miral::X11Support x11_enabled_by_default{miral::X11Support{}.default_to_enabled()}; - std::string const output = tmpnam(nullptr); + std::string const output = mir::test::create_temp_file(); auto client_env_value(std::string const& key) const -> std::string { diff --git a/tests/miral/static_display_config.cpp b/tests/miral/static_display_config.cpp index 89830f08533..02dc9dad3ff 100644 --- a/tests/miral/static_display_config.cpp +++ b/tests/miral/static_display_config.cpp @@ -19,6 +19,7 @@ #include "mir/logging/logger.h" #include +#include "mir/test/file_utils.h" #include #include @@ -151,7 +152,9 @@ struct StaticDisplayConfig : Test TEST_F(StaticDisplayConfig, nonexistent_config_file_is_no_error) { - miral::StaticDisplayConfig{tmpnam(nullptr)}; + auto filename = mir::test::create_temp_file(); + unlink(filename.c_str()); + miral::StaticDisplayConfig{filename}; } TEST_F(StaticDisplayConfig, empty_config_input_causes_AbnormalExit) diff --git a/tests/unit-tests/frontend_wayland/test_desktop_file_manager.cpp b/tests/unit-tests/frontend_wayland/test_desktop_file_manager.cpp index 8ed4174a2c8..e56287b22b7 100644 --- a/tests/unit-tests/frontend_wayland/test_desktop_file_manager.cpp +++ b/tests/unit-tests/frontend_wayland/test_desktop_file_manager.cpp @@ -19,6 +19,7 @@ #include "mir/test/doubles/mock_scene_session.h" #include "mir/scene/surface_observer.h" #include "mir/test/doubles/explicit_executor.h" +#include "mir/test/file_utils.h" #include "mir_test_framework/open_wrapper.h" #include @@ -194,7 +195,7 @@ TEST_F(DesktopFileManager, can_resolve_from_valid_flatpak_info) std::stringstream flatpak_info_path; flatpak_info_path << "/proc/" << PID << "/root/.flatpak-info"; auto const flatpak_info = flatpak_info_path.str(); - auto tmp_file_name = std::tmpnam(NULL); + auto tmp_file_name = mir::test::create_temp_file().c_str(); { std::ofstream tmp_file; tmp_file.open(tmp_file_name); @@ -228,7 +229,7 @@ TEST_F(DesktopFileManager, app_id_will_not_resolve_from_flatpak_info_when_name_i std::stringstream flatpak_info_path; flatpak_info_path << "/proc/" << PID << "/root/.flatpak-info"; auto const flatpak_info = flatpak_info_path.str(); - auto tmp_file_name = std::tmpnam(NULL); + auto tmp_file_name = mir::test::create_temp_file().c_str(); { std::ofstream tmp_file; tmp_file.open(tmp_file_name); diff --git a/tests/unit-tests/frontend_wayland/test_g_desktop_file_cache.cpp b/tests/unit-tests/frontend_wayland/test_g_desktop_file_cache.cpp index 8a426a9806e..10674a14f8a 100644 --- a/tests/unit-tests/frontend_wayland/test_g_desktop_file_cache.cpp +++ b/tests/unit-tests/frontend_wayland/test_g_desktop_file_cache.cpp @@ -18,6 +18,7 @@ #include "src/server/frontend_wayland/foreign_toplevel_manager_v1.h" #include "mir/test/doubles/stub_main_loop.h" +#include "mir/test/file_utils.h" #include #include @@ -43,9 +44,7 @@ struct GDesktopFileCache : Test static void SetUpTestSuite() { // Establish the temporary directory - std::filesystem::path tmp_dir_path {std::filesystem::temp_directory_path() /= std::tmpnam(nullptr)}; - std::filesystem::create_directories(tmp_dir_path); - std::string desktop_file_directory_name = tmp_dir_path; + std::string desktop_file_directory_name = mir::test::create_temp_dir(); setenv("XDG_DATA_DIRS", desktop_file_directory_name.c_str(), 1); applications_dir = desktop_file_directory_name + "/applications"; mkdir(applications_dir.c_str(), S_IRWXU);