mirror of
https://github.com/falk-werner/webfuse-provider
synced 2026-03-02 04:09:18 +00:00
added unit tests for open (adapter)
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
#ifndef MOCK_FUSE_HPP
|
||||
#define MOCK_FUSE_HPP
|
||||
|
||||
#include "webfuse/adapter/impl/fuse_wrapper.h"
|
||||
#include <gmock/gmock.h>
|
||||
|
||||
@@ -15,5 +18,6 @@ public:
|
||||
MOCK_METHOD2(fuse_reply_err, int (fuse_req_t req, int err));
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
#endif
|
||||
|
||||
30
test/webfuse/mocks/mock_jsonrpc_proxy.cc
Normal file
30
test/webfuse/mocks/mock_jsonrpc_proxy.cc
Normal file
@@ -0,0 +1,30 @@
|
||||
#include "webfuse/mocks/mock_jsonrpc_proxy.hpp"
|
||||
#include "webfuse/utils/wrap.hpp"
|
||||
|
||||
extern "C"
|
||||
{
|
||||
static webfuse_test::MockJsonRpcProxy * webfuse_test_MockJsonRpcProxy = nullptr;
|
||||
|
||||
WF_WRAP_FUNC6(webfuse_test_MockJsonRpcProxy, void, wf_jsonrpc_proxy_vinvoke,
|
||||
struct wf_jsonrpc_proxy *,
|
||||
wf_jsonrpc_proxy_finished_fn *,
|
||||
void *,
|
||||
char const *,
|
||||
char const *,
|
||||
va_list);
|
||||
}
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
MockJsonRpcProxy::MockJsonRpcProxy()
|
||||
{
|
||||
webfuse_test_MockJsonRpcProxy = this;
|
||||
}
|
||||
|
||||
MockJsonRpcProxy::~MockJsonRpcProxy()
|
||||
{
|
||||
webfuse_test_MockJsonRpcProxy = nullptr;
|
||||
}
|
||||
|
||||
}
|
||||
27
test/webfuse/mocks/mock_jsonrpc_proxy.hpp
Normal file
27
test/webfuse/mocks/mock_jsonrpc_proxy.hpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef MOCK_JSONRPC_PROXY_HPP
|
||||
#define MOCK_JSONRPC_PROXY_HPP
|
||||
|
||||
#include "webfuse/core/jsonrpc/proxy_intern.h"
|
||||
#include <gmock/gmock.h>
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
class MockJsonRpcProxy
|
||||
{
|
||||
public:
|
||||
MockJsonRpcProxy();
|
||||
virtual ~MockJsonRpcProxy();
|
||||
MOCK_METHOD6(wf_jsonrpc_proxy_vinvoke, void (
|
||||
struct wf_jsonrpc_proxy * proxy,
|
||||
wf_jsonrpc_proxy_finished_fn * finished,
|
||||
void * user_data,
|
||||
char const * method_name,
|
||||
char const * param_info,
|
||||
va_list args));
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
27
test/webfuse/mocks/mock_operations_context.cc
Normal file
27
test/webfuse/mocks/mock_operations_context.cc
Normal file
@@ -0,0 +1,27 @@
|
||||
#include "webfuse/mocks/mock_operations_context.hpp"
|
||||
#include "webfuse/utils/wrap.hpp"
|
||||
|
||||
extern "C"
|
||||
{
|
||||
static webfuse_test::MockOperationsContext * webfuse_test_MockOperationsContext = nullptr;
|
||||
|
||||
WF_WRAP_FUNC1(webfuse_test_MockOperationsContext,
|
||||
struct wf_jsonrpc_proxy *, wf_impl_operations_context_get_proxy,
|
||||
struct wf_impl_operations_context *);
|
||||
|
||||
}
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
MockOperationsContext::MockOperationsContext()
|
||||
{
|
||||
webfuse_test_MockOperationsContext = this;
|
||||
}
|
||||
|
||||
MockOperationsContext::~MockOperationsContext()
|
||||
{
|
||||
webfuse_test_MockOperationsContext = nullptr;
|
||||
}
|
||||
|
||||
}
|
||||
22
test/webfuse/mocks/mock_operations_context.hpp
Normal file
22
test/webfuse/mocks/mock_operations_context.hpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#ifndef MOCK_OPERATIONS_CONTEXT_HPP
|
||||
#define MOCK_OPERATIONS_CONTEXT_HPP
|
||||
|
||||
#include "webfuse/adapter/impl/operations.h"
|
||||
#include <gmock/gmock.h>
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
class MockOperationsContext
|
||||
{
|
||||
public:
|
||||
MockOperationsContext();
|
||||
virtual ~MockOperationsContext();
|
||||
MOCK_METHOD1(wf_impl_operations_context_get_proxy, wf_jsonrpc_proxy * (
|
||||
struct wf_impl_operations_context * context));
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -1,28 +1,101 @@
|
||||
#include "webfuse/adapter/impl/operation/open.h"
|
||||
#include "webfuse/adapter/impl/operations.h"
|
||||
|
||||
#include "webfuse/core/status.h"
|
||||
|
||||
#include "webfuse/mocks/mock_fuse.hpp"
|
||||
#include "webfuse/adapter/impl/session.h"
|
||||
#include "webfuse/mocks/mock_operations_context.hpp"
|
||||
#include "webfuse/mocks/mock_jsonrpc_proxy.hpp"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
using webfuse_test::MockJsonRpcProxy;
|
||||
using webfuse_test::MockOperationsContext;
|
||||
using webfuse_test::FuseMock;
|
||||
using testing::_;
|
||||
using testing::Return;
|
||||
using testing::StrEq;
|
||||
|
||||
TEST(wf_impl_operation_open, invoke_proxy)
|
||||
{
|
||||
MockJsonRpcProxy proxy;
|
||||
EXPECT_CALL(proxy, wf_jsonrpc_proxy_vinvoke(_,_,_,StrEq("open"),StrEq("sii"),_)).Times(1);
|
||||
|
||||
MockOperationsContext context;
|
||||
EXPECT_CALL(context, wf_impl_operations_context_get_proxy(_)).Times(1)
|
||||
.WillOnce(Return(reinterpret_cast<wf_jsonrpc_proxy*>(&proxy)));
|
||||
|
||||
wf_impl_operations_context op_context;
|
||||
op_context.name = nullptr;
|
||||
FuseMock fuse;
|
||||
EXPECT_CALL(fuse, fuse_req_userdata(_)).Times(1).WillOnce(Return(&op_context));
|
||||
|
||||
|
||||
fuse_req_t request = nullptr;
|
||||
fuse_ino_t inode = 1;
|
||||
fuse_file_info file_info;
|
||||
file_info.flags = 0;
|
||||
wf_impl_operation_open(request, inode, &file_info);
|
||||
}
|
||||
|
||||
TEST(wf_impl_operation_open, fail_rpc_null)
|
||||
{
|
||||
wf_impl_session session;
|
||||
memset(&session, 0, sizeof(session));
|
||||
wf_impl_operations_context context =
|
||||
{&session, 1.0f, nullptr };
|
||||
MockOperationsContext context;
|
||||
EXPECT_CALL(context, wf_impl_operations_context_get_proxy(_)).Times(1)
|
||||
.WillOnce(Return(nullptr));
|
||||
|
||||
FuseMock fuse;
|
||||
EXPECT_CALL(fuse, fuse_req_userdata(_)).Times(1)
|
||||
.WillOnce(Return(reinterpret_cast<void*>(&context)));
|
||||
EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1)
|
||||
.WillOnce(Return(0));
|
||||
EXPECT_CALL(fuse, fuse_req_userdata(_)).Times(1).WillOnce(Return(nullptr));
|
||||
EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0));
|
||||
|
||||
fuse_req_t request = nullptr;
|
||||
fuse_ino_t inode = 1;
|
||||
fuse_file_info * file_info = nullptr;
|
||||
wf_impl_operation_open(request, inode, file_info);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(wf_impl_operation_open, finished)
|
||||
{
|
||||
FuseMock fuse;
|
||||
EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(1).WillOnce(Return(0));
|
||||
|
||||
json_t * result = json_object();
|
||||
json_object_set_new(result, "handle", json_integer(42));
|
||||
wf_impl_operation_open_finished(nullptr, result, nullptr);
|
||||
json_decref(result);
|
||||
}
|
||||
|
||||
TEST(wf_impl_operation_open, finished_fail_error)
|
||||
{
|
||||
FuseMock fuse;
|
||||
EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(0);
|
||||
EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0));
|
||||
|
||||
json_t * error = json_object();
|
||||
json_object_set_new(error, "code", json_integer(WF_BAD));
|
||||
wf_impl_operation_open_finished(nullptr, nullptr, error);
|
||||
json_decref(error);
|
||||
}
|
||||
|
||||
TEST(wf_impl_operation_open, finished_fail_no_handle)
|
||||
{
|
||||
FuseMock fuse;
|
||||
EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(0);
|
||||
EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0));
|
||||
|
||||
json_t * result = json_object();
|
||||
wf_impl_operation_open_finished(nullptr, result, nullptr);
|
||||
json_decref(result);
|
||||
}
|
||||
|
||||
TEST(wf_impl_operation_open, finished_fail_invalid_handle_type)
|
||||
{
|
||||
FuseMock fuse;
|
||||
EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(0);
|
||||
EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0));
|
||||
|
||||
json_t * result = json_object();
|
||||
json_object_set_new(result, "handle", json_string("42"));
|
||||
wf_impl_operation_open_finished(nullptr, result, nullptr);
|
||||
json_decref(result);
|
||||
}
|
||||
|
||||
@@ -71,4 +71,32 @@
|
||||
} \
|
||||
}
|
||||
|
||||
#define WF_WRAP_FUNC5( GLOBAL_VAR, RETURN_TYPE, FUNC_NAME, ARG1_TYPE, ARG2_TYPE, ARG3_TYPE, ARG4_TYPE, ARG5_TYPE ) \
|
||||
extern RETURN_TYPE __real_ ## FUNC_NAME (ARG1_TYPE, ARG2_TYPE, ARG3_TYPE, ARG4_TYPE, ARG5_TYPE); \
|
||||
RETURN_TYPE __wrap_ ## FUNC_NAME (ARG1_TYPE arg1, ARG2_TYPE arg2, ARG3_TYPE arg3, ARG4_TYPE arg4, ARG5_TYPE arg5) \
|
||||
{ \
|
||||
if (nullptr == GLOBAL_VAR ) \
|
||||
{ \
|
||||
return __real_ ## FUNC_NAME (arg1, arg2, arg3, arg4, arg5); \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
return GLOBAL_VAR -> FUNC_NAME(arg1, arg2, arg3, arg4, arg5); \
|
||||
} \
|
||||
}
|
||||
|
||||
#define WF_WRAP_FUNC6( GLOBAL_VAR, RETURN_TYPE, FUNC_NAME, ARG1_TYPE, ARG2_TYPE, ARG3_TYPE, ARG4_TYPE, ARG5_TYPE, ARG6_TYPE ) \
|
||||
extern RETURN_TYPE __real_ ## FUNC_NAME (ARG1_TYPE, ARG2_TYPE, ARG3_TYPE, ARG4_TYPE, ARG5_TYPE, ARG6_TYPE); \
|
||||
RETURN_TYPE __wrap_ ## FUNC_NAME (ARG1_TYPE arg1, ARG2_TYPE arg2, ARG3_TYPE arg3, ARG4_TYPE arg4, ARG5_TYPE arg5, ARG6_TYPE arg6) \
|
||||
{ \
|
||||
if (nullptr == GLOBAL_VAR ) \
|
||||
{ \
|
||||
return __real_ ## FUNC_NAME (arg1, arg2, arg3, arg4, arg5, arg6); \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
return GLOBAL_VAR -> FUNC_NAME(arg1, arg2, arg3, arg4, arg5, arg6); \
|
||||
} \
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user