This commit is contained in:
2023-11-30 09:18:10 -06:00
parent 8436f2e2bb
commit 97487cf0c4
380 changed files with 144184 additions and 144135 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -1,77 +1,77 @@
/*
Copyright <2018-2023> <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_common.hpp"
#include "comm/curl/curl_comm.hpp"
#include "types/repertory.hpp"
namespace repertory {
TEST(curl_comm, can_create_s3_host_config) {
s3_config config{};
config.bucket = "repertory";
config.url = "https://s3.test.com";
config.region = "any";
auto hc = curl_comm::create_host_config(config, false);
EXPECT_STREQ("https", hc.protocol.c_str());
EXPECT_STREQ("repertory.s3.test.com", hc.host_name_or_ip.c_str());
EXPECT_TRUE(hc.path.empty());
}
TEST(curl_comm, can_create_s3_host_config_with_path_style) {
s3_config config{};
config.bucket = "repertory";
config.url = "https://s3.test.com";
config.region = "any";
auto hc = curl_comm::create_host_config(config, true);
EXPECT_STREQ("https", hc.protocol.c_str());
EXPECT_STREQ("s3.test.com", hc.host_name_or_ip.c_str());
EXPECT_STREQ("/repertory", hc.path.c_str());
}
TEST(curl_comm, can_create_s3_host_config_with_region) {
s3_config config{};
config.bucket = "repertory";
config.url = "https://s3.test.com";
config.region = "any";
config.use_region_in_url = true;
auto hc = curl_comm::create_host_config(config, false);
EXPECT_STREQ("https", hc.protocol.c_str());
EXPECT_STREQ("repertory.s3.any.test.com", hc.host_name_or_ip.c_str());
EXPECT_TRUE(hc.path.empty());
}
TEST(curl_comm, can_create_s3_host_config_with_region_and_path_style) {
s3_config config{};
config.bucket = "repertory";
config.url = "https://s3.test.com";
config.region = "any";
config.use_region_in_url = true;
auto hc = curl_comm::create_host_config(config, true);
EXPECT_STREQ("https", hc.protocol.c_str());
EXPECT_STREQ("s3.any.test.com", hc.host_name_or_ip.c_str());
EXPECT_STREQ("/repertory", hc.path.c_str());
}
} // namespace repertory
/*
Copyright <2018-2023> <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_common.hpp"
#include "comm/curl/curl_comm.hpp"
#include "types/repertory.hpp"
namespace repertory {
TEST(curl_comm, can_create_s3_host_config) {
s3_config config{};
config.bucket = "repertory";
config.url = "https://s3.test.com";
config.region = "any";
auto hc = curl_comm::create_host_config(config, false);
EXPECT_STREQ("https", hc.protocol.c_str());
EXPECT_STREQ("repertory.s3.test.com", hc.host_name_or_ip.c_str());
EXPECT_TRUE(hc.path.empty());
}
TEST(curl_comm, can_create_s3_host_config_with_path_style) {
s3_config config{};
config.bucket = "repertory";
config.url = "https://s3.test.com";
config.region = "any";
auto hc = curl_comm::create_host_config(config, true);
EXPECT_STREQ("https", hc.protocol.c_str());
EXPECT_STREQ("s3.test.com", hc.host_name_or_ip.c_str());
EXPECT_STREQ("/repertory", hc.path.c_str());
}
TEST(curl_comm, can_create_s3_host_config_with_region) {
s3_config config{};
config.bucket = "repertory";
config.url = "https://s3.test.com";
config.region = "any";
config.use_region_in_url = true;
auto hc = curl_comm::create_host_config(config, false);
EXPECT_STREQ("https", hc.protocol.c_str());
EXPECT_STREQ("repertory.s3.any.test.com", hc.host_name_or_ip.c_str());
EXPECT_TRUE(hc.path.empty());
}
TEST(curl_comm, can_create_s3_host_config_with_region_and_path_style) {
s3_config config{};
config.bucket = "repertory";
config.url = "https://s3.test.com";
config.region = "any";
config.use_region_in_url = true;
auto hc = curl_comm::create_host_config(config, true);
EXPECT_STREQ("https", hc.protocol.c_str());
EXPECT_STREQ("s3.any.test.com", hc.host_name_or_ip.c_str());
EXPECT_STREQ("/repertory", hc.path.c_str());
}
} // namespace repertory

View File

@@ -1 +1 @@
test
test

View File

@@ -1 +1 @@
moose
moose

View File

@@ -1,170 +1,170 @@
/*
Copyright <2018-2023> <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_common.hpp"
#include "app_config.hpp"
#include "events/consumers/console_consumer.hpp"
#include "providers/encrypt/encrypt_provider.hpp"
#include "utils/path_utils.hpp"
namespace repertory {
/* TEST(encrypt_provider, can_construct_encrypt_provider) {
ASSERT_TRUE(
utils::file::delete_directory_recursively("./encrypt_provider_test"));
{
console_consumer cc{};
event_system::instance().start();
app_config cfg(provider_type::encrypt, "./encrypt_provider_test");
encrypt_provider provider(cfg);
event_system::instance().stop();
}
ASSERT_TRUE(
utils::file::delete_directory_recursively("./encrypt_provider_test"));
}
TEST(encrypt_provider, can_get_file_list) {
ASSERT_TRUE(
utils::file::delete_directory_recursively("./encrypt_provider_test"));
{
console_consumer cc{};
event_system::instance().start();
const auto path = std::filesystem::path(utils::path::absolute(__FILE__))
.parent_path()
.string();
app_config cfg(provider_type::encrypt, "./encrypt_provider_test");
EXPECT_STREQ(
path.c_str(),
cfg.set_value_by_name("EncryptConfig.Path", path.c_str()).c_str());
EXPECT_STREQ(
"test_token",
cfg.set_value_by_name("EncryptConfig.EncryptionToken", "test_token")
.c_str());
encrypt_provider provider(cfg);
api_file_list list{};
EXPECT_EQ(api_error::success, provider.get_file_list(list));
api_file_list list2{};
EXPECT_EQ(api_error::success, provider.get_file_list(list2));
EXPECT_EQ(list.size(), list2.size());
for (std::size_t idx = 0U; idx < list.size(); idx++) {
EXPECT_STREQ(list.at(idx).api_path.c_str(),
list2.at(idx).api_path.c_str());
EXPECT_STREQ(list.at(idx).api_parent.c_str(),
list2.at(idx).api_parent.c_str());
EXPECT_EQ(list.at(idx).accessed_date, list2.at(idx).accessed_date);
EXPECT_EQ(list.at(idx).changed_date, list2.at(idx).changed_date);
EXPECT_EQ(list.at(idx).file_size, list2.at(idx).file_size);
EXPECT_TRUE(list.at(idx).key.empty());
EXPECT_TRUE(list2.at(idx).key.empty());
EXPECT_EQ(list.at(idx).modified_date, list2.at(idx).modified_date);
EXPECT_STREQ(list.at(idx).source_path.c_str(),
list2.at(idx).source_path.c_str());
}
event_system::instance().stop();
}
ASSERT_TRUE(
utils::file::delete_directory_recursively("./encrypt_provider_test"));
}
TEST(encrypt_provider, can_encrypt_and_decrypt_file) {
ASSERT_TRUE(
utils::file::delete_directory_recursively("./encrypt_provider_test"));
{
console_consumer cc{};
event_system::instance().start();
const auto path = std::filesystem::path(utils::path::absolute(__FILE__))
.parent_path()
.string();
app_config cfg(provider_type::encrypt, "./encrypt_provider_test");
EXPECT_STREQ(
path.c_str(),
cfg.set_value_by_name("EncryptConfig.Path", path.c_str()).c_str());
EXPECT_STREQ(
"test_token",
cfg.set_value_by_name("EncryptConfig.EncryptionToken", "test_token")
.c_str());
encrypt_provider provider(cfg);
api_file_list list{};
EXPECT_EQ(api_error::success, provider.get_file_list(list));
for (const auto &file : list) {
std::string file_path{file.api_path};
EXPECT_EQ(api_error::success,
utils::encryption::decrypt_file_path(
cfg.get_encrypt_config().encryption_token, file_path));
file_path =
utils::path::combine(cfg.get_encrypt_config().path, {file_path});
EXPECT_TRUE(std::filesystem::equivalent(file.source_path, file_path));
std::vector<char> encrypted_data{};
stop_type stop_requested = false;
EXPECT_EQ(api_error::success,
provider.read_file_bytes(file.api_path, file.file_size, 0U,
encrypted_data, stop_requested));
EXPECT_EQ(file.file_size, encrypted_data.size());
std::vector<char> decrypted_data{};
EXPECT_TRUE(utils::encryption::decrypt_data(
cfg.get_encrypt_config().encryption_token, encrypted_data,
decrypted_data));
native_file::native_file_ptr nf{};
EXPECT_EQ(api_error::success, native_file::open(file.source_path, nf));
std::uint64_t file_size{};
EXPECT_TRUE(nf->get_file_size(file_size));
EXPECT_EQ(file_size, decrypted_data.size());
std::size_t bytes_read{};
std::vector<char> file_data{};
file_data.resize(file_size);
EXPECT_TRUE(nf->read_bytes(file_data.data(), file_size, 0U, bytes_read));
nf->close();
EXPECT_EQ(file_size, bytes_read);
for (std::uint64_t i = 0U; i < bytes_read; i++) {
ASSERT_EQ(file_data[i], decrypted_data[i]);
}
}
event_system::instance().stop();
}
ASSERT_TRUE(
utils::file::delete_directory_recursively("./encrypt_provider_test"));
} */
} // namespace repertory
/*
Copyright <2018-2023> <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_common.hpp"
#include "app_config.hpp"
#include "events/consumers/console_consumer.hpp"
#include "providers/encrypt/encrypt_provider.hpp"
#include "utils/path_utils.hpp"
namespace repertory {
/* TEST(encrypt_provider, can_construct_encrypt_provider) {
ASSERT_TRUE(
utils::file::delete_directory_recursively("./encrypt_provider_test"));
{
console_consumer cc{};
event_system::instance().start();
app_config cfg(provider_type::encrypt, "./encrypt_provider_test");
encrypt_provider provider(cfg);
event_system::instance().stop();
}
ASSERT_TRUE(
utils::file::delete_directory_recursively("./encrypt_provider_test"));
}
TEST(encrypt_provider, can_get_file_list) {
ASSERT_TRUE(
utils::file::delete_directory_recursively("./encrypt_provider_test"));
{
console_consumer cc{};
event_system::instance().start();
const auto path = std::filesystem::path(utils::path::absolute(__FILE__))
.parent_path()
.string();
app_config cfg(provider_type::encrypt, "./encrypt_provider_test");
EXPECT_STREQ(
path.c_str(),
cfg.set_value_by_name("EncryptConfig.Path", path.c_str()).c_str());
EXPECT_STREQ(
"test_token",
cfg.set_value_by_name("EncryptConfig.EncryptionToken", "test_token")
.c_str());
encrypt_provider provider(cfg);
api_file_list list{};
EXPECT_EQ(api_error::success, provider.get_file_list(list));
api_file_list list2{};
EXPECT_EQ(api_error::success, provider.get_file_list(list2));
EXPECT_EQ(list.size(), list2.size());
for (std::size_t idx = 0U; idx < list.size(); idx++) {
EXPECT_STREQ(list.at(idx).api_path.c_str(),
list2.at(idx).api_path.c_str());
EXPECT_STREQ(list.at(idx).api_parent.c_str(),
list2.at(idx).api_parent.c_str());
EXPECT_EQ(list.at(idx).accessed_date, list2.at(idx).accessed_date);
EXPECT_EQ(list.at(idx).changed_date, list2.at(idx).changed_date);
EXPECT_EQ(list.at(idx).file_size, list2.at(idx).file_size);
EXPECT_TRUE(list.at(idx).key.empty());
EXPECT_TRUE(list2.at(idx).key.empty());
EXPECT_EQ(list.at(idx).modified_date, list2.at(idx).modified_date);
EXPECT_STREQ(list.at(idx).source_path.c_str(),
list2.at(idx).source_path.c_str());
}
event_system::instance().stop();
}
ASSERT_TRUE(
utils::file::delete_directory_recursively("./encrypt_provider_test"));
}
TEST(encrypt_provider, can_encrypt_and_decrypt_file) {
ASSERT_TRUE(
utils::file::delete_directory_recursively("./encrypt_provider_test"));
{
console_consumer cc{};
event_system::instance().start();
const auto path = std::filesystem::path(utils::path::absolute(__FILE__))
.parent_path()
.string();
app_config cfg(provider_type::encrypt, "./encrypt_provider_test");
EXPECT_STREQ(
path.c_str(),
cfg.set_value_by_name("EncryptConfig.Path", path.c_str()).c_str());
EXPECT_STREQ(
"test_token",
cfg.set_value_by_name("EncryptConfig.EncryptionToken", "test_token")
.c_str());
encrypt_provider provider(cfg);
api_file_list list{};
EXPECT_EQ(api_error::success, provider.get_file_list(list));
for (const auto &file : list) {
std::string file_path{file.api_path};
EXPECT_EQ(api_error::success,
utils::encryption::decrypt_file_path(
cfg.get_encrypt_config().encryption_token, file_path));
file_path =
utils::path::combine(cfg.get_encrypt_config().path, {file_path});
EXPECT_TRUE(std::filesystem::equivalent(file.source_path, file_path));
std::vector<char> encrypted_data{};
stop_type stop_requested = false;
EXPECT_EQ(api_error::success,
provider.read_file_bytes(file.api_path, file.file_size, 0U,
encrypted_data, stop_requested));
EXPECT_EQ(file.file_size, encrypted_data.size());
std::vector<char> decrypted_data{};
EXPECT_TRUE(utils::encryption::decrypt_data(
cfg.get_encrypt_config().encryption_token, encrypted_data,
decrypted_data));
native_file::native_file_ptr nf{};
EXPECT_EQ(api_error::success, native_file::open(file.source_path, nf));
std::uint64_t file_size{};
EXPECT_TRUE(nf->get_file_size(file_size));
EXPECT_EQ(file_size, decrypted_data.size());
std::size_t bytes_read{};
std::vector<char> file_data{};
file_data.resize(file_size);
EXPECT_TRUE(nf->read_bytes(file_data.data(), file_size, 0U, bytes_read));
nf->close();
EXPECT_EQ(file_size, bytes_read);
for (std::uint64_t i = 0U; i < bytes_read; i++) {
ASSERT_EQ(file_data[i], decrypted_data[i]);
}
}
event_system::instance().stop();
}
ASSERT_TRUE(
utils::file::delete_directory_recursively("./encrypt_provider_test"));
} */
} // namespace repertory

View File

