new_build_system (#18)
All checks were successful
BlockStorage/repertory/pipeline/head This commit looks good

Reviewed-on: #18
This commit is contained in:
2024-09-06 15:05:48 +00:00
parent 9d3e4b8767
commit a7239558bd
191 changed files with 10683 additions and 10598 deletions

View File

@ -0,0 +1,61 @@
/*
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_input_dir() -> std::string;
[[nodiscard]] auto get_test_output_dir() -> std::string;
} // namespace repertory::test
#endif // REPERTORY_TEST_INCLUDE_TEST_HPP_

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

@ -0,0 +1,142 @@
/*
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 std::recursive_mutex file_mtx{};
static std::vector<std::unique_ptr<repertory::utils::file::i_fs_item>>
generated_files{};
static void delete_generated_files() {
repertory::recur_mutex_lock lock{file_mtx};
std::optional<std::string> parent_path;
for (auto &&path : generated_files) {
if (parent_path->empty()) {
parent_path = repertory::utils::path::get_parent_path(path->get_path());
}
[[maybe_unused]] auto removed = path->remove();
}
generated_files.clear();
if (parent_path.has_value()) {
EXPECT_TRUE(
repertory::utils::file::directory(*parent_path).remove_recursively());
}
}
struct file_deleter final {
~file_deleter() { delete_generated_files(); }
};
static auto deleter{std::make_unique<file_deleter>()};
} // 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_input_dir() -> std::string {
static auto test_path = ([]() -> std::string {
auto dir = utils::get_environment_variable("PROJECT_TEST_DIR");
return utils::path::combine(dir.empty() ? "." : dir, {"test_config"});
})();
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

@ -19,12 +19,7 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "gtest/gtest.h"
#include "utils/common.hpp"
#include "utils/collection.hpp"
#include "utils/string.hpp"
#include "test.hpp"
namespace repertory {
TEST(utils_common, calculate_read_size) {
@ -129,40 +124,40 @@ TEST(utils_common, create_uuid_string) {
#endif // defined(PROJECT_ENABLE_STDUUID)
#if defined(PROJECT_ENABLE_LIBSODIUM)
TEST(utils_common, generate_random) {
TEST(utils_common, generate_secure_random) {
{
auto r1 = utils::generate_random<std::size_t>();
auto r2 = utils::generate_random<std::size_t>();
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_random<std::vector<std::uint8_t>>(6U);
auto r2 = utils::generate_random<std::vector<std::uint8_t>>(6U);
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_random<std::array<std::uint8_t, 4U>>();
auto r2 = utils::generate_random<std::array<std::uint8_t, 4U>>();
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_random<std::string>(6U);
auto r2 = utils::generate_random<std::string>(6U);
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_random<std::wstring>(6U);
auto r2 = utils::generate_random<std::wstring>(6U);
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()));
@ -243,7 +238,6 @@ TEST(utils_common, generate_random_between_throws_error_on_invalid_range) {
std::range_error);
}
#if defined(PROJECT_ENABLE_LIBSODIUM)
TEST(utils_common, generate_random_string) {
static constexpr const auto max_iterations{10000L};
@ -271,14 +265,13 @@ TEST(utils_common, generate_random_string_for_zero_length) {
EXPECT_TRUE(utils::generate_random_string(0U).empty());
EXPECT_TRUE(utils::generate_random_wstring(0U).empty());
}
#endif // defined(PROJECT_ENABLE_LIBSODIUM)
TEST(utils_common, get_environment_variable) {
static constexpr const std::string path_env{"PATH"};
std::string path;
#if defined(_WIN32)
path.resize(MAX_PATH + 1U);
path.resize(repertory::max_path_length + 1U);
auto size = ::GetEnvironmentVariableA(path_env.c_str(), path.data(), 0U);
path.resize(size);
@ -298,7 +291,7 @@ TEST(utils_common, get_environment_variable) {
#if defined(PROJECT_ENABLE_BOOST)
TEST(utils_common, get_next_available_port) {
std::uint16_t available_port{};
for (std::uint16_t port = 1U; port < 65535; ++port) {
for (std::uint16_t port = 1025U; port < 1030U; ++port) {
EXPECT_TRUE(utils::get_next_available_port(port, available_port));
EXPECT_GE(available_port, port);
}

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

@ -19,22 +19,19 @@
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)
#include "gtest/gtest.h"
#include "utils/collection.hpp"
#include "utils/encryption.hpp"
namespace repertory {
static const std::string token{"moose"};
static const std::wstring token_w{L"moose"};
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(
"182072537ada59e4d6b18034a80302ebae935f66adbdf0f271d3d36309c2d481",
"ab4a0b004e824962913f7c0f79582b6ec7a3b8726426ca61d1a0a28ce5049e96",
utils::collection::to_hex_string(key1).c_str());
auto key2 =
@ -50,9 +47,15 @@ TEST(utils_encryption, generate_key) {
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"590ac70125bec4501172937f6a2cbdeb22a87b5e40d5595eccd06b2b20548d8f",
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");
@ -68,12 +71,12 @@ TEST(utils_encryption, generate_key) {
EXPECT_NE(key4_w, key4);
}
TEST(utils_encryption, generate_key_default_hasher_is_sha256) {
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_sha256(
return utils::encryption::create_hash_blake2b_256(
std::string_view(reinterpret_cast<const char *>(data), size));
});
EXPECT_EQ(key1, key2);
@ -82,7 +85,7 @@ TEST(utils_encryption, generate_key_default_hasher_is_sha256) {
utils::encryption::generate_key<utils::encryption::hash_256_t>(token_w);
auto key2_w = utils::encryption::generate_key<utils::encryption::hash_256_t>(
token_w, [](auto &&data, auto &&size) -> auto {
return utils::encryption::create_hash_sha256(std::wstring_view(
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);
@ -93,44 +96,44 @@ TEST(utils_encryption, generate_key_default_hasher_is_sha256) {
TEST(utils_encryption, generate_key_with_hasher) {
auto key1 = utils::encryption::generate_key<utils::encryption::hash_256_t>(
token, [](auto &&data, auto &&size) -> auto {
return utils::encryption::create_hash_sha256(
std::string_view(reinterpret_cast<const char *>(data), size));
});
token, utils::encryption::blake2b_256_hasher);
EXPECT_STREQ(
"182072537ada59e4d6b18034a80302ebae935f66adbdf0f271d3d36309c2d481",
"ab4a0b004e824962913f7c0f79582b6ec7a3b8726426ca61d1a0a28ce5049e96",
utils::collection::to_hex_string(key1).c_str());
auto key2 = utils::encryption::generate_key<utils::encryption::hash_256_t>(
token, [](auto &&data, auto &&size) -> auto {
return utils::encryption::create_hash_blake2b_256(
std::string_view(reinterpret_cast<const char *>(data), size));
});
token, utils::encryption::sha256_hasher);
EXPECT_NE(key1, key2);
EXPECT_STREQ(
"ab4a0b004e824962913f7c0f79582b6ec7a3b8726426ca61d1a0a28ce5049e96",
"182072537ada59e4d6b18034a80302ebae935f66adbdf0f271d3d36309c2d481",
utils::collection::to_hex_string(key2).c_str());
auto key1_w = utils::encryption::generate_key<utils::encryption::hash_256_t>(
token_w, [](auto &&data, auto &&size) -> auto {
return utils::encryption::create_hash_sha256(std::wstring_view(
reinterpret_cast<const wchar_t *>(data), size / sizeof(wchar_t)));
});
token_w, utils::encryption::blake2b_256_hasher);
#if defined(_WIN32)
EXPECT_STREQ(
L"590ac70125bec4501172937f6a2cbdeb22a87b5e40d5595eccd06b2b20548d8f",
L"4f5eb2a2ab34e3777b230465283923080b9ba59311e74058ccd74185131d11fe",
utils::collection::to_hex_wstring(key1_w).c_str());
auto key2_w = utils::encryption::generate_key<utils::encryption::hash_256_t>(
token_w, [](auto &&data, auto &&size) -> auto {
return utils::encryption::create_hash_blake2b_256(std::wstring_view(
reinterpret_cast<const wchar_t *>(data), size / sizeof(wchar_t)));
});
EXPECT_NE(key1_w, key2_w);
#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);
@ -241,6 +244,39 @@ TEST(utils_encryption, decryption_failure) {
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

View File

@ -0,0 +1,66 @@
/*
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 : public utils::error::i_exception_handler {
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_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,517 @@
/*
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_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());
}
}
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));
}
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));
path1 = "// server/cow";
path2 = "// server/cow";
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

@ -19,11 +19,31 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "gtest/gtest.h"
#include "test.hpp"
#include "utils/common.hpp"
#include "utils/path.hpp"
#include "utils/string.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) {
@ -31,10 +51,17 @@ TEST(utils_path, constants) {
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) {
@ -86,6 +113,12 @@ TEST(utils_path, get_dot) {
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>());
@ -96,40 +129,56 @@ TEST(utils_path, get_slash) {
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(R"(\test\path)", s.c_str());
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(R"(\test\path)", s.c_str());
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(R"(\test\path\again)", s.c_str());
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(R"(r:\test\path\again)", s.c_str());
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(R"(r:\path\again)", s.c_str());
EXPECT_STREQ(test_path(R"(r:\path\again)").c_str(), s.c_str());
s = utils::path::combine("R:", {});
EXPECT_STREQ("r:", s.c_str());
EXPECT_STREQ(test_path("r:").c_str(), s.c_str());
s = utils::path::combine("R:", {"\\"});
EXPECT_STREQ("r:", s.c_str());
EXPECT_STREQ(test_path("r:").c_str(), s.c_str());
s = utils::path::combine("\\\\moose", {"cow"});
EXPECT_STREQ("\\\\moose\\cow", s.c_str());
#endif
}
@ -204,6 +253,15 @@ TEST(utils_path, create_api_path) {
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, finalize) {
@ -212,104 +270,137 @@ TEST(utils_path, finalize) {
s = utils::path::finalize(R"(\)");
#if defined(_WIN32)
EXPECT_STREQ(R"(\)", s.c_str());
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(R"(\)", s.c_str());
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(R"(\)", s.c_str());
EXPECT_STREQ("\\\\", s.c_str());
#else
EXPECT_STREQ("/", s.c_str());
#endif
s = utils::path::finalize("//");
#if defined(_WIN32)
EXPECT_STREQ(R"(\)", s.c_str());
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(R"(\cow\moose\dog\chicken)", s.c_str());
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(R"(\cow\moose\dog\chicken)", s.c_str());
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(R"(\cow\moose\dog\chicken)", s.c_str());
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("d:", s.c_str());
EXPECT_STREQ(test_path("d:").c_str(), s.c_str());
s = utils::path::finalize("D:\\");
EXPECT_STREQ("d:", s.c_str());
EXPECT_STREQ(test_path("d:").c_str(), s.c_str());
s = utils::path::finalize("D:\\moose");
EXPECT_STREQ("d:\\moose", s.c_str());
EXPECT_STREQ(test_path("d:\\moose").c_str(), s.c_str());
s = utils::path::finalize("D:\\moose\\");
EXPECT_STREQ("d:\\moose", s.c_str());
EXPECT_STREQ(test_path("d:\\moose").c_str(), s.c_str());
s = utils::path::finalize("D:/");
EXPECT_STREQ("d:", s.c_str());
EXPECT_STREQ(test_path("d:").c_str(), s.c_str());
s = utils::path::finalize("D:/moose");
EXPECT_STREQ("d:\\moose", s.c_str());
EXPECT_STREQ(test_path("d:\\moose").c_str(), s.c_str());
s = utils::path::finalize("D:/moose/");
EXPECT_STREQ("d:\\moose", s.c_str());
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::absolute(std::filesystem::current_path().string());
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());
auto home_env = utils::get_environment_variable("HOME");
#endif // defined(_WIN32)
auto home = utils::path::absolute(home_env);
path = utils::path::absolute("~");
EXPECT_STREQ(home.c_str(), path.c_str());
// path = utils::path::absolute("~/.local");
}
TEST(utils_path, absolute_can_resolve_path_variables) {
std::string home{};
#if defined(_WIN32)
home.resize(MAX_PATH + 1U);
home.resize(repertory::max_path_length + 1U);
auto size = ::GetEnvironmentVariableA("USERPROFILE", home.data(), 0U);
home.resize(size);
@ -331,4 +422,128 @@ TEST(utils_path, absolute_can_resolve_path_variables) {
EXPECT_STREQ(home.c_str(), expanded_str.c_str());
#endif // defined(_WIN32)
}
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

@ -19,9 +19,7 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "gtest/gtest.h"
#include "utils/string.hpp"
#include "test.hpp"
namespace repertory {
TEST(utils_string, begins_with) {