1
0
mirror of https://github.com/falk-werner/webfuse-provider synced 2026-03-02 04:09:18 +00:00

refactor: replaced jansson by own json parser implementation

This commit is contained in:
Falk Werner
2020-07-12 00:39:55 +02:00
parent 63ca5d5a6d
commit 63cc5b5388
35 changed files with 443 additions and 423 deletions

View File

@@ -1,28 +1,26 @@
#include <gtest/gtest.h>
#include "webfuse_provider/impl/jsonrpc/request.h"
#include "webfuse_provider/impl/json/parser.h"
#include <gtest/gtest.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));
char text[] = "{\"method\": \"method\", \"params\": {}, \"id\": 42}";
wfp_json_doc * doc = wfp_impl_json_parse(text);
ASSERT_TRUE(wfp_jsonrpc_is_request(request));
ASSERT_TRUE(wfp_jsonrpc_is_request(wfp_impl_json_root(doc)));
json_decref(request);
wfp_impl_json_dispose(doc);
}
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));
char text[] = "{\"method\": \"method\", \"params\": [], \"id\": 42}";
wfp_json_doc * doc = wfp_impl_json_parse(text);
ASSERT_TRUE(wfp_jsonrpc_is_request(request));
ASSERT_TRUE(wfp_jsonrpc_is_request(wfp_impl_json_root(doc)));
json_decref(request);
wfp_impl_json_dispose(doc);
}
TEST(wfp_jsonrpc_is_request, null_request)
@@ -32,81 +30,70 @@ TEST(wfp_jsonrpc_is_request, null_request)
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));
char text[] = "[\"method\", { }, 42]";
wfp_json_doc * doc = wfp_impl_json_parse(text);
ASSERT_FALSE(wfp_jsonrpc_is_request(wfp_impl_json_root(doc)));
ASSERT_FALSE(wfp_jsonrpc_is_request(request));
json_decref(request);
wfp_impl_json_dispose(doc);
}
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());
char text[] = "{\"method\": \"method\", \"params\": { }}";
wfp_json_doc * doc = wfp_impl_json_parse(text);
ASSERT_FALSE(wfp_jsonrpc_is_request(wfp_impl_json_root(doc)));
ASSERT_FALSE(wfp_jsonrpc_is_request(request));
json_decref(request);
wfp_impl_json_dispose(doc);
}
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"));
char text[] = "{\"method\": \"method\", \"params\": { }, \"id\": \"42\"}";
wfp_json_doc * doc = wfp_impl_json_parse(text);
ASSERT_FALSE(wfp_jsonrpc_is_request(wfp_impl_json_root(doc)));
ASSERT_FALSE(wfp_jsonrpc_is_request(request));
json_decref(request);
wfp_impl_json_dispose(doc);
}
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));
char text[] = "{\"params\": { }, \"id\": 42}";
wfp_json_doc * doc = wfp_impl_json_parse(text);
ASSERT_FALSE(wfp_jsonrpc_is_request(wfp_impl_json_root(doc)));
ASSERT_FALSE(wfp_jsonrpc_is_request(request));
json_decref(request);
wfp_impl_json_dispose(doc);
}
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));
char text[] = "{\"method\": 42, \"params\": {}, \"id\": 42}";
wfp_json_doc * doc = wfp_impl_json_parse(text);
ASSERT_FALSE(wfp_jsonrpc_is_request(wfp_impl_json_root(doc)));
ASSERT_FALSE(wfp_jsonrpc_is_request(request));
json_decref(request);
wfp_impl_json_dispose(doc);
}
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));
char text[] = "{\"method\": \"method\", \"id\": 42}";
wfp_json_doc * doc = wfp_impl_json_parse(text);
ASSERT_FALSE(wfp_jsonrpc_is_request(wfp_impl_json_root(doc)));
ASSERT_FALSE(wfp_jsonrpc_is_request(request));
json_decref(request);
wfp_impl_json_dispose(doc);
}
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));
char text[] = "{\"method\": \"method\", \"params\": \"params\", \"id\": 42}";
wfp_json_doc * doc = wfp_impl_json_parse(text);
ASSERT_FALSE(wfp_jsonrpc_is_request(wfp_impl_json_root(doc)));
ASSERT_FALSE(wfp_jsonrpc_is_request(request));
json_decref(request);
wfp_impl_json_dispose(doc);
}

