parent
63ca5d5a6d
commit
63cc5b5388
@ -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)));
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in new issue