libcryfs/test/cpp-utils/system/HomedirTest.cpp

77 lines
2.5 KiB
C++
Raw Normal View History

#include <gtest/gtest.h>
#include <cpp-utils/system/homedir.h>
#include <cpp-utils/tempfile/TempDir.h>
using cpputils::system::HomeDirectory;
using cpputils::system::FakeHomeDirectoryRAII;
using cpputils::system::FakeTempHomeDirectoryRAII;
using cpputils::TempDir;
namespace bf = boost::filesystem;
TEST(HomedirTest, HomedirExists) {
EXPECT_TRUE(bf::exists(HomeDirectory::get()));
}
2018-05-19 08:00:37 +02:00
TEST(HomedirTest, AppDataDirIsValid) {
auto dir = HomeDirectory::getXDGDataDir();
EXPECT_FALSE(dir.empty());
2018-05-19 10:19:46 +02:00
EXPECT_GE(std::distance(dir.begin(), dir.end()), 2u); // has at least two components
2018-05-19 08:00:37 +02:00
for(const auto& component : dir) {
EXPECT_TRUE(component.string() == "/" || bf::native(component.string()));
2018-05-19 08:00:37 +02:00
}
}
TEST(HomedirTest, FakeHomeDirectorySetsHomedirCorrectly) {
TempDir fakeHomeDir, fakeAppDataDir;
FakeHomeDirectoryRAII a(fakeHomeDir.path(), fakeAppDataDir.path());
EXPECT_EQ(fakeHomeDir.path(), HomeDirectory::get());
EXPECT_EQ(fakeAppDataDir.path(), HomeDirectory::getXDGDataDir());
}
TEST(HomedirTest, FakeHomeDirectoryResetsHomedirCorrectly) {
bf::path actualHomeDir = HomeDirectory::get();
bf::path actualAppDataDir = HomeDirectory::getXDGDataDir();
{
TempDir fakeHomeDir, fakeAppDataDir;
FakeHomeDirectoryRAII a(fakeHomeDir.path(), fakeAppDataDir.path());
EXPECT_NE(actualHomeDir, HomeDirectory::get());
EXPECT_NE(actualAppDataDir, HomeDirectory::getXDGDataDir());
}
EXPECT_EQ(actualHomeDir, HomeDirectory::get());
EXPECT_EQ(actualAppDataDir, HomeDirectory::getXDGDataDir());
}
TEST(HomedirTest, FakeTempHomeDirectorySetsHomedirCorrectly) {
bf::path actualHomeDir = HomeDirectory::get();
bf::path actualAppDataDir = HomeDirectory::getXDGDataDir();
FakeTempHomeDirectoryRAII a;
EXPECT_NE(actualHomeDir, HomeDirectory::get());
EXPECT_NE(actualAppDataDir, HomeDirectory::getXDGDataDir());
}
TEST(HomedirTest, FakeTempHomeDirectoryResetsHomedirCorrectly) {
bf::path actualHomeDir = HomeDirectory::get();
bf::path actualAppDataDir = HomeDirectory::getXDGDataDir();
{
FakeTempHomeDirectoryRAII a;
EXPECT_NE(actualHomeDir, HomeDirectory::get());
EXPECT_NE(actualAppDataDir, HomeDirectory::getXDGDataDir());
}
EXPECT_EQ(actualHomeDir, HomeDirectory::get());
EXPECT_EQ(actualAppDataDir, HomeDirectory::getXDGDataDir());
}
TEST(HomedirTest, FakeTempHomeDirectoryUsesDifferentDirsForHomedirAndAppdataDir) {
FakeTempHomeDirectoryRAII a;
EXPECT_NE(HomeDirectory::get(), HomeDirectory::getXDGDataDir());
}