Add a sanity check that mountdir can't be equal to rootdir

This commit is contained in:
Sebastian Messmer 2015-10-29 20:17:52 +01:00
parent c572e7f353
commit b7af7ca2f2
6 changed files with 24 additions and 12 deletions

View File

@ -143,6 +143,12 @@ namespace cryfs {
}
}
void Cli::_sanityChecks(const ProgramOptions &options) {
if (bf::path(options.baseDir()) == bf::path(options.mountDir())) {
throw std::runtime_error("Can't mount into base directory");
}
}
int Cli::main(int argc, char *argv[]) {
cpputils::showBacktraceOnSigSegv();
_showVersion();
@ -150,6 +156,7 @@ namespace cryfs {
ProgramOptions options = program_options::Parser(argc, argv).parse();
try {
_sanityChecks(options);
_runFilesystem(options);
} catch (const std::runtime_error &e) {
std::cerr << "Error: " << e.what() << std::endl;

View File

@ -20,6 +20,7 @@ namespace cryfs {
static bool _checkPassword(const std::string &password);
static void _showVersion();
static void _initLogfile(const program_options::ProgramOptions &options);
static void _sanityChecks(const program_options::ProgramOptions &options);
};
}

View File

@ -6,17 +6,17 @@ using cpputils::TempFile;
using CliTest_Setup = CliTest;
TEST_F(CliTest_Setup, NoSpecialOptions) {
EXPECT_RUN_SUCCESS({basedir.path().c_str(), mountdir.path().c_str()});
EXPECT_RUN_SUCCESS({basedir->path().c_str(), mountdir->path().c_str()});
}
TEST_F(CliTest_Setup, NotexistingLogfileGiven) {
TempFile notexisting_logfile(false);
EXPECT_RUN_SUCCESS({basedir.path().c_str(), mountdir.path().c_str(), "--logfile", notexisting_logfile.path().c_str()});
EXPECT_RUN_SUCCESS({basedir->path().c_str(), mountdir->path().c_str(), "--logfile", notexisting_logfile.path().c_str()});
//TODO Expect logfile is used (check logfile content)
}
TEST_F(CliTest_Setup, ExistingLogfileGiven) {
EXPECT_RUN_SUCCESS({basedir.path().c_str(), mountdir.path().c_str(), "--logfile", logfile.path().c_str()});
EXPECT_RUN_SUCCESS({basedir->path().c_str(), mountdir->path().c_str(), "--logfile", logfile.path().c_str()});
//TODO Expect logfile is used (check logfile content)
}

View File

@ -7,7 +7,7 @@ TEST_F(CliTest_ShowingHelp, HelpLongOption) {
}
TEST_F(CliTest_ShowingHelp, HelpLongOptionTogetherWithOtherOptions) {
EXPECT_EXIT_WITH_HELP_MESSAGE({"/", "/mountdir", "--help"});
EXPECT_EXIT_WITH_HELP_MESSAGE({basedir->path().c_str(), mountdir->path().c_str(), "--help"});
}
TEST_F(CliTest_ShowingHelp, HelpShortOption) {
@ -15,7 +15,7 @@ TEST_F(CliTest_ShowingHelp, HelpShortOption) {
}
TEST_F(CliTest_ShowingHelp, HelpShortOptionTogetherWithOtherOptions) {
EXPECT_EXIT_WITH_HELP_MESSAGE({"/", "/mountdir", "-h"});
EXPECT_EXIT_WITH_HELP_MESSAGE({basedir->path().c_str(), mountdir->path().c_str(), "-h"});
}
TEST_F(CliTest_ShowingHelp, MissingAllOptions) {
@ -23,5 +23,5 @@ TEST_F(CliTest_ShowingHelp, MissingAllOptions) {
}
TEST_F(CliTest_ShowingHelp, MissingDir) {
EXPECT_EXIT_WITH_HELP_MESSAGE({"/"});
EXPECT_EXIT_WITH_HELP_MESSAGE({basedir->path().c_str()});
}

View File

@ -5,7 +5,6 @@
// - mountdir exists but belongs to other user
// - mountdir exists but is missing permissions
// - TODO when else is libfuse failing? What requirements are there for the mountdir?)
//TODO Test what happens if basedir == mountdir
namespace bf = boost::filesystem;
@ -39,7 +38,7 @@ public:
}
vector<const char*> args() {
vector<const char*> result = {basedir.path().c_str(), mountdir.path().c_str()};
vector<const char*> result = {basedir->path().c_str(), mountdir->path().c_str()};
if (GetParam().externalConfigfile) {
result.push_back("--config");
result.push_back(configfile.path().c_str());
@ -69,8 +68,13 @@ TEST_P(CliTest_WrongEnvironment, NoErrorCondition) {
Test_Run_Success();
}
TEST_P(CliTest_WrongEnvironment, MountDirIsBaseDir) {
mountdir = basedir;
Test_Run_Error("Error: Can't mount into base directory");
}
TEST_P(CliTest_WrongEnvironment, BaseDir_DoesntExist) {
basedir.remove();
basedir->remove();
Test_Run_Error("Error: Base directory not found");
}

View File

@ -9,10 +9,10 @@
class CliTest : public ::testing::Test {
public:
CliTest(): basedir(), mountdir(), logfile(), configfile(false) {}
CliTest(): basedir(std::make_shared<cpputils::TempDir>()), mountdir(std::make_shared<cpputils::TempDir>()), logfile(), configfile(false) {}
cpputils::TempDir basedir;
cpputils::TempDir mountdir;
std::shared_ptr<cpputils::TempDir> basedir;
std::shared_ptr<cpputils::TempDir> mountdir;
cpputils::TempFile logfile;
cpputils::TempFile configfile;