From 534ae9efa8339272beab6082a5b1c2a825e1b50e Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Wed, 4 Jan 2023 19:24:14 +0100 Subject: [PATCH] add test for write --- CMakeLists.txt | 1 + src/webfuse/provider.cpp | 14 ++++++++ test-src/integration/test_write.cpp | 53 +++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 test-src/integration/test_write.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index ae5f8e7..bbdfa06 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -86,6 +86,7 @@ if(NOT(WITHOUT_TEST)) test-src/integration/test_mknod.cpp test-src/integration/test_unlink.cpp test-src/integration/test_read.cpp + test-src/integration/test_write.cpp ) target_include_directories(integration_tests PRIVATE test-src/integration ${GTEST_INCLUDE_DIRS} ${GMOCK_INCLUDE_DIRS}) diff --git a/src/webfuse/provider.cpp b/src/webfuse/provider.cpp index 6abe535..01a56e2 100644 --- a/src/webfuse/provider.cpp +++ b/src/webfuse/provider.cpp @@ -106,6 +106,9 @@ public: case request_type::read: fs_read(reader, writer); break; + case request_type::write: + fs_write(reader, writer); + break; case request_type::readdir: fs_readdir(reader, writer); break; @@ -303,6 +306,17 @@ private: } } + void fs_write(messagereader & reader, messagewriter & writer) + { + auto const path = reader.read_str(); + auto const buffer = reader.read_bytes(); + auto const offset = reader.read_u64(); + auto const handle = reader.read_u64(); + + auto const result = fs_.write(path, buffer.c_str(), buffer.size(), offset, handle); + writer.write_i32(result); + } + void fs_readdir(messagereader & reader, messagewriter & writer) { auto const path = reader.read_str(); diff --git a/test-src/integration/test_write.cpp b/test-src/integration/test_write.cpp new file mode 100644 index 0000000..c53df32 --- /dev/null +++ b/test-src/integration/test_write.cpp @@ -0,0 +1,53 @@ +#include "webfuse/webfuse.hpp" +#include "webfuse/test/fixture.hpp" +#include "webfuse/test/filesystem_mock.hpp" + +#include +#include +#include + + +using testing::_; +using testing::Return; +using testing::Invoke; +using testing::AnyNumber; +using testing::AtMost; + +TEST(write, success) +{ + webfuse::filesystem_mock fs; + EXPECT_CALL(fs, access("/",_)).Times(AnyNumber()).WillRepeatedly(Return(0)); + EXPECT_CALL(fs, getattr(_,_)).WillRepeatedly(Invoke([](std::string const & path, struct stat * attr){ + memset(reinterpret_cast(attr),0, sizeof(struct stat)); + + if (path == "/") + { + attr->st_nlink = 1; + attr->st_mode = S_IFDIR | 0755; + return 0; + } + if (path == "/some_file") + { + attr->st_nlink = 1; + attr->st_mode = S_IFREG | 0644; + return 0; + } + else + { + return -ENOENT; + } + })); + EXPECT_CALL(fs, open("/some_file", _, _)).WillOnce(Return(0)); + EXPECT_CALL(fs, write("/some_file", _, _, _, _)).Times(AnyNumber()).WillRepeatedly(Invoke([](auto const &, char const * buffer, size_t buffer_size, uint64_t offset, auto){ + return buffer_size; + })); + EXPECT_CALL(fs, release("/some_file", _)).Times(AtMost(1)).WillOnce(Return(0)); + + webfuse::fixture fixture(fs); + auto const path = fixture.get_path() + "/some_file"; + + { + std::ofstream out(path); + out << "Test"; + } +}