2015-10-29 19:34:36 +01:00
|
|
|
#pragma once
|
|
|
|
#ifndef MESSMER_CRYFS_TEST_CLI_TESTUTILS_CLITEST_H
|
|
|
|
#define MESSMER_CRYFS_TEST_CLI_TESTUTILS_CLITEST_H
|
|
|
|
|
2018-09-07 14:00:47 +02:00
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#include <codecvt>
|
|
|
|
#include <dokan/dokan.h>
|
|
|
|
#endif
|
|
|
|
|
2016-02-11 16:39:42 +01:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <gmock/gmock.h>
|
|
|
|
#include <cpp-utils/tempfile/TempDir.h>
|
|
|
|
#include <cpp-utils/tempfile/TempFile.h>
|
2016-02-23 21:07:23 +01:00
|
|
|
#include <cryfs-cli/Cli.h>
|
|
|
|
#include <cryfs-cli/VersionChecker.h>
|
2016-02-11 16:39:42 +01:00
|
|
|
#include <cpp-utils/logging/logging.h>
|
|
|
|
#include <cpp-utils/process/subprocess.h>
|
|
|
|
#include <cpp-utils/network/FakeHttpClient.h>
|
2018-12-09 18:27:53 +01:00
|
|
|
#include <cpp-utils/lock/ConditionBarrier.h>
|
2019-01-26 08:38:34 +01:00
|
|
|
#include "../../cryfs/impl/testutils/MockConsole.h"
|
|
|
|
#include "../../cryfs/impl/testutils/TestWithFakeHomeDirectory.h"
|
2019-01-19 22:02:41 +01:00
|
|
|
#include <fspp/fuse/Fuse.h>
|
2019-01-26 08:38:34 +01:00
|
|
|
#include <cryfs/impl/ErrorCodes.h>
|
2018-02-02 01:08:01 +01:00
|
|
|
#include <cpp-utils/testutils/CaptureStderrRAII.h>
|
2018-09-07 14:00:47 +02:00
|
|
|
#include <regex>
|
|
|
|
#include <string>
|
2015-10-29 19:34:36 +01:00
|
|
|
|
2017-09-30 10:18:46 +02:00
|
|
|
class CliTest : public ::testing::Test, TestWithFakeHomeDirectory {
|
2015-10-29 19:34:36 +01:00
|
|
|
public:
|
2016-01-25 15:01:34 +01:00
|
|
|
CliTest(): _basedir(), _mountdir(), basedir(_basedir.path()), mountdir(_mountdir.path()), logfile(), configfile(false), console(std::make_shared<MockConsole>()) {}
|
2015-10-29 19:34:36 +01:00
|
|
|
|
2015-10-30 17:23:08 +01:00
|
|
|
cpputils::TempDir _basedir;
|
|
|
|
cpputils::TempDir _mountdir;
|
|
|
|
boost::filesystem::path basedir;
|
|
|
|
boost::filesystem::path mountdir;
|
2015-10-29 19:34:36 +01:00
|
|
|
cpputils::TempFile logfile;
|
|
|
|
cpputils::TempFile configfile;
|
2016-01-25 15:01:34 +01:00
|
|
|
std::shared_ptr<MockConsole> console;
|
2015-10-29 19:34:36 +01:00
|
|
|
|
2017-09-30 23:24:33 +02:00
|
|
|
cpputils::unique_ref<cpputils::HttpClient> _httpClient() {
|
|
|
|
cpputils::unique_ref<cpputils::FakeHttpClient> httpClient = cpputils::make_unique_ref<cpputils::FakeHttpClient>();
|
2016-01-28 18:55:26 +01:00
|
|
|
httpClient->addWebsite("https://www.cryfs.org/version_info.json", "{\"version_info\":{\"current\":\"0.8.5\"}}");
|
2019-06-08 22:06:17 +02:00
|
|
|
return httpClient;
|
2016-01-28 18:55:26 +01:00
|
|
|
}
|
|
|
|
|
2018-12-09 18:27:53 +01:00
|
|
|
int run(const std::vector<std::string>& args, std::function<void()> onMounted) {
|
|
|
|
std::vector<const char*> _args;
|
|
|
|
_args.reserve(args.size() + 1);
|
2019-01-19 22:02:41 +01:00
|
|
|
_args.emplace_back("cryfs");
|
2018-12-09 18:27:53 +01:00
|
|
|
for (const std::string& arg : args) {
|
|
|
|
_args.emplace_back(arg.c_str());
|
|
|
|
}
|
2015-11-03 21:22:35 +01:00
|
|
|
auto &keyGenerator = cpputils::Random::PseudoRandom();
|
2018-09-04 01:51:59 +02:00
|
|
|
ON_CALL(*console, askPassword(testing::StrEq("Password: "))).WillByDefault(testing::Return("pass"));
|
|
|
|
ON_CALL(*console, askPassword(testing::StrEq("Confirm Password: "))).WillByDefault(testing::Return("pass"));
|
2016-02-13 15:06:28 +01:00
|
|
|
// Run Cryfs
|
2019-01-19 22:02:41 +01:00
|
|
|
return cryfs_cli::Cli(keyGenerator, cpputils::SCrypt::TestSettings, console).main(_args.size(), _args.data(), _httpClient(), std::move(onMounted));
|
2015-10-29 19:34:36 +01:00
|
|
|
}
|
|
|
|
|
2018-09-07 07:44:23 +02:00
|
|
|
void EXPECT_EXIT_WITH_HELP_MESSAGE(const std::vector<std::string>& args, const std::string &message, cryfs::ErrorCode errorCode) {
|
2018-09-08 13:29:28 +02:00
|
|
|
EXPECT_RUN_ERROR(args, "Usage:[^\\x00]*"+message, errorCode);
|
2015-10-29 19:34:36 +01:00
|
|
|
}
|
|
|
|
|
2018-12-11 06:20:18 +01:00
|
|
|
void EXPECT_RUN_ERROR(const std::vector<std::string>& args, const std::string& message, cryfs::ErrorCode errorCode, std::function<void ()> onMounted = [] {}) {
|
|
|
|
FilesystemOutput filesystem_output = run_filesystem(args, boost::none, std::move(onMounted));
|
2018-09-08 04:54:01 +02:00
|
|
|
|
|
|
|
EXPECT_EQ(exitCode(errorCode), filesystem_output.exit_code);
|
2018-12-11 06:20:18 +01:00
|
|
|
if (!std::regex_search(filesystem_output.stderr_, std::regex(message))) {
|
|
|
|
std::cerr << filesystem_output.stderr_ << std::endl;
|
|
|
|
EXPECT_TRUE(false);
|
|
|
|
}
|
2015-10-29 19:34:36 +01:00
|
|
|
}
|
|
|
|
|
2018-12-11 06:20:18 +01:00
|
|
|
void EXPECT_RUN_SUCCESS(const std::vector<std::string>& args, const boost::optional<boost::filesystem::path> &mountDir, std::function<void ()> onMounted = [] {}) {
|
2015-11-03 22:02:04 +01:00
|
|
|
//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");
|
2018-09-08 04:54:01 +02:00
|
|
|
|
2018-12-11 06:20:18 +01:00
|
|
|
FilesystemOutput filesystem_output = run_filesystem(args, mountDir, std::move(onMounted));
|
2018-09-08 04:54:01 +02:00
|
|
|
|
2018-12-09 18:27:53 +01:00
|
|
|
EXPECT_EQ(0, filesystem_output.exit_code);
|
2018-12-11 06:20:18 +01:00
|
|
|
if (!std::regex_search(filesystem_output.stdout_, std::regex("Mounting filesystem"))) {
|
|
|
|
std::cerr << filesystem_output.stdout_ << std::endl;
|
|
|
|
EXPECT_TRUE(false);
|
|
|
|
}
|
2018-09-08 04:54:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct FilesystemOutput final {
|
|
|
|
int exit_code;
|
|
|
|
std::string stdout_;
|
|
|
|
std::string stderr_;
|
|
|
|
};
|
|
|
|
|
2018-12-09 18:27:53 +01:00
|
|
|
static void _unmount(const boost::filesystem::path &mountDir) {
|
2019-01-19 22:02:41 +01:00
|
|
|
fspp::fuse::Fuse::unmount(mountDir, true);
|
2018-12-09 18:27:53 +01:00
|
|
|
}
|
|
|
|
|
2018-12-11 06:20:18 +01:00
|
|
|
FilesystemOutput run_filesystem(const std::vector<std::string>& args, boost::optional<boost::filesystem::path> mountDirForUnmounting, std::function<void()> onMounted) {
|
2018-12-09 18:27:53 +01:00
|
|
|
testing::internal::CaptureStdout();
|
|
|
|
testing::internal::CaptureStderr();
|
|
|
|
|
|
|
|
bool exited = false;
|
|
|
|
cpputils::ConditionBarrier isMountedOrFailedBarrier;
|
|
|
|
|
|
|
|
std::future<int> exit_code = std::async(std::launch::async, [&] {
|
|
|
|
int exit_code = run(args, [&] { isMountedOrFailedBarrier.release(); });
|
|
|
|
// just in case it fails, we also want to release the barrier.
|
|
|
|
// if it succeeds, this will release it a second time, which doesn't hurt.
|
|
|
|
exited = true;
|
|
|
|
isMountedOrFailedBarrier.release();
|
|
|
|
return exit_code;
|
|
|
|
});
|
|
|
|
|
|
|
|
std::future<bool> on_mounted_success = std::async(std::launch::async, [&] {
|
|
|
|
isMountedOrFailedBarrier.wait();
|
|
|
|
if (exited) {
|
|
|
|
// file system already exited on its own, this indicates an error. It should have stayed mounted.
|
|
|
|
// while the exit_code from run() will signal an error in this case, we didn't encounter another
|
|
|
|
// error in the onMounted future, so return true here.
|
|
|
|
return true;
|
2015-11-02 21:20:10 +01:00
|
|
|
}
|
2018-12-09 18:27:53 +01:00
|
|
|
// now we know the filesystem stayed online, so we can call the onMounted callback
|
|
|
|
onMounted();
|
|
|
|
// and unmount it afterwards
|
|
|
|
if (mountDirForUnmounting.is_initialized()) {
|
|
|
|
_unmount(*mountDirForUnmounting);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2018-12-11 06:20:18 +01:00
|
|
|
if(std::future_status::ready != on_mounted_success.wait_for(std::chrono::seconds(1000))) {
|
2018-12-09 18:27:53 +01:00
|
|
|
testing::internal::GetCapturedStdout(); // stop capturing stdout
|
|
|
|
testing::internal::GetCapturedStderr(); // stop capturing stderr
|
|
|
|
|
2018-12-11 06:20:18 +01:00
|
|
|
std::cerr << "onMounted thread (e.g. used for unmount) didn't finish" << std::endl;
|
2018-12-09 18:27:53 +01:00
|
|
|
// The std::future destructor of a future created with std::async blocks until the future is ready.
|
|
|
|
// so, instead of causing a deadlock, rather abort
|
|
|
|
exit(EXIT_FAILURE);
|
2018-09-08 04:54:01 +02:00
|
|
|
}
|
2018-12-09 18:27:53 +01:00
|
|
|
EXPECT_TRUE(on_mounted_success.get()); // this also re-throws any potential exceptions
|
2018-09-07 14:01:21 +02:00
|
|
|
|
2018-12-11 06:20:18 +01:00
|
|
|
if(std::future_status::ready != exit_code.wait_for(std::chrono::seconds(1000))) {
|
2018-12-09 18:27:53 +01:00
|
|
|
testing::internal::GetCapturedStdout(); // stop capturing stdout
|
|
|
|
testing::internal::GetCapturedStderr(); // stop capturing stderr
|
|
|
|
|
2018-12-11 06:20:18 +01:00
|
|
|
std::cerr << "Filesystem thread didn't finish" << std::endl;
|
2018-12-09 18:27:53 +01:00
|
|
|
// The std::future destructor of a future created with std::async blocks until the future is ready.
|
|
|
|
// so, instead of causing a deadlock, rather abort
|
|
|
|
exit(EXIT_FAILURE);
|
2018-09-08 04:54:01 +02:00
|
|
|
}
|
2018-09-07 14:01:21 +02:00
|
|
|
|
2018-12-09 18:27:53 +01:00
|
|
|
return {
|
2018-12-11 06:20:18 +01:00
|
|
|
exit_code.get(), // this also re-throws any potential exceptions
|
2018-12-09 18:27:53 +01:00
|
|
|
testing::internal::GetCapturedStdout(),
|
|
|
|
testing::internal::GetCapturedStderr()
|
|
|
|
};
|
2015-10-29 19:34:36 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|