2015-09-29 14:29:10 +02:00
|
|
|
#include "testutils/ProgramOptionsTestBase.h"
|
2016-02-23 21:07:23 +01:00
|
|
|
#include <cryfs-cli/program_options/Parser.h>
|
2016-02-11 16:39:42 +01:00
|
|
|
#include <cryfs/config/CryCipher.h>
|
2016-06-28 10:37:08 +02:00
|
|
|
#include <cpp-utils/pointer/unique_ref_boost_optional_gtest_workaround.h>
|
2018-02-01 19:21:47 +01:00
|
|
|
#include <gitversion/gitversion.h>
|
2018-02-02 01:08:01 +01:00
|
|
|
#include <cryfs/CryfsException.h>
|
|
|
|
#include <cpp-utils/testutils/CaptureStderrRAII.h>
|
2015-09-29 14:29:10 +02:00
|
|
|
|
2015-10-30 19:53:15 +01:00
|
|
|
using namespace cryfs;
|
2015-09-29 14:29:10 +02:00
|
|
|
using namespace cryfs::program_options;
|
|
|
|
using std::vector;
|
2016-05-10 01:07:02 +02:00
|
|
|
using std::string;
|
2015-10-24 19:56:05 +02:00
|
|
|
using boost::none;
|
2015-11-18 11:01:48 +01:00
|
|
|
namespace bf = boost::filesystem;
|
2018-02-02 01:08:01 +01:00
|
|
|
using cpputils::CaptureStderrRAII;
|
2015-09-29 14:29:10 +02:00
|
|
|
|
2018-09-07 14:00:47 +02:00
|
|
|
#if !defined(_MSC_VER)
|
|
|
|
constexpr const char *basedir = "/home/user/baseDir";
|
|
|
|
constexpr const char *mountdir = "/home/user/mountDir";
|
|
|
|
constexpr const char *logfile = "/home/user/logfile";
|
|
|
|
constexpr const char *configfile = "/home/user/configfile";
|
|
|
|
#else
|
|
|
|
constexpr const char *basedir = "C:\\basedir";
|
|
|
|
constexpr const char *mountdir = "C:\\mountdir";
|
|
|
|
constexpr const char *logfile = "C:\\logfile";
|
|
|
|
constexpr const char *configfile = "C:\\configfile";
|
|
|
|
#endif
|
|
|
|
|
2015-09-29 14:29:10 +02:00
|
|
|
class ProgramOptionsParserTest: public ProgramOptionsTestBase {
|
|
|
|
public:
|
|
|
|
ProgramOptions parse(std::initializer_list<const char*> options) {
|
2016-05-10 01:07:02 +02:00
|
|
|
vector<const char*> _options = options;
|
2015-10-30 19:53:15 +01:00
|
|
|
return Parser(_options.size(), _options.data()).parse(CryCiphers::supportedCipherNames());
|
2015-09-29 14:29:10 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-10-28 15:27:30 +01:00
|
|
|
TEST_F(ProgramOptionsParserTest, MissingAllOptions) {
|
2018-02-02 01:08:01 +01:00
|
|
|
CaptureStderrRAII captureStderr;
|
|
|
|
try {
|
|
|
|
parse({"./myExecutable"});
|
|
|
|
EXPECT_TRUE(false); // expect throws
|
|
|
|
} catch (const CryfsException& e) {
|
|
|
|
EXPECT_EQ(ErrorCode::InvalidArguments, e.errorCode());
|
|
|
|
captureStderr.EXPECT_MATCHES("Usage:"); // expect show usage information
|
|
|
|
}
|
2015-09-29 14:29:10 +02:00
|
|
|
}
|
|
|
|
|
2015-10-28 15:27:30 +01:00
|
|
|
TEST_F(ProgramOptionsParserTest, MissingDir) {
|
2018-02-02 01:08:01 +01:00
|
|
|
CaptureStderrRAII captureStderr;
|
|
|
|
try {
|
2018-09-07 14:00:47 +02:00
|
|
|
parse({"./myExecutable", basedir});
|
2018-02-02 01:08:01 +01:00
|
|
|
EXPECT_TRUE(false); // expect throw
|
|
|
|
} catch (const CryfsException& e) {
|
|
|
|
EXPECT_EQ(ErrorCode::InvalidArguments, e.errorCode());
|
|
|
|
captureStderr.EXPECT_MATCHES("Usage:"); // expect show usage information
|
|
|
|
}
|
2015-09-29 14:29:10 +02:00
|
|
|
}
|
|
|
|
|
2015-10-28 15:27:30 +01:00
|
|
|
TEST_F(ProgramOptionsParserTest, HelpLongOption) {
|
2018-02-02 01:08:01 +01:00
|
|
|
CaptureStderrRAII captureStderr;
|
|
|
|
try {
|
|
|
|
parse({"./myExecutable", "--help"});
|
|
|
|
EXPECT_TRUE(false); // expect throw
|
|
|
|
} catch (const CryfsException& e) {
|
|
|
|
EXPECT_EQ(ErrorCode::Success, e.errorCode());
|
|
|
|
captureStderr.EXPECT_MATCHES("Usage:"); // expect show usage information
|
|
|
|
}
|
2015-09-29 14:29:10 +02:00
|
|
|
}
|
|
|
|
|
2015-10-28 15:27:30 +01:00
|
|
|
TEST_F(ProgramOptionsParserTest, HelpShortOption) {
|
2018-02-02 01:08:01 +01:00
|
|
|
CaptureStderrRAII captureStderr;
|
|
|
|
try {
|
|
|
|
parse({"./myExecutable", "-h"});
|
|
|
|
EXPECT_TRUE(false); // expect throw
|
|
|
|
} catch (const CryfsException& e) {
|
|
|
|
EXPECT_EQ(ErrorCode::Success, e.errorCode());
|
|
|
|
captureStderr.EXPECT_MATCHES("Usage:"); // expect show usage information
|
|
|
|
}
|
2015-10-30 19:53:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ProgramOptionsParserTest, ShowCiphers) {
|
2018-02-02 01:08:01 +01:00
|
|
|
CaptureStderrRAII captureStderr;
|
|
|
|
try {
|
|
|
|
parse({"./myExecutable", "--show-ciphers"});
|
|
|
|
EXPECT_TRUE(false); // expect throw
|
|
|
|
} catch (const CryfsException& e) {
|
|
|
|
EXPECT_EQ(ErrorCode::Success, e.errorCode());
|
|
|
|
captureStderr.EXPECT_MATCHES("aes-256-gcm"); // expect show ciphers
|
|
|
|
}
|
2015-09-29 14:29:10 +02:00
|
|
|
}
|
|
|
|
|
2016-06-27 07:15:43 +02:00
|
|
|
TEST_F(ProgramOptionsParserTest, BaseDir_Absolute) {
|
2018-09-07 14:00:47 +02:00
|
|
|
ProgramOptions options = parse({"./myExecutable", basedir, mountdir});
|
|
|
|
EXPECT_EQ(basedir, options.baseDir());
|
2015-09-29 14:29:10 +02:00
|
|
|
}
|
|
|
|
|
2016-06-27 07:15:43 +02:00
|
|
|
TEST_F(ProgramOptionsParserTest, Basedir_Relative) {
|
2018-09-07 14:00:47 +02:00
|
|
|
ProgramOptions options = parse({"./myExecutable", "baseDir", mountdir});
|
2015-11-18 11:01:48 +01:00
|
|
|
EXPECT_EQ(bf::current_path() / "baseDir", options.baseDir());
|
|
|
|
}
|
|
|
|
|
2016-06-27 07:15:43 +02:00
|
|
|
TEST_F(ProgramOptionsParserTest, MountDir_Absolute) {
|
2018-09-07 14:00:47 +02:00
|
|
|
ProgramOptions options = parse({"./myExecutable", basedir, mountdir});
|
|
|
|
EXPECT_EQ(mountdir, options.mountDir());
|
2016-06-27 07:15:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ProgramOptionsParserTest, MountDir_Relative) {
|
2018-09-07 14:00:47 +02:00
|
|
|
ProgramOptions options = parse({"./myExecutable", basedir, "mountDir"});
|
2015-11-18 11:01:48 +01:00
|
|
|
EXPECT_EQ(bf::current_path() / "mountDir", options.mountDir());
|
|
|
|
}
|
|
|
|
|
2018-02-01 18:45:31 +01:00
|
|
|
TEST_F(ProgramOptionsParserTest, Foreground_False) {
|
2018-09-07 14:00:47 +02:00
|
|
|
ProgramOptions options = parse({"./myExecutable", basedir, "mountdir"});
|
2018-02-01 18:45:31 +01:00
|
|
|
EXPECT_FALSE(options.foreground());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ProgramOptionsParserTest, Foreground_True) {
|
2018-09-07 14:00:47 +02:00
|
|
|
ProgramOptions options = parse({"./myExecutable", "-f", basedir, "mountdir"});
|
2018-02-01 18:45:31 +01:00
|
|
|
EXPECT_TRUE(options.foreground());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ProgramOptionsParserTest, AllowFilesystemUpgrade_False) {
|
2018-09-07 14:00:47 +02:00
|
|
|
ProgramOptions options = parse({"./myExecutable", basedir, "mountdir"});
|
2018-02-01 18:45:31 +01:00
|
|
|
EXPECT_FALSE(options.allowFilesystemUpgrade());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ProgramOptionsParserTest, AllowFilesystemUpgrade_True) {
|
2018-09-07 14:00:47 +02:00
|
|
|
ProgramOptions options = parse({"./myExecutable", "--allow-filesystem-upgrade", basedir, "mountdir"});
|
2018-02-01 18:45:31 +01:00
|
|
|
EXPECT_TRUE(options.allowFilesystemUpgrade());
|
|
|
|
}
|
|
|
|
|
2015-10-24 19:56:05 +02:00
|
|
|
TEST_F(ProgramOptionsParserTest, LogfileGiven) {
|
2018-09-07 14:00:47 +02:00
|
|
|
ProgramOptions options = parse({"./myExecutable", basedir, "--logfile", logfile, mountdir});
|
|
|
|
EXPECT_EQ(logfile, options.logFile().value());
|
2015-10-24 19:56:05 +02:00
|
|
|
}
|
|
|
|
|
2015-11-18 11:01:48 +01:00
|
|
|
TEST_F(ProgramOptionsParserTest, LogfileGiven_RelativePath) {
|
2018-09-07 14:00:47 +02:00
|
|
|
ProgramOptions options = parse({"./myExecutable", basedir, "--logfile", "mylogfile", mountdir});
|
2015-11-18 11:01:48 +01:00
|
|
|
EXPECT_EQ(bf::current_path() / "mylogfile", options.logFile().value());
|
|
|
|
}
|
|
|
|
|
2016-06-27 07:15:43 +02:00
|
|
|
TEST_F(ProgramOptionsParserTest, LogfileNotGiven) {
|
2018-09-07 14:00:47 +02:00
|
|
|
ProgramOptions options = parse({"./myExecutable", basedir, mountdir});
|
2016-06-27 07:15:43 +02:00
|
|
|
EXPECT_EQ(none, options.logFile());
|
|
|
|
}
|
|
|
|
|
2015-10-24 19:56:05 +02:00
|
|
|
TEST_F(ProgramOptionsParserTest, ConfigfileGiven) {
|
2018-09-07 14:00:47 +02:00
|
|
|
ProgramOptions options = parse({"./myExecutable", basedir, "--config", configfile, mountdir});
|
|
|
|
EXPECT_EQ(configfile, options.configFile().value());
|
2015-10-24 19:56:05 +02:00
|
|
|
}
|
|
|
|
|
2015-11-18 11:01:48 +01:00
|
|
|
TEST_F(ProgramOptionsParserTest, ConfigfileGiven_RelativePath) {
|
2018-09-07 14:00:47 +02:00
|
|
|
ProgramOptions options = parse({"./myExecutable", basedir, "--config", "myconfigfile", mountdir});
|
2015-11-18 11:01:48 +01:00
|
|
|
EXPECT_EQ(bf::current_path() / "myconfigfile", options.configFile().value());
|
|
|
|
}
|
|
|
|
|
2016-06-27 07:15:43 +02:00
|
|
|
TEST_F(ProgramOptionsParserTest, ConfigfileNotGiven) {
|
2018-09-07 14:00:47 +02:00
|
|
|
ProgramOptions options = parse({"./myExecutable", basedir, mountdir});
|
2016-06-27 07:15:43 +02:00
|
|
|
EXPECT_EQ(none, options.configFile());
|
|
|
|
}
|
|
|
|
|
2015-10-30 19:53:15 +01:00
|
|
|
TEST_F(ProgramOptionsParserTest, CipherGiven) {
|
2018-09-07 14:00:47 +02:00
|
|
|
ProgramOptions options = parse({"./myExecutable", basedir, "--cipher", "aes-256-gcm", mountdir});
|
2015-10-30 19:53:15 +01:00
|
|
|
EXPECT_EQ("aes-256-gcm", options.cipher().value());
|
|
|
|
}
|
|
|
|
|
2016-06-27 07:15:43 +02:00
|
|
|
TEST_F(ProgramOptionsParserTest, CipherNotGiven) {
|
2018-09-07 14:00:47 +02:00
|
|
|
ProgramOptions options = parse({"./myExecutable", basedir, mountdir});
|
2016-06-27 07:15:43 +02:00
|
|
|
EXPECT_EQ(none, options.cipher());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ProgramOptionsParserTest, InvalidCipher) {
|
2018-02-02 01:08:01 +01:00
|
|
|
try {
|
2018-09-07 14:00:47 +02:00
|
|
|
parse({"./myExecutable", basedir, "--cipher", "invalid-cipher", mountdir});
|
2018-02-02 01:08:01 +01:00
|
|
|
EXPECT_TRUE(false); // expect throw
|
|
|
|
} catch (const CryfsException& e) {
|
|
|
|
EXPECT_EQ(ErrorCode::InvalidArguments, e.errorCode());
|
|
|
|
EXPECT_THAT(e.what(), testing::MatchesRegex(".*Invalid cipher: invalid-cipher.*"));
|
|
|
|
}
|
2016-06-27 07:15:43 +02:00
|
|
|
}
|
|
|
|
|
2015-11-12 20:43:11 +01:00
|
|
|
TEST_F(ProgramOptionsParserTest, UnmountAfterIdleMinutesGiven) {
|
2018-09-07 14:00:47 +02:00
|
|
|
ProgramOptions options = parse({"./myExecutable", basedir, "--unmount-idle", "10", mountdir});
|
2015-11-12 20:43:11 +01:00
|
|
|
EXPECT_EQ(10, options.unmountAfterIdleMinutes().value());
|
|
|
|
}
|
|
|
|
|
2015-11-13 00:06:53 +01:00
|
|
|
TEST_F(ProgramOptionsParserTest, UnmountAfterIdleMinutesGiven_Float) {
|
2018-09-07 14:00:47 +02:00
|
|
|
ProgramOptions options = parse({"./myExecutable", basedir, "--unmount-idle", "0.5", mountdir});
|
2015-11-13 00:06:53 +01:00
|
|
|
EXPECT_EQ(0.5, options.unmountAfterIdleMinutes().value());
|
|
|
|
}
|
|
|
|
|
2016-06-27 07:15:43 +02:00
|
|
|
TEST_F(ProgramOptionsParserTest, UnmountAfterIdleMinutesNotGiven) {
|
2018-09-07 14:00:47 +02:00
|
|
|
ProgramOptions options = parse({"./myExecutable", basedir, mountdir});
|
2016-06-27 07:15:43 +02:00
|
|
|
EXPECT_EQ(none, options.unmountAfterIdleMinutes());
|
|
|
|
}
|
|
|
|
|
2016-03-24 07:45:41 +01:00
|
|
|
TEST_F(ProgramOptionsParserTest, BlocksizeGiven) {
|
2018-09-07 14:00:47 +02:00
|
|
|
ProgramOptions options = parse({"./myExecutable", basedir, "--blocksize", "10240", mountdir});
|
2016-03-24 07:45:41 +01:00
|
|
|
EXPECT_EQ(10240u, options.blocksizeBytes().value());
|
|
|
|
}
|
|
|
|
|
2016-06-27 07:15:43 +02:00
|
|
|
TEST_F(ProgramOptionsParserTest, BlocksizeNotGiven) {
|
2018-09-07 14:00:47 +02:00
|
|
|
ProgramOptions options = parse({"./myExecutable", basedir, mountdir});
|
2016-06-27 07:15:43 +02:00
|
|
|
EXPECT_EQ(none, options.blocksizeBytes());
|
2015-10-30 19:53:15 +01:00
|
|
|
}
|
|
|
|
|
2016-06-27 07:07:58 +02:00
|
|
|
TEST_F(ProgramOptionsParserTest, MissingBlockIsIntegrityViolationGiven_True) {
|
2018-09-07 14:00:47 +02:00
|
|
|
ProgramOptions options = parse({"./myExecutable", basedir, "--missing-block-is-integrity-violation", "true", mountdir});
|
2016-06-27 07:07:58 +02:00
|
|
|
EXPECT_TRUE(options.missingBlockIsIntegrityViolation().value());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ProgramOptionsParserTest, MissingBlockIsIntegrityViolationGiven_False) {
|
2018-09-07 14:00:47 +02:00
|
|
|
ProgramOptions options = parse({"./myExecutable", basedir, "--missing-block-is-integrity-violation", "false", mountdir});
|
2016-06-27 07:07:58 +02:00
|
|
|
EXPECT_FALSE(options.missingBlockIsIntegrityViolation().value());
|
|
|
|
}
|
|
|
|
|
2018-04-29 22:41:21 +02:00
|
|
|
TEST_F(ProgramOptionsParserTest, AllowIntegrityViolations_True) {
|
2018-09-07 14:00:47 +02:00
|
|
|
ProgramOptions options = parse({"./myExecutable", basedir, "--allow-integrity-violations", mountdir});
|
2018-04-29 22:41:21 +02:00
|
|
|
EXPECT_TRUE(options.allowIntegrityViolations());
|
2017-09-16 17:18:04 +02:00
|
|
|
}
|
|
|
|
|
2018-04-29 22:41:21 +02:00
|
|
|
TEST_F(ProgramOptionsParserTest, AllowIntegrityViolations_False) {
|
2018-09-07 14:00:47 +02:00
|
|
|
ProgramOptions options = parse({"./myExecutable", basedir, mountdir});
|
2018-04-29 22:41:21 +02:00
|
|
|
EXPECT_FALSE(options.allowIntegrityViolations());
|
2017-09-16 17:18:04 +02:00
|
|
|
}
|
|
|
|
|
2016-06-27 07:07:58 +02:00
|
|
|
TEST_F(ProgramOptionsParserTest, MissingBlockIsIntegrityViolationNotGiven) {
|
2018-09-07 14:00:47 +02:00
|
|
|
ProgramOptions options = parse({"./myExecutable", basedir, mountdir});
|
2016-06-27 07:07:58 +02:00
|
|
|
EXPECT_EQ(none, options.missingBlockIsIntegrityViolation());
|
|
|
|
}
|
|
|
|
|
2015-09-29 14:29:10 +02:00
|
|
|
TEST_F(ProgramOptionsParserTest, FuseOptionGiven) {
|
2018-09-07 14:00:47 +02:00
|
|
|
ProgramOptions options = parse({"./myExecutable", basedir, mountdir, "--", "-f"});
|
|
|
|
EXPECT_EQ(basedir, options.baseDir());
|
|
|
|
EXPECT_EQ(mountdir, options.mountDir());
|
2016-05-10 01:07:02 +02:00
|
|
|
EXPECT_VECTOR_EQ({"-f"}, options.fuseOptions());
|
2015-09-29 14:29:10 +02:00
|
|
|
}
|
2016-06-27 07:15:43 +02:00
|
|
|
|
|
|
|
TEST_F(ProgramOptionsParserTest, FuseOptionGiven_Empty) {
|
2018-09-07 14:00:47 +02:00
|
|
|
ProgramOptions options = parse({"./myExecutable", basedir, mountdir, "--"});
|
|
|
|
EXPECT_EQ(basedir, options.baseDir());
|
|
|
|
EXPECT_EQ(mountdir, options.mountDir());
|
2016-06-27 07:15:43 +02:00
|
|
|
EXPECT_VECTOR_EQ({}, options.fuseOptions());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ProgramOptionsParserTest, FuseOptionNotGiven) {
|
2018-09-07 14:00:47 +02:00
|
|
|
ProgramOptions options = parse({"./myExecutable", basedir, mountdir});
|
|
|
|
EXPECT_EQ(basedir, options.baseDir());
|
|
|
|
EXPECT_EQ(mountdir, options.mountDir());
|
2016-06-27 07:15:43 +02:00
|
|
|
EXPECT_VECTOR_EQ({}, options.fuseOptions());
|
|
|
|
}
|
2017-08-23 10:56:03 +02:00
|
|
|
|
|
|
|
TEST_F(ProgramOptionsParserTest, DirectFuseOptionsGiven_AfterPositionalOptions) {
|
2018-09-07 14:00:47 +02:00
|
|
|
ProgramOptions options = parse({"./myExecutable", basedir, mountdir, "-o", "my_opt"});
|
2017-08-23 10:56:03 +02:00
|
|
|
EXPECT_VECTOR_EQ({"-o", "my_opt"}, options.fuseOptions());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ProgramOptionsParserTest, DirectFuseOptionsGiven_BeforePositionalOptions) {
|
2018-09-07 14:00:47 +02:00
|
|
|
ProgramOptions options = parse({"./myExecutable", "-o", "my_opt", basedir, mountdir});
|
2017-08-23 10:56:03 +02:00
|
|
|
EXPECT_VECTOR_EQ({"-o", "my_opt"}, options.fuseOptions());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ProgramOptionsParserTest, DirectFuseOptionsGiven_BeforeAndAfterPositionalOptions) {
|
2018-09-07 14:00:47 +02:00
|
|
|
ProgramOptions options = parse({"./myExecutable", "-o", "first", "-o", "second", basedir, "-o", "third", "-o", "fourth", mountdir, "-o", "fifth", "-o", "sixth"});
|
2017-08-23 10:56:03 +02:00
|
|
|
EXPECT_VECTOR_EQ({"-o", "first", "-o", "second", "-o", "third", "-o", "fourth", "-o", "fifth", "-o", "sixth"}, options.fuseOptions());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ProgramOptionsParserTest, DirectAndIndirectFuseOptionsGiven) {
|
2018-09-07 14:00:47 +02:00
|
|
|
ProgramOptions options = parse({"./myExecutable", basedir, mountdir, "-o", "my_opt", "--", "-o", "other_opt"});
|
2017-08-23 10:56:03 +02:00
|
|
|
EXPECT_VECTOR_EQ({"-o", "other_opt", "-o", "my_opt"}, options.fuseOptions());
|
|
|
|
}
|