2.0.0-rc (#9)
Some checks failed
BlockStorage/repertory_osx/pipeline/head This commit looks good
BlockStorage/repertory_windows/pipeline/head This commit looks good
BlockStorage/repertory/pipeline/head There was a failure building this commit
BlockStorage/repertory_linux_builds/pipeline/head This commit looks good
BlockStorage/repertory_osx_builds/pipeline/head There was a failure building this commit

### Issues

* \#1 \[bug\] Unable to mount S3 due to 'item_not_found' exception
* \#2 Require bucket name for S3 mounts
* \#3 \[bug\] File size is not being updated in S3 mount
* \#4 Upgrade to libfuse-3.x.x
* \#5 Switch to renterd for Sia support
* \#6 Switch to cpp-httplib to further reduce dependencies
* \#7 Remove global_data and calculate used disk space per provider
* \#8 Switch to libcurl for S3 mount support

### Changes from v1.x.x

* Added read-only encrypt provider
  * Pass-through mount point that transparently encrypts source data using `XChaCha20-Poly1305`
* Added S3 encryption support via `XChaCha20-Poly1305`
* Added replay protection to remote mounts
* Added support base64 writes in remote FUSE
* Created static linked Linux binaries for `amd64` and `aarch64` using `musl-libc`
* Removed legacy Sia renter support
* Removed Skynet support
* Fixed multiple remote mount WinFSP API issues on \*NIX servers
* Implemented chunked read and write
  * Writes for non-cached files are performed in chunks of 8Mib
* Removed `repertory-ui` support
* Removed `FreeBSD` support
* Switched to `libsodium` over `CryptoPP`
* Switched to `XChaCha20-Poly1305` for remote mounts
* Updated `GoogleTest` to v1.14.0
* Updated `JSON for Modern C++` to v3.11.2
* Updated `OpenSSL` to v1.1.1w
* Updated `RocksDB` to v8.5.3
* Updated `WinFSP` to 2023
* Updated `boost` to v1.78.0
* Updated `cURL` to v8.3.0
* Updated `zlib` to v1.3
* Use `upload_manager` for all providers
  * Adds a delay to uploads to prevent excessive API calls
  * Supports re-upload after mount restart for incomplete uploads
  * NOTE: Uploads for all providers are full file (no resume support)
    * Multipart upload support is planned for S3

Reviewed-on: #9
This commit is contained in:
2023-10-29 06:55:59 +00:00
parent 3ff46723b8
commit f43c41f88a
839 changed files with 98214 additions and 92959 deletions

View File

@ -1,155 +0,0 @@
/*
Copyright <2018-2022> <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 defined(REPERTORY_ENABLE_S3_TESTING)
#include "comm/aws_s3/aws_s3_comm.hpp"
#include "common.hpp"
#include "fixtures/aws_s3_comm_fixture.hpp"
#include "test_common.hpp"
namespace repertory {
TEST_F(aws_s3_comm_test, upload_file) {
auto ret = s3_comm_->upload_file(
"/repertory/test.txt", __FILE__, "", []() -> std::string { return ""; },
[](const std::string &) -> api_error { return api_error::success; }, false);
EXPECT_EQ(api_error::success, ret);
ret = s3_comm_->upload_file(
"/repertory/subdir/test2.txt", __FILE__, "", []() -> std::string { return ""; },
[](const std::string &) -> api_error { return api_error::success; }, false);
EXPECT_EQ(api_error::success, ret);
}
TEST_F(aws_s3_comm_test, get_directory_items) {
directory_item_list list{};
auto ret = s3_comm_->get_directory_items(
"/repertory/subdir", [](directory_item &, const bool &) {}, list);
EXPECT_EQ(api_error::success, ret);
}
TEST_F(aws_s3_comm_test, list_files) {
api_file_list list{};
auto ret = s3_comm_->get_file_list(
[](const std::string &) -> std::string { return ""; },
[](const std::string &, const std::string &object_name) -> std::string {
return object_name;
},
list);
EXPECT_EQ(api_error::success, ret);
}
TEST_F(aws_s3_comm_test, read_file_bytes) {
bool stop_requested = false;
std::vector<char> data;
auto ret = s3_comm_->read_file_bytes(
"/repertory/test.txt", 2, 0, data, []() -> std::string { return ""; },
[]() -> std::uint64_t { return 0ull; }, []() -> std::string { return ""; }, stop_requested);
EXPECT_EQ(api_error::success, ret);
}
TEST_F(aws_s3_comm_test, exists) {
EXPECT_TRUE(s3_comm_->exists("/repertory/test.txt", []() -> std::string { return ""; }));
EXPECT_FALSE(s3_comm_->exists("/repertory/subdir/test.txt", []() -> std::string { return ""; }));
}
TEST_F(aws_s3_comm_test, get_file) {
api_file file{};
auto ret = s3_comm_->get_file(
"/repertory/test.txt", []() -> std::string { return ""; },
[](const std::string &, const std::string &object_name) -> std::string {
return object_name;
},
[]() -> std::string { return ""; }, file);
EXPECT_EQ(api_error::success, ret);
}
TEST_F(aws_s3_comm_test, remove_file) {
auto ret = s3_comm_->remove_file("/repertory/test.txt", []() -> std::string { return ""; });
EXPECT_EQ(api_error::success, ret);
ret = s3_comm_->remove_file("/repertory/subdir/test2.txt", []() -> std::string { return ""; });
EXPECT_EQ(api_error::success, ret);
}
TEST_F(aws_s3_comm_test, rename_file) {
auto ret = s3_comm_->upload_file(
"/repertory/test_r1.txt", __FILE__, "", []() -> std::string { return ""; },
[](const std::string &) -> api_error { return api_error::success; }, false);
EXPECT_EQ(api_error::success, ret);
s3_comm_->remove_file("/repertory/test_r2.txt", []() -> std::string { return ""; });
ret = s3_comm_->rename_file("/repertory/test_r1.txt", "/repertory/test_r2.txt");
EXPECT_EQ(api_error::not_implemented, ret);
EXPECT_TRUE(s3_comm_->exists("/repertory/test_r1.txt", []() -> std::string { return ""; }));
EXPECT_FALSE(s3_comm_->exists("/repertory/test_r2.txt", []() -> std::string { return ""; }));
}
TEST_F(aws_s3_comm_test, create_bucket_and_remove_bucket) {
auto ret = s3_comm_->create_bucket("/repertory2");
EXPECT_EQ(api_error::success, ret);
ret = s3_comm_->remove_bucket("/repertory2");
EXPECT_EQ(api_error::success, ret);
}
TEST_F(aws_s3_comm_test, upload_file_encrypted) {
const auto source_file_path = generate_test_file_name("./", "awscomm");
auto file_size = 2u * utils::encryption::encrypting_reader::get_data_chunk_size() + 3u;
auto source_file = create_random_file(source_file_path, file_size);
auto stop_requested = false;
std::string key;
auto ret = s3_comm_->upload_file(
"/repertory/test.txt", source_file_path, "test", []() -> std::string { return ""; },
[&key](const std::string &k) -> api_error {
key = k;
std::cout << "key:" << key << std::endl;
return api_error::success;
},
stop_requested);
EXPECT_EQ(api_error::success, ret);
std::uint64_t offset = 0u;
auto remain = file_size;
while ((ret == api_error::success) && remain) {
std::vector<char> data;
ret = s3_comm_->read_file_bytes(
"/repertory/test.txt",
std::min(remain, utils::encryption::encrypting_reader::get_data_chunk_size()), offset, data,
[&key]() -> std::string { return key; },
[&file_size]() -> std::uint64_t { return file_size; },
[]() -> std::string { return "test"; }, stop_requested);
EXPECT_EQ(api_error::success, ret);
std::vector<char> data2(data.size());
std::size_t bytes_read{};
source_file->read_bytes(&data2[0u], data2.size(), offset, bytes_read);
EXPECT_EQ(0, std::memcmp(&data2[0u], &data[0u], data2.size()));
remain -= data.size();
offset += data.size();
}
source_file->close();
utils::file::delete_file(source_file_path);
s3_comm_->remove_file("/repertory/test.txt", [&key]() -> std::string { return key; });
}
} // namespace repertory
#endif // REPERTORY_ENABLE_S3_TESTING

View File

@ -1,23 +1,27 @@
/*
Copyright <2018-2022> <scott.e.graves@protonmail.com>
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
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 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.
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 "app_config.hpp"
#include "test_common.hpp"
#include "app_config.hpp"
#include "utils/file_utils.hpp"
#include "utils/path_utils.hpp"
#include "utils/string_utils.hpp"
@ -26,11 +30,13 @@ namespace repertory {
class config_test : public ::testing::Test {
public:
void SetUp() override {
utils::file::delete_directory_recursively(utils::path::absolute("./data"));
ASSERT_TRUE(utils::file::delete_directory_recursively(
utils::path::absolute("./data")));
}
void TearDown() override {
utils::file::delete_directory_recursively(utils::path::absolute("./data"));
EXPECT_TRUE(utils::file::delete_directory_recursively(
utils::path::absolute("./data")));
}
};
@ -59,7 +65,7 @@ const auto DEFAULT_SIA_CONFIG = "{\n"
" },\n"
" \"LowFreqIntervalSeconds\": 3600,\n"
" \"MaxCacheSizeBytes\": 21474836480,\n"
" \"MinimumRedundancy\": 2.5,\n"
" \"MaxUploadCount\": 5,\n"
" \"OnlineCheckRetrySeconds\": 60,\n"
" \"OrphanedFileRetentionDays\": 15,\n"
" \"PreferredDownloadType\": \"fallback\",\n"
@ -77,7 +83,6 @@ const auto DEFAULT_SIA_CONFIG = "{\n"
" },\n"
" \"RetryReadCount\": 6,\n"
" \"RingBufferFileSize\": 512,\n"
" \"StorageByteMonth\": \"0\",\n"
" \"Version\": " +
std::to_string(REPERTORY_CONFIG_VERSION) +
"\n"
@ -112,7 +117,7 @@ const auto DEFAULT_S3_CONFIG = "{\n"
" \"RemoteClientPoolSize\": 10,\n"
" \"RemoteHostNameOrIp\": \"\",\n"
" \"RemoteMaxConnections\": 20,\n"
" \"RemotePort\": 20002,\n"
" \"RemotePort\": 20001,\n"
" \"RemoteReceiveTimeoutSeconds\": 120,\n"
" \"RemoteSendTimeoutSeconds\": 30,\n"
" \"RemoteToken\": \"\"\n"
@ -127,85 +132,25 @@ const auto DEFAULT_S3_CONFIG = "{\n"
" \"Region\": \"any\",\n"
" \"SecretKey\": \"\",\n"
" \"TimeoutMs\": 60000,\n"
" \"URL\": \"\"\n"
" \"URL\": \"\",\n"
" \"UseRegionInURL\": false\n"
" },\n"
" \"Version\": " +
std::to_string(REPERTORY_CONFIG_VERSION) +
"\n"
"}";
const auto DEFAULT_SKYNET_CONFIG = "{\n"
" \"ApiAuth\": \"\",\n"
" \"ApiPort\": 11104,\n"
" \"ApiUser\": \"repertory\",\n"
" \"ChunkDownloaderTimeoutSeconds\": 30,\n"
" \"EnableChunkDownloaderTimeout\": true,\n"
" \"EnableCommDurationEvents\": false,\n"
" \"EnableDriveEvents\": false,\n"
" \"EnableMaxCacheSize\": true,\n"
#ifdef _WIN32
" \"EnableMountManager\": false,\n"
#endif
" \"EventLevel\": \"normal\",\n"
" \"EvictionDelayMinutes\": 30,\n"
" \"EvictionUsesAccessedTime\": false,\n"
" \"HighFreqIntervalSeconds\": 30,\n"
" \"LowFreqIntervalSeconds\": 3600,\n"
" \"MaxCacheSizeBytes\": 21474836480,\n"
" \"MaxUploadCount\": 5,\n"
" \"OnlineCheckRetrySeconds\": 60,\n"
" \"PreferredDownloadType\": \"fallback\",\n"
" \"ReadAheadCount\": 4,\n"
" \"RemoteMount\": {\n"
" \"EnableRemoteMount\": false,\n"
" \"IsRemoteMount\": false,\n"
" \"RemoteClientPoolSize\": 10,\n"
" \"RemoteHostNameOrIp\": \"\",\n"
" \"RemoteMaxConnections\": 20,\n"
" \"RemotePort\": 20003,\n"
" \"RemoteReceiveTimeoutSeconds\": 120,\n"
" \"RemoteSendTimeoutSeconds\": 30,\n"
" \"RemoteToken\": \"\"\n"
" },\n"
" \"RetryReadCount\": 6,\n"
" \"RingBufferFileSize\": 512,\n"
" \"SkynetConfig\": {\n"
" \"EncryptionToken\": \"\",\n"
" \"PortalList\": [\n"
" {\n"
" \"AgentString\": \"\",\n"
" \"ApiPassword\": \"\",\n"
" \"ApiPort\": 443,\n"
" \"AuthPassword\": \"\",\n"
" \"AuthURL\": \"" +
DEFAULT_SKYNET_URLS[1u] +
"\",\n"
" \"AuthUser\": \"\",\n"
" \"HostNameOrIp\": \"" +
DEFAULT_SKYNET_URLS[0u] +
"\",\n"
" \"Path\": \"\",\n"
" \"Protocol\": \"https\",\n"
" \"TimeoutMs\": 60000\n"
" }\n"
" ]\n"
" },\n"
" \"Version\": " +
std::to_string(REPERTORY_CONFIG_VERSION) +
"\n"
"}";
TEST_F(config_test, sia_default_settings) {
const auto config_file =
utils::path::absolute(utils::path::combine("./data/sia", {"config.json"}));
const auto config_file = utils::path::absolute(
utils::path::combine("./data/sia", {"config.json"}));
for (int i = 0; i < 2; i++) {
app_config config(provider_type::sia, "./data");
config.set_remote_token("");
config.set_api_auth("");
config.set_value_by_name("HostConfig.ApiPassword", "");
EXPECT_TRUE(config.set_value_by_name("HostConfig.ApiPassword", "").empty());
json data;
utils::file::read_json_file(config_file, data);
EXPECT_TRUE(utils::file::read_json_file(config_file, data));
EXPECT_STREQ(DEFAULT_SIA_CONFIG.c_str(), data.dump(2).c_str());
EXPECT_TRUE(utils::file::is_directory("./data/sia/cache"));
EXPECT_TRUE(utils::file::is_directory("./data/sia/logs"));
@ -221,29 +166,13 @@ TEST_F(config_test, s3_default_settings) {
config.set_remote_token("");
config.set_api_auth("");
json data;
utils::file::read_json_file(config_file, data);
EXPECT_TRUE(utils::file::read_json_file(config_file, data));
EXPECT_STREQ(DEFAULT_S3_CONFIG.c_str(), data.dump(2).c_str());
EXPECT_TRUE(utils::file::is_directory("./data/s3/cache"));
EXPECT_TRUE(utils::file::is_directory("./data/s3/logs"));
}
}
TEST_F(config_test, skynet_default_settings) {
const auto config_file =
utils::path::absolute(utils::path::combine("./data/skynet", {"config.json"}));
for (int i = 0; i < 2; i++) {
app_config config(provider_type::skynet, "./data");
config.set_remote_token("");
config.set_api_auth("");
json data;
utils::file::read_json_file(config_file, data);
EXPECT_STREQ(DEFAULT_SKYNET_CONFIG.c_str(), data.dump(2).c_str());
EXPECT_TRUE(utils::file::is_directory("./data/skynet/cache"));
EXPECT_TRUE(utils::file::is_directory("./data/skynet/logs"));
}
}
TEST_F(config_test, api_path) {
std::string original_value;
{
@ -464,35 +393,21 @@ TEST_F(config_test, max_cache_size_bytes) {
TEST_F(config_test, max_upload_count) {
{
app_config config(provider_type::skynet, "./data");
app_config config(provider_type::sia, "./data");
config.set_max_upload_count(8u);
EXPECT_EQ(std::uint8_t(8u), config.get_max_upload_count());
}
{
app_config config(provider_type::skynet, "./data");
app_config config(provider_type::sia, "./data");
EXPECT_EQ(std::uint8_t(8u), config.get_max_upload_count());
}
{
app_config config(provider_type::skynet, "./data");
app_config config(provider_type::sia, "./data");
config.set_max_upload_count(0u);
EXPECT_EQ(std::uint8_t(1u), config.get_max_upload_count());
}
}
TEST_F(config_test, minimum_redundancy) {
double original_value;
{
app_config config(provider_type::sia, "./data");
original_value = config.get_minimum_redundancy();
config.set_minimum_redundancy(original_value + 0.1);
EXPECT_EQ(original_value + 0.1, config.get_minimum_redundancy());
}
{
app_config config(provider_type::sia, "./data");
EXPECT_EQ(original_value + 0.1, config.get_minimum_redundancy());
}
}
TEST_F(config_test, online_check_retry_secs) {
std::uint16_t original_value;
{
@ -559,61 +474,28 @@ TEST_F(config_test, read_ahead_count) {
}
}
TEST_F(config_test, storage_byte_month) {
api_currency original_value;
{
app_config config(provider_type::sia, "./data");
original_value = config.get_storage_byte_month();
config.set_storage_byte_month(original_value + 5);
EXPECT_EQ(original_value + 5, config.get_storage_byte_month());
}
{
app_config config(provider_type::sia, "./data");
EXPECT_EQ(original_value + 5, config.get_storage_byte_month());
}
}
TEST_F(config_test, get_cache_directory) {
{
app_config config(provider_type::sia, "./data");
EXPECT_STREQ(utils::path::absolute("./data/sia/cache").c_str(),
config.get_cache_directory().c_str());
}
{
app_config config(provider_type::skynet, "./data");
EXPECT_STREQ(utils::path::absolute("./data/skynet/cache").c_str(),
config.get_cache_directory().c_str());
}
}
TEST_F(config_test, get_config_file_path) {
{
const auto config_file =
utils::path::absolute(utils::path::combine("./data/sia", {"config.json"}));
const auto config_file = utils::path::absolute(
utils::path::combine("./data/sia", {"config.json"}));
app_config config(provider_type::sia, "./data");
EXPECT_STREQ(config_file.c_str(), config.get_config_file_path().c_str());
}
{
const auto config_file =
utils::path::absolute(utils::path::combine("./data/skynet", {"config.json"}));
app_config config(provider_type::skynet, "./data");
EXPECT_STREQ(config_file.c_str(), config.get_config_file_path().c_str());
}
}
TEST_F(config_test, get_data_directory) {
{
app_config config(provider_type::sia, "./data");
EXPECT_STREQ(utils::path::absolute("./data/sia").c_str(), config.get_data_directory().c_str());
}
{
app_config config(provider_type::skynet, "./data");
EXPECT_STREQ(utils::path::absolute("./data/skynet").c_str(),
EXPECT_STREQ(utils::path::absolute("./data/sia").c_str(),
config.get_data_directory().c_str());
}
}
@ -624,12 +506,6 @@ TEST_F(config_test, get_log_directory) {
EXPECT_STREQ(utils::path::absolute("./data/sia/logs").c_str(),
config.get_log_directory().c_str());
}
{
app_config config(provider_type::skynet, "./data");
EXPECT_STREQ(utils::path::absolute("./data/skynet/logs").c_str(),
config.get_log_directory().c_str());
}
}
TEST_F(config_test, ring_buffer_file_size) {
@ -685,7 +561,8 @@ TEST_F(config_test, preferred_download_type) {
}
TEST_F(config_test, default_agent_name) {
EXPECT_STREQ("Sia-Agent", app_config::default_agent_name(provider_type::sia).c_str());
EXPECT_STREQ("Sia-Agent",
app_config::default_agent_name(provider_type::sia).c_str());
}
TEST_F(config_test, default_api_port) {
@ -695,7 +572,6 @@ TEST_F(config_test, default_api_port) {
TEST_F(config_test, default_data_directory) {
const std::string data_directory[] = {
app_config::default_data_directory(provider_type::sia),
app_config::default_data_directory(provider_type::skynet),
};
#ifdef _WIN32
@ -706,13 +582,12 @@ TEST_F(config_test, default_data_directory) {
utils::path::combine(utils::get_environment_variable("HOME"), {".local"});
#endif
#ifdef __APPLE__
const auto local_app_data = utils::path::combine(utils::get_environment_variable("HOME"),
{"Library/Application Support"});
const auto local_app_data = utils::path::combine(
utils::get_environment_variable("HOME"), {"Library/Application Support"});
#endif
auto expected_directory = utils::path::combine(local_app_data, {"/repertory2/sia"});
auto expected_directory =
utils::path::combine(local_app_data, {"/repertory2/sia"});
EXPECT_STREQ(expected_directory.c_str(), data_directory[0].c_str());
expected_directory = utils::path::combine(local_app_data, {"/repertory2/skynet"});
EXPECT_STREQ(expected_directory.c_str(), data_directory[1].c_str());
}
TEST_F(config_test, default_rpc_port) {
@ -720,22 +595,13 @@ TEST_F(config_test, default_rpc_port) {
}
TEST_F(config_test, get_provider_display_name) {
EXPECT_STREQ("Sia", app_config::get_provider_display_name(provider_type::sia).c_str());
EXPECT_STREQ("Skynet", app_config::get_provider_display_name(provider_type::skynet).c_str());
}
TEST_F(config_test, get_provider_minimum_version) {
EXPECT_STREQ(MIN_SIA_VERSION,
app_config::get_provider_minimum_version(provider_type::sia).c_str());
EXPECT_STREQ(
"Sia", app_config::get_provider_display_name(provider_type::sia).c_str());
}
TEST_F(config_test, get_provider_name) {
EXPECT_STREQ("sia", app_config::get_provider_name(provider_type::sia).c_str());
EXPECT_STREQ("skynet", app_config::get_provider_name(provider_type::skynet).c_str());
}
TEST_F(config_test, get_provider_path_name) {
EXPECT_STREQ("siapath", app_config::get_provider_path_name(provider_type::sia).c_str());
EXPECT_STREQ("sia",
app_config::get_provider_name(provider_type::sia).c_str());
}
TEST_F(config_test, get_version) {
@ -743,11 +609,6 @@ TEST_F(config_test, get_version) {
app_config config(provider_type::sia, "./data");
EXPECT_EQ(REPERTORY_CONFIG_VERSION, config.get_version());
}
{
app_config config(provider_type::skynet, "./data");
EXPECT_EQ(REPERTORY_CONFIG_VERSION, config.get_version());
}
}
TEST_F(config_test, enable_remote_mount) {
@ -937,7 +798,8 @@ TEST_F(config_test, retry_read_count_minimum_value) {
TEST_F(config_test, cache_timeout_seconds_minimum_value) {
{
app_config config(provider_type::s3, "./data");
config.set_value_by_name("S3Config.CacheTimeoutSeconds", "1");
EXPECT_FALSE(
config.set_value_by_name("S3Config.CacheTimeoutSeconds", "1").empty());
EXPECT_EQ(std::uint16_t(5u), config.get_s3_config().cache_timeout_secs);
}
}

77
tests/curl_comm_test.cpp Normal file
View File

@ -0,0 +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

View File

@ -1,24 +1,28 @@
/*
Copyright <2018-2022> <scott.e.graves@protonmail.com>
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
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 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.
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 "fixtures/directory_db_fixture.hpp"
#include "test_common.hpp"
#include "fixtures/directory_db_fixture.hpp"
namespace repertory {
static const auto dirs = {"/",
"/root",
@ -31,7 +35,7 @@ static const auto dirs = {"/",
TEST_F(directory_db_test, is_directory) {
for (const auto &dir : dirs) {
db_->create_directory(dir);
EXPECT_EQ(api_error::success, db_->create_directory(dir));
}
for (const auto &dir : dirs) {
@ -41,7 +45,7 @@ TEST_F(directory_db_test, is_directory) {
TEST_F(directory_db_test, remove_directory) {
for (const auto &dir : dirs) {
db_->create_directory(dir);
EXPECT_EQ(api_error::success, db_->create_directory(dir));
}
EXPECT_EQ(api_error::success, db_->remove_directory("/root/sub2/sub2_sub1"));
@ -52,7 +56,7 @@ TEST_F(directory_db_test, remove_directory) {
TEST_F(directory_db_test, get_sub_directory_count) {
for (const auto &dir : dirs) {
db_->create_directory(dir);
EXPECT_EQ(api_error::success, db_->create_directory(dir));
}
EXPECT_EQ(1u, db_->get_sub_directory_count("/"));
@ -66,7 +70,7 @@ TEST_F(directory_db_test, get_sub_directory_count) {
TEST_F(directory_db_test, populate_sub_directories) {
for (const auto &dir : dirs) {
db_->create_directory(dir);
EXPECT_EQ(api_error::success, db_->create_directory(dir));
}
directory_item_list list{};
@ -79,72 +83,72 @@ TEST_F(directory_db_test, populate_sub_directories) {
std::cout << "/" << std::endl;
db_->populate_sub_directories(
"/", [](directory_item &, const bool &) {}, list);
"/", [](directory_item &) {}, list);
EXPECT_EQ(1u, list.size());
dump_directory_list();
std::cout << std::endl << "/root" << std::endl;
db_->populate_sub_directories(
"/root", [](directory_item &, const bool &) {}, list);
"/root", [](directory_item &) {}, list);
EXPECT_EQ(3u, list.size());
dump_directory_list();
std::cout << std::endl << "/root/sub1" << std::endl;
db_->populate_sub_directories(
"/root/sub1", [](directory_item &, const bool &) {}, list);
"/root/sub1", [](directory_item &) {}, list);
EXPECT_EQ(0u, list.size());
dump_directory_list();
std::cout << std::endl << "/root/sub2" << std::endl;
db_->populate_sub_directories(
"/root/sub2", [](directory_item &, const bool &) {}, list);
"/root/sub2", [](directory_item &) {}, list);
EXPECT_EQ(2u, list.size());
dump_directory_list();
std::cout << std::endl << "/root/sub2/sub2_sub1" << std::endl;
db_->populate_sub_directories(
"/root/sub2/sub2_sub1", [](directory_item &, const bool &) {}, list);
"/root/sub2/sub2_sub1", [](directory_item &) {}, list);
EXPECT_EQ(0u, list.size());
dump_directory_list();
std::cout << std::endl << "/root/sub2/sub2_sub2" << std::endl;
db_->populate_sub_directories(
"/root/sub2/sub2_sub2", [](directory_item &, const bool &) {}, list);
"/root/sub2/sub2_sub2", [](directory_item &) {}, list);
EXPECT_EQ(1u, list.size());
dump_directory_list();
std::cout << std::endl << "/root/sub3" << std::endl;
db_->populate_sub_directories(
"/root/sub3", [](directory_item &, const bool &) {}, list);
"/root/sub3", [](directory_item &) {}, list);
EXPECT_EQ(0u, list.size());
dump_directory_list();
}
TEST_F(directory_db_test, is_file) {
for (const auto &dir : dirs) {
db_->create_directory(dir);
EXPECT_EQ(api_error::success, db_->create_directory(dir));
}
EXPECT_EQ(api_error::success, db_->create_file("/cow.txt"));
EXPECT_TRUE(db_->is_file("/cow.txt"));
EXPECT_FALSE(db_->is_directory("/cow.txt"));
EXPECT_EQ(api_error::file_exists, db_->create_file("/cow.txt"));
EXPECT_EQ(api_error::file_exists, db_->create_directory("/cow.txt"));
EXPECT_EQ(api_error::item_exists, db_->create_file("/cow.txt"));
EXPECT_EQ(api_error::item_exists, db_->create_directory("/cow.txt"));
}
TEST_F(directory_db_test, remove_file) {
for (const auto &dir : dirs) {
db_->create_directory(dir);
EXPECT_EQ(api_error::success, db_->create_directory(dir));
}
EXPECT_EQ(api_error::success, db_->create_file("/cow.txt"));
EXPECT_EQ(api_error::item_is_file, db_->remove_directory("/cow.txt"));
EXPECT_EQ(api_error::directory_not_found, db_->remove_directory("/cow.txt"));
EXPECT_TRUE(db_->remove_file("/cow.txt"));
EXPECT_FALSE(db_->is_file("/cow.txt"));
}
TEST_F(directory_db_test, get_directory_item_count) {
db_->create_directory("/");
EXPECT_EQ(api_error::success, db_->create_directory("/"));
EXPECT_EQ(api_error::success, db_->create_file("/cow.txt"));
EXPECT_EQ(api_error::success, db_->create_file("/cow2.txt"));
@ -153,19 +157,19 @@ TEST_F(directory_db_test, get_directory_item_count) {
}
TEST_F(directory_db_test, get_file) {
db_->create_directory("/");
EXPECT_EQ(api_error::success, db_->create_directory("/"));
EXPECT_EQ(api_error::success, db_->create_file("/cow.txt"));
api_file file{};
EXPECT_EQ(api_error::success, db_->get_file("/cow.txt", file, [](api_file &file) {
EXPECT_STREQ("/cow.txt", file.api_path.c_str());
}));
EXPECT_EQ(api_error::success,
db_->get_file("/cow.txt", file, [](api_file &file) {
EXPECT_STREQ("/cow.txt", file.api_path.c_str());
}));
EXPECT_STREQ("/cow.txt", file.api_path.c_str());
}
TEST_F(directory_db_test, get_file_list) {
db_->create_directory("/");
EXPECT_EQ(api_error::success, db_->create_directory("/"));
EXPECT_EQ(api_error::success, db_->create_file("/cow.txt"));
EXPECT_EQ(api_error::success, db_->create_file("/cow2.txt"));
@ -186,7 +190,7 @@ TEST_F(directory_db_test, get_file_list) {
}
TEST_F(directory_db_test, get_total_item_count) {
db_->create_directory("/");
EXPECT_EQ(api_error::success, db_->create_directory("/"));
EXPECT_EQ(api_error::success, db_->create_file("/cow.txt"));
EXPECT_EQ(api_error::success, db_->create_file("/cow2.txt"));
@ -198,7 +202,7 @@ TEST_F(directory_db_test, get_total_item_count) {
}
TEST_F(directory_db_test, populate_directory_files) {
db_->create_directory("/");
EXPECT_EQ(api_error::success, db_->create_directory("/"));
EXPECT_EQ(api_error::success, db_->create_file("/cow.txt"));
EXPECT_EQ(api_error::success, db_->create_file("/cow2.txt"));
@ -207,8 +211,8 @@ TEST_F(directory_db_test, populate_directory_files) {
int i = 0;
db_->populate_directory_files(
"/",
[&i](directory_item &di, const bool &) {
di.meta[META_SIZE] = utils::string::from_int32(i + 1);
[&i](directory_item &di) {
di.meta[META_SIZE] = std::to_string(i + 1);
EXPECT_FALSE(di.directory);
if (i++ == 0) {
EXPECT_STREQ("/cow.txt", &di.api_path[0]);
@ -229,7 +233,7 @@ TEST_F(directory_db_test, populate_directory_files) {
TEST_F(directory_db_test, create_directory_fails_if_directory_exists) {
for (const auto &dir : dirs) {
db_->create_directory(dir);
EXPECT_EQ(api_error::success, db_->create_directory(dir));
}
EXPECT_EQ(api_error::directory_exists, db_->create_file("/root/sub1"));
@ -238,16 +242,16 @@ TEST_F(directory_db_test, create_directory_fails_if_directory_exists) {
TEST_F(directory_db_test, create_file_fails_if_file_exists) {
for (const auto &dir : dirs) {
db_->create_directory(dir);
EXPECT_EQ(api_error::success, db_->create_directory(dir));
}
EXPECT_EQ(api_error::success, db_->create_file("/cow.txt"));
EXPECT_EQ(api_error::file_exists, db_->create_directory("/cow.txt"));
EXPECT_EQ(api_error::item_exists, db_->create_directory("/cow.txt"));
}
TEST_F(directory_db_test, create_file_fails_if_parent_does_not_exist) {
for (const auto &dir : dirs) {
db_->create_directory(dir);
EXPECT_EQ(api_error::success, db_->create_directory(dir));
}
EXPECT_EQ(api_error::directory_not_found, db_->create_file("/moose/cow.txt"));
@ -255,7 +259,7 @@ TEST_F(directory_db_test, create_file_fails_if_parent_does_not_exist) {
TEST_F(directory_db_test, create_directory_fails_if_parent_does_not_exist) {
for (const auto &dir : dirs) {
db_->create_directory(dir);
EXPECT_EQ(api_error::success, db_->create_directory(dir));
}
EXPECT_EQ(api_error::directory_not_found, db_->create_file("/cow/moose"));
@ -263,7 +267,7 @@ TEST_F(directory_db_test, create_directory_fails_if_parent_does_not_exist) {
TEST_F(directory_db_test, remove_file_fails_if_directory_exists) {
for (const auto &dir : dirs) {
db_->create_directory(dir);
EXPECT_EQ(api_error::success, db_->create_directory(dir));
}
EXPECT_FALSE(db_->remove_file("/root/sub1"));
@ -271,17 +275,17 @@ TEST_F(directory_db_test, remove_file_fails_if_directory_exists) {
TEST_F(directory_db_test, remove_directory_fails_if_file_exists) {
for (const auto &dir : dirs) {
db_->create_directory(dir);
EXPECT_EQ(api_error::success, db_->create_directory(dir));
}
EXPECT_EQ(api_error::success, db_->create_file("/cow.txt"));
EXPECT_EQ(api_error::item_is_file, db_->remove_directory("/cow.txt"));
EXPECT_EQ(api_error::directory_not_found, db_->remove_directory("/cow.txt"));
}
TEST_F(directory_db_test, remove_directory_fails_if_sub_directories_exist) {
db_->create_directory("/");
db_->create_directory("/sub");
db_->create_directory("/sub/sub2");
EXPECT_EQ(api_error::success, db_->create_directory("/"));
EXPECT_EQ(api_error::success, db_->create_directory("/sub"));
EXPECT_EQ(api_error::success, db_->create_directory("/sub/sub2"));
EXPECT_EQ(api_error::directory_not_empty, db_->remove_directory("/sub"));
EXPECT_TRUE(db_->is_directory("/sub"));
@ -289,25 +293,27 @@ TEST_F(directory_db_test, remove_directory_fails_if_sub_directories_exist) {
}
TEST_F(directory_db_test, remove_directory_fails_if_files_exist) {
db_->create_directory("/");
db_->create_directory("/sub");
db_->create_file("/sub/test.txt");
EXPECT_EQ(api_error::success, db_->create_directory("/"));
EXPECT_EQ(api_error::success, db_->create_directory("/sub"));
EXPECT_EQ(api_error::success, db_->create_file("/sub/test.txt"));
EXPECT_EQ(api_error::directory_not_empty, db_->remove_directory("/sub"));
EXPECT_TRUE(db_->is_directory("/sub"));
EXPECT_TRUE(db_->is_file("/sub/test.txt"));
}
TEST_F(directory_db_test, remove_directory_fails_for_root_directory_by_default) {
db_->create_directory("/");
TEST_F(directory_db_test,
remove_directory_fails_for_root_directory_by_default) {
EXPECT_EQ(api_error::success, db_->create_directory("/"));
EXPECT_EQ(api_error::access_denied, db_->remove_directory("/"));
EXPECT_TRUE(db_->is_directory("/"));
}
TEST_F(directory_db_test,
remove_directory_succeeds_for_root_directory_if_allow_remove_root_is_true) {
db_->create_directory("/");
TEST_F(
directory_db_test,
remove_directory_succeeds_for_root_directory_if_allow_remove_root_is_true) {
EXPECT_EQ(api_error::success, db_->create_directory("/"));
EXPECT_EQ(api_error::success, db_->remove_directory("/", true));
EXPECT_FALSE(db_->is_directory("/"));

View File

@ -1,689 +0,0 @@
/*
Copyright <2018-2022> <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 "app_config.hpp"
#include "download/download_manager.hpp"
#include "events/consumers/console_consumer.hpp"
#include "mocks/mock_open_file_table.hpp"
#include "test_common.hpp"
#include "utils/encrypting_reader.hpp"
#include "utils/event_capture.hpp"
#include "utils/file_utils.hpp"
#include "utils/path_utils.hpp"
#include "utils/string_utils.hpp"
#include "utils/utils.hpp"
namespace repertory {
static std::string get_source_file_name() {
return generate_test_file_name("./", "downloader_manager");
}
TEST(downloader_manager, no_hanging_on_download_fail) {
utils::file::delete_directory_recursively(utils::path::absolute("./chunk_data"));
{
app_config config(provider_type::sia, "./chunk_data");
console_consumer c;
event_system::instance().start();
event_capture ec({"download_begin", "download_end"}, {"download_timeout"});
const auto test_source = get_source_file_name();
utils::file::delete_file(test_source);
const auto chunk_size = utils::encryption::encrypting_reader::get_data_chunk_size();
const auto file_size = (chunk_size * 5u) + 2u;
const auto test_dest = get_source_file_name();
utils::file::delete_file(test_dest);
auto nf = create_random_file(test_source, file_size);
EXPECT_NE(nf, nullptr);
if (nf) {
api_reader_callback api_reader = [&](const std::string &, const std::size_t &,
const std::uint64_t &, std::vector<char> &,
const bool &) -> api_error {
#ifdef _WIN32
::SetLastError(ERROR_ACCESS_DENIED);
#else
utils::set_last_error_code(EACCES);
#endif
return api_error::os_error;
};
filesystem_item fsi{};
fsi.api_path = "/test_chunk";
fsi.directory = false;
fsi.lock = std::make_shared<std::recursive_mutex>();
fsi.source_path = test_dest;
fsi.size = file_size;
download_manager dm(config, api_reader, true);
mock_open_file_table oft(&dm, &fsi);
EXPECT_CALL(oft, get_open_count(_)).WillRepeatedly(Return(1ull));
dm.start(&oft);
EXPECT_EQ(api_error::os_error, dm.download_file(1, fsi));
dm.stop();
EXPECT_EQ(0u, dm.get_download_count());
nf->close();
utils::file::delete_file(fsi.source_path);
}
utils::file::delete_file(test_source);
event_system::instance().stop();
}
utils::file::delete_directory_recursively(utils::path::absolute("./chunk_data"));
}
TEST(downloader_manager, single_chunk_no_offset) {
utils::file::delete_directory_recursively(utils::path::absolute("./chunk_data"));
{
app_config config(provider_type::sia, "./chunk_data");
console_consumer c;
event_system::instance().start();
event_capture ec({"download_begin", "download_end", "download_progress"}, {"DownloadTimeout"});
const auto test_source = get_source_file_name();
utils::file::delete_file(test_source);
const auto chunk_size = utils::encryption::encrypting_reader::get_data_chunk_size();
const auto file_size = (chunk_size * 5u);
auto nf = create_random_file(test_source, file_size);
EXPECT_NE(nf, nullptr);
if (nf) {
api_reader_callback api_reader = [&](const std::string &, const std::size_t &size,
const std::uint64_t &offset, std::vector<char> &data,
const bool &) -> api_error {
data.resize(size);
std::size_t bytes_read{};
auto ret = nf->read_bytes(&data[0u], data.size(), offset, bytes_read) ? api_error::success
: api_error::os_error;
EXPECT_EQ(api_error::success, ret);
EXPECT_EQ(bytes_read, data.size());
return ret;
};
filesystem_item fsi{};
fsi.api_path = "/test_chunk";
fsi.directory = false;
fsi.lock = std::make_shared<std::recursive_mutex>();
fsi.source_path = test_source + "0";
fsi.size = file_size;
download_manager dm(config, api_reader, true);
mock_open_file_table oft(&dm, &fsi);
EXPECT_CALL(oft, get_open_count(_)).WillRepeatedly(Return(1ull));
dm.start(&oft);
std::vector<char> read_buffer;
EXPECT_EQ(api_error::success, dm.read_bytes(1, fsi, chunk_size, 0, read_buffer));
EXPECT_EQ(chunk_size, read_buffer.size());
std::size_t bytes_read{};
std::vector<char> source_buffer;
source_buffer.resize(chunk_size);
EXPECT_TRUE(nf->read_bytes(&source_buffer[0u], source_buffer.size(), 0, bytes_read));
EXPECT_EQ(read_buffer.size(), bytes_read);
EXPECT_EQ(read_buffer.size(), source_buffer.size());
if (read_buffer.size() == source_buffer.size()) {
EXPECT_EQ(0, memcmp(&read_buffer[0u], &source_buffer[0u], read_buffer.size()));
}
dm.stop();
EXPECT_EQ(0u, dm.get_download_count());
nf->close();
utils::file::delete_file(fsi.source_path);
}
utils::file::delete_file(test_source);
event_system::instance().stop();
}
utils::file::delete_directory_recursively(utils::path::absolute("./chunk_data"));
}
TEST(downloader_manager, single_chunk_offset_overlap) {
utils::file::delete_directory_recursively(utils::path::absolute("./chunk_data"));
{
app_config config(provider_type::sia, "./chunk_data");
console_consumer c;
event_system::instance().start();
event_capture ec(
{
"download_begin",
"download_end",
"download_progress",
},
{"download_timeout"});
const auto test_source = get_source_file_name();
utils::file::delete_file(test_source);
const auto chunk_size = utils::encryption::encrypting_reader::get_data_chunk_size();
const auto file_size = (chunk_size * 5u);
auto nf = create_random_file(test_source, file_size);
EXPECT_NE(nf, nullptr);
if (nf) {
api_reader_callback api_reader = [&](const std::string &, const std::size_t &size,
const std::uint64_t &offset, std::vector<char> &data,
const bool &) -> api_error {
data.resize(size);
std::size_t bytes_read{};
auto ret = nf->read_bytes(&data[0u], data.size(), offset, bytes_read) ? api_error::success
: api_error::os_error;
EXPECT_EQ(api_error::success, ret);
EXPECT_EQ(bytes_read, data.size());
return ret;
};
filesystem_item fsi{};
fsi.api_path = "/test_chunk";
fsi.directory = false;
fsi.lock = std::make_shared<std::recursive_mutex>();
fsi.source_path = test_source + "0";
fsi.size = file_size;
download_manager dm(config, api_reader, true);
mock_open_file_table oft(&dm, &fsi);
EXPECT_CALL(oft, get_open_count(_)).WillRepeatedly(Return(1ull));
dm.start(&oft);
std::vector<char> read_buffer;
EXPECT_EQ(api_error::success, dm.read_bytes(1, fsi, chunk_size, chunk_size / 2, read_buffer));
EXPECT_EQ(chunk_size, read_buffer.size());
std::size_t bytes_read{};
std::vector<char> source_buffer;
source_buffer.resize(chunk_size);
EXPECT_TRUE(
nf->read_bytes(&source_buffer[0u], source_buffer.size(), chunk_size / 2, bytes_read));
EXPECT_EQ(read_buffer.size(), bytes_read);
EXPECT_EQ(read_buffer.size(), source_buffer.size());
if (read_buffer.size() == source_buffer.size()) {
EXPECT_EQ(0, memcmp(&read_buffer[0u], &source_buffer[0u], read_buffer.size()));
}
dm.stop();
EXPECT_EQ(0, dm.get_download_count());
nf->close();
utils::file::delete_file(fsi.source_path);
utils::file::delete_file(test_source);
event_system::instance().stop();
}
}
utils::file::delete_directory_recursively(utils::path::absolute("./chunk_data"));
}
TEST(downloader_manager, check_no_overflow_on_read_greater_than_file_size) {
utils::file::delete_directory_recursively(utils::path::absolute("./chunk_data"));
{
app_config config(provider_type::sia, "./chunk_data");
console_consumer c;
event_system::instance().start();
event_capture ec({}, {
"download_begin",
"download_end",
"download_timeout",
});
const auto test_source = get_source_file_name();
utils::file::delete_file(test_source);
const auto chunk_size = utils::encryption::encrypting_reader::get_data_chunk_size();
const auto file_size = (chunk_size * 5u);
auto nf = create_random_file(test_source, file_size);
EXPECT_NE(nf, nullptr);
if (nf) {
api_reader_callback api_reader = [&](const std::string &, const std::size_t &size,
const std::uint64_t &offset, std::vector<char> &data,
const bool &) -> api_error {
data.resize(size);
std::size_t bytes_read{};
auto ret = nf->read_bytes(&data[0u], data.size(), offset, bytes_read) ? api_error::success
: api_error::os_error;
EXPECT_EQ(api_error::success, ret);
EXPECT_EQ(bytes_read, data.size());
return ret;
};
filesystem_item fsi{};
fsi.api_path = "/test_chunk";
fsi.directory = false;
fsi.lock = std::make_shared<std::recursive_mutex>();
fsi.source_path = test_source + "0";
fsi.size = file_size;
download_manager dm(config, api_reader, true);
mock_open_file_table oft(&dm, &fsi);
EXPECT_CALL(oft, get_open_count(_)).WillRepeatedly(Return(1ull));
dm.start(&oft);
std::vector<char> data;
EXPECT_EQ(api_error::success, dm.read_bytes(1, fsi, file_size * 2, file_size, data));
EXPECT_EQ(0, data.size());
dm.stop();
EXPECT_EQ(0, dm.get_download_count());
nf->close();
utils::file::delete_file(fsi.source_path);
utils::file::delete_file(test_source);
event_system::instance().stop();
}
}
utils::file::delete_directory_recursively(utils::path::absolute("./chunk_data"));
}
TEST(downloader_manager, check_read_size_greater_than_file_size) {
utils::file::delete_directory_recursively(utils::path::absolute("./chunk_data"));
{
app_config config(provider_type::sia, "./chunk_data");
console_consumer c;
event_system::instance().start();
event_capture ec(
{
"download_begin",
"download_end",
"download_progress",
},
{"download_timeout"});
const auto test_source = get_source_file_name();
utils::file::delete_file(test_source);
const auto chunk_size = utils::encryption::encrypting_reader::get_data_chunk_size();
const auto file_size = (chunk_size * 5u);
auto nf = create_random_file(test_source, file_size);
EXPECT_NE(nf, nullptr);
if (nf) {
api_reader_callback api_reader = [&](const std::string &, const std::size_t &size,
const std::uint64_t &offset, std::vector<char> &data,
const bool &) -> api_error {
data.resize(size);
std::size_t bytes_read{};
auto ret = nf->read_bytes(&data[0u], data.size(), offset, bytes_read) ? api_error::success
: api_error::os_error;
EXPECT_EQ(api_error::success, ret);
EXPECT_EQ(bytes_read, data.size());
return ret;
};
filesystem_item fsi{};
fsi.api_path = "/test_chunk";
fsi.directory = false;
fsi.lock = std::make_shared<std::recursive_mutex>();
fsi.source_path = test_source + "0";
fsi.size = file_size;
download_manager dm(config, api_reader, true);
mock_open_file_table oft(&dm, &fsi);
EXPECT_CALL(oft, get_open_count(_)).WillRepeatedly(Return(1ull));
dm.start(&oft);
std::vector<char> data;
EXPECT_EQ(api_error::success, dm.read_bytes(1, fsi, file_size * 2, 0, data));
EXPECT_EQ(file_size, data.size());
dm.stop();
EXPECT_EQ(0, dm.get_download_count());
nf->close();
utils::file::delete_file(fsi.source_path);
utils::file::delete_file(test_source);
event_system::instance().stop();
}
}
utils::file::delete_directory_recursively(utils::path::absolute("./chunk_data"));
}
TEST(downloader_manager, download_file) {
utils::file::delete_directory_recursively(utils::path::absolute("./chunk_data"));
{
app_config config(provider_type::sia, "./chunk_data");
console_consumer c;
event_system::instance().start();
event_capture ec(
{
"download_begin",
"download_end",
"download_progress",
},
{"download_timeout"});
const auto test_source = get_source_file_name();
utils::file::delete_file(test_source);
const auto chunk_size = utils::encryption::encrypting_reader::get_data_chunk_size();
const auto file_size = (chunk_size * 5u + 8u);
const auto test_dest = get_source_file_name();
utils::file::delete_file(test_dest);
auto nf = create_random_file(test_source, file_size);
EXPECT_NE(nf, nullptr);
if (nf) {
api_reader_callback api_reader = [&](const std::string &, const std::size_t &size,
const std::uint64_t &offset, std::vector<char> &data,
const bool &) -> api_error {
data.resize(size);
std::size_t bytes_read{};
auto ret = nf->read_bytes(&data[0u], data.size(), offset, bytes_read) ? api_error::success
: api_error::os_error;
EXPECT_EQ(api_error::success, ret);
EXPECT_EQ(bytes_read, data.size());
return ret;
};
filesystem_item fsi{};
fsi.api_path = "/test_chunk";
fsi.directory = false;
fsi.lock = std::make_shared<std::recursive_mutex>();
fsi.source_path = test_dest;
fsi.size = file_size;
download_manager dm(config, api_reader, true);
mock_open_file_table oft(&dm, &fsi);
EXPECT_CALL(oft, get_open_count(_)).WillRepeatedly(Return(1ull));
dm.start(&oft);
EXPECT_EQ(api_error::success, dm.download_file(1, fsi));
std::uint64_t source_size;
EXPECT_TRUE(utils::file::get_file_size(test_source, source_size));
std::uint64_t current_size;
EXPECT_TRUE(utils::file::get_file_size(fsi.source_path, current_size));
EXPECT_EQ(source_size, current_size);
EXPECT_STRCASEEQ(utils::file::generate_sha256(test_source).c_str(),
utils::file::generate_sha256(fsi.source_path).c_str());
dm.stop();
EXPECT_EQ(0, dm.get_download_count());
nf->close();
utils::file::delete_file(fsi.source_path);
}
utils::file::delete_file(test_source);
event_system::instance().stop();
}
utils::file::delete_directory_recursively(utils::path::absolute("./chunk_data"));
}
TEST(downloader_manager, download_timeout) {
console_consumer c;
event_system::instance().start();
utils::file::delete_directory_recursively(utils::path::absolute("./chunk_data"));
{
app_config config(provider_type::sia, "./chunk_data");
config.set_chunk_downloader_timeout_secs(5);
config.set_enable_chunk_downloader_timeout(true);
event_capture ec({
"filesystem_item_closed",
"download_timeout",
"download_begin",
"download_end",
});
const auto test_source = get_source_file_name();
utils::file::delete_file(test_source);
const auto chunk_size = utils::encryption::encrypting_reader::get_data_chunk_size();
const auto file_size = (chunk_size * 5u) + 2;
const auto test_dest = get_source_file_name();
utils::file::delete_file(test_dest);
auto nf = create_random_file(test_source, file_size);
EXPECT_NE(nf, nullptr);
if (nf) {
filesystem_item *pfsi = nullptr;
auto sent = false;
api_reader_callback api_reader = [&](const std::string &, const std::size_t &,
const std::uint64_t &, std::vector<char> &,
const bool &stop_requested) -> api_error {
if (not sent) {
sent = true;
event_system::instance().raise<filesystem_item_closed>(pfsi->api_path, pfsi->source_path,
false, false);
}
while (not stop_requested) {
std::this_thread::sleep_for(1ms);
}
return api_error::download_failed;
};
filesystem_item fsi{};
fsi.api_path = "/test_chunk";
fsi.directory = false;
fsi.lock = std::make_shared<std::recursive_mutex>();
fsi.source_path = test_dest;
fsi.size = file_size;
pfsi = &fsi;
download_manager dm(config, api_reader, true);
mock_open_file_table oft;
EXPECT_CALL(oft, get_open_count(_)).WillRepeatedly(Return(0ull));
dm.start(&oft);
EXPECT_EQ(api_error::download_timeout, dm.download_file(1, fsi));
dm.stop();
EXPECT_EQ(0, dm.get_download_count());
nf->close();
utils::file::delete_file(fsi.source_path);
utils::file::delete_file(test_source);
}
event_system::instance().stop();
}
utils::file::delete_directory_recursively(utils::path::absolute("./chunk_data"));
}
TEST(downloader_manager, download_pause_resume) {
utils::file::delete_directory_recursively(utils::path::absolute("./chunk_data"));
{
app_config config(provider_type::sia, "./chunk_data");
console_consumer c;
event_system::instance().start();
event_capture ec(
{
"download_begin",
"download_end",
"download_progress",
"download_paused",
"download_resumed",
},
{"download_timeout"});
const auto test_source = get_source_file_name();
utils::file::delete_file(test_source);
const auto chunk_size = utils::encryption::encrypting_reader::get_data_chunk_size();
const auto file_size = (chunk_size * 50u);
const auto test_dest = get_source_file_name();
utils::file::delete_file(test_dest);
auto nf = create_random_file(test_source, file_size);
EXPECT_NE(nf, nullptr);
if (nf) {
api_reader_callback api_reader = [&](const std::string &, const std::size_t &size,
const std::uint64_t &offset, std::vector<char> &data,
const bool &) -> api_error {
data.resize(size);
std::size_t bytes_read{};
auto ret = nf->read_bytes(&data[0u], data.size(), offset, bytes_read) ? api_error::success
: api_error::os_error;
EXPECT_EQ(api_error::success, ret);
EXPECT_EQ(bytes_read, data.size());
return ret;
};
filesystem_item fsi{};
fsi.api_path = "/test_chunk";
fsi.directory = false;
fsi.lock = std::make_shared<std::recursive_mutex>();
fsi.source_path = test_dest;
fsi.size = file_size;
download_manager dm(config, api_reader, true);
mock_open_file_table oft(&dm, &fsi);
EXPECT_CALL(oft, get_open_count(_)).WillRepeatedly(Return(1ull));
dm.start(&oft);
std::thread th([&]() { EXPECT_EQ(api_error::success, dm.download_file(1, fsi)); });
EXPECT_TRUE(ec.wait_for_event("download_begin"));
EXPECT_TRUE(dm.pause_download(fsi.api_path));
dm.rename_download(fsi.api_path, fsi.api_path + "_cow");
fsi.api_path += "_cow";
dm.resume_download(fsi.api_path);
th.join();
std::uint64_t source_size;
EXPECT_TRUE(utils::file::get_file_size(test_source, source_size));
std::uint64_t current_size;
EXPECT_TRUE(utils::file::get_file_size(fsi.source_path, current_size));
EXPECT_EQ(source_size, current_size);
EXPECT_STRCASEEQ(utils::file::generate_sha256(test_source).c_str(),
utils::file::generate_sha256(fsi.source_path).c_str());
dm.stop();
EXPECT_EQ(0, dm.get_download_count());
nf->close();
utils::file::delete_file(fsi.source_path);
}
utils::file::delete_file(test_source);
event_system::instance().stop();
}
utils::file::delete_directory_recursively(utils::path::absolute("./chunk_data"));
}
TEST(downloader_manager, store_and_resume_incomplete_download_after_write) {
utils::file::delete_directory_recursively(utils::path::absolute("./chunk_data"));
{
app_config config(provider_type::sia, "./chunk_data");
console_consumer c;
event_system::instance().start();
const auto test_source = get_source_file_name();
utils::file::delete_file(test_source);
const auto chunk_size = utils::encryption::encrypting_reader::get_data_chunk_size();
const auto file_size = (chunk_size * 10u);
const auto test_dest = utils::path::absolute("./test_chunk_dest");
utils::file::delete_file(test_dest);
auto nf = create_random_file(test_source, file_size);
EXPECT_NE(nf, nullptr);
if (nf) {
api_reader_callback api_reader = [&](const std::string &, const std::size_t &size,
const std::uint64_t &offset, std::vector<char> &data,
const bool &) -> api_error {
data.resize(size);
std::size_t bytes_read{};
auto ret = nf->read_bytes(&data[0u], data.size(), offset, bytes_read) ? api_error::success
: api_error::os_error;
EXPECT_EQ(api_error::success, ret);
EXPECT_EQ(bytes_read, data.size());
std::this_thread::sleep_for(2s);
return ret;
};
filesystem_item fsi{};
fsi.api_path = "/test_chunk";
fsi.directory = false;
fsi.lock = std::make_shared<std::recursive_mutex>();
fsi.source_path = test_dest;
fsi.size = file_size;
download_manager dm(config, api_reader, true);
mock_open_file_table oft(&dm, &fsi);
EXPECT_CALL(oft, get_open_count(_)).WillRepeatedly(Return(0ull));
filesystem_item fsi2{};
EXPECT_CALL(oft, force_schedule_upload(_)).WillOnce(DoAll(SaveArg<0>(&fsi2), Return()));
EXPECT_CALL(oft, open(_, _))
.WillOnce(DoAll(SetArgReferee<1>(1u), Return(api_error::success)));
EXPECT_CALL(oft, close(1u)).WillOnce(Return());
{
event_capture ec({
"download_begin",
"download_end",
"download_progress",
"download_stored",
});
dm.start(&oft);
std::size_t bytes_written{};
EXPECT_EQ(api_error::success,
dm.write_bytes(1, fsi, chunk_size - 2u, {'a', 'a', 'a', 'a'}, bytes_written));
dm.stop();
ec.wait_for_event("download_stored");
}
{
event_capture ec({
"download_begin",
"download_end",
"download_progress",
"download_restored",
});
dm.start(&oft);
ec.wait_for_event("download_restored");
ec.wait_for_event("download_end");
dm.stop();
}
nf->close();
utils::file::delete_file(fsi.source_path);
}
utils::file::delete_file(test_source);
event_system::instance().stop();
}
utils::file::delete_directory_recursively(utils::path::absolute("./chunk_data"));
}
} // namespace repertory

View File

@ -1,221 +0,0 @@
/*
Copyright <2018-2022> <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 "app_config.hpp"
#include "download/download.hpp"
#include "mocks/mock_open_file_table.hpp"
#include "test_common.hpp"
#include "utils/event_capture.hpp"
#include "utils/path_utils.hpp"
namespace repertory {
static const std::size_t READ_SIZE = 1024u * 1024u;
static std::string get_source_file_name() { return generate_test_file_name("./", "download"); }
static void write_and_test(download &d, const std::vector<char> &buffer, const bool &to_end) {
if (to_end) {
std::size_t bytes_written{};
EXPECT_EQ(api_error::success, d.write_bytes(0u, 20u * READ_SIZE, buffer, bytes_written,
[](std::uint64_t, std::uint64_t, bool) {}));
std::vector<char> data;
EXPECT_EQ(api_error::success, d.read_bytes(0u, buffer.size(), 20u * READ_SIZE, data));
EXPECT_EQ(std::vector<char>(buffer), data);
} else {
std::size_t bytes_written{};
EXPECT_EQ(api_error::success,
d.write_bytes(0u, READ_SIZE + READ_SIZE - 2u, buffer, bytes_written,
[](std::uint64_t, std::uint64_t, bool) {}));
std::vector<char> data;
EXPECT_EQ(api_error::success, d.read_bytes(0u, 4u, READ_SIZE + READ_SIZE - 2u, data));
EXPECT_EQ(std::vector<char>(buffer), data);
}
}
TEST(download, write_non_cached_file) {
mock_open_file_table oft;
console_consumer c;
event_system::instance().start();
utils::file::delete_directory_recursively(utils::path::absolute("./download_data"));
const auto source_file_path = get_source_file_name();
const auto source_file_size = 20u * READ_SIZE;
utils::file::delete_file(source_file_path);
auto source_file = create_random_file(source_file_path, source_file_size);
EXPECT_CALL(oft, get_open_count(_)).WillRepeatedly(Return(0ull));
filesystem_item fsi2{};
EXPECT_CALL(oft, force_schedule_upload(_))
.Times(2)
.WillRepeatedly(DoAll(SaveArg<0>(&fsi2), Return()));
api_reader_callback api_reader = [&](const std::string &, const std::size_t &size,
const std::uint64_t &offset, std::vector<char> &data,
const bool &) -> api_error {
data.resize(size);
const auto chunk = (offset / READ_SIZE);
if (chunk < 1u || (chunk > 2u)) {
std::this_thread::sleep_for(100ms);
}
std::size_t bytes_read{};
auto ret = source_file->read_bytes(&data[0u], data.size(), offset, bytes_read)
? api_error::success
: api_error::os_error;
if (ret != api_error::success) {
std::cout << utils::get_last_error_code() << std::endl;
}
EXPECT_EQ(api_error::success, ret);
EXPECT_EQ(bytes_read, data.size());
return ret;
};
filesystem_item fsi{};
fsi.api_path = "/Test.dat";
fsi.size = source_file_size;
fsi.api_parent = "/";
fsi.directory = false;
fsi.lock = std::make_shared<std::recursive_mutex>();
{
event_capture ec({"download_begin", "download_end", "download_progress"});
app_config config(provider_type::sia, "./download_data");
{
download d(config, fsi, api_reader, READ_SIZE, oft);
write_and_test(d, {'a', 'a', 'a', 'a'}, false);
ec.wait_for_event("download_end");
const auto new_source_file_path = d.get_source_path();
EXPECT_TRUE(utils::file::is_file(new_source_file_path));
EXPECT_STRNE(source_file_path.c_str(), new_source_file_path.c_str());
EXPECT_STREQ(new_source_file_path.c_str(), fsi2.source_path.c_str());
EXPECT_STREQ(fsi.api_path.c_str(), fsi2.api_path.c_str());
EXPECT_FALSE(fsi2.source_path_changed);
EXPECT_FALSE(fsi2.changed);
write_and_test(d, {'b', 'b', 'b', 'b'}, false);
filesystem_item fsi3{};
boost::dynamic_bitset<> read_state, write_state;
std::size_t chunk_size{}, last_chunk_size{};
d.get_state_information(fsi3, chunk_size, last_chunk_size, read_state, write_state);
EXPECT_STREQ(fsi2.source_path.c_str(), fsi3.source_path.c_str());
EXPECT_FALSE(write_state[0]);
EXPECT_TRUE(write_state[1]);
EXPECT_TRUE(write_state[2]);
for (std::size_t i = 3u; i < write_state.size(); i++) {
EXPECT_FALSE(write_state[i]);
}
EXPECT_TRUE(read_state.all());
}
}
source_file->close();
event_system::instance().stop();
utils::file::delete_directory_recursively(utils::path::absolute("./download_data"));
utils::file::delete_file(source_file_path);
}
TEST(Download, write_non_cached_file_and_grow_size) {
mock_open_file_table oft;
console_consumer c;
event_system::instance().start();
utils::file::delete_directory_recursively(utils::path::absolute("./download_data"));
const auto source_file_path = get_source_file_name();
const auto source_file_size = 20u * READ_SIZE;
utils::file::delete_file(source_file_path);
auto sourceFile = create_random_file(source_file_path, source_file_size);
EXPECT_CALL(oft, get_open_count(_)).WillRepeatedly(Return(0ull));
filesystem_item fsi2{};
EXPECT_CALL(oft, force_schedule_upload(_)).WillOnce(DoAll(SaveArg<0>(&fsi2), Return()));
api_reader_callback api_reader = [&](const std::string &, const std::size_t &size,
const std::uint64_t &offset, std::vector<char> &data,
const bool &) -> api_error {
data.resize(size);
const auto chunk = (offset / READ_SIZE);
if (chunk < 20u) {
std::this_thread::sleep_for(100ms);
}
std::size_t bytes_read{};
auto ret = sourceFile->read_bytes(&data[0u], data.size(), offset, bytes_read)
? api_error::success
: api_error::os_error;
if (ret != api_error::success) {
std::cout << utils::get_last_error_code() << std::endl;
}
EXPECT_EQ(api_error::success, ret);
EXPECT_EQ(bytes_read, data.size());
return ret;
};
filesystem_item fsi{};
fsi.api_path = "/Test.dat";
fsi.size = source_file_size;
fsi.api_parent = "/";
fsi.directory = false;
fsi.lock = std::make_shared<std::recursive_mutex>();
{
event_capture ec({"download_begin", "download_end", "download_progress"});
app_config config(provider_type::sia, "./download_data");
{
download d(config, fsi, api_reader, READ_SIZE, oft);
write_and_test(d, {'a', 'a', 'a', 'a'}, true);
ec.wait_for_event("download_end");
const auto new_source_file_path = d.get_source_path();
EXPECT_TRUE(utils::file::is_file(new_source_file_path));
EXPECT_STRNE(source_file_path.c_str(), new_source_file_path.c_str());
EXPECT_STREQ(new_source_file_path.c_str(), fsi2.source_path.c_str());
EXPECT_STREQ(fsi.api_path.c_str(), fsi2.api_path.c_str());
EXPECT_FALSE(fsi2.source_path_changed);
EXPECT_FALSE(fsi2.changed);
EXPECT_EQ(source_file_size + 4u, fsi2.size);
filesystem_item fsi3{};
boost::dynamic_bitset<> read_state, write_state;
std::size_t chunk_size{}, last_chunk_size{};
d.get_state_information(fsi3, chunk_size, last_chunk_size, read_state, write_state);
EXPECT_STREQ(fsi2.source_path.c_str(), fsi3.source_path.c_str());
EXPECT_EQ(21u, write_state.size());
EXPECT_EQ(write_state.size(), read_state.size());
EXPECT_EQ(source_file_size + 4u, fsi3.size);
for (std::size_t i = 0u; i < write_state.size() - 1u; i++) {
EXPECT_FALSE(write_state[i]);
}
EXPECT_TRUE(write_state[20u]);
EXPECT_TRUE(read_state.all());
}
}
sourceFile->close();
event_system::instance().stop();
utils::file::delete_directory_recursively(utils::path::absolute("./download_data"));
utils::file::delete_file(source_file_path);
}
} // namespace repertory

View File

@ -1,429 +0,0 @@
/*
Copyright <2018-2022> <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 "app_config.hpp"
#include "download/direct_download.hpp"
#include "download/download.hpp"
#include "download/ring_download.hpp"
#include "mocks/mock_open_file_table.hpp"
#include "test_common.hpp"
#include "utils/file_utils.hpp"
#include "utils/path_utils.hpp"
namespace repertory {
static const std::size_t READ_SIZE = 1024u * 1024u;
static const std::int32_t DOWNLOADER_COUNT = 3;
static std::string get_source_file_name() { return generate_test_file_name("./", "downloaders"); }
static std::shared_ptr<i_download> create_download(const std::int32_t &index, app_config &config,
filesystem_item &fsi,
api_reader_callback &api_reader,
i_open_file_table &oft) {
return (index == 0) ? std::dynamic_pointer_cast<i_download>(std::make_shared<ring_download>(
config, fsi, api_reader, 0, READ_SIZE, 6u * READ_SIZE))
: (index == 1) ? std::dynamic_pointer_cast<i_download>(
std::make_shared<direct_download>(config, fsi, api_reader, 0))
: std::dynamic_pointer_cast<i_download>(std::make_shared<download>(
config, fsi, api_reader, 6u * READ_SIZE, oft));
}
static void run_full_file_test(const std::size_t &max_read_size,
const std::size_t &source_file_size) {
const auto source_file_path = get_source_file_name();
{
console_consumer c;
utils::file::delete_directory_recursively(utils::path::absolute("./downloaders_data"));
app_config config(provider_type::sia, "./downloaders_data");
utils::file::delete_file(source_file_path);
auto source_file = create_random_file(source_file_path, source_file_size);
EXPECT_NE(source_file, nullptr);
if (source_file) {
api_reader_callback api_reader =
[&source_file](const std::string &, const std::size_t &size, const std::uint64_t &offset,
std::vector<char> &data, const bool &) -> api_error {
data.resize(size);
std::size_t bytes_read{};
auto ret = source_file->read_bytes(&data[0u], data.size(), offset, bytes_read)
? api_error::success
: api_error::os_error;
if (ret != api_error::success) {
std::cout << utils::get_last_error_code() << std::endl;
}
EXPECT_EQ(api_error::success, ret);
EXPECT_EQ(bytes_read, data.size());
return ret;
};
event_system::instance().start();
for (auto i = 0; i < DOWNLOADER_COUNT; i++) {
filesystem_item fsi{};
fsi.api_path = "/Test.dat";
fsi.size = source_file_size;
fsi.api_parent = "/";
fsi.directory = false;
fsi.lock = std::make_shared<std::recursive_mutex>();
mock_open_file_table oft;
EXPECT_CALL(oft, get_open_count(_)).WillRepeatedly(Return(1ull));
auto download = create_download(i, config, fsi, api_reader, oft);
auto size_remain = source_file_size;
auto result_code = api_error::success;
auto offset = 0ull;
auto total_read = 0ull;
while ((result_code == api_error::success) && (size_remain > 0)) {
const auto read_size = std::min(max_read_size, static_cast<std::size_t>(size_remain));
std::vector<char> data;
if ((result_code = download->read_bytes(0u, read_size, offset, data)) ==
api_error::success) {
total_read += data.size();
EXPECT_EQ(read_size, data.size());
if (data.size() == read_size) {
std::size_t bytes_read = 0;
std::vector<char> data2(data.size());
result_code = source_file->read_bytes(&data2[0u], data2.size(), offset, bytes_read)
? api_error::success
: api_error::os_error;
EXPECT_EQ(data.size(), data2.size());
EXPECT_EQ(api_error::success, result_code);
const auto comp = memcmp(&data[0u], &data2[0u], data.size());
EXPECT_EQ(0, comp);
}
size_remain -= read_size;
offset += read_size;
}
EXPECT_EQ(api_error::success, result_code);
}
EXPECT_EQ(source_file_size, total_read);
download.reset();
}
source_file->close();
}
event_system::instance().stop();
}
utils::file::delete_file(source_file_path);
utils::file::delete_directory_recursively(utils::path::absolute("./downloaders_data"));
}
static void run_read_past_full_buffer_test() {
const auto max_read_size = READ_SIZE;
const auto source_file_path = get_source_file_name();
const auto source_file_size = 20u * READ_SIZE;
utils::file::delete_directory_recursively(utils::path::absolute("./downloaders_data"));
{
app_config config(provider_type::sia, "./downloaders_data");
utils::file::delete_file(source_file_path);
console_consumer c;
auto source_file = create_random_file(source_file_path, source_file_size);
EXPECT_NE(source_file, nullptr);
if (source_file) {
api_reader_callback api_reader = [&](const std::string &, const std::size_t &size,
const std::uint64_t &offset, std::vector<char> &data,
const bool &) -> api_error {
data.resize(size);
std::size_t bytes_read{};
const auto ret = source_file->read_bytes(&data[0u], data.size(), offset, bytes_read)
? api_error::success
: api_error::os_error;
EXPECT_EQ(api_error::success, ret);
EXPECT_EQ(bytes_read, data.size());
return ret;
};
event_system::instance().start();
for (auto i = 0; i < DOWNLOADER_COUNT; i++) {
filesystem_item fsi{};
fsi.api_path = "/Test.dat";
fsi.size = source_file_size;
fsi.api_parent = "/";
fsi.directory = false;
fsi.lock = std::make_shared<std::recursive_mutex>();
mock_open_file_table oft;
EXPECT_CALL(oft, get_open_count(_)).WillRepeatedly(Return(1ull));
auto download = create_download(i, config, fsi, api_reader, oft);
auto size_remain = source_file_size;
auto result_code = api_error::success;
auto offset = 0ull;
while ((result_code == api_error::success) && (size_remain > 0)) {
const auto read_size = std::min(max_read_size, static_cast<std::size_t>(size_remain));
std::vector<char> data;
if ((result_code = download->read_bytes(0u, read_size, offset, data)) ==
api_error::success) {
EXPECT_EQ(read_size, data.size());
if (data.size() == read_size) {
std::size_t bytes_read = 0;
std::vector<char> data2(data.size());
result_code = source_file->read_bytes(&data2[0u], data2.size(), offset, bytes_read)
? api_error::success
: api_error::os_error;
EXPECT_EQ(api_error::success, result_code);
EXPECT_EQ(data.size(), bytes_read);
const auto comp = memcmp(&data[0u], &data2[0u], data.size());
EXPECT_EQ(0, comp);
}
if (size_remain == source_file_size) {
size_remain -= (read_size + (6ull * READ_SIZE));
offset += (read_size + (6ull * READ_SIZE));
} else {
size_remain -= read_size;
offset += read_size;
}
}
EXPECT_EQ(api_error::success, result_code);
}
download.reset();
}
source_file->close();
utils::file::delete_file(source_file_path);
}
event_system::instance().stop();
}
utils::file::delete_directory_recursively(utils::path::absolute("./downloaders_data"));
}
static void run_read_with_seek_behind() {
const auto max_read_size = READ_SIZE;
const auto source_file_path = get_source_file_name();
const auto source_file_size = 20u * READ_SIZE;
utils::file::delete_directory_recursively(utils::path::absolute("./downloaders_data"));
{
app_config config(provider_type::sia, "./downloaders_data");
utils::file::delete_file(source_file_path);
console_consumer c;
auto source_file = create_random_file(source_file_path, source_file_size);
EXPECT_NE(source_file, nullptr);
if (source_file) {
api_reader_callback api_reader = [&](const std::string &, const std::size_t &size,
const std::uint64_t &offset, std::vector<char> &data,
const bool &) -> api_error {
data.resize(size);
std::size_t bytes_read{};
auto ret = source_file->read_bytes(&data[0u], data.size(), offset, bytes_read)
? api_error::success
: api_error::os_error;
EXPECT_EQ(api_error::success, ret);
EXPECT_EQ(bytes_read, data.size());
return ret;
};
event_system::instance().start();
for (auto i = 0; i < DOWNLOADER_COUNT; i++) {
filesystem_item fsi{};
fsi.api_path = "/Test.dat";
fsi.size = source_file_size;
fsi.api_parent = "/";
fsi.directory = false;
fsi.lock = std::make_shared<std::recursive_mutex>();
mock_open_file_table oft;
EXPECT_CALL(oft, get_open_count(_)).WillRepeatedly(Return(1ull));
auto download = create_download(i, config, fsi, api_reader, oft);
auto size_remain = source_file_size;
auto result_code = api_error::success;
auto offset = 0ull;
auto read_count = 0;
while ((result_code == api_error::success) && (size_remain > 0)) {
const auto read_size = std::min(max_read_size, static_cast<std::size_t>(size_remain));
std::vector<char> data;
if ((result_code = download->read_bytes(0u, read_size, offset, data)) ==
api_error::success) {
EXPECT_EQ(read_size, data.size());
if (data.size() == read_size) {
std::size_t bytes_read = 0;
std::vector<char> data2(data.size());
result_code = source_file->read_bytes(&data2[0u], data2.size(), offset, bytes_read)
? api_error::success
: api_error::os_error;
EXPECT_EQ(api_error::success, result_code);
EXPECT_EQ(data.size(), bytes_read);
const auto comp = memcmp(&data[0u], &data2[0u], data.size());
EXPECT_EQ(0, comp);
}
if (++read_count == 3) {
size_remain += (read_size * 2);
offset -= (read_size * 2);
} else {
size_remain -= read_size;
offset += read_size;
}
}
EXPECT_EQ(api_error::success, result_code);
}
download.reset();
}
source_file->close();
utils::file::delete_file(source_file_path);
}
event_system::instance().stop();
}
utils::file::delete_directory_recursively(utils::path::absolute("./downloaders_data"));
}
static void run_seek_begin_to_end_to_begin() {
const auto max_read_size = READ_SIZE;
const auto source_file_path = get_source_file_name();
const auto source_file_size = 20u * READ_SIZE;
utils::file::delete_directory_recursively(utils::path::absolute("./downloaders_data"));
{
app_config config(provider_type::sia, "./downloaders_data");
utils::file::delete_file(source_file_path);
console_consumer c;
auto source_file = create_random_file(source_file_path, source_file_size);
EXPECT_NE(source_file, nullptr);
if (source_file) {
api_reader_callback api_reader = [&](const std::string &, const std::size_t &size,
const std::uint64_t &offset, std::vector<char> &data,
const bool &) -> api_error {
data.resize(size);
std::size_t bytes_read{};
auto ret = source_file->read_bytes(&data[0u], data.size(), offset, bytes_read)
? api_error::success
: api_error::os_error;
EXPECT_EQ(api_error::success, ret);
EXPECT_EQ(bytes_read, data.size());
return ret;
};
event_system::instance().start();
for (auto i = 0; i < DOWNLOADER_COUNT; i++) {
filesystem_item fsi{};
fsi.api_path = "/Test.dat";
fsi.size = source_file_size;
fsi.api_parent = "/";
fsi.directory = false;
fsi.lock = std::make_shared<std::recursive_mutex>();
mock_open_file_table oft;
EXPECT_CALL(oft, get_open_count(_)).WillRepeatedly(Return(1ull));
auto download = create_download(i, config, fsi, api_reader, oft);
auto size_remain = source_file_size;
auto result_code = api_error::success;
auto offset = 0ull;
auto readCount = 0;
while ((result_code == api_error::success) && (readCount < 4)) {
const auto read_size = std::min(max_read_size, static_cast<std::size_t>(size_remain));
std::vector<char> data;
if ((result_code = download->read_bytes(0u, read_size, offset, data)) ==
api_error::success) {
EXPECT_EQ(read_size, data.size());
if (data.size() == read_size) {
std::size_t bytes_read = 0;
std::vector<char> data2(data.size());
result_code = source_file->read_bytes(&data2[0u], data2.size(), offset, bytes_read)
? api_error::success
: api_error::os_error;
EXPECT_EQ(api_error::success, result_code);
EXPECT_EQ(data.size(), bytes_read);
const auto comp = memcmp(&data[0u], &data2[0u], data.size());
EXPECT_EQ(0, comp);
if (comp) {
std::cout << "mismatch-" << read_size << ':' << offset << std::endl;
}
}
++readCount;
if (readCount == 1) {
size_remain = read_size;
offset = source_file_size - read_size;
} else if (readCount == 2) {
std::this_thread::sleep_for(10ms);
size_remain = source_file_size;
offset = 0ull;
} else {
size_remain -= read_size;
offset += read_size;
}
}
EXPECT_EQ(api_error::success, result_code);
}
download.reset();
}
source_file->close();
utils::file::delete_file(source_file_path);
}
event_system::instance().stop();
}
utils::file::delete_directory_recursively(utils::path::absolute("./downloaders_data"));
}
TEST(downloaders, read_full_file) {
const auto max_read_size = READ_SIZE;
const auto source_file_size = 20u * READ_SIZE;
run_full_file_test(max_read_size, source_file_size);
}
TEST(downloaders, read_full_file_with_overlapping_chunks) {
const auto max_read_size = READ_SIZE + (READ_SIZE / 2);
const auto source_file_size = 20u * READ_SIZE;
run_full_file_test(max_read_size, source_file_size);
}
TEST(downloaders, read_full_file_with_non_matching_chunk_size) {
const auto max_read_size = READ_SIZE;
const auto source_file_size = (20u * READ_SIZE) + 252u;
run_full_file_test(max_read_size, source_file_size);
}
TEST(downloaders, read_full_file_with_partial_reads) {
const auto max_read_size = 32u * 1024u;
const auto source_file_size = 20u * READ_SIZE;
run_full_file_test(max_read_size, source_file_size);
}
TEST(downloaders, read_full_file_with_partial_overlapping_reads) {
const auto max_read_size = (READ_SIZE / 2) + 20u;
const auto source_file_size = 20u * READ_SIZE;
run_full_file_test(max_read_size, source_file_size);
}
TEST(downloaders, read_past_full_buffer) { run_read_past_full_buffer_test(); }
TEST(downloaders, read_with_seek_behind) { run_read_with_seek_behind(); }
TEST(downloaders, seek_begin_to_end_to_begin) { run_seek_begin_to_end_to_begin(); }
} // namespace repertory

View File

@ -0,0 +1 @@
test

1
tests/encrypt/test.txt Normal file
View File

@ -0,0 +1 @@
moose

View File

@ -0,0 +1,172 @@
/*
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_TRUE(list.at(idx).encryption_token.empty());
EXPECT_TRUE(list2.at(idx).encryption_token.empty());
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,22 +1,27 @@
/*
Copyright <2018-2022> <scott.e.graves@protonmail.com>
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
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 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.
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"
@ -28,209 +33,240 @@ static std::string get_source_file_name() {
TEST(encrypting_reader, get_encrypted_file_name) {
const auto source_file_name = get_source_file_name();
utils::file::delete_file(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) {
bool stop_requested = false;
utils::encryption::encrypting_reader reader("test.dat", source_file_name, stop_requested,
token);
stop_type stop_requested = false;
utils::encryption::encrypting_reader reader("test.dat", source_file_name,
stop_requested, token);
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));
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();
}
utils::file::delete_file(source_file_name);
EXPECT_TRUE(utils::file::retry_delete_file(source_file_name));
}
TEST(encrypting_reader, file_data) {
const auto source_file_name = get_source_file_name();
utils::file::delete_file(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());
source_file_name,
8u * utils::encryption::encrypting_reader::get_data_chunk_size());
EXPECT_TRUE(source_file != nullptr);
if (source_file) {
bool stop_requested = false;
utils::encryption::encrypting_reader reader("test.dat", source_file_name, stop_requested,
token);
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++) {
std::vector<char> buffer(utils::encryption::encrypting_reader::get_encrypted_chunk_size());
data_buffer buffer(
utils::encryption::encrypting_reader::get_encrypted_chunk_size());
for (std::uint8_t j = 0u; j < 2u; j++) {
utils::encryption::encrypting_reader::reader_function(&buffer[(buffer.size() / 2u) * j],
buffer.size() / 2u, 1u, &reader);
EXPECT_EQ(buffer.size() / 2u,
utils::encryption::encrypting_reader::reader_function(
&buffer[(buffer.size() / 2u) * j], buffer.size() / 2u, 1u,
&reader));
}
std::vector<char> decrypted_data;
EXPECT_TRUE(utils::encryption::decrypt_data(token, buffer, decrypted_data));
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());
EXPECT_EQ(utils::encryption::encrypting_reader::get_data_chunk_size(),
decrypted_data.size());
std::size_t bytes_read{};
std::vector<char> file_data(decrypted_data.size());
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()));
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();
}
utils::file::delete_file(source_file_name);
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();
utils::file::delete_file(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());
source_file_name,
8u * utils::encryption::encrypting_reader::get_data_chunk_size());
EXPECT_TRUE(source_file != nullptr);
if (source_file) {
bool stop_requested = false;
utils::encryption::encrypting_reader reader("test.dat", source_file_name, stop_requested,
token);
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) {
std::vector<char> buffer(utils::encryption::encrypting_reader::get_encrypted_chunk_size() *
2u);
utils::encryption::encrypting_reader::reader_function(&buffer[0u], buffer.size(), 1u,
&reader);
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++) {
std::vector<char> decrypted_data;
data_buffer decrypted_data;
const auto offset = (j * (buffer.size() / 2u));
EXPECT_TRUE(utils::encryption::decrypt_data(
token,
std::vector<char>(buffer.begin() + offset,
buffer.begin() + offset + (buffer.size() / 2u)),
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{};
std::vector<char> file_data(decrypted_data.size());
source_file->read_bytes(
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()));
(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();
}
utils::file::delete_file(source_file_name);
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();
utils::file::delete_file(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());
source_file_name,
8u * utils::encryption::encrypting_reader::get_data_chunk_size());
EXPECT_TRUE(source_file != nullptr);
if (source_file) {
bool stop_requested = false;
utils::encryption::encrypting_reader reader("test.dat", source_file_name, stop_requested,
token);
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(), io_stream->tellg());
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++) {
std::vector<char> buffer(utils::encryption::encrypting_reader::get_encrypted_chunk_size());
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_FALSE(
io_stream
->read(&buffer[(buffer.size() / 2u) * j], buffer.size() / 2u)
.fail());
EXPECT_TRUE(io_stream->good());
}
std::vector<char> decrypted_data;
EXPECT_TRUE(utils::encryption::decrypt_data(token, buffer, decrypted_data));
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());
EXPECT_EQ(utils::encryption::encrypting_reader::get_data_chunk_size(),
decrypted_data.size());
std::size_t bytes_read{};
std::vector<char> file_data(decrypted_data.size());
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()));
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();
}
utils::file::delete_file(source_file_name);
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();
utils::file::delete_file(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());
source_file_name,
8u * utils::encryption::encrypting_reader::get_data_chunk_size());
EXPECT_TRUE(source_file != nullptr);
if (source_file) {
bool stop_requested = false;
utils::encryption::encrypting_reader reader("test.dat", source_file_name, stop_requested,
token);
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(), io_stream->tellg());
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) {
std::vector<char> buffer(utils::encryption::encrypting_reader::get_encrypted_chunk_size() *
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++) {
std::vector<char> decrypted_data;
data_buffer decrypted_data;
const auto offset = (j * (buffer.size() / 2u));
EXPECT_TRUE(utils::encryption::decrypt_data(
token,
std::vector<char>(buffer.begin() + offset,
buffer.begin() + offset + (buffer.size() / 2u)),
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{};
std::vector<char> file_data(decrypted_data.size());
source_file->read_bytes(
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()));
(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();
}
utils::file::delete_file(source_file_name);
EXPECT_TRUE(utils::file::retry_delete_file(source_file_name));
}
} // namespace repertory

View File

@ -1,22 +1,27 @@
/*
Copyright <2018-2022> <scott.e.graves@protonmail.com>
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:
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 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.
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"
@ -24,8 +29,10 @@ namespace repertory {
static const std::string buffer = "cow moose dog chicken";
static const std::string token = "moose";
static void test_encrypted_result(const std::vector<char> &result) {
EXPECT_EQ(buffer.size() + utils::encryption::encrypting_reader::get_header_size(), result.size());
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());
@ -35,42 +42,45 @@ static void test_encrypted_result(const std::vector<char> &result) {
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());
EXPECT_STREQ(
"182072537ada59e4d6b18034a80302ebae935f66adbdf0f271d3d36309c2d481",
str.c_str());
}
TEST(encryption, encrypt_data_buffer) {
std::vector<char> result;
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);
std::vector<char> result;
data_buffer result;
utils::encryption::encrypt_data(key, buffer, result);
test_encrypted_result(result);
}
TEST(encryption, encrypt_data_pointer) {
std::vector<char> result;
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);
std::vector<char> result;
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);
std::vector<char> result;
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_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());
@ -78,7 +88,7 @@ TEST(encryption, decrypt_data_pointer) {
TEST(encryption, decrypt_data_buffer_with_key) {
const auto key = utils::encryption::generate_key(token);
std::vector<char> result;
data_buffer result;
utils::encryption::encrypt_data(key, &buffer[0u], buffer.size(), result);
std::string data;
@ -90,11 +100,12 @@ TEST(encryption, decrypt_data_buffer_with_key) {
TEST(encryption, decrypt_data_pointer_with_key) {
const auto key = utils::encryption::generate_key(token);
std::vector<char> result;
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_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());
@ -102,7 +113,7 @@ TEST(encryption, decrypt_data_pointer_with_key) {
TEST(encryption, decryption_failure) {
const auto key = utils::encryption::generate_key(token);
std::vector<char> result;
data_buffer result;
utils::encryption::encrypt_data(key, &buffer[0u], buffer.size(), result);
result[0u] = 0u;
result[1u] = 1u;
@ -111,15 +122,4 @@ TEST(encryption, decryption_failure) {
std::string data;
EXPECT_FALSE(utils::encryption::decrypt_data(key, result, data));
}
TEST(encryption, encryption_failure) {
std::vector<char> result;
auto key = CryptoPP::SecByteBlock();
try {
utils::encryption::encrypt_data(key, &buffer[0u], buffer.size(), result);
EXPECT_TRUE(false);
} catch (const std::exception &e) {
std::cout << e.what() << std::endl;
}
}
} // namespace repertory

View File

@ -0,0 +1,680 @@
/*
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 "types/repertory.hpp"
#include "utils/event_capture.hpp"
#include "utils/path_utils.hpp"
namespace repertory {
static constexpr const std::size_t test_chunk_size = 1024u;
static void test_closeable_open_file(const file_manager::open_file &o,
bool directory, const api_error &e,
std::uint64_t size,
const std::string &source_path) {
EXPECT_EQ(directory, o.is_directory());
EXPECT_EQ(e, o.get_api_error());
EXPECT_EQ(std::size_t(0u), o.get_open_file_count());
EXPECT_EQ(std::uint64_t(size), o.get_file_size());
EXPECT_STREQ(source_path.c_str(), o.get_source_path().c_str());
EXPECT_TRUE(o.can_close());
}
static void validate_write(file_manager::open_file &o, std::size_t offset,
data_buffer data, std::size_t bytes_written) {
EXPECT_EQ(data.size(), bytes_written);
data_buffer read_data{};
EXPECT_EQ(api_error::success, o.read(data.size(), offset, read_data));
EXPECT_TRUE(std::equal(data.begin(), data.end(), read_data.begin()));
}
TEST(open_file, properly_initializes_state_for_0_byte_file) {
const auto source_path = generate_test_file_name(".", "test");
mock_provider mp;
mock_upload_manager um;
EXPECT_CALL(mp, is_direct_only()).WillRepeatedly(Return(false));
filesystem_item fsi;
fsi.directory = false;
fsi.api_path = "/test.txt";
fsi.size = 0u;
fsi.source_path = source_path;
file_manager::open_file o(test_chunk_size, 0U, fsi, mp, um);
EXPECT_EQ(std::size_t(0u), o.get_read_state().size());
EXPECT_FALSE(o.is_modified());
EXPECT_EQ(test_chunk_size, o.get_chunk_size());
}
TEST(open_file, properly_initializes_state_based_on_chunk_size) {
const auto source_path = generate_test_file_name(".", "test");
mock_provider mp;
mock_upload_manager um;
EXPECT_CALL(mp, is_direct_only()).WillRepeatedly(Return(false));
filesystem_item fsi;
fsi.directory = false;
fsi.api_path = "/test.txt";
fsi.size = 8u;
fsi.source_path = source_path;
EXPECT_CALL(um, remove_resume)
.WillOnce(
[&fsi](const std::string &api_path, const std::string &source_path) {
EXPECT_EQ(fsi.api_path, api_path);
EXPECT_EQ(fsi.source_path, source_path);
});
file_manager::open_file o(1u, 0U, fsi, mp, um);
EXPECT_EQ(std::size_t(8u), o.get_read_state().size());
EXPECT_TRUE(o.get_read_state().none());
EXPECT_FALSE(o.is_modified());
EXPECT_CALL(mp, set_item_meta(fsi.api_path, META_SOURCE, _))
.WillOnce(Return(api_error::success));
EXPECT_EQ(std::size_t(1u), o.get_chunk_size());
}
TEST(open_file, will_not_change_source_path_for_0_byte_file) {
const auto source_path = generate_test_file_name(".", "test");
mock_provider mp;
mock_upload_manager um;
EXPECT_CALL(mp, is_direct_only()).WillRepeatedly(Return(false));
filesystem_item fsi;
fsi.directory = false;
fsi.api_path = "/test.txt";
fsi.size = 0u;
fsi.source_path = source_path;
file_manager::open_file o(0u, 0U, fsi, mp, um);
test_closeable_open_file(o, false, api_error::success, 0u, source_path);
o.close();
EXPECT_EQ(api_error::success, o.get_api_error());
EXPECT_STREQ(source_path.c_str(), o.get_source_path().c_str());
EXPECT_TRUE(utils::file::is_file(fsi.source_path));
}
TEST(open_file, will_change_source_path_if_file_size_is_greater_than_0) {
const auto source_path = generate_test_file_name(".", "test");
mock_provider mp;
mock_upload_manager um;
EXPECT_CALL(mp, is_direct_only()).WillRepeatedly(Return(false));
filesystem_item fsi;
fsi.api_path = "/test.txt";
fsi.size = test_chunk_size;
fsi.source_path = source_path;
EXPECT_CALL(um, remove_resume)
.WillOnce(
[&fsi](const std::string &api_path, const std::string &source_path) {
EXPECT_EQ(fsi.api_path, api_path);
EXPECT_EQ(fsi.source_path, source_path);
});
EXPECT_CALL(mp, set_item_meta(fsi.api_path, META_SOURCE, _))
.WillOnce([&fsi](const std::string &, const std::string &,
const std::string &source_path) -> api_error {
EXPECT_STRNE(fsi.source_path.c_str(), source_path.c_str());
return api_error::success;
});
file_manager::open_file o(test_chunk_size, 0U, fsi, mp, um);
test_closeable_open_file(o, false, api_error::success, test_chunk_size,
source_path);
o.close();
EXPECT_EQ(api_error::download_stopped, o.get_api_error());
EXPECT_STRNE(source_path.c_str(), o.get_source_path().c_str());
EXPECT_FALSE(utils::file::is_file(source_path));
}
TEST(open_file,
will_not_change_source_path_if_file_size_matches_existing_source) {
const auto source_path = generate_test_file_name(".", "test");
create_random_file(source_path, test_chunk_size)->close();
mock_provider mp;
mock_upload_manager um;
EXPECT_CALL(mp, is_direct_only()).WillRepeatedly(Return(false));
filesystem_item fsi;
fsi.api_path = "/test.txt";
fsi.size = test_chunk_size;
fsi.source_path = source_path;
file_manager::open_file o(test_chunk_size, 0U, fsi, mp, um);
test_closeable_open_file(o, false, api_error::success, test_chunk_size,
source_path);
o.close();
EXPECT_EQ(api_error::success, o.get_api_error());
EXPECT_STREQ(source_path.c_str(), o.get_source_path().c_str());
EXPECT_TRUE(utils::file::is_file(source_path));
}
TEST(open_file, write_with_incomplete_download) {
const auto source_path = generate_test_file_name(".", "test");
auto nf = create_random_file(generate_test_file_name(".", "test_src"),
test_chunk_size * 2u);
mock_provider mp;
mock_upload_manager um;
EXPECT_CALL(mp, is_direct_only()).WillRepeatedly(Return(false));
filesystem_item fsi;
fsi.api_path = "/test.txt";
fsi.size = test_chunk_size * 2u;
fsi.source_path = source_path;
file_manager::open_file o(test_chunk_size, 0U, fsi, mp, um);
test_closeable_open_file(o, false, api_error::success, test_chunk_size * 2u,
source_path);
EXPECT_CALL(mp, set_item_meta(fsi.api_path, _))
.WillOnce([](const std::string &, const api_meta_map &meta) -> api_error {
EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_CHANGED).empty()));
EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_MODIFIED).empty()));
EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_WRITTEN).empty()));
return api_error::success;
});
EXPECT_CALL(mp, read_file_bytes)
.WillRepeatedly([&nf](const std::string & /* api_path */,
std::size_t size, std::uint64_t offset,
data_buffer &data,
stop_type &stop_requested) -> api_error {
if (stop_requested) {
return api_error::download_stopped;
}
if (offset == 0u) {
std::size_t bytes_read{};
data.resize(size);
auto ret = nf->read_bytes(&data[0u], size, offset, bytes_read)
? api_error::success
: api_error::os_error;
EXPECT_EQ(bytes_read, data.size());
return ret;
}
while (not stop_requested) {
std::this_thread::sleep_for(100ms);
}
return api_error::download_stopped;
});
EXPECT_CALL(um, remove_upload).WillOnce([&fsi](const std::string &api_path) {
EXPECT_EQ(fsi.api_path, api_path);
});
EXPECT_CALL(um, store_resume)
.Times(2)
.WillRepeatedly([&fsi](const i_open_file &o) {
EXPECT_EQ(fsi.api_path, o.get_api_path());
EXPECT_EQ(fsi.source_path, o.get_source_path());
});
data_buffer data = {10, 9, 8};
std::size_t bytes_written{};
EXPECT_EQ(api_error::success, o.write(0u, data, bytes_written));
validate_write(o, 0u, data, bytes_written);
const auto test_state = [&]() {
EXPECT_STREQ(source_path.c_str(), o.get_source_path().c_str());
EXPECT_FALSE(o.can_close());
EXPECT_TRUE(o.is_modified());
EXPECT_TRUE(o.get_read_state(0u));
EXPECT_FALSE(o.get_read_state(1u));
};
test_state();
o.close();
nf->close();
test_state();
EXPECT_EQ(api_error::download_incomplete, o.get_api_error());
EXPECT_TRUE(utils::file::is_file(fsi.source_path));
}
TEST(open_file, write_new_file) {
const auto source_path = generate_test_file_name(".", "test");
mock_provider mp;
mock_upload_manager um;
EXPECT_CALL(mp, is_direct_only()).WillRepeatedly(Return(false));
filesystem_item fsi;
fsi.api_path = "/test.txt";
fsi.size = 0u;
fsi.source_path = source_path;
EXPECT_CALL(um, store_resume).WillOnce([&fsi](const i_open_file &o) {
EXPECT_EQ(fsi.api_path, o.get_api_path());
EXPECT_EQ(fsi.source_path, o.get_source_path());
});
file_manager::open_file o(test_chunk_size, 0U, fsi, mp, um);
test_closeable_open_file(o, false, api_error::success, 0u, source_path);
data_buffer data = {10, 9, 8};
EXPECT_CALL(mp, set_item_meta(fsi.api_path, _))
.WillOnce([&data](const std::string &,
const api_meta_map &meta) -> api_error {
EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_CHANGED).empty()));
EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_MODIFIED).empty()));
EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_SIZE).empty()));
EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_WRITTEN).empty()));
EXPECT_EQ(data.size(), utils::string::to_size_t(meta.at(META_SIZE)));
return api_error::success;
})
.WillOnce([](const std::string &, const api_meta_map &meta) -> api_error {
EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_CHANGED).empty()));
EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_MODIFIED).empty()));
EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_WRITTEN).empty()));
return api_error::success;
});
EXPECT_CALL(um, remove_upload)
.Times(2)
.WillRepeatedly([&fsi](const std::string &api_path) {
EXPECT_EQ(fsi.api_path, api_path);
});
EXPECT_CALL(um, queue_upload).WillOnce([&fsi](const i_open_file &o) {
EXPECT_EQ(fsi.api_path, o.get_api_path());
EXPECT_EQ(fsi.source_path, o.get_source_path());
});
std::size_t bytes_written{};
EXPECT_EQ(api_error::success, o.write(0u, data, bytes_written));
const auto test_state = [&]() {
EXPECT_STREQ(source_path.c_str(), o.get_source_path().c_str());
EXPECT_FALSE(o.can_close());
EXPECT_TRUE(o.is_modified());
EXPECT_TRUE(o.get_read_state(0u));
EXPECT_EQ(std::size_t(1u), o.get_read_state().size());
EXPECT_EQ(data.size(), o.get_file_size());
};
test_state();
o.close();
test_state();
EXPECT_EQ(api_error::success, o.get_api_error());
EXPECT_TRUE(utils::file::is_file(fsi.source_path));
}
TEST(open_file, write_new_file_multiple_chunks) {
const auto source_path = generate_test_file_name(".", "test");
mock_provider mp;
mock_upload_manager um;
EXPECT_CALL(mp, is_direct_only()).WillRepeatedly(Return(false));
filesystem_item fsi;
fsi.api_path = "/test.txt";
fsi.size = 0u;
fsi.source_path = source_path;
EXPECT_CALL(um, store_resume).WillOnce([&fsi](const i_open_file &o) {
EXPECT_EQ(fsi.api_path, o.get_api_path());
EXPECT_EQ(fsi.source_path, o.get_source_path());
});
file_manager::open_file o(test_chunk_size, 0U, fsi, mp, um);
test_closeable_open_file(o, false, api_error::success, 0u, source_path);
data_buffer data = {10, 9, 8};
EXPECT_CALL(mp, set_item_meta(fsi.api_path, _))
.WillOnce([&data](const std::string &,
const api_meta_map &meta) -> api_error {
EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_CHANGED).empty()));
EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_MODIFIED).empty()));
EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_SIZE).empty()));
EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_WRITTEN).empty()));
EXPECT_EQ(data.size(), utils::string::to_size_t(meta.at(META_SIZE)));
return api_error::success;
})
.WillOnce([](const std::string &, const api_meta_map &meta) -> api_error {
EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_CHANGED).empty()));
EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_MODIFIED).empty()));
EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_WRITTEN).empty()));
return api_error::success;
})
.WillOnce(
[&data](const std::string &, const api_meta_map &meta) -> api_error {
EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_CHANGED).empty()));
EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_MODIFIED).empty()));
EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_SIZE).empty()));
EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_WRITTEN).empty()));
EXPECT_EQ(data.size() + test_chunk_size,
utils::string::to_size_t(meta.at(META_SIZE)));
return api_error::success;
})
.WillOnce([](const std::string &, const api_meta_map &meta) -> api_error {
EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_CHANGED).empty()));
EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_MODIFIED).empty()));
EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_WRITTEN).empty()));
return api_error::success;
});
EXPECT_CALL(um, remove_upload)
.Times(4)
.WillRepeatedly([&fsi](const std::string &api_path) {
EXPECT_EQ(fsi.api_path, api_path);
});
EXPECT_CALL(um, queue_upload).WillOnce([&fsi](const i_open_file &o) {
EXPECT_EQ(fsi.api_path, o.get_api_path());
EXPECT_EQ(fsi.source_path, o.get_source_path());
});
std::size_t bytes_written{};
EXPECT_EQ(api_error::success, o.write(0u, data, bytes_written));
EXPECT_EQ(api_error::success, o.write(test_chunk_size, data, bytes_written));
const auto test_state = [&]() {
EXPECT_STREQ(source_path.c_str(), o.get_source_path().c_str());
EXPECT_FALSE(o.can_close());
EXPECT_TRUE(o.is_modified());
EXPECT_EQ(std::size_t(2u), o.get_read_state().size());
for (std::size_t i = 0u; i < 2u; i++) {
EXPECT_TRUE(o.get_read_state(i));
}
EXPECT_EQ(data.size() + test_chunk_size, o.get_file_size());
};
test_state();
o.close();
test_state();
EXPECT_EQ(api_error::success, o.get_api_error());
EXPECT_TRUE(utils::file::is_file(fsi.source_path));
}
TEST(open_file, resize_file_to_0_bytes) {
const auto source_path = generate_test_file_name(".", "test");
create_random_file(source_path, test_chunk_size * 4u)->close();
mock_provider mp;
mock_upload_manager um;
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;
file_manager::open_file o(test_chunk_size, 0U, fsi, mp, um);
test_closeable_open_file(o, false, api_error::success, fsi.size, source_path);
EXPECT_CALL(mp, set_item_meta(fsi.api_path, _))
.WillOnce([](const std::string &, const api_meta_map &meta) -> api_error {
EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_CHANGED).empty()));
EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_MODIFIED).empty()));
EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_SIZE).empty()));
EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_WRITTEN).empty()));
EXPECT_EQ(std::size_t(0u),
utils::string::to_size_t(meta.at(META_SIZE)));
return api_error::success;
});
EXPECT_CALL(um, remove_upload).WillOnce([&fsi](const std ::string &api_path) {
EXPECT_EQ(fsi.api_path, api_path);
});
EXPECT_CALL(um, queue_upload).WillOnce([&fsi](const i_open_file &o) {
EXPECT_EQ(fsi.api_path, o.get_api_path());
EXPECT_EQ(fsi.source_path, o.get_source_path());
});
EXPECT_CALL(um, store_resume).WillOnce([&fsi](const i_open_file &o) {
EXPECT_EQ(fsi.api_path, o.get_api_path());
EXPECT_EQ(fsi.source_path, o.get_source_path());
});
EXPECT_EQ(api_error::success, o.resize(0u));
EXPECT_EQ(std::size_t(0u), o.get_file_size());
EXPECT_FALSE(o.can_close());
EXPECT_TRUE(o.is_modified());
EXPECT_EQ(std::size_t(0u), o.get_read_state().size());
}
TEST(open_file, resize_file_by_full_chunk) {
const auto source_path = generate_test_file_name(".", "test");
create_random_file(source_path, test_chunk_size * 4u)->close();
mock_provider mp;
mock_upload_manager um;
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;
EXPECT_CALL(um, store_resume).WillOnce([&fsi](const i_open_file &o) {
EXPECT_EQ(fsi.api_path, o.get_api_path());
EXPECT_EQ(fsi.source_path, o.get_source_path());
});
file_manager::open_file o(test_chunk_size, 0U, fsi, mp, um);
test_closeable_open_file(o, false, api_error::success, fsi.size, source_path);
EXPECT_CALL(mp, set_item_meta(fsi.api_path, _))
.WillOnce([](const std::string &, const api_meta_map &meta) -> api_error {
EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_CHANGED).empty()));
EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_MODIFIED).empty()));
EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_SIZE).empty()));
EXPECT_NO_THROW(EXPECT_FALSE(meta.at(META_WRITTEN).empty()));
EXPECT_EQ(std::size_t(test_chunk_size * 3u),
utils::string::to_size_t(meta.at(META_SIZE)));
return api_error::success;
});
EXPECT_CALL(um, remove_upload).WillOnce([&fsi](const std::string &api_path) {
EXPECT_EQ(fsi.api_path, api_path);
});
EXPECT_CALL(um, queue_upload).WillOnce([&fsi](const i_open_file &o) {
EXPECT_EQ(fsi.api_path, o.get_api_path());
EXPECT_EQ(fsi.source_path, o.get_source_path());
});
EXPECT_EQ(api_error::success, o.resize(test_chunk_size * 3u));
EXPECT_EQ(std::size_t(test_chunk_size * 3u), o.get_file_size());
EXPECT_FALSE(o.can_close());
EXPECT_TRUE(o.is_modified());
EXPECT_EQ(std::size_t(3u), o.get_read_state().size());
}
TEST(open_file, can_add_handle) {
event_system::instance().start();
console_consumer c;
const auto source_path = generate_test_file_name(".", "test");
mock_provider mp;
mock_upload_manager um;
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("filesystem_item_opened", [&fsi](const event &e) {
const auto &ee = dynamic_cast<const filesystem_item_opened &>(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("0", ee.get_directory().get<std::string>().c_str());
});
event_consumer ec2("filesystem_item_handle_opened", [&fsi](const event &e) {
const auto &ee = dynamic_cast<const filesystem_item_handle_opened &>(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("0", ee.get_directory().get<std::string>().c_str());
EXPECT_STREQ("1", ee.get_handle().get<std::string>().c_str());
});
EXPECT_CALL(mp, set_item_meta(fsi.api_path, META_SOURCE, _))
.WillOnce(Return(api_error::success));
EXPECT_CALL(um, remove_resume)
.WillOnce(
[&fsi](const std::string &api_path, const std::string &source_path) {
EXPECT_EQ(fsi.api_path, api_path);
EXPECT_EQ(fsi.source_path, source_path);
});
event_capture capture(
{"filesystem_item_opened", "filesystem_item_handle_opened"});
file_manager::open_file o(test_chunk_size, 0U, fsi, mp, um);
#ifdef _WIN32
o.add(1u, {});
EXPECT_EQ(nullptr, o.get_open_data(1u).directory_buffer);
#else
o.add(1u, O_RDWR | O_SYNC);
EXPECT_EQ(O_RDWR | O_SYNC, o.get_open_data(1u));
#endif
capture.wait_for_empty();
event_system::instance().stop();
}
TEST(open_file, can_remove_handle) {
event_system::instance().start();
console_consumer c;
const auto source_path = generate_test_file_name(".", "test");
mock_provider mp;
mock_upload_manager um;
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("filesystem_item_closed", [&fsi](const event &e) {
const auto &ee = dynamic_cast<const filesystem_item_closed &>(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("0", ee.get_directory().get<std::string>().c_str());
});
event_consumer ec2("filesystem_item_handle_closed", [&fsi](const event &e) {
const auto &ee = dynamic_cast<const filesystem_item_handle_closed &>(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("0", ee.get_directory().get<std::string>().c_str());
EXPECT_STREQ("1", ee.get_handle().get<std::string>().c_str());
});
EXPECT_CALL(um, remove_resume)
.WillOnce(
[&fsi](const std::string &api_path, const std::string &source_path) {
EXPECT_EQ(fsi.api_path, api_path);
EXPECT_EQ(fsi.source_path, source_path);
});
EXPECT_CALL(mp, set_item_meta(fsi.api_path, META_SOURCE, _))
.WillOnce(Return(api_error::success));
event_capture capture({
"filesystem_item_opened",
"filesystem_item_handle_opened",
"filesystem_item_handle_closed",
"filesystem_item_closed",
});
file_manager::open_file o(test_chunk_size, 0U, fsi, mp, um);
#ifdef _WIN32
o.add(1u, {});
#else
o.add(1u, O_RDWR | O_SYNC);
#endif
o.remove(1u);
capture.wait_for_empty();
event_system::instance().stop();
}
TEST(open_file,
can_read_locally_after_write_with_file_size_greater_than_existing_size) {}
TEST(open_file, test_valid_download_chunks) {}
TEST(open_file, test_full_download_with_partial_chunk) {}
TEST(open_file, source_is_read_after_full_download) {}
} // namespace repertory

View File

@ -0,0 +1,566 @@
/*
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/file_utils.hpp"
#include "utils/unix/unix_utils.hpp"
namespace repertory {
static constexpr const std::size_t test_chunk_size = 1024u;
TEST(ring_buffer_open_file, can_forward_to_last_chunk) {
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.directory = false;
fsi.api_path = "/test.txt";
fsi.size = test_chunk_size * 16u;
fsi.source_path = source_path;
{
file_manager::ring_buffer_open_file rb("./ring_buffer_directory",
test_chunk_size, 30U, fsi, mp, 8u);
rb.set(0u, 3u);
rb.forward(4u);
EXPECT_EQ(std::size_t(7u), rb.get_current_chunk());
EXPECT_EQ(std::size_t(0u), rb.get_first_chunk());
EXPECT_EQ(std::size_t(7u), rb.get_last_chunk());
for (std::size_t chunk = 0u; chunk < 8u; chunk++) {
EXPECT_TRUE(rb.get_read_state(chunk));
}
}
EXPECT_TRUE(
utils::file::delete_directory_recursively("./ring_buffer_directory"));
}
TEST(ring_buffer_open_file,
can_forward_to_last_chunk_if_count_is_greater_than_remaining) {
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.directory = false;
fsi.api_path = "/test.txt";
fsi.size = test_chunk_size * 16u;
fsi.source_path = source_path;
{
file_manager::ring_buffer_open_file rb("./ring_buffer_directory",
test_chunk_size, 30U, fsi, mp, 8u);
rb.set(0u, 3u);
rb.forward(100u);
EXPECT_EQ(std::size_t(15u), rb.get_current_chunk());
EXPECT_EQ(std::size_t(8u), rb.get_first_chunk());
EXPECT_EQ(std::size_t(15u), rb.get_last_chunk());
for (std::size_t chunk = 8u; chunk <= 15u; chunk++) {
EXPECT_FALSE(rb.get_read_state(chunk));
}
}
EXPECT_TRUE(
utils::file::delete_directory_recursively("./ring_buffer_directory"));
}
TEST(ring_buffer_open_file, can_forward_after_last_chunk) {
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.directory = false;
fsi.api_path = "/test.txt";
fsi.size = test_chunk_size * 16u;
fsi.source_path = source_path;
{
file_manager::ring_buffer_open_file rb("./ring_buffer_directory",
test_chunk_size, 30U, fsi, mp, 8u);
rb.set(0u, 3u);
rb.forward(5u);
EXPECT_EQ(std::size_t(8u), rb.get_current_chunk());
EXPECT_EQ(std::size_t(1u), rb.get_first_chunk());
EXPECT_EQ(std::size_t(8u), rb.get_last_chunk());
EXPECT_FALSE(rb.get_read_state(8u));
for (std::size_t chunk = 1u; chunk < 8u; chunk++) {
EXPECT_TRUE(rb.get_read_state(chunk));
}
}
EXPECT_TRUE(
utils::file::delete_directory_recursively("./ring_buffer_directory"));
}
TEST(ring_buffer_open_file, can_forward_and_rollover_after_last_chunk) {
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.directory = false;
fsi.api_path = "/test.txt";
fsi.size = test_chunk_size * 32u;
fsi.source_path = source_path;
{
file_manager::ring_buffer_open_file rb("./ring_buffer_directory",
test_chunk_size, 30U, fsi, mp, 8u);
rb.set(16u, 20u);
rb.forward(8u);
EXPECT_EQ(std::size_t(28u), rb.get_current_chunk());
EXPECT_EQ(std::size_t(21u), rb.get_first_chunk());
EXPECT_EQ(std::size_t(28u), rb.get_last_chunk());
}
EXPECT_TRUE(
utils::file::delete_directory_recursively("./ring_buffer_directory"));
}
TEST(ring_buffer_open_file, can_reverse_to_first_chunk) {
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.directory = false;
fsi.api_path = "/test.txt";
fsi.size = test_chunk_size * 16u;
fsi.source_path = source_path;
{
file_manager::ring_buffer_open_file rb("./ring_buffer_directory",
test_chunk_size, 30U, fsi, mp, 8u);
rb.set(0u, 3u);
rb.reverse(3u);
EXPECT_EQ(std::size_t(0u), rb.get_current_chunk());
EXPECT_EQ(std::size_t(0u), rb.get_first_chunk());
EXPECT_EQ(std::size_t(7u), rb.get_last_chunk());
for (std::size_t chunk = 0u; chunk < 8u; chunk++) {
EXPECT_TRUE(rb.get_read_state(chunk));
}
}
EXPECT_TRUE(
utils::file::delete_directory_recursively("./ring_buffer_directory"));
}
TEST(ring_buffer_open_file,
can_reverse_to_first_chunk_if_count_is_greater_than_remaining) {
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.directory = false;
fsi.api_path = "/test.txt";
fsi.size = test_chunk_size * 16u;
fsi.source_path = source_path;
{
file_manager::ring_buffer_open_file rb("./ring_buffer_directory",
test_chunk_size, 30U, fsi, mp, 8u);
rb.set(0u, 3u);
rb.reverse(13u);
EXPECT_EQ(std::size_t(0u), rb.get_current_chunk());
EXPECT_EQ(std::size_t(0u), rb.get_first_chunk());
EXPECT_EQ(std::size_t(7u), rb.get_last_chunk());
for (std::size_t chunk = 0u; chunk < 8u; chunk++) {
EXPECT_TRUE(rb.get_read_state(chunk));
}
}
EXPECT_TRUE(
utils::file::delete_directory_recursively("./ring_buffer_directory"));
}
TEST(ring_buffer_open_file, can_reverse_before_first_chunk) {
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.directory = false;
fsi.api_path = "/test.txt";
fsi.size = test_chunk_size * 16u;
fsi.source_path = source_path;
{
file_manager::ring_buffer_open_file rb("./ring_buffer_directory",
test_chunk_size, 30U, fsi, mp, 8u);
rb.set(1u, 3u);
rb.reverse(3u);
EXPECT_EQ(std::size_t(0u), rb.get_current_chunk());
EXPECT_EQ(std::size_t(0u), rb.get_first_chunk());
EXPECT_EQ(std::size_t(7u), rb.get_last_chunk());
EXPECT_FALSE(rb.get_read_state(0u));
for (std::size_t chunk = 1u; chunk < 8u; chunk++) {
EXPECT_TRUE(rb.get_read_state(chunk));
}
}
EXPECT_TRUE(
utils::file::delete_directory_recursively("./ring_buffer_directory"));
}
TEST(ring_buffer_open_file, can_reverse_and_rollover_before_first_chunk) {
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.directory = false;
fsi.api_path = "/test.txt";
fsi.size = test_chunk_size * 32u;
fsi.source_path = source_path;
{
file_manager::ring_buffer_open_file rb("./ring_buffer_directory",
test_chunk_size, 30U, fsi, mp, 8u);
rb.set(16u, 20u);
rb.reverse(8u);
EXPECT_EQ(std::size_t(12u), rb.get_current_chunk());
EXPECT_EQ(std::size_t(12u), rb.get_first_chunk());
EXPECT_EQ(std::size_t(19u), rb.get_last_chunk());
EXPECT_FALSE(rb.get_read_state(12u));
EXPECT_FALSE(rb.get_read_state(13u));
EXPECT_FALSE(rb.get_read_state(14u));
EXPECT_FALSE(rb.get_read_state(15u));
for (std::size_t chunk = 16u; chunk <= rb.get_last_chunk(); chunk++) {
EXPECT_TRUE(rb.get_read_state(chunk));
}
}
EXPECT_TRUE(
utils::file::delete_directory_recursively("./ring_buffer_directory"));
}
TEST(ring_buffer_open_file, can_reverse_full_ring) {
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.directory = false;
fsi.api_path = "/test.txt";
fsi.size = test_chunk_size * 32u;
fsi.source_path = source_path;
{
file_manager::ring_buffer_open_file rb("./ring_buffer_directory",
test_chunk_size, 30U, fsi, mp, 8u);
rb.set(8u, 15u);
rb.reverse(16u);
EXPECT_EQ(std::size_t(0u), rb.get_current_chunk());
EXPECT_EQ(std::size_t(0u), rb.get_first_chunk());
EXPECT_EQ(std::size_t(7u), rb.get_last_chunk());
for (std::size_t chunk = 0u; chunk <= rb.get_last_chunk(); chunk++) {
EXPECT_FALSE(rb.get_read_state(chunk));
}
}
EXPECT_TRUE(
utils::file::delete_directory_recursively("./ring_buffer_directory"));
}
TEST(ring_buffer_open_file, read_full_file) {
const auto download_source_path = generate_test_file_name(".", "test");
auto nf = create_random_file(download_source_path, test_chunk_size * 32u);
const auto dest_path = generate_test_file_name(".", "test");
mock_provider mp;
EXPECT_CALL(mp, is_direct_only()).WillRepeatedly(Return(false));
filesystem_item fsi;
fsi.directory = false;
fsi.api_path = "/test.txt";
fsi.size = test_chunk_size * 32u;
fsi.source_path = generate_test_file_name(".", "test");
EXPECT_CALL(mp, read_file_bytes)
.WillRepeatedly([&nf](const std::string & /* api_path */,
std::size_t size, std::uint64_t offset,
data_buffer &data,
stop_type &stop_requested) -> api_error {
EXPECT_FALSE(stop_requested);
std::size_t bytes_read{};
data.resize(size);
auto ret = nf->read_bytes(&data[0u], size, offset, bytes_read)
? api_error::success
: api_error::os_error;
EXPECT_EQ(bytes_read, data.size());
return ret;
});
{
file_manager::ring_buffer_open_file rb("./ring_buffer_directory",
test_chunk_size, 30U, fsi, mp, 8u);
native_file_ptr nf2;
EXPECT_EQ(api_error::success, native_file::create_or_open(dest_path, nf2));
auto to_read = fsi.size;
std::size_t chunk = 0u;
while (to_read) {
data_buffer data{};
EXPECT_EQ(api_error::success,
rb.read(test_chunk_size, chunk * test_chunk_size, data));
std::size_t bytes_written{};
EXPECT_TRUE(nf2->write_bytes(data.data(), data.size(),
chunk * test_chunk_size, bytes_written));
chunk++;
to_read -= data.size();
}
nf2->close();
nf->close();
EXPECT_STREQ(utils::file::generate_sha256(download_source_path).c_str(),
utils::file::generate_sha256(dest_path).c_str());
}
EXPECT_TRUE(
utils::file::delete_directory_recursively("./ring_buffer_directory"));
}
TEST(ring_buffer_open_file, read_full_file_in_reverse) {
const auto download_source_path = generate_test_file_name(".", "test");
auto nf = create_random_file(download_source_path, test_chunk_size * 32u);
const auto dest_path = generate_test_file_name(".", "test");
mock_provider mp;
EXPECT_CALL(mp, is_direct_only()).WillRepeatedly(Return(false));
filesystem_item fsi;
fsi.directory = false;
fsi.api_path = "/test.txt";
fsi.size = test_chunk_size * 32u;
fsi.source_path = generate_test_file_name(".", "test");
EXPECT_CALL(mp, read_file_bytes)
.WillRepeatedly([&nf](const std::string & /* api_path */,
std::size_t size, std::uint64_t offset,
data_buffer &data,
stop_type &stop_requested) -> api_error {
EXPECT_FALSE(stop_requested);
std::size_t bytes_read{};
data.resize(size);
auto ret = nf->read_bytes(&data[0u], size, offset, bytes_read)
? api_error::success
: api_error::os_error;
EXPECT_EQ(bytes_read, data.size());
return ret;
});
{
file_manager::ring_buffer_open_file rb("./ring_buffer_directory",
test_chunk_size, 30U, fsi, mp, 8u);
native_file_ptr nf2;
EXPECT_EQ(api_error::success, native_file::create_or_open(dest_path, nf2));
auto to_read = fsi.size;
std::size_t chunk = rb.get_total_chunks() - 1u;
while (to_read) {
data_buffer data{};
EXPECT_EQ(api_error::success,
rb.read(test_chunk_size, chunk * test_chunk_size, data));
std::size_t bytes_written{};
EXPECT_TRUE(nf2->write_bytes(data.data(), data.size(),
chunk * test_chunk_size, bytes_written));
chunk--;
to_read -= data.size();
}
nf2->close();
nf->close();
EXPECT_STREQ(utils::file::generate_sha256(download_source_path).c_str(),
utils::file::generate_sha256(dest_path).c_str());
}
EXPECT_TRUE(
utils::file::delete_directory_recursively("./ring_buffer_directory"));
}
TEST(ring_buffer_open_file, read_full_file_in_partial_chunks) {
const auto download_source_path = generate_test_file_name(".", "test");
auto nf = create_random_file(download_source_path, test_chunk_size * 32u);
const auto dest_path = generate_test_file_name(".", "test");
mock_provider mp;
EXPECT_CALL(mp, is_direct_only()).WillRepeatedly(Return(false));
filesystem_item fsi;
fsi.directory = false;
fsi.api_path = "/test.txt";
fsi.size = test_chunk_size * 32u;
fsi.source_path = generate_test_file_name(".", "test");
EXPECT_CALL(mp, read_file_bytes)
.WillRepeatedly([&nf](const std::string & /* api_path */,
std::size_t size, std::uint64_t offset,
data_buffer &data,
stop_type &stop_requested) -> api_error {
EXPECT_FALSE(stop_requested);
std::size_t bytes_read{};
data.resize(size);
auto ret = nf->read_bytes(&data[0u], size, offset, bytes_read)
? api_error::success
: api_error::os_error;
EXPECT_EQ(bytes_read, data.size());
return ret;
});
{
file_manager::ring_buffer_open_file rb("./ring_buffer_directory",
test_chunk_size, 30U, fsi, mp, 8u);
native_file_ptr nf2;
EXPECT_EQ(api_error::success, native_file::create_or_open(dest_path, nf2));
auto total_read = std::uint64_t(0u);
while (total_read < fsi.size) {
data_buffer data{};
EXPECT_EQ(api_error::success, rb.read(3u, total_read, data));
std::size_t bytes_written{};
EXPECT_TRUE(nf2->write_bytes(data.data(), data.size(), total_read,
bytes_written));
total_read += data.size();
}
nf2->close();
nf->close();
EXPECT_STREQ(utils::file::generate_sha256(download_source_path).c_str(),
utils::file::generate_sha256(dest_path).c_str());
}
EXPECT_TRUE(
utils::file::delete_directory_recursively("./ring_buffer_directory"));
}
TEST(ring_buffer_open_file, read_full_file_in_partial_chunks_in_reverse) {
const auto download_source_path = generate_test_file_name(".", "test");
auto nf = create_random_file(download_source_path, test_chunk_size * 32u);
const auto dest_path = generate_test_file_name(".", "test");
mock_provider mp;
EXPECT_CALL(mp, is_direct_only()).WillRepeatedly(Return(false));
filesystem_item fsi;
fsi.directory = false;
fsi.api_path = "/test.txt";
fsi.size = test_chunk_size * 32u;
fsi.source_path = generate_test_file_name(".", "test");
EXPECT_CALL(mp, read_file_bytes)
.WillRepeatedly([&nf](const std::string & /* api_path */,
std::size_t size, std::uint64_t offset,
data_buffer &data,
stop_type &stop_requested) -> api_error {
EXPECT_FALSE(stop_requested);
std::size_t bytes_read{};
data.resize(size);
auto ret = nf->read_bytes(&data[0u], size, offset, bytes_read)
? api_error::success
: api_error::os_error;
EXPECT_EQ(bytes_read, data.size());
return ret;
});
{
file_manager::ring_buffer_open_file rb("./ring_buffer_directory",
test_chunk_size, 30U, fsi, mp, 8u);
native_file_ptr nf2;
EXPECT_EQ(api_error::success, native_file::create_or_open(dest_path, nf2));
auto total_read = std::uint64_t(0u);
const auto read_size = 3u;
while (total_read < fsi.size) {
const auto offset = fsi.size - total_read - read_size;
const auto remain = fsi.size - total_read;
data_buffer data{};
EXPECT_EQ(api_error::success,
rb.read(static_cast<std::size_t>(
std::min(remain, std::uint64_t(read_size))),
(remain >= read_size) ? offset : 0u, data));
std::size_t bytes_written{};
EXPECT_TRUE(nf2->write_bytes(data.data(), data.size(),
(remain >= read_size) ? offset : 0u,
bytes_written));
total_read += data.size();
}
nf2->close();
nf->close();
EXPECT_STREQ(utils::file::generate_sha256(download_source_path).c_str(),
utils::file::generate_sha256(dest_path).c_str());
}
EXPECT_TRUE(
utils::file::delete_directory_recursively("./ring_buffer_directory"));
}
} // namespace repertory

1851
tests/file_manager_test.cpp Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,190 @@
/*
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, fsi.encryption_token, _))
.WillOnce([&fsi](const std::string &, 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, fsi.encryption_token, _))
.WillOnce([&cv, &fsi, &mtx](const std::string &, 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, fsi.encryption_token, _))
.WillOnce([&fsi](const std::string &, 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,69 +0,0 @@
/*
Copyright <2018-2022> <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_AWS_S3_COMM_FIXTURE_H
#define REPERTORY_AWS_S3_COMM_FIXTURE_H
#if defined(REPERTORY_ENABLE_S3_TESTING)
#include "comm/aws_s3/aws_s3_comm.hpp"
#include "app_config.hpp"
#include "test_common.hpp"
#include "utils/path_utils.hpp"
namespace repertory {
class aws_s3_comm_test : public ::testing::Test {
private:
console_consumer c_;
protected:
std::unique_ptr<app_config> config_;
std::unique_ptr<aws_s3_comm> s3_comm_;
public:
void SetUp() override {
const auto path = utils::path::absolute("./aws_test/");
utils::file::delete_directory_recursively(path);
{
app_config config(provider_type::s3, "../../filebase");
config_ = std::make_unique<app_config>(provider_type::s3, "./aws_test");
config_->set_event_level(event_level::verbose);
config_->set_value_by_name("S3Config.AccessKey", config.get_s3_config().access_key);
config_->set_value_by_name("S3Config.SecretKey", config.get_s3_config().secret_key);
config_->set_value_by_name("S3Config.Region", config.get_s3_config().region);
config_->set_value_by_name("S3Config.URL", config.get_s3_config().url);
config_->set_value_by_name("S3Config.Bucket", "");
}
s3_comm_ = std::make_unique<aws_s3_comm>(*config_);
event_system::instance().start();
}
void TearDown() override {
event_system::instance().stop();
s3_comm_.reset();
config_.reset();
const auto path = utils::path::absolute("./aws_test/");
utils::file::delete_directory_recursively(path);
}
};
} // namespace repertory
#endif // REPERTORY_ENABLE_S3_TESTING
#endif // REPERTORY_AWS_S3_COMM_FIXTURE_H

View File

@ -1,27 +1,31 @@
/*
Copyright <2018-2022> <scott.e.graves@protonmail.com>
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
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 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.
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_DIRECTORY_DB_FIXTURE_H
#define REPERTORY_DIRECTORY_DB_FIXTURE_H
#include "test_common.hpp"
#include "app_config.hpp"
#include "db/directory_db.hpp"
#include "test_common.hpp"
#include "utils/path_utils.hpp"
namespace repertory {
@ -35,8 +39,9 @@ protected:
public:
void SetUp() override {
utils::file::delete_directory_recursively(config_location_);
config_ = std::make_unique<app_config>(provider_type::skynet, config_location_);
ASSERT_TRUE(utils::file::delete_directory_recursively(config_location_));
config_ =
std::make_unique<app_config>(provider_type::encrypt, config_location_);
db_ = std::make_unique<directory_db>(*config_.get());
}
@ -44,7 +49,7 @@ public:
db_.reset();
config_.reset();
utils::file::delete_directory_recursively(config_location_);
EXPECT_TRUE(utils::file::delete_directory_recursively(config_location_));
}
};
} // namespace repertory

View File

@ -1,133 +0,0 @@
/*
Copyright <2018-2022> <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_FIXTURES_FUSE_FIXTURE_HPP_
#define TESTS_FIXTURES_FUSE_FIXTURE_HPP_
#if !_WIN32
#include "app_config.hpp"
#include "drives/fuse/fuse_drive.hpp"
#include "mocks/mock_comm.hpp"
#include "mocks/mock_s3_comm.hpp"
#include "platform/platform.hpp"
#include "providers/s3/s3_provider.hpp"
#include "providers/skynet/skynet_provider.hpp"
#include "providers/sia/sia_provider.hpp"
#include "test_common.hpp"
namespace repertory {
class fuse_test : public ::testing::Test {
public:
mock_comm mock_sia_comm_;
#ifdef REPERTORY_ENABLE_SKYNET
mock_comm mock_skynet_comm_;
#endif
#ifdef REPERTORY_ENABLE_S3_TESTING
/* mock_s3_comm mock_s3_comm_; */
#ifdef REPERTORY_ENABLE_SKYNET
std::array<std::tuple<std::shared_ptr<app_config>, std::shared_ptr<i_provider>,
std::shared_ptr<fuse_drive>>,
2>
provider_tests_;
#else
std::array<std::tuple<std::shared_ptr<app_config>, std::shared_ptr<i_provider>,
std::shared_ptr<fuse_drive>>,
1>
provider_tests_;
#endif
#else
#ifdef REPERTORY_ENABLE_SKYNET
std::array<std::tuple<std::shared_ptr<app_config>, std::shared_ptr<i_provider>,
std::shared_ptr<fuse_drive>>,
2>
provider_tests_;
#else
std::array<std::tuple<std::shared_ptr<app_config>, std::shared_ptr<i_provider>,
std::shared_ptr<fuse_drive>>,
1>
provider_tests_;
#endif
#endif
lock_data lock_data_;
protected:
void SetUp() override {
std::size_t provider_index = 0u;
utils::file::delete_directory_recursively("./fuse_test");
auto config = std::make_shared<app_config>(provider_type::sia, "./fuse_test");
config->set_enable_drive_events(true);
config->set_event_level(event_level::verbose);
config->set_api_port(11115);
auto sp = std::make_shared<sia_provider>(*config, mock_sia_comm_);
auto drive = std::make_shared<fuse_drive>(*config, lock_data_, *sp);
provider_tests_[provider_index++] = {config, sp, drive};
#ifdef REPERTORY_ENABLE_SKYNET
utils::file::delete_directory_recursively("./fuse_test2");
config = std::make_shared<app_config>(provider_type::skynet, "./fuse_test2");
config->set_enable_drive_events(true);
config->set_event_level(event_level::verbose);
config->set_api_port(11116);
auto skp = std::make_shared<skynet_provider>(*config, mock_skynet_comm_);
drive = std::make_shared<fuse_drive>(*config, lock_data_, *skp);
provider_tests_[provider_index++] = {config, skp, drive};
#endif
#ifdef REPERTORY_ENABLE_S3_TESTING
// utils::file::delete_directory_recursively("./fuse_test3");
// config = std::make_shared<app_config>(provider_type::s3, "./fuse_test3");
// config->SetEnableDriveEvents(true);
// config->set_event_level(event_level::Verbose);
// config->SetAPIPort(11117);
//{
// app_config config(provider_type::s3, "../../filebase");
// config->set_event_level(event_level::Verbose);
// config->set_value_by_name("S3Config.AccessKey", config.get_s3_config().AccessKey);
// config->set_value_by_name("S3Config.SecretKey", config.get_s3_config().SecretKey);
// config->set_value_by_name("S3Config.Region", config.get_s3_config().Region);
// config->set_value_by_name("S3Config.URL", config.get_s3_config().URL);
// config->set_value_by_name("S3Config.Bucket", "repertory");
//}
// mock_s3_comm_.SetS3Config(config->get_s3_config());
// auto s3p = std::make_shared<s3_provider>(*config, mock_s3_comm_);
// drive = std::make_shared<fuse_drive>(*config, lock_data_, *s3p);
// provider_tests_[provider_index++] = {config, s3p, drive};
#endif
event_system::instance().start();
}
void TearDown() override {
for (auto &i : provider_tests_) {
std::get<2>(i).reset();
}
event_system::instance().stop();
for (auto &i : provider_tests_) {
std::get<1>(i).reset();
std::get<0>(i).reset();
}
utils::file::delete_directory_recursively("./fuse_test");
utils::file::delete_directory_recursively("./fuse_test2");
utils::file::delete_directory_recursively("./fuse_test3");
}
};
} // namespace repertory
#endif
#endif // TESTS_FIXTURES_FUSE_FIXTURE_HPP_

57
tests/fixtures/meta_db_fixture.hpp vendored Normal file
View File

@ -0,0 +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.
*/
#ifndef REPERTORY_META_DB_FIXTURE_H
#define REPERTORY_META_DB_FIXTURE_H
#include "test_common.hpp"
#include "app_config.hpp"
#include "db/meta_db.hpp"
#include "utils/path_utils.hpp"
namespace repertory {
class meta_db_test : public ::testing::Test {
private:
const std::string config_location_ = utils::path::absolute("./metadb");
protected:
std::unique_ptr<app_config> config_;
std::unique_ptr<meta_db> db_;
public:
void SetUp() override {
ASSERT_TRUE(utils::file::delete_directory_recursively(config_location_));
config_ =
std::make_unique<app_config>(provider_type::sia, config_location_);
db_ = std::make_unique<meta_db>(*config_.get());
}
void TearDown() override {
db_.reset();
config_.reset();
EXPECT_TRUE(utils::file::delete_directory_recursively(config_location_));
}
};
} // namespace repertory
#endif // REPERTORY_META_DB_FIXTURE_H

88
tests/fixtures/s3_comm_fixture.hpp vendored Normal file
View File

@ -0,0 +1,88 @@
/*
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_S3_COMM_FIXTURE_H
#define REPERTORY_S3_COMM_FIXTURE_H
#if defined(REPERTORY_ENABLE_S3) && defined(REPERTORY_ENABLE_S3_TESTING)
#include "test_common.hpp"
#include "app_config.hpp"
#include "comm/s3/s3_comm.hpp"
#include "utils/path_utils.hpp"
namespace repertory {
class s3_comm_test : public ::testing::Test {
private:
console_consumer c_;
protected:
std::unique_ptr<app_config> config_;
std::unique_ptr<s3_comm> s3_comm_;
public:
void SetUp() override {
const auto path = utils::path::absolute("./test/");
ASSERT_TRUE(utils::file::delete_directory_recursively(path));
{
app_config config(provider_type::s3,
utils::path::combine(get_test_dir(), {"filebase"}));
config_ = std::make_unique<app_config>(provider_type::s3, "./test");
config_->set_event_level(event_level::verbose);
EXPECT_FALSE(config_
->set_value_by_name("S3Config.AccessKey",
config.get_s3_config().access_key)
.empty());
EXPECT_FALSE(config_
->set_value_by_name("S3Config.SecretKey",
config.get_s3_config().secret_key)
.empty());
EXPECT_FALSE(config_
->set_value_by_name("S3Config.Region",
config.get_s3_config().region)
.empty());
EXPECT_FALSE(
config_->set_value_by_name("S3Config.URL", config.get_s3_config().url)
.empty());
EXPECT_FALSE(
config_->set_value_by_name("S3Config.Bucket", "repertory").empty());
}
s3_comm_ = std::make_unique<s3_comm>(*config_);
event_system::instance().start();
}
void TearDown() override {
event_system::instance().stop();
s3_comm_.reset();
config_.reset();
const auto path = utils::path::absolute("./test/");
EXPECT_TRUE(utils::file::delete_directory_recursively(path));
}
};
} // namespace repertory
#endif // REPERTORY_ENABLE_S3_TESTING
#endif // REPERTORY_S3_COMM_FIXTURE_H

View File

@ -0,0 +1,149 @@
/*
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_S3_PROVIDER_FILE_LIST_FIXTURE_H
#define REPERTORY_S3_PROVIDER_FILE_LIST_FIXTURE_H
#if defined(REPERTORY_ENABLE_S3) && defined(REPERTORY_ENABLE_S3_TESTING)
#include "test_common.hpp"
#include "mocks/mock_s3_comm.hpp"
#include "providers/s3/s3_provider.hpp"
#include "types/s3.hpp"
#include "utils/path_utils.hpp"
namespace repertory {
class s3_provider_file_list_test : public ::testing::Test {
public:
console_consumer c;
api_file_list list;
std::unique_ptr<app_config> cfg;
std::unique_ptr<mock_s3_comm> comm;
std::unique_ptr<s3_provider> provider;
std::array<std::uint64_t, 10> times = {
utils::get_file_time_now(), utils::get_file_time_now(),
utils::get_file_time_now(), utils::get_file_time_now(),
utils::get_file_time_now(), utils::get_file_time_now(),
utils::get_file_time_now(), utils::get_file_time_now(),
utils::get_file_time_now(), utils::get_file_time_now(),
};
public:
void SetUp() override {
ASSERT_TRUE(utils::file::delete_directory_recursively(
utils::path::absolute("./data")));
event_system::instance().start();
cfg = std::make_unique<app_config>(provider_type::s3,
utils::path::absolute("./data"));
EXPECT_FALSE(cfg->set_value_by_name("S3Config.Bucket", "bucket").empty());
EXPECT_FALSE(
cfg->set_value_by_name("S3Config.URL", "https://url.com").empty());
comm = std::make_unique<mock_s3_comm>(cfg->get_s3_config());
provider = std::make_unique<s3_provider>(*cfg, *comm);
const auto create_file = [this](std::size_t idx,
bool create_sub_directory = false) {
api_file file{};
file.accessed_date = times[idx];
if (create_sub_directory) {
file.api_path = utils::path::create_api_path(
"/dir/file_" + std::to_string(idx) + ".txt");
} else {
file.api_path = utils::path::create_api_path(
"/file_" + std::to_string(idx) + ".txt");
}
file.api_parent = utils::path::get_parent_api_path(file.api_path);
file.changed_date = times[idx] + 1u;
file.creation_date = times[idx] + 2u;
file.encryption_token = "";
file.file_size = 100u + idx;
file.modified_date = times[idx] + 3u;
this->list.emplace_back(std::move(file));
};
for (std::size_t idx = 0u; idx < times.size(); idx++) {
create_file(idx);
}
for (std::size_t idx = 0u; idx < times.size(); idx++) {
create_file(idx, true);
}
EXPECT_CALL(*comm, get_file_list)
.WillRepeatedly(
[this](const get_api_file_token_callback &get_api_file_token,
const get_name_callback &get_name, api_file_list &l) {
for (auto i : list) {
auto object_name = i.api_path;
object_name = get_name(
*(utils::string::split(i.api_path, '/', false).end() - 1u),
object_name);
i.api_path = object_name;
i.api_parent = utils::path::get_parent_api_path(i.api_path);
i.encryption_token = get_api_file_token(i.api_path);
l.emplace_back(i);
}
return api_error::success;
});
EXPECT_CALL(*comm, get_file)
.WillRepeatedly(
[this](const std::string &api_path, const get_key_callback &get_key,
const get_name_callback &get_name,
const get_token_callback &get_token, api_file &file) {
auto f = std::find_if(list.begin(), list.end(),
[&api_path](const auto &f) -> bool {
return f.api_path == api_path;
});
if (f == list.end()) {
return api_error::item_not_found;
}
file = *f;
auto object_name = api_path;
const auto key = get_key();
object_name = get_name(key, object_name);
file.api_path = utils::path::create_api_path(object_name);
file.api_parent = utils::path::get_parent_api_path(file.api_path);
file.encryption_token = get_token();
return api_error::success;
});
}
void TearDown() override {
provider->stop();
comm.reset();
provider.reset();
cfg.reset();
event_system::instance().stop();
EXPECT_TRUE(utils::file::delete_directory_recursively(
utils::path::absolute("./data")));
}
};
} // namespace repertory
#endif // REPERTORY_ENABLE_S3_TESTING
#endif // REPERTORY_S3_PROVIDER_FILE_LIST_FIXTURE_H

View File

@ -1,105 +0,0 @@
/*
Copyright <2018-2022> <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_SKYNET_PROVIDER_FIXTURE_H
#define REPERTORY_SKYNET_PROVIDER_FIXTURE_H
#if defined(REPERTORY_ENABLE_SKYNET)
#include "comm/curl/curl_comm.hpp"
#include "app_config.hpp"
#include "mocks/mock_open_file_table.hpp"
#include "providers/skynet/skynet_provider.hpp"
#include "test_common.hpp"
#include "utils/path_utils.hpp"
namespace repertory {
class skynet_provider_test : public ::testing::Test {
private:
console_consumer c_;
const std::string config_location_ = utils::path::absolute("./skynetprovider");
protected:
std::unique_ptr<app_config> config_;
std::unique_ptr<curl_comm> curl_comm_;
std::unique_ptr<skynet_provider> provider_;
mock_open_file_table oft_;
public:
void SetUp() override {
utils::file::delete_directory_recursively(config_location_);
config_ = std::make_unique<app_config>(provider_type::skynet, config_location_);
config_->set_event_level(event_level::verbose);
event_system::instance().start();
curl_comm_ = std::make_unique<curl_comm>(*config_.get());
provider_ = std::make_unique<skynet_provider>(*config_.get(), *curl_comm_.get());
const auto res = provider_->start(
[&](const std::string &api_path, const std::string &api_parent, const std::string &source,
const bool &directory, const std::uint64_t &created_date,
const std::uint64_t &accessed_date, const std::uint64_t &modified_date,
const std::uint64_t &changed_date) {
event_system::instance().raise<filesystem_item_added>(api_path, api_parent, directory);
#ifdef _WIN32
provider_->set_item_meta(
api_path, {{META_ATTRIBUTES, std::to_string(directory ? FILE_ATTRIBUTE_DIRECTORY
: FILE_ATTRIBUTE_NORMAL |
FILE_ATTRIBUTE_ARCHIVE)},
{META_CREATION, std::to_string(created_date)},
{META_WRITTEN, std::to_string(modified_date)},
{META_MODIFIED, std::to_string(modified_date)},
{ META_ACCESSED,
std::to_string(accessed_date) }});
#else
provider_->set_item_meta(
api_path,
{{META_CREATION, std::to_string(created_date)},
{META_MODIFIED, std::to_string(modified_date)},
{META_WRITTEN, std::to_string(modified_date)},
{META_ACCESSED, std::to_string(accessed_date)},
{META_OSXFLAGS, "0"},
{META_BACKUP, "0"},
{META_CHANGED, std::to_string(changed_date)},
{META_MODE, utils::string::from_uint32(directory ? S_IRUSR | S_IWUSR | S_IXUSR
: S_IRUSR | S_IWUSR)},
{META_UID, utils::string::from_uint32(getuid())},
{META_GID, utils::string::from_uint32(getgid())}});
#endif
if (not directory && not source.empty()) {
provider_->set_source_path(api_path, source);
}
},
&oft_);
EXPECT_FALSE(res);
}
void TearDown() override {
provider_->stop();
event_system::instance().stop();
curl_comm_.reset();
config_.reset();
provider_.reset();
utils::file::delete_directory_recursively(config_location_);
}
};
} // namespace repertory
#endif // REPERTORY_ENABLE_SKYNET
#endif // REPERTORY_SKYNET_PROVIDER_FIXTURE_H

View File

@ -1,81 +1,146 @@
/*
Copyright <2018-2022> <scott.e.graves@protonmail.com>
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
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 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.
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 "app_config.hpp"
#include "drives/winfsp/winfsp_drive.hpp"
#include "mocks/mock_comm.hpp"
#include "platform/platform.hpp"
#include "providers/sia/sia_provider.hpp"
#include "providers/skynet/skynet_provider.hpp"
#include "test_common.hpp"
#include "app_config.hpp"
#include "comm/s3/s3_comm.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:
mock_comm mock_sia_comm_;
mock_comm mock_skynet_comm_;
std::array<std::tuple<std::shared_ptr<app_config>, std::shared_ptr<i_provider>,
std::shared_ptr<winfsp_drive>>,
2>
provider_tests_;
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;
#ifdef REPERTORY_ENABLE_S3
std::unique_ptr<s3_comm> s3_comm_;
#endif
protected:
void SetUp() override {
if (PROVIDER_INDEX != 0) {
utils::file::delete_directory_recursively("./winfsp_test");
auto config = std::make_shared<app_config>(provider_type::sia, "./winfsp_test");
config->set_enable_drive_events(true);
config->set_event_level(event_level::verbose);
config->set_api_port(11115);
auto sp = std::make_shared<sia_provider>(*config, mock_sia_comm_);
auto drive = std::make_shared<winfsp_drive>(*config, lock_data_, *sp);
provider_tests_[0] = {config, sp, drive};
if (PROVIDER_INDEX == 1) {
#ifdef REPERTORY_ENABLE_S3
EXPECT_TRUE(utils::file::delete_directory_recursively(
"./winfsp_test" + std::to_string(PROVIDER_INDEX)));
config = std::make_shared<app_config>(provider_type::skynet, "./winfsp_test");
config->set_enable_drive_events(true);
config->set_event_level(event_level::verbose);
config->set_api_port(11116);
auto skp = std::make_shared<skynet_provider>(*config, mock_skynet_comm_);
drive = std::make_shared<winfsp_drive>(*config, lock_data_, *skp);
provider_tests_[1] = {config, skp, drive};
app_config src_cfg(provider_type::s3,
utils::path::combine(get_test_dir(), {"filebase"}));
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.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();
event_system::instance().start();
s3_comm_ = std::make_unique<s3_comm>(*config);
provider = std::make_unique<s3_provider>(*config, *s3_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) {
for (auto &i : provider_tests_) {
std::get<2>(i).reset();
}
drive.reset();
provider.reset();
#ifdef REPERTORY_ENABLE_S3
s3_comm_.reset();
#endif
comm.reset();
config.reset();
event_system::instance().stop();
for (auto &i : provider_tests_) {
std::get<1>(i).reset();
std::get<0>(i).reset();
}
utils::file::delete_directory_recursively("./winfsp_test");
EXPECT_TRUE(utils::file::delete_directory_recursively(
"./winfsp_test" + std::to_string(PROVIDER_INDEX)));
}
}
};

724
tests/fuse_drive_test.cpp Normal file
View File

@ -0,0 +1,724 @@
/*
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 "app_config.hpp"
#include "comm/curl/curl_comm.hpp"
#include "comm/s3/s3_comm.hpp"
#include "drives/fuse/fuse_drive.hpp"
#include "platform/platform.hpp"
#include "providers/s3/s3_provider.hpp"
#include "providers/sia/sia_provider.hpp"
#include "types/repertory.hpp"
#include "utils/event_capture.hpp"
#include "utils/file_utils.hpp"
#include "utils/utils.hpp"
#ifndef ACCESSPERMS
#define ACCESSPERMS (S_IRWXU | S_IRWXG | S_IRWXO) /* 0777 */
#endif
namespace repertory {
static void execute_mount(app_config &cfg, lock_data &ld, i_provider &provider,
const std::vector<std::string> &drive_args,
std::thread &th) {
EXPECT_EQ(0, fuse_drive(cfg, ld, provider).mount(drive_args));
th.join();
}
static void execute_unmount(i_provider &provider,
const std::string &mount_location) {
filesystem_item fsi{};
EXPECT_EQ(api_error::success, provider.get_filesystem_item("/", true, fsi));
EXPECT_STREQ("/", fsi.api_path.c_str());
EXPECT_TRUE(fsi.api_parent.empty());
EXPECT_TRUE(fsi.directory);
EXPECT_TRUE(fsi.encryption_token.empty());
EXPECT_EQ(std::uint64_t(0u), fsi.size);
EXPECT_TRUE(fsi.source_path.empty());
EXPECT_FALSE(fsi.is_encrypted());
api_meta_map meta{};
EXPECT_EQ(api_error::success, provider.get_item_meta("/", meta));
for (const auto &kv : meta) {
std::cout << kv.first << '=' << kv.second << std::endl;
}
auto unmounted = false;
for (int i = 0; not unmounted && (i < 50); i++) {
unmounted = (fuse_base::unmount(mount_location) == 0);
if (not unmounted) {
std::this_thread::sleep_for(100ms);
}
}
EXPECT_TRUE(unmounted);
}
static auto create_file_and_test(const std::string &mount_location,
std::string name) -> std::string {
auto file_path =
utils::path::absolute(utils::path::combine(mount_location, {name}));
auto fd =
open(file_path.c_str(), O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP);
EXPECT_LE(1, fd);
EXPECT_TRUE(utils::file::is_file(file_path));
EXPECT_FALSE(utils::file::is_directory(file_path));
std::uint64_t file_size{};
EXPECT_TRUE(utils::file::get_file_size(file_path, file_size));
EXPECT_EQ(0u, file_size);
EXPECT_EQ(0, close(fd));
std::this_thread::sleep_for(1s);
return file_path;
}
static void rmdir_and_test(const std::string &directory_path) {
int ret = 0;
for (auto i = 0; ((ret = rmdir(directory_path.c_str())) != 0) && (i < 20);
i++) {
std::this_thread::sleep_for(100ms);
}
EXPECT_EQ(0, ret);
std::this_thread::sleep_for(1s);
EXPECT_FALSE(utils::file::is_directory(directory_path));
EXPECT_FALSE(utils::file::is_file(directory_path));
}
static void unlink_file_and_test(const std::string &file_path) {
int ret = 0;
for (auto i = 0; ((ret = unlink(file_path.c_str())) != 0) && (i < 20); i++) {
std::this_thread::sleep_for(100ms);
}
EXPECT_EQ(0, ret);
std::this_thread::sleep_for(1s);
EXPECT_FALSE(utils::file::is_directory(file_path));
EXPECT_FALSE(utils::file::is_file(file_path));
}
static void test_chmod(const std::string &api_path,
const std::string &file_path, i_provider &provider) {
EXPECT_EQ(0, chmod(file_path.c_str(), S_IRUSR | S_IWUSR));
std::string mode;
EXPECT_EQ(api_error::success,
provider.get_item_meta(api_path.c_str(), META_MODE, mode));
EXPECT_EQ(static_cast<std::uint32_t>(S_IRUSR | S_IWUSR),
ACCESSPERMS & utils::string::to_uint32(mode));
}
static void test_chown(const std::string &api_path,
const std::string &file_path, i_provider &provider) {
EXPECT_EQ(0, chown(file_path.c_str(), -1, 0));
std::string gid;
EXPECT_EQ(api_error::success,
provider.get_item_meta(api_path, META_GID, gid));
EXPECT_STREQ("0", gid.c_str());
EXPECT_EQ(0, chown(file_path.c_str(), 0, -1));
std::string uid;
EXPECT_EQ(api_error::success,
provider.get_item_meta(api_path, META_UID, uid));
EXPECT_STREQ("0", uid.c_str());
}
static void test_mkdir(const std::string &api_path,
const std::string &directory_path,
i_provider &provider) {
EXPECT_EQ(0, mkdir(directory_path.c_str(),
S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP));
EXPECT_TRUE(utils::file::is_directory(directory_path));
EXPECT_FALSE(utils::file::is_file(directory_path));
std::string uid;
EXPECT_EQ(api_error::success,
provider.get_item_meta(api_path, META_UID, uid));
EXPECT_EQ(getuid(), utils::string::to_uint32(uid));
std::string gid;
EXPECT_EQ(api_error::success,
provider.get_item_meta(api_path, META_GID, gid));
EXPECT_EQ(getgid(), utils::string::to_uint32(gid));
std::string mode;
EXPECT_EQ(api_error::success,
provider.get_item_meta(api_path, META_MODE, mode));
EXPECT_EQ(static_cast<std::uint32_t>(S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP |
S_IXGRP),
ACCESSPERMS & (utils::string::to_uint32(mode)));
}
static void test_write_and_read(const std::string &api_path,
const std::string &file_path,
i_provider &provider) {
auto fd =
open(file_path.c_str(), O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP);
EXPECT_LE(1, fd);
std::string data = "TestData";
EXPECT_EQ(data.size(),
static_cast<std::size_t>(write(fd, &data[0], data.size())));
EXPECT_EQ(0, lseek(fd, 0, SEEK_SET));
data_buffer read_data;
read_data.resize(data.size());
EXPECT_EQ(data.size(), static_cast<std::size_t>(
read(fd, &read_data[0], read_data.size())));
EXPECT_EQ(0, memcmp(&data[0], &read_data[0], data.size()));
EXPECT_EQ(0, close(fd));
std::this_thread::sleep_for(1s);
std::uint64_t file_size{};
EXPECT_TRUE(utils::file::get_file_size(file_path, file_size));
EXPECT_EQ(data.size(), file_size);
filesystem_item fsi{};
EXPECT_EQ(api_error::success,
provider.get_filesystem_item(api_path, false, fsi));
EXPECT_TRUE(utils::file::get_file_size(fsi.source_path, file_size));
EXPECT_EQ(data.size(), file_size);
}
static void test_rename_file(const std::string &from_file_path,
const std::string &to_file_path,
i_provider &provider) {
auto fd = open(from_file_path.c_str(), O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP);
EXPECT_LE(1, fd);
close(fd);
std::this_thread::sleep_for(1s);
if (provider.is_rename_supported()) {
EXPECT_EQ(0, rename(from_file_path.c_str(), to_file_path.c_str()));
EXPECT_FALSE(utils::file::is_file(from_file_path));
EXPECT_TRUE(utils::file::is_file(to_file_path));
} else {
EXPECT_EQ(-1, rename(from_file_path.c_str(), to_file_path.c_str()));
EXPECT_TRUE(utils::file::is_file(from_file_path));
EXPECT_FALSE(utils::file::is_file(to_file_path));
}
}
static void test_rename_directory(const std::string &from_dir_path,
const std::string &to_dir_path,
i_provider &provider) {
EXPECT_EQ(0, mkdir(from_dir_path.c_str(),
S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP));
std::this_thread::sleep_for(1s);
EXPECT_TRUE(utils::file::is_directory(from_dir_path));
if (provider.is_rename_supported()) {
EXPECT_EQ(0, rename(from_dir_path.c_str(), to_dir_path.c_str()));
EXPECT_FALSE(utils::file::is_directory(from_dir_path));
EXPECT_TRUE(utils::file::is_directory(to_dir_path));
} else {
EXPECT_EQ(-1, rename(from_dir_path.c_str(), to_dir_path.c_str()));
EXPECT_TRUE(utils::file::is_directory(from_dir_path));
EXPECT_FALSE(utils::file::is_directory(to_dir_path));
}
}
static void test_truncate(const std::string &file_path) {
EXPECT_EQ(0, truncate(file_path.c_str(), 10u));
std::uint64_t file_size{};
EXPECT_TRUE(utils::file::get_file_size(file_path, file_size));
EXPECT_EQ(std::uint64_t(10u), file_size);
}
static void test_ftruncate(const std::string &file_path) {
auto fd = open(file_path.c_str(), O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP);
EXPECT_LE(1, fd);
EXPECT_EQ(0, ftruncate(fd, 10u));
std::uint64_t file_size{};
EXPECT_TRUE(utils::file::get_file_size(file_path, file_size));
EXPECT_EQ(std::uint64_t(10u), file_size);
close(fd);
}
#ifndef __APPLE__
static void test_fallocate(const std::string &api_path,
const std::string &file_path, i_provider &provider) {
auto fd =
open(file_path.c_str(), O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP);
EXPECT_LE(1, fd);
EXPECT_EQ(0, fallocate(fd, 0, 0, 16));
std::uint64_t file_size{};
EXPECT_TRUE(utils::file::get_file_size(file_path, file_size));
EXPECT_EQ(16u, file_size);
EXPECT_EQ(0, close(fd));
filesystem_item fsi{};
EXPECT_EQ(api_error::success,
provider.get_filesystem_item(api_path, false, fsi));
file_size = 0u;
EXPECT_TRUE(utils::file::get_file_size(fsi.source_path, file_size));
EXPECT_EQ(16u, file_size);
}
#endif
static void test_file_getattr(const std::string & /* api_path */,
const std::string &file_path,
i_provider & /* provider */) {
auto fd =
open(file_path.c_str(), O_CREAT | O_RDONLY, S_IRUSR | S_IWUSR | S_IRGRP);
EXPECT_LE(1, fd);
EXPECT_EQ(0, close(fd));
struct stat64 st {};
EXPECT_EQ(0, stat64(file_path.c_str(), &st));
EXPECT_EQ(static_cast<std::uint32_t>(S_IRUSR | S_IWUSR | S_IRGRP),
ACCESSPERMS & st.st_mode);
EXPECT_FALSE(S_ISDIR(st.st_mode));
EXPECT_TRUE(S_ISREG(st.st_mode));
}
static void test_directory_getattr(const std::string & /* api_path */,
const std::string &directory_path,
i_provider & /* provider */) {
EXPECT_EQ(0, mkdir(directory_path.c_str(),
S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP));
struct stat64 st {};
EXPECT_EQ(0, stat64(directory_path.c_str(), &st));
EXPECT_EQ(static_cast<std::uint32_t>(S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP |
S_IXGRP),
ACCESSPERMS & st.st_mode);
EXPECT_TRUE(S_ISDIR(st.st_mode));
EXPECT_FALSE(S_ISREG(st.st_mode));
}
static void
test_write_operations_fail_if_read_only(const std::string &api_path,
const std::string &file_path,
i_provider &provider) {
auto fd =
open(file_path.c_str(), O_CREAT | O_RDONLY, S_IRUSR | S_IWUSR | S_IRGRP);
EXPECT_LE(1, fd);
std::string data = "TestData";
EXPECT_EQ(-1, write(fd, &data[0], data.size()));
EXPECT_EQ(-1, ftruncate(fd, 9u));
#ifndef __APPLE__
EXPECT_EQ(-1, fallocate(fd, 0, 0, 16));
#endif
EXPECT_EQ(0, close(fd));
std::this_thread::sleep_for(1s);
std::uint64_t file_size{};
EXPECT_TRUE(utils::file::get_file_size(file_path, file_size));
EXPECT_EQ(std::size_t(0u), file_size);
filesystem_item fsi{};
EXPECT_EQ(api_error::success,
provider.get_filesystem_item(api_path, false, fsi));
EXPECT_TRUE(utils::file::get_file_size(fsi.source_path, file_size));
EXPECT_EQ(std::size_t(0u), file_size);
}
#if !__APPLE__ && HAS_SETXATTR
static void test_xattr_invalid_parameters(const std::string &file_path) {
std::string attr = "moose";
EXPECT_EQ(-1, setxattr(nullptr, "user.test_attr", &attr[0], attr.size(),
XATTR_CREATE));
EXPECT_EQ(errno, EFAULT);
EXPECT_EQ(-1, setxattr(file_path.c_str(), nullptr, &attr[0], attr.size(),
XATTR_CREATE));
EXPECT_EQ(errno, EFAULT);
EXPECT_EQ(-1, setxattr(file_path.c_str(), "user.test_attr", nullptr,
attr.size(), XATTR_CREATE));
EXPECT_EQ(errno, EFAULT);
EXPECT_EQ(0, setxattr(file_path.c_str(), "user.test_attr", nullptr, 0,
XATTR_CREATE));
}
static void test_xattr_create_and_get(const std::string &file_path) {
std::string attr = "moose";
EXPECT_EQ(0, setxattr(file_path.c_str(), "user.test_attr", &attr[0],
attr.size(), XATTR_CREATE));
std::string val;
val.resize(attr.size());
EXPECT_EQ(attr.size(),
static_cast<std::size_t>(getxattr(
file_path.c_str(), "user.test_attr", &val[0], val.size())));
EXPECT_STREQ(attr.c_str(), val.c_str());
}
static void test_xattr_listxattr(const std::string &file_path) {
std::string attr = "moose";
EXPECT_EQ(0, setxattr(file_path.c_str(), "user.test_attr", &attr[0],
attr.size(), XATTR_CREATE));
EXPECT_EQ(0, setxattr(file_path.c_str(), "user.test_attr2", &attr[0],
attr.size(), XATTR_CREATE));
std::string val;
val.resize(attr.size());
EXPECT_EQ(attr.size(),
static_cast<std::size_t>(getxattr(
file_path.c_str(), "user.test_attr", &val[0], val.size())));
EXPECT_STREQ(attr.c_str(), val.c_str());
std::string data;
auto size = listxattr(file_path.c_str(), &data[0u], 0u);
EXPECT_EQ(31, size);
data.resize(size);
EXPECT_EQ(size, listxattr(file_path.c_str(), &data[0u], size));
char *ptr = &data[0u];
EXPECT_STREQ("user.test_attr", ptr);
ptr += strlen(ptr) + 1;
EXPECT_STREQ("user.test_attr2", ptr);
}
static void test_xattr_replace(const std::string &file_path) {
std::string attr = "moose";
EXPECT_EQ(0, setxattr(file_path.c_str(), "user.test_attr", &attr[0],
attr.size(), XATTR_CREATE));
attr = "cow";
EXPECT_EQ(0, setxattr(file_path.c_str(), "user.test_attr", &attr[0],
attr.size(), XATTR_REPLACE));
std::string val;
val.resize(attr.size());
EXPECT_EQ(attr.size(),
static_cast<std::size_t>(getxattr(
file_path.c_str(), "user.test_attr", &val[0], val.size())));
EXPECT_STREQ(attr.c_str(), val.c_str());
}
static void test_xattr_default_create(const std::string &file_path) {
std::string attr = "moose";
EXPECT_EQ(0, setxattr(file_path.c_str(), "user.test_attr", &attr[0],
attr.size(), 0));
std::string val;
val.resize(attr.size());
EXPECT_EQ(attr.size(),
static_cast<std::size_t>(getxattr(
file_path.c_str(), "user.test_attr", &val[0], val.size())));
EXPECT_STREQ(attr.c_str(), val.c_str());
}
static void test_xattr_default_replace(const std::string &file_path) {
std::string attr = "moose";
EXPECT_EQ(0, setxattr(file_path.c_str(), "user.test_attr", &attr[0],
attr.size(), 0));
attr = "cow";
EXPECT_EQ(0, setxattr(file_path.c_str(), "user.test_attr", &attr[0],
attr.size(), 0));
std::string val;
val.resize(attr.size());
EXPECT_EQ(attr.size(),
static_cast<std::size_t>(getxattr(
file_path.c_str(), "user.test_attr", &val[0], val.size())));
EXPECT_STREQ(attr.c_str(), val.c_str());
}
static void test_xattr_create_fails_if_exists(const std::string &file_path) {
std::string attr = "moose";
EXPECT_EQ(0, setxattr(file_path.c_str(), "user.test_attr", &attr[0],
attr.size(), 0));
EXPECT_EQ(-1, setxattr(file_path.c_str(), "user.test_attr", &attr[0],
attr.size(), XATTR_CREATE));
EXPECT_EQ(EEXIST, errno);
}
static void
test_xattr_create_fails_if_not_exists(const std::string &file_path) {
std::string attr = "moose";
EXPECT_EQ(-1, setxattr(file_path.c_str(), "user.test_attr", &attr[0],
attr.size(), XATTR_REPLACE));
EXPECT_EQ(ENODATA, errno);
}
static void test_xattr_removexattr(const std::string &file_path) {
std::string attr = "moose";
EXPECT_EQ(0, setxattr(file_path.c_str(), "user.test_attr", &attr[0],
attr.size(), XATTR_CREATE));
EXPECT_EQ(0, removexattr(file_path.c_str(), "user.test_attr"));
std::string val;
val.resize(attr.size());
EXPECT_EQ(-1,
getxattr(file_path.c_str(), "user.test_attr", &val[0], val.size()));
EXPECT_EQ(ENODATA, errno);
}
#endif
TEST(fuse_drive, all_tests) {
auto current_directory = std::filesystem::current_path();
for (std::size_t idx = 0U; idx < 2U; idx++) {
std::filesystem::current_path(current_directory);
const auto test_directory =
utils::path::absolute("./fuse_drive" + std::to_string(idx));
const auto mount_location = utils::path::absolute(
utils::path::combine(test_directory, {"mount", std::to_string(idx)}));
EXPECT_TRUE(utils::file::delete_directory_recursively(mount_location));
EXPECT_TRUE(utils::file::create_full_directory_path(mount_location));
{
std::unique_ptr<app_config> config_ptr{};
#ifdef REPERTORY_ENABLE_S3
std::unique_ptr<s3_comm> s3_comm_ptr{};
#endif
std::unique_ptr<curl_comm> comm_ptr{};
std::unique_ptr<i_provider> provider_ptr{};
std::unique_ptr<lock_data> lock_data_ptr{};
const auto cfg_directory = utils::path::absolute(
utils::path::combine(test_directory, {"cfg", std::to_string(idx)}));
EXPECT_TRUE(utils::file::delete_directory_recursively(cfg_directory));
EXPECT_TRUE(utils::file::create_full_directory_path(cfg_directory));
switch (idx) {
case 0U: {
#ifdef REPERTORY_ENABLE_S3
config_ptr =
std::make_unique<app_config>(provider_type::s3, cfg_directory);
{
app_config src_cfg(
provider_type::s3,
utils::path::combine(get_test_dir(), {"filebase"}));
config_ptr->set_enable_drive_events(true);
config_ptr->set_event_level(event_level::verbose);
config_ptr->set_s3_config(src_cfg.get_s3_config());
}
s3_comm_ptr = std::make_unique<s3_comm>(*config_ptr);
provider_ptr = std::make_unique<s3_provider>(*config_ptr, *s3_comm_ptr);
lock_data_ptr = std::make_unique<lock_data>(
provider_type::s3, "unittests_" + std::to_string(idx));
#else
continue;
#endif
} break;
case 1U: {
config_ptr =
std::make_unique<app_config>(provider_type::sia, cfg_directory);
{
app_config src_cfg(provider_type::sia,
utils::path::combine(get_test_dir(), {"sia"}));
config_ptr->set_enable_drive_events(true);
config_ptr->set_event_level(event_level::debug);
config_ptr->set_host_config(src_cfg.get_host_config());
}
comm_ptr = std::make_unique<curl_comm>(config_ptr->get_host_config());
provider_ptr = std::make_unique<sia_provider>(*config_ptr, *comm_ptr);
lock_data_ptr = std::make_unique<lock_data>(
provider_type::sia, "unittests_" + std::to_string(idx));
} break;
default:
std::cerr << "provider at index " << idx << " is not implemented"
<< std::endl;
continue;
}
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) {
EXPECT_EQ(0,
system(("mount|grep \"" + mount_location + "\"").c_str()));
auto file_path = create_file_and_test(mount_location, "chmod_test");
test_chmod(utils::path::create_api_path("chmod_test"), file_path,
*provider_ptr);
unlink_file_and_test(file_path);
file_path = create_file_and_test(mount_location, "chown_test");
test_chown(utils::path::create_api_path("chown_test"), file_path,
*provider_ptr);
unlink_file_and_test(file_path);
file_path = utils::path::combine(mount_location, {"mkdir_test"});
test_mkdir(utils::path::create_api_path("mkdir_test"), file_path,
*provider_ptr);
rmdir_and_test(file_path);
file_path = create_file_and_test(mount_location, "write_read_test");
test_write_and_read(utils::path::create_api_path("write_read_test"),
file_path, *provider_ptr);
unlink_file_and_test(file_path);
file_path =
create_file_and_test(mount_location, "from_rename_file_test");
auto to_file_path = utils::path::absolute(
utils::path::combine(mount_location, {"to_rename_file_test"}));
test_rename_file(file_path, to_file_path, *provider_ptr);
EXPECT_TRUE(utils::file::retry_delete_file(file_path));
EXPECT_TRUE(utils::file::retry_delete_file(to_file_path));
file_path = utils::path::absolute(
utils::path::combine(mount_location, {"from_rename_dir_test"}));
to_file_path = utils::path::absolute(
utils::path::combine(mount_location, {"to_rename_dir_test"}));
test_rename_directory(file_path, to_file_path, *provider_ptr);
EXPECT_TRUE(utils::file::retry_delete_directory(file_path.c_str()));
EXPECT_TRUE(
utils::file::retry_delete_directory(to_file_path.c_str()));
file_path =
create_file_and_test(mount_location, "truncate_file_test");
test_truncate(file_path);
unlink_file_and_test(file_path);
file_path =
create_file_and_test(mount_location, "ftruncate_file_test");
test_ftruncate(file_path);
unlink_file_and_test(file_path);
#ifndef __APPLE__
file_path =
create_file_and_test(mount_location, "fallocate_file_test");
test_fallocate(utils::path::create_api_path("fallocate_file_test"),
file_path, *provider_ptr);
unlink_file_and_test(file_path);
#endif
file_path =
create_file_and_test(mount_location, "write_fails_ro_test");
test_write_operations_fail_if_read_only(
utils::path::create_api_path("write_fails_ro_test"), file_path,
*provider_ptr);
unlink_file_and_test(file_path);
file_path = create_file_and_test(mount_location, "getattr.txt");
test_file_getattr(utils::path::create_api_path("getattr.txt"),
file_path, *provider_ptr);
unlink_file_and_test(file_path);
file_path = utils::path::combine(mount_location, {"getattr_dir"});
test_directory_getattr(utils::path::create_api_path("getattr_dir"),
file_path, *provider_ptr);
rmdir_and_test(file_path);
#if !__APPLE__ && HAS_SETXATTR
file_path =
create_file_and_test(mount_location, "xattr_invalid_names_test");
test_xattr_invalid_parameters(file_path);
unlink_file_and_test(file_path);
file_path =
create_file_and_test(mount_location, "xattr_create_get_test");
test_xattr_create_and_get(file_path);
unlink_file_and_test(file_path);
file_path =
create_file_and_test(mount_location, "xattr_listxattr_test");
test_xattr_listxattr(file_path);
unlink_file_and_test(file_path);
file_path =
create_file_and_test(mount_location, "xattr_replace_test");
test_xattr_replace(file_path);
unlink_file_and_test(file_path);
file_path =
create_file_and_test(mount_location, "xattr_default_create_test");
test_xattr_default_create(file_path);
unlink_file_and_test(file_path);
file_path = create_file_and_test(mount_location,
"xattr_default_replace_test");
test_xattr_default_replace(file_path);
unlink_file_and_test(file_path);
file_path = create_file_and_test(mount_location,
"xattr_create_fails_exists_test");
test_xattr_create_fails_if_exists(file_path);
unlink_file_and_test(file_path);
file_path = create_file_and_test(
mount_location, "xattr_create_fails_not_exists_test");
test_xattr_create_fails_if_not_exists(file_path);
unlink_file_and_test(file_path);
file_path =
create_file_and_test(mount_location, "xattr_removexattr_test");
test_xattr_removexattr(file_path);
unlink_file_and_test(file_path);
#endif
}
execute_unmount(*provider_ptr, mount_location);
});
auto drive_args =
std::vector<std::string>({"unittests", "-f", mount_location});
execute_mount(*config_ptr, *lock_data_ptr, *provider_ptr, drive_args, th);
ec.wait_for_empty();
}
EXPECT_TRUE(utils::file::delete_directory_recursively(mount_location));
EXPECT_TRUE(utils::file::delete_directory_recursively(test_directory));
}
std::filesystem::current_path(current_directory);
}
} // namespace repertory
#endif

File diff suppressed because it is too large Load Diff

View File

@ -1,49 +1,43 @@
/*
Copyright <2018-2022> <scott.e.graves@protonmail.com>
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
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 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.
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 "common.hpp"
#include "platform/platform.hpp"
#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());
lock_data l2(provider_type::skynet, "1");
EXPECT_EQ(lock_result::success, l2.grab_lock());
std::thread([]() {
lock_data l(provider_type::sia, "1");
EXPECT_EQ(lock_result::locked, l.grab_lock(10));
lock_data l2(provider_type::skynet, "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));
lock_data l2(provider_type::skynet, "1");
EXPECT_EQ(lock_result::success, l2.grab_lock(10));
}).join();
#ifdef _WIN32
@ -60,60 +54,51 @@ 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::skynet, "1");
EXPECT_TRUE(l2.set_mount_state(true, "D:", 98));
lock_data l2(provider_type::remote, "1");
EXPECT_TRUE(l2.set_mount_state(true, "D:", 97));
lock_data l3(provider_type::remote, "1");
EXPECT_TRUE(l3.set_mount_state(true, "E:", 97));
lock_data l4(provider_type::remote, "2");
EXPECT_TRUE(l4.set_mount_state(true, "F:", 96));
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":"E:","PID":97})",
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":"F:","PID":96})",
EXPECT_STREQ(R"({"Active":true,"Location":"E:","PID":96})",
mount_state["Remote2"].dump().c_str());
EXPECT_STREQ(R"({"Active":true,"Location":"C:","PID":99})", mount_state["Sia1"].dump().c_str());
EXPECT_STREQ(R"({"Active":true,"Location":"D:","PID":98})",
mount_state["Skynet1"].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(l4.set_mount_state(false, "F:", 96));
EXPECT_TRUE(l.get_mount_state(mount_state));
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());
EXPECT_STREQ(R"({"Active":false,"Location":"","PID":-1})", mount_state["Sia1"].dump().c_str());
EXPECT_STREQ(R"({"Active":false,"Location":"","PID":-1})", mount_state["Skynet1"].dump().c_str());
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));
lock_data l2(provider_type::skynet, "1");
EXPECT_TRUE(l2.set_mount_state(true, "/mnt/2", 98));
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_STREQ(R"({"Active":true,"Location":"/mnt/2","PID":98})",
mount_state["Skynet1"].dump().c_str());
EXPECT_TRUE(l.set_mount_state(false, "/mnt/1", 99));
EXPECT_TRUE(l2.set_mount_state(false, "/mnt/t", 98));
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["Skynet1"].dump().c_str());
EXPECT_STREQ(R"({"Active":false,"Location":"","PID":-1})",
mount_state["Sia1"].dump().c_str());
}
#endif
} // namespace repertory

292
tests/meta_db_test.cpp Normal file
View File

@ -0,0 +1,292 @@
/*
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 "fixtures/meta_db_fixture.hpp"
namespace repertory {
TEST_F(meta_db_test, get_and_set_item_meta) {
api_meta_map meta{
{"test", "test_value"},
{"test2", "test_value2"},
{"test3", "test_value3"},
};
EXPECT_EQ(api_error::success, db_->set_item_meta("/test/item", meta));
api_meta_map meta2{};
EXPECT_EQ(api_error::success, db_->get_item_meta("/test/item", meta2));
EXPECT_EQ(meta, meta2);
}
TEST_F(meta_db_test, get_and_set_item_meta_single_key) {
EXPECT_EQ(api_error::success,
db_->set_item_meta("/test/item", "test", "moose"));
std::string value;
EXPECT_EQ(api_error::success,
db_->get_item_meta("/test/item", "test", value));
EXPECT_STREQ("moose", value.c_str());
}
TEST_F(meta_db_test,
get_item_meta_fails_with_not_found_for_items_that_dont_exist) {
api_meta_map meta{};
EXPECT_EQ(api_error::item_not_found, db_->get_item_meta("/test/item", meta));
std::string value;
EXPECT_EQ(api_error::item_not_found,
db_->get_item_meta("/test/item", "test", value));
EXPECT_TRUE(value.empty());
}
TEST_F(meta_db_test, get_item_meta_exists) {
EXPECT_EQ(api_error::success,
db_->set_item_meta("/test/item", "test", "value"));
EXPECT_TRUE(db_->get_item_meta_exists("/test/item"));
}
TEST_F(meta_db_test, get_item_meta_exists_is_false_if_not_found) {
EXPECT_FALSE(db_->get_item_meta_exists("/test/item"));
}
TEST_F(meta_db_test, remove_item_meta) {
api_meta_map meta{
{"test", "test_value"},
{"test2", "test_value2"},
{"test3", "test_value3"},
};
EXPECT_EQ(api_error::success, db_->set_item_meta("/test/item", meta));
EXPECT_EQ(api_error::success, db_->remove_item_meta("/test/item"));
api_meta_map meta2{};
EXPECT_EQ(api_error::item_not_found, db_->get_item_meta("/test/item", meta2));
EXPECT_TRUE(meta2.empty());
}
TEST_F(meta_db_test, remove_item_meta_single_key) {
api_meta_map meta{
{"test", "test_value"},
{"test2", "test_value2"},
{"test3", "test_value3"},
};
EXPECT_EQ(api_error::success, db_->set_item_meta("/test/item", meta));
EXPECT_EQ(api_error::success, db_->remove_item_meta("/test/item", "test"));
api_meta_map meta2{};
EXPECT_EQ(api_error::success, db_->get_item_meta("/test/item", meta2));
meta.erase("test");
EXPECT_EQ(meta, meta2);
}
TEST_F(meta_db_test, get_and_set_source_path) {
EXPECT_EQ(api_error::success,
db_->set_source_path("/test/item", "/test/path"));
std::string value;
EXPECT_EQ(api_error::success,
db_->get_item_meta("/test/item", META_SOURCE, value));
EXPECT_STREQ("/test/path", value.c_str());
}
// TEST_F(meta_db_test, set_source_path_fails_on_empty_path) {
// EXPECT_EQ(api_error::invalid_operation, db_->set_source_path("/test/item",
// ""));
//
// std::string value;
// EXPECT_EQ(api_error::item_not_found, db_->get_item_meta("/test/item",
// META_SOURCE, value)); EXPECT_TRUE(value.empty());
// }
TEST_F(meta_db_test, get_api_path_from_source) {
EXPECT_EQ(api_error::success,
db_->set_source_path("/test/item", "/test/path"));
std::string value;
EXPECT_EQ(api_error::success,
db_->get_api_path_from_source("/test/path", value));
EXPECT_STREQ("/test/item", value.c_str());
}
TEST_F(meta_db_test, get_api_path_from_source_succeeds_after_change) {
EXPECT_EQ(api_error::success,
db_->set_source_path("/test/item", "/test/path"));
EXPECT_EQ(api_error::success,
db_->set_source_path("/test/item", "/test/path2"));
std::string value;
EXPECT_EQ(api_error::success,
db_->get_api_path_from_source("/test/path2", value));
EXPECT_STREQ("/test/item", value.c_str());
value.clear();
EXPECT_EQ(api_error::item_not_found,
db_->get_api_path_from_source("/test/path", value));
EXPECT_TRUE(value.empty());
}
TEST_F(meta_db_test, get_api_path_from_source_fails_after_remove_all_meta) {
EXPECT_EQ(api_error::success,
db_->set_source_path("/test/item", "/test/path"));
EXPECT_EQ(api_error::success, db_->remove_item_meta("/test/item"));
std::string value;
EXPECT_EQ(api_error::item_not_found,
db_->get_api_path_from_source("/test/path", value));
EXPECT_TRUE(value.empty());
}
TEST_F(meta_db_test, get_api_path_from_source_fails_after_remove_source_key) {
EXPECT_EQ(api_error::success,
db_->set_source_path("/test/item", "/test/path"));
EXPECT_EQ(api_error::success,
db_->remove_item_meta("/test/item", META_SOURCE));
std::string value;
EXPECT_EQ(api_error::item_not_found,
db_->get_api_path_from_source("/test/path", value));
EXPECT_TRUE(value.empty());
}
TEST_F(meta_db_test, get_source_path_exists) {
EXPECT_EQ(api_error::success,
db_->set_source_path("/test/item", "/test/path"));
EXPECT_TRUE(db_->get_source_path_exists("/test/path"));
}
TEST_F(meta_db_test, get_source_path_exists_is_false_if_not_found) {
EXPECT_FALSE(db_->get_source_path_exists("/test/item"));
}
TEST_F(meta_db_test, get_api_path_from_key) {
EXPECT_EQ(api_error::success,
db_->set_item_meta("/test/item", META_KEY, "key"));
std::string value;
EXPECT_EQ(api_error::success, db_->get_api_path_from_key("key", value));
EXPECT_STREQ("/test/item", value.c_str());
}
TEST_F(meta_db_test, remove_item_meta_succeeds_for_items_that_dont_exist) {
EXPECT_EQ(api_error::success, db_->remove_item_meta("/test/item"));
EXPECT_EQ(api_error::success, db_->remove_item_meta("/test/item", "test"));
}
TEST_F(meta_db_test, remove_item_meta_removes_source_path) {
EXPECT_EQ(api_error::success,
db_->set_source_path("/test/item", "/source/path"));
EXPECT_EQ(api_error::success, db_->remove_item_meta("/test/item"));
std::string api_path;
EXPECT_EQ(api_error::item_not_found,
db_->get_api_path_from_source("/source/path", api_path));
EXPECT_TRUE(api_path.empty());
}
TEST_F(meta_db_test, remove_item_meta_by_key_removes_source_path) {
EXPECT_EQ(api_error::success,
db_->set_source_path("/test/item", "/source/path"));
EXPECT_EQ(api_error::success,
db_->remove_item_meta("/test/item", META_SOURCE));
std::string api_path;
EXPECT_EQ(api_error::item_not_found,
db_->get_api_path_from_source("/source/path", api_path));
EXPECT_TRUE(api_path.empty());
}
TEST_F(meta_db_test, remove_item_meta_removes_key) {
EXPECT_EQ(api_error::success,
db_->set_item_meta("/test/item", META_KEY, "key"));
EXPECT_EQ(api_error::success, db_->remove_item_meta("/test/item"));
std::string api_path;
EXPECT_EQ(api_error::item_not_found,
db_->get_api_path_from_key("key", api_path));
EXPECT_TRUE(api_path.empty());
}
TEST_F(meta_db_test, remove_item_meta_by_key_removes_key) {
EXPECT_EQ(api_error::success,
db_->set_item_meta("/test/item", META_KEY, "key"));
EXPECT_EQ(api_error::success, db_->remove_item_meta("/test/item", META_KEY));
std::string api_path;
EXPECT_EQ(api_error::item_not_found,
db_->get_api_path_from_key("/source/path", api_path));
EXPECT_TRUE(api_path.empty());
}
TEST_F(meta_db_test, rename_item_meta) {
api_meta_map meta{
{"test", "test_value"},
{"test2", "test_value2"},
{"test3", "test_value3"},
};
EXPECT_EQ(api_error::success, db_->set_item_meta("/test/item", meta));
EXPECT_EQ(api_error::success,
db_->rename_item_meta("", "/test/item", "/test/item2"));
api_meta_map meta2{};
EXPECT_EQ(api_error::item_not_found, db_->get_item_meta("/test/item", meta2));
EXPECT_EQ(api_error::success, db_->get_item_meta("/test/item2", meta2));
EXPECT_EQ(meta, meta2);
}
TEST_F(meta_db_test, rename_item_meta_with_key) {
api_meta_map meta{
{META_KEY, "test_key"},
{"test2", "test_value2"},
{"test3", "test_value3"},
};
EXPECT_EQ(api_error::success, db_->set_item_meta("/test/item", meta));
EXPECT_EQ(api_error::success,
db_->rename_item_meta("", "/test/item", "/test/item2"));
std::string api_path;
EXPECT_EQ(api_error::success,
db_->get_api_path_from_key("test_key", api_path));
EXPECT_STREQ("/test/item2", api_path.c_str());
}
TEST_F(meta_db_test, rename_item_meta_with_source_path) {
api_meta_map meta{
{META_SOURCE, "/test/source"},
{"test2", "test_value2"},
{"test3", "test_value3"},
};
EXPECT_EQ(api_error::success, db_->set_item_meta("/test/item", meta));
EXPECT_EQ(api_error::success,
db_->rename_item_meta("/test/source", "/test/item", "/test/item2"));
std::string api_path;
EXPECT_EQ(api_error::success,
db_->get_api_path_from_source("/test/source", api_path));
EXPECT_STREQ("/test/item2", api_path.c_str());
}
} // namespace repertory

View File

@ -1,176 +0,0 @@
/*
Copyright <2018-2022> <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_COMM_HPP_
#define TESTS_MOCKS_MOCK_COMM_HPP_
#include "test_common.hpp"
#include "comm/i_comm.hpp"
namespace repertory {
class mock_comm : public virtual i_comm {
private:
struct mock_data {
api_error error;
json result;
json json_error;
bool persist = false;
};
std::unordered_map<std::string, std::deque<mock_data>> return_lookup_;
public:
void push_return(const std::string &op, const std::string &path, const json &result,
const json &error, const api_error &apiError, bool persist = false) {
const auto lookup_path = op + path;
if (return_lookup_.find(lookup_path) == return_lookup_.end()) {
return_lookup_[lookup_path] = std::deque<mock_data>();
};
mock_data data = {apiError, result, error, persist};
return_lookup_[lookup_path].emplace_back(data);
}
void remove_return(const std::string &op, const std::string &path) {
const auto lookup_path = op + path;
return_lookup_.erase(lookup_path);
}
private:
api_error process(const std::string &op, const std::string &path, json &data, json &error) {
const auto lookup_path = op + path;
if ((return_lookup_.find(lookup_path) == return_lookup_.end()) ||
return_lookup_[lookup_path].empty()) {
EXPECT_STREQ("", ("unexpected path: " + lookup_path).c_str());
return api_error::comm_error;
}
error = return_lookup_[lookup_path].front().json_error;
data = return_lookup_[lookup_path].front().result;
const auto ret = return_lookup_[lookup_path].front().error;
if (not return_lookup_[lookup_path].front().persist) {
return_lookup_[lookup_path].pop_front();
}
return ret;
}
public:
api_error get(const std::string &path, json &data, json &error) override {
return process("get", path, data, error);
}
api_error get(const host_config &, const std::string &path, json &data, json &error) override {
return process("get", path, data, error);
}
api_error get(const std::string &path, const http_parameters &, json &data,
json &error) override {
return process("get_params", path, data, error);
}
api_error get(const host_config &, const std::string &path, const http_parameters &, json &data,
json &error) override {
return process("get_params", path, data, error);
}
api_error get_range(const std::string & /*path*/, const std::uint64_t & /*data_size*/,
const http_parameters & /*parameters*/,
const std::string & /*encryption_token*/, std::vector<char> & /*data*/,
const http_ranges & /*ranges*/, json & /*error*/,
const bool & /*stop_requested*/) override {
return api_error::error;
}
api_error get_range(const host_config & /*hc*/, const std::string & /*path*/,
const std::uint64_t & /*data_size*/, const http_parameters & /*parameters*/,
const std::string & /*encryption_token*/, std::vector<char> & /*data*/,
const http_ranges & /*ranges*/, json & /*error*/,
const bool & /*stop_requested*/) override {
return api_error::error;
}
api_error get_range_and_headers(const std::string & /*path*/, const std::uint64_t & /*data_size*/,
const http_parameters & /*parameters*/,
const std::string & /*encryption_token*/,
std::vector<char> & /*data*/, const http_ranges & /*ranges*/,
json & /*error*/, http_headers & /*headers*/,
const bool & /*stop_requested*/) override {
return api_error::error;
}
api_error get_range_and_headers(const host_config & /*hc*/, const std::string & /*path*/,
const std::uint64_t & /*data_size*/,
const http_parameters & /*parameters*/,
const std::string & /*encryption_token*/,
std::vector<char> & /*data*/, const http_ranges & /*ranges*/,
json & /*error*/, http_headers & /*headers*/,
const bool & /*stop_requested*/) override {
return api_error::error;
}
api_error get_raw(const std::string &, const http_parameters &, std::vector<char> &, json &,
const bool &) override {
throw std::runtime_error("not implemented: get_raw");
return api_error::comm_error;
}
api_error get_raw(const host_config &, const std::string &, const http_parameters &,
std::vector<char> &, json &, const bool &) override {
throw std::runtime_error("not implemented: get_raw");
return api_error::comm_error;
}
api_error post(const std::string &path, json &data, json &error) override {
return process("post", path, data, error);
}
api_error post(const host_config &, const std::string &path, json &data, json &error) override {
return process("post", path, data, error);
}
api_error post(const std::string &path, const http_parameters &, json &data,
json &error) override {
return process("post_params", path, data, error);
}
api_error post(const host_config &, const std::string &path, const http_parameters &, json &data,
json &error) override {
return process("post_params", path, data, error);
}
api_error post_file(const std::string &, const std::string &, const http_parameters &, json &,
json &, const bool &) override {
return api_error::error;
}
api_error post_file(const host_config &, const std::string &, const std::string &,
const http_parameters &, json &, json &, const bool &) override {
return api_error::error;
}
api_error post_multipart_file(const std::string &, const std::string &, const std::string &,
const std::string &, json &, json &, const bool &) override {
return api_error::error;
}
api_error post_multipart_file(const host_config &, const std::string &, const std::string &,
const std::string &, const std::string &, json &, json &,
const bool &) override {
return api_error::error;
}
};
} // namespace repertory
#endif // TESTS_MOCKS_MOCK_COMM_HPP_

View File

@ -1,26 +1,30 @@
/*
Copyright <2018-2022> <scott.e.graves@protonmail.com>
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
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 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.
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 "common.hpp"
#include "test_common.hpp"
#include "drives/fuse/i_fuse_drive.hpp"
#include "types/remote.hpp"
#include "types/repertory.hpp"
@ -39,11 +43,17 @@ private:
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; }
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{};
@ -67,7 +77,8 @@ public:
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 {
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;
}
@ -81,7 +92,9 @@ public:
return api_error::success;
}
std::uint64_t get_total_drive_space() const override { return 100 * 1024 * 1024; }
std::uint64_t get_total_drive_space() const override {
return 100 * 1024 * 1024;
}
std::uint64_t get_total_item_count() const override { return 0u; }
@ -96,22 +109,28 @@ public:
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});
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,
const 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});
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::delete_file(to_file_path)) {
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)) {
} else if (utils::file::is_directory(to_file_path) ||
utils::file::is_file(to_file_path)) {
errno = EEXIST;
return -1;
}
@ -125,8 +144,6 @@ public:
const std::string &value) override {
meta_[api_path][key] = value;
}
void update_directory_item(directory_item &) const override {}
};
} // namespace repertory

View File

@ -0,0 +1,97 @@
/*
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 &cb), (override));
MOCK_METHOD(api_error, native_operation,
(std::uint64_t new_file_size,
const native_operation_callback &cb),
(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,70 +0,0 @@
/*
Copyright <2018-2022> <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_TABLE_HPP_
#define TESTS_MOCKS_MOCK_OPEN_FILE_TABLE_HPP_
#include "download/download_manager.hpp"
#include "drives/i_open_file_table.hpp"
#include "test_common.hpp"
namespace repertory {
class mock_open_file_table : public virtual i_open_file_table {
public:
explicit mock_open_file_table(download_manager *dm = nullptr, filesystem_item *fsi = nullptr)
: dm_(dm), fsi_(fsi) {}
private:
download_manager *dm_;
filesystem_item *fsi_;
public:
MOCK_CONST_METHOD1(get_open_count, std::uint64_t(const std::string &api_path));
MOCK_CONST_METHOD0(has_no_open_file_handles, bool());
MOCK_METHOD1(close, void(const std::uint64_t &handle));
MOCK_CONST_METHOD1(contains_restore, bool(const std::string &api_path));
MOCK_METHOD1(evict_file, bool(const std::string &api_path));
MOCK_CONST_METHOD1(get_directory_items, directory_item_list(const std::string &api_path));
MOCK_METHOD2(get_open_file, bool(const std::string &api_path, struct filesystem_item *&fsi));
MOCK_CONST_METHOD0(get_open_files, std::unordered_map<std::string, std::size_t>());
MOCK_METHOD1(force_schedule_upload, void(const struct filesystem_item &fsi));
MOCK_METHOD2(open, api_error(const struct filesystem_item &fsi, std::uint64_t &handle));
MOCK_METHOD3(set_item_meta, api_error(const std::string &api_path, const std::string &key,
const std::string &value));
bool perform_locked_operation(locked_operation_callback /*locked_operation*/) override {
if (fsi_ && dm_) {
fsi_->source_path = dm_->get_source_path(fsi_->api_path);
}
return false;
}
void update_directory_item(directory_item &) const override {}
};
} // namespace repertory
#endif // TESTS_MOCKS_MOCK_OPEN_FILE_TABLE_HPP_

View File

@ -1,84 +1,162 @@
/*
Copyright <2018-2022> <scott.e.graves@protonmail.com>
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
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 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.
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_MOCKPROVIDER_HPP_
#define TESTS_MOCKS_MOCKPROVIDER_HPP_
#ifndef TESTS_MOCKS_MOCK_PROVIDER_HPP_
#define TESTS_MOCKS_MOCK_PROVIDER_HPP_
#include "test_common.hpp"
#include "providers/i_provider.hpp"
#include "test_common.hpp"
#include "types/repertory.hpp"
namespace repertory {
class mock_provider : public virtual i_provider {
public:
mock_provider(const bool &allow_rename = true) : allow_rename_(allow_rename) {}
mock_provider(bool allow_rename = true) : allow_rename_(allow_rename) {}
private:
const bool allow_rename_;
public:
MOCK_METHOD2(create_directory, api_error(const std::string &api_path, const api_meta_map &meta));
MOCK_METHOD2(create_directory_clone_source_meta,
api_error(const std::string &source_api_path, const std::string &api_path));
MOCK_METHOD2(create_file, api_error(const std::string &api_path, const api_meta_map &meta));
MOCK_CONST_METHOD2(get_api_path_from_source,
api_error(const std::string &source_path, std::string &api_path));
MOCK_CONST_METHOD1(get_directory_item_count, std::uint64_t(const std::string &api_path));
MOCK_CONST_METHOD2(get_directory_items,
api_error(const std::string &api_path, directory_item_list &list));
MOCK_CONST_METHOD2(get_file, api_error(const std::string &api_path, ApiFile &apiFile));
MOCK_CONST_METHOD1(get_file_list, api_error(api_file_list &list));
MOCK_CONST_METHOD2(get_file_size,
api_error(const std::string &api_path, std::uint64_t &file_size));
MOCK_CONST_METHOD3(get_filesystem_item, api_error(const std::string &api_path,
const bool &directory, filesystem_item &fsi));
MOCK_CONST_METHOD2(get_filesystem_item_from_source_path,
api_error(const std::string &source_path, filesystem_item &fsi));
MOCK_CONST_METHOD2(get_item_meta, api_error(const std::string &api_path, api_meta_map &meta));
MOCK_CONST_METHOD3(get_item_meta, api_error(const std::string &api_path, const std::string &key,
std::string &value));
MOCK_CONST_METHOD0(get_pinned_files, std::vector<std::string>());
MOCK_CONST_METHOD0(get_provider_type, provider_type());
MOCK_CONST_METHOD0(get_total_drive_space, std::uint64_t());
MOCK_CONST_METHOD0(get_total_item_count, std::uint64_t());
MOCK_CONST_METHOD0(get_used_drive_space, std::uint64_t());
MOCK_CONST_METHOD1(is_directory, bool(const std::string &api_path));
MOCK_CONST_METHOD1(is_file, bool(const std::string &api_path));
bool is_file_writeable(const std::string &api_path) const override { return true; }
MOCK_CONST_METHOD0(IsOnline, bool());
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_METHOD5(read_file_bytes, api_error(const std::string &path, const std::size_t &size,
const std::uint64_t &offset, std::vector<char> &data,
const bool &stop_requested));
MOCK_METHOD1(remove_directory, api_error(const std::string &api_path));
MOCK_METHOD1(remove_file, api_error(const std::string &api_path));
MOCK_METHOD2(remove_item_meta, api_error(const std::string &api_path, const std::string &key));
MOCK_METHOD2(rename_file,
api_error(const std::string &from_api_path, const std::string &to_api_path));
MOCK_METHOD3(set_item_meta, api_error(const std::string &api_path, const std::string &key,
const std::string &value));
MOCK_METHOD2(set_item_meta, api_error(const std::string &api_path, const api_meta_map &meta));
MOCK_METHOD2(set_source_path,
api_error(const std::string &api_path, const std::string &source_path));
MOCK_METHOD2(start, bool(api_item_added_callback api_item_added, i_open_file_table *oft));
MOCK_METHOD0(stop, void());
MOCK_METHOD3(upload_file, api_error(const std::string &api_path, const std::string &source_path,
const std::string &encryption_token));
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,
const std::string &encryption_token, stop_type &stop_requested),
(override));
};
} // namespace repertory
#endif // TESTS_MOCKS_MOCKPROVIDER_HPP_
#endif // TESTS_MOCKS_MOCK_PROVIDER_HPP_

View File

@ -1,79 +1,185 @@
/*
Copyright <2018-2022> <scott.e.graves@protonmail.com>
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
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 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.
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_S3_COMM_HPP_
#define TESTS_MOCKS_MOCK_S3_COMM_HPP_
#ifdef REPERTORY_ENABLE_S3_TESTING
#if defined(REPERTORY_ENABLE_S3) && defined(REPERTORY_ENABLE_S3_TESTING)
#include "test_common.hpp"
#include "comm/i_s3_comm.hpp"
#include "types/repertory.hpp"
namespace repertory {
/* class mock_s3_comm final : public i_s3_comm { */
/* private: */
/* S3Config s3_config_; */
/* */
/* public: */
/* MOCK_METHOD1(create_bucket, api_error(const std::string &api_path)); */
/* MOCK_CONST_METHOD1(Exists, bool(const std::string &api_path)); */
/* */
/* MOCK_CONST_METHOD3(get_bucket_name_and_object_name, */
/* void(const std::string &api_path, std::string &bucketName, */
/* std::string &objectName)); */
/* */
/* MOCK_CONST_METHOD2(get_directory_item_count, */
/* std::size_t(const std::string &api_path, */
/* const MetaProviderCallback &metaProviderCallback)); */
/* */
/* MOCK_CONST_METHOD3(get_directory_items, */
/* api_error(const std::string &api_path, */
/* const MetaProviderCallback &metaProviderCallback, */
/* directory_item_list &list)); */
/* */
/* S3Config GetS3Config() override { return s3Config_; } */
/* */
/* S3Config GetS3Config() const override { return s3Config_; } */
/* */
/* MOCK_CONST_METHOD2(GetFile, api_error(const std::string &api_path, ApiFile &apiFile)); */
/* */
/* MOCK_CONST_METHOD1(get_file_list, api_error(ApiFileList &apiFileList)); */
/* */
/* bool IsOnline() const override { return true; } */
/* */
/* MOCK_CONST_METHOD5(read_file_bytes, */
/* api_error(const std::string &path, const std::size_t &size, */
/* const std::uint64_t &offset, std::vector<char> &data, */
/* const bool &stop_requested)); */
/* */
/* MOCK_METHOD1(remove_bucket, api_error(const std::string &api_path)); */
/* */
/* MOCK_METHOD1(RemoveFile, api_error(const std::string &api_path)); */
/* */
/* MOCK_METHOD2(RenameFile, */
/* api_error(const std::string &api_path, const std::string &newApiFilePath));
*/
/* */
/* void SetS3Config(S3Config s3Config) { s3Config_ = std::move(s3Config); } */
/* */
/* MOCK_METHOD3(upload_file, api_error(const std::string &api_path, */
/* const std::string &sourcePath, const bool
* &stop_requested)); */
/* }; */
class mock_s3_comm final : public i_s3_comm {
public:
mock_s3_comm(s3_config cfg) : s3_config_(cfg) {}
private:
s3_config s3_config_;
public:
// [[nodiscard]] virtual api_error create_directory(const std::string
// &api_path) = 0;
//
MOCK_METHOD(api_error, create_directory, (const std::string &api_path),
(override));
// [[nodiscard]] virtual api_error directory_exists(const std::string
// &api_path) const = 0;
//
MOCK_METHOD(api_error, directory_exists, (const std::string &api_path),
(const, override));
// [[nodiscard]] virtual api_error file_exists(const std::string &api_path,
// const get_key_callback
// &get_key) const = 0;
MOCK_METHOD(api_error, file_exists,
(const std::string &api_path, const get_key_callback &get_key),
(const, override));
// [[nodiscard]] virtual std::size_t
// get_directory_item_count(const std::string &api_path,
// meta_provider_callback meta_provider) const
// = 0;
//
MOCK_METHOD(std::size_t, get_directory_item_count,
(const std::string &api_path,
meta_provider_callback meta_provider),
(const, override));
// [[nodiscard]] virtual api_error get_directory_items(const std::string
// &api_path,
// const
// meta_provider_callback
// &meta_provider,
// directory_item_list
// &list) const = 0;
//
MOCK_METHOD(api_error, get_directory_items,
(const std::string &api_path,
meta_provider_callback meta_provider,
directory_item_list &list),
(const, override));
// [[nodiscard]] virtual api_error
// get_directory_list(api_file_list &list) const = 0;
//
MOCK_METHOD(api_error, get_directory_list, (api_file_list & list),
(const, override));
// [[nodiscard]] virtual api_error get_file(const std::string &api_path,
// const get_key_callback &get_key,
// const get_name_callback &get_name,
// const get_token_callback
// &get_token, api_file &file) const
// = 0;
//
MOCK_METHOD(api_error, get_file,
(const std::string &api_path, const get_key_callback &get_key,
const get_name_callback &get_name,
const get_token_callback &get_token, api_file &file),
(const, override));
// [[nodiscard]] virtual api_error
// get_file_list(const get_api_file_token_callback &get_api_file_token,
// const get_name_callback &get_name, api_file_list &list) const
// = 0;
//
MOCK_METHOD(api_error, get_file_list,
(const get_api_file_token_callback &get_api_file_token,
const get_name_callback &get_name, api_file_list &list),
(const, override));
// [[nodiscard]] virtual api_error get_object_list(std::vector<directory_item>
// &list) const = 0;
//
MOCK_METHOD(api_error, get_object_list, (std::vector<directory_item> & list),
(const, override));
// virtual std::string get_object_name(const std::string &api_path,
// const get_key_callback &get_key) const
// = 0;
//
MOCK_METHOD(std::string, get_object_name,
(const std::string &api_path, const get_key_callback &get_key),
(const, override));
[[nodiscard]] s3_config get_s3_config() override { return s3_config_; }
[[nodiscard]] s3_config get_s3_config() const override { return s3_config_; }
// [[nodiscard]] virtual bool is_online() const = 0;
//
MOCK_METHOD(bool, is_online, (), (const, override));
// [[nodiscard]] virtual api_error
// read_file_bytes(const std::string &api_path, std::size_t size, const
// std::uint64_t &offset,
// data_buffer &data, const get_key_callback &get_key,
// const get_size_callback &get_size, const get_token_callback
// &get_token, stop_type &stop_requested) const = 0;
//
MOCK_METHOD(api_error, read_file_bytes,
(const std::string &api_path, std::size_t size,
std::uint64_t offset, data_buffer &data,
const get_key_callback &get_key,
const get_size_callback &get_size,
const get_token_callback &get_token, stop_type &stop_requested),
(const, override));
// [[nodiscard]] virtual api_error remove_directory(const std::string
// &api_path) = 0;
//
MOCK_METHOD(api_error, remove_directory, (const std::string &api_path),
(override));
// [[nodiscard]] virtual api_error remove_file(const std::string &api_path,
// const get_key_callback
// &get_key) = 0;
//
MOCK_METHOD(api_error, remove_file,
(const std::string &api_path, const get_key_callback &get_key),
(override));
// [[nodiscard]] virtual api_error rename_file(const std::string &api_path,
// const std::string
// &new_api_path) = 0;
//
MOCK_METHOD(api_error, rename_file,
(const std::string &api_path, const std::string &new_api_path),
(override));
// [[nodiscard]] virtual api_error
// upload_file(const std::string &api_path, const std::string &source_path,
// const std::string &encryption_token, const get_key_callback
// &get_key, const set_key_callback &set_key, stop_type
// &stop_requested) = 0;
MOCK_METHOD(api_error, upload_file,
(const std::string &api_path, const std::string &source_path,
const std::string &encryption_token,
const get_key_callback &get_key, const set_key_callback &set_key,
stop_type &stop_requested),
(override));
};
} // namespace repertory
#endif // REPERTORY_ENABLE_S3_TESTING

View File

@ -0,0 +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_

View File

@ -1,26 +1,30 @@
/*
Copyright <2018-2022> <scott.e.graves@protonmail.com>
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
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 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.
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 "common.hpp"
#include "test_common.hpp"
#include "drives/winfsp/i_winfsp_drive.hpp"
#include "utils/file_utils.hpp"
#include "utils/path_utils.hpp"
@ -29,15 +33,21 @@
namespace repertory {
class mock_winfsp_drive final : public virtual i_winfsp_drive {
public:
explicit mock_winfsp_drive(const std::string &mount_location) : mount_location_(mount_location) {}
explicit mock_winfsp_drive(std::string mount_location)
: mount_location_(std::move(mount_location)) {}
private:
const std::string mount_location_;
public:
std::uint64_t get_directory_item_count(const std::string &api_path) const override { return 1; }
[[nodiscard]] auto
get_directory_item_count(const std::string & /*api_path*/) const
-> std::uint64_t override {
return 1;
}
directory_item_list get_directory_items(const std::string &api_path) const override {
[[nodiscard]] auto get_directory_items(const std::string & /*api_path*/) const
-> directory_item_list override {
directory_item_list list{};
directory_item di{};
@ -57,20 +67,26 @@ public:
return list;
}
std::uint64_t get_file_size(const std::string &api_path) const override { return 0; }
[[nodiscard]] auto get_file_size(const std::string & /*api_path*/) const
-> std::uint64_t override {
return 0;
}
api_error get_item_meta(const std::string &api_path, api_meta_map &meta) const override {
auto get_item_meta(const std::string & /*api_path*/, api_meta_map &meta) const
-> api_error override {
return api_error::error;
}
api_error get_item_meta(const std::string &api_path, const std::string &name,
std::string &value) const override {
auto get_item_meta(const std::string & /*api_path*/,
const std::string & /*name*/,
std::string & /*value*/) const -> api_error override {
return api_error::error;
}
NTSTATUS get_security_by_name(PWSTR file_name, PUINT32 attributes,
PSECURITY_DESCRIPTOR descriptor,
std::uint64_t *descriptor_size) override {
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) {
@ -81,7 +97,8 @@ public:
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)) {
"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 {
@ -97,11 +114,17 @@ public:
return ret;
}
std::uint64_t get_total_drive_space() const override { return 100 * 1024 * 1024; }
[[nodiscard]] auto get_total_drive_space() const -> std::uint64_t override {
return 100 * 1024 * 1024;
}
std::uint64_t get_total_item_count() const override { return 0; }
[[nodiscard]] auto get_total_item_count() const -> std::uint64_t override {
return 0;
}
std::uint64_t get_used_drive_space() const 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 {
@ -110,14 +133,16 @@ public:
volume_label = "TestVolumeLabel";
}
api_error populate_file_info(const std::string &api_path, remote::file_info &file_info) override {
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);
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);
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) {
@ -125,7 +150,8 @@ public:
}
file_info.AllocationSize =
directory ? 0
: utils::divide_with_ceiling(file_info.FileSize, WINFSP_ALLOCATION_UNIT) *
: utils::divide_with_ceiling(file_info.FileSize,
WINFSP_ALLOCATION_UNIT) *
WINFSP_ALLOCATION_UNIT;
file_info.FileAttributes = fi.FileAttributes;
file_info.ChangeTime = fi.ChangeTime.QuadPart;

View File

@ -1,22 +1,26 @@
/*
Copyright <2018-2022> <scott.e.graves@protonmail.com>
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
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 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.
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 {
@ -26,12 +30,12 @@ TEST(packet, encrypt_and_decrypt) {
packet.encrypt("moose");
std::uint32_t size{};
packet.decode(size);
EXPECT_EQ(0, packet.decode(size));
EXPECT_EQ(0, packet.decrypt("moose"));
std::string data;
packet.decode(data);
EXPECT_EQ(0, packet.decode(data));
EXPECT_STREQ("test", &data[0]);
EXPECT_STREQ("test", data.c_str());
}
} // namespace repertory

View File

@ -1,22 +1,26 @@
/*
Copyright <2018-2022> <scott.e.graves@protonmail.com>
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
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 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.
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 {
@ -67,10 +71,10 @@ TEST(path_utils, create_api_path) {
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());
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());
EXPECT_STREQ("/cow/moose/dog/chicken", s.c_str());
}
TEST(path_utils, finalize) {

652
tests/providers_test.cpp Normal file
View File

@ -0,0 +1,652 @@
/*
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 "comm/s3/s3_comm.hpp"
#include "events/event_system.hpp"
#include "file_manager/file_manager.hpp"
#include "platform/platform.hpp"
#include "providers/encrypt/encrypt_provider.hpp"
#include "providers/i_provider.hpp"
#include "providers/s3/s3_provider.hpp"
#include "providers/sia/sia_provider.hpp"
#include "utils/file_utils.hpp"
#include "utils/path_utils.hpp"
#include "utils/string_utils.hpp"
#include "utils/utils.hpp"
#include "gtest/gtest.h"
namespace {
#ifdef _WIN32
static constexpr auto getgid() -> std::uint32_t { return 0U; }
static constexpr auto getuid() -> std::uint32_t { return 0U; }
#endif
const auto check_forced_dirs = [](const repertory::directory_item_list &list) {
static auto forced_dirs = std::array<std::string, 2>{".", ".."};
for (std::size_t i = 0U; i < forced_dirs.size(); ++i) {
const auto &di = list.at(i);
EXPECT_TRUE(di.directory);
EXPECT_STREQ(forced_dirs.at(i).c_str(), di.api_path.c_str());
EXPECT_STREQ("", di.api_parent.c_str());
EXPECT_EQ(std::size_t(0U), di.size);
}
};
const auto create_directory = [](repertory::i_provider &provider,
const std::string &api_path) {
auto date = repertory::utils::get_file_time_now();
auto meta = repertory::create_meta_attributes(
date, 0U, date + 1U, date + 2U, true, "", getgid(), "", 0700, date + 3U,
1U, 2U, 0U, api_path + "_src", getuid(), date + 4U);
EXPECT_EQ(repertory::api_error::success,
provider.create_directory(api_path, meta));
bool exists{};
EXPECT_EQ(repertory::api_error::success,
provider.is_directory(api_path, exists));
EXPECT_TRUE(exists);
};
const auto create_file = [](repertory::i_provider &provider,
const std::string &api_path) {
auto source_path = repertory::generate_test_file_name(".", "test");
auto date = repertory::utils::get_file_time_now();
auto meta = repertory::create_meta_attributes(
date, 0U, date + 1U, date + 2U, false, "", getgid(), "", 0700, date + 3U,
1U, 2U, 0U, source_path, getuid(), date + 4U);
EXPECT_EQ(repertory::api_error::success,
provider.create_file(api_path, meta));
bool exists{};
EXPECT_EQ(repertory::api_error::success, provider.is_file(api_path, exists));
EXPECT_TRUE(exists);
EXPECT_TRUE(repertory::utils::file::delete_file(source_path));
};
const auto decrypt_parts = [](const repertory::app_config &cfg,
std::string &path) {
if (path != "/" && path != "." && path != "..") {
auto parts = repertory::utils::string::split(path, '/', false);
for (auto &part : parts) {
if (part.empty()) {
continue;
}
EXPECT_EQ(repertory::api_error::success,
repertory::utils::encryption::decrypt_file_name(
cfg.get_encrypt_config().encryption_token, part));
}
path = repertory::utils::string::join(parts, '/');
}
};
} // namespace
namespace repertory {
static void can_create_and_remove_directory(i_provider &provider) {
if (provider.is_direct_only()) {
api_meta_map meta{};
EXPECT_EQ(api_error::not_implemented,
provider.create_directory("/moose", meta));
EXPECT_EQ(api_error::not_implemented, provider.remove_directory("/moose"));
return;
}
create_directory(provider, "/pt01");
EXPECT_EQ(api_error::success, provider.remove_directory("/pt01"));
bool exists{};
EXPECT_EQ(api_error::success, provider.is_directory("/pt01", exists));
EXPECT_FALSE(exists);
}
static void create_directory_fails_if_already_exists(i_provider &provider) {
if (provider.is_direct_only()) {
return;
}
create_directory(provider, "/pt01");
api_meta_map meta{};
EXPECT_EQ(api_error::directory_exists,
provider.create_directory("/pt01", meta));
EXPECT_EQ(api_error::success, provider.remove_directory("/pt01"));
}
static void
create_directory_fails_if_file_already_exists(i_provider &provider) {
if (provider.is_direct_only()) {
return;
}
create_file(provider, "/pt01");
api_meta_map meta{};
EXPECT_EQ(api_error::item_exists, provider.create_directory("/pt01", meta));
EXPECT_EQ(api_error::success, provider.remove_file("/pt01"));
}
static void create_directory_clone_source_meta(i_provider &provider) {
if (provider.is_direct_only()) {
EXPECT_EQ(api_error::not_implemented,
provider.create_directory_clone_source_meta("/moose", "/moose"));
return;
}
create_directory(provider, "/clone");
api_meta_map meta_orig{};
EXPECT_EQ(api_error::success, provider.get_item_meta("/clone", meta_orig));
EXPECT_EQ(api_error::success,
provider.create_directory_clone_source_meta("/clone", "/clone2"));
api_meta_map meta_clone{};
EXPECT_EQ(api_error::success, provider.get_item_meta("/clone2", meta_clone));
EXPECT_EQ(meta_orig.size(), meta_clone.size());
for (const auto &kv : meta_orig) {
EXPECT_STREQ(kv.second.c_str(), meta_clone[kv.first].c_str());
}
EXPECT_EQ(api_error::success, provider.remove_directory("/clone"));
EXPECT_EQ(api_error::success, provider.remove_directory("/clone2"));
}
static void create_directory_clone_source_meta_fails_if_already_exists(
i_provider &provider) {
if (provider.is_direct_only()) {
return;
}
create_directory(provider, "/clone");
create_directory(provider, "/clone2");
EXPECT_EQ(api_error::directory_exists,
provider.create_directory_clone_source_meta("/clone", "/clone2"));
EXPECT_EQ(api_error::success, provider.remove_directory("/clone"));
EXPECT_EQ(api_error::success, provider.remove_directory("/clone2"));
}
static void create_directory_clone_source_meta_fails_if_directory_not_found(
i_provider &provider) {
if (provider.is_direct_only()) {
return;
}
EXPECT_EQ(api_error::directory_not_found,
provider.create_directory_clone_source_meta("/clone", "/clone2"));
}
static void create_directory_clone_source_meta_fails_if_file_already_exists(
i_provider &provider) {
if (provider.is_direct_only()) {
return;
}
create_directory(provider, "/clone");
create_file(provider, "/clone2");
EXPECT_EQ(api_error::item_exists,
provider.create_directory_clone_source_meta("/clone", "/clone2"));
EXPECT_EQ(api_error::success, provider.remove_directory("/clone"));
EXPECT_EQ(api_error::success, provider.remove_file("/clone2"));
}
static void can_create_and_remove_file(i_provider &provider) {
if (provider.is_direct_only()) {
api_meta_map meta{};
EXPECT_EQ(api_error::not_implemented,
provider.create_file("/moose.txt", meta));
return;
}
create_file(provider, "/pt01.txt");
bool exists{};
EXPECT_EQ(api_error::success, provider.is_file("/pt01.txt", exists));
EXPECT_TRUE(exists);
EXPECT_EQ(api_error::success, provider.remove_file("/pt01.txt"));
EXPECT_EQ(api_error::success, provider.is_file("/pt01.txt", exists));
EXPECT_FALSE(exists);
}
static void create_file_fails_if_already_exists(i_provider &provider) {
if (provider.is_direct_only()) {
return;
}
create_file(provider, "/pt01.txt");
api_meta_map meta{};
EXPECT_EQ(api_error::item_exists, provider.create_file("/pt01.txt", meta));
EXPECT_EQ(api_error::success, provider.remove_file("/pt01.txt"));
}
static void
create_file_fails_if_directory_already_exists(i_provider &provider) {
if (provider.is_direct_only()) {
return;
}
create_directory(provider, "/pt01");
api_meta_map meta{};
EXPECT_EQ(api_error::directory_exists, provider.create_file("/pt01", meta));
EXPECT_EQ(api_error::success, provider.remove_directory("/pt01"));
}
static void get_api_path_from_source(const app_config &cfg,
i_provider &provider) {
if (provider.get_provider_type() == provider_type::encrypt) {
const auto source_path =
utils::path::combine(cfg.get_encrypt_config().path, {"test.txt"});
std::string api_path{};
EXPECT_EQ(api_error::success,
provider.get_api_path_from_source(source_path, api_path));
std::string file_name{api_path.substr(1U)};
decrypt_parts(cfg, file_name);
EXPECT_STREQ("test.txt", file_name.c_str());
return;
}
create_file(provider, "/pt01.txt");
filesystem_item fsi{};
EXPECT_EQ(api_error::success,
provider.get_filesystem_item("/pt01.txt", false, fsi));
std::string api_path{};
EXPECT_EQ(api_error::success,
provider.get_api_path_from_source(fsi.source_path, api_path));
EXPECT_STREQ("/pt01.txt", api_path.c_str());
}
static void
get_api_path_from_source_fails_if_file_not_found(const app_config &cfg,
i_provider &provider) {
std::string source_path{};
if (provider.get_provider_type() == provider_type::encrypt) {
source_path = utils::path::combine(cfg.get_encrypt_config().path,
{"test_not_found.txt"});
} else {
source_path = utils::path::combine("./", {"test_not_found.txt"});
}
std::string api_path{};
EXPECT_EQ(api_error::item_not_found,
provider.get_api_path_from_source(source_path, api_path));
EXPECT_TRUE(api_path.empty());
}
static void get_directory_item_count(const app_config &cfg,
i_provider &provider) {
if (provider.get_provider_type() == provider_type::encrypt) {
EXPECT_EQ(std::size_t(2U), provider.get_directory_item_count("/"));
EXPECT_EQ(std::size_t(0U), provider.get_directory_item_count("/not_found"));
const auto source_path =
utils::path::combine(cfg.get_encrypt_config().path, {"sub10"});
std::string api_path{};
EXPECT_EQ(api_error::success,
provider.get_api_path_from_source(source_path, api_path));
EXPECT_EQ(std::size_t(1U), provider.get_directory_item_count(api_path));
}
}
static void get_directory_items(const app_config &cfg, i_provider &provider) {
directory_item_list list{};
EXPECT_EQ(api_error::success, provider.get_directory_items("/", list));
check_forced_dirs(list);
if (provider.get_provider_type() == provider_type::encrypt) {
EXPECT_EQ(std::size_t(4U), list.size());
directory_item_list list_decrypted{list.begin() + 2U, list.end()};
for (auto &di : list_decrypted) {
decrypt_parts(cfg, di.api_parent);
decrypt_parts(cfg, di.api_path);
}
auto dir = std::find_if(
list_decrypted.begin(), list_decrypted.end(),
[](const directory_item &di) -> bool { return di.directory; });
EXPECT_LT(dir, list_decrypted.end());
EXPECT_STREQ("/sub10", dir->api_path.c_str());
EXPECT_STREQ("/", dir->api_parent.c_str());
EXPECT_EQ(std::size_t(0U), dir->size);
auto file = std::find_if(
list_decrypted.begin(), list_decrypted.end(),
[](const directory_item &di) -> bool { return not di.directory; });
EXPECT_LT(file, list_decrypted.end());
EXPECT_STREQ("/test.txt", file->api_path.c_str());
EXPECT_STREQ("/", file->api_parent.c_str());
#ifdef _WIN32
EXPECT_EQ(std::size_t(47U), file->size);
#else
EXPECT_EQ(std::size_t(46U), file->size);
#endif
const auto source_path =
utils::path::combine(cfg.get_encrypt_config().path, {"sub10"});
std::string api_path{};
EXPECT_EQ(api_error::success,
provider.get_api_path_from_source(source_path, api_path));
list.clear();
EXPECT_EQ(api_error::success, provider.get_directory_items(api_path, list));
check_forced_dirs(list);
EXPECT_EQ(std::size_t(3U), list.size());
directory_item_list list_decrypted2{list.begin() + 2U, list.end()};
for (auto &di : list_decrypted2) {
decrypt_parts(cfg, di.api_parent);
decrypt_parts(cfg, di.api_path);
}
auto file2 = std::find_if(
list_decrypted2.begin(), list_decrypted2.end(),
[](const directory_item &di) -> bool { return not di.directory; });
EXPECT_LT(file2, list_decrypted2.end());
EXPECT_STREQ("/sub10/moose.txt", file2->api_path.c_str());
EXPECT_STREQ("/sub10", file2->api_parent.c_str());
#ifdef _WIN32
EXPECT_EQ(std::size_t(46U), file2->size);
#else
EXPECT_EQ(std::size_t(45U), file2->size);
#endif
}
}
static void
get_directory_items_fails_if_directory_not_found(i_provider &provider) {
directory_item_list list{};
EXPECT_EQ(api_error::directory_not_found,
provider.get_directory_items("/not_found", list));
EXPECT_TRUE(list.empty());
}
static void get_directory_items_fails_if_item_is_file(const app_config &cfg,
i_provider &provider) {
if (provider.get_provider_type() == provider_type::encrypt) {
const auto source_path =
utils::path::combine(cfg.get_encrypt_config().path, {"test.txt"});
std::string api_path{};
EXPECT_EQ(api_error::success,
provider.get_api_path_from_source(source_path, api_path));
directory_item_list list{};
EXPECT_EQ(api_error::item_exists,
provider.get_directory_items(api_path, list));
EXPECT_TRUE(list.empty());
}
}
static void get_file(const app_config &cfg, i_provider &provider) {
if (provider.get_provider_type() == provider_type::encrypt) {
const auto source_path =
utils::path::combine(cfg.get_encrypt_config().path, {"test.txt"});
std::string api_path{};
EXPECT_EQ(api_error::success,
provider.get_api_path_from_source(source_path, api_path));
api_file file{};
EXPECT_EQ(api_error::success, provider.get_file(api_path, file));
decrypt_parts(cfg, file.api_path);
decrypt_parts(cfg, file.api_parent);
EXPECT_STREQ("/test.txt", file.api_path.c_str());
EXPECT_STREQ("/", file.api_parent.c_str());
#ifdef _WIN32
EXPECT_EQ(std::size_t(47U), file.file_size);
#else
EXPECT_EQ(std::size_t(46U), file.file_size);
#endif
EXPECT_TRUE(file.encryption_token.empty());
EXPECT_STREQ(source_path.c_str(), file.source_path.c_str());
}
}
static void get_file_fails_if_file_not_found(i_provider &provider) {
api_file file{};
EXPECT_EQ(api_error::item_not_found, provider.get_file("/not_found", file));
}
static void get_file_fails_if_item_is_directory(const app_config &cfg,
i_provider &provider) {
if (provider.get_provider_type() == provider_type::encrypt) {
const auto source_path =
utils::path::combine(cfg.get_encrypt_config().path, {"sub10"});
std::string api_path{};
EXPECT_EQ(api_error::success,
provider.get_api_path_from_source(source_path, api_path));
api_file file{};
EXPECT_EQ(api_error::directory_exists, provider.get_file(api_path, file));
}
}
static void get_file_list(const app_config &cfg, i_provider &provider) {
api_file_list list{};
EXPECT_EQ(api_error::success, provider.get_file_list(list));
if (provider.get_provider_type() == provider_type::encrypt) {
EXPECT_EQ(std::size_t(2U), list.size());
std::vector<std::string> expected_parents{
{"/"},
{"/sub10"},
};
std::vector<std::string> expected_paths{
{"/test.txt"},
{"/sub10/moose.txt"},
};
for (auto &file : list) {
decrypt_parts(cfg, file.api_parent);
decrypt_parts(cfg, file.api_path);
utils::remove_element_from(expected_parents, file.api_parent);
utils::remove_element_from(expected_paths, file.api_path);
}
EXPECT_TRUE(expected_parents.empty());
EXPECT_TRUE(expected_paths.empty());
}
}
static void run_tests(const app_config &cfg, i_provider &provider) {
get_file_list(cfg, provider);
ASSERT_FALSE(::testing::Test::HasFailure());
can_create_and_remove_directory(provider);
can_create_and_remove_file(provider);
create_directory_fails_if_already_exists(provider);
create_directory_fails_if_file_already_exists(provider);
create_directory_clone_source_meta(provider);
create_directory_clone_source_meta_fails_if_already_exists(provider);
create_directory_clone_source_meta_fails_if_directory_not_found(provider);
create_directory_clone_source_meta_fails_if_file_already_exists(provider);
create_file_fails_if_already_exists(provider);
create_file_fails_if_directory_already_exists(provider);
get_api_path_from_source(cfg, provider);
get_api_path_from_source_fails_if_file_not_found(cfg, provider);
// TODO: continue here
get_directory_item_count(cfg, provider);
get_directory_items(cfg, provider);
get_directory_items_fails_if_directory_not_found(provider);
get_directory_items_fails_if_item_is_file(cfg, provider);
get_file(cfg, provider);
get_file_fails_if_file_not_found(provider);
get_file_fails_if_item_is_directory(cfg, provider);
}
TEST(providers, encrypt_provider) {
const auto config_path = utils::path::absolute("./providers_test_encrypt");
ASSERT_TRUE(utils::file::delete_directory_recursively(config_path));
console_consumer cc{};
event_system::instance().start();
{
app_config cfg(provider_type::encrypt, config_path);
const auto encrypt_path = utils::path::combine(
std::filesystem::path(utils::path::absolute(__FILE__))
.parent_path()
.string(),
{"encrypt"});
EXPECT_STREQ(
encrypt_path.c_str(),
cfg.set_value_by_name("EncryptConfig.Path", encrypt_path.c_str())
.c_str());
EXPECT_STREQ(
"test_token",
cfg.set_value_by_name("EncryptConfig.EncryptionToken", "test_token")
.c_str());
encrypt_provider provider{cfg};
file_manager fm(cfg, provider);
fm.start();
EXPECT_TRUE(provider.start(
[&provider](bool directory, api_file &file) -> api_error {
return provider_meta_handler(provider, directory, file);
},
&fm));
EXPECT_EQ(provider_type::encrypt, provider.get_provider_type());
EXPECT_TRUE(provider.is_direct_only());
EXPECT_TRUE(provider.is_online());
EXPECT_FALSE(provider.is_rename_supported());
run_tests(cfg, provider);
provider.stop();
fm.stop();
}
event_system::instance().stop();
ASSERT_TRUE(utils::file::delete_directory_recursively(config_path));
}
#if defined(REPERTORY_ENABLE_S3) && defined(REPERTORY_ENABLE_S3_TESTING)
TEST(providers, s3_provider) {
const auto config_path = utils::path::absolute("./providers_test_s3");
ASSERT_TRUE(utils::file::delete_directory_recursively(config_path));
console_consumer cc{};
event_system::instance().start();
{
app_config cfg(provider_type::s3, config_path);
{
app_config src_cfg(provider_type::s3,
utils::path::combine(get_test_dir(), {"filebase"}));
cfg.set_s3_config(src_cfg.get_s3_config());
}
s3_comm comm{cfg};
s3_provider provider{cfg, comm};
file_manager fm(cfg, provider);
fm.start();
EXPECT_TRUE(provider.start(
[&provider](bool directory, api_file &file) -> api_error {
return provider_meta_handler(provider, directory, file);
},
&fm));
EXPECT_EQ(provider_type::s3, provider.get_provider_type());
EXPECT_FALSE(provider.is_direct_only());
EXPECT_TRUE(provider.is_online());
EXPECT_FALSE(provider.is_rename_supported());
run_tests(cfg, provider);
provider.stop();
fm.stop();
}
event_system::instance().stop();
ASSERT_TRUE(utils::file::delete_directory_recursively(config_path));
}
#endif
TEST(providers, sia_provider) {
const auto config_path = utils::path::absolute("./providers_test_sia");
ASSERT_TRUE(utils::file::delete_directory_recursively(config_path));
console_consumer cc{};
event_system::instance().start();
{
app_config cfg(provider_type::sia, config_path);
{
app_config src_cfg(provider_type::sia,
utils::path::combine(get_test_dir(), {"sia"}));
cfg.set_host_config(src_cfg.get_host_config());
}
curl_comm comm{cfg.get_host_config()};
sia_provider provider{cfg, comm};
file_manager fm(cfg, provider);
fm.start();
EXPECT_TRUE(provider.start(
[&provider](bool directory, api_file &file) -> api_error {
return provider_meta_handler(provider, directory, file);
},
&fm));
EXPECT_EQ(provider_type::sia, provider.get_provider_type());
EXPECT_FALSE(provider.is_direct_only());
EXPECT_TRUE(provider.is_online());
EXPECT_FALSE(provider.is_rename_supported());
run_tests(cfg, provider);
provider.stop();
fm.stop();
}
event_system::instance().stop();
ASSERT_TRUE(utils::file::delete_directory_recursively(config_path));
}
} // namespace repertory

File diff suppressed because it is too large Load Diff

View File

@ -1,25 +1,29 @@
/*
Copyright <2018-2022> <scott.e.graves@protonmail.com>
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
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 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.
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 "drives/winfsp/i_winfsp_drive.hpp"
#include "drives/winfsp/remotewinfsp/remote_client.hpp"
#include "events/consumers/console_consumer.hpp"
#include "test_common.hpp"
#ifdef _WIN32
#include "drives/winfsp/remotewinfsp/remote_server.hpp"
#include "mocks/mock_winfsp_drive.hpp"
@ -27,6 +31,7 @@
#include "drives/fuse/remotefuse/remote_server.hpp"
#include "mocks/mock_fuse_drive.hpp"
#endif
#include "types/repertory.hpp"
#include "utils/utils.hpp"
using namespace repertory;
@ -37,138 +42,153 @@ static std::string mount_location_;
static void can_delete_test(remote_client &client) {
const auto test_file = utils::path::absolute("./win_remote/candelete.txt");
utils::file::delete_file(test_file);
auto api_path = utils::string::from_utf8(test_file).substr(mount_location_.size());
EXPECT_TRUE(utils::file::retry_delete_file(test_file));
auto api_path =
utils::string::from_utf8(test_file).substr(mount_location_.size());
native_file::native_file_ptr nf;
native_file::create_or_open(test_file, nf);
EXPECT_EQ(api_error::success, native_file::create_or_open(test_file, nf));
EXPECT_TRUE(nf);
if (nf) {
EXPECT_EQ(STATUS_INVALID_HANDLE,
client.winfsp_can_delete(reinterpret_cast<PVOID>(nf->get_handle()), &api_path[0]));
client.winfsp_can_delete(
reinterpret_cast<PVOID>(nf->get_handle()), &api_path[0]));
nf->close();
utils::file::delete_file(test_file);
EXPECT_TRUE(utils::file::retry_delete_file(test_file));
}
}
static void create_and_close_test(remote_client &client) {
template <typename t>
static void create_and_close_test(remote_client &client, t &server) {
const auto test_file = utils::path::absolute("./win_remote/create.txt");
utils::file::delete_file(test_file);
auto api_path = utils::string::from_utf8(test_file).substr(mount_location_.size());
EXPECT_TRUE(utils::file::retry_delete_file(test_file));
auto api_path =
utils::string::from_utf8(test_file).substr(mount_location_.size());
PVOID file_desc = reinterpret_cast<PVOID>(REPERTORY_INVALID_HANDLE);
remote::file_info fi{};
std::string normalized_name;
BOOLEAN exists = 0;
const auto ret =
client.winfsp_create(&api_path[0], 0, GENERIC_READ | GENERIC_WRITE, FILE_ATTRIBUTE_NORMAL, 0,
&file_desc, &fi, normalized_name, exists);
BOOLEAN exists = 0u;
const auto ret = client.winfsp_create(
&api_path[0], 0, GENERIC_READ | GENERIC_WRITE, FILE_ATTRIBUTE_NORMAL, 0,
&file_desc, &fi, normalized_name, exists);
EXPECT_EQ(STATUS_SUCCESS, ret);
EXPECT_EQ(1u, client.get_open_file_count(utils::string::to_utf8(api_path)));
EXPECT_EQ(1u, server.get_open_file_count(test_file));
EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc));
EXPECT_EQ(0u, client.get_open_file_count(utils::string::to_utf8(api_path)));
EXPECT_EQ(0u, server.get_open_file_count(test_file));
utils::file::delete_file(test_file);
EXPECT_TRUE(utils::file::retry_delete_file(test_file));
}
static void cleanup_test(remote_client &client) {
const auto test_file = utils::path::absolute("./win_remote/cleanup.txt");
utils::file::delete_file(test_file);
auto api_path = utils::string::from_utf8(test_file).substr(mount_location_.size());
EXPECT_TRUE(utils::file::retry_delete_file(test_file));
auto api_path =
utils::string::from_utf8(test_file).substr(mount_location_.size());
PVOID file_desc = reinterpret_cast<PVOID>(REPERTORY_INVALID_HANDLE);
remote::file_info fi{};
std::string normalized_name;
BOOLEAN exists = 0;
auto ret =
client.winfsp_create(&api_path[0], 0, GENERIC_READ | GENERIC_WRITE, FILE_ATTRIBUTE_NORMAL, 0,
&file_desc, &fi, normalized_name, exists);
BOOLEAN exists = 0u;
auto ret = client.winfsp_create(&api_path[0], 0, GENERIC_READ | GENERIC_WRITE,
FILE_ATTRIBUTE_NORMAL, 0, &file_desc, &fi,
normalized_name, exists);
EXPECT_EQ(STATUS_SUCCESS, ret);
BOOLEAN was_closed = 0;
EXPECT_EQ(STATUS_SUCCESS, client.winfsp_cleanup(file_desc, &api_path[0], 0, was_closed));
BOOLEAN was_closed = 0u;
EXPECT_EQ(STATUS_SUCCESS,
client.winfsp_cleanup(file_desc, &api_path[0], 0, was_closed));
EXPECT_FALSE(was_closed);
EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc));
utils::file::delete_file(test_file);
EXPECT_TRUE(utils::file::retry_delete_file(test_file));
}
static void flush_test(remote_client &client) {
const auto test_file = utils::path::absolute("./win_remote/flush.txt");
utils::file::delete_file(test_file);
auto api_path = utils::string::from_utf8(test_file).substr(mount_location_.size());
EXPECT_TRUE(utils::file::retry_delete_file(test_file));
auto api_path =
utils::string::from_utf8(test_file).substr(mount_location_.size());
PVOID file_desc = reinterpret_cast<PVOID>(REPERTORY_INVALID_HANDLE);
remote::file_info fi{};
std::string normalized_name;
BOOLEAN exists = 0;
auto ret =
client.winfsp_create(&api_path[0], 0, GENERIC_READ | GENERIC_WRITE, FILE_ATTRIBUTE_NORMAL, 0,
&file_desc, &fi, normalized_name, exists);
BOOLEAN exists = 0u;
auto ret = client.winfsp_create(&api_path[0], 0, GENERIC_READ | GENERIC_WRITE,
FILE_ATTRIBUTE_NORMAL, 0, &file_desc, &fi,
normalized_name, exists);
EXPECT_EQ(STATUS_SUCCESS, ret);
EXPECT_EQ(STATUS_SUCCESS, client.winfsp_flush(file_desc, &fi));
EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc));
utils::file::delete_file(test_file);
EXPECT_TRUE(utils::file::retry_delete_file(test_file));
}
static void get_file_info_test(remote_client &client) {
const auto test_file = utils::path::absolute("./win_remote/getfileinfo.txt");
utils::file::delete_file(test_file);
auto api_path = utils::string::from_utf8(test_file).substr(mount_location_.size());
EXPECT_TRUE(utils::file::retry_delete_file(test_file));
auto api_path =
utils::string::from_utf8(test_file).substr(mount_location_.size());
PVOID file_desc = reinterpret_cast<PVOID>(REPERTORY_INVALID_HANDLE);
remote::file_info fi{};
std::string normalized_name;
BOOLEAN exists = 0;
auto ret =
client.winfsp_create(&api_path[0], 0, GENERIC_READ | GENERIC_WRITE, FILE_ATTRIBUTE_NORMAL, 0,
&file_desc, &fi, normalized_name, exists);
BOOLEAN exists = 0u;
auto ret = client.winfsp_create(&api_path[0], 0, GENERIC_READ | GENERIC_WRITE,
FILE_ATTRIBUTE_NORMAL, 0, &file_desc, &fi,
normalized_name, exists);
EXPECT_EQ(STATUS_SUCCESS, ret);
EXPECT_EQ(STATUS_SUCCESS, client.winfsp_get_file_info(file_desc, &fi));
EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc));
utils::file::delete_file(test_file);
EXPECT_TRUE(utils::file::retry_delete_file(test_file));
}
static void get_security_by_name_test(remote_client &client) {
const auto test_file = utils::path::absolute("./win_remote/getsecuritybyname.txt");
utils::file::delete_file(test_file);
auto api_path = utils::string::from_utf8(test_file).substr(mount_location_.size());
const auto test_file =
utils::path::absolute("./win_remote/getsecuritybyname.txt");
EXPECT_TRUE(utils::file::retry_delete_file(test_file));
auto api_path =
utils::string::from_utf8(test_file).substr(mount_location_.size());
PVOID file_desc = reinterpret_cast<PVOID>(REPERTORY_INVALID_HANDLE);
remote::file_info fi{};
std::string normalized_name;
BOOLEAN exists = 0;
auto ret =
client.winfsp_create(&api_path[0], 0, GENERIC_READ | GENERIC_WRITE, FILE_ATTRIBUTE_NORMAL, 0,
&file_desc, &fi, normalized_name, exists);
BOOLEAN exists = 0u;
auto ret = client.winfsp_create(&api_path[0], 0, GENERIC_READ | GENERIC_WRITE,
FILE_ATTRIBUTE_NORMAL, 0, &file_desc, &fi,
normalized_name, exists);
EXPECT_EQ(STATUS_SUCCESS, ret);
EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc));
UINT32 attributes = 0;
UINT32 attributes = 0u;
std::uint64_t securityDescriptorSize = 1024;
std::wstring strDescriptor;
ret = client.winfsp_get_security_by_name(&api_path[0], &attributes, &securityDescriptorSize,
strDescriptor);
ret = client.winfsp_get_security_by_name(
&api_path[0], &attributes, &securityDescriptorSize, strDescriptor);
EXPECT_EQ(STATUS_SUCCESS, ret);
EXPECT_EQ(FILE_ATTRIBUTE_NORMAL, attributes);
EXPECT_EQ(static_cast<UINT32>(FILE_ATTRIBUTE_NORMAL), attributes);
EXPECT_FALSE(strDescriptor.empty());
utils::file::delete_file(test_file);
EXPECT_TRUE(utils::file::retry_delete_file(test_file));
}
static void get_volume_info_test(remote_client &client) {
UINT64 total_size = 0u;
UINT64 free_size = 0u;
std::string volume_label;
EXPECT_EQ(STATUS_SUCCESS, client.winfsp_get_volume_info(total_size, free_size, volume_label));
EXPECT_EQ(100, free_size);
EXPECT_EQ(200, total_size);
EXPECT_STREQ(&volume_label[0], "TestVolumeLabel");
EXPECT_EQ(STATUS_SUCCESS,
client.winfsp_get_volume_info(total_size, free_size, volume_label));
EXPECT_EQ(100u, free_size);
EXPECT_EQ(200u, total_size);
EXPECT_STREQ(volume_label.c_str(), "TestVolumeLabel");
}
static void mounted_test(remote_client &client) {
@ -178,73 +198,79 @@ static void mounted_test(remote_client &client) {
static void open_test(remote_client &client) {
const auto test_file = utils::path::absolute("./win_remote/open.txt");
utils::file::delete_file(test_file);
auto api_path = utils::string::from_utf8(test_file).substr(mount_location_.size());
EXPECT_TRUE(utils::file::retry_delete_file(test_file));
auto api_path =
utils::string::from_utf8(test_file).substr(mount_location_.size());
PVOID file_desc = reinterpret_cast<PVOID>(REPERTORY_INVALID_HANDLE);
PVOID file_desc2 = reinterpret_cast<PVOID>(REPERTORY_INVALID_HANDLE);
{
remote::file_info fi{};
std::string normalized_name;
BOOLEAN exists = 0;
auto ret =
client.winfsp_create(&api_path[0], 0, GENERIC_READ | GENERIC_WRITE, FILE_ATTRIBUTE_NORMAL,
0, &file_desc, &fi, normalized_name, exists);
BOOLEAN exists = 0u;
auto ret = client.winfsp_create(
&api_path[0], 0, GENERIC_READ | GENERIC_WRITE, FILE_ATTRIBUTE_NORMAL, 0,
&file_desc, &fi, normalized_name, exists);
EXPECT_EQ(STATUS_SUCCESS, ret);
}
{
remote::file_info fi{};
std::string normalized_name;
const auto ret = client.winfsp_open(&api_path[0], 0, GENERIC_READ | GENERIC_WRITE, &file_desc2,
&fi, normalized_name);
const auto ret =
client.winfsp_open(&api_path[0], 0, GENERIC_READ | GENERIC_WRITE,
&file_desc2, &fi, normalized_name);
EXPECT_EQ(STATUS_SUCCESS, ret);
}
EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc));
EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc2));
utils::file::delete_file(test_file);
EXPECT_TRUE(utils::file::retry_delete_file(test_file));
}
static void overwrite_test(remote_client &client) {
const auto test_file = utils::path::absolute("./win_remote/overwrite.txt");
utils::file::delete_file(test_file);
auto api_path = utils::string::from_utf8(test_file).substr(mount_location_.size());
EXPECT_TRUE(utils::file::retry_delete_file(test_file));
auto api_path =
utils::string::from_utf8(test_file).substr(mount_location_.size());
PVOID file_desc = reinterpret_cast<PVOID>(REPERTORY_INVALID_HANDLE);
remote::file_info fi{};
std::string normalized_name;
BOOLEAN exists = 0;
auto ret =
client.winfsp_create(&api_path[0], 0, GENERIC_READ | GENERIC_WRITE, FILE_ATTRIBUTE_NORMAL, 0,
&file_desc, &fi, normalized_name, exists);
BOOLEAN exists = 0u;
auto ret = client.winfsp_create(&api_path[0], 0, GENERIC_READ | GENERIC_WRITE,
FILE_ATTRIBUTE_NORMAL, 0, &file_desc, &fi,
normalized_name, exists);
EXPECT_EQ(STATUS_SUCCESS, ret);
const UINT32 attributes = FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_ARCHIVE;
const BOOLEAN replace_attributes = 0;
const UINT64 allocation_size = 0;
ret = client.winfsp_overwrite(file_desc, attributes, replace_attributes, allocation_size, &fi);
const BOOLEAN replace_attributes = 0u;
const UINT64 allocation_size = 0u;
ret = client.winfsp_overwrite(file_desc, attributes, replace_attributes,
allocation_size, &fi);
EXPECT_EQ(STATUS_SUCCESS, ret);
EXPECT_EQ(0, fi.FileSize);
EXPECT_EQ(0u, fi.FileSize);
EXPECT_EQ(attributes, fi.FileAttributes);
EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc));
utils::file::delete_file(test_file);
EXPECT_TRUE(utils::file::retry_delete_file(test_file));
}
static void create_and_read_directory_test(remote_client &client) {
const auto test_directory = utils::path::absolute("./win_remote/readdirectory");
utils::file::delete_directory(test_directory);
auto api_path = utils::string::from_utf8(test_directory).substr(mount_location_.size());
const auto test_directory =
utils::path::absolute("./win_remote/readdirectory");
EXPECT_TRUE(utils::file::delete_directory(test_directory));
auto api_path =
utils::string::from_utf8(test_directory).substr(mount_location_.size());
PVOID file_desc = reinterpret_cast<PVOID>(REPERTORY_INVALID_HANDLE);
remote::file_info fi{};
std::string normalized_name;
BOOLEAN exists = 0;
auto ret =
client.winfsp_create(&api_path[0], FILE_DIRECTORY_FILE, GENERIC_READ | GENERIC_WRITE,
FILE_ATTRIBUTE_DIRECTORY, 0, &file_desc, &fi, normalized_name, exists);
BOOLEAN exists = 0u;
auto ret = client.winfsp_create(
&api_path[0], FILE_DIRECTORY_FILE, GENERIC_READ | GENERIC_WRITE,
FILE_ATTRIBUTE_DIRECTORY, 0, &file_desc, &fi, normalized_name, exists);
EXPECT_EQ(STATUS_SUCCESS, ret);
EXPECT_TRUE(utils::file::is_directory(test_directory));
@ -252,25 +278,27 @@ static void create_and_read_directory_test(remote_client &client) {
json list;
ret = client.winfsp_read_directory(file_desc, nullptr, nullptr, list);
EXPECT_EQ(STATUS_SUCCESS, ret);
EXPECT_EQ(2, list.size());
EXPECT_EQ(2u, list.size());
EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc));
utils::file::delete_directory(test_directory);
EXPECT_TRUE(utils::file::delete_directory(test_directory));
}
static void open_and_read_directory_test(remote_client &client) {
const auto test_directory = utils::path::absolute("./win_remote/openreaddirectory");
utils::file::delete_directory(test_directory);
auto api_path = utils::string::from_utf8(test_directory).substr(mount_location_.size());
const auto test_directory =
utils::path::absolute("./win_remote/openreaddirectory");
EXPECT_TRUE(utils::file::delete_directory(test_directory));
auto api_path =
utils::string::from_utf8(test_directory).substr(mount_location_.size());
PVOID file_desc = reinterpret_cast<PVOID>(REPERTORY_INVALID_HANDLE);
remote::file_info fi{};
std::string normalized_name;
BOOLEAN exists = 0;
auto ret =
client.winfsp_create(&api_path[0], FILE_DIRECTORY_FILE, GENERIC_READ | GENERIC_WRITE,
FILE_ATTRIBUTE_DIRECTORY, 0, &file_desc, &fi, normalized_name, exists);
BOOLEAN exists = 0u;
auto ret = client.winfsp_create(
&api_path[0], FILE_DIRECTORY_FILE, GENERIC_READ | GENERIC_WRITE,
FILE_ATTRIBUTE_DIRECTORY, 0, &file_desc, &fi, normalized_name, exists);
EXPECT_EQ(STATUS_SUCCESS, ret);
EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc));
@ -278,48 +306,52 @@ static void open_and_read_directory_test(remote_client &client) {
EXPECT_TRUE(utils::file::is_directory(test_directory));
file_desc = reinterpret_cast<PVOID>(REPERTORY_INVALID_HANDLE);
ret = client.winfsp_open(&api_path[0], FILE_DIRECTORY_FILE, GENERIC_READ | GENERIC_WRITE,
&file_desc, &fi, normalized_name);
ret = client.winfsp_open(&api_path[0], FILE_DIRECTORY_FILE,
GENERIC_READ | GENERIC_WRITE, &file_desc, &fi,
normalized_name);
EXPECT_EQ(STATUS_SUCCESS, ret);
json item_list;
ret = client.winfsp_read_directory(file_desc, nullptr, nullptr, item_list);
EXPECT_EQ(STATUS_SUCCESS, ret);
EXPECT_EQ(2, item_list.size());
EXPECT_EQ(2u, item_list.size());
EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc));
utils::file::delete_directory(test_directory);
EXPECT_TRUE(utils::file::delete_directory(test_directory));
}
static void read_and_write_test(remote_client &client) {
const auto test_file = utils::path::absolute("./win_remote/readwrite.txt");
utils::file::delete_file(test_file);
auto api_path = utils::string::from_utf8(test_file).substr(mount_location_.size());
EXPECT_TRUE(utils::file::retry_delete_file(test_file));
auto api_path =
utils::string::from_utf8(test_file).substr(mount_location_.size());
PVOID file_desc = reinterpret_cast<PVOID>(REPERTORY_INVALID_HANDLE);
remote::file_info fi{};
std::string normalized_name;
BOOLEAN exists = 0;
auto ret =
client.winfsp_create(&api_path[0], 0, GENERIC_READ | GENERIC_WRITE, FILE_ATTRIBUTE_NORMAL, 0,
&file_desc, &fi, normalized_name, exists);
BOOLEAN exists = 0u;
auto ret = client.winfsp_create(&api_path[0], 0, GENERIC_READ | GENERIC_WRITE,
FILE_ATTRIBUTE_NORMAL, 0, &file_desc, &fi,
normalized_name, exists);
EXPECT_EQ(STATUS_SUCCESS, ret);
std::vector<char> buffer;
data_buffer buffer;
buffer.emplace_back('T');
buffer.emplace_back('e');
buffer.emplace_back('s');
buffer.emplace_back('t');
UINT32 bytes_transferred = 0;
ret = client.winfsp_write(file_desc, &buffer[0], 0, static_cast<UINT32>(buffer.size()), 0, 0,
UINT32 bytes_transferred = 0u;
ret = client.winfsp_write(file_desc, &buffer[0], 0,
static_cast<UINT32>(buffer.size()), 0, 0,
&bytes_transferred, &fi);
EXPECT_EQ(STATUS_SUCCESS, ret);
EXPECT_EQ(buffer.size(), bytes_transferred);
std::vector<char> buffer2(buffer.size());
UINT32 bytes_transferred2 = 0;
ret = client.winfsp_read(file_desc, &buffer2[0], 0, static_cast<UINT32>(buffer2.size()),
data_buffer buffer2(buffer.size());
UINT32 bytes_transferred2 = 0u;
ret = client.winfsp_read(file_desc, &buffer2[0], 0,
static_cast<UINT32>(buffer2.size()),
&bytes_transferred2);
EXPECT_EQ(STATUS_SUCCESS, ret);
EXPECT_EQ(bytes_transferred, bytes_transferred2);
@ -327,24 +359,26 @@ static void read_and_write_test(remote_client &client) {
EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc));
utils::file::delete_file(test_file);
EXPECT_TRUE(utils::file::retry_delete_file(test_file));
}
static void rename_test(remote_client &client) {
const auto test_file = utils::path::absolute("./win_remote/rename.txt");
const auto test_file2 = utils::path::absolute("./win_remote/rename2.txt");
utils::file::delete_file(test_file);
utils::file::delete_file(test_file2);
auto api_path = utils::string::from_utf8(test_file).substr(mount_location_.size());
auto api_path2 = utils::string::from_utf8(test_file2).substr(mount_location_.size());
EXPECT_TRUE(utils::file::retry_delete_file(test_file));
EXPECT_TRUE(utils::file::retry_delete_file(test_file2));
auto api_path =
utils::string::from_utf8(test_file).substr(mount_location_.size());
auto api_path2 =
utils::string::from_utf8(test_file2).substr(mount_location_.size());
PVOID file_desc = reinterpret_cast<PVOID>(REPERTORY_INVALID_HANDLE);
remote::file_info fi{};
std::string normalized_name;
BOOLEAN exists = 0;
auto ret =
client.winfsp_create(&api_path[0], 0, GENERIC_READ | GENERIC_WRITE, FILE_ATTRIBUTE_NORMAL, 0,
&file_desc, &fi, normalized_name, exists);
BOOLEAN exists = 0u;
auto ret = client.winfsp_create(&api_path[0], 0, GENERIC_READ | GENERIC_WRITE,
FILE_ATTRIBUTE_NORMAL, 0, &file_desc, &fi,
normalized_name, exists);
EXPECT_EQ(STATUS_SUCCESS, ret);
ret = client.winfsp_rename(file_desc, &api_path[0], &api_path2[0], 0);
@ -354,23 +388,24 @@ static void rename_test(remote_client &client) {
EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc));
utils::file::delete_file(test_file);
utils::file::delete_file(test_file2);
EXPECT_TRUE(utils::file::retry_delete_file(test_file));
EXPECT_TRUE(utils::file::retry_delete_file(test_file2));
}
static void set_basic_info_test(remote_client &client) {
const auto test_file = utils::path::absolute("./win_remote/setbasicinfo.txt");
utils::file::delete_file(test_file);
utils::file::delete_file(test_file);
auto api_path = utils::string::from_utf8(test_file).substr(mount_location_.size());
EXPECT_TRUE(utils::file::retry_delete_file(test_file));
EXPECT_TRUE(utils::file::retry_delete_file(test_file));
auto api_path =
utils::string::from_utf8(test_file).substr(mount_location_.size());
PVOID file_desc = reinterpret_cast<PVOID>(REPERTORY_INVALID_HANDLE);
remote::file_info fi{};
std::string normalized_name;
BOOLEAN exists = 0;
auto ret =
client.winfsp_create(&api_path[0], 0, GENERIC_READ | GENERIC_WRITE, FILE_ATTRIBUTE_NORMAL, 0,
&file_desc, &fi, normalized_name, exists);
BOOLEAN exists = 0u;
auto ret = client.winfsp_create(&api_path[0], 0, GENERIC_READ | GENERIC_WRITE,
FILE_ATTRIBUTE_NORMAL, 0, &file_desc, &fi,
normalized_name, exists);
EXPECT_EQ(STATUS_SUCCESS, ret);
const auto attributes = FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_ARCHIVE;
@ -386,16 +421,18 @@ static void set_basic_info_test(remote_client &client) {
const auto last_write_time = ft.QuadPart + 2;
const auto change_time = last_write_time;
#else
const auto creation_time = utils::unix_time_to_windows_time(utils::get_time_now());
const auto creation_time =
utils::unix_time_to_windows_time(utils::get_time_now());
const auto last_access_time = creation_time + 1;
const auto last_write_time = creation_time + 2;
const auto change_time = last_write_time;
#endif
EXPECT_EQ(STATUS_SUCCESS,
client.winfsp_set_basic_info(file_desc, attributes, creation_time, last_access_time,
last_write_time, change_time, &fi));
EXPECT_EQ(attributes, fi.FileAttributes);
client.winfsp_set_basic_info(file_desc, attributes, creation_time,
last_access_time, last_write_time,
change_time, &fi));
EXPECT_EQ(static_cast<std::uint32_t>(attributes), fi.FileAttributes);
EXPECT_EQ(creation_time, fi.CreationTime);
EXPECT_EQ(last_access_time, fi.LastAccessTime);
EXPECT_EQ(last_write_time, fi.LastWriteTime);
@ -403,35 +440,37 @@ static void set_basic_info_test(remote_client &client) {
EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc));
utils::file::delete_file(test_file);
EXPECT_TRUE(utils::file::retry_delete_file(test_file));
}
static void set_file_size_test(remote_client &client) {
const auto test_file = utils::path::absolute("./win_remote/setfilesize.txt");
utils::file::delete_file(test_file);
auto api_path = utils::string::from_utf8(test_file).substr(mount_location_.size());
EXPECT_TRUE(utils::file::retry_delete_file(test_file));
auto api_path =
utils::string::from_utf8(test_file).substr(mount_location_.size());
PVOID file_desc = reinterpret_cast<PVOID>(REPERTORY_INVALID_HANDLE);
remote::file_info fi{};
std::string normalized_name;
BOOLEAN exists = 0;
auto ret =
client.winfsp_create(&api_path[0], 0, GENERIC_READ | GENERIC_WRITE, FILE_ATTRIBUTE_NORMAL, 0,
&file_desc, &fi, normalized_name, exists);
BOOLEAN exists = 0u;
auto ret = client.winfsp_create(&api_path[0], 0, GENERIC_READ | GENERIC_WRITE,
FILE_ATTRIBUTE_NORMAL, 0, &file_desc, &fi,
normalized_name, exists);
EXPECT_EQ(STATUS_SUCCESS, ret);
const UINT64 new_file_size = 34;
const BOOLEAN set_allocation_size = 0;
const UINT64 new_file_size = 34u;
const BOOLEAN set_allocation_size = 0u;
EXPECT_EQ(STATUS_SUCCESS,
client.winfsp_set_file_size(file_desc, new_file_size, set_allocation_size, &fi));
client.winfsp_set_file_size(file_desc, new_file_size,
set_allocation_size, &fi));
std::uint64_t file_size = 0;
utils::file::get_file_size(test_file, file_size);
EXPECT_EQ(34, file_size);
std::uint64_t file_size = 0u;
EXPECT_TRUE(utils::file::get_file_size(test_file, file_size));
EXPECT_EQ(34u, file_size);
EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc));
utils::file::delete_file(test_file);
EXPECT_TRUE(utils::file::retry_delete_file(test_file));
}
static void unmounted_test(remote_client &client) {
@ -441,7 +480,7 @@ static void unmounted_test(remote_client &client) {
TEST(remote_winfsp, all_tests) {
std::uint16_t port = 0;
const auto found_port = utils::get_next_available_port(20000u, port);
const auto found_port = utils::get_next_available_port(20020u, port);
EXPECT_TRUE(found_port);
if (found_port) {
console_consumer c;
@ -463,12 +502,14 @@ TEST(remote_winfsp, all_tests) {
mock_fuse_drive drive(mount_location_);
remote_fuse::remote_server server(config, drive, mount_location_);
#endif
std::this_thread::sleep_for(2s);
std::thread([&]() {
remote_client client(config);
can_delete_test(client);
cleanup_test(client);
create_and_close_test(client);
create_and_close_test(client, server);
create_and_read_directory_test(client);
flush_test(client);
get_file_info_test(client);
@ -487,6 +528,6 @@ TEST(remote_winfsp, all_tests) {
}
event_system::instance().stop();
utils::file::delete_directory_recursively("./win_remote");
EXPECT_TRUE(utils::file::delete_directory_recursively("./win_remote"));
}
} // namespace winfsp_test

View File

@ -1,126 +0,0 @@
/*
Copyright <2018-2022> <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 "db/retry_db.hpp"
#include "test_common.hpp"
namespace repertory {
TEST(retry_db, exists_and_set) {
const std::string data_directory = "./retrydb_exists_and_set";
utils::file::delete_directory_recursively(data_directory);
{
app_config config(provider_type::sia, data_directory);
retry_db db(config);
const auto api_path = "my_test/path.txt";
EXPECT_FALSE(db.exists(api_path));
db.set(api_path);
EXPECT_TRUE(db.exists(api_path));
#ifdef _WIN32
EXPECT_FALSE(db.exists(utils::string::to_upper(api_path)));
#endif
}
utils::file::delete_directory_recursively(data_directory);
}
TEST(retry_db, rename) {
const std::string data_directory = "./retrydb_rename";
utils::file::delete_directory_recursively(data_directory);
{
app_config config(provider_type::sia, data_directory);
retry_db db(config);
const auto api_path = "my_test/path.txt";
const auto api_path2 = "my_test/path2.txt";
db.set(api_path);
db.rename(api_path, api_path2);
EXPECT_FALSE(db.exists(api_path));
EXPECT_TRUE(db.exists(api_path2));
}
utils::file::delete_directory_recursively(data_directory);
}
TEST(retry_db, remove) {
const std::string data_directory = "./retrydb_remove";
utils::file::delete_directory_recursively(data_directory);
{
app_config config(provider_type::sia, data_directory);
retry_db db(config);
const auto api_path = "my_test/path.txt";
db.set(api_path);
db.remove(api_path);
EXPECT_FALSE(db.exists(api_path));
}
utils::file::delete_directory_recursively(data_directory);
}
TEST(retry_db, process_all) {
const std::string data_directory = "./retrydb_process_all";
utils::file::delete_directory_recursively(data_directory);
{
app_config config(provider_type::sia, data_directory);
retry_db db(config);
const auto api_path = "my_test/path.txt";
for (auto i = 0; i < 10; i++) {
db.set(api_path + std::to_string(i));
}
std::vector<std::string> items;
db.process_all([&items](const std::string &api_path) -> bool {
items.emplace_back(api_path);
return false;
});
EXPECT_EQ((std::size_t)10u, items.size());
for (auto i = 0; i < 10; i++) {
EXPECT_TRUE(db.exists(api_path + std::to_string(i)));
utils::remove_element_from(items, api_path + std::to_string(i));
}
EXPECT_TRUE(items.empty());
db.process_all([&items](const std::string &api_path) -> bool {
items.emplace_back(api_path);
return true;
});
EXPECT_EQ((std::size_t)10u, items.size());
for (auto i = 0; i < 10; i++) {
EXPECT_FALSE(db.exists(api_path + std::to_string(i)));
utils::remove_element_from(items, api_path + std::to_string(i));
}
EXPECT_TRUE(items.empty());
db.process_all([&items](const std::string &api_path) -> bool {
items.emplace_back(api_path);
return true;
});
EXPECT_TRUE(items.empty());
}
utils::file::delete_directory_recursively(data_directory);
}
} // namespace repertory

205
tests/s3_comm_test.cpp Normal file
View File

@ -0,0 +1,205 @@
/*
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 defined(REPERTORY_ENABLE_S3) && defined(REPERTORY_ENABLE_S3_TESTING)
#include "test_common.hpp"
#include "comm/s3/s3_comm.hpp"
#include "fixtures/s3_comm_fixture.hpp"
#include "types/repertory.hpp"
namespace repertory {
TEST_F(s3_comm_test, create_and_remove_directory) {
auto ret = s3_comm_->create_directory("/dir");
EXPECT_EQ(api_error::success, ret);
EXPECT_EQ(api_error::directory_exists, s3_comm_->directory_exists("/dir"));
ret = s3_comm_->remove_directory("/dir");
EXPECT_EQ(api_error::success, ret);
EXPECT_EQ(api_error::directory_not_found, s3_comm_->directory_exists("/dir"));
ret = s3_comm_->remove_directory("/dir");
EXPECT_TRUE(ret == api_error::success ||
ret == api_error::directory_not_found);
}
TEST_F(s3_comm_test, upload_file) {
stop_type stop_requested = false;
auto ret = s3_comm_->upload_file(
"/test.txt", __FILE__, "", []() -> std::string { return ""; },
[](const std::string &) -> api_error { return api_error::success; },
stop_requested);
EXPECT_EQ(api_error::success, ret);
ret = s3_comm_->upload_file(
"/subdir/test2.txt", __FILE__, "", []() -> std::string { return ""; },
[](const std::string &) -> api_error { return api_error::success; },
stop_requested);
EXPECT_EQ(api_error::success, ret);
}
TEST_F(s3_comm_test, get_directory_items) {
directory_item_list list{};
auto ret = s3_comm_->get_directory_items(
"/subdir", [](directory_item &) {}, list);
EXPECT_EQ(api_error::success, ret);
}
TEST_F(s3_comm_test, list_directories) {
api_file_list list{};
auto ret = s3_comm_->get_directory_list(list);
EXPECT_EQ(api_error::success, ret);
}
TEST_F(s3_comm_test, list_files) {
api_file_list list{};
auto ret = s3_comm_->get_file_list(
[](const std::string &) -> std::string { return ""; },
[](const std::string &, const std::string &object_name) -> std::string {
return object_name;
},
list);
EXPECT_EQ(api_error::success, ret);
}
TEST_F(s3_comm_test, read_file_bytes) {
stop_type stop_requested = false;
data_buffer data;
auto ret = s3_comm_->read_file_bytes(
"/test.txt", 2, 0, data, []() -> std::string { return ""; },
[]() -> std::uint64_t { return 0ull; },
[]() -> std::string { return ""; }, stop_requested);
EXPECT_EQ(api_error::success, ret);
}
TEST_F(s3_comm_test, exists) {
EXPECT_EQ(
api_error::item_exists,
s3_comm_->file_exists("/test.txt", []() -> std::string { return ""; }));
EXPECT_EQ(api_error::item_not_found,
s3_comm_->file_exists("/subdir/test.txt",
[]() -> std::string { return ""; }));
}
TEST_F(s3_comm_test, get_file) {
api_file file{};
auto ret = s3_comm_->get_file(
"/test.txt", []() -> std::string { return ""; },
[](const std::string &, const std::string &object_name) -> std::string {
return object_name;
},
[]() -> std::string { return ""; }, file);
EXPECT_EQ(api_error::success, ret);
}
TEST_F(s3_comm_test, remove_file) {
auto ret =
s3_comm_->remove_file("/test.txt", []() -> std::string { return ""; });
EXPECT_EQ(api_error::success, ret);
ret = s3_comm_->remove_file("/subdir/test2.txt",
[]() -> std::string { return ""; });
EXPECT_EQ(api_error::success, ret);
}
TEST_F(s3_comm_test, rename_file) {
stop_type stop_requested = false;
auto ret =
s3_comm_->remove_file("/test_r2.txt", []() -> std::string { return ""; });
ret = s3_comm_->upload_file(
"/test_r1.txt", __FILE__, "", []() -> std::string { return ""; },
[](const std::string &) -> api_error { return api_error::success; },
stop_requested);
EXPECT_EQ(api_error::success, ret);
ret = s3_comm_->rename_file("/test_r1.txt", "/test_r2.txt");
EXPECT_EQ(api_error::not_implemented, ret);
EXPECT_EQ(api_error::item_exists,
s3_comm_->file_exists("/test_r1.txt",
[]() -> std::string { return ""; }));
EXPECT_EQ(api_error::item_not_found,
s3_comm_->file_exists("/test_r2.txt",
[]() -> std::string { return ""; }));
EXPECT_EQ(api_error::success,
s3_comm_->remove_file("/test_r1.txt",
[]() -> std::string { return ""; }));
}
TEST_F(s3_comm_test, upload_file_encrypted) {
const auto source_file_path = generate_test_file_name("./", "awscomm");
auto file_size =
2u * utils::encryption::encrypting_reader::get_data_chunk_size() + 3u;
auto source_file = create_random_file(source_file_path, file_size);
stop_type stop_requested = false;
std::string key;
auto ret = s3_comm_->upload_file(
"/test.txt", source_file_path, "test", []() -> std::string { return ""; },
[&key](const std::string &k) -> api_error {
key = k;
std::cout << "key:" << key << std::endl;
return api_error::success;
},
stop_requested);
EXPECT_EQ(api_error::success, ret);
std::uint64_t offset = 0u;
auto remain = file_size;
while ((ret == api_error::success) && remain) {
data_buffer data;
ret = s3_comm_->read_file_bytes(
"/test.txt",
std::min(remain,
utils::encryption::encrypting_reader::get_data_chunk_size()),
offset, data, [&key]() -> std::string { return key; },
[&file_size]() -> std::uint64_t { return file_size; },
[]() -> std::string { return "test"; }, stop_requested);
EXPECT_EQ(api_error::success, ret);
data_buffer data2(data.size());
std::size_t bytes_read{};
EXPECT_TRUE(
source_file->read_bytes(&data2[0u], data2.size(), offset, bytes_read));
EXPECT_EQ(0, std::memcmp(&data2[0u], &data[0u], data2.size()));
remain -= data.size();
offset += data.size();
}
source_file->close();
EXPECT_TRUE(utils::file::retry_delete_file(source_file_path));
EXPECT_EQ(api_error::success,
s3_comm_->remove_file("/test.txt",
[&key]() -> std::string { return key; }));
}
TEST_F(s3_comm_test, get_directory_item_count) {}
TEST_F(s3_comm_test, get_object_list) {}
TEST_F(s3_comm_test, get_object_name) {}
TEST_F(s3_comm_test, is_online) {}
} // namespace repertory
#endif // REPERTORY_ENABLE_S3_TESTING

189
tests/s3_provider_test.cpp Normal file
View File

@ -0,0 +1,189 @@
/*
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 defined(REPERTORY_ENABLE_S3) && defined(REPERTORY_ENABLE_S3_TESTING)
#include "test_common.hpp"
#include "app_config.hpp"
#include "events/consumers/console_consumer.hpp"
#include "events/event_system.hpp"
#include "file_manager/file_manager.hpp"
#include "fixtures/s3_provider_file_list_fixture.hpp"
#include "mocks/mock_s3_comm.hpp"
#include "platform/platform.hpp"
#include "providers/s3/s3_provider.hpp"
#include "types/s3.hpp"
#include "types/startup_exception.hpp"
#include "utils/path_utils.hpp"
#include "utils/utils.hpp"
namespace repertory {
// TEST(s3_provider, can_construct_s3_provider) {
// {
// app_config cfg(provider_type::s3, "./s3_provider_test");
// EXPECT_FALSE(cfg.set_value_by_name("S3Config.Bucket", "bucket").empty());
// EXPECT_FALSE(
// cfg.set_value_by_name("S3Config.URL", "https://url.com").empty());
// mock_s3_comm comm(cfg.get_s3_config());
// s3_provider s3(cfg, comm);
// EXPECT_EQ(s3.get_total_item_count(), 0u);
// }
//
// EXPECT_TRUE(utils::file::delete_directory_recursively("./s3_provider_test"));
// }
//
TEST(s3_provider, start_fails_with_empty_bucket) {
{
app_config cfg(provider_type::s3, "./s3_provider_test");
EXPECT_TRUE(cfg.set_value_by_name("S3Config.Bucket", "").empty());
EXPECT_FALSE(
cfg.set_value_by_name("S3Config.URL", "https://url.com").empty());
mock_s3_comm comm(cfg.get_s3_config());
s3_provider s3(cfg, comm);
file_manager fm(cfg, s3);
try {
auto res = s3.start(
[](bool, api_file & /* file */) -> api_error {
return api_error::success;
},
&fm);
std::cerr << "unexpected return-should throw|err|" << res << std::endl;
} catch (const startup_exception &e) {
EXPECT_STREQ("s3 bucket name cannot be empty", e.what());
return;
}
throw std::runtime_error("exception not thrown");
}
EXPECT_TRUE(utils::file::delete_directory_recursively("./s3_provider_test"));
}
TEST(s3_provider, start_fails_when_provider_is_offline) {
{
app_config cfg(provider_type::s3, "./s3_provider_test");
EXPECT_FALSE(cfg.set_value_by_name("S3Config.Bucket", "bucket").empty());
EXPECT_FALSE(
cfg.set_value_by_name("S3Config.URL", "https://url.com").empty());
cfg.set_online_check_retry_secs(2u);
mock_s3_comm comm(cfg.get_s3_config());
s3_provider s3(cfg, comm);
file_manager fm(cfg, s3);
EXPECT_CALL(comm, is_online()).WillRepeatedly(Return(false));
EXPECT_FALSE(s3.start([](bool, api_file & /* file */)
-> api_error { return api_error::success; },
&fm));
}
EXPECT_TRUE(utils::file::delete_directory_recursively("./s3_provider_test"));
}
// TEST(s3_provider, get_empty_file_list) {
// {
// app_config cfg(provider_type::s3, "./s3_provider_test");
// EXPECT_FALSE(cfg.set_value_by_name("S3Config.Bucket", "bucket").empty());
// EXPECT_FALSE(
// cfg.set_value_by_name("S3Config.URL", "https://url.com").empty());
// cfg.set_online_check_retry_secs(2u);
// mock_s3_comm comm(cfg.get_s3_config());
// s3_provider s3(cfg, comm);
// file_manager fm(cfg, s3);
//
// api_file_list list{};
// EXPECT_CALL(comm, get_file_list)
// .WillOnce([](const get_api_file_token_callback &,
// const get_name_callback &,
// api_file_list &) { return api_error::success; });
//
// EXPECT_EQ(api_error::success, s3.get_file_list(list));
// }
//
// EXPECT_TRUE(utils::file::delete_directory_recursively("./s3_provider_test"));
// }
//
// TEST_F(s3_provider_file_list_test, can_add_new_files_and_directories) {
// provider->set_callback([this](bool directory, api_file &file) -> api_error
// {
// std::cout << "added|api_path|" << file.api_path << "|api_parent|"
// << file.api_parent << "|source|" << file.source_path
// << "|directory|" << directory << "|create_date|"
// << file.creation_date << "|access_date|" << file.accessed_date
// << "|modified_date|" << file.modified_date << "|changed_date|"
// << file.changed_date << std::endl;
// return provider_meta_handler(*provider, directory, file);
// });
//
// api_file_list l{};
// auto res = provider->get_file_list(l);
// EXPECT_EQ(api_error::success, res);
// EXPECT_EQ(list.size(), l.size());
// EXPECT_EQ(std::size_t(22u), provider->get_total_item_count());
//
// bool exists{};
// EXPECT_EQ(api_error::success, provider->is_directory("/", exists));
// EXPECT_TRUE(exists);
//
// EXPECT_EQ(api_error::success, provider->is_directory("/dir", exists));
// EXPECT_TRUE(exists);
//
// const auto check_file = [this, &l](std::size_t idx,
// bool check_sub_directory) {
// const auto &file = l.at(idx);
// const auto base_idx = idx - (check_sub_directory ? l.size() / 2 : 0u);
// EXPECT_EQ(this->times[base_idx], file.accessed_date);
// if (check_sub_directory) {
// EXPECT_EQ(utils::path::create_api_path("/dir/file_" +
// std::to_string(base_idx) +
// ".txt"),
// file.api_path);
// EXPECT_EQ(utils::path::get_parent_api_path(utils::path::create_api_path(
// "/dir/file_" + std::to_string(base_idx) + ".txt")),
// file.api_parent);
// } else {
// EXPECT_EQ(utils::path::create_api_path("/file_" +
// std::to_string(base_idx) +
// ".txt"),
// file.api_path);
// EXPECT_EQ(utils::path::get_parent_api_path(utils::path::create_api_path(
// "/file_" + std::to_string(base_idx) + ".txt")),
// file.api_parent);
// }
// EXPECT_EQ(this->times[base_idx] + 1u, file.changed_date);
// EXPECT_EQ(this->times[base_idx] + 2u, file.creation_date);
// EXPECT_TRUE(file.encryption_token.empty());
// EXPECT_EQ(100u + base_idx, file.file_size);
// EXPECT_EQ(this->times[base_idx] + 3u, file.modified_date);
// };
//
// for (std::size_t idx = 0u; idx < l.size() / 2u; idx++) {
// check_file(idx, false);
// }
//
// for (std::size_t idx = l.size() / 2u; idx < l.size(); idx++) {
// check_file(idx, true);
// }
// }
} // namespace repertory
#endif // REPERTORY_ENABLE_S3_TESTING

326
tests/sia_provider_test.cpp Normal file
View File

@ -0,0 +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

View File

@ -1,803 +0,0 @@
/*
Copyright <2018-2022> <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 defined(REPERTORY_ENABLE_SKYNET)
#include "fixtures/skynet_provider_fixture.hpp"
#include "utils/encrypting_reader.hpp"
#include "utils/encryption.hpp"
#define EXTERNAL_SKYLINK "AACoqIuN00YdDhS21dUMpMYFYGDeGmPnGoNWOkItkmzLfw"
namespace repertory {
static void populate_file_meta(const std::string &, api_meta_map &meta) {
meta[META_ACCESSED] = std::to_string(utils::get_file_time_now());
meta[META_MODIFIED] = std::to_string(utils::get_file_time_now());
meta[META_CREATION] = std::to_string(utils::get_file_time_now());
}
TEST_F(skynet_provider_test, create_directory_and_create_file) {
api_meta_map directory_meta{};
EXPECT_EQ(api_error::success, provider_->get_item_meta("/", directory_meta));
EXPECT_TRUE(provider_->is_directory("/"));
api_meta_map file_meta{};
populate_file_meta("/test.txt", file_meta);
EXPECT_EQ(api_error::success, provider_->create_file("/test.txt", file_meta));
EXPECT_TRUE(provider_->is_file("/test.txt"));
EXPECT_TRUE(provider_->is_file_writeable("/test.txt"));
}
TEST_F(skynet_provider_test, get_file) {
api_meta_map directory_meta{};
EXPECT_EQ(api_error::success, provider_->get_item_meta("/", directory_meta));
EXPECT_TRUE(provider_->is_directory("/"));
api_meta_map file_meta{};
populate_file_meta("/test.txt", file_meta);
EXPECT_EQ(api_error::success, provider_->create_file("/test.txt", file_meta));
EXPECT_TRUE(provider_->is_file("/test.txt"));
api_file apiFile{};
EXPECT_EQ(api_error::success, provider_->get_file("/test.txt", apiFile));
EXPECT_STREQ("/test.txt", apiFile.api_path.c_str());
EXPECT_STREQ("/", apiFile.api_parent.c_str());
EXPECT_EQ(utils::string::to_uint64(file_meta[META_ACCESSED]), apiFile.accessed_date);
EXPECT_EQ(utils::string::to_uint64(file_meta[META_MODIFIED]), apiFile.changed_date);
EXPECT_EQ(utils::string::to_uint64(file_meta[META_CREATION]), apiFile.created_date);
EXPECT_EQ(0, apiFile.file_size);
EXPECT_EQ(utils::string::to_uint64(file_meta[META_MODIFIED]), apiFile.modified_date);
EXPECT_TRUE(apiFile.recoverable);
EXPECT_EQ(3.0, apiFile.redundancy);
EXPECT_TRUE(utils::file::is_file(apiFile.source_path));
}
TEST_F(skynet_provider_test, get_directory_item_count) {
api_meta_map directory_meta{};
EXPECT_EQ(api_error::success, provider_->get_item_meta("/", directory_meta));
EXPECT_EQ(api_error::success, provider_->create_directory("/sub", directory_meta));
api_meta_map file_meta{};
populate_file_meta("/test.txt", file_meta);
EXPECT_EQ(api_error::success, provider_->create_file("/test.txt", file_meta));
EXPECT_EQ(2, provider_->get_directory_item_count("/"));
}
TEST_F(skynet_provider_test, get_directory_items) {
api_meta_map directory_meta{};
EXPECT_EQ(api_error::success, provider_->get_item_meta("/", directory_meta));
EXPECT_EQ(api_error::success, provider_->create_directory("/sub", directory_meta));
api_meta_map file_meta{};
populate_file_meta("/test.txt", file_meta);
EXPECT_EQ(api_error::success, provider_->create_file("/test.txt", file_meta));
directory_item_list itemList;
EXPECT_EQ(api_error::success, provider_->get_directory_items("/", itemList));
EXPECT_EQ(std::size_t(4), itemList.size());
EXPECT_STREQ("/sub", itemList[2u].api_path.c_str());
EXPECT_STREQ("/", itemList[2u].api_parent.c_str());
EXPECT_TRUE(itemList[2u].directory);
EXPECT_EQ(0u, itemList[2u].size);
// itemList[0].MetaMap;
EXPECT_STREQ("/test.txt", itemList[3u].api_path.c_str());
EXPECT_STREQ("/", itemList[3u].api_parent.c_str());
EXPECT_FALSE(itemList[3u].directory);
EXPECT_EQ(0u, itemList[3u].size);
// itemList[1].MetaMap;
}
TEST_F(skynet_provider_test, get_file_list) {
api_meta_map directory_meta{};
EXPECT_EQ(api_error::success, provider_->get_item_meta("/", directory_meta));
EXPECT_EQ(api_error::success, provider_->create_directory("/sub", directory_meta));
api_meta_map file_meta{};
populate_file_meta("/test.txt", file_meta);
EXPECT_EQ(api_error::success, provider_->create_file("/test.txt", file_meta));
populate_file_meta("/test2.txt", file_meta);
EXPECT_EQ(api_error::success, provider_->create_file("/test2.txt", file_meta));
api_file_list fileList;
EXPECT_EQ(api_error::success, provider_->get_file_list(fileList));
EXPECT_EQ(std::size_t(2), fileList.size());
}
TEST_F(skynet_provider_test, get_fileSize) {
api_meta_map directory_meta{};
EXPECT_EQ(api_error::success, provider_->get_item_meta("/", directory_meta));
api_meta_map file_meta{};
populate_file_meta("/test.txt", file_meta);
EXPECT_EQ(api_error::success, provider_->create_file("/test.txt", file_meta));
std::uint64_t file_size = 100ull;
EXPECT_EQ(api_error::success, provider_->get_file_size("/test.txt", file_size));
EXPECT_EQ(std::uint64_t(0), file_size);
}
TEST_F(skynet_provider_test, get_host_config) {
auto hc = provider_->get_host_config(false);
EXPECT_TRUE(hc.agent_string.empty());
EXPECT_TRUE(hc.api_password.empty());
EXPECT_EQ(hc.api_port, 443);
EXPECT_STREQ(DEFAULT_SKYNET_URLS[0u].c_str(), hc.host_name_or_ip.c_str());
EXPECT_STREQ("/", hc.path.c_str());
EXPECT_STREQ("https", hc.protocol.c_str());
EXPECT_STREQ(DEFAULT_SKYNET_URLS[1u].c_str(), hc.auth_url.c_str());
hc = provider_->get_host_config(true);
EXPECT_TRUE(hc.agent_string.empty());
EXPECT_TRUE(hc.api_password.empty());
EXPECT_EQ(hc.api_port, 443);
EXPECT_STREQ(DEFAULT_SKYNET_URLS[0u].c_str(), hc.host_name_or_ip.c_str());
EXPECT_STREQ("/skynet/skyfile", hc.path.c_str());
EXPECT_STREQ(DEFAULT_SKYNET_URLS[1u].c_str(), hc.auth_url.c_str());
EXPECT_STREQ("https", hc.protocol.c_str());
const auto string_list = config_->get_value_by_name("SkynetConfig.PortalList");
json list = json::parse(string_list);
list[0u]["AuthUser"] = "test_user";
list[0u]["AuthURL"] = "test_url";
list[0u]["AuthPassword"] = "test_pwd";
config_->set_value_by_name("SkynetConfig.PortalList", list.dump());
provider_->update_portal_list();
hc = provider_->get_host_config(false);
EXPECT_TRUE(hc.agent_string.empty());
EXPECT_TRUE(hc.api_password.empty());
EXPECT_EQ(hc.api_port, 443);
EXPECT_STREQ(DEFAULT_SKYNET_URLS[0u].c_str(), hc.host_name_or_ip.c_str());
EXPECT_STREQ("/", hc.path.c_str());
EXPECT_STREQ("https", hc.protocol.c_str());
EXPECT_STREQ("test_user", hc.auth_user.c_str());
EXPECT_STREQ("test_url", hc.auth_url.c_str());
EXPECT_STREQ("test_pwd", hc.auth_password.c_str());
hc = provider_->get_host_config(true);
EXPECT_TRUE(hc.agent_string.empty());
EXPECT_TRUE(hc.api_password.empty());
EXPECT_EQ(hc.api_port, 443);
EXPECT_STREQ(DEFAULT_SKYNET_URLS[0u].c_str(), hc.host_name_or_ip.c_str());
EXPECT_STREQ("/skynet/skyfile", hc.path.c_str());
EXPECT_STREQ("https", hc.protocol.c_str());
EXPECT_STREQ("test_user", hc.auth_user.c_str());
EXPECT_STREQ("test_url", hc.auth_url.c_str());
EXPECT_STREQ("test_pwd", hc.auth_password.c_str());
}
TEST_F(skynet_provider_test, remove_directory) {
api_meta_map directory_meta{};
EXPECT_EQ(api_error::success, provider_->get_item_meta("/", directory_meta));
EXPECT_EQ(api_error::success, provider_->create_directory("/sub", directory_meta));
EXPECT_TRUE(provider_->is_directory("/sub"));
EXPECT_EQ(api_error::success, provider_->remove_directory("/sub"));
EXPECT_FALSE(provider_->is_directory("/sub"));
}
TEST_F(skynet_provider_test, recreate_directory_after_remove_directory) {
api_meta_map directory_meta{};
EXPECT_EQ(api_error::success, provider_->get_item_meta("/", directory_meta));
EXPECT_EQ(api_error::success, provider_->create_directory("/sub", directory_meta));
EXPECT_TRUE(provider_->is_directory("/sub"));
EXPECT_EQ(api_error::success, provider_->remove_directory("/sub"));
EXPECT_FALSE(provider_->is_directory("/sub"));
EXPECT_EQ(api_error::success, provider_->create_directory("/sub", directory_meta));
EXPECT_TRUE(provider_->is_directory("/sub"));
}
TEST_F(skynet_provider_test, remove_directory_fails_if_sub_directories_exist) {
api_meta_map directory_meta{};
EXPECT_EQ(api_error::success, provider_->get_item_meta("/", directory_meta));
EXPECT_EQ(api_error::success, provider_->create_directory("/sub", directory_meta));
EXPECT_EQ(api_error::success, provider_->create_directory("/sub/sub2", directory_meta));
EXPECT_TRUE(provider_->is_directory("/sub/sub2"));
EXPECT_EQ(api_error::directory_not_empty, provider_->remove_directory("/sub"));
EXPECT_TRUE(provider_->is_directory("/sub"));
EXPECT_TRUE(provider_->is_directory("/sub/sub2"));
}
TEST_F(skynet_provider_test, remove_directory_fails_if_files_exist) {
api_meta_map directory_meta{};
EXPECT_EQ(api_error::success, provider_->get_item_meta("/", directory_meta));
EXPECT_EQ(api_error::success, provider_->create_directory("/sub", directory_meta));
EXPECT_TRUE(provider_->is_directory("/sub"));
api_meta_map file_meta{};
populate_file_meta("/sub/test.txt", file_meta);
EXPECT_EQ(api_error::success, provider_->create_file("/sub/test.txt", file_meta));
EXPECT_EQ(api_error::directory_not_empty, provider_->remove_directory("/sub"));
EXPECT_TRUE(provider_->is_directory("/sub"));
EXPECT_TRUE(provider_->is_file("/sub/test.txt"));
}
TEST_F(skynet_provider_test, remove_directory_fails_for_root_directory) {
api_meta_map directory_meta{};
EXPECT_EQ(api_error::success, provider_->get_item_meta("/", directory_meta));
EXPECT_EQ(api_error::access_denied, provider_->remove_directory("/"));
EXPECT_TRUE(provider_->is_directory("/"));
}
TEST_F(skynet_provider_test, remove_file) {
api_meta_map directory_meta{};
EXPECT_EQ(api_error::success, provider_->get_item_meta("/", directory_meta));
EXPECT_TRUE(provider_->is_directory("/"));
api_meta_map file_meta{};
populate_file_meta("/test.txt", file_meta);
EXPECT_EQ(api_error::success, provider_->create_file("/test.txt", file_meta));
EXPECT_TRUE(provider_->is_file("/test.txt"));
EXPECT_EQ(api_error::success, provider_->remove_file("/test.txt"));
EXPECT_FALSE(provider_->is_file("/test.txt"));
}
TEST_F(skynet_provider_test, recreate_file_after_remove_file) {
api_meta_map directory_meta{};
EXPECT_EQ(api_error::success, provider_->get_item_meta("/", directory_meta));
EXPECT_TRUE(provider_->is_directory("/"));
api_meta_map file_meta{};
populate_file_meta("/test.txt", file_meta);
EXPECT_EQ(api_error::success, provider_->create_file("/test.txt", file_meta));
EXPECT_TRUE(provider_->is_file("/test.txt"));
EXPECT_EQ(api_error::success, provider_->remove_file("/test.txt"));
EXPECT_FALSE(provider_->is_file("/test.txt"));
EXPECT_EQ(api_error::success, provider_->create_file("/test.txt", file_meta));
EXPECT_TRUE(provider_->is_file("/test.txt"));
}
TEST_F(skynet_provider_test, rename_file) {
api_meta_map directory_meta{};
EXPECT_EQ(api_error::success, provider_->get_item_meta("/", directory_meta));
api_meta_map file_meta{};
populate_file_meta("/test.txt", file_meta);
EXPECT_EQ(api_error::success, provider_->create_file("/test.txt", file_meta));
EXPECT_EQ(api_error::success, provider_->get_item_meta("/test.txt", file_meta));
EXPECT_EQ(api_error::success, provider_->rename_file("/test.txt", "/test2.txt"));
EXPECT_TRUE(provider_->is_file("/test2.txt"));
EXPECT_FALSE(provider_->is_file("/test.txt"));
api_meta_map fileMeta2{};
EXPECT_EQ(api_error::success, provider_->get_item_meta("/test2.txt", fileMeta2));
EXPECT_EQ(file_meta.size(), fileMeta2.size());
for (const auto &kv : file_meta) {
EXPECT_STREQ(file_meta[kv.first].c_str(), fileMeta2[kv.first].c_str());
}
}
TEST_F(skynet_provider_test, upload_file_and_read_file_bytes) {
config_->set_value_by_name("SkynetConfig.EncryptionToken", "");
api_meta_map directory_meta{};
EXPECT_EQ(api_error::success, provider_->get_item_meta("/", directory_meta));
api_meta_map file_meta{};
populate_file_meta("/test.txt", file_meta);
EXPECT_EQ(api_error::success, provider_->create_file("/test.txt", file_meta));
filesystem_item fsi{};
EXPECT_EQ(api_error::success, provider_->get_filesystem_item("/test.txt", false, fsi));
EXPECT_TRUE(fsi.encryption_token.empty());
EXPECT_FALSE(fsi.is_encrypted());
json j = {{"test", "test"}, {"test2", "test"}};
EXPECT_TRUE(utils::file::write_json_file(fsi.source_path, j));
EXPECT_EQ(api_error::success,
provider_->upload_file(fsi.api_path, fsi.source_path, fsi.encryption_token));
while (provider_->is_processing(fsi.api_path)) {
std::this_thread::sleep_for(1ms);
}
std::string id;
EXPECT_EQ(api_error::success, provider_->get_item_meta(fsi.api_path, META_ID, id));
json skynet_info = json::parse(id);
std::cout << skynet_info.dump(2) << std::endl;
EXPECT_NE(0u, skynet_info["skylink"].size());
std::uint64_t file_size = 0;
utils::file::get_file_size(fsi.source_path, file_size);
std::vector<char> data;
auto stop_requested = false;
EXPECT_EQ(api_error::success,
provider_->read_file_bytes("/test.txt", static_cast<std::size_t>(file_size), 0, data,
stop_requested));
EXPECT_EQ(file_size, std::uint64_t(data.size()));
EXPECT_STREQ(j.dump().c_str(), json::parse(std::string(&data[0], data.size())).dump().c_str());
http_ranges ranges = {{0, 0}};
http_headers headers;
json error;
EXPECT_EQ(api_error::success,
curl_comm_->get_range_and_headers(
provider_->get_host_config(false), "/" + skynet_info["skylink"].get<std::string>(),
0u, {{"format", "concat"}}, "", data, ranges, error, headers, stop_requested));
for (const auto &header : headers) {
std::cout << header.first << ":" << header.second << std::endl;
}
json meta_data;
EXPECT_EQ(api_error::success,
provider_->get_skynet_metadata(skynet_info["skylink"].get<std::string>(), meta_data));
EXPECT_STREQ(meta_data["filename"].get<std::string>().c_str(), "test.txt");
}
TEST_F(skynet_provider_test, upload_encrypted_file_and_read_file_bytes) {
config_->set_value_by_name("SkynetConfig.EncryptionToken", "TestToken");
api_meta_map directory_meta{};
EXPECT_EQ(api_error::success, provider_->get_item_meta("/", directory_meta));
api_meta_map file_meta{};
populate_file_meta("/test.txt", file_meta);
EXPECT_EQ(api_error::success, provider_->create_file("/test.txt", file_meta));
filesystem_item fsi{};
EXPECT_EQ(api_error::success, provider_->get_filesystem_item("/test.txt", false, fsi));
EXPECT_STREQ("TestToken", fsi.encryption_token.c_str());
EXPECT_TRUE(fsi.is_encrypted());
const auto file_size = 2u * utils::encryption::encrypting_reader::get_data_chunk_size() + 3u;
auto source_file = create_random_file(fsi.source_path, file_size);
EXPECT_EQ(api_error::success,
provider_->upload_file(fsi.api_path, fsi.source_path, fsi.encryption_token));
while (provider_->is_processing(fsi.api_path)) {
std::this_thread::sleep_for(1ms);
}
std::string id;
EXPECT_EQ(api_error::success, provider_->get_item_meta(fsi.api_path, META_ID, id));
json skynet_info = json::parse(id);
std::cout << skynet_info.dump(2) << std::endl;
EXPECT_NE(0u, skynet_info["skylink"].size());
auto stop_requested = false;
const auto size = file_size / 3u;
const auto size_remain = file_size % 3u;
for (std::uint8_t i = 0u; i < 3u; i++) {
const auto read_size = size + (i == 2u ? size_remain : 0u);
std::vector<char> data;
EXPECT_EQ(api_error::success,
provider_->read_file_bytes("/test.txt", read_size, i * size, data, stop_requested));
std::size_t bytes_read{};
std::vector<char> b(read_size);
EXPECT_TRUE(source_file->read_bytes(&b[0u], b.size(), i * size, bytes_read));
EXPECT_EQ(b.size(), read_size);
EXPECT_EQ(0, std::memcmp(&data[0u], &b[0u], data.size()));
}
source_file->close();
http_ranges ranges = {{0, 0}};
http_headers headers;
json error;
auto err = api_error::error;
for (std::size_t j = 0u; (err != api_error::success) && (j < config_->get_retry_read_count());
j++) {
std::vector<char> data;
err = curl_comm_->get_range_and_headers(
provider_->get_host_config(false), "/" + skynet_info["skylink"].get<std::string>(), 0u,
{{"format", "concat"}}, "", data, ranges, error, headers, stop_requested);
}
EXPECT_EQ(api_error::success, err);
for (const auto &header : headers) {
std::cout << header.first << ":" << header.second << std::endl;
}
json meta_data;
EXPECT_EQ(api_error::success,
provider_->get_skynet_metadata(skynet_info["skylink"].get<std::string>(), meta_data));
const auto encrypted_file_name = meta_data["filename"].get<std::string>();
std::vector<char> buffer;
EXPECT_TRUE(utils::from_hex_string(encrypted_file_name, buffer));
std::string file_name;
decrypt_and_verify(buffer, fsi.encryption_token, file_name);
EXPECT_STREQ("test.txt", file_name.c_str());
config_->set_value_by_name("SkynetConfig.EncryptionToken", "");
}
TEST_F(skynet_provider_test, upload_tiny_encrypted_file_and_read_file_bytes) {
config_->set_value_by_name("SkynetConfig.EncryptionToken", "TestToken");
api_meta_map directory_meta{};
EXPECT_EQ(api_error::success, provider_->get_item_meta("/", directory_meta));
api_meta_map file_meta{};
populate_file_meta("/test.txt", file_meta);
EXPECT_EQ(api_error::success, provider_->create_file("/test.txt", file_meta));
filesystem_item fsi{};
EXPECT_EQ(api_error::success, provider_->get_filesystem_item("/test.txt", false, fsi));
EXPECT_STREQ("TestToken", fsi.encryption_token.c_str());
EXPECT_TRUE(fsi.is_encrypted());
json j = {{"test", "test"}, {"test2", "test"}};
EXPECT_TRUE(utils::file::write_json_file(fsi.source_path, j));
EXPECT_EQ(api_error::success,
provider_->upload_file(fsi.api_path, fsi.source_path, fsi.encryption_token));
while (provider_->is_processing(fsi.api_path)) {
std::this_thread::sleep_for(1ms);
}
std::string id;
EXPECT_EQ(api_error::success, provider_->get_item_meta(fsi.api_path, META_ID, id));
json skynet_info = json::parse(id);
std::cout << skynet_info.dump(2) << std::endl;
EXPECT_NE(0u, skynet_info["skylink"].size());
std::uint64_t file_size = 0;
utils::file::get_file_size(fsi.source_path, file_size);
std::vector<char> data;
auto stop_requested = false;
EXPECT_EQ(api_error::success,
provider_->read_file_bytes("/test.txt", static_cast<std::size_t>(file_size), 0, data,
stop_requested));
EXPECT_EQ(file_size, std::uint64_t(data.size()));
const auto str = std::string(&data[0u], data.size());
EXPECT_STREQ(j.dump().c_str(), json::parse(str).dump().c_str());
http_ranges ranges = {{0, 0}};
http_headers headers;
json error;
auto apiError = api_error::error;
for (std::size_t i = 0; (apiError != api_error::success) && (i < config_->get_retry_read_count());
i++) {
apiError = curl_comm_->get_range_and_headers(
provider_->get_host_config(false), "/" + skynet_info["skylink"].get<std::string>(), 0u,
{{"format", "concat"}}, "", data, ranges, error, headers, stop_requested);
}
EXPECT_EQ(api_error::success, apiError);
for (const auto &header : headers) {
std::cout << header.first << ":" << header.second << std::endl;
}
json meta_data;
EXPECT_EQ(api_error::success,
provider_->get_skynet_metadata(skynet_info["skylink"].get<std::string>(), meta_data));
const auto encrypted_file_name = meta_data["filename"].get<std::string>();
std::vector<char> buffer;
EXPECT_TRUE(utils::from_hex_string(encrypted_file_name, buffer));
std::string file_name;
decrypt_and_verify(buffer, fsi.encryption_token, file_name);
EXPECT_STREQ("test.txt", file_name.c_str());
config_->set_value_by_name("SkynetConfig.EncryptionToken", "");
}
TEST_F(skynet_provider_test, import_and_export) {
api_meta_map directory_meta{};
EXPECT_EQ(api_error::success, provider_->get_item_meta("/", directory_meta));
skylink_import sl{"", "", "AACeCiD6WQG6DzDcCdIu3cFPSxMUMoQPx46NYSyijNMKUA", ""};
EXPECT_EQ(api_error::success, provider_->import_skylink(sl));
EXPECT_TRUE(provider_->is_file("/repertory_test_import.txt"));
std::uint64_t file_size = 0u;
EXPECT_EQ(api_error::success, provider_->get_file_size("/repertory_test_import.txt", file_size));
EXPECT_EQ(std::uint64_t(3u), file_size);
api_meta_map meta;
EXPECT_EQ(api_error::success, provider_->get_item_meta("/repertory_test_import.txt", meta));
const auto id = json::parse(meta[META_ID]);
EXPECT_STREQ("AACeCiD6WQG6DzDcCdIu3cFPSxMUMoQPx46NYSyijNMKUA",
id["skylink"].get<std::string>().c_str());
const auto test_success = [](json &result) {
EXPECT_EQ(std::size_t(1u), result["success"].size());
auto i = skylink_import::from_json(result["success"][0u]);
EXPECT_STREQ("/", i.directory.c_str());
EXPECT_STREQ("repertory_test_import.txt", i.file_name.c_str());
EXPECT_STREQ("AACeCiD6WQG6DzDcCdIu3cFPSxMUMoQPx46NYSyijNMKUA", i.skylink.c_str());
EXPECT_STREQ("", i.token.c_str());
};
{
auto export_with_failure =
provider_->export_list({"/repertory_test_import.txt", "/repertory_test_import2.txt"});
std::cout << export_with_failure.dump(2) << std::endl;
EXPECT_EQ(std::size_t(1), export_with_failure["failed"].size());
EXPECT_STREQ("/repertory_test_import2.txt",
export_with_failure["failed"][0u].get<std::string>().c_str());
test_success(export_with_failure);
}
{
auto export_all = provider_->export_all();
std::cout << export_all.dump(2) << std::endl;
EXPECT_TRUE(export_all["failed"].empty());
test_success(export_all);
}
provider_->remove_file("/repertory_test_import.txt");
}
TEST_F(skynet_provider_test, import_and_export_with_different_directory) {
api_meta_map directory_meta{};
EXPECT_EQ(api_error::success, provider_->get_item_meta("/", directory_meta));
skylink_import sl{"/test/sub", "", "AACeCiD6WQG6DzDcCdIu3cFPSxMUMoQPx46NYSyijNMKUA", ""};
EXPECT_EQ(api_error::success, provider_->import_skylink(sl));
EXPECT_TRUE(provider_->is_directory("/test"));
EXPECT_TRUE(provider_->is_directory("/test/sub"));
EXPECT_TRUE(provider_->is_file("/test/sub/repertory_test_import.txt"));
std::uint64_t file_size = 0;
EXPECT_EQ(api_error::success,
provider_->get_file_size("/test/sub/repertory_test_import.txt", file_size));
EXPECT_EQ(std::uint64_t(3), file_size);
api_meta_map meta;
EXPECT_EQ(api_error::success,
provider_->get_item_meta("/test/sub/repertory_test_import.txt", meta));
const auto id = json::parse(meta[META_ID]);
EXPECT_STREQ("AACeCiD6WQG6DzDcCdIu3cFPSxMUMoQPx46NYSyijNMKUA",
id["skylink"].get<std::string>().c_str());
const auto test_success = [](json &result) {
EXPECT_EQ(std::size_t(1), result["success"].size());
auto i = skylink_import::from_json(result["success"][0u]);
EXPECT_STREQ("/test/sub", i.directory.c_str());
EXPECT_STREQ("repertory_test_import.txt", i.file_name.c_str());
EXPECT_STREQ("AACeCiD6WQG6DzDcCdIu3cFPSxMUMoQPx46NYSyijNMKUA", i.skylink.c_str());
EXPECT_STREQ("", i.token.c_str());
};
{
auto export_with_failure = provider_->export_list(
{"/test/sub/repertory_test_import.txt", "/test/sub/repertory_test_import2.txt"});
std::cout << export_with_failure.dump(2) << std::endl;
EXPECT_EQ(std::size_t(1), export_with_failure["failed"].size());
EXPECT_STREQ("/test/sub/repertory_test_import2.txt",
export_with_failure["failed"][0u].get<std::string>().c_str());
test_success(export_with_failure);
}
{
auto export_all = provider_->export_all();
std::cout << export_all.dump(2) << std::endl;
EXPECT_TRUE(export_all["failed"].empty());
test_success(export_all);
}
provider_->remove_file("/test/sub/repertory_test_import.txt");
}
TEST_F(skynet_provider_test, import_directory) {
const auto expected_json_data = json::parse(
R"({ "filename": "skynet_test", "length": 36, "subfiles": { "sub_dir/test_03": { "contenttype": "application/octet-stream", "filename": "sub_dir/test_03", "len": 9, "offset": 18 }, "sub_dir/test_04": { "contenttype": "application/octet-stream", "filename": "sub_dir/test_04", "len": 9, "offset": 27 }, "test_01": { "contenttype": "application/octet-stream", "filename": "test_01", "len": 9 }, "test_02": { "contenttype": "application/octet-stream", "filename": "test_02", "len": 9, "offset": 9 } }, "tryfiles": [ "index.html" ] })");
api_meta_map directory_meta{};
EXPECT_EQ(api_error::success, provider_->get_item_meta("/", directory_meta));
skylink_import sl{"", "", EXTERNAL_SKYLINK, ""};
EXPECT_EQ(api_error::success, provider_->import_skylink(sl));
for (const auto &subFile : expected_json_data["subfiles"]) {
const auto api_path = utils::path::create_api_path(
utils::path::combine("/", {subFile["filename"].get<std::string>()}));
EXPECT_TRUE(provider_->is_file(api_path));
std::uint64_t file_size = 0;
EXPECT_EQ(api_error::success, provider_->get_file_size(api_path, file_size));
EXPECT_EQ(subFile["len"].get<std::uint64_t>(), file_size);
api_meta_map meta;
EXPECT_EQ(api_error::success, provider_->get_item_meta(api_path, meta));
const auto id = json::parse(meta[META_ID]);
const auto skylink = sl.skylink + api_path;
EXPECT_STREQ(skylink.c_str(), id["skylink"].get<std::string>().c_str());
}
}
TEST_F(skynet_provider_test, rename_file_fails_on_skylinks_with_directory_paths) {
api_meta_map directory_meta{};
EXPECT_EQ(api_error::success, provider_->get_item_meta("/", directory_meta));
EXPECT_EQ(api_error::success, provider_->create_directory("/test_rename2", {}));
EXPECT_EQ(api_error::success, provider_->create_directory("/test_rename2/static", {}));
EXPECT_EQ(api_error::success, provider_->create_directory("/test_rename2/static/css", {}));
skylink_import sl{"/test_rename", "", EXTERNAL_SKYLINK, ""};
EXPECT_EQ(api_error::success, provider_->import_skylink(sl));
if (not ::testing::Test::HasFailure()) {
EXPECT_EQ(api_error::access_denied,
provider_->rename_file("/test_rename/test_01", "/test_rename/test_01_"));
EXPECT_EQ(api_error::access_denied, provider_->rename_file("/test_rename/sub_dir/test_03",
"/test_rename/sub_dir/test_03_"));
EXPECT_EQ(api_error::access_denied,
provider_->rename_file("/test_rename/test_01", "/test_rename2/test_01_"));
EXPECT_EQ(api_error::access_denied, provider_->rename_file("/test_rename/sub_dir/test_03",
"/test_rename2/sub_dir/test_03_"));
EXPECT_EQ(api_error::success, provider_->create_directory("/test_rename/a", {}));
EXPECT_EQ(api_error::success, provider_->create_directory("/test_rename/sub_dir/b", {}));
EXPECT_EQ(api_error::access_denied,
provider_->rename_file("/test_rename/test_01", "/test_rename/a/test_01_"));
EXPECT_EQ(api_error::access_denied, provider_->rename_file("/test_rename/sub_dir/test_03",
"/test_rename/static/b/test_03_"));
}
}
TEST_F(skynet_provider_test, rename_file_succeeds_with_logical_directory_paths) {
api_meta_map directory_meta{};
EXPECT_EQ(api_error::success, provider_->get_item_meta("/", directory_meta));
EXPECT_EQ(api_error::success, provider_->create_directory("/test_rename2", {}));
EXPECT_EQ(api_error::success, provider_->create_directory("/test_rename2/sub_dir", {}));
skylink_import sl{"/test_rename", "", EXTERNAL_SKYLINK, ""};
EXPECT_EQ(api_error::success, provider_->import_skylink(sl));
if (not ::testing::Test::HasFailure()) {
EXPECT_EQ(api_error::success,
provider_->rename_file("/test_rename/test_01", "/test_rename2/test_01"));
EXPECT_EQ(api_error::success, provider_->rename_file("/test_rename/sub_dir/test_03",
"/test_rename2/sub_dir/test_03"));
}
}
TEST_F(skynet_provider_test, export_with_nested_directory_paths) {
api_meta_map directory_meta{};
EXPECT_EQ(api_error::success, provider_->get_item_meta("/", directory_meta));
skylink_import sl{"/test_export/nested", "", EXTERNAL_SKYLINK, ""};
EXPECT_EQ(api_error::success, provider_->import_skylink(sl));
if (not ::testing::Test::HasFailure()) {
const auto json_data = provider_->export_all();
for (const auto &e : json_data["success"]) {
EXPECT_STREQ("/test_export/nested", e["directory"].get<std::string>().c_str());
}
}
}
TEST_F(skynet_provider_test, export_with_nested_directory_paths_in_root) {
api_meta_map directory_meta{};
EXPECT_EQ(api_error::success, provider_->get_item_meta("/", directory_meta));
skylink_import sl{"/", "", EXTERNAL_SKYLINK, ""};
EXPECT_EQ(api_error::success, provider_->import_skylink(sl));
if (not ::testing::Test::HasFailure()) {
const auto json_data = provider_->export_all();
for (const auto &e : json_data["success"]) {
EXPECT_STREQ("/", e["directory"].get<std::string>().c_str());
}
}
}
TEST_F(skynet_provider_test, is_file_writeable_is_false_for_nested_skylinks) {
api_meta_map directory_meta{};
EXPECT_EQ(api_error::success, provider_->get_item_meta("/", directory_meta));
skylink_import sl{"/", "", EXTERNAL_SKYLINK, ""};
EXPECT_EQ(api_error::success, provider_->import_skylink(sl));
if (not ::testing::Test::HasFailure()) {
api_file_list list{};
provider_->get_file_list(list);
for (const auto &file : list) {
EXPECT_FALSE(provider_->is_file_writeable(file.api_path));
}
}
}
#ifdef REPERTORY_ENABLE_SKYNET_PREMIUM_TESTS
TEST_F(skynet_provider_test, authenticated_upload) {
app_config config(provider_type::skynet, "../..");
auto sk = config.get_skynet_config();
EXPECT_FALSE(sk.portal_list[0u].auth_user.empty());
sk.portal_list[0u].path = "/skynet/skyfile";
curl_comm comm(config);
const auto source_file = __FILE__;
const auto file_name = utils::path::strip_to_file_name(source_file);
auto id = utils::create_uuid_string();
utils::string::replace(id, "-", "");
json data, error;
auto sr = false;
EXPECT_EQ(api_error::success,
comm.post_multipart_file(sk.portal_list.at(0), "", file_name, source_file, "repertory",
data, error, sr));
std::cout << data.dump(2) << std::endl;
std::cout << error.dump(2) << std::endl;
}
TEST_F(skynet_provider_test, authenticated_upload_access_denied_with_invalid_username) {
app_config config(provider_type::skynet, "../..");
auto sk = config.get_skynet_config();
sk.portal_list[0u].auth_user = "cowaoeutnhsaoetuh@aosetuh.com";
sk.portal_list[0u].path = "/skynet/skyfile";
curl_comm comm(config);
const auto source_file = __FILE__;
const auto file_name = utils::path::strip_to_file_name(source_file);
auto id = utils::create_uuid_string();
utils::string::replace(id, "-", "");
json data, error;
auto sr = false;
EXPECT_EQ(api_error::access_denied,
comm.post_multipart_file(sk.portal_list.at(0), "", file_name, source_file, "repertory",
data, error, sr));
std::cout << data.dump(2) << std::endl;
std::cout << error.dump(2) << std::endl;
}
TEST_F(skynet_provider_test, authenticated_upload_access_denied_with_invalid_password) {
app_config config(provider_type::skynet, "../..");
auto sk = config.get_skynet_config();
EXPECT_FALSE(sk.portal_list[0u].auth_user.empty());
sk.portal_list[0u].auth_password = "1";
sk.portal_list[0u].path = "/skynet/skyfile";
curl_comm comm(config);
const auto source_file = __FILE__;
const auto file_name = utils::path::strip_to_file_name(source_file);
auto id = utils::create_uuid_string();
utils::string::replace(id, "-", "");
json data, error;
auto sr = false;
EXPECT_EQ(api_error::access_denied,
comm.post_multipart_file(sk.portal_list.at(0), "", file_name, source_file, "repertory",
data, error, sr));
std::cout << data.dump(2) << std::endl;
std::cout << error.dump(2) << std::endl;
}
#endif // REPERTORY_ENABLE_SKYNET_PREMIUM_TESTS
} // namespace repertory
// TODO Test import without encryption token when global encryption token is set
// TODO Test encrypted file import
// TODO Test encrypted directory import
#endif // REPERTORY_ENABLE_SKYNET

View File

@ -1 +0,0 @@
test_03

View File

@ -1 +0,0 @@
test_04

View File

@ -1 +0,0 @@
test_01

View File

@ -1 +0,0 @@
test_02

View File

@ -0,0 +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

View File

@ -1,36 +1,51 @@
/*
Copyright <2018-2022> <scott.e.graves@protonmail.com>
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
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 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.
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 {
native_file_ptr create_random_file(std::string path, const size_t &size) {
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));
std::vector<char> buf(size);
CryptoPP::OS_GenerateRandomBlock(false, reinterpret_cast<CryptoPP::byte *>(&buf[0u]),
buf.size());
data_buffer buf(size);
randombytes_buf(buf.data(), buf.size());
std::size_t bytes_written{};
nf->write_bytes(&buf[0u], buf.size(), 0u, bytes_written);
EXPECT_TRUE(nf->write_bytes(&buf[0u], buf.size(), 0u, bytes_written));
nf->flush();
std::uint64_t current_size;
@ -41,10 +56,25 @@ native_file_ptr create_random_file(std::string path, const size_t &size) {
return nf;
}
std::string generate_test_file_name(const std::string &directory,
const std::string &file_name_no_extension) {
static std::atomic<std::uint32_t> idx(0u);
return utils::path::absolute(utils::path::combine(
directory, {file_name_no_extension + utils::string::from_uint32(idx++) + ".dat"}));
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,20 +1,23 @@
/*
Copyright <2018-2022> <scott.e.graves@protonmail.com>
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
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 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.
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_
@ -23,14 +26,12 @@
#undef U
#endif
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "common.hpp"
#include "events/consumers/console_consumer.hpp"
#include "events/events.hpp"
#include "events/event_system.hpp"
#include "providers/passthrough/passthroughprovider.hpp"
#include "events/events.hpp"
#ifdef _WIN32
// Disable DLL-interface warnings
@ -43,48 +44,28 @@
#include "utils/native_file.hpp"
#define COMMA ,
#define RENTER_FILE_DATA(api_path, size) \
{ \
{ \
"file" COMMA { \
{"filesize" COMMA size} COMMA{"siapath" COMMA api_path} COMMA{ \
"redundancy" COMMA 1.0} COMMA{"available" COMMA false} COMMA{ \
"expiration" COMMA 0} COMMA{"ondisk" COMMA true} COMMA{ \
"recoverable" COMMA false} COMMA{"renewing" COMMA false} COMMA{ \
"localpath" COMMA \
".\\"} COMMA{"uploadedbytes" COMMA 0} COMMA{"uploadprogress" COMMA 0} COMMA{ \
"accesstime" COMMA "2019-02-21T02:24:37.653091916-06:00"} COMMA{ \
"changetime" COMMA "2019-02-21T02:24:37.653091916-06:00"} COMMA{ \
"createtime" COMMA "2019-02-21T02:24:37.653091916-06:00"} COMMA{ \
"modtime" COMMA "2019-02-21T02:24:37.653091916-06:00"} COMMA \
} \
} \
}
#define RENTER_DIR_DATA(api_path) \
{ \
{ \
"directories", { \
{ \
{"siapath", api_path}, {"numfiles", 0}, { "numsubdirs", 0 } \
} \
} \
} \
}
using ::testing::_;
using namespace ::testing;
namespace repertory {
native_file_ptr create_random_file(std::string path, const size_t &size);
[[nodiscard]] auto create_random_file(std::string path, std::size_t size)
-> native_file_ptr;
std::string generate_test_file_name(const std::string &directory,
const std::string &file_name_no_extension);
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) {
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,25 +1,29 @@
/*
Copyright <2018-2022> <scott.e.graves@protonmail.com>
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
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 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.
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 {
@ -61,33 +65,38 @@ public:
l.unlock();
for (size_t i = 0; i < non_fired_event_names_.size(); i++) {
EXPECT_EQ(non_fired_event_names_.end(),
std::find(non_fired_event_names_.begin(), non_fired_event_names_.end(),
event.get_name()));
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() < 5)) {
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();
}
bool wait_for_event(const std::string &event_name) {
[[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() < 5) &&
(missing = (std::find(fired_event_names_.begin(), fired_event_names_.end(),
event_name) == fired_event_names_.end()))) {
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();

View File

@ -1,24 +1,27 @@
/*
Copyright <2018-2022> <scott.e.graves@protonmail.com>
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
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 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.
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/skynet.hpp"
#include "utils/file_utils.hpp"
#include "utils/path_utils.hpp"
#include "utils/string_utils.hpp"
#include "utils/utils.hpp"
@ -27,33 +30,38 @@ namespace repertory {
#ifdef _WIN32
TEST(utils, convert_api_date) {
LARGE_INTEGER li{};
li.QuadPart = utils::convert_api_date("2019-02-21T03:24:37.653091916-06:00");
li.QuadPart = utils::convert_api_date("2009-10-12T17:50:30.111Z");
SYSTEMTIME st{};
FileTimeToSystemTime((FILETIME *)&li, &st);
FileTimeToSystemTime(reinterpret_cast<FILETIME *>(&li), &st);
SYSTEMTIME lt{};
SystemTimeToTzSpecificLocalTime(nullptr, &st, &lt);
EXPECT_EQ(2019, lt.wYear);
EXPECT_EQ(2, lt.wMonth);
EXPECT_EQ(21, lt.wDay);
EXPECT_EQ(2009, lt.wYear);
EXPECT_EQ(10, lt.wMonth);
EXPECT_EQ(12, lt.wDay);
EXPECT_EQ(3, lt.wHour);
EXPECT_EQ(24, lt.wMinute);
EXPECT_EQ(37, lt.wSecond);
EXPECT_EQ(653, lt.wMilliseconds);
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(36, uuid1.size());
ASSERT_EQ(36, uuid2.size());
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"));

View File

@ -1,22 +1,26 @@
/*
Copyright <2018-2022> <scott.e.graves@protonmail.com>
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
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 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.
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 {

View File

@ -1,27 +1,32 @@
/*
Copyright <2018-2022> <scott.e.graves@protonmail.com>
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
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 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.
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 "fixtures/winfsp_fixture.hpp"
#include "utils/event_capture.hpp"
#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{};
@ -29,9 +34,10 @@ void launch_app(std::string cmd) {
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()) + ")");
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);
@ -44,70 +50,25 @@ void launch_app(std::string cmd) {
}
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***********************")
#define TEST_HEADER(func) \
event_system::instance().raise<test_begin>( \
std::string(func) + \
"\r\n***********************\r\n***********************")
static auto mount_setup(const std::size_t &idx, winfsp_test *test, std::string &mount_point) {
// Mock communicator setup
if (idx == 0) {
auto &mock_comm = test->mock_sia_comm_;
mock_comm.push_return("get", "/wallet", {}, {}, api_error::success, true);
mock_comm.push_return("get", "/renter/dir/",
{{"directories", {{{"numfiles", 0}, {"numsubdirs", 0}}}}}, {},
api_error::success, true);
mock_comm.push_return("get", "/renter/file/", {}, {{"message", "no file known"}},
api_error::comm_error, true);
mock_comm.push_return("get", "/renter/dir/autorun.inf", {}, {{"message", "no file known"}},
api_error::comm_error, true);
mock_comm.push_return("get", "/renter/dir/.git", {}, {{"message", "no file known"}},
api_error::comm_error, true);
mock_comm.push_return("get", "/renter/file/.git", {}, {{"message", "no file known"}},
api_error::comm_error, true);
mock_comm.push_return("get", "/renter/dir/HEAD", {}, {{"message", "no file known"}},
api_error::comm_error, true);
mock_comm.push_return("get", "/renter/file/HEAD", {}, {{"message", "no file known"}},
api_error::comm_error, true);
mock_comm.push_return("get", "/renter/file/autorun.inf", {}, {{"message", "no file known"}},
api_error::comm_error, true);
mock_comm.push_return("get", "/renter/dir/AutoRun.inf", {}, {{"message", "no file known"}},
api_error::comm_error, true);
mock_comm.push_return("get", "/renter/file/AutoRun.inf", {}, {{"message", "no file known"}},
api_error::comm_error, true);
mock_comm.push_return("get", "/renter/dir/Desktop.ini", {}, {{"message", "no file known"}},
api_error::comm_error, true);
mock_comm.push_return("get", "/renter/file/Desktop.ini", {}, {{"message", "no file known"}},
api_error::comm_error, true);
json files;
files["files"] = std::vector<json>();
mock_comm.push_return("get", "/renter/files", files, {}, api_error::success, true);
mock_comm.push_return("get", "/renter/prices", {}, {{"message", "offline"}},
api_error::comm_error, true);
mock_comm.push_return(
"get", "/daemon/version",
{{"version", app_config::get_provider_minimum_version(provider_type::sia)}}, {},
api_error::success, true);
// Mount setup
mount_point = "U:";
return std::vector<std::string>({"unittests", "-f", mount_point});
}
if (idx == 1) {
// Mount setup
mount_point = "V:";
return std::vector<std::string>({"unittests", "-f", mount_point});
}
static auto mount_setup(std::string &mount_point) {
mount_point = "U:";
return std::vector<std::string>({"unittests", "-f", mount_point});
}
static void execute_mount(const std::size_t &idx, winfsp_test *test,
const std::vector<std::string> &driveArgs, std::thread &th) {
ASSERT_EQ(0, std::get<2>(test->provider_tests_[idx])->mount(driveArgs));
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(const std::size_t &idx, winfsp_test *test, const std::string &mount_point) {
std::get<2>(test->provider_tests_[idx])->shutdown();
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);
@ -119,31 +80,19 @@ static void unmount(const std::size_t &idx, winfsp_test *test, const std::string
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_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(const std::size_t &idx, winfsp_test *test, const std::string &mount_point) {
static auto create_test(winfsp_test *test, const std::string &mount_point) {
TEST_HEADER(__FUNCTION__);
if (idx == 0) {
auto &mock_comm = test->mock_sia_comm_;
mock_comm.push_return("get", "/renter/dir/test_create.txt", {}, {{"message", "no file known"}},
api_error::comm_error, true);
mock_comm.push_return("get", "/renter/file/test_create.txt", {}, {{"message", "no file known"}},
api_error::comm_error);
mock_comm.push_return("post_params", "/renter/delete/test_create.txt", {},
{{"message", "no file known"}}, api_error::comm_error);
mock_comm.push_return("post_params", "/renter/upload/test_create.txt", {}, {},
api_error::success);
mock_comm.push_return("get", "/renter/file/test_create.txt",
RENTER_FILE_DATA("test_create.txt", 0), {}, api_error::success, true);
}
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);
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));
@ -154,157 +103,49 @@ static auto create_test(const std::size_t &idx, winfsp_test *test, const std::st
EXPECT_EQ(0, file_size);
std::string attr;
EXPECT_EQ(api_error::success, std::get<1>(test->provider_tests_[idx])
->get_item_meta("/test_create.txt", META_ATTRIBUTES, 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));
event_system::instance().stop();
event_system::instance().start();
if (idx == 0) {
auto &mock_comm = test->mock_sia_comm_;
mock_comm.remove_return("get", "/renter/dir/test_create.txt");
mock_comm.remove_return("post_params", "/renter/delete/test_create.txt");
mock_comm.remove_return("post_params", "/renter/upload/test_create.txt");
mock_comm.remove_return("get", "/renter/file/test_create.txt");
}
return file;
}
static void delete_file_test(const std::size_t &idx, winfsp_test *test, const std::string &file) {
static void delete_file_test(const std::string &file) {
TEST_HEADER(__FUNCTION__);
if (idx == 0) {
auto &mock_comm = test->mock_sia_comm_;
mock_comm.push_return("get", "/renter/dir/test_create.txt", {}, {{"message", "no file known"}},
api_error::comm_error, true);
mock_comm.push_return("get", "/renter/file/test_create.txt",
RENTER_FILE_DATA("test_create.txt", 0), {}, api_error::success);
mock_comm.push_return("get", "/renter/file/test_create.txt",
RENTER_FILE_DATA("test_create.txt", 0), {}, api_error::success);
mock_comm.push_return("get", "/renter/file/test_create.txt",
RENTER_FILE_DATA("test_create.txt", 0), {}, api_error::success);
mock_comm.push_return("get", "/renter/file/test_create.txt",
RENTER_FILE_DATA("test_create.txt", 0), {}, api_error::success);
mock_comm.push_return("get", "/renter/file/test_create.txt", {}, {{"message", "no file known"}},
api_error::comm_error, true);
mock_comm.push_return("post_params", "/renter/delete/test_create.txt", {}, {},
api_error::success);
}
{
event_capture ec({"file_removed"});
EXPECT_TRUE(utils::file::delete_file(file));
EXPECT_FALSE(utils::file::is_file(file));
event_system::instance().stop();
event_system::instance().start();
}
if (idx == 0) {
auto &mock_comm = test->mock_sia_comm_;
mock_comm.remove_return("post_params", "/renter/delete/test_create.txt");
}
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::size_t &idx, winfsp_test *test,
const std::string &directory) {
static void create_directory_test(const std::string &directory) {
TEST_HEADER(__FUNCTION__);
if (idx == 0) {
auto &mock_comm = test->mock_sia_comm_;
mock_comm.push_return("get", "/renter/dir/" + directory.substr(3), {},
{{"message", "no file known"}}, api_error::comm_error);
mock_comm.push_return("get", "/renter/dir/" + directory.substr(3), {},
{{"message", "no file known"}}, api_error::comm_error);
mock_comm.push_return("get", "/renter/file/" + directory.substr(3), {},
{{"message", "no file known"}}, api_error::comm_error, true);
mock_comm.push_return("get", "/renter/dir/" + directory.substr(3),
{{"directories", {{{"numfiles", 0}, {"numsubdirs", 0}}}}}, {},
api_error::success, true);
mock_comm.push_return("post_params", "/renter/dir/" + directory.substr(3), {}, {},
api_error::success);
}
EXPECT_FALSE(::PathIsDirectory(&directory[0]));
EXPECT_TRUE(::CreateDirectoryA(&directory[0], nullptr));
EXPECT_TRUE(::PathIsDirectory(&directory[0]));
if (idx == 0) {
auto &mock_comm = test->mock_sia_comm_;
mock_comm.remove_return("get", "/renter/dir/" + directory.substr(3));
mock_comm.remove_return("get", "/renter/file/" + directory.substr(3));
mock_comm.remove_return("post_params", "/renter/dir/" + directory.substr(3));
}
}
static void remove_directory_test(const std::size_t &idx, winfsp_test *test,
const std::string &directory) {
static void remove_directory_test(const std::string &directory) {
TEST_HEADER(__FUNCTION__);
if (idx == 0) {
auto &mock_comm = test->mock_sia_comm_;
mock_comm.push_return("get", "/renter/dir/" + directory.substr(3),
{{"directories", {{{"numfiles", 0}, {"numsubdirs", 0}}}}}, {},
api_error::success);
mock_comm.push_return("get", "/renter/dir/" + directory.substr(3),
{{"directories", {{{"numfiles", 0}, {"numsubdirs", 0}}}}}, {},
api_error::success);
mock_comm.push_return("get", "/renter/dir/" + directory.substr(3),
{{"directories", {{{"numfiles", 0}, {"numsubdirs", 0}}}}}, {},
api_error::success);
mock_comm.push_return("get", "/renter/dir/" + directory.substr(3),
{{"directories", {{{"numfiles", 0}, {"numsubdirs", 0}}}}}, {},
api_error::success);
mock_comm.push_return("post_params", "/renter/dir/" + directory.substr(3), {}, {},
api_error::success);
mock_comm.push_return("get", "/renter/dir/" + directory.substr(3), {},
{{"message", "no file known"}}, api_error::comm_error, true);
mock_comm.push_return("get", "/renter/file/" + directory.substr(3), {},
{{"message", "no file known"}}, api_error::comm_error, true);
}
{
event_capture ec({"directory_removed"});
EXPECT_TRUE(::PathIsDirectory(&directory[0]));
EXPECT_TRUE(::RemoveDirectoryA(&directory[0]));
EXPECT_FALSE(::PathIsDirectory(&directory[0]));
event_system::instance().stop();
event_system::instance().start();
}
if (idx == 0) {
auto &mock_comm = test->mock_sia_comm_;
mock_comm.remove_return("post_params", "/renter/dir/" + directory.substr(3));
}
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::size_t &idx, winfsp_test *test,
const std::string &mount_point) {
static void write_file_test(const std::string &mount_point) {
TEST_HEADER(__FUNCTION__);
if (idx == 0) {
auto &mock_comm = test->mock_sia_comm_;
mock_comm.push_return("get", "/renter/dir/",
{{"directories", {{{"numfiles", 0}, {"numsubdirs", 0}}}}}, {},
api_error::success, true);
mock_comm.push_return("get", "/renter/dir/test_write.txt", {}, {{"message", "no file known"}},
api_error::comm_error, true);
mock_comm.push_return("get", "/renter/file/test_write.txt", {}, {{"message", "no file known"}},
api_error::comm_error);
mock_comm.push_return("post_params", "/renter/upload/test_write.txt", {}, {},
api_error::success);
mock_comm.push_return("post_params", "/renter/upload/test_write.txt", {}, {},
api_error::success);
mock_comm.push_return("get", "/renter/file/test_write.txt",
RENTER_FILE_DATA("test_write.txt", 0), {}, api_error::success);
mock_comm.push_return("get", "/renter/file/test_write.txt",
RENTER_FILE_DATA("test_write.txt", 10), {}, api_error::success, true);
}
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);
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_TRUE(::WriteFile(handle, &data[0], static_cast<DWORD>(data.size()),
&bytes_written, nullptr));
EXPECT_EQ(10, bytes_written);
EXPECT_TRUE(::CloseHandle(handle));
@ -313,185 +154,80 @@ static void write_file_test(const std::size_t &idx, winfsp_test *test,
std::uint64_t file_size;
EXPECT_TRUE(utils::file::get_file_size(file, file_size));
EXPECT_EQ(10, file_size);
event_system::instance().stop();
event_system::instance().start();
if (idx == 0) {
auto &mock_comm = test->mock_sia_comm_;
mock_comm.remove_return("get", "/renter/dir/");
mock_comm.remove_return("post_params", "/renter/upload/test_write.txt");
}
}
static void read_file_test(const std::size_t &idx, winfsp_test *test,
const std::string &mount_point) {
static void read_file_test(const std::string &mount_point) {
TEST_HEADER(__FUNCTION__);
if (idx == 0) {
auto &mock_comm = test->mock_sia_comm_;
mock_comm.push_return("get", "/renter/dir/",
{{"directories", {{{"numfiles", 0}, {"numsubdirs", 0}}}}}, {},
api_error::success, true);
mock_comm.push_return("get", "/renter/dir/test_read.txt", {}, {{"message", "no file known"}},
api_error::comm_error, true);
mock_comm.push_return("get", "/renter/file/test_read.txt", {}, {{"message", "no file known"}},
api_error::comm_error);
mock_comm.push_return("post_params", "/renter/upload/test_read.txt", {}, {}, api_error::success,
true);
mock_comm.push_return("get", "/renter/file/test_read.txt", RENTER_FILE_DATA("test_read.txt", 0),
{}, api_error::success);
mock_comm.push_return("get", "/renter/file/test_read.txt",
RENTER_FILE_DATA("test_read.txt", 10), {}, api_error::success, true);
}
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);
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_TRUE(::WriteFile(handle, &data[0], static_cast<DWORD>(data.size()),
&bytes_written, nullptr));
EXPECT_EQ(10, bytes_written);
std::vector<char> data2;
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_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));
event_system::instance().stop();
event_system::instance().start();
}
static void rename_file_test(const std::size_t &idx, winfsp_test *test,
static void rename_file_test(winfsp_test *test,
const std::string &mount_point) {
TEST_HEADER(__FUNCTION__);
if (idx == 0) {
auto &mock_comm = test->mock_sia_comm_;
mock_comm.push_return("get", "/renter/dir/rename_file.txt", {}, {{"message", "no file known"}},
api_error::comm_error, true);
mock_comm.push_return("get", "/renter/file/rename_file.txt", {}, {{"message", "no file known"}},
api_error::comm_error);
mock_comm.push_return("post_params", "/renter/upload/rename_file.txt", {}, {},
api_error::success);
mock_comm.push_return("get", "/renter/file/rename_file.txt",
RENTER_FILE_DATA("rename_file.txt", 0), {}, api_error::success, true);
mock_comm.push_return("get", "/renter/dir/rename_file2.txt", {}, {{"message", "no file known"}},
api_error::comm_error, true);
mock_comm.push_return("get", "/renter/file/rename_file2.txt", {},
{{"message", "no file known"}}, api_error::comm_error);
mock_comm.push_return("get", "/renter/file/rename_file2.txt", {},
{{"message", "no file known"}}, api_error::comm_error);
mock_comm.push_return("get", "/renter/file/rename_file2.txt", {},
{{"message", "no file known"}}, api_error::comm_error);
mock_comm.push_return("post_params", "/renter/rename/rename_file.txt", {}, {},
api_error::success);
}
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);
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));
auto &provider = *std::get<1>(test->provider_tests_[idx]);
api_meta_map meta1{};
EXPECT_EQ(api_error::success, provider.get_item_meta("/rename_file.txt", 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]));
if (idx == 0) {
auto &mock_comm = test->mock_sia_comm_;
mock_comm.remove_return("get", "/renter/file/rename_file.txt");
mock_comm.push_return("get", "/renter/file/rename_file.txt", {}, {{"message", "no file known"}},
api_error::comm_error, true);
mock_comm.push_return("get", "/renter/file/rename_file2.txt",
RENTER_FILE_DATA("rename_file2.txt", 0), {}, api_error::success, true);
}
EXPECT_TRUE(utils::file::is_file(file2));
EXPECT_FALSE(utils::file::is_file(file));
api_meta_map meta2{};
EXPECT_EQ(api_error::success, provider.get_item_meta("/rename_file2.txt", 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, provider.get_filesystem_item("/rename_file2.txt", false, 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,
provider.get_filesystem_item_from_source_path(fsi.source_path, fsi2));
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, provider.get_item_meta("/rename_file.txt", meta2));
if (idx == 0) {
auto &mock_comm = test->mock_sia_comm_;
mock_comm.remove_return("post_params", "/renter/upload/rename_file.txt");
mock_comm.remove_return("post_params", "/renter/rename/rename_file.txt");
}
EXPECT_EQ(api_error::item_not_found,
test->provider->get_item_meta("/rename_file.txt", meta2));
}
static void rename_directory_test(const std::size_t &idx, winfsp_test *test,
const std::string &mount_point) {
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});
if (idx == 0) {
auto &mock_comm = test->mock_sia_comm_;
mock_comm.push_return("get", "/renter/dir/" + directory, {}, {{"message", "no file known"}},
api_error::comm_error);
mock_comm.push_return("get", "/renter/dir/" + directory, {}, {{"message", "no file known"}},
api_error::comm_error);
mock_comm.push_return("get", "/renter/file/" + directory, {}, {{"message", "no file known"}},
api_error::comm_error, true);
mock_comm.push_return("get", "/renter/dir/" + directory2, {}, {{"message", "no file known"}},
api_error::comm_error);
mock_comm.push_return("get", "/renter/dir/" + directory2, {}, {{"message", "no file known"}},
api_error::comm_error);
mock_comm.push_return("get", "/renter/dir/" + directory2, {}, {{"message", "no file known"}},
api_error::comm_error);
mock_comm.push_return("get", "/renter/file/" + directory2, {}, {{"message", "no file known"}},
api_error::comm_error, true);
mock_comm.push_return("get", "/renter/dir/" + directory, RENTER_DIR_DATA(directory), {},
api_error::success);
mock_comm.push_return("get", "/renter/dir/" + directory, RENTER_DIR_DATA(directory), {},
api_error::success);
mock_comm.push_return("get", "/renter/dir/" + directory, RENTER_DIR_DATA(directory), {},
api_error::success);
mock_comm.push_return("get", "/renter/dir/" + directory, RENTER_DIR_DATA(directory), {},
api_error::success);
mock_comm.push_return("get", "/renter/dir/" + directory, RENTER_DIR_DATA(directory), {},
api_error::success);
mock_comm.push_return("get", "/renter/dir/" + directory, RENTER_DIR_DATA(directory), {},
api_error::success);
mock_comm.push_return("get", "/renter/dir/" + directory, RENTER_DIR_DATA(directory), {},
api_error::success);
mock_comm.push_return("get", "/renter/dir/" + directory, RENTER_DIR_DATA(directory), {},
api_error::success);
mock_comm.push_return("post_params", "/renter/dir/" + directory, {}, {}, api_error::success);
mock_comm.push_return("post_params", "/renter/dir/" + directory, {}, {}, api_error::success);
mock_comm.push_return("post_params", "/renter/dir/" + directory2, {}, {}, api_error::success);
mock_comm.push_return("get", "/renter/dir/" + directory2, RENTER_DIR_DATA(directory2), {},
api_error::success, true);
mock_comm.push_return("get", "/renter/dir/" + directory, {}, {{"message", "no file known"}},
api_error::comm_error, true);
}
EXPECT_FALSE(::PathIsDirectory(&full_directory[0]));
EXPECT_TRUE(::CreateDirectoryA(&full_directory[0], nullptr));
@ -499,36 +235,16 @@ static void rename_directory_test(const std::size_t &idx, winfsp_test *test,
EXPECT_TRUE(::MoveFile(&full_directory[0], &full_directory2[0]));
EXPECT_FALSE(::PathIsDirectory(&full_directory[0]));
EXPECT_TRUE(::PathIsDirectory(&full_directory2[0]));
if (idx == 0) {
auto &mock_comm = test->mock_sia_comm_;
mock_comm.remove_return("post_params", "/renter/dir/" + directory);
mock_comm.remove_return("get", "/renter/dir/" + directory2);
mock_comm.remove_return("get", "/renter/file/" + directory2);
mock_comm.remove_return("post_params", "/renter/dir/" + directory2);
}
}
static void get_set_basic_info_test(const std::size_t &idx, winfsp_test *test,
const std::string &mount_point) {
static void get_set_basic_info_test(const std::string &mount_point) {
TEST_HEADER(__FUNCTION__);
if (idx == 0) {
auto &mock_comm = test->mock_sia_comm_;
mock_comm.push_return("get", "/renter/dir/setbasicinfo_file.txt", {},
{{"message", "no file known"}}, api_error::comm_error, true);
mock_comm.push_return("get", "/renter/file/setbasicinfo_file.txt", {},
{{"message", "no file known"}}, api_error::comm_error);
mock_comm.push_return("post_params", "/renter/upload/setbasicinfo_file.txt", {}, {},
api_error::success, true);
mock_comm.push_return("get", "/renter/file/setbasicinfo_file.txt",
RENTER_FILE_DATA("setbasicinfo_file.txt", 0), {}, api_error::success,
true);
}
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);
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{};
@ -559,126 +275,110 @@ static void get_set_basic_info_test(const std::size_t &idx, winfsp_test *test,
fbi.LastAccessTime = *(LARGE_INTEGER *)&test_la_time;
fbi.LastWriteTime = *(LARGE_INTEGER *)&test_lw_time;
EXPECT_TRUE(::SetFileInformationByHandle(handle, FileBasicInfo, &fbi, sizeof(FILE_BASIC_INFO)));
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_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;
<< 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;
<< fbi2.CreationTime.QuadPart << " " << fbi2.LastAccessTime.QuadPart
<< " " << fbi2.LastWriteTime.QuadPart << std::endl;
EXPECT_TRUE(::CloseHandle(handle));
}
static void overwrite_file_test(const std::size_t &idx, winfsp_test *test,
const std::string &mount_point) {
static void overwrite_file_test(const std::string &mount_point) {
TEST_HEADER(__FUNCTION__);
if (idx == 0) {
auto &mock_comm = test->mock_sia_comm_;
mock_comm.push_return("get", "/renter/dir/test_overwrite.txt", {},
{{"message", "no file known"}}, api_error::comm_error, true);
mock_comm.push_return("get", "/renter/dir/test_overwrite2.txt", {},
{{"message", "no file known"}}, api_error::comm_error, true);
mock_comm.push_return("get", "/renter/file/test_overwrite.txt", {},
{{"message", "no file known"}}, api_error::comm_error);
mock_comm.push_return("post_params", "/renter/upload/test_overwrite.txt", {}, {},
api_error::success, true);
mock_comm.push_return("post_params", "/renter/upload/test_overwrite2.txt", {}, {},
api_error::success, true);
mock_comm.push_return("post_params", "/renter/delete/test_overwrite2.txt", {}, {},
api_error::success, true);
}
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);
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_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"});
if (idx == 0) {
auto &mock_comm = test->mock_sia_comm_;
mock_comm.push_return("get", "/renter/file/test_overwrite2.txt", {},
{{"message", "no file known"}}, api_error::comm_error);
mock_comm.push_return("get", "/renter/file/test_overwrite.txt",
RENTER_FILE_DATA("test_overwrite.txt", 0), {}, api_error::success,
true);
mock_comm.push_return("get", "/renter/file/test_overwrite2.txt",
RENTER_FILE_DATA("test_overwrite2.txt", 0), {}, api_error::success,
true);
}
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));
}
}
if (idx == 0) {
auto &mock_comm = test->mock_sia_comm_;
mock_comm.remove_return("post_params", "/renter/upload/test_overwrite.txt");
mock_comm.remove_return("post_params", "/renter/upload/test_overwrite2.txt");
}
}
TEST_F(winfsp_test, all_tests) {
if (PROVIDER_INDEX == 0) {
for (std::size_t idx = 0; idx < provider_tests_.size(); 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"));
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"));
}
} else {
const auto idx = PROVIDER_INDEX - 1;
std::string mount_point;
const auto drive_args = mount_setup(idx, this, mount_point);
event_capture ec({"drive_mounted"});
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(idx, this, mount_point);
delete_file_test(idx, this, file);
}
{
const auto testDir = utils::path::combine(mount_point, {"TestDir"});
create_directory_test(idx, this, testDir);
remove_directory_test(idx, this, testDir);
}
write_file_test(idx, this, mount_point);
read_file_test(idx, this, mount_point);
rename_file_test(idx, this, mount_point);
rename_directory_test(idx, this, mount_point);
overwrite_file_test(idx, this, mount_point);
get_set_basic_info_test(idx, this, mount_point);
}
if (mounted) {
unmount(idx, this, mount_point);
}
});
execute_mount(idx, this, drive_args, th);
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