@@ -1,272 +1,272 @@
/*
Copyright <2018-2023> <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_common.hpp"
#include "types/repertory.hpp"
#include "utils/encrypting_reader.hpp"
#include "utils/file_utils.hpp"
#include "utils/path_utils.hpp"
namespace repertory {
static std::string get_source_file_name() {
return generate_test_file_name("./", "encrypting_reader");
}
TEST(encrypting_reader, get_encrypted_file_name) {
const auto source_file_name = get_source_file_name();
ASSERT_TRUE(utils::file::retry_delete_file(source_file_name));
const auto token = std::string("moose");
auto source_file = create_random_file(source_file_name, 1024ul);
EXPECT_TRUE(source_file != nullptr);
if (source_file) {
stop_type stop_requested = false;
utils::encryption::encrypting_reader reader(
"test.dat", source_file_name, stop_requested, token, std::nullopt);
auto file_name = reader.get_encrypted_file_name();
std::cout << file_name << std::endl;
EXPECT_EQ(api_error::success,
utils::encryption::decrypt_file_name(token, file_name));
std::cout << file_name << std::endl;
EXPECT_STREQ("test.dat", file_name.c_str());
source_file->close();
}
EXPECT_TRUE(utils::file::retry_delete_file(source_file_name));
}
TEST(encrypting_reader, file_data) {
const auto source_file_name = get_source_file_name();
EXPECT_TRUE(utils::file::retry_delete_file(source_file_name));
const auto token = std::string("moose");
auto source_file = create_random_file(
source_file_name,
8u * utils::encryption::encrypting_reader::get_data_chunk_size());
EXPECT_TRUE(source_file != nullptr);
if (source_file) {
stop_type stop_requested = false;
utils::encryption::encrypting_reader reader("test.dat", source_file_name,
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++) {
EXPECT_EQ(buffer.size() / 2u,
utils::encryption::encrypting_reader::reader_function(
&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_bytes(
&file_data[0u], file_data.size(),
utils::encryption::encrypting_reader::get_data_chunk_size() * i,
bytes_read));
EXPECT_EQ(0, std::memcmp(&file_data[0u], &decrypted_data[0u],
file_data.size()));
}
source_file->close();
}
EXPECT_TRUE(utils::file::retry_delete_file(source_file_name));
}
TEST(encrypting_reader, file_data_in_multiple_chunks) {
const auto source_file_name = get_source_file_name();
ASSERT_TRUE(utils::file::retry_delete_file(source_file_name));
const auto token = std::string("moose");
auto source_file = create_random_file(
source_file_name,
8u * utils::encryption::encrypting_reader::get_data_chunk_size());
EXPECT_TRUE(source_file != nullptr);
if (source_file) {
stop_type stop_requested = false;
utils::encryption::encrypting_reader reader("test.dat", source_file_name,
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(
&buffer[0u], 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(buffer.begin() + offset,
buffer.begin() + 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_bytes(
&file_data[0u], file_data.size(),
(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[0u], &decrypted_data[0u],
file_data.size()));
}
}
source_file->close();
}
EXPECT_TRUE(utils::file::retry_delete_file(source_file_name));
}
TEST(encrypting_reader, file_data_as_stream) {
const auto source_file_name = get_source_file_name();
ASSERT_TRUE(utils::file::retry_delete_file(source_file_name));
const auto token = std::string("moose");
auto source_file = create_random_file(
source_file_name,
8u * utils::encryption::encrypting_reader::get_data_chunk_size());
EXPECT_TRUE(source_file != nullptr);
if (source_file) {
stop_type stop_requested = false;
utils::encryption::encrypting_reader reader("test.dat", source_file_name,
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(i * buffer.size()).fail());
EXPECT_TRUE(io_stream->good());
for (std::uint8_t j = 0u; j < 2u; j++) {
EXPECT_FALSE(
io_stream
->read(&buffer[(buffer.size() / 2u) * j], 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_bytes(
&file_data[0u], file_data.size(),
utils::encryption::encrypting_reader::get_data_chunk_size() * i,
bytes_read));
EXPECT_EQ(0, std::memcmp(&file_data[0u], &decrypted_data[0u],
file_data.size()));
}
source_file->close();
}
EXPECT_TRUE(utils::file::retry_delete_file(source_file_name));
}
TEST(encrypting_reader, file_data_in_multiple_chunks_as_stream) {
const auto source_file_name = get_source_file_name();
ASSERT_TRUE(utils::file::retry_delete_file(source_file_name));
const auto token = std::string("moose");
auto source_file = create_random_file(
source_file_name,
8u * utils::encryption::encrypting_reader::get_data_chunk_size());
EXPECT_TRUE(source_file != nullptr);
if (source_file) {
stop_type stop_requested = false;
utils::encryption::encrypting_reader reader("test.dat", source_file_name,
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(&buffer[0u], 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(buffer.begin() + offset,
buffer.begin() + 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_bytes(
&file_data[0u], file_data.size(),
(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[0u], &decrypted_data[0u],
file_data.size()));
}
}
source_file->close();
}
EXPECT_TRUE(utils::file::retry_delete_file(source_file_name));
}
} // namespace repertory
/*
Copyright <2018-2023> <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_common.hpp"
#include "types/repertory.hpp"
#include "utils/encrypting_reader.hpp"
#include "utils/file_utils.hpp"
#include "utils/path_utils.hpp"
namespace repertory {
static std::string get_source_file_name() {
return generate_test_file_name("./", "encrypting_reader");
}
TEST(encrypting_reader, get_encrypted_file_name) {
const auto source_file_name = get_source_file_name();
ASSERT_TRUE(utils::file::retry_delete_file(source_file_name));
const auto token = std::string("moose");
auto source_file = create_random_file(source_file_name, 1024ul);
EXPECT_TRUE(source_file != nullptr);
if (source_file) {
stop_type stop_requested = false;
utils::encryption::encrypting_reader reader(
"test.dat", source_file_name, stop_requested, token, std::nullopt);
auto file_name = reader.get_encrypted_file_name();
std::cout << file_name << std::endl;
EXPECT_EQ(api_error::success,
utils::encryption::decrypt_file_name(token, file_name));
std::cout << file_name << std::endl;
EXPECT_STREQ("test.dat", file_name.c_str());
source_file->close();
}
EXPECT_TRUE(utils::file::retry_delete_file(source_file_name));
}
TEST(encrypting_reader, file_data) {
const auto source_file_name = get_source_file_name();
EXPECT_TRUE(utils::file::retry_delete_file(source_file_name));
const auto token = std::string("moose");
auto source_file = create_random_file(
source_file_name,
8u * utils::encryption::encrypting_reader::get_data_chunk_size());
EXPECT_TRUE(source_file != nullptr);
if (source_file) {
stop_type stop_requested = false;
utils::encryption::encrypting_reader reader("test.dat", source_file_name,
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++) {
EXPECT_EQ(buffer.size() / 2u,
utils::encryption::encrypting_reader::reader_function(
&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_bytes(
&file_data[0u], file_data.size(),
utils::encryption::encrypting_reader::get_data_chunk_size() * i,
bytes_read));
EXPECT_EQ(0, std::memcmp(&file_data[0u], &decrypted_data[0u],
file_data.size()));
}
source_file->close();
}
EXPECT_TRUE(utils::file::retry_delete_file(source_file_name));
}
TEST(encrypting_reader, file_data_in_multiple_chunks) {
const auto source_file_name = get_source_file_name();
ASSERT_TRUE(utils::file::retry_delete_file(source_file_name));
const auto token = std::string("moose");
auto source_file = create_random_file(
source_file_name,
8u * utils::encryption::encrypting_reader::get_data_chunk_size());
EXPECT_TRUE(source_file != nullptr);
if (source_file) {
stop_type stop_requested = false;
utils::encryption::encrypting_reader reader("test.dat", source_file_name,
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(
&buffer[0u], 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(buffer.begin() + offset,
buffer.begin() + 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_bytes(
&file_data[0u], file_data.size(),
(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[0u], &decrypted_data[0u],
file_data.size()));
}
}
source_file->close();
}
EXPECT_TRUE(utils::file::retry_delete_file(source_file_name));
}
TEST(encrypting_reader, file_data_as_stream) {
const auto source_file_name = get_source_file_name();
ASSERT_TRUE(utils::file::retry_delete_file(source_file_name));
const auto token = std::string("moose");
auto source_file = create_random_file(
source_file_name,
8u * utils::encryption::encrypting_reader::get_data_chunk_size());
EXPECT_TRUE(source_file != nullptr);
if (source_file) {
stop_type stop_requested = false;
utils::encryption::encrypting_reader reader("test.dat", source_file_name,
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(i * buffer.size()).fail());
EXPECT_TRUE(io_stream->good());
for (std::uint8_t j = 0u; j < 2u; j++) {
EXPECT_FALSE(
io_stream
->read(&buffer[(buffer.size() / 2u) * j], 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_bytes(
&file_data[0u], file_data.size(),
utils::encryption::encrypting_reader::get_data_chunk_size() * i,
bytes_read));
EXPECT_EQ(0, std::memcmp(&file_data[0u], &decrypted_data[0u],
file_data.size()));
}
source_file->close();
}
EXPECT_TRUE(utils::file::retry_delete_file(source_file_name));
}
TEST(encrypting_reader, file_data_in_multiple_chunks_as_stream) {
const auto source_file_name = get_source_file_name();
ASSERT_TRUE(utils::file::retry_delete_file(source_file_name));
const auto token = std::string("moose");
auto source_file = create_random_file(
source_file_name,
8u * utils::encryption::encrypting_reader::get_data_chunk_size());
EXPECT_TRUE(source_file != nullptr);
if (source_file) {
stop_type stop_requested = false;
utils::encryption::encrypting_reader reader("test.dat", source_file_name,
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(&buffer[0u], 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(buffer.begin() + offset,
buffer.begin() + 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_bytes(
&file_data[0u], file_data.size(),
(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[0u], &decrypted_data[0u],
file_data.size()));
}
}
source_file->close();
}
EXPECT_TRUE(utils::file::retry_delete_file(source_file_name));
}
} // namespace repertory

View File

@@ -1,125 +1,125 @@
/*
Copyright <2018-2023> <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_common.hpp"
#include "types/repertory.hpp"
#include "utils/encryption.hpp"
#include "utils/file_utils.hpp"
namespace repertory {
static const std::string buffer = "cow moose dog chicken";
static const std::string token = "moose";
static void test_encrypted_result(const data_buffer &result) {
EXPECT_EQ(buffer.size() +
utils::encryption::encrypting_reader::get_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(encryption, generate_key) {
const auto key = utils::encryption::generate_key(token);
const auto str = utils::to_hex_string(key);
EXPECT_STREQ(
"182072537ada59e4d6b18034a80302ebae935f66adbdf0f271d3d36309c2d481",
str.c_str());
}
TEST(encryption, encrypt_data_buffer) {
data_buffer result;
utils::encryption::encrypt_data(token, buffer, result);
test_encrypted_result(result);
}
TEST(encryption, encrypt_data_buffer_with_key) {
const auto key = utils::encryption::generate_key(token);
data_buffer result;
utils::encryption::encrypt_data(key, buffer, result);
test_encrypted_result(result);
}
TEST(encryption, encrypt_data_pointer) {
data_buffer result;
utils::encryption::encrypt_data(token, &buffer[0u], buffer.size(), result);
test_encrypted_result(result);
}
TEST(encryption, encrypt_data_pointer_with_key) {
const auto key = utils::encryption::generate_key(token);
data_buffer result;
utils::encryption::encrypt_data(key, &buffer[0u], buffer.size(), result);
test_encrypted_result(result);
}
TEST(encryption, decrypt_data_pointer) {
const auto key = utils::encryption::generate_key(token);
data_buffer result;
utils::encryption::encrypt_data(key, &buffer[0u], buffer.size(), result);
std::string data;
EXPECT_TRUE(
utils::encryption::decrypt_data(token, &result[0u], result.size(), data));
EXPECT_EQ(buffer.size(), data.size());
EXPECT_STREQ(buffer.c_str(), data.c_str());
}
TEST(encryption, decrypt_data_buffer_with_key) {
const auto key = utils::encryption::generate_key(token);
data_buffer result;
utils::encryption::encrypt_data(key, &buffer[0u], 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(encryption, decrypt_data_pointer_with_key) {
const auto key = utils::encryption::generate_key(token);
data_buffer result;
utils::encryption::encrypt_data(key, &buffer[0u], buffer.size(), result);
std::string data;
EXPECT_TRUE(
utils::encryption::decrypt_data(key, &result[0u], result.size(), data));
EXPECT_EQ(buffer.size(), data.size());
EXPECT_STREQ(buffer.c_str(), data.c_str());
}
TEST(encryption, decryption_failure) {
const auto key = utils::encryption::generate_key(token);
data_buffer result;
utils::encryption::encrypt_data(key, &buffer[0u], buffer.size(), result);
result[0u] = 0u;
result[1u] = 1u;
result[2u] = 2u;
std::string data;
EXPECT_FALSE(utils::encryption::decrypt_data(key, result, data));
}
} // namespace repertory
/*
Copyright <2018-2023> <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_common.hpp"
#include "types/repertory.hpp"
#include "utils/encryption.hpp"
#include "utils/file_utils.hpp"
namespace repertory {
static const std::string buffer = "cow moose dog chicken";
static const std::string token = "moose";
static void test_encrypted_result(const data_buffer &result) {
EXPECT_EQ(buffer.size() +
utils::encryption::encrypting_reader::get_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(encryption, generate_key) {
const auto key = utils::encryption::generate_key(token);
const auto str = utils::to_hex_string(key);
EXPECT_STREQ(
"182072537ada59e4d6b18034a80302ebae935f66adbdf0f271d3d36309c2d481",
str.c_str());
}
TEST(encryption, encrypt_data_buffer) {
data_buffer result;
utils::encryption::encrypt_data(token, buffer, result);
test_encrypted_result(result);
}
TEST(encryption, encrypt_data_buffer_with_key) {
const auto key = utils::encryption::generate_key(token);
data_buffer result;
utils::encryption::encrypt_data(key, buffer, result);
test_encrypted_result(result);
}
TEST(encryption, encrypt_data_pointer) {
data_buffer result;
utils::encryption::encrypt_data(token, &buffer[0u], buffer.size(), result);
test_encrypted_result(result);
}
TEST(encryption, encrypt_data_pointer_with_key) {
const auto key = utils::encryption::generate_key(token);
data_buffer result;
utils::encryption::encrypt_data(key, &buffer[0u], buffer.size(), result);
test_encrypted_result(result);
}
TEST(encryption, decrypt_data_pointer) {
const auto key = utils::encryption::generate_key(token);
data_buffer result;
utils::encryption::encrypt_data(key, &buffer[0u], buffer.size(), result);
std::string data;
EXPECT_TRUE(
utils::encryption::decrypt_data(token, &result[0u], result.size(), data));
EXPECT_EQ(buffer.size(), data.size());
EXPECT_STREQ(buffer.c_str(), data.c_str());
}
TEST(encryption, decrypt_data_buffer_with_key) {
const auto key = utils::encryption::generate_key(token);
data_buffer result;
utils::encryption::encrypt_data(key, &buffer[0u], 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(encryption, decrypt_data_pointer_with_key) {
const auto key = utils::encryption::generate_key(token);
data_buffer result;
utils::encryption::encrypt_data(key, &buffer[0u], buffer.size(), result);
std::string data;
EXPECT_TRUE(
utils::encryption::decrypt_data(key, &result[0u], result.size(), data));
EXPECT_EQ(buffer.size(), data.size());
EXPECT_STREQ(buffer.c_str(), data.c_str());
}
TEST(encryption, decryption_failure) {
const auto key = utils::encryption::generate_key(token);
data_buffer result;
utils::encryption::encrypt_data(key, &buffer[0u], buffer.size(), result);
result[0u] = 0u;
result[1u] = 1u;
result[2u] = 2u;
std::string data;
EXPECT_FALSE(utils::encryption::decrypt_data(key, result, data));
}
} // namespace repertory

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1,184 +1,184 @@
/*
Copyright <2018-2023> <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_common.hpp"
#include "file_manager/file_manager.hpp"
#include "mocks/mock_provider.hpp"
#include "mocks/mock_upload_manager.hpp"
#include "utils/event_capture.hpp"
#include "utils/path_utils.hpp"
namespace repertory {
static constexpr const std::size_t test_chunk_size = 1024u;
TEST(upload, can_upload_a_valid_file) {
console_consumer c;
event_system::instance().start();
const auto source_path = generate_test_file_name(".", "test");
mock_provider mp;
EXPECT_CALL(mp, is_direct_only()).WillRepeatedly(Return(false));
filesystem_item fsi;
fsi.api_path = "/test.txt";
fsi.size = test_chunk_size * 4u;
fsi.source_path = source_path;
event_consumer ec("file_upload_completed", [&fsi](const event &e) {
const auto &ee = dynamic_cast<const file_upload_completed &>(e);
EXPECT_STREQ(fsi.api_path.c_str(),
ee.get_api_path().get<std::string>().c_str());
EXPECT_STREQ(fsi.source_path.c_str(),
ee.get_source().get<std::string>().c_str());
EXPECT_STREQ("success", ee.get_result().get<std::string>().c_str());
EXPECT_STREQ("0", ee.get_cancelled().get<std::string>().c_str());
});
EXPECT_CALL(mp, upload_file(fsi.api_path, fsi.source_path, _))
.WillOnce([&fsi](const std::string &, const std::string &,
stop_type &stop_requested) -> api_error {
EXPECT_FALSE(stop_requested);
return api_error::success;
});
file_manager::upload upload(fsi, mp);
event_capture e({"file_upload_completed"});
e.wait_for_empty();
EXPECT_EQ(api_error::success, upload.get_api_error());
EXPECT_FALSE(upload.is_cancelled());
event_system::instance().stop();
}
TEST(upload, can_cancel_upload) {
console_consumer c;
event_system::instance().start();
const auto source_path = generate_test_file_name(".", "test");
// create_random_file(source_path, test_chunk_size * 4u)->close();
mock_provider mp;
EXPECT_CALL(mp, is_direct_only()).WillRepeatedly(Return(false));
filesystem_item fsi;
fsi.api_path = "/test.txt";
fsi.size = test_chunk_size * 4u;
fsi.source_path = source_path;
event_consumer ec("file_upload_completed", [&fsi](const event &e) {
const auto &ee = dynamic_cast<const file_upload_completed &>(e);
EXPECT_STREQ(fsi.api_path.c_str(),
ee.get_api_path().get<std::string>().c_str());
EXPECT_STREQ(fsi.source_path.c_str(),
ee.get_source().get<std::string>().c_str());
EXPECT_STREQ("upload_stopped", ee.get_result().get<std::string>().c_str());
EXPECT_STREQ("1", ee.get_cancelled().get<std::string>().c_str());
});
std::mutex mtx;
std::condition_variable cv;
EXPECT_CALL(mp, upload_file(fsi.api_path, fsi.source_path, _))
.WillOnce([&cv, &fsi, &mtx](const std::string &, const std::string &,
stop_type &stop_requested) -> api_error {
EXPECT_FALSE(stop_requested);
unique_mutex_lock l(mtx);
cv.notify_one();
l.unlock();
l.lock();
cv.wait(l);
l.unlock();
EXPECT_TRUE(stop_requested);
return api_error::upload_stopped;
});
unique_mutex_lock l(mtx);
file_manager::upload upload(fsi, mp);
cv.wait(l);
upload.cancel();
cv.notify_one();
l.unlock();
event_capture e({"file_upload_completed"});
e.wait_for_empty();
EXPECT_EQ(api_error::upload_stopped, upload.get_api_error());
EXPECT_TRUE(upload.is_cancelled());
event_system::instance().stop();
}
TEST(upload, can_stop_upload) {
console_consumer c;
event_system::instance().start();
const auto source_path = generate_test_file_name(".", "test");
mock_provider mp;
EXPECT_CALL(mp, is_direct_only()).WillRepeatedly(Return(false));
filesystem_item fsi;
fsi.api_path = "/test.txt";
fsi.size = test_chunk_size * 4u;
fsi.source_path = source_path;
event_consumer ec("file_upload_completed", [&fsi](const event &e) {
const auto &ee = dynamic_cast<const file_upload_completed &>(e);
EXPECT_STREQ(fsi.api_path.c_str(),
ee.get_api_path().get<std::string>().c_str());
EXPECT_STREQ(fsi.source_path.c_str(),
ee.get_source().get<std::string>().c_str());
EXPECT_STREQ("upload_stopped", ee.get_result().get<std::string>().c_str());
EXPECT_STREQ("0", ee.get_cancelled().get<std::string>().c_str());
});
EXPECT_CALL(mp, upload_file(fsi.api_path, fsi.source_path, _))
.WillOnce([&fsi](const std::string &, const std::string &,
stop_type &stop_requested) -> api_error {
std::this_thread::sleep_for(3s);
EXPECT_TRUE(stop_requested);
return api_error::upload_stopped;
});
event_capture e({"file_upload_completed"});
{ file_manager::upload upload(fsi, mp); }
e.wait_for_empty();
event_system::instance().stop();
}
} // namespace repertory
/*
Copyright <2018-2023> <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_common.hpp"
#include "file_manager/file_manager.hpp"
#include "mocks/mock_provider.hpp"
#include "mocks/mock_upload_manager.hpp"
#include "utils/event_capture.hpp"
#include "utils/path_utils.hpp"
namespace repertory {
static constexpr const std::size_t test_chunk_size = 1024u;
TEST(upload, can_upload_a_valid_file) {
console_consumer c;
event_system::instance().start();
const auto source_path = generate_test_file_name(".", "test");
mock_provider mp;
EXPECT_CALL(mp, is_direct_only()).WillRepeatedly(Return(false));
filesystem_item fsi;
fsi.api_path = "/test.txt";
fsi.size = test_chunk_size * 4u;
fsi.source_path = source_path;
event_consumer ec("file_upload_completed", [&fsi](const event &e) {
const auto &ee = dynamic_cast<const file_upload_completed &>(e);
EXPECT_STREQ(fsi.api_path.c_str(),
ee.get_api_path().get<std::string>().c_str());
EXPECT_STREQ(fsi.source_path.c_str(),
ee.get_source().get<std::string>().c_str());
EXPECT_STREQ("success", ee.get_result().get<std::string>().c_str());
EXPECT_STREQ("0", ee.get_cancelled().get<std::string>().c_str());
});
EXPECT_CALL(mp, upload_file(fsi.api_path, fsi.source_path, _))
.WillOnce([&fsi](const std::string &, const std::string &,
stop_type &stop_requested) -> api_error {
EXPECT_FALSE(stop_requested);
return api_error::success;
});
file_manager::upload upload(fsi, mp);
event_capture e({"file_upload_completed"});
e.wait_for_empty();
EXPECT_EQ(api_error::success, upload.get_api_error());
EXPECT_FALSE(upload.is_cancelled());
event_system::instance().stop();
}
TEST(upload, can_cancel_upload) {
console_consumer c;
event_system::instance().start();
const auto source_path = generate_test_file_name(".", "test");
// create_random_file(source_path, test_chunk_size * 4u)->close();
mock_provider mp;
EXPECT_CALL(mp, is_direct_only()).WillRepeatedly(Return(false));
filesystem_item fsi;
fsi.api_path = "/test.txt";
fsi.size = test_chunk_size * 4u;
fsi.source_path = source_path;
event_consumer ec("file_upload_completed", [&fsi](const event &e) {
const auto &ee = dynamic_cast<const file_upload_completed &>(e);
EXPECT_STREQ(fsi.api_path.c_str(),
ee.get_api_path().get<std::string>().c_str());
EXPECT_STREQ(fsi.source_path.c_str(),
ee.get_source().get<std::string>().c_str());
EXPECT_STREQ("upload_stopped", ee.get_result().get<std::string>().c_str());
EXPECT_STREQ("1", ee.get_cancelled().get<std::string>().c_str());
});
std::mutex mtx;
std::condition_variable cv;
EXPECT_CALL(mp, upload_file(fsi.api_path, fsi.source_path, _))
.WillOnce([&cv, &fsi, &mtx](const std::string &, const std::string &,
stop_type &stop_requested) -> api_error {
EXPECT_FALSE(stop_requested);
unique_mutex_lock l(mtx);
cv.notify_one();
l.unlock();
l.lock();
cv.wait(l);
l.unlock();
EXPECT_TRUE(stop_requested);
return api_error::upload_stopped;
});
unique_mutex_lock l(mtx);
file_manager::upload upload(fsi, mp);
cv.wait(l);
upload.cancel();
cv.notify_one();
l.unlock();
event_capture e({"file_upload_completed"});
e.wait_for_empty();
EXPECT_EQ(api_error::upload_stopped, upload.get_api_error());
EXPECT_TRUE(upload.is_cancelled());
event_system::instance().stop();
}
TEST(upload, can_stop_upload) {
console_consumer c;
event_system::instance().start();
const auto source_path = generate_test_file_name(".", "test");
mock_provider mp;
EXPECT_CALL(mp, is_direct_only()).WillRepeatedly(Return(false));
filesystem_item fsi;
fsi.api_path = "/test.txt";
fsi.size = test_chunk_size * 4u;
fsi.source_path = source_path;
event_consumer ec("file_upload_completed", [&fsi](const event &e) {
const auto &ee = dynamic_cast<const file_upload_completed &>(e);
EXPECT_STREQ(fsi.api_path.c_str(),
ee.get_api_path().get<std::string>().c_str());
EXPECT_STREQ(fsi.source_path.c_str(),
ee.get_source().get<std::string>().c_str());
EXPECT_STREQ("upload_stopped", ee.get_result().get<std::string>().c_str());
EXPECT_STREQ("0", ee.get_cancelled().get<std::string>().c_str());
});
EXPECT_CALL(mp, upload_file(fsi.api_path, fsi.source_path, _))
.WillOnce([&fsi](const std::string &, const std::string &,
stop_type &stop_requested) -> api_error {
std::this_thread::sleep_for(3s);
EXPECT_TRUE(stop_requested);
return api_error::upload_stopped;
});
event_capture e({"file_upload_completed"});
{ file_manager::upload upload(fsi, mp); }
e.wait_for_empty();
event_system::instance().stop();
}
} // namespace repertory

View File

@@ -1,148 +1,148 @@
/*
Copyright <2018-2023> <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_WINFSP_FIXTURE_H
#define REPERTORY_WINFSP_FIXTURE_H
#if _WIN32
#include "test_common.hpp"
#include "app_config.hpp"
#include "comm/curl/curl_comm.hpp"
#include "drives/winfsp/winfsp_drive.hpp"
#include "platform/platform.hpp"
#include "providers/s3/s3_provider.hpp"
#include "providers/sia/sia_provider.hpp"
extern std::size_t PROVIDER_INDEX;
namespace repertory {
class winfsp_test : public ::testing::Test {
public:
lock_data lock_data_;
std::unique_ptr<app_config> config;
std::unique_ptr<curl_comm> comm;
std::unique_ptr<i_provider> provider;
std::unique_ptr<winfsp_drive> drive;
protected:
void SetUp() override {
if (PROVIDER_INDEX != 0) {
if (PROVIDER_INDEX == 1) {
#ifdef REPERTORY_ENABLE_S3
EXPECT_TRUE(utils::file::delete_directory_recursively(
"./winfsp_test" + std::to_string(PROVIDER_INDEX)));
app_config src_cfg(provider_type::s3,
utils::path::combine(get_test_dir(), {"storj"}));
config = std::make_unique<app_config>(
provider_type::s3,
"./winfsp_test" + std::to_string(PROVIDER_INDEX));
EXPECT_FALSE(config
->set_value_by_name("S3Config.AccessKey",
src_cfg.get_s3_config().access_key)
.empty());
EXPECT_FALSE(config
->set_value_by_name("S3Config.SecretKey",
src_cfg.get_s3_config().secret_key)
.empty());
EXPECT_FALSE(config
->set_value_by_name("S3Config.Region",
src_cfg.get_s3_config().region)
.empty());
EXPECT_FALSE(
config
->set_value_by_name("S3Config.EncryptionToken",
src_cfg.get_s3_config().encryption_token)
.empty());
EXPECT_FALSE(
config
->set_value_by_name("S3Config.URL", src_cfg.get_s3_config().url)
.empty());
EXPECT_FALSE(
config->set_value_by_name("S3Config.Bucket", "repertory").empty());
config->set_event_level(event_level::verbose);
config->set_enable_drive_events(true);
event_system::instance().start();
comm = std::make_unique<curl_comm>(config->get_s3_config());
provider = std::make_unique<s3_provider>(*config, *comm);
drive = std::make_unique<winfsp_drive>(*config, lock_data_, *provider);
#endif
return;
}
if (PROVIDER_INDEX == 2) {
EXPECT_TRUE(utils::file::delete_directory_recursively(
"./winfsp_test" + std::to_string(PROVIDER_INDEX)));
app_config src_cfg(provider_type::sia,
utils::path::combine(get_test_dir(), {"sia"}));
config = std::make_unique<app_config>(
provider_type::sia,
"./winfsp_test" + std::to_string(PROVIDER_INDEX));
[[maybe_unused]] auto val = config->set_value_by_name(
"HostConfig.AgentString", src_cfg.get_host_config().agent_string);
EXPECT_FALSE(
config
->set_value_by_name("HostConfig.ApiPassword",
src_cfg.get_host_config().api_password)
.empty());
EXPECT_FALSE(config
->set_value_by_name(
"HostConfig.ApiPort",
std::to_string(src_cfg.get_host_config().api_port))
.empty());
EXPECT_FALSE(
config
->set_value_by_name("HostConfig.HostNameOrIp",
src_cfg.get_host_config().host_name_or_ip)
.empty());
config->set_event_level(event_level::debug);
config->set_enable_drive_events(true);
event_system::instance().start();
comm = std::make_unique<curl_comm>(config->get_host_config());
provider = std::make_unique<sia_provider>(*config, *comm);
drive = std::make_unique<winfsp_drive>(*config, lock_data_, *provider);
return;
}
}
}
void TearDown() override {
if (PROVIDER_INDEX != 0) {
drive.reset();
provider.reset();
comm.reset();
config.reset();
event_system::instance().stop();
EXPECT_TRUE(utils::file::delete_directory_recursively(
"./winfsp_test" + std::to_string(PROVIDER_INDEX)));
}
}
};
} // namespace repertory
#endif
#endif // REPERTORY_WINFSP_FIXTURE_H
/*
Copyright <2018-2023> <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_WINFSP_FIXTURE_H
#define REPERTORY_WINFSP_FIXTURE_H
#if _WIN32
#include "test_common.hpp"
#include "app_config.hpp"
#include "comm/curl/curl_comm.hpp"
#include "drives/winfsp/winfsp_drive.hpp"
#include "platform/platform.hpp"
#include "providers/s3/s3_provider.hpp"
#include "providers/sia/sia_provider.hpp"
extern std::size_t PROVIDER_INDEX;
namespace repertory {
class winfsp_test : public ::testing::Test {
public:
lock_data lock_data_;
std::unique_ptr<app_config> config;
std::unique_ptr<curl_comm> comm;
std::unique_ptr<i_provider> provider;
std::unique_ptr<winfsp_drive> drive;
protected:
void SetUp() override {
if (PROVIDER_INDEX != 0) {
if (PROVIDER_INDEX == 1) {
#ifdef REPERTORY_ENABLE_S3
EXPECT_TRUE(utils::file::delete_directory_recursively(
"./winfsp_test" + std::to_string(PROVIDER_INDEX)));
app_config src_cfg(provider_type::s3,
utils::path::combine(get_test_dir(), {"storj"}));
config = std::make_unique<app_config>(
provider_type::s3,
"./winfsp_test" + std::to_string(PROVIDER_INDEX));
EXPECT_FALSE(config
->set_value_by_name("S3Config.AccessKey",
src_cfg.get_s3_config().access_key)
.empty());
EXPECT_FALSE(config
->set_value_by_name("S3Config.SecretKey",
src_cfg.get_s3_config().secret_key)
.empty());
EXPECT_FALSE(config
->set_value_by_name("S3Config.Region",
src_cfg.get_s3_config().region)
.empty());
EXPECT_FALSE(
config
->set_value_by_name("S3Config.EncryptionToken",
src_cfg.get_s3_config().encryption_token)
.empty());
EXPECT_FALSE(
config
->set_value_by_name("S3Config.URL", src_cfg.get_s3_config().url)
.empty());
EXPECT_FALSE(
config->set_value_by_name("S3Config.Bucket", "repertory").empty());
config->set_event_level(event_level::verbose);
config->set_enable_drive_events(true);
event_system::instance().start();
comm = std::make_unique<curl_comm>(config->get_s3_config());
provider = std::make_unique<s3_provider>(*config, *comm);
drive = std::make_unique<winfsp_drive>(*config, lock_data_, *provider);
#endif
return;
}
if (PROVIDER_INDEX == 2) {
EXPECT_TRUE(utils::file::delete_directory_recursively(
"./winfsp_test" + std::to_string(PROVIDER_INDEX)));
app_config src_cfg(provider_type::sia,
utils::path::combine(get_test_dir(), {"sia"}));
config = std::make_unique<app_config>(
provider_type::sia,
"./winfsp_test" + std::to_string(PROVIDER_INDEX));
[[maybe_unused]] auto val = config->set_value_by_name(
"HostConfig.AgentString", src_cfg.get_host_config().agent_string);
EXPECT_FALSE(
config
->set_value_by_name("HostConfig.ApiPassword",
src_cfg.get_host_config().api_password)
.empty());
EXPECT_FALSE(config
->set_value_by_name(
"HostConfig.ApiPort",
std::to_string(src_cfg.get_host_config().api_port))
.empty());
EXPECT_FALSE(
config
->set_value_by_name("HostConfig.HostNameOrIp",
src_cfg.get_host_config().host_name_or_ip)
.empty());
config->set_event_level(event_level::debug);
config->set_enable_drive_events(true);
event_system::instance().start();
comm = std::make_unique<curl_comm>(config->get_host_config());
provider = std::make_unique<sia_provider>(*config, *comm);
drive = std::make_unique<winfsp_drive>(*config, lock_data_, *provider);
return;
}
}
}
void TearDown() override {
if (PROVIDER_INDEX != 0) {
drive.reset();
provider.reset();
comm.reset();
config.reset();
event_system::instance().stop();
EXPECT_TRUE(utils::file::delete_directory_recursively(
"./winfsp_test" + std::to_string(PROVIDER_INDEX)));
}
}
};
} // namespace repertory
#endif
#endif // REPERTORY_WINFSP_FIXTURE_H

File diff suppressed because it is too large Load Diff

View File

@@ -1,104 +1,104 @@
/*
Copyright <2018-2023> <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_common.hpp"
#include "platform/platform.hpp"
namespace repertory {
TEST(lock_data, lock_and_unlock) {
{
lock_data l(provider_type::sia, "1");
EXPECT_EQ(lock_result::success, l.grab_lock());
std::thread([]() {
lock_data l2(provider_type::sia, "1");
EXPECT_EQ(lock_result::locked, l2.grab_lock(10));
}).join();
}
std::thread([]() {
lock_data l(provider_type::sia, "1");
EXPECT_EQ(lock_result::success, l.grab_lock(10));
}).join();
#ifdef _WIN32
lock_data l2(provider_type::remote, "1");
EXPECT_EQ(lock_result::success, l2.grab_lock());
lock_data l3(provider_type::remote, "2");
EXPECT_EQ(lock_result::success, l3.grab_lock());
#endif
}
#ifdef _WIN32
TEST(lock_data, set_and_unset_mount_state) {
lock_data l(provider_type::sia, "1");
EXPECT_TRUE(l.set_mount_state(true, "C:", 99));
lock_data l2(provider_type::remote, "1");
EXPECT_TRUE(l2.set_mount_state(true, "D:", 97));
lock_data l3(provider_type::remote, "2");
EXPECT_TRUE(l3.set_mount_state(true, "E:", 96));
json mount_state;
EXPECT_TRUE(l.get_mount_state(mount_state));
EXPECT_STREQ(R"({"Active":true,"Location":"C:","PID":99})",
mount_state["Sia1"].dump().c_str());
EXPECT_STREQ(R"({"Active":true,"Location":"D:","PID":97})",
mount_state["Remote1"].dump().c_str());
EXPECT_STREQ(R"({"Active":true,"Location":"E:","PID":96})",
mount_state["Remote2"].dump().c_str());
EXPECT_TRUE(l.set_mount_state(false, "C:", 99));
EXPECT_TRUE(l2.set_mount_state(false, "D:", 98));
EXPECT_TRUE(l3.set_mount_state(false, "E:", 97));
EXPECT_TRUE(l.get_mount_state(mount_state));
EXPECT_STREQ(R"({"Active":false,"Location":"","PID":-1})",
mount_state["Sia1"].dump().c_str());
EXPECT_STREQ(R"({"Active":false,"Location":"","PID":-1})",
mount_state["Remote1"].dump().c_str());
EXPECT_STREQ(R"({"Active":false,"Location":"","PID":-1})",
mount_state["Remote2"].dump().c_str());
}
#else
TEST(lock_data, set_and_unset_mount_state) {
lock_data l(provider_type::sia, "1");
EXPECT_TRUE(l.set_mount_state(true, "/mnt/1", 99));
json mount_state;
EXPECT_TRUE(l.get_mount_state(mount_state));
EXPECT_STREQ(R"({"Active":true,"Location":"/mnt/1","PID":99})",
mount_state["Sia1"].dump().c_str());
EXPECT_TRUE(l.set_mount_state(false, "/mnt/1", 99));
EXPECT_TRUE(l.get_mount_state(mount_state));
EXPECT_STREQ(R"({"Active":false,"Location":"","PID":-1})",
mount_state["Sia1"].dump().c_str());
}
#endif
} // namespace repertory
/*
Copyright <2018-2023> <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_common.hpp"
#include "platform/platform.hpp"
namespace repertory {
TEST(lock_data, lock_and_unlock) {
{
lock_data l(provider_type::sia, "1");
EXPECT_EQ(lock_result::success, l.grab_lock());
std::thread([]() {
lock_data l2(provider_type::sia, "1");
EXPECT_EQ(lock_result::locked, l2.grab_lock(10));
}).join();
}
std::thread([]() {
lock_data l(provider_type::sia, "1");
EXPECT_EQ(lock_result::success, l.grab_lock(10));
}).join();
#ifdef _WIN32
lock_data l2(provider_type::remote, "1");
EXPECT_EQ(lock_result::success, l2.grab_lock());
lock_data l3(provider_type::remote, "2");
EXPECT_EQ(lock_result::success, l3.grab_lock());
#endif
}
#ifdef _WIN32
TEST(lock_data, set_and_unset_mount_state) {
lock_data l(provider_type::sia, "1");
EXPECT_TRUE(l.set_mount_state(true, "C:", 99));
lock_data l2(provider_type::remote, "1");
EXPECT_TRUE(l2.set_mount_state(true, "D:", 97));
lock_data l3(provider_type::remote, "2");
EXPECT_TRUE(l3.set_mount_state(true, "E:", 96));
json mount_state;
EXPECT_TRUE(l.get_mount_state(mount_state));
EXPECT_STREQ(R"({"Active":true,"Location":"C:","PID":99})",
mount_state["Sia1"].dump().c_str());
EXPECT_STREQ(R"({"Active":true,"Location":"D:","PID":97})",
mount_state["Remote1"].dump().c_str());
EXPECT_STREQ(R"({"Active":true,"Location":"E:","PID":96})",
mount_state["Remote2"].dump().c_str());
EXPECT_TRUE(l.set_mount_state(false, "C:", 99));
EXPECT_TRUE(l2.set_mount_state(false, "D:", 98));
EXPECT_TRUE(l3.set_mount_state(false, "E:", 97));
EXPECT_TRUE(l.get_mount_state(mount_state));
EXPECT_STREQ(R"({"Active":false,"Location":"","PID":-1})",
mount_state["Sia1"].dump().c_str());
EXPECT_STREQ(R"({"Active":false,"Location":"","PID":-1})",
mount_state["Remote1"].dump().c_str());
EXPECT_STREQ(R"({"Active":false,"Location":"","PID":-1})",
mount_state["Remote2"].dump().c_str());
}
#else
TEST(lock_data, set_and_unset_mount_state) {
lock_data l(provider_type::sia, "1");
EXPECT_TRUE(l.set_mount_state(true, "/mnt/1", 99));
json mount_state;
EXPECT_TRUE(l.get_mount_state(mount_state));
EXPECT_STREQ(R"({"Active":true,"Location":"/mnt/1","PID":99})",
mount_state["Sia1"].dump().c_str());
EXPECT_TRUE(l.set_mount_state(false, "/mnt/1", 99));
EXPECT_TRUE(l.get_mount_state(mount_state));
EXPECT_STREQ(R"({"Active":false,"Location":"","PID":-1})",
mount_state["Sia1"].dump().c_str());
}
#endif
} // namespace repertory

View File

@@ -1,151 +1,151 @@
/*
Copyright <2018-2023> <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 TESTS_MOCKS_MOCK_FUSE_DRIVE_HPP_
#define TESTS_MOCKS_MOCK_FUSE_DRIVE_HPP_
#ifndef _WIN32
#include "test_common.hpp"
#include "drives/fuse/i_fuse_drive.hpp"
#include "types/remote.hpp"
#include "types/repertory.hpp"
#include "utils/file_utils.hpp"
#include "utils/path_utils.hpp"
#include "utils/utils.hpp"
namespace repertory {
class mock_fuse_drive final : public virtual i_fuse_drive {
public:
explicit mock_fuse_drive(std::string mount_location)
: mount_location_(std::move(mount_location)) {}
private:
const std::string mount_location_;
std::unordered_map<std::string, api_meta_map> meta_;
public:
api_error check_owner(const std::string &) const override {
return api_error::success;
}
api_error check_parent_access(const std::string &, int) const override {
return api_error::success;
}
std::uint64_t get_directory_item_count(const std::string &) const override {
return 1;
}
directory_item_list get_directory_items(const std::string &) const override {
directory_item_list list{};
directory_item di{};
di.api_path = ".";
di.directory = true;
di.size = 0;
di.meta = {{META_ATTRIBUTES, "16"},
{META_MODIFIED, std::to_string(utils::get_file_time_now())},
{META_WRITTEN, std::to_string(utils::get_file_time_now())},
{META_ACCESSED, std::to_string(utils::get_file_time_now())},
{META_CREATION, std::to_string(utils::get_file_time_now())}};
list.emplace_back(di);
di.api_path = "..";
list.emplace_back(di);
return list;
}
std::uint64_t get_file_size(const std::string &) const override { return 0u; }
api_error get_item_meta(const std::string &api_path,
api_meta_map &meta) const override {
meta = const_cast<mock_fuse_drive *>(this)->meta_[api_path];
return api_error::success;
}
api_error get_item_meta(const std::string &api_path, const std::string &name,
std::string &value) const override {
value = const_cast<mock_fuse_drive *>(this)->meta_[api_path][name];
if (value.empty()) {
value = "0";
}
return api_error::success;
}
std::uint64_t get_total_drive_space() const override {
return 100 * 1024 * 1024;
}
std::uint64_t get_total_item_count() const override { return 0u; }
std::uint64_t get_used_drive_space() const override { return 0u; }
void get_volume_info(UINT64 &total_size, UINT64 &free_size,
std::string &volume_label) const override {
free_size = 100u;
total_size = 200u;
volume_label = "TestVolumeLabel";
}
void populate_stat(const directory_item &, struct stat &) const override {}
int rename_directory(const std::string &from_api_path,
const std::string &to_api_path) override {
const auto from_file_path =
utils::path::combine(mount_location_, {from_api_path});
const auto to_file_path =
utils::path::combine(mount_location_, {to_api_path});
return rename(from_file_path.c_str(), to_file_path.c_str());
}
int rename_file(const std::string &from_api_path,
const std::string &to_api_path, bool overwrite) override {
const auto from_file_path =
utils::path::combine(mount_location_, {from_api_path});
const auto to_file_path =
utils::path::combine(mount_location_, {to_api_path});
if (overwrite) {
if (not utils::file::retry_delete_file(to_file_path)) {
return -1;
}
} else if (utils::file::is_directory(to_file_path) ||
utils::file::is_file(to_file_path)) {
errno = EEXIST;
return -1;
}
return rename(from_file_path.c_str(), to_file_path.c_str());
}
bool is_processing(const std::string &) const override { return false; }
void set_item_meta(const std::string &api_path, const std::string &key,
const std::string &value) override {
meta_[api_path][key] = value;
}
};
} // namespace repertory
#endif // _WIN32
#endif // TESTS_MOCKS_MOCK_FUSE_DRIVE_HPP_
/*
Copyright <2018-2023> <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 TESTS_MOCKS_MOCK_FUSE_DRIVE_HPP_
#define TESTS_MOCKS_MOCK_FUSE_DRIVE_HPP_
#ifndef _WIN32
#include "test_common.hpp"
#include "drives/fuse/i_fuse_drive.hpp"
#include "types/remote.hpp"
#include "types/repertory.hpp"
#include "utils/file_utils.hpp"
#include "utils/path_utils.hpp"
#include "utils/utils.hpp"
namespace repertory {
class mock_fuse_drive final : public virtual i_fuse_drive {
public:
explicit mock_fuse_drive(std::string mount_location)
: mount_location_(std::move(mount_location)) {}
private:
const std::string mount_location_;
std::unordered_map<std::string, api_meta_map> meta_;
public:
api_error check_owner(const std::string &) const override {
return api_error::success;
}
api_error check_parent_access(const std::string &, int) const override {
return api_error::success;
}
std::uint64_t get_directory_item_count(const std::string &) const override {
return 1;
}
directory_item_list get_directory_items(const std::string &) const override {
directory_item_list list{};
directory_item di{};
di.api_path = ".";
di.directory = true;
di.size = 0;
di.meta = {{META_ATTRIBUTES, "16"},
{META_MODIFIED, std::to_string(utils::get_file_time_now())},
{META_WRITTEN, std::to_string(utils::get_file_time_now())},
{META_ACCESSED, std::to_string(utils::get_file_time_now())},
{META_CREATION, std::to_string(utils::get_file_time_now())}};
list.emplace_back(di);
di.api_path = "..";
list.emplace_back(di);
return list;
}
std::uint64_t get_file_size(const std::string &) const override { return 0u; }
api_error get_item_meta(const std::string &api_path,
api_meta_map &meta) const override {
meta = const_cast<mock_fuse_drive *>(this)->meta_[api_path];
return api_error::success;
}
api_error get_item_meta(const std::string &api_path, const std::string &name,
std::string &value) const override {
value = const_cast<mock_fuse_drive *>(this)->meta_[api_path][name];
if (value.empty()) {
value = "0";
}
return api_error::success;
}
std::uint64_t get_total_drive_space() const override {
return 100 * 1024 * 1024;
}
std::uint64_t get_total_item_count() const override { return 0u; }
std::uint64_t get_used_drive_space() const override { return 0u; }
void get_volume_info(UINT64 &total_size, UINT64 &free_size,
std::string &volume_label) const override {
free_size = 100u;
total_size = 200u;
volume_label = "TestVolumeLabel";
}
void populate_stat(const directory_item &, struct stat &) const override {}
int rename_directory(const std::string &from_api_path,
const std::string &to_api_path) override {
const auto from_file_path =
utils::path::combine(mount_location_, {from_api_path});
const auto to_file_path =
utils::path::combine(mount_location_, {to_api_path});
return rename(from_file_path.c_str(), to_file_path.c_str());
}
int rename_file(const std::string &from_api_path,
const std::string &to_api_path, bool overwrite) override {
const auto from_file_path =
utils::path::combine(mount_location_, {from_api_path});
const auto to_file_path =
utils::path::combine(mount_location_, {to_api_path});
if (overwrite) {
if (not utils::file::retry_delete_file(to_file_path)) {
return -1;
}
} else if (utils::file::is_directory(to_file_path) ||
utils::file::is_file(to_file_path)) {
errno = EEXIST;
return -1;
}
return rename(from_file_path.c_str(), to_file_path.c_str());
}
bool is_processing(const std::string &) const override { return false; }
void set_item_meta(const std::string &api_path, const std::string &key,
const std::string &value) override {
meta_[api_path][key] = value;
}
};
} // namespace repertory
#endif // _WIN32
#endif // TESTS_MOCKS_MOCK_FUSE_DRIVE_HPP_

View File

@@ -1,97 +1,99 @@
/*
Copyright <2018-2023> <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 TESTS_MOCKS_MOCK_OPEN_FILE_HPP_
#define TESTS_MOCKS_MOCK_OPEN_FILE_HPP_
#include "test_common.hpp"
#include "file_manager/i_open_file.hpp"
namespace repertory {
class mock_open_file : public virtual i_closeable_open_file {
public:
MOCK_METHOD(std::string, get_api_path, (), (const, override));
MOCK_METHOD(std::size_t, get_chunk_size, (), (const, override));
MOCK_METHOD(std::uint64_t, get_file_size, (), (const, override));
MOCK_METHOD(filesystem_item, get_filesystem_item, (), (const, override));
MOCK_METHOD(open_file_data, get_open_data, (std::uint64_t handle),
(const, override));
MOCK_METHOD(std::size_t, get_open_file_count, (), (const, override));
MOCK_METHOD(boost::dynamic_bitset<>, get_read_state, (), (const, override));
MOCK_METHOD(bool, get_read_state, (std::size_t chunk), (const, override));
MOCK_METHOD(std::string, get_source_path, (), (const, override));
MOCK_METHOD(bool, is_directory, (), (const, override));
MOCK_METHOD(api_error, native_operation,
(const native_operation_callback &callback), (override));
MOCK_METHOD(api_error, native_operation,
(std::uint64_t new_file_size,
const native_operation_callback &callback),
(override));
MOCK_METHOD(api_error, read,
(std::size_t read_size, std::uint64_t read_offset,
data_buffer &data),
(override));
MOCK_METHOD(api_error, resize, (std::uint64_t new_file_size), (override));
MOCK_METHOD(void, set_api_path, (const std::string &api_path), (override));
MOCK_METHOD(api_error, write,
(std::uint64_t write_offset, const data_buffer &data,
std::size_t &bytes_written),
(override));
MOCK_METHOD(void, add, (std::uint64_t handle, open_file_data ofd),
(override));
MOCK_METHOD(bool, can_close, (), (const, override));
MOCK_METHOD(bool, close, (), (override));
MOCK_METHOD(std::vector<std::uint64_t>, get_handles, (), (const, override));
MOCK_METHOD((std::map<std::uint64_t, open_file_data>), get_open_data, (),
(const, override));
MOCK_METHOD(bool, is_complete, (), (const, override));
MOCK_METHOD(bool, is_modified, (), (const, override));
MOCK_METHOD(bool, is_write_supported, (), (const, override));
MOCK_METHOD(void, remove, (std::uint64_t handle), (override));
};
} // namespace repertory
#endif // TESTS_MOCKS_MOCK_OPEN_FILE_HPP_
/*
Copyright <2018-2023> <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 TESTS_MOCKS_MOCK_OPEN_FILE_HPP_
#define TESTS_MOCKS_MOCK_OPEN_FILE_HPP_
#include "test_common.hpp"
#include "file_manager/i_open_file.hpp"
namespace repertory {
class mock_open_file : public virtual i_closeable_open_file {
public:
MOCK_METHOD(std::string, get_api_path, (), (const, override));
MOCK_METHOD(std::size_t, get_chunk_size, (), (const, override));
MOCK_METHOD(std::uint64_t, get_file_size, (), (const, override));
MOCK_METHOD(filesystem_item, get_filesystem_item, (), (const, override));
MOCK_METHOD(open_file_data, get_open_data, (std::uint64_t handle),
(const, override));
MOCK_METHOD(std::size_t, get_open_file_count, (), (const, override));
MOCK_METHOD(boost::dynamic_bitset<>, get_read_state, (), (const, override));
MOCK_METHOD(bool, get_read_state, (std::size_t chunk), (const, override));
MOCK_METHOD(std::string, get_source_path, (), (const, override));
MOCK_METHOD(bool, has_handle, (std::uint64_t handle), (const, override));
MOCK_METHOD(bool, is_directory, (), (const, override));
MOCK_METHOD(api_error, native_operation,
(const native_operation_callback &callback), (override));
MOCK_METHOD(api_error, native_operation,
(std::uint64_t new_file_size,
const native_operation_callback &callback),
(override));
MOCK_METHOD(api_error, read,
(std::size_t read_size, std::uint64_t read_offset,
data_buffer &data),
(override));
MOCK_METHOD(api_error, resize, (std::uint64_t new_file_size), (override));
MOCK_METHOD(void, set_api_path, (const std::string &api_path), (override));
MOCK_METHOD(api_error, write,
(std::uint64_t write_offset, const data_buffer &data,
std::size_t &bytes_written),
(override));
MOCK_METHOD(void, add, (std::uint64_t handle, open_file_data ofd),
(override));
MOCK_METHOD(bool, can_close, (), (const, override));
MOCK_METHOD(bool, close, (), (override));
MOCK_METHOD(std::vector<std::uint64_t>, get_handles, (), (const, override));
MOCK_METHOD((std::map<std::uint64_t, open_file_data>), get_open_data, (),
(const, override));
MOCK_METHOD(bool, is_complete, (), (const, override));
MOCK_METHOD(bool, is_modified, (), (const, override));
MOCK_METHOD(bool, is_write_supported, (), (const, override));
MOCK_METHOD(void, remove, (std::uint64_t handle), (override));
};
} // namespace repertory
#endif // TESTS_MOCKS_MOCK_OPEN_FILE_HPP_

View File

@@ -1,162 +1,162 @@
/*
Copyright <2018-2023> <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 TESTS_MOCKS_MOCK_PROVIDER_HPP_
#define TESTS_MOCKS_MOCK_PROVIDER_HPP_
#include "test_common.hpp"
#include "providers/i_provider.hpp"
#include "types/repertory.hpp"
namespace repertory {
class mock_provider : public virtual i_provider {
public:
mock_provider(bool allow_rename = true) : allow_rename_(allow_rename) {}
private:
const bool allow_rename_;
public:
MOCK_METHOD(api_error, create_directory,
(const std::string &api_path, api_meta_map &meta), (override));
MOCK_METHOD(api_error, create_directory_clone_source_meta,
(const std::string &source_api_path, const std::string &api_path),
(override));
MOCK_METHOD(api_error, create_file,
(const std::string &api_path, api_meta_map &meta), (override));
MOCK_METHOD(api_error, get_api_path_from_source,
(const std::string &source_path, std::string &api_path),
(const, override));
MOCK_METHOD(std::uint64_t, get_directory_item_count,
(const std::string &api_path), (const, override));
MOCK_METHOD(api_error, get_directory_items,
(const std::string &api_path, directory_item_list &list),
(const, override));
MOCK_METHOD(api_error, get_file,
(const std::string &api_path, api_file &file), (const, override));
MOCK_METHOD(api_error, get_file_list, (api_file_list & list),
(const, override));
MOCK_METHOD(api_error, get_file_size,
(const std::string &api_path, std::uint64_t &file_size),
(const, override));
MOCK_METHOD(api_error, get_filesystem_item,
(const std::string &api_path, bool directory,
filesystem_item &fsi),
(const, override));
MOCK_METHOD(api_error, get_filesystem_item_and_file,
(const std::string &api_path, api_file &file,
filesystem_item &fsi),
(const, override));
MOCK_METHOD(api_error, get_filesystem_item_from_source_path,
(const std::string &source_path, filesystem_item &fsi),
(const, override));
MOCK_METHOD(api_error, get_item_meta,
(const std::string &api_path, api_meta_map &meta),
(const, override));
MOCK_METHOD(api_error, get_item_meta,
(const std::string &api_path, const std::string &key,
std::string &value),
(const, override));
MOCK_METHOD((std::vector<std::string>), get_pinned_files, (),
(const, override));
MOCK_METHOD(provider_type, get_provider_type, (), (const, override));
MOCK_METHOD(std::uint64_t, get_total_drive_space, (), (const, override));
MOCK_METHOD(std::uint64_t, get_total_item_count, (), (const, override));
MOCK_METHOD(std::uint64_t, get_used_drive_space, (), (const, override));
MOCK_METHOD(bool, is_direct_only, (), (const, override));
MOCK_METHOD(api_error, is_directory,
(const std::string &api_path, bool &exists), (const, override));
MOCK_METHOD(api_error, is_file, (const std::string &api_path, bool &exists),
(const, override));
bool is_file_writeable(const std::string & /* api_path */) const override {
return true;
}
MOCK_METHOD(bool, is_online, (), (const, override));
bool is_rename_supported() const override { return allow_rename_; }
MOCK_METHOD(api_error, read_file_bytes,
(const std::string &path, std::size_t size, std::uint64_t offset,
data_buffer &data, stop_type &stop_requested),
(override));
MOCK_METHOD(api_error, remove_directory, (const std::string &api_path),
(override));
MOCK_METHOD(api_error, remove_file, (const std::string &api_path),
(override));
MOCK_METHOD(api_error, remove_item_meta,
(const std::string &api_path, const std::string &key),
(override));
MOCK_METHOD(api_error, rename_file,
(const std::string &from_api_path,
const std::string &to_api_path),
(override));
MOCK_METHOD(api_error, set_item_meta,
(const std::string &api_path, const std::string &key,
const std::string &value),
(override));
MOCK_METHOD(api_error, set_item_meta,
(const std::string &api_path, const api_meta_map &meta),
(override));
MOCK_METHOD(bool, start,
(api_item_added_callback api_item_added, i_file_manager *fm),
(override));
MOCK_METHOD(void, stop, (), (override));
MOCK_METHOD(api_error, upload_file,
(const std::string &api_path, const std::string &source_path,
stop_type &stop_requested),
(override));
};
} // namespace repertory
#endif // TESTS_MOCKS_MOCK_PROVIDER_HPP_
/*
Copyright <2018-2023> <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 TESTS_MOCKS_MOCK_PROVIDER_HPP_
#define TESTS_MOCKS_MOCK_PROVIDER_HPP_
#include "test_common.hpp"
#include "providers/i_provider.hpp"
#include "types/repertory.hpp"
namespace repertory {
class mock_provider : public virtual i_provider {
public:
mock_provider(bool allow_rename = true) : allow_rename_(allow_rename) {}
private:
const bool allow_rename_;
public:
MOCK_METHOD(api_error, create_directory,
(const std::string &api_path, api_meta_map &meta), (override));
MOCK_METHOD(api_error, create_directory_clone_source_meta,
(const std::string &source_api_path, const std::string &api_path),
(override));
MOCK_METHOD(api_error, create_file,
(const std::string &api_path, api_meta_map &meta), (override));
MOCK_METHOD(api_error, get_api_path_from_source,
(const std::string &source_path, std::string &api_path),
(const, override));
MOCK_METHOD(std::uint64_t, get_directory_item_count,
(const std::string &api_path), (const, override));
MOCK_METHOD(api_error, get_directory_items,
(const std::string &api_path, directory_item_list &list),
(const, override));
MOCK_METHOD(api_error, get_file,
(const std::string &api_path, api_file &file), (const, override));
MOCK_METHOD(api_error, get_file_list, (api_file_list & list),
(const, override));
MOCK_METHOD(api_error, get_file_size,
(const std::string &api_path, std::uint64_t &file_size),
(const, override));
MOCK_METHOD(api_error, get_filesystem_item,
(const std::string &api_path, bool directory,
filesystem_item &fsi),
(const, override));
MOCK_METHOD(api_error, get_filesystem_item_and_file,
(const std::string &api_path, api_file &file,
filesystem_item &fsi),
(const, override));
MOCK_METHOD(api_error, get_filesystem_item_from_source_path,
(const std::string &source_path, filesystem_item &fsi),
(const, override));
MOCK_METHOD(api_error, get_item_meta,
(const std::string &api_path, api_meta_map &meta),
(const, override));
MOCK_METHOD(api_error, get_item_meta,
(const std::string &api_path, const std::string &key,
std::string &value),
(const, override));
MOCK_METHOD((std::vector<std::string>), get_pinned_files, (),
(const, override));
MOCK_METHOD(provider_type, get_provider_type, (), (const, override));
MOCK_METHOD(std::uint64_t, get_total_drive_space, (), (const, override));
MOCK_METHOD(std::uint64_t, get_total_item_count, (), (const, override));
MOCK_METHOD(std::uint64_t, get_used_drive_space, (), (const, override));
MOCK_METHOD(bool, is_direct_only, (), (const, override));
MOCK_METHOD(api_error, is_directory,
(const std::string &api_path, bool &exists), (const, override));
MOCK_METHOD(api_error, is_file, (const std::string &api_path, bool &exists),
(const, override));
bool is_file_writeable(const std::string & /* api_path */) const override {
return true;
}
MOCK_METHOD(bool, is_online, (), (const, override));
bool is_rename_supported() const override { return allow_rename_; }
MOCK_METHOD(api_error, read_file_bytes,
(const std::string &path, std::size_t size, std::uint64_t offset,
data_buffer &data, stop_type &stop_requested),
(override));
MOCK_METHOD(api_error, remove_directory, (const std::string &api_path),
(override));
MOCK_METHOD(api_error, remove_file, (const std::string &api_path),
(override));
MOCK_METHOD(api_error, remove_item_meta,
(const std::string &api_path, const std::string &key),
(override));
MOCK_METHOD(api_error, rename_file,
(const std::string &from_api_path,
const std::string &to_api_path),
(override));
MOCK_METHOD(api_error, set_item_meta,
(const std::string &api_path, const std::string &key,
const std::string &value),
(override));
MOCK_METHOD(api_error, set_item_meta,
(const std::string &api_path, const api_meta_map &meta),
(override));
MOCK_METHOD(bool, start,
(api_item_added_callback api_item_added, i_file_manager *fm),
(override));
MOCK_METHOD(void, stop, (), (override));
MOCK_METHOD(api_error, upload_file,
(const std::string &api_path, const std::string &source_path,
stop_type &stop_requested),
(override));
};
} // namespace repertory
#endif // TESTS_MOCKS_MOCK_PROVIDER_HPP_

