refactor: replaced jansson by own json parser implementation

pull/3/head
Falk Werner 4 years ago
parent 63ca5d5a6d
commit 63cc5b5388

@ -4,7 +4,6 @@
#include <string.h>
#include <libwebsockets.h>
#include <jansson.h>
#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; }

@ -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

@ -3,6 +3,7 @@
#include "webfuse_provider/impl/json/reader.h"
#include <stdlib.h>
#include <string.h>
#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,

@ -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,

@ -1,17 +1,28 @@
#include "webfuse_provider/impl/jsonrpc/error.h"
json_t *
wfp_jsonrpc_error(
#include <stdlib.h>
#include <string.h>
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);
}

@ -1,7 +1,6 @@
#ifndef WFP_JSONRPC_ERROR_H
#define WFP_JSONRPC_ERROR_H
#include <jansson.h>
#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,

@ -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);

@ -11,7 +11,6 @@
using std::size_t;
#endif
#include <jansson.h>
#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
}

@ -1,17 +1,18 @@
#ifndef WFP_JSONRPC_PROXY_FINISHED_FN_H
#define WFP_JSONRPC_PROXY_FINISHED_FN_H
#include <jansson.h>
#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
}

@ -1,14 +1,17 @@
#include "webfuse_provider/impl/jsonrpc/request.h"
#include "webfuse_provider/impl/json/node.h"
#include <stdlib.h>
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)));
}

@ -5,15 +5,15 @@
#include <stdbool.h>
#endif
#include <jansson.h>
#ifdef __cplusplus
extern "C"
{
#endif
struct wfp_json;
extern bool wfp_jsonrpc_is_request(
json_t * message);
struct wfp_json const * message);
#ifdef __cplusplus
}

@ -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);
}
}

@ -5,15 +5,16 @@
#include <stdbool.h>
#endif
#include <jansson.h>
#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
}

@ -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);

@ -1,26 +1,27 @@
#include "webfuse_provider/impl/operation/close.h"
#include <limits.h>
#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);
}

@ -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(

@ -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);

@ -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(

@ -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);

@ -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(

@ -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);

@ -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(

@ -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);

@ -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(

@ -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);

@ -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(

@ -1,8 +1,5 @@
#include "webfuse_provider/impl/provider.h"
#include <stdbool.h>
#include <string.h>
#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 <stdbool.h>
#include <string.h>
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);
}

@ -5,7 +5,6 @@
#include <stdbool.h>
#endif
#include <jansson.h>
#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);

@ -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());
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);
}

@ -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);
}

@ -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);

@ -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);
}

@ -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);
}

@ -1,58 +1,82 @@
#include <string>
#include <gtest/gtest.h>
#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>
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);
}

@ -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);
}

Loading…
Cancel
Save