View File

@@ -1,37 +1,35 @@
#include <gtest/gtest.h>
#include "webfuse_provider/impl/jsonrpc/response.h"
#include "webfuse_provider/impl/json/parser.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));
char text[] = "{\"result\": {}, \"id\": 42}";
wfp_json_doc * doc = wfp_impl_json_parse(text);
ASSERT_TRUE(wfp_jsonrpc_is_response(message));
ASSERT_TRUE(wfp_jsonrpc_is_response(wfp_impl_json_root(doc)));
json_decref(message);
wfp_impl_json_dispose(doc);
}
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));
char text[] = "{\"result\": \"also valid\", \"id\": 42}";
wfp_json_doc * doc = wfp_impl_json_parse(text);
ASSERT_TRUE(wfp_jsonrpc_is_response(message));
ASSERT_TRUE(wfp_jsonrpc_is_response(wfp_impl_json_root(doc)));
json_decref(message);
wfp_impl_json_dispose(doc);
}
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));
char text[] = "{\"error\": { }, \"id\": 42}";
wfp_json_doc * doc = wfp_impl_json_parse(text);
ASSERT_TRUE(wfp_jsonrpc_is_response(message));
ASSERT_TRUE(wfp_jsonrpc_is_response(wfp_impl_json_root(doc)));
json_decref(message);
wfp_impl_json_dispose(doc);
}
TEST(wfp_jsonrpc_is_response, invalid_null)
@@ -41,54 +39,51 @@ TEST(wfp_jsonrpc_is_response, invalid_null)
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));
char text[] = "[{ }, 42]";
wfp_json_doc * doc = wfp_impl_json_parse(text);
ASSERT_FALSE(wfp_jsonrpc_is_response(message));
ASSERT_FALSE(wfp_jsonrpc_is_response(wfp_impl_json_root(doc)));
json_decref(message);
wfp_impl_json_dispose(doc);
}
TEST(wfp_jsonrpc_is_response, invalid_missing_id)
{
json_t * message = json_object();
json_object_set_new(message, "result", json_object());
char text[] = "{\"result\": { } }";
wfp_json_doc * doc = wfp_impl_json_parse(text);
ASSERT_FALSE(wfp_jsonrpc_is_response(message));
ASSERT_FALSE(wfp_jsonrpc_is_response(wfp_impl_json_root(doc)));
json_decref(message);
wfp_impl_json_dispose(doc);
}
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"));
char text[] = "{\"result\": { }, \"id\": \"42\"}";
wfp_json_doc * doc = wfp_impl_json_parse(text);
ASSERT_FALSE(wfp_jsonrpc_is_response(message));
ASSERT_FALSE(wfp_jsonrpc_is_response(wfp_impl_json_root(doc)));
json_decref(message);
wfp_impl_json_dispose(doc);
}
TEST(wfp_jsonrpc_is_response, invalid_missing_result_and_error)
{
json_t * message = json_object();
json_object_set_new(message, "id", json_integer(42));
char text[] = "{\"id\": \"42\"}";
wfp_json_doc * doc = wfp_impl_json_parse(text);
ASSERT_FALSE(wfp_jsonrpc_is_response(message));
ASSERT_FALSE(wfp_jsonrpc_is_response(wfp_impl_json_root(doc)));
json_decref(message);
wfp_impl_json_dispose(doc);
}
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));
char text[] = "{\"error\": [], \"id\": \"42\"}";
wfp_json_doc * doc = wfp_impl_json_parse(text);
ASSERT_FALSE(wfp_jsonrpc_is_response(message));
ASSERT_FALSE(wfp_jsonrpc_is_response(wfp_impl_json_root(doc)));
json_decref(message);
wfp_impl_json_dispose(doc);
}

View File

@@ -1,5 +1,7 @@
#include <gtest/gtest.h>
#include "webfuse_provider/impl/jsonrpc/proxy.h"
#include "webfuse_provider/impl/jsonrpc/error.h"
#include "webfuse_provider/impl/json/parser.h"
#include "webfuse_provider/status.h"
#include "webfuse_provider/impl/timer/manager.h"
@@ -21,12 +23,6 @@ using testing::SaveArg;
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
{
std::string response;
@@ -58,8 +54,8 @@ namespace
struct FinishedContext
{
bool is_called;
json_t * result;
json_t * error;
wfp_json const * result;
wfp_jsonrpc_error * error;
FinishedContext()
: is_called(false)
@@ -71,27 +67,25 @@ namespace
~FinishedContext()
{
if (nullptr != result)
{
json_decref(result);
}
if (nullptr != error)
{
json_decref(error);
wfp_jsonrpc_error_dispose(error);
}
}
};
void jsonrpc_finished(
void * user_data,
json_t const * result,
json_t const * error)
wfp_json const * result,
wfp_jsonrpc_error 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);
context->result = result;
if (nullptr != error)
{
context->error = wfp_jsonrpc_error_create(error->code, error->message);
}
}
}
@@ -154,7 +148,7 @@ TEST(wfp_jsonrpc_proxy, invoke_fails_if_another_request_is_pending)
ASSERT_FALSE(finished_context.is_called);
ASSERT_TRUE(finished_context2.is_called);
ASSERT_EQ(WFP_BAD_BUSY, jsonrpc_get_status(finished_context2.error));
ASSERT_EQ(WFP_BAD_BUSY, finished_context2.error->code);
wfp_jsonrpc_proxy_dispose(proxy);
wfp_timer_manager_dispose(timer_manager);
@@ -194,17 +188,15 @@ TEST(wfp_jsonrpc_proxy, on_result)
ASSERT_TRUE(send_context.is_called);
json_t * response = json_object();
json_object_set_new(response, "result", json_string("okay"));
json_object_set_new(response, "id", json_integer(42));
char response_text[] = "{\"result\": \"okay\", \"id\": 42}";
wfp_json_doc * doc = wfp_impl_json_parse(response_text);
wfp_jsonrpc_proxy_onresult(proxy, response);
json_decref(response);
wfp_jsonrpc_proxy_onresult(proxy, wfp_impl_json_root(doc));
wfp_impl_json_dispose(doc);
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));
ASSERT_NE(nullptr, finished_context.result);
wfp_jsonrpc_proxy_dispose(proxy);
wfp_timer_manager_dispose(timer_manager);
@@ -224,12 +216,11 @@ TEST(wfp_jsonrpc_proxy, on_result_reject_response_with_unknown_id)
ASSERT_TRUE(send_context.is_called);
json_t * response = json_object();
json_object_set_new(response, "result", json_string("okay"));
json_object_set_new(response, "id", json_integer(1234));
char response_text[] = "{\"result\": \"okay\", \"id\": 1234}";
wfp_json_doc * doc = wfp_impl_json_parse(response_text);
wfp_jsonrpc_proxy_onresult(proxy, response);
json_decref(response);
wfp_jsonrpc_proxy_onresult(proxy, wfp_impl_json_root(doc));
wfp_impl_json_dispose(doc);
ASSERT_FALSE(finished_context.is_called);
@@ -255,7 +246,7 @@ TEST(wfp_jsonrpc_proxy, timeout)
wfp_timer_manager_check(timer_manager);
ASSERT_TRUE(finished_context.is_called);
ASSERT_EQ(WFP_BAD_TIMEOUT, jsonrpc_get_status(finished_context.error));
ASSERT_EQ(WFP_BAD_TIMEOUT, finished_context.error->code);
wfp_jsonrpc_proxy_dispose(proxy);
wfp_timer_manager_dispose(timer_manager);
@@ -349,12 +340,11 @@ TEST(wfp_jsonrpc_proxy, on_result_swallow_if_no_request_pending)
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));
char response_text[] = "{\"result\": \"okay\", \"id\": 42}";
wfp_json_doc * doc = wfp_impl_json_parse(response_text);
wfp_jsonrpc_proxy_onresult(proxy, response);
json_decref(response);
wfp_jsonrpc_proxy_onresult(proxy, wfp_impl_json_root(doc));
wfp_impl_json_dispose(doc);
wfp_jsonrpc_proxy_dispose(proxy);
wfp_timer_manager_dispose(timer_manager);