View File

@@ -1,44 +1,44 @@
/*
Copyright <2018-2023> <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 TESTS_MOCKS_MOCK_UPLOAD_MANAGER_HPP_
#define TESTS_MOCKS_MOCK_UPLOAD_MANAGER_HPP_
#include "test_common.hpp"
#include "file_manager/i_upload_manager.hpp"
namespace repertory {
class mock_upload_manager : public i_upload_manager {
public:
MOCK_METHOD(void, queue_upload, (const i_open_file &o), (override));
MOCK_METHOD(void, remove_resume,
(const std::string &api_path, const std::string &source_path),
(override));
MOCK_METHOD(void, remove_upload, (const std::string &api_path), (override));
MOCK_METHOD(void, store_resume, (const i_open_file &o), (override));
};
} // namespace repertory
#endif // TESTS_MOCKS_MOCK_UPLOAD_MANAGER_HPP_
/*
Copyright <2018-2023> <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 TESTS_MOCKS_MOCK_UPLOAD_MANAGER_HPP_
#define TESTS_MOCKS_MOCK_UPLOAD_MANAGER_HPP_
#include "test_common.hpp"
#include "file_manager/i_upload_manager.hpp"
namespace repertory {
class mock_upload_manager : public i_upload_manager {
public:
MOCK_METHOD(void, queue_upload, (const i_open_file &o), (override));
MOCK_METHOD(void, remove_resume,
(const std::string &api_path, const std::string &source_path),
(override));
MOCK_METHOD(void, remove_upload, (const std::string &api_path), (override));
MOCK_METHOD(void, store_resume, (const i_open_file &o), (override));
};
} // namespace repertory
#endif // TESTS_MOCKS_MOCK_UPLOAD_MANAGER_HPP_

View File

@@ -1,168 +1,168 @@
/*
Copyright <2018-2023> <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 TESTS_MOCKS_MOCK_WINFSP_DRIVE_HPP_
#define TESTS_MOCKS_MOCK_WINFSP_DRIVE_HPP_
#ifdef _WIN32
#include "test_common.hpp"
#include "drives/winfsp/i_winfsp_drive.hpp"
#include "utils/file_utils.hpp"
#include "utils/path_utils.hpp"
#include "utils/utils.hpp"
namespace repertory {
class mock_winfsp_drive final : public virtual i_winfsp_drive {
public:
explicit mock_winfsp_drive(std::string mount_location)
: mount_location_(std::move(mount_location)) {}
private:
const std::string mount_location_;
public:
[[nodiscard]] auto
get_directory_item_count(const std::string & /*api_path*/) const
-> std::uint64_t override {
return 1;
}
[[nodiscard]] auto get_directory_items(const std::string & /*api_path*/) const
-> directory_item_list override {
directory_item_list list{};
directory_item di{};
di.api_path = ".";
di.directory = true;
di.size = 0u;
di.meta = {{META_ATTRIBUTES, "16"},
{META_MODIFIED, std::to_string(utils::get_file_time_now())},
{META_WRITTEN, std::to_string(utils::get_file_time_now())},
{META_ACCESSED, std::to_string(utils::get_file_time_now())},
{META_CREATION, std::to_string(utils::get_file_time_now())}};
list.emplace_back(di);
di.api_path = "..";
list.emplace_back(di);
return list;
}
[[nodiscard]] auto get_file_size(const std::string & /*api_path*/) const
-> std::uint64_t override {
return 0;
}
auto get_item_meta(const std::string & /*api_path*/, api_meta_map &meta) const
-> api_error override {
return api_error::error;
}
auto get_item_meta(const std::string & /*api_path*/,
const std::string & /*name*/,
std::string & /*value*/) const -> api_error override {
return api_error::error;
}
auto get_security_by_name(PWSTR /*file_name*/, PUINT32 attributes,
PSECURITY_DESCRIPTOR descriptor,
std::uint64_t *descriptor_size)
-> NTSTATUS override {
auto ret = STATUS_SUCCESS;
if (attributes) {
*attributes = FILE_ATTRIBUTE_NORMAL;
}
if (descriptor_size) {
ULONG sz = 0;
PSECURITY_DESCRIPTOR sd = nullptr;
if (::ConvertStringSecurityDescriptorToSecurityDescriptor(
"O:BAG:BAD:P(A;;FA;;;SY)(A;;FA;;;BA)(A;;FA;;;WD)",
SDDL_REVISION_1, &sd, &sz)) {
if (sz > *descriptor_size) {
ret = STATUS_BUFFER_TOO_SMALL;
} else {
::CopyMemory(descriptor, sd, sz);
}
*descriptor_size = sz;
::LocalFree(sd);
} else {
ret = FspNtStatusFromWin32(::GetLastError());
}
}
return ret;
}
[[nodiscard]] auto get_total_drive_space() const -> std::uint64_t override {
return 100 * 1024 * 1024;
}
[[nodiscard]] auto get_total_item_count() const -> std::uint64_t override {
return 0;
}
[[nodiscard]] auto get_used_drive_space() const -> std::uint64_t override {
return 0;
}
void get_volume_info(UINT64 &total_size, UINT64 &free_size,
std::string &volume_label) const override {
free_size = 100;
total_size = 200;
volume_label = "TestVolumeLabel";
}
auto populate_file_info(const std::string &api_path,
remote::file_info &file_info) -> api_error override {
const auto file_path = utils::path::combine(mount_location_, {api_path});
const auto directory = utils::file::is_directory(file_path);
const auto attributes =
FILE_FLAG_BACKUP_SEMANTICS |
(directory ? FILE_ATTRIBUTE_DIRECTORY : FILE_ATTRIBUTE_NORMAL);
const auto share_mode = FILE_SHARE_READ | FILE_SHARE_WRITE;
auto handle = ::CreateFileA(&file_path[0], GENERIC_READ, share_mode,
nullptr, OPEN_EXISTING, attributes, nullptr);
FILE_BASIC_INFO fi{};
::GetFileInformationByHandleEx(handle, FileBasicInfo, &fi, sizeof(fi));
if (not directory) {
utils::file::get_file_size(file_path, file_info.FileSize);
}
file_info.AllocationSize =
directory ? 0
: utils::divide_with_ceiling(file_info.FileSize,
WINFSP_ALLOCATION_UNIT) *
WINFSP_ALLOCATION_UNIT;
file_info.FileAttributes = fi.FileAttributes;
file_info.ChangeTime = fi.ChangeTime.QuadPart;
file_info.CreationTime = fi.CreationTime.QuadPart;
file_info.LastAccessTime = fi.LastAccessTime.QuadPart;
file_info.LastWriteTime = fi.LastWriteTime.QuadPart;
::CloseHandle(handle);
return api_error::success;
}
};
} // namespace repertory
#endif // _WIN32
#endif // TESTS_MOCKS_MOCK_WINFSP_DRIVE_HPP_
/*
Copyright <2018-2023> <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 TESTS_MOCKS_MOCK_WINFSP_DRIVE_HPP_
#define TESTS_MOCKS_MOCK_WINFSP_DRIVE_HPP_
#ifdef _WIN32
#include "test_common.hpp"
#include "drives/winfsp/i_winfsp_drive.hpp"
#include "utils/file_utils.hpp"
#include "utils/path_utils.hpp"
#include "utils/utils.hpp"
namespace repertory {
class mock_winfsp_drive final : public virtual i_winfsp_drive {
public:
explicit mock_winfsp_drive(std::string mount_location)
: mount_location_(std::move(mount_location)) {}
private:
const std::string mount_location_;
public:
[[nodiscard]] auto
get_directory_item_count(const std::string & /*api_path*/) const
-> std::uint64_t override {
return 1;
}
[[nodiscard]] auto get_directory_items(const std::string & /*api_path*/) const
-> directory_item_list override {
directory_item_list list{};
directory_item di{};
di.api_path = ".";
di.directory = true;
di.size = 0u;
di.meta = {{META_ATTRIBUTES, "16"},
{META_MODIFIED, std::to_string(utils::get_file_time_now())},
{META_WRITTEN, std::to_string(utils::get_file_time_now())},
{META_ACCESSED, std::to_string(utils::get_file_time_now())},
{META_CREATION, std::to_string(utils::get_file_time_now())}};
list.emplace_back(di);
di.api_path = "..";
list.emplace_back(di);
return list;
}
[[nodiscard]] auto get_file_size(const std::string & /*api_path*/) const
-> std::uint64_t override {
return 0;
}
auto get_item_meta(const std::string & /*api_path*/, api_meta_map &meta) const
-> api_error override {
return api_error::error;
}
auto get_item_meta(const std::string & /*api_path*/,
const std::string & /*name*/,
std::string & /*value*/) const -> api_error override {
return api_error::error;
}
auto get_security_by_name(PWSTR /*file_name*/, PUINT32 attributes,
PSECURITY_DESCRIPTOR descriptor,
std::uint64_t *descriptor_size)
-> NTSTATUS override {
auto ret = STATUS_SUCCESS;
if (attributes) {
*attributes = FILE_ATTRIBUTE_NORMAL;
}
if (descriptor_size) {
ULONG sz = 0;
PSECURITY_DESCRIPTOR sd = nullptr;
if (::ConvertStringSecurityDescriptorToSecurityDescriptor(
"O:BAG:BAD:P(A;;FA;;;SY)(A;;FA;;;BA)(A;;FA;;;WD)",
SDDL_REVISION_1, &sd, &sz)) {
if (sz > *descriptor_size) {
ret = STATUS_BUFFER_TOO_SMALL;
} else {
::CopyMemory(descriptor, sd, sz);
}
*descriptor_size = sz;
::LocalFree(sd);
} else {
ret = FspNtStatusFromWin32(::GetLastError());
}
}
return ret;
}
[[nodiscard]] auto get_total_drive_space() const -> std::uint64_t override {
return 100 * 1024 * 1024;
}
[[nodiscard]] auto get_total_item_count() const -> std::uint64_t override {
return 0;
}
[[nodiscard]] auto get_used_drive_space() const -> std::uint64_t override {
return 0;
}
void get_volume_info(UINT64 &total_size, UINT64 &free_size,
std::string &volume_label) const override {
free_size = 100;
total_size = 200;
volume_label = "TestVolumeLabel";
}
auto populate_file_info(const std::string &api_path,
remote::file_info &file_info) -> api_error override {
const auto file_path = utils::path::combine(mount_location_, {api_path});
const auto directory = utils::file::is_directory(file_path);
const auto attributes =
FILE_FLAG_BACKUP_SEMANTICS |
(directory ? FILE_ATTRIBUTE_DIRECTORY : FILE_ATTRIBUTE_NORMAL);
const auto share_mode = FILE_SHARE_READ | FILE_SHARE_WRITE;
auto handle = ::CreateFileA(&file_path[0], GENERIC_READ, share_mode,
nullptr, OPEN_EXISTING, attributes, nullptr);
FILE_BASIC_INFO fi{};
::GetFileInformationByHandleEx(handle, FileBasicInfo, &fi, sizeof(fi));
if (not directory) {
utils::file::get_file_size(file_path, file_info.FileSize);
}
file_info.AllocationSize =
directory ? 0
: utils::divide_with_ceiling(file_info.FileSize,
WINFSP_ALLOCATION_UNIT) *
WINFSP_ALLOCATION_UNIT;
file_info.FileAttributes = fi.FileAttributes;
file_info.ChangeTime = fi.ChangeTime.QuadPart;
file_info.CreationTime = fi.CreationTime.QuadPart;
file_info.LastAccessTime = fi.LastAccessTime.QuadPart;
file_info.LastWriteTime = fi.LastWriteTime.QuadPart;
::CloseHandle(handle);
return api_error::success;
}
};
} // namespace repertory
#endif // _WIN32
#endif // TESTS_MOCKS_MOCK_WINFSP_DRIVE_HPP_

