1
0
mirror of https://github.com/falk-werner/webfuse synced 2024-10-27 20:34:10 +00:00

add test for symlink

This commit is contained in:
Falk Werner 2023-01-01 17:05:42 +01:00
parent d75cc0736b
commit 761b6edb05
4 changed files with 103 additions and 13 deletions

View File

@ -74,6 +74,7 @@ if(NOT(WITHOUT_TEST))
test-src/integration/test_access.cpp test-src/integration/test_access.cpp
test-src/integration/test_readdir.cpp test-src/integration/test_readdir.cpp
test-src/integration/test_readlink.cpp test-src/integration/test_readlink.cpp
test-src/integration/test_symlink.cpp
) )
target_include_directories(integration_tests PRIVATE test-src/integration ${GTEST_INCLUDE_DIRS} ${GMOCK_INCLUDE_DIRS}) target_include_directories(integration_tests PRIVATE test-src/integration ${GTEST_INCLUDE_DIRS} ${GMOCK_INCLUDE_DIRS})

View File

@ -64,6 +64,9 @@ public:
case request_type::readlink: case request_type::readlink:
fs_readlink(reader, writer); fs_readlink(reader, writer);
break; break;
case request_type::symlink:
fs_symlink(reader, writer);
break;
case request_type::readdir: case request_type::readdir:
fs_readdir(reader, writer); fs_readdir(reader, writer);
break; break;
@ -97,19 +100,6 @@ private:
} }
} }
void fs_readdir(messagereader & reader, messagewriter & writer)
{
auto const path = reader.read_str();
std::vector<std::string> entries;
auto const result = fs_.readdir(path, entries, static_cast<uint64_t>(-1));
writer.write_i32(result);
if (0 == result)
{
writer.write_strings(entries);
}
}
void fs_readlink(messagereader & reader, messagewriter & writer) void fs_readlink(messagereader & reader, messagewriter & writer)
{ {
auto const path = reader.read_str(); auto const path = reader.read_str();
@ -123,6 +113,28 @@ private:
} }
} }
void fs_symlink(messagereader & reader, messagewriter & writer)
{
auto const from = reader.read_str();
auto const to = reader.read_str();
auto const result = fs_.symlink(from, to);
writer.write_i32(result);
}
void fs_readdir(messagereader & reader, messagewriter & writer)
{
auto const path = reader.read_str();
std::vector<std::string> entries;
auto const result = fs_.readdir(path, entries, static_cast<uint64_t>(-1));
writer.write_i32(result);
if (0 == result)
{
writer.write_strings(entries);
}
}
filesystem_i & fs_; filesystem_i & fs_;
ws_client client; ws_client client;
}; };

View File

@ -2,6 +2,9 @@
#include "webfuse/test/fixture.hpp" #include "webfuse/test/fixture.hpp"
#include "webfuse/test/filesystem_mock.hpp" #include "webfuse/test/filesystem_mock.hpp"
#include <gtest/gtest.h>
using testing::_; using testing::_;
using testing::Return; using testing::Return;
using testing::Invoke; using testing::Invoke;

View File

@ -0,0 +1,74 @@
#include "webfuse/webfuse.hpp"
#include "webfuse/test/fixture.hpp"
#include "webfuse/test/filesystem_mock.hpp"
#include <gtest/gtest.h>
using testing::_;
using testing::Return;
using testing::Invoke;
using testing::AnyNumber;
TEST(symlink, sucessfully_create_symlink)
{
bool link_created = false;
webfuse::filesystem_mock fs;
EXPECT_CALL(fs, access("/",_)).Times(AnyNumber()).WillRepeatedly(Return(0));
EXPECT_CALL(fs, getattr(_,_)).WillRepeatedly(Invoke([&link_created](std::string const & path, struct stat * attr){
memset(reinterpret_cast<void*>(attr),0, sizeof(struct stat));
if (path == "/")
{
attr->st_nlink = 1;
attr->st_mode = S_IFDIR | 0755;
return 0;
}
else if ((path == "/some_link") && (link_created))
{
attr->st_nlink = 1;
attr->st_mode = S_IFLNK | 0755;
return 0;
}
else
{
return -ENOENT;
}
}));
EXPECT_CALL(fs, symlink("link-target", "/some_link")).WillOnce(Invoke([&link_created](auto const &, auto const &){
link_created = true;
return 0;
}));
webfuse::fixture fixture(fs);
auto const path = fixture.get_path() + "/some_link";
ASSERT_EQ(0, ::symlink("link-target", path.c_str()));
}
TEST(symlink, failed_to_create_symlink)
{
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<void*>(attr),0, sizeof(struct stat));
if (path == "/")
{
attr->st_nlink = 1;
attr->st_mode = S_IFDIR | 0755;
return 0;
}
else
{
return -ENOENT;
}
}));
EXPECT_CALL(fs, symlink("link-target", "/some_link")).WillOnce(Return(-EDQUOT));
webfuse::fixture fixture(fs);
auto const path = fixture.get_path() + "/some_link";
ASSERT_NE(0, ::symlink("link-target", path.c_str()));
ASSERT_EQ(EDQUOT, errno);
}