From f42eefbc6b602137b2a67aa0515f77ee57bc38b6 Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Thu, 29 Oct 2015 19:55:26 +0100 Subject: [PATCH] Refactor CliTest_WrongEnvironment into a parametrized test --- test/cli/CliTest_WrongEnvironment.cpp | 143 ++++++-------------------- test/cli/testutils/CliTest.h | 8 +- 2 files changed, 38 insertions(+), 113 deletions(-) diff --git a/test/cli/CliTest_WrongEnvironment.cpp b/test/cli/CliTest_WrongEnvironment.cpp index 04772bd5..e7150901 100644 --- a/test/cli/CliTest_WrongEnvironment.cpp +++ b/test/cli/CliTest_WrongEnvironment.cpp @@ -10,9 +10,17 @@ namespace bf = boost::filesystem; +using ::testing::Values; +using ::testing::WithParamInterface; +using std::vector; + +struct TestConfig { + bool externalConfigfile; + bool logIsNotStderr; +}; //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 { +class CliTest_WrongEnvironment: public CliTest, public WithParamInterface { public: void RemoveReadPermission(const bf::path &dir) { //TODO Take read permission from basedir in a better way @@ -20,148 +28,65 @@ public: } void Test_Run_Success() { - EXPECT_RUN_SUCCESS( - {basedir.path().c_str(), mountdir.path().c_str()} - ); + EXPECT_RUN_SUCCESS(args()); } void Test_Run_Error(const char *expectedError) { EXPECT_RUN_ERROR( - {basedir.path().c_str(), mountdir.path().c_str()}, + args(), expectedError ); } - void Test_Run_LogIsNotStderr_Error(const char *expectedError) { - //Error message should be shown on stderr, even if a logfile is specified. - EXPECT_RUN_ERROR( - {basedir.path().c_str(), mountdir.path().c_str(), "--logfile", logfile.path().c_str()}, - expectedError - ); + vector args() { + vector result = {basedir.path().c_str(), mountdir.path().c_str()}; + if (GetParam().externalConfigfile) { + result.push_back("--config"); + result.push_back(configfile.path().c_str()); + } + if (GetParam().logIsNotStderr) { + result.push_back("--logfile"); + result.push_back(logfile.path().c_str()); + } + return result; } - - void Test_Run_ExternalConfigfile_Error(const char *expectedError) { - //Config file writing is one of the first things happening. This test case ensures that even if - //the config file is not written to the base directory, a wrong base directory is recognized correctly. - EXPECT_RUN_ERROR( - {basedir.path().c_str(), mountdir.path().c_str(), "--config", configfile.path().c_str()}, - expectedError - ); - } - - void Test_Run_ExternalConfigfile_LogIsNotStderr_Error(const char *expectedError) { - EXPECT_RUN_ERROR( - {basedir.path().c_str(), mountdir.path().c_str(), "--logfile", logfile.path().c_str(), "--config", configfile.path().c_str()}, - expectedError - ); - } }; -TEST_F(CliTest_WrongEnvironment, NoErrorCondition) { - //Counter-Test. Test that it doesn't fail if we call it without an error condition. +INSTANTIATE_TEST_CASE_P(DefaultParams, CliTest_WrongEnvironment, Values(TestConfig({false, false}))); +INSTANTIATE_TEST_CASE_P(ExternalConfigfile, CliTest_WrongEnvironment, Values(TestConfig({true, false}))); +INSTANTIATE_TEST_CASE_P(LogIsNotStderr, CliTest_WrongEnvironment, Values(TestConfig({false, true}))); +INSTANTIATE_TEST_CASE_P(ExternalConfigfile_LogIsNotStderr, CliTest_WrongEnvironment, Values(TestConfig({true, true}))); + +//Counter-Test. Test that it doesn't fail if we call it without an error condition. +TEST_P(CliTest_WrongEnvironment, NoErrorCondition) { Test_Run_Success(); } -TEST_F(CliTest_WrongEnvironment, BaseDir_DoesntExist) { +TEST_P(CliTest_WrongEnvironment, BaseDir_DoesntExist) { basedir.remove(); Test_Run_Error("Error: Base directory not found"); } -TEST_F(CliTest_WrongEnvironment, BaseDir_DoesntExist_LogIsNotStderr) { - basedir.remove(); - Test_Run_LogIsNotStderr_Error("Error: Base directory not found"); -} - -TEST_F(CliTest_WrongEnvironment, BaseDir_DoesntExist_ExternalConfigfile) { - basedir.remove(); - Test_Run_ExternalConfigfile_Error("Error: Base directory not found"); -} - -TEST_F(CliTest_WrongEnvironment, BaseDir_DoesntExist_ExternalConfigfile_LogIsNotStderr) { - basedir.remove(); - Test_Run_ExternalConfigfile_LogIsNotStderr_Error("Error: Base directory not found"); -} - //TODO finish the following test cases /* -TEST_F(CliTest_WrongEnvironment, BaseDir_NoReadPermission) { +TEST_P(CliTest_WrongEnvironment, BaseDir_NoReadPermission) { RemoveReadPermission(basedir); Test_Run_Error("Error: Base directory not readable"); } -TEST_F(CliTest_WrongEnvironment, BaseDir_NoReadPermission_LogIsNotStderr) { - RemoveReadPermission(basedir); - Test_Run_LogIsNotStderr_Error("Error: Base directory not readable"); -} - -TEST_F(CliTest_WrongEnvironment, BaseDir_NoReadPermission_ExternalConfigfile) { - RemoveReadPermission(basedir); - Test_Run_ExternalConfigfile_Error("Error: Base directory not readable"); -} - -TEST_F(CliTest_WrongEnvironment, BaseDir_NoReadPermission_ExternalConfigfile_LogIsNotStderr) { - RemoveReadPermission(basedir); - Test_Run_ExternalConfigfile_LogIsNotStderrError("Error: Base directory not readable"); -} - -TEST_F(CliTest_WrongEnvironment, BaseDir_NoWritePermission) { +TEST_P(CliTest_WrongEnvironment, BaseDir_NoWritePermission) { RemoveReadPermission(basedir); Test_Run_Error("Error: Base directory not writeable"); } -TEST_F(CliTest_WrongEnvironment, BaseDir_NoWritePermission_LogIsNotStderr) { - RemoveReadPermission(basedir); - Test_Run_LogIsNotStderr_Error("Error: Base directory not writeable"); -} - -TEST_F(CliTest_WrongEnvironment, BaseDir_NoWritePermission_ExternalConfigfile) { - RemoveReadPermission(basedir); - Test_Run_ExternalConfigfile_Error("Error: Base directory not writeable"); -} - -TEST_F(CliTest_WrongEnvironment, BaseDir_NoWritePermission_ExternalConfigfile_LogIsNotStderr) { - RemoveReadPermission(basedir); - Test_Run_ExternalConfigfile_LogIsNotStderrError("Error: Base directory not writeable"); -} - -TEST_F(CliTest_WrongEnvironment, BaseDir_NoAccessPermission) { +TEST_P(CliTest_WrongEnvironment, BaseDir_NoAccessPermission) { RemoveAccessPermission(basedir); Test_Run_Error("Error: Base directory not accessable"); } -TEST_F(CliTest_WrongEnvironment, BaseDir_NoAccessPermission_LogIsNotStderr) { - RemoveAccessPermission(basedir); - Test_Run_LogIsNotStderr_Error("Error: Base directory not accessable"); -} - -TEST_F(CliTest_WrongEnvironment, BaseDir_NoAccessPermission_ExternalConfigfile) { - RemoveAccessPermission(basedir); - Test_Run_ExternalConfigfile_Error("Error: Base directory not accessable"); -} - -TEST_F(CliTest_WrongEnvironment, BaseDir_NoAccessPermission_ExternalConfigfile_LogIsNotStderr) { - RemoveAccessPermission(basedir); - Test_Run_ExternalConfigfile_LogIsNotStderrError("Error: Base directory not accessable"); -} - -TEST_F(CliTest_WrongEnvironment, BaseDir_NoPermission) { +TEST_P(CliTest_WrongEnvironment, BaseDir_NoPermission) { RemoveAllPermissions(basedir); Test_Run_Error("Error: Base directory not accessable"); } -TEST_F(CliTest_WrongEnvironment, BaseDir_NoPermission_LogIsNotStderr) { - RemoveAllPermissions(basedir); - Test_Run_LogIsNotStderr_Error("Error: Base directory not accessable"); -} - -TEST_F(CliTest_WrongEnvironment, BaseDir_NoPermission_ExternalConfigfile) { - RemoveAllPermissions(basedir); - Test_Run_ExternalConfigfile_Error("Error: Base directory not accessable"); -} - -TEST_F(CliTest_WrongEnvironment, BaseDir_NoPermission_ExternalConfigfile_LogIsNotStderr) { - RemoveAllPermissions(basedir); - Test_Run_ExternalConfigfile_LogIsNotStderrError("Error: Base directory not accessable"); -} - */ diff --git a/test/cli/testutils/CliTest.h b/test/cli/testutils/CliTest.h index 81d389c6..49d306bb 100644 --- a/test/cli/testutils/CliTest.h +++ b/test/cli/testutils/CliTest.h @@ -16,7 +16,7 @@ public: cpputils::TempFile logfile; cpputils::TempFile configfile; - void run(std::initializer_list args) { + void run(std::vector args) { std::vector _args; _args.reserve(args.size()+1); _args.push_back(const_cast("cryfs")); @@ -26,11 +26,11 @@ public: cryfs::Cli().main(_args.size(), _args.data()); } - void EXPECT_EXIT_WITH_HELP_MESSAGE(std::initializer_list args) { + void EXPECT_EXIT_WITH_HELP_MESSAGE(std::vector args) { EXPECT_RUN_ERROR(args, "Usage"); } - void EXPECT_RUN_ERROR(std::initializer_list args, const char *message) { + void EXPECT_RUN_ERROR(std::vector args, const char *message) { EXPECT_EXIT( run(args), ::testing::ExitedWithCode(1), @@ -38,7 +38,7 @@ public: ); } - void EXPECT_RUN_SUCCESS(std::initializer_list args) { + void EXPECT_RUN_SUCCESS(std::vector args) { //TODO /*EXPECT_EXIT( run(args),