1
0
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:
Falk Werner
2020-06-16 23:39:45 +02:00
parent d041936abf
commit 2f80ebffcc
169 changed files with 384 additions and 418 deletions

View 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;
}
}

View 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

View File

@@ -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);
}
}

View File

@@ -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

View 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);
}

View 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);
}

View 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);
}

View 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);
}

View 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);
}

View File

@@ -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);
}

View 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);
}

View 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);
}

View 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));
}

View 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);
}

View 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));
}

View 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);
}

View 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));
}

View 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);
}

View 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);
}

View 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));
}

View 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);
}

View 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());
}
}

View 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

View 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;
}

View 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;
}
}

View 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

View 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();
}
}

View 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

View 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"));
}

View 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);
}

View 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);
}

View 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);
}

View 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);
}

View 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);
}

View 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);
}

View 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);
}

View 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();
}

View 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);
}