Refactor Cli test cases

This commit is contained in:
Sebastian Messmer 2015-10-29 19:34:36 +01:00
parent cdf0025b15
commit a358ae859a
5 changed files with 150 additions and 117 deletions

View File

@ -0,0 +1,29 @@
#include "testutils/CliTest.h"
using cpputils::TempFile;
//Tests that cryfs is correctly setup according to the CLI parameters specified
using CliTest_Setup = CliTest;
TEST_F(CliTest_Setup, NoSpecialOptions) {
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()});
//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()});
//TODO Expect logfile is used (check logfile content)
}
TEST_F(CliTest_Setup, ConfigfileGiven) {
EXPECT_RUN_SUCCESS({"/home/user/baseDir", "--config", configfile.path().c_str(), "/home/user/mountDir"});
}
TEST_F(CliTest_Setup, FuseOptionGiven) {
EXPECT_RUN_SUCCESS({"/home/user/baseDir", "/home/user/mountDir", "--", "-f"});
}

View File

@ -0,0 +1,27 @@
#include "testutils/CliTest.h"
using CliTest_ShowingHelp = CliTest;
TEST_F(CliTest_ShowingHelp, HelpLongOption) {
EXPECT_EXIT_WITH_HELP_MESSAGE({"--help"});
}
TEST_F(CliTest_ShowingHelp, HelpLongOptionTogetherWithOtherOptions) {
EXPECT_EXIT_WITH_HELP_MESSAGE({"/", "/mountdir", "--help"});
}
TEST_F(CliTest_ShowingHelp, HelpShortOption) {
EXPECT_EXIT_WITH_HELP_MESSAGE({"-h"});
}
TEST_F(CliTest_ShowingHelp, HelpShortOptionTogetherWithOtherOptions) {
EXPECT_EXIT_WITH_HELP_MESSAGE({"/", "/mountdir", "-h"});
}
TEST_F(CliTest_ShowingHelp, MissingAllOptions) {
EXPECT_EXIT_WITH_HELP_MESSAGE({});
}
TEST_F(CliTest_ShowingHelp, MissingDir) {
EXPECT_EXIT_WITH_HELP_MESSAGE({"/"});
}

View File

