2016-02-21 22:15:27 +01:00
|
|
|
#include <gtest/gtest.h>
|
2016-02-23 21:07:23 +01:00
|
|
|
#include <cryfs-cli/Environment.h>
|
2016-02-21 22:15:27 +01:00
|
|
|
#include <boost/optional.hpp>
|
2018-04-22 07:04:21 +02:00
|
|
|
#include <boost/filesystem.hpp>
|
2018-09-04 00:46:08 +02:00
|
|
|
#include <cpp-utils/system/env.h>
|
2016-02-21 22:15:27 +01:00
|
|
|
|
2019-01-19 22:02:41 +01:00
|
|
|
using namespace cryfs_cli;
|
2016-02-21 22:15:27 +01:00
|
|
|
using std::string;
|
|
|
|
using boost::optional;
|
|
|
|
using boost::none;
|
|
|
|
|
2018-09-07 14:00:47 +02:00
|
|
|
#if defined(_MSC_VER)
|
|
|
|
constexpr const char* some_local_state_dir = "C:/my/local/state/dir";
|
|
|
|
#else
|
|
|
|
constexpr const char* some_local_state_dir = "/my/local/state/dir";
|
|
|
|
#endif
|
|
|
|
|
2018-04-22 07:04:21 +02:00
|
|
|
namespace bf = boost::filesystem;
|
|
|
|
|
2016-02-21 22:15:27 +01:00
|
|
|
class EnvironmentTest : public ::testing::Test {
|
|
|
|
public:
|
|
|
|
// WithEnv sets an environment variable while it is in scope.
|
|
|
|
// Once it leaves scope, the environment is reset.
|
|
|
|
class WithEnv {
|
|
|
|
public:
|
|
|
|
WithEnv(const string &key, const string &value): _key(key) , _oldValue(none) {
|
|
|
|
char *oldValue = std::getenv(key.c_str());
|
|
|
|
if (nullptr != oldValue) {
|
|
|
|
_oldValue = string(oldValue);
|
|
|
|
}
|
2018-09-04 00:46:08 +02:00
|
|
|
cpputils::setenv(key.c_str(), value.c_str());
|
2016-02-21 22:15:27 +01:00
|
|
|
}
|
|
|
|
~WithEnv() {
|
|
|
|
if (none == _oldValue) {
|
2018-09-04 00:46:08 +02:00
|
|
|
cpputils::unsetenv(_key.c_str());
|
2016-02-21 22:15:27 +01:00
|
|
|
} else {
|
2018-09-04 00:46:08 +02:00
|
|
|
cpputils::setenv(_key.c_str(), _oldValue->c_str());
|
2016-02-21 22:15:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
string _key;
|
|
|
|
optional<string> _oldValue;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(EnvironmentTest, Noninteractive_Unset) {
|
|
|
|
EXPECT_FALSE(Environment::isNoninteractive());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EnvironmentTest, Noninteractive_Set) {
|
2023-07-08 23:48:59 +02:00
|
|
|
const WithEnv env("CRYFS_FRONTEND", "noninteractive");
|
2016-02-21 22:15:27 +01:00
|
|
|
EXPECT_TRUE(Environment::isNoninteractive());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EnvironmentTest, Noninteractive_SetToOtherValue) {
|
2023-07-08 23:48:59 +02:00
|
|
|
const WithEnv env("CRYFS_FRONTEND", "someotherfrontend");
|
2016-02-21 22:15:27 +01:00
|
|
|
EXPECT_FALSE(Environment::isNoninteractive());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EnvironmentTest, NoUpdateCheck_Unset) {
|
|
|
|
EXPECT_FALSE(Environment::noUpdateCheck());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EnvironmentTest, NoUpdateCheck_Set) {
|
2023-07-08 23:48:59 +02:00
|
|
|
const WithEnv env("CRYFS_NO_UPDATE_CHECK", "true");
|
2016-02-21 22:15:27 +01:00
|
|
|
EXPECT_TRUE(Environment::noUpdateCheck());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EnvironmentTest, NoUpdateCheck_SetToOtherValue) {
|
2023-07-08 23:48:59 +02:00
|
|
|
const WithEnv env("CRYFS_NO_UPDATE_CHECK", "someothervalue");
|
2016-02-21 22:15:27 +01:00
|
|
|
// No matter what the value is, setting the environment variable says we don't do update checks.
|
|
|
|
EXPECT_TRUE(Environment::noUpdateCheck());
|
|
|
|
}
|
2018-04-22 07:04:21 +02:00
|
|
|
|
|
|
|
TEST_F(EnvironmentTest, LocalStateDir_NotSet) {
|
|
|
|
EXPECT_EQ(Environment::defaultLocalStateDir(), Environment::localStateDir());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EnvironmentTest, LocalStateDir_Set) {
|
2023-07-08 23:48:59 +02:00
|
|
|
const WithEnv env("CRYFS_LOCAL_STATE_DIR", some_local_state_dir);
|
2018-09-07 14:00:47 +02:00
|
|
|
EXPECT_EQ(some_local_state_dir, Environment::localStateDir().string());
|
2018-04-22 07:04:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EnvironmentTest, LocalStateDir_ConvertsRelativeToAbsolutePath_WithDot) {
|
2023-07-08 23:48:59 +02:00
|
|
|
const WithEnv env("CRYFS_LOCAL_STATE_DIR", "./dir");
|
2018-05-21 01:20:38 +02:00
|
|
|
EXPECT_EQ((bf::current_path() / "./dir").string(), Environment::localStateDir().string());
|
2018-04-22 07:04:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EnvironmentTest, LocalStateDir_ConvertsRelativeToAbsolutePath_WithoutDot) {
|
2023-07-08 23:48:59 +02:00
|
|
|
const WithEnv env("CRYFS_LOCAL_STATE_DIR", "dir");
|
2018-05-21 01:20:38 +02:00
|
|
|
EXPECT_EQ((bf::current_path() / "dir").string(), Environment::localStateDir().string());
|
2018-04-22 07:04:21 +02:00
|
|
|
}
|