From 63cc5b5388223f383fc0f4a28c3ca45a2e05f794 Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Sun, 12 Jul 2020 00:39:55 +0200 Subject: [PATCH] refactor: replaced jansson by own json parser implementation --- lib/webfuse_provider/impl/client_protocol.c | 19 ++-- lib/webfuse_provider/impl/json/node.c | 10 +- lib/webfuse_provider/impl/json/parser.c | 9 ++ lib/webfuse_provider/impl/json/parser.h | 4 + lib/webfuse_provider/impl/jsonrpc/error.c | 28 +++-- lib/webfuse_provider/impl/jsonrpc/error.h | 15 ++- lib/webfuse_provider/impl/jsonrpc/proxy.c | 2 +- lib/webfuse_provider/impl/jsonrpc/proxy.h | 4 +- .../impl/jsonrpc/proxy_finished_fn.h | 9 +- lib/webfuse_provider/impl/jsonrpc/request.c | 15 ++- lib/webfuse_provider/impl/jsonrpc/request.h | 6 +- lib/webfuse_provider/impl/jsonrpc/response.c | 61 +++++----- lib/webfuse_provider/impl/jsonrpc/response.h | 9 +- .../impl/jsonrpc/response_intern.h | 9 +- lib/webfuse_provider/impl/operation/close.c | 23 ++-- lib/webfuse_provider/impl/operation/close.h | 2 +- lib/webfuse_provider/impl/operation/getattr.c | 11 +- lib/webfuse_provider/impl/operation/getattr.h | 2 +- lib/webfuse_provider/impl/operation/lookup.c | 17 +-- lib/webfuse_provider/impl/operation/lookup.h | 2 +- lib/webfuse_provider/impl/operation/open.c | 17 +-- lib/webfuse_provider/impl/operation/open.h | 2 +- lib/webfuse_provider/impl/operation/read.c | 29 ++--- lib/webfuse_provider/impl/operation/read.h | 2 +- lib/webfuse_provider/impl/operation/readdir.c | 11 +- lib/webfuse_provider/impl/operation/readdir.h | 2 +- lib/webfuse_provider/impl/provider.c | 27 +++-- lib/webfuse_provider/impl/provider.h | 5 +- .../jsonrpc/test_is_request.cc | 105 ++++++++---------- .../jsonrpc/test_is_response.cc | 71 ++++++------ test/webfuse_provider/jsonrpc/test_proxy.cc | 64 +++++------ test/webfuse_provider/jsonrpc/test_request.cc | 30 +++-- .../webfuse_provider/jsonrpc/test_response.cc | 101 ++++++++--------- .../jsonrpc/test_response_parser.cc | 76 ++++++++----- .../provider/operation/test_close.cc | 59 ++++------ 35 files changed, 439 insertions(+), 419 deletions(-) diff --git a/lib/webfuse_provider/impl/client_protocol.c b/lib/webfuse_provider/impl/client_protocol.c index 9379450..cd7830a 100644 --- a/lib/webfuse_provider/impl/client_protocol.c +++ b/lib/webfuse_provider/impl/client_protocol.c @@ -4,7 +4,6 @@ #include #include -#include #include "webfuse_provider/impl/client_config.h" #include "webfuse_provider/impl/provider.h" @@ -21,6 +20,7 @@ #include "webfuse_provider/impl/jsonrpc/response.h" #include "webfuse_provider/impl/jsonrpc/request.h" #include "webfuse_provider/impl/jsonrpc/proxy.h" +#include "webfuse_provider/impl/json/parser.h" #define WFP_DEFAULT_TIMEOUT (10 * 1000) @@ -36,12 +36,13 @@ static void wfp_impl_client_protocol_respond( static void wfp_impl_client_protocol_process( struct wfp_client_protocol * protocol, - char const * data, + char * data, size_t length) { - json_t * message = json_loadb(data, length, 0, NULL); - if (NULL != message) + struct wfp_json_doc * doc = wfp_impl_json_parse_buffer(data, length); + if (NULL != doc) { + struct wfp_json const * message = wfp_impl_json_root(doc); if (wfp_jsonrpc_is_response(message)) { wfp_jsonrpc_proxy_onresult(protocol->proxy, message); @@ -59,15 +60,15 @@ static void wfp_impl_client_protocol_process( wfp_impl_provider_invoke(&context, message); } - json_decref(message); + wfp_impl_json_dispose(doc); } } static void wfp_impl_client_protocol_on_add_filesystem_finished( void * user_data, - json_t const * result, - json_t const * WFP_UNUSED_PARAM(error)) + struct wfp_json const * result, + struct wfp_jsonrpc_error const * WFP_UNUSED_PARAM(error)) { struct wfp_client_protocol * protocol = user_data; if (NULL == protocol->wsi) { return; } @@ -99,8 +100,8 @@ static void wfp_impl_client_protocol_add_filesystem( static void wfp_impl_client_protocol_on_authenticate_finished( void * user_data, - json_t const * result, - json_t const * WFP_UNUSED_PARAM(error)) + struct wfp_json const * result, + struct wfp_jsonrpc_error const * WFP_UNUSED_PARAM(error)) { struct wfp_client_protocol * protocol = user_data; if (NULL == protocol->wsi) { return; } diff --git a/lib/webfuse_provider/impl/json/node.c b/lib/webfuse_provider/impl/json/node.c index f9a323a..37ba7b0 100644 --- a/lib/webfuse_provider/impl/json/node.c +++ b/lib/webfuse_provider/impl/json/node.c @@ -8,35 +8,35 @@ bool wfp_impl_json_is_bool( struct wfp_json const * json) { - return (WFP_JSON_BOOL == json->type); + return ((NULL != json) &&(WFP_JSON_BOOL == json->type)); } bool wfp_impl_json_is_int( struct wfp_json const * json) { - return (WFP_JSON_INT == json->type); + return ((NULL != json) && (WFP_JSON_INT == json->type)); } bool wfp_impl_json_is_string( struct wfp_json const * json) { - return (WFP_JSON_STRING == json->type); + return ((NULL != json) && (WFP_JSON_STRING == json->type)); } bool wfp_impl_json_is_array( struct wfp_json const * json) { - return (WFP_JSON_ARRAY == json->type); + return ((NULL != json) && (WFP_JSON_ARRAY == json->type)); } bool wfp_impl_json_is_object( struct wfp_json const * json) { - return (WFP_JSON_OBJECT == json->type); + return ((NULL != json) && (WFP_JSON_OBJECT == json->type)); } bool diff --git a/lib/webfuse_provider/impl/json/parser.c b/lib/webfuse_provider/impl/json/parser.c index 2ab7348..a858bfc 100644 --- a/lib/webfuse_provider/impl/json/parser.c +++ b/lib/webfuse_provider/impl/json/parser.c @@ -3,6 +3,7 @@ #include "webfuse_provider/impl/json/reader.h" #include +#include #define WFP_IMPL_JSON_DEFAULT_CAPACITY 4 @@ -47,6 +48,14 @@ wfp_impl_json_parse_object( struct wfp_json_reader * reader, struct wfp_json * json); + +extern struct wfp_json_doc * +wfp_impl_json_parse( + char * data) +{ + return wfp_impl_json_parse_buffer(data, strlen(data)); +} + struct wfp_json_doc * wfp_impl_json_parse_buffer( char * data, diff --git a/lib/webfuse_provider/impl/json/parser.h b/lib/webfuse_provider/impl/json/parser.h index 12ba56e..857f0ee 100644 --- a/lib/webfuse_provider/impl/json/parser.h +++ b/lib/webfuse_provider/impl/json/parser.h @@ -15,6 +15,10 @@ extern "C" struct wfp_json; struct wfp_json_doc; +extern struct wfp_json_doc * +wfp_impl_json_parse( + char * data); + extern struct wfp_json_doc * wfp_impl_json_parse_buffer( char * data, diff --git a/lib/webfuse_provider/impl/jsonrpc/error.c b/lib/webfuse_provider/impl/jsonrpc/error.c index abe8a71..f36944a 100644 --- a/lib/webfuse_provider/impl/jsonrpc/error.c +++ b/lib/webfuse_provider/impl/jsonrpc/error.c @@ -1,17 +1,28 @@ #include "webfuse_provider/impl/jsonrpc/error.h" -json_t * -wfp_jsonrpc_error( +#include +#include + +struct wfp_jsonrpc_error * +wfp_jsonrpc_error_create( int code, char const * message) { - json_t * error = json_object(); - json_object_set_new(error, "code", json_integer(code)); - json_object_set_new(error, "message", json_string(message)); + struct wfp_jsonrpc_error * error = malloc(sizeof(struct wfp_jsonrpc_error)); + error->code = code; + error->message = strdup(message); return error; } +void +wfp_jsonrpc_error_dispose( + struct wfp_jsonrpc_error * error) +{ + free(error->message); + free(error); +} + void wfp_jsonrpc_propate_error( wfp_jsonrpc_proxy_finished_fn * finised, @@ -19,9 +30,10 @@ wfp_jsonrpc_propate_error( int code, char const * message) { - json_t * error = wfp_jsonrpc_error(code, message); - finised(user_data, NULL, error); + struct wfp_jsonrpc_error error; + error.code = code; + error.message = (char*) message; - json_decref(error); + finised(user_data, NULL, &error); } diff --git a/lib/webfuse_provider/impl/jsonrpc/error.h b/lib/webfuse_provider/impl/jsonrpc/error.h index dd54f0a..31576fc 100644 --- a/lib/webfuse_provider/impl/jsonrpc/error.h +++ b/lib/webfuse_provider/impl/jsonrpc/error.h @@ -1,7 +1,6 @@ #ifndef WFP_JSONRPC_ERROR_H #define WFP_JSONRPC_ERROR_H -#include #include "webfuse_provider/impl/jsonrpc/proxy_finished_fn.h" #ifdef __cplusplus @@ -9,11 +8,21 @@ extern "C" { #endif -extern json_t * -wfp_jsonrpc_error( +struct wfp_jsonrpc_error +{ + int code; + char * message; +}; + +extern struct wfp_jsonrpc_error * +wfp_jsonrpc_error_create( int code, char const * message); +extern void +wfp_jsonrpc_error_dispose( + struct wfp_jsonrpc_error * error); + extern void wfp_jsonrpc_propate_error( wfp_jsonrpc_proxy_finished_fn * finised, diff --git a/lib/webfuse_provider/impl/jsonrpc/proxy.c b/lib/webfuse_provider/impl/jsonrpc/proxy.c index 62a1175..11be6ad 100644 --- a/lib/webfuse_provider/impl/jsonrpc/proxy.c +++ b/lib/webfuse_provider/impl/jsonrpc/proxy.c @@ -183,7 +183,7 @@ extern void wfp_jsonrpc_proxy_vnotify( void wfp_jsonrpc_proxy_onresult( struct wfp_jsonrpc_proxy * proxy, - json_t * message) + struct wfp_json const * message) { struct wfp_jsonrpc_response response; wfp_jsonrpc_response_init(&response, message); diff --git a/lib/webfuse_provider/impl/jsonrpc/proxy.h b/lib/webfuse_provider/impl/jsonrpc/proxy.h index 04abdd0..6bf230d 100644 --- a/lib/webfuse_provider/impl/jsonrpc/proxy.h +++ b/lib/webfuse_provider/impl/jsonrpc/proxy.h @@ -11,7 +11,6 @@ using std::size_t; #endif -#include #include "webfuse_provider/impl/jsonrpc/send_fn.h" #include "webfuse_provider/impl/jsonrpc/proxy_finished_fn.h" @@ -22,6 +21,7 @@ extern "C" { struct wfp_jsonrpc_proxy; struct wfp_timer_manager; struct wfp_json_writer; +struct wfp_json; typedef void wfp_jsonrpc_custom_write_fn( @@ -70,7 +70,7 @@ extern void wfp_jsonrpc_proxy_notify( extern void wfp_jsonrpc_proxy_onresult( struct wfp_jsonrpc_proxy * proxy, - json_t * message); + struct wfp_json const * message); #ifdef __cplusplus } diff --git a/lib/webfuse_provider/impl/jsonrpc/proxy_finished_fn.h b/lib/webfuse_provider/impl/jsonrpc/proxy_finished_fn.h index 6d2b0d3..03523ab 100644 --- a/lib/webfuse_provider/impl/jsonrpc/proxy_finished_fn.h +++ b/lib/webfuse_provider/impl/jsonrpc/proxy_finished_fn.h @@ -1,17 +1,18 @@ #ifndef WFP_JSONRPC_PROXY_FINISHED_FN_H #define WFP_JSONRPC_PROXY_FINISHED_FN_H -#include - #ifdef __cplusplus extern "C" { #endif +struct wfp_json; +struct wfp_jsonrpc_error; + typedef void wfp_jsonrpc_proxy_finished_fn( void * user_data, - json_t const * result, - json_t const * error); + struct wfp_json const * result, + struct wfp_jsonrpc_error const * error); #ifdef __cplusplus } diff --git a/lib/webfuse_provider/impl/jsonrpc/request.c b/lib/webfuse_provider/impl/jsonrpc/request.c index 8caf7a1..768bc9f 100644 --- a/lib/webfuse_provider/impl/jsonrpc/request.c +++ b/lib/webfuse_provider/impl/jsonrpc/request.c @@ -1,14 +1,17 @@ #include "webfuse_provider/impl/jsonrpc/request.h" +#include "webfuse_provider/impl/json/node.h" #include bool wfp_jsonrpc_is_request( - json_t * message) + struct wfp_json const * message) { - json_t * id = json_object_get(message, "id"); - json_t * method = json_object_get(message, "method"); - json_t * params = json_object_get(message, "params"); + if (NULL == message) { return false; } - return (json_is_integer(id) && json_is_string(method) && - (json_is_array(params) || json_is_object(params))); + struct wfp_json const * id = wfp_impl_json_object_get(message, "id"); + struct wfp_json const * method = wfp_impl_json_object_get(message, "method"); + struct wfp_json const * params = wfp_impl_json_object_get(message, "params"); + + return (wfp_impl_json_is_int(id) && wfp_impl_json_is_string(method) && + (wfp_impl_json_is_array(params) || wfp_impl_json_is_object(params))); } diff --git a/lib/webfuse_provider/impl/jsonrpc/request.h b/lib/webfuse_provider/impl/jsonrpc/request.h index 5a33986..5f26d82 100644 --- a/lib/webfuse_provider/impl/jsonrpc/request.h +++ b/lib/webfuse_provider/impl/jsonrpc/request.h @@ -5,15 +5,15 @@ #include #endif -#include - #ifdef __cplusplus extern "C" { #endif +struct wfp_json; + extern bool wfp_jsonrpc_is_request( - json_t * message); + struct wfp_json const * message); #ifdef __cplusplus } diff --git a/lib/webfuse_provider/impl/jsonrpc/response.c b/lib/webfuse_provider/impl/jsonrpc/response.c index acc7b49..3b225ca 100644 --- a/lib/webfuse_provider/impl/jsonrpc/response.c +++ b/lib/webfuse_provider/impl/jsonrpc/response.c @@ -1,53 +1,57 @@ #include "webfuse_provider/impl/jsonrpc/response_intern.h" #include "webfuse_provider/impl/jsonrpc/error.h" #include "webfuse_provider/status.h" +#include "webfuse_provider/impl/json/node.h" bool wfp_jsonrpc_is_response( - json_t * message) + struct wfp_json const * message) { - json_t * id = json_object_get(message, "id"); - json_t * err = json_object_get(message, "error"); - json_t * result = json_object_get(message, "result"); + if (NULL == message) { return false; } - return (json_is_integer(id) && - (json_is_object(err) || (NULL != result))); + struct wfp_json const * id = wfp_impl_json_object_get(message, "id"); + struct wfp_json const * err = wfp_impl_json_object_get(message, "error"); + struct wfp_json const * result = wfp_impl_json_object_get(message, "result"); + + return (wfp_impl_json_is_int(id) && + (wfp_impl_json_is_object(err) || (NULL != result))); } void wfp_jsonrpc_response_init( struct wfp_jsonrpc_response * result, - json_t * response) + struct wfp_json const * response) { result->id = -1; result->result = NULL; result->error = NULL; - json_t * id_holder = json_object_get(response, "id"); - if (!json_is_integer(id_holder)) + struct wfp_json const * id_holder = wfp_impl_json_object_get(response, "id"); + if (!wfp_impl_json_is_int(id_holder)) { - result->error = wfp_jsonrpc_error(WFP_BAD_FORMAT, "invalid format: missing id"); + result->error = wfp_jsonrpc_error_create(WFP_BAD_FORMAT, "invalid format: missing id"); return; } - result->id = json_integer_value(id_holder); - result->result = json_object_get(response, "result"); - if (NULL != result->result) - { - json_incref(result->result); - } - else + result->id = wfp_impl_json_get_int(id_holder); + result->result = wfp_impl_json_object_get(response, "result"); + if (NULL == result->result) { - json_t * error = json_object_get(response, "error"); - if ((json_is_object(error)) && (json_is_integer(json_object_get(error, "code")))) + struct wfp_json const * error = wfp_impl_json_object_get(response, "error"); + if ((wfp_impl_json_is_object(error)) && (wfp_impl_json_is_int(wfp_impl_json_object_get(error, "code")))) { - result->error = error; - json_incref(result->error); + int code = wfp_impl_json_get_int(wfp_impl_json_object_get(error, "code")); + char const * message = ""; + if (wfp_impl_json_is_string(wfp_impl_json_object_get(error, "message"))) + { + message = wfp_impl_json_get_string(wfp_impl_json_object_get(error, "message")); + } + result->error = wfp_jsonrpc_error_create(code, message); } else { - result->error = wfp_jsonrpc_error(WFP_BAD_FORMAT, "invalid format: invalid error object"); + result->error = wfp_jsonrpc_error_create(WFP_BAD_FORMAT, "invalid format: invalid error object"); } } } @@ -56,13 +60,8 @@ void wfp_jsonrpc_response_cleanup( struct wfp_jsonrpc_response * response) { - if (NULL != response->result) - { - json_decref(response->result); - } - - if (NULL != response->error) - { - json_decref(response->error); - } + if (NULL != response->error) + { + wfp_jsonrpc_error_dispose(response->error); + } } diff --git a/lib/webfuse_provider/impl/jsonrpc/response.h b/lib/webfuse_provider/impl/jsonrpc/response.h index eae5cb8..94e9161 100644 --- a/lib/webfuse_provider/impl/jsonrpc/response.h +++ b/lib/webfuse_provider/impl/jsonrpc/response.h @@ -5,15 +5,16 @@ #include #endif -#include - #ifdef __cplusplus extern "C" { #endif -extern bool wfp_jsonrpc_is_response( - json_t * message); +struct wfp_json; + +extern bool +wfp_jsonrpc_is_response( + struct wfp_json const * message); #ifdef __cplusplus } diff --git a/lib/webfuse_provider/impl/jsonrpc/response_intern.h b/lib/webfuse_provider/impl/jsonrpc/response_intern.h index e03dea7..012fbd3 100644 --- a/lib/webfuse_provider/impl/jsonrpc/response_intern.h +++ b/lib/webfuse_provider/impl/jsonrpc/response_intern.h @@ -14,16 +14,19 @@ using std::size_t; extern "C" { #endif +struct wfp_json; +struct wfp_jsonrpc_error; + struct wfp_jsonrpc_response { - json_t * result; - json_t * error; + struct wfp_json const * result; + struct wfp_jsonrpc_error * error; int id; }; extern void wfp_jsonrpc_response_init( struct wfp_jsonrpc_response * response, - json_t * message); + struct wfp_json const * message); extern void wfp_jsonrpc_response_cleanup( struct wfp_jsonrpc_response * response); diff --git a/lib/webfuse_provider/impl/operation/close.c b/lib/webfuse_provider/impl/operation/close.c index 3b87a49..cd864bf 100644 --- a/lib/webfuse_provider/impl/operation/close.c +++ b/lib/webfuse_provider/impl/operation/close.c @@ -1,26 +1,27 @@ #include "webfuse_provider/impl/operation/close.h" #include #include "webfuse_provider/impl/util/util.h" +#include "webfuse_provider/impl/json/node.h" void wfp_impl_close( struct wfp_impl_invokation_context * context, - json_t * params, + struct wfp_json const * params, int WFP_UNUSED_PARAM(id)) { - size_t const param_count = json_array_size(params); + size_t const param_count = wfp_impl_json_array_size(params); if (4 == param_count) { - json_t * inode_holder = json_array_get(params, 1); - json_t * handle_holder = json_array_get(params, 2); - json_t * flags_holder = json_array_get(params, 3); + struct wfp_json const * inode_holder = wfp_impl_json_array_get(params, 1); + struct wfp_json const * handle_holder = wfp_impl_json_array_get(params, 2); + struct wfp_json const * flags_holder = wfp_impl_json_array_get(params, 3); - if (json_is_integer(inode_holder) && - json_is_integer(handle_holder) && - json_is_integer(flags_holder)) + if (wfp_impl_json_is_int(inode_holder) && + wfp_impl_json_is_int(handle_holder) && + wfp_impl_json_is_int(flags_holder)) { - ino_t inode = (ino_t) json_integer_value(inode_holder); - uint32_t handle = (uint32_t) (json_integer_value(handle_holder) & UINT32_MAX); - int flags = json_integer_value(flags_holder); + ino_t inode = (ino_t) wfp_impl_json_get_int(inode_holder); + uint32_t handle = (uint32_t) (wfp_impl_json_get_int(handle_holder) & UINT32_MAX); + int flags = wfp_impl_json_get_int(flags_holder); context->provider->close(inode, handle, flags, context->user_data); } diff --git a/lib/webfuse_provider/impl/operation/close.h b/lib/webfuse_provider/impl/operation/close.h index 7c6fd7b..f769906 100644 --- a/lib/webfuse_provider/impl/operation/close.h +++ b/lib/webfuse_provider/impl/operation/close.h @@ -10,7 +10,7 @@ extern "C" extern void wfp_impl_close( struct wfp_impl_invokation_context * context, - json_t * params, + struct wfp_json const * params, int id); extern void wfp_impl_close_default( diff --git a/lib/webfuse_provider/impl/operation/getattr.c b/lib/webfuse_provider/impl/operation/getattr.c index 5f64ff8..dbd6c98 100644 --- a/lib/webfuse_provider/impl/operation/getattr.c +++ b/lib/webfuse_provider/impl/operation/getattr.c @@ -6,21 +6,22 @@ #include "webfuse_provider/impl/request.h" #include "webfuse_provider/impl/message_writer.h" #include "webfuse_provider/impl/util/util.h" +#include "webfuse_provider/impl/json/node.h" void wfp_impl_getattr( struct wfp_impl_invokation_context * context, - json_t * params, + struct wfp_json const * params, int id) { - size_t const count = json_array_size(params); + size_t const count = wfp_impl_json_array_size(params); if (2 == count) { - json_t * inode_holder = json_array_get(params, 1); + struct wfp_json const * inode_holder = wfp_impl_json_array_get(params, 1); - if (json_is_integer(inode_holder)) + if (wfp_impl_json_is_int(inode_holder)) { - ino_t inode = (ino_t) json_integer_value(inode_holder); + ino_t inode = (ino_t) wfp_impl_json_get_int(inode_holder); struct wfp_request * request = wfp_impl_request_create(context->request, id); context->provider->getattr(request, inode, context->user_data); diff --git a/lib/webfuse_provider/impl/operation/getattr.h b/lib/webfuse_provider/impl/operation/getattr.h index a14f8de..fd65c94 100644 --- a/lib/webfuse_provider/impl/operation/getattr.h +++ b/lib/webfuse_provider/impl/operation/getattr.h @@ -14,7 +14,7 @@ extern void wfp_impl_respond_getattr( extern void wfp_impl_getattr( struct wfp_impl_invokation_context * context, - json_t * params, + struct wfp_json const * params, int id); extern void wfp_impl_getattr_default( diff --git a/lib/webfuse_provider/impl/operation/lookup.c b/lib/webfuse_provider/impl/operation/lookup.c index bb3ff27..1097843 100644 --- a/lib/webfuse_provider/impl/operation/lookup.c +++ b/lib/webfuse_provider/impl/operation/lookup.c @@ -6,23 +6,24 @@ #include "webfuse_provider/impl/request.h" #include "webfuse_provider/impl/message_writer.h" #include "webfuse_provider/impl/util/util.h" +#include "webfuse_provider/impl/json/node.h" void wfp_impl_lookup( struct wfp_impl_invokation_context * context, - json_t * params, + struct wfp_json const * params, int id) { - size_t const count = json_array_size(params); + size_t const count = wfp_impl_json_array_size(params); if (3 == count) { - json_t * inode_holder = json_array_get(params, 1); - json_t * name_holder = json_array_get(params, 2); + struct wfp_json const * inode_holder = wfp_impl_json_array_get(params, 1); + struct wfp_json const * name_holder = wfp_impl_json_array_get(params, 2); - if (json_is_integer(inode_holder) && - json_is_string(name_holder)) + if (wfp_impl_json_is_int(inode_holder) && + wfp_impl_json_is_string(name_holder)) { - ino_t inode = json_integer_value(inode_holder); - char const * name = json_string_value(name_holder); + ino_t inode = wfp_impl_json_get_int(inode_holder); + char const * name = wfp_impl_json_get_string(name_holder); struct wfp_request * request = wfp_impl_request_create(context->request, id); context->provider->lookup(request, inode, name, context->user_data); diff --git a/lib/webfuse_provider/impl/operation/lookup.h b/lib/webfuse_provider/impl/operation/lookup.h index c4caf6c..67c451a 100644 --- a/lib/webfuse_provider/impl/operation/lookup.h +++ b/lib/webfuse_provider/impl/operation/lookup.h @@ -14,7 +14,7 @@ extern void wfp_impl_respond_lookup( extern void wfp_impl_lookup( struct wfp_impl_invokation_context * context, - json_t * params, + struct wfp_json const * params, int id); extern void wfp_impl_lookup_default( diff --git a/lib/webfuse_provider/impl/operation/open.c b/lib/webfuse_provider/impl/operation/open.c index e74c07b..6639ab8 100644 --- a/lib/webfuse_provider/impl/operation/open.c +++ b/lib/webfuse_provider/impl/operation/open.c @@ -3,23 +3,24 @@ #include "webfuse_provider/impl/request.h" #include "webfuse_provider/impl/message_writer.h" #include "webfuse_provider/impl/util/util.h" +#include "webfuse_provider/impl/json/node.h" void wfp_impl_open( struct wfp_impl_invokation_context * context, - json_t * params, + struct wfp_json const * params, int id) { - size_t const count = json_array_size(params); + size_t const count = wfp_impl_json_array_size(params); if (3 == count) { - json_t * inode_holder = json_array_get(params, 1); - json_t * flags_holder = json_array_get(params, 2); + struct wfp_json const * inode_holder = wfp_impl_json_array_get(params, 1); + struct wfp_json const * flags_holder = wfp_impl_json_array_get(params, 2); - if (json_is_integer(inode_holder) && - json_is_integer(flags_holder)) + if (wfp_impl_json_is_int(inode_holder) && + wfp_impl_json_is_int(flags_holder)) { - ino_t inode = (ino_t) json_integer_value(inode_holder); - int flags = (ino_t) json_integer_value(flags_holder); + ino_t inode = (ino_t) wfp_impl_json_get_int(inode_holder); + int flags = (ino_t) wfp_impl_json_get_int(flags_holder); struct wfp_request * request = wfp_impl_request_create(context->request, id); diff --git a/lib/webfuse_provider/impl/operation/open.h b/lib/webfuse_provider/impl/operation/open.h index 052d185..637bebe 100644 --- a/lib/webfuse_provider/impl/operation/open.h +++ b/lib/webfuse_provider/impl/operation/open.h @@ -14,7 +14,7 @@ extern void wfp_impl_respond_open( extern void wfp_impl_open( struct wfp_impl_invokation_context * context, - json_t * params, + struct wfp_json const * params, int id); extern void wfp_impl_open_default( diff --git a/lib/webfuse_provider/impl/operation/read.c b/lib/webfuse_provider/impl/operation/read.c index 3f0b698..7478b05 100644 --- a/lib/webfuse_provider/impl/operation/read.c +++ b/lib/webfuse_provider/impl/operation/read.c @@ -6,29 +6,30 @@ #include "webfuse_provider/impl/request.h" #include "webfuse_provider/impl/message_writer.h" #include "webfuse_provider/impl/util/util.h" +#include "webfuse_provider/impl/json/node.h" void wfp_impl_read( struct wfp_impl_invokation_context * context, - json_t * params, + struct wfp_json const * params, int id) { - size_t const count = json_array_size(params); + size_t const count = wfp_impl_json_array_size(params); if (5 == count) { - json_t * inode_holder = json_array_get(params, 1); - json_t * handle_holder = json_array_get(params, 2); - json_t * offset_holder = json_array_get(params, 3); - json_t * length_holder = json_array_get(params, 4); + struct wfp_json const * inode_holder = wfp_impl_json_array_get(params, 1); + struct wfp_json const * handle_holder = wfp_impl_json_array_get(params, 2); + struct wfp_json const * offset_holder = wfp_impl_json_array_get(params, 3); + struct wfp_json const * length_holder = wfp_impl_json_array_get(params, 4); - if (json_is_integer(inode_holder) && - json_is_integer(handle_holder) && - json_is_integer(offset_holder) && - json_is_integer(length_holder)) + if (wfp_impl_json_is_int(inode_holder) && + wfp_impl_json_is_int(handle_holder) && + wfp_impl_json_is_int(offset_holder) && + wfp_impl_json_is_int(length_holder)) { - ino_t inode = (ino_t) json_integer_value(inode_holder); - int handle = json_integer_value(handle_holder); - size_t offset = json_integer_value(offset_holder); - size_t length = json_integer_value(length_holder); + ino_t inode = (ino_t) wfp_impl_json_get_int(inode_holder); + int handle = wfp_impl_json_get_int(handle_holder); + size_t offset = wfp_impl_json_get_int(offset_holder); + size_t length = wfp_impl_json_get_int(length_holder); struct wfp_request * request = wfp_impl_request_create(context->request, id); context->provider->read(request, inode, handle, offset, length, context->user_data); diff --git a/lib/webfuse_provider/impl/operation/read.h b/lib/webfuse_provider/impl/operation/read.h index be6f734..bf8f189 100644 --- a/lib/webfuse_provider/impl/operation/read.h +++ b/lib/webfuse_provider/impl/operation/read.h @@ -15,7 +15,7 @@ extern void wfp_impl_respond_read( extern void wfp_impl_read( struct wfp_impl_invokation_context * context, - json_t * params, + struct wfp_json const * params, int id); extern void wfp_impl_read_default( diff --git a/lib/webfuse_provider/impl/operation/readdir.c b/lib/webfuse_provider/impl/operation/readdir.c index 0e7d0ae..5c312f4 100644 --- a/lib/webfuse_provider/impl/operation/readdir.c +++ b/lib/webfuse_provider/impl/operation/readdir.c @@ -4,20 +4,21 @@ #include "webfuse_provider/impl/request.h" #include "webfuse_provider/impl/message_writer.h" #include "webfuse_provider/impl/util/util.h" +#include "webfuse_provider/impl/json/node.h" void wfp_impl_readdir( struct wfp_impl_invokation_context * context, - json_t * params, + struct wfp_json const * params, int id) { - size_t const count = json_array_size(params); + size_t const count = wfp_impl_json_array_size(params); if (2 == count) { - json_t * inode_holder = json_array_get(params, 1); + struct wfp_json const * inode_holder = wfp_impl_json_array_get(params, 1); - if (json_is_integer(inode_holder)) + if (wfp_impl_json_is_int(inode_holder)) { - ino_t inode = (ino_t) json_integer_value(inode_holder); + ino_t inode = (ino_t) wfp_impl_json_get_int(inode_holder); struct wfp_request * request = wfp_impl_request_create(context->request, id); context->provider->readdir(request, inode, context->user_data); diff --git a/lib/webfuse_provider/impl/operation/readdir.h b/lib/webfuse_provider/impl/operation/readdir.h index 8884e9e..fa9d53e 100644 --- a/lib/webfuse_provider/impl/operation/readdir.h +++ b/lib/webfuse_provider/impl/operation/readdir.h @@ -14,7 +14,7 @@ extern void wfp_impl_respond_readdir( extern void wfp_impl_readdir( struct wfp_impl_invokation_context * context, - json_t * params, + struct wfp_json const * params, int id); extern void wfp_impl_readdir_default( diff --git a/lib/webfuse_provider/impl/provider.c b/lib/webfuse_provider/impl/provider.c index 1042802..88a57da 100644 --- a/lib/webfuse_provider/impl/provider.c +++ b/lib/webfuse_provider/impl/provider.c @@ -1,8 +1,5 @@ #include "webfuse_provider/impl/provider.h" -#include -#include - #include "webfuse_provider/impl/request.h" #include "webfuse_provider/impl/operation/lookup.h" #include "webfuse_provider/impl/operation/getattr.h" @@ -10,10 +7,16 @@ #include "webfuse_provider/impl/operation/open.h" #include "webfuse_provider/impl/operation/close.h" #include "webfuse_provider/impl/operation/read.h" +#include "webfuse_provider/impl/json/node.h" + +#include +#include + +struct wfp_json; typedef void wfp_impl_invoke_fn( struct wfp_impl_invokation_context * context, - json_t * params, + struct wfp_json const * params, int id); @@ -27,7 +30,7 @@ struct wfp_impl_method static void wfp_impl_provider_invoke_method( struct wfp_impl_invokation_context * context, char const * method_name, - json_t * params, + struct wfp_json const * params, int id) { static struct wfp_impl_method const methods[] = @@ -86,16 +89,16 @@ void wfp_impl_provider_init_from_prototype( void wfp_impl_provider_invoke( struct wfp_impl_invokation_context * context, - json_t * request) + struct wfp_json const * request) { - json_t * method_holder = json_object_get(request, "method"); - json_t * params = json_object_get(request, "params"); - json_t * id_holder = json_object_get(request, "id"); + struct wfp_json const * method_holder = wfp_impl_json_object_get(request, "method"); + struct wfp_json const * params = wfp_impl_json_object_get(request, "params"); + struct wfp_json const * id_holder = wfp_impl_json_object_get(request, "id"); - if ((json_is_string(method_holder)) && (json_is_array(params))) + if ((wfp_impl_json_is_string(method_holder)) && (wfp_impl_json_is_array(params))) { - char const * method = json_string_value(method_holder); - int id = json_is_integer(id_holder) ? json_integer_value(id_holder) : 0; + char const * method = wfp_impl_json_get_string(method_holder); + int id = wfp_impl_json_is_int(id_holder) ? wfp_impl_json_get_int(id_holder) : 0; wfp_impl_provider_invoke_method(context, method, params, id); } diff --git a/lib/webfuse_provider/impl/provider.h b/lib/webfuse_provider/impl/provider.h index f3c00c7..16579ef 100644 --- a/lib/webfuse_provider/impl/provider.h +++ b/lib/webfuse_provider/impl/provider.h @@ -5,7 +5,6 @@ #include #endif -#include #include "webfuse_provider/client_config.h" @@ -14,6 +13,8 @@ extern "C" { #endif +struct wfp_json; + struct wfp_provider { wfp_connected_fn * connected; @@ -44,7 +45,7 @@ extern void wfp_impl_provider_init_from_prototype( extern void wfp_impl_provider_invoke( struct wfp_impl_invokation_context * context, - json_t * request); + struct wfp_json const * request); extern bool wfp_impl_provider_is_authentication_enabled( struct wfp_provider * provider); diff --git a/test/webfuse_provider/jsonrpc/test_is_request.cc b/test/webfuse_provider/jsonrpc/test_is_request.cc index 945ec04..bb8360c 100644 --- a/test/webfuse_provider/jsonrpc/test_is_request.cc +++ b/test/webfuse_provider/jsonrpc/test_is_request.cc @@ -1,28 +1,26 @@ -#include #include "webfuse_provider/impl/jsonrpc/request.h" +#include "webfuse_provider/impl/json/parser.h" + +#include 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()); - - ASSERT_FALSE(wfp_jsonrpc_is_request(request)); + 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))); - 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)); - - ASSERT_FALSE(wfp_jsonrpc_is_request(request)); + 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))); - 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)); - - ASSERT_FALSE(wfp_jsonrpc_is_request(request)); + 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))); - 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)); - - ASSERT_FALSE(wfp_jsonrpc_is_request(request)); + 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))); - json_decref(request); + wfp_impl_json_dispose(doc); } diff --git a/test/webfuse_provider/jsonrpc/test_is_response.cc b/test/webfuse_provider/jsonrpc/test_is_response.cc index 856f350..8d0c149 100644 --- a/test/webfuse_provider/jsonrpc/test_is_response.cc +++ b/test/webfuse_provider/jsonrpc/test_is_response.cc @@ -1,37 +1,35 @@ #include #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); } diff --git a/test/webfuse_provider/jsonrpc/test_proxy.cc b/test/webfuse_provider/jsonrpc/test_proxy.cc index 8dcf6ea..899eec2 100644 --- a/test/webfuse_provider/jsonrpc/test_proxy.cc +++ b/test/webfuse_provider/jsonrpc/test_proxy.cc @@ -1,5 +1,7 @@ #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" @@ -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(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(&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); diff --git a/test/webfuse_provider/jsonrpc/test_request.cc b/test/webfuse_provider/jsonrpc/test_request.cc index d2ab05a..6b94183 100644 --- a/test/webfuse_provider/jsonrpc/test_request.cc +++ b/test/webfuse_provider/jsonrpc/test_request.cc @@ -1,37 +1,33 @@ #include "webfuse_provider/impl/jsonrpc/request.h" +#include "webfuse_provider/impl/json/parser.h" #include 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); } diff --git a/test/webfuse_provider/jsonrpc/test_response.cc b/test/webfuse_provider/jsonrpc/test_response.cc index 9ab7c31..b8f36a2 100644 --- a/test/webfuse_provider/jsonrpc/test_response.cc +++ b/test/webfuse_provider/jsonrpc/test_response.cc @@ -1,147 +1,136 @@ #include #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); } diff --git a/test/webfuse_provider/jsonrpc/test_response_parser.cc b/test/webfuse_provider/jsonrpc/test_response_parser.cc index 69a2e43..018dd44 100644 --- a/test/webfuse_provider/jsonrpc/test_response_parser.cc +++ b/test/webfuse_provider/jsonrpc/test_response_parser.cc @@ -1,58 +1,82 @@ -#include -#include - #include "webfuse_provider/impl/jsonrpc/response_intern.h" +#include "webfuse_provider/impl/jsonrpc/error.h" +#include "webfuse_provider/impl/json/parser.h" +#include -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) +TEST(response_parser, fail_no_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); + wfp_impl_json_dispose(doc); +} + +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)); - // 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); + 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)); - // 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.error->code); ASSERT_EQ(42, response.id); 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); + struct wfp_jsonrpc_response response; + wfp_jsonrpc_response_init(&response, wfp_impl_json_root(doc)); - // 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); + 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 503bb80..f4063eb 100644 --- a/test/webfuse_provider/provider/operation/test_close.cc +++ b/test/webfuse_provider/provider/operation/test_close.cc @@ -1,4 +1,5 @@ #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" @@ -10,23 +11,16 @@ using ::testing::_; TEST(wfp_close, close) { - int inode = 42; - int handle = 0xdeadbeef; - int flags = 23; - MockProvider provider; - EXPECT_CALL(provider, close(inode, handle, flags)).Times(1); + EXPECT_CALL(provider, close(42, 101, 23)).Times(1); wfp_impl_invokation_context context = create_context(provider); - json_t * params = json_array(); - json_array_append_new(params, json_string("test.filesystem")); - json_array_append_new(params, json_integer(inode)); - json_array_append_new(params, json_integer(handle)); - json_array_append_new(params, json_integer(flags)); + char params[] = "[\"test.filesystem\", 42, 101, 23]"; + wfp_json_doc * doc = wfp_impl_json_parse(params); - wfp_impl_close(&context, params, 42); - json_decref(params); + wfp_impl_close(&context, wfp_impl_json_root(doc), 42); + wfp_impl_json_dispose(doc); } TEST(wfp_close, close_fail_invalid_param_count) @@ -36,9 +30,11 @@ TEST(wfp_close, close_fail_invalid_param_count) wfp_impl_invokation_context context = create_context(provider); - json_t * params = json_array(); - wfp_impl_close(&context, params, 42); - json_decref(params); + char params[] = "[]"; + wfp_json_doc * doc = wfp_impl_json_parse(params); + + wfp_impl_close(&context, wfp_impl_json_root(doc), 42); + wfp_impl_json_dispose(doc); } TEST(wfp_close, close_fail_inode_invalid_type) @@ -48,14 +44,11 @@ TEST(wfp_close, close_fail_inode_invalid_type) wfp_impl_invokation_context context = create_context(provider); - json_t * params = json_array(); - json_array_append_new(params, json_string("test.filesystem")); - json_array_append_new(params, json_string("42")); - json_array_append_new(params, json_integer(0)); - json_array_append_new(params, json_integer(0)); + char params[] = "[\"test.filesystem\", \"42\", 0, 0]"; + wfp_json_doc * doc = wfp_impl_json_parse(params); - wfp_impl_close(&context, params, 42); - json_decref(params); + wfp_impl_close(&context, wfp_impl_json_root(doc), 42); + wfp_impl_json_dispose(doc); } TEST(wfp_close, close_fail_handle_invalid_type) @@ -65,14 +58,11 @@ TEST(wfp_close, close_fail_handle_invalid_type) wfp_impl_invokation_context context = create_context(provider); - json_t * params = json_array(); - json_array_append_new(params, json_string("test.filesystem")); - json_array_append_new(params, json_integer(0)); - json_array_append_new(params, json_string("42")); - json_array_append_new(params, json_integer(0)); + char params[] = "[\"test.filesystem\", 0, \"42\", 0]"; + wfp_json_doc * doc = wfp_impl_json_parse(params); - wfp_impl_close(&context, params, 42); - json_decref(params); + wfp_impl_close(&context, wfp_impl_json_root(doc), 42); + wfp_impl_json_dispose(doc); } TEST(wfp_close, close_fail_flags_invalid_type) @@ -82,14 +72,11 @@ TEST(wfp_close, close_fail_flags_invalid_type) wfp_impl_invokation_context context = create_context(provider); - json_t * params = json_array(); - json_array_append_new(params, json_string("test.filesystem")); - json_array_append_new(params, json_integer(0)); - json_array_append_new(params, json_integer(0)); - json_array_append_new(params, json_string("42")); + char params[] = "[\"test.filesystem\", 0, 0, \"42\"]"; + wfp_json_doc * doc = wfp_impl_json_parse(params); - wfp_impl_close(&context, params, 42); - json_decref(params); + wfp_impl_close(&context, wfp_impl_json_root(doc), 42); + wfp_impl_json_dispose(doc); }