v2.0.2-rc (#27)
Some checks reported errors
BlockStorage/repertory/pipeline/head Something is wrong with the build of this commit

## v2.0.2-rc

### BREAKING CHANGES

* Refactored `config.json` - will need to verify configuration settings prior to mounting

### Issues

* \#12 \[Unit Test\] Complete all providers unit tests
* \#14 \[Unit Test\] SQLite mini-ORM unit tests and cleanup
* \#16 Add support for bucket name in Sia provider
* \#17 Update to common c++ build system
  * A single 64-bit Linux Jenkins server is used to build all Linux and Windows versions
  * All dependency sources are now included
  * MSVC is no longer supported
  * MSYS2 is required for building Windows binaries on Windows
  * OS X support is temporarily disabled
* \#19 \[bug\] Rename file is broken for files that are existing
* \#23 \[bug\] Incorrect file size displayed while upload is pending
* \#24 RocksDB implementations should be transactional
* \#25 Writes should block when maximum cache size is reached
* \#26 Complete ring buffer and direct download support

### Changes from v2.0.1-rc

* Ability to choose between RocksDB and SQLite databases
* Added direct reads and implemented download fallback
* Corrected file times on S3 and Sia providers
* Corrected handling of `chown()` and `chmod()`
* Fixed erroneous download of chunks after resize

Reviewed-on: #27
This commit is contained in:
2024-12-28 15:56:40 -06:00
parent 1b8de3b097
commit 8dd46b8ad8
790 changed files with 49979 additions and 417734 deletions

View File

@ -0,0 +1,63 @@
/*
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.
*/
#ifndef REPERTORY_TEST_INCLUDE_TEST_HPP_
#define REPERTORY_TEST_INCLUDE_TEST_HPP_
#if defined(U)
#undef U
#endif // defined(U)
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using ::testing::_;
using namespace ::testing;
#define COMMA ,
#include "utils/all.hpp"
namespace repertory::test {
[[nodiscard]] auto create_random_file(std::size_t size)
-> utils::file::i_file &;
[[nodiscard]] auto
generate_test_file_name(std::string_view file_name_no_extension) -> std::string;
#if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
template <typename buffer_t, typename result_t>
static void decrypt_and_verify(const buffer_t &buffer, std::string_view token,
result_t &result) {
EXPECT_TRUE(utils::encryption::decrypt_data(token, buffer, result));
}
#endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
auto generate_test_directory() -> utils::file::i_directory &;
[[nodiscard]] auto get_test_config_dir() -> std::string;
[[nodiscard]] auto get_test_input_dir() -> std::string;
[[nodiscard]] auto get_test_output_dir() -> std::string;
} // namespace repertory::test
#endif // REPERTORY_TEST_INCLUDE_TEST_HPP_

146
support/test/src/test.cpp Normal file
View File

@ -0,0 +1,146 @@
/*
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.
*/
#include "test.hpp"
extern int PROJECT_TEST_RESULT;
namespace {
static std::recursive_mutex file_mtx{};
static std::vector<std::unique_ptr<repertory::utils::file::i_fs_item>>
generated_files{};
struct file_deleter final {
std::string test_output_dir;
~file_deleter() {
generated_files.clear();
if (PROJECT_TEST_RESULT == 0) {
EXPECT_TRUE(repertory::utils::file::directory(test_output_dir)
.remove_recursively());
}
}
};
const auto deleter{
std::make_unique<file_deleter>(repertory::test::get_test_output_dir()),
};
} // namespace
namespace repertory::test {
auto create_random_file(std::size_t size) -> utils::file::i_file & {
auto path = generate_test_file_name("random");
auto file = utils::file::file::open_or_create_file(path);
EXPECT_TRUE(*file);
if (*file) {
data_buffer buf(size);
#if defined(PROJECT_ENABLE_LIBSODIUM)
randombytes_buf(buf.data(), buf.size());
#else // !defined(PROJECT_ENABLE_LIBSODIUM)
thread_local std::mt19937 gen(static_cast<unsigned long>(
std::time(nullptr) ^ std::random_device{}()));
std::uniform_int_distribution<std::uint8_t> dis(0U, 255U);
std::generate(buf.begin(), buf.end(), [&]() -> auto { return dis(gen); });
#endif // defined(PROJECT_ENABLE_LIBSODIUM)
std::size_t bytes_written{};
EXPECT_TRUE(file->write(buf, 0U, &bytes_written));
EXPECT_EQ(size, bytes_written);
EXPECT_EQ(size, file->size());
}
recur_mutex_lock lock{file_mtx};
generated_files.emplace_back(std::move(file));
return *dynamic_cast<utils::file::i_file *>(generated_files.back().get());
}
auto generate_test_directory() -> utils::file::i_directory & {
auto path = utils::path::combine(
get_test_output_dir(),
{
std::string{"test_dir"} + std::to_string(generated_files.size()),
});
recur_mutex_lock lock{file_mtx};
generated_files.emplace_back(std::unique_ptr<utils::file::i_fs_item>(
new utils::file::directory{path}));
auto &ret =
*dynamic_cast<utils::file::i_directory *>(generated_files.back().get());
EXPECT_TRUE(ret.create_directory());
return ret;
}
auto generate_test_file_name(std::string_view file_name_no_extension)
-> std::string {
auto path = utils::path::combine(
get_test_output_dir(), {
std::string{file_name_no_extension} +
std::to_string(generated_files.size()),
});
recur_mutex_lock lock{file_mtx};
generated_files.emplace_back(
std::unique_ptr<utils::file::i_file>(new utils::file::file{path}));
return generated_files.back()->get_path();
}
auto get_test_config_dir() -> std::string {
static auto test_path = ([]() -> std::string {
auto dir = utils::get_environment_variable("PROJECT_TEST_CONFIG_DIR");
return utils::path::combine(dir.empty() ? "." : dir, {"test_config"});
})();
return test_path;
}
auto get_test_input_dir() -> std::string {
static auto test_path = ([]() -> std::string {
auto dir = utils::get_environment_variable("PROJECT_TEST_INPUT_DIR");
return utils::path::combine(dir.empty() ? "." : dir, {"test_input"});
})();
return test_path;
}
auto get_test_output_dir() -> std::string {
static auto test_path = ([]() -> std::string {
auto temp = utils::file::create_temp_name("project_test");
#if defined(_WIN32)
auto path = utils::path::combine("%TEMP%", {temp});
#else // !defined(_WIN32)
auto path = utils::path::combine("/tmp", {temp});
#endif // defined(_WIN32)
if (not utils::file::directory(path).exists()) {
EXPECT_TRUE(utils::file::directory{path}.create_directory());
}
return path;
})();
return test_path;
}
} // namespace repertory::test

View File

@ -0,0 +1,250 @@
/*
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.
*/
#include "test.hpp"
namespace repertory {
TEST(utils_collection, excludes) {
auto data = {"cow", "moose", "dog", "chicken"};
EXPECT_FALSE(utils::collection::excludes(data, "chicken"));
EXPECT_FALSE(utils::collection::excludes(data, "cow"));
EXPECT_FALSE(utils::collection::excludes(data, "dog"));
EXPECT_FALSE(utils::collection::excludes(data, "moose"));
EXPECT_TRUE(utils::collection::excludes(data, "mouse"));
}
TEST(utils_collection, includes) {
auto data = {"cow", "moose", "dog", "chicken"};
EXPECT_FALSE(utils::collection::includes(data, "mice"));
EXPECT_TRUE(utils::collection::includes(data, "chicken"));
EXPECT_TRUE(utils::collection::includes(data, "cow"));
EXPECT_TRUE(utils::collection::includes(data, "dog"));
EXPECT_TRUE(utils::collection::includes(data, "moose"));
}
TEST(utils_collection, from_hex_string) {
{
auto data = "0xABCDEF10";
std::vector<std::uint8_t> val{};
EXPECT_TRUE(utils::collection::from_hex_string(data, val));
EXPECT_EQ(4U, val.size());
}
{
auto data = " 0xABCDEF10 ";
std::vector<std::uint8_t> val{};
EXPECT_TRUE(utils::collection::from_hex_string(data, val));
EXPECT_EQ(4U, val.size());
}
{
auto data = "ABCDEF10";
std::vector<std::uint8_t> val{};
EXPECT_TRUE(utils::collection::from_hex_string(data, val));
EXPECT_EQ(4U, val.size());
}
{
auto data = "ACDEF";
std::vector<std::uint8_t> val{};
EXPECT_TRUE(utils::collection::from_hex_string(data, val));
EXPECT_EQ(3U, val.size());
}
{
auto data = " ACDEF ";
std::vector<std::uint8_t> val{};
EXPECT_TRUE(utils::collection::from_hex_string(data, val));
EXPECT_EQ(3U, val.size());
}
{
auto data = "";
std::vector<std::uint8_t> val{};
EXPECT_TRUE(utils::collection::from_hex_string(data, val));
EXPECT_TRUE(val.empty());
}
{
auto data = L"0xABCDEF10";
std::vector<std::uint8_t> val{};
EXPECT_TRUE(utils::collection::from_hex_string(data, val));
EXPECT_EQ(4U, val.size());
}
{
auto data = L" 0xABCDEF10 ";
std::vector<std::uint8_t> val{};
EXPECT_TRUE(utils::collection::from_hex_string(data, val));
EXPECT_EQ(4U, val.size());
}
{
auto data = L"ABCDEF10";
std::vector<std::uint8_t> val{};
EXPECT_TRUE(utils::collection::from_hex_string(data, val));
EXPECT_EQ(4U, val.size());
}
{
auto data = L"ACDEF";
std::vector<std::uint8_t> val{};
EXPECT_TRUE(utils::collection::from_hex_string(data, val));
EXPECT_EQ(3U, val.size());
}
{
auto data = L" ACDEF ";
std::vector<std::uint8_t> val{};
EXPECT_TRUE(utils::collection::from_hex_string(data, val));
EXPECT_EQ(3U, val.size());
}
{
auto data = L"";
std::vector<std::uint8_t> val{};
EXPECT_TRUE(utils::collection::from_hex_string(data, val));
EXPECT_TRUE(val.empty());
}
}
TEST(utils_collection, from_hex_string_fails) {
{
auto data = "ABCDEF1Z";
std::vector<std::uint8_t> val{};
EXPECT_FALSE(utils::collection::from_hex_string(data, val));
EXPECT_TRUE(val.empty());
}
{
auto data = "ABC DEF1";
std::vector<std::uint8_t> val{};
EXPECT_FALSE(utils::collection::from_hex_string(data, val));
EXPECT_TRUE(val.empty());
}
{
auto data = "0x";
std::vector<std::uint8_t> val{};
EXPECT_FALSE(utils::collection::from_hex_string(data, val));
EXPECT_TRUE(val.empty());
}
{
auto data = " 0x ";
std::vector<std::uint8_t> val{};
EXPECT_FALSE(utils::collection::from_hex_string(data, val));
EXPECT_TRUE(val.empty());
}
{
auto data = L"ABCDEF1Z";
std::vector<std::uint8_t> val{};
EXPECT_FALSE(utils::collection::from_hex_string(data, val));
EXPECT_TRUE(val.empty());
}
{
auto data = L"ABC DEF1";
std::vector<std::uint8_t> val{};
EXPECT_FALSE(utils::collection::from_hex_string(data, val));
EXPECT_TRUE(val.empty());
}
{
auto data = L"0x";
std::vector<std::uint8_t> val{};
EXPECT_FALSE(utils::collection::from_hex_string(data, val));
EXPECT_TRUE(val.empty());
}
{
auto data = L" 0x";
std::vector<std::uint8_t> val{};
EXPECT_FALSE(utils::collection::from_hex_string(data, val));
EXPECT_TRUE(val.empty());
}
}
TEST(utils_collection, to_hex_string) {
{
std::array<std::int8_t, 2U> col{
static_cast<std::int8_t>(0xFF),
static_cast<std::int8_t>(0xEE),
};
auto str = utils::collection::to_hex_string(col);
EXPECT_STREQ("ffee", str.c_str());
auto w_str = utils::collection::to_hex_wstring(col);
EXPECT_STREQ(L"ffee", w_str.c_str());
}
{
std::array<std::uint8_t, 2U> col{
static_cast<std::uint8_t>(0xFF),
static_cast<std::uint8_t>(0xEE),
};
auto str = utils::collection::to_hex_string(col);
EXPECT_STREQ("ffee", str.c_str());
auto w_str = utils::collection::to_hex_wstring(col);
EXPECT_STREQ(L"ffee", w_str.c_str());
}
}
TEST(utils_collection, remove_element) {
{
std::vector<std::uint8_t> col{
static_cast<std::uint8_t>(0xFF),
static_cast<std::uint8_t>(0xEE),
};
utils::collection::remove_element(col, 0xFF);
EXPECT_EQ(1U, col.size());
EXPECT_EQ(static_cast<std::uint8_t>(0xEE), col.at(0U));
}
{
std::vector<std::uint8_t> col{
static_cast<std::uint8_t>(0xFF),
static_cast<std::uint8_t>(0xEE),
};
utils::collection::remove_element(col, 0xEE);
EXPECT_EQ(1U, col.size());
EXPECT_EQ(static_cast<std::uint8_t>(0xFF), col.at(0U));
}
{
std::vector<std::uint8_t> col{
static_cast<std::uint8_t>(0xFF),
static_cast<std::uint8_t>(0xEE),
};
utils::collection::remove_element(col, 0xEF);
EXPECT_EQ(2U, col.size());
EXPECT_EQ(static_cast<std::uint8_t>(0xFF), col.at(0U));
EXPECT_EQ(static_cast<std::uint8_t>(0xEE), col.at(1U));
}
}
} // namespace repertory

View File

@ -0,0 +1,306 @@
/*
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.
*/
#include "test.hpp"
namespace repertory {
TEST(utils_common, calculate_read_size) {
auto read_size = utils::calculate_read_size(0U, 0U, 0U);
EXPECT_EQ(0U, read_size);
read_size = utils::calculate_read_size(5U, 0U, 0U);
EXPECT_EQ(0U, read_size);
read_size = utils::calculate_read_size(0U, 6U, 7U);
EXPECT_EQ(0U, read_size);
read_size = utils::calculate_read_size(7U, 1U, 7U);
EXPECT_EQ(0U, read_size);
read_size = utils::calculate_read_size(5U, 5U, 0U);
EXPECT_EQ(5U, read_size);
read_size = utils::calculate_read_size(5U, 5U, 1U);
EXPECT_EQ(4U, read_size);
}
TEST(utils_common, version_equal) {
EXPECT_EQ(0, utils::compare_version_strings("", ""));
EXPECT_EQ(0, utils::compare_version_strings("1.0", "1.0"));
EXPECT_EQ(0, utils::compare_version_strings("1.0.0", "1.0"));
EXPECT_EQ(0, utils::compare_version_strings("1.0.0.0", "1.0"));
EXPECT_EQ(0, utils::compare_version_strings("1.0.0.0", "1.0.0"));
EXPECT_EQ(0, utils::compare_version_strings(L"", L""));
EXPECT_EQ(0, utils::compare_version_strings(L"1.0", L"1.0"));
EXPECT_EQ(0, utils::compare_version_strings(L"1.0.0", L"1.0"));
EXPECT_EQ(0, utils::compare_version_strings(L"1.0.0.0", L"1.0"));
EXPECT_EQ(0, utils::compare_version_strings(L"1.0.0.0", L"1.0.0"));
}
TEST(utils_common, version_greater) {
EXPECT_EQ(1, utils::compare_version_strings("1.0.1", ""));
EXPECT_EQ(1, utils::compare_version_strings("1.0.1", "1.0"));
EXPECT_EQ(1, utils::compare_version_strings("1.0.1", "1.0.0"));
EXPECT_EQ(1, utils::compare_version_strings("1.0.1", "1.0.0.0"));
EXPECT_EQ(1, utils::compare_version_strings("1.0.1.0", "1.0"));
EXPECT_EQ(1, utils::compare_version_strings("1.0.1.0", "1.0.0"));
EXPECT_EQ(1, utils::compare_version_strings("1.0.1.0", "1.0.0.0"));
EXPECT_EQ(1, utils::compare_version_strings("1.0", "0.9.9"));
EXPECT_EQ(1, utils::compare_version_strings("1.0.1", "0.9.9"));
EXPECT_EQ(1, utils::compare_version_strings("1.0.1.0", "0.9.9"));
EXPECT_EQ(1, utils::compare_version_strings("1.0.1", ""));
EXPECT_EQ(1, utils::compare_version_strings(L"1.0.1", L"1.0"));
EXPECT_EQ(1, utils::compare_version_strings(L"1.0.1", L"1.0.0"));
EXPECT_EQ(1, utils::compare_version_strings(L"1.0.1", L"1.0.0.0"));
EXPECT_EQ(1, utils::compare_version_strings(L"1.0.1.0", L"1.0"));
EXPECT_EQ(1, utils::compare_version_strings(L"1.0.1.0", L"1.0.0"));
EXPECT_EQ(1, utils::compare_version_strings(L"1.0.1.0", L"1.0.0.0"));
EXPECT_EQ(1, utils::compare_version_strings(L"1.0", L"0.9.9"));
EXPECT_EQ(1, utils::compare_version_strings(L"1.0.1", L"0.9.9"));
EXPECT_EQ(1, utils::compare_version_strings(L"1.0.1.0", L"0.9.9"));
}
TEST(utils_common, version_less) {
EXPECT_EQ(-1, utils::compare_version_strings("", "1.0"));
EXPECT_EQ(-1, utils::compare_version_strings("0.9.9", "1.0"));
EXPECT_EQ(-1, utils::compare_version_strings("0.9.9", "1.0.1"));
EXPECT_EQ(-1, utils::compare_version_strings("0.9.9", "1.0.1.0"));
EXPECT_EQ(-1, utils::compare_version_strings("1.0", "1.0.1"));
EXPECT_EQ(-1, utils::compare_version_strings("1.0", "1.0.1.0"));
EXPECT_EQ(-1, utils::compare_version_strings("1.0.0", "1.0.1"));
EXPECT_EQ(-1, utils::compare_version_strings("1.0.0", "1.0.1.0"));
EXPECT_EQ(-1, utils::compare_version_strings("1.0.0.0", "1.0.1"));
EXPECT_EQ(-1, utils::compare_version_strings("1.0.0.0", "1.0.1.0"));
EXPECT_EQ(-1, utils::compare_version_strings(L"", L"1.0"));
EXPECT_EQ(-1, utils::compare_version_strings(L"0.9.9", L"1.0"));
EXPECT_EQ(-1, utils::compare_version_strings(L"0.9.9", L"1.0.1"));
EXPECT_EQ(-1, utils::compare_version_strings(L"0.9.9", L"1.0.1.0"));
EXPECT_EQ(-1, utils::compare_version_strings(L"1.0", L"1.0.1"));
EXPECT_EQ(-1, utils::compare_version_strings(L"1.0", L"1.0.1.0"));
EXPECT_EQ(-1, utils::compare_version_strings(L"1.0.0", L"1.0.1"));
EXPECT_EQ(-1, utils::compare_version_strings(L"1.0.0", L"1.0.1.0"));
EXPECT_EQ(-1, utils::compare_version_strings(L"1.0.0.0", L"1.0.1"));
EXPECT_EQ(-1, utils::compare_version_strings(L"1.0.0.0", L"1.0.1.0"));
}
#if defined(PROJECT_ENABLE_STDUUID)
TEST(utils_common, create_uuid_string) {
{
const auto uuid1 = utils::create_uuid_string();
const auto uuid2 = utils::create_uuid_string();
ASSERT_EQ(36U, uuid1.size());
ASSERT_EQ(36U, uuid2.size());
ASSERT_STRNE(uuid1.c_str(), uuid2.c_str());
}
{
const auto uuid1 = utils::create_uuid_wstring();
const auto uuid2 = utils::create_uuid_wstring();
ASSERT_EQ(36U, uuid1.size());
ASSERT_EQ(36U, uuid2.size());
ASSERT_STRNE(uuid1.c_str(), uuid2.c_str());
}
}
#endif // defined(PROJECT_ENABLE_STDUUID)
#if defined(PROJECT_ENABLE_LIBSODIUM)
TEST(utils_common, generate_secure_random) {
{
auto r1 = utils::generate_secure_random<std::size_t>();
auto r2 = utils::generate_secure_random<std::size_t>();
EXPECT_NE(r1, r2);
}
{
auto r1 = utils::generate_secure_random<std::vector<std::uint8_t>>(6U);
auto r2 = utils::generate_secure_random<std::vector<std::uint8_t>>(6U);
EXPECT_EQ(6U, r1.size());
EXPECT_EQ(r1.size(), r2.size());
EXPECT_NE(r1, r2);
}
{
auto r1 = utils::generate_secure_random<std::array<std::uint8_t, 4U>>();
auto r2 = utils::generate_secure_random<std::array<std::uint8_t, 4U>>();
EXPECT_EQ(4U, r1.size());
EXPECT_EQ(r1.size(), r2.size());
EXPECT_NE(0, std::memcmp(r1.data(), r2.data(), r1.size()));
}
{
auto r1 = utils::generate_secure_random<std::string>(6U);
auto r2 = utils::generate_secure_random<std::string>(6U);
EXPECT_EQ(6U, r1.size());
EXPECT_EQ(r1.size(), r2.size());
EXPECT_NE(0, std::memcmp(r1.data(), r2.data(), r1.size()));
}
{
auto r1 = utils::generate_secure_random<std::wstring>(6U);
auto r2 = utils::generate_secure_random<std::wstring>(6U);
EXPECT_EQ(6U, r1.size());
EXPECT_EQ(r1.size(), r2.size());
EXPECT_NE(0, std::memcmp(r1.data(), r2.data(), r1.size()));
}
}
#endif // defined(PROJECT_ENABLE_LIBSODIUM)
TEST(utils_common, divide_with_ceiling) {
auto r = utils::divide_with_ceiling(12, 5);
EXPECT_EQ(3, r);
r = utils::divide_with_ceiling(12, 4);
EXPECT_EQ(3, r);
r = utils::divide_with_ceiling(1, 2);
EXPECT_EQ(1, r);
r = utils::divide_with_ceiling(2, 2);
EXPECT_EQ(1, r);
r = utils::divide_with_ceiling(0, 2);
EXPECT_EQ(0, r);
}
TEST(utils_common, generate_random_between_for_signed_integers) {
static constexpr const auto max_iterations{1000000UL};
for (std::size_t idx = 0U; idx < max_iterations; ++idx) {
auto res = utils::generate_random_between(5, 12);
EXPECT_GE(res, 5);
EXPECT_LE(res, 12);
}
for (std::size_t idx = 0U; idx < max_iterations; ++idx) {
auto res = utils::generate_random_between(-5, 12);
EXPECT_GE(res, -5);
EXPECT_LE(res, 12);
}
for (std::size_t idx = 0U; idx < max_iterations; ++idx) {
auto res = utils::generate_random_between(-5, -1);
EXPECT_GE(res, -5);
EXPECT_LE(res, -1);
}
}
TEST(utils_common, generate_random_between_for_unsigned_integers) {
static constexpr const auto max_iterations{1000000UL};
for (std::size_t idx = 0U; idx < max_iterations; ++idx) {
auto res = utils::generate_random_between(5U, 12U);
EXPECT_GE(res, 5);
EXPECT_LE(res, 12);
}
}
TEST(utils_common, generate_random_between_throws_error_on_invalid_range) {
EXPECT_THROW(
{
try {
[[maybe_unused]] auto res = utils::generate_random_between(12, 5);
} catch (const std::range_error &e) {
EXPECT_STREQ("end must be greater than begin", e.what());
throw;
}
},
std::range_error);
EXPECT_THROW(
{
try {
[[maybe_unused]] auto res = utils::generate_random_between(12, 12);
} catch (const std::range_error &e) {
EXPECT_STREQ("end must be greater than begin", e.what());
throw;
}
},
std::range_error);
}
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());
}
TEST(utils_common, get_environment_variable) {
static constexpr const std::string path_env{"PATH"};
std::string path;
#if defined(_WIN32)
path.resize(repertory::max_path_length + 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 = 1025U; port < 1030U; ++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

View File

@ -0,0 +1,300 @@
/*
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.
*/
#include "test.hpp"
#if defined(PROJECT_ENABLE_SQLITE)
namespace repertory {
class utils_db_sqlite : public ::testing::Test {
public:
utils::db::sqlite::db3_t db3;
void SetUp() override {
{
sqlite3 *db3_ptr{nullptr};
auto res = sqlite3_open(":memory:", &db3_ptr);
ASSERT_EQ(SQLITE_OK, res);
ASSERT_TRUE(db3_ptr != nullptr);
db3 = utils::db::sqlite::db3_t{
db3_ptr,
utils::db::sqlite::sqlite3_deleter(),
};
}
utils::db::sqlite::db3_stmt_t db3_stmt;
{
std::string sql{
"CREATE TABLE [table] (column1 TEXT PRIMARY KEY UNIQUE "
"NOT NULL, column2 TEXT NOT NULL);",
};
sqlite3_stmt *stmt_ptr{nullptr};
auto res =
sqlite3_prepare_v2(db3.get(), sql.c_str(), -1, &stmt_ptr, nullptr);
db3_stmt = utils::db::sqlite::db3_stmt_t{
stmt_ptr,
utils::db::sqlite::sqlite3_statement_deleter(),
};
ASSERT_EQ(SQLITE_OK, res);
}
auto res = sqlite3_step(db3_stmt.get());
ASSERT_EQ(SQLITE_DONE, res);
}
void TearDown() override { db3.reset(); }
};
static void common_insert(sqlite3 &db3, bool dump = false) {
auto query = utils::db::sqlite::db_insert{db3, "table"}
.column_value("column1", "test0")
.column_value("column2", "test1");
if (dump) {
std::cout << query.dump() << std::endl;
}
auto res = query.go();
EXPECT_TRUE(res.ok());
}
static void common_select(sqlite3 &db3, std::string value1, std::string value2,
bool dump = false) {
auto query = utils::db::sqlite::db_select{db3, "table"};
if (dump) {
std::cout << query.dump() << std::endl;
}
auto res = query.go();
EXPECT_TRUE(res.ok());
EXPECT_TRUE(res.has_row());
std::size_t row_count{};
while (res.has_row()) {
std::optional<utils::db::sqlite::db_result::row> row;
EXPECT_TRUE(res.get_row(row));
EXPECT_TRUE(row.has_value());
if (row.has_value()) {
auto columns = row.value().get_columns();
EXPECT_EQ(std::size_t(2U), columns.size());
EXPECT_STREQ("column1", columns[0U].get_name().c_str());
EXPECT_STREQ(value1.c_str(),
columns[0U].get_value<std::string>().c_str());
EXPECT_STREQ("column2", columns[1U].get_name().c_str());
EXPECT_STREQ(value2.c_str(),
columns[1U].get_value<std::string>().c_str());
for (auto &&column : columns) {
std::cout << column.get_index() << ':';
std::cout << column.get_name() << ':';
std::cout << column.get_value<std::string>() << std::endl;
}
}
++row_count;
}
EXPECT_EQ(std::size_t(1U), row_count);
}
static void common_delete(sqlite3 &db3, bool dump = false) {
{
auto query = utils::db::sqlite::db_delete{db3, "table"};
if (dump) {
std::cout << query.dump() << std::endl;
}
auto res = query.go();
EXPECT_TRUE(res.ok());
}
{
auto query = utils::db::sqlite::db_select{db3, "table"};
auto res = query.go();
EXPECT_TRUE(res.ok());
std::size_t row_count{};
while (res.has_row()) {
++row_count;
}
EXPECT_EQ(std::size_t(0U), row_count);
}
}
TEST_F(utils_db_sqlite, db_delete_query) {
auto query = utils::db::sqlite::db_delete{*db3.get(), "table"};
auto query_str = query.dump();
std::cout << query_str << std::endl;
EXPECT_STREQ(R"(DELETE FROM "table";)", query_str.c_str());
}
TEST_F(utils_db_sqlite, db_delete_where_query) {
auto query = utils::db::sqlite::db_delete{*db3.get(), "table"}
.where("column1")
.equals("test1")
.and_()
.where("column2")
.equals("test2");
auto query_str = query.dump();
std::cout << query_str << std::endl;
EXPECT_STREQ(R"(DELETE FROM "table" WHERE "column1"=?1 AND "column2"=?2;)",
query_str.c_str());
}
TEST_F(utils_db_sqlite, db_insert_query) {
auto query = utils::db::sqlite::db_insert{*db3.get(), "table"}
.column_value("column1", "test9")
.column_value("column2", "test9");
auto query_str = query.dump();
std::cout << query_str << std::endl;
EXPECT_STREQ(R"(INSERT INTO "table" ("column1", "column2") VALUES (?1, ?2);)",
query_str.c_str());
}
TEST_F(utils_db_sqlite, db_insert_or_replace_query) {
auto query = utils::db::sqlite::db_insert{*db3.get(), "table"}
.or_replace()
.column_value("column1", "test1")
.column_value("column2", "test2");
auto query_str = query.dump();
std::cout << query_str << std::endl;
EXPECT_STREQ(
R"(INSERT OR REPLACE INTO "table" ("column1", "column2") VALUES (?1, ?2);)",
query_str.c_str());
}
TEST_F(utils_db_sqlite, db_select_query) {
auto query = utils::db::sqlite::db_select{*db3.get(), "table"};
auto query_str = query.dump();
std::cout << query_str << std::endl;
EXPECT_STREQ(R"(SELECT * FROM "table";)", query_str.c_str());
}
TEST_F(utils_db_sqlite, db_select_where_query) {
auto query = utils::db::sqlite::db_select{*db3.get(), "table"}
.where("column1")
.equals("test1")
.and_()
.where("column2")
.equals("test2");
auto query_str = query.dump();
std::cout << query_str << std::endl;
EXPECT_STREQ(R"(SELECT * FROM "table" WHERE "column1"=?1 AND "column2"=?2;)",
query_str.c_str());
}
TEST_F(utils_db_sqlite, db_select_where_with_group_query) {
auto query =
utils::db::sqlite::db_select{*db3.get(), "table"}
.group([](auto &grp) {
grp.where("column1").equals("a").or_().where("column1").equals("b");
})
.and_()
.group([](auto &grp) {
grp.where("column2").equals("c").or_().where("column2").equals("d");
})
.or_()
.group([](auto &grp) {
grp.where("column1").equals("e").or_().where("column2").equals("f");
});
auto query_str = query.dump();
std::cout << query_str << std::endl;
EXPECT_STREQ(
R"(SELECT * FROM "table" WHERE ("column1"=?1 OR "column1"=?2) AND ("column2"=?3 OR "column2"=?4) OR ("column1"=?5 OR "column2"=?6);)",
query_str.c_str());
}
TEST_F(utils_db_sqlite, db_select_columns_query) {
auto query = utils::db::sqlite::db_select{*db3.get(), "table"}
.column("column1")
.column("column2")
.where("column1")
.equals("test1")
.and_()
.where("column2")
.equals("test2");
auto query_str = query.dump();
std::cout << query_str << std::endl;
EXPECT_STREQ(
R"(SELECT column1, column2 FROM "table" WHERE "column1"=?1 AND "column2"=?2;)",
query_str.c_str());
}
TEST_F(utils_db_sqlite, db_update_query) {
auto query = utils::db::sqlite::db_update{*db3.get(), "table"}
.column_value("column1", "moose")
.where("column1")
.equals("test1")
.and_()
.where("column2")
.equals("test2");
auto query_str = query.dump();
std::cout << query_str << std::endl;
EXPECT_STREQ(
R"(UPDATE "table" SET "column1"=?1 WHERE "column1"=?2 AND "column2"=?3;)",
query_str.c_str());
}
TEST_F(utils_db_sqlite, insert_select_delete) {
common_insert(*db3.get(), true);
common_select(*db3.get(), "test0", "test1", true);
common_delete(*db3.get(), true);
}
TEST_F(utils_db_sqlite, insert_update_delete) {
common_insert(*db3.get());
{
auto query = utils::db::sqlite::db_update{*db3.get(), "table"}
.column_value("column1", "moose")
.where("column1")
.equals("test0");
std::cout << query.dump() << std::endl;
auto res = query.go();
EXPECT_TRUE(res.ok());
}
common_select(*db3.get(), "moose", "test1");
common_delete(*db3.get());
}
TEST_F(utils_db_sqlite, insert_or_replace_and_delete) {
common_insert(*db3.get());
{
auto query = utils::db::sqlite::db_insert{*db3.get(), "table"}
.or_replace()
.column_value("column1", "test0")
.column_value("column2", "moose");
std::cout << query.dump() << std::endl;
auto res = query.go();
EXPECT_TRUE(res.ok());
}
common_select(*db3.get(), "test0", "moose");
common_delete(*db3.get());
}
} // namespace repertory
#endif // defined(PROJECT_ENABLE_SQLITE)

View File

@ -0,0 +1,225 @@
/*
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.
*/
#include "test.hpp"
#if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
namespace repertory {
TEST(utils_encrypting_reader, read_file_data) {
const auto token = std::string("moose");
auto &source_file = test::create_random_file(
8U * utils::encryption::encrypting_reader::get_data_chunk_size());
EXPECT_TRUE(source_file);
if (source_file) {
stop_type stop_requested{false};
utils::encryption::encrypting_reader reader(
"test.dat", source_file.get_path(), stop_requested, token);
for (std::uint8_t i = 0U; i < 8U; i++) {
data_buffer buffer(
utils::encryption::encrypting_reader::get_encrypted_chunk_size());
for (std::uint8_t j = 0U; j < 2U; j++) {
ASSERT_EQ(
buffer.size() / 2U,
utils::encryption::encrypting_reader::reader_function(
reinterpret_cast<char *>(&buffer[(buffer.size() / 2U) * j]),
buffer.size() / 2U, 1U, &reader));
}
data_buffer decrypted_data;
EXPECT_TRUE(
utils::encryption::decrypt_data(token, buffer, decrypted_data));
EXPECT_EQ(utils::encryption::encrypting_reader::get_data_chunk_size(),
decrypted_data.size());
std::size_t bytes_read{};
data_buffer file_data(decrypted_data.size());
EXPECT_TRUE(source_file.read(
file_data,
utils::encryption::encrypting_reader::get_data_chunk_size() * i,
&bytes_read));
EXPECT_EQ(0, std::memcmp(file_data.data(), decrypted_data.data(),
file_data.size()));
}
}
}
TEST(utils_encrypting_reader, read_file_data_in_multiple_chunks) {
const auto token = std::string("moose");
auto &source_file = test::create_random_file(
8U * utils::encryption::encrypting_reader::get_data_chunk_size());
EXPECT_TRUE(source_file);
if (source_file) {
stop_type stop_requested{false};
utils::encryption::encrypting_reader reader(
"test.dat", source_file.get_path(), stop_requested, token);
for (std::uint8_t i = 0U; i < 8U; i += 2U) {
data_buffer buffer(
utils::encryption::encrypting_reader::get_encrypted_chunk_size() *
2U);
EXPECT_EQ(buffer.size(),
utils::encryption::encrypting_reader::reader_function(
reinterpret_cast<char *>(buffer.data()), buffer.size(), 1U,
&reader));
for (std::uint8_t j = 0U; j < 2U; j++) {
data_buffer decrypted_data;
const auto offset = (j * (buffer.size() / 2U));
EXPECT_TRUE(utils::encryption::decrypt_data(
token,
data_buffer(
std::next(buffer.begin(), static_cast<std::int64_t>(offset)),
std::next(buffer.begin(), static_cast<std::int64_t>(
offset + (buffer.size() / 2U)))),
decrypted_data));
EXPECT_EQ(utils::encryption::encrypting_reader::get_data_chunk_size(),
decrypted_data.size());
std::size_t bytes_read{};
data_buffer file_data(decrypted_data.size());
EXPECT_TRUE(source_file.read(
file_data,
(utils::encryption::encrypting_reader::get_data_chunk_size() * i) +
(j *
utils::encryption::encrypting_reader::get_data_chunk_size()),
&bytes_read));
EXPECT_EQ(0, std::memcmp(file_data.data(), decrypted_data.data(),
file_data.size()));
}
}
}
}
TEST(utils_encrypting_reader, read_file_data_as_stream) {
const auto token = std::string("moose");
auto &source_file = test::create_random_file(
8U * utils::encryption::encrypting_reader::get_data_chunk_size());
EXPECT_TRUE(source_file);
if (source_file) {
stop_type stop_requested{false};
utils::encryption::encrypting_reader reader(
"test.dat", source_file.get_path(), stop_requested, token);
auto io_stream = reader.create_iostream();
EXPECT_FALSE(io_stream->seekg(0, std::ios_base::end).fail());
EXPECT_TRUE(io_stream->good());
EXPECT_EQ(reader.get_total_size(),
static_cast<std::uint64_t>(io_stream->tellg()));
EXPECT_FALSE(io_stream->seekg(0, std::ios_base::beg).fail());
EXPECT_TRUE(io_stream->good());
for (std::uint8_t i = 0U; i < 8U; i++) {
data_buffer buffer(
utils::encryption::encrypting_reader::get_encrypted_chunk_size());
EXPECT_FALSE(
io_stream->seekg(static_cast<std::streamoff>(i * buffer.size()))
.fail());
EXPECT_TRUE(io_stream->good());
for (std::uint8_t j = 0U; j < 2U; j++) {
EXPECT_FALSE(
io_stream
->read(
reinterpret_cast<char *>(&buffer[(buffer.size() / 2U) * j]),
static_cast<std::streamsize>(buffer.size()) / 2U)
.fail());
EXPECT_TRUE(io_stream->good());
}
data_buffer decrypted_data;
EXPECT_TRUE(
utils::encryption::decrypt_data(token, buffer, decrypted_data));
EXPECT_EQ(utils::encryption::encrypting_reader::get_data_chunk_size(),
decrypted_data.size());
std::size_t bytes_read{};
data_buffer file_data(decrypted_data.size());
EXPECT_TRUE(source_file.read(
file_data,
utils::encryption::encrypting_reader::get_data_chunk_size() * i,
&bytes_read));
EXPECT_EQ(0, std::memcmp(file_data.data(), decrypted_data.data(),
file_data.size()));
}
}
}
TEST(utils_encrypting_reader, read_file_data_in_multiple_chunks_as_stream) {
const auto token = std::string("moose");
auto &source_file = test::create_random_file(
8u * utils::encryption::encrypting_reader::get_data_chunk_size());
EXPECT_TRUE(source_file);
if (source_file) {
stop_type stop_requested{false};
utils::encryption::encrypting_reader reader(
"test.dat", source_file.get_path(), stop_requested, token);
auto io_stream = reader.create_iostream();
EXPECT_FALSE(io_stream->seekg(0, std::ios_base::end).fail());
EXPECT_TRUE(io_stream->good());
EXPECT_EQ(reader.get_total_size(),
static_cast<std::uint64_t>(io_stream->tellg()));
EXPECT_FALSE(io_stream->seekg(0, std::ios_base::beg).fail());
EXPECT_TRUE(io_stream->good());
for (std::uint8_t i = 0U; i < 8U; i += 2U) {
data_buffer buffer(
utils::encryption::encrypting_reader::get_encrypted_chunk_size() *
2U);
EXPECT_FALSE(io_stream
->read(reinterpret_cast<char *>(buffer.data()),
static_cast<std::streamsize>(buffer.size()))
.fail());
EXPECT_TRUE(io_stream->good());
for (std::uint8_t j = 0U; j < 2U; j++) {
data_buffer decrypted_data;
const auto offset = (j * (buffer.size() / 2U));
EXPECT_TRUE(utils::encryption::decrypt_data(
token,
data_buffer(
std::next(buffer.begin(), static_cast<std::int64_t>(offset)),
std::next(buffer.begin(), static_cast<std::int64_t>(
offset + (buffer.size() / 2U)))),
decrypted_data));
EXPECT_EQ(utils::encryption::encrypting_reader::get_data_chunk_size(),
decrypted_data.size());
std::size_t bytes_read{};
data_buffer file_data(decrypted_data.size());
EXPECT_TRUE(source_file.read(
file_data,
(utils::encryption::encrypting_reader::get_data_chunk_size() * i) +
(j *
utils::encryption::encrypting_reader::get_data_chunk_size()),
&bytes_read));
EXPECT_EQ(0, std::memcmp(file_data.data(), decrypted_data.data(),
file_data.size()));
}
}
}
}
} // namespace repertory
#endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)

View File

@ -0,0 +1,283 @@
/*
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.
*/
#include "test.hpp"
#if defined(PROJECT_ENABLE_LIBSODIUM)
namespace repertory {
static constexpr const std::string_view token{"moose"};
static constexpr const std::wstring_view token_w{L"moose"};
TEST(utils_encryption, generate_key) {
auto key1 =
utils::encryption::generate_key<utils::encryption::hash_256_t>(token);
EXPECT_STREQ(
"ab4a0b004e824962913f7c0f79582b6ec7a3b8726426ca61d1a0a28ce5049e96",
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);
#if defined(_WIN32)
EXPECT_STREQ(
L"4f5eb2a2ab34e3777b230465283923080b9ba59311e74058ccd74185131d11fe",
utils::collection::to_hex_wstring(key1_w).c_str());
#else // !defined(_WIN32)
EXPECT_STREQ(
L"0392d95ed3eee9772fbb9af68fedf829a8eb0adbe8575d9691cc9a752196766a",
utils::collection::to_hex_wstring(key1_w).c_str());
#endif
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_blake2b_256) {
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_blake2b_256(
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_blake2b_256(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, utils::encryption::blake2b_256_hasher);
EXPECT_STREQ(
"ab4a0b004e824962913f7c0f79582b6ec7a3b8726426ca61d1a0a28ce5049e96",
utils::collection::to_hex_string(key1).c_str());
auto key2 = utils::encryption::generate_key<utils::encryption::hash_256_t>(
token, utils::encryption::sha256_hasher);
EXPECT_NE(key1, key2);
EXPECT_STREQ(
"182072537ada59e4d6b18034a80302ebae935f66adbdf0f271d3d36309c2d481",
utils::collection::to_hex_string(key2).c_str());
auto key1_w = utils::encryption::generate_key<utils::encryption::hash_256_t>(
token_w, utils::encryption::blake2b_256_hasher);
#if defined(_WIN32)
EXPECT_STREQ(
L"4f5eb2a2ab34e3777b230465283923080b9ba59311e74058ccd74185131d11fe",
utils::collection::to_hex_wstring(key1_w).c_str());
#else // !defined(_WIN32)
EXPECT_STREQ(
L"0392d95ed3eee9772fbb9af68fedf829a8eb0adbe8575d9691cc9a752196766a",
utils::collection::to_hex_wstring(key1_w).c_str());
#endif
auto key2_w = utils::encryption::generate_key<utils::encryption::hash_256_t>(
token_w, utils::encryption::sha256_hasher);
EXPECT_NE(key1_w, key2_w);
#if defined(_WIN32)
EXPECT_STREQ(
L"918e4c6d39bb373f139b5fac8ec0548a9770da399b2835608974ffeac7fab6c4",
utils::collection::to_hex_wstring(key2_w).c_str());
#else // !defined(_WIN32)
EXPECT_STREQ(
L"590ac70125bec4501172937f6a2cbdeb22a87b5e40d5595eccd06b2b20548d8f",
utils::collection::to_hex_wstring(key2_w).c_str());
#endif
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 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, 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<utils::encryption::hash_256_t>(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<utils::encryption::hash_256_t>(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<utils::encryption::hash_256_t>(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<utils::encryption::hash_256_t>(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<utils::encryption::hash_256_t>(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<utils::encryption::hash_256_t>(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));
}
TEST(utils_encryption, decrypt_file_name) {
auto &source_file = test::create_random_file(
8U * utils::encryption::encrypting_reader::get_data_chunk_size());
EXPECT_TRUE(source_file);
if (source_file) {
stop_type stop_requested{false};
utils::encryption::encrypting_reader reader(
"test.dat", source_file.get_path(), stop_requested, token,
std::nullopt);
auto file_name = reader.get_encrypted_file_name();
EXPECT_EQ(true, utils::encryption::decrypt_file_name(token, file_name));
EXPECT_STREQ("test.dat", file_name.c_str());
}
}
TEST(utils_encryption, decrypt_file_path) {
auto &source_file = test::create_random_file(
8U * utils::encryption::encrypting_reader::get_data_chunk_size());
EXPECT_TRUE(source_file);
if (source_file) {
stop_type stop_requested{false};
utils::encryption::encrypting_reader reader(
"test.dat", source_file.get_path(), stop_requested, token, "moose/cow");
auto file_path = reader.get_encrypted_file_path();
EXPECT_EQ(true, utils::encryption::decrypt_file_path(token, file_path));
EXPECT_STREQ("/moose/cow/test.dat", file_path.c_str());
}
}
#endif // defined(PROJECT_ENABLE_BOOST)
} // namespace repertory
#endif // defined(PROJECT_ENABLE_LIBSODIUM)

View File

@ -0,0 +1,73 @@
/*
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.
*/
#include "test.hpp"
namespace repertory {
template <typename T, typename U>
constexpr bool is_decay_equ = std::is_same_v<std::decay_t<T>, U>;
TEST(utils_error, check_default_exception_handler) {
EXPECT_TRUE(utils::error::get_exception_handler() != nullptr);
if (&utils::error::default_exception_handler ==
utils::error::get_exception_handler()) {
auto default_handler_is_iostream =
is_decay_equ<decltype(utils::error::default_exception_handler),
utils::error::iostream_exception_handler>;
EXPECT_TRUE(default_handler_is_iostream);
}
}
TEST(utils_error, can_override_exception_handler) {
struct my_exc_handler final : public utils::error::i_exception_handler {
MOCK_METHOD(void, handle_error,
(std::string_view function_name, std::string_view msg),
(const, override));
MOCK_METHOD(void, handle_exception, (std::string_view function_name),
(const, override));
MOCK_METHOD(void, handle_exception,
(std::string_view function_name, const std::exception &ex),
(const, override));
};
my_exc_handler handler{};
utils::error::set_exception_handler(&handler);
EXPECT_CALL(handler, handle_error("test_func", "error")).WillOnce(Return());
utils::error::handle_error("test_func", "error");
EXPECT_CALL(handler, handle_exception("test_func")).WillOnce(Return());
utils::error::handle_exception("test_func");
auto ex = std::runtime_error("moose");
EXPECT_CALL(handler, handle_exception(_, _))
.WillOnce(
[&ex](std::string_view function_name, const std::exception &ex2) {
EXPECT_EQ("test_func_ex", function_name);
EXPECT_STREQ(ex.what(), ex2.what());
});
utils::error::handle_exception("test_func_ex", ex);
utils::error::set_exception_handler(&utils::error::default_exception_handler);
}
} // namespace repertory

View File

@ -0,0 +1,523 @@
/*
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.
*/
#include "test.hpp"
namespace {
static constexpr const auto file_type_count{1U};
}
namespace repertory {
TEST(utils_file, can_create_and_remove_file) {
for (auto idx = 0U; idx < file_type_count; ++idx) {
auto path = test::generate_test_file_name("utils_file");
EXPECT_FALSE(utils::file::file(path).exists() ||
utils::file::directory(path).exists());
auto file = idx == 0U ? utils::file::file::open_or_create_file(path)
: utils::file::thread_file::open_or_create_file(path);
EXPECT_TRUE(*file);
EXPECT_TRUE(utils::file::file(path).exists());
EXPECT_TRUE(file->exists());
EXPECT_TRUE(file->remove());
EXPECT_FALSE(utils::file::file(path).exists());
EXPECT_FALSE(file->exists());
}
}
TEST(utils_file, can_open_file) {
for (auto idx = 0U; idx < file_type_count; ++idx) {
auto path = test::generate_test_file_name("utils_file");
{
auto file = idx == 0U
? utils::file::file::open_or_create_file(path)
: utils::file::thread_file::open_or_create_file(path);
EXPECT_TRUE(*file);
}
{
auto file = idx == 0U ? utils::file::file::open_file(path)
: utils::file::thread_file::open_file(path);
EXPECT_TRUE(*file);
}
}
}
TEST(utils_file, open_file_fails_if_not_found) {
for (auto idx = 0U; idx < file_type_count; ++idx) {
auto path = test::generate_test_file_name("utils_file");
auto file = idx == 0U ? utils::file::file::open_file(path)
: utils::file::thread_file::open_file(path);
EXPECT_FALSE(*file);
}
}
TEST(utils_file, write_fails_for_read_only_file) {
for (auto idx = 0U; idx < file_type_count; ++idx) {
auto path = test::generate_test_file_name("utils_file");
auto file = idx == 0U
? utils::file::file::open_or_create_file(path, true)
: utils::file::thread_file::open_or_create_file(path, true);
EXPECT_TRUE(utils::file::file(path).exists());
EXPECT_TRUE(*file);
std::size_t bytes_written{};
EXPECT_FALSE(file->write(reinterpret_cast<const unsigned char *>("0"), 1U,
0U, &bytes_written));
EXPECT_EQ(0U, bytes_written);
}
}
// TEST(utils_file, can_attach_file) {
// for (auto idx = 0U; idx < file_type_count; ++idx) {
// auto path = test::generate_test_file_name("utils_file");
// auto file = idx == 0U ? utils::file::file::open_or_create_file(path)
// :
// utils::file::thread_file::open_or_create_file(path);
// auto file2 =
// idx == 0U ? utils::file::file::attach_file(file->get_handle())
// :
// utils::file::thread_file::attach_file(file->get_handle());
// EXPECT_TRUE(*file);
// EXPECT_TRUE(*file2);
// EXPECT_EQ(file->get_path(), file2->get_path());
// }
// }
#if defined(PROJECT_ENABLE_JSON)
TEST(utils_file, read_and_write_json_file) {
auto path = test::generate_test_file_name("utils_file");
auto json_data = nlohmann::json({{"moose", "cow"}});
EXPECT_TRUE(utils::file::write_json_file(path, json_data));
{
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 json_data = nlohmann::json({{"moose", "cow"}});
EXPECT_TRUE(utils::file::write_json_file(path, json_data, "moose"));
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 path =
utils::string::from_utf8(test::generate_test_file_name("utils_file"));
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)
#endif // defined(PROJECT_ENABLE_JSON)
#if defined(PROJECT_ENABLE_LIBDSM)
TEST(utils_file, smb_create_smb_path) {
auto path = "//server/share";
auto rel_path = "test/test.txt";
auto smb_path = utils::file::smb_create_smb_path(path, rel_path);
EXPECT_STREQ("//server/share/test/test.txt", smb_path.c_str());
rel_path = "/test/test.txt";
smb_path = utils::file::smb_create_smb_path(path, rel_path);
EXPECT_STREQ("//server/share/test/test.txt", smb_path.c_str());
rel_path = "test\\test.txt";
smb_path = utils::file::smb_create_smb_path(path, rel_path);
EXPECT_STREQ("//server/share/test/test.txt", smb_path.c_str());
rel_path = "\\test\\test.txt";
smb_path = utils::file::smb_create_smb_path(path, rel_path);
EXPECT_STREQ("//server/share/test/test.txt", smb_path.c_str());
}
TEST(utils_file, smb_create_relative_path) {
auto path = "//server/share/test.txt";
auto rel_path = utils::file::smb_create_relative_path(path);
EXPECT_STREQ("\\test.txt", rel_path.c_str());
path = "//server/share/test";
rel_path = utils::file::smb_create_relative_path(path);
EXPECT_STREQ("\\test", rel_path.c_str());
path = "//server/share/test/";
rel_path = utils::file::smb_create_relative_path(path);
EXPECT_STREQ("\\test", rel_path.c_str());
path = "//server/share/test/";
rel_path = utils::file::smb_create_relative_path(path);
EXPECT_STREQ("\\test", rel_path.c_str());
}
TEST(utils_file, smb_create_search_path) {
auto path = "//server/share";
auto search_path = utils::file::smb_create_search_path(path);
EXPECT_STREQ("\\*", search_path.c_str());
path = "//server/share/";
search_path = utils::file::smb_create_search_path(path);
EXPECT_STREQ("\\*", search_path.c_str());
path = "//server/share/folder";
search_path = utils::file::smb_create_search_path(path);
EXPECT_STREQ("\\folder\\*", search_path.c_str());
path = "//server/share/folder/";
search_path = utils::file::smb_create_search_path(path);
EXPECT_STREQ("\\folder\\*", search_path.c_str());
path = "//server/share/folder/next";
search_path = utils::file::smb_create_search_path(path);
EXPECT_STREQ("\\folder\\next\\*", search_path.c_str());
path = "//server/share/folder/next/";
search_path = utils::file::smb_create_search_path(path);
EXPECT_STREQ("\\folder\\next\\*", search_path.c_str());
}
TEST(utils_file, smb_parent_is_same) {
auto path1 = "//server/share";
auto path2 = "//server/share";
EXPECT_TRUE(utils::file::smb_parent_is_same(path1, path2));
path1 = "//server/share/";
path2 = "//server/share/";
EXPECT_TRUE(utils::file::smb_parent_is_same(path1, path2));
path1 = "//server/share/one";
path2 = "//server/share/two";
EXPECT_TRUE(utils::file::smb_parent_is_same(path1, path2));
path1 = "// server/cow";
path2 = "// server/cow";
EXPECT_TRUE(utils::file::smb_parent_is_same(path1, path2));
}
TEST(utils_file, smb_parent_is_not_same) {
auto path1 = "server/share";
auto path2 = "//server/share";
EXPECT_FALSE(utils::file::smb_parent_is_same(path1, path2));
path1 = "server/share/";
path2 = "server/share/";
EXPECT_FALSE(utils::file::smb_parent_is_same(path1, path2));
path1 = "//server1/share/one";
path2 = "//server/share/two";
EXPECT_FALSE(utils::file::smb_parent_is_same(path1, path2));
path1 = "//server/share";
path2 = "//server/share2";
EXPECT_FALSE(utils::file::smb_parent_is_same(path1, path2));
path1 = "//server/share/";
path2 = "//server/share2/";
EXPECT_FALSE(utils::file::smb_parent_is_same(path1, path2));
path1 = "//server/share/one";
path2 = "//server/share2/two";
EXPECT_FALSE(utils::file::smb_parent_is_same(path1, path2));
path1 = "//server";
path2 = "//server/share/two";
EXPECT_FALSE(utils::file::smb_parent_is_same(path1, path2));
path1 = "//server/";
path2 = "//server/";
EXPECT_FALSE(utils::file::smb_parent_is_same(path1, path2));
path1 = "//server";
path2 = "//server";
EXPECT_FALSE(utils::file::smb_parent_is_same(path1, path2));
}
#endif // defined(PROJECT_ENABLE_LIBDSM)
TEST(utils_file, directory_exists_in_path) {
auto &test_dir = test::generate_test_directory();
EXPECT_FALSE(
utils::file::directory_exists_in_path(test_dir.get_path(), "moose"));
EXPECT_FALSE(utils::file::directory_exists_in_path(
utils::string::from_utf8(test_dir.get_path()), L"moose"));
EXPECT_FALSE(utils::file::file_exists_in_path(test_dir.get_path(), "moose"));
EXPECT_FALSE(utils::file::file_exists_in_path(
utils::string::from_utf8(test_dir.get_path()), L"moose"));
auto sub_dir = test_dir.create_directory("moose");
EXPECT_TRUE(sub_dir != nullptr);
if (sub_dir) {
EXPECT_TRUE(
utils::file::directory_exists_in_path(test_dir.get_path(), "moose"));
EXPECT_FALSE(
utils::file::file_exists_in_path(test_dir.get_path(), "moose"));
EXPECT_TRUE(utils::file::directory_exists_in_path(
utils::string::from_utf8(test_dir.get_path()), L"moose"));
EXPECT_FALSE(utils::file::file_exists_in_path(
utils::string::from_utf8(test_dir.get_path()), L"moose"));
}
}
TEST(utils_file, file_exists_in_path) {
auto &test_dir = test::generate_test_directory();
EXPECT_FALSE(
utils::file::file_exists_in_path(test_dir.get_path(), "moose.txt"));
EXPECT_FALSE(utils::file::file_exists_in_path(
utils::string::from_utf8(test_dir.get_path()), L"moose.txt"));
EXPECT_FALSE(
utils::file::directory_exists_in_path(test_dir.get_path(), "moose.txt"));
EXPECT_FALSE(utils::file::directory_exists_in_path(
utils::string::from_utf8(test_dir.get_path()), L"moose.txt"));
auto sub_file = test_dir.create_file("moose.txt", false);
EXPECT_TRUE(sub_file != nullptr);
if (sub_file) {
EXPECT_TRUE(
utils::file::file_exists_in_path(test_dir.get_path(), "moose.txt"));
EXPECT_FALSE(utils::file::directory_exists_in_path(test_dir.get_path(),
"moose.txt"));
EXPECT_TRUE(utils::file::file_exists_in_path(
utils::string::from_utf8(test_dir.get_path()), L"moose.txt"));
EXPECT_FALSE(utils::file::directory_exists_in_path(
utils::string::from_utf8(test_dir.get_path()), L"moose.txt"));
}
}
TEST(utils_file, get_free_drive_space) {
#if defined(_WIN32)
auto space = utils::file::get_free_drive_space("C:");
auto space2 = utils::file::get_free_drive_space(L"C:");
#else // defined(_WIN32)
auto space = utils::file::get_free_drive_space("/");
auto space2 = utils::file::get_free_drive_space(L"/");
#endif // !defined(_WIN32)
EXPECT_TRUE(space.has_value());
EXPECT_LT(0U, space.value());
EXPECT_TRUE(space2.has_value());
EXPECT_EQ(space.value(), space2.value());
}
TEST(utils_file, get_free_drive_space_fails_for_bad_path) {
std::string name{"free_drive_space_test_XXXXXX"};
auto temp = utils::file::create_temp_name("free_drive_space_test");
auto space = utils::file::get_free_drive_space(temp);
EXPECT_FALSE(space.has_value());
}
TEST(utils_file, get_total_drive_space) {
#if defined(_WIN32)
auto space = utils::file::get_total_drive_space("C:");
auto space2 = utils::file::get_total_drive_space(L"C:");
#else // defined(_WIN32)
auto space = utils::file::get_total_drive_space("/");
auto space2 = utils::file::get_total_drive_space(L"/");
#endif // !defined(_WIN32)
EXPECT_TRUE(space.has_value());
EXPECT_LT(0U, space.value());
EXPECT_TRUE(space2.has_value());
EXPECT_EQ(space.value(), space2.value());
}
TEST(utils_file, create_temp_name) {
{
auto temp = utils::file::create_temp_name("test_temp");
EXPECT_EQ(18U, temp.size());
auto temp2 = utils::file::create_temp_name("test_temp");
EXPECT_STRNE(temp.c_str(), temp2.c_str());
EXPECT_TRUE(utils::string::begins_with(temp, "test_temp_"));
}
{
auto temp = utils::file::create_temp_name(L"test_temp");
EXPECT_EQ(18U, temp.size());
auto temp2 = utils::file::create_temp_name(L"test_temp");
EXPECT_STRNE(temp.c_str(), temp2.c_str());
EXPECT_TRUE(utils::string::begins_with(temp, L"test_temp_"));
}
}
TEST(utils_file, get_total_drive_space_fails_for_bad_path) {
auto temp = utils::file::create_temp_name("total_drive_space_test");
auto space = utils::file::get_total_drive_space(temp);
EXPECT_FALSE(space.has_value());
}
TEST(utils_file, get_times) {
{
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));
EXPECT_LT(0U, times->get(utils::file::time_type::modified));
EXPECT_LT(0U, times->get(utils::file::time_type::written));
}
{
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));
EXPECT_LT(0U, times->get(utils::file::time_type::modified));
EXPECT_LT(0U, times->get(utils::file::time_type::written));
}
}
TEST(utils_file, get_times_fails_if_not_found) {
auto temp = utils::path::combine(".", {"get_times_test"});
auto times = utils::file::get_times(temp);
EXPECT_FALSE(times.has_value());
}
TEST(utils_file, get_time) {
{
auto file_path = 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(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_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_path, utils::file::time_type::written);
EXPECT_TRUE(file_time.has_value());
EXPECT_LT(0U, file_time.value());
}
{
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(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_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_path, utils::file::time_type::written);
EXPECT_TRUE(file_time.has_value());
EXPECT_LT(0U, file_time.value());
}
}
TEST(utils_file, get_time_fails_if_not_found) {
auto temp = utils::path::combine(".", {"get_times_test"});
auto file_time =
utils::file::get_time(temp, utils::file::time_type::accessed);
EXPECT_FALSE(file_time.has_value());
}
} // namespace repertory

View File

@ -0,0 +1,186 @@
/*
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.
*/
#include "test.hpp"
#if defined(PROJECT_ENABLE_LIBSODIUM)
namespace repertory {
TEST(utils_hash, hash_type_sizes) {
EXPECT_EQ(32U, utils::encryption::hash_256_t{}.size());
EXPECT_EQ(48U, utils::encryption::hash_384_t{}.size());
EXPECT_EQ(64U, utils::encryption::hash_512_t{}.size());
}
TEST(utils_hash, default_hasher_is_blake2b) {
EXPECT_EQ(
&utils::encryption::blake2b_256_hasher,
&utils::encryption::default_create_hash<utils::encryption::hash_256_t>());
EXPECT_EQ(
&utils::encryption::blake2b_384_hasher,
&utils::encryption::default_create_hash<utils::encryption::hash_384_t>());
EXPECT_EQ(
&utils::encryption::blake2b_512_hasher,
&utils::encryption::default_create_hash<utils::encryption::hash_512_t>());
}
TEST(utils_hash, blake2b_256) {
auto hash = utils::collection::to_hex_string(
utils::encryption::create_hash_blake2b_256("a"));
EXPECT_STREQ(
"8928aae63c84d87ea098564d1e03ad813f107add474e56aedd286349c0c03ea4",
hash.c_str());
hash = utils::collection::to_hex_string(
utils::encryption::create_hash_blake2b_256(L"a"));
#if defined(_WIN32)
EXPECT_STREQ(
"d2373b17cd8a8e19e39f52fa4905a274f93805fbb8bb4c7f3cb4b2cd6708ec8a",
hash.c_str());
#else // !defined(_WIN32)
EXPECT_STREQ(
"9fdf5757d7eea386f0d34d2c0e202527986febf1ebb4315fcf7fff40776fa41d",
hash.c_str());
#endif
hash = utils::collection::to_hex_string(
utils::encryption::create_hash_blake2b_256({1U}));
EXPECT_STREQ(
"ee155ace9c40292074cb6aff8c9ccdd273c81648ff1149ef36bcea6ebb8a3e25",
hash.c_str());
}
TEST(utils_hash, blake2b_384) {
auto hash = utils::collection::to_hex_string(
utils::encryption::create_hash_blake2b_384("a"));
EXPECT_STREQ("7d40de16ff771d4595bf70cbda0c4ea0a066a6046fa73d34471cd4d93d827d7"
"c94c29399c50de86983af1ec61d5dcef0",
hash.c_str());
hash = utils::collection::to_hex_string(
utils::encryption::create_hash_blake2b_384(L"a"));
#if defined(_WIN32)
EXPECT_STREQ("637fe31d1e955760ef31043d525d9321826a778ddbe82fcde45a98394241380"
"96675e2f87e36b53ab223a7fd254198fd",
hash.c_str());
#else // !defined(_WIN32)
EXPECT_STREQ("9d469bd9dab9d4b48b8688de7c22704a8de1b081294f9be294100dfa9f05c92"
"e8d3616476e46cd14f9e613fed80fd157",
hash.c_str());
#endif
hash = utils::collection::to_hex_string(
utils::encryption::create_hash_blake2b_384({1U}));
EXPECT_STREQ("42cfe875d08d816538103b906bb0b05202e0b09c4e981680c1110684fc7845b"
"c91c178fa167afcc445490644b2bf5f5b",
hash.c_str());
}
TEST(utils_hash, blake2b_512) {
auto hash = utils::collection::to_hex_string(
utils::encryption::create_hash_blake2b_512("a"));
EXPECT_STREQ(
"333fcb4ee1aa7c115355ec66ceac917c8bfd815bf7587d325aec1864edd24e34d5abe2c6"
"b1b5ee3face62fed78dbef802f2a85cb91d455a8f5249d330853cb3c",
hash.c_str());
hash = utils::collection::to_hex_string(
utils::encryption::create_hash_blake2b_512(L"a"));
#if defined(_WIN32)
EXPECT_STREQ(
"05970b95468b0b1941066ff189091493e73859ce41cde5ad08118e93ea1d81a57a144296"
"a26a9fe7781481bde97b886725e36e30b305d8bd5cce1ae36bf1564a",
hash.c_str());
#else // !defined(_WIN32)
EXPECT_STREQ(
"bbc187c6e4d8525655d0ada62d16eed59f3db3ab07e04fb0483fd4ae21d88b984774add9"
"b3fbcff56f9638091013994f8e2d4646fdbbcb4879e2b5160bbb755d",
hash.c_str());
#endif
hash = utils::collection::to_hex_string(
utils::encryption::create_hash_blake2b_512({1U}));
EXPECT_STREQ(
"9545ba37b230d8a2e716c4707586542780815b7c4088edcb9af6a9452d50f32474d5ba9a"
"ab52a67aca864ef2696981c2eadf49020416136afd838fb048d21653",
hash.c_str());
}
TEST(utils_hash, sha256) {
auto hash = utils::collection::to_hex_string(
utils::encryption::create_hash_sha256("a"));
EXPECT_STREQ(
"ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb",
hash.c_str());
hash = utils::collection::to_hex_string(
utils::encryption::create_hash_sha256(L"a"));
#if defined(_WIN32)
EXPECT_STREQ(
"ffe9aaeaa2a2d5048174df0b80599ef0197ec024c4b051bc9860cff58ef7f9f3",
hash.c_str());
#else // !defined(_WIN32)
EXPECT_STREQ(
"a2d398922901344d08180dc41d3e9d73d8c148c7f6e092835bbb28e02dbcf184",
hash.c_str());
#endif
hash = utils::collection::to_hex_string(
utils::encryption::create_hash_sha256({1U}));
EXPECT_STREQ(
"4bf5122f344554c53bde2ebb8cd2b7e3d1600ad631c385a5d7cce23c7785459a",
hash.c_str());
}
TEST(utils_hash, sha512) {
auto hash = utils::collection::to_hex_string(
utils::encryption::create_hash_sha512("a"));
EXPECT_STREQ(
"1f40fc92da241694750979ee6cf582f2d5d7d28e18335de05abc54d0560e0f5302860c65"
"2bf08d560252aa5e74210546f369fbbbce8c12cfc7957b2652fe9a75",
hash.c_str());
hash = utils::collection::to_hex_string(
utils::encryption::create_hash_sha512(L"a"));
#if defined(_WIN32)
EXPECT_STREQ(
"5c2ca3d50f46ece6066c53bd1a490cbe5f72d2738ae9417332e91e5c3f75205c639d71a9"
"a41d67d965fa137dddf439e0ab9443a6ea44915e90d8b5b566d1c076",
hash.c_str());
#else // !defined(_WIN32)
EXPECT_STREQ(
"a93498d992e81915075144cb304d2bdf040b336283f888252244882d8366dd3a6e2d9749"
"077114dda1a9aa1a7b69d33f7a781f003ccd12e599a6341014f29aaf",
hash.c_str());
#endif
hash = utils::collection::to_hex_string(
utils::encryption::create_hash_sha512({1U}));
EXPECT_STREQ(
"7b54b66836c1fbdd13d2441d9e1434dc62ca677fb68f5fe66a464baadecdbd00576f8d6b"
"5ac3bcc80844b7d50b1cc6603444bbe7cfcf8fc0aa1ee3c636d9e339",
hash.c_str());
}
} // namespace repertory
#endif // defined(PROJECT_ENABLE_LIBSODIUM)

View File

@ -0,0 +1,552 @@
/*
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.
*/
#include "test.hpp"
#if defined(_WIN32)
namespace {
static const auto test_path = [](std::string str) -> std::string {
#if defined(PROJECT_ENABLE_WIN32_LONG_PATH_NAMES)
if (repertory::utils::string::begins_with(str, "\\")) {
str = repertory::utils::string::to_lower(
std::filesystem::current_path().string().substr(0U, 2U)) +
str;
}
str = std::string{repertory::utils::path::long_notation} + str;
#else // !defined(PROJECT_ENABLE_WIN32_LONG_PATH_NAMES)
if (repertory::utils::string::begins_with(str, "\\")) {
str = repertory::utils::string::to_lower(
std::filesystem::current_path().string().substr(0U, 2U)) +
str;
}
#endif // defined(PROJECT_ENABLE_WIN32_LONG_PATH_NAMES)
return repertory::utils::string::right_trim(str, '\\');
};
} // namespace
#endif // defined(_WIN32)
namespace repertory {
TEST(utils_path, constants) {
EXPECT_EQ(std::string_view{"\\"}, utils::path::backslash);
EXPECT_EQ(std::wstring_view{L"\\"}, utils::path::backslash_w);
EXPECT_EQ(std::string_view{"."}, utils::path::dot);
EXPECT_EQ(std::wstring_view{L"."}, utils::path::dot_w);
EXPECT_EQ(std::string_view{".\\"}, utils::path::dot_backslash);
EXPECT_EQ(std::wstring_view{L".\\"}, utils::path::dot_backslash_w);
EXPECT_EQ(std::string_view{"./"}, utils::path::dot_slash);
EXPECT_EQ(std::wstring_view{L"./"}, utils::path::dot_slash_w);
EXPECT_EQ(std::string_view{"/"}, utils::path::slash);
EXPECT_EQ(std::wstring_view{L"/"}, utils::path::slash_w);
#if defined(_WIN32)
EXPECT_EQ(std::string_view{"\\\\"}, utils::path::unc_notation);
EXPECT_EQ(std::wstring_view{L"\\\\"}, utils::path::unc_notation_w);
#endif // defined(_WIN32)
}
TEST(utils_path, directory_seperator) {
#if defined(_WIN32)
EXPECT_EQ(utils::path::backslash, utils::path::directory_seperator);
EXPECT_EQ(utils::path::backslash_w, utils::path::directory_seperator_w);
EXPECT_EQ(utils::path::slash, utils::path::not_directory_seperator);
EXPECT_EQ(utils::path::slash_w, utils::path::not_directory_seperator_w);
#else // !defined(_WIN32)
EXPECT_EQ(utils::path::slash, utils::path::directory_seperator);
EXPECT_EQ(utils::path::slash_w, utils::path::directory_seperator_w);
EXPECT_EQ(utils::path::backslash, utils::path::not_directory_seperator);
EXPECT_EQ(utils::path::backslash_w, utils::path::not_directory_seperator_w);
#endif // defined(_WIN32)
}
TEST(utils_path, get_directory_seperator) {
#if defined(_WIN32)
EXPECT_EQ(utils::path::backslash,
utils::path::get_directory_seperator<char>());
EXPECT_EQ(utils::path::backslash_w,
utils::path::get_directory_seperator<wchar_t>());
EXPECT_EQ(utils::path::slash,
utils::path::get_not_directory_seperator<char>());
EXPECT_EQ(utils::path::slash_w,
utils::path::get_not_directory_seperator<wchar_t>());
#else // !defined(_WIN32)
EXPECT_EQ(utils::path::slash, utils::path::get_directory_seperator<char>());
EXPECT_EQ(utils::path::slash_w,
utils::path::get_directory_seperator<wchar_t>());
EXPECT_EQ(utils::path::backslash,
utils::path::get_not_directory_seperator<char>());
EXPECT_EQ(utils::path::backslash_w,
utils::path::get_not_directory_seperator<wchar_t>());
#endif // defined(_WIN32)
}
TEST(utils_path, get_backslash) {
EXPECT_EQ(utils::path::backslash, utils::path::get_backslash<char>());
EXPECT_EQ(utils::path::backslash_w, utils::path::get_backslash<wchar_t>());
}
TEST(utils_path, get_dot) {
EXPECT_EQ(utils::path::dot, utils::path::get_dot<char>());
EXPECT_EQ(utils::path::dot_w, utils::path::get_dot<wchar_t>());
}
TEST(utils_path, get_dot_backslash) {
EXPECT_EQ(utils::path::dot_backslash, utils::path::get_dot_backslash<char>());
EXPECT_EQ(utils::path::dot_backslash_w,
utils::path::get_dot_backslash<wchar_t>());
}
TEST(utils_path, get_dot_slash) {
EXPECT_EQ(utils::path::dot_slash, utils::path::get_dot_slash<char>());
EXPECT_EQ(utils::path::dot_slash_w, utils::path::get_dot_slash<wchar_t>());
}
TEST(utils_path, get_slash) {
EXPECT_EQ(utils::path::slash, utils::path::get_slash<char>());
EXPECT_EQ(utils::path::slash_w, utils::path::get_slash<wchar_t>());
}
TEST(utils_path, get_long_notation) {
EXPECT_EQ(utils::path::long_notation, utils::path::get_long_notation<char>());
EXPECT_EQ(utils::path::long_notation_w,
utils::path::get_long_notation<wchar_t>());
}
TEST(utils_path, combine) {
auto s = utils::path::combine(R"(\test\path)", {});
#if defined(_WIN32)
EXPECT_STREQ(test_path(R"(\test\path)").c_str(), s.c_str());
#else
EXPECT_STREQ("/test/path", s.c_str());
#endif
s = utils::path::combine(R"(\test)", {R"(\path)"});
#if defined(_WIN32)
EXPECT_STREQ(test_path(R"(\test\path)").c_str(), s.c_str());
#else
EXPECT_STREQ("/test/path", s.c_str());
#endif
s = utils::path::combine(R"(\test)", {R"(\path)", R"(\again\)"});
#if defined(_WIN32)
EXPECT_STREQ(test_path(R"(\test\path\again)").c_str(), s.c_str());
#else
EXPECT_STREQ("/test/path/again", s.c_str());
#endif
s = utils::path::combine("/home/test/.dest", {".state"});
#if defined(_WIN32)
EXPECT_STREQ(test_path(R"(\home\test\.dest\.state)").c_str(), s.c_str());
#else
EXPECT_STREQ("/home/test/.dest/.state", s.c_str());
#endif
#if defined(_WIN32)
s = utils::path::combine(R"(R:\test)", {R"(\path)", R"(\again\)"});
EXPECT_STREQ(test_path(R"(r:\test\path\again)").c_str(), s.c_str());
s = utils::path::combine("R:", {R"(\path)", R"(\again\)"});
EXPECT_STREQ(test_path(R"(r:\path\again)").c_str(), s.c_str());
s = utils::path::combine("R:", {});
EXPECT_STREQ(test_path("r:").c_str(), s.c_str());
s = utils::path::combine("R:", {"\\"});
EXPECT_STREQ(test_path("r:").c_str(), s.c_str());
s = utils::path::combine("\\\\moose", {"cow"});
EXPECT_STREQ("\\\\moose\\cow", s.c_str());
#endif
}
TEST(utils_path, format_path) {
std::string path{"./"};
utils::path::format_path(path, utils::path::slash, utils::path::backslash);
EXPECT_STREQ(".", path.c_str());
path = "~/.test";
utils::path::format_path(path, utils::path::slash, utils::path::backslash);
EXPECT_STREQ("~/.test", path.c_str());
path = "\\";
utils::path::format_path(path, utils::path::slash, utils::path::backslash);
EXPECT_STREQ("/", path.c_str());
path = "\\\\";
utils::path::format_path(path, utils::path::slash, utils::path::backslash);
EXPECT_STREQ("/", path.c_str());
path = "\\\\\\";
utils::path::format_path(path, utils::path::slash, utils::path::backslash);
EXPECT_STREQ("/", path.c_str());
path = "\\\\\\\\";
utils::path::format_path(path, utils::path::slash, utils::path::backslash);
EXPECT_STREQ("/", path.c_str());
path = "/";
utils::path::format_path(path, utils::path::slash, utils::path::backslash);
EXPECT_STREQ("/", path.c_str());
path = "//";
utils::path::format_path(path, utils::path::slash, utils::path::backslash);
EXPECT_STREQ("/", path.c_str());
path = "///";
utils::path::format_path(path, utils::path::slash, utils::path::backslash);
EXPECT_STREQ("/", path.c_str());
path = "////";
utils::path::format_path(path, utils::path::slash, utils::path::backslash);
EXPECT_STREQ("/", path.c_str());
}
TEST(utils_path, create_api_path) {
auto s = utils::path::create_api_path("");
EXPECT_STREQ("/", s.c_str());
s = utils::path::create_api_path(R"(\)");
EXPECT_STREQ("/", s.c_str());
s = utils::path::create_api_path("/");
EXPECT_STREQ("/", s.c_str());
s = utils::path::create_api_path(".");
EXPECT_STREQ("/", s.c_str());
s = utils::path::create_api_path("./");
EXPECT_STREQ("/", s.c_str());
s = utils::path::create_api_path(R"(\\)");
EXPECT_STREQ("/", s.c_str());
s = utils::path::create_api_path("//");
EXPECT_STREQ("/", s.c_str());
s = utils::path::create_api_path("/cow///moose/////dog/chicken");
EXPECT_STREQ("/cow/moose/dog/chicken", s.c_str());
s = utils::path::create_api_path("\\cow\\\\\\moose\\\\\\\\dog\\chicken/");
EXPECT_STREQ("/cow/moose/dog/chicken", s.c_str());
s = utils::path::create_api_path("/cow\\\\/moose\\\\/\\dog\\chicken\\");
EXPECT_STREQ("/cow/moose/dog/chicken", s.c_str());
s = utils::path::create_api_path(".state");
EXPECT_STREQ("/.state", s.c_str());
s = utils::path::create_api_path("/.state/.local");
EXPECT_STREQ("/.state/.local", s.c_str());
s = utils::path::create_api_path("./.state/.local");
EXPECT_STREQ("/.state/.local", s.c_str());
}
TEST(utils_path, get_parent_api_path) {
auto s = utils::path::get_parent_api_path("");
EXPECT_STREQ("/", s.c_str());
s = utils::path::get_parent_api_path("/");
EXPECT_STREQ("/", s.c_str());
s = utils::path::get_parent_api_path("/moose");
EXPECT_STREQ("/", s.c_str());
s = utils::path::get_parent_api_path("/moose/cow");
EXPECT_STREQ("/moose", s.c_str());
s = utils::path::get_parent_api_path("/moose/cow/");
EXPECT_STREQ("/moose", s.c_str());
}
TEST(utils_path, finalize) {
auto s = utils::path::finalize("");
EXPECT_STREQ("", s.c_str());
s = utils::path::finalize(R"(\)");
#if defined(_WIN32)
EXPECT_STREQ(test_path(R"(\)").c_str(), s.c_str());
#else
EXPECT_STREQ("/", s.c_str());
#endif
s = utils::path::finalize("/");
#if defined(_WIN32)
EXPECT_STREQ(test_path(R"(\)").c_str(), s.c_str());
#else
EXPECT_STREQ("/", s.c_str());
#endif
s = utils::path::finalize(R"(\\)");
#if defined(_WIN32)
EXPECT_STREQ("\\\\", s.c_str());
#else
EXPECT_STREQ("/", s.c_str());
#endif
s = utils::path::finalize("//");
#if defined(_WIN32)
EXPECT_STREQ("\\\\", s.c_str());
#else
EXPECT_STREQ("/", s.c_str());
#endif
s = utils::path::finalize("/cow///moose/////dog/chicken");
#if defined(_WIN32)
EXPECT_STREQ(test_path(R"(\cow\moose\dog\chicken)").c_str(), s.c_str());
#else
EXPECT_STREQ("/cow/moose/dog/chicken", s.c_str());
#endif
s = utils::path::finalize("\\cow\\\\\\moose\\\\\\\\dog\\chicken/");
#if defined(_WIN32)
EXPECT_STREQ(test_path(R"(\cow\moose\dog\chicken)").c_str(), s.c_str());
#else
EXPECT_STREQ("/cow/moose/dog/chicken", s.c_str());
#endif
s = utils::path::finalize("/cow\\\\/moose\\\\/\\dog\\chicken\\");
#if defined(_WIN32)
EXPECT_STREQ(test_path(R"(\cow\moose\dog\chicken)").c_str(), s.c_str());
#else
EXPECT_STREQ("/cow/moose/dog/chicken", s.c_str());
#endif
#if defined(_WIN32)
s = utils::path::finalize("D:");
EXPECT_STREQ(test_path("d:").c_str(), s.c_str());
s = utils::path::finalize("D:\\");
EXPECT_STREQ(test_path("d:").c_str(), s.c_str());
s = utils::path::finalize("D:\\moose");
EXPECT_STREQ(test_path("d:\\moose").c_str(), s.c_str());
s = utils::path::finalize("D:\\moose\\");
EXPECT_STREQ(test_path("d:\\moose").c_str(), s.c_str());
s = utils::path::finalize("D:/");
EXPECT_STREQ(test_path("d:").c_str(), s.c_str());
s = utils::path::finalize("D:/moose");
EXPECT_STREQ(test_path("d:\\moose").c_str(), s.c_str());
s = utils::path::finalize("D:/moose/");
EXPECT_STREQ(test_path("d:\\moose").c_str(), s.c_str());
s = utils::path::finalize("\\\\moose\\cow");
EXPECT_STREQ("\\\\moose\\cow", s.c_str());
s = utils::path::finalize("//moose/cow");
EXPECT_STREQ("\\\\moose\\cow", s.c_str());
#else // !defined(_WIN32)
s = utils::path::finalize("\\\\moose\\cow");
EXPECT_STREQ("/moose/cow", s.c_str());
s = utils::path::finalize("//moose/cow");
EXPECT_STREQ("/moose/cow", s.c_str());
#endif // defined(_WIN32)
}
TEST(utils_path, absolute) {
auto dir = utils::path::get_current_path<std::string>();
auto path = utils::path::absolute(".");
EXPECT_STREQ(dir.c_str(), path.c_str());
path = utils::path::absolute("./");
EXPECT_STREQ(dir.c_str(), path.c_str());
path = utils::path::absolute(R"(.\)");
EXPECT_STREQ(dir.c_str(), path.c_str());
#if defined(_WIN32)
path = utils::path::absolute(R"(.\moose)");
EXPECT_STREQ((dir + R"(\moose)").c_str(), path.c_str());
path = utils::path::absolute(R"(./moose)");
EXPECT_STREQ((dir + R"(\moose)").c_str(), path.c_str());
path = utils::path::absolute(R"(\\server\share)");
EXPECT_STREQ(R"(\\server\share)", path.c_str());
path = utils::path::absolute(R"(//server/share)");
EXPECT_STREQ(R"(\\server\share)", path.c_str());
auto home_env = utils::get_environment_variable("USERPROFILE");
#else // !defined(_WIN32)
path = utils::path::absolute(R"(.\moose)");
EXPECT_STREQ((dir + R"(/moose)").c_str(), path.c_str());
path = utils::path::absolute(R"(./moose)");
EXPECT_STREQ((dir + R"(/moose)").c_str(), path.c_str());
path = utils::path::absolute(R"(\\server\share)");
EXPECT_STREQ(R"(/server/share)", path.c_str());
#endif // defined(_WIN32)
}
TEST(utils_path, absolute_can_resolve_path_variables) {
#if defined(_WIN32)
auto home =
utils::path::absolute(utils::get_environment_variable("USERPROFILE"));
EXPECT_STREQ(home.c_str(), utils::path::absolute("%USERPROFILE%").c_str());
#else // !defined(_WIN32)
auto home = utils::path::absolute(utils::get_environment_variable("HOME"));
#endif // defined(_WIN32)
auto expanded_str = utils::path::absolute("~\\");
EXPECT_STREQ(home.c_str(), expanded_str.c_str());
expanded_str = utils::path::absolute("~/");
EXPECT_STREQ(home.c_str(), expanded_str.c_str());
expanded_str = utils::path::absolute("~");
EXPECT_STREQ("~", expanded_str.c_str());
}
TEST(utils_path, get_parent_path) {
#if defined(_WIN32)
{
auto dir = R"(c:\test)";
auto parent = utils::path::get_parent_path(dir);
EXPECT_STREQ("c:", parent.c_str());
dir = R"(c:\test\file.txt)";
parent = utils::path::get_parent_path(dir);
EXPECT_STREQ(R"(c:\test)", parent.c_str());
dir = "c:";
parent = utils::path::get_parent_path(dir);
EXPECT_STREQ("c:", parent.c_str());
}
{
auto dir = LR"(c:\test)";
auto parent = utils::path::get_parent_path(dir);
EXPECT_STREQ(L"c:", parent.c_str());
dir = LR"(c:\test\file.txt)";
parent = utils::path::get_parent_path(dir);
EXPECT_STREQ(LR"(c:\test)", parent.c_str());
dir = L"c:";
parent = utils::path::get_parent_path(dir);
EXPECT_STREQ(L"c:", parent.c_str());
}
#else // !defined(_WIN32)
{
auto dir = "/test";
auto parent = utils::path::get_parent_path(dir);
EXPECT_STREQ("/", parent.c_str());
dir = "/test/test";
parent = utils::path::get_parent_path(dir);
EXPECT_STREQ("/test", parent.c_str());
}
{
auto dir = L"/test";
auto parent = utils::path::get_parent_path(dir);
EXPECT_STREQ(L"/", parent.c_str());
dir = L"/test/test";
parent = utils::path::get_parent_path(dir);
EXPECT_STREQ(L"/test", parent.c_str());
}
#endif // defined(_WIN32)
}
TEST(utils_path, contains_trash_directory) {
#if defined(_WIN32)
{
auto dir = R"(c:\$recycle.bin)";
EXPECT_TRUE(utils::path::contains_trash_directory(dir));
dir = R"(c:\$recycle.bin\moose.txt)";
EXPECT_TRUE(utils::path::contains_trash_directory(dir));
}
{
auto dir = LR"(c:\$recycle.bin)";
EXPECT_TRUE(utils::path::contains_trash_directory(dir));
dir = LR"(c:\$recycle.bin\moose.txt)";
EXPECT_TRUE(utils::path::contains_trash_directory(dir));
}
#else // !defined(_WIN32)
{
auto dir = "/$recycle.bin";
EXPECT_TRUE(utils::path::contains_trash_directory(dir));
dir = "/$recycle.bin/moose.txt";
EXPECT_TRUE(utils::path::contains_trash_directory(dir));
}
{
auto dir = L"/$recycle.bin";
EXPECT_TRUE(utils::path::contains_trash_directory(dir));
dir = L"/$recycle.bin/moose.txt";
EXPECT_TRUE(utils::path::contains_trash_directory(dir));
}
#endif // defined(_WIN32)
}
TEST(utils_path, does_not_contain_trash_directory) {
#if defined(_WIN32)
{
auto dir = R"(c:\recycle.bin)";
EXPECT_FALSE(utils::path::contains_trash_directory(dir));
dir = R"(c:\recycle.bin\moose.txt)";
EXPECT_FALSE(utils::path::contains_trash_directory(dir));
}
{
auto dir = LR"(c:\recycle.bin)";
EXPECT_FALSE(utils::path::contains_trash_directory(dir));
dir = LR"(c:\recycle.bin\moose.txt)";
EXPECT_FALSE(utils::path::contains_trash_directory(dir));
}
#else // !defined(_WIN32)
{
auto dir = "/recycle.bin";
EXPECT_FALSE(utils::path::contains_trash_directory(dir));
dir = "/recycle.bin/moose.txt)";
EXPECT_FALSE(utils::path::contains_trash_directory(dir));
}
{
auto dir = L"/recycle.bin";
EXPECT_FALSE(utils::path::contains_trash_directory(dir));
dir = L"/recycle.bin/moose.txt)";
EXPECT_FALSE(utils::path::contains_trash_directory(dir));
}
#endif // defined(_WIN32)
}
} // namespace repertory

View File

@ -0,0 +1,137 @@
/*
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.
*/
#include "test.hpp"
namespace repertory {
TEST(utils_string, begins_with) {
std::string str{"moose"};
EXPECT_TRUE(utils::string::begins_with(str, "m"));
EXPECT_TRUE(utils::string::begins_with(str, "mo"));
EXPECT_TRUE(utils::string::begins_with(str, "moo"));
EXPECT_TRUE(utils::string::begins_with(str, "moos"));
EXPECT_TRUE(utils::string::begins_with(str, "moose"));
EXPECT_FALSE(utils::string::begins_with(str, "a"));
EXPECT_FALSE(utils::string::begins_with(str, "ma"));
EXPECT_FALSE(utils::string::begins_with(str, "moose1"));
std::wstring str_w{L"moose"};
EXPECT_TRUE(utils::string::begins_with(str_w, L"m"));
EXPECT_TRUE(utils::string::begins_with(str_w, L"mo"));
EXPECT_TRUE(utils::string::begins_with(str_w, L"moo"));
EXPECT_TRUE(utils::string::begins_with(str_w, L"moos"));
EXPECT_TRUE(utils::string::begins_with(str_w, L"moose"));
EXPECT_FALSE(utils::string::begins_with(str_w, L"a"));
EXPECT_FALSE(utils::string::begins_with(str_w, L"ma"));
EXPECT_FALSE(utils::string::begins_with(str_w, L"moose1"));
}
TEST(utils_string, contains) {
std::string str{R"(\\)"};
EXPECT_TRUE(utils::string::contains(str, "\\"));
EXPECT_FALSE(utils::string::contains(str, "/"));
std::wstring str_w{LR"(\\)"};
EXPECT_TRUE(utils::string::contains(str_w, L"\\"));
EXPECT_FALSE(utils::string::contains(str_w, L"/"));
}
TEST(utils_string, replace) {
std::string str{"moose"};
utils::string::replace(str, 'o', '0');
EXPECT_STREQ("m00se", str.c_str());
std::wstring str_w{L"moose"};
utils::string::replace(str_w, 'o', '0');
EXPECT_STREQ(L"m00se", str_w.c_str());
std::string str2{"\\\\\\"};
utils::string::replace(str2, '\\', '/');
EXPECT_STREQ("///", str2.c_str());
std::wstring str_w2{L"\\\\\\"};
utils::string::replace(str_w2, '\\', '/');
EXPECT_STREQ(L"///", str_w2.c_str());
std::string str3{"///"};
utils::string::replace(str3, '/', '\\');
EXPECT_STREQ("\\\\\\", str3.c_str());
std::wstring str_w3{L"///"};
utils::string::replace(str_w3, '/', '\\');
EXPECT_STREQ(L"\\\\\\", str_w3.c_str());
str.clear();
utils::string::replace(str, '/', '\\');
EXPECT_STREQ("", str.c_str());
str_w.clear();
utils::string::replace(str_w, '/', '\\');
EXPECT_STREQ(L"", str_w.c_str());
}
TEST(utils_string, replace_string) {
std::string str{"moose"};
utils::string::replace(str, "o", "0");
EXPECT_STREQ("m00se", str.c_str());
std::wstring str_w{L"moose"};
utils::string::replace(str_w, L"o", L"0");
EXPECT_STREQ(L"m00se", str_w.c_str());
}
TEST(utils_string, is_numeric) {
EXPECT_TRUE(utils::string::is_numeric("100"));
EXPECT_TRUE(utils::string::is_numeric("+100"));
EXPECT_TRUE(utils::string::is_numeric("-100"));
EXPECT_TRUE(utils::string::is_numeric("100.00"));
EXPECT_TRUE(utils::string::is_numeric("+100.00"));
EXPECT_TRUE(utils::string::is_numeric("-100.00"));
EXPECT_FALSE(utils::string::is_numeric("1.00.00"));
EXPECT_FALSE(utils::string::is_numeric("+1.00.00"));
EXPECT_FALSE(utils::string::is_numeric("-1.00.00"));
EXPECT_FALSE(utils::string::is_numeric("a1"));
EXPECT_FALSE(utils::string::is_numeric("1a"));
EXPECT_FALSE(utils::string::is_numeric("+"));
EXPECT_FALSE(utils::string::is_numeric("-"));
EXPECT_FALSE(utils::string::is_numeric(""));
}
TEST(utils_string, to_bool) {
EXPECT_TRUE(utils::string::to_bool("1"));
EXPECT_TRUE(utils::string::to_bool("-1"));
EXPECT_TRUE(utils::string::to_bool("0.1"));
EXPECT_TRUE(utils::string::to_bool("-0.1"));
EXPECT_TRUE(utils::string::to_bool("00000.1000000"));
EXPECT_TRUE(utils::string::to_bool("true"));
EXPECT_FALSE(utils::string::to_bool("false"));
EXPECT_FALSE(utils::string::to_bool("0"));
EXPECT_FALSE(utils::string::to_bool("00000.00000"));
}
} // namespace repertory