2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright (C) 2017-2022 Mnemosyne LLC.
|
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0), GPLv3 (SPDX: GPL-3.0),
|
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
#include "transmission.h"
|
|
|
|
#include "error.h"
|
|
|
|
#include "file.h"
|
2021-08-16 01:45:50 +00:00
|
|
|
#include "platform.h"
|
2020-08-11 18:11:55 +00:00
|
|
|
#include "subprocess.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
#include "gtest/internal/gtest-port.h" // GetArgvs()
|
|
|
|
|
|
|
|
#include "test-fixtures.h"
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <cstdlib>
|
2022-04-08 01:50:26 +00:00
|
|
|
#include <map>
|
2020-08-11 18:11:55 +00:00
|
|
|
#include <string>
|
2022-04-08 01:50:26 +00:00
|
|
|
#include <string_view>
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#define setenv(key, value, unused) SetEnvironmentVariableA(key, value)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace libtransmission
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace test
|
|
|
|
{
|
|
|
|
|
2021-08-16 01:45:50 +00:00
|
|
|
std::string getTestProgramPath(std::string const& filename)
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
2022-03-27 17:37:29 +00:00
|
|
|
auto const exe_path = makeString(tr_sys_path_resolve(testing::internal::GetArgvs().front().data()));
|
2022-03-21 20:22:50 +00:00
|
|
|
auto const exe_dir = tr_sys_path_dirname(exe_path);
|
2022-05-21 16:10:58 +00:00
|
|
|
return std::string{ exe_dir } + TR_PATH_DELIMITER + filename;
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
class SubprocessTest
|
|
|
|
: public ::testing::Test
|
|
|
|
, public testing::WithParamInterface<std::string>
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
|
|
|
protected:
|
|
|
|
Sandbox sandbox_;
|
|
|
|
|
|
|
|
[[nodiscard]] std::string buildSandboxPath(std::string const& filename) const
|
|
|
|
{
|
|
|
|
auto path = sandbox_.path();
|
|
|
|
path += TR_PATH_DELIMITER;
|
|
|
|
path += filename;
|
|
|
|
tr_sys_path_native_separators(&path.front());
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] static std::string nativeCwd()
|
|
|
|
{
|
|
|
|
auto path = makeString(tr_sys_dir_get_current(nullptr));
|
|
|
|
tr_sys_path_native_separators(&path.front());
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
std::string const arg_dump_args_{ "--dump-args" };
|
|
|
|
std::string const arg_dump_env_{ "--dump-env" };
|
|
|
|
std::string const arg_dump_cwd_{ "--dump-cwd" };
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
std::string self_path_;
|
|
|
|
|
2022-01-24 19:07:55 +00:00
|
|
|
static void waitForFileToExist(std::string const& path)
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
auto const test = [path]()
|
|
|
|
{
|
2022-03-27 17:37:29 +00:00
|
|
|
return tr_sys_path_exists(path.data());
|
2021-08-15 09:41:48 +00:00
|
|
|
};
|
2020-10-14 22:27:52 +00:00
|
|
|
EXPECT_TRUE(waitFor(test, 30000));
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetUp() override
|
|
|
|
{
|
|
|
|
self_path_ = GetParam();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_P(SubprocessTest, SpawnAsyncMissingExec)
|
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
auto const missing_exe_path = std::string{ TR_IF_WIN32("C:\\", "/") "tr-missing-test-exe" TR_IF_WIN32(".exe", "") };
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2021-11-24 19:25:23 +00:00
|
|
|
auto args = std::array<char const*, 2>{ missing_exe_path.data(), nullptr };
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
tr_error* error = nullptr;
|
2021-11-24 19:25:23 +00:00
|
|
|
auto const ret = tr_spawn_async(std::data(args), {}, nullptr, &error);
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_FALSE(ret);
|
|
|
|
EXPECT_NE(nullptr, error);
|
|
|
|
EXPECT_NE(0, error->code);
|
|
|
|
EXPECT_NE(nullptr, error->message);
|
|
|
|
|
|
|
|
tr_error_clear(&error);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_P(SubprocessTest, SpawnAsyncArgs)
|
|
|
|
{
|
|
|
|
auto const result_path = buildSandboxPath("result.txt");
|
|
|
|
bool const allow_batch_metachars = TR_IF_WIN32(false, true) || !tr_str_has_suffix(self_path_.c_str(), ".cmd");
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
auto const test_arg1 = std::string{ "arg1 " };
|
|
|
|
auto const test_arg2 = std::string{ " arg2" };
|
|
|
|
auto const test_arg3 = std::string{};
|
|
|
|
auto const test_arg4 = std::string{ "\"arg3'^! $PATH %PATH% \\" };
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2021-11-24 19:25:23 +00:00
|
|
|
auto const args = std::array<char const*, 8>{ self_path_.c_str(),
|
|
|
|
result_path.data(),
|
|
|
|
arg_dump_args_.data(),
|
|
|
|
test_arg1.data(),
|
|
|
|
test_arg2.data(),
|
|
|
|
test_arg3.data(),
|
|
|
|
allow_batch_metachars ? test_arg4.data() : nullptr,
|
|
|
|
nullptr };
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
tr_error* error = nullptr;
|
2021-11-24 19:25:23 +00:00
|
|
|
bool const ret = tr_spawn_async(std::data(args), {}, nullptr, &error);
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_TRUE(ret) << args[0] << ' ' << args[1];
|
2022-03-21 20:22:50 +00:00
|
|
|
EXPECT_EQ(nullptr, error) << *error;
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
waitForFileToExist(result_path);
|
|
|
|
|
2022-03-27 17:37:29 +00:00
|
|
|
auto fd = tr_sys_file_open(result_path.data(), TR_SYS_FILE_READ, 0); // NOLINT
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_NE(TR_BAD_SYS_FILE, fd);
|
|
|
|
|
|
|
|
auto buffer = std::array<char, 1024>{};
|
|
|
|
|
2021-10-01 20:28:01 +00:00
|
|
|
buffer[0] = '\0';
|
2022-03-27 17:37:29 +00:00
|
|
|
EXPECT_TRUE(tr_sys_file_read_line(fd, buffer.data(), buffer.size()));
|
2021-11-17 21:56:50 +00:00
|
|
|
buffer.back() = '\0';
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_EQ(test_arg1, buffer.data());
|
|
|
|
|
2021-10-01 20:28:01 +00:00
|
|
|
buffer[0] = '\0';
|
2022-03-27 17:37:29 +00:00
|
|
|
EXPECT_TRUE(tr_sys_file_read_line(fd, buffer.data(), buffer.size()));
|
2021-11-17 21:56:50 +00:00
|
|
|
buffer.back() = '\0';
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_EQ(test_arg2, buffer.data());
|
|
|
|
|
2021-10-01 20:28:01 +00:00
|
|
|
buffer[0] = '\0';
|
2022-03-27 17:37:29 +00:00
|
|
|
EXPECT_TRUE(tr_sys_file_read_line(fd, buffer.data(), buffer.size()));
|
2021-11-17 21:56:50 +00:00
|
|
|
buffer.back() = '\0';
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_EQ(test_arg3, buffer.data());
|
|
|
|
|
|
|
|
if (allow_batch_metachars)
|
|
|
|
{
|
2021-10-01 20:28:01 +00:00
|
|
|
buffer[0] = '\0';
|
2022-03-27 17:37:29 +00:00
|
|
|
EXPECT_TRUE(tr_sys_file_read_line(fd, buffer.data(), buffer.size()));
|
2021-11-17 21:56:50 +00:00
|
|
|
buffer.back() = '\0';
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_EQ(test_arg4, buffer.data());
|
|
|
|
}
|
|
|
|
|
2022-03-27 17:37:29 +00:00
|
|
|
EXPECT_FALSE(tr_sys_file_read_line(fd, buffer.data(), buffer.size()));
|
2021-11-17 21:56:50 +00:00
|
|
|
buffer.back() = '\0';
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-03-27 17:37:29 +00:00
|
|
|
tr_sys_file_close(fd);
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_P(SubprocessTest, SpawnAsyncEnv)
|
|
|
|
{
|
|
|
|
auto const result_path = buildSandboxPath("result.txt");
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
auto const test_env_key1 = std::string{ "VAR1" };
|
|
|
|
auto const test_env_key2 = std::string{ "_VAR_2_" };
|
|
|
|
auto const test_env_key3 = std::string{ "vAr#" };
|
|
|
|
auto const test_env_key4 = std::string{ "FOO" };
|
|
|
|
auto const test_env_key5 = std::string{ "ZOO" };
|
|
|
|
auto const test_env_key6 = std::string{ "TR_MISSING_TEST_ENV_KEY" };
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
auto const test_env_value1 = std::string{ "value1 " };
|
|
|
|
auto const test_env_value2 = std::string{ " value2" };
|
|
|
|
auto const test_env_value3 = std::string{ " \"value3'^! $PATH %PATH% " };
|
|
|
|
auto const test_env_value4 = std::string{ "bar" };
|
|
|
|
auto const test_env_value5 = std::string{ "jar" };
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2021-11-24 19:25:23 +00:00
|
|
|
auto args = std::array<char const*, 10>{
|
|
|
|
self_path_.c_str(), //
|
|
|
|
result_path.data(), //
|
|
|
|
arg_dump_env_.data(), //
|
|
|
|
test_env_key1.data(), //
|
|
|
|
test_env_key2.data(), //
|
|
|
|
test_env_key3.data(), //
|
|
|
|
test_env_key4.data(), //
|
|
|
|
test_env_key5.data(), //
|
|
|
|
test_env_key6.data(), //
|
2021-08-15 09:41:48 +00:00
|
|
|
nullptr, //
|
2020-08-11 18:11:55 +00:00
|
|
|
};
|
|
|
|
|
2021-11-24 19:25:23 +00:00
|
|
|
auto const env = std::map<std::string_view, std::string_view>{
|
|
|
|
{ test_env_key1, test_env_value1 },
|
|
|
|
{ test_env_key2, test_env_value2 },
|
|
|
|
{ test_env_key3, test_env_value3 },
|
|
|
|
{ test_env_key5, test_env_value5 },
|
2020-08-11 18:11:55 +00:00
|
|
|
};
|
|
|
|
|
2022-01-24 19:07:55 +00:00
|
|
|
setenv("FOO", "bar", 1 /*true*/); // inherited
|
|
|
|
setenv("ZOO", "tar", 1 /*true*/); // overridden
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
tr_error* error = nullptr;
|
2021-11-24 19:25:23 +00:00
|
|
|
bool const ret = tr_spawn_async(std::data(args), env, nullptr, &error);
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_TRUE(ret);
|
2022-03-21 20:22:50 +00:00
|
|
|
EXPECT_EQ(nullptr, error) << *error;
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
waitForFileToExist(result_path);
|
|
|
|
|
2022-03-27 17:37:29 +00:00
|
|
|
auto fd = tr_sys_file_open(result_path.data(), TR_SYS_FILE_READ, 0); // NOLINT
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_NE(TR_BAD_SYS_FILE, fd);
|
|
|
|
|
|
|
|
auto buffer = std::array<char, 1024>{};
|
|
|
|
|
2021-10-01 20:28:01 +00:00
|
|
|
buffer[0] = '\0';
|
2022-03-27 17:37:29 +00:00
|
|
|
EXPECT_TRUE(tr_sys_file_read_line(fd, buffer.data(), buffer.size()));
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_EQ(test_env_value1, buffer.data());
|
|
|
|
|
2021-10-01 20:28:01 +00:00
|
|
|
buffer[0] = '\0';
|
2022-03-27 17:37:29 +00:00
|
|
|
EXPECT_TRUE(tr_sys_file_read_line(fd, buffer.data(), buffer.size()));
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_EQ(test_env_value2, buffer.data());
|
|
|
|
|
2021-10-01 20:28:01 +00:00
|
|
|
buffer[0] = '\0';
|
2022-03-27 17:37:29 +00:00
|
|
|
EXPECT_TRUE(tr_sys_file_read_line(fd, buffer.data(), buffer.size()));
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_EQ(test_env_value3, buffer.data());
|
|
|
|
|
2021-10-01 20:28:01 +00:00
|
|
|
buffer[0] = '\0';
|
2022-03-27 17:37:29 +00:00
|
|
|
EXPECT_TRUE(tr_sys_file_read_line(fd, buffer.data(), buffer.size()));
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_EQ(test_env_value4, buffer.data());
|
|
|
|
|
2021-10-01 20:28:01 +00:00
|
|
|
buffer[0] = '\0';
|
2022-03-27 17:37:29 +00:00
|
|
|
EXPECT_TRUE(tr_sys_file_read_line(fd, buffer.data(), buffer.size()));
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_EQ(test_env_value5, buffer.data());
|
|
|
|
|
2021-10-01 20:28:01 +00:00
|
|
|
buffer[0] = '\0';
|
2022-03-27 17:37:29 +00:00
|
|
|
EXPECT_TRUE(tr_sys_file_read_line(fd, buffer.data(), buffer.size()));
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_STREQ("<null>", buffer.data());
|
|
|
|
|
2022-03-27 17:37:29 +00:00
|
|
|
EXPECT_FALSE(tr_sys_file_read_line(fd, buffer.data(), buffer.size()));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-03-27 17:37:29 +00:00
|
|
|
tr_sys_file_close(fd);
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_P(SubprocessTest, SpawnAsyncCwdExplicit)
|
|
|
|
{
|
|
|
|
auto const test_dir = sandbox_.path();
|
|
|
|
auto const result_path = buildSandboxPath("result.txt");
|
|
|
|
|
2021-11-24 19:25:23 +00:00
|
|
|
auto const args = std::array<char const*, 4>{ self_path_.c_str(), result_path.data(), arg_dump_cwd_.data(), nullptr };
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
tr_error* error = nullptr;
|
2021-11-24 19:25:23 +00:00
|
|
|
bool const ret = tr_spawn_async(std::data(args), {}, test_dir.c_str(), &error);
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_TRUE(ret);
|
2022-03-21 20:22:50 +00:00
|
|
|
EXPECT_EQ(nullptr, error) << *error;
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
waitForFileToExist(result_path);
|
|
|
|
|
2022-03-27 17:37:29 +00:00
|
|
|
auto fd = tr_sys_file_open(result_path.data(), TR_SYS_FILE_READ, 0); // NOLINT
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_NE(TR_BAD_SYS_FILE, fd);
|
|
|
|
|
|
|
|
auto buffer = std::array<char, 1024>{};
|
2022-03-27 17:37:29 +00:00
|
|
|
EXPECT_TRUE(tr_sys_file_read_line(fd, buffer.data(), buffer.size()));
|
2021-08-15 09:41:48 +00:00
|
|
|
EXPECT_EQ(
|
|
|
|
makeString(tr_sys_path_native_separators(tr_strdup(test_dir.c_str()))),
|
2020-08-11 18:11:55 +00:00
|
|
|
tr_sys_path_native_separators(&buffer.front()));
|
|
|
|
|
2022-03-27 17:37:29 +00:00
|
|
|
EXPECT_FALSE(tr_sys_file_read_line(fd, buffer.data(), buffer.size()));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-03-27 17:37:29 +00:00
|
|
|
tr_sys_file_close(fd);
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_P(SubprocessTest, SpawnAsyncCwdInherit)
|
|
|
|
{
|
|
|
|
auto const result_path = buildSandboxPath("result.txt");
|
|
|
|
auto const expected_cwd = nativeCwd();
|
|
|
|
|
2021-11-24 19:25:23 +00:00
|
|
|
auto const args = std::array<char const*, 4>{ self_path_.c_str(), result_path.data(), arg_dump_cwd_.data(), nullptr };
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
tr_error* error = nullptr;
|
2021-11-24 19:25:23 +00:00
|
|
|
auto const ret = tr_spawn_async(std::data(args), {}, nullptr, &error);
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_TRUE(ret);
|
2022-03-21 20:22:50 +00:00
|
|
|
EXPECT_EQ(nullptr, error) << *error;
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
waitForFileToExist(result_path);
|
2022-03-27 17:37:29 +00:00
|
|
|
auto fd = tr_sys_file_open(result_path.data(), TR_SYS_FILE_READ, 0); // NOLINT
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_NE(TR_BAD_SYS_FILE, fd);
|
|
|
|
|
|
|
|
auto buffer = std::array<char, 1024>{};
|
2022-03-27 17:37:29 +00:00
|
|
|
EXPECT_TRUE(tr_sys_file_read_line(fd, buffer.data(), buffer.size()));
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_EQ(expected_cwd, tr_sys_path_native_separators(&buffer.front()));
|
2022-03-27 17:37:29 +00:00
|
|
|
EXPECT_FALSE(tr_sys_file_read_line(fd, buffer.data(), buffer.size()));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-03-27 17:37:29 +00:00
|
|
|
tr_sys_file_close(fd);
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_P(SubprocessTest, SpawnAsyncCwdMissing)
|
|
|
|
{
|
|
|
|
auto const result_path = buildSandboxPath("result.txt");
|
|
|
|
|
2021-11-24 19:25:23 +00:00
|
|
|
auto const args = std::array<char const*, 4>{ self_path_.c_str(), result_path.data(), arg_dump_cwd_.data(), nullptr };
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
tr_error* error = nullptr;
|
2021-11-24 19:25:23 +00:00
|
|
|
auto const ret = tr_spawn_async(std::data(args), {}, TR_IF_WIN32("C:\\", "/") "tr-missing-test-work-dir", &error);
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_FALSE(ret);
|
|
|
|
EXPECT_NE(nullptr, error);
|
|
|
|
EXPECT_NE(0, error->code);
|
|
|
|
EXPECT_NE(nullptr, error->message);
|
|
|
|
tr_error_clear(&error);
|
|
|
|
}
|
|
|
|
|
2021-08-16 01:45:50 +00:00
|
|
|
INSTANTIATE_TEST_SUITE_P(
|
2020-08-11 18:11:55 +00:00
|
|
|
Subprocess,
|
|
|
|
SubprocessTest,
|
2021-08-16 01:45:50 +00:00
|
|
|
TR_IF_WIN32(
|
|
|
|
::testing::Values( //
|
|
|
|
getTestProgramPath("subprocess-test.exe"),
|
|
|
|
getTestProgramPath("subprocess-test.cmd")),
|
|
|
|
::testing::Values( //
|
|
|
|
getTestProgramPath("subprocess-test"))));
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
} // namespace test
|
|
|
|
|
|
|
|
} // namespace libtransmission
|