Fix argument string ownership

This commit is contained in:
Sebastian Messmer 2018-09-06 22:44:23 -07:00
parent d8b66d0967
commit a9e2dea97e
3 changed files with 16 additions and 16 deletions

View File

@ -24,7 +24,7 @@ public:
}; };
TEST_F(CliTest_IntegrityCheck, givenIncorrectFilesystemId_thenFails) { TEST_F(CliTest_IntegrityCheck, givenIncorrectFilesystemId_thenFails) {
vector<const char*> args {basedir.string().c_str(), mountdir.string().c_str(), "--cipher", "aes-256-gcm", "-f"}; vector<string> args {basedir.string().c_str(), mountdir.string().c_str(), "--cipher", "aes-256-gcm", "-f"};
//TODO Remove "-f" parameter, once EXPECT_RUN_SUCCESS can handle that //TODO Remove "-f" parameter, once EXPECT_RUN_SUCCESS can handle that
EXPECT_RUN_SUCCESS(args, mountdir); EXPECT_RUN_SUCCESS(args, mountdir);
modifyFilesystemId(); modifyFilesystemId();
@ -36,7 +36,7 @@ TEST_F(CliTest_IntegrityCheck, givenIncorrectFilesystemId_thenFails) {
} }
TEST_F(CliTest_IntegrityCheck, givenIncorrectFilesystemKey_thenFails) { TEST_F(CliTest_IntegrityCheck, givenIncorrectFilesystemKey_thenFails) {
vector<const char*> args {basedir.string().c_str(), mountdir.string().c_str(), "--cipher", "aes-256-gcm", "-f"}; vector<string> args {basedir.string().c_str(), mountdir.string().c_str(), "--cipher", "aes-256-gcm", "-f"};
//TODO Remove "-f" parameter, once EXPECT_RUN_SUCCESS can handle that //TODO Remove "-f" parameter, once EXPECT_RUN_SUCCESS can handle that
EXPECT_RUN_SUCCESS(args, mountdir); EXPECT_RUN_SUCCESS(args, mountdir);
modifyFilesystemKey(); modifyFilesystemKey();

View File

@ -52,15 +52,15 @@ public:
); );
} }
vector<const char*> args() { vector<string> args() {
vector<const char*> result = {basedir.string().c_str(), mountdir.string().c_str()}; vector<string> result = {basedir.string(), mountdir.string()};
if (GetParam().externalConfigfile) { if (GetParam().externalConfigfile) {
result.push_back("--config"); result.push_back("--config");
result.push_back(configfile.path().string().c_str()); result.push_back(configfile.path().string());
} }
if (GetParam().logIsNotStderr) { if (GetParam().logIsNotStderr) {
result.push_back("--logfile"); result.push_back("--logfile");
result.push_back(logfile.path().string().c_str()); result.push_back(logfile.path().string());
} }
if (GetParam().runningInForeground) { if (GetParam().runningInForeground) {
result.push_back("-f"); result.push_back("-f");

View File

@ -41,12 +41,12 @@ public:
return std::move(httpClient); return std::move(httpClient);
} }
int run(std::vector<const char*> args) { int run(const std::vector<std::string>& args) {
std::vector<const char*> _args; std::vector<const char*> _args;
_args.reserve(args.size()+1); _args.reserve(args.size() + 1);
_args.push_back("cryfs"); _args.emplace_back("cryfs");
for (const char *arg : args) { for (const std::string& arg : args) {
_args.push_back(arg); _args.emplace_back(arg.c_str());
} }
auto &keyGenerator = cpputils::Random::PseudoRandom(); auto &keyGenerator = cpputils::Random::PseudoRandom();
ON_CALL(*console, askPassword(testing::StrEq("Password: "))).WillByDefault(testing::Return("pass")); ON_CALL(*console, askPassword(testing::StrEq("Password: "))).WillByDefault(testing::Return("pass"));
@ -55,18 +55,18 @@ public:
return cryfs::Cli(keyGenerator, cpputils::SCrypt::TestSettings, console).main(_args.size(), _args.data(), _httpClient()); return cryfs::Cli(keyGenerator, cpputils::SCrypt::TestSettings, console).main(_args.size(), _args.data(), _httpClient());
} }
void EXPECT_EXIT_WITH_HELP_MESSAGE(std::vector<const char*> args, const std::string &message, cryfs::ErrorCode errorCode) { void EXPECT_EXIT_WITH_HELP_MESSAGE(const std::vector<std::string>& args, const std::string &message, cryfs::ErrorCode errorCode) {
EXPECT_RUN_ERROR(args, (".*Usage:.*"+message).c_str(), errorCode); EXPECT_RUN_ERROR(args, (".*Usage:.*"+message).c_str(), errorCode);
} }
void EXPECT_RUN_ERROR(std::vector<const char*> args, const char* message, cryfs::ErrorCode errorCode) { void EXPECT_RUN_ERROR(const std::vector<std::string>& args, const char* message, cryfs::ErrorCode errorCode) {
cpputils::CaptureStderrRAII capturedStderr; cpputils::CaptureStderrRAII capturedStderr;
int exit_code = run(args); int exit_code = run(args);
capturedStderr.EXPECT_MATCHES(message); capturedStderr.EXPECT_MATCHES(message);
EXPECT_EQ(exitCode(errorCode), exit_code); EXPECT_EQ(exitCode(errorCode), exit_code);
} }
void EXPECT_RUN_SUCCESS(std::vector<const char*> args, const boost::filesystem::path &mountDir) { void EXPECT_RUN_SUCCESS(const std::vector<std::string>& args, const boost::filesystem::path &mountDir) {
//TODO Make this work when run in background //TODO Make this work when run in background
ASSERT(std::find(args.begin(), args.end(), string("-f")) != args.end(), "Currently only works if run in foreground"); ASSERT(std::find(args.begin(), args.end(), string("-f")) != args.end(), "Currently only works if run in foreground");
bool unmount_success = false; bool unmount_success = false;