updated build system
	
		
			
	
		
	
	
		
	
		
			Some checks failed
		
		
	
	
		
			
				
	
				BlockStorage/repertory/pipeline/head There was a failure building this commit
				
			
		
		
	
	
				
					
				
			
		
			Some checks failed
		
		
	
	BlockStorage/repertory/pipeline/head There was a failure building this commit
				
			This commit is contained in:
		| @@ -20,10 +20,12 @@ | ||||
|   SOFTWARE. | ||||
| */ | ||||
| #include "gtest/gtest.h" | ||||
| #include <utils/collection.hpp> | ||||
|  | ||||
| #include "utils/common.hpp" | ||||
|  | ||||
| #include "utils/collection.hpp" | ||||
| #include "utils/string.hpp" | ||||
|  | ||||
| namespace repertory { | ||||
| TEST(utils_common, calculate_read_size) { | ||||
|   auto read_size = utils::calculate_read_size(0U, 0U, 0U); | ||||
| @@ -240,4 +242,72 @@ TEST(utils_common, generate_random_between_throws_error_on_invalid_range) { | ||||
|       }, | ||||
|       std::range_error); | ||||
| } | ||||
|  | ||||
| #if defined(PROJECT_ENABLE_LIBSODIUM) | ||||
| TEST(utils_common, generate_random_string) { | ||||
|   static constexpr const auto max_iterations{10000L}; | ||||
|  | ||||
|   const auto test_string = [](auto str) { | ||||
|     static std::vector<decltype(str)> list{}; | ||||
|  | ||||
|     EXPECT_FALSE(utils::collection::includes(list, str)); | ||||
|     list.push_back(str); | ||||
|  | ||||
|     EXPECT_EQ(16U, str.size()); | ||||
|     for (auto &&ch : str) { | ||||
|       auto ch_int = static_cast<std::uint32_t>(ch); | ||||
|       EXPECT_GE(ch_int, 48U); | ||||
|       EXPECT_LE(ch_int, 73U + 48U); | ||||
|     } | ||||
|   }; | ||||
|  | ||||
|   for (std::size_t idx = 0U; idx < max_iterations; ++idx) { | ||||
|     test_string(utils::generate_random_string(16U)); | ||||
|     test_string(utils::generate_random_wstring(16U)); | ||||
|   } | ||||
| } | ||||
|  | ||||
| TEST(utils_common, generate_random_string_for_zero_length) { | ||||
|   EXPECT_TRUE(utils::generate_random_string(0U).empty()); | ||||
|   EXPECT_TRUE(utils::generate_random_wstring(0U).empty()); | ||||
| } | ||||
| #endif // defined(PROJECT_ENABLE_LIBSODIUM) | ||||
|  | ||||
| TEST(utils_common, get_environment_variable) { | ||||
|   static constexpr const std::string path_env{"PATH"}; | ||||
|   std::string path; | ||||
|  | ||||
| #if defined(_WIN32) | ||||
|   path.resize(MAX_PATH + 1U); | ||||
|   auto size = ::GetEnvironmentVariableA(path_env.c_str(), path.data(), 0U); | ||||
|  | ||||
|   path.resize(size); | ||||
|   ::GetEnvironmentVariableA(path_env.c_str(), path.data(), | ||||
|                             static_cast<DWORD>(path.size())); | ||||
| #else  // !defined(_WIN32) | ||||
|   path = std::getenv(path_env.c_str()); | ||||
| #endif // defined(_WIN32) | ||||
|  | ||||
|   EXPECT_STREQ(path.c_str(), utils::get_environment_variable(path_env).c_str()); | ||||
|   EXPECT_STREQ( | ||||
|       utils::string::from_utf8(path).c_str(), | ||||
|       utils::get_environment_variable(utils::string::from_utf8(path_env)) | ||||
|           .c_str()); | ||||
| } | ||||
|  | ||||
| #if defined(PROJECT_ENABLE_BOOST) | ||||
| TEST(utils_common, get_next_available_port) { | ||||
|   std::uint16_t available_port{}; | ||||
|   for (std::uint16_t port = 1U; port < 65535; ++port) { | ||||
|     EXPECT_TRUE(utils::get_next_available_port(port, available_port)); | ||||
|     EXPECT_GE(available_port, port); | ||||
|   } | ||||
| } | ||||
|  | ||||
| TEST(utils_common, get_next_available_port_fails_if_starting_point_is_zero) { | ||||
|   std::uint16_t available_port{}; | ||||
|   EXPECT_FALSE(utils::get_next_available_port(0U, available_port)); | ||||
|   EXPECT_EQ(0U, available_port); | ||||
| } | ||||
| #endif // defined(PROJECT_ENABLE_BOOST) | ||||
| } // namespace repertory | ||||
|   | ||||
| @@ -19,7 +19,7 @@ | ||||
|  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||||
|  SOFTWARE. | ||||
| */ | ||||
| #if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) | ||||
| #if defined(PROJECT_ENABLE_LIBSODIUM) | ||||
|  | ||||
| #include "gtest/gtest.h" | ||||
|  | ||||
| @@ -27,8 +27,117 @@ | ||||
| #include "utils/encryption.hpp" | ||||
|  | ||||
| namespace repertory { | ||||
| static const std::string token{"moose"}; | ||||
| static const std::wstring token_w{L"moose"}; | ||||
|  | ||||
| TEST(utils_encryption, generate_key) { | ||||
|   auto key1 = | ||||
|       utils::encryption::generate_key<utils::encryption::hash_256_t>(token); | ||||
|   EXPECT_STREQ( | ||||
|       "182072537ada59e4d6b18034a80302ebae935f66adbdf0f271d3d36309c2d481", | ||||
|       utils::collection::to_hex_string(key1).c_str()); | ||||
|  | ||||
|   auto key2 = | ||||
|       utils::encryption::generate_key<utils::encryption::hash_256_t>("moose"); | ||||
|   auto key3 = | ||||
|       utils::encryption::generate_key<utils::encryption::hash_256_t>("moose"); | ||||
|   EXPECT_EQ(key2, key3); | ||||
|  | ||||
|   auto key4 = | ||||
|       utils::encryption::generate_key<utils::encryption::hash_256_t>("moose2"); | ||||
|   EXPECT_NE(key2, key4); | ||||
|  | ||||
|   auto key1_w = | ||||
|       utils::encryption::generate_key<utils::encryption::hash_256_t>(token_w); | ||||
|   EXPECT_NE(key1, key1_w); | ||||
|   EXPECT_STREQ( | ||||
|       L"590ac70125bec4501172937f6a2cbdeb22a87b5e40d5595eccd06b2b20548d8f", | ||||
|       utils::collection::to_hex_wstring(key1_w).c_str()); | ||||
|  | ||||
|   auto key2_w = | ||||
|       utils::encryption::generate_key<utils::encryption::hash_256_t>(L"moose"); | ||||
|   auto key3_w = | ||||
|       utils::encryption::generate_key<utils::encryption::hash_256_t>(L"moose"); | ||||
|   EXPECT_EQ(key2_w, key3_w); | ||||
|   EXPECT_NE(key2_w, key2); | ||||
|   EXPECT_NE(key3_w, key3); | ||||
|  | ||||
|   auto key4_w = | ||||
|       utils::encryption::generate_key<utils::encryption::hash_256_t>(L"moose2"); | ||||
|   EXPECT_NE(key2_w, key4_w); | ||||
|   EXPECT_NE(key4_w, key4); | ||||
| } | ||||
|  | ||||
| TEST(utils_encryption, generate_key_default_hasher_is_sha256) { | ||||
|   auto key1 = | ||||
|       utils::encryption::generate_key<utils::encryption::hash_256_t>(token); | ||||
|   auto key2 = utils::encryption::generate_key<utils::encryption::hash_256_t>( | ||||
|       token, [](auto &&data, auto &&size) -> auto { | ||||
|         return utils::encryption::create_hash_sha256( | ||||
|             std::string_view(reinterpret_cast<const char *>(data), size)); | ||||
|       }); | ||||
|   EXPECT_EQ(key1, key2); | ||||
|  | ||||
|   auto key1_w = | ||||
|       utils::encryption::generate_key<utils::encryption::hash_256_t>(token_w); | ||||
|   auto key2_w = utils::encryption::generate_key<utils::encryption::hash_256_t>( | ||||
|       token_w, [](auto &&data, auto &&size) -> auto { | ||||
|         return utils::encryption::create_hash_sha256(std::wstring_view( | ||||
|             reinterpret_cast<const wchar_t *>(data), size / sizeof(wchar_t))); | ||||
|       }); | ||||
|   EXPECT_EQ(key1_w, key2_w); | ||||
|  | ||||
|   EXPECT_NE(key1_w, key1); | ||||
|   EXPECT_NE(key2_w, key2); | ||||
| } | ||||
|  | ||||
| TEST(utils_encryption, generate_key_with_hasher) { | ||||
|   auto key1 = utils::encryption::generate_key<utils::encryption::hash_256_t>( | ||||
|       token, [](auto &&data, auto &&size) -> auto { | ||||
|         return utils::encryption::create_hash_sha256( | ||||
|             std::string_view(reinterpret_cast<const char *>(data), size)); | ||||
|       }); | ||||
|   EXPECT_STREQ( | ||||
|       "182072537ada59e4d6b18034a80302ebae935f66adbdf0f271d3d36309c2d481", | ||||
|       utils::collection::to_hex_string(key1).c_str()); | ||||
|  | ||||
|   auto key2 = utils::encryption::generate_key<utils::encryption::hash_256_t>( | ||||
|       token, [](auto &&data, auto &&size) -> auto { | ||||
|         return utils::encryption::create_hash_blake2b_256( | ||||
|             std::string_view(reinterpret_cast<const char *>(data), size)); | ||||
|       }); | ||||
|   EXPECT_NE(key1, key2); | ||||
|  | ||||
|   EXPECT_STREQ( | ||||
|       "ab4a0b004e824962913f7c0f79582b6ec7a3b8726426ca61d1a0a28ce5049e96", | ||||
|       utils::collection::to_hex_string(key2).c_str()); | ||||
|  | ||||
|   auto key1_w = utils::encryption::generate_key<utils::encryption::hash_256_t>( | ||||
|       token_w, [](auto &&data, auto &&size) -> auto { | ||||
|         return utils::encryption::create_hash_sha256(std::wstring_view( | ||||
|             reinterpret_cast<const wchar_t *>(data), size / sizeof(wchar_t))); | ||||
|       }); | ||||
|   EXPECT_STREQ( | ||||
|       L"590ac70125bec4501172937f6a2cbdeb22a87b5e40d5595eccd06b2b20548d8f", | ||||
|       utils::collection::to_hex_wstring(key1_w).c_str()); | ||||
|  | ||||
|   auto key2_w = utils::encryption::generate_key<utils::encryption::hash_256_t>( | ||||
|       token_w, [](auto &&data, auto &&size) -> auto { | ||||
|         return utils::encryption::create_hash_blake2b_256(std::wstring_view( | ||||
|             reinterpret_cast<const wchar_t *>(data), size / sizeof(wchar_t))); | ||||
|       }); | ||||
|   EXPECT_NE(key1_w, key2_w); | ||||
|  | ||||
|   EXPECT_STREQ( | ||||
|       L"0392d95ed3eee9772fbb9af68fedf829a8eb0adbe8575d9691cc9a752196766a", | ||||
|       utils::collection::to_hex_wstring(key2_w).c_str()); | ||||
|  | ||||
|   EXPECT_NE(key1_w, key1); | ||||
|   EXPECT_NE(key2_w, key2); | ||||
| } | ||||
|  | ||||
| #if defined(PROJECT_ENABLE_BOOST) | ||||
| static const std::string buffer = "cow moose dog chicken"; | ||||
| static const std::string token = "moose"; | ||||
|  | ||||
| static void test_encrypted_result(const data_buffer &result) { | ||||
|   EXPECT_EQ(buffer.size() + utils::encryption::encryption_header_size, | ||||
| @@ -39,14 +148,6 @@ static void test_encrypted_result(const data_buffer &result) { | ||||
|   EXPECT_STREQ(buffer.c_str(), data.c_str()); | ||||
| } | ||||
|  | ||||
| TEST(utils_encryption, generate_key) { | ||||
|   const auto key = utils::encryption::generate_key(token); | ||||
|   const auto str = utils::collection::to_hex_string(key); | ||||
|   EXPECT_STREQ( | ||||
|       "182072537ada59e4d6b18034a80302ebae935f66adbdf0f271d3d36309c2d481", | ||||
|       str.c_str()); | ||||
| } | ||||
|  | ||||
| TEST(utils_encryption, encrypt_data_buffer) { | ||||
|   data_buffer result; | ||||
|   utils::encryption::encrypt_data(token, buffer, result); | ||||
| @@ -54,7 +155,8 @@ TEST(utils_encryption, encrypt_data_buffer) { | ||||
| } | ||||
|  | ||||
| TEST(utils_encryption, encrypt_data_buffer_with_key) { | ||||
|   const auto key = utils::encryption::generate_key(token); | ||||
|   const auto key = | ||||
|       utils::encryption::generate_key<utils::encryption::hash_256_t>(token); | ||||
|   data_buffer result; | ||||
|   utils::encryption::encrypt_data(key, buffer, result); | ||||
|   test_encrypted_result(result); | ||||
| @@ -69,7 +171,8 @@ TEST(utils_encryption, encrypt_data_pointer) { | ||||
| } | ||||
|  | ||||
| TEST(utils_encryption, encrypt_data_pointer_with_key) { | ||||
|   const auto key = utils::encryption::generate_key(token); | ||||
|   const auto key = | ||||
|       utils::encryption::generate_key<utils::encryption::hash_256_t>(token); | ||||
|   data_buffer result; | ||||
|   utils::encryption::encrypt_data( | ||||
|       key, reinterpret_cast<const unsigned char *>(buffer.data()), | ||||
| @@ -78,7 +181,8 @@ TEST(utils_encryption, encrypt_data_pointer_with_key) { | ||||
| } | ||||
|  | ||||
| TEST(utils_encryption, decrypt_data_pointer) { | ||||
|   const auto key = utils::encryption::generate_key(token); | ||||
|   const auto key = | ||||
|       utils::encryption::generate_key<utils::encryption::hash_256_t>(token); | ||||
|   data_buffer result; | ||||
|   utils::encryption::encrypt_data( | ||||
|       key, reinterpret_cast<const unsigned char *>(buffer.data()), | ||||
| @@ -93,7 +197,8 @@ TEST(utils_encryption, decrypt_data_pointer) { | ||||
| } | ||||
|  | ||||
| TEST(utils_encryption, decrypt_data_buffer_with_key) { | ||||
|   const auto key = utils::encryption::generate_key(token); | ||||
|   const auto key = | ||||
|       utils::encryption::generate_key<utils::encryption::hash_256_t>(token); | ||||
|   data_buffer result; | ||||
|   utils::encryption::encrypt_data( | ||||
|       key, reinterpret_cast<const unsigned char *>(buffer.data()), | ||||
| @@ -107,7 +212,8 @@ TEST(utils_encryption, decrypt_data_buffer_with_key) { | ||||
| } | ||||
|  | ||||
| TEST(utils_encryption, decrypt_data_pointer_with_key) { | ||||
|   const auto key = utils::encryption::generate_key(token); | ||||
|   const auto key = | ||||
|       utils::encryption::generate_key<utils::encryption::hash_256_t>(token); | ||||
|   data_buffer result; | ||||
|   utils::encryption::encrypt_data( | ||||
|       key, reinterpret_cast<const unsigned char *>(buffer.data()), | ||||
| @@ -122,7 +228,8 @@ TEST(utils_encryption, decrypt_data_pointer_with_key) { | ||||
| } | ||||
|  | ||||
| TEST(utils_encryption, decryption_failure) { | ||||
|   const auto key = utils::encryption::generate_key(token); | ||||
|   const auto key = | ||||
|       utils::encryption::generate_key<utils::encryption::hash_256_t>(token); | ||||
|   data_buffer result; | ||||
|   utils::encryption::encrypt_data( | ||||
|       key, reinterpret_cast<const unsigned char *>(buffer.data()), | ||||
| @@ -134,6 +241,7 @@ TEST(utils_encryption, decryption_failure) { | ||||
|   std::string data; | ||||
|   EXPECT_FALSE(utils::encryption::decrypt_data(key, result, data)); | ||||
| } | ||||
| #endif // defined(PROJECT_ENABLE_BOOST) | ||||
| } // namespace repertory | ||||
|  | ||||
| #endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) | ||||
| #endif // defined(PROJECT_ENABLE_LIBSODIUM) | ||||
|   | ||||
| @@ -304,4 +304,31 @@ TEST(utils_path, absolute) { | ||||
|  | ||||
|   // path = utils::path::absolute("~/.local"); | ||||
| } | ||||
|  | ||||
| TEST(utils_path, absolute_can_resolve_path_variables) { | ||||
|   std::string home{}; | ||||
|  | ||||
| #if defined(_WIN32) | ||||
|   home.resize(MAX_PATH + 1U); | ||||
|   auto size = ::GetEnvironmentVariableA("USERPROFILE", home.data(), 0U); | ||||
|  | ||||
|   home.resize(size); | ||||
|   ::GetEnvironmentVariableA("USERPROFILE", home.data(), | ||||
|                             static_cast<DWORD>(home.size())); | ||||
|   home = utils::path::absolute(home); | ||||
|  | ||||
|   auto expanded_str = utils::path::absolute("%USERPROFILE%"); | ||||
|   EXPECT_STREQ(home.c_str(), expanded_str.c_str()); | ||||
|  | ||||
|   expanded_str = utils::path::absolute("~"); | ||||
|   EXPECT_STREQ(home.c_str(), expanded_str.c_str()); | ||||
|   EXPECT_STREQ((home + home).c_str(), expanded_str.c_str()); | ||||
| #else  // !defined(_WIN32) | ||||
|   home = std::getenv("HOME"); | ||||
|   home = utils::path::absolute(home); | ||||
|  | ||||
|   auto expanded_str = utils::path::absolute("~"); | ||||
|   EXPECT_STREQ(home.c_str(), expanded_str.c_str()); | ||||
| #endif // defined(_WIN32) | ||||
| } | ||||
| } // namespace repertory | ||||
|   | ||||
		Reference in New Issue
	
	Block a user