From f68d7e2ae66c6827046ee7338d0431960792194d Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Sat, 31 Aug 2024 16:47:57 -0500 Subject: [PATCH] updated build system --- support/include/utils/file.hpp | 13 +++ support/src/utils/file.cpp | 6 +- support/src/utils/file_smb_file.cpp | 3 +- support/test/src/utils/file_test.cpp | 116 ++++++++++++++++++--------- 4 files changed, 99 insertions(+), 39 deletions(-) diff --git a/support/include/utils/file.hpp b/support/include/utils/file.hpp index 8f65a9c9..370b0bd0 100644 --- a/support/include/utils/file.hpp +++ b/support/include/utils/file.hpp @@ -67,9 +67,11 @@ get_free_drive_space(std::string_view path) -> std::optional; [[nodiscard]] auto get_free_drive_space(std::wstring_view path) -> std::optional; +// INFO: has test [[nodiscard]] auto get_time(std::string_view path, time_type type) -> std::optional; +// INFO: has test [[nodiscard]] auto get_time(std::wstring_view path, time_type type) -> std::optional; @@ -227,6 +229,7 @@ public: [[nodiscard]] auto sha256() -> std::optional; #endif // defined(PROJECT_ENABLE_LIBSODIUM) + [[nodiscard]] auto size() const -> std::optional override; [[nodiscard]] auto truncate(std::size_t size) -> bool override; @@ -770,6 +773,7 @@ public: #if defined(PROJECT_ENABLE_JSON) #if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) +// INFO: has test [[nodiscard]] auto read_json_file(std::string_view path, nlohmann::json &data, std::optional password = std::nullopt) -> bool; @@ -779,6 +783,7 @@ read_json_file(std::string_view path, nlohmann::json &data, std::wstring_view path, nlohmann::json &data, std::optional password = std::nullopt) -> bool; +// INFO: has test [[nodiscard]] auto write_json_file( std::string_view path, const nlohmann::json &data, std::optional password = std::nullopt) -> bool; @@ -792,6 +797,7 @@ read_json_file(std::string_view path, nlohmann::json &data, [[nodiscard]] auto read_json_file(std::string_view path, nlohmann::json &data) -> bool; +// INFO: has test [[nodiscard]] auto read_json_file(std::wstring_view path, nlohmann::json &data) -> bool; @@ -799,11 +805,13 @@ read_json_file(std::string_view path, nlohmann::json &data, [[nodiscard]] auto write_json_file(std::string_view path, const nlohmann::json &data) -> bool; +// INFO: has test [[nodiscard]] auto write_json_file(std::wstring_view path, const nlohmann::json &data) -> bool; #endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) #endif // defined(PROJECT_ENABLE_JSON) +// INFO: has test template [[nodiscard]] inline auto directory_exists_in_path_t( std::basic_string_view path, @@ -812,6 +820,7 @@ template return directory(utils::path::combine(path, {sub_directory})).exists(); } +// INFO: has test template [[nodiscard]] inline auto file_exists_in_path_t( std::basic_string_view path, @@ -819,21 +828,25 @@ template return file(utils::path::combine(path, {file_name})).exists(); } +// INFO: has test inline auto directory_exists_in_path(std::string_view path, std::string_view sub_directory) -> bool { return directory_exists_in_path_t(path, sub_directory); } +// INFO: has test inline auto directory_exists_in_path(std::wstring_view path, std::wstring_view sub_directory) -> bool { return directory_exists_in_path_t(path, sub_directory); } +// INFO: has test inline auto file_exists_in_path(std::string_view path, std::string_view file_name) -> bool { return file_exists_in_path_t(path, file_name); } +// INFO: has test inline auto file_exists_in_path(std::wstring_view path, std::wstring_view file_name) -> bool { return file_exists_in_path_t(path, file_name); diff --git a/support/src/utils/file.cpp b/support/src/utils/file.cpp index 21349cc4..e39ccb35 100644 --- a/support/src/utils/file.cpp +++ b/support/src/utils/file.cpp @@ -176,8 +176,10 @@ auto get_times(std::string_view path) -> std::optional { file_times ret{}; #if defined(_WIN32) - auto file_handle = ::CreateFileA(std::string{path}.c_str(), GENERIC_READ, 0, - nullptr, OPEN_EXISTING, 0, nullptr); + auto file_handle = + ::CreateFileA(std::string{path}.c_str(), GENERIC_READ, + FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, + nullptr, OPEN_EXISTING, 0U, nullptr); if (file_handle == INVALID_HANDLE_VALUE) { throw std::runtime_error("failed to get file times|" + std::string{path} + '|' + diff --git a/support/src/utils/file_smb_file.cpp b/support/src/utils/file_smb_file.cpp index 126a7888..dde4eb71 100644 --- a/support/src/utils/file_smb_file.cpp +++ b/support/src/utils/file_smb_file.cpp @@ -49,7 +49,8 @@ auto smb_file::copy_to(std::string_view new_path, // auto to_path = utils::path::absolute(new_path); throw std::runtime_error("failed to copy file|" + path_ + '|' + - std::string{new_path} + "|not implemented"); + std::string{new_path} + '|' + + std::to_string(overwrite) + "|not implemented"); } catch (const std::exception &e) { utils::error::handle_exception(function_name, e); } catch (...) { diff --git a/support/test/src/utils/file_test.cpp b/support/test/src/utils/file_test.cpp index 0ace3ebd..d726c336 100644 --- a/support/test/src/utils/file_test.cpp +++ b/support/test/src/utils/file_test.cpp @@ -105,38 +105,76 @@ TEST(utils_file, write_fails_for_read_only_file) { TEST(utils_file, read_and_write_json_file) { auto path = test::generate_test_file_name("utils_file"); - auto data = nlohmann::json({{"moose", "cow"}}); - EXPECT_TRUE(utils::file::write_json_file(path, data)); + auto json_data = nlohmann::json({{"moose", "cow"}}); + EXPECT_TRUE(utils::file::write_json_file(path, json_data)); - nlohmann::json data2{}; - EXPECT_TRUE(utils::file::read_json_file(path, data2)); - EXPECT_STREQ(data.dump().c_str(), data2.dump().c_str()); + { + nlohmann::json result_data{}; + EXPECT_TRUE(utils::file::read_json_file(path, result_data)); + EXPECT_STREQ(json_data.dump().c_str(), result_data.dump().c_str()); + } + + { + nlohmann::json result_data{}; + EXPECT_TRUE(utils::file::read_json_file(utils::string::from_utf8(path), + result_data)); + EXPECT_STREQ(json_data.dump().c_str(), result_data.dump().c_str()); + } } #if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) TEST(utils_file, read_and_write_json_file_encrypted) { - auto path = test::generate_test_file_name("utils_file"); + { + auto path = test::generate_test_file_name("utils_file"); - auto data = nlohmann::json({{"moose", "cow"}}); - EXPECT_TRUE(utils::file::write_json_file(path, data, "moose")); + auto json_data = nlohmann::json({{"moose", "cow"}}); + EXPECT_TRUE(utils::file::write_json_file(path, json_data, "moose")); - nlohmann::json data2{}; - EXPECT_TRUE(utils::file::read_json_file(path, data2, "moose")); - EXPECT_STREQ(data.dump().c_str(), data2.dump().c_str()); + nlohmann::json result_data{}; + EXPECT_TRUE(utils::file::read_json_file(path, result_data, "moose")); + EXPECT_STREQ(json_data.dump().c_str(), result_data.dump().c_str()); + + { + auto file = utils::file::file::open_file(path); + data_buffer encrypted_data{}; + EXPECT_TRUE(file->read_all(encrypted_data, 0U)); + + data_buffer decrypted_data{}; + EXPECT_TRUE(utils::encryption::decrypt_data("moose", encrypted_data, + decrypted_data)); + EXPECT_STREQ(json_data.dump().c_str(), + nlohmann::json::parse(std::string(decrypted_data.begin(), + decrypted_data.end())) + .dump() + .c_str()); + } + } { - auto file = utils::file::file::open_file(path); - data_buffer encrypted_data{}; - EXPECT_TRUE(file->read_all(encrypted_data, 0U)); + auto path = + utils::string::from_utf8(test::generate_test_file_name("utils_file")); - data_buffer decrypted_data{}; - EXPECT_TRUE(utils::encryption::decrypt_data("moose", encrypted_data, - decrypted_data)); - EXPECT_STREQ(data.dump().c_str(), - nlohmann::json::parse( - std::string(decrypted_data.begin(), decrypted_data.end())) - .dump() - .c_str()); + auto json_data = nlohmann::json({{"moose", "cow"}}); + EXPECT_TRUE(utils::file::write_json_file(path, json_data, L"moose")); + + nlohmann::json result_data{}; + EXPECT_TRUE(utils::file::read_json_file(path, result_data, L"moose")); + EXPECT_STREQ(json_data.dump().c_str(), result_data.dump().c_str()); + + { + auto file = utils::file::file::open_file(path); + data_buffer encrypted_data{}; + EXPECT_TRUE(file->read_all(encrypted_data, 0U)); + + data_buffer decrypted_data{}; + EXPECT_TRUE(utils::encryption::decrypt_data("moose", encrypted_data, + decrypted_data)); + EXPECT_STREQ(json_data.dump().c_str(), + nlohmann::json::parse(std::string(decrypted_data.begin(), + decrypted_data.end())) + .dump() + .c_str()); + } } } #endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) @@ -394,7 +432,8 @@ TEST(utils_file, get_total_drive_space_fails_for_bad_path) { TEST(utils_file, get_times) { { - auto times = utils::file::get_times(__FILE__); + auto times = + utils::file::get_times(test::create_random_file(1U).get_path()); EXPECT_TRUE(times.has_value()); EXPECT_LT(0U, times->get(utils::file::time_type::accessed)); EXPECT_LT(0U, times->get(utils::file::time_type::created)); @@ -403,7 +442,8 @@ TEST(utils_file, get_times) { } { - auto times = utils::file::get_times(utils::string::from_utf8(__FILE__)); + auto times = utils::file::get_times( + utils::string::from_utf8(test::create_random_file(1U).get_path())); EXPECT_TRUE(times.has_value()); EXPECT_LT(0U, times->get(utils::file::time_type::accessed)); EXPECT_LT(0U, times->get(utils::file::time_type::created)); @@ -420,45 +460,49 @@ TEST(utils_file, get_times_fails_if_not_found) { TEST(utils_file, get_time) { { + auto file_path = test::create_random_file(1U).get_path(); auto file_time = - utils::file::get_time(__FILE__, utils::file::time_type::accessed); + utils::file::get_time(file_path, utils::file::time_type::accessed); EXPECT_TRUE(file_time.has_value()); EXPECT_LT(0U, file_time.value()); file_time = - utils::file::get_time(__FILE__, utils::file::time_type::created); + utils::file::get_time(file_path, utils::file::time_type::created); EXPECT_TRUE(file_time.has_value()); EXPECT_LT(0U, file_time.value()); file_time = - utils::file::get_time(__FILE__, utils::file::time_type::modified); + utils::file::get_time(file_path, utils::file::time_type::modified); EXPECT_TRUE(file_time.has_value()); EXPECT_LT(0U, file_time.value()); file_time = - utils::file::get_time(__FILE__, utils::file::time_type::written); + utils::file::get_time(file_path, utils::file::time_type::written); EXPECT_TRUE(file_time.has_value()); EXPECT_LT(0U, file_time.value()); } { - auto file_time = utils::file::get_time(utils::string::from_utf8(__FILE__), - utils::file::time_type::accessed); + auto file_path = + utils::string::from_utf8(test::create_random_file(1U).get_path()); + + auto file_time = + utils::file::get_time(file_path, utils::file::time_type::accessed); EXPECT_TRUE(file_time.has_value()); EXPECT_LT(0U, file_time.value()); - file_time = utils::file::get_time(utils::string::from_utf8(__FILE__), - utils::file::time_type::created); + file_time = + utils::file::get_time(file_path, utils::file::time_type::created); EXPECT_TRUE(file_time.has_value()); EXPECT_LT(0U, file_time.value()); - file_time = utils::file::get_time(utils::string::from_utf8(__FILE__), - utils::file::time_type::modified); + file_time = + utils::file::get_time(file_path, utils::file::time_type::modified); EXPECT_TRUE(file_time.has_value()); EXPECT_LT(0U, file_time.value()); - file_time = utils::file::get_time(utils::string::from_utf8(__FILE__), - utils::file::time_type::written); + file_time = + utils::file::get_time(file_path, utils::file::time_type::written); EXPECT_TRUE(file_time.has_value()); EXPECT_LT(0U, file_time.value()); }