@ -1,13 +1,4 @@
#include <google/gtest/gtest.h>
#include "../../src/Cli.h"
#include <messmer/cpp-utils/tempfile/TempDir.h>
#include <messmer/cpp-utils/tempfile/TempFile.h>
using ::testing::Test;
using ::testing::ExitedWithCode;
using cpputils::TempDir;
using cpputils::TempFile;
namespace bf = boost::filesystem;
#include "testutils/CliTest.h"
//TODO Test CLI ends with error message (before daemonization), if
// - mountdir does not exist
@ -17,50 +8,21 @@ namespace bf = boost::filesystem;
//TODO Test what happens if basedir == mountdir
//TODO Test all stuff (basedir missing, not readable, not writeable, not accessible, ...) also with "-f" foreground flag.
class CliTest : public Test {
namespace bf = boost::filesystem;
//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:
CliTest(): basedir(), mountdir(), logfile(), configfile(false) {}
TempDir basedir;
TempDir mountdir;
TempFile logfile;
TempFile configfile;
void run(std::initializer_list<const char*> args) {
std::vector<char*> _args;
_args.reserve(args.size()+1);
_args.push_back(const_cast<char*>("cryfs"));
for (const char *arg : args) {
_args.push_back(const_cast<char*>(arg));
}
cryfs::Cli().main(_args.size(), _args.data());
}
void EXPECT_EXIT_WITH_HELP_MESSAGE(std::initializer_list<const char*> args) {
EXPECT_RUN_ERROR(args, "Usage");
}
void EXPECT_RUN_ERROR(std::initializer_list<const char*> args, const char *message) {
EXPECT_EXIT(
run(args),
ExitedWithCode(1),
message
);
}
void EXPECT_RUN_SUCCESS(std::initializer_list<const char*> args) {
//TODO
/*EXPECT_EXIT(
run(args),
ExitedWithCode(0),
"Filesystem is running"
);*/
//TODO Then stop running cryfs process again
}
void RemoveReadPermission(const bf::path &dir) {
//TODO Take read permission from basedir in a better way
system((string("chmod -rwx ")+basedir.path().c_str()).c_str());
system((std::string("chmod -rwx ")+dir.c_str()).c_str());
}
void Test_Run_Success() {
EXPECT_RUN_SUCCESS(
{basedir.path().c_str(), mountdir.path().c_str()}
);
}
void Test_Run_Error(const char *expectedError) {
@ -92,154 +54,112 @@ public:
{basedir.path().c_str(), mountdir.path().c_str(), "--logfile", logfile.path().c_str(), "--config", configfile.path().c_str()},
expectedError
);
}
}
};
TEST_F(CliTest, HelpLongOption) {
EXPECT_EXIT_WITH_HELP_MESSAGE({"--help"});
TEST_F(CliTest_WrongEnvironment, NoErrorCondition) {
//Counter-Test. Test that it doesn't fail if we call it without an error condition.
Test_Run_Success();
}
TEST_F(CliTest, HelpLongOptionTogetherWithOtherOptions) {
EXPECT_EXIT_WITH_HELP_MESSAGE({"/", "/mountdir", "--help"});
}
TEST_F(CliTest, HelpShortOption) {
EXPECT_EXIT_WITH_HELP_MESSAGE({"-h"});
}
TEST_F(CliTest, HelpShortOptionTogetherWithOtherOptions) {
EXPECT_EXIT_WITH_HELP_MESSAGE({"/", "/mountdir", "-h"});
}
TEST_F(CliTest, MissingAllOptions) {
EXPECT_EXIT_WITH_HELP_MESSAGE({});
}
TEST_F(CliTest, MissingDir) {
EXPECT_EXIT_WITH_HELP_MESSAGE({"/"});
}
TEST_F(CliTest, NoSpecialOptions) {
EXPECT_RUN_SUCCESS({basedir.path().c_str(), mountdir.path().c_str()});
}
TEST_F(CliTest, NotexistingLogfileGiven) {
TempFile notexisting_logfile(false);
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, ExistingLogfileGiven) {
EXPECT_RUN_SUCCESS({basedir.path().c_str(), mountdir.path().c_str(), "--logfile", logfile.path().c_str()});
//TODO Expect logfile is used (check logfile content)
}
TEST_F(CliTest, ConfigfileGiven) {
EXPECT_RUN_SUCCESS({"/home/user/baseDir", "--config", configfile.path().c_str(), "/home/user/mountDir"});
}
TEST_F(CliTest, FuseOptionGiven) {
EXPECT_RUN_SUCCESS({"/home/user/baseDir", "/home/user/mountDir", "--", "-f"});
}
TEST_F(CliTest, BaseDir_DoesntExist) {
TEST_F(CliTest_WrongEnvironment, BaseDir_DoesntExist) {
basedir.remove();
Test_Run_Error("Error: Base directory not found");
}
TEST_F(CliTest, BaseDir_DoesntExist_LogIsNotStderr) {
TEST_F(CliTest_WrongEnvironment, BaseDir_DoesntExist_LogIsNotStderr) {
basedir.remove();
Test_Run_LogIsNotStderr_Error("Error: Base directory not found");
}
TEST_F(CliTest, BaseDir_DoesntExist_ExternalConfigfile) {
TEST_F(CliTest_WrongEnvironment, BaseDir_DoesntExist_ExternalConfigfile) {
basedir.remove();
Test_Run_ExternalConfigfile_Error("Error: Base directory not found");
}
TEST_F(CliTest, BaseDir_DoesntExist_ExternalConfigfile_LogIsNotStderr) {
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, BaseDir_NoReadPermission) {
TEST_F(CliTest_WrongEnvironment, BaseDir_NoReadPermission) {
RemoveReadPermission(basedir);
Test_Run_Error("Error: Base directory not readable");
}
TEST_F(CliTest, BaseDir_NoReadPermission_LogIsNotStderr) {
TEST_F(CliTest_WrongEnvironment, BaseDir_NoReadPermission_LogIsNotStderr) {
RemoveReadPermission(basedir);
Test_Run_LogIsNotStderr_Error("Error: Base directory not readable");
}
TEST_F(CliTest, BaseDir_NoReadPermission_ExternalConfigfile) {
TEST_F(CliTest_WrongEnvironment, BaseDir_NoReadPermission_ExternalConfigfile) {
RemoveReadPermission(basedir);
Test_Run_ExternalConfigfile_Error("Error: Base directory not readable");
}
TEST_F(CliTest, BaseDir_NoReadPermission_ExternalConfigfile_LogIsNotStderr) {
TEST_F(CliTest_WrongEnvironment, BaseDir_NoReadPermission_ExternalConfigfile_LogIsNotStderr) {
RemoveReadPermission(basedir);
Test_Run_ExternalConfigfile_LogIsNotStderrError("Error: Base directory not readable");
}
TEST_F(CliTest, BaseDir_NoWritePermission) {
TEST_F(CliTest_WrongEnvironment, BaseDir_NoWritePermission) {
RemoveReadPermission(basedir);
Test_Run_Error("Error: Base directory not writeable");
}
TEST_F(CliTest, BaseDir_NoWritePermission_LogIsNotStderr) {
TEST_F(CliTest_WrongEnvironment, BaseDir_NoWritePermission_LogIsNotStderr) {
RemoveReadPermission(basedir);
Test_Run_LogIsNotStderr_Error("Error: Base directory not writeable");
}
TEST_F(CliTest, BaseDir_NoWritePermission_ExternalConfigfile) {
TEST_F(CliTest_WrongEnvironment, BaseDir_NoWritePermission_ExternalConfigfile) {
RemoveReadPermission(basedir);
Test_Run_ExternalConfigfile_Error("Error: Base directory not writeable");
}
TEST_F(CliTest, BaseDir_NoWritePermission_ExternalConfigfile_LogIsNotStderr) {
TEST_F(CliTest_WrongEnvironment, BaseDir_NoWritePermission_ExternalConfigfile_LogIsNotStderr) {
RemoveReadPermission(basedir);
Test_Run_ExternalConfigfile_LogIsNotStderrError("Error: Base directory not writeable");
}
TEST_F(CliTest, BaseDir_NoAccessPermission) {
TEST_F(CliTest_WrongEnvironment, BaseDir_NoAccessPermission) {
RemoveAccessPermission(basedir);
Test_Run_Error("Error: Base directory not accessable");
}
TEST_F(CliTest, BaseDir_NoAccessPermission_LogIsNotStderr) {
TEST_F(CliTest_WrongEnvironment, BaseDir_NoAccessPermission_LogIsNotStderr) {
RemoveAccessPermission(basedir);
Test_Run_LogIsNotStderr_Error("Error: Base directory not accessable");
}
TEST_F(CliTest, BaseDir_NoAccessPermission_ExternalConfigfile) {
TEST_F(CliTest_WrongEnvironment, BaseDir_NoAccessPermission_ExternalConfigfile) {
RemoveAccessPermission(basedir);
Test_Run_ExternalConfigfile_Error("Error: Base directory not accessable");
}
TEST_F(CliTest, BaseDir_NoAccessPermission_ExternalConfigfile_LogIsNotStderr) {
TEST_F(CliTest_WrongEnvironment, BaseDir_NoAccessPermission_ExternalConfigfile_LogIsNotStderr) {
RemoveAccessPermission(basedir);
Test_Run_ExternalConfigfile_LogIsNotStderrError("Error: Base directory not accessable");
}
TEST_F(CliTest, BaseDir_NoPermission) {
TEST_F(CliTest_WrongEnvironment, BaseDir_NoPermission) {
RemoveAllPermissions(basedir);
Test_Run_Error("Error: Base directory not accessable");
}
TEST_F(CliTest, BaseDir_NoPermission_LogIsNotStderr) {
TEST_F(CliTest_WrongEnvironment, BaseDir_NoPermission_LogIsNotStderr) {
RemoveAllPermissions(basedir);
Test_Run_LogIsNotStderr_Error("Error: Base directory not accessable");
}
TEST_F(CliTest, BaseDir_NoPermission_ExternalConfigfile) {
TEST_F(CliTest_WrongEnvironment, BaseDir_NoPermission_ExternalConfigfile) {
RemoveAllPermissions(basedir);
Test_Run_ExternalConfigfile_Error("Error: Base directory not accessable");
}
TEST_F(CliTest, BaseDir_NoPermission_ExternalConfigfile_LogIsNotStderr) {
TEST_F(CliTest_WrongEnvironment, BaseDir_NoPermission_ExternalConfigfile_LogIsNotStderr) {
RemoveAllPermissions(basedir);
Test_Run_ExternalConfigfile_LogIsNotStderrError("Error: Base directory not accessable");
}

View File

@ -0,0 +1,5 @@
//
// Created by heinzi on 29.10.15.
//
#include "CliTest.h"

View File

@ -0,0 +1,52 @@
#pragma once
#ifndef MESSMER_CRYFS_TEST_CLI_TESTUTILS_CLITEST_H
#define MESSMER_CRYFS_TEST_CLI_TESTUTILS_CLITEST_H
#include <google/gtest/gtest.h>
#include <messmer/cpp-utils/tempfile/TempDir.h>
#include <messmer/cpp-utils/tempfile/TempFile.h>
#include "../../../src/Cli.h"
class CliTest : public ::testing::Test {
public:
CliTest(): basedir(), mountdir(), logfile(), configfile(false) {}
cpputils::TempDir basedir;
cpputils::TempDir mountdir;
cpputils::TempFile logfile;
cpputils::TempFile configfile;
void run(std::initializer_list<const char*> args) {
std::vector<char*> _args;
_args.reserve(args.size()+1);
_args.push_back(const_cast<char*>("cryfs"));
for (const char *arg : args) {
_args.push_back(const_cast<char*>(arg));
}
cryfs::Cli().main(_args.size(), _args.data());
}
void EXPECT_EXIT_WITH_HELP_MESSAGE(std::initializer_list<const char*> args) {
EXPECT_RUN_ERROR(args, "Usage");
}
void EXPECT_RUN_ERROR(std::initializer_list<const char*> args, const char *message) {
EXPECT_EXIT(
run(args),
::testing::ExitedWithCode(1),
message
);
}
void EXPECT_RUN_SUCCESS(std::initializer_list<const char*> args) {
//TODO
/*EXPECT_EXIT(
run(args),
::testing::ExitedWithCode(0),
"Filesystem is running"
);*/
//TODO Then stop running cryfs process again
}
};
#endif