View File

@@ -1,37 +1,33 @@
#include "webfuse_provider/impl/jsonrpc/request.h"
#include "webfuse_provider/impl/json/parser.h"
#include <gtest/gtest.h>
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));
char text[] = "{\"method\": \"some_method\", \"params\": { }, \"id\": 42}";
wfp_json_doc * doc = wfp_impl_json_parse(text);
ASSERT_TRUE(wfp_jsonrpc_is_request(request));
ASSERT_TRUE(wfp_jsonrpc_is_request(wfp_impl_json_root(doc)));
json_decref(request);
wfp_impl_json_dispose(doc);
}
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));
char text[] = "{\"method\": \"some_method\", \"id\": 42}";
wfp_json_doc * doc = wfp_impl_json_parse(text);
ASSERT_FALSE(wfp_jsonrpc_is_request(request));
ASSERT_FALSE(wfp_jsonrpc_is_request(wfp_impl_json_root(doc)));
json_decref(request);
wfp_impl_json_dispose(doc);
}
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));
char text[] = "{\"method\": \"some_method\", \"params\": \"invalid_params\", \"id\": 42}";
wfp_json_doc * doc = wfp_impl_json_parse(text);
ASSERT_FALSE(wfp_jsonrpc_is_request(request));
ASSERT_FALSE(wfp_jsonrpc_is_request(wfp_impl_json_root(doc)));
json_decref(request);
wfp_impl_json_dispose(doc);
}

View File

