1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
|
#include "testutils/CliTest.h"
#include <cpp-utils/system/env.h>
namespace bf = boost::filesystem;
using ::testing::Values;
using ::testing::WithParamInterface;
using ::testing::Return;
using std::vector;
using cpputils::TempFile;
using cryfs::ErrorCode;
struct TestConfig {
bool externalConfigfile;
bool logIsNotStderr;
bool runningInForeground;
};
//Tests what happens if cryfs is run in the wrong environment, i.e. with a base directory that doesn't exist or similar
class CliTest_WrongEnvironment: public CliTest, public WithParamInterface<TestConfig> {
public:
void SetAllPermissions(const bf::path &dir) {
bf::permissions(dir, bf::owner_write|bf::owner_read|bf::owner_exe);
}
void SetNoReadPermission(const bf::path &dir) {
bf::permissions(dir, bf::owner_write|bf::owner_exe);
}
void SetNoWritePermission(const bf::path &dir) {
bf::permissions(dir, bf::owner_read|bf::owner_exe);
}
void SetNoExePermission(const bf::path &dir) {
bf::permissions(dir, bf::owner_read|bf::owner_write);
}
void SetNoPermission(const bf::path &dir) {
bf::permissions(dir, bf::no_perms);
}
void Test_Run_Success() {
EXPECT_RUN_SUCCESS(args(), mountdir);
}
void Test_Run_Error(const char *expectedError, cryfs::ErrorCode errorCode) {
EXPECT_RUN_ERROR(
args(),
expectedError,
errorCode
);
}
vector<string> args() {
vector<string> result = {basedir.string(), mountdir.string()};
if (GetParam().externalConfigfile) {
result.push_back("--config");
result.push_back(configfile.path().string());
}
if (GetParam().logIsNotStderr) {
result.push_back("--logfile");
result.push_back(logfile.path().string());
}
if (GetParam().runningInForeground) {
result.push_back("-f");
}
// Test case should be non-interactive, so don't ask for cipher.
result.push_back("--cipher");
result.push_back("aes-256-gcm");
return result;
}
};
INSTANTIATE_TEST_SUITE_P(DefaultParams, CliTest_WrongEnvironment, Values(TestConfig({false, false, false})));
INSTANTIATE_TEST_SUITE_P(ExternalConfigfile, CliTest_WrongEnvironment, Values(TestConfig({true, false, false})));
INSTANTIATE_TEST_SUITE_P(LogIsNotStderr, CliTest_WrongEnvironment, Values(TestConfig({false, true, false})));
INSTANTIATE_TEST_SUITE_P(ExternalConfigfile_LogIsNotStderr, CliTest_WrongEnvironment, Values(TestConfig({true, true, false})));
INSTANTIATE_TEST_SUITE_P(RunningInForeground, CliTest_WrongEnvironment, Values(TestConfig({false, false, true})));
INSTANTIATE_TEST_SUITE_P(RunningInForeground_ExternalConfigfile, CliTest_WrongEnvironment, Values(TestConfig({true, false, true})));
INSTANTIATE_TEST_SUITE_P(RunningInForeground_LogIsNotStderr, CliTest_WrongEnvironment, Values(TestConfig({false, true, true})));
INSTANTIATE_TEST_SUITE_P(RunningInForeground_ExternalConfigfile_LogIsNotStderr, CliTest_WrongEnvironment, Values(TestConfig({true, true, true})));
//Counter-Test. Test that it doesn't fail if we call it without an error condition.
TEST_P(CliTest_WrongEnvironment, NoErrorCondition) {
if (!GetParam().runningInForeground) {return;} // TODO Make this work also if run in background (see CliTest::EXPECT_RUN_SUCCESS)
Test_Run_Success();
}
TEST_P(CliTest_WrongEnvironment, MountDirIsBaseDir) {
mountdir = basedir;
Test_Run_Error("Error 18: base directory can't be inside the mount directory", ErrorCode::BaseDirInsideMountDir);
}
bf::path make_relative(const bf::path &path) {
bf::path result;
const bf::path cwd = bf::current_path();
for(auto iter = ++cwd.begin(); iter!=cwd.end(); ++iter) {
result /= "..";
}
result /= path.relative_path();
return result;
}
TEST_P(CliTest_WrongEnvironment, MountDirIsBaseDir_MountDirRelative) {
mountdir = make_relative(basedir);
Test_Run_Error("Error 18: base directory can't be inside the mount directory", ErrorCode::BaseDirInsideMountDir);
}
TEST_P(CliTest_WrongEnvironment, MountDirIsBaseDir_BaseDirRelative) {
mountdir = basedir;
basedir = make_relative(basedir);
Test_Run_Error("Error 18: base directory can't be inside the mount directory", ErrorCode::BaseDirInsideMountDir);
}
TEST_P(CliTest_WrongEnvironment, MountDirIsBaseDir_BothRelative) {
basedir = make_relative(basedir);
mountdir = basedir;
Test_Run_Error("Error 18: base directory can't be inside the mount directory", ErrorCode::BaseDirInsideMountDir);
}
TEST_P(CliTest_WrongEnvironment, BaseDir_DoesntExist) {
_basedir.remove();
// ON_CALL and not EXPECT_CALL, because this is a death test (i.e. it is forked) and gmock EXPECT_CALL in fork children don't report to parents.
ON_CALL(*console, askYesNo("Could not find base directory. Do you want to create it?", testing::_)).WillByDefault(Return(false));
Test_Run_Error("Error 16: base directory not found", ErrorCode::InaccessibleBaseDir);
}
TEST_P(CliTest_WrongEnvironment, BaseDir_DoesntExist_Noninteractive) {
_basedir.remove();
// We can't set an EXPECT_CALL().Times(0), because this is a death test (i.e. it is forked) and gmock EXPECT_CALL in fork children don't report to parents.
// So we set a default answer that shouldn't crash and check it's not called by checking that it crashes.
ON_CALL(*console, askYesNo("Could not find base directory. Do you want to create it?", testing::_)).WillByDefault(Return(true));
cpputils::setenv("CRYFS_FRONTEND", "noninteractive");
Test_Run_Error("Error 16: base directory not found", ErrorCode::InaccessibleBaseDir);
cpputils::unsetenv("CRYFS_FRONTEND");
}
TEST_P(CliTest_WrongEnvironment, BaseDir_DoesntExist_Create) {
if (!GetParam().runningInForeground) {return;} // TODO Make this work also if run in background (see CliTest::EXPECT_RUN_SUCCESS)
_basedir.remove();
ON_CALL(*console, askYesNo("Could not find base directory. Do you want to create it?", testing::_)).WillByDefault(Return(true));
Test_Run_Success();
EXPECT_TRUE(bf::exists(_basedir.path()) && bf::is_directory(_basedir.path()));
}
TEST_P(CliTest_WrongEnvironment, BaseDir_IsNotDirectory) {
const TempFile basedirfile;
basedir = basedirfile.path();
Test_Run_Error("Error 16: base directory is not a directory", ErrorCode::InaccessibleBaseDir);
}
TEST_P(CliTest_WrongEnvironment, BaseDir_AllPermissions) {
if (!GetParam().runningInForeground) {return;} // TODO Make this work also if run in background (see CliTest::EXPECT_RUN_SUCCESS)
//Counter-Test. Test it doesn't fail if permissions are there.
SetAllPermissions(basedir);
Test_Run_Success();
}
// boost::filesystem doesn't set permissions on Windows correctly
#if !defined(_MSC_VER)
TEST_P(CliTest_WrongEnvironment, BaseDir_NoReadPermission) {
SetNoReadPermission(basedir);
Test_Run_Error("Error 16: Could not read from base directory", ErrorCode::InaccessibleBaseDir);
}
TEST_P(CliTest_WrongEnvironment, BaseDir_NoExePermission) {
SetNoExePermission(basedir);
Test_Run_Error("Error 16: Could not write to base directory", ErrorCode::InaccessibleBaseDir);
}
TEST_P(CliTest_WrongEnvironment, BaseDir_NoWritePermission) {
SetNoWritePermission(basedir);
Test_Run_Error("Error 16: Could not write to base directory", ErrorCode::InaccessibleBaseDir);
}
TEST_P(CliTest_WrongEnvironment, BaseDir_NoPermission) {
SetNoPermission(basedir);
Test_Run_Error("Error 16: Could not write to base directory", ErrorCode::InaccessibleBaseDir);
}
#endif
TEST_P(CliTest_WrongEnvironment, MountDir_DoesntExist) {
_mountdir.remove();
// ON_CALL and not EXPECT_CALL, because this is a death test (i.e. it is forked) and gmock EXPECT_CALL in fork children don't report to parents.
ON_CALL(*console, askYesNo("Could not find mount directory. Do you want to create it?", testing::_)).WillByDefault(Return(false));
Test_Run_Error("mount directory not found", ErrorCode::InaccessibleMountDir);
}
TEST_P(CliTest_WrongEnvironment, MountDir_DoesntExist_Noninteractive) {
_mountdir.remove();
// We can't set an EXPECT_CALL().Times(0), because this is a death test (i.e. it is forked) and gmock EXPECT_CALL in fork children don't report to parents.
// So we set a default answer that shouldn't crash and check it's not called by checking that it crashes.
ON_CALL(*console, askYesNo("Could not find base directory. Do you want to create it?", testing::_)).WillByDefault(Return(true));
cpputils::setenv("CRYFS_FRONTEND", "noninteractive");
Test_Run_Error("mount directory not found", ErrorCode::InaccessibleMountDir);
cpputils::unsetenv("CRYFS_FRONTEND");
}
TEST_P(CliTest_WrongEnvironment, MountDir_DoesntExist_Create) {
if (!GetParam().runningInForeground) {return;} // TODO Make this work also if run in background (see CliTest::EXPECT_RUN_SUCCESS)
_mountdir.remove();
ON_CALL(*console, askYesNo("Could not find mount directory. Do you want to create it?", testing::_)).WillByDefault(Return(true));
Test_Run_Success();
EXPECT_TRUE(bf::exists(_mountdir.path()) && bf::is_directory(_mountdir.path()));
}
TEST_P(CliTest_WrongEnvironment, MountDir_IsNotDirectory) {
const TempFile mountdirfile;
mountdir = mountdirfile.path();
Test_Run_Error("Error 17: mount directory is not a directory", ErrorCode::InaccessibleMountDir);
}
TEST_P(CliTest_WrongEnvironment, MountDir_AllPermissions) {
if (!GetParam().runningInForeground) {return;} // TODO Make this work also if run in background (see CliTest::EXPECT_RUN_SUCCESS)
//Counter-Test. Test it doesn't fail if permissions are there.
SetAllPermissions(mountdir);
Test_Run_Success();
}
// boost::filesystem doesn't set permissions on Windows correctly
#if !defined(_MSC_VER)
TEST_P(CliTest_WrongEnvironment, MountDir_NoReadPermission) {
SetNoReadPermission(mountdir);
Test_Run_Error("Error 17: Could not read from mount directory", ErrorCode::InaccessibleMountDir);
}
TEST_P(CliTest_WrongEnvironment, MountDir_NoExePermission) {
SetNoExePermission(mountdir);
Test_Run_Error("Error 17: Could not write to mount directory", ErrorCode::InaccessibleMountDir);
}
TEST_P(CliTest_WrongEnvironment, MountDir_NoWritePermission) {
SetNoWritePermission(mountdir);
Test_Run_Error("Error 17: Could not write to mount directory", ErrorCode::InaccessibleMountDir);
}
TEST_P(CliTest_WrongEnvironment, MountDir_NoPermission) {
SetNoPermission(mountdir);
Test_Run_Error("Error 17: Could not write to mount directory", ErrorCode::InaccessibleMountDir);
}
#endif
|