update
This commit is contained in:
		
							
								
								
									
										141
									
								
								support/3rd_party/test/src/utils/encryption_test.cpp
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										141
									
								
								support/3rd_party/test/src/utils/encryption_test.cpp
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,141 @@ | ||||
| /* | ||||
|  Copyright <2018-2024> <scott.e.graves@protonmail.com> | ||||
|  | ||||
|   Permission is hereby granted, free of charge, to any person obtaining a copy | ||||
|  of this software and associated documentation files (the "Software"), to deal | ||||
|  in the Software without restriction, including without limitation the rights to | ||||
|  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | ||||
|  of the Software, and to permit persons to whom the Software is furnished to do | ||||
|  so, subject to the following conditions: | ||||
|  | ||||
|   The above copyright notice and this permission notice shall be included in all | ||||
|  copies or substantial portions of the Software. | ||||
|  | ||||
|   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||
|  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||
|  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||
|  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||
|  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||
|  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) | ||||
|  | ||||
| #include "gtest/gtest.hpp" | ||||
|  | ||||
| #include "utils/collection.hpp" | ||||
| #include "utils/encrypt.hpp" | ||||
| #include "utils/file.hpp" | ||||
| #include "utils/string.hpp" | ||||
|  | ||||
| namespace repertory { | ||||
| 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, | ||||
|             result.size()); | ||||
|   std::string data; | ||||
|   EXPECT_TRUE(utils::encryption::decrypt_data(token, result, data)); | ||||
|   EXPECT_EQ(buffer.size(), data.size()); | ||||
|   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); | ||||
|   test_encrypted_result(result); | ||||
| } | ||||
|  | ||||
| TEST(utils_encryption, encrypt_data_buffer_with_key) { | ||||
|   const auto key = utils::encryption::generate_key(token); | ||||
|   data_buffer result; | ||||
|   utils::encryption::encrypt_data(key, buffer, result); | ||||
|   test_encrypted_result(result); | ||||
| } | ||||
|  | ||||
| TEST(utils_encryption, encrypt_data_pointer) { | ||||
|   data_buffer result; | ||||
|   utils::encryption::encrypt_data( | ||||
|       token, reinterpret_cast<const unsigned char *>(buffer.data()), | ||||
|       buffer.size(), result); | ||||
|   test_encrypted_result(result); | ||||
| } | ||||
|  | ||||
| TEST(utils_encryption, encrypt_data_pointer_with_key) { | ||||
|   const auto key = utils::encryption::generate_key(token); | ||||
|   data_buffer result; | ||||
|   utils::encryption::encrypt_data( | ||||
|       key, reinterpret_cast<const unsigned char *>(buffer.data()), | ||||
|       buffer.size(), result); | ||||
|   test_encrypted_result(result); | ||||
| } | ||||
|  | ||||
| TEST(utils_encryption, decrypt_data_pointer) { | ||||
|   const auto key = utils::encryption::generate_key(token); | ||||
|   data_buffer result; | ||||
|   utils::encryption::encrypt_data( | ||||
|       key, reinterpret_cast<const unsigned char *>(buffer.data()), | ||||
|       buffer.size(), result); | ||||
|  | ||||
|   std::string data; | ||||
|   EXPECT_TRUE(utils::encryption::decrypt_data(token, result.data(), | ||||
|                                               result.size(), data)); | ||||
|  | ||||
|   EXPECT_EQ(buffer.size(), data.size()); | ||||
|   EXPECT_STREQ(buffer.c_str(), data.c_str()); | ||||
| } | ||||
|  | ||||
| TEST(utils_encryption, decrypt_data_buffer_with_key) { | ||||
|   const auto key = utils::encryption::generate_key(token); | ||||
|   data_buffer result; | ||||
|   utils::encryption::encrypt_data( | ||||
|       key, reinterpret_cast<const unsigned char *>(buffer.data()), | ||||
|       buffer.size(), result); | ||||
|  | ||||
|   std::string data; | ||||
|   EXPECT_TRUE(utils::encryption::decrypt_data(key, result, data)); | ||||
|  | ||||
|   EXPECT_EQ(buffer.size(), data.size()); | ||||
|   EXPECT_STREQ(buffer.c_str(), data.c_str()); | ||||
| } | ||||
|  | ||||
| TEST(utils_encryption, decrypt_data_pointer_with_key) { | ||||
|   const auto key = utils::encryption::generate_key(token); | ||||
|   data_buffer result; | ||||
|   utils::encryption::encrypt_data( | ||||
|       key, reinterpret_cast<const unsigned char *>(buffer.data()), | ||||
|       buffer.size(), result); | ||||
|  | ||||
|   std::string data; | ||||
|   EXPECT_TRUE( | ||||
|       utils::encryption::decrypt_data(key, result.data(), result.size(), data)); | ||||
|  | ||||
|   EXPECT_EQ(buffer.size(), data.size()); | ||||
|   EXPECT_STREQ(buffer.c_str(), data.c_str()); | ||||
| } | ||||
|  | ||||
| TEST(utils_encryption, decryption_failure) { | ||||
|   const auto key = utils::encryption::generate_key(token); | ||||
|   data_buffer result; | ||||
|   utils::encryption::encrypt_data( | ||||
|       key, reinterpret_cast<const unsigned char *>(buffer.data()), | ||||
|       buffer.size(), result); | ||||
|   result[0U] = 0U; | ||||
|   result[1U] = 1U; | ||||
|   result[2U] = 2U; | ||||
|  | ||||
|   std::string data; | ||||
|   EXPECT_FALSE(utils::encryption::decrypt_data(key, result, data)); | ||||
| } | ||||
| } // namespace repertory | ||||
|  | ||||
| #endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) | ||||
		Reference in New Issue
	
	Block a user