diff --git a/cpp/src/CMakeLists.txt b/cpp/src/CMakeLists.txt index 2fe59c9e8783c4b14ed939a2c958738dd8de516c..31f883ca3ad2982cbb4fb6127a816360b25ab90c 100644 --- a/cpp/src/CMakeLists.txt +++ b/cpp/src/CMakeLists.txt @@ -48,9 +48,10 @@ endif() set(VEREIGNLIB_SRC vereign/core/rand.cc - vereign/core/temp.cc - vereign/core/fs.cc vereign/core/string.cc + vereign/fs/util.cc + vereign/fs/operations.cc + vereign/fs/path.cc vereign/restapi/detail/http_reader.cc vereign/restapi/client.cc diff --git a/cpp/src/csandbox.cc b/cpp/src/csandbox.cc index cdf8de983f263a51b356e9ea05961554cb6db480..25de708a6707e55c775ed6fdf8f621833a5b0e8a 100644 --- a/cpp/src/csandbox.cc +++ b/cpp/src/csandbox.cc @@ -1,3 +1,4 @@ +#include "vereign/kvstore/sqlite_storage.hh" #include <boost/core/ignore_unused.hpp> #include <iostream> #include <boost/filesystem.hpp> @@ -5,12 +6,24 @@ #include <vereign/bytes/view.hh> #include <vereign/bytes/view_dump.hh> +#include <vereign/fs/util.hh> +#include <vereign/fs/path.hh> +#include <vereign/fs/operations.hh> -namespace fs = boost::filesystem; + +using namespace vereign; auto main(int argc, char** argv) -> int { boost::ignore_unused(argc); boost::ignore_unused(argv); + auto dir = fs::TempDir("trtr_"); + auto rm = fs::RemoveAllGuard{dir}; + std::cout << dir << std::endl; + + std::ofstream f{fs::path::Join(dir, "hello")}; + f << "ops"; + f.close(); + return 0; } diff --git a/cpp/src/vereign/core/fs.cc b/cpp/src/vereign/core/fs.cc deleted file mode 100644 index bf79c66acc39b0d7784c995822d8a0a1994dc898..0000000000000000000000000000000000000000 --- a/cpp/src/vereign/core/fs.cc +++ /dev/null @@ -1,16 +0,0 @@ -#include <vereign/core/fs.hh> - -#include <boost/filesystem/operations.hpp> - -namespace vereign::core { - -RemoveFileGuard::RemoveFileGuard(boost::filesystem::path path) - : path_{std::move(path)} -{ -} - -RemoveFileGuard::~RemoveFileGuard() { - boost::filesystem::remove(path_); -} - -} // namespace vereign::core diff --git a/cpp/src/vereign/core/fs.hh b/cpp/src/vereign/core/fs.hh deleted file mode 100644 index 6eb1baded98a2c7a0bf70729f3f5784eab88d9c3..0000000000000000000000000000000000000000 --- a/cpp/src/vereign/core/fs.hh +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef __VEREIGN_CORE_FS_HH -#define __VEREIGN_CORE_FS_HH - -#include <boost/filesystem/path.hpp> - -namespace vereign::core { - -/** - * A RAII guard that deletes a file upon destruction. - */ -class RemoveFileGuard { -public: - RemoveFileGuard(boost::filesystem::path path); - ~RemoveFileGuard(); - - RemoveFileGuard(const RemoveFileGuard&) = delete; - auto operator=(const RemoveFileGuard&) -> RemoveFileGuard& = delete; -private: - boost::filesystem::path path_; -}; - -} // namespace vereign::core - -#endif // __VEREIGN_CORE_FS_HH diff --git a/cpp/src/vereign/core/string.cc b/cpp/src/vereign/core/string.cc index 8fb23c828a698cd28071c35f9fa6eab28b1ef7ba..bf2340fcb9ebafd0b6c8ec5ccdec511ee24a5ecd 100644 --- a/cpp/src/vereign/core/string.cc +++ b/cpp/src/vereign/core/string.cc @@ -61,7 +61,7 @@ auto narrow(std::wstring_view utf16_str) -> std::string { std::string result; result.resize(num_chars); - num_chars = WideCharToMultiByte( + int new_size = WideCharToMultiByte( CP_UTF8, 0, utf16_str.data(), @@ -72,13 +72,23 @@ auto narrow(std::wstring_view utf16_str) -> std::string { nullptr ); - if (num_chars == 0) { + if (new_size == 0) { return ""; } + result.resize(new_size); + return result; } #endif +auto tail(const std::string& src, std::size_t length) -> std::string { + if (length >= src.size()) { + return src; + } + + return src.substr(src.size() - length); +} + } // vereign::string diff --git a/cpp/src/vereign/core/string.hh b/cpp/src/vereign/core/string.hh index 48da81694cec98dd8871d1ec61677a5a1fb62e76..62429b28a0d77feaf77575edd8b630372b571f87 100644 --- a/cpp/src/vereign/core/string.hh +++ b/cpp/src/vereign/core/string.hh @@ -13,6 +13,20 @@ auto narrow(std::wstring_view utf16_str) -> std::string; #endif +/** + * Retrieve string tail. + * + * @code + * std::string str = "foobar"; + * std::string tail = vereign::string::tail(str, 3); + * assert(tail == "bar"); + * @endcode + * + * @param src Source string. + * @param length How many characters from the string tail to return. + */ +auto tail(const std::string& src, std::size_t length) -> std::string; + } // vereign::string #endif // __VEREIGN_CORE_STRING_HH diff --git a/cpp/src/vereign/core/temp.cc b/cpp/src/vereign/core/temp.cc deleted file mode 100644 index bce1880273af9a2ead7a7c8d9027fdf2dc199d61..0000000000000000000000000000000000000000 --- a/cpp/src/vereign/core/temp.cc +++ /dev/null @@ -1,16 +0,0 @@ -#include <vereign/core/temp.hh> - -#include <vereign/core/rand.hh> -#include <boost/filesystem/operations.hpp> - -namespace vereign::core { - -auto TempFilePath(boost::filesystem::path dir, std::string_view prefix) -> boost::filesystem::path { - return dir.append(prefix).concat(RandLowerAlpha(10)); -} - -auto TempFilePath(std::string_view prefix) -> boost::filesystem::path { - return TempFilePath(boost::filesystem::temp_directory_path(), prefix); -} - -} // namespace vereign::core diff --git a/cpp/src/vereign/core/temp.hh b/cpp/src/vereign/core/temp.hh deleted file mode 100644 index bc8dec74849a05263c84368006bf66eaeb25e448..0000000000000000000000000000000000000000 --- a/cpp/src/vereign/core/temp.hh +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef __VEREIGN_CORE_TEMP_HH -#define __VEREIGN_CORE_TEMP_HH - -#include <boost/filesystem/path.hpp> -#include <string_view> - -namespace vereign::core { - -auto TempFilePath(boost::filesystem::path dir, std::string_view prefix) -> boost::filesystem::path; -auto TempFilePath(std::string_view prefix) -> boost::filesystem::path; - -} // namespace vereign::core - -#endif // __VEREIGN_CORE_TEMP_HH diff --git a/cpp/src/vereign/fs/errors.hh b/cpp/src/vereign/fs/errors.hh new file mode 100644 index 0000000000000000000000000000000000000000..035dc67edc34f39c03bbd90bbc649896af48cc3b --- /dev/null +++ b/cpp/src/vereign/fs/errors.hh @@ -0,0 +1,28 @@ +#ifndef __VEREIGN_FS_ERRORS_HH +#define __VEREIGN_FS_ERRORS_HH + +#include <exception> + +namespace vereign::fs { + +class Error : public std::exception { + auto what() const noexcept -> const char* override { + return "filesystem error"; + } +}; + +class HomeNotFoundError : public std::exception { + auto what() const noexcept -> const char* override { + return "cannot find user's home dir"; + } +}; + +class TempDirNotFoundError : public std::exception { + auto what() const noexcept -> const char* override { + return "cannot find user's temp dir"; + } +}; + +} // namespace vereign::fs + +#endif // __VEREIGN_FS_ERRORS_HH diff --git a/cpp/src/vereign/fs/operations.cc b/cpp/src/vereign/fs/operations.cc new file mode 100644 index 0000000000000000000000000000000000000000..4ebcad13b8fc38807f5754dae166408add50ea27 --- /dev/null +++ b/cpp/src/vereign/fs/operations.cc @@ -0,0 +1,29 @@ +#include <vereign/fs/operations.hh> + +#include <vereign/fs/util.hh> +#include <vereign/core/string.hh> + +#include <boost/filesystem/directory.hpp> +#include <boost/filesystem/operations.hpp> +#include <fstream> + +namespace vereign::fs { + +auto Exists(const std::string& path) -> bool { +#ifdef _WIN32 + std::ifstream f{string::widen(path)}; +#else + std::ifstream f{path}; +#endif + return f.good(); +} + +auto IsDirectory(const std::string& path) -> bool { + return boost::filesystem::is_directory(fs::detail::StringToPath(path)); +} + +auto CreateDirectory(const std::string& path) -> bool { + return boost::filesystem::create_directory(fs::detail::StringToPath(path)); +} + +} // namespace vereign::fs diff --git a/cpp/src/vereign/fs/operations.hh b/cpp/src/vereign/fs/operations.hh new file mode 100644 index 0000000000000000000000000000000000000000..969289657a91ad03d4338b01f502d79d5bc79b49 --- /dev/null +++ b/cpp/src/vereign/fs/operations.hh @@ -0,0 +1,29 @@ +#ifndef __VEREIGN_FS_OPERATIONS_HH +#define __VEREIGN_FS_OPERATIONS_HH + +#include <string> + +namespace vereign::fs { + +/** + * Check if file exists and is readable. + */ +auto Exists(const std::string& path) -> bool; + +/** + * Check if given path is a directory. + * + * @param path Path to check. + */ +auto IsDirectory(const std::string& path) -> bool; + +/** + * Creates the last directory of the provided path. + * + * @param path Path of the directory that will be created. + */ +auto CreateDirectory(const std::string& path) -> bool; + +} // namespace vereign::fs + +#endif // __VEREIGN_FS_OPERATIONS_HH diff --git a/cpp/src/vereign/fs/path.cc b/cpp/src/vereign/fs/path.cc new file mode 100644 index 0000000000000000000000000000000000000000..fc92ce61945f17a34df5f3575b4605303733a978 --- /dev/null +++ b/cpp/src/vereign/fs/path.cc @@ -0,0 +1,37 @@ +#include <vereign/fs/path.hh> + +namespace vereign::fs::path { + +/** + * Convert relative path from host OS to unix format. + * + * It replaces the host path separator with the unix path separator. + * For example "foo\\bar" on windows becomes "foo/bar". + * + * @param path Source path. + */ +auto to_unix(std::string path) -> std::string { + if (sep[0] != unix_sep[0]) { + std::replace(path.begin(), path.end(), sep[0], unix_sep[0]); + } + + return path; +} + +/** + * Convert relative path from unix format to native OS format. + * + * It replaces the unix path separator with the native path separator. + * For example "foo/bar" on windows becomes "foo\\bar". + * + * @param path Source path. + */ +auto to_native(std::string path) -> std::string { + if (sep[0] != unix_sep[0]) { + std::replace(path.begin(), path.end(), unix_sep[0], sep[0]); + } + + return path; +} + +} // namespace vereign::fs::path diff --git a/cpp/src/vereign/fs/path.hh b/cpp/src/vereign/fs/path.hh new file mode 100644 index 0000000000000000000000000000000000000000..87d58adc00845b30d16ed155010a1ea23ca4b244 --- /dev/null +++ b/cpp/src/vereign/fs/path.hh @@ -0,0 +1,60 @@ +#ifndef __VEREIGN_FS_PATH_HH +#define __VEREIGN_FS_PATH_HH + +#include <boost/core/ignore_unused.hpp> +#include <algorithm> +#include <string> + +namespace vereign::fs::path { + +#if defined(_WIN32) + constexpr inline const char* unix_sep = "/"; + constexpr inline const char* sep = "\\"; + constexpr inline const char* prefix = "\\\\?\\"; + constexpr inline const char* drive_sep = ":\\"; + constexpr inline const char invalid_escape_char = '_'; +#else + constexpr inline const char* unix_sep = "/"; + constexpr inline const char* sep = "/"; + constexpr inline const char* prefix = "/"; + constexpr inline const char* drive_sep = ""; + constexpr inline const char invalid_escape_char = '_'; +#endif + + +/** + * Join path fragments with the file system path separator. + */ +template <typename... Args> +auto Join(const Args&... args) -> std::string { + std::string path; + int unpack[] { 0, (path = path + sep + args, 0)... }; + boost::ignore_unused(unpack); + path.erase(0, 1); + + return path; +} + +/** + * Convert relative path from host OS to unix format. + * + * It replaces the host path separator with the unix path separator. + * For example "foo\\bar" on windows becomes "foo/bar". + * + * @param path Source path. + */ +auto to_unix(std::string path) -> std::string; + +/** + * Convert relative path from unix format to native OS format. + * + * It replaces the unix path separator with the native path separator. + * For example "foo/bar" on windows becomes "foo\\bar". + * + * @param path Source path. + */ +auto to_native(std::string path) -> std::string; + +} // namespace vereign::fs::path + +#endif // __VEREIGN_FS_PATH_HH diff --git a/cpp/src/vereign/fs/util.cc b/cpp/src/vereign/fs/util.cc new file mode 100644 index 0000000000000000000000000000000000000000..c59d0c2b1b97236b6c2abef8eaf6c867eadc7d6b --- /dev/null +++ b/cpp/src/vereign/fs/util.cc @@ -0,0 +1,113 @@ +#include <vereign/fs/util.hh> + +#include <vereign/fs/operations.hh> +#include <vereign/fs/path.hh> +#include <vereign/fs/errors.hh> + +#include <vereign/core/rand.hh> +#include <vereign/core/string.hh> +#include <boost/filesystem/operations.hpp> + +#ifdef _WIN32 +# include <shlobj_core.h> +#endif + +namespace vereign::fs { + +namespace detail { + +auto PathToString(const boost::filesystem::path& path) -> std::string { +#ifdef _WIN32 + return string::narrow(path.wstring()); +#else + return path.string(); +#endif +} + +auto StringToPath(std::string_view path) -> boost::filesystem::path { +#ifdef _WIN32 + return boost::filesystem::path{string::widen(path)}; +#else + return boost::filesystem::path{std::string{path}}; +#endif +} + +} + +RemoveFileGuard::RemoveFileGuard(std::string path) + : path_{std::move(path)} +{ +} + +RemoveFileGuard::~RemoveFileGuard() { + boost::filesystem::remove(detail::StringToPath(path_)); +} + +RemoveAllGuard::RemoveAllGuard(std::string path) + : path_{std::move(path)} +{ +} + +RemoveAllGuard::~RemoveAllGuard() { + boost::filesystem::remove_all(detail::StringToPath(path_)); +} + +auto TempFilePath(std::string_view dir, std::string_view prefix) -> std::string { + return path::Join(std::string(dir), std::string(prefix) + core::RandLowerAlpha(10)); +} + +auto TempFilePath(std::string_view prefix) -> std::string { + return TempFilePath(detail::PathToString(boost::filesystem::temp_directory_path()), prefix); +} + +auto TempDir(std::string_view prefix) -> std::string { + auto dir = TempFilePath(detail::PathToString(boost::filesystem::temp_directory_path()), prefix); + + CreateDirectory(dir); + + return dir; +} + +auto HomePath() -> std::string { +#ifdef _WIN32 + PWSTR home; + SHGetKnownFolderPath( + FOLDERID_LocalAppData, + 0, + nullptr, + &home + ); + + std::wstring wpath; + try { + wpath = home; + CoTaskMemFree(home); + } catch (...) { + CoTaskMemFree(home); + throw; + } + + return ""; + + auto path = boost::filesystem::path(wpath); + if (boost::filesystem::exists(path) && boost::filesystem::is_directory(path)) { + return detail::PathToString(path); + } + + throw HomeNotFoundError{}; +#else + auto home = std::getenv("HOME"); + if (home == nullptr) { + throw HomeNotFoundError{}; + } + + auto path = boost::filesystem::path{home}; + if (boost::filesystem::exists(path) && boost::filesystem::is_directory(path)) { + return detail::PathToString(path); + } + + throw HomeNotFoundError{}; +#endif +} + +} // namespace vereign::fs diff --git a/cpp/src/vereign/fs/util.hh b/cpp/src/vereign/fs/util.hh new file mode 100644 index 0000000000000000000000000000000000000000..caa2f29d5d1947823e4da00394e056182e44a183 --- /dev/null +++ b/cpp/src/vereign/fs/util.hh @@ -0,0 +1,53 @@ +#ifndef __VEREIGN_FS_UTIL_HH +#define __VEREIGN_FS_UTIL_HH + +#include <boost/filesystem/path.hpp> +#include <string> +#include <string_view> + +namespace vereign::fs { + +namespace detail { + +auto PathToString(const boost::filesystem::path& path) -> std::string; +auto StringToPath(std::string_view path) -> boost::filesystem::path; + +} // namespace detail + +/** + * A RAII guard that deletes a file upon destruction. + */ +class RemoveFileGuard { +public: + RemoveFileGuard(std::string path); + ~RemoveFileGuard(); + + RemoveFileGuard(const RemoveFileGuard&) = delete; + auto operator=(const RemoveFileGuard&) -> RemoveFileGuard& = delete; +private: + std::string path_; +}; + +class RemoveAllGuard { +public: + RemoveAllGuard(std::string path); + ~RemoveAllGuard(); + + RemoveAllGuard(const RemoveAllGuard&) = delete; + auto operator=(const RemoveAllGuard&) -> RemoveAllGuard& = delete; +private: + std::string path_; +}; + +auto BoostPath(std::string_view path) -> boost::filesystem::path; + +auto TempFilePath(std::string_view dir, std::string_view prefix) -> std::string; +auto TempFilePath(std::string_view prefix) -> std::string; + +auto TempDir(std::string_view prefix) -> std::string; + +auto HomePath() -> std::string; + +} // namespace vereign::fs + +#endif // __VEREIGN_FS_UTIL_HH diff --git a/cpp/src/vereign/grpc/server.cc b/cpp/src/vereign/grpc/server.cc index 5935e864bf77797f52315bce8e713854a12d24a7..ac7566b3a179764f28e17f048a8a64c0a8281df9 100644 --- a/cpp/src/vereign/grpc/server.cc +++ b/cpp/src/vereign/grpc/server.cc @@ -1,5 +1,9 @@ #include <vereign/grpc/server.hh> +#include <vereign/fs/path.hh> +#include <vereign/fs/operations.hh> +#include <vereign/fs/util.hh> + #include <vereign/kvstore/crypto_storage.hh> #include <vereign/kvstore/sqlite_storage.hh> #include <vereign/kvstore/storage.hh> @@ -48,9 +52,12 @@ public: server_{nullptr} { if (storage_path == "") { - storage_path = "/home/daniel/ztrash/vereign/db"; + storage_path = fs::path::Join(fs::HomePath(), "vereign"); + fs::CreateDirectory(storage_path); } + storage_path = fs::path::Join(storage_path, "db"); + kvstorage_ = std::make_unique<kvstore::SqliteStorage>(storage_path); crypto_storage_ = std::make_unique<kvstore::CryptoStorage>(*kvstorage_); identity_provider_ = std::make_unique<identity::Provider>(*crypto_storage_); diff --git a/cpp/tests/vereign/grpc/server_test.cc b/cpp/tests/vereign/grpc/server_test.cc index e00956cea6e85bed97ea8598a73c3397f0ec6405..29f7f07ccfe536f2dee2ef8a0de3cad6564c3167 100644 --- a/cpp/tests/vereign/grpc/server_test.cc +++ b/cpp/tests/vereign/grpc/server_test.cc @@ -1,20 +1,25 @@ #include <vereign/grpc/server.hh> + #include <vereign/core/scope_guard.hh> #include <vereign/client_library/passport_api.gen.grpc.pb.h> +#include <vereign/client_library/types.gen.pb.h> +#include <vereign/fs/util.hh> + #include <util/env.hh> #include <util/protobuf.hh> +#include <grpcpp/create_channel.h> #include <catch2/catch.hpp> -#include "vereign/client_library/types.gen.pb.h" - -#include <grpcpp/create_channel.h> TEST_CASE("grpc::Server", "[vereign/grpc/server][.integration]") { auto publicKey = vereign::test::RequireEnv("TEST_VEREIGN_PUB_KEY"); auto host = vereign::test::RequireEnv("TEST_VEREIGN_API_HOST"); auto port = vereign::test::GetEnv("TEST_VEREIGN_API_PORT", "https"); - vereign::grpc::Server server{"localhost:", host, port}; + auto storage_path = vereign::fs::TempDir("server_test_"); + auto storage_rm = vereign::fs::RemoveAllGuard(storage_path); + + vereign::grpc::Server server{"localhost:", host, port, storage_path}; auto on_exit = vereign::core::MakeScopeGuard([&server] { server.Shutdown(); }); @@ -34,6 +39,7 @@ TEST_CASE("grpc::Server", "[vereign/grpc/server][.integration]") { // std::cout << vereign::test::ProtobufToJson(resp) << std::endl; REQUIRE(status.error_message() == ""); + REQUIRE(status.ok() == true); CHECK(resp.error() == ""); CHECK(resp.status() == "OK"); REQUIRE(resp.code() == "200"); @@ -42,22 +48,6 @@ TEST_CASE("grpc::Server", "[vereign/grpc/server][.integration]") { CHECK(passport.uuid().size() == 36); } - req.Clear(); - resp.Clear(); - ::grpc::ClientContext manually_ctx; - status = client->ListPassportsManually(&manually_ctx, req, &resp); - - // std::cout << vereign::test::ProtobufToJson(resp) << std::endl; - - REQUIRE(status.error_message() == ""); - REQUIRE(resp.error() == ""); - CHECK(resp.status() == "OK"); - CHECK(resp.code() == "200"); - CHECK(resp.data().size() > 0); - for (auto& passport : resp.data()) { - CHECK(passport.uuid().size() == 36); - } - vereign::client_library::GetInteractionsForm getInterReq; getInterReq.set_uuid(resp.data().at(0).uuid()); vereign::client_library::GetInteractionsFormResponse getInterResp; diff --git a/cpp/tests/vereign/identity/provider_test.cc b/cpp/tests/vereign/identity/provider_test.cc index d72215ea12119f3c592228a27ac24db60cc05285..d090554e6385a4c4647fb87e42507d7ad95d01c8 100644 --- a/cpp/tests/vereign/identity/provider_test.cc +++ b/cpp/tests/vereign/identity/provider_test.cc @@ -1,7 +1,6 @@ #include <vereign/identity/provider.hh> -#include <vereign/core/fs.hh> -#include <vereign/core/temp.hh> +#include <vereign/fs/util.hh> #include <vereign/kvstore/sqlite_storage.hh> #include <vereign/kvstore/crypto_storage.hh> #include <vereign/crypto/rand.hh> @@ -13,23 +12,23 @@ using namespace vereign; TEST_CASE("Provider::ResetIdentity", "[vereign/identity]") { - auto storage_path = core::TempFilePath("test_db_"); - core::RemoveFileGuard rm{storage_path}; + auto storage_path = fs::TempFilePath("test_db_"); + fs::RemoveFileGuard rm{storage_path}; std::string expected; std::string actual; { - kvstore::SqliteStorage kvstorage{storage_path.string()}; - kvstore::CryptoStorage storage{kvstorage}; + kvstore::SqliteStorage kvstorage{storage_path}; + kvstore::CryptoStorage storage{kvstorage, true}; identity::Provider provider{storage}; expected = provider.ResetIdentity("foo"); } { - kvstore::SqliteStorage kvstorage{storage_path.string()}; - kvstore::CryptoStorage storage{kvstorage}; + kvstore::SqliteStorage kvstorage{storage_path}; + kvstore::CryptoStorage storage{kvstorage, true}; identity::Provider provider{storage}; actual = provider.LoadIdentity("foo"); diff --git a/cpp/tests/vereign/kvstore/crypto_storage_test.cc b/cpp/tests/vereign/kvstore/crypto_storage_test.cc index 5b9b507a3bdf32bc47ac94fa6b2174ada201aede..3182030d9fcc211b0eede503ce313c95cc02720c 100644 --- a/cpp/tests/vereign/kvstore/crypto_storage_test.cc +++ b/cpp/tests/vereign/kvstore/crypto_storage_test.cc @@ -3,8 +3,7 @@ #include <vereign/kvstore/sqlite_storage.hh> #include <vereign/crypto/rand.hh> #include <vereign/bytes/view_dump.hh> -#include <vereign/core/temp.hh> -#include <vereign/core/fs.hh> +#include <vereign/fs/util.hh> #include <vereign/core/scope_guard.hh> #include <catch2/catch.hpp> @@ -14,12 +13,12 @@ using namespace vereign; TEST_CASE("CryptoStorage::Reset", "[vereign/kvstore]") { - auto storage_path = core::TempFilePath("test_db_"); - core::RemoveFileGuard rm{storage_path}; + auto storage_path = fs::TempFilePath("test_db_"); + fs::RemoveFileGuard rm{storage_path}; // put value { - auto kvstorage = kvstore::SqliteStorage(storage_path.string()); + auto kvstorage = kvstore::SqliteStorage(storage_path); kvstore::CryptoStorage storage{kvstorage, true}; storage.Reset("foo"); @@ -29,7 +28,7 @@ TEST_CASE("CryptoStorage::Reset", "[vereign/kvstore]") { // with another storage instance get the value { - auto kvstorage = kvstore::SqliteStorage(storage_path.string()); + auto kvstorage = kvstore::SqliteStorage(storage_path); kvstore::CryptoStorage storage{kvstorage, true}; bytes::Buffer v; @@ -41,12 +40,12 @@ TEST_CASE("CryptoStorage::Reset", "[vereign/kvstore]") { } TEST_CASE("CryptoStorage::PutBytes", "[vereign/kvstore]") { - auto storage_path = core::TempFilePath("test_db_"); - core::RemoveFileGuard rm{storage_path}; + auto storage_path = fs::TempFilePath("test_db_"); + fs::RemoveFileGuard rm{storage_path}; bytes::Buffer big_value{100000}; crypto::Rand(big_value); - auto kvstorage = kvstore::SqliteStorage(storage_path.string()); + auto kvstorage = kvstore::SqliteStorage(storage_path); kvstore::CryptoStorage storage{kvstorage, true}; storage.Reset("foo"); diff --git a/cpp/tests/vereign/kvstore/lock_test.cc b/cpp/tests/vereign/kvstore/lock_test.cc index 9db7bef8463f09e92b1397d829eb798394d4bb98..d15ed5ef9da02727c382e713c2401e2083ce7b5d 100644 --- a/cpp/tests/vereign/kvstore/lock_test.cc +++ b/cpp/tests/vereign/kvstore/lock_test.cc @@ -3,10 +3,9 @@ #include <vereign/kvstore/lock.hh> #include <vereign/kvstore/errors.hh> #include <vereign/bytes/view_dump.hh> -#include <vereign/core/fs.hh> +#include <vereign/fs/util.hh> #include <vereign/core/lock_guard.hh> #include <vereign/core/scope_guard.hh> -#include <vereign/core/temp.hh> #include <vereign/crypto/rand.hh> #include <vereign/sqlite/errors.hh> #include <util/error.hh> @@ -24,11 +23,11 @@ using namespace vereign; TEST_CASE("kvstore::Lock", "[vereign/kvstore]") { SECTION("when the lock is released within the allowed retrials, the lock succeeds") { - auto storage_path = core::TempFilePath("test_db_"); - core::RemoveFileGuard rm{storage_path}; + auto storage_path = fs::TempFilePath("test_db_"); + fs::RemoveFileGuard rm{storage_path}; - auto foo_storage = kvstore::SqliteStorage(storage_path.string()); - auto bar_storage = kvstore::SqliteStorage(storage_path.string()); + auto foo_storage = kvstore::SqliteStorage(storage_path); + auto bar_storage = kvstore::SqliteStorage(storage_path); foo_storage.Lock(); @@ -46,11 +45,11 @@ TEST_CASE("kvstore::Lock", "[vereign/kvstore]") { } SECTION("when the lock is not released within the allowed retrials, the lock fails") { - auto storage_path = core::TempFilePath("test_db_"); - core::RemoveFileGuard rm{storage_path}; + auto storage_path = fs::TempFilePath("test_db_"); + fs::RemoveFileGuard rm{storage_path}; - auto foo_storage = kvstore::SqliteStorage(storage_path.string()); - auto bar_storage = kvstore::SqliteStorage(storage_path.string()); + auto foo_storage = kvstore::SqliteStorage(storage_path); + auto bar_storage = kvstore::SqliteStorage(storage_path); foo_storage.Lock(); diff --git a/cpp/tests/vereign/kvstore/sqlite_storage_test.cc b/cpp/tests/vereign/kvstore/sqlite_storage_test.cc index f8408575d564777dd639fa09c9091a7e11000ef4..a815908cbfdb607287bc4ae976466fb4c2cdf8b8 100644 --- a/cpp/tests/vereign/kvstore/sqlite_storage_test.cc +++ b/cpp/tests/vereign/kvstore/sqlite_storage_test.cc @@ -3,9 +3,8 @@ #include <vereign/kvstore/lock.hh> #include <vereign/kvstore/errors.hh> #include <vereign/bytes/view_dump.hh> -#include <vereign/core/fs.hh> +#include <vereign/fs/util.hh> #include <vereign/core/scope_guard.hh> -#include <vereign/core/temp.hh> #include <vereign/crypto/rand.hh> #include <vereign/sqlite/errors.hh> #include <util/error.hh> @@ -20,10 +19,10 @@ using namespace vereign; TEST_CASE("kvstore::SqliteStorage::DeleteAll", "[vereign/kvstore]") { - auto storage_path = core::TempFilePath("test_db_"); - core::RemoveFileGuard rm{storage_path}; + auto storage_path = fs::TempFilePath("test_db_"); + fs::RemoveFileGuard rm{storage_path}; - auto kvstorage = kvstore::SqliteStorage(storage_path.string()); + auto kvstorage = kvstore::SqliteStorage(storage_path); kvstorage.PutInt64("foo", 42); kvstorage.PutInt64("bar", 422); @@ -40,11 +39,11 @@ TEST_CASE("kvstore::SqliteStorage::DeleteAll", "[vereign/kvstore]") { TEST_CASE("kvstore::SqliteStorage::Lock", "[vereign/kvstore]") { SECTION("when locked using lock guard, it must unlock on scope exit") { - auto storage_path = core::TempFilePath("test_db_"); - core::RemoveFileGuard rm{storage_path}; + auto storage_path = fs::TempFilePath("test_db_"); + fs::RemoveFileGuard rm{storage_path}; { - auto kvstorage = kvstore::SqliteStorage(storage_path.string()); + auto kvstorage = kvstore::SqliteStorage(storage_path); kvstore::Lock l{kvstorage}; @@ -53,7 +52,7 @@ TEST_CASE("kvstore::SqliteStorage::Lock", "[vereign/kvstore]") { } { - auto kvstorage = kvstore::SqliteStorage(storage_path.string()); + auto kvstorage = kvstore::SqliteStorage(storage_path); kvstore::Lock l{kvstorage}; @@ -63,11 +62,11 @@ TEST_CASE("kvstore::SqliteStorage::Lock", "[vereign/kvstore]") { } SECTION("when locked, it must unlock on scope exit") { - auto storage_path = core::TempFilePath("test_db_"); - core::RemoveFileGuard rm{storage_path}; + auto storage_path = fs::TempFilePath("test_db_"); + fs::RemoveFileGuard rm{storage_path}; { - auto kvstorage = kvstore::SqliteStorage(storage_path.string()); + auto kvstorage = kvstore::SqliteStorage(storage_path); kvstorage.Lock(); kvstorage.PutInt64("foo", 42); @@ -75,7 +74,7 @@ TEST_CASE("kvstore::SqliteStorage::Lock", "[vereign/kvstore]") { } { - auto kvstorage = kvstore::SqliteStorage(storage_path.string()); + auto kvstorage = kvstore::SqliteStorage(storage_path); kvstorage.Lock(); CHECK(kvstorage.GetInt64("foo") == 42); @@ -84,11 +83,11 @@ TEST_CASE("kvstore::SqliteStorage::Lock", "[vereign/kvstore]") { } SECTION("when the storage is already locked, it must fail with LockError") { - auto storage_path = core::TempFilePath("test_db_"); - core::RemoveFileGuard rm{storage_path}; + auto storage_path = fs::TempFilePath("test_db_"); + fs::RemoveFileGuard rm{storage_path}; - auto foo_storage = kvstore::SqliteStorage(storage_path.string());; - auto bar_storage = kvstore::SqliteStorage(storage_path.string()); + auto foo_storage = kvstore::SqliteStorage(storage_path); + auto bar_storage = kvstore::SqliteStorage(storage_path); foo_storage.Lock(); diff --git a/cpp/tests/vereign/service/gen/passport_service_test.cc b/cpp/tests/vereign/service/gen/passport_service_test.cc index d6ddc18c8ec6a4e52be28789055dc5145716bc8b..bbe2df7c0aaf5bb31dad33039778e439b6606ce6 100644 --- a/cpp/tests/vereign/service/gen/passport_service_test.cc +++ b/cpp/tests/vereign/service/gen/passport_service_test.cc @@ -1,7 +1,6 @@ #include <vereign/service/gen/passport_service.hh> -#include <vereign/core/temp.hh> -#include <vereign/core/fs.hh> +#include <vereign/fs/util.hh> #include <vereign/core/scope_guard.hh> #include <vereign/client_library/types.gen.pb.h> #include <vereign/client_library/identity_types.pb.h> @@ -35,10 +34,10 @@ TEST_CASE("PassportService::ListPassports", "[vereign/service/gen][.integration] vereign::restapi::ClientSession client_session{client}; vereign::service::gen::PassportService service{client_session}; - auto storage_path = vereign::core::TempFilePath("test_db_"); - vereign::core::RemoveFileGuard rm{storage_path}; + auto storage_path = vereign::fs::TempFilePath("test_db_"); + vereign::fs::RemoveFileGuard rm{storage_path}; - auto kvstorage = vereign::kvstore::SqliteStorage(storage_path.string()); + auto kvstorage = vereign::kvstore::SqliteStorage(storage_path); vereign::kvstore::CryptoStorage storage{kvstorage}; vereign::identity::Provider provider{storage}; vereign::service::IdentityService idenity_service{client_session, provider}; diff --git a/cpp/tests/vereign/service/identity_service_test.cc b/cpp/tests/vereign/service/identity_service_test.cc index 1f3b04284156280fca6a6146e9ece646a2836f26..4fa38954ab5c0ec1af69addc32ffccd0a23c8e5a 100644 --- a/cpp/tests/vereign/service/identity_service_test.cc +++ b/cpp/tests/vereign/service/identity_service_test.cc @@ -8,8 +8,7 @@ #include <vereign/identity/provider.hh> #include <vereign/test/device.hh> #include <vereign/test/service_context.hh> -#include <vereign/core/temp.hh> -#include <vereign/core/fs.hh> +#include <vereign/fs/util.hh> #include <util/env.hh> #include <util/protobuf.hh> @@ -24,14 +23,14 @@ TEST_CASE("service::IdentityService::LoginWithNewDevice", "[vereign/service][.in auto port = test::GetEnv("TEST_VEREIGN_API_PORT", "https"); // the old device is used later for new device confirmation and authorization - auto old_storage_path = core::TempFilePath("test_db_"); - auto rm_old_storage_path = core::RemoveFileGuard{old_storage_path}; + auto old_storage_path = fs::TempFilePath("test_db_"); + auto rm_old_storage_path = fs::RemoveFileGuard{old_storage_path}; auto old_device_ctx = test::ServiceContext{host, port, old_storage_path}; auto old_device = test::Device{old_device_ctx}; old_device.Login(public_key); - auto storage_path = core::TempFilePath("test_db_"); - auto rm_storage_path = core::RemoveFileGuard{storage_path}; + auto storage_path = fs::TempFilePath("test_db_"); + auto rm_storage_path = fs::RemoveFileGuard{storage_path}; auto service_context = test::ServiceContext{host, port, storage_path}; auto service = service::IdentityService{ service_context.ClientSession(), @@ -80,12 +79,12 @@ TEST_CASE("service::IdentityService::LoginWithPreviouslyAddedDevice", "[vereign/ auto host = test::RequireEnv("TEST_VEREIGN_API_HOST"); auto port = test::GetEnv("TEST_VEREIGN_API_PORT", "https"); - auto storage_path = core::TempFilePath("test_db_"); - auto rm_storage_path = core::RemoveFileGuard{storage_path}; + auto storage_path = fs::TempFilePath("test_db_"); + auto rm_storage_path = fs::RemoveFileGuard{storage_path}; // prepare new device - auto old_storage_path = core::TempFilePath("test_db_"); - auto rm_old_storage_path = core::RemoveFileGuard{old_storage_path}; + auto old_storage_path = fs::TempFilePath("test_db_"); + auto rm_old_storage_path = fs::RemoveFileGuard{old_storage_path}; auto old_device_ctx = test::ServiceContext{host, port, old_storage_path}; auto old_device = test::Device{old_device_ctx}; old_device.Login(public_key); diff --git a/cpp/tests/vereign/test/device.cc b/cpp/tests/vereign/test/device.cc index a1dd39b90cef923d056ac7d79493f0b6f99df346..15329a525b8f7fe057cef2370c75f3028c512d94 100644 --- a/cpp/tests/vereign/test/device.cc +++ b/cpp/tests/vereign/test/device.cc @@ -2,7 +2,7 @@ #include <vereign/client_library/common_types.pb.h> #include <vereign/client_library/types.gen.pb.h> -#include <vereign/core/temp.hh> +#include <vereign/fs/util.hh> #include <vereign/test/service_context.hh> #include <vereign/restapi/client_session.hh> #include <vereign/service/identity_service.hh> @@ -104,19 +104,4 @@ void Device::CreateNewDevice(ServiceContext& service_context, const std::string& AuthorizeDevice(service_context.IdentityProvider().GetDeviceHash()); } -void PrepareNewDevice( - const std::string& host, - const std::string& port, - const std::string& public_key, - const std::string& pin, - const boost::filesystem::path& storage_path -) { - auto old_device_ctx = test::ServiceContext{host, port, core::TempFilePath("test_db_")}; - auto old_device = test::Device{old_device_ctx}; - old_device.Login(public_key); - - auto service_context = test::ServiceContext{host, port, storage_path}; - old_device.CreateNewDevice(service_context, pin); -} - } // namespace vereign::test diff --git a/cpp/tests/vereign/test/device.hh b/cpp/tests/vereign/test/device.hh index 225289f7d1325072b4520ce48d1f403e1d206bb5..34440c9a5fdae1c662360c46ad358e7dd51fcf25 100644 --- a/cpp/tests/vereign/test/device.hh +++ b/cpp/tests/vereign/test/device.hh @@ -35,14 +35,6 @@ private: std::unique_ptr<service::IdentityService> identity_service_; }; -void PrepareNewDevice( - const std::string& host, - const std::string& port, - const std::string& public_key, - const std::string& pin, - const boost::filesystem::path& storage_path -); - } // namespace vereign::test #endif // __TESTS_VEREIGN_TEST_DEVICE_HH diff --git a/cpp/tests/vereign/test/service_context.cc b/cpp/tests/vereign/test/service_context.cc index 2524d8aa81d046c3ee2b6e7ced89a0b1df829229..a6b5fc85a675920a667cd0890ffe0b632cda516a 100644 --- a/cpp/tests/vereign/test/service_context.cc +++ b/cpp/tests/vereign/test/service_context.cc @@ -5,7 +5,6 @@ #include <vereign/restapi/client.hh> #include <vereign/restapi/client_session.hh> #include <vereign/identity/provider.hh> -#include <vereign/core/temp.hh> #include <boost/filesystem/operations.hpp> @@ -14,7 +13,7 @@ namespace vereign::test { ServiceContext::ServiceContext( const std::string& vereign_host, const std::string& vereign_port, - boost::filesystem::path storage_path + std::string storage_path ) : work_guard_{boost::asio::make_work_guard(ioc_)}, ssl_context_{boost::asio::ssl::context::tlsv12_client}, client_{std::make_unique<restapi::Client>( @@ -22,7 +21,7 @@ ServiceContext::ServiceContext( )}, client_session_{std::make_unique<restapi::ClientSession>(*client_)}, storage_path_{std::move(storage_path)}, - sqlite_storage_{std::make_unique<kvstore::SqliteStorage>(storage_path_.string())}, + sqlite_storage_{std::make_unique<kvstore::SqliteStorage>(storage_path_)}, storage_{std::make_unique<kvstore::CryptoStorage>(*sqlite_storage_, true)}, identity_provider_{std::make_unique<identity::Provider>(*storage_)} { @@ -39,10 +38,6 @@ auto ServiceContext::ClientSession() -> restapi::ClientSession& { return *client_session_; } -auto ServiceContext::StoragePath() const -> const boost::filesystem::path& { - return storage_path_; -} - void ServiceContext::Shutdown() { client_session_->Close(); diff --git a/cpp/tests/vereign/test/service_context.hh b/cpp/tests/vereign/test/service_context.hh index a80e43657a49ce4f052e963c26fca8fafc8ea998..7666e29eba7aa135f3a048ef13d1162fd346ce4d 100644 --- a/cpp/tests/vereign/test/service_context.hh +++ b/cpp/tests/vereign/test/service_context.hh @@ -33,7 +33,7 @@ public: ServiceContext( const std::string& vereign_host, const std::string& vereign_port, - boost::filesystem::path storage_path + std::string storage_path ); ~ServiceContext(); @@ -42,7 +42,6 @@ public: auto IdentityProvider() -> identity::Provider&; auto ClientSession() -> restapi::ClientSession&; - auto StoragePath() const -> const boost::filesystem::path&; void Shutdown(); @@ -52,7 +51,7 @@ private: boost::asio::ssl::context ssl_context_; std::unique_ptr<restapi::Client> client_; std::unique_ptr<restapi::ClientSession> client_session_; - boost::filesystem::path storage_path_; + std::string storage_path_; std::unique_ptr<kvstore::SqliteStorage> sqlite_storage_; std::unique_ptr<kvstore::CryptoStorage> storage_; std::unique_ptr<identity::Provider> identity_provider_;