View File

@@ -1,41 +1,41 @@
/*
Copyright <2018-2023> <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_common.hpp"
#include "comm/packet/packet.hpp"
namespace repertory {
TEST(packet, encrypt_and_decrypt) {
packet test_packet;
test_packet.encode("test");
test_packet.encrypt("moose");
std::uint32_t size{};
EXPECT_EQ(0, test_packet.decode(size));
EXPECT_EQ(0, test_packet.decrypt("moose"));
std::string data;
EXPECT_EQ(0, test_packet.decode(data));
EXPECT_STREQ("test", data.c_str());
}
} // namespace repertory
/*
Copyright <2018-2023> <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_common.hpp"
#include "comm/packet/packet.hpp"
namespace repertory {
TEST(packet, encrypt_and_decrypt) {
packet test_packet;
test_packet.encode("test");
test_packet.encrypt("moose");
std::uint32_t size{};
EXPECT_EQ(0, test_packet.decode(size));
EXPECT_EQ(0, test_packet.decrypt("moose"));
std::string data;
EXPECT_EQ(0, test_packet.decode(data));
EXPECT_STREQ("test", data.c_str());
}
} // namespace repertory

View File

@@ -1,133 +1,133 @@
/*
Copyright <2018-2023> <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_common.hpp"
#include "utils/path_utils.hpp"
namespace repertory {
TEST(path_utils, combine) {
auto s = utils::path::combine(R"(\test\path)", {});
#ifdef _WIN32
EXPECT_STREQ(R"(\test\path)", s.c_str());
#else
EXPECT_STREQ("/test/path", s.c_str());
#endif
s = utils::path::combine(R"(\test)", {R"(\path)"});
#ifdef _WIN32
EXPECT_STREQ(R"(\test\path)", s.c_str());
#else
EXPECT_STREQ("/test/path", s.c_str());
#endif
s = utils::path::combine(R"(\test)", {R"(\path)", R"(\again\)"});
#ifdef _WIN32
EXPECT_STREQ(R"(\test\path\again)", s.c_str());
#else
EXPECT_STREQ("/test/path/again", s.c_str());
#endif
}
TEST(path_utils, 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(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());
}
TEST(path_utils, finalize) {
auto s = utils::path::finalize("");
EXPECT_STREQ("", s.c_str());
s = utils::path::finalize(R"(\)");
#ifdef _WIN32
EXPECT_STREQ(R"(\)", s.c_str());
#else
EXPECT_STREQ("/", s.c_str());
#endif
s = utils::path::finalize("/");
#ifdef _WIN32
EXPECT_STREQ(R"(\)", s.c_str());
#else
EXPECT_STREQ("/", s.c_str());
#endif
s = utils::path::finalize(R"(\\)");
#ifdef _WIN32
EXPECT_STREQ(R"(\)", s.c_str());
#else
EXPECT_STREQ("/", s.c_str());
#endif
s = utils::path::finalize("//");
#ifdef _WIN32
EXPECT_STREQ(R"(\)", s.c_str());
#else
EXPECT_STREQ("/", s.c_str());
#endif
s = utils::path::finalize("/cow///moose/////dog/chicken");
#ifdef _WIN32
EXPECT_STREQ(R"(\cow\moose\dog\chicken)", s.c_str());
#else
EXPECT_STREQ("/cow/moose/dog/chicken", s.c_str());
#endif
s = utils::path::finalize("\\cow\\\\\\moose\\\\\\\\dog\\chicken/");
#ifdef _WIN32
EXPECT_STREQ(R"(\cow\moose\dog\chicken)", s.c_str());
#else
EXPECT_STREQ("/cow/moose/dog/chicken", s.c_str());
#endif
s = utils::path::finalize("/cow\\\\/moose\\\\/\\dog\\chicken\\");
#ifdef _WIN32
EXPECT_STREQ(R"(\cow\moose\dog\chicken)", s.c_str());
#else
EXPECT_STREQ("/cow/moose/dog/chicken", s.c_str());
#endif
}
} // namespace repertory
/*
Copyright <2018-2023> <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_common.hpp"
#include "utils/path_utils.hpp"
namespace repertory {
TEST(path_utils, combine) {
auto s = utils::path::combine(R"(\test\path)", {});
#ifdef _WIN32
EXPECT_STREQ(R"(\test\path)", s.c_str());
#else
EXPECT_STREQ("/test/path", s.c_str());
#endif
s = utils::path::combine(R"(\test)", {R"(\path)"});
#ifdef _WIN32
EXPECT_STREQ(R"(\test\path)", s.c_str());
#else
EXPECT_STREQ("/test/path", s.c_str());
#endif
s = utils::path::combine(R"(\test)", {R"(\path)", R"(\again\)"});
#ifdef _WIN32
EXPECT_STREQ(R"(\test\path\again)", s.c_str());
#else
EXPECT_STREQ("/test/path/again", s.c_str());
#endif
}
TEST(path_utils, 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(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());
}
TEST(path_utils, finalize) {
auto s = utils::path::finalize("");
EXPECT_STREQ("", s.c_str());
s = utils::path::finalize(R"(\)");
#ifdef _WIN32
EXPECT_STREQ(R"(\)", s.c_str());
#else
EXPECT_STREQ("/", s.c_str());
#endif
s = utils::path::finalize("/");
#ifdef _WIN32
EXPECT_STREQ(R"(\)", s.c_str());
#else
EXPECT_STREQ("/", s.c_str());
#endif
s = utils::path::finalize(R"(\\)");
#ifdef _WIN32
EXPECT_STREQ(R"(\)", s.c_str());
#else
EXPECT_STREQ("/", s.c_str());
#endif
s = utils::path::finalize("//");
#ifdef _WIN32
EXPECT_STREQ(R"(\)", s.c_str());
#else
EXPECT_STREQ("/", s.c_str());
#endif
s = utils::path::finalize("/cow///moose/////dog/chicken");
#ifdef _WIN32
EXPECT_STREQ(R"(\cow\moose\dog\chicken)", s.c_str());
#else
EXPECT_STREQ("/cow/moose/dog/chicken", s.c_str());
#endif
s = utils::path::finalize("\\cow\\\\\\moose\\\\\\\\dog\\chicken/");
#ifdef _WIN32
EXPECT_STREQ(R"(\cow\moose\dog\chicken)", s.c_str());
#else
EXPECT_STREQ("/cow/moose/dog/chicken", s.c_str());
#endif
s = utils::path::finalize("/cow\\\\/moose\\\\/\\dog\\chicken\\");
#ifdef _WIN32
EXPECT_STREQ(R"(\cow\moose\dog\chicken)", s.c_str());
#else
EXPECT_STREQ("/cow/moose/dog/chicken", s.c_str());
#endif
}
} // namespace repertory

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1,326 +1,326 @@
/*
Copyright <2018-2023> <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_common.hpp"
#include "app_config.hpp"
#include "comm/curl/curl_comm.hpp"
#include "events/consumers/console_consumer.hpp"
#include "events/event_system.hpp"
#include "file_manager/file_manager.hpp"
#include "platform/platform.hpp"
#include "providers/sia/sia_provider.hpp"
#include "types/startup_exception.hpp"
#include "utils/path_utils.hpp"
#include "utils/utils.hpp"
namespace repertory {
/* TEST(sia_provider, can_construct_sia_provider) {
app_config cfg(provider_type::sia,
utils::path::combine(get_test_dir(), {"sia"}));
curl_comm comm(cfg.get_host_config());
sia_provider provider(cfg, comm);
EXPECT_EQ(provider_type::sia, provider.get_provider_type());
EXPECT_FALSE(provider.is_direct_only());
EXPECT_FALSE(provider.is_rename_supported());
}
TEST(sia_provider, can_create_and_remove_directory) {
console_consumer cc{};
event_system::instance().start();
app_config cfg(provider_type::sia,
utils::path::combine(get_test_dir(), {"sia"}));
curl_comm comm(cfg.get_host_config());
sia_provider sia(cfg, comm);
i_provider &provider = sia;
EXPECT_TRUE(provider.start(
[&](bool directory, api_file &file) -> api_error {
return provider_meta_handler(provider, directory, file);
},
nullptr));
api_meta_map meta{};
meta[META_ACCESSED] = std::to_string(utils::get_file_time_now());
meta[META_CHANGED] = std::to_string(utils::get_file_time_now());
meta[META_CREATION] = std::to_string(utils::get_file_time_now());
meta[META_MODIFIED] = std::to_string(utils::get_file_time_now());
EXPECT_EQ(api_error::success, provider.create_directory("/moose2", meta));
bool exists{};
EXPECT_EQ(api_error::success, provider.is_directory("/moose2", exists));
EXPECT_TRUE(exists);
EXPECT_EQ(api_error::success, provider.is_file("/moose2", exists));
EXPECT_FALSE(exists);
EXPECT_FALSE(provider.is_file_writeable("/moose2"));
EXPECT_EQ(api_error::directory_exists, provider.remove_file("/moose2"));
EXPECT_EQ(api_error::success, provider.remove_directory("/moose2"));
api_meta_map m{};
EXPECT_EQ(api_error::item_not_found, provider.get_item_meta("/moose2", m));
EXPECT_TRUE(m.empty());
provider.stop();
event_system::instance().stop();
}
TEST(sia_provider, can_create_and_remove_file) {
console_consumer cc{};
event_system::instance().start();
app_config cfg(provider_type::sia,
utils::path::combine(get_test_dir(), {"sia"}));
curl_comm comm(cfg.get_host_config());
sia_provider sia(cfg, comm);
i_provider &provider = sia;
EXPECT_TRUE(provider.start(
[&](bool directory, api_file &file) -> api_error {
return provider_meta_handler(provider, directory, file);
},
nullptr));
api_meta_map meta{};
meta[META_ACCESSED] = std::to_string(utils::get_file_time_now());
meta[META_CHANGED] = std::to_string(utils::get_file_time_now());
meta[META_CREATION] = std::to_string(utils::get_file_time_now());
meta[META_MODIFIED] = std::to_string(utils::get_file_time_now());
meta[META_SIZE] = "0";
EXPECT_EQ(api_error::success, provider.create_file("/moose.txt", meta));
bool exists{};
EXPECT_EQ(api_error::success, provider.is_file("/moose.txt", exists));
EXPECT_TRUE(exists);
EXPECT_EQ(api_error::success, provider.is_directory("/moose.txt", exists));
EXPECT_FALSE(exists);
EXPECT_TRUE(provider.is_file_writeable("/moose.txt"));
EXPECT_EQ(api_error::item_not_found, provider.remove_directory("/moose.txt"));
EXPECT_EQ(api_error::success, provider.remove_file("/moose.txt"));
api_meta_map m{};
EXPECT_EQ(api_error::item_not_found, provider.get_item_meta("/moose.txt", m));
EXPECT_TRUE(m.empty());
provider.stop();
event_system::instance().stop();
}
TEST(sia_provider, can_get_file_list) {
console_consumer cc{};
event_system::instance().start();
app_config cfg(provider_type::sia,
utils::path::combine(get_test_dir(), {"sia"}));
curl_comm comm(cfg.get_host_config());
sia_provider sia(cfg, comm);
i_provider &provider = sia;
EXPECT_TRUE(provider.start(
[&](bool directory, api_file &file) -> api_error {
return provider_meta_handler(provider, directory, file);
},
nullptr));
api_file_list list{};
EXPECT_EQ(api_error::success, provider.get_file_list(list));
provider.stop();
event_system::instance().stop();
}
TEST(sia_provider, can_get_and_set_item_meta) {
console_consumer cc{};
event_system::instance().start();
app_config cfg(provider_type::sia,
utils::path::combine(get_test_dir(), {"sia"}));
curl_comm comm(cfg.get_host_config());
sia_provider sia(cfg, comm);
i_provider &provider = sia;
EXPECT_TRUE(provider.start(
[&](bool directory, api_file &file) -> api_error {
return provider_meta_handler(provider, directory, file);
},
nullptr));
api_meta_map meta{};
meta[META_ACCESSED] = std::to_string(utils::get_file_time_now());
meta[META_CHANGED] = std::to_string(utils::get_file_time_now());
meta[META_CREATION] = std::to_string(utils::get_file_time_now());
meta[META_MODIFIED] = std::to_string(utils::get_file_time_now());
EXPECT_EQ(api_error::success, provider.create_directory("/moose2", meta));
EXPECT_EQ(api_error::success, provider.create_file("/moose2.txt", meta));
{
api_meta_map m{
{"test_one", "one"},
{"test_two", "two"},
};
EXPECT_EQ(api_error::success, provider.set_item_meta("/moose2", m));
api_meta_map m2{};
EXPECT_EQ(api_error::success, provider.get_item_meta("/moose2", m2));
EXPECT_EQ(meta.size() + m.size(), m2.size());
EXPECT_STREQ("one", m2["test_one"].c_str());
EXPECT_STREQ("two", m2["test_two"].c_str());
}
{
api_meta_map m{
{"test_one", "one1"},
{"test_two", "two2"},
};
EXPECT_EQ(api_error::success, provider.set_item_meta("/moose2.txt", m));
api_meta_map m2{};
EXPECT_EQ(api_error::success, provider.get_item_meta("/moose2.txt", m2));
EXPECT_EQ(meta.size() + m.size(), m2.size());
EXPECT_STREQ("one1", m2["test_one"].c_str());
EXPECT_STREQ("two2", m2["test_two"].c_str());
}
EXPECT_EQ(api_error::success, provider.remove_directory("/moose2"));
EXPECT_EQ(api_error::success, provider.remove_file("/moose2.txt"));
provider.stop();
event_system::instance().stop();
}
TEST(sia_provider, can_get_and_set_individual_item_meta) {
console_consumer cc{};
event_system::instance().start();
app_config cfg(provider_type::sia,
utils::path::combine(get_test_dir(), {"sia"}));
curl_comm comm(cfg.get_host_config());
sia_provider sia(cfg, comm);
i_provider &provider = sia;
EXPECT_TRUE(provider.start(
[&](bool directory, api_file &file) -> api_error {
return provider_meta_handler(provider, directory, file);
},
nullptr));
api_meta_map meta{};
meta[META_ACCESSED] = std::to_string(utils::get_file_time_now());
meta[META_CHANGED] = std::to_string(utils::get_file_time_now());
meta[META_CREATION] = std::to_string(utils::get_file_time_now());
meta[META_MODIFIED] = std::to_string(utils::get_file_time_now());
EXPECT_EQ(api_error::success, provider.create_directory("/moose2", meta));
EXPECT_EQ(api_error::success, provider.create_file("/moose2.txt", meta));
{
EXPECT_EQ(api_error::success,
provider.set_item_meta("/moose2", "test_meta", "cow2"));
std::string value{};
EXPECT_EQ(api_error::success,
provider.get_item_meta("/moose2", "test_meta", value));
EXPECT_STREQ("cow2", value.c_str());
}
{
EXPECT_EQ(api_error::success,
provider.set_item_meta("/moose2.txt", "test_meta", "cow"));
std::string value{};
EXPECT_EQ(api_error::success,
provider.get_item_meta("/moose2.txt", "test_meta", value));
EXPECT_STREQ("cow", value.c_str());
}
EXPECT_EQ(api_error::success, provider.remove_directory("/moose2"));
EXPECT_EQ(api_error::success, provider.remove_file("/moose2.txt"));
provider.stop();
event_system::instance().stop();
}
TEST(sia_provider, can_read_file_bytes) {
console_consumer cc{};
event_system::instance().start();
app_config cfg(provider_type::sia,
utils::path::combine(get_test_dir(), {"sia"}));
curl_comm comm(cfg.get_host_config());
sia_provider sia(cfg, comm);
i_provider &provider = sia;
EXPECT_TRUE(provider.start(
[&](bool directory, api_file &file) -> api_error {
return provider_meta_handler(provider, directory, file);
},
nullptr));
data_buffer data{};
stop_type stop_requested = false;
EXPECT_EQ(api_error::success,
provider.read_file_bytes("/renterd_linux_amd64.zip", 10U, 0U, data,
stop_requested));
EXPECT_EQ(10U, data.size());
provider.stop();
event_system::instance().stop();
}
TEST(sia_provider, can_upload_file) {
console_consumer cc{};
event_system::instance().start();
app_config cfg(provider_type::sia,
utils::path::combine(get_test_dir(), {"sia"}));
curl_comm comm(cfg.get_host_config());
sia_provider sia(cfg, comm);
i_provider &provider = sia;
EXPECT_TRUE(provider.start(
[&](bool directory, api_file &file) -> api_error {
return provider_meta_handler(provider, directory, file);
},
nullptr));
stop_type stop_requested = false;
EXPECT_EQ(api_error::success,
provider.upload_file("/sia_provider_test.cpp", __FILE__, "",
stop_requested));
api_meta_map meta = {{"test", "test"}};
EXPECT_EQ(api_error::success,
provider.set_item_meta("/sia_provider_test.cpp", meta));
EXPECT_EQ(api_error::success, provider.remove_file("/sia_provider_test.cpp"));
provider.stop();
event_system::instance().stop();
} */
} // namespace repertory
/*
Copyright <2018-2023> <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_common.hpp"
#include "app_config.hpp"
#include "comm/curl/curl_comm.hpp"
#include "events/consumers/console_consumer.hpp"
#include "events/event_system.hpp"
#include "file_manager/file_manager.hpp"
#include "platform/platform.hpp"
#include "providers/sia/sia_provider.hpp"
#include "types/startup_exception.hpp"
#include "utils/path_utils.hpp"
#include "utils/utils.hpp"
namespace repertory {
/* TEST(sia_provider, can_construct_sia_provider) {
app_config cfg(provider_type::sia,
utils::path::combine(get_test_dir(), {"sia"}));
curl_comm comm(cfg.get_host_config());
sia_provider provider(cfg, comm);
EXPECT_EQ(provider_type::sia, provider.get_provider_type());
EXPECT_FALSE(provider.is_direct_only());
EXPECT_FALSE(provider.is_rename_supported());
}
TEST(sia_provider, can_create_and_remove_directory) {
console_consumer cc{};
event_system::instance().start();
app_config cfg(provider_type::sia,
utils::path::combine(get_test_dir(), {"sia"}));
curl_comm comm(cfg.get_host_config());
sia_provider sia(cfg, comm);
i_provider &provider = sia;
EXPECT_TRUE(provider.start(
[&](bool directory, api_file &file) -> api_error {
return provider_meta_handler(provider, directory, file);
},
nullptr));
api_meta_map meta{};
meta[META_ACCESSED] = std::to_string(utils::get_file_time_now());
meta[META_CHANGED] = std::to_string(utils::get_file_time_now());
meta[META_CREATION] = std::to_string(utils::get_file_time_now());
meta[META_MODIFIED] = std::to_string(utils::get_file_time_now());
EXPECT_EQ(api_error::success, provider.create_directory("/moose2", meta));
bool exists{};
EXPECT_EQ(api_error::success, provider.is_directory("/moose2", exists));
EXPECT_TRUE(exists);
EXPECT_EQ(api_error::success, provider.is_file("/moose2", exists));
EXPECT_FALSE(exists);
EXPECT_FALSE(provider.is_file_writeable("/moose2"));
EXPECT_EQ(api_error::directory_exists, provider.remove_file("/moose2"));
EXPECT_EQ(api_error::success, provider.remove_directory("/moose2"));
api_meta_map m{};
EXPECT_EQ(api_error::item_not_found, provider.get_item_meta("/moose2", m));
EXPECT_TRUE(m.empty());
provider.stop();
event_system::instance().stop();
}
TEST(sia_provider, can_create_and_remove_file) {
console_consumer cc{};
event_system::instance().start();
app_config cfg(provider_type::sia,
utils::path::combine(get_test_dir(), {"sia"}));
curl_comm comm(cfg.get_host_config());
sia_provider sia(cfg, comm);
i_provider &provider = sia;
EXPECT_TRUE(provider.start(
[&](bool directory, api_file &file) -> api_error {
return provider_meta_handler(provider, directory, file);
},
nullptr));
api_meta_map meta{};
meta[META_ACCESSED] = std::to_string(utils::get_file_time_now());
meta[META_CHANGED] = std::to_string(utils::get_file_time_now());
meta[META_CREATION] = std::to_string(utils::get_file_time_now());
meta[META_MODIFIED] = std::to_string(utils::get_file_time_now());
meta[META_SIZE] = "0";
EXPECT_EQ(api_error::success, provider.create_file("/moose.txt", meta));
bool exists{};
EXPECT_EQ(api_error::success, provider.is_file("/moose.txt", exists));
EXPECT_TRUE(exists);
EXPECT_EQ(api_error::success, provider.is_directory("/moose.txt", exists));
EXPECT_FALSE(exists);
EXPECT_TRUE(provider.is_file_writeable("/moose.txt"));
EXPECT_EQ(api_error::item_not_found, provider.remove_directory("/moose.txt"));
EXPECT_EQ(api_error::success, provider.remove_file("/moose.txt"));
api_meta_map m{};
EXPECT_EQ(api_error::item_not_found, provider.get_item_meta("/moose.txt", m));
EXPECT_TRUE(m.empty());
provider.stop();
event_system::instance().stop();
}
TEST(sia_provider, can_get_file_list) {
console_consumer cc{};
event_system::instance().start();
app_config cfg(provider_type::sia,
utils::path::combine(get_test_dir(), {"sia"}));
curl_comm comm(cfg.get_host_config());
sia_provider sia(cfg, comm);
i_provider &provider = sia;
EXPECT_TRUE(provider.start(
[&](bool directory, api_file &file) -> api_error {
return provider_meta_handler(provider, directory, file);
},
nullptr));
api_file_list list{};
EXPECT_EQ(api_error::success, provider.get_file_list(list));
provider.stop();
event_system::instance().stop();
}
TEST(sia_provider, can_get_and_set_item_meta) {
console_consumer cc{};
event_system::instance().start();
app_config cfg(provider_type::sia,
utils::path::combine(get_test_dir(), {"sia"}));
curl_comm comm(cfg.get_host_config());
sia_provider sia(cfg, comm);
i_provider &provider = sia;
EXPECT_TRUE(provider.start(
[&](bool directory, api_file &file) -> api_error {
return provider_meta_handler(provider, directory, file);
},
nullptr));
api_meta_map meta{};
meta[META_ACCESSED] = std::to_string(utils::get_file_time_now());
meta[META_CHANGED] = std::to_string(utils::get_file_time_now());
meta[META_CREATION] = std::to_string(utils::get_file_time_now());
meta[META_MODIFIED] = std::to_string(utils::get_file_time_now());
EXPECT_EQ(api_error::success, provider.create_directory("/moose2", meta));
EXPECT_EQ(api_error::success, provider.create_file("/moose2.txt", meta));
{
api_meta_map m{
{"test_one", "one"},
{"test_two", "two"},
};
EXPECT_EQ(api_error::success, provider.set_item_meta("/moose2", m));
api_meta_map m2{};
EXPECT_EQ(api_error::success, provider.get_item_meta("/moose2", m2));
EXPECT_EQ(meta.size() + m.size(), m2.size());
EXPECT_STREQ("one", m2["test_one"].c_str());
EXPECT_STREQ("two", m2["test_two"].c_str());
}
{
api_meta_map m{
{"test_one", "one1"},
{"test_two", "two2"},
};
EXPECT_EQ(api_error::success, provider.set_item_meta("/moose2.txt", m));
api_meta_map m2{};
EXPECT_EQ(api_error::success, provider.get_item_meta("/moose2.txt", m2));
EXPECT_EQ(meta.size() + m.size(), m2.size());
EXPECT_STREQ("one1", m2["test_one"].c_str());
EXPECT_STREQ("two2", m2["test_two"].c_str());
}
EXPECT_EQ(api_error::success, provider.remove_directory("/moose2"));
EXPECT_EQ(api_error::success, provider.remove_file("/moose2.txt"));
provider.stop();
event_system::instance().stop();
}
TEST(sia_provider, can_get_and_set_individual_item_meta) {
console_consumer cc{};
event_system::instance().start();
app_config cfg(provider_type::sia,
utils::path::combine(get_test_dir(), {"sia"}));
curl_comm comm(cfg.get_host_config());
sia_provider sia(cfg, comm);
i_provider &provider = sia;
EXPECT_TRUE(provider.start(
[&](bool directory, api_file &file) -> api_error {
return provider_meta_handler(provider, directory, file);
},
nullptr));
api_meta_map meta{};
meta[META_ACCESSED] = std::to_string(utils::get_file_time_now());
meta[META_CHANGED] = std::to_string(utils::get_file_time_now());
meta[META_CREATION] = std::to_string(utils::get_file_time_now());
meta[META_MODIFIED] = std::to_string(utils::get_file_time_now());
EXPECT_EQ(api_error::success, provider.create_directory("/moose2", meta));
EXPECT_EQ(api_error::success, provider.create_file("/moose2.txt", meta));
{
EXPECT_EQ(api_error::success,
provider.set_item_meta("/moose2", "test_meta", "cow2"));
std::string value{};
EXPECT_EQ(api_error::success,
provider.get_item_meta("/moose2", "test_meta", value));
EXPECT_STREQ("cow2", value.c_str());
}
{
EXPECT_EQ(api_error::success,
provider.set_item_meta("/moose2.txt", "test_meta", "cow"));
std::string value{};
EXPECT_EQ(api_error::success,
provider.get_item_meta("/moose2.txt", "test_meta", value));
EXPECT_STREQ("cow", value.c_str());
}
EXPECT_EQ(api_error::success, provider.remove_directory("/moose2"));
EXPECT_EQ(api_error::success, provider.remove_file("/moose2.txt"));
provider.stop();
event_system::instance().stop();
}
TEST(sia_provider, can_read_file_bytes) {
console_consumer cc{};
event_system::instance().start();
app_config cfg(provider_type::sia,
utils::path::combine(get_test_dir(), {"sia"}));
curl_comm comm(cfg.get_host_config());
sia_provider sia(cfg, comm);
i_provider &provider = sia;
EXPECT_TRUE(provider.start(
[&](bool directory, api_file &file) -> api_error {
return provider_meta_handler(provider, directory, file);
},
nullptr));
data_buffer data{};
stop_type stop_requested = false;
EXPECT_EQ(api_error::success,
provider.read_file_bytes("/renterd_linux_amd64.zip", 10U, 0U, data,
stop_requested));
EXPECT_EQ(10U, data.size());
provider.stop();
event_system::instance().stop();
}
TEST(sia_provider, can_upload_file) {
console_consumer cc{};
event_system::instance().start();
app_config cfg(provider_type::sia,
utils::path::combine(get_test_dir(), {"sia"}));
curl_comm comm(cfg.get_host_config());
sia_provider sia(cfg, comm);
i_provider &provider = sia;
EXPECT_TRUE(provider.start(
[&](bool directory, api_file &file) -> api_error {
return provider_meta_handler(provider, directory, file);
},
nullptr));
stop_type stop_requested = false;
EXPECT_EQ(api_error::success,
provider.upload_file("/sia_provider_test.cpp", __FILE__, "",
stop_requested));
api_meta_map meta = {{"test", "test"}};
EXPECT_EQ(api_error::success,
provider.set_item_meta("/sia_provider_test.cpp", meta));
EXPECT_EQ(api_error::success, provider.remove_file("/sia_provider_test.cpp"));
provider.stop();
event_system::instance().stop();
} */
} // namespace repertory

