From b7af7ca2f2297b166ee6297ed096410280f699b7 Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Thu, 29 Oct 2015 20:17:52 +0100 Subject: [PATCH] Add a sanity check that mountdir can't be equal to rootdir --- src/Cli.cpp | 7 +++++++ src/Cli.h | 1 + test/cli/CliTest_Setup.cpp | 6 +++--- test/cli/CliTest_ShowingHelp.cpp | 6 +++--- test/cli/CliTest_WrongEnvironment.cpp | 10 +++++++--- test/cli/testutils/CliTest.h | 6 +++--- 6 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/Cli.cpp b/src/Cli.cpp index b2ecfeb4..ef6aac12 100644 --- a/src/Cli.cpp +++ b/src/Cli.cpp @@ -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; diff --git a/src/Cli.h b/src/Cli.h index 6759aff4..29600af9 100644 --- a/src/Cli.h +++ b/src/Cli.h @@ -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); }; } diff --git a/test/cli/CliTest_Setup.cpp b/test/cli/CliTest_Setup.cpp index 61743bc6..4d7a65e3 100644 --- a/test/cli/CliTest_Setup.cpp +++ b/test/cli/CliTest_Setup.cpp @@ -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) } diff --git a/test/cli/CliTest_ShowingHelp.cpp b/test/cli/CliTest_ShowingHelp.cpp index 92233c67..c75c21e0 100644 --- a/test/cli/CliTest_ShowingHelp.cpp +++ b/test/cli/CliTest_ShowingHelp.cpp @@ -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()}); } diff --git a/test/cli/CliTest_WrongEnvironment.cpp b/test/cli/CliTest_WrongEnvironment.cpp index adefff78..8cfcc5f2 100644 --- a/test/cli/CliTest_WrongEnvironment.cpp +++ b/test/cli/CliTest_WrongEnvironment.cpp @@ -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 args() { - vector result = {basedir.path().c_str(), mountdir.path().c_str()}; + vector 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"); } diff --git a/test/cli/testutils/CliTest.h b/test/cli/testutils/CliTest.h index 49d306bb..71f103dc 100644 --- a/test/cli/testutils/CliTest.h +++ b/test/cli/testutils/CliTest.h @@ -9,10 +9,10 @@ class CliTest : public ::testing::Test { public: - CliTest(): basedir(), mountdir(), logfile(), configfile(false) {} + CliTest(): basedir(std::make_shared()), mountdir(std::make_shared()), logfile(), configfile(false) {} - cpputils::TempDir basedir; - cpputils::TempDir mountdir; + std::shared_ptr basedir; + std::shared_ptr mountdir; cpputils::TempFile logfile; cpputils::TempFile configfile;