From f650bd4e3986ef2de2c36cc519c061e78812f222 Mon Sep 17 00:00:00 2001
From: Daniel Lyubomirov <dennislt@gmail.com>
Date: Wed, 24 Jun 2020 13:13:18 +0300
Subject: [PATCH] [17] crypto storage windows fixes

---
 cpp/CMakeLists.txt                            |  2 --
 cpp/src/CMakeLists.txt                        |  4 ++++
 cpp/src/vereign/bytes/view.hh                 |  7 ++++---
 cpp/src/vereign/kvstore/crypto_storage.cc     |  3 ++-
 .../vereign/service/identity_service_test.cc  | 21 +++++++++++++------
 cpp/tests/vereign/test/service_context.cc     |  2 --
 6 files changed, 25 insertions(+), 14 deletions(-)

diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt
index 3a833a5..35a77a1 100644
--- a/cpp/CMakeLists.txt
+++ b/cpp/CMakeLists.txt
@@ -142,8 +142,6 @@ else()
 endif()
 
 find_package(nlohmann_json 3.7.3 REQUIRED)
-
-set(SQLite3_DIR ${VENDOR_INSTALL_DIR})
 find_package(SQLite3 REQUIRED)
 
 add_subdirectory("src")
diff --git a/cpp/src/CMakeLists.txt b/cpp/src/CMakeLists.txt
index 62c0db6..c97a227 100644
--- a/cpp/src/CMakeLists.txt
+++ b/cpp/src/CMakeLists.txt
@@ -93,6 +93,10 @@ if (LINUX)
   list(APPEND VEREIGNLIB_SRC
     vereign/kvstore/detail/linux_crypto_storage.cc
   )
+elseif (WIN32)
+  list(APPEND VEREIGNLIB_SRC
+    vereign/kvstore/detail/linux_crypto_storage.cc
+  )
 endif()
 
 # file(GLOB GENERATED_SERVICES_SRC vereign/service/gen/*.cc)
diff --git a/cpp/src/vereign/bytes/view.hh b/cpp/src/vereign/bytes/view.hh
index fb3f5b9..ddf1c75 100644
--- a/cpp/src/vereign/bytes/view.hh
+++ b/cpp/src/vereign/bytes/view.hh
@@ -5,6 +5,7 @@
 #include <string>
 #include <string_view>
 #include <stdexcept>
+#include <algorithm>
 
 namespace vereign::bytes {
 
@@ -61,7 +62,7 @@ public:
     return size_;
   }
 
-  auto String() noexcept -> std::string_view {
+  auto String() const noexcept -> std::string_view {
     return std::string_view{CharData(), size_};
   }
 
@@ -82,8 +83,8 @@ public:
   }
 
 private:
-  std::size_t size_;
-  const uint8_t* data_;
+  std::size_t size_ = 0;
+  const uint8_t* data_ = nullptr;
 };
 
 } // namespace vereign::bytes
diff --git a/cpp/src/vereign/kvstore/crypto_storage.cc b/cpp/src/vereign/kvstore/crypto_storage.cc
index 4a24ea5..20b2957 100644
--- a/cpp/src/vereign/kvstore/crypto_storage.cc
+++ b/cpp/src/vereign/kvstore/crypto_storage.cc
@@ -1,7 +1,8 @@
 #include <vereign/kvstore/crypto_storage.hh>
 
 #if defined(_WIN32)
-# include <vereign/kvstore/detail/win_crypto_storage.hh>
+// # include <vereign/kvstore/detail/win_crypto_storage.hh>
+# include <vereign/kvstore/detail/linux_crypto_storage.hh>
 #else
 # include <vereign/kvstore/detail/linux_crypto_storage.hh>
 #endif
diff --git a/cpp/tests/vereign/service/identity_service_test.cc b/cpp/tests/vereign/service/identity_service_test.cc
index f793944..7c94edc 100644
--- a/cpp/tests/vereign/service/identity_service_test.cc
+++ b/cpp/tests/vereign/service/identity_service_test.cc
@@ -9,6 +9,7 @@
 #include <vereign/test/device.hh>
 #include <vereign/test/service_context.hh>
 #include <vereign/core/temp.hh>
+#include <vereign/core/fs.hh>
 
 #include <util/env.hh>
 #include <util/protobuf.hh>
@@ -23,11 +24,15 @@ 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_device_ctx = test::ServiceContext{host, port, core::TempFilePath("test_db_")};
+  auto old_storage_path = core::TempFilePath("test_db_");
+  auto rm_old_storage_path = core::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 service_context = test::ServiceContext{host, port, core::TempFilePath("test_db_")};
+  auto storage_path = core::TempFilePath("test_db_");
+  auto rm_storage_path = core::RemoveFileGuard{storage_path};
+  auto service_context = test::ServiceContext{host, port, storage_path};
   auto service = service::IdentityService{
     service_context.IdentityProvider(),
     service_context.ClientSession()
@@ -65,7 +70,7 @@ TEST_CASE("service::IdentityService::LoginWithNewDevice", "[vereign/service][.in
   CHECK(list->status() == "OK");
   REQUIRE(list->code() == "200");
   CHECK(list->data().size() > 0);
-  for (auto& passport : list->data()) {
+  for (const auto& passport : list->data()) {
     CHECK(passport.uuid().size() == 36);
   }
 }
@@ -75,9 +80,13 @@ TEST_CASE("service::IdentityService::LoginWithPreviouslyAddedDevice", "[vereign/
   auto host = test::RequireEnv("TEST_VEREIGN_API_HOST");
   auto port = test::GetEnv("TEST_VEREIGN_API_PORT", "https");
 
-  // prepare new device
   auto storage_path = core::TempFilePath("test_db_");
-  auto old_device_ctx = test::ServiceContext{host, port, core::TempFilePath("test_db_")};
+  auto rm_storage_path = core::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_device_ctx = test::ServiceContext{host, port, old_storage_path};
   auto old_device = test::Device{old_device_ctx};
   old_device.Login(public_key);
   auto old_service_context = test::ServiceContext{host, port, storage_path};
@@ -115,7 +124,7 @@ TEST_CASE("service::IdentityService::LoginWithPreviouslyAddedDevice", "[vereign/
   CHECK(list->status() == "OK");
   REQUIRE(list->code() == "200");
   CHECK(list->data().size() > 0);
-  for (auto& passport : list->data()) {
+  for (const auto& passport : list->data()) {
     CHECK(passport.uuid().size() == 36);
   }
 }
diff --git a/cpp/tests/vereign/test/service_context.cc b/cpp/tests/vereign/test/service_context.cc
index 7f326c9..0e76643 100644
--- a/cpp/tests/vereign/test/service_context.cc
+++ b/cpp/tests/vereign/test/service_context.cc
@@ -50,8 +50,6 @@ void ServiceContext::Shutdown() {
   if (service_thread_.joinable()) {
     service_thread_.join();
   }
-
-  boost::filesystem::remove(storage_path_);
 }
 
 ServiceContext::~ServiceContext() {
-- 
GitLab