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:
31
test/webfuse_provider/tests/core/jsonrpc/mock_timer.cc
Normal file
31
test/webfuse_provider/tests/core/jsonrpc/mock_timer.cc
Normal file
@@ -0,0 +1,31 @@
|
||||
#include "webfuse_provider/tests/core/jsonrpc/mock_timer.hpp"
|
||||
#include "webfuse_provider/utils/wrap.hpp"
|
||||
|
||||
extern "C"
|
||||
{
|
||||
static wf_jsonrpc_test::ITimer * wf_jsonrpc_MockTimer = nullptr;
|
||||
|
||||
WF_WRAP_FUNC0(wf_jsonrpc_MockTimer, wf_timer_manager *, wf_timer_manager_create);
|
||||
WF_WRAP_FUNC1(wf_jsonrpc_MockTimer, void, wf_timer_manager_dispose, wf_timer_manager *);
|
||||
WF_WRAP_FUNC1(wf_jsonrpc_MockTimer, void, wf_timer_manager_check, wf_timer_manager *);
|
||||
|
||||
WF_WRAP_FUNC3(wf_jsonrpc_MockTimer, wf_timer *, wf_timer_create, wf_timer_manager *, wf_timer_on_timer_fn *, void *);
|
||||
WF_WRAP_FUNC1(wf_jsonrpc_MockTimer, void, wf_timer_dispose, wf_timer *);
|
||||
WF_WRAP_FUNC2(wf_jsonrpc_MockTimer, void, wf_timer_start, wf_timer *, int);
|
||||
WF_WRAP_FUNC1(wf_jsonrpc_MockTimer, void, wf_timer_cancel, wf_timer *);
|
||||
|
||||
}
|
||||
|
||||
namespace wf_jsonrpc_test
|
||||
{
|
||||
MockTimer::MockTimer()
|
||||
{
|
||||
wf_jsonrpc_MockTimer = this;
|
||||
}
|
||||
|
||||
MockTimer::~MockTimer()
|
||||
{
|
||||
wf_jsonrpc_MockTimer = nullptr;
|
||||
}
|
||||
|
||||
}
|
||||
48
test/webfuse_provider/tests/core/jsonrpc/mock_timer.hpp
Normal file
48
test/webfuse_provider/tests/core/jsonrpc/mock_timer.hpp
Normal file
@@ -0,0 +1,48 @@
|
||||
#ifndef WF_JSONRPC_MOCK_TIMERMANAGER_HPP
|
||||
#define WF_JSONRPC_MOCK_TIMERMANAGER_HPP
|
||||
|
||||
#include "webfuse_provider/impl/timer/timer.h"
|
||||
#include "webfuse_provider/impl/timer/manager.h"
|
||||
#include <gmock/gmock.h>
|
||||
|
||||
namespace wf_jsonrpc_test
|
||||
{
|
||||
|
||||
class ITimer
|
||||
{
|
||||
public:
|
||||
virtual ~ITimer() = default;
|
||||
virtual wf_timer_manager * wf_timer_manager_create() = 0;
|
||||
virtual void wf_timer_manager_dispose(wf_timer_manager * manager) = 0;
|
||||
virtual void wf_timer_manager_check(wf_timer_manager * manager) = 0;
|
||||
virtual wf_timer * wf_timer_create(
|
||||
wf_timer_manager * manager,
|
||||
wf_timer_on_timer_fn * on_timer,
|
||||
void * user_data) = 0;
|
||||
virtual void wf_timer_dispose(wf_timer * timer) = 0;
|
||||
virtual void wf_timer_start(wf_timer * timer, int timeout_ms) = 0;
|
||||
virtual void wf_timer_cancel(wf_timer * timer) = 0;
|
||||
};
|
||||
|
||||
class MockTimer: public ITimer
|
||||
{
|
||||
public:
|
||||
MockTimer();
|
||||
~MockTimer() override;
|
||||
MOCK_METHOD0(wf_timer_manager_create, wf_timer_manager * ());
|
||||
MOCK_METHOD1(wf_timer_manager_dispose, void(wf_timer_manager * manager));
|
||||
MOCK_METHOD1(wf_timer_manager_check, void (wf_timer_manager * manager));
|
||||
MOCK_METHOD3(wf_timer_create, wf_timer *(
|
||||
wf_timer_manager * manager,
|
||||
wf_timer_on_timer_fn * on_timer,
|
||||
void * user_data));
|
||||
MOCK_METHOD1(wf_timer_dispose, void (wf_timer * timer));
|
||||
MOCK_METHOD2(wf_timer_start, void (wf_timer * timer, int timeout_ms));
|
||||
MOCK_METHOD1(wf_timer_cancel, void (wf_timer * timer));
|
||||
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,41 @@
|
||||
#include "webfuse_provider/tests/core/jsonrpc/mock_timer_callback.hpp"
|
||||
|
||||
extern "C"
|
||||
{
|
||||
using wf_jsonrpc_test::MockTimerCallback;
|
||||
|
||||
static void wf_jsonrpc_test_MockTimerCallback_on_timer(
|
||||
wf_timer * timer,
|
||||
void * user_data)
|
||||
{
|
||||
auto * self = reinterpret_cast<MockTimerCallback*>(user_data);
|
||||
self->on_timer(timer, user_data);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace wf_jsonrpc_test
|
||||
{
|
||||
|
||||
MockTimerCallback::MockTimerCallback()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
MockTimerCallback::~MockTimerCallback()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
wf_timer_on_timer_fn * MockTimerCallback::on_timer_fn()
|
||||
{
|
||||
return &wf_jsonrpc_test_MockTimerCallback_on_timer;
|
||||
}
|
||||
|
||||
void * MockTimerCallback::user_data()
|
||||
{
|
||||
return reinterpret_cast<void*>(this);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
#ifndef WF_JSONRPC_MOCK_TIMERCALLBACK_HPP
|
||||
#define WF_JSONRPC_MOCK_TIMERCALLBACK_HPP
|
||||
|
||||
#include "webfuse_provider/impl/timer/on_timer_fn.h"
|
||||
#include <gmock/gmock.h>
|
||||
|
||||
namespace wf_jsonrpc_test
|
||||
{
|
||||
class MockTimerCallback
|
||||
{
|
||||
public:
|
||||
MockTimerCallback();
|
||||
virtual ~MockTimerCallback();
|
||||
wf_timer_on_timer_fn * on_timer_fn();
|
||||
void * user_data();
|
||||
|
||||
MOCK_METHOD2(on_timer, void (wf_timer * timer, void * user_data));
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
112
test/webfuse_provider/tests/core/jsonrpc/test_is_request.cc
Normal file
112
test/webfuse_provider/tests/core/jsonrpc/test_is_request.cc
Normal file
@@ -0,0 +1,112 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "webfuse_provider/impl/jsonrpc/request.h"
|
||||
|
||||
TEST(wf_jsonrpc_is_request, request_with_object_params)
|
||||
{
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("method"));
|
||||
json_object_set_new(request, "params", json_object());
|
||||
json_object_set_new(request, "id", json_integer(42));
|
||||
|
||||
ASSERT_TRUE(wf_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_is_request, request_with_array_params)
|
||||
{
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("method"));
|
||||
json_object_set_new(request, "params", json_array());
|
||||
json_object_set_new(request, "id", json_integer(42));
|
||||
|
||||
ASSERT_TRUE(wf_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_is_request, null_request)
|
||||
{
|
||||
ASSERT_FALSE(wf_jsonrpc_is_request(nullptr));
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_is_request, invalid_request)
|
||||
{
|
||||
json_t * request = json_array();
|
||||
json_array_append_new(request, json_string("method"));
|
||||
json_array_append_new(request, json_object());
|
||||
json_array_append_new(request, json_integer(42));
|
||||
|
||||
ASSERT_FALSE(wf_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_is_request, invalid_request_without_id)
|
||||
{
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("method"));
|
||||
json_object_set_new(request, "params", json_object());
|
||||
|
||||
ASSERT_FALSE(wf_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_is_request, invalid_request_due_to_invalid_id)
|
||||
{
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("method"));
|
||||
json_object_set_new(request, "params", json_object());
|
||||
json_object_set_new(request, "id", json_string("42"));
|
||||
|
||||
ASSERT_FALSE(wf_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_is_request, invalid_request_without_method)
|
||||
{
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "params", json_object());
|
||||
json_object_set_new(request, "id", json_integer(42));
|
||||
|
||||
ASSERT_FALSE(wf_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_is_request, invalid_request_due_to_invalid_method)
|
||||
{
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_integer(42));
|
||||
json_object_set_new(request, "params", json_object());
|
||||
json_object_set_new(request, "id", json_integer(42));
|
||||
|
||||
ASSERT_FALSE(wf_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_is_request, invalid_request_without_params)
|
||||
{
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("method"));
|
||||
json_object_set_new(request, "id", json_integer(42));
|
||||
|
||||
ASSERT_FALSE(wf_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_is_request, invalid_request_due_to_invalid_params)
|
||||
{
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "methdo", json_string("method"));
|
||||
json_object_set_new(request, "params", json_string("params"));
|
||||
json_object_set_new(request, "id", json_integer(42));
|
||||
|
||||
ASSERT_FALSE(wf_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
94
test/webfuse_provider/tests/core/jsonrpc/test_is_response.cc
Normal file
94
test/webfuse_provider/tests/core/jsonrpc/test_is_response.cc
Normal file
@@ -0,0 +1,94 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "webfuse_provider/impl/jsonrpc/response.h"
|
||||
|
||||
TEST(wf_jsonrpc_is_response, valid_result)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "result", json_object());
|
||||
json_object_set_new(message, "id", json_integer(42));
|
||||
|
||||
ASSERT_TRUE(wf_jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_is_response, valid_result_string)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "result", json_string("also valid"));
|
||||
json_object_set_new(message, "id", json_integer(42));
|
||||
|
||||
ASSERT_TRUE(wf_jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_is_response, valid_error)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "error", json_object());
|
||||
json_object_set_new(message, "id", json_integer(42));
|
||||
|
||||
ASSERT_TRUE(wf_jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_is_response, invalid_null)
|
||||
{
|
||||
ASSERT_FALSE(wf_jsonrpc_is_response(nullptr));
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_is_response, invalid_message)
|
||||
{
|
||||
json_t * message = json_array();
|
||||
json_array_append_new(message, json_object());
|
||||
json_array_append_new(message, json_integer(42));
|
||||
|
||||
ASSERT_FALSE(wf_jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_is_response, invalid_missing_id)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "result", json_object());
|
||||
|
||||
ASSERT_FALSE(wf_jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_is_response, invalid_id_wrong_type)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "result", json_object());
|
||||
json_object_set_new(message, "id", json_string("42"));
|
||||
|
||||
ASSERT_FALSE(wf_jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
|
||||
TEST(wf_jsonrpc_is_response, invalid_missing_result_and_error)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "id", json_integer(42));
|
||||
|
||||
ASSERT_FALSE(wf_jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_is_response, invalid_error_wrong_type)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "error", json_array());
|
||||
json_object_set_new(message, "id", json_integer(42));
|
||||
|
||||
ASSERT_FALSE(wf_jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
430
test/webfuse_provider/tests/core/jsonrpc/test_proxy.cc
Normal file
430
test/webfuse_provider/tests/core/jsonrpc/test_proxy.cc
Normal file
@@ -0,0 +1,430 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "webfuse_provider/impl/jsonrpc/proxy.h"
|
||||
#include "webfuse_provider/status.h"
|
||||
#include "webfuse_provider/impl/timer/manager.h"
|
||||
|
||||
#include "webfuse_provider/tests/core/jsonrpc/mock_timer.hpp"
|
||||
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
using wf_jsonrpc_test::MockTimer;
|
||||
using testing::Return;
|
||||
using testing::_;
|
||||
using testing::DoAll;
|
||||
using testing::SaveArg;
|
||||
|
||||
#define WF_DEFAULT_TIMEOUT (10 * 1000)
|
||||
|
||||
namespace
|
||||
{
|
||||
int jsonrpc_get_status(json_t * error)
|
||||
{
|
||||
json_t * code = json_object_get(error, "code");
|
||||
return (json_is_integer(code)) ? json_integer_value(code) : WF_BAD_FORMAT;
|
||||
}
|
||||
|
||||
struct SendContext
|
||||
{
|
||||
json_t * response;
|
||||
bool result;
|
||||
bool is_called;
|
||||
|
||||
explicit SendContext(bool result_ = true)
|
||||
: response(nullptr)
|
||||
, result(result_)
|
||||
, is_called(false)
|
||||
{
|
||||
}
|
||||
|
||||
~SendContext()
|
||||
{
|
||||
if (nullptr != response)
|
||||
{
|
||||
json_decref(response);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
bool jsonrpc_send(
|
||||
json_t * request,
|
||||
void * user_data)
|
||||
{
|
||||
SendContext * context = reinterpret_cast<SendContext*>(user_data);
|
||||
context->is_called = true;
|
||||
context->response = request;
|
||||
json_incref(request);
|
||||
|
||||
return context->result;
|
||||
}
|
||||
|
||||
struct FinishedContext
|
||||
{
|
||||
bool is_called;
|
||||
json_t * result;
|
||||
json_t * error;
|
||||
|
||||
FinishedContext()
|
||||
: is_called(false)
|
||||
, result(nullptr)
|
||||
, error(nullptr)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
~FinishedContext()
|
||||
{
|
||||
if (nullptr != result)
|
||||
{
|
||||
json_decref(result);
|
||||
}
|
||||
|
||||
if (nullptr != error)
|
||||
{
|
||||
json_decref(error);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
void jsonrpc_finished(
|
||||
void * user_data,
|
||||
json_t const * result,
|
||||
json_t const * error)
|
||||
{
|
||||
FinishedContext * context = reinterpret_cast<FinishedContext*>(user_data);
|
||||
context->is_called = true;
|
||||
context->result = json_deep_copy(result);
|
||||
context->error = json_deep_copy(error);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_proxy, init)
|
||||
{
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext context;
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, user_data);
|
||||
|
||||
wf_jsonrpc_proxy_dispose(proxy);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
|
||||
ASSERT_FALSE(context.is_called);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_proxy, invoke)
|
||||
{
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
wf_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
|
||||
|
||||
ASSERT_TRUE(send_context.is_called);
|
||||
ASSERT_TRUE(json_is_object(send_context.response));
|
||||
|
||||
json_t * method = json_object_get(send_context.response, "method");
|
||||
ASSERT_TRUE(json_is_string(method));
|
||||
ASSERT_STREQ("foo", json_string_value(method));
|
||||
|
||||
json_t * params = json_object_get(send_context.response, "params");
|
||||
ASSERT_TRUE(json_is_array(params));
|
||||
ASSERT_EQ(2, json_array_size(params));
|
||||
ASSERT_TRUE(json_is_string(json_array_get(params, 0)));
|
||||
ASSERT_STREQ("bar", json_string_value(json_array_get(params, 0)));
|
||||
ASSERT_TRUE(json_is_integer(json_array_get(params, 1)));
|
||||
ASSERT_EQ(42, json_integer_value(json_array_get(params, 1)));
|
||||
|
||||
json_t * id = json_object_get(send_context.response, "id");
|
||||
ASSERT_TRUE(json_is_integer(id));
|
||||
|
||||
ASSERT_FALSE(finished_context.is_called);
|
||||
|
||||
wf_jsonrpc_proxy_dispose(proxy);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
|
||||
ASSERT_TRUE(finished_context.is_called);
|
||||
ASSERT_FALSE(nullptr == finished_context.error);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_proxy, invoke_calls_finish_if_send_fails)
|
||||
{
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext send_context(false);
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
wf_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
|
||||
|
||||
ASSERT_TRUE(send_context.is_called);
|
||||
ASSERT_TRUE(json_is_object(send_context.response));
|
||||
|
||||
ASSERT_TRUE(finished_context.is_called);
|
||||
ASSERT_FALSE(nullptr == finished_context.error);
|
||||
|
||||
wf_jsonrpc_proxy_dispose(proxy);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_proxy, invoke_fails_if_another_request_is_pending)
|
||||
{
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
wf_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
|
||||
|
||||
FinishedContext finished_context2;
|
||||
void * finished_data2 = reinterpret_cast<void*>(&finished_context2);
|
||||
wf_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data2, "foo", "");
|
||||
|
||||
ASSERT_TRUE(send_context.is_called);
|
||||
ASSERT_TRUE(json_is_object(send_context.response));
|
||||
|
||||
ASSERT_FALSE(finished_context.is_called);
|
||||
|
||||
ASSERT_TRUE(finished_context2.is_called);
|
||||
ASSERT_EQ(WF_BAD_BUSY, jsonrpc_get_status(finished_context2.error));
|
||||
|
||||
wf_jsonrpc_proxy_dispose(proxy);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_proxy, invoke_fails_if_request_is_invalid)
|
||||
{
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
wf_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "?", "error");
|
||||
|
||||
ASSERT_FALSE(send_context.is_called);
|
||||
|
||||
ASSERT_TRUE(finished_context.is_called);
|
||||
ASSERT_EQ(WF_BAD, jsonrpc_get_status(finished_context.error));
|
||||
|
||||
wf_jsonrpc_proxy_dispose(proxy);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_proxy, on_result)
|
||||
{
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
wf_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
|
||||
|
||||
ASSERT_TRUE(send_context.is_called);
|
||||
ASSERT_TRUE(json_is_object(send_context.response));
|
||||
|
||||
json_t * id = json_object_get(send_context.response, "id");
|
||||
ASSERT_TRUE(json_is_number(id));
|
||||
|
||||
json_t * response = json_object();
|
||||
json_object_set_new(response, "result", json_string("okay"));
|
||||
json_object_set(response, "id", id);
|
||||
|
||||
wf_jsonrpc_proxy_onresult(proxy, response);
|
||||
json_decref(response);
|
||||
|
||||
ASSERT_TRUE(finished_context.is_called);
|
||||
ASSERT_EQ(nullptr, finished_context.error);
|
||||
ASSERT_TRUE(json_is_string(finished_context.result));
|
||||
ASSERT_STREQ("okay", json_string_value(finished_context.result));
|
||||
|
||||
wf_jsonrpc_proxy_dispose(proxy);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_proxy, on_result_reject_response_with_unknown_id)
|
||||
{
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
wf_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
|
||||
|
||||
ASSERT_TRUE(send_context.is_called);
|
||||
ASSERT_TRUE(json_is_object(send_context.response));
|
||||
|
||||
json_t * id = json_object_get(send_context.response, "id");
|
||||
ASSERT_TRUE(json_is_number(id));
|
||||
|
||||
json_t * response = json_object();
|
||||
json_object_set_new(response, "result", json_string("okay"));
|
||||
json_object_set_new(response, "id", json_integer(1 + json_integer_value(id)));
|
||||
|
||||
wf_jsonrpc_proxy_onresult(proxy, response);
|
||||
json_decref(response);
|
||||
|
||||
ASSERT_FALSE(finished_context.is_called);
|
||||
|
||||
wf_jsonrpc_proxy_dispose(proxy);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_proxy, timeout)
|
||||
{
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(timer_manager, 0, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
wf_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
|
||||
|
||||
ASSERT_TRUE(send_context.is_called);
|
||||
ASSERT_TRUE(json_is_object(send_context.response));
|
||||
|
||||
std::this_thread::sleep_for(10ms);
|
||||
wf_timer_manager_check(timer_manager);
|
||||
|
||||
ASSERT_TRUE(finished_context.is_called);
|
||||
ASSERT_EQ(WF_BAD_TIMEOUT, jsonrpc_get_status(finished_context.error));
|
||||
|
||||
wf_jsonrpc_proxy_dispose(proxy);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_proxy, cleanup_pending_request)
|
||||
{
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(timer_manager, 10, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
wf_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
|
||||
|
||||
ASSERT_TRUE(send_context.is_called);
|
||||
ASSERT_TRUE(json_is_object(send_context.response));
|
||||
|
||||
ASSERT_FALSE(finished_context.is_called);
|
||||
|
||||
wf_jsonrpc_proxy_dispose(proxy);
|
||||
|
||||
ASSERT_TRUE(finished_context.is_called);
|
||||
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
|
||||
|
||||
TEST(wf_jsonrpc_proxy, notify)
|
||||
{
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
wf_jsonrpc_proxy_notify(proxy, "foo", "si", "bar", 42);
|
||||
|
||||
ASSERT_TRUE(send_context.is_called);
|
||||
ASSERT_TRUE(json_is_object(send_context.response));
|
||||
|
||||
json_t * method = json_object_get(send_context.response, "method");
|
||||
ASSERT_TRUE(json_is_string(method));
|
||||
ASSERT_STREQ("foo", json_string_value(method));
|
||||
|
||||
json_t * params = json_object_get(send_context.response, "params");
|
||||
ASSERT_TRUE(json_is_array(params));
|
||||
ASSERT_EQ(2, json_array_size(params));
|
||||
ASSERT_TRUE(json_is_string(json_array_get(params, 0)));
|
||||
ASSERT_STREQ("bar", json_string_value(json_array_get(params, 0)));
|
||||
ASSERT_TRUE(json_is_integer(json_array_get(params, 1)));
|
||||
ASSERT_EQ(42, json_integer_value(json_array_get(params, 1)));
|
||||
|
||||
json_t * id = json_object_get(send_context.response, "id");
|
||||
ASSERT_EQ(nullptr, id);
|
||||
|
||||
wf_jsonrpc_proxy_dispose(proxy);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_proxy, notify_dont_send_invalid_request)
|
||||
{
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
wf_jsonrpc_proxy_notify(proxy, "foo", "?");
|
||||
|
||||
ASSERT_FALSE(send_context.is_called);
|
||||
|
||||
wf_jsonrpc_proxy_dispose(proxy);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_proxy, swallow_timeout_if_no_request_pending)
|
||||
{
|
||||
MockTimer timer_api;
|
||||
|
||||
wf_timer_on_timer_fn * on_timer = nullptr;
|
||||
void * timer_context = nullptr;
|
||||
EXPECT_CALL(timer_api, wf_timer_create(_, _, _))
|
||||
.Times(1)
|
||||
.WillOnce(DoAll(SaveArg<1>(&on_timer), SaveArg<2>(&timer_context), Return(nullptr)));
|
||||
EXPECT_CALL(timer_api, wf_timer_dispose(_)).Times(1);
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(nullptr, 1, &jsonrpc_send, send_data);
|
||||
|
||||
on_timer(nullptr, timer_context);
|
||||
ASSERT_FALSE(send_context.is_called);
|
||||
|
||||
|
||||
wf_jsonrpc_proxy_dispose(proxy);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_proxy, on_result_swallow_if_no_request_pending)
|
||||
{
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
json_t * response = json_object();
|
||||
json_object_set_new(response, "result", json_string("okay"));
|
||||
json_object_set_new(response, "id", json_integer(42));
|
||||
|
||||
wf_jsonrpc_proxy_onresult(proxy, response);
|
||||
json_decref(response);
|
||||
|
||||
wf_jsonrpc_proxy_dispose(proxy);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
138
test/webfuse_provider/tests/core/jsonrpc/test_request.cc
Normal file
138
test/webfuse_provider/tests/core/jsonrpc/test_request.cc
Normal file
@@ -0,0 +1,138 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "webfuse_provider/impl/jsonrpc/request.h"
|
||||
#include "webfuse_provider/status.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
struct Context
|
||||
{
|
||||
json_t * response;
|
||||
};
|
||||
|
||||
bool jsonrpc_send(
|
||||
json_t * request,
|
||||
void * user_data)
|
||||
{
|
||||
Context * context = reinterpret_cast<Context*>(user_data);
|
||||
context->response = request;
|
||||
json_incref(request);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_request, create_dispose)
|
||||
{
|
||||
Context context{nullptr};
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
|
||||
struct wf_jsonrpc_request * request =
|
||||
wf_jsonrpc_request_create(42, &jsonrpc_send, user_data);
|
||||
|
||||
ASSERT_NE(nullptr, request);
|
||||
ASSERT_EQ(user_data, wf_jsonrpc_request_get_userdata(request));
|
||||
|
||||
wf_jsonrpc_request_dispose(request);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_request, respond)
|
||||
{
|
||||
Context context{nullptr};
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
|
||||
struct wf_jsonrpc_request * request =
|
||||
wf_jsonrpc_request_create(42, &jsonrpc_send, user_data);
|
||||
|
||||
wf_jsonrpc_respond(request, json_string("okay"));
|
||||
|
||||
ASSERT_NE(nullptr, context.response);
|
||||
|
||||
|
||||
json_t * response = reinterpret_cast<json_t*>(context.response);
|
||||
ASSERT_TRUE(json_is_object(response));
|
||||
|
||||
json_t * id = json_object_get(response, "id");
|
||||
ASSERT_TRUE(json_is_integer(id));
|
||||
ASSERT_EQ(42, json_integer_value(id));
|
||||
|
||||
json_t * result = json_object_get(response, "result");
|
||||
ASSERT_TRUE(json_is_string(result));
|
||||
ASSERT_STREQ("okay", json_string_value(result));
|
||||
|
||||
ASSERT_EQ(nullptr, json_object_get(response, "error"));
|
||||
|
||||
json_decref(response);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_request, respond_error)
|
||||
{
|
||||
Context context{nullptr};
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
|
||||
struct wf_jsonrpc_request * request =
|
||||
wf_jsonrpc_request_create(42, &jsonrpc_send, user_data);
|
||||
|
||||
wf_jsonrpc_respond_error(request, WF_BAD, "Bad");
|
||||
|
||||
ASSERT_NE(nullptr, context.response);
|
||||
|
||||
|
||||
json_t * response = reinterpret_cast<json_t*>(context.response);
|
||||
ASSERT_TRUE(json_is_object(response));
|
||||
|
||||
json_t * id = json_object_get(response, "id");
|
||||
ASSERT_TRUE(json_is_integer(id));
|
||||
ASSERT_EQ(42, json_integer_value(id));
|
||||
|
||||
ASSERT_EQ(nullptr, json_object_get(response, "result"));
|
||||
|
||||
json_t * err = json_object_get(response, "error");
|
||||
ASSERT_TRUE(json_is_object(err));
|
||||
|
||||
json_t * err_code = json_object_get(err, "code");
|
||||
ASSERT_TRUE(json_is_integer(err_code));
|
||||
ASSERT_EQ(WF_BAD, json_integer_value(err_code));
|
||||
|
||||
json_t * err_message = json_object_get(err, "message");
|
||||
ASSERT_TRUE(json_is_string(err_message));
|
||||
ASSERT_STREQ("Bad", json_string_value(err_message));
|
||||
|
||||
json_decref(response);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_request, is_request_object_params)
|
||||
{
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("some_method"));
|
||||
json_object_set_new(request, "params", json_object());
|
||||
json_object_set_new(request, "id", json_integer(42));
|
||||
|
||||
ASSERT_TRUE(wf_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_request, is_request_fail_missing_params)
|
||||
{
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("some_method"));
|
||||
json_object_set_new(request, "id", json_integer(42));
|
||||
|
||||
ASSERT_FALSE(wf_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_request, is_request_fail_params_wrong_type)
|
||||
{
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("some_method"));
|
||||
json_object_set_new(request, "params", json_string("invalid_params"));
|
||||
json_object_set_new(request, "id", json_integer(42));
|
||||
|
||||
ASSERT_FALSE(wf_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
147
test/webfuse_provider/tests/core/jsonrpc/test_response.cc
Normal file
147
test/webfuse_provider/tests/core/jsonrpc/test_response.cc
Normal file
@@ -0,0 +1,147 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "webfuse_provider/impl/jsonrpc/response_intern.h"
|
||||
#include "webfuse_provider/status.h"
|
||||
|
||||
TEST(wf_json_response, init_result)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "result", json_integer(47));
|
||||
json_object_set_new(message, "id", json_integer(11));
|
||||
|
||||
struct wf_jsonrpc_response response;
|
||||
wf_jsonrpc_response_init(&response, message);
|
||||
|
||||
ASSERT_EQ(nullptr, response.error);
|
||||
ASSERT_TRUE(json_is_integer(response.result));
|
||||
ASSERT_EQ(47, json_integer_value(response.result));
|
||||
ASSERT_EQ(11, response.id);
|
||||
|
||||
wf_jsonrpc_response_cleanup(&response);
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wf_json_response, init_error)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_t * err = json_object();
|
||||
json_object_set_new(err, "code", json_integer(42));
|
||||
json_object_set_new(err, "message", json_string("Don't Panic!"));
|
||||
json_object_set_new(message, "error", err);
|
||||
json_object_set_new(message, "id", json_integer(23));
|
||||
|
||||
struct wf_jsonrpc_response response;
|
||||
wf_jsonrpc_response_init(&response, message);
|
||||
|
||||
ASSERT_EQ(42, json_integer_value(json_object_get(response.error, "code")));
|
||||
ASSERT_STREQ("Don't Panic!", json_string_value(json_object_get(response.error, "message")));
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
ASSERT_EQ(23, response.id);
|
||||
|
||||
wf_jsonrpc_response_cleanup(&response);
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wf_json_response, init_fail_missing_result_and_error)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "id", json_integer(12));
|
||||
|
||||
struct wf_jsonrpc_response response;
|
||||
wf_jsonrpc_response_init(&response, message);
|
||||
|
||||
ASSERT_EQ(WF_BAD_FORMAT, json_integer_value(json_object_get(response.error, "code")));
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
ASSERT_EQ(12, response.id);
|
||||
|
||||
wf_jsonrpc_response_cleanup(&response);
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wf_json_response, init_fail_missing_id)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "result", json_integer(47));
|
||||
|
||||
struct wf_jsonrpc_response response;
|
||||
wf_jsonrpc_response_init(&response, message);
|
||||
|
||||
ASSERT_EQ(WF_BAD_FORMAT, json_integer_value(json_object_get(response.error, "code")));
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
ASSERT_EQ(-1, response.id);
|
||||
|
||||
wf_jsonrpc_response_cleanup(&response);
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wf_json_response, init_fail_wrong_id_type)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "result", json_integer(47));
|
||||
json_object_set_new(message, "id", json_string("42"));
|
||||
|
||||
struct wf_jsonrpc_response response;
|
||||
wf_jsonrpc_response_init(&response, message);
|
||||
|
||||
ASSERT_EQ(WF_BAD_FORMAT, json_integer_value(json_object_get(response.error, "code")));
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
ASSERT_EQ(-1, response.id);
|
||||
|
||||
wf_jsonrpc_response_cleanup(&response);
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wf_json_response, init_fail_error_missing_code)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_t * err = json_object();
|
||||
json_object_set_new(err, "message", json_string("Don't Panic!"));
|
||||
json_object_set_new(message, "error", err);
|
||||
json_object_set_new(message, "id", json_integer(23));
|
||||
|
||||
struct wf_jsonrpc_response response;
|
||||
wf_jsonrpc_response_init(&response, message);
|
||||
|
||||
ASSERT_EQ(WF_BAD_FORMAT, json_integer_value(json_object_get(response.error, "code")));
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
ASSERT_EQ(23, response.id);
|
||||
|
||||
wf_jsonrpc_response_cleanup(&response);
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wf_json_response, init_fail_error_wrong_code_type)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_t * err = json_object();
|
||||
json_object_set_new(err, "code", json_string("42"));
|
||||
json_object_set_new(err, "message", json_string("Don't Panic!"));
|
||||
json_object_set_new(message, "error", err);
|
||||
json_object_set_new(message, "id", json_integer(23));
|
||||
|
||||
struct wf_jsonrpc_response response;
|
||||
wf_jsonrpc_response_init(&response, message);
|
||||
|
||||
ASSERT_EQ(WF_BAD_FORMAT, json_integer_value(json_object_get(response.error, "code")));
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
ASSERT_EQ(23, response.id);
|
||||
|
||||
wf_jsonrpc_response_cleanup(&response);
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wf_json_response, init_fail_error_wrong_type)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "error", json_string("invalid error type"));
|
||||
json_object_set_new(message, "id", json_integer(23));
|
||||
|
||||
struct wf_jsonrpc_response response;
|
||||
wf_jsonrpc_response_init(&response, message);
|
||||
|
||||
ASSERT_EQ(WF_BAD_FORMAT, json_integer_value(json_object_get(response.error, "code")));
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
ASSERT_EQ(23, response.id);
|
||||
|
||||
wf_jsonrpc_response_cleanup(&response);
|
||||
json_decref(message);
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
#include <string>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "webfuse_provider/impl/jsonrpc/response_intern.h"
|
||||
|
||||
|
||||
static void response_parse_str(
|
||||
std::string const & buffer,
|
||||
struct wf_jsonrpc_response * response)
|
||||
{
|
||||
json_t * message = json_loadb(buffer.c_str(), buffer.size(), 0, nullptr);
|
||||
if (nullptr != message)
|
||||
{
|
||||
wf_jsonrpc_response_init(response, message);
|
||||
json_decref(message);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(response_parser, test)
|
||||
{
|
||||
struct wf_jsonrpc_response response;
|
||||
|
||||
// no object
|
||||
response_parse_str("[]", &response);
|
||||
ASSERT_NE(nullptr, response.error);
|
||||
ASSERT_EQ(-1, response.id);
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
wf_jsonrpc_response_cleanup(&response);
|
||||
|
||||
// empty
|
||||
response_parse_str("{}", &response);
|
||||
ASSERT_NE(nullptr, response.error);
|
||||
ASSERT_EQ(-1, response.id);
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
wf_jsonrpc_response_cleanup(&response);
|
||||
|
||||
// no data
|
||||
response_parse_str("{\"id\":42}", &response);
|
||||
ASSERT_NE(nullptr, response.error);
|
||||
ASSERT_EQ(42, response.id);
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
wf_jsonrpc_response_cleanup(&response);
|
||||
|
||||
// custom error code
|
||||
response_parse_str("{\"error\":{\"code\": 42}, \"id\": 42}", &response);
|
||||
ASSERT_NE(nullptr, response.error);
|
||||
ASSERT_EQ(42, json_integer_value(json_object_get(response.error, "code")));
|
||||
ASSERT_EQ(42, response.id);
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
wf_jsonrpc_response_cleanup(&response);
|
||||
|
||||
// valid response
|
||||
response_parse_str("{\"result\": true, \"id\": 42}", &response);
|
||||
ASSERT_EQ(nullptr, response.error);
|
||||
ASSERT_EQ(42, response.id);
|
||||
ASSERT_NE(nullptr, response.result);
|
||||
wf_jsonrpc_response_cleanup(&response);
|
||||
}
|
||||
241
test/webfuse_provider/tests/core/jsonrpc/test_server.cc
Normal file
241
test/webfuse_provider/tests/core/jsonrpc/test_server.cc
Normal file
@@ -0,0 +1,241 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "webfuse_provider/impl/jsonrpc/server.h"
|
||||
#include "webfuse_provider/impl/jsonrpc/request.h"
|
||||
#include "webfuse_provider/status.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
struct Context
|
||||
{
|
||||
json_t * response;
|
||||
bool is_called;
|
||||
};
|
||||
|
||||
bool jsonrpc_send(
|
||||
json_t * request,
|
||||
void * user_data)
|
||||
{
|
||||
Context * context = reinterpret_cast<Context*>(user_data);
|
||||
context->is_called = true;
|
||||
context->response = request;
|
||||
json_incref(request);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void sayHello(
|
||||
struct wf_jsonrpc_request * request,
|
||||
char const * method_name,
|
||||
json_t * params,
|
||||
void * user_data)
|
||||
{
|
||||
(void) method_name;
|
||||
(void) params;
|
||||
(void) user_data;
|
||||
|
||||
json_t * result = json_string("Hello");
|
||||
wf_jsonrpc_respond(request, result);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_server, process_request)
|
||||
{
|
||||
struct wf_jsonrpc_server * server = wf_jsonrpc_server_create();
|
||||
wf_jsonrpc_server_add(server, "sayHello", &sayHello, nullptr);
|
||||
|
||||
Context context{nullptr, false};
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("sayHello"));
|
||||
json_object_set_new(request, "params", json_array());
|
||||
json_object_set_new(request, "id", json_integer(23));
|
||||
wf_jsonrpc_server_process(server, request, &jsonrpc_send, user_data);
|
||||
|
||||
ASSERT_TRUE(context.is_called);
|
||||
ASSERT_NE(nullptr, context.response);
|
||||
ASSERT_TRUE(json_is_object(context.response));
|
||||
|
||||
json_t * id = json_object_get(context.response, "id");
|
||||
ASSERT_TRUE(json_is_integer(id));
|
||||
ASSERT_EQ(23, json_integer_value(id));
|
||||
|
||||
json_t * result = json_object_get(context.response, "result");
|
||||
ASSERT_TRUE(json_is_string(result));
|
||||
ASSERT_STREQ("Hello", json_string_value(result));
|
||||
|
||||
json_decref(context.response);
|
||||
json_decref(request);
|
||||
wf_jsonrpc_server_dispose(server);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_server, process_request_with_oject_params)
|
||||
{
|
||||
struct wf_jsonrpc_server * server = wf_jsonrpc_server_create();
|
||||
wf_jsonrpc_server_add(server, "sayHello", &sayHello, nullptr);
|
||||
|
||||
Context context{nullptr, false};
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("sayHello"));
|
||||
json_object_set_new(request, "params", json_object());
|
||||
json_object_set_new(request, "id", json_integer(23));
|
||||
wf_jsonrpc_server_process(server, request, &jsonrpc_send, user_data);
|
||||
|
||||
ASSERT_TRUE(context.is_called);
|
||||
ASSERT_NE(nullptr, context.response);
|
||||
ASSERT_TRUE(json_is_object(context.response));
|
||||
|
||||
json_t * id = json_object_get(context.response, "id");
|
||||
ASSERT_TRUE(json_is_integer(id));
|
||||
ASSERT_EQ(23, json_integer_value(id));
|
||||
|
||||
json_t * result = json_object_get(context.response, "result");
|
||||
ASSERT_TRUE(json_is_string(result));
|
||||
ASSERT_STREQ("Hello", json_string_value(result));
|
||||
|
||||
json_decref(context.response);
|
||||
json_decref(request);
|
||||
wf_jsonrpc_server_dispose(server);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_server, invoke_unknown_method)
|
||||
{
|
||||
struct wf_jsonrpc_server * server = wf_jsonrpc_server_create();
|
||||
wf_jsonrpc_server_add(server, "sayHello", &sayHello, nullptr);
|
||||
|
||||
Context context{nullptr, false};
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("greet"));
|
||||
json_object_set_new(request, "params", json_array());
|
||||
json_object_set_new(request, "id", json_integer(42));
|
||||
wf_jsonrpc_server_process(server, request, &jsonrpc_send, user_data);
|
||||
|
||||
ASSERT_TRUE(context.is_called);
|
||||
ASSERT_NE(nullptr, context.response);
|
||||
ASSERT_TRUE(json_is_object(context.response));
|
||||
|
||||
json_t * id = json_object_get(context.response, "id");
|
||||
ASSERT_TRUE(json_is_integer(id));
|
||||
ASSERT_EQ(42, json_integer_value(id));
|
||||
|
||||
json_t * err = json_object_get(context.response, "error");
|
||||
ASSERT_TRUE(json_is_object(err));
|
||||
|
||||
json_t * err_code = json_object_get(err, "code");
|
||||
ASSERT_TRUE(json_is_integer(err_code));
|
||||
ASSERT_EQ(WF_BAD_NOTIMPLEMENTED, json_integer_value(err_code));
|
||||
|
||||
json_t * err_message = json_object_get(err, "message");
|
||||
ASSERT_TRUE(json_is_string(err_message));
|
||||
|
||||
json_decref(context.response);
|
||||
json_decref(request);
|
||||
wf_jsonrpc_server_dispose(server);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_server, skip_invalid_request_missing_id)
|
||||
{
|
||||
struct wf_jsonrpc_server * server = wf_jsonrpc_server_create();
|
||||
|
||||
Context context{nullptr, false};
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("sayHello"));
|
||||
json_object_set_new(request, "params", json_array());
|
||||
wf_jsonrpc_server_process(server, request, &jsonrpc_send, user_data);
|
||||
|
||||
ASSERT_FALSE(context.is_called);
|
||||
|
||||
json_decref(request);
|
||||
wf_jsonrpc_server_dispose(server);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_server, skip_invalid_request_wrong_id_type)
|
||||
{
|
||||
struct wf_jsonrpc_server * server = wf_jsonrpc_server_create();
|
||||
|
||||
Context context{nullptr, false};
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("sayHello"));
|
||||
json_object_set_new(request, "params", json_array());
|
||||
json_object_set_new(request, "id", json_string("42"));
|
||||
wf_jsonrpc_server_process(server, request, &jsonrpc_send, user_data);
|
||||
|
||||
ASSERT_FALSE(context.is_called);
|
||||
|
||||
json_decref(request);
|
||||
wf_jsonrpc_server_dispose(server);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_server, skip_invalid_request_missing_params)
|
||||
{
|
||||
struct wf_jsonrpc_server * server = wf_jsonrpc_server_create();
|
||||
|
||||
Context context{nullptr, false};
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("sayHello"));
|
||||
json_object_set_new(request, "id", json_integer(42));
|
||||
wf_jsonrpc_server_process(server, request, &jsonrpc_send, user_data);
|
||||
|
||||
ASSERT_FALSE(context.is_called);
|
||||
|
||||
json_decref(request);
|
||||
wf_jsonrpc_server_dispose(server);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_server, skip_invalid_request_wrong_params_type)
|
||||
{
|
||||
struct wf_jsonrpc_server * server = wf_jsonrpc_server_create();
|
||||
|
||||
Context context{nullptr, false};
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string("sayHello"));
|
||||
json_object_set_new(request, "params", json_string("invalid"));
|
||||
json_object_set_new(request, "id", json_integer(42));
|
||||
wf_jsonrpc_server_process(server, request, &jsonrpc_send, user_data);
|
||||
|
||||
ASSERT_FALSE(context.is_called);
|
||||
|
||||
json_decref(request);
|
||||
wf_jsonrpc_server_dispose(server);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_server, skip_invalid_request_missing_method)
|
||||
{
|
||||
struct wf_jsonrpc_server * server = wf_jsonrpc_server_create();
|
||||
|
||||
Context context{nullptr, false};
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "params", json_array());
|
||||
json_object_set_new(request, "id", json_integer(42));
|
||||
wf_jsonrpc_server_process(server, request, &jsonrpc_send, user_data);
|
||||
|
||||
ASSERT_FALSE(context.is_called);
|
||||
|
||||
json_decref(request);
|
||||
wf_jsonrpc_server_dispose(server);
|
||||
}
|
||||
|
||||
TEST(wf_jsonrpc_server, skip_invalid_request_wront_method_type)
|
||||
{
|
||||
struct wf_jsonrpc_server * server = wf_jsonrpc_server_create();
|
||||
|
||||
Context context{nullptr, false};
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_integer(42));
|
||||
json_object_set_new(request, "params", json_array());
|
||||
json_object_set_new(request, "id", json_integer(42));
|
||||
wf_jsonrpc_server_process(server, request, &jsonrpc_send, user_data);
|
||||
|
||||
ASSERT_FALSE(context.is_called);
|
||||
|
||||
json_decref(request);
|
||||
wf_jsonrpc_server_dispose(server);
|
||||
}
|
||||
122
test/webfuse_provider/tests/core/test_base64.cc
Normal file
122
test/webfuse_provider/tests/core/test_base64.cc
Normal file
@@ -0,0 +1,122 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "webfuse_provider/impl/base64.h"
|
||||
|
||||
TEST(Base64, EncodedSize)
|
||||
{
|
||||
ASSERT_EQ(4, wf_base64_encoded_size(1));
|
||||
ASSERT_EQ(4, wf_base64_encoded_size(2));
|
||||
ASSERT_EQ(4, wf_base64_encoded_size(3));
|
||||
|
||||
ASSERT_EQ(8, wf_base64_encoded_size(4));
|
||||
ASSERT_EQ(8, wf_base64_encoded_size(5));
|
||||
ASSERT_EQ(8, wf_base64_encoded_size(6));
|
||||
|
||||
ASSERT_EQ(120, wf_base64_encoded_size(90));
|
||||
}
|
||||
|
||||
TEST(Base64, Encode)
|
||||
{
|
||||
char buffer[42];
|
||||
|
||||
std::string in = "Hello";
|
||||
size_t length = wf_base64_encode((uint8_t const*) in.c_str(), in.size(), buffer, 42);
|
||||
ASSERT_EQ(8, length);
|
||||
ASSERT_STREQ("SGVsbG8=", buffer);
|
||||
|
||||
in = "Hello\n";
|
||||
length = wf_base64_encode((uint8_t const*) in.c_str(), in.size(), buffer, 42);
|
||||
ASSERT_EQ(8, length);
|
||||
ASSERT_STREQ("SGVsbG8K", buffer);
|
||||
|
||||
in = "Blue";
|
||||
length = wf_base64_encode((uint8_t const*) in.c_str(), in.size(), buffer, 42);
|
||||
ASSERT_EQ(8, length);
|
||||
ASSERT_STREQ("Qmx1ZQ==", buffer);
|
||||
}
|
||||
|
||||
TEST(Base64, FailedToEncodeBufferTooSmall)
|
||||
{
|
||||
char buffer[1];
|
||||
|
||||
std::string in = "Hello";
|
||||
size_t length = wf_base64_encode((uint8_t const*) in.c_str(), in.size(), buffer, 1);
|
||||
ASSERT_EQ(0, length);
|
||||
}
|
||||
|
||||
TEST(Base64, DecodedSize)
|
||||
{
|
||||
std::string in = "SGVsbG8="; // Hello
|
||||
size_t length = wf_base64_decoded_size(in.c_str(), in.size());
|
||||
ASSERT_EQ(5, length);
|
||||
|
||||
in = "SGVsbG8K"; // Hello\n
|
||||
length = wf_base64_decoded_size(in.c_str(), in.size());
|
||||
ASSERT_EQ(6, length);
|
||||
|
||||
in = "Qmx1ZQ=="; // Blue
|
||||
length = wf_base64_decoded_size(in.c_str(), in.size());
|
||||
ASSERT_EQ(4, length);
|
||||
}
|
||||
|
||||
TEST(Base64, IsValid)
|
||||
{
|
||||
std::string in = "SGVsbG8="; // Hello
|
||||
ASSERT_TRUE(wf_base64_isvalid(in.c_str(), in.size()));
|
||||
|
||||
in = "SGVsbG8K"; // Hello\n
|
||||
ASSERT_TRUE(wf_base64_isvalid(in.c_str(), in.size()));
|
||||
|
||||
in = "Qmx1ZQ=="; // Blue
|
||||
ASSERT_TRUE(wf_base64_isvalid(in.c_str(), in.size()));
|
||||
|
||||
in = "Qmx1ZQ=a";
|
||||
ASSERT_FALSE(wf_base64_isvalid(in.c_str(), in.size()));
|
||||
|
||||
in = "Qmx1ZQ";
|
||||
ASSERT_FALSE(wf_base64_isvalid(in.c_str(), in.size()));
|
||||
|
||||
in = "Qmx1ZQ=";
|
||||
ASSERT_FALSE(wf_base64_isvalid(in.c_str(), in.size()));
|
||||
|
||||
in = "Qmx1Z===";
|
||||
ASSERT_FALSE(wf_base64_isvalid(in.c_str(), in.size()));
|
||||
|
||||
in = "Qmx1ZQ?=";
|
||||
ASSERT_FALSE(wf_base64_isvalid(in.c_str(), in.size()));
|
||||
|
||||
in = "Qm?1ZQ==";
|
||||
ASSERT_FALSE(wf_base64_isvalid(in.c_str(), in.size()));
|
||||
}
|
||||
|
||||
TEST(Base64, Decode)
|
||||
{
|
||||
char buffer[42];
|
||||
|
||||
std::string in = "SGVsbG8="; // Hello
|
||||
size_t length = wf_base64_decode(in.c_str(), in.size(), (uint8_t*) buffer, 42);
|
||||
ASSERT_EQ(5, length);
|
||||
buffer[length] = '\0';
|
||||
ASSERT_STREQ("Hello", buffer);
|
||||
|
||||
in = "SGVsbG8K"; // Hello\n
|
||||
length = wf_base64_decode(in.c_str(), in.size(), (uint8_t*) buffer, 42);
|
||||
ASSERT_EQ(6, length);
|
||||
buffer[length] = '\0';
|
||||
ASSERT_STREQ("Hello\n", buffer);
|
||||
|
||||
in = "Qmx1ZQ=="; // Blue
|
||||
length = wf_base64_decode(in.c_str(), in.size(), (uint8_t*) buffer, 42);
|
||||
ASSERT_EQ(4, length);
|
||||
buffer[length] = '\0';
|
||||
ASSERT_STREQ("Blue", buffer);
|
||||
}
|
||||
|
||||
TEST(Base64, FailToDecodeBufferTooSmall)
|
||||
{
|
||||
char buffer[1];
|
||||
|
||||
std::string in = "SGVsbG8="; // Hello
|
||||
size_t length = wf_base64_decode(in.c_str(), in.size(), (uint8_t*) buffer, 1);
|
||||
ASSERT_EQ(0, length);
|
||||
}
|
||||
|
||||
29
test/webfuse_provider/tests/core/test_container_of.cc
Normal file
29
test/webfuse_provider/tests/core/test_container_of.cc
Normal file
@@ -0,0 +1,29 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "webfuse_provider/impl/container_of.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
struct MyStruct
|
||||
{
|
||||
int first;
|
||||
int second;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
TEST(ContainerOf, FirstMember)
|
||||
{
|
||||
MyStruct my_struct = {23, 42};
|
||||
|
||||
int * first = &my_struct.first;
|
||||
ASSERT_EQ(&my_struct, wf_container_of(first, MyStruct, first));
|
||||
}
|
||||
|
||||
TEST(ContainerOf, SecondMember)
|
||||
{
|
||||
MyStruct my_struct = {23, 42};
|
||||
|
||||
int * second = &my_struct.second;
|
||||
ASSERT_EQ(&my_struct, wf_container_of(second, MyStruct, second));
|
||||
}
|
||||
22
test/webfuse_provider/tests/core/test_message.cc
Normal file
22
test/webfuse_provider/tests/core/test_message.cc
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <cstring>
|
||||
#include "webfuse_provider/impl/message.h"
|
||||
|
||||
TEST(wf_message, create)
|
||||
{
|
||||
json_t * value = json_object();
|
||||
|
||||
struct wf_message * message = wf_message_create(value);
|
||||
ASSERT_NE(nullptr, message);
|
||||
ASSERT_EQ(2, message->length);
|
||||
ASSERT_TRUE(0 == strncmp("{}", message->data, 2));
|
||||
|
||||
wf_message_dispose(message);
|
||||
json_decref(value);
|
||||
}
|
||||
|
||||
TEST(wf_message, fail_to_create)
|
||||
{
|
||||
struct wf_message * message = wf_message_create(nullptr);
|
||||
ASSERT_EQ(nullptr, message);
|
||||
}
|
||||
52
test/webfuse_provider/tests/core/test_message_queue.cc
Normal file
52
test/webfuse_provider/tests/core/test_message_queue.cc
Normal file
@@ -0,0 +1,52 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "webfuse_provider/impl/message_queue.h"
|
||||
#include "webfuse_provider/impl/message.h"
|
||||
#include "webfuse_provider/impl/slist.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
struct wf_slist_item * create_message(char const * content)
|
||||
{
|
||||
json_t * value = json_object();
|
||||
json_object_set_new(value, "content", json_string(content));
|
||||
struct wf_message * message = wf_message_create(value);
|
||||
|
||||
json_decref(value);
|
||||
return &message->item;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
TEST(wf_message_queue, cleanup_empty_list)
|
||||
{
|
||||
struct wf_slist queue;
|
||||
wf_slist_init(&queue);
|
||||
|
||||
wf_message_queue_cleanup(&queue);
|
||||
ASSERT_TRUE(wf_slist_empty(&queue));
|
||||
}
|
||||
|
||||
TEST(wf_message_queue, cleanup_one_element)
|
||||
{
|
||||
struct wf_slist queue;
|
||||
wf_slist_init(&queue);
|
||||
|
||||
wf_slist_append(&queue, create_message("Hello"));
|
||||
|
||||
wf_message_queue_cleanup(&queue);
|
||||
ASSERT_TRUE(wf_slist_empty(&queue));
|
||||
}
|
||||
|
||||
TEST(wf_message_queue, cleanup_multiple_element)
|
||||
{
|
||||
struct wf_slist queue;
|
||||
wf_slist_init(&queue);
|
||||
|
||||
wf_slist_append(&queue, create_message("Hello"));
|
||||
wf_slist_append(&queue, create_message("World"));
|
||||
wf_slist_append(&queue, create_message("!"));
|
||||
|
||||
wf_message_queue_cleanup(&queue);
|
||||
ASSERT_TRUE(wf_slist_empty(&queue));
|
||||
}
|
||||
139
test/webfuse_provider/tests/core/test_slist.cc
Normal file
139
test/webfuse_provider/tests/core/test_slist.cc
Normal file
@@ -0,0 +1,139 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "webfuse_provider/impl/slist.h"
|
||||
|
||||
TEST(wf_slist, init)
|
||||
{
|
||||
struct wf_slist list;
|
||||
wf_slist_init(&list);
|
||||
|
||||
ASSERT_EQ(nullptr, list.head.next);
|
||||
ASSERT_EQ(nullptr, list.last->next);
|
||||
ASSERT_EQ(&list.head, list.last);
|
||||
ASSERT_TRUE(wf_slist_empty(&list));
|
||||
ASSERT_EQ(nullptr, wf_slist_first(&list));
|
||||
}
|
||||
|
||||
TEST(wf_slist, append)
|
||||
{
|
||||
struct wf_slist list;
|
||||
struct wf_slist_item item[3];
|
||||
|
||||
wf_slist_init(&list);
|
||||
ASSERT_TRUE(wf_slist_empty(&list));
|
||||
|
||||
wf_slist_append(&list, &item[0]);
|
||||
ASSERT_NE(&list.head, list.last);
|
||||
ASSERT_FALSE(wf_slist_empty(&list));
|
||||
ASSERT_EQ(&item[0], wf_slist_first(&list));
|
||||
ASSERT_EQ(&item[0], list.head.next);
|
||||
ASSERT_EQ(&item[0], list.last);
|
||||
ASSERT_EQ(nullptr, list.last->next);
|
||||
ASSERT_EQ(nullptr, item[0].next);
|
||||
|
||||
wf_slist_append(&list, &item[1]);
|
||||
ASSERT_NE(&list.head, list.last);
|
||||
ASSERT_FALSE(wf_slist_empty(&list));
|
||||
ASSERT_EQ(&item[0], wf_slist_first(&list));
|
||||
ASSERT_EQ(&item[0], list.head.next);
|
||||
ASSERT_EQ(&item[1], list.last);
|
||||
ASSERT_EQ(nullptr, list.last->next);
|
||||
ASSERT_EQ(&item[1], item[0].next);
|
||||
ASSERT_EQ(nullptr, item[1].next);
|
||||
|
||||
wf_slist_append(&list, &item[2]);
|
||||
ASSERT_NE(&list.head, list.last);
|
||||
ASSERT_FALSE(wf_slist_empty(&list));
|
||||
ASSERT_EQ(&item[0], wf_slist_first(&list));
|
||||
ASSERT_EQ(&item[0], list.head.next);
|
||||
ASSERT_EQ(&item[2], list.last);
|
||||
ASSERT_EQ(nullptr, list.last->next);
|
||||
ASSERT_EQ(&item[1], item[0].next);
|
||||
ASSERT_EQ(&item[2], item[1].next);
|
||||
ASSERT_EQ(nullptr, item[2].next);
|
||||
}
|
||||
|
||||
TEST(wf_slist_remove_after, remove_first)
|
||||
{
|
||||
struct wf_slist list;
|
||||
struct wf_slist_item item[3];
|
||||
|
||||
wf_slist_init(&list);
|
||||
wf_slist_append(&list, &item[0]);
|
||||
wf_slist_append(&list, &item[1]);
|
||||
wf_slist_append(&list, &item[2]);
|
||||
|
||||
wf_slist_item * removed;
|
||||
|
||||
removed = wf_slist_remove_first(&list);
|
||||
ASSERT_FALSE(wf_slist_empty(&list));
|
||||
ASSERT_EQ(&item[0], removed);
|
||||
|
||||
removed = wf_slist_remove_first(&list);
|
||||
ASSERT_FALSE(wf_slist_empty(&list));
|
||||
ASSERT_EQ(&item[1], removed);
|
||||
|
||||
removed = wf_slist_remove_first(&list);
|
||||
ASSERT_TRUE(wf_slist_empty(&list));
|
||||
ASSERT_EQ(&item[2], removed);
|
||||
|
||||
ASSERT_EQ(nullptr, list.head.next);
|
||||
ASSERT_EQ(nullptr, list.last->next);
|
||||
ASSERT_EQ(&list.head, list.last);
|
||||
ASSERT_EQ(nullptr, wf_slist_first(&list));
|
||||
}
|
||||
|
||||
TEST(wf_slist_remove_after, remove_last)
|
||||
{
|
||||
struct wf_slist list;
|
||||
struct wf_slist_item item[3];
|
||||
|
||||
wf_slist_init(&list);
|
||||
wf_slist_append(&list, &item[0]);
|
||||
wf_slist_append(&list, &item[1]);
|
||||
wf_slist_append(&list, &item[2]);
|
||||
|
||||
wf_slist_item * removed;
|
||||
|
||||
removed = wf_slist_remove_after(&list, &item[1]);
|
||||
ASSERT_FALSE(wf_slist_empty(&list));
|
||||
ASSERT_EQ(&item[2], removed);
|
||||
|
||||
removed = wf_slist_remove_after(&list, &item[0]);
|
||||
ASSERT_FALSE(wf_slist_empty(&list));
|
||||
ASSERT_EQ(&item[1], removed);
|
||||
|
||||
removed = wf_slist_remove_after(&list, &list.head);
|
||||
ASSERT_TRUE(wf_slist_empty(&list));
|
||||
ASSERT_EQ(&item[0], removed);
|
||||
|
||||
ASSERT_EQ(nullptr, list.head.next);
|
||||
ASSERT_EQ(nullptr, list.last->next);
|
||||
ASSERT_EQ(&list.head, list.last);
|
||||
ASSERT_EQ(nullptr, wf_slist_first(&list));
|
||||
}
|
||||
|
||||
TEST(wf_slist_remove_after, remove_after)
|
||||
{
|
||||
struct wf_slist list;
|
||||
struct wf_slist_item item[3];
|
||||
|
||||
wf_slist_init(&list);
|
||||
wf_slist_append(&list, &item[0]);
|
||||
wf_slist_append(&list, &item[1]);
|
||||
wf_slist_append(&list, &item[2]);
|
||||
|
||||
wf_slist_item * removed;
|
||||
|
||||
removed = wf_slist_remove_after(&list, &item[0]);
|
||||
ASSERT_FALSE(wf_slist_empty(&list));
|
||||
ASSERT_EQ(&item[1], removed);
|
||||
|
||||
ASSERT_NE(&list.head, list.last);
|
||||
ASSERT_FALSE(wf_slist_empty(&list));
|
||||
ASSERT_EQ(&item[0], wf_slist_first(&list));
|
||||
ASSERT_EQ(&item[0], list.head.next);
|
||||
ASSERT_EQ(&item[2], list.last);
|
||||
ASSERT_EQ(nullptr, list.last->next);
|
||||
ASSERT_EQ(&item[2], item[0].next);
|
||||
ASSERT_EQ(nullptr, item[2].next);
|
||||
}
|
||||
30
test/webfuse_provider/tests/core/test_status.cc
Normal file
30
test/webfuse_provider/tests/core/test_status.cc
Normal file
@@ -0,0 +1,30 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "webfuse_provider/impl/status_intern.h"
|
||||
|
||||
TEST(wf_status, tostring)
|
||||
{
|
||||
ASSERT_STREQ("Good", wf_status_tostring(WF_GOOD));
|
||||
ASSERT_STREQ("Bad", wf_status_tostring(WF_BAD));
|
||||
ASSERT_STREQ("Bad (not implemented)", wf_status_tostring(WF_BAD_NOTIMPLEMENTED));
|
||||
ASSERT_STREQ("Bad (busy)", wf_status_tostring(WF_BAD_BUSY));
|
||||
ASSERT_STREQ("Bad (timeout)", wf_status_tostring(WF_BAD_TIMEOUT));
|
||||
ASSERT_STREQ("Bad (format)", wf_status_tostring(WF_BAD_FORMAT));
|
||||
ASSERT_STREQ("Bad (no entry)", wf_status_tostring(WF_BAD_NOENTRY));
|
||||
ASSERT_STREQ("Bad (access denied)", wf_status_tostring(WF_BAD_ACCESS_DENIED));
|
||||
|
||||
ASSERT_STREQ("Bad (unknown)", wf_status_tostring(-1));
|
||||
}
|
||||
|
||||
TEST(wf_status, to_rc)
|
||||
{
|
||||
ASSERT_EQ(0, wf_status_to_rc(WF_GOOD));
|
||||
ASSERT_EQ(-ENOENT, wf_status_to_rc(WF_BAD));
|
||||
ASSERT_EQ(-ENOSYS, wf_status_to_rc(WF_BAD_NOTIMPLEMENTED));
|
||||
ASSERT_EQ(-ENOENT, wf_status_to_rc(WF_BAD_BUSY));
|
||||
ASSERT_EQ(-ETIMEDOUT, wf_status_to_rc(WF_BAD_TIMEOUT));
|
||||
ASSERT_EQ(-ENOENT, wf_status_to_rc(WF_BAD_FORMAT));
|
||||
ASSERT_EQ(-ENOENT, wf_status_to_rc(WF_BAD_NOENTRY));
|
||||
ASSERT_EQ(-EACCES, wf_status_to_rc(WF_BAD_ACCESS_DENIED));
|
||||
|
||||
ASSERT_EQ(-ENOENT, wf_status_to_rc(-1));
|
||||
}
|
||||
92
test/webfuse_provider/tests/core/test_url.cc
Normal file
92
test/webfuse_provider/tests/core/test_url.cc
Normal file
@@ -0,0 +1,92 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "webfuse_provider/impl/url.h"
|
||||
|
||||
TEST(url, ParseWs)
|
||||
{
|
||||
struct wf_url url;
|
||||
bool result = wf_url_init(&url, "ws://localhost/");
|
||||
ASSERT_TRUE(result);
|
||||
ASSERT_EQ(80, url.port);
|
||||
ASSERT_FALSE(url.use_tls);
|
||||
ASSERT_STREQ("localhost", url.host);
|
||||
ASSERT_STREQ("/", url.path);
|
||||
|
||||
wf_url_cleanup(&url);
|
||||
}
|
||||
|
||||
TEST(url, ParswWss)
|
||||
{
|
||||
struct wf_url url;
|
||||
bool result = wf_url_init(&url, "wss://localhost/");
|
||||
ASSERT_TRUE(result);
|
||||
ASSERT_EQ(443, url.port);
|
||||
ASSERT_TRUE(url.use_tls);
|
||||
ASSERT_STREQ("localhost", url.host);
|
||||
ASSERT_STREQ("/", url.path);
|
||||
|
||||
wf_url_cleanup(&url);
|
||||
}
|
||||
|
||||
TEST(url, ParseIPAdress)
|
||||
{
|
||||
struct wf_url url;
|
||||
bool result = wf_url_init(&url, "ws://127.0.0.1/");
|
||||
ASSERT_TRUE(result);
|
||||
ASSERT_EQ(80, url.port);
|
||||
ASSERT_STREQ("127.0.0.1", url.host);
|
||||
ASSERT_STREQ("/", url.path);
|
||||
|
||||
wf_url_cleanup(&url);
|
||||
}
|
||||
|
||||
TEST(url, ParsePort)
|
||||
{
|
||||
struct wf_url url;
|
||||
bool result = wf_url_init(&url, "ws://localhost:54321/");
|
||||
ASSERT_TRUE(result);
|
||||
ASSERT_EQ(54321, url.port);
|
||||
|
||||
wf_url_cleanup(&url);
|
||||
}
|
||||
|
||||
TEST(url, ParseNonEmptyPath)
|
||||
{
|
||||
struct wf_url url;
|
||||
bool result = wf_url_init(&url, "ws://localhost/some_path?query");
|
||||
ASSERT_TRUE(result);
|
||||
ASSERT_STREQ("/some_path?query", url.path);
|
||||
|
||||
wf_url_cleanup(&url);
|
||||
}
|
||||
|
||||
TEST(url, FailToParseUnknownProtocol)
|
||||
{
|
||||
struct wf_url url;
|
||||
bool result = wf_url_init(&url, "unknown://localhost/");
|
||||
ASSERT_FALSE(result);
|
||||
ASSERT_EQ(0, url.port);
|
||||
ASSERT_EQ(nullptr, url.path);
|
||||
ASSERT_EQ(nullptr, url.host);
|
||||
}
|
||||
|
||||
TEST(url, FailToParseMissingProtocol)
|
||||
{
|
||||
struct wf_url url;
|
||||
bool result = wf_url_init(&url, "unknown");
|
||||
ASSERT_FALSE(result);
|
||||
ASSERT_EQ(0, url.port);
|
||||
ASSERT_EQ(nullptr, url.path);
|
||||
ASSERT_EQ(nullptr, url.host);
|
||||
}
|
||||
|
||||
TEST(url, FailToParseMissingPath)
|
||||
{
|
||||
struct wf_url url;
|
||||
bool result = wf_url_init(&url, "ws://localhost");
|
||||
ASSERT_FALSE(result);
|
||||
ASSERT_EQ(0, url.port);
|
||||
ASSERT_EQ(nullptr, url.path);
|
||||
ASSERT_EQ(nullptr, url.host);
|
||||
}
|
||||
|
||||
47
test/webfuse_provider/tests/core/test_util.cc
Normal file
47
test/webfuse_provider/tests/core/test_util.cc
Normal file
@@ -0,0 +1,47 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "webfuse_provider/impl/json_util.h"
|
||||
|
||||
TEST(jsonrpc_util, get_int)
|
||||
{
|
||||
json_t * object = json_object();
|
||||
json_object_set_new(object, "key", json_integer(23));
|
||||
int value = wf_impl_json_get_int(object, "key", 42);
|
||||
ASSERT_EQ(23, value);
|
||||
|
||||
json_decref(object);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_util, failed_to_get_null_object)
|
||||
{
|
||||
int value = wf_impl_json_get_int(nullptr, "key", 42);
|
||||
|
||||
ASSERT_EQ(42, value);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_util, failed_to_get_not_object)
|
||||
{
|
||||
json_t * object = json_array();
|
||||
int value = wf_impl_json_get_int(nullptr, "key", 42);
|
||||
ASSERT_EQ(42, value);
|
||||
|
||||
json_decref(object);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_util, failed_to_get_invalid_key)
|
||||
{
|
||||
json_t * object = json_object();
|
||||
int value = wf_impl_json_get_int(object, "key", 42);
|
||||
ASSERT_EQ(42, value);
|
||||
|
||||
json_decref(object);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_util, failed_to_get_invalid_value_type)
|
||||
{
|
||||
json_t * object = json_object();
|
||||
json_object_set_new(object, "key", json_string("42"));
|
||||
int value = wf_impl_json_get_int(object, "key", 42);
|
||||
ASSERT_EQ(42, value);
|
||||
|
||||
json_decref(object);
|
||||
}
|
||||
38
test/webfuse_provider/tests/core/timer/test_timepoint.cc
Normal file
38
test/webfuse_provider/tests/core/timer/test_timepoint.cc
Normal file
@@ -0,0 +1,38 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "webfuse_provider/impl/timer/timepoint.h"
|
||||
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
TEST(wf_timer_timepoint, now)
|
||||
{
|
||||
wf_timer_timepoint start = wf_timer_timepoint_now();
|
||||
std::this_thread::sleep_for(42ms);
|
||||
wf_timer_timepoint end = wf_timer_timepoint_now();
|
||||
|
||||
ASSERT_LT(start, end);
|
||||
ASSERT_LT(end, start + 500);
|
||||
}
|
||||
|
||||
TEST(wf_timer_timepoint, in_msec)
|
||||
{
|
||||
wf_timer_timepoint now = wf_timer_timepoint_now();
|
||||
wf_timer_timepoint later = wf_timer_timepoint_in_msec(42);
|
||||
|
||||
ASSERT_LT(now, later);
|
||||
ASSERT_LT(later, now + 500);
|
||||
}
|
||||
|
||||
TEST(wf_timer_timepoint, elapsed)
|
||||
{
|
||||
wf_timer_timepoint now;
|
||||
|
||||
now = wf_timer_timepoint_now();
|
||||
ASSERT_TRUE(wf_timer_timepoint_is_elapsed(now - 1));
|
||||
|
||||
now = wf_timer_timepoint_now();
|
||||
ASSERT_FALSE(wf_timer_timepoint_is_elapsed(now + 500));
|
||||
}
|
||||
148
test/webfuse_provider/tests/core/timer/test_timer.cc
Normal file
148
test/webfuse_provider/tests/core/timer/test_timer.cc
Normal file
@@ -0,0 +1,148 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <cstddef>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
#include "webfuse_provider/impl/timer/timer.h"
|
||||
#include "webfuse_provider/impl/timer/manager.h"
|
||||
|
||||
using std::size_t;
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
extern "C"
|
||||
{
|
||||
void on_timeout(struct wf_timer * timer, void * user_data)
|
||||
{
|
||||
(void) timer;
|
||||
|
||||
bool * triggered = reinterpret_cast<bool*>(user_data);
|
||||
*triggered = true;
|
||||
}
|
||||
}
|
||||
|
||||
TEST(wf_timer, init)
|
||||
{
|
||||
bool triggered = false;
|
||||
struct wf_timer_manager * manager = wf_timer_manager_create();
|
||||
struct wf_timer * timer = wf_timer_create(manager, &on_timeout, reinterpret_cast<void*>(&triggered));
|
||||
|
||||
wf_timer_dispose(timer);
|
||||
wf_timer_manager_dispose(manager);
|
||||
}
|
||||
|
||||
TEST(wf_timer, trigger)
|
||||
{
|
||||
bool triggered = false;
|
||||
struct wf_timer_manager * manager = wf_timer_manager_create();
|
||||
struct wf_timer * timer = wf_timer_create(manager, &on_timeout, reinterpret_cast<void*>(&triggered));
|
||||
|
||||
wf_timer_start(timer, -1);
|
||||
wf_timer_manager_check(manager);
|
||||
|
||||
ASSERT_TRUE(triggered);
|
||||
|
||||
wf_timer_dispose(timer);
|
||||
wf_timer_manager_dispose(manager);
|
||||
}
|
||||
|
||||
TEST(wf_timer, trigger_on_dispose)
|
||||
{
|
||||
bool triggered = false;
|
||||
struct wf_timer_manager * manager = wf_timer_manager_create();
|
||||
struct wf_timer * timer = wf_timer_create(manager, &on_timeout, reinterpret_cast<void*>(&triggered));
|
||||
|
||||
wf_timer_start(timer, (5 * 60 * 1000));
|
||||
|
||||
wf_timer_manager_dispose(manager);
|
||||
ASSERT_TRUE(triggered);
|
||||
|
||||
wf_timer_dispose(timer);
|
||||
}
|
||||
|
||||
TEST(wf_timer, cancel)
|
||||
{
|
||||
bool triggered = false;
|
||||
struct wf_timer_manager * manager = wf_timer_manager_create();
|
||||
struct wf_timer * timer = wf_timer_create(manager, &on_timeout, reinterpret_cast<void*>(&triggered));
|
||||
|
||||
wf_timer_start(timer, 250);
|
||||
std::this_thread::sleep_for(500ms);
|
||||
wf_timer_cancel(timer);
|
||||
wf_timer_manager_check(manager);
|
||||
|
||||
ASSERT_FALSE(triggered);
|
||||
|
||||
wf_timer_dispose(timer);
|
||||
wf_timer_manager_dispose(manager);
|
||||
}
|
||||
|
||||
TEST(wf_timer, cancel_multiple_timers)
|
||||
{
|
||||
static size_t const count = 5;
|
||||
struct wf_timer_manager * manager = wf_timer_manager_create();
|
||||
struct wf_timer * timer[count];
|
||||
|
||||
bool triggered = false;
|
||||
for(size_t i = 0; i < count; i++)
|
||||
{
|
||||
timer[i] = wf_timer_create(manager, &on_timeout, reinterpret_cast<void*>(&triggered));
|
||||
wf_timer_start(timer[i], 0);
|
||||
}
|
||||
|
||||
std::this_thread::sleep_for(10ms);
|
||||
for(size_t i = 0; i < count; i++)
|
||||
{
|
||||
wf_timer_cancel(timer[i]);
|
||||
}
|
||||
|
||||
wf_timer_manager_check(manager);
|
||||
ASSERT_FALSE(triggered);
|
||||
|
||||
for(size_t i = 0; i < count; i++)
|
||||
{
|
||||
wf_timer_dispose(timer[i]);
|
||||
}
|
||||
wf_timer_manager_dispose(manager);
|
||||
}
|
||||
|
||||
TEST(wf_timer, multiple_timers)
|
||||
{
|
||||
static size_t const count = 5;
|
||||
struct wf_timer_manager * manager = wf_timer_manager_create();
|
||||
struct wf_timer * timer[count];
|
||||
bool triggered[count];
|
||||
|
||||
for(size_t i = 0; i < count; i++)
|
||||
{
|
||||
timer[i] = wf_timer_create(manager, &on_timeout, reinterpret_cast<void*>(&triggered[i]));
|
||||
triggered[i] = false;
|
||||
wf_timer_start(timer[i], (300 - (50 * i)));
|
||||
}
|
||||
|
||||
for(size_t i = 0; i < count; i++)
|
||||
{
|
||||
std::this_thread::sleep_for(100ms);
|
||||
wf_timer_manager_check(manager);
|
||||
}
|
||||
|
||||
for(size_t i = 0; i < count; i++)
|
||||
{
|
||||
ASSERT_TRUE(triggered[i]);
|
||||
wf_timer_dispose(timer[i]);
|
||||
}
|
||||
|
||||
wf_timer_manager_dispose(manager);
|
||||
}
|
||||
|
||||
TEST(wf_timer, dont_trigger_null_callback)
|
||||
{
|
||||
struct wf_timer_manager * manager = wf_timer_manager_create();
|
||||
struct wf_timer * timer = wf_timer_create(manager, nullptr, nullptr);
|
||||
|
||||
wf_timer_start(timer, -1);
|
||||
wf_timer_manager_check(manager);
|
||||
|
||||
wf_timer_dispose(timer);
|
||||
wf_timer_manager_dispose(manager);
|
||||
}
|
||||
Reference in New Issue
Block a user