You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
352 lines
8.5 KiB
352 lines
8.5 KiB
3 years ago
|
/*
|
||
|
* Copyright (C) 2022 Riyyi
|
||
|
*
|
||
|
* SPDX-License-Identifier: MIT
|
||
|
*/
|
||
|
|
||
3 years ago
|
#include <cstdio> // stderr
|
||
3 years ago
|
#include <filesystem>
|
||
|
#include <string>
|
||
3 years ago
|
#include <unistd.h> // geteuid, setegid, seteuid
|
||
3 years ago
|
#include <vector>
|
||
|
|
||
|
#include "dotfile.h"
|
||
|
#include "machine.h"
|
||
|
#include "macro.h"
|
||
|
#include "testcase.h"
|
||
|
#include "testsuite.h"
|
||
|
#include "util/file.h"
|
||
|
#include "util/system.h"
|
||
|
|
||
3 years ago
|
const bool root = !geteuid() ? true : false;
|
||
3 years ago
|
const std::filesystem::path homeDirectory = "/home/" + Machine::the().username();
|
||
|
const size_t homeDirectorySize = homeDirectory.string().size();
|
||
3 years ago
|
|
||
3 years ago
|
void createTestDotfiles(const std::vector<std::string>& fileNames, const std::vector<std::string>& fileContents, bool asRoot = false)
|
||
3 years ago
|
{
|
||
3 years ago
|
VERIFY(fileNames.size() == fileContents.size(), return);
|
||
3 years ago
|
|
||
3 years ago
|
if (root && !asRoot) {
|
||
3 years ago
|
setegid(Machine::the().gid());
|
||
|
seteuid(Machine::the().uid());
|
||
|
}
|
||
|
|
||
3 years ago
|
for (size_t i = 0; i < fileNames.size(); ++i) {
|
||
|
auto fileName = fileNames.at(i);
|
||
|
|
||
|
auto directory = std::filesystem::path { fileName }.parent_path();
|
||
|
if (!directory.empty() && !std::filesystem::exists(directory)) {
|
||
|
std::filesystem::create_directories(directory);
|
||
|
}
|
||
|
|
||
|
auto file = Util::File::create(fileName);
|
||
|
if (file.data().size() == 0) {
|
||
|
file.append(fileContents.at(i).c_str()).flush();
|
||
|
}
|
||
|
}
|
||
3 years ago
|
|
||
3 years ago
|
if (root && !asRoot) {
|
||
3 years ago
|
seteuid(0);
|
||
|
setegid(0);
|
||
|
}
|
||
3 years ago
|
}
|
||
|
|
||
|
void removeTestDotfiles(const std::vector<std::string>& files)
|
||
|
{
|
||
|
for (auto file : files) {
|
||
|
// Get the top-level directory
|
||
|
if (file.find(homeDirectory) == 0) {
|
||
|
file = file.substr(homeDirectorySize + 1);
|
||
|
}
|
||
|
file = file.substr(0, file.find_first_of('/'));
|
||
|
|
||
|
// Delete recursively in working directory
|
||
|
if (std::filesystem::exists(file)) {
|
||
|
std::filesystem::remove_all(file);
|
||
|
}
|
||
|
|
||
|
// Delete recursively in home directory
|
||
3 years ago
|
file = homeDirectory / file;
|
||
3 years ago
|
if (std::filesystem::exists(file)) {
|
||
|
std::filesystem::remove_all(file);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// -----------------------------------------
|
||
|
|
||
|
TEST_CASE(AddDotfiles)
|
||
|
{
|
||
|
std::vector<std::string> fileNames = {
|
||
3 years ago
|
homeDirectory / "__test-file-1",
|
||
|
homeDirectory / "__subdir/__test-file-2",
|
||
|
homeDirectory / "__subdir/__another-subdir/__test-file-3",
|
||
3 years ago
|
};
|
||
|
|
||
|
std::vector<std::string> fileContents = {
|
||
|
R"(home directory file 1
|
||
|
)",
|
||
|
R"(home directory file 2
|
||
|
)",
|
||
|
R"(home directory file 3
|
||
|
)",
|
||
|
};
|
||
|
|
||
|
createTestDotfiles(fileNames, fileContents);
|
||
|
|
||
|
Dotfile::the().add(fileNames);
|
||
|
|
||
|
for (const auto& file : fileNames) {
|
||
3 years ago
|
VERIFY(std::filesystem::exists(file), continue);
|
||
|
VERIFY(std::filesystem::exists(file.substr(homeDirectorySize + 1)), continue);
|
||
3 years ago
|
|
||
|
Util::File lhs(file);
|
||
|
Util::File rhs(file.substr(homeDirectorySize + 1));
|
||
|
EXPECT_EQ(lhs.data(), rhs.data());
|
||
|
}
|
||
|
|
||
|
removeTestDotfiles(fileNames);
|
||
|
}
|
||
|
|
||
|
TEST_CASE(AddNonExistentDotfiles)
|
||
|
{
|
||
3 years ago
|
stderr = Test::TestSuite::the().outputNull();
|
||
3 years ago
|
Dotfile::the().add({ homeDirectory / "__non-existent-test-file" });
|
||
3 years ago
|
stderr = Test::TestSuite::the().outputErr();
|
||
|
|
||
3 years ago
|
EXPECT(!std::filesystem::exists("__non-existent-test-file"));
|
||
3 years ago
|
removeTestDotfiles({ "__non-existent-test-file" });
|
||
3 years ago
|
}
|
||
|
|
||
|
TEST_CASE(PullDotfiles)
|
||
|
{
|
||
|
std::vector<std::string> fileNames = {
|
||
|
"__test-file-1",
|
||
|
"__subdir/__test-file-2",
|
||
|
"__subdir/__another-subdir/__test-file-3",
|
||
|
};
|
||
|
std::vector<std::string> homeFileNames;
|
||
|
for (const auto& file : fileNames) {
|
||
3 years ago
|
homeFileNames.push_back(homeDirectory / file);
|
||
3 years ago
|
}
|
||
|
|
||
|
std::vector<std::string> homeFileContents = {
|
||
|
R"(file 1 after pulling
|
||
|
)",
|
||
|
R"(file 2 after pulling
|
||
|
)",
|
||
|
R"(file 3 after pulling
|
||
|
)",
|
||
|
};
|
||
|
std::vector<std::string> workingDirectoryFileContents = {
|
||
|
R"(file 1
|
||
|
)",
|
||
|
R"(file 2
|
||
|
)",
|
||
|
R"(file 3
|
||
|
)",
|
||
|
};
|
||
|
|
||
|
createTestDotfiles(homeFileNames, homeFileContents);
|
||
|
createTestDotfiles(fileNames, workingDirectoryFileContents);
|
||
|
|
||
|
Dotfile::the().pull(fileNames);
|
||
|
|
||
|
for (size_t i = 0; i < fileNames.size(); ++i) {
|
||
3 years ago
|
VERIFY(std::filesystem::exists(homeFileNames.at(i)), continue);
|
||
|
VERIFY(std::filesystem::exists(fileNames.at(i)), continue);
|
||
3 years ago
|
|
||
|
Util::File lhs(homeFileNames.at(i));
|
||
|
Util::File rhs(fileNames.at(i));
|
||
|
EXPECT_EQ(lhs.data(), rhs.data());
|
||
|
}
|
||
|
|
||
|
removeTestDotfiles(fileNames);
|
||
|
}
|
||
|
|
||
|
TEST_CASE(PushDotfiles)
|
||
|
{
|
||
|
std::vector<std::string> fileNames = {
|
||
|
"__test-file-1",
|
||
|
"__subdir/__test-file-2",
|
||
|
"__subdir/__another-subdir/__test-file-3",
|
||
|
};
|
||
|
|
||
|
std::vector<std::string> fileContents = {
|
||
|
R"(working directory file 1
|
||
|
)",
|
||
|
R"(working directory file 2
|
||
|
)",
|
||
|
R"(working directory file 3
|
||
|
)",
|
||
|
};
|
||
|
|
||
|
createTestDotfiles(fileNames, fileContents);
|
||
|
|
||
|
Dotfile::the().push(fileNames);
|
||
|
|
||
|
for (const auto& file : fileNames) {
|
||
3 years ago
|
VERIFY(std::filesystem::exists(file), continue);
|
||
|
VERIFY(std::filesystem::exists(homeDirectory / file), continue);
|
||
3 years ago
|
|
||
|
Util::File lhs(file);
|
||
3 years ago
|
Util::File rhs(homeDirectory / file);
|
||
3 years ago
|
EXPECT_EQ(lhs.data(), rhs.data());
|
||
|
}
|
||
|
|
||
|
removeTestDotfiles(fileNames);
|
||
|
}
|
||
3 years ago
|
|
||
3 years ago
|
TEST_CASE(PushDotfilesWithExcludePath)
|
||
|
{
|
||
|
std::vector<std::string> fileNames = {
|
||
|
"__test-file-1",
|
||
|
"__subdir/__test-file-2",
|
||
|
"__subdir/__test-file-3",
|
||
|
"__another-subdir/__test-file-4.test",
|
||
|
};
|
||
|
|
||
|
createTestDotfiles(fileNames, { "", "", "", "" });
|
||
|
|
||
|
Dotfile::the().setExcludePaths({
|
||
|
{ Dotfile::ExcludeType::File, "__test-file-1" },
|
||
|
{ Dotfile::ExcludeType::Directory, "__subdir" },
|
||
|
{ Dotfile::ExcludeType::EndsWith, ".test" },
|
||
|
});
|
||
|
Dotfile::the().push(fileNames);
|
||
|
Dotfile::the().setExcludePaths({});
|
||
|
|
||
|
for (const auto& file : fileNames) {
|
||
3 years ago
|
EXPECT(!std::filesystem::exists(homeDirectory / file));
|
||
3 years ago
|
}
|
||
|
|
||
|
removeTestDotfiles(fileNames);
|
||
|
}
|
||
|
|
||
3 years ago
|
TEST_CASE(PushDotfilesSelectivelyComment)
|
||
|
{
|
||
|
std::vector<std::string> fileNames = {
|
||
|
"__test-file-1",
|
||
|
"__test-file-2",
|
||
|
"__test-file-3",
|
||
|
"__test-file-4",
|
||
|
"__test-file-5",
|
||
|
"__test-file-6",
|
||
|
"__test-file-7",
|
||
|
};
|
||
|
|
||
|
auto distro = Machine::the().distroId();
|
||
|
auto hostname = Machine::the().hostname();
|
||
|
auto username = Machine::the().username();
|
||
|
std::string placeholder = "@@@@";
|
||
|
|
||
|
std::vector<std::string> fileContents = {
|
||
|
"# >>> distro=" + distro + " hostname=" + hostname + " user=" + username + R"(
|
||
|
# this should be uncommented
|
||
|
# <<<
|
||
|
)",
|
||
|
"# >>> distro=" + placeholder + " hostname=" + hostname + " user=" + username + R"(
|
||
|
# this should remain commented
|
||
|
# <<<
|
||
|
)",
|
||
|
"# >>> distro=" + distro + " hostname=" + placeholder + " user=" + username + R"(
|
||
|
# this should remain commented
|
||
|
# <<<
|
||
|
)",
|
||
|
"# >>> distro=" + distro + " hostname=" + hostname + " user=" + placeholder + R"(
|
||
|
this should be commented
|
||
|
# <<<
|
||
|
)",
|
||
|
" # >>> distro=" + distro + R"(
|
||
|
# this should be uncommented
|
||
|
# <<<
|
||
|
)",
|
||
|
" # >>> user=" + username + R"(
|
||
|
# this should be uncommented
|
||
|
# <<<
|
||
|
)",
|
||
|
" # >>> hostname=" + hostname + R"(
|
||
|
this should remain uncommented
|
||
|
# <<<
|
||
|
)",
|
||
|
};
|
||
|
|
||
|
std::vector<std::string> pushedFileContents = {
|
||
|
"# >>> distro=" + distro + " hostname=" + hostname + " user=" + username + R"(
|
||
|
this should be uncommented
|
||
|
# <<<
|
||
|
)",
|
||
|
"# >>> distro=" + placeholder + " hostname=" + hostname + " user=" + username + R"(
|
||
|
# this should remain commented
|
||
|
# <<<
|
||
|
)",
|
||
|
"# >>> distro=" + distro + " hostname=" + placeholder + " user=" + username + R"(
|
||
|
# this should remain commented
|
||
|
# <<<
|
||
|
)",
|
||
|
"# >>> distro=" + distro + " hostname=" + hostname + " user=" + placeholder + R"(
|
||
|
# this should be commented
|
||
|
# <<<
|
||
|
)",
|
||
|
" # >>> distro=" + distro + R"(
|
||
|
this should be uncommented
|
||
|
# <<<
|
||
|
)",
|
||
|
" # >>> user=" + username + R"(
|
||
|
this should be uncommented
|
||
|
# <<<
|
||
|
)",
|
||
|
" # >>> hostname=" + hostname + R"(
|
||
|
this should remain uncommented
|
||
|
# <<<
|
||
|
)",
|
||
|
};
|
||
|
|
||
|
createTestDotfiles(fileNames, fileContents);
|
||
|
|
||
|
Dotfile::the().push(fileNames);
|
||
|
|
||
|
for (size_t i = 0; i < fileNames.size(); ++i) {
|
||
|
const auto& file = fileNames.at(i);
|
||
|
VERIFY(std::filesystem::exists(file), continue);
|
||
|
VERIFY(std::filesystem::exists(homeDirectory / file), continue);
|
||
|
|
||
|
Util::File lhs(homeDirectory / file);
|
||
|
EXPECT_EQ(lhs.data(), pushedFileContents.at(i));
|
||
|
}
|
||
|
|
||
|
removeTestDotfiles(fileNames);
|
||
|
}
|
||
|
|
||
3 years ago
|
TEST_CASE(AddSystemDotfiles)
|
||
|
{
|
||
3 years ago
|
VERIFY(geteuid() == 0, return);
|
||
3 years ago
|
|
||
|
Dotfile::the().setSystemDirectories({ "/etc", "/usr/lib" });
|
||
|
Dotfile::the().add({ "/etc/group", "/usr/lib/os-release" });
|
||
|
Dotfile::the().setSystemDirectories({});
|
||
|
|
||
|
EXPECT(std::filesystem::exists("etc/group"));
|
||
|
EXPECT(std::filesystem::exists("usr/lib/os-release"));
|
||
|
|
||
|
std::filesystem::remove_all(Dotfile::the().workingDirectory() / "etc");
|
||
|
std::filesystem::remove_all(Dotfile::the().workingDirectory() / "usr");
|
||
|
}
|
||
|
|
||
|
TEST_CASE(PullSystemDotfiles)
|
||
|
{
|
||
3 years ago
|
VERIFY(geteuid() == 0, return);
|
||
3 years ago
|
|
||
3 years ago
|
createTestDotfiles({ "etc/group" }, { "" }, true);
|
||
3 years ago
|
|
||
|
Dotfile::the().setSystemDirectories({ "/etc" });
|
||
|
Dotfile::the().pull({ "etc/group" });
|
||
|
Dotfile::the().setSystemDirectories({});
|
||
|
|
||
|
Util::File lhs("/etc/group");
|
||
|
Util::File rhs(Dotfile::the().workingDirectory() / "etc/group");
|
||
|
EXPECT_EQ(lhs.data(), rhs.data());
|
||
|
|
||
|
std::filesystem::remove_all(Dotfile::the().workingDirectory() / "etc");
|
||
|
}
|