@@ -1,147 +1,136 @@
#include <gtest/gtest.h>
#include "webfuse_provider/impl/jsonrpc/response_intern.h"
#include "webfuse_provider/status.h"
#include "webfuse_provider/impl/json/parser.h"
#include "webfuse_provider/impl/json/node.h"
#include "webfuse_provider/impl/jsonrpc/error.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));
char text[] = "{\"result\": 47, \"id\": 11}";
wfp_json_doc * doc = wfp_impl_json_parse(text);
struct wfp_jsonrpc_response response;
wfp_jsonrpc_response_init(&response, message);
wfp_jsonrpc_response_init(&response, wfp_impl_json_root(doc));
ASSERT_EQ(nullptr, response.error);
ASSERT_TRUE(json_is_integer(response.result));
ASSERT_EQ(47, json_integer_value(response.result));
ASSERT_TRUE(wfp_impl_json_is_int(response.result));
ASSERT_EQ(47, wfp_impl_json_get_int(response.result));
ASSERT_EQ(11, response.id);
wfp_jsonrpc_response_cleanup(&response);
json_decref(message);
wfp_impl_json_dispose(doc);
}
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));
char text[] = "{\"error\": {\"code\": 42, \"message\": \"Don't Panic!\"}, \"id\": 23}";
wfp_json_doc * doc = wfp_impl_json_parse(text);
struct wfp_jsonrpc_response response;
wfp_jsonrpc_response_init(&response, message);
wfp_jsonrpc_response_init(&response, wfp_impl_json_root(doc));
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(42, response.error->code);
ASSERT_STREQ("Don't Panic!", response.error->message);
ASSERT_EQ(nullptr, response.result);
ASSERT_EQ(23, response.id);
wfp_jsonrpc_response_cleanup(&response);
json_decref(message);
wfp_impl_json_dispose(doc);
}
TEST(wfp_json_response, init_fail_missing_result_and_error)
{
json_t * message = json_object();
json_object_set_new(message, "id", json_integer(12));
char text[] = "{\"id\": 12}";
wfp_json_doc * doc = wfp_impl_json_parse(text);
struct wfp_jsonrpc_response response;
wfp_jsonrpc_response_init(&response, message);
wfp_jsonrpc_response_init(&response, wfp_impl_json_root(doc));
ASSERT_EQ(WFP_BAD_FORMAT, json_integer_value(json_object_get(response.error, "code")));
ASSERT_EQ(WFP_BAD_FORMAT, response.error->code);
ASSERT_EQ(nullptr, response.result);
ASSERT_EQ(12, response.id);
wfp_jsonrpc_response_cleanup(&response);
json_decref(message);
wfp_impl_json_dispose(doc);
}
TEST(wfp_json_response, init_fail_missing_id)
{
json_t * message = json_object();
json_object_set_new(message, "result", json_integer(47));
char text[] = "{\"result\": 47}";
wfp_json_doc * doc = wfp_impl_json_parse(text);
struct wfp_jsonrpc_response response;
wfp_jsonrpc_response_init(&response, message);
wfp_jsonrpc_response_init(&response, wfp_impl_json_root(doc));
ASSERT_EQ(WFP_BAD_FORMAT, json_integer_value(json_object_get(response.error, "code")));
ASSERT_EQ(WFP_BAD_FORMAT, response.error->code);
ASSERT_EQ(nullptr, response.result);
ASSERT_EQ(-1, response.id);
wfp_jsonrpc_response_cleanup(&response);
json_decref(message);
wfp_impl_json_dispose(doc);
}
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"));
char text[] = "{\"result\": 47, \"id\": \"42\"}";
wfp_json_doc * doc = wfp_impl_json_parse(text);
struct wfp_jsonrpc_response response;
wfp_jsonrpc_response_init(&response, message);
wfp_jsonrpc_response_init(&response, wfp_impl_json_root(doc));
ASSERT_EQ(WFP_BAD_FORMAT, json_integer_value(json_object_get(response.error, "code")));
ASSERT_EQ(WFP_BAD_FORMAT, response.error->code);
ASSERT_EQ(nullptr, response.result);
ASSERT_EQ(-1, response.id);
wfp_jsonrpc_response_cleanup(&response);
json_decref(message);
wfp_impl_json_dispose(doc);
}
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));
char text[] = "{\"error\": {\"message\": \"Don't Panic!\"}, \"id\": 23}";
wfp_json_doc * doc = wfp_impl_json_parse(text);
struct wfp_jsonrpc_response response;
wfp_jsonrpc_response_init(&response, message);
wfp_jsonrpc_response_init(&response, wfp_impl_json_root(doc));
ASSERT_EQ(WFP_BAD_FORMAT, json_integer_value(json_object_get(response.error, "code")));
ASSERT_EQ(WFP_BAD_FORMAT,response.error->code);
ASSERT_EQ(nullptr, response.result);
ASSERT_EQ(23, response.id);
wfp_jsonrpc_response_cleanup(&response);
json_decref(message);
wfp_impl_json_dispose(doc);
}
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));
char text[] = "{\"error\": {\"code\": \"42\", \"message\": \"Don't Panic!\"}, \"id\": 23}";
wfp_json_doc * doc = wfp_impl_json_parse(text);
struct wfp_jsonrpc_response response;
wfp_jsonrpc_response_init(&response, message);
wfp_jsonrpc_response_init(&response, wfp_impl_json_root(doc));
ASSERT_EQ(WFP_BAD_FORMAT, json_integer_value(json_object_get(response.error, "code")));
ASSERT_EQ(WFP_BAD_FORMAT, response.error->code);
ASSERT_EQ(nullptr, response.result);
ASSERT_EQ(23, response.id);
wfp_jsonrpc_response_cleanup(&response);
json_decref(message);
wfp_impl_json_dispose(doc);
}
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));
char text[] = "{\"error\": \"invalid error type\", \"id\": 23}";
wfp_json_doc * doc = wfp_impl_json_parse(text);
struct wfp_jsonrpc_response response;
wfp_jsonrpc_response_init(&response, message);
wfp_jsonrpc_response_init(&response, wfp_impl_json_root(doc));
ASSERT_EQ(WFP_BAD_FORMAT, json_integer_value(json_object_get(response.error, "code")));
ASSERT_EQ(WFP_BAD_FORMAT, response.error->code);
ASSERT_EQ(nullptr, response.result);
ASSERT_EQ(23, response.id);
wfp_jsonrpc_response_cleanup(&response);
json_decref(message);
wfp_impl_json_dispose(doc);
}

