initial commit
Some checks failed
BlockStorage/repertory_osx/pipeline/head There was a failure building this commit
BlockStorage/repertory_windows/pipeline/head This commit looks good
BlockStorage/repertory_linux_builds/pipeline/head This commit looks good

This commit is contained in:
2022-03-05 00:30:50 -06:00
commit 3ff46723b8
626 changed files with 178600 additions and 0 deletions

176
tests/mocks/mock_comm.hpp Normal file
View File

@ -0,0 +1,176 @@
/*
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

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

View File

@ -0,0 +1,70 @@
/*
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

@ -0,0 +1,84 @@
/*
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_MOCKPROVIDER_HPP_
#define TESTS_MOCKS_MOCKPROVIDER_HPP_
#include "providers/i_provider.hpp"
#include "test_common.hpp"
namespace repertory {
class mock_provider : public virtual i_provider {
public:
mock_provider(const 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());
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));
};
} // namespace repertory
#endif // TESTS_MOCKS_MOCKPROVIDER_HPP_

View File

@ -0,0 +1,80 @@
/*
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_S3_COMM_HPP_
#define TESTS_MOCKS_MOCK_S3_COMM_HPP_
#ifdef REPERTORY_ENABLE_S3_TESTING
#include "test_common.hpp"
#include "comm/i_s3_comm.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)); */
/* }; */
} // namespace repertory
#endif // REPERTORY_ENABLE_S3_TESTING
#endif // TESTS_MOCKS_MOCK_S3_COMM_HPP_

View File

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