mirror of
https://github.com/falk-werner/webfuse-provider
synced 2026-03-02 04:09:18 +00:00
refactor: merged code structure
This commit is contained in:
17
test/webfuse_provider/mocks/fake_invokation_context.cc
Normal file
17
test/webfuse_provider/mocks/fake_invokation_context.cc
Normal file
@@ -0,0 +1,17 @@
|
||||
#include "webfuse_provider/mocks/fake_invokation_context.hpp"
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
wfp_impl_invokation_context create_context(MockProvider& provider, wfp_request * request)
|
||||
{
|
||||
wfp_impl_invokation_context context =
|
||||
{
|
||||
provider.get_provider(),
|
||||
provider.get_userdata(),
|
||||
request
|
||||
};
|
||||
return context;
|
||||
}
|
||||
|
||||
}
|
||||
16
test/webfuse_provider/mocks/fake_invokation_context.hpp
Normal file
16
test/webfuse_provider/mocks/fake_invokation_context.hpp
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef FAKE_INVOCATION_CONTEXT_HPP
|
||||
#define FAKE_INVOCATION_CONTEXT_HPP
|
||||
|
||||
#include "webfuse_provider/impl/provider.h"
|
||||
#include "webfuse_provider/mocks/mock_provider.hpp"
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
wfp_impl_invokation_context create_context(
|
||||
MockProvider& provider,
|
||||
wfp_request * request = nullptr);
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
50
test/webfuse_provider/mocks/lookup_matcher.hpp
Normal file
50
test/webfuse_provider/mocks/lookup_matcher.hpp
Normal file
@@ -0,0 +1,50 @@
|
||||
#ifndef WF_LOOKUP_MATCHER_HPP
|
||||
#define WF_LOOKUP_MATCHER_HPP
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include <jansson.h>
|
||||
#include <cstring>
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
MATCHER_P2(Lookup, parent, name, "")
|
||||
{
|
||||
if (!json_is_array(arg))
|
||||
{
|
||||
*result_listener << "json array expected";
|
||||
return false;
|
||||
}
|
||||
|
||||
json_t * parent_ = json_array_get(arg, 1);
|
||||
if (!json_is_integer(parent_))
|
||||
{
|
||||
*result_listener << "parent is expected to be an integer";
|
||||
return false;
|
||||
}
|
||||
if (parent != json_integer_value(parent_))
|
||||
{
|
||||
*result_listener << "parent mismatch: expected " << parent
|
||||
<< " but was " << json_integer_value(parent_);
|
||||
return false;
|
||||
}
|
||||
|
||||
json_t * name_ = json_array_get(arg, 2);
|
||||
if (!json_is_string(name_))
|
||||
{
|
||||
*result_listener << "name is expected to be a string";
|
||||
return false;
|
||||
}
|
||||
if (0 != strcmp(name, json_string_value(name_)))
|
||||
{
|
||||
*result_listener << "name mismatch: expected \"" << name
|
||||
<< "\" but was \"" << json_string_value(name_) << "\"";
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
18
test/webfuse_provider/mocks/mock_invokation_handler.hpp
Normal file
18
test/webfuse_provider/mocks/mock_invokation_handler.hpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#ifndef WF_MOCK_INVOKATION_HANDLER_HPP
|
||||
#define WF_MOCK_INVOKATION_HANDLER_HPP
|
||||
|
||||
#include "webfuse/utils/ws_server2.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
class MockInvokationHander: public IIvokationHandler
|
||||
{
|
||||
public:
|
||||
MOCK_METHOD2(Invoke, std::string(char const * method, json_t * params));
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
34
test/webfuse_provider/mocks/mock_jsonrpc_proxy.cc
Normal file
34
test/webfuse_provider/mocks/mock_jsonrpc_proxy.cc
Normal file
@@ -0,0 +1,34 @@
|
||||
#include "webfuse_provider/mocks/mock_jsonrpc_proxy.hpp"
|
||||
#include "webfuse_provider/utils/wrap.hpp"
|
||||
|
||||
extern "C"
|
||||
{
|
||||
static webfuse_test::MockJsonRpcProxy * webfuse_test_MockJsonRpcProxy = nullptr;
|
||||
|
||||
WF_WRAP_VFUNC5(webfuse_test_MockJsonRpcProxy, void, wf_jsonrpc_proxy_vinvoke,
|
||||
struct wf_jsonrpc_proxy *,
|
||||
wf_jsonrpc_proxy_finished_fn *,
|
||||
void *,
|
||||
char const *,
|
||||
char const *);
|
||||
|
||||
WF_WRAP_VFUNC3(webfuse_test_MockJsonRpcProxy, void, wf_jsonrpc_proxy_vnotify,
|
||||
struct wf_jsonrpc_proxy *,
|
||||
char const *,
|
||||
char const *);
|
||||
}
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
MockJsonRpcProxy::MockJsonRpcProxy()
|
||||
{
|
||||
webfuse_test_MockJsonRpcProxy = this;
|
||||
}
|
||||
|
||||
MockJsonRpcProxy::~MockJsonRpcProxy()
|
||||
{
|
||||
webfuse_test_MockJsonRpcProxy = nullptr;
|
||||
}
|
||||
|
||||
}
|
||||
30
test/webfuse_provider/mocks/mock_jsonrpc_proxy.hpp
Normal file
30
test/webfuse_provider/mocks/mock_jsonrpc_proxy.hpp
Normal file
@@ -0,0 +1,30 @@
|
||||
#ifndef MOCK_JSONRPC_PROXY_HPP
|
||||
#define MOCK_JSONRPC_PROXY_HPP
|
||||
|
||||
#include "webfuse_provider/impl/jsonrpc/proxy_intern.h"
|
||||
#include <gmock/gmock.h>
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
class MockJsonRpcProxy
|
||||
{
|
||||
public:
|
||||
MockJsonRpcProxy();
|
||||
virtual ~MockJsonRpcProxy();
|
||||
MOCK_METHOD5(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));
|
||||
MOCK_METHOD3(wf_jsonrpc_proxy_vnotify, void (
|
||||
struct wf_jsonrpc_proxy * proxy,
|
||||
char const * method_name,
|
||||
char const * param_info));
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
27
test/webfuse_provider/mocks/mock_operation_context.cc
Normal file
27
test/webfuse_provider/mocks/mock_operation_context.cc
Normal file
@@ -0,0 +1,27 @@
|
||||
#include "webfuse_provider/mocks/mock_operation_context.hpp"
|
||||
#include "webfuse/utils/wrap.hpp"
|
||||
|
||||
extern "C"
|
||||
{
|
||||
static webfuse_test::MockOperationContext * webfuse_test_MockOperationContext = nullptr;
|
||||
|
||||
WF_WRAP_FUNC1(webfuse_test_MockOperationContext,
|
||||
struct wf_jsonrpc_proxy *, wf_impl_operation_context_get_proxy,
|
||||
struct wf_impl_operation_context *);
|
||||
|
||||
}
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
MockOperationContext::MockOperationContext()
|
||||
{
|
||||
webfuse_test_MockOperationContext = this;
|
||||
}
|
||||
|
||||
MockOperationContext::~MockOperationContext()
|
||||
{
|
||||
webfuse_test_MockOperationContext = nullptr;
|
||||
}
|
||||
|
||||
}
|
||||
22
test/webfuse_provider/mocks/mock_operation_context.hpp
Normal file
22
test/webfuse_provider/mocks/mock_operation_context.hpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#ifndef MOCK_OPERATIONS_CONTEXT_HPP
|
||||
#define MOCK_OPERATIONS_CONTEXT_HPP
|
||||
|
||||
#include "webfuse/adapter/impl/operation/context.h"
|
||||
#include <gmock/gmock.h>
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
class MockOperationContext
|
||||
{
|
||||
public:
|
||||
MockOperationContext();
|
||||
virtual ~MockOperationContext();
|
||||
MOCK_METHOD1(wf_impl_operation_context_get_proxy, wf_jsonrpc_proxy * (
|
||||
struct wf_impl_operation_context * context));
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
130
test/webfuse_provider/mocks/mock_provider.cc
Normal file
130
test/webfuse_provider/mocks/mock_provider.cc
Normal file
@@ -0,0 +1,130 @@
|
||||
#include "webfuse_provider/mocks/mock_provider.hpp"
|
||||
|
||||
extern "C"
|
||||
{
|
||||
using webfuse_test::MockProvider;
|
||||
|
||||
static void webfuse_test_MockProvider_connected(
|
||||
void * user_data)
|
||||
{
|
||||
auto * provider = reinterpret_cast<MockProvider*>(user_data);
|
||||
provider->connected();
|
||||
}
|
||||
|
||||
static void webfuse_test_MockProvider_disconnected(
|
||||
void * user_data)
|
||||
{
|
||||
auto * provider = reinterpret_cast<MockProvider*>(user_data);
|
||||
provider->disconnected();
|
||||
}
|
||||
|
||||
static void webfuse_test_MockProvider_lookup(
|
||||
wfp_request * request,
|
||||
ino_t parent,
|
||||
char const * name,
|
||||
void * user_data)
|
||||
{
|
||||
auto * provider = reinterpret_cast<MockProvider*>(user_data);
|
||||
provider->lookup(request, parent, name);
|
||||
}
|
||||
|
||||
static void webfuse_test_MockProvider_getattr(
|
||||
wfp_request * request,
|
||||
ino_t inode,
|
||||
void * user_data)
|
||||
{
|
||||
auto * provider = reinterpret_cast<MockProvider*>(user_data);
|
||||
provider->getattr(request, inode);
|
||||
}
|
||||
|
||||
static void webfuse_test_MockProvider_readdir(
|
||||
wfp_request * request,
|
||||
ino_t directory,
|
||||
void * user_data)
|
||||
{
|
||||
auto * provider = reinterpret_cast<MockProvider*>(user_data);
|
||||
provider->readdir(request, directory);
|
||||
}
|
||||
|
||||
static void webfuse_test_MockProvider_open(
|
||||
wfp_request * request,
|
||||
ino_t inode,
|
||||
int flags,
|
||||
void * user_data)
|
||||
{
|
||||
auto * provider = reinterpret_cast<MockProvider*>(user_data);
|
||||
provider->open(request, inode, flags);
|
||||
|
||||
}
|
||||
|
||||
static void webfuse_test_MockProvider_close(
|
||||
ino_t inode,
|
||||
uint32_t handle,
|
||||
int flags,
|
||||
void * user_data)
|
||||
{
|
||||
auto * provider = reinterpret_cast<MockProvider*>(user_data);
|
||||
provider->close(inode, handle, flags);
|
||||
|
||||
}
|
||||
|
||||
static void webfuse_test_MockProvider_read(
|
||||
wfp_request * request,
|
||||
ino_t inode,
|
||||
uint32_t handle,
|
||||
size_t offset,
|
||||
size_t length,
|
||||
void * user_data)
|
||||
{
|
||||
auto * provider = reinterpret_cast<MockProvider*>(user_data);
|
||||
provider->read(request, inode, handle, offset, length);
|
||||
}
|
||||
|
||||
static void webfuse_test_MockProvider_get_credentials(
|
||||
wfp_credentials * credentials,
|
||||
void * user_data)
|
||||
{
|
||||
auto * provider = reinterpret_cast<MockProvider*>(user_data);
|
||||
provider->get_credentials(credentials);
|
||||
}
|
||||
|
||||
|
||||
static wfp_provider const webfuse_test_MockProvider =
|
||||
{
|
||||
&webfuse_test_MockProvider_connected,
|
||||
&webfuse_test_MockProvider_disconnected,
|
||||
&webfuse_test_MockProvider_lookup,
|
||||
&webfuse_test_MockProvider_getattr,
|
||||
&webfuse_test_MockProvider_readdir,
|
||||
&webfuse_test_MockProvider_open,
|
||||
&webfuse_test_MockProvider_close,
|
||||
&webfuse_test_MockProvider_read,
|
||||
&webfuse_test_MockProvider_get_credentials,
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
MockProvider::MockProvider()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
MockProvider::~MockProvider()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
wfp_provider const * MockProvider::get_provider()
|
||||
{
|
||||
return &webfuse_test_MockProvider;
|
||||
}
|
||||
|
||||
void * MockProvider::get_userdata()
|
||||
{
|
||||
return reinterpret_cast<void*>(this);
|
||||
}
|
||||
|
||||
}
|
||||
31
test/webfuse_provider/mocks/mock_provider.hpp
Normal file
31
test/webfuse_provider/mocks/mock_provider.hpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#ifndef WF_MOCK_PROVIDER_HPP
|
||||
#define WF_MOCK_PROVIDER_HPP
|
||||
|
||||
#include "webfuse_provider/impl/provider.h"
|
||||
#include <gmock/gmock.h>
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
class MockProvider
|
||||
{
|
||||
public:
|
||||
MockProvider();
|
||||
~MockProvider();
|
||||
wfp_provider const * get_provider();
|
||||
void * get_userdata();
|
||||
MOCK_METHOD0(connected, void ());
|
||||
MOCK_METHOD0(disconnected, void ());
|
||||
MOCK_METHOD0(on_timer, void());
|
||||
MOCK_METHOD3(lookup, void(wfp_request * request, ino_t parent, char const * name));
|
||||
MOCK_METHOD2(getattr, void(wfp_request * request, ino_t inode));
|
||||
MOCK_METHOD2(readdir, void(wfp_request * request, ino_t directory));
|
||||
MOCK_METHOD3(open, void(wfp_request * request, ino_t inode, int flags));
|
||||
MOCK_METHOD3(close, void(ino_t inode, uint32_t handle, int flags));
|
||||
MOCK_METHOD5(read, void(wfp_request * request, ino_t inode, uint32_t handle, size_t offset, size_t length));
|
||||
MOCK_METHOD1(get_credentials, void(wfp_credentials * credentials));
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
217
test/webfuse_provider/mocks/mock_provider_client.cc
Normal file
217
test/webfuse_provider/mocks/mock_provider_client.cc
Normal file
@@ -0,0 +1,217 @@
|
||||
#include "webfuse_provider/mocks/mock_provider_client.hpp"
|
||||
#include "webfuse_provider/operation/error.h"
|
||||
#include "webfuse_provider/dirbuffer.h"
|
||||
|
||||
extern "C"
|
||||
{
|
||||
using webfuse_test::IProviderClient;
|
||||
using webfuse_test::ProviderClientException;
|
||||
|
||||
static void webfuse_test_iproviderclient_onconnected(
|
||||
void * user_data)
|
||||
{
|
||||
auto * self = reinterpret_cast<IProviderClient*>(user_data);
|
||||
self->OnConnected();
|
||||
}
|
||||
|
||||
static void webfuse_test_iproviderclient_ondisconnected(
|
||||
void * user_data)
|
||||
{
|
||||
auto * self = reinterpret_cast<IProviderClient*>(user_data);
|
||||
self->OnDisconnected();
|
||||
}
|
||||
|
||||
static void webfuse_test_iproviderclient_onlookup(
|
||||
struct wfp_request * request,
|
||||
ino_t parent,
|
||||
char const * name,
|
||||
void * user_data)
|
||||
{
|
||||
auto * self = reinterpret_cast<IProviderClient*>(user_data);
|
||||
|
||||
try
|
||||
{
|
||||
struct stat buffer;
|
||||
self->Lookup(parent, name, &buffer);
|
||||
wfp_respond_lookup(request, &buffer);
|
||||
}
|
||||
catch (ProviderClientException& ex)
|
||||
{
|
||||
wfp_respond_error(request, ex.GetErrorCode());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
wfp_respond_error(request, WF_BAD);
|
||||
}
|
||||
}
|
||||
|
||||
static void webfuse_test_iproviderclient_ongetattr(
|
||||
struct wfp_request * request,
|
||||
ino_t inode,
|
||||
void * user_data)
|
||||
{
|
||||
auto * self = reinterpret_cast<IProviderClient*>(user_data);
|
||||
|
||||
try
|
||||
{
|
||||
struct stat buffer;
|
||||
memset(&buffer, 0, sizeof(struct stat));
|
||||
self->GetAttr(inode, &buffer);
|
||||
wfp_respond_getattr(request,&buffer);
|
||||
}
|
||||
catch (ProviderClientException& ex)
|
||||
{
|
||||
wfp_respond_error(request, ex.GetErrorCode());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
wfp_respond_error(request, WF_BAD);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static void webfuse_test_iproviderclient_onreaddir(
|
||||
struct wfp_request * request,
|
||||
ino_t directory,
|
||||
void * user_data)
|
||||
{
|
||||
auto * self = reinterpret_cast<IProviderClient*>(user_data);
|
||||
wfp_dirbuffer * buffer = wfp_dirbuffer_create();
|
||||
|
||||
try
|
||||
{
|
||||
self->ReadDir(directory, buffer);
|
||||
wfp_respond_readdir(request, buffer);
|
||||
}
|
||||
catch (ProviderClientException& ex)
|
||||
{
|
||||
wfp_respond_error(request, ex.GetErrorCode());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
wfp_respond_error(request, WF_BAD);
|
||||
}
|
||||
|
||||
wfp_dirbuffer_dispose(buffer);
|
||||
|
||||
}
|
||||
|
||||
static void webfuse_test_iproviderclient_onopen(
|
||||
struct wfp_request * request,
|
||||
ino_t inode,
|
||||
int flags,
|
||||
void * user_data)
|
||||
{
|
||||
auto * self = reinterpret_cast<IProviderClient*>(user_data);
|
||||
|
||||
try
|
||||
{
|
||||
uint32_t handle = 0;
|
||||
self->Open(inode, flags, &handle);
|
||||
wfp_respond_open(request, handle);
|
||||
}
|
||||
catch (ProviderClientException& ex)
|
||||
{
|
||||
wfp_respond_error(request, ex.GetErrorCode());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
wfp_respond_error(request, WF_BAD);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static void webfuse_test_iproviderclient_onclose(
|
||||
ino_t inode,
|
||||
uint32_t handle,
|
||||
int flags,
|
||||
void * user_data)
|
||||
{
|
||||
auto * self = reinterpret_cast<IProviderClient*>(user_data);
|
||||
self->Close(inode, handle, flags);
|
||||
}
|
||||
|
||||
static void webfuse_test_iproviderclient_onread(
|
||||
struct wfp_request * request,
|
||||
ino_t inode,
|
||||
uint32_t handle,
|
||||
size_t offset,
|
||||
size_t length,
|
||||
void * user_data)
|
||||
{
|
||||
auto * self = reinterpret_cast<IProviderClient*>(user_data);
|
||||
char * data = new char[length];
|
||||
|
||||
try
|
||||
{
|
||||
size_t bytes_read = 0;
|
||||
self->Read(inode, handle, offset, length, data, &bytes_read);
|
||||
wfp_respond_read(request, data, bytes_read);
|
||||
}
|
||||
catch (ProviderClientException& ex)
|
||||
{
|
||||
wfp_respond_error(request, ex.GetErrorCode());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
wfp_respond_error(request, WF_BAD);
|
||||
}
|
||||
|
||||
delete[] data;
|
||||
}
|
||||
|
||||
static void webfuse_test_iproviderclient_get_credentials(
|
||||
wfp_credentials * credentials,
|
||||
void * user_data)
|
||||
{
|
||||
auto * self = reinterpret_cast<IProviderClient*>(user_data);
|
||||
|
||||
try
|
||||
{
|
||||
self->GetCredentials(credentials);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
// swallow
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
ProviderClientException::ProviderClientException(wf_status error_code)
|
||||
: runtime_error("ProviderClientException")
|
||||
, error_code_(error_code)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
wf_status ProviderClientException::GetErrorCode()
|
||||
{
|
||||
return error_code_;
|
||||
}
|
||||
|
||||
|
||||
void IProviderClient::AttachTo(wfp_client_config * config, bool enableAuthentication)
|
||||
{
|
||||
void * self = reinterpret_cast<void *>(this);
|
||||
wfp_client_config_set_userdata(config, self);
|
||||
wfp_client_config_set_onconnected(config, &webfuse_test_iproviderclient_onconnected);
|
||||
wfp_client_config_set_ondisconnected(config, &webfuse_test_iproviderclient_ondisconnected);
|
||||
|
||||
wfp_client_config_set_onlookup(config, &webfuse_test_iproviderclient_onlookup);
|
||||
wfp_client_config_set_ongetattr(config, &webfuse_test_iproviderclient_ongetattr);
|
||||
wfp_client_config_set_onreaddir(config, &webfuse_test_iproviderclient_onreaddir);
|
||||
wfp_client_config_set_onopen(config, &webfuse_test_iproviderclient_onopen);
|
||||
wfp_client_config_set_onclose(config, &webfuse_test_iproviderclient_onclose);
|
||||
wfp_client_config_set_onread(config, &webfuse_test_iproviderclient_onread);
|
||||
|
||||
if (enableAuthentication)
|
||||
{
|
||||
wfp_client_config_enable_authentication(config, &webfuse_test_iproviderclient_get_credentials);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
53
test/webfuse_provider/mocks/mock_provider_client.hpp
Normal file
53
test/webfuse_provider/mocks/mock_provider_client.hpp
Normal file
@@ -0,0 +1,53 @@
|
||||
#ifndef WF_MOCK_PROVIDER_CLIENT_HPP
|
||||
#define WF_MOCK_PROVIDER_CLIENT_HPP
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include "webfuse_provider/client_config.h"
|
||||
#include "webfuse_provider/status.h"
|
||||
#include <stdexcept>
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
class ProviderClientException: public std::runtime_error
|
||||
{
|
||||
public:
|
||||
explicit ProviderClientException(wf_status error_code);
|
||||
wf_status GetErrorCode();
|
||||
private:
|
||||
wf_status error_code_;
|
||||
};
|
||||
|
||||
class IProviderClient
|
||||
{
|
||||
public:
|
||||
virtual ~IProviderClient() = default;
|
||||
virtual void OnConnected() = 0;
|
||||
virtual void OnDisconnected() = 0;
|
||||
virtual void Lookup(ino_t parent, char const * name, struct stat * result) = 0;
|
||||
virtual void GetAttr(ino_t inode, struct stat * buffer) = 0;
|
||||
virtual void ReadDir(ino_t directory, wfp_dirbuffer * buffer) = 0;
|
||||
virtual void Open(ino_t inode, int flags, uint32_t * handle) = 0;
|
||||
virtual void Close(ino_t inode, uint32_t handle, int flags) = 0;
|
||||
virtual void Read(ino_t inode, uint32_t handle, size_t offset, size_t length, char * buffer, size_t * bytes_read) = 0;
|
||||
virtual void GetCredentials(wfp_credentials * credentials) = 0;
|
||||
|
||||
void AttachTo(wfp_client_config * config, bool enableAuthentication = false);
|
||||
};
|
||||
|
||||
class MockProviderClient: public IProviderClient
|
||||
{
|
||||
public:
|
||||
~MockProviderClient() override = default;
|
||||
MOCK_METHOD0( OnConnected, void());
|
||||
MOCK_METHOD0( OnDisconnected, void());
|
||||
MOCK_METHOD3( Lookup, void(ino_t parent, char const * name, struct stat * result));
|
||||
MOCK_METHOD2( GetAttr, void(ino_t inode, struct stat * buffer));
|
||||
MOCK_METHOD2( ReadDir, void(ino_t directory, wfp_dirbuffer * buffer));
|
||||
MOCK_METHOD3( Open, void(ino_t inode, int flags, uint32_t * handle));
|
||||
MOCK_METHOD3( Close, void(ino_t inode, uint32_t handle, int flags));
|
||||
MOCK_METHOD6( Read, void(ino_t inode, uint32_t handle, size_t offset, size_t length, char * buffer, size_t * bytes_read));
|
||||
MOCK_METHOD1( GetCredentials, void (wfp_credentials * credentials));
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
50
test/webfuse_provider/mocks/mock_request.cc
Normal file
50
test/webfuse_provider/mocks/mock_request.cc
Normal file
@@ -0,0 +1,50 @@
|
||||
#include "webfuse_provider/mocks/mock_request.hpp"
|
||||
#include <cstdlib>
|
||||
|
||||
extern "C"
|
||||
{
|
||||
|
||||
static void webfuse_test_MockRequest_respond(
|
||||
json_t * response,
|
||||
void * user_data)
|
||||
{
|
||||
auto * request = reinterpret_cast<webfuse_test::MockRequest*>(user_data);
|
||||
|
||||
json_t * result = json_object_get(response, "result");
|
||||
json_t * error = json_object_get(response, "error");
|
||||
json_t * id_holder = json_object_get(response, "id");
|
||||
|
||||
int id = -1;
|
||||
if (json_is_integer(id_holder))
|
||||
{
|
||||
id = json_integer_value(id_holder);
|
||||
}
|
||||
|
||||
if (nullptr != result)
|
||||
{
|
||||
request->respond(result, id);
|
||||
}
|
||||
else
|
||||
{
|
||||
request->respond_error(error, id);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
struct wfp_request * MockRequest::create_request(int id)
|
||||
{
|
||||
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;
|
||||
|
||||
return request;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
147
test/webfuse_provider/mocks/mock_request.hpp
Normal file
147
test/webfuse_provider/mocks/mock_request.hpp
Normal file
@@ -0,0 +1,147 @@
|
||||
#ifndef WF_MOCK_REQUEST_HPP
|
||||
#define WF_MOCK_REQUEST_HPP
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include <jansson.h>
|
||||
#include <cstring>
|
||||
#include "webfuse_provider/impl/request.h"
|
||||
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
class MockRequest
|
||||
{
|
||||
public:
|
||||
struct wfp_request * create_request(int id);
|
||||
MOCK_METHOD2(respond, void(json_t * result, int id));
|
||||
MOCK_METHOD2(respond_error, void(json_t * error, int id));
|
||||
};
|
||||
|
||||
MATCHER_P3(StatMatcher, inode, mode, file_type, "")
|
||||
{
|
||||
json_t * inode_holder = json_object_get(arg, "inode");
|
||||
if ((!json_is_integer(inode_holder)) || (inode != json_integer_value(inode_holder)))
|
||||
{
|
||||
*result_listener << "missing inode";
|
||||
return false;
|
||||
}
|
||||
|
||||
json_t * mode_holder = json_object_get(arg, "mode");
|
||||
if ((!json_is_integer(mode_holder)) || (mode != json_integer_value(mode_holder)))
|
||||
{
|
||||
*result_listener << "missing mode";
|
||||
return false;
|
||||
}
|
||||
|
||||
json_t * type_holder = json_object_get(arg, "type");
|
||||
if ((!json_is_string(type_holder)) || (0 != strcmp(file_type, json_string_value(type_holder))))
|
||||
{
|
||||
*result_listener << "missing type";
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
MATCHER_P(OpenMatcher, handle, "")
|
||||
{
|
||||
json_t * handle_holder = json_object_get(arg, "handle");
|
||||
if ((!json_is_integer(handle_holder)) || (handle != json_integer_value(handle_holder)))
|
||||
{
|
||||
*result_listener << "missing handle";
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
MATCHER_P3(ReadResultMatcher, data, format, count, "")
|
||||
{
|
||||
json_t * format_holder = json_object_get(arg, "format");
|
||||
if ((!json_is_string(format_holder)) || (0 != strcmp(format, json_string_value(format_holder))))
|
||||
{
|
||||
*result_listener << "invalid or missing format: " << json_string_value(format_holder);
|
||||
return false;
|
||||
}
|
||||
|
||||
json_t * count_holder = json_object_get(arg, "count");
|
||||
if ((!json_is_integer(count_holder)) || (count != json_integer_value(count_holder)))
|
||||
{
|
||||
*result_listener << "invalid or missing count: " << json_integer_value(count_holder);
|
||||
return false;
|
||||
}
|
||||
|
||||
json_t * data_holder = json_object_get(arg, "data");
|
||||
if ((!json_is_string(data_holder)) || (0 != strcmp(data, json_string_value(data_holder))))
|
||||
{
|
||||
*result_listener << "invalid or missing data: " << json_string_value(data_holder);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
MATCHER_P(ReaddirMatcher, contained_elements , "")
|
||||
{
|
||||
if (!json_is_array(arg))
|
||||
{
|
||||
*result_listener << "result is not array";
|
||||
return false;
|
||||
}
|
||||
|
||||
{
|
||||
size_t i;
|
||||
json_t * value;
|
||||
|
||||
json_array_foreach(arg, i, value)
|
||||
{
|
||||
json_t * inode = json_object_get(value, "inode");
|
||||
json_t * name = json_object_get(value, "name");
|
||||
|
||||
if(!json_is_integer(inode))
|
||||
{
|
||||
*result_listener << "invalid result: missing inode";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!json_is_string(name))
|
||||
{
|
||||
*result_listener << "invalid result: missing name";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for(size_t i = 0; NULL != contained_elements[i]; i++)
|
||||
{
|
||||
char const * element = contained_elements[i];
|
||||
bool found = false;
|
||||
size_t j;
|
||||
json_t * value;
|
||||
|
||||
json_array_foreach(arg, j, value)
|
||||
{
|
||||
json_t * name = json_object_get(value, "name");
|
||||
|
||||
found = (0 == strcmp(element, json_string_value(name)));
|
||||
if (found)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found)
|
||||
{
|
||||
*result_listener << "missing required directory element: " << element;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user