Refactor CliTest_WrongEnvironment into a parametrized test

This commit is contained in:
Sebastian Messmer 2015-10-29 19:55:26 +01:00
parent a358ae859a
commit f42eefbc6b
2 changed files with 38 additions and 113 deletions

View File

@ -10,9 +10,17 @@
namespace bf = boost::filesystem; 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 //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<TestConfig> {
public: public:
void RemoveReadPermission(const bf::path &dir) { void RemoveReadPermission(const bf::path &dir) {
//TODO Take read permission from basedir in a better way //TODO Take read permission from basedir in a better way
@ -20,148 +28,65 @@ public:
} }
void Test_Run_Success() { void Test_Run_Success() {
EXPECT_RUN_SUCCESS( EXPECT_RUN_SUCCESS(args());
{basedir.path().c_str(), mountdir.path().c_str()}
);
} }
void Test_Run_Error(const char *expectedError) { void Test_Run_Error(const char *expectedError) {
EXPECT_RUN_ERROR( EXPECT_RUN_ERROR(
{basedir.path().c_str(), mountdir.path().c_str()}, args(),
expectedError expectedError
); );
} }
void Test_Run_LogIsNotStderr_Error(const char *expectedError) { vector<const char*> args() {
//Error message should be shown on stderr, even if a logfile is specified. vector<const char*> result = {basedir.path().c_str(), mountdir.path().c_str()};
EXPECT_RUN_ERROR( if (GetParam().externalConfigfile) {
{basedir.path().c_str(), mountdir.path().c_str(), "--logfile", logfile.path().c_str()}, result.push_back("--config");
expectedError 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) { INSTANTIATE_TEST_CASE_P(DefaultParams, CliTest_WrongEnvironment, Values(TestConfig({false, false})));
//Counter-Test. Test that it doesn't fail if we call it without an error condition. 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_Run_Success();
} }
TEST_F(CliTest_WrongEnvironment, BaseDir_DoesntExist) { TEST_P(CliTest_WrongEnvironment, BaseDir_DoesntExist) {
basedir.remove(); basedir.remove();
Test_Run_Error("Error: Base directory not found"); 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 //TODO finish the following test cases
/* /*
TEST_F(CliTest_WrongEnvironment, BaseDir_NoReadPermission) { TEST_P(CliTest_WrongEnvironment, BaseDir_NoReadPermission) {
RemoveReadPermission(basedir); RemoveReadPermission(basedir);
Test_Run_Error("Error: Base directory not readable"); Test_Run_Error("Error: Base directory not readable");
} }
TEST_F(CliTest_WrongEnvironment, BaseDir_NoReadPermission_LogIsNotStderr) { TEST_P(CliTest_WrongEnvironment, BaseDir_NoWritePermission) {
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) {
RemoveReadPermission(basedir); RemoveReadPermission(basedir);
Test_Run_Error("Error: Base directory not writeable"); Test_Run_Error("Error: Base directory not writeable");
} }
TEST_F(CliTest_WrongEnvironment, BaseDir_NoWritePermission_LogIsNotStderr) { TEST_P(CliTest_WrongEnvironment, BaseDir_NoAccessPermission) {
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) {
RemoveAccessPermission(basedir); RemoveAccessPermission(basedir);
Test_Run_Error("Error: Base directory not accessable"); Test_Run_Error("Error: Base directory not accessable");
} }
TEST_F(CliTest_WrongEnvironment, BaseDir_NoAccessPermission_LogIsNotStderr) { TEST_P(CliTest_WrongEnvironment, BaseDir_NoPermission) {
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) {
RemoveAllPermissions(basedir); RemoveAllPermissions(basedir);
Test_Run_Error("Error: Base directory not accessable"); 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");
}
*/ */

View File

@ -16,7 +16,7 @@ public:
cpputils::TempFile logfile; cpputils::TempFile logfile;
cpputils::TempFile configfile; cpputils::TempFile configfile;
void run(std::initializer_list<const char*> args) { void run(std::vector<const char*> args) {
std::vector<char*> _args; std::vector<char*> _args;
_args.reserve(args.size()+1); _args.reserve(args.size()+1);
_args.push_back(const_cast<char*>("cryfs")); _args.push_back(const_cast<char*>("cryfs"));
@ -26,11 +26,11 @@ public:
cryfs::Cli().main(_args.size(), _args.data()); cryfs::Cli().main(_args.size(), _args.data());
} }
void EXPECT_EXIT_WITH_HELP_MESSAGE(std::initializer_list<const char*> args) { void EXPECT_EXIT_WITH_HELP_MESSAGE(std::vector<const char*> args) {
EXPECT_RUN_ERROR(args, "Usage"); EXPECT_RUN_ERROR(args, "Usage");
} }
void EXPECT_RUN_ERROR(std::initializer_list<const char*> args, const char *message) { void EXPECT_RUN_ERROR(std::vector<const char*> args, const char *message) {
EXPECT_EXIT( EXPECT_EXIT(
run(args), run(args),
::testing::ExitedWithCode(1), ::testing::ExitedWithCode(1),
@ -38,7 +38,7 @@ public:
); );
} }
void EXPECT_RUN_SUCCESS(std::initializer_list<const char*> args) { void EXPECT_RUN_SUCCESS(std::vector<const char*> args) {
//TODO //TODO
/*EXPECT_EXIT( /*EXPECT_EXIT(
run(args), run(args),