mirror of
https://github.com/falk-werner/webfuse
synced 2024-10-27 20:34:10 +00:00
add readdir test
This commit is contained in:
parent
0e0bb74872
commit
2234f69d2d
@ -72,6 +72,7 @@ if(NOT(WITHOUT_TEST))
|
|||||||
test-src/integration/webfuse/test/process.cpp
|
test-src/integration/webfuse/test/process.cpp
|
||||||
test-src/integration/webfuse/test/daemon.cpp
|
test-src/integration/webfuse/test/daemon.cpp
|
||||||
test-src/integration/test_access.cpp
|
test-src/integration/test_access.cpp
|
||||||
|
test-src/integration/test_readdir.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})
|
||||||
|
@ -63,6 +63,7 @@ public:
|
|||||||
break;
|
break;
|
||||||
case request_type::readdir:
|
case request_type::readdir:
|
||||||
fs_readdir(reader, writer);
|
fs_readdir(reader, writer);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
std::cout << "unknown request: " << ((int) req_type) << std::endl;
|
std::cout << "unknown request: " << ((int) req_type) << std::endl;
|
||||||
break;
|
break;
|
||||||
|
@ -36,16 +36,36 @@ int fs_getattr (std::string const & path, struct stat * attr)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(access, ok_NO_MEMCHECK)
|
TEST(readdir, existing_file)
|
||||||
{
|
{
|
||||||
webfuse::filesystem_mock fs;
|
webfuse::filesystem_mock fs;
|
||||||
EXPECT_CALL(fs, access("/",_)).Times(AnyNumber()).WillRepeatedly(Return(0));
|
EXPECT_CALL(fs, access("/",_)).Times(AnyNumber()).WillRepeatedly(Return(0));
|
||||||
EXPECT_CALL(fs, access("/foo",_)).WillOnce(Return(0));
|
EXPECT_CALL(fs, access("/foo", F_OK)).WillOnce(Return(0));
|
||||||
|
EXPECT_CALL(fs, access("/foo", R_OK)).WillOnce(Return(0));
|
||||||
|
EXPECT_CALL(fs, access("/foo", W_OK)).WillOnce(Return(0));
|
||||||
|
EXPECT_CALL(fs, access("/foo", X_OK)).WillOnce(Return(-EACCES));
|
||||||
EXPECT_CALL(fs, getattr(_,_)).WillRepeatedly(Invoke(fs_getattr));
|
EXPECT_CALL(fs, getattr(_,_)).WillRepeatedly(Invoke(fs_getattr));
|
||||||
|
|
||||||
webfuse::fixture fixture(fs);
|
webfuse::fixture fixture(fs);
|
||||||
auto const path = fixture.get_path() + "/foo";
|
auto const path = fixture.get_path() + "/foo";
|
||||||
|
|
||||||
int const rc = ::access(path.c_str(), F_OK);
|
ASSERT_EQ(0, ::access(path.c_str(), F_OK));
|
||||||
ASSERT_EQ(0, rc);
|
ASSERT_EQ(0, ::access(path.c_str(), R_OK));
|
||||||
|
ASSERT_EQ(0, ::access(path.c_str(), W_OK));
|
||||||
|
ASSERT_EQ(-1, ::access(path.c_str(), X_OK));
|
||||||
|
ASSERT_EQ(EACCES, errno);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(access, non_existing_file)
|
||||||
|
{
|
||||||
|
webfuse::filesystem_mock fs;
|
||||||
|
EXPECT_CALL(fs, access("/",_)).Times(AnyNumber()).WillRepeatedly(Return(0));
|
||||||
|
EXPECT_CALL(fs, access("/foo", F_OK)).WillOnce(Return(-ENOENT));
|
||||||
|
EXPECT_CALL(fs, getattr(_,_)).WillRepeatedly(Invoke(fs_getattr));
|
||||||
|
|
||||||
|
webfuse::fixture fixture(fs);
|
||||||
|
auto const path = fixture.get_path() + "/foo";
|
||||||
|
|
||||||
|
ASSERT_EQ(-1, ::access(path.c_str(), F_OK));
|
||||||
|
ASSERT_EQ(ENOENT, errno);
|
||||||
}
|
}
|
||||||
|
97
test-src/integration/test_readdir.cpp
Normal file
97
test-src/integration/test_readdir.cpp
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
#include "webfuse/webfuse.hpp"
|
||||||
|
#include "webfuse/test/fixture.hpp"
|
||||||
|
#include "webfuse/test/filesystem_mock.hpp"
|
||||||
|
#include "webfuse/test/daemon.hpp"
|
||||||
|
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <dirent.h>
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
|
using testing::_;
|
||||||
|
using testing::Return;
|
||||||
|
using testing::Invoke;
|
||||||
|
using testing::AnyNumber;
|
||||||
|
|
||||||
|
TEST(readdir, existing_dir)
|
||||||
|
{
|
||||||
|
webfuse::filesystem_mock fs;
|
||||||
|
EXPECT_CALL(fs, access("/",_)).Times(AnyNumber()).WillRepeatedly(Return(0));
|
||||||
|
EXPECT_CALL(fs, getattr(_,_)).WillRepeatedly(Invoke([](auto const & path, auto * attr){
|
||||||
|
memset(reinterpret_cast<void*>(attr),0, sizeof(struct stat));
|
||||||
|
|
||||||
|
if ((path == "/") or (path == "/some_dir"))
|
||||||
|
{
|
||||||
|
attr->st_nlink = 1;
|
||||||
|
attr->st_mode = S_IFDIR | 0755;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return -ENOENT;
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
EXPECT_CALL(fs, readdir("/some_dir",_,_)).WillOnce(Invoke([](auto const & path, auto & entries, auto handle) {
|
||||||
|
entries.push_back("foo");
|
||||||
|
return 0;
|
||||||
|
}));
|
||||||
|
|
||||||
|
webfuse::fixture fixture(fs);
|
||||||
|
auto const path = fixture.get_path() + "/some_dir";
|
||||||
|
|
||||||
|
std::unordered_map<std::string, bool> expected_entries = {
|
||||||
|
{".", false},
|
||||||
|
{"..", false},
|
||||||
|
{"foo", false},
|
||||||
|
};
|
||||||
|
|
||||||
|
DIR * dir = opendir(path.c_str());
|
||||||
|
ASSERT_NE(nullptr, dir);
|
||||||
|
dirent * entry = readdir(dir);
|
||||||
|
int count = 0;
|
||||||
|
while (nullptr != entry)
|
||||||
|
{
|
||||||
|
count++;
|
||||||
|
|
||||||
|
auto it = expected_entries.find(entry->d_name);
|
||||||
|
ASSERT_NE(expected_entries.end(), it);
|
||||||
|
ASSERT_FALSE(it->second);
|
||||||
|
it->second = true;
|
||||||
|
|
||||||
|
entry = readdir(dir);
|
||||||
|
}
|
||||||
|
closedir(dir);
|
||||||
|
|
||||||
|
ASSERT_EQ(3, count);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(readdir, non_existing_dir)
|
||||||
|
{
|
||||||
|
webfuse::filesystem_mock fs;
|
||||||
|
EXPECT_CALL(fs, access("/",_)).Times(AnyNumber()).WillRepeatedly(Return(0));
|
||||||
|
EXPECT_CALL(fs, getattr(_,_)).WillRepeatedly(Invoke([](auto const & path, auto * 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, readdir("/some_dir",_,_)).Times(0);
|
||||||
|
|
||||||
|
webfuse::fixture fixture(fs);
|
||||||
|
auto const path = fixture.get_path() + "/some_dir";
|
||||||
|
|
||||||
|
DIR * dir = opendir(path.c_str());
|
||||||
|
ASSERT_EQ(nullptr, dir);
|
||||||
|
ASSERT_EQ(ENOENT, errno);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user