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:
34
test/webfuse_provider/json_matcher.hpp
Normal file
34
test/webfuse_provider/json_matcher.hpp
Normal file
@@ -0,0 +1,34 @@
|
||||
#ifndef WF_JSON_MATCHER_HPP
|
||||
#define FW_JSON_MATCHER_HPP
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <jansson.h>
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
MATCHER_P(JsonMatcher, expected_str, "")
|
||||
{
|
||||
std::cout << "--- JsonMatcher ---" << std::endl;
|
||||
bool matches = false;
|
||||
json_t * expected = json_loads(expected_str, 0, nullptr);
|
||||
if (nullptr != expected)
|
||||
{
|
||||
matches = (1 == json_equal(expected, arg));
|
||||
if (!matches)
|
||||
{
|
||||
char * actual = json_dumps(arg, 0);
|
||||
std::cout << actual << std::endl;
|
||||
*result_listener << "where arg is " << actual;
|
||||
free(actual);
|
||||
}
|
||||
|
||||
json_decref(expected);
|
||||
}
|
||||
|
||||
return true; //matches;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
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
|
||||
31
test/webfuse_provider/tests/core/jsonrpc/mock_timer.cc
Normal file
31
test/webfuse_provider/tests/core/jsonrpc/mock_timer.cc
Normal file
@@ -0,0 +1,31 @@
|
||||
#include "webfuse_provider/tests/core/jsonrpc/mock_timer.hpp"
|
||||
#include "webfuse_provider/utils/wrap.hpp"
|
||||
|
||||
extern "C"
|
||||
{
|
||||
static wf_jsonrpc_test::ITimer * wf_jsonrpc_MockTimer = nullptr;
|
||||
|
||||
WF_WRAP_FUNC0(wf_jsonrpc_MockTimer, wf_timer_manager *, wf_timer_manager_create);
|
||||
WF_WRAP_FUNC1(wf_jsonrpc_MockTimer, void, wf_timer_manager_dispose, wf_timer_manager *);
|
||||
WF_WRAP_FUNC1(wf_jsonrpc_MockTimer, void, wf_timer_manager_check, wf_timer_manager *);
|
||||
|
||||
WF_WRAP_FUNC3(wf_jsonrpc_MockTimer, wf_timer *, wf_timer_create, wf_timer_manager *, wf_timer_on_timer_fn *, void *);
|
||||
WF_WRAP_FUNC1(wf_jsonrpc_MockTimer, void, wf_timer_dispose, wf_timer *);
|
||||
WF_WRAP_FUNC2(wf_jsonrpc_MockTimer, void, wf_timer_start, wf_timer *, int);
|
||||
WF_WRAP_FUNC1(wf_jsonrpc_MockTimer, void, wf_timer_cancel, wf_timer *);
|
||||
|
||||
}
|
||||
|
||||
namespace wf_jsonrpc_test
|
||||
{
|
||||
MockTimer::MockTimer()
|
||||
{
|
||||
wf_jsonrpc_MockTimer = this;
|
||||
}
|
||||
|
||||
MockTimer::~MockTimer()
|
||||
{
|
||||
wf_jsonrpc_MockTimer = nullptr;
|
||||
}
|
||||
|
||||
}
|
||||
48
test/webfuse_provider/tests/core/jsonrpc/mock_timer.hpp
Normal file
48
test/webfuse_provider/tests/core/jsonrpc/mock_timer.hpp
Normal file
@@ -0,0 +1,48 @@
|
||||
#ifndef WF_JSONRPC_MOCK_TIMERMANAGER_HPP
|
||||
#define WF_JSONRPC_MOCK_TIMERMANAGER_HPP
|
||||
|
||||
#include "webfuse_provider/impl/timer/timer.h"
|
||||
#include "webfuse_provider/impl/timer/manager.h"
|
||||
#include <gmock/gmock.h>
|
||||
|
||||
namespace wf_jsonrpc_test
|
||||
{
|
||||
|
||||
class ITimer
|
||||
{
|
||||
public:
|
||||
virtual ~ITimer() = default;
|
||||
virtual wf_timer_manager * wf_timer_manager_create() = 0;
|
||||
virtual void wf_timer_manager_dispose(wf_timer_manager * manager) = 0;
|
||||
virtual void wf_timer_manager_check(wf_timer_manager * manager) = 0;
|
||||
virtual wf_timer * wf_timer_create(
|
||||
wf_timer_manager * manager,
|
||||
wf_timer_on_timer_fn * on_timer,
|
||||
void * user_data) = 0;
|
||||
virtual void wf_timer_dispose(wf_timer * timer) = 0;
|
||||
virtual void wf_timer_start(wf_timer * timer, int timeout_ms) = 0;
|
||||
virtual void wf_timer_cancel(wf_timer * timer) = 0;
|
||||
};
|
||||
|
||||
class MockTimer: public ITimer
|
||||
{
|
||||
public:
|
||||
MockTimer();
|
||||
~MockTimer() override;
|
||||
MOCK_METHOD0(wf_timer_manager_create, wf_timer_manager * ());
|
||||
MOCK_METHOD1(wf_timer_manager_dispose, void(wf_timer_manager * manager));
|
||||
MOCK_METHOD1(wf_timer_manager_check, void (wf_timer_manager * manager));
|
||||
MOCK_METHOD3(wf_timer_create, wf_timer *(
|
||||
wf_timer_manager * manager,
|
||||
wf_timer_on_timer_fn * on_timer,
|
||||
void * user_data));
|
||||
MOCK_METHOD1(wf_timer_dispose, void (wf_timer * timer));
|
||||
MOCK_METHOD2(wf_timer_start, void (wf_timer * timer, int timeout_ms));
|
||||
MOCK_METHOD1(wf_timer_cancel, void (wf_timer * timer));
|
||||
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,41 @@
|
||||
#include "webfuse_provider/tests/core/jsonrpc/mock_timer_callback.hpp"
|
||||
|
||||
extern "C"
|
||||
{
|
||||
using wf_jsonrpc_test::MockTimerCallback;
|
||||
|
||||
static void wf_jsonrpc_test_MockTimerCallback_on_timer(
|
||||
wf_timer * timer,
|
||||
void * user_data)
|
||||
{
|
||||
auto * self = reinterpret_cast<MockTimerCallback*>(user_data);
|
||||
self->on_timer(timer, user_data);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace wf_jsonrpc_test
|
||||
{
|
||||
|
||||
MockTimerCallback::MockTimerCallback()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
MockTimerCallback::~MockTimerCallback()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
wf_timer_on_timer_fn * MockTimerCallback::on_timer_fn()
|
||||
{
|
||||
return &wf_jsonrpc_test_MockTimerCallback_on_timer;
|
||||
}
|
||||
|
||||
void * MockTimerCallback::user_data()
|
||||
{
|
||||
return reinterpret_cast<void*>(this);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
#ifndef WF_JSONRPC_MOCK_TIMERCALLBACK_HPP
|
||||
#define WF_JSONRPC_MOCK_TIMERCALLBACK_HPP
|
||||
|
||||
#include "webfuse_provider/impl/timer/on_timer_fn.h"
|
||||
#include <gmock/gmock.h>
|
||||
|
||||
namespace wf_jsonrpc_test
|
||||
{
|
||||
class MockTimerCallback
|
||||
{
|
||||
public:
|
||||
MockTimerCallback();
|
||||
virtual ~MockTimerCallback();
|
||||
wf_timer_on_timer_fn * on_timer_fn();
|
||||
void * user_data();
|
||||
|
||||
MOCK_METHOD2(on_timer, void (wf_timer * timer, void * user_data));
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
112
test/webfuse_provider/tests/core/jsonrpc/test_is_request.cc
Normal file
112
test/webfuse_provider/tests/core/jsonrpc/test_is_request.cc
Normal file
@@ -0,0 +1,112 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "webfuse_provider/impl/jsonrpc/request.h"
|
||||
|
||||
TEST(wf_jsonrpc_is_request, request_with_object_params)
|
||||
{
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("method"));
|
||||
json_object_set_new(request, "params", json_object());
|
||||
json_object_set_new(request, "id", json_integer(42));
|
||||
|
||||
ASSERT_TRUE(wf_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_is_request, request_with_array_params)
|
||||
{
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("method"));
|
||||
json_object_set_new(request, "params", json_array());
|
||||
json_object_set_new(request, "id", json_integer(42));
|
||||
|
||||
ASSERT_TRUE(wf_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_is_request, null_request)
|
||||
{
|
||||
ASSERT_FALSE(wf_jsonrpc_is_request(nullptr));
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_is_request, invalid_request)
|
||||
{
|
||||
json_t * request = json_array();
|
||||
json_array_append_new(request, json_string("method"));
|
||||
json_array_append_new(request, json_object());
|
||||
json_array_append_new(request, json_integer(42));
|
||||
|
||||
ASSERT_FALSE(wf_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_is_request, invalid_request_without_id)
|
||||
{
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("method"));
|
||||
json_object_set_new(request, "params", json_object());
|
||||
|
||||
ASSERT_FALSE(wf_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_is_request, invalid_request_due_to_invalid_id)
|
||||
{
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("method"));
|
||||
json_object_set_new(request, "params", json_object());
|
||||
json_object_set_new(request, "id", json_string("42"));
|
||||
|
||||
ASSERT_FALSE(wf_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_is_request, invalid_request_without_method)
|
||||
{
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "params", json_object());
|
||||
json_object_set_new(request, "id", json_integer(42));
|
||||
|
||||
ASSERT_FALSE(wf_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_is_request, invalid_request_due_to_invalid_method)
|
||||
{
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_integer(42));
|
||||
json_object_set_new(request, "params", json_object());
|
||||
json_object_set_new(request, "id", json_integer(42));
|
||||
|
||||
ASSERT_FALSE(wf_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_is_request, invalid_request_without_params)
|
||||
{
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("method"));
|
||||
json_object_set_new(request, "id", json_integer(42));
|
||||
|
||||
ASSERT_FALSE(wf_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_is_request, invalid_request_due_to_invalid_params)
|
||||
{
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "methdo", json_string("method"));
|
||||
json_object_set_new(request, "params", json_string("params"));
|
||||
json_object_set_new(request, "id", json_integer(42));
|
||||
|
||||
ASSERT_FALSE(wf_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
94
test/webfuse_provider/tests/core/jsonrpc/test_is_response.cc
Normal file
94
test/webfuse_provider/tests/core/jsonrpc/test_is_response.cc
Normal file
@@ -0,0 +1,94 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "webfuse_provider/impl/jsonrpc/response.h"
|
||||
|
||||
TEST(wf_jsonrpc_is_response, valid_result)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "result", json_object());
|
||||
json_object_set_new(message, "id", json_integer(42));
|
||||
|
||||
ASSERT_TRUE(wf_jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_is_response, valid_result_string)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "result", json_string("also valid"));
|
||||
json_object_set_new(message, "id", json_integer(42));
|
||||
|
||||
ASSERT_TRUE(wf_jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_is_response, valid_error)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "error", json_object());
|
||||
json_object_set_new(message, "id", json_integer(42));
|
||||
|
||||
ASSERT_TRUE(wf_jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_is_response, invalid_null)
|
||||
{
|
||||
ASSERT_FALSE(wf_jsonrpc_is_response(nullptr));
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_is_response, invalid_message)
|
||||
{
|
||||
json_t * message = json_array();
|
||||
json_array_append_new(message, json_object());
|
||||
json_array_append_new(message, json_integer(42));
|
||||
|
||||
ASSERT_FALSE(wf_jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_is_response, invalid_missing_id)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "result", json_object());
|
||||
|
||||
ASSERT_FALSE(wf_jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_is_response, invalid_id_wrong_type)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "result", json_object());
|
||||
json_object_set_new(message, "id", json_string("42"));
|
||||
|
||||
ASSERT_FALSE(wf_jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
|
||||
TEST(wf_jsonrpc_is_response, invalid_missing_result_and_error)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "id", json_integer(42));
|
||||
|
||||
ASSERT_FALSE(wf_jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_is_response, invalid_error_wrong_type)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "error", json_array());
|
||||
json_object_set_new(message, "id", json_integer(42));
|
||||
|
||||
ASSERT_FALSE(wf_jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
430
test/webfuse_provider/tests/core/jsonrpc/test_proxy.cc
Normal file
430
test/webfuse_provider/tests/core/jsonrpc/test_proxy.cc
Normal file
@@ -0,0 +1,430 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "webfuse_provider/impl/jsonrpc/proxy.h"
|
||||
#include "webfuse_provider/status.h"
|
||||
#include "webfuse_provider/impl/timer/manager.h"
|
||||
|
||||
#include "webfuse_provider/tests/core/jsonrpc/mock_timer.hpp"
|
||||
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
using wf_jsonrpc_test::MockTimer;
|
||||
using testing::Return;
|
||||
using testing::_;
|
||||
using testing::DoAll;
|
||||
using testing::SaveArg;
|
||||
|
||||
#define WF_DEFAULT_TIMEOUT (10 * 1000)
|
||||
|
||||
namespace
|
||||
{
|
||||
int jsonrpc_get_status(json_t * error)
|
||||
{
|
||||
json_t * code = json_object_get(error, "code");
|
||||
return (json_is_integer(code)) ? json_integer_value(code) : WF_BAD_FORMAT;
|
||||
}
|
||||
|
||||
struct SendContext
|
||||
{
|
||||
json_t * response;
|
||||
bool result;
|
||||
bool is_called;
|
||||
|
||||
explicit SendContext(bool result_ = true)
|
||||
: response(nullptr)
|
||||
, result(result_)
|
||||
, is_called(false)
|
||||
{
|
||||
}
|
||||
|
||||
~SendContext()
|
||||
{
|
||||
if (nullptr != response)
|
||||
{
|
||||
json_decref(response);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
bool jsonrpc_send(
|
||||
json_t * request,
|
||||
void * user_data)
|
||||
{
|
||||
SendContext * context = reinterpret_cast<SendContext*>(user_data);
|
||||
context->is_called = true;
|
||||
context->response = request;
|
||||
json_incref(request);
|
||||
|
||||
return context->result;
|
||||
}
|
||||
|
||||
struct FinishedContext
|
||||
{
|
||||
bool is_called;
|
||||
json_t * result;
|
||||
json_t * error;
|
||||
|
||||
FinishedContext()
|
||||
: is_called(false)
|
||||
, result(nullptr)
|
||||
, error(nullptr)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
~FinishedContext()
|
||||
{
|
||||
if (nullptr != result)
|
||||
{
|
||||
json_decref(result);
|
||||
}
|
||||
|
||||
if (nullptr != error)
|
||||
{
|
||||
json_decref(error);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
void jsonrpc_finished(
|
||||
void * user_data,
|
||||
json_t const * result,
|
||||
json_t const * error)
|
||||
{
|
||||
FinishedContext * context = reinterpret_cast<FinishedContext*>(user_data);
|
||||
context->is_called = true;
|
||||
context->result = json_deep_copy(result);
|
||||
context->error = json_deep_copy(error);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_proxy, init)
|
||||
{
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext context;
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, user_data);
|
||||
|
||||
wf_jsonrpc_proxy_dispose(proxy);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
|
||||
ASSERT_FALSE(context.is_called);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_proxy, invoke)
|
||||
{
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
wf_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
|
||||
|
||||
ASSERT_TRUE(send_context.is_called);
|
||||
ASSERT_TRUE(json_is_object(send_context.response));
|
||||
|
||||
json_t * method = json_object_get(send_context.response, "method");
|
||||
ASSERT_TRUE(json_is_string(method));
|
||||
ASSERT_STREQ("foo", json_string_value(method));
|
||||
|
||||
json_t * params = json_object_get(send_context.response, "params");
|
||||
ASSERT_TRUE(json_is_array(params));
|
||||
ASSERT_EQ(2, json_array_size(params));
|
||||
ASSERT_TRUE(json_is_string(json_array_get(params, 0)));
|
||||
ASSERT_STREQ("bar", json_string_value(json_array_get(params, 0)));
|
||||
ASSERT_TRUE(json_is_integer(json_array_get(params, 1)));
|
||||
ASSERT_EQ(42, json_integer_value(json_array_get(params, 1)));
|
||||
|
||||
json_t * id = json_object_get(send_context.response, "id");
|
||||
ASSERT_TRUE(json_is_integer(id));
|
||||
|
||||
ASSERT_FALSE(finished_context.is_called);
|
||||
|
||||
wf_jsonrpc_proxy_dispose(proxy);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
|
||||
ASSERT_TRUE(finished_context.is_called);
|
||||
ASSERT_FALSE(nullptr == finished_context.error);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_proxy, invoke_calls_finish_if_send_fails)
|
||||
{
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext send_context(false);
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
wf_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
|
||||
|
||||
ASSERT_TRUE(send_context.is_called);
|
||||
ASSERT_TRUE(json_is_object(send_context.response));
|
||||
|
||||
ASSERT_TRUE(finished_context.is_called);
|
||||
ASSERT_FALSE(nullptr == finished_context.error);
|
||||
|
||||
wf_jsonrpc_proxy_dispose(proxy);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_proxy, invoke_fails_if_another_request_is_pending)
|
||||
{
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
wf_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
|
||||
|
||||
FinishedContext finished_context2;
|
||||
void * finished_data2 = reinterpret_cast<void*>(&finished_context2);
|
||||
wf_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data2, "foo", "");
|
||||
|
||||
ASSERT_TRUE(send_context.is_called);
|
||||
ASSERT_TRUE(json_is_object(send_context.response));
|
||||
|
||||
ASSERT_FALSE(finished_context.is_called);
|
||||
|
||||
ASSERT_TRUE(finished_context2.is_called);
|
||||
ASSERT_EQ(WF_BAD_BUSY, jsonrpc_get_status(finished_context2.error));
|
||||
|
||||
wf_jsonrpc_proxy_dispose(proxy);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_proxy, invoke_fails_if_request_is_invalid)
|
||||
{
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
wf_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "?", "error");
|
||||
|
||||
ASSERT_FALSE(send_context.is_called);
|
||||
|
||||
ASSERT_TRUE(finished_context.is_called);
|
||||
ASSERT_EQ(WF_BAD, jsonrpc_get_status(finished_context.error));
|
||||
|
||||
wf_jsonrpc_proxy_dispose(proxy);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_proxy, on_result)
|
||||
{
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
wf_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
|
||||
|
||||
ASSERT_TRUE(send_context.is_called);
|
||||
ASSERT_TRUE(json_is_object(send_context.response));
|
||||
|
||||
json_t * id = json_object_get(send_context.response, "id");
|
||||
ASSERT_TRUE(json_is_number(id));
|
||||
|
||||
json_t * response = json_object();
|
||||
json_object_set_new(response, "result", json_string("okay"));
|
||||
json_object_set(response, "id", id);
|
||||
|
||||
wf_jsonrpc_proxy_onresult(proxy, response);
|
||||
json_decref(response);
|
||||
|
||||
ASSERT_TRUE(finished_context.is_called);
|
||||
ASSERT_EQ(nullptr, finished_context.error);
|
||||
ASSERT_TRUE(json_is_string(finished_context.result));
|
||||
ASSERT_STREQ("okay", json_string_value(finished_context.result));
|
||||
|
||||
wf_jsonrpc_proxy_dispose(proxy);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_proxy, on_result_reject_response_with_unknown_id)
|
||||
{
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
wf_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
|
||||
|
||||
ASSERT_TRUE(send_context.is_called);
|
||||
ASSERT_TRUE(json_is_object(send_context.response));
|
||||
|
||||
json_t * id = json_object_get(send_context.response, "id");
|
||||
ASSERT_TRUE(json_is_number(id));
|
||||
|
||||
json_t * response = json_object();
|
||||
json_object_set_new(response, "result", json_string("okay"));
|
||||
json_object_set_new(response, "id", json_integer(1 + json_integer_value(id)));
|
||||
|
||||
wf_jsonrpc_proxy_onresult(proxy, response);
|
||||
json_decref(response);
|
||||
|
||||
ASSERT_FALSE(finished_context.is_called);
|
||||
|
||||
wf_jsonrpc_proxy_dispose(proxy);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_proxy, timeout)
|
||||
{
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(timer_manager, 0, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
wf_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
|
||||
|
||||
ASSERT_TRUE(send_context.is_called);
|
||||
ASSERT_TRUE(json_is_object(send_context.response));
|
||||
|
||||
std::this_thread::sleep_for(10ms);
|
||||
wf_timer_manager_check(timer_manager);
|
||||
|
||||
ASSERT_TRUE(finished_context.is_called);
|
||||
ASSERT_EQ(WF_BAD_TIMEOUT, jsonrpc_get_status(finished_context.error));
|
||||
|
||||
wf_jsonrpc_proxy_dispose(proxy);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_proxy, cleanup_pending_request)
|
||||
{
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(timer_manager, 10, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
wf_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
|
||||
|
||||
ASSERT_TRUE(send_context.is_called);
|
||||
ASSERT_TRUE(json_is_object(send_context.response));
|
||||
|
||||
ASSERT_FALSE(finished_context.is_called);
|
||||
|
||||
wf_jsonrpc_proxy_dispose(proxy);
|
||||
|
||||
ASSERT_TRUE(finished_context.is_called);
|
||||
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
|
||||
|
||||
TEST(wf_jsonrpc_proxy, notify)
|
||||
{
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
wf_jsonrpc_proxy_notify(proxy, "foo", "si", "bar", 42);
|
||||
|
||||
ASSERT_TRUE(send_context.is_called);
|
||||
ASSERT_TRUE(json_is_object(send_context.response));
|
||||
|
||||
json_t * method = json_object_get(send_context.response, "method");
|
||||
ASSERT_TRUE(json_is_string(method));
|
||||
ASSERT_STREQ("foo", json_string_value(method));
|
||||
|
||||
json_t * params = json_object_get(send_context.response, "params");
|
||||
ASSERT_TRUE(json_is_array(params));
|
||||
ASSERT_EQ(2, json_array_size(params));
|
||||
ASSERT_TRUE(json_is_string(json_array_get(params, 0)));
|
||||
ASSERT_STREQ("bar", json_string_value(json_array_get(params, 0)));
|
||||
ASSERT_TRUE(json_is_integer(json_array_get(params, 1)));
|
||||
ASSERT_EQ(42, json_integer_value(json_array_get(params, 1)));
|
||||
|
||||
json_t * id = json_object_get(send_context.response, "id");
|
||||
ASSERT_EQ(nullptr, id);
|
||||
|
||||
wf_jsonrpc_proxy_dispose(proxy);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_proxy, notify_dont_send_invalid_request)
|
||||
{
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
wf_jsonrpc_proxy_notify(proxy, "foo", "?");
|
||||
|
||||
ASSERT_FALSE(send_context.is_called);
|
||||
|
||||
wf_jsonrpc_proxy_dispose(proxy);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_proxy, swallow_timeout_if_no_request_pending)
|
||||
{
|
||||
MockTimer timer_api;
|
||||
|
||||
wf_timer_on_timer_fn * on_timer = nullptr;
|
||||
void * timer_context = nullptr;
|
||||
EXPECT_CALL(timer_api, wf_timer_create(_, _, _))
|
||||
.Times(1)
|
||||
.WillOnce(DoAll(SaveArg<1>(&on_timer), SaveArg<2>(&timer_context), Return(nullptr)));
|
||||
EXPECT_CALL(timer_api, wf_timer_dispose(_)).Times(1);
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(nullptr, 1, &jsonrpc_send, send_data);
|
||||
|
||||
on_timer(nullptr, timer_context);
|
||||
ASSERT_FALSE(send_context.is_called);
|
||||
|
||||
|
||||
wf_jsonrpc_proxy_dispose(proxy);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_proxy, on_result_swallow_if_no_request_pending)
|
||||
{
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
json_t * response = json_object();
|
||||
json_object_set_new(response, "result", json_string("okay"));
|
||||
json_object_set_new(response, "id", json_integer(42));
|
||||
|
||||
wf_jsonrpc_proxy_onresult(proxy, response);
|
||||
json_decref(response);
|
||||
|
||||
wf_jsonrpc_proxy_dispose(proxy);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
138
test/webfuse_provider/tests/core/jsonrpc/test_request.cc
Normal file
138
test/webfuse_provider/tests/core/jsonrpc/test_request.cc
Normal file
@@ -0,0 +1,138 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "webfuse_provider/impl/jsonrpc/request.h"
|
||||
#include "webfuse_provider/status.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
struct Context
|
||||
{
|
||||
json_t * response;
|
||||
};
|
||||
|
||||
bool jsonrpc_send(
|
||||
json_t * request,
|
||||
void * user_data)
|
||||
{
|
||||
Context * context = reinterpret_cast<Context*>(user_data);
|
||||
context->response = request;
|
||||
json_incref(request);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_request, create_dispose)
|
||||
{
|
||||
Context context{nullptr};
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
|
||||
struct wf_jsonrpc_request * request =
|
||||
wf_jsonrpc_request_create(42, &jsonrpc_send, user_data);
|
||||
|
||||
ASSERT_NE(nullptr, request);
|
||||
ASSERT_EQ(user_data, wf_jsonrpc_request_get_userdata(request));
|
||||
|
||||
wf_jsonrpc_request_dispose(request);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_request, respond)
|
||||
{
|
||||
Context context{nullptr};
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
|
||||
struct wf_jsonrpc_request * request =
|
||||
wf_jsonrpc_request_create(42, &jsonrpc_send, user_data);
|
||||
|
||||
wf_jsonrpc_respond(request, json_string("okay"));
|
||||
|
||||
ASSERT_NE(nullptr, context.response);
|
||||
|
||||
|
||||
json_t * response = reinterpret_cast<json_t*>(context.response);
|
||||
ASSERT_TRUE(json_is_object(response));
|
||||
|
||||
json_t * id = json_object_get(response, "id");
|
||||
ASSERT_TRUE(json_is_integer(id));
|
||||
ASSERT_EQ(42, json_integer_value(id));
|
||||
|
||||
json_t * result = json_object_get(response, "result");
|
||||
ASSERT_TRUE(json_is_string(result));
|
||||
ASSERT_STREQ("okay", json_string_value(result));
|
||||
|
||||
ASSERT_EQ(nullptr, json_object_get(response, "error"));
|
||||
|
||||
json_decref(response);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_request, respond_error)
|
||||
{
|
||||
Context context{nullptr};
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
|
||||
struct wf_jsonrpc_request * request =
|
||||
wf_jsonrpc_request_create(42, &jsonrpc_send, user_data);
|
||||
|
||||
wf_jsonrpc_respond_error(request, WF_BAD, "Bad");
|
||||
|
||||
ASSERT_NE(nullptr, context.response);
|
||||
|
||||
|
||||
json_t * response = reinterpret_cast<json_t*>(context.response);
|
||||
ASSERT_TRUE(json_is_object(response));
|
||||
|
||||
json_t * id = json_object_get(response, "id");
|
||||
ASSERT_TRUE(json_is_integer(id));
|
||||
ASSERT_EQ(42, json_integer_value(id));
|
||||
|
||||
ASSERT_EQ(nullptr, json_object_get(response, "result"));
|
||||
|
||||
json_t * err = json_object_get(response, "error");
|
||||
ASSERT_TRUE(json_is_object(err));
|
||||
|
||||
json_t * err_code = json_object_get(err, "code");
|
||||
ASSERT_TRUE(json_is_integer(err_code));
|
||||
ASSERT_EQ(WF_BAD, json_integer_value(err_code));
|
||||
|
||||
json_t * err_message = json_object_get(err, "message");
|
||||
ASSERT_TRUE(json_is_string(err_message));
|
||||
ASSERT_STREQ("Bad", json_string_value(err_message));
|
||||
|
||||
json_decref(response);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_request, is_request_object_params)
|
||||
{
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("some_method"));
|
||||
json_object_set_new(request, "params", json_object());
|
||||
json_object_set_new(request, "id", json_integer(42));
|
||||
|
||||
ASSERT_TRUE(wf_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_request, is_request_fail_missing_params)
|
||||
{
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("some_method"));
|
||||
json_object_set_new(request, "id", json_integer(42));
|
||||
|
||||
ASSERT_FALSE(wf_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_request, is_request_fail_params_wrong_type)
|
||||
{
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("some_method"));
|
||||
json_object_set_new(request, "params", json_string("invalid_params"));
|
||||
json_object_set_new(request, "id", json_integer(42));
|
||||
|
||||
ASSERT_FALSE(wf_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
147
test/webfuse_provider/tests/core/jsonrpc/test_response.cc
Normal file
147
test/webfuse_provider/tests/core/jsonrpc/test_response.cc
Normal file
@@ -0,0 +1,147 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "webfuse_provider/impl/jsonrpc/response_intern.h"
|
||||
#include "webfuse_provider/status.h"
|
||||
|
||||
TEST(wf_json_response, init_result)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "result", json_integer(47));
|
||||
json_object_set_new(message, "id", json_integer(11));
|
||||
|
||||
struct wf_jsonrpc_response response;
|
||||
wf_jsonrpc_response_init(&response, message);
|
||||
|
||||
ASSERT_EQ(nullptr, response.error);
|
||||
ASSERT_TRUE(json_is_integer(response.result));
|
||||
ASSERT_EQ(47, json_integer_value(response.result));
|
||||
ASSERT_EQ(11, response.id);
|
||||
|
||||
wf_jsonrpc_response_cleanup(&response);
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wf_json_response, init_error)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_t * err = json_object();
|
||||
json_object_set_new(err, "code", json_integer(42));
|
||||
json_object_set_new(err, "message", json_string("Don't Panic!"));
|
||||
json_object_set_new(message, "error", err);
|
||||
json_object_set_new(message, "id", json_integer(23));
|
||||
|
||||
struct wf_jsonrpc_response response;
|
||||
wf_jsonrpc_response_init(&response, message);
|
||||
|
||||
ASSERT_EQ(42, json_integer_value(json_object_get(response.error, "code")));
|
||||
ASSERT_STREQ("Don't Panic!", json_string_value(json_object_get(response.error, "message")));
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
ASSERT_EQ(23, response.id);
|
||||
|
||||
wf_jsonrpc_response_cleanup(&response);
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wf_json_response, init_fail_missing_result_and_error)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "id", json_integer(12));
|
||||
|
||||
struct wf_jsonrpc_response response;
|
||||
wf_jsonrpc_response_init(&response, message);
|
||||
|
||||
ASSERT_EQ(WF_BAD_FORMAT, json_integer_value(json_object_get(response.error, "code")));
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
ASSERT_EQ(12, response.id);
|
||||
|
||||
wf_jsonrpc_response_cleanup(&response);
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wf_json_response, init_fail_missing_id)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "result", json_integer(47));
|
||||
|
||||
struct wf_jsonrpc_response response;
|
||||
wf_jsonrpc_response_init(&response, message);
|
||||
|
||||
ASSERT_EQ(WF_BAD_FORMAT, json_integer_value(json_object_get(response.error, "code")));
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
ASSERT_EQ(-1, response.id);
|
||||
|
||||
wf_jsonrpc_response_cleanup(&response);
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wf_json_response, init_fail_wrong_id_type)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "result", json_integer(47));
|
||||
json_object_set_new(message, "id", json_string("42"));
|
||||
|
||||
struct wf_jsonrpc_response response;
|
||||
wf_jsonrpc_response_init(&response, message);
|
||||
|
||||
ASSERT_EQ(WF_BAD_FORMAT, json_integer_value(json_object_get(response.error, "code")));
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
ASSERT_EQ(-1, response.id);
|
||||
|
||||
wf_jsonrpc_response_cleanup(&response);
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wf_json_response, init_fail_error_missing_code)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_t * err = json_object();
|
||||
json_object_set_new(err, "message", json_string("Don't Panic!"));
|
||||
json_object_set_new(message, "error", err);
|
||||
json_object_set_new(message, "id", json_integer(23));
|
||||
|
||||
struct wf_jsonrpc_response response;
|
||||
wf_jsonrpc_response_init(&response, message);
|
||||
|
||||
ASSERT_EQ(WF_BAD_FORMAT, json_integer_value(json_object_get(response.error, "code")));
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
ASSERT_EQ(23, response.id);
|
||||
|
||||
wf_jsonrpc_response_cleanup(&response);
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wf_json_response, init_fail_error_wrong_code_type)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_t * err = json_object();
|
||||
json_object_set_new(err, "code", json_string("42"));
|
||||
json_object_set_new(err, "message", json_string("Don't Panic!"));
|
||||
json_object_set_new(message, "error", err);
|
||||
json_object_set_new(message, "id", json_integer(23));
|
||||
|
||||
struct wf_jsonrpc_response response;
|
||||
wf_jsonrpc_response_init(&response, message);
|
||||
|
||||
ASSERT_EQ(WF_BAD_FORMAT, json_integer_value(json_object_get(response.error, "code")));
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
ASSERT_EQ(23, response.id);
|
||||
|
||||
wf_jsonrpc_response_cleanup(&response);
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wf_json_response, init_fail_error_wrong_type)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "error", json_string("invalid error type"));
|
||||
json_object_set_new(message, "id", json_integer(23));
|
||||
|
||||
struct wf_jsonrpc_response response;
|
||||
wf_jsonrpc_response_init(&response, message);
|
||||
|
||||
ASSERT_EQ(WF_BAD_FORMAT, json_integer_value(json_object_get(response.error, "code")));
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
ASSERT_EQ(23, response.id);
|
||||
|
||||
wf_jsonrpc_response_cleanup(&response);
|
||||
json_decref(message);
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
#include <string>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "webfuse_provider/impl/jsonrpc/response_intern.h"
|
||||
|
||||
|
||||
static void response_parse_str(
|
||||
std::string const & buffer,
|
||||
struct wf_jsonrpc_response * response)
|
||||
{
|
||||
json_t * message = json_loadb(buffer.c_str(), buffer.size(), 0, nullptr);
|
||||
if (nullptr != message)
|
||||
{
|
||||
wf_jsonrpc_response_init(response, message);
|
||||
json_decref(message);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(response_parser, test)
|
||||
{
|
||||
struct wf_jsonrpc_response response;
|
||||
|
||||
// no object
|
||||
response_parse_str("[]", &response);
|
||||
ASSERT_NE(nullptr, response.error);
|
||||
ASSERT_EQ(-1, response.id);
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
wf_jsonrpc_response_cleanup(&response);
|
||||
|
||||
// empty
|
||||
response_parse_str("{}", &response);
|
||||
ASSERT_NE(nullptr, response.error);
|
||||
ASSERT_EQ(-1, response.id);
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
wf_jsonrpc_response_cleanup(&response);
|
||||
|
||||
// no data
|
||||
response_parse_str("{\"id\":42}", &response);
|
||||
ASSERT_NE(nullptr, response.error);
|
||||
ASSERT_EQ(42, response.id);
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
wf_jsonrpc_response_cleanup(&response);
|
||||
|
||||
// custom error code
|
||||
response_parse_str("{\"error\":{\"code\": 42}, \"id\": 42}", &response);
|
||||
ASSERT_NE(nullptr, response.error);
|
||||
ASSERT_EQ(42, json_integer_value(json_object_get(response.error, "code")));
|
||||
ASSERT_EQ(42, response.id);
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
wf_jsonrpc_response_cleanup(&response);
|
||||
|
||||
// valid response
|
||||
response_parse_str("{\"result\": true, \"id\": 42}", &response);
|
||||
ASSERT_EQ(nullptr, response.error);
|
||||
ASSERT_EQ(42, response.id);
|
||||
ASSERT_NE(nullptr, response.result);
|
||||
wf_jsonrpc_response_cleanup(&response);
|
||||
}
|
||||
241
test/webfuse_provider/tests/core/jsonrpc/test_server.cc
Normal file
241
test/webfuse_provider/tests/core/jsonrpc/test_server.cc
Normal file
@@ -0,0 +1,241 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "webfuse_provider/impl/jsonrpc/server.h"
|
||||
#include "webfuse_provider/impl/jsonrpc/request.h"
|
||||
#include "webfuse_provider/status.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
struct Context
|
||||
{
|
||||
json_t * response;
|
||||
bool is_called;
|
||||
};
|
||||
|
||||
bool jsonrpc_send(
|
||||
json_t * request,
|
||||
void * user_data)
|
||||
{
|
||||
Context * context = reinterpret_cast<Context*>(user_data);
|
||||
context->is_called = true;
|
||||
context->response = request;
|
||||
json_incref(request);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void sayHello(
|
||||
struct wf_jsonrpc_request * request,
|
||||
char const * method_name,
|
||||
json_t * params,
|
||||
void * user_data)
|
||||
{
|
||||
(void) method_name;
|
||||
(void) params;
|
||||
(void) user_data;
|
||||
|
||||
json_t * result = json_string("Hello");
|
||||
wf_jsonrpc_respond(request, result);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_server, process_request)
|
||||
{
|
||||
struct wf_jsonrpc_server * server = wf_jsonrpc_server_create();
|
||||
wf_jsonrpc_server_add(server, "sayHello", &sayHello, nullptr);
|
||||
|
||||
Context context{nullptr, false};
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("sayHello"));
|
||||
json_object_set_new(request, "params", json_array());
|
||||
json_object_set_new(request, "id", json_integer(23));
|
||||
wf_jsonrpc_server_process(server, request, &jsonrpc_send, user_data);
|
||||
|
||||
ASSERT_TRUE(context.is_called);
|
||||
ASSERT_NE(nullptr, context.response);
|
||||
ASSERT_TRUE(json_is_object(context.response));
|
||||
|
||||
json_t * id = json_object_get(context.response, "id");
|
||||
ASSERT_TRUE(json_is_integer(id));
|
||||
ASSERT_EQ(23, json_integer_value(id));
|
||||
|
||||
json_t * result = json_object_get(context.response, "result");
|
||||
ASSERT_TRUE(json_is_string(result));
|
||||
ASSERT_STREQ("Hello", json_string_value(result));
|
||||
|
||||
json_decref(context.response);
|
||||
json_decref(request);
|
||||
wf_jsonrpc_server_dispose(server);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_server, process_request_with_oject_params)
|
||||
{
|
||||
struct wf_jsonrpc_server * server = wf_jsonrpc_server_create();
|
||||
wf_jsonrpc_server_add(server, "sayHello", &sayHello, nullptr);
|
||||
|
||||
Context context{nullptr, false};
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("sayHello"));
|
||||
json_object_set_new(request, "params", json_object());
|
||||
json_object_set_new(request, "id", json_integer(23));
|
||||
wf_jsonrpc_server_process(server, request, &jsonrpc_send, user_data);
|
||||
|
||||
ASSERT_TRUE(context.is_called);
|
||||
ASSERT_NE(nullptr, context.response);
|
||||
ASSERT_TRUE(json_is_object(context.response));
|
||||
|
||||
json_t * id = json_object_get(context.response, "id");
|
||||
ASSERT_TRUE(json_is_integer(id));
|
||||
ASSERT_EQ(23, json_integer_value(id));
|
||||
|
||||
json_t * result = json_object_get(context.response, "result");
|
||||
ASSERT_TRUE(json_is_string(result));
|
||||
ASSERT_STREQ("Hello", json_string_value(result));
|
||||
|
||||
json_decref(context.response);
|
||||
json_decref(request);
|
||||
wf_jsonrpc_server_dispose(server);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_server, invoke_unknown_method)
|
||||
{
|
||||
struct wf_jsonrpc_server * server = wf_jsonrpc_server_create();
|
||||
wf_jsonrpc_server_add(server, "sayHello", &sayHello, nullptr);
|
||||
|
||||
Context context{nullptr, false};
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("greet"));
|
||||
json_object_set_new(request, "params", json_array());
|
||||
json_object_set_new(request, "id", json_integer(42));
|
||||
wf_jsonrpc_server_process(server, request, &jsonrpc_send, user_data);
|
||||
|
||||
ASSERT_TRUE(context.is_called);
|
||||
ASSERT_NE(nullptr, context.response);
|
||||
ASSERT_TRUE(json_is_object(context.response));
|
||||
|
||||
json_t * id = json_object_get(context.response, "id");
|
||||
ASSERT_TRUE(json_is_integer(id));
|
||||
ASSERT_EQ(42, json_integer_value(id));
|
||||
|
||||
json_t * err = json_object_get(context.response, "error");
|
||||
ASSERT_TRUE(json_is_object(err));
|
||||
|
||||
json_t * err_code = json_object_get(err, "code");
|
||||
ASSERT_TRUE(json_is_integer(err_code));
|
||||
ASSERT_EQ(WF_BAD_NOTIMPLEMENTED, json_integer_value(err_code));
|
||||
|
||||
json_t * err_message = json_object_get(err, "message");
|
||||
ASSERT_TRUE(json_is_string(err_message));
|
||||
|
||||
json_decref(context.response);
|
||||
json_decref(request);
|
||||
wf_jsonrpc_server_dispose(server);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_server, skip_invalid_request_missing_id)
|
||||
{
|
||||
struct wf_jsonrpc_server * server = wf_jsonrpc_server_create();
|
||||
|
||||
Context context{nullptr, false};
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("sayHello"));
|
||||
json_object_set_new(request, "params", json_array());
|
||||
wf_jsonrpc_server_process(server, request, &jsonrpc_send, user_data);
|
||||
|
||||
ASSERT_FALSE(context.is_called);
|
||||
|
||||
json_decref(request);
|
||||
wf_jsonrpc_server_dispose(server);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_server, skip_invalid_request_wrong_id_type)
|
||||
{
|
||||
struct wf_jsonrpc_server * server = wf_jsonrpc_server_create();
|
||||
|
||||
Context context{nullptr, false};
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("sayHello"));
|
||||
json_object_set_new(request, "params", json_array());
|
||||
json_object_set_new(request, "id", json_string("42"));
|
||||
wf_jsonrpc_server_process(server, request, &jsonrpc_send, user_data);
|
||||
|
||||
ASSERT_FALSE(context.is_called);
|
||||
|
||||
json_decref(request);
|
||||
wf_jsonrpc_server_dispose(server);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_server, skip_invalid_request_missing_params)
|
||||
{
|
||||
struct wf_jsonrpc_server * server = wf_jsonrpc_server_create();
|
||||
|
||||
Context context{nullptr, false};
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("sayHello"));
|
||||
json_object_set_new(request, "id", json_integer(42));
|
||||
wf_jsonrpc_server_process(server, request, &jsonrpc_send, user_data);
|
||||
|
||||
ASSERT_FALSE(context.is_called);
|
||||
|
||||
json_decref(request);
|
||||
wf_jsonrpc_server_dispose(server);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_server, skip_invalid_request_wrong_params_type)
|
||||
{
|
||||
struct wf_jsonrpc_server * server = wf_jsonrpc_server_create();
|
||||
|
||||
Context context{nullptr, false};
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("sayHello"));
|
||||
json_object_set_new(request, "params", json_string("invalid"));
|
||||
json_object_set_new(request, "id", json_integer(42));
|
||||
wf_jsonrpc_server_process(server, request, &jsonrpc_send, user_data);
|
||||
|
||||
ASSERT_FALSE(context.is_called);
|
||||
|
||||
json_decref(request);
|
||||
wf_jsonrpc_server_dispose(server);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_server, skip_invalid_request_missing_method)
|
||||
{
|
||||
struct wf_jsonrpc_server * server = wf_jsonrpc_server_create();
|
||||
|
||||
Context context{nullptr, false};
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "params", json_array());
|
||||
json_object_set_new(request, "id", json_integer(42));
|
||||
wf_jsonrpc_server_process(server, request, &jsonrpc_send, user_data);
|
||||
|
||||
ASSERT_FALSE(context.is_called);
|
||||
|
||||
json_decref(request);
|
||||
wf_jsonrpc_server_dispose(server);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_server, skip_invalid_request_wront_method_type)
|
||||
{
|
||||
struct wf_jsonrpc_server * server = wf_jsonrpc_server_create();
|
||||
|
||||
Context context{nullptr, false};
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_integer(42));
|
||||
json_object_set_new(request, "params", json_array());
|
||||
json_object_set_new(request, "id", json_integer(42));
|
||||
wf_jsonrpc_server_process(server, request, &jsonrpc_send, user_data);
|
||||
|
||||
ASSERT_FALSE(context.is_called);
|
||||
|
||||
json_decref(request);
|
||||
wf_jsonrpc_server_dispose(server);
|
||||
}
|
||||
122
test/webfuse_provider/tests/core/test_base64.cc
Normal file
122
test/webfuse_provider/tests/core/test_base64.cc
Normal file
@@ -0,0 +1,122 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "webfuse_provider/impl/base64.h"
|
||||
|
||||
TEST(Base64, EncodedSize)
|
||||
{
|
||||
ASSERT_EQ(4, wf_base64_encoded_size(1));
|
||||
ASSERT_EQ(4, wf_base64_encoded_size(2));
|
||||
ASSERT_EQ(4, wf_base64_encoded_size(3));
|
||||
|
||||
ASSERT_EQ(8, wf_base64_encoded_size(4));
|
||||
ASSERT_EQ(8, wf_base64_encoded_size(5));
|
||||
ASSERT_EQ(8, wf_base64_encoded_size(6));
|
||||
|
||||
ASSERT_EQ(120, wf_base64_encoded_size(90));
|
||||
}
|
||||
|
||||
TEST(Base64, Encode)
|
||||
{
|
||||
char buffer[42];
|
||||
|
||||
std::string in = "Hello";
|
||||
size_t length = wf_base64_encode((uint8_t const*) in.c_str(), in.size(), buffer, 42);
|
||||
ASSERT_EQ(8, length);
|
||||
ASSERT_STREQ("SGVsbG8=", buffer);
|
||||
|
||||
in = "Hello\n";
|
||||
length = wf_base64_encode((uint8_t const*) in.c_str(), in.size(), buffer, 42);
|
||||
ASSERT_EQ(8, length);
|
||||
ASSERT_STREQ("SGVsbG8K", buffer);
|
||||
|
||||
in = "Blue";
|
||||
length = wf_base64_encode((uint8_t const*) in.c_str(), in.size(), buffer, 42);
|
||||
ASSERT_EQ(8, length);
|
||||
ASSERT_STREQ("Qmx1ZQ==", buffer);
|
||||
}
|
||||
|
||||
TEST(Base64, FailedToEncodeBufferTooSmall)
|
||||
{
|
||||
char buffer[1];
|
||||
|
||||
std::string in = "Hello";
|
||||
size_t length = wf_base64_encode((uint8_t const*) in.c_str(), in.size(), buffer, 1);
|
||||
ASSERT_EQ(0, length);
|
||||
}
|
||||
|
||||
TEST(Base64, DecodedSize)
|
||||
{
|
||||
std::string in = "SGVsbG8="; // Hello
|
||||
size_t length = wf_base64_decoded_size(in.c_str(), in.size());
|
||||
ASSERT_EQ(5, length);
|
||||
|
||||
in = "SGVsbG8K"; // Hello\n
|
||||
length = wf_base64_decoded_size(in.c_str(), in.size());
|
||||
ASSERT_EQ(6, length);
|
||||
|
||||
in = "Qmx1ZQ=="; // Blue
|
||||
length = wf_base64_decoded_size(in.c_str(), in.size());
|
||||
ASSERT_EQ(4, length);
|
||||
}
|
||||
|
||||
TEST(Base64, IsValid)
|
||||
{
|
||||
std::string in = "SGVsbG8="; // Hello
|
||||
ASSERT_TRUE(wf_base64_isvalid(in.c_str(), in.size()));
|
||||
|
||||
in = "SGVsbG8K"; // Hello\n
|
||||
ASSERT_TRUE(wf_base64_isvalid(in.c_str(), in.size()));
|
||||
|
||||
in = "Qmx1ZQ=="; // Blue
|
||||
ASSERT_TRUE(wf_base64_isvalid(in.c_str(), in.size()));
|
||||
|
||||
in = "Qmx1ZQ=a";
|
||||
ASSERT_FALSE(wf_base64_isvalid(in.c_str(), in.size()));
|
||||
|
||||
in = "Qmx1ZQ";
|
||||
ASSERT_FALSE(wf_base64_isvalid(in.c_str(), in.size()));
|
||||
|
||||
in = "Qmx1ZQ=";
|
||||
ASSERT_FALSE(wf_base64_isvalid(in.c_str(), in.size()));
|
||||
|
||||
in = "Qmx1Z===";
|
||||
ASSERT_FALSE(wf_base64_isvalid(in.c_str(), in.size()));
|
||||
|
||||
in = "Qmx1ZQ?=";
|
||||
ASSERT_FALSE(wf_base64_isvalid(in.c_str(), in.size()));
|
||||
|
||||
in = "Qm?1ZQ==";
|
||||
ASSERT_FALSE(wf_base64_isvalid(in.c_str(), in.size()));
|
||||
}
|
||||
|
||||
TEST(Base64, Decode)
|
||||
{
|
||||
char buffer[42];
|
||||
|
||||
std::string in = "SGVsbG8="; // Hello
|
||||
size_t length = wf_base64_decode(in.c_str(), in.size(), (uint8_t*) buffer, 42);
|
||||
ASSERT_EQ(5, length);
|
||||
buffer[length] = '\0';
|
||||
ASSERT_STREQ("Hello", buffer);
|
||||
|
||||
in = "SGVsbG8K"; // Hello\n
|
||||
length = wf_base64_decode(in.c_str(), in.size(), (uint8_t*) buffer, 42);
|
||||
ASSERT_EQ(6, length);
|
||||
buffer[length] = '\0';
|
||||
ASSERT_STREQ("Hello\n", buffer);
|
||||
|
||||
in = "Qmx1ZQ=="; // Blue
|
||||
length = wf_base64_decode(in.c_str(), in.size(), (uint8_t*) buffer, 42);
|
||||
ASSERT_EQ(4, length);
|
||||
buffer[length] = '\0';
|
||||
ASSERT_STREQ("Blue", buffer);
|
||||
}
|
||||
|
||||
TEST(Base64, FailToDecodeBufferTooSmall)
|
||||
{
|
||||
char buffer[1];
|
||||
|
||||
std::string in = "SGVsbG8="; // Hello
|
||||
size_t length = wf_base64_decode(in.c_str(), in.size(), (uint8_t*) buffer, 1);
|
||||
ASSERT_EQ(0, length);
|
||||
}
|
||||
|
||||
29
test/webfuse_provider/tests/core/test_container_of.cc
Normal file
29
test/webfuse_provider/tests/core/test_container_of.cc
Normal file
@@ -0,0 +1,29 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "webfuse_provider/impl/container_of.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
struct MyStruct
|
||||
{
|
||||
int first;
|
||||
int second;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
TEST(ContainerOf, FirstMember)
|
||||
{
|
||||
MyStruct my_struct = {23, 42};
|
||||
|
||||
int * first = &my_struct.first;
|
||||
ASSERT_EQ(&my_struct, wf_container_of(first, MyStruct, first));
|
||||
}
|
||||
|
||||
TEST(ContainerOf, SecondMember)
|
||||
{
|
||||
MyStruct my_struct = {23, 42};
|
||||
|
||||
int * second = &my_struct.second;
|
||||
ASSERT_EQ(&my_struct, wf_container_of(second, MyStruct, second));
|
||||
}
|
||||
22
test/webfuse_provider/tests/core/test_message.cc
Normal file
22
test/webfuse_provider/tests/core/test_message.cc
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <cstring>
|
||||
#include "webfuse_provider/impl/message.h"
|
||||
|
||||
TEST(wf_message, create)
|
||||
{
|
||||
json_t * value = json_object();
|
||||
|
||||
struct wf_message * message = wf_message_create(value);
|
||||
ASSERT_NE(nullptr, message);
|
||||
ASSERT_EQ(2, message->length);
|
||||
ASSERT_TRUE(0 == strncmp("{}", message->data, 2));
|
||||
|
||||
wf_message_dispose(message);
|
||||
json_decref(value);
|
||||
}
|
||||
|
||||
TEST(wf_message, fail_to_create)
|
||||
{
|
||||
struct wf_message * message = wf_message_create(nullptr);
|
||||
ASSERT_EQ(nullptr, message);
|
||||
}
|
||||
52
test/webfuse_provider/tests/core/test_message_queue.cc
Normal file
52
test/webfuse_provider/tests/core/test_message_queue.cc
Normal file
@@ -0,0 +1,52 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "webfuse_provider/impl/message_queue.h"
|
||||
#include "webfuse_provider/impl/message.h"
|
||||
#include "webfuse_provider/impl/slist.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
struct wf_slist_item * create_message(char const * content)
|
||||
{
|
||||
json_t * value = json_object();
|
||||
json_object_set_new(value, "content", json_string(content));
|
||||
struct wf_message * message = wf_message_create(value);
|
||||
|
||||
json_decref(value);
|
||||
return &message->item;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
TEST(wf_message_queue, cleanup_empty_list)
|
||||
{
|
||||
struct wf_slist queue;
|
||||
wf_slist_init(&queue);
|
||||
|
||||
wf_message_queue_cleanup(&queue);
|
||||
ASSERT_TRUE(wf_slist_empty(&queue));
|
||||
}
|
||||
|
||||
TEST(wf_message_queue, cleanup_one_element)
|
||||
{
|
||||
struct wf_slist queue;
|
||||
wf_slist_init(&queue);
|
||||
|
||||
wf_slist_append(&queue, create_message("Hello"));
|
||||
|
||||
wf_message_queue_cleanup(&queue);
|
||||
ASSERT_TRUE(wf_slist_empty(&queue));
|
||||
}
|
||||
|
||||
TEST(wf_message_queue, cleanup_multiple_element)
|
||||
{
|
||||
struct wf_slist queue;
|
||||
wf_slist_init(&queue);
|
||||
|
||||
wf_slist_append(&queue, create_message("Hello"));
|
||||
wf_slist_append(&queue, create_message("World"));
|
||||
wf_slist_append(&queue, create_message("!"));
|
||||
|
||||
wf_message_queue_cleanup(&queue);
|
||||
ASSERT_TRUE(wf_slist_empty(&queue));
|
||||
}
|
||||
139
test/webfuse_provider/tests/core/test_slist.cc
Normal file
139
test/webfuse_provider/tests/core/test_slist.cc
Normal file
@@ -0,0 +1,139 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "webfuse_provider/impl/slist.h"
|
||||
|
||||
TEST(wf_slist, init)
|
||||
{
|
||||
struct wf_slist list;
|
||||
wf_slist_init(&list);
|
||||
|
||||
ASSERT_EQ(nullptr, list.head.next);
|
||||
ASSERT_EQ(nullptr, list.last->next);
|
||||
ASSERT_EQ(&list.head, list.last);
|
||||
ASSERT_TRUE(wf_slist_empty(&list));
|
||||
ASSERT_EQ(nullptr, wf_slist_first(&list));
|
||||
}
|
||||
|
||||
TEST(wf_slist, append)
|
||||
{
|
||||
struct wf_slist list;
|
||||
struct wf_slist_item item[3];
|
||||
|
||||
wf_slist_init(&list);
|
||||
ASSERT_TRUE(wf_slist_empty(&list));
|
||||
|
||||
wf_slist_append(&list, &item[0]);
|
||||
ASSERT_NE(&list.head, list.last);
|
||||
ASSERT_FALSE(wf_slist_empty(&list));
|
||||
ASSERT_EQ(&item[0], wf_slist_first(&list));
|
||||
ASSERT_EQ(&item[0], list.head.next);
|
||||
ASSERT_EQ(&item[0], list.last);
|
||||
ASSERT_EQ(nullptr, list.last->next);
|
||||
ASSERT_EQ(nullptr, item[0].next);
|
||||
|
||||
wf_slist_append(&list, &item[1]);
|
||||
ASSERT_NE(&list.head, list.last);
|
||||
ASSERT_FALSE(wf_slist_empty(&list));
|
||||
ASSERT_EQ(&item[0], wf_slist_first(&list));
|
||||
ASSERT_EQ(&item[0], list.head.next);
|
||||
ASSERT_EQ(&item[1], list.last);
|
||||
ASSERT_EQ(nullptr, list.last->next);
|
||||
ASSERT_EQ(&item[1], item[0].next);
|
||||
ASSERT_EQ(nullptr, item[1].next);
|
||||
|
||||
wf_slist_append(&list, &item[2]);
|
||||
ASSERT_NE(&list.head, list.last);
|
||||
ASSERT_FALSE(wf_slist_empty(&list));
|
||||
ASSERT_EQ(&item[0], wf_slist_first(&list));
|
||||
ASSERT_EQ(&item[0], list.head.next);
|
||||
ASSERT_EQ(&item[2], list.last);
|
||||
ASSERT_EQ(nullptr, list.last->next);
|
||||
ASSERT_EQ(&item[1], item[0].next);
|
||||
ASSERT_EQ(&item[2], item[1].next);
|
||||
ASSERT_EQ(nullptr, item[2].next);
|
||||
}
|
||||
|
||||
TEST(wf_slist_remove_after, remove_first)
|
||||
{
|
||||
struct wf_slist list;
|
||||
struct wf_slist_item item[3];
|
||||
|
||||
wf_slist_init(&list);
|
||||
wf_slist_append(&list, &item[0]);
|
||||
wf_slist_append(&list, &item[1]);
|
||||
wf_slist_append(&list, &item[2]);
|
||||
|
||||
wf_slist_item * removed;
|
||||
|
||||
removed = wf_slist_remove_first(&list);
|
||||
ASSERT_FALSE(wf_slist_empty(&list));
|
||||
ASSERT_EQ(&item[0], removed);
|
||||
|
||||
removed = wf_slist_remove_first(&list);
|
||||
ASSERT_FALSE(wf_slist_empty(&list));
|
||||
ASSERT_EQ(&item[1], removed);
|
||||
|
||||
removed = wf_slist_remove_first(&list);
|
||||
ASSERT_TRUE(wf_slist_empty(&list));
|
||||
ASSERT_EQ(&item[2], removed);
|
||||
|
||||
ASSERT_EQ(nullptr, list.head.next);
|
||||
ASSERT_EQ(nullptr, list.last->next);
|
||||
ASSERT_EQ(&list.head, list.last);
|
||||
ASSERT_EQ(nullptr, wf_slist_first(&list));
|
||||
}
|
||||
|
||||
TEST(wf_slist_remove_after, remove_last)
|
||||
{
|
||||
struct wf_slist list;
|
||||
struct wf_slist_item item[3];
|
||||
|
||||
wf_slist_init(&list);
|
||||
wf_slist_append(&list, &item[0]);
|
||||
wf_slist_append(&list, &item[1]);
|
||||
wf_slist_append(&list, &item[2]);
|
||||
|
||||
wf_slist_item * removed;
|
||||
|
||||
removed = wf_slist_remove_after(&list, &item[1]);
|
||||
ASSERT_FALSE(wf_slist_empty(&list));
|
||||
ASSERT_EQ(&item[2], removed);
|
||||
|
||||
removed = wf_slist_remove_after(&list, &item[0]);
|
||||
ASSERT_FALSE(wf_slist_empty(&list));
|
||||
ASSERT_EQ(&item[1], removed);
|
||||
|
||||
removed = wf_slist_remove_after(&list, &list.head);
|
||||
ASSERT_TRUE(wf_slist_empty(&list));
|
||||
ASSERT_EQ(&item[0], removed);
|
||||
|
||||
ASSERT_EQ(nullptr, list.head.next);
|
||||
ASSERT_EQ(nullptr, list.last->next);
|
||||
ASSERT_EQ(&list.head, list.last);
|
||||
ASSERT_EQ(nullptr, wf_slist_first(&list));
|
||||
}
|
||||
|
||||
TEST(wf_slist_remove_after, remove_after)
|
||||
{
|
||||
struct wf_slist list;
|
||||
struct wf_slist_item item[3];
|
||||
|
||||
wf_slist_init(&list);
|
||||
wf_slist_append(&list, &item[0]);
|
||||
wf_slist_append(&list, &item[1]);
|
||||
wf_slist_append(&list, &item[2]);
|
||||
|
||||
wf_slist_item * removed;
|
||||
|
||||
removed = wf_slist_remove_after(&list, &item[0]);
|
||||
ASSERT_FALSE(wf_slist_empty(&list));
|
||||
ASSERT_EQ(&item[1], removed);
|
||||
|
||||
ASSERT_NE(&list.head, list.last);
|
||||
ASSERT_FALSE(wf_slist_empty(&list));
|
||||
ASSERT_EQ(&item[0], wf_slist_first(&list));
|
||||
ASSERT_EQ(&item[0], list.head.next);
|
||||
ASSERT_EQ(&item[2], list.last);
|
||||
ASSERT_EQ(nullptr, list.last->next);
|
||||
ASSERT_EQ(&item[2], item[0].next);
|
||||
ASSERT_EQ(nullptr, item[2].next);
|
||||
}
|
||||
30
test/webfuse_provider/tests/core/test_status.cc
Normal file
30
test/webfuse_provider/tests/core/test_status.cc
Normal file
@@ -0,0 +1,30 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "webfuse_provider/impl/status_intern.h"
|
||||
|
||||
TEST(wf_status, tostring)
|
||||
{
|
||||
ASSERT_STREQ("Good", wf_status_tostring(WF_GOOD));
|
||||
ASSERT_STREQ("Bad", wf_status_tostring(WF_BAD));
|
||||
ASSERT_STREQ("Bad (not implemented)", wf_status_tostring(WF_BAD_NOTIMPLEMENTED));
|
||||
ASSERT_STREQ("Bad (busy)", wf_status_tostring(WF_BAD_BUSY));
|
||||
ASSERT_STREQ("Bad (timeout)", wf_status_tostring(WF_BAD_TIMEOUT));
|
||||
ASSERT_STREQ("Bad (format)", wf_status_tostring(WF_BAD_FORMAT));
|
||||
ASSERT_STREQ("Bad (no entry)", wf_status_tostring(WF_BAD_NOENTRY));
|
||||
ASSERT_STREQ("Bad (access denied)", wf_status_tostring(WF_BAD_ACCESS_DENIED));
|
||||
|
||||
ASSERT_STREQ("Bad (unknown)", wf_status_tostring(-1));
|
||||
}
|
||||
|
||||
TEST(wf_status, to_rc)
|
||||
{
|
||||
ASSERT_EQ(0, wf_status_to_rc(WF_GOOD));
|
||||
ASSERT_EQ(-ENOENT, wf_status_to_rc(WF_BAD));
|
||||
ASSERT_EQ(-ENOSYS, wf_status_to_rc(WF_BAD_NOTIMPLEMENTED));
|
||||
ASSERT_EQ(-ENOENT, wf_status_to_rc(WF_BAD_BUSY));
|
||||
ASSERT_EQ(-ETIMEDOUT, wf_status_to_rc(WF_BAD_TIMEOUT));
|
||||
ASSERT_EQ(-ENOENT, wf_status_to_rc(WF_BAD_FORMAT));
|
||||
ASSERT_EQ(-ENOENT, wf_status_to_rc(WF_BAD_NOENTRY));
|
||||
ASSERT_EQ(-EACCES, wf_status_to_rc(WF_BAD_ACCESS_DENIED));
|
||||
|
||||
ASSERT_EQ(-ENOENT, wf_status_to_rc(-1));
|
||||
}
|
||||
92
test/webfuse_provider/tests/core/test_url.cc
Normal file
92
test/webfuse_provider/tests/core/test_url.cc
Normal file
@@ -0,0 +1,92 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "webfuse_provider/impl/url.h"
|
||||
|
||||
TEST(url, ParseWs)
|
||||
{
|
||||
struct wf_url url;
|
||||
bool result = wf_url_init(&url, "ws://localhost/");
|
||||
ASSERT_TRUE(result);
|
||||
ASSERT_EQ(80, url.port);
|
||||
ASSERT_FALSE(url.use_tls);
|
||||
ASSERT_STREQ("localhost", url.host);
|
||||
ASSERT_STREQ("/", url.path);
|
||||
|
||||
wf_url_cleanup(&url);
|
||||
}
|
||||
|
||||
TEST(url, ParswWss)
|
||||
{
|
||||
struct wf_url url;
|
||||
bool result = wf_url_init(&url, "wss://localhost/");
|
||||
ASSERT_TRUE(result);
|
||||
ASSERT_EQ(443, url.port);
|
||||
ASSERT_TRUE(url.use_tls);
|
||||
ASSERT_STREQ("localhost", url.host);
|
||||
ASSERT_STREQ("/", url.path);
|
||||
|
||||
wf_url_cleanup(&url);
|
||||
}
|
||||
|
||||
TEST(url, ParseIPAdress)
|
||||
{
|
||||
struct wf_url url;
|
||||
bool result = wf_url_init(&url, "ws://127.0.0.1/");
|
||||
ASSERT_TRUE(result);
|
||||
ASSERT_EQ(80, url.port);
|
||||
ASSERT_STREQ("127.0.0.1", url.host);
|
||||
ASSERT_STREQ("/", url.path);
|
||||
|
||||
wf_url_cleanup(&url);
|
||||
}
|
||||
|
||||
TEST(url, ParsePort)
|
||||
{
|
||||
struct wf_url url;
|
||||
bool result = wf_url_init(&url, "ws://localhost:54321/");
|
||||
ASSERT_TRUE(result);
|
||||
ASSERT_EQ(54321, url.port);
|
||||
|
||||
wf_url_cleanup(&url);
|
||||
}
|
||||
|
||||
TEST(url, ParseNonEmptyPath)
|
||||
{
|
||||
struct wf_url url;
|
||||
bool result = wf_url_init(&url, "ws://localhost/some_path?query");
|
||||
ASSERT_TRUE(result);
|
||||
ASSERT_STREQ("/some_path?query", url.path);
|
||||
|
||||
wf_url_cleanup(&url);
|
||||
}
|
||||
|
||||
TEST(url, FailToParseUnknownProtocol)
|
||||
{
|
||||
struct wf_url url;
|
||||
bool result = wf_url_init(&url, "unknown://localhost/");
|
||||
ASSERT_FALSE(result);
|
||||
ASSERT_EQ(0, url.port);
|
||||
ASSERT_EQ(nullptr, url.path);
|
||||
ASSERT_EQ(nullptr, url.host);
|
||||
}
|
||||
|
||||
TEST(url, FailToParseMissingProtocol)
|
||||
{
|
||||
struct wf_url url;
|
||||
bool result = wf_url_init(&url, "unknown");
|
||||
ASSERT_FALSE(result);
|
||||
ASSERT_EQ(0, url.port);
|
||||
ASSERT_EQ(nullptr, url.path);
|
||||
ASSERT_EQ(nullptr, url.host);
|
||||
}
|
||||
|
||||
TEST(url, FailToParseMissingPath)
|
||||
{
|
||||
struct wf_url url;
|
||||
bool result = wf_url_init(&url, "ws://localhost");
|
||||
ASSERT_FALSE(result);
|
||||
ASSERT_EQ(0, url.port);
|
||||
ASSERT_EQ(nullptr, url.path);
|
||||
ASSERT_EQ(nullptr, url.host);
|
||||
}
|
||||
|
||||
47
test/webfuse_provider/tests/core/test_util.cc
Normal file
47
test/webfuse_provider/tests/core/test_util.cc
Normal file
@@ -0,0 +1,47 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "webfuse_provider/impl/json_util.h"
|
||||
|
||||
TEST(jsonrpc_util, get_int)
|
||||
{
|
||||
json_t * object = json_object();
|
||||
json_object_set_new(object, "key", json_integer(23));
|
||||
int value = wf_impl_json_get_int(object, "key", 42);
|
||||
ASSERT_EQ(23, value);
|
||||
|
||||
json_decref(object);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_util, failed_to_get_null_object)
|
||||
{
|
||||
int value = wf_impl_json_get_int(nullptr, "key", 42);
|
||||
|
||||
ASSERT_EQ(42, value);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_util, failed_to_get_not_object)
|
||||
{
|
||||
json_t * object = json_array();
|
||||
int value = wf_impl_json_get_int(nullptr, "key", 42);
|
||||
ASSERT_EQ(42, value);
|
||||
|
||||
json_decref(object);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_util, failed_to_get_invalid_key)
|
||||
{
|
||||
json_t * object = json_object();
|
||||
int value = wf_impl_json_get_int(object, "key", 42);
|
||||
ASSERT_EQ(42, value);
|
||||
|
||||
json_decref(object);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_util, failed_to_get_invalid_value_type)
|
||||
{
|
||||
json_t * object = json_object();
|
||||
json_object_set_new(object, "key", json_string("42"));
|
||||
int value = wf_impl_json_get_int(object, "key", 42);
|
||||
ASSERT_EQ(42, value);
|
||||
|
||||
json_decref(object);
|
||||
}
|
||||
38
test/webfuse_provider/tests/core/timer/test_timepoint.cc
Normal file
38
test/webfuse_provider/tests/core/timer/test_timepoint.cc
Normal file
@@ -0,0 +1,38 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "webfuse_provider/impl/timer/timepoint.h"
|
||||
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
TEST(wf_timer_timepoint, now)
|
||||
{
|
||||
wf_timer_timepoint start = wf_timer_timepoint_now();
|
||||
std::this_thread::sleep_for(42ms);
|
||||
wf_timer_timepoint end = wf_timer_timepoint_now();
|
||||
|
||||
ASSERT_LT(start, end);
|
||||
ASSERT_LT(end, start + 500);
|
||||
}
|
||||
|
||||
TEST(wf_timer_timepoint, in_msec)
|
||||
{
|
||||
wf_timer_timepoint now = wf_timer_timepoint_now();
|
||||
wf_timer_timepoint later = wf_timer_timepoint_in_msec(42);
|
||||
|
||||
ASSERT_LT(now, later);
|
||||
ASSERT_LT(later, now + 500);
|
||||
}
|
||||
|
||||
TEST(wf_timer_timepoint, elapsed)
|
||||
{
|
||||
wf_timer_timepoint now;
|
||||
|
||||
now = wf_timer_timepoint_now();
|
||||
ASSERT_TRUE(wf_timer_timepoint_is_elapsed(now - 1));
|
||||
|
||||
now = wf_timer_timepoint_now();
|
||||
ASSERT_FALSE(wf_timer_timepoint_is_elapsed(now + 500));
|
||||
}
|
||||
148
test/webfuse_provider/tests/core/timer/test_timer.cc
Normal file
148
test/webfuse_provider/tests/core/timer/test_timer.cc
Normal file
@@ -0,0 +1,148 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <cstddef>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
#include "webfuse_provider/impl/timer/timer.h"
|
||||
#include "webfuse_provider/impl/timer/manager.h"
|
||||
|
||||
using std::size_t;
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
extern "C"
|
||||
{
|
||||
void on_timeout(struct wf_timer * timer, void * user_data)
|
||||
{
|
||||
(void) timer;
|
||||
|
||||
bool * triggered = reinterpret_cast<bool*>(user_data);
|
||||
*triggered = true;
|
||||
}
|
||||
}
|
||||
|
||||
TEST(wf_timer, init)
|
||||
{
|
||||
bool triggered = false;
|
||||
struct wf_timer_manager * manager = wf_timer_manager_create();
|
||||
struct wf_timer * timer = wf_timer_create(manager, &on_timeout, reinterpret_cast<void*>(&triggered));
|
||||
|
||||
wf_timer_dispose(timer);
|
||||
wf_timer_manager_dispose(manager);
|
||||
}
|
||||
|
||||
TEST(wf_timer, trigger)
|
||||
{
|
||||
bool triggered = false;
|
||||
struct wf_timer_manager * manager = wf_timer_manager_create();
|
||||
struct wf_timer * timer = wf_timer_create(manager, &on_timeout, reinterpret_cast<void*>(&triggered));
|
||||
|
||||
wf_timer_start(timer, -1);
|
||||
wf_timer_manager_check(manager);
|
||||
|
||||
ASSERT_TRUE(triggered);
|
||||
|
||||
wf_timer_dispose(timer);
|
||||
wf_timer_manager_dispose(manager);
|
||||
}
|
||||
|
||||
TEST(wf_timer, trigger_on_dispose)
|
||||
{
|
||||
bool triggered = false;
|
||||
struct wf_timer_manager * manager = wf_timer_manager_create();
|
||||
struct wf_timer * timer = wf_timer_create(manager, &on_timeout, reinterpret_cast<void*>(&triggered));
|
||||
|
||||
wf_timer_start(timer, (5 * 60 * 1000));
|
||||
|
||||
wf_timer_manager_dispose(manager);
|
||||
ASSERT_TRUE(triggered);
|
||||
|
||||
wf_timer_dispose(timer);
|
||||
}
|
||||
|
||||
TEST(wf_timer, cancel)
|
||||
{
|
||||
bool triggered = false;
|
||||
struct wf_timer_manager * manager = wf_timer_manager_create();
|
||||
struct wf_timer * timer = wf_timer_create(manager, &on_timeout, reinterpret_cast<void*>(&triggered));
|
||||
|
||||
wf_timer_start(timer, 250);
|
||||
std::this_thread::sleep_for(500ms);
|
||||
wf_timer_cancel(timer);
|
||||
wf_timer_manager_check(manager);
|
||||
|
||||
ASSERT_FALSE(triggered);
|
||||
|
||||
wf_timer_dispose(timer);
|
||||
wf_timer_manager_dispose(manager);
|
||||
}
|
||||
|
||||
TEST(wf_timer, cancel_multiple_timers)
|
||||
{
|
||||
static size_t const count = 5;
|
||||
struct wf_timer_manager * manager = wf_timer_manager_create();
|
||||
struct wf_timer * timer[count];
|
||||
|
||||
bool triggered = false;
|
||||
for(size_t i = 0; i < count; i++)
|
||||
{
|
||||
timer[i] = wf_timer_create(manager, &on_timeout, reinterpret_cast<void*>(&triggered));
|
||||
wf_timer_start(timer[i], 0);
|
||||
}
|
||||
|
||||
std::this_thread::sleep_for(10ms);
|
||||
for(size_t i = 0; i < count; i++)
|
||||
{
|
||||
wf_timer_cancel(timer[i]);
|
||||
}
|
||||
|
||||
wf_timer_manager_check(manager);
|
||||
ASSERT_FALSE(triggered);
|
||||
|
||||
for(size_t i = 0; i < count; i++)
|
||||
{
|
||||
wf_timer_dispose(timer[i]);
|
||||
}
|
||||
wf_timer_manager_dispose(manager);
|
||||
}
|
||||
|
||||
TEST(wf_timer, multiple_timers)
|
||||
{
|
||||
static size_t const count = 5;
|
||||
struct wf_timer_manager * manager = wf_timer_manager_create();
|
||||
struct wf_timer * timer[count];
|
||||
bool triggered[count];
|
||||
|
||||
for(size_t i = 0; i < count; i++)
|
||||
{
|
||||
timer[i] = wf_timer_create(manager, &on_timeout, reinterpret_cast<void*>(&triggered[i]));
|
||||
triggered[i] = false;
|
||||
wf_timer_start(timer[i], (300 - (50 * i)));
|
||||
}
|
||||
|
||||
for(size_t i = 0; i < count; i++)
|
||||
{
|
||||
std::this_thread::sleep_for(100ms);
|
||||
wf_timer_manager_check(manager);
|
||||
}
|
||||
|
||||
for(size_t i = 0; i < count; i++)
|
||||
{
|
||||
ASSERT_TRUE(triggered[i]);
|
||||
wf_timer_dispose(timer[i]);
|
||||
}
|
||||
|
||||
wf_timer_manager_dispose(manager);
|
||||
}
|
||||
|
||||
TEST(wf_timer, dont_trigger_null_callback)
|
||||
{
|
||||
struct wf_timer_manager * manager = wf_timer_manager_create();
|
||||
struct wf_timer * timer = wf_timer_create(manager, nullptr, nullptr);
|
||||
|
||||
wf_timer_start(timer, -1);
|
||||
wf_timer_manager_check(manager);
|
||||
|
||||
wf_timer_dispose(timer);
|
||||
wf_timer_manager_dispose(manager);
|
||||
}
|
||||
85
test/webfuse_provider/tests/integration/file.cc
Normal file
85
test/webfuse_provider/tests/integration/file.cc
Normal file
@@ -0,0 +1,85 @@
|
||||
#include "webfuse_provider/tests/integration/file.hpp"
|
||||
#include <cstdio>
|
||||
#include <sstream>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
bool invoke(std::string const & command)
|
||||
{
|
||||
int exit_code = -1;
|
||||
|
||||
FILE * file = ::popen(command.c_str(), "r");
|
||||
if (nullptr != file)
|
||||
{
|
||||
exit_code = ::pclose(file);
|
||||
}
|
||||
|
||||
return (0 == exit_code);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
File::File(std::string const& path)
|
||||
: path_(path)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
File::~File()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool File::isFile()
|
||||
{
|
||||
std::stringstream command;
|
||||
command << "./fs_check -c is_file -f " << path_;
|
||||
|
||||
return invoke(command.str());
|
||||
}
|
||||
|
||||
bool File::isDirectory()
|
||||
{
|
||||
std::stringstream command;
|
||||
command << "./fs_check -c is_dir -f " << path_;
|
||||
|
||||
return invoke(command.str());
|
||||
}
|
||||
|
||||
bool File::hasAccessRights(int accessRights)
|
||||
{
|
||||
std::stringstream command;
|
||||
command << "./fs_check -c has_mode -f " << path_ << " -a " << accessRights;
|
||||
|
||||
return invoke(command.str());
|
||||
}
|
||||
|
||||
bool File::hasSize(size_t size)
|
||||
{
|
||||
std::stringstream command;
|
||||
command << "./fs_check -c has_size -f " << path_ << " -a " << size;
|
||||
|
||||
return invoke(command.str());
|
||||
}
|
||||
|
||||
bool File::hasSubdirectory(std::string const & subdir)
|
||||
{
|
||||
std::stringstream command;
|
||||
command << "./fs_check -c has_subdir -f " << path_ << " -a " << subdir;
|
||||
|
||||
return invoke(command.str());
|
||||
}
|
||||
|
||||
bool File::hasContents(std::string const & contents)
|
||||
{
|
||||
std::stringstream command;
|
||||
command << "./fs_check -c has_contents -f " << path_ << " -a " << contents;
|
||||
|
||||
return invoke(command.str());
|
||||
}
|
||||
|
||||
}
|
||||
26
test/webfuse_provider/tests/integration/file.hpp
Normal file
26
test/webfuse_provider/tests/integration/file.hpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef WF_TEST_INTEGRATION_FILE_HPP
|
||||
#define WF_TEST_INTEGRATION_FILE_HPP
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
class File final
|
||||
{
|
||||
public:
|
||||
explicit File(std::string const& path);
|
||||
~File();
|
||||
bool isFile();
|
||||
bool isDirectory();
|
||||
bool hasAccessRights(int accessRights);
|
||||
bool hasSize(size_t size);
|
||||
bool hasSubdirectory(std::string const & subdir);
|
||||
bool hasContents(std::string const & contents);
|
||||
private:
|
||||
std::string path_;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
288
test/webfuse_provider/tests/integration/fs_check.c
Normal file
288
test/webfuse_provider/tests/integration/fs_check.c
Normal file
@@ -0,0 +1,288 @@
|
||||
/* Why this tool is used:
|
||||
*
|
||||
* In order to test webfuse as a fuse filesystem, file system operations should be made.
|
||||
* To check for memory leaks, valgind (memcheck) is used.
|
||||
*
|
||||
* Early tests discovered some ugly behavior of valgrind:
|
||||
* - valgrind intercepts syscalls like open, read and write
|
||||
* - valgrind does not expect that syscalls are handled within the process to be checked
|
||||
*
|
||||
* There is a more or less (un-) documented switch, which changes valgrind's bevahior, but
|
||||
* this caused other problems.
|
||||
*
|
||||
* The second approach used GTests's death tests. Death tests were used quite a while,
|
||||
* until we discovered a configuration bug when running CTest:
|
||||
* - memory leaks did not lead to test error
|
||||
*
|
||||
* After fixing CTest configuration, memory leaks within the death tests were shown.
|
||||
* Which is correct, since death tests pematurely exits the program an therefore no
|
||||
* cleanup is done.
|
||||
*
|
||||
* Finally, it was decided to use good old popen together with this tool.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <dirent.h>
|
||||
|
||||
#include <getopt.h>
|
||||
|
||||
struct command;
|
||||
|
||||
typedef bool
|
||||
command_invoke_fn(
|
||||
struct command * command);
|
||||
|
||||
struct command
|
||||
{
|
||||
command_invoke_fn * invoke;
|
||||
char * file;
|
||||
char * arg;
|
||||
bool success;
|
||||
bool has_arg;
|
||||
};
|
||||
|
||||
static bool print_usage(
|
||||
struct command * command)
|
||||
{
|
||||
printf(
|
||||
"fs_check, (c) 2020 by Webfuse authors (https://github.com/falk-werner/webfuse)\n"
|
||||
"Checks file information\n"
|
||||
"\n"
|
||||
"Usage:\n"
|
||||
"\t./fs_check --command <command> --file <file> [--arg <arg>]\n"
|
||||
"\n"
|
||||
"Options:\n"
|
||||
"\t-c, --command - Check to perform (see below)\n"
|
||||
"\t-f, --file - Path to file to check\n"
|
||||
"\t-a, --arg - Argument, depending on command\n"
|
||||
"\n"
|
||||
"Commands:\n"
|
||||
"\tis_file - checks, if <file> is a regular file; no arg\n"
|
||||
"\tis_dir - checks, if <file> is a directory; no arg\n"
|
||||
"\thas_mode - checks, if <file> has the mode given in <arg>\n"
|
||||
"\thas_size - checks, if <file> has the size given in <arg>\n"
|
||||
"\thas_subdir - checks, if <file> contains the sub directory given in <arg>\n"
|
||||
"\thas_contents - checks, if <file> has the contents given in <arg>\n"
|
||||
);
|
||||
|
||||
return command->success;
|
||||
}
|
||||
|
||||
static bool is_file(
|
||||
struct command * command)
|
||||
{
|
||||
struct stat buffer;
|
||||
int rc = stat(command->file, &buffer);
|
||||
|
||||
return ((0 == rc) && (S_ISREG(buffer.st_mode)));
|
||||
}
|
||||
|
||||
static bool is_dir(
|
||||
struct command * command)
|
||||
{
|
||||
struct stat buffer;
|
||||
int rc = stat(command->file, &buffer);
|
||||
|
||||
return ((0 == rc) && (S_ISDIR(buffer.st_mode)));
|
||||
}
|
||||
|
||||
static bool has_mode(
|
||||
struct command * command)
|
||||
{
|
||||
mode_t mode = (mode_t) atoi(command->arg);
|
||||
struct stat buffer;
|
||||
int rc = stat(command->file, &buffer);
|
||||
|
||||
return ((0 == rc) && (mode == (buffer.st_mode & 0777)));
|
||||
}
|
||||
|
||||
static bool has_size(
|
||||
struct command * command)
|
||||
{
|
||||
int size = atoi(command->arg);
|
||||
struct stat buffer;
|
||||
int rc = stat(command->file, &buffer);
|
||||
|
||||
return ((0 == rc) && (size == (buffer.st_size)));
|
||||
}
|
||||
|
||||
static bool has_subdir(
|
||||
struct command * command)
|
||||
{
|
||||
bool result = false;
|
||||
char const * subdir = command->arg;
|
||||
DIR * dir = opendir(command->file);
|
||||
if (NULL != dir)
|
||||
{
|
||||
struct dirent * entry = readdir(dir);
|
||||
while (NULL != entry)
|
||||
{
|
||||
if (0 == strcmp(subdir, entry->d_name))
|
||||
{
|
||||
result = true;
|
||||
break;
|
||||
}
|
||||
|
||||
entry = readdir(dir);
|
||||
}
|
||||
|
||||
closedir(dir);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static bool has_contents(
|
||||
struct command * command)
|
||||
{
|
||||
bool result = false;
|
||||
char const * contents = command->arg;
|
||||
size_t length = strlen(contents);
|
||||
|
||||
char * buffer = malloc(length);
|
||||
FILE * file = fopen(command->file, "rb");
|
||||
{
|
||||
ssize_t count = fread(buffer, 1, length, file);
|
||||
fclose(file);
|
||||
|
||||
result = (count == (ssize_t) length) && (0 == strncmp(buffer, contents, length));
|
||||
}
|
||||
|
||||
free(buffer);
|
||||
return result;
|
||||
}
|
||||
|
||||
static bool get_command(
|
||||
struct command * command,
|
||||
char const * name)
|
||||
{
|
||||
static struct {
|
||||
char const * name;
|
||||
command_invoke_fn * invoke;
|
||||
bool has_arg;
|
||||
} commands[] =
|
||||
{
|
||||
{"is_file" , &is_file , false},
|
||||
{"is_dir" , &is_dir , false},
|
||||
{"has_mode" , &has_mode , true},
|
||||
{"has_size" , &has_size , true},
|
||||
{"has_subdir" , &has_subdir , true},
|
||||
{"has_contents", &has_contents, true},
|
||||
{NULL, NULL, false}
|
||||
};
|
||||
|
||||
for (int i = 0; NULL != commands[i].name; i++)
|
||||
{
|
||||
if (0 == strcmp(commands[i].name, name))
|
||||
{
|
||||
command->invoke = commands[i].invoke;
|
||||
command->has_arg = commands[i].has_arg;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static void command_init(
|
||||
struct command * command,
|
||||
int argc,
|
||||
char * argv[])
|
||||
{
|
||||
static struct option const options[] =
|
||||
{
|
||||
{"file" , required_argument, NULL, 'f'},
|
||||
{"command", required_argument, NULL, 'c'},
|
||||
{"arg" , required_argument, NULL, 'a'},
|
||||
{"help" , no_argument , NULL, 'h'},
|
||||
{NULL, 0, NULL, 0}
|
||||
};
|
||||
|
||||
command->invoke = &print_usage;
|
||||
command->file = NULL;
|
||||
command->arg = NULL;
|
||||
command->success = true;
|
||||
command->has_arg = false;
|
||||
|
||||
optind = 0;
|
||||
bool is_finished = false;
|
||||
while (!is_finished)
|
||||
{
|
||||
int option_index = 0;
|
||||
int const c = getopt_long(argc, argv, "f:c:a:h", options, &option_index);
|
||||
|
||||
switch(c)
|
||||
{
|
||||
case -1:
|
||||
is_finished = true;
|
||||
break;
|
||||
case 'c':
|
||||
if (!get_command(command, optarg))
|
||||
{
|
||||
fprintf(stderr, "error: unknown command\n");
|
||||
command->invoke = &print_usage;
|
||||
command->success = false;
|
||||
is_finished = true;
|
||||
}
|
||||
break;
|
||||
case 'f':
|
||||
free(command->file);
|
||||
command->file = strdup(optarg);
|
||||
break;
|
||||
case 'a':
|
||||
free(command->arg);
|
||||
command->arg = strdup(optarg);
|
||||
break;
|
||||
case 'h':
|
||||
command->invoke = &print_usage;
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "error: unknown argument\n");
|
||||
command->invoke = &print_usage;
|
||||
command->success = false;
|
||||
is_finished = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (command->success)
|
||||
{
|
||||
if (NULL == command->file)
|
||||
{
|
||||
fprintf(stderr, "error: missing required arg: -f\n");
|
||||
command->invoke = &print_usage;
|
||||
command->success = false;
|
||||
}
|
||||
else if ((command->has_arg) && (NULL == command->arg))
|
||||
{
|
||||
fprintf(stderr, "error: missing required arg: -a\n");
|
||||
command->invoke = &print_usage;
|
||||
command->success = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void command_cleanup(
|
||||
struct command * command)
|
||||
{
|
||||
free(command->file);
|
||||
free(command->arg);
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
struct command command;
|
||||
command_init(&command, argc, argv);
|
||||
|
||||
bool success = command.invoke(&command);
|
||||
|
||||
command_cleanup(&command);
|
||||
return success ? EXIT_SUCCESS : EXIT_FAILURE;
|
||||
}
|
||||
148
test/webfuse_provider/tests/integration/provider.cc
Normal file
148
test/webfuse_provider/tests/integration/provider.cc
Normal file
@@ -0,0 +1,148 @@
|
||||
#include "webfuse_provider/tests/integration/provider.hpp"
|
||||
#include "webfuse_provider.h"
|
||||
#include "webfuse_provider/impl/client.h"
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <chrono>
|
||||
#include <string>
|
||||
#include "webfuse/utils/static_filesystem.h"
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
namespace
|
||||
{
|
||||
enum class ConnectionState
|
||||
{
|
||||
disconnected,
|
||||
connected,
|
||||
connecting
|
||||
};
|
||||
}
|
||||
|
||||
extern "C"
|
||||
{
|
||||
|
||||
void
|
||||
webfuse_test_provider_onconnected(
|
||||
void * user_data)
|
||||
{
|
||||
auto * fs = reinterpret_cast<wfp_static_filesystem*>(user_data);
|
||||
auto * connection_state = reinterpret_cast<ConnectionState*>(wfp_static_filesystem_get_user_data(fs));
|
||||
*connection_state = ConnectionState::connected;
|
||||
}
|
||||
|
||||
void
|
||||
webfuse_test_provider_ondisconnected(
|
||||
void * user_data)
|
||||
{
|
||||
auto * fs = reinterpret_cast<wfp_static_filesystem*>(user_data);
|
||||
auto * connection_state = reinterpret_cast<ConnectionState*>(wfp_static_filesystem_get_user_data(fs));
|
||||
*connection_state = ConnectionState::disconnected;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
class Provider::Private
|
||||
{
|
||||
public:
|
||||
explicit Private(char const * url)
|
||||
: is_shutdown_requested(false)
|
||||
, connection_state(ConnectionState::connecting)
|
||||
{
|
||||
config = wfp_client_config_create();
|
||||
wfp_client_config_set_certpath(config, "client-cert.pem");
|
||||
wfp_client_config_set_keypath(config, "client-key.pem");
|
||||
wfp_client_config_set_ca_filepath(config, "server-cert.pem");
|
||||
wfp_client_config_set_onconnected(config, &webfuse_test_provider_onconnected);
|
||||
wfp_client_config_set_ondisconnected(config, &webfuse_test_provider_ondisconnected);
|
||||
|
||||
fs = wfp_static_filesystem_create(config);
|
||||
wfp_static_filesystem_set_user_data(fs, reinterpret_cast<void*>(&connection_state));
|
||||
wfp_static_filesystem_add_text(fs, "hello.txt", 0444, "Hello, World");
|
||||
|
||||
client = wfp_client_create(config);
|
||||
wfp_client_connect(client, url);
|
||||
while (ConnectionState::connecting == connection_state)
|
||||
{
|
||||
wfp_client_service(client);
|
||||
}
|
||||
|
||||
if (ConnectionState::connected == connection_state)
|
||||
{
|
||||
thread = std::thread(Run, this);
|
||||
std::this_thread::sleep_for(200ms);
|
||||
}
|
||||
else
|
||||
{
|
||||
wfp_client_dispose(client);
|
||||
|
||||
wfp_static_filesystem_dispose(fs);
|
||||
wfp_client_config_dispose(config);
|
||||
|
||||
throw std::runtime_error("unable to connect");
|
||||
}
|
||||
}
|
||||
|
||||
~Private()
|
||||
{
|
||||
RequestShutdown();
|
||||
thread.join();
|
||||
|
||||
wfp_client_disconnect(client);
|
||||
while (ConnectionState::disconnected != connection_state)
|
||||
{
|
||||
wfp_client_service(client);
|
||||
}
|
||||
|
||||
wfp_client_dispose(client);
|
||||
|
||||
wfp_static_filesystem_dispose(fs);
|
||||
wfp_client_config_dispose(config);
|
||||
}
|
||||
|
||||
bool IsShutdownRequested()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(shutdown_lock);
|
||||
return is_shutdown_requested;
|
||||
}
|
||||
private:
|
||||
void RequestShutdown()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(shutdown_lock);
|
||||
is_shutdown_requested = true;
|
||||
wfp_client_interrupt(client);
|
||||
}
|
||||
|
||||
static void Run(Provider::Private * context)
|
||||
{
|
||||
while (!context->IsShutdownRequested())
|
||||
{
|
||||
wfp_client_service(context->client);
|
||||
}
|
||||
}
|
||||
|
||||
std::mutex shutdown_lock;
|
||||
std::thread thread;
|
||||
bool is_shutdown_requested;
|
||||
ConnectionState connection_state;
|
||||
|
||||
wfp_client_config * config;
|
||||
wfp_static_filesystem * fs;
|
||||
public:
|
||||
wfp_client * client;
|
||||
};
|
||||
|
||||
Provider::Provider(char const * url)
|
||||
: d(new Provider::Private(url))
|
||||
{
|
||||
}
|
||||
|
||||
Provider::~Provider()
|
||||
{
|
||||
delete d;
|
||||
}
|
||||
|
||||
}
|
||||
19
test/webfuse_provider/tests/integration/provider.hpp
Normal file
19
test/webfuse_provider/tests/integration/provider.hpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#ifndef WF_TEST_INTEGRATION_PROVIDER
|
||||
#define WF_TEST_INTEGRATION_PROVIDER
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
class Provider
|
||||
{
|
||||
public:
|
||||
explicit Provider(char const * url);
|
||||
~Provider();
|
||||
private:
|
||||
class Private;
|
||||
Private * d;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
154
test/webfuse_provider/tests/integration/server.cc
Normal file
154
test/webfuse_provider/tests/integration/server.cc
Normal file
@@ -0,0 +1,154 @@
|
||||
#include "webfuse_provider/tests/integration/server.hpp"
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <sstream>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include "webfuse_adapter.h"
|
||||
#include "webfuse/adapter/impl/server.h"
|
||||
|
||||
#define WF_PATH_MAX (100)
|
||||
|
||||
extern "C"
|
||||
{
|
||||
|
||||
static void webfuse_test_server_cleanup_mountpoint(
|
||||
void * user_data)
|
||||
{
|
||||
char * path = reinterpret_cast<char*>(user_data);
|
||||
rmdir(path);
|
||||
free(path);
|
||||
}
|
||||
|
||||
static struct wf_mountpoint *
|
||||
webfuse_test_server_create_mountpoint(
|
||||
char const * filesystem,
|
||||
void * user_data)
|
||||
{
|
||||
char const * base_dir = reinterpret_cast<char const*>(user_data);
|
||||
char path[WF_PATH_MAX];
|
||||
snprintf(path, WF_PATH_MAX, "%s/%s", base_dir, filesystem);
|
||||
mkdir(path, 0755);
|
||||
struct wf_mountpoint * mountpoint = wf_mountpoint_create(path);
|
||||
wf_mountpoint_set_userdata(
|
||||
mountpoint,
|
||||
reinterpret_cast<void*>(strdup(path)),
|
||||
&webfuse_test_server_cleanup_mountpoint);
|
||||
|
||||
return mountpoint;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
class Server::Private
|
||||
{
|
||||
public:
|
||||
Private()
|
||||
: is_shutdown_requested(false)
|
||||
{
|
||||
snprintf(base_dir, WF_PATH_MAX, "%s", "/tmp/webfuse_test_integration_XXXXXX");
|
||||
char const * result = mkdtemp(base_dir);
|
||||
if (NULL == result)
|
||||
{
|
||||
throw std::runtime_error("unable to create temp dir");
|
||||
}
|
||||
|
||||
|
||||
config = wf_server_config_create();
|
||||
wf_server_config_set_port(config, 0);
|
||||
wf_server_config_set_mountpoint_factory(config,
|
||||
&webfuse_test_server_create_mountpoint,
|
||||
reinterpret_cast<void*>(base_dir));
|
||||
wf_server_config_set_keypath(config, "server-key.pem");
|
||||
wf_server_config_set_certpath(config, "server-cert.pem");
|
||||
|
||||
server = wf_server_create(config);
|
||||
|
||||
while (!wf_impl_server_is_operational(server))
|
||||
{
|
||||
wf_server_service(server);
|
||||
}
|
||||
|
||||
thread = std::thread(Run, this);
|
||||
|
||||
}
|
||||
|
||||
~Private()
|
||||
{
|
||||
RequestShutdown();
|
||||
thread.join();
|
||||
rmdir(base_dir);
|
||||
wf_server_dispose(server);
|
||||
wf_server_config_dispose(config);
|
||||
}
|
||||
|
||||
bool IsShutdownRequested()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(shutdown_lock);
|
||||
return is_shutdown_requested;
|
||||
}
|
||||
|
||||
std::string GetUrl(void) const
|
||||
{
|
||||
int const port = wf_server_get_port(server);
|
||||
std::ostringstream stream;
|
||||
stream << "wss://localhost:" << port << "/";
|
||||
return stream.str();
|
||||
}
|
||||
|
||||
private:
|
||||
void RequestShutdown()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(shutdown_lock);
|
||||
is_shutdown_requested = true;
|
||||
wf_server_interrupt(server);
|
||||
}
|
||||
|
||||
static void Run(Server::Private * context)
|
||||
{
|
||||
while (!context->IsShutdownRequested())
|
||||
{
|
||||
wf_server_service(context->server);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
std::mutex shutdown_lock;
|
||||
std::thread thread;
|
||||
bool is_shutdown_requested;
|
||||
|
||||
|
||||
public:
|
||||
char base_dir[WF_PATH_MAX];
|
||||
wf_server_config * config;
|
||||
wf_server * server;
|
||||
};
|
||||
|
||||
Server::Server()
|
||||
: d(new Server::Private())
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Server::~Server()
|
||||
{
|
||||
delete d;
|
||||
}
|
||||
|
||||
char const * Server::GetBaseDir(void) const
|
||||
{
|
||||
return d->base_dir;
|
||||
}
|
||||
|
||||
std::string Server::GetUrl(void) const
|
||||
{
|
||||
return d->GetUrl();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
25
test/webfuse_provider/tests/integration/server.hpp
Normal file
25
test/webfuse_provider/tests/integration/server.hpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#ifndef WF_TEST_INTEGRATION_SERVER_HPP
|
||||
#define WF_TEST_INTEGRATION_SERVER_HPP
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
class Server
|
||||
{
|
||||
public:
|
||||
Server();
|
||||
~Server();
|
||||
void Start(void);
|
||||
void Stop(void);
|
||||
char const * GetBaseDir(void) const;
|
||||
std::string GetUrl(void) const;
|
||||
private:
|
||||
class Private;
|
||||
Private * d;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
97
test/webfuse_provider/tests/integration/test_integration.cc
Normal file
97
test/webfuse_provider/tests/integration/test_integration.cc
Normal file
@@ -0,0 +1,97 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "webfuse_provider/tests/integration/server.hpp"
|
||||
#include "webfuse_provider/tests/integration/provider.hpp"
|
||||
#include "webfuse_provider/tests/integration/file.hpp"
|
||||
|
||||
#include <cstdio>
|
||||
#include <csignal>
|
||||
#include <cstring>
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <dirent.h>
|
||||
|
||||
#include <jansson.h>
|
||||
#include "webfuse_provider/impl/lws_log.h"
|
||||
|
||||
using webfuse_test::Server;
|
||||
using webfuse_test::Provider;
|
||||
using webfuse_test::File;
|
||||
|
||||
namespace
|
||||
{
|
||||
class IntegrationTest: public ::testing::Test
|
||||
{
|
||||
public:
|
||||
IntegrationTest()
|
||||
: server(nullptr)
|
||||
, provider(nullptr)
|
||||
{
|
||||
json_object_seed(0);
|
||||
wf_lwslog_disable();
|
||||
}
|
||||
|
||||
protected:
|
||||
void SetUp()
|
||||
{
|
||||
server = new Server();
|
||||
provider = new Provider(server->GetUrl().c_str());
|
||||
}
|
||||
|
||||
void TearDown()
|
||||
{
|
||||
delete provider;
|
||||
delete server;
|
||||
}
|
||||
|
||||
char const * GetBaseDir() const
|
||||
{
|
||||
return server->GetBaseDir();
|
||||
}
|
||||
private:
|
||||
Server * server;
|
||||
Provider * provider;
|
||||
};
|
||||
}
|
||||
|
||||
TEST_F(IntegrationTest, HasMountpoint)
|
||||
{
|
||||
struct stat buffer;
|
||||
int rc = stat(GetBaseDir(), &buffer);
|
||||
|
||||
ASSERT_EQ(0, rc);
|
||||
ASSERT_TRUE(S_ISDIR(buffer.st_mode));
|
||||
}
|
||||
|
||||
TEST_F(IntegrationTest, ProvidesTextFile)
|
||||
{
|
||||
std::string file_name = std::string(GetBaseDir()) + "/cprovider/hello.txt";
|
||||
|
||||
File file(file_name);
|
||||
ASSERT_TRUE(file.isFile());
|
||||
ASSERT_TRUE(file.hasAccessRights(0444));
|
||||
ASSERT_TRUE(file.hasSize(12));
|
||||
}
|
||||
|
||||
TEST_F(IntegrationTest, ReadTextFile)
|
||||
{
|
||||
std::string file_name = std::string(GetBaseDir()) + "/cprovider/hello.txt";
|
||||
|
||||
File file(file_name);
|
||||
ASSERT_TRUE(file.hasContents("Hello, World"));
|
||||
}
|
||||
|
||||
TEST_F(IntegrationTest, ReadDir)
|
||||
{
|
||||
std::string dir_name = std::string(GetBaseDir()) + "/cprovider";
|
||||
|
||||
File dir(dir_name);
|
||||
ASSERT_TRUE(dir.isDirectory());
|
||||
ASSERT_TRUE(dir.hasSubdirectory("."));
|
||||
ASSERT_TRUE(dir.hasSubdirectory(".."));
|
||||
ASSERT_TRUE(dir.hasSubdirectory("hello.txt"));
|
||||
ASSERT_FALSE(dir.hasSubdirectory("other"));
|
||||
}
|
||||
112
test/webfuse_provider/tests/integration/test_lowlevel.cc
Normal file
112
test/webfuse_provider/tests/integration/test_lowlevel.cc
Normal file
@@ -0,0 +1,112 @@
|
||||
#include "webfuse_adapter.h"
|
||||
#include "webfuse_provider.h"
|
||||
#include <libwebsockets.h>
|
||||
|
||||
#include "webfuse/utils/tempdir.hpp"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
using ::webfuse_test::TempDir;
|
||||
|
||||
extern "C"
|
||||
{
|
||||
|
||||
wf_mountpoint *
|
||||
wf_test_integration_lowlevel_create_mountpoint(
|
||||
char const *, void * user_data)
|
||||
{
|
||||
auto * tempDir = reinterpret_cast<TempDir*>(user_data);
|
||||
return wf_mountpoint_create(tempDir->path());
|
||||
}
|
||||
|
||||
void
|
||||
wf_test_integration_lowlevel_on_connected(
|
||||
void * user_data)
|
||||
{
|
||||
int * state = reinterpret_cast<int*>(user_data);
|
||||
*state = 1;
|
||||
}
|
||||
|
||||
void
|
||||
wf_test_integration_lowlevel_on_disconnected(
|
||||
void * user_data)
|
||||
{
|
||||
int * state = reinterpret_cast<int*>(user_data);
|
||||
*state = -1;
|
||||
}
|
||||
|
||||
bool
|
||||
wf_test_integration_lowlevel_authenticate(
|
||||
struct wf_credentials const * credentials,
|
||||
void * )
|
||||
{
|
||||
char const * username = wf_credentials_get(credentials, "username");
|
||||
char const * password = wf_credentials_get(credentials, "password");
|
||||
|
||||
return ((0 == strcmp(username, "bob")) && (0 == strcmp(password, "secret")));
|
||||
}
|
||||
|
||||
void
|
||||
wf_test_integration_lowlevel_get_credentials(
|
||||
struct wfp_credentials * credentials,
|
||||
void * )
|
||||
{
|
||||
wfp_credentials_set_type(credentials, "username");
|
||||
wfp_credentials_add(credentials, "username", "bob");
|
||||
wfp_credentials_add(credentials, "password", "secret");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
TEST(integration, lowlevel)
|
||||
{
|
||||
TempDir dir("wf_test");
|
||||
|
||||
wf_server_protocol * server_protocol = wf_server_protocol_create(
|
||||
&wf_test_integration_lowlevel_create_mountpoint,
|
||||
reinterpret_cast<void*>(&dir));
|
||||
ASSERT_NE(nullptr, server_protocol);
|
||||
wf_server_protocol_add_authenticator(server_protocol, "username",
|
||||
&wf_test_integration_lowlevel_authenticate, nullptr);
|
||||
|
||||
int state = 0;
|
||||
wfp_client_config * client_config = wfp_client_config_create();
|
||||
ASSERT_NE(nullptr, client_config);
|
||||
wfp_client_config_set_userdata(client_config, reinterpret_cast<void*>(&state));
|
||||
wfp_client_config_set_onconnected(client_config, &wf_test_integration_lowlevel_on_connected);
|
||||
wfp_client_config_set_ondisconnected(client_config, &wf_test_integration_lowlevel_on_disconnected);
|
||||
wfp_client_config_enable_authentication(client_config, &wf_test_integration_lowlevel_get_credentials);
|
||||
|
||||
wfp_client_protocol * client_protocol = wfp_client_protocol_create(client_config);
|
||||
ASSERT_NE(nullptr, client_protocol);
|
||||
|
||||
lws_protocols protocols[3];
|
||||
memset(protocols, 0, 3 * sizeof(lws_protocols));
|
||||
wf_server_protocol_init_lws(server_protocol, &protocols[0]);
|
||||
wfp_client_protocol_init_lws(client_protocol, &protocols[1]);
|
||||
|
||||
lws_context_creation_info info;
|
||||
memset(&info, 0, sizeof(info));
|
||||
info.port = 8080;
|
||||
info.protocols = protocols;
|
||||
info.vhost_name = "localhost";
|
||||
info.ws_ping_pong_interval = 10;
|
||||
info.options = LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE;
|
||||
|
||||
struct lws_context * context = lws_create_context(&info);
|
||||
ASSERT_NE(nullptr, context);
|
||||
|
||||
wfp_client_protocol_connect(client_protocol, context, "ws://localhost:8080/");
|
||||
while (0 == state)
|
||||
{
|
||||
lws_service(context, 0);
|
||||
}
|
||||
|
||||
EXPECT_EQ(1, state);
|
||||
|
||||
lws_context_destroy(context);
|
||||
|
||||
wfp_client_protocol_dispose(client_protocol);
|
||||
wfp_client_config_dispose(client_config);
|
||||
wf_server_protocol_dispose(server_protocol);
|
||||
}
|
||||
99
test/webfuse_provider/tests/provider/operation/test_close.cc
Normal file
99
test/webfuse_provider/tests/provider/operation/test_close.cc
Normal file
@@ -0,0 +1,99 @@
|
||||
#include "webfuse_provider/impl/operation/close.h"
|
||||
#include "webfuse_provider/mocks/mock_provider.hpp"
|
||||
#include "webfuse_provider/mocks/fake_invokation_context.hpp"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
using ::webfuse_test::MockProvider;
|
||||
using ::webfuse_test::create_context;
|
||||
using ::testing::_;
|
||||
|
||||
TEST(wfp_close, close)
|
||||
{
|
||||
int inode = 42;
|
||||
int handle = 0xdeadbeef;
|
||||
int flags = 23;
|
||||
|
||||
MockProvider provider;
|
||||
EXPECT_CALL(provider, close(inode, handle, flags)).Times(1);
|
||||
|
||||
wfp_impl_invokation_context context = create_context(provider);
|
||||
|
||||
json_t * params = json_array();
|
||||
json_array_append_new(params, json_string("test.filesystem"));
|
||||
json_array_append_new(params, json_integer(inode));
|
||||
json_array_append_new(params, json_integer(handle));
|
||||
json_array_append_new(params, json_integer(flags));
|
||||
|
||||
wfp_impl_close(&context, params, 42);
|
||||
json_decref(params);
|
||||
}
|
||||
|
||||
TEST(wfp_close, close_fail_invalid_param_count)
|
||||
{
|
||||
MockProvider provider;
|
||||
EXPECT_CALL(provider, close(_,_,_)).Times(0);
|
||||
|
||||
wfp_impl_invokation_context context = create_context(provider);
|
||||
|
||||
json_t * params = json_array();
|
||||
wfp_impl_close(&context, params, 42);
|
||||
json_decref(params);
|
||||
}
|
||||
|
||||
TEST(wfp_close, close_fail_inode_invalid_type)
|
||||
{
|
||||
MockProvider provider;
|
||||
EXPECT_CALL(provider, close(_,_,_)).Times(0);
|
||||
|
||||
wfp_impl_invokation_context context = create_context(provider);
|
||||
|
||||
json_t * params = json_array();
|
||||
json_array_append_new(params, json_string("test.filesystem"));
|
||||
json_array_append_new(params, json_string("42"));
|
||||
json_array_append_new(params, json_integer(0));
|
||||
json_array_append_new(params, json_integer(0));
|
||||
|
||||
wfp_impl_close(&context, params, 42);
|
||||
json_decref(params);
|
||||
}
|
||||
|
||||
TEST(wfp_close, close_fail_handle_invalid_type)
|
||||
{
|
||||
MockProvider provider;
|
||||
EXPECT_CALL(provider, close(_,_,_)).Times(0);
|
||||
|
||||
wfp_impl_invokation_context context = create_context(provider);
|
||||
|
||||
json_t * params = json_array();
|
||||
json_array_append_new(params, json_string("test.filesystem"));
|
||||
json_array_append_new(params, json_integer(0));
|
||||
json_array_append_new(params, json_string("42"));
|
||||
json_array_append_new(params, json_integer(0));
|
||||
|
||||
wfp_impl_close(&context, params, 42);
|
||||
json_decref(params);
|
||||
}
|
||||
|
||||
TEST(wfp_close, close_fail_flags_invalid_type)
|
||||
{
|
||||
MockProvider provider;
|
||||
EXPECT_CALL(provider, close(_,_,_)).Times(0);
|
||||
|
||||
wfp_impl_invokation_context context = create_context(provider);
|
||||
|
||||
json_t * params = json_array();
|
||||
json_array_append_new(params, json_string("test.filesystem"));
|
||||
json_array_append_new(params, json_integer(0));
|
||||
json_array_append_new(params, json_integer(0));
|
||||
json_array_append_new(params, json_string("42"));
|
||||
|
||||
wfp_impl_close(&context, params, 42);
|
||||
json_decref(params);
|
||||
}
|
||||
|
||||
|
||||
TEST(wfp_close, default_nop)
|
||||
{
|
||||
wfp_impl_close_default(0, 0, 0, nullptr);
|
||||
}
|
||||
107
test/webfuse_provider/tests/provider/operation/test_getattr.cc
Normal file
107
test/webfuse_provider/tests/provider/operation/test_getattr.cc
Normal file
@@ -0,0 +1,107 @@
|
||||
#include "webfuse_provider/impl/operation/getattr.h"
|
||||
#include "webfuse_provider/mocks/mock_request.hpp"
|
||||
#include "webfuse_provider/mocks/mock_provider.hpp"
|
||||
#include "webfuse_provider/mocks/fake_invokation_context.hpp"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <cstdlib>
|
||||
|
||||
using ::webfuse_test::MockProvider;
|
||||
using ::webfuse_test::MockRequest;
|
||||
using ::webfuse_test::StatMatcher;
|
||||
using ::webfuse_test::create_context;
|
||||
using ::testing::_;
|
||||
using ::testing::Invoke;
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
void free_request(wfp_request * request, ino_t)
|
||||
{
|
||||
free(request);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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(StatMatcher(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(StatMatcher(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);
|
||||
}
|
||||
|
||||
TEST(wfp_impl_getattr, invoke_provider)
|
||||
{
|
||||
ino_t inode = 23;
|
||||
MockProvider provider;
|
||||
EXPECT_CALL(provider,getattr(_, inode)).Times(1).WillOnce(Invoke(free_request));
|
||||
|
||||
wfp_request request = {nullptr, nullptr, 0};
|
||||
wfp_impl_invokation_context context = create_context(provider, &request);
|
||||
|
||||
json_t * params = json_array();
|
||||
json_array_append_new(params, json_string("test.filesystem"));
|
||||
json_array_append_new(params, json_integer(inode));
|
||||
|
||||
wfp_impl_getattr(&context, params, 42);
|
||||
json_decref(params);
|
||||
}
|
||||
|
||||
TEST(wfp_impl_getattr, fail_invalid_param_count)
|
||||
{
|
||||
MockProvider provider;
|
||||
EXPECT_CALL(provider,getattr(_, _)).Times(0);
|
||||
|
||||
wfp_request request = {nullptr, nullptr, 0};
|
||||
wfp_impl_invokation_context context = create_context(provider, &request);
|
||||
|
||||
json_t * params = json_array();
|
||||
json_array_append_new(params, json_string("test.filesystem"));
|
||||
|
||||
wfp_impl_getattr(&context, params, 42);
|
||||
json_decref(params);
|
||||
}
|
||||
|
||||
TEST(wfp_impl_getattr, fail_invalid_inode_type)
|
||||
{
|
||||
MockProvider provider;
|
||||
EXPECT_CALL(provider,getattr(_, _)).Times(0);
|
||||
|
||||
wfp_request request = {nullptr, nullptr, 0};
|
||||
wfp_impl_invokation_context context = create_context(provider, &request);
|
||||
|
||||
json_t * params = json_array();
|
||||
json_array_append_new(params, json_string("test.filesystem"));
|
||||
json_array_append_new(params, json_string("42"));
|
||||
|
||||
wfp_impl_getattr(&context, params, 42);
|
||||
json_decref(params);
|
||||
}
|
||||
129
test/webfuse_provider/tests/provider/operation/test_lookup.cc
Normal file
129
test/webfuse_provider/tests/provider/operation/test_lookup.cc
Normal file
@@ -0,0 +1,129 @@
|
||||
#include "webfuse_provider/impl/operation/lookup.h"
|
||||
#include "webfuse_provider/mocks/mock_request.hpp"
|
||||
#include "webfuse_provider/mocks/mock_provider.hpp"
|
||||
#include "webfuse_provider/mocks/fake_invokation_context.hpp"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <cstdlib>
|
||||
|
||||
using ::webfuse_test::MockProvider;
|
||||
using ::webfuse_test::MockRequest;
|
||||
using ::webfuse_test::StatMatcher;
|
||||
using ::webfuse_test::create_context;
|
||||
using ::testing::_;
|
||||
using ::testing::Invoke;
|
||||
using ::testing::StrEq;
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
void free_request(wfp_request * request, ino_t, char const *)
|
||||
{
|
||||
free(request);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
TEST(wfp_impl_lookup, invoke_provider)
|
||||
{
|
||||
ino_t inode = 42;
|
||||
MockProvider provider;
|
||||
EXPECT_CALL(provider,lookup(_, inode,StrEq("some.file"))).Times(1)
|
||||
.WillOnce(Invoke(free_request));
|
||||
|
||||
wfp_request request = {nullptr, nullptr, 0};
|
||||
wfp_impl_invokation_context context = create_context(provider, &request);
|
||||
|
||||
json_t * params = json_array();
|
||||
json_array_append_new(params, json_string("test.filesystem"));
|
||||
json_array_append_new(params, json_integer(inode));
|
||||
json_array_append_new(params, json_string("some.file"));
|
||||
|
||||
wfp_impl_lookup(&context, params, 42);
|
||||
json_decref(params);
|
||||
}
|
||||
|
||||
TEST(wfp_impl_lookup, fail_invalid_param_count)
|
||||
{
|
||||
MockProvider provider;
|
||||
EXPECT_CALL(provider,lookup(_, _,_)).Times(0);
|
||||
|
||||
wfp_request request = {nullptr, nullptr, 0};
|
||||
wfp_impl_invokation_context context = create_context(provider, &request);
|
||||
|
||||
json_t * params = json_array();
|
||||
json_array_append_new(params, json_string("test.filesystem"));
|
||||
json_array_append_new(params, json_integer(23));
|
||||
|
||||
wfp_impl_lookup(&context, params, 42);
|
||||
json_decref(params);
|
||||
}
|
||||
|
||||
TEST(wfp_impl_lookup, fail_invalid_inode_type)
|
||||
{
|
||||
MockProvider provider;
|
||||
EXPECT_CALL(provider,lookup(_, _,_)).Times(0);
|
||||
|
||||
wfp_request request = {nullptr, nullptr, 0};
|
||||
wfp_impl_invokation_context context = create_context(provider, &request);
|
||||
|
||||
json_t * params = json_array();
|
||||
json_array_append_new(params, json_string("test.filesystem"));
|
||||
json_array_append_new(params, json_string("23"));
|
||||
json_array_append_new(params, json_string("some.file"));
|
||||
|
||||
wfp_impl_lookup(&context, params, 42);
|
||||
json_decref(params);
|
||||
}
|
||||
|
||||
TEST(wfp_impl_lookup, fail_invalid_name_type)
|
||||
{
|
||||
MockProvider provider;
|
||||
EXPECT_CALL(provider,lookup(_, _,_)).Times(0);
|
||||
|
||||
wfp_request request = {nullptr, nullptr, 0};
|
||||
wfp_impl_invokation_context context = create_context(provider, &request);
|
||||
|
||||
json_t * params = json_array();
|
||||
json_array_append_new(params, json_string("test.filesystem"));
|
||||
json_array_append_new(params, json_integer(23));
|
||||
json_array_append_new(params, json_integer(1));
|
||||
|
||||
wfp_impl_lookup(&context, params, 42);
|
||||
json_decref(params);
|
||||
}
|
||||
|
||||
TEST(wfp_impl_lookup, default_responds_error)
|
||||
{
|
||||
MockRequest request;
|
||||
auto * req = request.create_request(42);
|
||||
EXPECT_CALL(request, respond_error(_,42)).Times(1);
|
||||
|
||||
wfp_impl_lookup_default(req, 1, "some.file", nullptr);
|
||||
}
|
||||
|
||||
TEST(wfp_impl_lookup, respond_file)
|
||||
{
|
||||
MockRequest request;
|
||||
auto * req = request.create_request(42);
|
||||
EXPECT_CALL(request, respond(StatMatcher(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_lookup(req, &buffer);
|
||||
}
|
||||
|
||||
TEST(wfp_impl_lookup, respond_dir)
|
||||
{
|
||||
MockRequest request;
|
||||
auto * req = request.create_request(42);
|
||||
EXPECT_CALL(request, respond(StatMatcher(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_lookup(req, &buffer);
|
||||
}
|
||||
114
test/webfuse_provider/tests/provider/operation/test_open.cc
Normal file
114
test/webfuse_provider/tests/provider/operation/test_open.cc
Normal file
@@ -0,0 +1,114 @@
|
||||
#include "webfuse_provider/impl/operation/open.h"
|
||||
#include "webfuse_provider/mocks/mock_request.hpp"
|
||||
#include "webfuse_provider/mocks/mock_provider.hpp"
|
||||
#include "webfuse_provider/mocks/fake_invokation_context.hpp"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <cstdlib>
|
||||
|
||||
using ::webfuse_test::MockProvider;
|
||||
using ::webfuse_test::MockRequest;
|
||||
using ::webfuse_test::OpenMatcher;
|
||||
using ::webfuse_test::create_context;
|
||||
using ::testing::_;
|
||||
using ::testing::Invoke;
|
||||
using ::testing::StrEq;
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
void free_request(wfp_request * request, ino_t, int)
|
||||
{
|
||||
free(request);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
TEST(wfp_impl_open, invoke_provider)
|
||||
{
|
||||
ino_t inode = 42;
|
||||
int flags = 0;
|
||||
MockProvider provider;
|
||||
EXPECT_CALL(provider,open(_, inode, flags)).Times(1)
|
||||
.WillOnce(Invoke(free_request));
|
||||
|
||||
wfp_request request = {nullptr, nullptr, 0};
|
||||
wfp_impl_invokation_context context = create_context(provider, &request);
|
||||
|
||||
json_t * params = json_array();
|
||||
json_array_append_new(params, json_string("test.filesystem"));
|
||||
json_array_append_new(params, json_integer(inode));
|
||||
json_array_append_new(params, json_integer(flags));
|
||||
|
||||
wfp_impl_open(&context, params, 42);
|
||||
json_decref(params);
|
||||
}
|
||||
|
||||
TEST(wfp_impl_open, fail_invalid_param_count)
|
||||
{
|
||||
MockProvider provider;
|
||||
EXPECT_CALL(provider,open(_, _, _)).Times(0);
|
||||
|
||||
wfp_request request = {nullptr, nullptr, 0};
|
||||
wfp_impl_invokation_context context = create_context(provider, &request);
|
||||
|
||||
json_t * params = json_array();
|
||||
json_array_append_new(params, json_string("test.filesystem"));
|
||||
json_array_append_new(params, json_integer(23));
|
||||
|
||||
wfp_impl_open(&context, params, 42);
|
||||
json_decref(params);
|
||||
}
|
||||
|
||||
TEST(wfp_impl_open, fail_invalid_inode_type)
|
||||
{
|
||||
MockProvider provider;
|
||||
EXPECT_CALL(provider,open(_, _, _)).Times(0);
|
||||
|
||||
wfp_request request = {nullptr, nullptr, 0};
|
||||
wfp_impl_invokation_context context = create_context(provider, &request);
|
||||
|
||||
json_t * params = json_array();
|
||||
json_array_append_new(params, json_string("test.filesystem"));
|
||||
json_array_append_new(params, json_string(""));
|
||||
json_array_append_new(params, json_integer(0));
|
||||
|
||||
wfp_impl_open(&context, params, 42);
|
||||
json_decref(params);
|
||||
}
|
||||
|
||||
TEST(wfp_impl_open, fail_invalid_flags_type)
|
||||
{
|
||||
MockProvider provider;
|
||||
EXPECT_CALL(provider,open(_, _, _)).Times(0);
|
||||
|
||||
wfp_request request = {nullptr, nullptr, 0};
|
||||
wfp_impl_invokation_context context = create_context(provider, &request);
|
||||
|
||||
json_t * params = json_array();
|
||||
json_array_append_new(params, json_string("test.filesystem"));
|
||||
json_array_append_new(params, json_integer(23));
|
||||
json_array_append_new(params, json_string(""));
|
||||
|
||||
wfp_impl_open(&context, params, 42);
|
||||
json_decref(params);
|
||||
}
|
||||
|
||||
TEST(wfp_impl_open, default_responds_error)
|
||||
{
|
||||
MockRequest request;
|
||||
auto * req = request.create_request(42);
|
||||
EXPECT_CALL(request, respond_error(_,42)).Times(1);
|
||||
|
||||
wfp_impl_open_default(req, 1, 0, nullptr);
|
||||
}
|
||||
|
||||
TEST(wfp_impl_open, respond)
|
||||
{
|
||||
MockRequest request;
|
||||
auto * req = request.create_request(42);
|
||||
EXPECT_CALL(request, respond(OpenMatcher(23), 42)).Times(1);
|
||||
|
||||
wfp_impl_respond_open(req, 23);
|
||||
}
|
||||
|
||||
173
test/webfuse_provider/tests/provider/operation/test_read.cc
Normal file
173
test/webfuse_provider/tests/provider/operation/test_read.cc
Normal file
@@ -0,0 +1,173 @@
|
||||
#include "webfuse_provider/impl/operation/read.h"
|
||||
#include "webfuse_provider/mocks/mock_request.hpp"
|
||||
#include "webfuse_provider/mocks/mock_provider.hpp"
|
||||
#include "webfuse_provider/mocks/fake_invokation_context.hpp"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <cstdlib>
|
||||
|
||||
using ::webfuse_test::MockProvider;
|
||||
using ::webfuse_test::MockRequest;
|
||||
using ::webfuse_test::ReadResultMatcher;
|
||||
using ::webfuse_test::create_context;
|
||||
using ::testing::_;
|
||||
using ::testing::Invoke;
|
||||
using ::testing::StrEq;
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
void free_request(wfp_request * request, ino_t, uint32_t, size_t ,size_t)
|
||||
{
|
||||
free(request);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
TEST(wfp_impl_read, invoke_provider)
|
||||
{
|
||||
ino_t inode = 42;
|
||||
uint32_t handle = 5;
|
||||
size_t offset = 2;
|
||||
size_t length = 1;
|
||||
MockProvider provider;
|
||||
EXPECT_CALL(provider, read(_, inode, handle, offset, length)).Times(1)
|
||||
.WillOnce(Invoke(free_request));
|
||||
|
||||
wfp_request request = {nullptr, nullptr, 0};
|
||||
wfp_impl_invokation_context context = create_context(provider, &request);
|
||||
|
||||
json_t * params = json_array();
|
||||
json_array_append_new(params, json_string("test.filesystem"));
|
||||
json_array_append_new(params, json_integer(inode));
|
||||
json_array_append_new(params, json_integer(handle));
|
||||
json_array_append_new(params, json_integer(offset));
|
||||
json_array_append_new(params, json_integer(length));
|
||||
|
||||
wfp_impl_read(&context, params, 42);
|
||||
json_decref(params);
|
||||
}
|
||||
|
||||
TEST(wfp_impl_read, fail_invalid_param_count)
|
||||
{
|
||||
MockProvider provider;
|
||||
EXPECT_CALL(provider, read(_, _, _, _, _)).Times(0);
|
||||
|
||||
wfp_request request = {nullptr, nullptr, 0};
|
||||
wfp_impl_invokation_context context = create_context(provider, &request);
|
||||
|
||||
json_t * params = json_array();
|
||||
json_array_append_new(params, json_string("test.filesystem"));
|
||||
json_array_append_new(params, json_integer(1));
|
||||
json_array_append_new(params, json_integer(2));
|
||||
json_array_append_new(params, json_integer(3));
|
||||
json_array_append_new(params, json_integer(4));
|
||||
json_array_append_new(params, json_integer(5));
|
||||
|
||||
wfp_impl_read(&context, params, 42);
|
||||
json_decref(params);
|
||||
}
|
||||
|
||||
TEST(wfp_impl_read, fail_invalid_inode_type)
|
||||
{
|
||||
MockProvider provider;
|
||||
EXPECT_CALL(provider, read(_, _, _, _, _)).Times(0);
|
||||
|
||||
wfp_request request = {nullptr, nullptr, 0};
|
||||
wfp_impl_invokation_context context = create_context(provider, &request);
|
||||
|
||||
json_t * params = json_array();
|
||||
json_array_append_new(params, json_string("test.filesystem"));
|
||||
json_array_append_new(params, json_string("42"));
|
||||
json_array_append_new(params, json_integer(2));
|
||||
json_array_append_new(params, json_integer(3));
|
||||
json_array_append_new(params, json_integer(4));
|
||||
|
||||
wfp_impl_read(&context, params, 42);
|
||||
json_decref(params);
|
||||
}
|
||||
|
||||
TEST(wfp_impl_read, fail_invalid_handle_type)
|
||||
{
|
||||
MockProvider provider;
|
||||
EXPECT_CALL(provider, read(_, _, _, _, _)).Times(0);
|
||||
|
||||
wfp_request request = {nullptr, nullptr, 0};
|
||||
wfp_impl_invokation_context context = create_context(provider, &request);
|
||||
|
||||
json_t * params = json_array();
|
||||
json_array_append_new(params, json_string("test.filesystem"));
|
||||
json_array_append_new(params, json_integer(1));
|
||||
json_array_append_new(params, json_string("42"));
|
||||
json_array_append_new(params, json_integer(3));
|
||||
json_array_append_new(params, json_integer(4));
|
||||
|
||||
wfp_impl_read(&context, params, 42);
|
||||
json_decref(params);
|
||||
}
|
||||
|
||||
TEST(wfp_impl_read, fail_invalid_offset_type)
|
||||
{
|
||||
MockProvider provider;
|
||||
EXPECT_CALL(provider, read(_, _, _, _, _)).Times(0);
|
||||
|
||||
wfp_request request = {nullptr, nullptr, 0};
|
||||
wfp_impl_invokation_context context = create_context(provider, &request);
|
||||
|
||||
json_t * params = json_array();
|
||||
json_array_append_new(params, json_string("test.filesystem"));
|
||||
json_array_append_new(params, json_integer(1));
|
||||
json_array_append_new(params, json_integer(2));
|
||||
json_array_append_new(params, json_string("42"));
|
||||
json_array_append_new(params, json_integer(4));
|
||||
|
||||
wfp_impl_read(&context, params, 42);
|
||||
json_decref(params);
|
||||
}
|
||||
|
||||
TEST(wfp_impl_read, fail_invalid_length_type)
|
||||
{
|
||||
MockProvider provider;
|
||||
EXPECT_CALL(provider, read(_, _, _, _, _)).Times(0);
|
||||
|
||||
wfp_request request = {nullptr, nullptr, 0};
|
||||
wfp_impl_invokation_context context = create_context(provider, &request);
|
||||
|
||||
json_t * params = json_array();
|
||||
json_array_append_new(params, json_string("test.filesystem"));
|
||||
json_array_append_new(params, json_integer(1));
|
||||
json_array_append_new(params, json_integer(2));
|
||||
json_array_append_new(params, json_integer(3));
|
||||
json_array_append_new(params, json_string("42"));
|
||||
|
||||
wfp_impl_read(&context, params, 42);
|
||||
json_decref(params);
|
||||
}
|
||||
|
||||
TEST(wfp_impl_read, default_responds_error)
|
||||
{
|
||||
MockRequest request;
|
||||
auto * req = request.create_request(42);
|
||||
EXPECT_CALL(request, respond_error(_,42)).Times(1);
|
||||
|
||||
wfp_impl_read_default(req, 0, 0, 1, 2, nullptr);
|
||||
}
|
||||
|
||||
TEST(wfp_impl_read, respond)
|
||||
{
|
||||
MockRequest request;
|
||||
auto * req = request.create_request(42);
|
||||
EXPECT_CALL(request, respond(ReadResultMatcher("d2Y=", "base64", 2), 42)).Times(1);
|
||||
|
||||
char const data[] = "wf";
|
||||
wfp_impl_respond_read(req, data, 2);
|
||||
}
|
||||
|
||||
TEST(wfp_impl_read, respond_empty)
|
||||
{
|
||||
MockRequest request;
|
||||
auto * req = request.create_request(42);
|
||||
EXPECT_CALL(request, respond(ReadResultMatcher("", "identity", 0), 42)).Times(1);
|
||||
|
||||
wfp_impl_respond_read(req, nullptr, 0);
|
||||
}
|
||||
100
test/webfuse_provider/tests/provider/operation/test_readdir.cc
Normal file
100
test/webfuse_provider/tests/provider/operation/test_readdir.cc
Normal file
@@ -0,0 +1,100 @@
|
||||
#include "webfuse_provider/impl/operation/readdir.h"
|
||||
#include "webfuse_provider/mocks/mock_request.hpp"
|
||||
#include "webfuse_provider/mocks/mock_provider.hpp"
|
||||
#include "webfuse_provider/mocks/fake_invokation_context.hpp"
|
||||
#include "webfuse_provider/dirbuffer.h"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <cstdlib>
|
||||
|
||||
using ::webfuse_test::MockProvider;
|
||||
using ::webfuse_test::MockRequest;
|
||||
using ::webfuse_test::ReaddirMatcher;
|
||||
using ::webfuse_test::create_context;
|
||||
using ::testing::_;
|
||||
using ::testing::Invoke;
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
void free_request(wfp_request * request, ino_t)
|
||||
{
|
||||
free(request);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
TEST(wfp_impl_readdir, invoke_provider)
|
||||
{
|
||||
ino_t inode = 23;
|
||||
MockProvider provider;
|
||||
EXPECT_CALL(provider,readdir(_, inode)).Times(1).WillOnce(Invoke(free_request));
|
||||
|
||||
wfp_request request = {nullptr, nullptr, 0};
|
||||
wfp_impl_invokation_context context = create_context(provider, &request);
|
||||
|
||||
json_t * params = json_array();
|
||||
json_array_append_new(params, json_string("test.filesystem"));
|
||||
json_array_append_new(params, json_integer(inode));
|
||||
|
||||
wfp_impl_readdir(&context, params, 42);
|
||||
json_decref(params);
|
||||
}
|
||||
|
||||
TEST(wfp_impl_readdir, fail_invalid_param_count)
|
||||
{
|
||||
MockProvider provider;
|
||||
EXPECT_CALL(provider,readdir(_, _)).Times(0);
|
||||
|
||||
wfp_request request = {nullptr, nullptr, 0};
|
||||
wfp_impl_invokation_context context = create_context(provider, &request);
|
||||
|
||||
json_t * params = json_array();
|
||||
json_array_append_new(params, json_string("test.filesystem"));
|
||||
json_array_append_new(params, json_integer(1));
|
||||
json_array_append_new(params, json_integer(1));
|
||||
|
||||
wfp_impl_readdir(&context, params, 42);
|
||||
json_decref(params);
|
||||
}
|
||||
|
||||
TEST(wfp_impl_readdir, fail_invalid_inode_type)
|
||||
{
|
||||
MockProvider provider;
|
||||
EXPECT_CALL(provider,readdir(_, _)).Times(0);
|
||||
|
||||
wfp_request request = {nullptr, nullptr, 0};
|
||||
wfp_impl_invokation_context context = create_context(provider, &request);
|
||||
|
||||
json_t * params = json_array();
|
||||
json_array_append_new(params, json_string("test.filesystem"));
|
||||
json_array_append_new(params, json_string("1"));
|
||||
|
||||
wfp_impl_readdir(&context, params, 42);
|
||||
json_decref(params);
|
||||
}
|
||||
|
||||
TEST(wfp_impl_readdir, default_responds_error)
|
||||
{
|
||||
MockRequest request;
|
||||
auto * req = request.create_request(42);
|
||||
EXPECT_CALL(request, respond_error(_,42)).Times(1);
|
||||
|
||||
wfp_impl_readdir_default(req, 0, nullptr);
|
||||
}
|
||||
|
||||
TEST(wfp_impl_readdir, respond)
|
||||
{
|
||||
char const item0[] = "some.file";
|
||||
char const * items[] = { item0, nullptr };
|
||||
|
||||
MockRequest request;
|
||||
auto * req = request.create_request(42);
|
||||
EXPECT_CALL(request, respond(ReaddirMatcher(items), 42)).Times(1);
|
||||
|
||||
wfp_dirbuffer * buffer = wfp_dirbuffer_create();
|
||||
wfp_dirbuffer_add(buffer, item0, 42);
|
||||
wfp_impl_respond_readdir(req, buffer);
|
||||
wfp_dirbuffer_dispose(buffer);
|
||||
|
||||
}
|
||||
268
test/webfuse_provider/tests/provider/test_client_protocol.cc
Normal file
268
test/webfuse_provider/tests/provider/test_client_protocol.cc
Normal file
@@ -0,0 +1,268 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <gmock/gmock.h>
|
||||
|
||||
#include <webfuse_provider/client_protocol.h>
|
||||
#include <webfuse_provider/client_config.h>
|
||||
#include "webfuse_provider/utils/ws_server.h"
|
||||
#include "webfuse_provider/mocks/mock_provider_client.hpp"
|
||||
#include "webfuse_provider/protocol_names.h"
|
||||
#include "webfuse_provider/utils/timeout_watcher.hpp"
|
||||
|
||||
#include <libwebsockets.h>
|
||||
|
||||
#include <cstring>
|
||||
#include <thread>
|
||||
#include <atomic>
|
||||
|
||||
using webfuse_test::WsServer;
|
||||
using webfuse_test::MockProviderClient;
|
||||
using webfuse_test::IProviderClient;
|
||||
using webfuse_test::TimeoutWatcher;
|
||||
using testing::_;
|
||||
using testing::AtMost;
|
||||
using testing::Invoke;
|
||||
|
||||
#define DEFAULT_TIMEOUT (std::chrono::milliseconds(5 * 1000))
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
class ClientProtocolFixture
|
||||
{
|
||||
ClientProtocolFixture(ClientProtocolFixture const &) = delete;
|
||||
ClientProtocolFixture& operator=(ClientProtocolFixture const &) = delete;
|
||||
public:
|
||||
explicit ClientProtocolFixture(IProviderClient& client, bool enableAuthentication = false)
|
||||
{
|
||||
server = new WsServer(WF_PROTOCOL_NAME_ADAPTER_SERVER);
|
||||
|
||||
config = wfp_client_config_create();
|
||||
client.AttachTo(config, enableAuthentication);
|
||||
protocol = wfp_client_protocol_create(config);
|
||||
|
||||
memset(protocols, 0, sizeof(struct lws_protocols) * 2);
|
||||
wfp_client_protocol_init_lws(protocol, protocols);
|
||||
|
||||
memset(&info, 0, sizeof(struct lws_context_creation_info));
|
||||
info.port = CONTEXT_PORT_NO_LISTEN;
|
||||
info.protocols = protocols;
|
||||
info.uid = -1;
|
||||
info.gid = -1;
|
||||
|
||||
context = lws_create_context(&info);
|
||||
}
|
||||
|
||||
~ClientProtocolFixture()
|
||||
{
|
||||
lws_context_destroy(context);
|
||||
wfp_client_protocol_dispose(protocol);
|
||||
wfp_client_config_dispose(config);
|
||||
delete server;
|
||||
}
|
||||
|
||||
void Connect()
|
||||
{
|
||||
TimeoutWatcher watcher(DEFAULT_TIMEOUT);
|
||||
|
||||
wfp_client_protocol_connect(protocol, context, server->GetUrl().c_str());
|
||||
while (!server->IsConnected())
|
||||
{
|
||||
watcher.check();
|
||||
lws_service(context, 0);
|
||||
}
|
||||
}
|
||||
|
||||
void Disconnect()
|
||||
{
|
||||
wfp_client_protocol_disconnect(protocol);
|
||||
}
|
||||
|
||||
void SendToClient(json_t * request)
|
||||
{
|
||||
server->SendMessage(request);
|
||||
}
|
||||
|
||||
json_t * ReceiveMessageFromClient()
|
||||
{
|
||||
TimeoutWatcher watcher(DEFAULT_TIMEOUT);
|
||||
json_t * result = server->ReceiveMessage();
|
||||
while (nullptr == result)
|
||||
{
|
||||
watcher.check();
|
||||
lws_service(context, 0);
|
||||
result = server->ReceiveMessage();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void AwaitAuthentication(
|
||||
std::string const & expected_username,
|
||||
std::string const & expected_password)
|
||||
{
|
||||
json_t * request = ReceiveMessageFromClient();
|
||||
ASSERT_TRUE(json_is_object(request));
|
||||
|
||||
json_t * method = json_object_get(request, "method");
|
||||
ASSERT_TRUE(json_is_string(method));
|
||||
ASSERT_STREQ("authenticate", json_string_value(method));
|
||||
|
||||
json_t * id = json_object_get(request, "id");
|
||||
ASSERT_TRUE(json_is_integer(id));
|
||||
|
||||
json_t * params = json_object_get(request, "params");
|
||||
ASSERT_TRUE(json_is_array(params));
|
||||
ASSERT_EQ(2, json_array_size(params));
|
||||
|
||||
json_t * type = json_array_get(params, 0);
|
||||
ASSERT_TRUE(json_is_string(type));
|
||||
ASSERT_STREQ("username", json_string_value(type));
|
||||
|
||||
json_t * credentials = json_array_get(params, 1);
|
||||
ASSERT_TRUE(json_is_object(credentials));
|
||||
|
||||
json_t * username = json_object_get(credentials, "username");
|
||||
ASSERT_TRUE(json_is_string(username));
|
||||
ASSERT_STREQ(expected_username.c_str(), json_string_value(username));
|
||||
|
||||
json_t * password = json_object_get(credentials, "password");
|
||||
ASSERT_TRUE(json_is_string(password));
|
||||
ASSERT_STREQ(expected_password.c_str(), json_string_value(password));
|
||||
|
||||
json_t * response = json_object();
|
||||
json_object_set_new(response, "result", json_object());
|
||||
json_object_set(response, "id", id);
|
||||
SendToClient(response);
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
void AwaitAddFilesystem(std::string& filesystemName)
|
||||
{
|
||||
json_t * addFilesystemRequest = ReceiveMessageFromClient();
|
||||
ASSERT_NE(nullptr, addFilesystemRequest);
|
||||
ASSERT_TRUE(json_is_object(addFilesystemRequest));
|
||||
|
||||
json_t * method = json_object_get(addFilesystemRequest, "method");
|
||||
ASSERT_TRUE(json_is_string(method));
|
||||
ASSERT_STREQ("add_filesystem", json_string_value(method));
|
||||
|
||||
json_t * params = json_object_get(addFilesystemRequest, "params");
|
||||
ASSERT_TRUE(json_is_array(params));
|
||||
ASSERT_EQ(1, json_array_size(params));
|
||||
|
||||
json_t * filesystem = json_array_get(params, 0);
|
||||
ASSERT_TRUE(json_is_string(filesystem));
|
||||
filesystemName = json_string_value(filesystem);
|
||||
|
||||
json_t * id = json_object_get(addFilesystemRequest, "id");
|
||||
ASSERT_TRUE(json_is_integer(id));
|
||||
|
||||
json_t * response = json_object();
|
||||
json_t * result = json_object();
|
||||
json_object_set(result, "id", filesystem);
|
||||
json_object_set_new(response, "result", result);
|
||||
json_object_set(response, "id", id);
|
||||
|
||||
SendToClient(response);
|
||||
|
||||
json_decref(addFilesystemRequest);
|
||||
}
|
||||
|
||||
private:
|
||||
WsServer * server;
|
||||
wfp_client_config * config;
|
||||
wfp_client_protocol * protocol;
|
||||
struct lws_context_creation_info info;
|
||||
struct lws_protocols protocols[2];
|
||||
struct lws_context * context;
|
||||
|
||||
};
|
||||
|
||||
void GetCredentials(wfp_credentials * credentials)
|
||||
{
|
||||
wfp_credentials_set_type(credentials, "username");
|
||||
wfp_credentials_add(credentials, "username", "bob");
|
||||
wfp_credentials_add(credentials, "password", "secret");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
TEST(client_protocol, connect)
|
||||
{
|
||||
MockProviderClient provider;
|
||||
ClientProtocolFixture fixture(provider);
|
||||
|
||||
EXPECT_CALL(provider, OnConnected()).Times(AtMost(1));
|
||||
EXPECT_CALL(provider, OnDisconnected()).Times(1);
|
||||
|
||||
fixture.Connect();
|
||||
if (HasFatalFailure()) { return; }
|
||||
|
||||
std::string filesystem;
|
||||
fixture.AwaitAddFilesystem(filesystem);
|
||||
if (HasFatalFailure()) { return; }
|
||||
}
|
||||
|
||||
TEST(client_protocol, disconnect_without_connect)
|
||||
{
|
||||
MockProviderClient provider;
|
||||
ClientProtocolFixture fixture(provider);
|
||||
|
||||
EXPECT_CALL(provider, OnDisconnected()).Times(1);
|
||||
|
||||
fixture.Disconnect();
|
||||
}
|
||||
|
||||
TEST(client_protocol, connect_with_username_authentication)
|
||||
{
|
||||
MockProviderClient provider;
|
||||
ClientProtocolFixture fixture(provider, true);
|
||||
|
||||
EXPECT_CALL(provider, OnConnected()).Times(AtMost(1));
|
||||
EXPECT_CALL(provider, OnDisconnected()).Times(1);
|
||||
EXPECT_CALL(provider, GetCredentials(_)).Times(1).WillOnce(Invoke(GetCredentials));
|
||||
|
||||
fixture.Connect();
|
||||
if (HasFatalFailure()) { return; }
|
||||
|
||||
fixture.AwaitAuthentication("bob", "secret");
|
||||
if (HasFatalFailure()) { return; }
|
||||
|
||||
std::string filesystem;
|
||||
fixture.AwaitAddFilesystem(filesystem);
|
||||
if (HasFatalFailure()) { return; }
|
||||
}
|
||||
|
||||
TEST(client_protocol, getattr)
|
||||
{
|
||||
MockProviderClient provider;
|
||||
ClientProtocolFixture fixture(provider);
|
||||
|
||||
EXPECT_CALL(provider, OnConnected()).Times(1);
|
||||
EXPECT_CALL(provider, OnDisconnected()).Times(1);
|
||||
EXPECT_CALL(provider, GetAttr(1, _)).Times(1);
|
||||
|
||||
fixture.Connect();
|
||||
if (HasFatalFailure()) { return; }
|
||||
|
||||
std::string filesystem;
|
||||
fixture.AwaitAddFilesystem(filesystem);
|
||||
if (HasFatalFailure()) { return; }
|
||||
|
||||
json_t * params = json_array();
|
||||
json_array_append_new(params, json_string(filesystem.c_str()));
|
||||
json_array_append_new(params, json_integer(1));
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("getattr"));
|
||||
json_object_set_new(request, "params", params);
|
||||
json_object_set_new(request, "id", json_integer(42));
|
||||
|
||||
fixture.SendToClient(request);
|
||||
json_t * response = fixture.ReceiveMessageFromClient();
|
||||
ASSERT_TRUE(json_is_object(response));
|
||||
|
||||
json_decref(response);
|
||||
|
||||
fixture.Disconnect();
|
||||
}
|
||||
32
test/webfuse_provider/tests/provider/test_dirbuffer.cc
Normal file
32
test/webfuse_provider/tests/provider/test_dirbuffer.cc
Normal file
@@ -0,0 +1,32 @@
|
||||
#include "webfuse_provider/impl/dirbuffer.h"
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(DirBuffer, CreateDispose)
|
||||
{
|
||||
wfp_dirbuffer * buffer = wfp_impl_dirbuffer_create();
|
||||
wfp_impl_dirbuffer_dispose(buffer);
|
||||
}
|
||||
|
||||
TEST(DirBuffer, Add)
|
||||
{
|
||||
wfp_dirbuffer * buffer = wfp_impl_dirbuffer_create();
|
||||
wfp_impl_dirbuffer_add(buffer, "answer", 42);
|
||||
|
||||
ASSERT_EQ(1, json_array_size(buffer->entries));
|
||||
|
||||
json_t * entry = json_array_get(buffer->entries, 0);
|
||||
ASSERT_STREQ("answer", json_string_value(json_object_get(entry, "name")));
|
||||
ASSERT_EQ(42, json_integer_value(json_object_get(entry, "inode")));
|
||||
|
||||
wfp_impl_dirbuffer_dispose(buffer);
|
||||
}
|
||||
|
||||
TEST(DirBuffer, Take)
|
||||
{
|
||||
wfp_dirbuffer * buffer = wfp_impl_dirbuffer_create();
|
||||
json_t * entries = wfp_impl_dirbuffer_take(buffer);
|
||||
wfp_impl_dirbuffer_dispose(buffer);
|
||||
|
||||
ASSERT_TRUE(json_is_array(entries));
|
||||
json_decref(entries);
|
||||
}
|
||||
149
test/webfuse_provider/utils/adapter_client.cc
Normal file
149
test/webfuse_provider/utils/adapter_client.cc
Normal file
@@ -0,0 +1,149 @@
|
||||
#include "webfuse/utils/adapter_client.hpp"
|
||||
#include "webfuse/utils/tempdir.hpp"
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
enum class Command
|
||||
{
|
||||
run,
|
||||
shutdown,
|
||||
connect,
|
||||
disconnect,
|
||||
authenticate,
|
||||
add_filesystem
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
class AdapterClient::Private
|
||||
{
|
||||
public:
|
||||
Private(
|
||||
wf_client_callback_fn * callback,
|
||||
void * user_data,
|
||||
std::string const & url)
|
||||
: client(wf_client_create(callback, user_data))
|
||||
, url_(url)
|
||||
, command(Command::run)
|
||||
, tempdir("webfuse_adpter_client")
|
||||
{
|
||||
thread = std::thread(&Run, this);
|
||||
}
|
||||
|
||||
~Private()
|
||||
{
|
||||
ApplyCommand(Command::shutdown);
|
||||
thread.join();
|
||||
wf_client_dispose(client);
|
||||
}
|
||||
|
||||
void ApplyCommand(Command actual_command)
|
||||
{
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
command = actual_command;
|
||||
}
|
||||
|
||||
wf_client_interrupt(client);
|
||||
}
|
||||
|
||||
std::string GetDir()
|
||||
{
|
||||
return tempdir.path();
|
||||
}
|
||||
|
||||
private:
|
||||
static void Run(Private * self)
|
||||
{
|
||||
bool is_running = true;
|
||||
while (is_running)
|
||||
{
|
||||
Command actual_command;
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(self->mutex);
|
||||
actual_command = self->command;
|
||||
self->command = Command::run;
|
||||
}
|
||||
|
||||
switch (actual_command)
|
||||
{
|
||||
case Command::run:
|
||||
wf_client_service(self->client);
|
||||
break;
|
||||
case Command::connect:
|
||||
wf_client_connect(self->client, self->url_.c_str());
|
||||
break;
|
||||
case Command::disconnect:
|
||||
wf_client_disconnect(self->client);
|
||||
break;
|
||||
case Command::authenticate:
|
||||
wf_client_authenticate(self->client);
|
||||
break;
|
||||
case Command::add_filesystem:
|
||||
wf_client_add_filesystem(self->client, self->tempdir.path(), "test");
|
||||
break;
|
||||
case Command::shutdown:
|
||||
// fall-through
|
||||
default:
|
||||
is_running = false;
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
wf_client * client;
|
||||
std::string url_;
|
||||
Command command;
|
||||
TempDir tempdir;
|
||||
std::thread thread;
|
||||
std::mutex mutex;
|
||||
};
|
||||
|
||||
AdapterClient::AdapterClient(
|
||||
wf_client_callback_fn * callback,
|
||||
void * user_data,
|
||||
std::string const & url)
|
||||
: d(new Private(callback, user_data, url))
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
AdapterClient::~AdapterClient()
|
||||
{
|
||||
delete d;
|
||||
}
|
||||
|
||||
void AdapterClient::Connect()
|
||||
{
|
||||
d->ApplyCommand(Command::connect);
|
||||
}
|
||||
|
||||
void AdapterClient::Disconnect()
|
||||
{
|
||||
d->ApplyCommand(Command::disconnect);
|
||||
}
|
||||
|
||||
void AdapterClient::Authenticate()
|
||||
{
|
||||
d->ApplyCommand(Command::authenticate);
|
||||
}
|
||||
|
||||
void AdapterClient::AddFileSystem()
|
||||
{
|
||||
d->ApplyCommand(Command::add_filesystem);
|
||||
}
|
||||
|
||||
std::string AdapterClient::GetDir() const
|
||||
{
|
||||
return d->GetDir();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
32
test/webfuse_provider/utils/adapter_client.hpp
Normal file
32
test/webfuse_provider/utils/adapter_client.hpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#ifndef WF_UTILS_ADAPTER_CLIENT_HPP
|
||||
#define WF_UTILS_APAPTER_CLIENT_HPP
|
||||
|
||||
#include "webfuse/adapter/client.h"
|
||||
#include <string>
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
class AdapterClient
|
||||
{
|
||||
AdapterClient(AdapterClient const &) = delete;
|
||||
AdapterClient& operator=(AdapterClient const &) = delete;
|
||||
public:
|
||||
AdapterClient(
|
||||
wf_client_callback_fn * callback,
|
||||
void * user_data,
|
||||
std::string const & url);
|
||||
~AdapterClient();
|
||||
void Connect();
|
||||
void Disconnect();
|
||||
void Authenticate();
|
||||
void AddFileSystem();
|
||||
std::string GetDir() const;
|
||||
private:
|
||||
class Private;
|
||||
Private * d;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
38
test/webfuse_provider/utils/file_utils.cc
Normal file
38
test/webfuse_provider/utils/file_utils.cc
Normal file
@@ -0,0 +1,38 @@
|
||||
#include "webfuse_provider/utils/file_utils.hpp"
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
bool is_dir(std::string const & path)
|
||||
{
|
||||
struct stat info;
|
||||
int rc = stat(path.c_str(), &info);
|
||||
|
||||
return (0 == rc) && (S_ISDIR(info.st_mode));
|
||||
}
|
||||
|
||||
bool is_symlink(std::string const & path)
|
||||
{
|
||||
struct stat info;
|
||||
int rc = lstat(path.c_str(), &info);
|
||||
|
||||
return (0 == rc) && (S_ISLNK(info.st_mode));
|
||||
}
|
||||
|
||||
bool is_same_path(std::string const & path, std::string const & other)
|
||||
{
|
||||
struct stat info;
|
||||
int rc = stat(path.c_str(), &info);
|
||||
|
||||
struct stat info_other;
|
||||
int rc_other = stat(other.c_str(), &info_other);
|
||||
|
||||
return (0 == rc) && (0 == rc_other) && (info.st_ino == info_other.st_ino);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
17
test/webfuse_provider/utils/file_utils.hpp
Normal file
17
test/webfuse_provider/utils/file_utils.hpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#ifndef WF_TEST_FILE_UTILS_HPP
|
||||
#define WF_TEST_FILE_UTILS_HPP
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
bool is_dir(std::string const & path);
|
||||
|
||||
bool is_symlink(std::string const & path);
|
||||
|
||||
bool is_same_path(std::string const & path, std::string const & other);
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
16
test/webfuse_provider/utils/jansson_test_environment.cc
Normal file
16
test/webfuse_provider/utils/jansson_test_environment.cc
Normal file
@@ -0,0 +1,16 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <jansson.h>
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
class JanssonTestEnvironment: public ::testing::Environment
|
||||
{
|
||||
public:
|
||||
void SetUp()
|
||||
{
|
||||
json_object_seed(0);
|
||||
}
|
||||
};
|
||||
#
|
||||
}
|
||||
113
test/webfuse_provider/utils/path.c
Normal file
113
test/webfuse_provider/utils/path.c
Normal file
@@ -0,0 +1,113 @@
|
||||
#include "webfuse_provider/utils/path.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define WF_PATH_DEFAULT_CAPACITY (8)
|
||||
|
||||
struct wf_path
|
||||
{
|
||||
char * * elements;
|
||||
size_t count;
|
||||
size_t capacity;
|
||||
};
|
||||
|
||||
static void
|
||||
wf_path_add(
|
||||
struct wf_path * path,
|
||||
char const * element,
|
||||
size_t element_size)
|
||||
{
|
||||
if (0 < element_size)
|
||||
{
|
||||
if (path->count >= path->capacity)
|
||||
{
|
||||
size_t new_capacity = 2 * path->capacity;
|
||||
size_t new_size = sizeof(char*) * new_capacity;
|
||||
|
||||
char * * elements = realloc(path->elements, new_size);
|
||||
if (NULL != elements)
|
||||
{
|
||||
path->elements = elements;
|
||||
path->capacity = new_capacity;
|
||||
}
|
||||
}
|
||||
|
||||
if (path->count < path->capacity)
|
||||
{
|
||||
path->elements[path->count] = strndup(element, element_size);
|
||||
path->count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct wf_path *
|
||||
wf_path_create(
|
||||
char const * value)
|
||||
{
|
||||
struct wf_path * path = malloc(sizeof(struct wf_path));
|
||||
path->elements = malloc(sizeof(char*) * WF_PATH_DEFAULT_CAPACITY);
|
||||
path->capacity = WF_PATH_DEFAULT_CAPACITY;
|
||||
path->count = 0;
|
||||
|
||||
char const * remainder = value;
|
||||
char const * pos = strchr(remainder, '/');
|
||||
while (NULL != pos)
|
||||
{
|
||||
wf_path_add(path, remainder, (pos - remainder));
|
||||
remainder = pos + 1;
|
||||
pos = strchr(remainder, '/');
|
||||
}
|
||||
|
||||
wf_path_add(path, remainder, strlen(remainder));
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
void
|
||||
wf_path_dispose(
|
||||
struct wf_path * path)
|
||||
{
|
||||
for(size_t i = 0; i < path->count; i++)
|
||||
{
|
||||
free(path->elements[i]);
|
||||
}
|
||||
|
||||
free(path->elements);
|
||||
free(path);
|
||||
(void) path;
|
||||
}
|
||||
|
||||
size_t
|
||||
wf_path_element_count(
|
||||
struct wf_path * path)
|
||||
{
|
||||
return path->count;
|
||||
}
|
||||
|
||||
char const *
|
||||
wf_path_get_element(
|
||||
struct wf_path * path,
|
||||
size_t i)
|
||||
{
|
||||
char const * result = NULL;
|
||||
if (i < path->count)
|
||||
{
|
||||
result = path->elements[i];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
char const *
|
||||
wf_path_get_filename(
|
||||
struct wf_path * path)
|
||||
{
|
||||
char const * result = NULL;
|
||||
|
||||
if (0 < path->count)
|
||||
{
|
||||
result = path->elements[path->count - 1];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
43
test/webfuse_provider/utils/path.h
Normal file
43
test/webfuse_provider/utils/path.h
Normal file
@@ -0,0 +1,43 @@
|
||||
#ifndef WF_PATH_H
|
||||
#define WF_PATH_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stddef.h>
|
||||
#else
|
||||
#include <cstddef>
|
||||
using ::std::size_t;
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_path;
|
||||
|
||||
extern struct wf_path *
|
||||
wf_path_create(
|
||||
char const * value);
|
||||
|
||||
extern void
|
||||
wf_path_dispose(
|
||||
struct wf_path * path);
|
||||
|
||||
extern size_t
|
||||
wf_path_element_count(
|
||||
struct wf_path * path);
|
||||
|
||||
extern char const *
|
||||
wf_path_get_element(
|
||||
struct wf_path * path,
|
||||
size_t i);
|
||||
|
||||
extern char const *
|
||||
wf_path_get_filename(
|
||||
struct wf_path * path);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
421
test/webfuse_provider/utils/static_filesystem.c
Normal file
421
test/webfuse_provider/utils/static_filesystem.c
Normal file
@@ -0,0 +1,421 @@
|
||||
#include "webfuse_provider/utils/static_filesystem.h"
|
||||
#include "webfuse_provider/client_config.h"
|
||||
#include "webfuse_provider/dirbuffer.h"
|
||||
#include "webfuse_provider/operation/error.h"
|
||||
|
||||
#include "webfuse_provider/utils/path.h"
|
||||
#include "webfuse_provider/impl/util.h"
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define WFP_STATIC_FILESYSTEM_DEFAULT_CAPACITY (16)
|
||||
#define WFP_STATIC_FILSYSTEM_INODE_ROOT (1)
|
||||
#define WFP_STATIC_FILESYSTEM_MAX_READ_SIZE (4 * 1024)
|
||||
|
||||
struct wfp_static_filesystem_entry
|
||||
{
|
||||
size_t inode;
|
||||
size_t parent;
|
||||
char * name;
|
||||
bool is_file;
|
||||
int mode;
|
||||
size_t size;
|
||||
char * content;
|
||||
wfp_static_filesystem_read_fn * read;
|
||||
wfp_static_filesystem_get_info_fn * get_info;
|
||||
void * user_data;
|
||||
};
|
||||
|
||||
struct wfp_static_filesystem
|
||||
{
|
||||
struct wfp_static_filesystem_entry * entries;
|
||||
size_t size;
|
||||
size_t capacity;
|
||||
void * user_data;
|
||||
};
|
||||
|
||||
static struct wfp_static_filesystem_entry *
|
||||
wfp_static_filesystem_get_entry(
|
||||
struct wfp_static_filesystem * filesystem,
|
||||
size_t inode)
|
||||
{
|
||||
struct wfp_static_filesystem_entry * entry = NULL;
|
||||
|
||||
if ((0 < inode) && (inode <= filesystem->size))
|
||||
{
|
||||
entry = &filesystem->entries[inode - 1];
|
||||
}
|
||||
|
||||
return entry;
|
||||
}
|
||||
|
||||
static struct wfp_static_filesystem_entry *
|
||||
wfp_static_filesystem_get_entry_by_name(
|
||||
struct wfp_static_filesystem * filesystem,
|
||||
size_t parent,
|
||||
char const * name)
|
||||
{
|
||||
struct wfp_static_filesystem_entry * entry = NULL;
|
||||
for(size_t i = 0; i < filesystem->size; i++)
|
||||
{
|
||||
struct wfp_static_filesystem_entry * current = &filesystem->entries[i];
|
||||
if ((parent == current->parent) && (0 == strcmp(name, current->name)))
|
||||
{
|
||||
entry = current;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return entry;
|
||||
}
|
||||
|
||||
static struct wfp_static_filesystem_entry *
|
||||
wfp_static_filesystem_add_entry(
|
||||
struct wfp_static_filesystem * filesystem)
|
||||
{
|
||||
struct wfp_static_filesystem_entry * entry = NULL;
|
||||
|
||||
if (filesystem->size >= filesystem->capacity)
|
||||
{
|
||||
struct wfp_static_filesystem_entry * entries;
|
||||
|
||||
size_t new_capacity = 2 * filesystem->capacity;
|
||||
size_t new_size = new_capacity * sizeof(struct wfp_static_filesystem_entry);
|
||||
entries = realloc(filesystem->entries, new_size);
|
||||
|
||||
if (NULL != entries)
|
||||
{
|
||||
filesystem->entries = entries;
|
||||
filesystem->capacity = new_capacity;
|
||||
}
|
||||
}
|
||||
|
||||
if (filesystem->size < filesystem->capacity)
|
||||
{
|
||||
entry = &filesystem->entries[filesystem->size];
|
||||
entry->inode = filesystem->size + 1;
|
||||
filesystem->size++;
|
||||
}
|
||||
|
||||
return entry;
|
||||
}
|
||||
|
||||
static size_t
|
||||
wfp_static_filesystem_entry_read(
|
||||
size_t offset,
|
||||
char * buffer,
|
||||
size_t buffer_size,
|
||||
void * user_data)
|
||||
{
|
||||
size_t result = 0;
|
||||
struct wfp_static_filesystem_entry * entry = user_data;
|
||||
if (offset < entry->size)
|
||||
{
|
||||
size_t remaining = (entry->size - offset);
|
||||
result = (buffer_size < remaining) ? buffer_size : remaining;
|
||||
memcpy(buffer, entry->content, result);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static void
|
||||
wfp_static_filesystem_entry_get_info(
|
||||
void * user_data,
|
||||
int * result_mode,
|
||||
size_t * result_size)
|
||||
{
|
||||
struct wfp_static_filesystem_entry * entry = user_data;
|
||||
*result_mode = entry->mode;
|
||||
*result_size = entry->size;
|
||||
}
|
||||
|
||||
static size_t
|
||||
wfp_static_filesystem_add_dir(
|
||||
struct wfp_static_filesystem * filesystem,
|
||||
size_t parent,
|
||||
char const * name
|
||||
)
|
||||
{
|
||||
struct wfp_static_filesystem_entry * entry = wfp_static_filesystem_get_entry_by_name(filesystem, parent, name);
|
||||
if (NULL == entry)
|
||||
{
|
||||
entry = wfp_static_filesystem_add_entry(filesystem);
|
||||
entry->parent = parent;
|
||||
entry->is_file = false;
|
||||
entry->mode = 0555;
|
||||
entry->name = strdup(name);
|
||||
entry->user_data = entry;
|
||||
entry->read = &wfp_static_filesystem_entry_read;
|
||||
entry->get_info = &wfp_static_filesystem_entry_get_info;
|
||||
entry->size = 0;
|
||||
entry->content = NULL;
|
||||
}
|
||||
|
||||
return entry->inode;
|
||||
}
|
||||
|
||||
static size_t
|
||||
wfp_static_filesystem_make_parent(
|
||||
struct wfp_static_filesystem * filesystem,
|
||||
struct wf_path * path)
|
||||
{
|
||||
size_t result = WFP_STATIC_FILSYSTEM_INODE_ROOT;
|
||||
|
||||
size_t count = wf_path_element_count(path);
|
||||
if (0 < count)
|
||||
{
|
||||
for(size_t i = 0; i < (count - 1); i++)
|
||||
{
|
||||
char const * name = wf_path_get_element(path, i);
|
||||
result = wfp_static_filesystem_add_dir(filesystem, result, name);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static void
|
||||
wfp_static_filesystem_stat(
|
||||
struct wfp_static_filesystem_entry * entry,
|
||||
struct stat * stat
|
||||
)
|
||||
{
|
||||
memset(stat, 0, sizeof(struct stat));
|
||||
|
||||
int mode;
|
||||
size_t size;
|
||||
entry->get_info(entry->user_data, &mode, &size);
|
||||
|
||||
stat->st_ino = entry->inode;
|
||||
stat->st_size = entry->size;
|
||||
stat->st_mode = entry->mode & 0777;
|
||||
stat->st_mode |= (entry->is_file) ? S_IFREG: S_IFDIR;
|
||||
}
|
||||
|
||||
static void wfp_static_filesystem_lookup(
|
||||
struct wfp_request * request,
|
||||
ino_t parent,
|
||||
char const * name,
|
||||
void * user_data)
|
||||
{
|
||||
struct wfp_static_filesystem * filesystem = user_data;
|
||||
struct wfp_static_filesystem_entry * entry = wfp_static_filesystem_get_entry_by_name(filesystem, parent, name);
|
||||
|
||||
if (NULL != entry)
|
||||
{
|
||||
struct stat stat;
|
||||
wfp_static_filesystem_stat(entry, &stat);
|
||||
wfp_respond_lookup(request, &stat);
|
||||
}
|
||||
else
|
||||
{
|
||||
wfp_respond_error(request, WF_BAD_NOENTRY);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void wfp_static_filesystem_getattr(
|
||||
struct wfp_request * request,
|
||||
ino_t inode,
|
||||
void * user_data)
|
||||
{
|
||||
struct wfp_static_filesystem * filesystem = user_data;
|
||||
struct wfp_static_filesystem_entry * entry = wfp_static_filesystem_get_entry(filesystem, inode);
|
||||
|
||||
if (NULL != entry)
|
||||
{
|
||||
struct stat stat;
|
||||
wfp_static_filesystem_stat(entry, &stat);
|
||||
wfp_respond_getattr(request, &stat);
|
||||
}
|
||||
else
|
||||
{
|
||||
wfp_respond_error(request, WF_BAD_NOENTRY);
|
||||
}
|
||||
}
|
||||
|
||||
static void wfp_static_filesystem_readdir(
|
||||
struct wfp_request * request,
|
||||
ino_t directory,
|
||||
void * user_data)
|
||||
{
|
||||
struct wfp_static_filesystem * filesystem = user_data;
|
||||
struct wfp_static_filesystem_entry * dir = wfp_static_filesystem_get_entry(filesystem, directory);
|
||||
|
||||
if ((NULL != dir) && (!dir->is_file))
|
||||
{
|
||||
struct wfp_dirbuffer * buffer = wfp_dirbuffer_create();
|
||||
wfp_dirbuffer_add(buffer, ".", dir->inode);
|
||||
wfp_dirbuffer_add(buffer, "..", dir->inode);
|
||||
|
||||
for(size_t i = 0; i < filesystem->size; i++)
|
||||
{
|
||||
struct wfp_static_filesystem_entry const * entry = &filesystem->entries[i];
|
||||
if (directory == entry->parent)
|
||||
{
|
||||
wfp_dirbuffer_add(buffer, entry->name, entry->inode);
|
||||
}
|
||||
}
|
||||
|
||||
wfp_respond_readdir(request, buffer);
|
||||
wfp_dirbuffer_dispose(buffer);
|
||||
}
|
||||
else
|
||||
{
|
||||
wfp_respond_error(request, WF_BAD_NOENTRY);
|
||||
}
|
||||
}
|
||||
|
||||
static void wfp_static_filesystem_open(
|
||||
struct wfp_request * request,
|
||||
ino_t inode,
|
||||
int flags,
|
||||
void * user_data)
|
||||
{
|
||||
struct wfp_static_filesystem * filesystem = user_data;
|
||||
struct wfp_static_filesystem_entry * entry = wfp_static_filesystem_get_entry(filesystem, inode);
|
||||
|
||||
if ((NULL != entry) && (entry->is_file))
|
||||
{
|
||||
if (O_RDONLY == (flags & O_ACCMODE))
|
||||
{
|
||||
wfp_respond_open(request, 0U);
|
||||
}
|
||||
else
|
||||
{
|
||||
wfp_respond_error(request, WF_BAD_ACCESS_DENIED);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
wfp_respond_error(request, WF_BAD_NOENTRY);
|
||||
}
|
||||
}
|
||||
|
||||
static void wfp_static_filesystem_read(
|
||||
struct wfp_request * request,
|
||||
ino_t inode,
|
||||
uint32_t WF_UNUSED_PARAM(handle),
|
||||
size_t offset,
|
||||
size_t length,
|
||||
void * user_data)
|
||||
{
|
||||
struct wfp_static_filesystem * filesystem = user_data;
|
||||
struct wfp_static_filesystem_entry * entry = wfp_static_filesystem_get_entry(filesystem, inode);
|
||||
|
||||
if ((NULL != entry) && (entry->is_file))
|
||||
{
|
||||
char buffer[WFP_STATIC_FILESYSTEM_MAX_READ_SIZE];
|
||||
size_t max_size = (length < WFP_STATIC_FILESYSTEM_MAX_READ_SIZE) ? length : WFP_STATIC_FILESYSTEM_MAX_READ_SIZE;
|
||||
|
||||
size_t count = entry->read(offset, buffer, max_size, entry->user_data);
|
||||
wfp_respond_read(request, buffer, count);
|
||||
}
|
||||
else
|
||||
{
|
||||
wfp_respond_error(request, WF_BAD_NOENTRY);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
struct wfp_static_filesystem *
|
||||
wfp_static_filesystem_create(
|
||||
struct wfp_client_config * config)
|
||||
{
|
||||
(void) config;
|
||||
|
||||
struct wfp_static_filesystem * filesystem = malloc(sizeof(struct wfp_static_filesystem));
|
||||
filesystem->entries = malloc(sizeof(struct wfp_static_filesystem_entry) * WFP_STATIC_FILESYSTEM_DEFAULT_CAPACITY);
|
||||
filesystem->size = 0;
|
||||
filesystem->capacity = WFP_STATIC_FILESYSTEM_DEFAULT_CAPACITY;
|
||||
filesystem->user_data = NULL;
|
||||
|
||||
wfp_static_filesystem_add_dir(filesystem, 0, "<root>");
|
||||
|
||||
wfp_client_config_set_userdata(config, filesystem);
|
||||
wfp_client_config_set_onlookup(config, &wfp_static_filesystem_lookup);
|
||||
wfp_client_config_set_ongetattr(config, &wfp_static_filesystem_getattr);
|
||||
wfp_client_config_set_onreaddir(config, &wfp_static_filesystem_readdir);
|
||||
wfp_client_config_set_onopen(config, &wfp_static_filesystem_open);
|
||||
wfp_client_config_set_onread(config, &wfp_static_filesystem_read);
|
||||
|
||||
return filesystem;
|
||||
}
|
||||
|
||||
void
|
||||
wfp_static_filesystem_dispose(
|
||||
struct wfp_static_filesystem * filesystem)
|
||||
{
|
||||
for(size_t i = 0; i < filesystem->size; i++)
|
||||
{
|
||||
struct wfp_static_filesystem_entry * entry = &filesystem->entries[i];
|
||||
free(entry->name);
|
||||
free(entry->content);
|
||||
}
|
||||
|
||||
free(filesystem->entries);
|
||||
free(filesystem);
|
||||
}
|
||||
|
||||
void
|
||||
wfp_static_filesystem_add(
|
||||
struct wfp_static_filesystem * filesystem,
|
||||
char const * path,
|
||||
int mode,
|
||||
char const * content,
|
||||
size_t length)
|
||||
{
|
||||
struct wf_path * path_ = wf_path_create(path);
|
||||
if (NULL != path_)
|
||||
{
|
||||
size_t parent = wfp_static_filesystem_make_parent(filesystem, path_);
|
||||
struct wfp_static_filesystem_entry * entry = wfp_static_filesystem_add_entry(filesystem);
|
||||
entry->parent = parent;
|
||||
entry->is_file = true;
|
||||
entry->name = strdup(wf_path_get_filename(path_));
|
||||
entry->mode = mode;
|
||||
entry->size = length;
|
||||
entry->get_info = &wfp_static_filesystem_entry_get_info;
|
||||
entry->read = &wfp_static_filesystem_entry_read;
|
||||
entry->user_data = entry;
|
||||
|
||||
entry->content = malloc(length);
|
||||
memcpy(entry->content, content, length);
|
||||
|
||||
wf_path_dispose(path_);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
wfp_static_filesystem_add_text(
|
||||
struct wfp_static_filesystem * filesystem,
|
||||
char const * path,
|
||||
int mode,
|
||||
char const * content)
|
||||
{
|
||||
size_t length = strlen(content);
|
||||
wfp_static_filesystem_add(filesystem, path, mode, content, length);
|
||||
}
|
||||
|
||||
void
|
||||
wfp_static_filesystem_set_user_data(
|
||||
struct wfp_static_filesystem * filesystem,
|
||||
void * user_data)
|
||||
{
|
||||
filesystem->user_data = user_data;
|
||||
}
|
||||
|
||||
void *
|
||||
wfp_static_filesystem_get_user_data(
|
||||
struct wfp_static_filesystem * filesystem)
|
||||
{
|
||||
return filesystem->user_data;
|
||||
}
|
||||
72
test/webfuse_provider/utils/static_filesystem.h
Normal file
72
test/webfuse_provider/utils/static_filesystem.h
Normal file
@@ -0,0 +1,72 @@
|
||||
#ifndef WFP_STATIC_FILESYSTEM_H
|
||||
#define WFP_STATIC_FILESYSTEM_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stddef.h>
|
||||
#else
|
||||
#include <cstddef>
|
||||
using ::std::size_t;
|
||||
#endif
|
||||
|
||||
#include <webfuse_provider/api.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wfp_client_config;
|
||||
|
||||
struct wfp_static_filesystem;
|
||||
|
||||
typedef size_t
|
||||
wfp_static_filesystem_read_fn(
|
||||
size_t offset,
|
||||
char * buffer,
|
||||
size_t buffer_size,
|
||||
void * user_data);
|
||||
|
||||
typedef void
|
||||
wfp_static_filesystem_get_info_fn(
|
||||
void * user_data,
|
||||
int * result_mode,
|
||||
size_t * result_size);
|
||||
|
||||
|
||||
extern WFP_API struct wfp_static_filesystem *
|
||||
wfp_static_filesystem_create(
|
||||
struct wfp_client_config * config);
|
||||
|
||||
extern WFP_API void
|
||||
wfp_static_filesystem_dispose(
|
||||
struct wfp_static_filesystem * filesystem);
|
||||
|
||||
extern WFP_API void
|
||||
wfp_static_filesystem_add(
|
||||
struct wfp_static_filesystem * filesystem,
|
||||
char const * path,
|
||||
int mode,
|
||||
char const * content,
|
||||
size_t length);
|
||||
|
||||
extern WFP_API void
|
||||
wfp_static_filesystem_add_text(
|
||||
struct wfp_static_filesystem * filesystem,
|
||||
char const * path,
|
||||
int mode,
|
||||
char const * content);
|
||||
|
||||
extern void
|
||||
wfp_static_filesystem_set_user_data(
|
||||
struct wfp_static_filesystem * filesystem,
|
||||
void * user_data);
|
||||
|
||||
extern void *
|
||||
wfp_static_filesystem_get_user_data(
|
||||
struct wfp_static_filesystem * filesystem);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
33
test/webfuse_provider/utils/tempdir.cc
Normal file
33
test/webfuse_provider/utils/tempdir.cc
Normal file
@@ -0,0 +1,33 @@
|
||||
#include "webfuse_provider/utils/tempdir.hpp"
|
||||
|
||||
#include <unistd.h>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
TempDir::TempDir(char const * prefix)
|
||||
{
|
||||
asprintf(&path_, "/tmp/%s_XXXXXX", prefix);
|
||||
char * result = mkdtemp(path_);
|
||||
if (NULL == result)
|
||||
{
|
||||
throw std::runtime_error("unable to create temp dir");
|
||||
}
|
||||
}
|
||||
|
||||
TempDir::~TempDir()
|
||||
{
|
||||
rmdir(path_);
|
||||
free(path_);
|
||||
}
|
||||
|
||||
char const * TempDir::path()
|
||||
{
|
||||
return path_;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
21
test/webfuse_provider/utils/tempdir.hpp
Normal file
21
test/webfuse_provider/utils/tempdir.hpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#ifndef WF_TEST_TEMPDIR_HPP
|
||||
#define WF_TEST_TEMPDIR_HPP
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
class TempDir
|
||||
{
|
||||
TempDir(TempDir const &) = delete;
|
||||
TempDir & operator=(TempDir const &) = delete;
|
||||
public:
|
||||
explicit TempDir(char const * prefix);
|
||||
~TempDir();
|
||||
char const * path();
|
||||
private:
|
||||
char * path_;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
58
test/webfuse_provider/utils/timeout_watcher.cc
Normal file
58
test/webfuse_provider/utils/timeout_watcher.cc
Normal file
@@ -0,0 +1,58 @@
|
||||
#include "webfuse_provider/utils/timeout_watcher.hpp"
|
||||
#include <stdexcept>
|
||||
#include <thread>
|
||||
|
||||
using std::chrono::milliseconds;
|
||||
using std::chrono::duration_cast;
|
||||
using std::chrono::steady_clock;
|
||||
|
||||
namespace
|
||||
{
|
||||
milliseconds now()
|
||||
{
|
||||
return duration_cast<milliseconds>(steady_clock::now().time_since_epoch());
|
||||
}
|
||||
}
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
TimeoutWatcher::TimeoutWatcher(milliseconds timeout)
|
||||
: startedAt(now())
|
||||
, timeout_(timeout)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
TimeoutWatcher::~TimeoutWatcher()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool TimeoutWatcher::isTimeout()
|
||||
{
|
||||
return (now() - startedAt) > timeout_;
|
||||
}
|
||||
|
||||
void TimeoutWatcher::check()
|
||||
{
|
||||
if (isTimeout())
|
||||
{
|
||||
throw std::runtime_error("timeout");
|
||||
}
|
||||
}
|
||||
|
||||
bool TimeoutWatcher::waitUntil(std::function<bool()> predicate)
|
||||
{
|
||||
bool result = predicate();
|
||||
while ((!result) && (!isTimeout()))
|
||||
{
|
||||
std::this_thread::yield();
|
||||
result = predicate();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
28
test/webfuse_provider/utils/timeout_watcher.hpp
Normal file
28
test/webfuse_provider/utils/timeout_watcher.hpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#ifndef WF_TEST_TIMEOUT_WATCHER_HPP
|
||||
#define WF_TEST_TIMEOUT_WATCHER_HPP
|
||||
|
||||
#include <chrono>
|
||||
#include <functional>
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
class TimeoutWatcher final
|
||||
{
|
||||
TimeoutWatcher(TimeoutWatcher const & other) = delete;
|
||||
TimeoutWatcher& operator=(TimeoutWatcher const & other) = delete;
|
||||
public:
|
||||
explicit TimeoutWatcher(std::chrono::milliseconds timeout);
|
||||
~TimeoutWatcher();
|
||||
bool isTimeout();
|
||||
void check();
|
||||
bool waitUntil(std::function<bool()> predicate);
|
||||
private:
|
||||
std::chrono::milliseconds startedAt;
|
||||
std::chrono::milliseconds timeout_;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
132
test/webfuse_provider/utils/wrap.hpp
Normal file
132
test/webfuse_provider/utils/wrap.hpp
Normal file
@@ -0,0 +1,132 @@
|
||||
#ifndef WF_WRAP_HPP
|
||||
#define WF_WRAP_HPP
|
||||
|
||||
#define WF_WRAP_FUNC0( GLOBAL_VAR, RETURN_TYPE, FUNC_NAME ) \
|
||||
extern RETURN_TYPE __real_ ## FUNC_NAME (); \
|
||||
RETURN_TYPE __wrap_ ## FUNC_NAME () \
|
||||
{ \
|
||||
if (nullptr == GLOBAL_VAR ) \
|
||||
{ \
|
||||
return __real_ ## FUNC_NAME (); \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
return GLOBAL_VAR -> FUNC_NAME(); \
|
||||
} \
|
||||
}
|
||||
|
||||
#define WF_WRAP_FUNC1( GLOBAL_VAR, RETURN_TYPE, FUNC_NAME, ARG1_TYPE ) \
|
||||
extern RETURN_TYPE __real_ ## FUNC_NAME (ARG1_TYPE); \
|
||||
RETURN_TYPE __wrap_ ## FUNC_NAME (ARG1_TYPE arg1) \
|
||||
{ \
|
||||
if (nullptr == GLOBAL_VAR ) \
|
||||
{ \
|
||||
return __real_ ## FUNC_NAME (arg1); \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
return GLOBAL_VAR -> FUNC_NAME(arg1); \
|
||||
} \
|
||||
}
|
||||
|
||||
#define WF_WRAP_FUNC2( GLOBAL_VAR, RETURN_TYPE, FUNC_NAME, ARG1_TYPE, ARG2_TYPE ) \
|
||||
extern RETURN_TYPE __real_ ## FUNC_NAME (ARG1_TYPE, ARG2_TYPE); \
|
||||
RETURN_TYPE __wrap_ ## FUNC_NAME (ARG1_TYPE arg1, ARG2_TYPE arg2) \
|
||||
{ \
|
||||
if (nullptr == GLOBAL_VAR ) \
|
||||
{ \
|
||||
return __real_ ## FUNC_NAME (arg1, arg2); \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
return GLOBAL_VAR -> FUNC_NAME(arg1, arg2); \
|
||||
} \
|
||||
}
|
||||
|
||||
#define WF_WRAP_FUNC3( GLOBAL_VAR, RETURN_TYPE, FUNC_NAME, ARG1_TYPE, ARG2_TYPE, ARG3_TYPE ) \
|
||||
extern RETURN_TYPE __real_ ## FUNC_NAME (ARG1_TYPE, ARG2_TYPE, ARG3_TYPE); \
|
||||
RETURN_TYPE __wrap_ ## FUNC_NAME (ARG1_TYPE arg1, ARG2_TYPE arg2, ARG3_TYPE arg3) \
|
||||
{ \
|
||||
if (nullptr == GLOBAL_VAR ) \
|
||||
{ \
|
||||
return __real_ ## FUNC_NAME (arg1, arg2, arg3); \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
return GLOBAL_VAR -> FUNC_NAME(arg1, arg2, arg3); \
|
||||
} \
|
||||
}
|
||||
|
||||
#define WF_WRAP_FUNC4( GLOBAL_VAR, RETURN_TYPE, FUNC_NAME, ARG1_TYPE, ARG2_TYPE, ARG3_TYPE, ARG4_TYPE ) \
|
||||
extern RETURN_TYPE __real_ ## FUNC_NAME (ARG1_TYPE, ARG2_TYPE, ARG3_TYPE, ARG4_TYPE); \
|
||||
RETURN_TYPE __wrap_ ## FUNC_NAME (ARG1_TYPE arg1, ARG2_TYPE arg2, ARG3_TYPE arg3, ARG4_TYPE arg4) \
|
||||
{ \
|
||||
if (nullptr == GLOBAL_VAR ) \
|
||||
{ \
|
||||
return __real_ ## FUNC_NAME (arg1, arg2, arg3, arg4); \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
return GLOBAL_VAR -> FUNC_NAME(arg1, arg2, arg3, arg4); \
|
||||
} \
|
||||
}
|
||||
|
||||
#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); \
|
||||
} \
|
||||
}
|
||||
|
||||
|
||||
#define WF_WRAP_VFUNC3( GLOBAL_VAR, RETURN_TYPE, FUNC_NAME, ARG1_TYPE, ARG2_TYPE, ARG3_TYPE ) \
|
||||
extern RETURN_TYPE __real_ ## FUNC_NAME (ARG1_TYPE, ARG2_TYPE, ARG3_TYPE, va_list); \
|
||||
RETURN_TYPE __wrap_ ## FUNC_NAME (ARG1_TYPE arg1, ARG2_TYPE arg2, ARG3_TYPE arg3, va_list args) \
|
||||
{ \
|
||||
if (nullptr == GLOBAL_VAR ) \
|
||||
{ \
|
||||
return __real_ ## FUNC_NAME (arg1, arg2, arg3, args); \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
return GLOBAL_VAR -> FUNC_NAME(arg1, arg2, arg3); \
|
||||
} \
|
||||
}
|
||||
|
||||
#define WF_WRAP_VFUNC5( 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, va_list); \
|
||||
RETURN_TYPE __wrap_ ## FUNC_NAME (ARG1_TYPE arg1, ARG2_TYPE arg2, ARG3_TYPE arg3, ARG4_TYPE arg4, ARG5_TYPE arg5, va_list args) \
|
||||
{ \
|
||||
if (nullptr == GLOBAL_VAR ) \
|
||||
{ \
|
||||
return __real_ ## FUNC_NAME (arg1, arg2, arg3, arg4, arg5, args); \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
return GLOBAL_VAR -> FUNC_NAME(arg1, arg2, arg3, arg4, arg5); \
|
||||
} \
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
306
test/webfuse_provider/utils/ws_server.cc
Normal file
306
test/webfuse_provider/utils/ws_server.cc
Normal file
@@ -0,0 +1,306 @@
|
||||
#include "webfuse_provider/utils/ws_server.h"
|
||||
#include "webfuse_provider/impl/lws_log.h"
|
||||
|
||||
#include <libwebsockets.h>
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <chrono>
|
||||
#include <sstream>
|
||||
#include <queue>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
class IServer
|
||||
{
|
||||
public:
|
||||
virtual ~IServer() = default;
|
||||
virtual void OnConnected(lws * wsi) = 0;
|
||||
virtual void OnConnectionClosed(lws * wsi) = 0;
|
||||
virtual void OnMessageReceived(struct lws * wsi, char const * data, size_t length) = 0;
|
||||
virtual void OnWritable(struct lws * wsi) = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
class WsServer::Private : IServer
|
||||
{
|
||||
Private(Private const &) = delete;
|
||||
Private & operator=(Private const &) = delete;
|
||||
public:
|
||||
Private(std::string const & protocol, int port);
|
||||
~Private();
|
||||
bool IsConnected();
|
||||
std::string GetUrl() const;
|
||||
void SendMessage(json_t * message);
|
||||
json_t * ReceiveMessage();
|
||||
void OnConnected(lws * wsi) override;
|
||||
void OnConnectionClosed(lws * wsi) override;
|
||||
void OnMessageReceived(struct lws * wsi, char const * data, size_t length) override;
|
||||
void OnWritable(struct lws * wsi) override;
|
||||
|
||||
private:
|
||||
static void run(Private * self);
|
||||
std::string protocol_;
|
||||
int port_;
|
||||
bool is_connected;
|
||||
bool is_shutdown_requested;
|
||||
lws * wsi_;
|
||||
lws_context * ws_context;
|
||||
lws_protocols ws_protocols[2];
|
||||
lws_context_creation_info info;
|
||||
std::thread context;
|
||||
std::mutex mutex;
|
||||
std::queue<std::string> writeQueue;
|
||||
std::queue<std::string> recvQueue;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
extern "C"
|
||||
{
|
||||
|
||||
static int wf_test_utils_ws_server_callback(
|
||||
struct lws * wsi,
|
||||
enum lws_callback_reasons reason,
|
||||
void * user,
|
||||
void * in,
|
||||
size_t len)
|
||||
{
|
||||
int result = 0;
|
||||
struct lws_protocols const * ws_protocol = lws_get_protocol(wsi);
|
||||
auto * server = reinterpret_cast<IServer*>(nullptr != ws_protocol ? ws_protocol->user : nullptr);
|
||||
|
||||
if (nullptr != server)
|
||||
{
|
||||
switch (reason)
|
||||
{
|
||||
case LWS_CALLBACK_ESTABLISHED:
|
||||
server->OnConnected(wsi);
|
||||
break;
|
||||
case LWS_CALLBACK_CLOSED:
|
||||
server->OnConnectionClosed(wsi);
|
||||
break;
|
||||
case LWS_CALLBACK_RECEIVE:
|
||||
{
|
||||
auto * data = reinterpret_cast<char const *>(in);
|
||||
server->OnMessageReceived(wsi, data, len);
|
||||
}
|
||||
break;
|
||||
case LWS_CALLBACK_SERVER_WRITEABLE:
|
||||
server->OnWritable(wsi);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
WsServer::WsServer(std::string const & protocol, int port)
|
||||
: d(new Private(protocol, port))
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
WsServer::~WsServer()
|
||||
{
|
||||
delete d;
|
||||
}
|
||||
|
||||
bool WsServer::IsConnected()
|
||||
{
|
||||
return d->IsConnected();
|
||||
}
|
||||
|
||||
void WsServer::SendMessage(json_t * message)
|
||||
{
|
||||
d->SendMessage(message);
|
||||
}
|
||||
|
||||
json_t * WsServer::ReceiveMessage()
|
||||
{
|
||||
return d->ReceiveMessage();
|
||||
}
|
||||
|
||||
std::string WsServer::GetUrl() const
|
||||
{
|
||||
return d->GetUrl();
|
||||
}
|
||||
|
||||
|
||||
WsServer::Private::Private(std::string const & protocol, int port)
|
||||
: protocol_(protocol)
|
||||
, port_(port)
|
||||
, is_connected(false)
|
||||
, is_shutdown_requested(false)
|
||||
, wsi_(nullptr)
|
||||
{
|
||||
wf_lwslog_disable();
|
||||
IServer * server = this;
|
||||
memset(ws_protocols, 0, sizeof(struct lws_protocols) * 2 );
|
||||
|
||||
ws_protocols[0].name = protocol_.c_str();
|
||||
ws_protocols[0].callback = &wf_test_utils_ws_server_callback;
|
||||
ws_protocols[0].per_session_data_size = 0;
|
||||
ws_protocols[0].user = reinterpret_cast<void*>(server);
|
||||
|
||||
memset(&info, 0, sizeof(struct lws_context_creation_info));
|
||||
info.port = port;
|
||||
info.mounts = NULL;
|
||||
info.protocols =ws_protocols;
|
||||
info.vhost_name = "localhost";
|
||||
info.ws_ping_pong_interval = 10;
|
||||
info.options = LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE;
|
||||
info.options |= LWS_SERVER_OPTION_EXPLICIT_VHOSTS;
|
||||
|
||||
ws_context = lws_create_context(&info);
|
||||
|
||||
struct lws_vhost * vhost = lws_create_vhost(ws_context, &info);
|
||||
port_ = lws_get_vhost_port(vhost);
|
||||
|
||||
context = std::thread(&run, this);
|
||||
}
|
||||
|
||||
WsServer::Private::~Private()
|
||||
{
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
is_shutdown_requested = true;
|
||||
}
|
||||
|
||||
lws_cancel_service(ws_context);
|
||||
context.join();
|
||||
lws_context_destroy(ws_context);
|
||||
}
|
||||
|
||||
void WsServer::Private::run(Private * self)
|
||||
{
|
||||
bool is_running = true;
|
||||
while (is_running)
|
||||
{
|
||||
lws_service(self->ws_context, 0);
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(self->mutex);
|
||||
is_running = !self->is_shutdown_requested;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool WsServer::Private::IsConnected()
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
return is_connected;
|
||||
}
|
||||
|
||||
void WsServer::Private::OnConnected(lws * wsi)
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
is_connected = true;
|
||||
wsi_ = wsi;
|
||||
}
|
||||
|
||||
void WsServer::Private::OnConnectionClosed(lws * wsi)
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
if (wsi == wsi_)
|
||||
{
|
||||
is_connected = false;
|
||||
wsi_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void WsServer::Private::OnWritable(struct lws * wsi)
|
||||
{
|
||||
bool notify = false;
|
||||
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
|
||||
if (!writeQueue.empty())
|
||||
{
|
||||
std::string const & message = writeQueue.front();
|
||||
|
||||
unsigned char * data = new unsigned char[LWS_PRE + message.size()];
|
||||
memcpy(&data[LWS_PRE], message.c_str(), message.size());
|
||||
lws_write(wsi, &data[LWS_PRE], message.size(), LWS_WRITE_TEXT);
|
||||
delete[] data;
|
||||
|
||||
writeQueue.pop();
|
||||
notify = !writeQueue.empty();
|
||||
}
|
||||
}
|
||||
|
||||
if (notify)
|
||||
{
|
||||
lws_callback_on_writable(wsi);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void WsServer::Private::SendMessage(json_t * message)
|
||||
{
|
||||
lws * wsi = nullptr;
|
||||
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
|
||||
if (nullptr != wsi_)
|
||||
{
|
||||
char* message_text = json_dumps(message, JSON_COMPACT);
|
||||
writeQueue.push(message_text);
|
||||
json_decref(message);
|
||||
free(message_text);
|
||||
wsi = wsi_;
|
||||
}
|
||||
}
|
||||
|
||||
if (nullptr != wsi)
|
||||
{
|
||||
lws_callback_on_writable(wsi_);
|
||||
}
|
||||
}
|
||||
|
||||
void WsServer::Private::OnMessageReceived(struct lws * wsi, char const * data, size_t length)
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
if (wsi == wsi_)
|
||||
{
|
||||
recvQueue.push(std::string(data, length));
|
||||
}
|
||||
}
|
||||
|
||||
json_t * WsServer::Private::ReceiveMessage()
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
|
||||
json_t * result = nullptr;
|
||||
if (!recvQueue.empty())
|
||||
{
|
||||
std::string const & message_text = recvQueue.front();
|
||||
result = json_loads(message_text.c_str(), JSON_DECODE_ANY, nullptr);
|
||||
recvQueue.pop();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string WsServer::Private::GetUrl() const
|
||||
{
|
||||
std::ostringstream stream;
|
||||
stream << "ws://localhost:" << port_ << "/";
|
||||
return stream.str();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
29
test/webfuse_provider/utils/ws_server.h
Normal file
29
test/webfuse_provider/utils/ws_server.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#ifndef WF_TEST_UTILS_WS_SERVER_HPP
|
||||
#define WF_TEST_UTILS_WS_SERVER_HPP
|
||||
|
||||
#include <jansson.h>
|
||||
#include <string>
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
class WsServer
|
||||
{
|
||||
WsServer(WsServer const &) = delete;
|
||||
WsServer & operator=(WsServer const &) = delete;
|
||||
public:
|
||||
WsServer(std::string const & protocol, int port = 0);
|
||||
~WsServer();
|
||||
bool IsConnected();
|
||||
std::string GetUrl() const;
|
||||
void SendMessage(json_t * message);
|
||||
json_t * ReceiveMessage();
|
||||
private:
|
||||
class Private;
|
||||
Private * d;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
331
test/webfuse_provider/utils/ws_server2.cc
Normal file
331
test/webfuse_provider/utils/ws_server2.cc
Normal file
@@ -0,0 +1,331 @@
|
||||
#include "webfuse_provider/utils/ws_server2.hpp"
|
||||
#include "webfuse_provider/impl/lws_log.h"
|
||||
|
||||
#include <libwebsockets.h>
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <chrono>
|
||||
#include <sstream>
|
||||
#include <queue>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
class IServer
|
||||
{
|
||||
public:
|
||||
virtual ~IServer() = default;
|
||||
virtual void OnConnected(lws * wsi) = 0;
|
||||
virtual void OnConnectionClosed(lws * wsi) = 0;
|
||||
virtual void OnMessageReceived(struct lws * wsi, char const * data, size_t length) = 0;
|
||||
virtual void OnWritable(struct lws * wsi) = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
extern "C"
|
||||
{
|
||||
|
||||
static int wf_test_utils_ws_server_callback(
|
||||
struct lws * wsi,
|
||||
enum lws_callback_reasons reason,
|
||||
void * user,
|
||||
void * in,
|
||||
size_t len)
|
||||
{
|
||||
int result = 0;
|
||||
struct lws_protocols const * ws_protocol = lws_get_protocol(wsi);
|
||||
auto * server = reinterpret_cast<IServer*>(nullptr != ws_protocol ? ws_protocol->user : nullptr);
|
||||
|
||||
if (nullptr != server)
|
||||
{
|
||||
switch (reason)
|
||||
{
|
||||
case LWS_CALLBACK_ESTABLISHED:
|
||||
server->OnConnected(wsi);
|
||||
break;
|
||||
case LWS_CALLBACK_CLOSED:
|
||||
server->OnConnectionClosed(wsi);
|
||||
break;
|
||||
case LWS_CALLBACK_RECEIVE:
|
||||
{
|
||||
auto * data = reinterpret_cast<char const *>(in);
|
||||
server->OnMessageReceived(wsi, data, len);
|
||||
}
|
||||
break;
|
||||
case LWS_CALLBACK_SERVER_WRITEABLE:
|
||||
server->OnWritable(wsi);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
class WsServer2::Private : public IServer
|
||||
{
|
||||
Private(Private const &) = delete;
|
||||
Private & operator=(Private const &) = delete;
|
||||
public:
|
||||
Private(IIvokationHandler & handler, std::string const & protocol, int port, bool enable_tls);
|
||||
~Private();
|
||||
bool IsConnected();
|
||||
std::string const & GetUrl() const;
|
||||
void OnConnected(lws * wsi) override;
|
||||
void OnConnectionClosed(lws * wsi) override;
|
||||
void OnMessageReceived(struct lws * wsi, char const * data, size_t length) override;
|
||||
void OnWritable(struct lws * wsi) override;
|
||||
|
||||
void SendMessage(char const * message);
|
||||
void SendMessage(json_t * message);
|
||||
private:
|
||||
static void Run(Private * self);
|
||||
|
||||
IIvokationHandler & handler_;
|
||||
std::string protocol_;
|
||||
bool is_connected;
|
||||
bool is_shutdown_requested;
|
||||
lws * wsi_;
|
||||
lws_context * ws_context;
|
||||
lws_protocols ws_protocols[2];
|
||||
lws_context_creation_info info;
|
||||
std::string url;
|
||||
std::thread context;
|
||||
std::mutex mutex;
|
||||
std::queue<std::string> writeQueue;
|
||||
};
|
||||
|
||||
WsServer2::WsServer2(
|
||||
IIvokationHandler& handler,
|
||||
std::string const & protocol,
|
||||
int port,
|
||||
bool enable_tls)
|
||||
: d(new WsServer2::Private(handler, protocol, port, enable_tls))
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
WsServer2::~WsServer2()
|
||||
{
|
||||
delete d;
|
||||
}
|
||||
|
||||
bool WsServer2::IsConnected()
|
||||
{
|
||||
return d->IsConnected();
|
||||
}
|
||||
|
||||
std::string const & WsServer2::GetUrl() const
|
||||
{
|
||||
return d->GetUrl();
|
||||
}
|
||||
|
||||
void WsServer2::SendMessage(char const * message)
|
||||
{
|
||||
d->SendMessage(message);
|
||||
}
|
||||
|
||||
void WsServer2::SendMessage(json_t * message)
|
||||
{
|
||||
d->SendMessage(message);
|
||||
}
|
||||
|
||||
|
||||
WsServer2::Private::Private(
|
||||
IIvokationHandler & handler,
|
||||
std::string const & protocol,
|
||||
int port,
|
||||
bool enable_tls)
|
||||
: handler_(handler)
|
||||
, protocol_(protocol)
|
||||
, is_connected(false)
|
||||
, is_shutdown_requested(false)
|
||||
, wsi_(nullptr)
|
||||
{
|
||||
wf_lwslog_disable();
|
||||
IServer * server = this;
|
||||
memset(ws_protocols, 0, sizeof(struct lws_protocols) * 2 );
|
||||
|
||||
ws_protocols[0].name = protocol_.c_str();
|
||||
ws_protocols[0].callback = &wf_test_utils_ws_server_callback;
|
||||
ws_protocols[0].per_session_data_size = 0;
|
||||
ws_protocols[0].user = reinterpret_cast<void*>(server);
|
||||
|
||||
memset(&info, 0, sizeof(struct lws_context_creation_info));
|
||||
info.port = port;
|
||||
info.mounts = NULL;
|
||||
info.protocols =ws_protocols;
|
||||
info.vhost_name = "localhost";
|
||||
info.ws_ping_pong_interval = 10;
|
||||
info.options = LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE;
|
||||
info.options |= LWS_SERVER_OPTION_EXPLICIT_VHOSTS;
|
||||
|
||||
if (enable_tls)
|
||||
{
|
||||
info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
|
||||
info.ssl_cert_filepath = "server-cert.pem";
|
||||
info.ssl_private_key_filepath = "server-key.pem";
|
||||
}
|
||||
|
||||
ws_context = lws_create_context(&info);
|
||||
|
||||
std::ostringstream stream;
|
||||
struct lws_vhost * vhost = lws_create_vhost(ws_context, &info);
|
||||
stream << (enable_tls ? "wss://" : "ws://")
|
||||
<< "localhost:" << lws_get_vhost_port(vhost) << "/";
|
||||
url = stream.str();
|
||||
|
||||
context = std::thread(&Run, this);
|
||||
}
|
||||
|
||||
WsServer2::Private::~Private()
|
||||
{
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
is_shutdown_requested = true;
|
||||
}
|
||||
|
||||
lws_cancel_service(ws_context);
|
||||
context.join();
|
||||
lws_context_destroy(ws_context);
|
||||
}
|
||||
|
||||
void WsServer2::Private::Run(Private * self)
|
||||
{
|
||||
bool is_running = true;
|
||||
while (is_running)
|
||||
{
|
||||
lws_service(self->ws_context, 0);
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(self->mutex);
|
||||
is_running = !self->is_shutdown_requested;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool WsServer2::Private::IsConnected()
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
return is_connected;
|
||||
}
|
||||
|
||||
void WsServer2::Private::OnConnected(lws * wsi)
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
is_connected = true;
|
||||
wsi_ = wsi;
|
||||
}
|
||||
|
||||
void WsServer2::Private::OnConnectionClosed(lws * wsi)
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
if (wsi == wsi_)
|
||||
{
|
||||
is_connected = false;
|
||||
wsi_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void WsServer2::Private::OnWritable(struct lws * wsi)
|
||||
{
|
||||
bool notify = false;
|
||||
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
|
||||
if (!writeQueue.empty())
|
||||
{
|
||||
std::string const & message = writeQueue.front();
|
||||
|
||||
unsigned char * data = new unsigned char[LWS_PRE + message.size()];
|
||||
memcpy(&data[LWS_PRE], message.c_str(), message.size());
|
||||
lws_write(wsi, &data[LWS_PRE], message.size(), LWS_WRITE_TEXT);
|
||||
delete[] data;
|
||||
|
||||
writeQueue.pop();
|
||||
notify = !writeQueue.empty();
|
||||
}
|
||||
}
|
||||
|
||||
if (notify)
|
||||
{
|
||||
lws_callback_on_writable(wsi);
|
||||
}
|
||||
}
|
||||
|
||||
void WsServer2::Private::SendMessage(char const * message)
|
||||
{
|
||||
lws * wsi = nullptr;
|
||||
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
|
||||
if (nullptr != wsi_)
|
||||
{
|
||||
writeQueue.push(message);
|
||||
wsi = wsi_;
|
||||
}
|
||||
}
|
||||
|
||||
if (nullptr != wsi)
|
||||
{
|
||||
lws_callback_on_writable(wsi_);
|
||||
}
|
||||
}
|
||||
|
||||
void WsServer2::Private::SendMessage(json_t * message)
|
||||
{
|
||||
char* message_text = json_dumps(message, JSON_COMPACT);
|
||||
SendMessage(message_text);
|
||||
json_decref(message);
|
||||
free(message_text);
|
||||
}
|
||||
|
||||
void WsServer2::Private::OnMessageReceived(struct lws * wsi, char const * data, size_t length)
|
||||
{
|
||||
(void) wsi;
|
||||
|
||||
json_t * request = json_loadb(data, length, JSON_DECODE_ANY, nullptr);
|
||||
json_t * method = json_object_get(request, "method");
|
||||
json_t * params = json_object_get(request, "params");
|
||||
json_t * id = json_object_get(request, "id");
|
||||
|
||||
if (json_is_string(method) && json_is_array(params) && json_is_integer(id))
|
||||
{
|
||||
json_t * response = json_object();
|
||||
|
||||
try
|
||||
{
|
||||
std::string result_text = handler_.Invoke(json_string_value(method), params);
|
||||
json_t * result = json_loads(result_text.c_str(), JSON_DECODE_ANY, nullptr);
|
||||
json_object_set_new(response, "result", result);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
json_t * error = json_object();
|
||||
json_object_set_new(error, "code", json_integer(1));
|
||||
json_object_set_new(response, "error", error);
|
||||
}
|
||||
|
||||
json_object_set(response, "id", id);
|
||||
SendMessage(response);
|
||||
}
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
std::string const & WsServer2::Private::GetUrl() const
|
||||
{
|
||||
return url;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
39
test/webfuse_provider/utils/ws_server2.hpp
Normal file
39
test/webfuse_provider/utils/ws_server2.hpp
Normal file
@@ -0,0 +1,39 @@
|
||||
#ifndef WF_TEST_UTILS_WS_SERVER2_HPP
|
||||
#define WF_TEST_UTILS_WS_SERVER2_HPP
|
||||
|
||||
#include <jansson.h>
|
||||
#include <string>
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
class IIvokationHandler
|
||||
{
|
||||
public:
|
||||
virtual ~IIvokationHandler() = default;
|
||||
virtual std::string Invoke(char const * method, json_t * params) = 0;
|
||||
};
|
||||
|
||||
class WsServer2
|
||||
{
|
||||
WsServer2(WsServer2 const &) = delete;
|
||||
WsServer2 & operator=(WsServer2 const & ) = delete;
|
||||
public:
|
||||
WsServer2(
|
||||
IIvokationHandler& handler,
|
||||
std::string const & protocol,
|
||||
int port = 0,
|
||||
bool enable_tls = false);
|
||||
virtual ~WsServer2();
|
||||
bool IsConnected();
|
||||
std::string const & GetUrl() const;
|
||||
void SendMessage(char const * message);
|
||||
void SendMessage(json_t * message);
|
||||
private:
|
||||
class Private;
|
||||
Private * d;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user