mirror of
https://github.com/falk-werner/webfuse-provider
synced 2026-03-02 04:09:18 +00:00
reorganized library code
This commit is contained in:
@@ -1,112 +0,0 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "jsonrpc/request.h"
|
||||
|
||||
TEST(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(jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(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(jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_is_request, null_request)
|
||||
{
|
||||
ASSERT_FALSE(jsonrpc_is_request(nullptr));
|
||||
}
|
||||
|
||||
TEST(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(jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(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(jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(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(jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(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(jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(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(jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(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(jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
|
||||
TEST(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(jsonrpc_is_request(request));
|
||||
|
||||
json_decref(request);
|
||||
}
|
||||
@@ -1,94 +0,0 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "jsonrpc/response.h"
|
||||
|
||||
TEST(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(jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(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(jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(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(jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_is_response, invalid_null)
|
||||
{
|
||||
ASSERT_FALSE(jsonrpc_is_response(nullptr));
|
||||
}
|
||||
|
||||
TEST(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(jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_is_response, invalid_missing_id)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "result", json_object());
|
||||
|
||||
ASSERT_FALSE(jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(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(jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
|
||||
TEST(jsonrpc_is_response, invalid_missing_result_and_error)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "id", json_integer(42));
|
||||
|
||||
ASSERT_FALSE(jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(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(jsonrpc_is_response(message));
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
@@ -1,388 +0,0 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "jsonrpc/proxy.h"
|
||||
#include "webfuse/adapter/impl/time/timeout_manager.h"
|
||||
#include "webfuse/utils/msleep.hpp"
|
||||
#include "webfuse/core/json_util.h"
|
||||
|
||||
using webfuse_test::msleep;
|
||||
|
||||
#define WF_DEFAULT_TIMEOUT (10 * 1000)
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
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(jsonrpc_proxy, init)
|
||||
{
|
||||
struct wf_impl_timeout_manager timeout_manager;
|
||||
wf_impl_timeout_manager_init(&timeout_manager);
|
||||
|
||||
SendContext context;
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, user_data);
|
||||
|
||||
jsonrpc_proxy_dispose(proxy);
|
||||
wf_impl_timeout_manager_cleanup(&timeout_manager);
|
||||
|
||||
ASSERT_FALSE(context.is_called);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_proxy, invoke)
|
||||
{
|
||||
struct wf_impl_timeout_manager timeout_manager;
|
||||
wf_impl_timeout_manager_init(&timeout_manager);
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
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);
|
||||
|
||||
jsonrpc_proxy_dispose(proxy);
|
||||
wf_impl_timeout_manager_cleanup(&timeout_manager);
|
||||
|
||||
ASSERT_TRUE(finished_context.is_called);
|
||||
ASSERT_FALSE(nullptr == finished_context.error);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_proxy, invoke_calls_finish_if_send_fails)
|
||||
{
|
||||
struct wf_impl_timeout_manager timeout_manager;
|
||||
wf_impl_timeout_manager_init(&timeout_manager);
|
||||
|
||||
SendContext send_context(false);
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
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);
|
||||
|
||||
jsonrpc_proxy_dispose(proxy);
|
||||
wf_impl_timeout_manager_cleanup(&timeout_manager);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_proxy, invoke_fails_if_another_request_is_pending)
|
||||
{
|
||||
struct wf_impl_timeout_manager timeout_manager;
|
||||
wf_impl_timeout_manager_init(&timeout_manager);
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
|
||||
|
||||
FinishedContext finished_context2;
|
||||
void * finished_data2 = reinterpret_cast<void*>(&finished_context2);
|
||||
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, wf_impl_jsonrpc_get_status(finished_context2.error));
|
||||
|
||||
jsonrpc_proxy_dispose(proxy);
|
||||
wf_impl_timeout_manager_cleanup(&timeout_manager);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_proxy, invoke_fails_if_request_is_invalid)
|
||||
{
|
||||
struct wf_impl_timeout_manager timeout_manager;
|
||||
wf_impl_timeout_manager_init(&timeout_manager);
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
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, wf_impl_jsonrpc_get_status(finished_context.error));
|
||||
|
||||
jsonrpc_proxy_dispose(proxy);
|
||||
wf_impl_timeout_manager_cleanup(&timeout_manager);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_proxy, on_result)
|
||||
{
|
||||
struct wf_impl_timeout_manager timeout_manager;
|
||||
wf_impl_timeout_manager_init(&timeout_manager);
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
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);
|
||||
|
||||
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));
|
||||
|
||||
jsonrpc_proxy_dispose(proxy);
|
||||
wf_impl_timeout_manager_cleanup(&timeout_manager);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_proxy, on_result_reject_response_with_unknown_id)
|
||||
{
|
||||
struct wf_impl_timeout_manager timeout_manager;
|
||||
wf_impl_timeout_manager_init(&timeout_manager);
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
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)));
|
||||
|
||||
jsonrpc_proxy_onresult(proxy, response);
|
||||
json_decref(response);
|
||||
|
||||
ASSERT_FALSE(finished_context.is_called);
|
||||
|
||||
jsonrpc_proxy_dispose(proxy);
|
||||
wf_impl_timeout_manager_cleanup(&timeout_manager);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_proxy, timeout)
|
||||
{
|
||||
struct wf_impl_timeout_manager timeout_manager;
|
||||
wf_impl_timeout_manager_init(&timeout_manager);
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, 0, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
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));
|
||||
|
||||
msleep(10);
|
||||
wf_impl_timeout_manager_check(&timeout_manager);
|
||||
|
||||
ASSERT_TRUE(finished_context.is_called);
|
||||
ASSERT_EQ(WF_BAD_TIMEOUT, wf_impl_jsonrpc_get_status(finished_context.error));
|
||||
|
||||
jsonrpc_proxy_dispose(proxy);
|
||||
wf_impl_timeout_manager_cleanup(&timeout_manager);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_proxy, cleanup_pending_request)
|
||||
{
|
||||
struct wf_impl_timeout_manager timeout_manager;
|
||||
wf_impl_timeout_manager_init(&timeout_manager);
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, 10, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
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);
|
||||
ASSERT_NE(nullptr, timeout_manager.timers);
|
||||
|
||||
jsonrpc_proxy_dispose(proxy);
|
||||
|
||||
ASSERT_TRUE(finished_context.is_called);
|
||||
ASSERT_EQ(nullptr, timeout_manager.timers);
|
||||
|
||||
wf_impl_timeout_manager_cleanup(&timeout_manager);
|
||||
}
|
||||
|
||||
|
||||
|
||||
TEST(jsonrpc_proxy, notify)
|
||||
{
|
||||
struct wf_impl_timeout_manager timeout_manager;
|
||||
wf_impl_timeout_manager_init(&timeout_manager);
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
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);
|
||||
|
||||
|
||||
jsonrpc_proxy_dispose(proxy);
|
||||
wf_impl_timeout_manager_cleanup(&timeout_manager);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_proxy, notify_dont_send_invalid_request)
|
||||
{
|
||||
struct wf_impl_timeout_manager timeout_manager;
|
||||
wf_impl_timeout_manager_init(&timeout_manager);
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
jsonrpc_proxy_notify(proxy, "foo", "?");
|
||||
|
||||
ASSERT_FALSE(send_context.is_called);
|
||||
|
||||
jsonrpc_proxy_dispose(proxy);
|
||||
wf_impl_timeout_manager_cleanup(&timeout_manager);
|
||||
}
|
||||
@@ -1,103 +0,0 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "jsonrpc/request.h"
|
||||
#include "jsonrpc/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(jsonrpc_request, create_dispose)
|
||||
{
|
||||
Context context{nullptr};
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
|
||||
struct jsonrpc_request * request =
|
||||
jsonrpc_request_create(42, &jsonrpc_send, user_data);
|
||||
|
||||
ASSERT_NE(nullptr, request);
|
||||
ASSERT_EQ(user_data, jsonrpc_request_get_userdata(request));
|
||||
|
||||
jsonrpc_request_dispose(request);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_request, respond)
|
||||
{
|
||||
Context context{nullptr};
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
|
||||
struct jsonrpc_request * request =
|
||||
jsonrpc_request_create(42, &jsonrpc_send, user_data);
|
||||
|
||||
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(jsonrpc_request, respond_error)
|
||||
{
|
||||
Context context{nullptr};
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
|
||||
struct jsonrpc_request * request =
|
||||
jsonrpc_request_create(42, &jsonrpc_send, user_data);
|
||||
|
||||
jsonrpc_respond_error(request, JSONRPC_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(JSONRPC_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);
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "jsonrpc/impl/response.h"
|
||||
#include "webfuse/core/status.h"
|
||||
#include "webfuse/core/json_util.h"
|
||||
|
||||
TEST(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 jsonrpc_response response;
|
||||
jsonrpc_impl_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);
|
||||
|
||||
jsonrpc_impl_response_cleanup(&response);
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(json_response, init_error)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_t * err = json_object();
|
||||
json_object_set_new(err, "code", json_integer(WF_BAD_ACCESS_DENIED));
|
||||
json_object_set_new(err, "message", json_string("access denied"));
|
||||
json_object_set_new(message, "error", err);
|
||||
json_object_set_new(message, "id", json_integer(23));
|
||||
|
||||
struct jsonrpc_response response;
|
||||
jsonrpc_impl_response_init(&response, message);
|
||||
|
||||
ASSERT_EQ(WF_BAD_ACCESS_DENIED, wf_impl_jsonrpc_get_status(response.error)) << json_string_value(json_object_get(response.error, "message"));
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
ASSERT_EQ(23, response.id);
|
||||
|
||||
jsonrpc_impl_response_cleanup(&response);
|
||||
json_decref(message);
|
||||
}
|
||||
|
||||
TEST(json_response, init_format_error)
|
||||
{
|
||||
json_t * message = json_object();
|
||||
json_object_set_new(message, "id", json_integer(12));
|
||||
|
||||
struct jsonrpc_response response;
|
||||
jsonrpc_impl_response_init(&response, message);
|
||||
|
||||
ASSERT_EQ(WF_BAD_FORMAT, wf_impl_jsonrpc_get_status(response.error));
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
ASSERT_EQ(12, response.id);
|
||||
|
||||
jsonrpc_impl_response_cleanup(&response);
|
||||
json_decref(message);
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
#include <string>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "jsonrpc/impl/response.h"
|
||||
#include "webfuse/core/json_util.h"
|
||||
|
||||
|
||||
static void response_parse_str(
|
||||
std::string const & buffer,
|
||||
struct jsonrpc_response * response)
|
||||
{
|
||||
json_t * message = json_loadb(buffer.c_str(), buffer.size(), 0, nullptr);
|
||||
if (nullptr != message)
|
||||
{
|
||||
jsonrpc_impl_response_init(response, message);
|
||||
json_decref(message);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(response_parser, test)
|
||||
{
|
||||
struct jsonrpc_response response;
|
||||
|
||||
// no object
|
||||
response_parse_str("[]", &response);
|
||||
ASSERT_NE(nullptr, response.error);
|
||||
ASSERT_EQ(-1, response.id);
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
jsonrpc_impl_response_cleanup(&response);
|
||||
|
||||
// empty
|
||||
response_parse_str("{}", &response);
|
||||
ASSERT_NE(nullptr, response.error);
|
||||
ASSERT_EQ(-1, response.id);
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
jsonrpc_impl_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);
|
||||
jsonrpc_impl_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);
|
||||
jsonrpc_impl_response_cleanup(&response);
|
||||
|
||||
// valid response
|
||||
response_parse_str("{\"result\": true, \"id\": 42}", &response);
|
||||
ASSERT_EQ(WF_GOOD, response.error);
|
||||
ASSERT_EQ(42, response.id);
|
||||
ASSERT_NE(nullptr, response.result);
|
||||
jsonrpc_impl_response_cleanup(&response);
|
||||
}
|
||||
@@ -1,123 +0,0 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "jsonrpc/server.h"
|
||||
#include "jsonrpc/request.h"
|
||||
#include "jsonrpc/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 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");
|
||||
jsonrpc_respond(request, result);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
TEST(jsonrpc_server, process_request)
|
||||
{
|
||||
struct jsonrpc_server * server = jsonrpc_server_create();
|
||||
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));
|
||||
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);
|
||||
jsonrpc_server_dispose(server);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_server, invoke_unknown_method)
|
||||
{
|
||||
struct jsonrpc_server * server = jsonrpc_server_create();
|
||||
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));
|
||||
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(JSONRPC_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);
|
||||
jsonrpc_server_dispose(server);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_server, skip_invalid_request)
|
||||
{
|
||||
struct jsonrpc_server * server = 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());
|
||||
jsonrpc_server_process(server, request, &jsonrpc_send, user_data);
|
||||
|
||||
ASSERT_FALSE(context.is_called);
|
||||
|
||||
json_decref(request);
|
||||
jsonrpc_server_dispose(server);
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "webfuse/core/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);
|
||||
}
|
||||
Reference in New Issue
Block a user