View File

@@ -1,58 +1,82 @@
#include <string>
#include "webfuse_provider/impl/jsonrpc/response_intern.h"
#include "webfuse_provider/impl/jsonrpc/error.h"
#include "webfuse_provider/impl/json/parser.h"
#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)
TEST(response_parser, fail_no_object)
{
json_t * message = json_loadb(buffer.c_str(), buffer.size(), 0, nullptr);
if (nullptr != message)
{
wfp_jsonrpc_response_init(response, message);
json_decref(message);
}
char text[] = "[]";
wfp_json_doc * doc = wfp_impl_json_parse(text);
struct wfp_jsonrpc_response response;
wfp_jsonrpc_response_init(&response, wfp_impl_json_root(doc));
ASSERT_NE(nullptr, response.error);
ASSERT_EQ(-1, response.id);
ASSERT_EQ(nullptr, response.result);
wfp_jsonrpc_response_cleanup(&response);
wfp_impl_json_dispose(doc);
}
TEST(response_parser, test)
TEST(response_error, fail_empty_object)
{
char text[] = "{}";
wfp_json_doc * doc = wfp_impl_json_parse(text);
struct wfp_jsonrpc_response response;
wfp_jsonrpc_response_init(&response, wfp_impl_json_root(doc));
// 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);
wfp_impl_json_dispose(doc);
}
TEST(response_error, fail_no_data)
{
char text[] = "{\"id\":42}";
wfp_json_doc * doc = wfp_impl_json_parse(text);
struct wfp_jsonrpc_response response;
wfp_jsonrpc_response_init(&response, wfp_impl_json_root(doc));
// 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);
wfp_jsonrpc_response_cleanup(&response);
wfp_impl_json_dispose(doc);
}
TEST(response_error, fail_with_custom_error_code)
{
char text[] = "{\"error\":{\"code\": 42}, \"id\": 42}";
wfp_json_doc * doc = wfp_impl_json_parse(text);
struct wfp_jsonrpc_response response;
wfp_jsonrpc_response_init(&response, wfp_impl_json_root(doc));
ASSERT_NE(nullptr, response.error);
ASSERT_EQ(42, json_integer_value(json_object_get(response.error, "code")));
ASSERT_EQ(42, 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);
wfp_jsonrpc_response_cleanup(&response);
wfp_impl_json_dispose(doc);
}
TEST(response_parser, fail_invalid_response)
{
char text[] = "{\"result\": true, \"id\": 42}";
wfp_json_doc * doc = wfp_impl_json_parse(text);
struct wfp_jsonrpc_response response;
wfp_jsonrpc_response_init(&response, wfp_impl_json_root(doc));
ASSERT_EQ(nullptr, response.error);
ASSERT_EQ(42, response.id);
ASSERT_NE(nullptr, response.result);
wfp_jsonrpc_response_cleanup(&response);
wfp_impl_json_dispose(doc);
}