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>
|
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;
|
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) {
|
2015-09-29 14:29:10 +02:00
|
|
|
EXPECT_DEATH(
|
2015-10-16 04:06:00 +02:00
|
|
|
parse({"./myExecutable"}),
|
|
|
|
"Usage:"
|
2015-09-29 14:29:10 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-10-28 15:27:30 +01:00
|
|
|
TEST_F(ProgramOptionsParserTest, MissingDir) {
|
2015-09-29 14:29:10 +02:00
|
|
|
EXPECT_DEATH(
|
|
|
|
parse({"./myExecutable", "/home/user/baseDir"}),
|
|
|
|
"Usage:"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-10-28 15:27:30 +01:00
|
|
|
TEST_F(ProgramOptionsParserTest, HelpLongOption) {
|
2015-09-29 14:29:10 +02:00
|
|
|
EXPECT_DEATH(
|
2015-10-30 19:53:15 +01:00
|
|
|
parse({"./myExecutable", "--help"}),
|
|
|
|
"Usage:"
|
2015-09-29 14:29:10 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-10-28 15:27:30 +01:00
|
|
|
TEST_F(ProgramOptionsParserTest, HelpShortOption) {
|
2015-09-29 14:29:10 +02:00
|
|
|
EXPECT_DEATH(
|
2015-10-30 19:53:15 +01:00
|
|
|
parse({"./myExecutable", "-h"}),
|
|
|
|
"Usage:"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ProgramOptionsParserTest, ShowCiphers) {
|
|
|
|
EXPECT_EXIT(
|
|
|
|
parse({"./myExecutable", "--show-ciphers"}),
|
|
|
|
::testing::ExitedWithCode(0),
|
|
|
|
"aes-256-gcm"
|
2015-09-29 14:29:10 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-06-27 07:15:43 +02:00
|
|
|
TEST_F(ProgramOptionsParserTest, BaseDir_Absolute) {
|
2015-10-24 19:56:05 +02:00
|
|
|
ProgramOptions options = parse({"./myExecutable", "/home/user/baseDir", "/home/user/mountDir"});
|
2015-09-29 14:29:10 +02:00
|
|
|
EXPECT_EQ("/home/user/baseDir", options.baseDir());
|
|
|
|
}
|
|
|
|
|
2016-06-27 07:15:43 +02:00
|
|
|
TEST_F(ProgramOptionsParserTest, Basedir_Relative) {
|
2015-11-18 11:01:48 +01:00
|
|
|
ProgramOptions options = parse({"./myExecutable", "baseDir", "/home/user/mountDir"});
|
|
|
|
EXPECT_EQ(bf::current_path() / "baseDir", options.baseDir());
|
|
|
|
}
|
|
|
|
|
2016-06-27 07:15:43 +02:00
|
|
|
TEST_F(ProgramOptionsParserTest, MountDir_Absolute) {
|
|
|
|
ProgramOptions options = parse({"./myExecutable", "/home/user/baseDir", "/home/user/mountDir"});
|
|
|
|
EXPECT_EQ("/home/user/mountDir", options.mountDir());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ProgramOptionsParserTest, MountDir_Relative) {
|
2015-11-18 11:01:48 +01:00
|
|
|
ProgramOptions options = parse({"./myExecutable", "/home/user/baseDir", "mountDir"});
|
|
|
|
EXPECT_EQ(bf::current_path() / "mountDir", options.mountDir());
|
|
|
|
}
|
|
|
|
|
2015-10-24 19:56:05 +02:00
|
|
|
TEST_F(ProgramOptionsParserTest, LogfileGiven) {
|
2015-10-24 20:00:54 +02:00
|
|
|
ProgramOptions options = parse({"./myExecutable", "/home/user/baseDir", "--logfile", "/home/user/mylogfile", "/home/user/mountDir"});
|
2015-10-24 19:56:05 +02:00
|
|
|
EXPECT_EQ("/home/user/mylogfile", options.logFile().value());
|
|
|
|
}
|
|
|
|
|
2015-11-18 11:01:48 +01:00
|
|
|
TEST_F(ProgramOptionsParserTest, LogfileGiven_RelativePath) {
|
|
|
|
ProgramOptions options = parse({"./myExecutable", "/home/user/baseDir", "--logfile", "mylogfile", "/home/user/mountDir"});
|
|
|
|
EXPECT_EQ(bf::current_path() / "mylogfile", options.logFile().value());
|
|
|
|
}
|
|
|
|
|
2016-06-27 07:15:43 +02:00
|
|
|
TEST_F(ProgramOptionsParserTest, LogfileNotGiven) {
|
|
|
|
ProgramOptions options = parse({"./myExecutable", "/home/user/baseDir", "/home/user/mountDir"});
|
|
|
|
EXPECT_EQ(none, options.logFile());
|
|
|
|
}
|
|
|
|
|
2015-10-24 19:56:05 +02:00
|
|
|
TEST_F(ProgramOptionsParserTest, ConfigfileGiven) {
|
2015-10-24 20:00:54 +02:00
|
|
|
ProgramOptions options = parse({"./myExecutable", "/home/user/baseDir", "--config", "/home/user/myconfigfile", "/home/user/mountDir"});
|
2015-10-24 19:56:05 +02:00
|
|
|
EXPECT_EQ("/home/user/myconfigfile", options.configFile().value());
|
|
|
|
}
|
|
|
|
|
2015-11-18 11:01:48 +01:00
|
|
|
TEST_F(ProgramOptionsParserTest, ConfigfileGiven_RelativePath) {
|
|
|
|
ProgramOptions options = parse({"./myExecutable", "/home/user/baseDir", "--config", "myconfigfile", "/home/user/mountDir"});
|
|
|
|
EXPECT_EQ(bf::current_path() / "myconfigfile", options.configFile().value());
|
|
|
|
}
|
|
|
|
|
2016-06-27 07:15:43 +02:00
|
|
|
TEST_F(ProgramOptionsParserTest, ConfigfileNotGiven) {
|
|
|
|
ProgramOptions options = parse({"./myExecutable", "/home/user/baseDir", "/home/user/mountDir"});
|
|
|
|
EXPECT_EQ(none, options.configFile());
|
|
|
|
}
|
|
|
|
|
2015-10-30 19:53:15 +01:00
|
|
|
TEST_F(ProgramOptionsParserTest, CipherGiven) {
|
|
|
|
ProgramOptions options = parse({"./myExecutable", "/home/user/baseDir", "--cipher", "aes-256-gcm", "/home/user/mountDir"});
|
|
|
|
EXPECT_EQ("aes-256-gcm", options.cipher().value());
|
|
|
|
}
|
|
|
|
|
2016-06-27 07:15:43 +02:00
|
|
|
TEST_F(ProgramOptionsParserTest, CipherNotGiven) {
|
|
|
|
ProgramOptions options = parse({"./myExecutable", "/home/user/baseDir", "/home/user/mountDir"});
|
|
|
|
EXPECT_EQ(none, options.cipher());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ProgramOptionsParserTest, InvalidCipher) {
|
|
|
|
EXPECT_DEATH(
|
|
|
|
parse({"./myExecutable", "/home/user/baseDir", "--cipher", "invalid-cipher", "/home/user/mountDir"}),
|
|
|
|
"Invalid cipher: invalid-cipher"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-11-12 20:43:11 +01:00
|
|
|
TEST_F(ProgramOptionsParserTest, UnmountAfterIdleMinutesGiven) {
|
|
|
|
ProgramOptions options = parse({"./myExecutable", "/home/user/baseDir", "--unmount-idle", "10", "/home/user/mountDir"});
|
|
|
|
EXPECT_EQ(10, options.unmountAfterIdleMinutes().value());
|
|
|
|
}
|
|
|
|
|
2015-11-13 00:06:53 +01:00
|
|
|
TEST_F(ProgramOptionsParserTest, UnmountAfterIdleMinutesGiven_Float) {
|
|
|
|
ProgramOptions options = parse({"./myExecutable", "/home/user/baseDir", "--unmount-idle", "0.5", "/home/user/mountDir"});
|
|
|
|
EXPECT_EQ(0.5, options.unmountAfterIdleMinutes().value());
|
|
|
|
}
|
|
|
|
|
2016-06-27 07:15:43 +02:00
|
|
|
TEST_F(ProgramOptionsParserTest, UnmountAfterIdleMinutesNotGiven) {
|
|
|
|
ProgramOptions options = parse({"./myExecutable", "/home/user/baseDir", "/home/user/mountDir"});
|
|
|
|
EXPECT_EQ(none, options.unmountAfterIdleMinutes());
|
|
|
|
}
|
|
|
|
|
2016-03-24 07:45:41 +01:00
|
|
|
TEST_F(ProgramOptionsParserTest, BlocksizeGiven) {
|
|
|
|
ProgramOptions options = parse({"./myExecutable", "/home/user/baseDir", "--blocksize", "10240", "/home/user/mountDir"});
|
|
|
|
EXPECT_EQ(10240u, options.blocksizeBytes().value());
|
|
|
|
}
|
|
|
|
|
2016-06-27 07:15:43 +02:00
|
|
|
TEST_F(ProgramOptionsParserTest, BlocksizeNotGiven) {
|
|
|
|
ProgramOptions options = parse({"./myExecutable", "/home/user/baseDir", "/home/user/mountDir"});
|
|
|
|
EXPECT_EQ(none, options.blocksizeBytes());
|
2015-10-30 19:53:15 +01:00
|
|
|
}
|
|
|
|
|
2015-09-29 14:29:10 +02:00
|
|
|
TEST_F(ProgramOptionsParserTest, FuseOptionGiven) {
|
2015-10-24 19:56:05 +02:00
|
|
|
ProgramOptions options = parse({"./myExecutable", "/home/user/baseDir", "/home/user/mountDir", "--", "-f"});
|
2015-09-29 14:29:10 +02:00
|
|
|
EXPECT_EQ("/home/user/baseDir", options.baseDir());
|
|
|
|
EXPECT_EQ("/home/user/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) {
|
|
|
|
ProgramOptions options = parse({"./myExecutable", "/home/user/baseDir", "/home/user/mountDir", "--"});
|
|
|
|
EXPECT_EQ("/home/user/baseDir", options.baseDir());
|
|
|
|
EXPECT_EQ("/home/user/mountDir", options.mountDir());
|
|
|
|
EXPECT_VECTOR_EQ({}, options.fuseOptions());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ProgramOptionsParserTest, FuseOptionNotGiven) {
|
|
|
|
ProgramOptions options = parse({"./myExecutable", "/home/user/baseDir", "/home/user/mountDir"});
|
|
|
|
EXPECT_EQ("/home/user/baseDir", options.baseDir());
|
|
|
|
EXPECT_EQ("/home/user/mountDir", options.mountDir());
|
|
|
|
EXPECT_VECTOR_EQ({}, options.fuseOptions());
|
|
|
|
}
|