Skip to content

Commit

Permalink
tests: replace tmpnam use
Browse files Browse the repository at this point in the history
Fixes #2675
  • Loading branch information
Saviq committed Jul 18, 2024
1 parent 6842d73 commit 6c7a94a
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 17 deletions.
13 changes: 3 additions & 10 deletions tests/acceptance-tests/server_configuration_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#include "mir/options/option.h"

#include "mir/test/file_utils.h"

#include <gtest/gtest.h>
#include <gmock/gmock.h>

Expand All @@ -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<std::string> const&));
Expand Down Expand Up @@ -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";
Expand Down
29 changes: 29 additions & 0 deletions tests/include/mir/test/file_utils.h
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

#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_
1 change: 1 addition & 0 deletions tests/mir_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 44 additions & 0 deletions tests/mir_test/file_utils.cpp
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

#include <system_error>
#include <unistd.h>

#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;
}
4 changes: 3 additions & 1 deletion tests/miral/external_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include <miral/external_client.h>
#include <miral/x11_support.h>

#include "mir/test/file_utils.h"

#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <fstream>
Expand All @@ -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
{
Expand Down
5 changes: 4 additions & 1 deletion tests/miral/static_display_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "mir/logging/logger.h"

#include <mir/test/doubles/mock_display_configuration.h>
#include "mir/test/file_utils.h"

#include <gtest/gtest.h>
#include <gmock/gmock.h>
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <filesystem>
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <fstream>

#include <gmock/gmock.h>
Expand All @@ -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);
Expand Down

0 comments on commit 6c7a94a

Please sign in to comment.