2019-05-19 12:33:42 +00:00
|
|
|
#include <gtest/gtest.h>
|
2020-06-16 21:39:45 +00:00
|
|
|
#include "webfuse_provider/impl/jsonrpc/request.h"
|
|
|
|
#include "webfuse_provider/status.h"
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
struct Context
|
|
|
|
{
|
|
|
|
json_t * response;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool jsonrpc_send(
|
|
|
|
json_t * request,
|
|
|
|
void * user_data)
|
|
|
|
{
|
|
|
|
Context * context = reinterpret_cast<Context*>(user_data);
|
|
|
|
context->response = request;
|
|
|
|
json_incref(request);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
TEST(wfp_jsonrpc_request, create_dispose)
|
2019-05-19 12:33:42 +00:00
|
|
|
{
|
|
|
|
Context context{nullptr};
|
|
|
|
void * user_data = reinterpret_cast<void*>(&context);
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
struct wfp_jsonrpc_request * request =
|
|
|
|
wfp_jsonrpc_request_create(42, &jsonrpc_send, user_data);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
ASSERT_NE(nullptr, request);
|
2020-06-16 21:57:41 +00:00
|
|
|
ASSERT_EQ(user_data, wfp_jsonrpc_request_get_userdata(request));
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_jsonrpc_request_dispose(request);
|
2019-05-19 12:33:42 +00:00
|
|
|
}
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
TEST(wfp_jsonrpc_request, respond)
|
2019-05-19 12:33:42 +00:00
|
|
|
{
|
|
|
|
Context context{nullptr};
|
|
|
|
void * user_data = reinterpret_cast<void*>(&context);
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
struct wfp_jsonrpc_request * request =
|
|
|
|
wfp_jsonrpc_request_create(42, &jsonrpc_send, user_data);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_jsonrpc_respond(request, json_string("okay"));
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
ASSERT_NE(nullptr, context.response);
|
|
|
|
|
|
|
|
|
|
|
|
json_t * response = reinterpret_cast<json_t*>(context.response);
|
|
|
|
ASSERT_TRUE(json_is_object(response));
|
|
|
|
|
|
|
|
json_t * id = json_object_get(response, "id");
|
|
|
|
ASSERT_TRUE(json_is_integer(id));
|
|
|
|
ASSERT_EQ(42, json_integer_value(id));
|
|
|
|
|
|
|
|
json_t * result = json_object_get(response, "result");
|
|
|
|
ASSERT_TRUE(json_is_string(result));
|
|
|
|
ASSERT_STREQ("okay", json_string_value(result));
|
|
|
|
|
|
|
|
ASSERT_EQ(nullptr, json_object_get(response, "error"));
|
|
|
|
|
|
|
|
json_decref(response);
|
|
|
|
}
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
TEST(wfp_jsonrpc_request, respond_error)
|
2019-05-19 12:33:42 +00:00
|
|
|
{
|
|
|
|
Context context{nullptr};
|
|
|
|
void * user_data = reinterpret_cast<void*>(&context);
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
struct wfp_jsonrpc_request * request =
|
|
|
|
wfp_jsonrpc_request_create(42, &jsonrpc_send, user_data);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_jsonrpc_respond_error(request, WFP_BAD, "Bad");
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
ASSERT_NE(nullptr, context.response);
|
|
|
|
|
|
|
|
|
|
|
|
json_t * response = reinterpret_cast<json_t*>(context.response);
|
|
|
|
ASSERT_TRUE(json_is_object(response));
|
|
|
|
|
|
|
|
json_t * id = json_object_get(response, "id");
|
|
|
|
ASSERT_TRUE(json_is_integer(id));
|
|
|
|
ASSERT_EQ(42, json_integer_value(id));
|
|
|
|
|
|
|
|
ASSERT_EQ(nullptr, json_object_get(response, "result"));
|
|
|
|
|
|
|
|
json_t * err = json_object_get(response, "error");
|
|
|
|
ASSERT_TRUE(json_is_object(err));
|
|
|
|
|
|
|
|
json_t * err_code = json_object_get(err, "code");
|
|
|
|
ASSERT_TRUE(json_is_integer(err_code));
|
2020-06-16 21:57:41 +00:00
|
|
|
ASSERT_EQ(WFP_BAD, json_integer_value(err_code));
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
json_t * err_message = json_object_get(err, "message");
|
|
|
|
ASSERT_TRUE(json_is_string(err_message));
|
|
|
|
ASSERT_STREQ("Bad", json_string_value(err_message));
|
|
|
|
|
|
|
|
json_decref(response);
|
|
|
|
}
|
2020-03-22 12:36:03 +00:00
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
TEST(wfp_jsonrpc_request, is_request_object_params)
|
2020-03-22 12:36:03 +00:00
|
|
|
{
|
|
|
|
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));
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
ASSERT_TRUE(wfp_jsonrpc_is_request(request));
|
2020-03-22 12:36:03 +00:00
|
|
|
|
|
|
|
json_decref(request);
|
|
|
|
}
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
TEST(wfp_jsonrpc_request, is_request_fail_missing_params)
|
2020-03-22 12:36:03 +00:00
|
|
|
{
|
|
|
|
json_t * request = json_object();
|
|
|
|
json_object_set_new(request, "method", json_string("some_method"));
|
|
|
|
json_object_set_new(request, "id", json_integer(42));
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
ASSERT_FALSE(wfp_jsonrpc_is_request(request));
|
2020-03-22 12:36:03 +00:00
|
|
|
|
|
|
|
json_decref(request);
|
|
|
|
}
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
TEST(wfp_jsonrpc_request, is_request_fail_params_wrong_type)
|
2020-03-22 12:36:03 +00:00
|
|
|
{
|
|
|
|
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));
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
ASSERT_FALSE(wfp_jsonrpc_is_request(request));
|
2020-03-22 12:36:03 +00:00
|
|
|
|
|
|
|
json_decref(request);
|
|
|
|
}
|