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

added tests for wfp_impl_getattr

This commit is contained in:
Falk Werner 2020-03-29 20:41:31 +02:00
parent 8d28da5e82
commit 70e9f0a278
4 changed files with 52 additions and 26 deletions

View File

@ -47,6 +47,7 @@ add_executable(alltests
test/webfuse/tests/provider/test_url.cc test/webfuse/tests/provider/test_url.cc
test/webfuse/tests/provider/test_client_protocol.cc test/webfuse/tests/provider/test_client_protocol.cc
test/webfuse/tests/provider/operation/test_close.cc test/webfuse/tests/provider/operation/test_close.cc
test/webfuse/tests/provider/operation/test_getattr.cc
test/webfuse/tests/integration/test_integration.cc test/webfuse/tests/integration/test_integration.cc
test/webfuse/tests/integration/server.cc test/webfuse/tests/integration/server.cc
test/webfuse/tests/integration/provider.cc test/webfuse/tests/integration/provider.cc

View File

@ -1,15 +1,14 @@
#include "webfuse/mocks/mock_request.hpp" #include "webfuse/mocks/mock_request.hpp"
#include <cstdlib> #include <cstdlib>
namespace extern "C"
{ {
extern "C" void static void webfuse_test_MockRequest_respond(
respond(
json_t * response, json_t * response,
void * user_data) void * user_data)
{ {
webfuse_test::Request * request = reinterpret_cast<webfuse_test::Request*>(user_data); auto * request = reinterpret_cast<webfuse_test::MockRequest*>(user_data);
json_t * result = json_object_get(response, "result"); json_t * result = json_object_get(response, "result");
json_t * error = json_object_get(response, "error"); json_t * error = json_object_get(response, "error");
@ -31,21 +30,17 @@ respond(
} }
} }
} }
namespace webfuse_test namespace webfuse_test
{ {
struct wfp_request * struct wfp_request * MockRequest::create_request(int id)
request_create(
Request * req,
int id)
{ {
struct wfp_request * request = reinterpret_cast<struct wfp_request *>(malloc(sizeof(struct wfp_request))); auto * request = reinterpret_cast<wfp_request*>(malloc(sizeof(wfp_request)));
request->respond = &webfuse_test_MockRequest_respond;
request->user_data = reinterpret_cast<void*>(this);
request->id = id; request->id = id;
request->user_data = reinterpret_cast<void*>(req);
request->respond = &respond;
return request; return request;
} }

View File

@ -10,26 +10,14 @@
namespace webfuse_test namespace webfuse_test
{ {
class Request class MockRequest
{
public:
virtual ~Request() { }
virtual void respond(json_t * result, int id) = 0;
virtual void respond_error(json_t * error, int id) = 0;
};
class MockRequest: public Request
{ {
public: public:
struct wfp_request * create_request(int id);
MOCK_METHOD2(respond, void(json_t * result, int id)); MOCK_METHOD2(respond, void(json_t * result, int id));
MOCK_METHOD2(respond_error, void(json_t * error, int id)); MOCK_METHOD2(respond_error, void(json_t * error, int id));
}; };
extern struct wfp_request *
request_create(
Request * req,
int id);
MATCHER_P3(GetAttrMatcher, inode, mode, file_type, "") MATCHER_P3(GetAttrMatcher, inode, mode, file_type, "")
{ {
json_t * inode_holder = json_object_get(arg, "inode"); json_t * inode_holder = json_object_get(arg, "inode");

View File

@ -0,0 +1,42 @@
#include "webfuse/provider/impl/operation/getattr.h"
#include "webfuse/mocks/mock_request.hpp"
#include <gtest/gtest.h>
using ::webfuse_test::MockRequest;
using ::webfuse_test::GetAttrMatcher;
using ::testing::_;
TEST(wfp_impl_getattr, default_responds_error)
{
MockRequest request;
auto * req = request.create_request(42);
EXPECT_CALL(request, respond_error(_,42)).Times(1);
wfp_impl_getattr_default(req, 0, nullptr);
}
TEST(wfp_impl_getattr, respond_file)
{
MockRequest request;
auto * req = request.create_request(42);
EXPECT_CALL(request, respond(GetAttrMatcher(23, 0754, "file"), 42)).Times(1);
struct stat buffer;
memset(&buffer, 0, sizeof(buffer));
buffer.st_ino = 23;
buffer.st_mode = S_IFREG | 0754;
wfp_impl_respond_getattr(req, &buffer);
}
TEST(wfp_impl_getattr, respond_dir)
{
MockRequest request;
auto * req = request.create_request(42);
EXPECT_CALL(request, respond(GetAttrMatcher(23, 0754, "dir"), 42)).Times(1);
struct stat buffer;
memset(&buffer, 0, sizeof(buffer));
buffer.st_ino = 23;
buffer.st_mode = S_IFDIR | 0754;
wfp_impl_respond_getattr(req, &buffer);
}