mirror of
https://github.com/falk-werner/webfuse-provider
synced 2026-03-02 04:09:18 +00:00
simplified test structure
This commit is contained in:
31
test/webfuse_provider/jsonrpc/mock_timer.cc
Normal file
31
test/webfuse_provider/jsonrpc/mock_timer.cc
Normal file
@@ -0,0 +1,31 @@
|
||||
#include "webfuse_provider/jsonrpc/mock_timer.hpp"
|
||||
#include "webfuse_provider/utils/wrap.hpp"
|
||||
|
||||
extern "C"
|
||||
{
|
||||
static wfp_jsonrpc_test::ITimer * wfp_jsonrpc_MockTimer = nullptr;
|
||||
|
||||
WFP_WRAP_FUNC0(wfp_jsonrpc_MockTimer, wfp_timer_manager *, wfp_timer_manager_create);
|
||||
WFP_WRAP_FUNC1(wfp_jsonrpc_MockTimer, void, wfp_timer_manager_dispose, wfp_timer_manager *);
|
||||
WFP_WRAP_FUNC1(wfp_jsonrpc_MockTimer, void, wfp_timer_manager_check, wfp_timer_manager *);
|
||||
|
||||
WFP_WRAP_FUNC3(wfp_jsonrpc_MockTimer, wfp_timer *, wfp_timer_create, wfp_timer_manager *, wfp_timer_on_timer_fn *, void *);
|
||||
WFP_WRAP_FUNC1(wfp_jsonrpc_MockTimer, void, wfp_timer_dispose, wfp_timer *);
|
||||
WFP_WRAP_FUNC2(wfp_jsonrpc_MockTimer, void, wfp_timer_start, wfp_timer *, int);
|
||||
WFP_WRAP_FUNC1(wfp_jsonrpc_MockTimer, void, wfp_timer_cancel, wfp_timer *);
|
||||
|
||||
}
|
||||
|
||||
namespace wfp_jsonrpc_test
|
||||
{
|
||||
MockTimer::MockTimer()
|
||||
{
|
||||
wfp_jsonrpc_MockTimer = this;
|
||||
}
|
||||
|
||||
MockTimer::~MockTimer()
|
||||
{
|
||||
wfp_jsonrpc_MockTimer = nullptr;
|
||||
}
|
||||
|
||||
}
|
||||
48
test/webfuse_provider/jsonrpc/mock_timer.hpp
Normal file
48
test/webfuse_provider/jsonrpc/mock_timer.hpp
Normal file
@@ -0,0 +1,48 @@
|
||||
#ifndef WFP_JSONRPC_MOCK_TIMERMANAGER_HPP
|
||||
#define WFP_JSONRPC_MOCK_TIMERMANAGER_HPP
|
||||
|
||||
#include "webfuse_provider/impl/timer/timer.h"
|
||||
#include "webfuse_provider/impl/timer/manager.h"
|
||||
#include <gmock/gmock.h>
|
||||
|
||||
namespace wfp_jsonrpc_test
|
||||
{
|
||||
|
||||
class ITimer
|
||||
{
|
||||
public:
|
||||
virtual ~ITimer() = default;
|
||||
virtual wfp_timer_manager * wfp_timer_manager_create() = 0;
|
||||
virtual void wfp_timer_manager_dispose(wfp_timer_manager * manager) = 0;
|
||||
virtual void wfp_timer_manager_check(wfp_timer_manager * manager) = 0;
|
||||
virtual wfp_timer * wfp_timer_create(
|
||||
wfp_timer_manager * manager,
|
||||
wfp_timer_on_timer_fn * on_timer,
|
||||
void * user_data) = 0;
|
||||
virtual void wfp_timer_dispose(wfp_timer * timer) = 0;
|
||||
virtual void wfp_timer_start(wfp_timer * timer, int timeout_ms) = 0;
|
||||
virtual void wfp_timer_cancel(wfp_timer * timer) = 0;
|
||||
};
|
||||
|
||||
class MockTimer: public ITimer
|
||||
{
|
||||
public:
|
||||
MockTimer();
|
||||
~MockTimer() override;
|
||||
MOCK_METHOD0(wfp_timer_manager_create, wfp_timer_manager * ());
|
||||
MOCK_METHOD1(wfp_timer_manager_dispose, void(wfp_timer_manager * manager));
|
||||
MOCK_METHOD1(wfp_timer_manager_check, void (wfp_timer_manager * manager));
|
||||
MOCK_METHOD3(wfp_timer_create, wfp_timer *(
|
||||
wfp_timer_manager * manager,
|
||||
wfp_timer_on_timer_fn * on_timer,
|
||||
void * user_data));
|
||||
MOCK_METHOD1(wfp_timer_dispose, void (wfp_timer * timer));
|
||||
MOCK_METHOD2(wfp_timer_start, void (wfp_timer * timer, int timeout_ms));
|
||||
MOCK_METHOD1(wfp_timer_cancel, void (wfp_timer * timer));
|
||||
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
41
test/webfuse_provider/jsonrpc/mock_timer_callback.cc
Normal file
41
test/webfuse_provider/jsonrpc/mock_timer_callback.cc
Normal file
@@ -0,0 +1,41 @@
|
||||
#include "webfuse_provider/jsonrpc/mock_timer_callback.hpp"
|
||||
|
||||
extern "C"
|
||||
{
|
||||
using wfp_jsonrpc_test::MockTimerCallback;
|
||||
|
||||
static void wfp_jsonrpc_test_MockTimerCallback_on_timer(
|
||||
wfp_timer * timer,
|
||||
void * user_data)
|
||||
{
|
||||
auto * self = reinterpret_cast<MockTimerCallback*>(user_data);
|
||||
self->on_timer(timer, user_data);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace wfp_jsonrpc_test
|
||||
{
|
||||
|
||||
MockTimerCallback::MockTimerCallback()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
MockTimerCallback::~MockTimerCallback()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
wfp_timer_on_timer_fn * MockTimerCallback::on_timer_fn()
|
||||
{
|
||||
return &wfp_jsonrpc_test_MockTimerCallback_on_timer;
|
||||
}
|
||||
|
||||
void * MockTimerCallback::user_data()
|
||||
{
|
||||
return reinterpret_cast<void*>(this);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
23
test/webfuse_provider/jsonrpc/mock_timer_callback.hpp
Normal file
23
test/webfuse_provider/jsonrpc/mock_timer_callback.hpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#ifndef WFP_JSONRPC_MOCK_TIMERCALLBACK_HPP
|
||||
#define WFP_JSONRPC_MOCK_TIMERCALLBACK_HPP
|
||||
|
||||
#include "webfuse_provider/impl/timer/on_timer_fn.h"
|
||||
#include <gmock/gmock.h>
|
||||
|
||||
namespace wfp_jsonrpc_test
|
||||
{
|
||||
class MockTimerCallback
|
||||
{
|
||||
public:
|
||||
MockTimerCallback();
|
||||
virtual ~MockTimerCallback();
|
||||
wfp_timer_on_timer_fn * on_timer_fn();
|
||||
void * user_data();
|
||||
|
||||
MOCK_METHOD2(on_timer, void (wfp_timer * timer, void * user_data));
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
112
test/webfuse_provider/jsonrpc/test_is_request.cc
Normal file
112
test/webfuse_provider/jsonrpc/test_is_request.cc
Normal file
@@ -0,0 +1,112 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "webfuse_provider/impl/jsonrpc/request.h"
|
||||
|
||||
TEST(wfp_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(wfp_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(wfp_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(wfp_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(wfp_jsonrpc_is_request, null_request)
|
||||
{
|
||||
ASSERT_FALSE(wfp_jsonrpc_is_request(nullptr));
|
||||
}
|
||||
|
||||
TEST(wfp_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(wfp_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(wfp_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(wfp_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(wfp_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(wfp_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(wfp_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(wfp_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(wfp_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(wfp_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(wfp_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(wfp_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(wfp_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(wfp_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
94
test/webfuse_provider/jsonrpc/test_is_response.cc
Normal file
94
test/webfuse_provider/jsonrpc/test_is_response.cc
Normal file
@@ -0,0 +1,94 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "webfuse_provider/impl/jsonrpc/response.h"
|
||||
|
||||
TEST(wfp_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(wfp_jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wfp_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(wfp_jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wfp_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(wfp_jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wfp_jsonrpc_is_response, invalid_null)
|
||||
{
|
||||
ASSERT_FALSE(wfp_jsonrpc_is_response(nullptr));
|
||||
}
|
||||
|
||||
TEST(wfp_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(wfp_jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wfp_jsonrpc_is_response, invalid_missing_id)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "result", json_object());
|
||||
|
||||
ASSERT_FALSE(wfp_jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wfp_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(wfp_jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
|
||||
TEST(wfp_jsonrpc_is_response, invalid_missing_result_and_error)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "id", json_integer(42));
|
||||
|
||||
ASSERT_FALSE(wfp_jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wfp_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(wfp_jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
430
test/webfuse_provider/jsonrpc/test_proxy.cc
Normal file
430
test/webfuse_provider/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/jsonrpc/mock_timer.hpp"
|
||||
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
using wfp_jsonrpc_test::MockTimer;
|
||||
using testing::Return;
|
||||
using testing::_;
|
||||
using testing::DoAll;
|
||||
using testing::SaveArg;
|
||||
|
||||
#define WFP_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) : WFP_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(wfp_jsonrpc_proxy, init)
|
||||
{
|
||||
struct wfp_timer_manager * timer_manager = wfp_timer_manager_create();
|
||||
|
||||
SendContext context;
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
struct wfp_jsonrpc_proxy * proxy = wfp_jsonrpc_proxy_create(timer_manager, WFP_DEFAULT_TIMEOUT, &jsonrpc_send, user_data);
|
||||
|
||||
wfp_jsonrpc_proxy_dispose(proxy);
|
||||
wfp_timer_manager_dispose(timer_manager);
|
||||
|
||||
ASSERT_FALSE(context.is_called);
|
||||
}
|
||||
|
||||
TEST(wfp_jsonrpc_proxy, invoke)
|
||||
{
|
||||
struct wfp_timer_manager * timer_manager = wfp_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wfp_jsonrpc_proxy * proxy = wfp_jsonrpc_proxy_create(timer_manager, WFP_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
wfp_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);
|
||||
|
||||
wfp_jsonrpc_proxy_dispose(proxy);
|
||||
wfp_timer_manager_dispose(timer_manager);
|
||||
|
||||
ASSERT_TRUE(finished_context.is_called);
|
||||
ASSERT_FALSE(nullptr == finished_context.error);
|
||||
}
|
||||
|
||||
TEST(wfp_jsonrpc_proxy, invoke_calls_finish_if_send_fails)
|
||||
{
|
||||
struct wfp_timer_manager * timer_manager = wfp_timer_manager_create();
|
||||
|
||||
SendContext send_context(false);
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wfp_jsonrpc_proxy * proxy = wfp_jsonrpc_proxy_create(timer_manager, WFP_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
wfp_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);
|
||||
|
||||
wfp_jsonrpc_proxy_dispose(proxy);
|
||||
wfp_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
TEST(wfp_jsonrpc_proxy, invoke_fails_if_another_request_is_pending)
|
||||
{
|
||||
struct wfp_timer_manager * timer_manager = wfp_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wfp_jsonrpc_proxy * proxy = wfp_jsonrpc_proxy_create(timer_manager, WFP_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
wfp_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
|
||||
|
||||
FinishedContext finished_context2;
|
||||
void * finished_data2 = reinterpret_cast<void*>(&finished_context2);
|
||||
wfp_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(WFP_BAD_BUSY, jsonrpc_get_status(finished_context2.error));
|
||||
|
||||
wfp_jsonrpc_proxy_dispose(proxy);
|
||||
wfp_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
TEST(wfp_jsonrpc_proxy, invoke_fails_if_request_is_invalid)
|
||||
{
|
||||
struct wfp_timer_manager * timer_manager = wfp_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wfp_jsonrpc_proxy * proxy = wfp_jsonrpc_proxy_create(timer_manager, WFP_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
wfp_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "?", "error");
|
||||
|
||||
ASSERT_FALSE(send_context.is_called);
|
||||
|
||||
ASSERT_TRUE(finished_context.is_called);
|
||||
ASSERT_EQ(WFP_BAD, jsonrpc_get_status(finished_context.error));
|
||||
|
||||
wfp_jsonrpc_proxy_dispose(proxy);
|
||||
wfp_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
TEST(wfp_jsonrpc_proxy, on_result)
|
||||
{
|
||||
struct wfp_timer_manager * timer_manager = wfp_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wfp_jsonrpc_proxy * proxy = wfp_jsonrpc_proxy_create(timer_manager, WFP_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
wfp_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);
|
||||
|
||||
wfp_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));
|
||||
|
||||
wfp_jsonrpc_proxy_dispose(proxy);
|
||||
wfp_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
TEST(wfp_jsonrpc_proxy, on_result_reject_response_with_unknown_id)
|
||||
{
|
||||
struct wfp_timer_manager * timer_manager = wfp_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wfp_jsonrpc_proxy * proxy = wfp_jsonrpc_proxy_create(timer_manager, WFP_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
wfp_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)));
|
||||
|
||||
wfp_jsonrpc_proxy_onresult(proxy, response);
|
||||
json_decref(response);
|
||||
|
||||
ASSERT_FALSE(finished_context.is_called);
|
||||
|
||||
wfp_jsonrpc_proxy_dispose(proxy);
|
||||
wfp_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
TEST(wfp_jsonrpc_proxy, timeout)
|
||||
{
|
||||
struct wfp_timer_manager * timer_manager = wfp_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wfp_jsonrpc_proxy * proxy = wfp_jsonrpc_proxy_create(timer_manager, 0, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
wfp_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);
|
||||
wfp_timer_manager_check(timer_manager);
|
||||
|
||||
ASSERT_TRUE(finished_context.is_called);
|
||||
ASSERT_EQ(WFP_BAD_TIMEOUT, jsonrpc_get_status(finished_context.error));
|
||||
|
||||
wfp_jsonrpc_proxy_dispose(proxy);
|
||||
wfp_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
TEST(wfp_jsonrpc_proxy, cleanup_pending_request)
|
||||
{
|
||||
struct wfp_timer_manager * timer_manager = wfp_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wfp_jsonrpc_proxy * proxy = wfp_jsonrpc_proxy_create(timer_manager, 10, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
wfp_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);
|
||||
|
||||
wfp_jsonrpc_proxy_dispose(proxy);
|
||||
|
||||
ASSERT_TRUE(finished_context.is_called);
|
||||
|
||||
wfp_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
|
||||
|
||||
TEST(wfp_jsonrpc_proxy, notify)
|
||||
{
|
||||
struct wfp_timer_manager * timer_manager = wfp_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wfp_jsonrpc_proxy * proxy = wfp_jsonrpc_proxy_create(timer_manager, WFP_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
wfp_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);
|
||||
|
||||
wfp_jsonrpc_proxy_dispose(proxy);
|
||||
wfp_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
TEST(wfp_jsonrpc_proxy, notify_dont_send_invalid_request)
|
||||
{
|
||||
struct wfp_timer_manager * timer_manager = wfp_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wfp_jsonrpc_proxy * proxy = wfp_jsonrpc_proxy_create(timer_manager, WFP_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
wfp_jsonrpc_proxy_notify(proxy, "foo", "?");
|
||||
|
||||
ASSERT_FALSE(send_context.is_called);
|
||||
|
||||
wfp_jsonrpc_proxy_dispose(proxy);
|
||||
wfp_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
TEST(wfp_jsonrpc_proxy, swallow_timeout_if_no_request_pending)
|
||||
{
|
||||
MockTimer timer_api;
|
||||
|
||||
wfp_timer_on_timer_fn * on_timer = nullptr;
|
||||
void * timer_context = nullptr;
|
||||
EXPECT_CALL(timer_api, wfp_timer_create(_, _, _))
|
||||
.Times(1)
|
||||
.WillOnce(DoAll(SaveArg<1>(&on_timer), SaveArg<2>(&timer_context), Return(nullptr)));
|
||||
EXPECT_CALL(timer_api, wfp_timer_dispose(_)).Times(1);
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wfp_jsonrpc_proxy * proxy = wfp_jsonrpc_proxy_create(nullptr, 1, &jsonrpc_send, send_data);
|
||||
|
||||
on_timer(nullptr, timer_context);
|
||||
ASSERT_FALSE(send_context.is_called);
|
||||
|
||||
|
||||
wfp_jsonrpc_proxy_dispose(proxy);
|
||||
}
|
||||
|
||||
TEST(wfp_jsonrpc_proxy, on_result_swallow_if_no_request_pending)
|
||||
{
|
||||
struct wfp_timer_manager * timer_manager = wfp_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct wfp_jsonrpc_proxy * proxy = wfp_jsonrpc_proxy_create(timer_manager, WFP_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));
|
||||
|
||||
wfp_jsonrpc_proxy_onresult(proxy, response);
|
||||
json_decref(response);
|
||||
|
||||
wfp_jsonrpc_proxy_dispose(proxy);
|
||||
wfp_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
138
test/webfuse_provider/jsonrpc/test_request.cc
Normal file
138
test/webfuse_provider/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(wfp_jsonrpc_request, create_dispose)
|
||||
{
|
||||
Context context{nullptr};
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
|
||||
struct wfp_jsonrpc_request * request =
|
||||
wfp_jsonrpc_request_create(42, &jsonrpc_send, user_data);
|
||||
|
||||
ASSERT_NE(nullptr, request);
|
||||
ASSERT_EQ(user_data, wfp_jsonrpc_request_get_userdata(request));
|
||||
|
||||
wfp_jsonrpc_request_dispose(request);
|
||||
}
|
||||
|
||||
TEST(wfp_jsonrpc_request, respond)
|
||||
{
|
||||
Context context{nullptr};
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
|
||||
struct wfp_jsonrpc_request * request =
|
||||
wfp_jsonrpc_request_create(42, &jsonrpc_send, user_data);
|
||||
|
||||
wfp_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(wfp_jsonrpc_request, respond_error)
|
||||
{
|
||||
Context context{nullptr};
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
|
||||
struct wfp_jsonrpc_request * request =
|
||||
wfp_jsonrpc_request_create(42, &jsonrpc_send, user_data);
|
||||
|
||||
wfp_jsonrpc_respond_error(request, WFP_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(WFP_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(wfp_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(wfp_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(wfp_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(wfp_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(wfp_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(wfp_jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
147
test/webfuse_provider/jsonrpc/test_response.cc
Normal file
147
test/webfuse_provider/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(wfp_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 wfp_jsonrpc_response response;
|
||||
wfp_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);
|
||||
|
||||
wfp_jsonrpc_response_cleanup(&response);
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wfp_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 wfp_jsonrpc_response response;
|
||||
wfp_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);
|
||||
|
||||
wfp_jsonrpc_response_cleanup(&response);
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wfp_json_response, init_fail_missing_result_and_error)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "id", json_integer(12));
|
||||
|
||||
struct wfp_jsonrpc_response response;
|
||||
wfp_jsonrpc_response_init(&response, message);
|
||||
|
||||
ASSERT_EQ(WFP_BAD_FORMAT, json_integer_value(json_object_get(response.error, "code")));
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
ASSERT_EQ(12, response.id);
|
||||
|
||||
wfp_jsonrpc_response_cleanup(&response);
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wfp_json_response, init_fail_missing_id)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "result", json_integer(47));
|
||||
|
||||
struct wfp_jsonrpc_response response;
|
||||
wfp_jsonrpc_response_init(&response, message);
|
||||
|
||||
ASSERT_EQ(WFP_BAD_FORMAT, json_integer_value(json_object_get(response.error, "code")));
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
ASSERT_EQ(-1, response.id);
|
||||
|
||||
wfp_jsonrpc_response_cleanup(&response);
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wfp_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 wfp_jsonrpc_response response;
|
||||
wfp_jsonrpc_response_init(&response, message);
|
||||
|
||||
ASSERT_EQ(WFP_BAD_FORMAT, json_integer_value(json_object_get(response.error, "code")));
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
ASSERT_EQ(-1, response.id);
|
||||
|
||||
wfp_jsonrpc_response_cleanup(&response);
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wfp_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 wfp_jsonrpc_response response;
|
||||
wfp_jsonrpc_response_init(&response, message);
|
||||
|
||||
ASSERT_EQ(WFP_BAD_FORMAT, json_integer_value(json_object_get(response.error, "code")));
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
ASSERT_EQ(23, response.id);
|
||||
|
||||
wfp_jsonrpc_response_cleanup(&response);
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wfp_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 wfp_jsonrpc_response response;
|
||||
wfp_jsonrpc_response_init(&response, message);
|
||||
|
||||
ASSERT_EQ(WFP_BAD_FORMAT, json_integer_value(json_object_get(response.error, "code")));
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
ASSERT_EQ(23, response.id);
|
||||
|
||||
wfp_jsonrpc_response_cleanup(&response);
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(wfp_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 wfp_jsonrpc_response response;
|
||||
wfp_jsonrpc_response_init(&response, message);
|
||||
|
||||
ASSERT_EQ(WFP_BAD_FORMAT, json_integer_value(json_object_get(response.error, "code")));
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
ASSERT_EQ(23, response.id);
|
||||
|
||||
wfp_jsonrpc_response_cleanup(&response);
|
||||
json_decref(message);
|
||||
}
|
||||
58
test/webfuse_provider/jsonrpc/test_response_parser.cc
Normal file
58
test/webfuse_provider/jsonrpc/test_response_parser.cc
Normal 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 wfp_jsonrpc_response * response)
|
||||
{
|
||||
json_t * message = json_loadb(buffer.c_str(), buffer.size(), 0, nullptr);
|
||||
if (nullptr != message)
|
||||
{
|
||||
wfp_jsonrpc_response_init(response, message);
|
||||
json_decref(message);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(response_parser, test)
|
||||
{
|
||||
struct wfp_jsonrpc_response response;
|
||||
|
||||
// no object
|
||||
response_parse_str("[]", &response);
|
||||
ASSERT_NE(nullptr, response.error);
|
||||
ASSERT_EQ(-1, response.id);
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
wfp_jsonrpc_response_cleanup(&response);
|
||||
|
||||
// empty
|
||||
response_parse_str("{}", &response);
|
||||
ASSERT_NE(nullptr, response.error);
|
||||
ASSERT_EQ(-1, response.id);
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
wfp_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);
|
||||
wfp_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);
|
||||
wfp_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);
|
||||
wfp_jsonrpc_response_cleanup(&response);
|
||||
}
|
||||
Reference in New Issue
Block a user