View File

@@ -1,48 +1,48 @@
/*
Copyright <2018-2023> <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_common.hpp"
#include "utils/string_utils.hpp"
namespace repertory {
TEST(string_utils, 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(""));
}
} // namespace repertory
/*
Copyright <2018-2023> <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_common.hpp"
#include "utils/string_utils.hpp"
namespace repertory {
TEST(string_utils, 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(""));
}
} // namespace repertory

View File

@@ -1,80 +1,80 @@
/*
Copyright <2018-2023> <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_common.hpp"
#include "types/repertory.hpp"
#include "utils/path_utils.hpp"
#include "utils/utils.hpp"
namespace repertory {
static std::vector<std::string> generated_files;
void delete_generated_files() {
for (const auto &f : generated_files) {
EXPECT_TRUE(utils::file::retry_delete_file(f));
}
}
auto create_random_file(std::string path, std::size_t size) -> native_file_ptr {
native_file_ptr nf;
if (native_file::create_or_open(path, nf) == api_error::success) {
generated_files.emplace_back(utils::path::absolute(path));
EXPECT_TRUE(nf->truncate(0u));
data_buffer buf(size);
randombytes_buf(buf.data(), buf.size());
std::size_t bytes_written{};
EXPECT_TRUE(nf->write_bytes(&buf[0u], buf.size(), 0u, bytes_written));
nf->flush();
std::uint64_t current_size;
EXPECT_TRUE(utils::file::get_file_size(path, current_size));
EXPECT_EQ(size, current_size);
}
return nf;
}
auto generate_test_file_name(const std::string &directory,
const std::string &file_name_no_extension)
-> std::string {
static std::atomic<std::uint32_t> idx{0U};
auto path = utils::path::absolute(utils::path::combine(
directory, {file_name_no_extension + std::to_string(idx++) + ".dat"}));
generated_files.emplace_back(path);
return path;
}
auto get_test_dir() -> std::string {
auto dir = utils::get_environment_variable("REPERTORY_TEST_DIR");
if (not dir.empty()) {
return utils::path::absolute(dir);
}
#ifdef _WIN32
return utils::path::absolute("..\\..\\..");
#else
return utils::path::absolute("../..");
#endif
}
} // namespace repertory
/*
Copyright <2018-2023> <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_common.hpp"
#include "types/repertory.hpp"
#include "utils/path_utils.hpp"
#include "utils/utils.hpp"
namespace repertory {
static std::vector<std::string> generated_files;
void delete_generated_files() {
for (const auto &f : generated_files) {
EXPECT_TRUE(utils::file::retry_delete_file(f));
}
}
auto create_random_file(std::string path, std::size_t size) -> native_file_ptr {
native_file_ptr nf;
if (native_file::create_or_open(path, nf) == api_error::success) {
generated_files.emplace_back(utils::path::absolute(path));
EXPECT_TRUE(nf->truncate(0u));
data_buffer buf(size);
randombytes_buf(buf.data(), buf.size());
std::size_t bytes_written{};
EXPECT_TRUE(nf->write_bytes(&buf[0u], buf.size(), 0u, bytes_written));
nf->flush();
std::uint64_t current_size;
EXPECT_TRUE(utils::file::get_file_size(path, current_size));
EXPECT_EQ(size, current_size);
}
return nf;
}
auto generate_test_file_name(const std::string &directory,
const std::string &file_name_no_extension)
-> std::string {
static std::atomic<std::uint32_t> idx{0U};
auto path = utils::path::absolute(utils::path::combine(
directory, {file_name_no_extension + std::to_string(idx++) + ".dat"}));
generated_files.emplace_back(path);
return path;
}
auto get_test_dir() -> std::string {
auto dir = utils::get_environment_variable("REPERTORY_TEST_DIR");
if (not dir.empty()) {
return utils::path::absolute(dir);
}
#ifdef _WIN32
return utils::path::absolute("..\\..\\..");
#else
return utils::path::absolute("../..");
#endif
}
} // namespace repertory

View File

@@ -1,71 +1,71 @@
/*
Copyright <2018-2023> <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 TESTS_TEST_COMMON_HPP_
#define TESTS_TEST_COMMON_HPP_
#ifdef U
#undef U
#endif
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "events/consumers/console_consumer.hpp"
#include "events/event_system.hpp"
#include "events/events.hpp"
#ifdef _WIN32
// Disable DLL-interface warnings
#pragma warning(disable : 4251)
#pragma warning(disable : 4275)
#endif
#include "utils/encryption.hpp"
#include "utils/file_utils.hpp"
#include "utils/native_file.hpp"
#define COMMA ,
using ::testing::_;
using namespace ::testing;
namespace repertory {
[[nodiscard]] auto create_random_file(std::string path, std::size_t size)
-> native_file_ptr;
void delete_generated_files();
[[nodiscard]] auto
generate_test_file_name(const std::string &directory,
const std::string &file_name_no_extension)
-> std::string;
template <typename T, typename T2>
static void decrypt_and_verify(const T &buffer, const std::string &token,
T2 &result) {
EXPECT_TRUE(utils::encryption::decrypt_data(token, buffer, result));
}
[[nodiscard]] auto get_test_dir() -> std::string;
} // namespace repertory
#endif // TESTS_TEST_COMMON_HPP_
/*
Copyright <2018-2023> <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 TESTS_TEST_COMMON_HPP_
#define TESTS_TEST_COMMON_HPP_
#ifdef U
#undef U
#endif
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "events/consumers/console_consumer.hpp"
#include "events/event_system.hpp"
#include "events/events.hpp"
#ifdef _WIN32
// Disable DLL-interface warnings
#pragma warning(disable : 4251)
#pragma warning(disable : 4275)
#endif
#include "utils/encryption.hpp"
#include "utils/file_utils.hpp"
#include "utils/native_file.hpp"
#define COMMA ,
using ::testing::_;
using namespace ::testing;
namespace repertory {
[[nodiscard]] auto create_random_file(std::string path, std::size_t size)
-> native_file_ptr;
void delete_generated_files();
[[nodiscard]] auto
generate_test_file_name(const std::string &directory,
const std::string &file_name_no_extension)
-> std::string;
template <typename T, typename T2>
static void decrypt_and_verify(const T &buffer, const std::string &token,
T2 &result) {
EXPECT_TRUE(utils::encryption::decrypt_data(token, buffer, result));
}
[[nodiscard]] auto get_test_dir() -> std::string;
} // namespace repertory
#endif // TESTS_TEST_COMMON_HPP_

View File

@@ -1,108 +1,108 @@
/*
Copyright <2018-2023> <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 TESTS_UTILS_EVENT_CAPTURE_HPP_
#define TESTS_UTILS_EVENT_CAPTURE_HPP_
#include "test_common.hpp"
#include "utils/utils.hpp"
namespace repertory {
class event_capture final {
E_CONSUMER();
public:
explicit event_capture(std::vector<std::string> event_names,
std::vector<std::string> non_fired_event_names = {})
: event_names_(std::move(event_names)),
non_fired_event_names_(std::move(non_fired_event_names)) {
E_SUBSCRIBE_ALL(process_event);
}
~event_capture() {
wait_for_empty();
E_CONSUMER_RELEASE();
EXPECT_TRUE(event_names_.empty());
for (const auto &event_name : event_names_) {
std::cerr << '\t' << event_name << std::endl;
}
}
private:
std::vector<std::string> event_names_;
std::vector<std::string> fired_event_names_;
std::vector<std::string> non_fired_event_names_;
std::mutex mutex_;
std::condition_variable notify_;
public:
void process_event(const event &event) {
unique_mutex_lock l(mutex_);
utils::remove_element_from(event_names_, event.get_name());
fired_event_names_.emplace_back(event.get_name());
notify_.notify_all();
l.unlock();
for (size_t i = 0; i < non_fired_event_names_.size(); i++) {
const auto it = std::find(non_fired_event_names_.begin(),
non_fired_event_names_.end(), event.get_name());
EXPECT_EQ(non_fired_event_names_.end(), it);
if (it != non_fired_event_names_.end()) {
std::cerr << '\t' << *it << std::endl;
}
}
}
void wait_for_empty() {
const auto start_time = std::chrono::system_clock::now();
unique_mutex_lock l(mutex_);
while (not event_names_.empty() &&
(std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now() - start_time)
.count() < 10)) {
notify_.wait_for(l, 1s);
}
l.unlock();
}
[[nodiscard]] auto wait_for_event(const std::string &event_name) -> bool {
auto missing = true;
const auto start_time = std::chrono::system_clock::now();
unique_mutex_lock l(mutex_);
while ((std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now() - start_time)
.count() < 10) &&
(missing =
(std::find(fired_event_names_.begin(), fired_event_names_.end(),
event_name) == fired_event_names_.end()))) {
notify_.wait_for(l, 1s);
}
l.unlock();
return not missing;
}
};
} // namespace repertory
#endif // TESTS_UTILS_EVENT_CAPTURE_HPP_
/*
Copyright <2018-2023> <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 TESTS_UTILS_EVENT_CAPTURE_HPP_
#define TESTS_UTILS_EVENT_CAPTURE_HPP_
#include "test_common.hpp"
#include "utils/utils.hpp"
namespace repertory {
class event_capture final {
E_CONSUMER();
public:
explicit event_capture(std::vector<std::string> event_names,
std::vector<std::string> non_fired_event_names = {})
: event_names_(std::move(event_names)),
non_fired_event_names_(std::move(non_fired_event_names)) {
E_SUBSCRIBE_ALL(process_event);
}
~event_capture() {
wait_for_empty();
E_CONSUMER_RELEASE();
EXPECT_TRUE(event_names_.empty());
for (const auto &event_name : event_names_) {
std::cerr << '\t' << event_name << std::endl;
}
}
private:
std::vector<std::string> event_names_;
std::vector<std::string> fired_event_names_;
std::vector<std::string> non_fired_event_names_;
std::mutex mutex_;
std::condition_variable notify_;
public:
void process_event(const event &event) {
unique_mutex_lock l(mutex_);
utils::remove_element_from(event_names_, event.get_name());
fired_event_names_.emplace_back(event.get_name());
notify_.notify_all();
l.unlock();
for (size_t i = 0; i < non_fired_event_names_.size(); i++) {
const auto it = std::find(non_fired_event_names_.begin(),
non_fired_event_names_.end(), event.get_name());
EXPECT_EQ(non_fired_event_names_.end(), it);
if (it != non_fired_event_names_.end()) {
std::cerr << '\t' << *it << std::endl;
}
}
}
void wait_for_empty() {
const auto start_time = std::chrono::system_clock::now();
unique_mutex_lock l(mutex_);
while (not event_names_.empty() &&
(std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now() - start_time)
.count() < 10)) {
notify_.wait_for(l, 1s);
}
l.unlock();
}
[[nodiscard]] auto wait_for_event(const std::string &event_name) -> bool {
auto missing = true;
const auto start_time = std::chrono::system_clock::now();
unique_mutex_lock l(mutex_);
while ((std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now() - start_time)
.count() < 10) &&
(missing =
(std::find(fired_event_names_.begin(), fired_event_names_.end(),
event_name) == fired_event_names_.end()))) {
notify_.wait_for(l, 1s);
}
l.unlock();
return not missing;
}
};
} // namespace repertory
#endif // TESTS_UTILS_EVENT_CAPTURE_HPP_

View File

@@ -1,77 +1,77 @@
/*
Copyright <2018-2023> <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_common.hpp"
#include "utils/file_utils.hpp"
#include "utils/path_utils.hpp"
#include "utils/string_utils.hpp"
#include "utils/utils.hpp"
namespace repertory {
#ifdef _WIN32
TEST(utils, convert_api_date) {
LARGE_INTEGER li{};
li.QuadPart = utils::convert_api_date("2009-10-12T17:50:30.111Z");
SYSTEMTIME st{};
FileTimeToSystemTime(reinterpret_cast<FILETIME *>(&li), &st);
SYSTEMTIME lt{};
SystemTimeToTzSpecificLocalTime(nullptr, &st, &lt);
EXPECT_EQ(2009, lt.wYear);
EXPECT_EQ(10, lt.wMonth);
EXPECT_EQ(12, lt.wDay);
EXPECT_EQ(17, lt.wHour);
EXPECT_EQ(50, lt.wMinute);
EXPECT_EQ(20, lt.wSecond);
EXPECT_EQ(111, lt.wMilliseconds);
}
#endif
TEST(utils, 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[0], &uuid2[0]);
}
TEST(utils, generate_sha256) {
const auto res = utils::file::generate_sha256(__FILE__);
std::cout << res << std::endl;
}
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
/*
Copyright <2018-2023> <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_common.hpp"
#include "utils/file_utils.hpp"
#include "utils/path_utils.hpp"
#include "utils/string_utils.hpp"
#include "utils/utils.hpp"
namespace repertory {
#ifdef _WIN32
TEST(utils, convert_api_date) {
LARGE_INTEGER li{};
li.QuadPart = utils::convert_api_date("2009-10-12T17:50:30.111Z");
SYSTEMTIME st{};
FileTimeToSystemTime(reinterpret_cast<FILETIME *>(&li), &st);
SYSTEMTIME lt{};
SystemTimeToTzSpecificLocalTime(nullptr, &st, &lt);
EXPECT_EQ(2009, lt.wYear);
EXPECT_EQ(10, lt.wMonth);
EXPECT_EQ(12, lt.wDay);
EXPECT_EQ(17, lt.wHour);
EXPECT_EQ(50, lt.wMinute);
EXPECT_EQ(20, lt.wSecond);
EXPECT_EQ(111, lt.wMilliseconds);
}
#endif
TEST(utils, 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[0], &uuid2[0]);
}
TEST(utils, generate_sha256) {
const auto res = utils::file::generate_sha256(__FILE__);
std::cout << res << std::endl;
}
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

View File

@@ -1,57 +1,57 @@
/*
Copyright <2018-2023> <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_common.hpp"
#include "utils/utils.hpp"
namespace repertory {
TEST(version, equal) {
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"));
}
TEST(version, greater) {
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"));
}
TEST(version, less) {
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"));
}
} // namespace repertory
/*
Copyright <2018-2023> <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_common.hpp"
#include "utils/utils.hpp"
namespace repertory {
TEST(version, equal) {
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"));
}
TEST(version, greater) {
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"));
}
TEST(version, less) {
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"));
}
} // namespace repertory

View File

@@ -1,385 +1,385 @@
/*
Copyright <2018-2023> <scott.e.graves@protonmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#if _WIN32
#include "test_common.hpp"
#include "fixtures/winfsp_fixture.hpp"
#include "types/repertory.hpp"
#include "utils/event_capture.hpp"
namespace repertory {
void launch_app(std::string cmd) {
PROCESS_INFORMATION pi{};
STARTUPINFO si{};
si.cb = sizeof(si);
if (!::CreateProcessA(nullptr, (LPSTR)cmd.c_str(), nullptr, nullptr, FALSE,
CREATE_NEW_CONSOLE | CREATE_NEW_PROCESS_GROUP, nullptr,
nullptr, &si, &pi)) {
throw std::runtime_error("CreateProcess failed (" +
std::to_string(::GetLastError()) + ")");
}
::WaitForSingleObject(pi.hProcess, INFINITE);
DWORD code{};
::GetExitCodeProcess(pi.hProcess, &code);
::CloseHandle(pi.hProcess);
::CloseHandle(pi.hThread);
EXPECT_EQ(0, code);
}
E_SIMPLE1(test_begin, normal, false, std::string, test_name, TN, E_STRING);
#define TEST_HEADER(func) \
event_system::instance().raise<test_begin>( \
std::string(func) + \
"\r\n***********************\r\n***********************")
static auto mount_setup(std::string &mount_point) {
mount_point = "U:";
return std::vector<std::string>({"unittests", "-f", mount_point});
}
static void execute_mount(winfsp_test *test,
const std::vector<std::string> &drive_args,
std::thread &th) {
ASSERT_EQ(0, test->drive->mount(drive_args));
th.join();
}
static void unmount(winfsp_test *test, const std::string &mount_point) {
test->drive->shutdown();
auto mounted = utils::file::is_directory(mount_point);
for (auto i = 0; mounted && (i < 50); i++) {
std::this_thread::sleep_for(100ms);
mounted = utils::file::is_directory(mount_point);
}
EXPECT_FALSE(utils::file::is_directory(mount_point));
}
static void root_creation_test(const std::string &mount_point) {
TEST_HEADER(__FUNCTION__);
WIN32_FILE_ATTRIBUTE_DATA ad{};
EXPECT_TRUE(
::GetFileAttributesEx(&mount_point[0], GetFileExInfoStandard, &ad));
EXPECT_EQ(FILE_ATTRIBUTE_DIRECTORY, ad.dwFileAttributes);
EXPECT_EQ(0, ad.nFileSizeHigh);
EXPECT_EQ(0, ad.nFileSizeLow);
}
static auto create_test(winfsp_test *test, const std::string &mount_point) {
TEST_HEADER(__FUNCTION__);
auto file = utils::path::combine(mount_point, {{"test_create.txt"}});
auto handle = ::CreateFileA(&file[0], GENERIC_READ, FILE_SHARE_READ, nullptr,
CREATE_NEW, FILE_ATTRIBUTE_NORMAL, nullptr);
EXPECT_NE(INVALID_HANDLE_VALUE, handle);
EXPECT_TRUE(::CloseHandle(handle));
EXPECT_TRUE(utils::file::is_file(file));
std::uint64_t file_size;
EXPECT_TRUE(utils::file::get_file_size(file, file_size));
EXPECT_EQ(0, file_size);
std::string attr;
EXPECT_EQ(api_error::success, test->provider->get_item_meta(
"/test_create.txt", META_ATTRIBUTES, attr));
EXPECT_EQ(FILE_ATTRIBUTE_NORMAL, utils::string::to_uint32(attr));
return file;
}
static void delete_file_test(const std::string &file) {
TEST_HEADER(__FUNCTION__);
event_capture ec({"file_removed"});
EXPECT_TRUE(utils::file::retry_delete_file(file));
EXPECT_FALSE(utils::file::is_file(file));
}
static void create_directory_test(const std::string &directory) {
TEST_HEADER(__FUNCTION__);
EXPECT_FALSE(::PathIsDirectory(&directory[0]));
EXPECT_TRUE(::CreateDirectoryA(&directory[0], nullptr));
EXPECT_TRUE(::PathIsDirectory(&directory[0]));
}
static void remove_directory_test(const std::string &directory) {
TEST_HEADER(__FUNCTION__);
event_capture ec({"directory_removed"});
EXPECT_TRUE(::PathIsDirectory(&directory[0]));
EXPECT_TRUE(::RemoveDirectoryA(&directory[0]));
EXPECT_FALSE(::PathIsDirectory(&directory[0]));
}
static void write_file_test(const std::string &mount_point) {
TEST_HEADER(__FUNCTION__);
const auto file = utils::path::combine(mount_point, {"test_write.txt"});
auto handle =
::CreateFileA(&file[0], GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ,
nullptr, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, nullptr);
EXPECT_NE(INVALID_HANDLE_VALUE, handle);
const std::string data = "0123456789";
DWORD bytes_written = 0;
EXPECT_TRUE(::WriteFile(handle, &data[0], static_cast<DWORD>(data.size()),
&bytes_written, nullptr));
EXPECT_EQ(10, bytes_written);
EXPECT_TRUE(::CloseHandle(handle));
EXPECT_TRUE(utils::file::is_file(file));
std::uint64_t file_size;
EXPECT_TRUE(utils::file::get_file_size(file, file_size));
EXPECT_EQ(10, file_size);
}
static void read_file_test(const std::string &mount_point) {
TEST_HEADER(__FUNCTION__);
const auto file = utils::path::combine(mount_point, {"test_read.txt"});
auto handle =
::CreateFileA(&file[0], GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ,
nullptr, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, nullptr);
EXPECT_NE(INVALID_HANDLE_VALUE, handle);
const std::string data = "0123456789";
DWORD bytes_written = 0;
EXPECT_TRUE(::WriteFile(handle, &data[0], static_cast<DWORD>(data.size()),
&bytes_written, nullptr));
EXPECT_EQ(10, bytes_written);
data_buffer data2;
data2.resize(10);
DWORD bytes_read = 0;
EXPECT_EQ(0, ::SetFilePointer(handle, 0, nullptr, FILE_BEGIN));
EXPECT_TRUE(::ReadFile(handle, &data2[0], static_cast<DWORD>(data2.size()),
&bytes_read, nullptr));
EXPECT_EQ(10, bytes_read);
for (auto i = 0; i < data.size(); i++) {
EXPECT_EQ(data[i], data2[i]);
}
EXPECT_TRUE(::CloseHandle(handle));
}
static void rename_file_test(winfsp_test *test,
const std::string &mount_point) {
TEST_HEADER(__FUNCTION__);
const auto file = utils::path::combine(mount_point, {"rename_file.txt"});
auto handle = ::CreateFileA(&file[0], GENERIC_READ, FILE_SHARE_READ, nullptr,
CREATE_NEW, FILE_ATTRIBUTE_NORMAL, nullptr);
EXPECT_NE(INVALID_HANDLE_VALUE, handle);
EXPECT_TRUE(::CloseHandle(handle));
api_meta_map meta1{};
EXPECT_EQ(api_error::success,
test->provider->get_item_meta("/rename_file.txt", meta1));
const auto file2 = utils::path::combine(mount_point, {"rename_file2.txt"});
EXPECT_TRUE(::MoveFile(&file[0], &file2[0]));
EXPECT_TRUE(utils::file::is_file(file2));
EXPECT_FALSE(utils::file::is_file(file));
api_meta_map meta2{};
EXPECT_EQ(api_error::success,
test->provider->get_item_meta("/rename_file2.txt", meta2));
EXPECT_STREQ(meta1[META_SOURCE].c_str(), meta2[META_SOURCE].c_str());
filesystem_item fsi{};
EXPECT_EQ(api_error::success, test->provider->get_filesystem_item(
"/rename_file2.txt", false, fsi));
EXPECT_STREQ(meta1[META_SOURCE].c_str(), fsi.source_path.c_str());
filesystem_item fsi2{};
EXPECT_EQ(api_error::success,
test->provider->get_filesystem_item_from_source_path(
fsi.source_path, fsi2));
EXPECT_STREQ("/rename_file2.txt", fsi2.api_path.c_str());
EXPECT_EQ(api_error::item_not_found,
test->provider->get_item_meta("/rename_file.txt", meta2));
}
static void rename_directory_test(const std::string &mount_point) {
TEST_HEADER(__FUNCTION__);
std::string directory = "rename_dir";
const auto full_directory = utils::path::combine(mount_point, {directory});
std::string directory2 = "rename_dir2";
const auto full_directory2 = utils::path::combine(mount_point, {directory2});
EXPECT_FALSE(::PathIsDirectory(&full_directory[0]));
EXPECT_TRUE(::CreateDirectoryA(&full_directory[0], nullptr));
EXPECT_TRUE(::PathIsDirectory(&full_directory[0]));
EXPECT_TRUE(::MoveFile(&full_directory[0], &full_directory2[0]));
EXPECT_FALSE(::PathIsDirectory(&full_directory[0]));
EXPECT_TRUE(::PathIsDirectory(&full_directory2[0]));
}
static void get_set_basic_info_test(const std::string &mount_point) {
TEST_HEADER(__FUNCTION__);
const auto file =
utils::path::combine(mount_point, {"setbasicinfo_file.txt"});
auto handle =
::CreateFileA(&file[0], GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ,
nullptr, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, nullptr);
EXPECT_NE(INVALID_HANDLE_VALUE, handle);
SYSTEMTIME st{};
::GetSystemTime(&st);
st.wMinute = 0;
FILETIME test_ch_time{};
st.wMinute++;
::SystemTimeToFileTime(&st, &test_ch_time);
FILETIME test_cr_time{};
st.wMinute++;
::SystemTimeToFileTime(&st, &test_cr_time);
FILETIME test_la_time{};
st.wMinute++;
::SystemTimeToFileTime(&st, &test_la_time);
FILETIME test_lw_time{};
st.wMinute++;
::SystemTimeToFileTime(&st, &test_lw_time);
FILE_BASIC_INFO fbi{};
fbi.FileAttributes = FILE_ATTRIBUTE_HIDDEN;
fbi.ChangeTime.HighPart = test_ch_time.dwHighDateTime;
fbi.ChangeTime.LowPart = test_ch_time.dwLowDateTime;
fbi.CreationTime = *(LARGE_INTEGER *)&test_cr_time;
fbi.LastAccessTime = *(LARGE_INTEGER *)&test_la_time;
fbi.LastWriteTime = *(LARGE_INTEGER *)&test_lw_time;
EXPECT_TRUE(::SetFileInformationByHandle(handle, FileBasicInfo, &fbi,
sizeof(FILE_BASIC_INFO)));
FILE_BASIC_INFO fbi2{};
EXPECT_TRUE(::GetFileInformationByHandleEx(handle, FileBasicInfo, &fbi2,
sizeof(FILE_BASIC_INFO)));
EXPECT_EQ(0, memcmp(&fbi, &fbi2, sizeof(FILE_BASIC_INFO)));
std::cout << fbi.FileAttributes << " " << fbi.ChangeTime.QuadPart << " "
<< fbi.CreationTime.QuadPart << " " << fbi.LastAccessTime.QuadPart
<< " " << fbi.LastWriteTime.QuadPart << std::endl;
std::cout << fbi2.FileAttributes << " " << fbi2.ChangeTime.QuadPart << " "
<< fbi2.CreationTime.QuadPart << " " << fbi2.LastAccessTime.QuadPart
<< " " << fbi2.LastWriteTime.QuadPart << std::endl;
EXPECT_TRUE(::CloseHandle(handle));
}
static void overwrite_file_test(const std::string &mount_point) {
TEST_HEADER(__FUNCTION__);
const auto file = utils::path::combine("./", {"test_overwrite.txt"});
auto handle =
::CreateFileA(&file[0], GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ,
nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
EXPECT_NE(INVALID_HANDLE_VALUE, handle);
if (handle != INVALID_HANDLE_VALUE) {
const std::string data = "0123456789";
DWORD bytes_written = 0;
EXPECT_TRUE(::WriteFile(handle, &data[0], static_cast<DWORD>(data.size()),
&bytes_written, nullptr));
EXPECT_EQ(10, bytes_written);
EXPECT_TRUE(::CloseHandle(handle));
if (bytes_written == 10) {
const auto file2 =
utils::path::combine(mount_point, {"test_overwrite2.txt"});
EXPECT_TRUE(::CopyFile(&file[0], &file2[0], TRUE));
EXPECT_FALSE(::CopyFile(&file[0], &file2[0], TRUE));
}
}
}
TEST_F(winfsp_test, all_tests) {
if (PROVIDER_INDEX == 0) {
for (std::size_t idx = 0U; idx < 2U; idx++) {
launch_app(
("cmd.exe /c unittests.exe --gtest_filter=winfsp_test.all_tests "
"--provider_index " +
std::to_string(idx) + " > unittests" + std::to_string(idx) +
".log 2>&1"));
}
return;
}
#ifndef REPERTORY_ENABLE_S3
if (PROVIDER_INDEX == 1U) {
return;
}
#endif
std::string mount_point;
const auto drive_args = mount_setup(mount_point);
event_capture ec({
"drive_mounted",
"drive_unmounted",
"drive_unmount_pending",
"drive_mount_result",
});
std::thread th([&] {
const auto mounted = ec.wait_for_event("drive_mounted");
EXPECT_TRUE(mounted);
if (mounted) {
root_creation_test(mount_point);
{
const auto file = create_test(this, mount_point);
delete_file_test(file);
}
{
const auto dir = utils::path::combine(mount_point, {"TestDir"});
create_directory_test(dir);
remove_directory_test(dir);
}
write_file_test(mount_point);
read_file_test(mount_point);
// TODO enable after rename support is available
// rename_file_test(this, mount_point);
// rename_directory_test(mount_point);
overwrite_file_test(mount_point);
get_set_basic_info_test(mount_point);
}
if (mounted) {
unmount(this, mount_point);
ec.wait_for_empty();
}
});
execute_mount(this, drive_args, th);
}
} // namespace repertory
#endif
/*
Copyright <2018-2023> <scott.e.graves@protonmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#if _WIN32
#include "test_common.hpp"
#include "fixtures/winfsp_fixture.hpp"
#include "types/repertory.hpp"
#include "utils/event_capture.hpp"
namespace repertory {
void launch_app(std::string cmd) {
PROCESS_INFORMATION pi{};
STARTUPINFO si{};
si.cb = sizeof(si);
if (!::CreateProcessA(nullptr, (LPSTR)cmd.c_str(), nullptr, nullptr, FALSE,
CREATE_NEW_CONSOLE | CREATE_NEW_PROCESS_GROUP, nullptr,
nullptr, &si, &pi)) {
throw std::runtime_error("CreateProcess failed (" +
std::to_string(::GetLastError()) + ")");
}
::WaitForSingleObject(pi.hProcess, INFINITE);
DWORD code{};
::GetExitCodeProcess(pi.hProcess, &code);
::CloseHandle(pi.hProcess);
::CloseHandle(pi.hThread);
EXPECT_EQ(0, code);
}
E_SIMPLE1(test_begin, normal, false, std::string, test_name, TN, E_STRING);
#define TEST_HEADER(func) \
event_system::instance().raise<test_begin>( \
std::string(func) + \
"\r\n***********************\r\n***********************")
static auto mount_setup(std::string &mount_point) {
mount_point = "U:";
return std::vector<std::string>({"unittests", "-f", mount_point});
}
static void execute_mount(winfsp_test *test,
const std::vector<std::string> &drive_args,
std::thread &th) {
ASSERT_EQ(0, test->drive->mount(drive_args));
th.join();
}
static void unmount(winfsp_test *test, const std::string &mount_point) {
test->drive->shutdown();
auto mounted = utils::file::is_directory(mount_point);
for (auto i = 0; mounted && (i < 50); i++) {
std::this_thread::sleep_for(100ms);
mounted = utils::file::is_directory(mount_point);
}
EXPECT_FALSE(utils::file::is_directory(mount_point));
}
static void root_creation_test(const std::string &mount_point) {
TEST_HEADER(__FUNCTION__);
WIN32_FILE_ATTRIBUTE_DATA ad{};
EXPECT_TRUE(
::GetFileAttributesEx(&mount_point[0], GetFileExInfoStandard, &ad));
EXPECT_EQ(FILE_ATTRIBUTE_DIRECTORY, ad.dwFileAttributes);
EXPECT_EQ(0, ad.nFileSizeHigh);
EXPECT_EQ(0, ad.nFileSizeLow);
}
static auto create_test(winfsp_test *test, const std::string &mount_point) {
TEST_HEADER(__FUNCTION__);
auto file = utils::path::combine(mount_point, {{"test_create.txt"}});
auto handle = ::CreateFileA(&file[0], GENERIC_READ, FILE_SHARE_READ, nullptr,
CREATE_NEW, FILE_ATTRIBUTE_NORMAL, nullptr);
EXPECT_NE(INVALID_HANDLE_VALUE, handle);
EXPECT_TRUE(::CloseHandle(handle));
EXPECT_TRUE(utils::file::is_file(file));
std::uint64_t file_size;
EXPECT_TRUE(utils::file::get_file_size(file, file_size));
EXPECT_EQ(0, file_size);
std::string attr;
EXPECT_EQ(api_error::success, test->provider->get_item_meta(
"/test_create.txt", META_ATTRIBUTES, attr));
EXPECT_EQ(FILE_ATTRIBUTE_NORMAL, utils::string::to_uint32(attr));
return file;
}
static void delete_file_test(const std::string &file) {
TEST_HEADER(__FUNCTION__);
event_capture ec({"file_removed"});
EXPECT_TRUE(utils::file::retry_delete_file(file));
EXPECT_FALSE(utils::file::is_file(file));
}
static void create_directory_test(const std::string &directory) {
TEST_HEADER(__FUNCTION__);
EXPECT_FALSE(::PathIsDirectory(&directory[0]));
EXPECT_TRUE(::CreateDirectoryA(&directory[0], nullptr));
EXPECT_TRUE(::PathIsDirectory(&directory[0]));
}
static void remove_directory_test(const std::string &directory) {
TEST_HEADER(__FUNCTION__);
event_capture ec({"directory_removed"});
EXPECT_TRUE(::PathIsDirectory(&directory[0]));
EXPECT_TRUE(::RemoveDirectoryA(&directory[0]));
EXPECT_FALSE(::PathIsDirectory(&directory[0]));
}
static void write_file_test(const std::string &mount_point) {
TEST_HEADER(__FUNCTION__);
const auto file = utils::path::combine(mount_point, {"test_write.txt"});
auto handle =
::CreateFileA(&file[0], GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ,
nullptr, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, nullptr);
EXPECT_NE(INVALID_HANDLE_VALUE, handle);
const std::string data = "0123456789";
DWORD bytes_written = 0;
EXPECT_TRUE(::WriteFile(handle, &data[0], static_cast<DWORD>(data.size()),
&bytes_written, nullptr));
EXPECT_EQ(10, bytes_written);
EXPECT_TRUE(::CloseHandle(handle));
EXPECT_TRUE(utils::file::is_file(file));
std::uint64_t file_size;
EXPECT_TRUE(utils::file::get_file_size(file, file_size));
EXPECT_EQ(10, file_size);
}
static void read_file_test(const std::string &mount_point) {
TEST_HEADER(__FUNCTION__);
const auto file = utils::path::combine(mount_point, {"test_read.txt"});
auto handle =
::CreateFileA(&file[0], GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ,
nullptr, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, nullptr);
EXPECT_NE(INVALID_HANDLE_VALUE, handle);
const std::string data = "0123456789";
DWORD bytes_written = 0;
EXPECT_TRUE(::WriteFile(handle, &data[0], static_cast<DWORD>(data.size()),
&bytes_written, nullptr));
EXPECT_EQ(10, bytes_written);
data_buffer data2;
data2.resize(10);
DWORD bytes_read = 0;
EXPECT_EQ(0, ::SetFilePointer(handle, 0, nullptr, FILE_BEGIN));
EXPECT_TRUE(::ReadFile(handle, &data2[0], static_cast<DWORD>(data2.size()),
&bytes_read, nullptr));
EXPECT_EQ(10, bytes_read);
for (auto i = 0; i < data.size(); i++) {
EXPECT_EQ(data[i], data2[i]);
}
EXPECT_TRUE(::CloseHandle(handle));
}
static void rename_file_test(winfsp_test *test,
const std::string &mount_point) {
TEST_HEADER(__FUNCTION__);
const auto file = utils::path::combine(mount_point, {"rename_file.txt"});
auto handle = ::CreateFileA(&file[0], GENERIC_READ, FILE_SHARE_READ, nullptr,
CREATE_NEW, FILE_ATTRIBUTE_NORMAL, nullptr);
EXPECT_NE(INVALID_HANDLE_VALUE, handle);
EXPECT_TRUE(::CloseHandle(handle));
api_meta_map meta1{};
EXPECT_EQ(api_error::success,
test->provider->get_item_meta("/rename_file.txt", meta1));
const auto file2 = utils::path::combine(mount_point, {"rename_file2.txt"});
EXPECT_TRUE(::MoveFile(&file[0], &file2[0]));
EXPECT_TRUE(utils::file::is_file(file2));
EXPECT_FALSE(utils::file::is_file(file));
api_meta_map meta2{};
EXPECT_EQ(api_error::success,
test->provider->get_item_meta("/rename_file2.txt", meta2));
EXPECT_STREQ(meta1[META_SOURCE].c_str(), meta2[META_SOURCE].c_str());
filesystem_item fsi{};
EXPECT_EQ(api_error::success, test->provider->get_filesystem_item(
"/rename_file2.txt", false, fsi));
EXPECT_STREQ(meta1[META_SOURCE].c_str(), fsi.source_path.c_str());
filesystem_item fsi2{};
EXPECT_EQ(api_error::success,
test->provider->get_filesystem_item_from_source_path(
fsi.source_path, fsi2));
EXPECT_STREQ("/rename_file2.txt", fsi2.api_path.c_str());
EXPECT_EQ(api_error::item_not_found,
test->provider->get_item_meta("/rename_file.txt", meta2));
}
static void rename_directory_test(const std::string &mount_point) {
TEST_HEADER(__FUNCTION__);
std::string directory = "rename_dir";
const auto full_directory = utils::path::combine(mount_point, {directory});
std::string directory2 = "rename_dir2";
const auto full_directory2 = utils::path::combine(mount_point, {directory2});
EXPECT_FALSE(::PathIsDirectory(&full_directory[0]));
EXPECT_TRUE(::CreateDirectoryA(&full_directory[0], nullptr));
EXPECT_TRUE(::PathIsDirectory(&full_directory[0]));
EXPECT_TRUE(::MoveFile(&full_directory[0], &full_directory2[0]));
EXPECT_FALSE(::PathIsDirectory(&full_directory[0]));
EXPECT_TRUE(::PathIsDirectory(&full_directory2[0]));
}
static void get_set_basic_info_test(const std::string &mount_point) {
TEST_HEADER(__FUNCTION__);
const auto file =
utils::path::combine(mount_point, {"setbasicinfo_file.txt"});
auto handle =
::CreateFileA(&file[0], GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ,
nullptr, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, nullptr);
EXPECT_NE(INVALID_HANDLE_VALUE, handle);
SYSTEMTIME st{};
::GetSystemTime(&st);
st.wMinute = 0;
FILETIME test_ch_time{};
st.wMinute++;
::SystemTimeToFileTime(&st, &test_ch_time);
FILETIME test_cr_time{};
st.wMinute++;
::SystemTimeToFileTime(&st, &test_cr_time);
FILETIME test_la_time{};
st.wMinute++;
::SystemTimeToFileTime(&st, &test_la_time);
FILETIME test_lw_time{};
st.wMinute++;
::SystemTimeToFileTime(&st, &test_lw_time);
FILE_BASIC_INFO fbi{};
fbi.FileAttributes = FILE_ATTRIBUTE_HIDDEN;
fbi.ChangeTime.HighPart = test_ch_time.dwHighDateTime;
fbi.ChangeTime.LowPart = test_ch_time.dwLowDateTime;
fbi.CreationTime = *(LARGE_INTEGER *)&test_cr_time;
fbi.LastAccessTime = *(LARGE_INTEGER *)&test_la_time;
fbi.LastWriteTime = *(LARGE_INTEGER *)&test_lw_time;
EXPECT_TRUE(::SetFileInformationByHandle(handle, FileBasicInfo, &fbi,
sizeof(FILE_BASIC_INFO)));
FILE_BASIC_INFO fbi2{};
EXPECT_TRUE(::GetFileInformationByHandleEx(handle, FileBasicInfo, &fbi2,
sizeof(FILE_BASIC_INFO)));
EXPECT_EQ(0, memcmp(&fbi, &fbi2, sizeof(FILE_BASIC_INFO)));
std::cout << fbi.FileAttributes << " " << fbi.ChangeTime.QuadPart << " "
<< fbi.CreationTime.QuadPart << " " << fbi.LastAccessTime.QuadPart
<< " " << fbi.LastWriteTime.QuadPart << std::endl;
std::cout << fbi2.FileAttributes << " " << fbi2.ChangeTime.QuadPart << " "
<< fbi2.CreationTime.QuadPart << " " << fbi2.LastAccessTime.QuadPart
<< " " << fbi2.LastWriteTime.QuadPart << std::endl;
EXPECT_TRUE(::CloseHandle(handle));
}
static void overwrite_file_test(const std::string &mount_point) {
TEST_HEADER(__FUNCTION__);
const auto file = utils::path::combine("./", {"test_overwrite.txt"});
auto handle =
::CreateFileA(&file[0], GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ,
nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
EXPECT_NE(INVALID_HANDLE_VALUE, handle);
if (handle != INVALID_HANDLE_VALUE) {
const std::string data = "0123456789";
DWORD bytes_written = 0;
EXPECT_TRUE(::WriteFile(handle, &data[0], static_cast<DWORD>(data.size()),
&bytes_written, nullptr));
EXPECT_EQ(10, bytes_written);
EXPECT_TRUE(::CloseHandle(handle));
if (bytes_written == 10) {
const auto file2 =
utils::path::combine(mount_point, {"test_overwrite2.txt"});
EXPECT_TRUE(::CopyFile(&file[0], &file2[0], TRUE));
EXPECT_FALSE(::CopyFile(&file[0], &file2[0], TRUE));
}
}
}
TEST_F(winfsp_test, all_tests) {
if (PROVIDER_INDEX == 0) {
for (std::size_t idx = 0U; idx < 2U; idx++) {
launch_app(
("cmd.exe /c unittests.exe --gtest_filter=winfsp_test.all_tests "
"--provider_index " +
std::to_string(idx) + " > unittests" + std::to_string(idx) +
".log 2>&1"));
}
return;
}
#ifndef REPERTORY_ENABLE_S3
if (PROVIDER_INDEX == 1U) {
return;
}
#endif
std::string mount_point;
const auto drive_args = mount_setup(mount_point);
event_capture ec({
"drive_mounted",
"drive_unmounted",
"drive_unmount_pending",
"drive_mount_result",
});
std::thread th([&] {
const auto mounted = ec.wait_for_event("drive_mounted");
EXPECT_TRUE(mounted);
if (mounted) {
root_creation_test(mount_point);
{
const auto file = create_test(this, mount_point);
delete_file_test(file);
}
{
const auto dir = utils::path::combine(mount_point, {"TestDir"});
create_directory_test(dir);
remove_directory_test(dir);
}
write_file_test(mount_point);
read_file_test(mount_point);
// TODO enable after rename support is available
// rename_file_test(this, mount_point);
// rename_directory_test(mount_point);
overwrite_file_test(mount_point);
get_set_basic_info_test(mount_point);
}
if (mounted) {
unmount(this, mount_point);
ec.wait_for_empty();
}
});
execute_mount(this, drive_args, th);
}
} // namespace repertory
#endif