diff --git a/test/webfuse_provider/jsonrpc/test_is_request.cc b/test/webfuse_provider/jsonrpc/test_is_request.cc index bb8360c..3b05df8 100644 --- a/test/webfuse_provider/jsonrpc/test_is_request.cc +++ b/test/webfuse_provider/jsonrpc/test_is_request.cc @@ -1,26 +1,22 @@ #include "webfuse_provider/impl/jsonrpc/request.h" -#include "webfuse_provider/impl/json/parser.h" +#include "webfuse_provider/test_util/json_doc.hpp" #include +using webfuse_test::JsonDoc; + TEST(wfp_jsonrpc_is_request, request_with_object_params) { - char text[] = "{\"method\": \"method\", \"params\": {}, \"id\": 42}"; - wfp_json_doc * doc = wfp_impl_json_parse(text); - - ASSERT_TRUE(wfp_jsonrpc_is_request(wfp_impl_json_root(doc))); + JsonDoc doc("{\"method\": \"method\", \"params\": {}, \"id\": 42}"); - wfp_impl_json_dispose(doc); + ASSERT_TRUE(wfp_jsonrpc_is_request(doc.root())); } TEST(wfp_jsonrpc_is_request, request_with_array_params) { - char text[] = "{\"method\": \"method\", \"params\": [], \"id\": 42}"; - wfp_json_doc * doc = wfp_impl_json_parse(text); - - ASSERT_TRUE(wfp_jsonrpc_is_request(wfp_impl_json_root(doc))); + JsonDoc doc("{\"method\": \"method\", \"params\": [], \"id\": 42}"); - wfp_impl_json_dispose(doc); + ASSERT_TRUE(wfp_jsonrpc_is_request(doc.root())); } TEST(wfp_jsonrpc_is_request, null_request) @@ -30,70 +26,49 @@ TEST(wfp_jsonrpc_is_request, null_request) TEST(wfp_jsonrpc_is_request, invalid_request) { - char text[] = "[\"method\", { }, 42]"; - wfp_json_doc * doc = wfp_impl_json_parse(text); + JsonDoc doc("[\"method\", { }, 42]"); - ASSERT_FALSE(wfp_jsonrpc_is_request(wfp_impl_json_root(doc))); - - wfp_impl_json_dispose(doc); + ASSERT_FALSE(wfp_jsonrpc_is_request(doc.root())); } TEST(wfp_jsonrpc_is_request, invalid_request_without_id) { - char text[] = "{\"method\": \"method\", \"params\": { }}"; - wfp_json_doc * doc = wfp_impl_json_parse(text); + JsonDoc doc("{\"method\": \"method\", \"params\": { }}"); - ASSERT_FALSE(wfp_jsonrpc_is_request(wfp_impl_json_root(doc))); - - wfp_impl_json_dispose(doc); + ASSERT_FALSE(wfp_jsonrpc_is_request(doc.root())); } TEST(wfp_jsonrpc_is_request, invalid_request_due_to_invalid_id) { - char text[] = "{\"method\": \"method\", \"params\": { }, \"id\": \"42\"}"; - wfp_json_doc * doc = wfp_impl_json_parse(text); + JsonDoc doc("{\"method\": \"method\", \"params\": { }, \"id\": \"42\"}"); - ASSERT_FALSE(wfp_jsonrpc_is_request(wfp_impl_json_root(doc))); - - wfp_impl_json_dispose(doc); + ASSERT_FALSE(wfp_jsonrpc_is_request(doc.root())); } TEST(wfp_jsonrpc_is_request, invalid_request_without_method) { - char text[] = "{\"params\": { }, \"id\": 42}"; - wfp_json_doc * doc = wfp_impl_json_parse(text); + JsonDoc doc("{\"params\": { }, \"id\": 42}"); - ASSERT_FALSE(wfp_jsonrpc_is_request(wfp_impl_json_root(doc))); - - wfp_impl_json_dispose(doc); + ASSERT_FALSE(wfp_jsonrpc_is_request(doc.root())); } TEST(wfp_jsonrpc_is_request, invalid_request_due_to_invalid_method) { - char text[] = "{\"method\": 42, \"params\": {}, \"id\": 42}"; - wfp_json_doc * doc = wfp_impl_json_parse(text); + JsonDoc doc("{\"method\": 42, \"params\": {}, \"id\": 42}"); - ASSERT_FALSE(wfp_jsonrpc_is_request(wfp_impl_json_root(doc))); - - wfp_impl_json_dispose(doc); + ASSERT_FALSE(wfp_jsonrpc_is_request(doc.root())); } TEST(wfp_jsonrpc_is_request, invalid_request_without_params) { - char text[] = "{\"method\": \"method\", \"id\": 42}"; - wfp_json_doc * doc = wfp_impl_json_parse(text); + JsonDoc doc("{\"method\": \"method\", \"id\": 42}"); - ASSERT_FALSE(wfp_jsonrpc_is_request(wfp_impl_json_root(doc))); - - wfp_impl_json_dispose(doc); + ASSERT_FALSE(wfp_jsonrpc_is_request(doc.root())); } TEST(wfp_jsonrpc_is_request, invalid_request_due_to_invalid_params) { - char text[] = "{\"method\": \"method\", \"params\": \"params\", \"id\": 42}"; - wfp_json_doc * doc = wfp_impl_json_parse(text); + JsonDoc doc("{\"method\": \"method\", \"params\": \"params\", \"id\": 42}"); - ASSERT_FALSE(wfp_jsonrpc_is_request(wfp_impl_json_root(doc))); - - wfp_impl_json_dispose(doc); + ASSERT_FALSE(wfp_jsonrpc_is_request(doc.root())); } diff --git a/test/webfuse_provider/jsonrpc/test_is_response.cc b/test/webfuse_provider/jsonrpc/test_is_response.cc index 8d0c149..4165dde 100644 --- a/test/webfuse_provider/jsonrpc/test_is_response.cc +++ b/test/webfuse_provider/jsonrpc/test_is_response.cc @@ -1,35 +1,28 @@ #include #include "webfuse_provider/impl/jsonrpc/response.h" -#include "webfuse_provider/impl/json/parser.h" +#include "webfuse_provider/test_util/json_doc.hpp" + +using webfuse_test::JsonDoc; TEST(wfp_jsonrpc_is_response, valid_result) { - char text[] = "{\"result\": {}, \"id\": 42}"; - wfp_json_doc * doc = wfp_impl_json_parse(text); - - ASSERT_TRUE(wfp_jsonrpc_is_response(wfp_impl_json_root(doc))); + JsonDoc doc("{\"result\": {}, \"id\": 42}"); - wfp_impl_json_dispose(doc); + ASSERT_TRUE(wfp_jsonrpc_is_response(doc.root())); } TEST(wfp_jsonrpc_is_response, valid_result_string) { - char text[] = "{\"result\": \"also valid\", \"id\": 42}"; - wfp_json_doc * doc = wfp_impl_json_parse(text); - - ASSERT_TRUE(wfp_jsonrpc_is_response(wfp_impl_json_root(doc))); + JsonDoc doc("{\"result\": \"also valid\", \"id\": 42}"); - wfp_impl_json_dispose(doc); + ASSERT_TRUE(wfp_jsonrpc_is_response(doc.root())); } TEST(wfp_jsonrpc_is_response, valid_error) { - char text[] = "{\"error\": { }, \"id\": 42}"; - wfp_json_doc * doc = wfp_impl_json_parse(text); + JsonDoc doc("{\"error\": { }, \"id\": 42}"); - ASSERT_TRUE(wfp_jsonrpc_is_response(wfp_impl_json_root(doc))); - - wfp_impl_json_dispose(doc); + ASSERT_TRUE(wfp_jsonrpc_is_response(doc.root())); } TEST(wfp_jsonrpc_is_response, invalid_null) @@ -39,51 +32,36 @@ TEST(wfp_jsonrpc_is_response, invalid_null) TEST(wfp_jsonrpc_is_response, invalid_message) { - char text[] = "[{ }, 42]"; - wfp_json_doc * doc = wfp_impl_json_parse(text); - - ASSERT_FALSE(wfp_jsonrpc_is_response(wfp_impl_json_root(doc))); + JsonDoc doc("[{ }, 42]"); - wfp_impl_json_dispose(doc); + ASSERT_FALSE(wfp_jsonrpc_is_response(doc.root())); } TEST(wfp_jsonrpc_is_response, invalid_missing_id) { - char text[] = "{\"result\": { } }"; - wfp_json_doc * doc = wfp_impl_json_parse(text); + JsonDoc doc("{\"result\": { } }"); - ASSERT_FALSE(wfp_jsonrpc_is_response(wfp_impl_json_root(doc))); - - wfp_impl_json_dispose(doc); + ASSERT_FALSE(wfp_jsonrpc_is_response(doc.root())); } TEST(wfp_jsonrpc_is_response, invalid_id_wrong_type) { - char text[] = "{\"result\": { }, \"id\": \"42\"}"; - wfp_json_doc * doc = wfp_impl_json_parse(text); - - ASSERT_FALSE(wfp_jsonrpc_is_response(wfp_impl_json_root(doc))); + JsonDoc doc("{\"result\": { }, \"id\": \"42\"}"); - wfp_impl_json_dispose(doc); + ASSERT_FALSE(wfp_jsonrpc_is_response(doc.root())); } TEST(wfp_jsonrpc_is_response, invalid_missing_result_and_error) { - char text[] = "{\"id\": \"42\"}"; - wfp_json_doc * doc = wfp_impl_json_parse(text); + JsonDoc doc("{\"id\": \"42\"}"); - ASSERT_FALSE(wfp_jsonrpc_is_response(wfp_impl_json_root(doc))); - - wfp_impl_json_dispose(doc); + ASSERT_FALSE(wfp_jsonrpc_is_response(doc.root())); } TEST(wfp_jsonrpc_is_response, invalid_error_wrong_type) { - char text[] = "{\"error\": [], \"id\": \"42\"}"; - wfp_json_doc * doc = wfp_impl_json_parse(text); - - ASSERT_FALSE(wfp_jsonrpc_is_response(wfp_impl_json_root(doc))); + JsonDoc doc("{\"error\": [], \"id\": \"42\"}"); - wfp_impl_json_dispose(doc); + ASSERT_FALSE(wfp_jsonrpc_is_response(doc.root())); } diff --git a/test/webfuse_provider/jsonrpc/test_proxy.cc b/test/webfuse_provider/jsonrpc/test_proxy.cc index 899eec2..575fdf1 100644 --- a/test/webfuse_provider/jsonrpc/test_proxy.cc +++ b/test/webfuse_provider/jsonrpc/test_proxy.cc @@ -1,11 +1,11 @@ #include #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" #include "webfuse_provider/jsonrpc/mock_timer.hpp" +#include "webfuse_provider/test_util/json_doc.hpp" #include @@ -14,6 +14,7 @@ using namespace std::chrono_literals; using wfp_jsonrpc_test::MockTimer; +using webfuse_test::JsonDoc; using testing::Return; using testing::_; using testing::DoAll; @@ -188,11 +189,8 @@ TEST(wfp_jsonrpc_proxy, on_result) ASSERT_TRUE(send_context.is_called); - char response_text[] = "{\"result\": \"okay\", \"id\": 42}"; - wfp_json_doc * doc = wfp_impl_json_parse(response_text); - - wfp_jsonrpc_proxy_onresult(proxy, wfp_impl_json_root(doc)); - wfp_impl_json_dispose(doc); + JsonDoc doc("{\"result\": \"okay\", \"id\": 42}"); + wfp_jsonrpc_proxy_onresult(proxy, doc.root()); ASSERT_TRUE(finished_context.is_called); ASSERT_EQ(nullptr, finished_context.error); @@ -216,11 +214,8 @@ TEST(wfp_jsonrpc_proxy, on_result_reject_response_with_unknown_id) ASSERT_TRUE(send_context.is_called); - char response_text[] = "{\"result\": \"okay\", \"id\": 1234}"; - wfp_json_doc * doc = wfp_impl_json_parse(response_text); - - wfp_jsonrpc_proxy_onresult(proxy, wfp_impl_json_root(doc)); - wfp_impl_json_dispose(doc); + JsonDoc doc("{\"result\": \"okay\", \"id\": 1234}"); + wfp_jsonrpc_proxy_onresult(proxy, doc.root()); ASSERT_FALSE(finished_context.is_called); @@ -340,11 +335,8 @@ TEST(wfp_jsonrpc_proxy, on_result_swallow_if_no_request_pending) void * send_data = reinterpret_cast(&send_context); struct wfp_jsonrpc_proxy * proxy = wfp_jsonrpc_proxy_create(timer_manager, WFP_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); - char response_text[] = "{\"result\": \"okay\", \"id\": 42}"; - wfp_json_doc * doc = wfp_impl_json_parse(response_text); - - wfp_jsonrpc_proxy_onresult(proxy, wfp_impl_json_root(doc)); - wfp_impl_json_dispose(doc); + JsonDoc doc("{\"result\": \"okay\", \"id\": 42}"); + wfp_jsonrpc_proxy_onresult(proxy, doc.root()); wfp_jsonrpc_proxy_dispose(proxy); wfp_timer_manager_dispose(timer_manager); diff --git a/test/webfuse_provider/jsonrpc/test_request.cc b/test/webfuse_provider/jsonrpc/test_request.cc index 6b94183..9c90340 100644 --- a/test/webfuse_provider/jsonrpc/test_request.cc +++ b/test/webfuse_provider/jsonrpc/test_request.cc @@ -1,33 +1,26 @@ #include "webfuse_provider/impl/jsonrpc/request.h" -#include "webfuse_provider/impl/json/parser.h" +#include "webfuse_provider/test_util/json_doc.hpp" #include +using webfuse_test::JsonDoc; + TEST(wfp_jsonrpc_request, is_request_object_params) { - char text[] = "{\"method\": \"some_method\", \"params\": { }, \"id\": 42}"; - wfp_json_doc * doc = wfp_impl_json_parse(text); - - ASSERT_TRUE(wfp_jsonrpc_is_request(wfp_impl_json_root(doc))); + JsonDoc doc("{\"method\": \"some_method\", \"params\": { }, \"id\": 42}"); - wfp_impl_json_dispose(doc); + ASSERT_TRUE(wfp_jsonrpc_is_request(doc.root())); } TEST(wfp_jsonrpc_request, is_request_fail_missing_params) { - char text[] = "{\"method\": \"some_method\", \"id\": 42}"; - wfp_json_doc * doc = wfp_impl_json_parse(text); + JsonDoc doc("{\"method\": \"some_method\", \"id\": 42}"); - ASSERT_FALSE(wfp_jsonrpc_is_request(wfp_impl_json_root(doc))); - - wfp_impl_json_dispose(doc); + ASSERT_FALSE(wfp_jsonrpc_is_request(doc.root())); } TEST(wfp_jsonrpc_request, is_request_fail_params_wrong_type) { - 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(wfp_impl_json_root(doc))); + JsonDoc doc("{\"method\": \"some_method\", \"params\": \"invalid_params\", \"id\": 42}"); - wfp_impl_json_dispose(doc); + ASSERT_FALSE(wfp_jsonrpc_is_request(doc.root())); } diff --git a/test/webfuse_provider/jsonrpc/test_response.cc b/test/webfuse_provider/jsonrpc/test_response.cc index b8f36a2..fd9f919 100644 --- a/test/webfuse_provider/jsonrpc/test_response.cc +++ b/test/webfuse_provider/jsonrpc/test_response.cc @@ -1,17 +1,18 @@ #include #include "webfuse_provider/impl/jsonrpc/response_intern.h" #include "webfuse_provider/status.h" -#include "webfuse_provider/impl/json/parser.h" +#include "webfuse_provider/test_util/json_doc.hpp" #include "webfuse_provider/impl/json/node.h" #include "webfuse_provider/impl/jsonrpc/error.h" +using webfuse_test::JsonDoc; + TEST(wfp_json_response, init_result) { - char text[] = "{\"result\": 47, \"id\": 11}"; - wfp_json_doc * doc = wfp_impl_json_parse(text); + JsonDoc doc("{\"result\": 47, \"id\": 11}"); struct wfp_jsonrpc_response response; - wfp_jsonrpc_response_init(&response, wfp_impl_json_root(doc)); + wfp_jsonrpc_response_init(&response,doc.root()); ASSERT_EQ(nullptr, response.error); ASSERT_TRUE(wfp_impl_json_is_int(response.result)); @@ -19,16 +20,14 @@ TEST(wfp_json_response, init_result) ASSERT_EQ(11, response.id); wfp_jsonrpc_response_cleanup(&response); - wfp_impl_json_dispose(doc); } TEST(wfp_json_response, init_error) { - char text[] = "{\"error\": {\"code\": 42, \"message\": \"Don't Panic!\"}, \"id\": 23}"; - wfp_json_doc * doc = wfp_impl_json_parse(text); + JsonDoc doc("{\"error\": {\"code\": 42, \"message\": \"Don't Panic!\"}, \"id\": 23}"); struct wfp_jsonrpc_response response; - wfp_jsonrpc_response_init(&response, wfp_impl_json_root(doc)); + wfp_jsonrpc_response_init(&response, doc.root()); ASSERT_EQ(42, response.error->code); ASSERT_STREQ("Don't Panic!", response.error->message); @@ -36,101 +35,88 @@ TEST(wfp_json_response, init_error) ASSERT_EQ(23, response.id); wfp_jsonrpc_response_cleanup(&response); - wfp_impl_json_dispose(doc); } TEST(wfp_json_response, init_fail_missing_result_and_error) { - char text[] = "{\"id\": 12}"; - wfp_json_doc * doc = wfp_impl_json_parse(text); + JsonDoc doc("{\"id\": 12}"); struct wfp_jsonrpc_response response; - wfp_jsonrpc_response_init(&response, wfp_impl_json_root(doc)); + wfp_jsonrpc_response_init(&response, doc.root()); ASSERT_EQ(WFP_BAD_FORMAT, response.error->code); ASSERT_EQ(nullptr, response.result); ASSERT_EQ(12, response.id); wfp_jsonrpc_response_cleanup(&response); - wfp_impl_json_dispose(doc); } TEST(wfp_json_response, init_fail_missing_id) { - char text[] = "{\"result\": 47}"; - wfp_json_doc * doc = wfp_impl_json_parse(text); + JsonDoc doc("{\"result\": 47}"); struct wfp_jsonrpc_response response; - wfp_jsonrpc_response_init(&response, wfp_impl_json_root(doc)); + wfp_jsonrpc_response_init(&response, doc.root()); ASSERT_EQ(WFP_BAD_FORMAT, response.error->code); ASSERT_EQ(nullptr, response.result); ASSERT_EQ(-1, response.id); wfp_jsonrpc_response_cleanup(&response); - wfp_impl_json_dispose(doc); } TEST(wfp_json_response, init_fail_wrong_id_type) { - char text[] = "{\"result\": 47, \"id\": \"42\"}"; - wfp_json_doc * doc = wfp_impl_json_parse(text); + JsonDoc doc("{\"result\": 47, \"id\": \"42\"}"); struct wfp_jsonrpc_response response; - wfp_jsonrpc_response_init(&response, wfp_impl_json_root(doc)); + wfp_jsonrpc_response_init(&response, doc.root()); ASSERT_EQ(WFP_BAD_FORMAT, response.error->code); ASSERT_EQ(nullptr, response.result); ASSERT_EQ(-1, response.id); wfp_jsonrpc_response_cleanup(&response); - wfp_impl_json_dispose(doc); } TEST(wfp_json_response, init_fail_error_missing_code) { - char text[] = "{\"error\": {\"message\": \"Don't Panic!\"}, \"id\": 23}"; - wfp_json_doc * doc = wfp_impl_json_parse(text); + JsonDoc doc("{\"error\": {\"message\": \"Don't Panic!\"}, \"id\": 23}"); struct wfp_jsonrpc_response response; - wfp_jsonrpc_response_init(&response, wfp_impl_json_root(doc)); + wfp_jsonrpc_response_init(&response, doc.root()); ASSERT_EQ(WFP_BAD_FORMAT,response.error->code); ASSERT_EQ(nullptr, response.result); ASSERT_EQ(23, response.id); wfp_jsonrpc_response_cleanup(&response); - wfp_impl_json_dispose(doc); } TEST(wfp_json_response, init_fail_error_wrong_code_type) { - char text[] = "{\"error\": {\"code\": \"42\", \"message\": \"Don't Panic!\"}, \"id\": 23}"; - wfp_json_doc * doc = wfp_impl_json_parse(text); + JsonDoc doc("{\"error\": {\"code\": \"42\", \"message\": \"Don't Panic!\"}, \"id\": 23}"); struct wfp_jsonrpc_response response; - wfp_jsonrpc_response_init(&response, wfp_impl_json_root(doc)); + wfp_jsonrpc_response_init(&response, doc.root()); ASSERT_EQ(WFP_BAD_FORMAT, response.error->code); ASSERT_EQ(nullptr, response.result); ASSERT_EQ(23, response.id); wfp_jsonrpc_response_cleanup(&response); - wfp_impl_json_dispose(doc); } TEST(wfp_json_response, init_fail_error_wrong_type) { - char text[] = "{\"error\": \"invalid error type\", \"id\": 23}"; - wfp_json_doc * doc = wfp_impl_json_parse(text); + JsonDoc doc("{\"error\": \"invalid error type\", \"id\": 23}"); struct wfp_jsonrpc_response response; - wfp_jsonrpc_response_init(&response, wfp_impl_json_root(doc)); + wfp_jsonrpc_response_init(&response, doc.root()); ASSERT_EQ(WFP_BAD_FORMAT, response.error->code); ASSERT_EQ(nullptr, response.result); ASSERT_EQ(23, response.id); wfp_jsonrpc_response_cleanup(&response); - wfp_impl_json_dispose(doc); } diff --git a/test/webfuse_provider/jsonrpc/test_response_parser.cc b/test/webfuse_provider/jsonrpc/test_response_parser.cc index 018dd44..b93417b 100644 --- a/test/webfuse_provider/jsonrpc/test_response_parser.cc +++ b/test/webfuse_provider/jsonrpc/test_response_parser.cc @@ -1,61 +1,55 @@ #include "webfuse_provider/impl/jsonrpc/response_intern.h" #include "webfuse_provider/impl/jsonrpc/error.h" -#include "webfuse_provider/impl/json/parser.h" +#include "webfuse_provider/test_util/json_doc.hpp" #include +using webfuse_test::JsonDoc; TEST(response_parser, fail_no_object) { - char text[] = "[]"; - wfp_json_doc * doc = wfp_impl_json_parse(text); + JsonDoc doc("[]"); struct wfp_jsonrpc_response response; - wfp_jsonrpc_response_init(&response, wfp_impl_json_root(doc)); + wfp_jsonrpc_response_init(&response, doc.root()); 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_empty_object) { - char text[] = "{}"; - wfp_json_doc * doc = wfp_impl_json_parse(text); + JsonDoc doc("{}"); struct wfp_jsonrpc_response response; - wfp_jsonrpc_response_init(&response, wfp_impl_json_root(doc)); + wfp_jsonrpc_response_init(&response, doc.root()); 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); + JsonDoc doc("{\"id\":42}"); struct wfp_jsonrpc_response response; - wfp_jsonrpc_response_init(&response, wfp_impl_json_root(doc)); + wfp_jsonrpc_response_init(&response, doc.root()); ASSERT_NE(nullptr, response.error); ASSERT_EQ(42, response.id); ASSERT_EQ(nullptr, response.result); 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); + JsonDoc doc("{\"error\":{\"code\": 42}, \"id\": 42}"); struct wfp_jsonrpc_response response; - wfp_jsonrpc_response_init(&response, wfp_impl_json_root(doc)); + wfp_jsonrpc_response_init(&response, doc.root()); ASSERT_NE(nullptr, response.error); ASSERT_EQ(42, response.error->code); @@ -63,20 +57,17 @@ TEST(response_error, fail_with_custom_error_code) ASSERT_EQ(nullptr, response.result); 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); + JsonDoc doc("{\"result\": true, \"id\": 42}"); struct wfp_jsonrpc_response response; - wfp_jsonrpc_response_init(&response, wfp_impl_json_root(doc)); + wfp_jsonrpc_response_init(&response, doc.root()); 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); } diff --git a/test/webfuse_provider/provider/operation/test_close.cc b/test/webfuse_provider/provider/operation/test_close.cc index f4063eb..609cea1 100644 --- a/test/webfuse_provider/provider/operation/test_close.cc +++ b/test/webfuse_provider/provider/operation/test_close.cc @@ -1,12 +1,13 @@ #include "webfuse_provider/impl/operation/close.h" -#include "webfuse_provider/impl/json/parser.h" #include "webfuse_provider/mocks/mock_provider.hpp" #include "webfuse_provider/mocks/fake_invokation_context.hpp" +#include "webfuse_provider/test_util/json_doc.hpp" #include using ::webfuse_test::MockProvider; using ::webfuse_test::create_context; +using ::webfuse_test::JsonDoc; using ::testing::_; TEST(wfp_close, close) @@ -16,11 +17,9 @@ TEST(wfp_close, close) wfp_impl_invokation_context context = create_context(provider); - char params[] = "[\"test.filesystem\", 42, 101, 23]"; - wfp_json_doc * doc = wfp_impl_json_parse(params); + JsonDoc doc("[\"test.filesystem\", 42, 101, 23]"); - wfp_impl_close(&context, wfp_impl_json_root(doc), 42); - wfp_impl_json_dispose(doc); + wfp_impl_close(&context, doc.root(), 42); } TEST(wfp_close, close_fail_invalid_param_count) @@ -30,11 +29,9 @@ TEST(wfp_close, close_fail_invalid_param_count) wfp_impl_invokation_context context = create_context(provider); - char params[] = "[]"; - wfp_json_doc * doc = wfp_impl_json_parse(params); + JsonDoc doc("[]"); - wfp_impl_close(&context, wfp_impl_json_root(doc), 42); - wfp_impl_json_dispose(doc); + wfp_impl_close(&context, doc.root(), 42); } TEST(wfp_close, close_fail_inode_invalid_type) @@ -44,11 +41,9 @@ TEST(wfp_close, close_fail_inode_invalid_type) wfp_impl_invokation_context context = create_context(provider); - char params[] = "[\"test.filesystem\", \"42\", 0, 0]"; - wfp_json_doc * doc = wfp_impl_json_parse(params); + JsonDoc doc("[\"test.filesystem\", \"42\", 0, 0]"); - wfp_impl_close(&context, wfp_impl_json_root(doc), 42); - wfp_impl_json_dispose(doc); + wfp_impl_close(&context, doc.root(), 42); } TEST(wfp_close, close_fail_handle_invalid_type) @@ -58,11 +53,9 @@ TEST(wfp_close, close_fail_handle_invalid_type) wfp_impl_invokation_context context = create_context(provider); - char params[] = "[\"test.filesystem\", 0, \"42\", 0]"; - wfp_json_doc * doc = wfp_impl_json_parse(params); + JsonDoc doc("[\"test.filesystem\", 0, \"42\", 0]"); - wfp_impl_close(&context, wfp_impl_json_root(doc), 42); - wfp_impl_json_dispose(doc); + wfp_impl_close(&context, doc.root(), 42); } TEST(wfp_close, close_fail_flags_invalid_type) @@ -72,11 +65,9 @@ TEST(wfp_close, close_fail_flags_invalid_type) wfp_impl_invokation_context context = create_context(provider); - char params[] = "[\"test.filesystem\", 0, 0, \"42\"]"; - wfp_json_doc * doc = wfp_impl_json_parse(params); + JsonDoc doc("[\"test.filesystem\", 0, 0, \"42\"]"); - wfp_impl_close(&context, wfp_impl_json_root(doc), 42); - wfp_impl_json_dispose(doc); + wfp_impl_close(&context, doc.root(), 42); } diff --git a/test/webfuse_provider/provider/test_client.cc b/test/webfuse_provider/provider/test_client.cc index 1770cc3..64c7e7f 100644 --- a/test/webfuse_provider/provider/test_client.cc +++ b/test/webfuse_provider/provider/test_client.cc @@ -2,7 +2,7 @@ #include "webfuse_provider/test_util/webfuse_server.hpp" #include "webfuse_provider/mocks/mock_provider_client.hpp" #include "webfuse_provider/test_util/client.hpp" -#include "webfuse_provider/impl/json/parser.h" +#include "webfuse_provider/test_util/json_doc.hpp" #include "webfuse_provider/impl/json/node.h" #include @@ -12,6 +12,7 @@ using webfuse_test::WebfuseServer; using webfuse_test::MockProviderClient; using webfuse_test::Client; +using webfuse_test::JsonDoc; using testing::Invoke; using testing::_; using testing::StrEq; @@ -148,9 +149,8 @@ TEST(Client, Lookup) ASSERT_EQ(std::future_status::ready, connected.get_future().wait_for(TIMEOUT)); - std::string response_text = server.Lookup(1, "foo"); - wfp_json_doc * doc = wfp_impl_json_parse(const_cast(response_text.data())); - wfp_json const * response = wfp_impl_json_root(doc); + JsonDoc doc(server.Lookup(1, "foo")); + wfp_json const * response = doc.root(); ASSERT_TRUE(wfp_impl_json_is_object(response)); wfp_json const * result = wfp_impl_json_object_get(response, "result"); @@ -163,8 +163,6 @@ TEST(Client, Lookup) wfp_json const * type = wfp_impl_json_object_get(result, "type"); ASSERT_STREQ("file", wfp_impl_json_get_string(type)); - wfp_impl_json_dispose(doc); - client.Disconnect(); ASSERT_EQ(std::future_status::ready, disconnected.get_future().wait_for(TIMEOUT)); } @@ -198,9 +196,8 @@ TEST(Client, LookupFail) ASSERT_EQ(std::future_status::ready, connected.get_future().wait_for(TIMEOUT)); - std::string response_text = server.Lookup(1, "foo"); - wfp_json_doc * doc = wfp_impl_json_parse(const_cast(response_text.data())); - wfp_json const * response = wfp_impl_json_root(doc); + JsonDoc doc(server.Lookup(1, "foo")); + wfp_json const * response = doc.root(); ASSERT_TRUE(wfp_impl_json_is_object(response)); wfp_json const * error = wfp_impl_json_object_get(response, "error"); @@ -208,8 +205,6 @@ TEST(Client, LookupFail) wfp_json const * code = wfp_impl_json_object_get(error, "code"); ASSERT_NE(0, wfp_impl_json_get_int(code)); - wfp_impl_json_dispose(doc); - client.Disconnect(); ASSERT_EQ(std::future_status::ready, disconnected.get_future().wait_for(TIMEOUT)); } @@ -243,9 +238,8 @@ TEST(Client, Open) ASSERT_EQ(std::future_status::ready, connected.get_future().wait_for(TIMEOUT)); - std::string response_text = server.Open(1, 0); - wfp_json_doc * doc = wfp_impl_json_parse(const_cast(response_text.data())); - wfp_json const * response = wfp_impl_json_root(doc); + JsonDoc doc(server.Open(1, 0)); + wfp_json const * response = doc.root(); ASSERT_TRUE(wfp_impl_json_is_object(response)); wfp_json const * result = wfp_impl_json_object_get(response, "result"); @@ -253,8 +247,6 @@ TEST(Client, Open) wfp_json const * handle = wfp_impl_json_object_get(result, "handle"); ASSERT_EQ(4711, wfp_impl_json_get_int(handle)); - wfp_impl_json_dispose(doc); - client.Disconnect(); ASSERT_EQ(std::future_status::ready, disconnected.get_future().wait_for(TIMEOUT)); } @@ -289,9 +281,8 @@ TEST(Client, Read) ASSERT_EQ(std::future_status::ready, connected.get_future().wait_for(TIMEOUT)); - std::string response_text = server.Read(42, 5, 0, 1); - wfp_json_doc * doc = wfp_impl_json_parse(const_cast(response_text.data())); - wfp_json const * response = wfp_impl_json_root(doc); + JsonDoc doc(server.Read(42, 5, 0, 1)); + wfp_json const * response = doc.root(); ASSERT_TRUE(wfp_impl_json_is_object(response)); wfp_json const * result = wfp_impl_json_object_get(response, "result"); @@ -305,8 +296,6 @@ TEST(Client, Read) wfp_json const * data = wfp_impl_json_object_get(result, "data"); ASSERT_STREQ("Kg==", wfp_impl_json_get_string(data)); - wfp_impl_json_dispose(doc); - client.Disconnect(); ASSERT_EQ(std::future_status::ready, disconnected.get_future().wait_for(TIMEOUT)); } @@ -342,9 +331,8 @@ TEST(Client, ReadDir) ASSERT_EQ(std::future_status::ready, connected.get_future().wait_for(TIMEOUT)); - std::string response_text = server.ReadDir(42); - wfp_json_doc * doc = wfp_impl_json_parse(const_cast(response_text.data())); - wfp_json const * response = wfp_impl_json_root(doc); + JsonDoc doc(server.ReadDir(42)); + wfp_json const * response = doc.root(); ASSERT_TRUE(wfp_impl_json_is_object(response)); wfp_json const * result = wfp_impl_json_object_get(response, "result"); @@ -352,8 +340,6 @@ TEST(Client, ReadDir) ASSERT_TRUE(wfp_impl_json_is_array(result)); ASSERT_EQ(3, wfp_impl_json_array_size(result)); - wfp_impl_json_dispose(doc); - client.Disconnect(); ASSERT_EQ(std::future_status::ready, disconnected.get_future().wait_for(TIMEOUT)); } diff --git a/test/webfuse_provider/provider/test_client_protocol.cc b/test/webfuse_provider/provider/test_client_protocol.cc index 1a796c9..fa41cb4 100644 --- a/test/webfuse_provider/provider/test_client_protocol.cc +++ b/test/webfuse_provider/provider/test_client_protocol.cc @@ -7,7 +7,7 @@ #include "webfuse_provider/mocks/mock_provider_client.hpp" #include "webfuse_provider/protocol_names.h" #include "webfuse_provider/test_util/timeout_watcher.hpp" -#include "webfuse_provider/impl/json/parser.h" +#include "webfuse_provider/test_util/json_doc.hpp" #include "webfuse_provider/impl/json/node.h" #include @@ -20,6 +20,7 @@ using webfuse_test::WsServer; using webfuse_test::MockProviderClient; using webfuse_test::IProviderClient; using webfuse_test::TimeoutWatcher; +using webfuse_test::JsonDoc; using testing::_; using testing::AtMost; using testing::Invoke; @@ -102,9 +103,8 @@ public: std::string const & expected_username, std::string const & expected_password) { - std::string request_text = ReceiveMessageFromClient(); - wfp_json_doc * doc = wfp_impl_json_parse(const_cast(request_text.data())); - wfp_json const * request = wfp_impl_json_root(doc); + JsonDoc doc(ReceiveMessageFromClient()); + wfp_json const * request = doc.root(); ASSERT_TRUE(wfp_impl_json_is_object(request)); wfp_json const * method = wfp_impl_json_object_get(request, "method"); @@ -136,15 +136,12 @@ public: std::ostringstream response; response << "{\"result\": {}, \"id\": " << wfp_impl_json_get_int(id) << "}"; SendToClient(response.str()); - - wfp_impl_json_dispose(doc); } void AwaitAddFilesystem(std::string& filesystemName) { - std::string request_text = ReceiveMessageFromClient(); - wfp_json_doc * doc = wfp_impl_json_parse(const_cast(request_text.data())); - wfp_json const * request = wfp_impl_json_root(doc); + JsonDoc doc(ReceiveMessageFromClient()); + wfp_json const * request = doc.root(); ASSERT_TRUE(wfp_impl_json_is_object(request)); wfp_json const * method = wfp_impl_json_object_get(request, "method"); @@ -165,8 +162,6 @@ public: response << "{\"result\": {\"id\": \"" << wfp_impl_json_get_string(filesystem) << "\"}, \"id\": " << wfp_impl_json_get_int(id) << "}"; SendToClient(response.str()); - - wfp_impl_json_dispose(doc); } private: