266 lines
9.2 KiB
C++
266 lines
9.2 KiB
C++
/*
|
|
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 data = nlohmann::json({{"moose", "cow"}});
|
|
EXPECT_TRUE(utils::file::write_json_file(path, data));
|
|
|
|
nlohmann::json data2{};
|
|
EXPECT_TRUE(utils::file::read_json_file(path, data2));
|
|
EXPECT_STREQ(data.dump().c_str(), data2.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 data = nlohmann::json({{"moose", "cow"}});
|
|
EXPECT_TRUE(utils::file::write_json_file(path, data, "moose"));
|
|
|
|
nlohmann::json data2{};
|
|
EXPECT_TRUE(utils::file::read_json_file(path, data2, "moose"));
|
|
EXPECT_STREQ(data.dump().c_str(), data2.dump().c_str());
|
|
|
|
{
|
|
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(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)
|
|
} // namespace repertory
|