1
0
mirror of https://github.com/falk-werner/webfuse synced 2025-06-13 12:54:15 +00:00

fixed unit tests

This commit is contained in:
Falk Werner 2020-07-18 23:16:18 +02:00
parent 4b544ef35d
commit 017e04f77f
23 changed files with 424 additions and 665 deletions

View File

@ -122,7 +122,7 @@ wf_impl_client_protocol_on_add_filesystem_finished(
struct wf_client_protocol * protocol = context->protocol; struct wf_client_protocol * protocol = context->protocol;
int reason = WF_CLIENT_FILESYSTEM_ADD_FAILED; int reason = WF_CLIENT_FILESYSTEM_ADD_FAILED;
if (NULL == protocol->filesystem) if ((NULL == protocol->filesystem) && (NULL != result))
{ {
struct wf_json const * id = wf_impl_json_object_get(result, "id"); struct wf_json const * id = wf_impl_json_object_get(result, "id");
if (wf_impl_json_is_string(id)) if (wf_impl_json_is_string(id))

View File

@ -21,19 +21,22 @@ void wf_impl_credentials_init(
char const * type, char const * type,
struct wf_json const * data) struct wf_json const * data)
{ {
size_t count = wf_impl_json_object_size(data); size_t count = (NULL != data) ? wf_impl_json_object_size(data) : 0;
credentials->type = strdup(type); credentials->type = strdup(type);
credentials->capacity = (count > 0) ? count : WF_CREDENTIALS_INITIAL_CAPACITY; credentials->capacity = (count > 0) ? count : WF_CREDENTIALS_INITIAL_CAPACITY;
credentials->entries = malloc(sizeof(struct wf_credentials_entry) * credentials->capacity); credentials->entries = malloc(sizeof(struct wf_credentials_entry) * credentials->capacity);
credentials->size = 0; credentials->size = 0;
for (size_t i; i < count; i++) for (size_t i = 0; i < count; i++)
{ {
char const * key = wf_impl_json_object_key(data, i); char const * key = wf_impl_json_object_key(data, i);
struct wf_json const * value = wf_impl_json_object_value(data, i); struct wf_json const * value = wf_impl_json_object_value(data, i);
if (wf_impl_json_is_string(value))
{
wf_impl_credentials_add(credentials, key, wf_impl_json_string_get(value)); wf_impl_credentials_add(credentials, key, wf_impl_json_string_get(value));
} }
}
} }
void wf_impl_credentials_cleanup( void wf_impl_credentials_cleanup(
@ -82,6 +85,16 @@ void wf_impl_credentials_add(
char const * key, char const * key,
char const * value) char const * value)
{ {
for(size_t i = 0; i < credentials->size; i++)
{
if (0 == strcmp(key, credentials->entries[i].key))
{
free(credentials->entries[i].value);
credentials->entries[i].value = strdup(value);
return;
}
}
if (credentials->size >= credentials->capacity) if (credentials->size >= credentials->capacity)
{ {
credentials->capacity *= 2; credentials->capacity *= 2;

View File

@ -20,6 +20,8 @@ bool
wf_impl_jsonrpc_is_request( wf_impl_jsonrpc_is_request(
struct wf_json const * message) struct wf_json const * message)
{ {
if (NULL == message) { return false; }
struct wf_json const * id = wf_impl_json_object_get(message, "id"); struct wf_json const * id = wf_impl_json_object_get(message, "id");
struct wf_json const * method = wf_impl_json_object_get(message, "method"); struct wf_json const * method = wf_impl_json_object_get(message, "method");
struct wf_json const * params = wf_impl_json_object_get(message, "params"); struct wf_json const * params = wf_impl_json_object_get(message, "params");

View File

@ -7,6 +7,8 @@ bool
wf_impl_jsonrpc_is_response( wf_impl_jsonrpc_is_response(
struct wf_json const * message) struct wf_json const * message)
{ {
if (NULL == message) { return false; }
struct wf_json const * id = wf_impl_json_object_get(message, "id"); struct wf_json const * id = wf_impl_json_object_get(message, "id");
struct wf_json const * err = wf_impl_json_object_get(message, "error"); struct wf_json const * err = wf_impl_json_object_get(message, "error");
struct wf_json const * result = wf_impl_json_object_get(message, "result"); struct wf_json const * result = wf_impl_json_object_get(message, "result");
@ -26,7 +28,7 @@ wf_impl_jsonrpc_response_init(
result->error = NULL; result->error = NULL;
struct wf_json const * id_holder = wf_impl_json_object_get(response, "id"); struct wf_json const * id_holder = wf_impl_json_object_get(response, "id");
if (wf_impl_json_is_int(id_holder)) if (!wf_impl_json_is_int(id_holder))
{ {
result->error = wf_impl_jsonrpc_error(WF_BAD_FORMAT, "invalid format: missing id"); result->error = wf_impl_jsonrpc_error(WF_BAD_FORMAT, "invalid format: missing id");
return; return;
@ -49,6 +51,7 @@ wf_impl_jsonrpc_response_init(
} }
result->error = wf_impl_jsonrpc_error(code, message); result->error = wf_impl_jsonrpc_error(code, message);
result->result = NULL;
} }
} }

View File

@ -33,7 +33,7 @@ void wf_impl_operation_lookup_finished(
struct wf_json const * type_holder = wf_impl_json_object_get(result, "type"); struct wf_json const * type_holder = wf_impl_json_object_get(result, "type");
if ((wf_impl_json_is_int(inode_holder)) && if ((wf_impl_json_is_int(inode_holder)) &&
(wf_impl_json_is_int(mode_holder)) && (wf_impl_json_is_int(mode_holder)) &&
(wf_impl_json_is_int(type_holder))) (wf_impl_json_is_string(type_holder)))
{ {
memset(&buffer, 0, sizeof(struct stat)); memset(&buffer, 0, sizeof(struct stat));

View File

@ -78,7 +78,7 @@ void wf_impl_operation_readdir_finished(
struct wf_impl_dirbuffer buffer; struct wf_impl_dirbuffer buffer;
wf_impl_dirbuffer_init(&buffer); wf_impl_dirbuffer_init(&buffer);
if (wf_impl_json_is_array(result)) if ((NULL != result) && (wf_impl_json_is_array(result)))
{ {
size_t const count = wf_impl_json_array_size(result); size_t const count = wf_impl_json_array_size(result);
for(size_t i = 0; i < count; i++) for(size_t i = 0; i < count; i++)

View File

@ -8,6 +8,7 @@ wf_impl_json_get_int(
char const * key, char const * key,
int default_value) int default_value)
{ {
if (NULL == object) { return default_value; }
int result = default_value; int result = default_value;
struct wf_json const * holder = wf_impl_json_object_get(object, key); struct wf_json const * holder = wf_impl_json_object_get(object, key);

View File

@ -1,28 +1,21 @@
#include <gtest/gtest.h>
#include "webfuse/impl/jsonrpc/request.h" #include "webfuse/impl/jsonrpc/request.h"
#include "webfuse/test_util/json_doc.hpp"
#include <gtest/gtest.h>
using webfuse_test::JsonDoc;
TEST(wf_jsonrpc_is_request, request_with_object_params) TEST(wf_jsonrpc_is_request, request_with_object_params)
{ {
json_t * request = json_object(); JsonDoc doc("{\"method\": \"method\", \"params\": {}, \"id\": 42}");
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));
ASSERT_TRUE(wf_impl_jsonrpc_is_request(request)); ASSERT_TRUE(wf_impl_jsonrpc_is_request(doc.root()));
json_decref(request);
} }
TEST(wf_jsonrpc_is_request, request_with_array_params) TEST(wf_jsonrpc_is_request, request_with_array_params)
{ {
json_t * request = json_object(); JsonDoc doc("{\"method\": \"method\", \"params\": [], \"id\": 42}");
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));
ASSERT_TRUE(wf_impl_jsonrpc_is_request(request)); ASSERT_TRUE(wf_impl_jsonrpc_is_request(doc.root()));
json_decref(request);
} }
TEST(wf_jsonrpc_is_request, null_request) TEST(wf_jsonrpc_is_request, null_request)
@ -32,81 +25,49 @@ TEST(wf_jsonrpc_is_request, null_request)
TEST(wf_jsonrpc_is_request, invalid_request) TEST(wf_jsonrpc_is_request, invalid_request)
{ {
json_t * request = json_array(); JsonDoc doc("[\"method\", {}, 42]");
json_array_append_new(request, json_string("method"));
json_array_append_new(request, json_object());
json_array_append_new(request, json_integer(42));
ASSERT_FALSE(wf_impl_jsonrpc_is_request(request)); ASSERT_FALSE(wf_impl_jsonrpc_is_request(doc.root()));
json_decref(request);
} }
TEST(wf_jsonrpc_is_request, invalid_request_without_id) TEST(wf_jsonrpc_is_request, invalid_request_without_id)
{ {
json_t * request = json_object(); JsonDoc doc("{\"method\": \"method\", \"params\": {}}");
json_object_set_new(request, "method", json_string("method"));
json_object_set_new(request, "params", json_object());
ASSERT_FALSE(wf_impl_jsonrpc_is_request(request)); ASSERT_FALSE(wf_impl_jsonrpc_is_request(doc.root()));
json_decref(request);
} }
TEST(wf_jsonrpc_is_request, invalid_request_due_to_invalid_id) TEST(wf_jsonrpc_is_request, invalid_request_due_to_invalid_id)
{ {
json_t * request = json_object(); JsonDoc doc("{\"method\": \"method\", \"params\": [], \"id\": \"42\"}");
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"));
ASSERT_FALSE(wf_impl_jsonrpc_is_request(request)); ASSERT_FALSE(wf_impl_jsonrpc_is_request(doc.root()));
json_decref(request);
} }
TEST(wf_jsonrpc_is_request, invalid_request_without_method) TEST(wf_jsonrpc_is_request, invalid_request_without_method)
{ {
json_t * request = json_object(); JsonDoc doc("{\"params\": [], \"id\": 42}");
json_object_set_new(request, "params", json_object());
json_object_set_new(request, "id", json_integer(42));
ASSERT_FALSE(wf_impl_jsonrpc_is_request(request)); ASSERT_FALSE(wf_impl_jsonrpc_is_request(doc.root()));
json_decref(request);
} }
TEST(wf_jsonrpc_is_request, invalid_request_due_to_invalid_method) TEST(wf_jsonrpc_is_request, invalid_request_due_to_invalid_method)
{ {
json_t * request = json_object(); JsonDoc doc("{\"method\": 42, \"params\": [], \"id\": 42}");
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(wf_impl_jsonrpc_is_request(request)); ASSERT_FALSE(wf_impl_jsonrpc_is_request(doc.root()));
json_decref(request);
} }
TEST(wf_jsonrpc_is_request, invalid_request_without_params) TEST(wf_jsonrpc_is_request, invalid_request_without_params)
{ {
json_t * request = json_object(); JsonDoc doc("{\"method\": \"method\", \"id\": 42}");
json_object_set_new(request, "method", json_string("method"));
json_object_set_new(request, "id", json_integer(42));
ASSERT_FALSE(wf_impl_jsonrpc_is_request(request)); ASSERT_FALSE(wf_impl_jsonrpc_is_request(doc.root()));
json_decref(request);
} }
TEST(wf_jsonrpc_is_request, invalid_request_due_to_invalid_params) TEST(wf_jsonrpc_is_request, invalid_request_due_to_invalid_params)
{ {
json_t * request = json_object(); JsonDoc doc("{\"method\": \"method\", \"params\": \"params\", \"id\": 42}");
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(wf_impl_jsonrpc_is_request(request)); ASSERT_FALSE(wf_impl_jsonrpc_is_request(doc.root()));
json_decref(request);
} }

View File

@ -1,37 +1,28 @@
#include <gtest/gtest.h>
#include "webfuse/impl/jsonrpc/response.h" #include "webfuse/impl/jsonrpc/response.h"
#include "webfuse/test_util/json_doc.hpp"
#include <gtest/gtest.h>
using webfuse_test::JsonDoc;
TEST(wf_jsonrpc_is_response, valid_result) TEST(wf_jsonrpc_is_response, valid_result)
{ {
json_t * message = json_object(); JsonDoc doc("{\"result\": {}, \"id\": 42}");
json_object_set_new(message, "result", json_object());
json_object_set_new(message, "id", json_integer(42));
ASSERT_TRUE(wf_impl_jsonrpc_is_response(message)); ASSERT_TRUE(wf_impl_jsonrpc_is_response(doc.root()));
json_decref(message);
} }
TEST(wf_jsonrpc_is_response, valid_result_string) TEST(wf_jsonrpc_is_response, valid_result_string)
{ {
json_t * message = json_object(); JsonDoc doc("{\"result\": \"also valid\", \"id\": 42}");
json_object_set_new(message, "result", json_string("also valid"));
json_object_set_new(message, "id", json_integer(42));
ASSERT_TRUE(wf_impl_jsonrpc_is_response(message)); ASSERT_TRUE(wf_impl_jsonrpc_is_response(doc.root()));
json_decref(message);
} }
TEST(wf_jsonrpc_is_response, valid_error) TEST(wf_jsonrpc_is_response, valid_error)
{ {
json_t * message = json_object(); JsonDoc doc("{\"error\": {}, \"id\": 42}");
json_object_set_new(message, "error", json_object());
json_object_set_new(message, "id", json_integer(42));
ASSERT_TRUE(wf_impl_jsonrpc_is_response(message)); ASSERT_TRUE(wf_impl_jsonrpc_is_response(doc.root()));
json_decref(message);
} }
TEST(wf_jsonrpc_is_response, invalid_null) TEST(wf_jsonrpc_is_response, invalid_null)
@ -41,54 +32,36 @@ TEST(wf_jsonrpc_is_response, invalid_null)
TEST(wf_jsonrpc_is_response, invalid_message) TEST(wf_jsonrpc_is_response, invalid_message)
{ {
json_t * message = json_array(); JsonDoc doc("[{}, 42]");
json_array_append_new(message, json_object());
json_array_append_new(message, json_integer(42));
ASSERT_FALSE(wf_impl_jsonrpc_is_response(message)); ASSERT_FALSE(wf_impl_jsonrpc_is_response(doc.root()));
json_decref(message);
} }
TEST(wf_jsonrpc_is_response, invalid_missing_id) TEST(wf_jsonrpc_is_response, invalid_missing_id)
{ {
json_t * message = json_object(); JsonDoc doc("{\"result\": {}}");
json_object_set_new(message, "result", json_object());
ASSERT_FALSE(wf_impl_jsonrpc_is_response(message)); ASSERT_FALSE(wf_impl_jsonrpc_is_response(doc.root()));
json_decref(message);
} }
TEST(wf_jsonrpc_is_response, invalid_id_wrong_type) TEST(wf_jsonrpc_is_response, invalid_id_wrong_type)
{ {
json_t * message = json_object(); JsonDoc doc("{\"result\": {}, \"id\": \"42\"}");
json_object_set_new(message, "result", json_object());
json_object_set_new(message, "id", json_string("42"));
ASSERT_FALSE(wf_impl_jsonrpc_is_response(message)); ASSERT_FALSE(wf_impl_jsonrpc_is_response(doc.root()));
json_decref(message);
} }
TEST(wf_jsonrpc_is_response, invalid_missing_result_and_error) TEST(wf_jsonrpc_is_response, invalid_missing_result_and_error)
{ {
json_t * message = json_object(); JsonDoc doc("{\"id\": 42}");
json_object_set_new(message, "id", json_integer(42));
ASSERT_FALSE(wf_impl_jsonrpc_is_response(message)); ASSERT_FALSE(wf_impl_jsonrpc_is_response(doc.root()));
json_decref(message);
} }
TEST(wf_jsonrpc_is_response, invalid_error_wrong_type) TEST(wf_jsonrpc_is_response, invalid_error_wrong_type)
{ {
json_t * message = json_object(); JsonDoc doc("{\"error\": [], \"id\": 42}");
json_object_set_new(message, "error", json_array());
json_object_set_new(message, "id", json_integer(42));
ASSERT_FALSE(wf_impl_jsonrpc_is_response(message)); ASSERT_FALSE(wf_impl_jsonrpc_is_response(doc.root()));
json_decref(message);
} }

View File

@ -1,17 +1,20 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "webfuse/impl/jsonrpc/proxy.h" #include "webfuse/impl/jsonrpc/proxy.h"
#include "webfuse/impl/jsonrpc/error.h" #include "webfuse/impl/jsonrpc/error.h"
#include "webfuse/impl/json/node.h"
#include "webfuse/impl/message.h" #include "webfuse/impl/message.h"
#include "webfuse/status.h" #include "webfuse/status.h"
#include "webfuse/impl/timer/manager.h" #include "webfuse/impl/timer/manager.h"
#include "webfuse/jsonrpc/mock_timer.hpp" #include "webfuse/jsonrpc/mock_timer.hpp"
#include "webfuse/test_util/json_doc.hpp"
#include <thread> #include <thread>
#include <chrono> #include <chrono>
using namespace std::chrono_literals; using namespace std::chrono_literals;
using wf_jsonrpc_test::MockTimer; using wf_jsonrpc_test::MockTimer;
using webfuse_test::JsonDoc;
using testing::Return; using testing::Return;
using testing::_; using testing::_;
using testing::DoAll; using testing::DoAll;
@ -23,21 +26,18 @@ namespace
{ {
struct SendContext struct SendContext
{ {
json_t * response; JsonDoc doc;
wf_json const * response;
bool result; bool result;
bool is_called; bool is_called;
explicit SendContext(bool result_ = true) explicit SendContext(bool result_ = true)
: response(nullptr) : doc("null")
, response(nullptr)
, result(result_) , result(result_)
, is_called(false) , is_called(false)
{ {
} }
~SendContext()
{
json_decref(response);
}
}; };
bool jsonrpc_send( bool jsonrpc_send(
@ -46,11 +46,8 @@ namespace
{ {
SendContext * context = reinterpret_cast<SendContext*>(user_data); SendContext * context = reinterpret_cast<SendContext*>(user_data);
context->is_called = true; context->is_called = true;
if (nullptr != context->response) context->doc = std::move(JsonDoc(std::string(request->data, request->length)));
{ context->response = context->doc.root();
json_decref(context->response);
}
context->response = json_loadb(request->data, request->length, 0, nullptr);
wf_impl_message_dispose(request); wf_impl_message_dispose(request);
return context->result; return context->result;
@ -59,12 +56,10 @@ namespace
struct FinishedContext struct FinishedContext
{ {
bool is_called; bool is_called;
json_t * result;
wf_jsonrpc_error * error; wf_jsonrpc_error * error;
FinishedContext() FinishedContext()
: is_called(false) : is_called(false)
, result(nullptr)
, error(nullptr) , error(nullptr)
{ {
@ -72,23 +67,17 @@ namespace
~FinishedContext() ~FinishedContext()
{ {
if (nullptr != result)
{
json_decref(result);
}
wf_impl_jsonrpc_error_dispose(error); wf_impl_jsonrpc_error_dispose(error);
} }
}; };
void jsonrpc_finished( void jsonrpc_finished(
void * user_data, void * user_data,
json_t const * result, wf_json const *,
wf_jsonrpc_error const * error) wf_jsonrpc_error const * error)
{ {
FinishedContext * context = reinterpret_cast<FinishedContext*>(user_data); FinishedContext * context = reinterpret_cast<FinishedContext*>(user_data);
context->is_called = true; context->is_called = true;
context->result = json_deep_copy(result);
if (nullptr != error) if (nullptr != error)
{ {
@ -125,22 +114,22 @@ TEST(wf_jsonrpc_proxy, invoke)
wf_impl_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42); wf_impl_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
ASSERT_TRUE(send_context.is_called); ASSERT_TRUE(send_context.is_called);
ASSERT_TRUE(json_is_object(send_context.response)); ASSERT_TRUE(wf_impl_json_is_object(send_context.response));
json_t * method = json_object_get(send_context.response, "method"); wf_json const * method = wf_impl_json_object_get(send_context.response, "method");
ASSERT_TRUE(json_is_string(method)); ASSERT_TRUE(wf_impl_json_is_string(method));
ASSERT_STREQ("foo", json_string_value(method)); ASSERT_STREQ("foo", wf_impl_json_string_get(method));
json_t * params = json_object_get(send_context.response, "params"); wf_json const * params = wf_impl_json_object_get(send_context.response, "params");
ASSERT_TRUE(json_is_array(params)); ASSERT_TRUE(wf_impl_json_is_array(params));
ASSERT_EQ(2, json_array_size(params)); ASSERT_EQ(2, wf_impl_json_array_size(params));
ASSERT_TRUE(json_is_string(json_array_get(params, 0))); ASSERT_TRUE(wf_impl_json_is_string(wf_impl_json_array_get(params, 0)));
ASSERT_STREQ("bar", json_string_value(json_array_get(params, 0))); ASSERT_STREQ("bar", wf_impl_json_string_get(wf_impl_json_array_get(params, 0)));
ASSERT_TRUE(json_is_integer(json_array_get(params, 1))); ASSERT_TRUE(wf_impl_json_is_int(wf_impl_json_array_get(params, 1)));
ASSERT_EQ(42, json_integer_value(json_array_get(params, 1))); ASSERT_EQ(42, wf_impl_json_int_get(wf_impl_json_array_get(params, 1)));
json_t * id = json_object_get(send_context.response, "id"); wf_json const * id = wf_impl_json_object_get(send_context.response, "id");
ASSERT_TRUE(json_is_integer(id)); ASSERT_TRUE(wf_impl_json_is_int(id));
ASSERT_FALSE(finished_context.is_called); ASSERT_FALSE(finished_context.is_called);
@ -164,7 +153,7 @@ TEST(wf_jsonrpc_proxy, invoke_calls_finish_if_send_fails)
wf_impl_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42); wf_impl_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
ASSERT_TRUE(send_context.is_called); ASSERT_TRUE(send_context.is_called);
ASSERT_TRUE(json_is_object(send_context.response)); ASSERT_TRUE(wf_impl_json_is_object(send_context.response));
ASSERT_TRUE(finished_context.is_called); ASSERT_TRUE(finished_context.is_called);
ASSERT_FALSE(nullptr == finished_context.error); ASSERT_FALSE(nullptr == finished_context.error);
@ -190,7 +179,7 @@ TEST(wf_jsonrpc_proxy, invoke_if_another_request_is_pending)
wf_impl_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data2, "foo", ""); wf_impl_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data2, "foo", "");
ASSERT_TRUE(send_context.is_called); ASSERT_TRUE(send_context.is_called);
ASSERT_TRUE(json_is_object(send_context.response)); ASSERT_TRUE(wf_impl_json_is_object(send_context.response));
ASSERT_FALSE(finished_context.is_called); ASSERT_FALSE(finished_context.is_called);
@ -231,22 +220,16 @@ TEST(wf_jsonrpc_proxy, on_result)
wf_impl_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42); wf_impl_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
ASSERT_TRUE(send_context.is_called); ASSERT_TRUE(send_context.is_called);
ASSERT_TRUE(json_is_object(send_context.response)); ASSERT_TRUE(wf_impl_json_is_object(send_context.response));
json_t * id = json_object_get(send_context.response, "id"); wf_json const * id = wf_impl_json_object_get(send_context.response, "id");
ASSERT_TRUE(json_is_number(id)); ASSERT_TRUE(wf_impl_json_is_int(id));
json_t * response = json_object(); JsonDoc response("{\"result\": \"okay\", \"id\": " + std::to_string(wf_impl_json_int_get(id)) + "}");
json_object_set_new(response, "result", json_string("okay")); wf_impl_jsonrpc_proxy_onresult(proxy, response.root());
json_object_set(response, "id", id);
wf_impl_jsonrpc_proxy_onresult(proxy, response);
json_decref(response);
ASSERT_TRUE(finished_context.is_called); ASSERT_TRUE(finished_context.is_called);
ASSERT_EQ(nullptr, finished_context.error); ASSERT_EQ(nullptr, finished_context.error);
ASSERT_TRUE(json_is_string(finished_context.result));
ASSERT_STREQ("okay", json_string_value(finished_context.result));
wf_impl_jsonrpc_proxy_dispose(proxy); wf_impl_jsonrpc_proxy_dispose(proxy);
wf_impl_timer_manager_dispose(timer_manager); wf_impl_timer_manager_dispose(timer_manager);
@ -265,17 +248,13 @@ TEST(wf_jsonrpc_proxy, on_result_reject_response_with_unknown_id)
wf_impl_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42); wf_impl_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
ASSERT_TRUE(send_context.is_called); ASSERT_TRUE(send_context.is_called);
ASSERT_TRUE(json_is_object(send_context.response)); ASSERT_TRUE(wf_impl_json_is_object(send_context.response));
json_t * id = json_object_get(send_context.response, "id"); wf_json const * id = wf_impl_json_object_get(send_context.response, "id");
ASSERT_TRUE(json_is_number(id)); ASSERT_TRUE(wf_impl_json_is_int(id));
json_t * response = json_object(); JsonDoc response("{\"result\": \"okay\", \"id\": " + std::to_string(wf_impl_json_int_get(id) + 1) + "}");
json_object_set_new(response, "result", json_string("okay")); wf_impl_jsonrpc_proxy_onresult(proxy, response.root());
json_object_set_new(response, "id", json_integer(1 + json_integer_value(id)));
wf_impl_jsonrpc_proxy_onresult(proxy, response);
json_decref(response);
ASSERT_FALSE(finished_context.is_called); ASSERT_FALSE(finished_context.is_called);
@ -296,7 +275,7 @@ TEST(wf_jsonrpc_proxy, timeout)
wf_impl_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42); wf_impl_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
ASSERT_TRUE(send_context.is_called); ASSERT_TRUE(send_context.is_called);
ASSERT_TRUE(json_is_object(send_context.response)); ASSERT_TRUE(wf_impl_json_is_object(send_context.response));
std::this_thread::sleep_for(10ms); std::this_thread::sleep_for(10ms);
wf_impl_timer_manager_check(timer_manager); wf_impl_timer_manager_check(timer_manager);
@ -321,7 +300,7 @@ TEST(wf_jsonrpc_proxy, cleanup_pending_request)
wf_impl_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42); wf_impl_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
ASSERT_TRUE(send_context.is_called); ASSERT_TRUE(send_context.is_called);
ASSERT_TRUE(json_is_object(send_context.response)); ASSERT_TRUE(wf_impl_json_is_object(send_context.response));
ASSERT_FALSE(finished_context.is_called); ASSERT_FALSE(finished_context.is_called);
@ -345,22 +324,22 @@ TEST(wf_jsonrpc_proxy, notify)
wf_impl_jsonrpc_proxy_notify(proxy, "foo", "si", "bar", 42); wf_impl_jsonrpc_proxy_notify(proxy, "foo", "si", "bar", 42);
ASSERT_TRUE(send_context.is_called); ASSERT_TRUE(send_context.is_called);
ASSERT_TRUE(json_is_object(send_context.response)); ASSERT_TRUE(wf_impl_json_is_object(send_context.response));
json_t * method = json_object_get(send_context.response, "method"); wf_json const * method = wf_impl_json_object_get(send_context.response, "method");
ASSERT_TRUE(json_is_string(method)); ASSERT_TRUE(wf_impl_json_is_string(method));
ASSERT_STREQ("foo", json_string_value(method)); ASSERT_STREQ("foo", wf_impl_json_string_get(method));
json_t * params = json_object_get(send_context.response, "params"); wf_json const * params = wf_impl_json_object_get(send_context.response, "params");
ASSERT_TRUE(json_is_array(params)); ASSERT_TRUE(wf_impl_json_is_array(params));
ASSERT_EQ(2, json_array_size(params)); ASSERT_EQ(2, wf_impl_json_array_size(params));
ASSERT_TRUE(json_is_string(json_array_get(params, 0))); ASSERT_TRUE(wf_impl_json_is_string(wf_impl_json_array_get(params, 0)));
ASSERT_STREQ("bar", json_string_value(json_array_get(params, 0))); ASSERT_STREQ("bar", wf_impl_json_string_get(wf_impl_json_array_get(params, 0)));
ASSERT_TRUE(json_is_integer(json_array_get(params, 1))); ASSERT_TRUE(wf_impl_json_is_int(wf_impl_json_array_get(params, 1)));
ASSERT_EQ(42, json_integer_value(json_array_get(params, 1))); ASSERT_EQ(42, wf_impl_json_int_get(wf_impl_json_array_get(params, 1)));
json_t * id = json_object_get(send_context.response, "id"); wf_json const * id = wf_impl_json_object_get(send_context.response, "id");
ASSERT_EQ(nullptr, id); ASSERT_TRUE(wf_impl_json_is_undefined(id));
wf_impl_jsonrpc_proxy_dispose(proxy); wf_impl_jsonrpc_proxy_dispose(proxy);
wf_impl_timer_manager_dispose(timer_manager); wf_impl_timer_manager_dispose(timer_manager);
@ -390,12 +369,8 @@ TEST(wf_jsonrpc_proxy, on_result_swallow_if_no_request_pending)
void * send_data = reinterpret_cast<void*>(&send_context); void * send_data = reinterpret_cast<void*>(&send_context);
struct wf_jsonrpc_proxy * proxy = wf_impl_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); struct wf_jsonrpc_proxy * proxy = wf_impl_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
json_t * response = json_object(); JsonDoc response("{\"result\": \"okay\", \"id\": 42}");
json_object_set_new(response, "result", json_string("okay")); wf_impl_jsonrpc_proxy_onresult(proxy, response.root());
json_object_set_new(response, "id", json_integer(42));
wf_impl_jsonrpc_proxy_onresult(proxy, response);
json_decref(response);
wf_impl_jsonrpc_proxy_dispose(proxy); wf_impl_jsonrpc_proxy_dispose(proxy);
wf_impl_timer_manager_dispose(timer_manager); wf_impl_timer_manager_dispose(timer_manager);

View File

@ -1,7 +1,12 @@
#include <gtest/gtest.h>
#include "webfuse/impl/jsonrpc/request.h" #include "webfuse/impl/jsonrpc/request.h"
#include "webfuse/impl/json/node.h"
#include "webfuse/impl/message.h" #include "webfuse/impl/message.h"
#include "webfuse/status.h" #include "webfuse/status.h"
#include "webfuse/test_util/json_doc.hpp"
#include <gtest/gtest.h>
using webfuse_test::JsonDoc;
namespace namespace
{ {
@ -48,19 +53,18 @@ TEST(wf_jsonrpc_request, respond)
wf_impl_jsonrpc_respond(request); wf_impl_jsonrpc_respond(request);
json_t * response = json_loads(context.response.c_str(), 0, nullptr); JsonDoc doc(context.response);
ASSERT_TRUE(json_is_object(response)); struct wf_json const * response = doc.root();
ASSERT_TRUE(wf_impl_json_is_object(response));
json_t * id = json_object_get(response, "id"); struct wf_json const * id = wf_impl_json_object_get(response, "id");
ASSERT_TRUE(json_is_integer(id)); ASSERT_TRUE(wf_impl_json_is_int(id));
ASSERT_EQ(42, json_integer_value(id)); ASSERT_EQ(42, wf_impl_json_int_get(id));
json_t * result = json_object_get(response, "result"); struct wf_json const * result = wf_impl_json_object_get(response, "result");
ASSERT_TRUE(json_is_object(result)); ASSERT_TRUE(wf_impl_json_is_object(result));
ASSERT_EQ(nullptr, json_object_get(response, "error")); ASSERT_TRUE(wf_impl_json_is_undefined(wf_impl_json_object_get(response, "error")));
json_decref(response);
} }
TEST(wf_jsonrpc_request, respond_error) TEST(wf_jsonrpc_request, respond_error)
@ -73,60 +77,24 @@ TEST(wf_jsonrpc_request, respond_error)
wf_impl_jsonrpc_respond_error(request, WF_BAD, "Bad"); wf_impl_jsonrpc_respond_error(request, WF_BAD, "Bad");
json_t * response = json_loads(context.response.c_str(), 0, nullptr); JsonDoc doc(context.response);
ASSERT_TRUE(json_is_object(response)); struct wf_json const * response = doc.root();
ASSERT_TRUE(wf_impl_json_is_object(response));
json_t * id = json_object_get(response, "id"); struct wf_json const * id = wf_impl_json_object_get(response, "id");
ASSERT_TRUE(json_is_integer(id)); ASSERT_TRUE(wf_impl_json_is_int(id));
ASSERT_EQ(42, json_integer_value(id)); ASSERT_EQ(42, wf_impl_json_int_get(id));
ASSERT_EQ(nullptr, json_object_get(response, "result")); ASSERT_TRUE(wf_impl_json_is_undefined(wf_impl_json_object_get(response, "result")));
json_t * err = json_object_get(response, "error"); struct wf_json const * err = wf_impl_json_object_get(response, "error");
ASSERT_TRUE(json_is_object(err)); ASSERT_TRUE(wf_impl_json_is_object(err));
json_t * err_code = json_object_get(err, "code"); struct wf_json const * err_code = wf_impl_json_object_get(err, "code");
ASSERT_TRUE(json_is_integer(err_code)); ASSERT_TRUE(wf_impl_json_is_int(err_code));
ASSERT_EQ(WF_BAD, json_integer_value(err_code)); ASSERT_EQ(WF_BAD, wf_impl_json_int_get(err_code));
json_t * err_message = json_object_get(err, "message"); struct wf_json const * err_message = wf_impl_json_object_get(err, "message");
ASSERT_TRUE(json_is_string(err_message)); ASSERT_TRUE(wf_impl_json_is_string(err_message));
ASSERT_STREQ("Bad", json_string_value(err_message)); ASSERT_STREQ("Bad", wf_impl_json_string_get(err_message));
json_decref(response);
}
TEST(wf_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));
ASSERT_TRUE(wf_impl_jsonrpc_is_request(request));
json_decref(request);
}
TEST(wf_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));
ASSERT_FALSE(wf_impl_jsonrpc_is_request(request));
json_decref(request);
}
TEST(wf_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));
ASSERT_FALSE(wf_impl_jsonrpc_is_request(request));
json_decref(request);
} }

View File

@ -1,37 +1,34 @@
#include <gtest/gtest.h>
#include "webfuse/impl/jsonrpc/response_intern.h" #include "webfuse/impl/jsonrpc/response_intern.h"
#include "webfuse/impl/jsonrpc/error.h" #include "webfuse/impl/jsonrpc/error.h"
#include "webfuse/impl/json/node.h"
#include "webfuse/status.h" #include "webfuse/status.h"
#include "webfuse/test_util/json_doc.hpp"
#include <gtest/gtest.h>
using webfuse_test::JsonDoc;
TEST(wf_json_response, init_result) TEST(wf_json_response, init_result)
{ {
json_t * message = json_object(); JsonDoc doc("{\"result\": 47, \"id\": 11}");
json_object_set_new(message, "result", json_integer(47));
json_object_set_new(message, "id", json_integer(11));
struct wf_jsonrpc_response response; struct wf_jsonrpc_response response;
wf_impl_jsonrpc_response_init(&response, message); wf_impl_jsonrpc_response_init(&response, doc.root());
ASSERT_EQ(nullptr, response.error); ASSERT_EQ(nullptr, response.error);
ASSERT_TRUE(json_is_integer(response.result)); ASSERT_TRUE(wf_impl_json_is_int(response.result));
ASSERT_EQ(47, json_integer_value(response.result)); ASSERT_EQ(47, wf_impl_json_int_get(response.result));
ASSERT_EQ(11, response.id); ASSERT_EQ(11, response.id);
wf_impl_jsonrpc_response_cleanup(&response); wf_impl_jsonrpc_response_cleanup(&response);
json_decref(message);
} }
TEST(wf_json_response, init_error) TEST(wf_json_response, init_error)
{ {
json_t * message = json_object(); JsonDoc doc("{\"error\": {\"code\": 42, \"message\": \"Don't Panic!\"}, \"id\": 23}");
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));
struct wf_jsonrpc_response response; struct wf_jsonrpc_response response;
wf_impl_jsonrpc_response_init(&response, message); wf_impl_jsonrpc_response_init(&response, doc.root());
ASSERT_EQ(42, wf_impl_jsonrpc_error_code(response.error)); ASSERT_EQ(42, wf_impl_jsonrpc_error_code(response.error));
ASSERT_STREQ("Don't Panic!", wf_impl_jsonrpc_error_message(response.error)); ASSERT_STREQ("Don't Panic!", wf_impl_jsonrpc_error_message(response.error));
@ -39,110 +36,88 @@ TEST(wf_json_response, init_error)
ASSERT_EQ(23, response.id); ASSERT_EQ(23, response.id);
wf_impl_jsonrpc_response_cleanup(&response); wf_impl_jsonrpc_response_cleanup(&response);
json_decref(message);
} }
TEST(wf_json_response, init_fail_missing_result_and_error) TEST(wf_json_response, init_fail_missing_result_and_error)
{ {
json_t * message = json_object(); JsonDoc doc("{\"id\": 12}");
json_object_set_new(message, "id", json_integer(12));
struct wf_jsonrpc_response response; struct wf_jsonrpc_response response;
wf_impl_jsonrpc_response_init(&response, message); wf_impl_jsonrpc_response_init(&response, doc.root());
ASSERT_EQ(WF_BAD_FORMAT, wf_impl_jsonrpc_error_code(response.error)); ASSERT_EQ(WF_BAD_FORMAT, wf_impl_jsonrpc_error_code(response.error));
ASSERT_EQ(nullptr, response.result); ASSERT_EQ(nullptr, response.result);
ASSERT_EQ(12, response.id); ASSERT_EQ(12, response.id);
wf_impl_jsonrpc_response_cleanup(&response); wf_impl_jsonrpc_response_cleanup(&response);
json_decref(message);
} }
TEST(wf_json_response, init_fail_missing_id) TEST(wf_json_response, init_fail_missing_id)
{ {
json_t * message = json_object(); JsonDoc doc("{\"result\": 47}");
json_object_set_new(message, "result", json_integer(47));
struct wf_jsonrpc_response response; struct wf_jsonrpc_response response;
wf_impl_jsonrpc_response_init(&response, message); wf_impl_jsonrpc_response_init(&response, doc.root());
ASSERT_EQ(WF_BAD_FORMAT, wf_impl_jsonrpc_error_code(response.error)); ASSERT_EQ(WF_BAD_FORMAT, wf_impl_jsonrpc_error_code(response.error));
ASSERT_EQ(nullptr, response.result); ASSERT_EQ(nullptr, response.result);
ASSERT_EQ(-1, response.id); ASSERT_EQ(-1, response.id);
wf_impl_jsonrpc_response_cleanup(&response); wf_impl_jsonrpc_response_cleanup(&response);
json_decref(message);
} }
TEST(wf_json_response, init_fail_wrong_id_type) TEST(wf_json_response, init_fail_wrong_id_type)
{ {
json_t * message = json_object(); JsonDoc doc("{\"result\": 47, \"id\": \"42\"}");
json_object_set_new(message, "result", json_integer(47));
json_object_set_new(message, "id", json_string("42"));
struct wf_jsonrpc_response response; struct wf_jsonrpc_response response;
wf_impl_jsonrpc_response_init(&response, message); wf_impl_jsonrpc_response_init(&response, doc.root());
ASSERT_EQ(WF_BAD_FORMAT, wf_impl_jsonrpc_error_code(response.error)); ASSERT_EQ(WF_BAD_FORMAT, wf_impl_jsonrpc_error_code(response.error));
ASSERT_EQ(nullptr, response.result); ASSERT_EQ(nullptr, response.result);
ASSERT_EQ(-1, response.id); ASSERT_EQ(-1, response.id);
wf_impl_jsonrpc_response_cleanup(&response); wf_impl_jsonrpc_response_cleanup(&response);
json_decref(message);
} }
TEST(wf_json_response, init_fail_error_missing_code) TEST(wf_json_response, init_fail_error_missing_code)
{ {
json_t * message = json_object(); JsonDoc doc("{\"error\": {\"message\": \"Don't Panic!\"}, \"id\": 23}");
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));
struct wf_jsonrpc_response response; struct wf_jsonrpc_response response;
wf_impl_jsonrpc_response_init(&response, message); wf_impl_jsonrpc_response_init(&response, doc.root());
ASSERT_EQ(WF_BAD_FORMAT, wf_impl_jsonrpc_error_code(response.error)); ASSERT_EQ(WF_BAD_FORMAT, wf_impl_jsonrpc_error_code(response.error));
ASSERT_EQ(nullptr, response.result); ASSERT_EQ(nullptr, response.result);
ASSERT_EQ(23, response.id); ASSERT_EQ(23, response.id);
wf_impl_jsonrpc_response_cleanup(&response); wf_impl_jsonrpc_response_cleanup(&response);
json_decref(message);
} }
TEST(wf_json_response, init_fail_error_wrong_code_type) TEST(wf_json_response, init_fail_error_wrong_code_type)
{ {
json_t * message = json_object(); JsonDoc doc("{\"error\": {\"code\": \"42\", \"message\": \"Don't Panic!\"}, \"id\": 23}");
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));
struct wf_jsonrpc_response response; struct wf_jsonrpc_response response;
wf_impl_jsonrpc_response_init(&response, message); wf_impl_jsonrpc_response_init(&response, doc.root());
ASSERT_EQ(WF_BAD_FORMAT, wf_impl_jsonrpc_error_code(response.error)); ASSERT_EQ(WF_BAD_FORMAT, wf_impl_jsonrpc_error_code(response.error));
ASSERT_EQ(nullptr, response.result); ASSERT_EQ(nullptr, response.result);
ASSERT_EQ(23, response.id); ASSERT_EQ(23, response.id);
wf_impl_jsonrpc_response_cleanup(&response); wf_impl_jsonrpc_response_cleanup(&response);
json_decref(message);
} }
TEST(wf_json_response, init_fail_error_wrong_type) TEST(wf_json_response, init_fail_error_wrong_type)
{ {
json_t * message = json_object(); JsonDoc doc("{\"error\": \"invalid error type\", \"id\": 23}");
json_object_set_new(message, "error", json_string("invalid error type"));
json_object_set_new(message, "id", json_integer(23));
struct wf_jsonrpc_response response; struct wf_jsonrpc_response response;
wf_impl_jsonrpc_response_init(&response, message); wf_impl_jsonrpc_response_init(&response, doc.root());
ASSERT_EQ(WF_BAD_FORMAT, wf_impl_jsonrpc_error_code(response.error)); ASSERT_EQ(WF_BAD_FORMAT, wf_impl_jsonrpc_error_code(response.error));
ASSERT_EQ(nullptr, response.result); ASSERT_EQ(nullptr, response.result);
ASSERT_EQ(23, response.id); ASSERT_EQ(23, response.id);
wf_impl_jsonrpc_response_cleanup(&response); wf_impl_jsonrpc_response_cleanup(&response);
json_decref(message);
} }

View File

@ -1,57 +1,68 @@
#include <string>
#include <gtest/gtest.h>
#include "webfuse/impl/jsonrpc/response_intern.h" #include "webfuse/impl/jsonrpc/response_intern.h"
#include "webfuse/impl/jsonrpc/error.h" #include "webfuse/impl/jsonrpc/error.h"
#include "webfuse/test_util/json_doc.hpp"
#include <gtest/gtest.h>
#include <string>
static void response_parse_str( using webfuse_test::JsonDoc;
std::string const & buffer,
struct wf_jsonrpc_response * response) TEST(response_parser, invalid_no_object)
{ {
json_t * message = json_loadb(buffer.c_str(), buffer.size(), 0, nullptr); JsonDoc doc("[]");
if (nullptr != message) struct wf_jsonrpc_response response;
{ wf_impl_jsonrpc_response_init(&response, doc.root());
wf_impl_jsonrpc_response_init(response, message);
json_decref(message); ASSERT_NE(nullptr, response.error);
} ASSERT_EQ(-1, response.id);
ASSERT_EQ(nullptr, response.result);
wf_impl_jsonrpc_response_cleanup(&response);
} }
TEST(response_parser, test) TEST(response_parser, invalid_empty_object)
{ {
JsonDoc doc("{}");
struct wf_jsonrpc_response response; struct wf_jsonrpc_response response;
wf_impl_jsonrpc_response_init(&response, doc.root());
// no object
response_parse_str("[]", &response);
ASSERT_NE(nullptr, response.error); ASSERT_NE(nullptr, response.error);
ASSERT_EQ(-1, response.id); ASSERT_EQ(-1, response.id);
ASSERT_EQ(nullptr, response.result); ASSERT_EQ(nullptr, response.result);
wf_impl_jsonrpc_response_cleanup(&response); wf_impl_jsonrpc_response_cleanup(&response);
}
// empty TEST(response_parser, invalid_missin_result_and_error)
response_parse_str("{}", &response); {
ASSERT_NE(nullptr, response.error); JsonDoc doc("{\"id\":42}");
ASSERT_EQ(-1, response.id); struct wf_jsonrpc_response response;
ASSERT_EQ(nullptr, response.result); wf_impl_jsonrpc_response_init(&response, doc.root());
wf_impl_jsonrpc_response_cleanup(&response);
// no data
response_parse_str("{\"id\":42}", &response);
ASSERT_NE(nullptr, response.error); ASSERT_NE(nullptr, response.error);
ASSERT_EQ(42, response.id); ASSERT_EQ(42, response.id);
ASSERT_EQ(nullptr, response.result); ASSERT_EQ(nullptr, response.result);
wf_impl_jsonrpc_response_cleanup(&response); wf_impl_jsonrpc_response_cleanup(&response);
}
TEST(response_parser, custom_error_code)
{
JsonDoc doc("{\"error\":{\"code\": 42}, \"id\": 42}");
struct wf_jsonrpc_response response;
wf_impl_jsonrpc_response_init(&response, doc.root());
// custom error code
response_parse_str("{\"error\":{\"code\": 42}, \"id\": 42}", &response);
ASSERT_NE(nullptr, response.error); ASSERT_NE(nullptr, response.error);
ASSERT_EQ(42, wf_impl_jsonrpc_error_code(response.error)); ASSERT_EQ(42, wf_impl_jsonrpc_error_code(response.error));
ASSERT_EQ(42, response.id); ASSERT_EQ(42, response.id);
ASSERT_EQ(nullptr, response.result); ASSERT_EQ(nullptr, response.result);
wf_impl_jsonrpc_response_cleanup(&response); wf_impl_jsonrpc_response_cleanup(&response);
}
TEST(response_parser, valid_response)
{
JsonDoc doc("{\"result\": true, \"id\": 42}");
struct wf_jsonrpc_response response;
wf_impl_jsonrpc_response_init(&response, doc.root());
// valid response
response_parse_str("{\"result\": true, \"id\": 42}", &response);
ASSERT_EQ(nullptr, response.error); ASSERT_EQ(nullptr, response.error);
ASSERT_EQ(42, response.id); ASSERT_EQ(42, response.id);
ASSERT_NE(nullptr, response.result); ASSERT_NE(nullptr, response.result);

View File

@ -1,14 +1,29 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "webfuse/impl/jsonrpc/server.h" #include "webfuse/impl/jsonrpc/server.h"
#include "webfuse/impl/jsonrpc/request.h" #include "webfuse/impl/jsonrpc/request.h"
#include "webfuse/impl/json/node.h"
#include "webfuse/impl/message.h" #include "webfuse/impl/message.h"
#include "webfuse/status.h" #include "webfuse/status.h"
#include "webfuse/test_util/json_doc.hpp"
using webfuse_test::JsonDoc;
namespace namespace
{ {
struct Context class Context
{ {
json_t * response; public:
Context()
: doc("{}")
, response(nullptr)
, is_called(false)
{
}
JsonDoc doc;
wf_json const * response;
bool is_called; bool is_called;
}; };
@ -18,7 +33,8 @@ namespace
{ {
Context * context = reinterpret_cast<Context*>(user_data); Context * context = reinterpret_cast<Context*>(user_data);
context->is_called = true; context->is_called = true;
context->response = json_loadb(request->data, request->length, 0, nullptr); context->doc = std::move(JsonDoc(std::string(request->data, request->length)));
context->response = context->doc.root();
wf_impl_message_dispose(request); wf_impl_message_dispose(request);
return true; return true;
@ -27,7 +43,7 @@ namespace
void sayHello( void sayHello(
struct wf_jsonrpc_request * request, struct wf_jsonrpc_request * request,
char const * method_name, char const * method_name,
json_t * params, wf_json const * params,
void * user_data) void * user_data)
{ {
(void) method_name; (void) method_name;
@ -44,27 +60,23 @@ TEST(wf_jsonrpc_server, process_request)
struct wf_jsonrpc_server * server = wf_impl_jsonrpc_server_create(); struct wf_jsonrpc_server * server = wf_impl_jsonrpc_server_create();
wf_impl_jsonrpc_server_add(server, "sayHello", &sayHello, nullptr); wf_impl_jsonrpc_server_add(server, "sayHello", &sayHello, nullptr);
Context context{nullptr, false}; Context context;
void * user_data = reinterpret_cast<void*>(&context); void * user_data = reinterpret_cast<void*>(&context);
json_t * request = json_object();
json_object_set_new(request, "method", json_string("sayHello")); JsonDoc request("{\"method\": \"sayHello\", \"params\": [], \"id\": 23}");
json_object_set_new(request, "params", json_array()); wf_impl_jsonrpc_server_process(server, request.root(), &jsonrpc_send, user_data);
json_object_set_new(request, "id", json_integer(23));
wf_impl_jsonrpc_server_process(server, request, &jsonrpc_send, user_data);
ASSERT_TRUE(context.is_called); ASSERT_TRUE(context.is_called);
ASSERT_NE(nullptr, context.response); ASSERT_NE(nullptr, context.response);
ASSERT_TRUE(json_is_object(context.response)); ASSERT_TRUE(wf_impl_json_is_object(context.response));
json_t * id = json_object_get(context.response, "id"); struct wf_json const * id = wf_impl_json_object_get(context.response, "id");
ASSERT_TRUE(json_is_integer(id)); ASSERT_TRUE(wf_impl_json_is_int(id));
ASSERT_EQ(23, json_integer_value(id)); ASSERT_EQ(23, wf_impl_json_int_get(id));
json_t * result = json_object_get(context.response, "result"); struct wf_json const * result = wf_impl_json_object_get(context.response, "result");
ASSERT_TRUE(json_is_object(result)); ASSERT_TRUE(wf_impl_json_is_object(result));
json_decref(context.response);
json_decref(request);
wf_impl_jsonrpc_server_dispose(server); wf_impl_jsonrpc_server_dispose(server);
} }
@ -73,27 +85,22 @@ TEST(wf_jsonrpc_server, process_request_with_object_params)
struct wf_jsonrpc_server * server = wf_impl_jsonrpc_server_create(); struct wf_jsonrpc_server * server = wf_impl_jsonrpc_server_create();
wf_impl_jsonrpc_server_add(server, "sayHello", &sayHello, nullptr); wf_impl_jsonrpc_server_add(server, "sayHello", &sayHello, nullptr);
Context context{nullptr, false}; Context context;
void * user_data = reinterpret_cast<void*>(&context); void * user_data = reinterpret_cast<void*>(&context);
json_t * request = json_object(); JsonDoc request("{\"method\": \"sayHello\", \"params\": {}, \"id\": 23}");
json_object_set_new(request, "method", json_string("sayHello")); wf_impl_jsonrpc_server_process(server, request.root(), &jsonrpc_send, user_data);
json_object_set_new(request, "params", json_object());
json_object_set_new(request, "id", json_integer(23));
wf_impl_jsonrpc_server_process(server, request, &jsonrpc_send, user_data);
ASSERT_TRUE(context.is_called); ASSERT_TRUE(context.is_called);
ASSERT_NE(nullptr, context.response); ASSERT_NE(nullptr, context.response);
ASSERT_TRUE(json_is_object(context.response)); ASSERT_TRUE(wf_impl_json_is_object(context.response));
json_t * id = json_object_get(context.response, "id"); struct wf_json const * id = wf_impl_json_object_get(context.response, "id");
ASSERT_TRUE(json_is_integer(id)); ASSERT_TRUE(wf_impl_json_is_int(id));
ASSERT_EQ(23, json_integer_value(id)); ASSERT_EQ(23, wf_impl_json_int_get(id));
json_t * result = json_object_get(context.response, "result"); struct wf_json const * result = wf_impl_json_object_get(context.response, "result");
ASSERT_TRUE(json_is_object(result)); ASSERT_TRUE(wf_impl_json_is_object(result));
json_decref(context.response);
json_decref(request);
wf_impl_jsonrpc_server_dispose(server); wf_impl_jsonrpc_server_dispose(server);
} }
@ -102,34 +109,29 @@ TEST(wf_jsonrpc_server, invoke_unknown_method)
struct wf_jsonrpc_server * server = wf_impl_jsonrpc_server_create(); struct wf_jsonrpc_server * server = wf_impl_jsonrpc_server_create();
wf_impl_jsonrpc_server_add(server, "sayHello", &sayHello, nullptr); wf_impl_jsonrpc_server_add(server, "sayHello", &sayHello, nullptr);
Context context{nullptr, false}; Context context;
void * user_data = reinterpret_cast<void*>(&context); void * user_data = reinterpret_cast<void*>(&context);
json_t * request = json_object(); JsonDoc request("{\"method\": \"greet\", \"params\": [], \"id\": 42}");
json_object_set_new(request, "method", json_string("greet")); wf_impl_jsonrpc_server_process(server, request.root(), &jsonrpc_send, user_data);
json_object_set_new(request, "params", json_array());
json_object_set_new(request, "id", json_integer(42));
wf_impl_jsonrpc_server_process(server, request, &jsonrpc_send, user_data);
ASSERT_TRUE(context.is_called); ASSERT_TRUE(context.is_called);
ASSERT_NE(nullptr, context.response); ASSERT_NE(nullptr, context.response);
ASSERT_TRUE(json_is_object(context.response)); ASSERT_TRUE(wf_impl_json_is_object(context.response));
json_t * id = json_object_get(context.response, "id"); struct wf_json const * id = wf_impl_json_object_get(context.response, "id");
ASSERT_TRUE(json_is_integer(id)); ASSERT_TRUE(wf_impl_json_is_int(id));
ASSERT_EQ(42, json_integer_value(id)); ASSERT_EQ(42, wf_impl_json_int_get(id));
json_t * err = json_object_get(context.response, "error"); struct wf_json const * err = wf_impl_json_object_get(context.response, "error");
ASSERT_TRUE(json_is_object(err)); ASSERT_TRUE(wf_impl_json_is_object(err));
json_t * err_code = json_object_get(err, "code"); struct wf_json const * err_code = wf_impl_json_object_get(err, "code");
ASSERT_TRUE(json_is_integer(err_code)); ASSERT_TRUE(wf_impl_json_is_int(err_code));
ASSERT_EQ(WF_BAD_NOTIMPLEMENTED, json_integer_value(err_code)); ASSERT_EQ(WF_BAD_NOTIMPLEMENTED, wf_impl_json_int_get(err_code));
json_t * err_message = json_object_get(err, "message"); struct wf_json const * err_message = wf_impl_json_object_get(err, "message");
ASSERT_TRUE(json_is_string(err_message)); ASSERT_TRUE(wf_impl_json_is_string(err_message));
json_decref(context.response);
json_decref(request);
wf_impl_jsonrpc_server_dispose(server); wf_impl_jsonrpc_server_dispose(server);
} }
@ -137,16 +139,13 @@ TEST(wf_jsonrpc_server, skip_invalid_request_missing_id)
{ {
struct wf_jsonrpc_server * server = wf_impl_jsonrpc_server_create(); struct wf_jsonrpc_server * server = wf_impl_jsonrpc_server_create();
Context context{nullptr, false}; Context context;
void * user_data = reinterpret_cast<void*>(&context); void * user_data = reinterpret_cast<void*>(&context);
json_t * request = json_object(); JsonDoc request("{\"method\": \"sayHello\", \"params\": []}");
json_object_set_new(request, "method", json_string("sayHello")); wf_impl_jsonrpc_server_process(server, request.root(), &jsonrpc_send, user_data);
json_object_set_new(request, "params", json_array());
wf_impl_jsonrpc_server_process(server, request, &jsonrpc_send, user_data);
ASSERT_FALSE(context.is_called); ASSERT_FALSE(context.is_called);
json_decref(request);
wf_impl_jsonrpc_server_dispose(server); wf_impl_jsonrpc_server_dispose(server);
} }
@ -154,17 +153,13 @@ TEST(wf_jsonrpc_server, skip_invalid_request_wrong_id_type)
{ {
struct wf_jsonrpc_server * server = wf_impl_jsonrpc_server_create(); struct wf_jsonrpc_server * server = wf_impl_jsonrpc_server_create();
Context context{nullptr, false}; Context context;
void * user_data = reinterpret_cast<void*>(&context); void * user_data = reinterpret_cast<void*>(&context);
json_t * request = json_object(); JsonDoc request("{\"method\": \"sayHello\", \"params\": [], \"id\": \"42\"}");
json_object_set_new(request, "method", json_string("sayHello")); wf_impl_jsonrpc_server_process(server, request.root(), &jsonrpc_send, user_data);
json_object_set_new(request, "params", json_array());
json_object_set_new(request, "id", json_string("42"));
wf_impl_jsonrpc_server_process(server, request, &jsonrpc_send, user_data);
ASSERT_FALSE(context.is_called); ASSERT_FALSE(context.is_called);
json_decref(request);
wf_impl_jsonrpc_server_dispose(server); wf_impl_jsonrpc_server_dispose(server);
} }
@ -172,16 +167,13 @@ TEST(wf_jsonrpc_server, skip_invalid_request_missing_params)
{ {
struct wf_jsonrpc_server * server = wf_impl_jsonrpc_server_create(); struct wf_jsonrpc_server * server = wf_impl_jsonrpc_server_create();
Context context{nullptr, false}; Context context;
void * user_data = reinterpret_cast<void*>(&context); void * user_data = reinterpret_cast<void*>(&context);
json_t * request = json_object(); JsonDoc request("{\"method\": \"sayHello\", \"id\": 23}");
json_object_set_new(request, "method", json_string("sayHello")); wf_impl_jsonrpc_server_process(server, request.root(), &jsonrpc_send, user_data);
json_object_set_new(request, "id", json_integer(42));
wf_impl_jsonrpc_server_process(server, request, &jsonrpc_send, user_data);
ASSERT_FALSE(context.is_called); ASSERT_FALSE(context.is_called);
json_decref(request);
wf_impl_jsonrpc_server_dispose(server); wf_impl_jsonrpc_server_dispose(server);
} }
@ -189,17 +181,13 @@ TEST(wf_jsonrpc_server, skip_invalid_request_wrong_params_type)
{ {
struct wf_jsonrpc_server * server = wf_impl_jsonrpc_server_create(); struct wf_jsonrpc_server * server = wf_impl_jsonrpc_server_create();
Context context{nullptr, false}; Context context;
void * user_data = reinterpret_cast<void*>(&context); void * user_data = reinterpret_cast<void*>(&context);
json_t * request = json_object(); JsonDoc request("{\"method\": \"sayHello\", \"params\": \"invalid\", \"id\": 42}");
json_object_set_new(request, "method", json_string("sayHello")); wf_impl_jsonrpc_server_process(server, request.root(), &jsonrpc_send, user_data);
json_object_set_new(request, "params", json_string("invalid"));
json_object_set_new(request, "id", json_integer(42));
wf_impl_jsonrpc_server_process(server, request, &jsonrpc_send, user_data);
ASSERT_FALSE(context.is_called); ASSERT_FALSE(context.is_called);
json_decref(request);
wf_impl_jsonrpc_server_dispose(server); wf_impl_jsonrpc_server_dispose(server);
} }
@ -207,16 +195,13 @@ TEST(wf_jsonrpc_server, skip_invalid_request_missing_method)
{ {
struct wf_jsonrpc_server * server = wf_impl_jsonrpc_server_create(); struct wf_jsonrpc_server * server = wf_impl_jsonrpc_server_create();
Context context{nullptr, false}; Context context;
void * user_data = reinterpret_cast<void*>(&context); void * user_data = reinterpret_cast<void*>(&context);
json_t * request = json_object(); JsonDoc request("{\"params\": [], \"id\": 23}");
json_object_set_new(request, "params", json_array()); wf_impl_jsonrpc_server_process(server, request.root(), &jsonrpc_send, user_data);
json_object_set_new(request, "id", json_integer(42));
wf_impl_jsonrpc_server_process(server, request, &jsonrpc_send, user_data);
ASSERT_FALSE(context.is_called); ASSERT_FALSE(context.is_called);
json_decref(request);
wf_impl_jsonrpc_server_dispose(server); wf_impl_jsonrpc_server_dispose(server);
} }
@ -224,16 +209,12 @@ TEST(wf_jsonrpc_server, skip_invalid_request_wront_method_type)
{ {
struct wf_jsonrpc_server * server = wf_impl_jsonrpc_server_create(); struct wf_jsonrpc_server * server = wf_impl_jsonrpc_server_create();
Context context{nullptr, false}; Context context;
void * user_data = reinterpret_cast<void*>(&context); void * user_data = reinterpret_cast<void*>(&context);
json_t * request = json_object(); JsonDoc request("{\"method\": 42, \"params\": [], \"id\": 23}");
json_object_set_new(request, "method", json_integer(42)); wf_impl_jsonrpc_server_process(server, request.root(), &jsonrpc_send, user_data);
json_object_set_new(request, "params", json_array());
json_object_set_new(request, "id", json_integer(42));
wf_impl_jsonrpc_server_process(server, request, &jsonrpc_send, user_data);
ASSERT_FALSE(context.is_called); ASSERT_FALSE(context.is_called);
json_decref(request);
wf_impl_jsonrpc_server_dispose(server); wf_impl_jsonrpc_server_dispose(server);
} }

View File

@ -3,12 +3,14 @@
#include "webfuse/status.h" #include "webfuse/status.h"
#include "webfuse/test_util/json_doc.hpp"
#include "webfuse/mocks/mock_fuse.hpp" #include "webfuse/mocks/mock_fuse.hpp"
#include "webfuse/mocks/mock_operation_context.hpp" #include "webfuse/mocks/mock_operation_context.hpp"
#include "webfuse/mocks/mock_jsonrpc_proxy.hpp" #include "webfuse/mocks/mock_jsonrpc_proxy.hpp"
#include <gtest/gtest.h> #include <gtest/gtest.h>
using webfuse_test::JsonDoc;
using webfuse_test::MockJsonRpcProxy; using webfuse_test::MockJsonRpcProxy;
using webfuse_test::MockOperationContext; using webfuse_test::MockOperationContext;
using webfuse_test::FuseMock; using webfuse_test::FuseMock;
@ -80,16 +82,13 @@ TEST(wf_impl_operation_getattr, finished_file)
FuseMock fuse; FuseMock fuse;
EXPECT_CALL(fuse, fuse_reply_attr(_,_,_)).Times(1).WillOnce(Return(0)); EXPECT_CALL(fuse, fuse_reply_attr(_,_,_)).Times(1).WillOnce(Return(0));
json_t * result = json_object(); JsonDoc result("{\"mode\": 493, \"type\": \"file\"}");
json_object_set_new(result, "mode", json_integer(0755));
json_object_set_new(result, "type", json_string("file"));
auto * context = reinterpret_cast<wf_impl_operation_getattr_context*>(malloc(sizeof(wf_impl_operation_getattr_context))); auto * context = reinterpret_cast<wf_impl_operation_getattr_context*>(malloc(sizeof(wf_impl_operation_getattr_context)));
context->inode = 1; context->inode = 1;
context->gid = 0; context->gid = 0;
context->uid = 0; context->uid = 0;
wf_impl_operation_getattr_finished(context, result, nullptr); wf_impl_operation_getattr_finished(context, result.root(), nullptr);
json_decref(result);
} }
TEST(wf_impl_operation_getattr, finished_dir) TEST(wf_impl_operation_getattr, finished_dir)
@ -97,16 +96,13 @@ TEST(wf_impl_operation_getattr, finished_dir)
FuseMock fuse; FuseMock fuse;
EXPECT_CALL(fuse, fuse_reply_attr(_,_,_)).Times(1).WillOnce(Return(0)); EXPECT_CALL(fuse, fuse_reply_attr(_,_,_)).Times(1).WillOnce(Return(0));
json_t * result = json_object(); JsonDoc result("{\"mode\": 493, \"type\": \"dir\"}");
json_object_set_new(result, "mode", json_integer(0755));
json_object_set_new(result, "type", json_string("dir"));
auto * context = reinterpret_cast<wf_impl_operation_getattr_context*>(malloc(sizeof(wf_impl_operation_getattr_context))); auto * context = reinterpret_cast<wf_impl_operation_getattr_context*>(malloc(sizeof(wf_impl_operation_getattr_context)));
context->inode = 1; context->inode = 1;
context->gid = 0; context->gid = 0;
context->uid = 0; context->uid = 0;
wf_impl_operation_getattr_finished(context, result, nullptr); wf_impl_operation_getattr_finished(context, result.root(), nullptr);
json_decref(result);
} }
TEST(wf_impl_operation_getattr, finished_unknown_type) TEST(wf_impl_operation_getattr, finished_unknown_type)
@ -114,16 +110,13 @@ TEST(wf_impl_operation_getattr, finished_unknown_type)
FuseMock fuse; FuseMock fuse;
EXPECT_CALL(fuse, fuse_reply_attr(_,_,_)).Times(1).WillOnce(Return(0)); EXPECT_CALL(fuse, fuse_reply_attr(_,_,_)).Times(1).WillOnce(Return(0));
json_t * result = json_object(); JsonDoc result("{\"mode\": 493, \"type\": \"unknown\"}");
json_object_set_new(result, "mode", json_integer(0755));
json_object_set_new(result, "type", json_string("unknown"));
auto * context = reinterpret_cast<wf_impl_operation_getattr_context*>(malloc(sizeof(wf_impl_operation_getattr_context))); auto * context = reinterpret_cast<wf_impl_operation_getattr_context*>(malloc(sizeof(wf_impl_operation_getattr_context)));
context->inode = 1; context->inode = 1;
context->gid = 0; context->gid = 0;
context->uid = 0; context->uid = 0;
wf_impl_operation_getattr_finished(context, result, nullptr); wf_impl_operation_getattr_finished(context, result.root(), nullptr);
json_decref(result);
} }
TEST(wf_impl_operation_getattr, finished_fail_missing_mode) TEST(wf_impl_operation_getattr, finished_fail_missing_mode)
@ -132,15 +125,13 @@ TEST(wf_impl_operation_getattr, finished_fail_missing_mode)
EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(0); EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(0);
EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0)); EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0));
json_t * result = json_object(); JsonDoc result("{\"type\": \"file\"}");
json_object_set_new(result, "type", json_string("file"));
auto * context = reinterpret_cast<wf_impl_operation_getattr_context*>(malloc(sizeof(wf_impl_operation_getattr_context))); auto * context = reinterpret_cast<wf_impl_operation_getattr_context*>(malloc(sizeof(wf_impl_operation_getattr_context)));
context->inode = 1; context->inode = 1;
context->gid = 0; context->gid = 0;
context->uid = 0; context->uid = 0;
wf_impl_operation_getattr_finished(context, result, nullptr); wf_impl_operation_getattr_finished(context, result.root(), nullptr);
json_decref(result);
} }
TEST(wf_impl_operation_getattr, finished_fail_invalid_mode_type) TEST(wf_impl_operation_getattr, finished_fail_invalid_mode_type)
@ -149,33 +140,28 @@ TEST(wf_impl_operation_getattr, finished_fail_invalid_mode_type)
EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(0); EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(0);
EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0)); EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0));
json_t * result = json_object(); JsonDoc result("{\"mode\": \"0755\", \"type\": \"file\"}");
json_object_set_new(result, "mode", json_string("0755"));
json_object_set_new(result, "type", json_string("file"));
auto * context = reinterpret_cast<wf_impl_operation_getattr_context*>(malloc(sizeof(wf_impl_operation_getattr_context))); auto * context = reinterpret_cast<wf_impl_operation_getattr_context*>(malloc(sizeof(wf_impl_operation_getattr_context)));
context->inode = 1; context->inode = 1;
context->gid = 0; context->gid = 0;
context->uid = 0; context->uid = 0;
wf_impl_operation_getattr_finished(context, result, nullptr); wf_impl_operation_getattr_finished(context, result.root(), nullptr);
json_decref(result);
} }
TEST(wf_impl_operation_getattr, finished_fail_type_mode) TEST(wf_impl_operation_getattr, finished_fail_missing_type)
{ {
FuseMock fuse; FuseMock fuse;
EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(0); EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(0);
EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0)); EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0));
json_t * result = json_object(); JsonDoc result("{\"mode\": 493}");
json_object_set_new(result, "mode", json_integer(0755));
auto * context = reinterpret_cast<wf_impl_operation_getattr_context*>(malloc(sizeof(wf_impl_operation_getattr_context))); auto * context = reinterpret_cast<wf_impl_operation_getattr_context*>(malloc(sizeof(wf_impl_operation_getattr_context)));
context->inode = 1; context->inode = 1;
context->gid = 0; context->gid = 0;
context->uid = 0; context->uid = 0;
wf_impl_operation_getattr_finished(context, result, nullptr); wf_impl_operation_getattr_finished(context, result.root(), nullptr);
json_decref(result);
} }
TEST(wf_impl_operation_getattr, finished_fail_invalid_type_type) TEST(wf_impl_operation_getattr, finished_fail_invalid_type_type)
@ -184,16 +170,13 @@ TEST(wf_impl_operation_getattr, finished_fail_invalid_type_type)
EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(0); EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(0);
EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0)); EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0));
json_t * result = json_object(); JsonDoc result("{\"mode\": 493, \"type\": 42}");
json_object_set_new(result, "mode", json_integer(0755));
json_object_set_new(result, "type", json_integer(42));
auto * context = reinterpret_cast<wf_impl_operation_getattr_context*>(malloc(sizeof(wf_impl_operation_getattr_context))); auto * context = reinterpret_cast<wf_impl_operation_getattr_context*>(malloc(sizeof(wf_impl_operation_getattr_context)));
context->inode = 1; context->inode = 1;
context->gid = 0; context->gid = 0;
context->uid = 0; context->uid = 0;
wf_impl_operation_getattr_finished(context, result, nullptr); wf_impl_operation_getattr_finished(context, result.root(), nullptr);
json_decref(result);
} }
TEST(wf_impl_operation_getattr, finished_error) TEST(wf_impl_operation_getattr, finished_error)

View File

@ -3,12 +3,14 @@
#include "webfuse/status.h" #include "webfuse/status.h"
#include "webfuse/test_util/json_doc.hpp"
#include "webfuse/mocks/mock_fuse.hpp" #include "webfuse/mocks/mock_fuse.hpp"
#include "webfuse/mocks/mock_operation_context.hpp" #include "webfuse/mocks/mock_operation_context.hpp"
#include "webfuse/mocks/mock_jsonrpc_proxy.hpp" #include "webfuse/mocks/mock_jsonrpc_proxy.hpp"
#include <gtest/gtest.h> #include <gtest/gtest.h>
using webfuse_test::JsonDoc;
using webfuse_test::MockJsonRpcProxy; using webfuse_test::MockJsonRpcProxy;
using webfuse_test::MockOperationContext; using webfuse_test::MockOperationContext;
using webfuse_test::FuseMock; using webfuse_test::FuseMock;
@ -78,17 +80,12 @@ TEST(wf_impl_operation_lookup, finished_file)
FuseMock fuse; FuseMock fuse;
EXPECT_CALL(fuse, fuse_reply_entry(_,_)).Times(1).WillOnce(Return(0)); EXPECT_CALL(fuse, fuse_reply_entry(_,_)).Times(1).WillOnce(Return(0));
json_t * result = json_object(); JsonDoc result("{\"inode\": 42, \"mode\": 493, \"type\": \"file\"}");
json_object_set_new(result, "inode", json_integer(42));
json_object_set_new(result, "mode", json_integer(0755));
json_object_set_new(result, "type", json_string("file"));
auto * context = reinterpret_cast<wf_impl_operation_lookup_context*>(malloc(sizeof(wf_impl_operation_lookup_context))); auto * context = reinterpret_cast<wf_impl_operation_lookup_context*>(malloc(sizeof(wf_impl_operation_lookup_context)));
context->timeout = 1.0; context->timeout = 1.0;
context->gid = 0; context->gid = 0;
context->uid = 0; context->uid = 0;
wf_impl_operation_lookup_finished(context, result, nullptr); wf_impl_operation_lookup_finished(context, result.root(), nullptr);
json_decref(result);
} }
TEST(wf_impl_operation_lookup, finished_dir) TEST(wf_impl_operation_lookup, finished_dir)
@ -96,17 +93,12 @@ TEST(wf_impl_operation_lookup, finished_dir)
FuseMock fuse; FuseMock fuse;
EXPECT_CALL(fuse, fuse_reply_entry(_,_)).Times(1).WillOnce(Return(0)); EXPECT_CALL(fuse, fuse_reply_entry(_,_)).Times(1).WillOnce(Return(0));
json_t * result = json_object(); JsonDoc result("{\"inode\": 42, \"mode\": 493, \"type\": \"dir\"}");
json_object_set_new(result, "inode", json_integer(42));
json_object_set_new(result, "mode", json_integer(0755));
json_object_set_new(result, "type", json_string("dir"));
auto * context = reinterpret_cast<wf_impl_operation_lookup_context*>(malloc(sizeof(wf_impl_operation_lookup_context))); auto * context = reinterpret_cast<wf_impl_operation_lookup_context*>(malloc(sizeof(wf_impl_operation_lookup_context)));
context->timeout = 1.0; context->timeout = 1.0;
context->gid = 0; context->gid = 0;
context->uid = 0; context->uid = 0;
wf_impl_operation_lookup_finished(context, result, nullptr); wf_impl_operation_lookup_finished(context, result.root(), nullptr);
json_decref(result);
} }
TEST(wf_impl_operation_lookup, finished_unknown_type) TEST(wf_impl_operation_lookup, finished_unknown_type)
@ -114,17 +106,12 @@ TEST(wf_impl_operation_lookup, finished_unknown_type)
FuseMock fuse; FuseMock fuse;
EXPECT_CALL(fuse, fuse_reply_entry(_,_)).Times(1).WillOnce(Return(0)); EXPECT_CALL(fuse, fuse_reply_entry(_,_)).Times(1).WillOnce(Return(0));
json_t * result = json_object(); JsonDoc result("{\"inode\": 42, \"mode\": 493, \"type\": \"unknown\"}");
json_object_set_new(result, "inode", json_integer(42));
json_object_set_new(result, "mode", json_integer(0755));
json_object_set_new(result, "type", json_string("unknown"));
auto * context = reinterpret_cast<wf_impl_operation_lookup_context*>(malloc(sizeof(wf_impl_operation_lookup_context))); auto * context = reinterpret_cast<wf_impl_operation_lookup_context*>(malloc(sizeof(wf_impl_operation_lookup_context)));
context->timeout = 1.0; context->timeout = 1.0;
context->gid = 0; context->gid = 0;
context->uid = 0; context->uid = 0;
wf_impl_operation_lookup_finished(context, result, nullptr); wf_impl_operation_lookup_finished(context, result.root(), nullptr);
json_decref(result);
} }
TEST(wf_impl_operation_lookup, finished_fail_missing_inode) TEST(wf_impl_operation_lookup, finished_fail_missing_inode)
@ -133,16 +120,12 @@ TEST(wf_impl_operation_lookup, finished_fail_missing_inode)
EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(0); EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(0);
EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0)); EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0));
json_t * result = json_object(); JsonDoc result("{\"mode\": 493, \"type\": \"file\"}");
json_object_set_new(result, "mode", json_integer(0755));
json_object_set_new(result, "type", json_string("file"));
auto * context = reinterpret_cast<wf_impl_operation_lookup_context*>(malloc(sizeof(wf_impl_operation_lookup_context))); auto * context = reinterpret_cast<wf_impl_operation_lookup_context*>(malloc(sizeof(wf_impl_operation_lookup_context)));
context->timeout = 1.0; context->timeout = 1.0;
context->gid = 0; context->gid = 0;
context->uid = 0; context->uid = 0;
wf_impl_operation_lookup_finished(context, result, nullptr); wf_impl_operation_lookup_finished(context, result.root(), nullptr);
json_decref(result);
} }
TEST(wf_impl_operation_lookup, finished_fail_invalid_inode_type) TEST(wf_impl_operation_lookup, finished_fail_invalid_inode_type)
@ -151,17 +134,12 @@ TEST(wf_impl_operation_lookup, finished_fail_invalid_inode_type)
EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(0); EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(0);
EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0)); EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0));
json_t * result = json_object(); JsonDoc result("{\"inode\": \"42\", \"mode\": 493, \"type\": \"file\"}");
json_object_set_new(result, "inode", json_string("42"));
json_object_set_new(result, "mode", json_string("0755"));
json_object_set_new(result, "type", json_string("file"));
auto * context = reinterpret_cast<wf_impl_operation_lookup_context*>(malloc(sizeof(wf_impl_operation_lookup_context))); auto * context = reinterpret_cast<wf_impl_operation_lookup_context*>(malloc(sizeof(wf_impl_operation_lookup_context)));
context->timeout = 1.0; context->timeout = 1.0;
context->gid = 0; context->gid = 0;
context->uid = 0; context->uid = 0;
wf_impl_operation_lookup_finished(context, result, nullptr); wf_impl_operation_lookup_finished(context, result.root(), nullptr);
json_decref(result);
} }
TEST(wf_impl_operation_lookup, finished_fail_missing_mode) TEST(wf_impl_operation_lookup, finished_fail_missing_mode)
@ -170,16 +148,12 @@ TEST(wf_impl_operation_lookup, finished_fail_missing_mode)
EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(0); EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(0);
EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0)); EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0));
json_t * result = json_object(); JsonDoc result("{\"inode\": 42, \"type\": \"file\"}");
json_object_set_new(result, "inode", json_integer(42));
json_object_set_new(result, "type", json_string("file"));
auto * context = reinterpret_cast<wf_impl_operation_lookup_context*>(malloc(sizeof(wf_impl_operation_lookup_context))); auto * context = reinterpret_cast<wf_impl_operation_lookup_context*>(malloc(sizeof(wf_impl_operation_lookup_context)));
context->timeout = 1.0; context->timeout = 1.0;
context->gid = 0; context->gid = 0;
context->uid = 0; context->uid = 0;
wf_impl_operation_lookup_finished(context, result, nullptr); wf_impl_operation_lookup_finished(context, result.root(), nullptr);
json_decref(result);
} }
TEST(wf_impl_operation_lookup, finished_fail_invalid_mode_type) TEST(wf_impl_operation_lookup, finished_fail_invalid_mode_type)
@ -188,35 +162,26 @@ TEST(wf_impl_operation_lookup, finished_fail_invalid_mode_type)
EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(0); EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(0);
EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0)); EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0));
json_t * result = json_object(); JsonDoc result("{\"inode\": 42, \"mode\": \"0755\", \"type\": \"file\"}");
json_object_set_new(result, "inode", json_integer(42));
json_object_set_new(result, "mode", json_string("0755"));
json_object_set_new(result, "type", json_string("file"));
auto * context = reinterpret_cast<wf_impl_operation_lookup_context*>(malloc(sizeof(wf_impl_operation_lookup_context))); auto * context = reinterpret_cast<wf_impl_operation_lookup_context*>(malloc(sizeof(wf_impl_operation_lookup_context)));
context->timeout = 1.0; context->timeout = 1.0;
context->gid = 0; context->gid = 0;
context->uid = 0; context->uid = 0;
wf_impl_operation_lookup_finished(context, result, nullptr); wf_impl_operation_lookup_finished(context, result.root(), nullptr);
json_decref(result);
} }
TEST(wf_impl_operation_lookup, finished_fail_type_mode) TEST(wf_impl_operation_lookup, finished_fail_missing_type)
{ {
FuseMock fuse; FuseMock fuse;
EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(0); EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(0);
EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0)); EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0));
json_t * result = json_object(); JsonDoc result("{\"inode\": 42, \"mode\": 493}");
json_object_set_new(result, "inode", json_integer(42));
json_object_set_new(result, "mode", json_integer(0755));
auto * context = reinterpret_cast<wf_impl_operation_lookup_context*>(malloc(sizeof(wf_impl_operation_lookup_context))); auto * context = reinterpret_cast<wf_impl_operation_lookup_context*>(malloc(sizeof(wf_impl_operation_lookup_context)));
context->timeout = 1.0; context->timeout = 1.0;
context->gid = 0; context->gid = 0;
context->uid = 0; context->uid = 0;
wf_impl_operation_lookup_finished(context, result, nullptr); wf_impl_operation_lookup_finished(context, result.root(), nullptr);
json_decref(result);
} }
TEST(wf_impl_operation_lookup, finished_fail_invalid_type_type) TEST(wf_impl_operation_lookup, finished_fail_invalid_type_type)
@ -225,17 +190,12 @@ TEST(wf_impl_operation_lookup, finished_fail_invalid_type_type)
EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(0); EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(0);
EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0)); EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0));
json_t * result = json_object(); JsonDoc result("{\"inode\": 42, \"mode\": 493, \"type\": 42}");
json_object_set_new(result, "inode", json_integer(42));
json_object_set_new(result, "mode", json_integer(0755));
json_object_set_new(result, "type", json_integer(42));
auto * context = reinterpret_cast<wf_impl_operation_lookup_context*>(malloc(sizeof(wf_impl_operation_lookup_context))); auto * context = reinterpret_cast<wf_impl_operation_lookup_context*>(malloc(sizeof(wf_impl_operation_lookup_context)));
context->timeout = 1.0; context->timeout = 1.0;
context->gid = 0; context->gid = 0;
context->uid = 0; context->uid = 0;
wf_impl_operation_lookup_finished(context, result, nullptr); wf_impl_operation_lookup_finished(context, result.root(), nullptr);
json_decref(result);
} }
TEST(wf_impl_operation_lookup, finished_error) TEST(wf_impl_operation_lookup, finished_error)

View File

@ -3,12 +3,14 @@
#include "webfuse/status.h" #include "webfuse/status.h"
#include "webfuse/test_util/json_doc.hpp"
#include "webfuse/mocks/mock_fuse.hpp" #include "webfuse/mocks/mock_fuse.hpp"
#include "webfuse/mocks/mock_operation_context.hpp" #include "webfuse/mocks/mock_operation_context.hpp"
#include "webfuse/mocks/mock_jsonrpc_proxy.hpp" #include "webfuse/mocks/mock_jsonrpc_proxy.hpp"
#include <gtest/gtest.h> #include <gtest/gtest.h>
using webfuse_test::JsonDoc;
using webfuse_test::MockJsonRpcProxy; using webfuse_test::MockJsonRpcProxy;
using webfuse_test::MockOperationContext; using webfuse_test::MockOperationContext;
using webfuse_test::FuseMock; using webfuse_test::FuseMock;
@ -59,10 +61,8 @@ TEST(wf_impl_operation_open, finished)
FuseMock fuse; FuseMock fuse;
EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(1).WillOnce(Return(0)); EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(1).WillOnce(Return(0));
json_t * result = json_object(); JsonDoc result("{\"handle\": 42}");
json_object_set_new(result, "handle", json_integer(42)); wf_impl_operation_open_finished(nullptr, result.root(), nullptr);
wf_impl_operation_open_finished(nullptr, result, nullptr);
json_decref(result);
} }
TEST(wf_impl_operation_open, finished_fail_error) TEST(wf_impl_operation_open, finished_fail_error)
@ -82,9 +82,8 @@ TEST(wf_impl_operation_open, finished_fail_no_handle)
EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(0); EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(0);
EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0)); EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0));
json_t * result = json_object(); JsonDoc result("{}");
wf_impl_operation_open_finished(nullptr, result, nullptr); wf_impl_operation_open_finished(nullptr, result.root(), nullptr);
json_decref(result);
} }
TEST(wf_impl_operation_open, finished_fail_invalid_handle_type) TEST(wf_impl_operation_open, finished_fail_invalid_handle_type)
@ -93,8 +92,6 @@ TEST(wf_impl_operation_open, finished_fail_invalid_handle_type)
EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(0); EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(0);
EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0)); EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0));
json_t * result = json_object(); JsonDoc result("{\"handle\": \"42\"}");
json_object_set_new(result, "handle", json_string("42")); wf_impl_operation_open_finished(nullptr, result.root(), nullptr);
wf_impl_operation_open_finished(nullptr, result, nullptr);
json_decref(result);
} }

View File

@ -1,12 +1,14 @@
#include "webfuse/impl/operation/read.h" #include "webfuse/impl/operation/read.h"
#include "webfuse/impl/jsonrpc/error.h" #include "webfuse/impl/jsonrpc/error.h"
#include "webfuse/test_util/json_doc.hpp"
#include "webfuse/mocks/mock_fuse.hpp" #include "webfuse/mocks/mock_fuse.hpp"
#include "webfuse/mocks/mock_operation_context.hpp" #include "webfuse/mocks/mock_operation_context.hpp"
#include "webfuse/mocks/mock_jsonrpc_proxy.hpp" #include "webfuse/mocks/mock_jsonrpc_proxy.hpp"
#include <gtest/gtest.h> #include <gtest/gtest.h>
using webfuse_test::JsonDoc;
using webfuse_test::MockJsonRpcProxy; using webfuse_test::MockJsonRpcProxy;
using webfuse_test::MockOperationContext; using webfuse_test::MockOperationContext;
using webfuse_test::FuseMock; using webfuse_test::FuseMock;
@ -136,12 +138,8 @@ TEST(wf_impl_operation_read, finished)
FuseMock fuse; FuseMock fuse;
EXPECT_CALL(fuse, fuse_reply_buf(_,_,7)).Times(1).WillOnce(Return(0)); EXPECT_CALL(fuse, fuse_reply_buf(_,_,7)).Times(1).WillOnce(Return(0));
json_t * result = json_object(); JsonDoc result("{\"data\": \"brummni\", \"format\": \"identity\", \"count\": 7}");
json_object_set_new(result, "data", json_string("brummni")); wf_impl_operation_read_finished(nullptr, result.root(), nullptr);
json_object_set_new(result, "format", json_string("identity"));
json_object_set_new(result, "count", json_integer(7));
wf_impl_operation_read_finished(nullptr, result, nullptr);
json_decref(result);
} }
TEST(wf_impl_operation_read, finished_fail_no_data) TEST(wf_impl_operation_read, finished_fail_no_data)
@ -150,11 +148,8 @@ TEST(wf_impl_operation_read, finished_fail_no_data)
EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(0); EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(0);
EXPECT_CALL(fuse, fuse_reply_err(_, _)).Times(1).WillOnce(Return(0)); EXPECT_CALL(fuse, fuse_reply_err(_, _)).Times(1).WillOnce(Return(0));
json_t * result = json_object(); JsonDoc result("{\"format\": \"identity\", \"count\": 7}");
json_object_set_new(result, "format", json_string("identity")); wf_impl_operation_read_finished(nullptr, result.root(), nullptr);
json_object_set_new(result, "count", json_integer(7));
wf_impl_operation_read_finished(nullptr, result, nullptr);
json_decref(result);
} }
TEST(wf_impl_operation_read, finished_fail_invalid_data_type) TEST(wf_impl_operation_read, finished_fail_invalid_data_type)
@ -163,12 +158,8 @@ TEST(wf_impl_operation_read, finished_fail_invalid_data_type)
EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(0); EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(0);
EXPECT_CALL(fuse, fuse_reply_err(_, _)).Times(1).WillOnce(Return(0)); EXPECT_CALL(fuse, fuse_reply_err(_, _)).Times(1).WillOnce(Return(0));
json_t * result = json_object(); JsonDoc result("{\"data\": 42, \"format\": \"identity\", \"count\": 7}");
json_object_set_new(result, "data", json_integer(42)); wf_impl_operation_read_finished(nullptr, result.root(), nullptr);
json_object_set_new(result, "format", json_string("identity"));
json_object_set_new(result, "count", json_integer(7));
wf_impl_operation_read_finished(nullptr, result, nullptr);
json_decref(result);
} }
TEST(wf_impl_operation_read, finished_fail_no_format) TEST(wf_impl_operation_read, finished_fail_no_format)
@ -177,11 +168,8 @@ TEST(wf_impl_operation_read, finished_fail_no_format)
EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(0); EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(0);
EXPECT_CALL(fuse, fuse_reply_err(_, _)).Times(1).WillOnce(Return(0)); EXPECT_CALL(fuse, fuse_reply_err(_, _)).Times(1).WillOnce(Return(0));
json_t * result = json_object(); JsonDoc result("{\"data\": \"brummni\", \"count\": 7}");
json_object_set_new(result, "data", json_string("brummni")); wf_impl_operation_read_finished(nullptr, result.root(), nullptr);
json_object_set_new(result, "count", json_integer(7));
wf_impl_operation_read_finished(nullptr, result, nullptr);
json_decref(result);
} }
TEST(wf_impl_operation_read, finished_fail_invalid_format_type) TEST(wf_impl_operation_read, finished_fail_invalid_format_type)
@ -190,12 +178,8 @@ TEST(wf_impl_operation_read, finished_fail_invalid_format_type)
EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(0); EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(0);
EXPECT_CALL(fuse, fuse_reply_err(_, _)).Times(1).WillOnce(Return(0)); EXPECT_CALL(fuse, fuse_reply_err(_, _)).Times(1).WillOnce(Return(0));
json_t * result = json_object(); JsonDoc result("{\"data\": \"brummni\", \"format\": 42, \"count\": 7}");
json_object_set_new(result, "data", json_string("brummni")); wf_impl_operation_read_finished(nullptr, result.root(), nullptr);
json_object_set_new(result, "format", json_integer(42));
json_object_set_new(result, "count", json_integer(7));
wf_impl_operation_read_finished(nullptr, result, nullptr);
json_decref(result);
} }
TEST(wf_impl_operation_read, finished_fail_no_count) TEST(wf_impl_operation_read, finished_fail_no_count)
@ -204,11 +188,8 @@ TEST(wf_impl_operation_read, finished_fail_no_count)
EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(0); EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(0);
EXPECT_CALL(fuse, fuse_reply_err(_, _)).Times(1).WillOnce(Return(0)); EXPECT_CALL(fuse, fuse_reply_err(_, _)).Times(1).WillOnce(Return(0));
json_t * result = json_object(); JsonDoc result("{\"data\": \"brummni\", \"format\": \"identity\"}");
json_object_set_new(result, "data", json_string("brummni")); wf_impl_operation_read_finished(nullptr, result.root(), nullptr);
json_object_set_new(result, "format", json_string("identity"));
wf_impl_operation_read_finished(nullptr, result, nullptr);
json_decref(result);
} }
TEST(wf_impl_operation_read, finished_fail_invalid_count_type) TEST(wf_impl_operation_read, finished_fail_invalid_count_type)
@ -217,12 +198,8 @@ TEST(wf_impl_operation_read, finished_fail_invalid_count_type)
EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(0); EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(0);
EXPECT_CALL(fuse, fuse_reply_err(_, _)).Times(1).WillOnce(Return(0)); EXPECT_CALL(fuse, fuse_reply_err(_, _)).Times(1).WillOnce(Return(0));
json_t * result = json_object(); JsonDoc result("{\"data\": \"brummni\", \"format\": \"identity\", \"count\": \"7\"}");
json_object_set_new(result, "data", json_string("brummni")); wf_impl_operation_read_finished(nullptr, result.root(), nullptr);
json_object_set_new(result, "format", json_string("identity"));
json_object_set_new(result, "count", json_string("7"));
wf_impl_operation_read_finished(nullptr, result, nullptr);
json_decref(result);
} }
TEST(wf_impl_operation_read, finished_fail_error) TEST(wf_impl_operation_read, finished_fail_error)

View File

@ -3,12 +3,15 @@
#include "webfuse/status.h" #include "webfuse/status.h"
#include "webfuse/test_util/json_doc.hpp"
#include "webfuse/mocks/mock_fuse.hpp" #include "webfuse/mocks/mock_fuse.hpp"
#include "webfuse/mocks/mock_operation_context.hpp" #include "webfuse/mocks/mock_operation_context.hpp"
#include "webfuse/mocks/mock_jsonrpc_proxy.hpp" #include "webfuse/mocks/mock_jsonrpc_proxy.hpp"
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <sstream>
using webfuse_test::JsonDoc;
using webfuse_test::MockJsonRpcProxy; using webfuse_test::MockJsonRpcProxy;
using webfuse_test::MockOperationContext; using webfuse_test::MockOperationContext;
using webfuse_test::FuseMock; using webfuse_test::FuseMock;
@ -81,18 +84,12 @@ TEST(wf_impl_operation_readdir, finished)
FuseMock fuse; FuseMock fuse;
EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(1).WillOnce(Return(0)); EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(1).WillOnce(Return(0));
json_t * result = json_array(); JsonDoc result("[{\"name\": \"a.file\", \"inode\": 42}]");
json_t * item = json_object();
json_object_set_new(item, "name", json_string("a.file"));
json_object_set_new(item, "inode", json_integer(42));
json_array_append_new(result, item);
auto * context = reinterpret_cast<wf_impl_operation_readdir_context*>(malloc(sizeof(wf_impl_operation_readdir_context))); auto * context = reinterpret_cast<wf_impl_operation_readdir_context*>(malloc(sizeof(wf_impl_operation_readdir_context)));
context->request = nullptr; context->request = nullptr;
context->size = 1; context->size = 1;
context->offset = 0; context->offset = 0;
wf_impl_operation_readdir_finished(reinterpret_cast<void*>(context), result, nullptr); wf_impl_operation_readdir_finished(reinterpret_cast<void*>(context), result.root(), nullptr);
json_decref(result);
} }
TEST(wf_impl_operation_readdir, finished_many_items) TEST(wf_impl_operation_readdir, finished_many_items)
@ -100,22 +97,20 @@ TEST(wf_impl_operation_readdir, finished_many_items)
FuseMock fuse; FuseMock fuse;
EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(1).WillOnce(Return(0)); EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(1).WillOnce(Return(0));
json_t * result = json_array(); std::ostringstream stream;
for(int i = 0; i < 100; i++) stream << "[" << "{\"name\": \"file_0.txt\", \"inode\": 1}";
for(int i = 1; i < 100; i++)
{ {
json_t * item = json_object(); stream << ",{\"name\": \"file_" << i << ".txt\", \"inode\": " << (i+1) << "}";
json_object_set_new(item, "name", json_sprintf("file_%d.txt", i));
json_object_set_new(item, "inode", json_integer(1 + i));
json_array_append_new(result, item);
} }
stream << "]";
JsonDoc result(stream.str());
auto * context = reinterpret_cast<wf_impl_operation_readdir_context*>(malloc(sizeof(wf_impl_operation_readdir_context))); auto * context = reinterpret_cast<wf_impl_operation_readdir_context*>(malloc(sizeof(wf_impl_operation_readdir_context)));
context->request = nullptr; context->request = nullptr;
context->size = 100; context->size = 100;
context->offset = 0; context->offset = 0;
wf_impl_operation_readdir_finished(reinterpret_cast<void*>(context), result, nullptr); wf_impl_operation_readdir_finished(reinterpret_cast<void*>(context), result.root(), nullptr);
json_decref(result);
} }
TEST(wf_impl_operation_readdir, finished_read_after_end) TEST(wf_impl_operation_readdir, finished_read_after_end)
@ -123,18 +118,12 @@ TEST(wf_impl_operation_readdir, finished_read_after_end)
FuseMock fuse; FuseMock fuse;
EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(1).WillOnce(Return(0)); EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(1).WillOnce(Return(0));
json_t * result = json_array(); JsonDoc result("[{\"name\": \"a.file\", \"inode\": 42}]");
json_t * item = json_object();
json_object_set_new(item, "name", json_string("a.file"));
json_object_set_new(item, "inode", json_integer(42));
json_array_append_new(result, item);
auto * context = reinterpret_cast<wf_impl_operation_readdir_context*>(malloc(sizeof(wf_impl_operation_readdir_context))); auto * context = reinterpret_cast<wf_impl_operation_readdir_context*>(malloc(sizeof(wf_impl_operation_readdir_context)));
context->request = nullptr; context->request = nullptr;
context->size = 10; context->size = 10;
context->offset = 2; context->offset = 2;
wf_impl_operation_readdir_finished(reinterpret_cast<void*>(context), result, nullptr); wf_impl_operation_readdir_finished(reinterpret_cast<void*>(context), result.root(), nullptr);
json_decref(result);
} }
TEST(wf_impl_operation_readdir, finished_fail_error) TEST(wf_impl_operation_readdir, finished_fail_error)
@ -159,14 +148,12 @@ TEST(wf_impl_operation_readdir, finished_fail_invalid_result_type)
EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(0); EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(0);
EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0)); EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0));
json_t * result = json_object(); JsonDoc result("{}");
auto * context = reinterpret_cast<wf_impl_operation_readdir_context*>(malloc(sizeof(wf_impl_operation_readdir_context))); auto * context = reinterpret_cast<wf_impl_operation_readdir_context*>(malloc(sizeof(wf_impl_operation_readdir_context)));
context->request = nullptr; context->request = nullptr;
context->size = 1; context->size = 1;
context->offset = 0; context->offset = 0;
wf_impl_operation_readdir_finished(reinterpret_cast<void*>(context), result, nullptr); wf_impl_operation_readdir_finished(reinterpret_cast<void*>(context), result.root(), nullptr);
json_decref(result);
} }
TEST(wf_impl_operation_readdir, finished_fail_missing_name) TEST(wf_impl_operation_readdir, finished_fail_missing_name)
@ -175,17 +162,12 @@ TEST(wf_impl_operation_readdir, finished_fail_missing_name)
EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(0); EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(0);
EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0)); EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0));
json_t * result = json_array(); JsonDoc result("[{\"inode\": 42}]");
json_t * item = json_object();
json_object_set_new(item, "inode", json_integer(42));
json_array_append_new(result, item);
auto * context = reinterpret_cast<wf_impl_operation_readdir_context*>(malloc(sizeof(wf_impl_operation_readdir_context))); auto * context = reinterpret_cast<wf_impl_operation_readdir_context*>(malloc(sizeof(wf_impl_operation_readdir_context)));
context->request = nullptr; context->request = nullptr;
context->size = 1; context->size = 1;
context->offset = 0; context->offset = 0;
wf_impl_operation_readdir_finished(reinterpret_cast<void*>(context), result, nullptr); wf_impl_operation_readdir_finished(reinterpret_cast<void*>(context), result.root(), nullptr);
json_decref(result);
} }
TEST(wf_impl_operation_readdir, finished_fail_invalid_name_type) TEST(wf_impl_operation_readdir, finished_fail_invalid_name_type)
@ -194,18 +176,12 @@ TEST(wf_impl_operation_readdir, finished_fail_invalid_name_type)
EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(0); EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(0);
EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0)); EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0));
json_t * result = json_array(); JsonDoc result("[{\"name\": 42, \"inode\": 42}]");
json_t * item = json_object();
json_object_set_new(item, "name", json_integer(42));
json_object_set_new(item, "inode", json_integer(42));
json_array_append_new(result, item);
auto * context = reinterpret_cast<wf_impl_operation_readdir_context*>(malloc(sizeof(wf_impl_operation_readdir_context))); auto * context = reinterpret_cast<wf_impl_operation_readdir_context*>(malloc(sizeof(wf_impl_operation_readdir_context)));
context->request = nullptr; context->request = nullptr;
context->size = 1; context->size = 1;
context->offset = 0; context->offset = 0;
wf_impl_operation_readdir_finished(reinterpret_cast<void*>(context), result, nullptr); wf_impl_operation_readdir_finished(reinterpret_cast<void*>(context), result.root(), nullptr);
json_decref(result);
} }
TEST(wf_impl_operation_readdir, finished_fail_missing_inode) TEST(wf_impl_operation_readdir, finished_fail_missing_inode)
@ -214,17 +190,12 @@ TEST(wf_impl_operation_readdir, finished_fail_missing_inode)
EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(0); EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(0);
EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0)); EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0));
json_t * result = json_array(); JsonDoc result("[{\"name\": \"a.file\"}]");
json_t * item = json_object();
json_object_set_new(item, "name", json_string("a.file"));
json_array_append_new(result, item);
auto * context = reinterpret_cast<wf_impl_operation_readdir_context*>(malloc(sizeof(wf_impl_operation_readdir_context))); auto * context = reinterpret_cast<wf_impl_operation_readdir_context*>(malloc(sizeof(wf_impl_operation_readdir_context)));
context->request = nullptr; context->request = nullptr;
context->size = 1; context->size = 1;
context->offset = 0; context->offset = 0;
wf_impl_operation_readdir_finished(reinterpret_cast<void*>(context), result, nullptr); wf_impl_operation_readdir_finished(reinterpret_cast<void*>(context), result.root(), nullptr);
json_decref(result);
} }
TEST(wf_impl_operation_readdir, finished_fail_invalid_inode_type) TEST(wf_impl_operation_readdir, finished_fail_invalid_inode_type)
@ -233,18 +204,12 @@ TEST(wf_impl_operation_readdir, finished_fail_invalid_inode_type)
EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(0); EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(0);
EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0)); EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0));
json_t * result = json_array(); JsonDoc result("[{\"name\": \"a.file\", \"inode\": \"42\"}]");
json_t * item = json_object();
json_object_set_new(item, "name", json_string("a.file"));
json_object_set_new(item, "inode", json_string("42"));
json_array_append_new(result, item);
auto * context = reinterpret_cast<wf_impl_operation_readdir_context*>(malloc(sizeof(wf_impl_operation_readdir_context))); auto * context = reinterpret_cast<wf_impl_operation_readdir_context*>(malloc(sizeof(wf_impl_operation_readdir_context)));
context->request = nullptr; context->request = nullptr;
context->size = 1; context->size = 1;
context->offset = 0; context->offset = 0;
wf_impl_operation_readdir_finished(reinterpret_cast<void*>(context), result, nullptr); wf_impl_operation_readdir_finished(reinterpret_cast<void*>(context), result.root(), nullptr);
json_decref(result);
} }
TEST(wf_impl_operation_readdir, finished_fail_invalid_item_type) TEST(wf_impl_operation_readdir, finished_fail_invalid_item_type)
@ -253,13 +218,10 @@ TEST(wf_impl_operation_readdir, finished_fail_invalid_item_type)
EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(0); EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(0);
EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0)); EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0));
json_t * result = json_array(); JsonDoc result("[\"item\"]");
json_array_append_new(result, json_string("item"));
auto * context = reinterpret_cast<wf_impl_operation_readdir_context*>(malloc(sizeof(wf_impl_operation_readdir_context))); auto * context = reinterpret_cast<wf_impl_operation_readdir_context*>(malloc(sizeof(wf_impl_operation_readdir_context)));
context->request = nullptr; context->request = nullptr;
context->size = 1; context->size = 1;
context->offset = 0; context->offset = 0;
wf_impl_operation_readdir_finished(reinterpret_cast<void*>(context), result, nullptr); wf_impl_operation_readdir_finished(reinterpret_cast<void*>(context), result.root(), nullptr);
json_decref(result);
} }

View File

@ -1,8 +1,11 @@
#include <gtest/gtest.h>
#include "webfuse/credentials.h" #include "webfuse/credentials.h"
#include "webfuse/impl/credentials.h" #include "webfuse/impl/credentials.h"
#include "webfuse/test_util/json_doc.hpp"
#include <jansson.h> #include <jansson.h>
#include <gtest/gtest.h>
using webfuse_test::JsonDoc;
TEST(Credentials, Type) TEST(Credentials, Type)
{ {
@ -16,31 +19,27 @@ TEST(Credentials, Type)
TEST(Credentials, Get) TEST(Credentials, Get)
{ {
struct wf_credentials creds; struct wf_credentials creds;
json_t * data = json_object(); JsonDoc doc("{\"username\": \"bob\", \"password\": \"<secret>\"}");
json_object_set_new(data, "username", json_string("bob"));
json_object_set_new(data, "password", json_string("<secret>"));
wf_impl_credentials_init(&creds, "username", data); wf_impl_credentials_init(&creds, "username", doc.root());
ASSERT_STREQ("username", wf_credentials_type(&creds)); ASSERT_STREQ("username", wf_credentials_type(&creds));
ASSERT_STREQ("bob", wf_credentials_get(&creds, "username")); ASSERT_STREQ("bob", wf_credentials_get(&creds, "username"));
ASSERT_STREQ("<secret>", wf_credentials_get(&creds, "password")); ASSERT_STREQ("<secret>", wf_credentials_get(&creds, "password"));
wf_impl_credentials_cleanup(&creds); wf_impl_credentials_cleanup(&creds);
json_decref(data);
} }
TEST(Credentials, FailedToGetNonexistingValue) TEST(Credentials, FailedToGetNonexistingValue)
{ {
struct wf_credentials creds; struct wf_credentials creds;
json_t * data = json_object(); JsonDoc doc("{}");
wf_impl_credentials_init(&creds, "username", data); wf_impl_credentials_init(&creds, "username", doc.root());
ASSERT_STREQ("username", wf_credentials_type(&creds)); ASSERT_STREQ("username", wf_credentials_type(&creds));
ASSERT_STREQ(nullptr, wf_credentials_get(&creds, "username")); ASSERT_STREQ(nullptr, wf_credentials_get(&creds, "username"));
ASSERT_STREQ(nullptr, wf_credentials_get(&creds, "password")); ASSERT_STREQ(nullptr, wf_credentials_get(&creds, "password"));
wf_impl_credentials_cleanup(&creds); wf_impl_credentials_cleanup(&creds);
json_decref(data);
} }
TEST(Credentials, FailedToGetWithoutData) TEST(Credentials, FailedToGetWithoutData)
@ -58,30 +57,27 @@ TEST(Credentials, FailedToGetWithoutData)
TEST(Credentials, FailedToGetWrongDataType) TEST(Credentials, FailedToGetWrongDataType)
{ {
struct wf_credentials creds; struct wf_credentials creds;
json_t * data = json_string("invalid_creds"); JsonDoc doc("invalid_creds");
wf_impl_credentials_init(&creds, "username", data); wf_impl_credentials_init(&creds, "username", doc.root());
ASSERT_STREQ("username", wf_credentials_type(&creds)); ASSERT_STREQ("username", wf_credentials_type(&creds));
ASSERT_STREQ(nullptr, wf_credentials_get(&creds, "username")); ASSERT_STREQ(nullptr, wf_credentials_get(&creds, "username"));
ASSERT_STREQ(nullptr, wf_credentials_get(&creds, "password")); ASSERT_STREQ(nullptr, wf_credentials_get(&creds, "password"));
wf_impl_credentials_cleanup(&creds); wf_impl_credentials_cleanup(&creds);
json_decref(data);
} }
TEST(Credentials, FailedToGetWrongElementDataType) TEST(Credentials, FailedToGetWrongElementDataType)
{ {
struct wf_credentials creds; struct wf_credentials creds;
json_t * data = json_object(); JsonDoc doc("{\"username\": 42}");
json_object_set_new(data, "username", json_integer(42));
wf_impl_credentials_init(&creds, "username", data); wf_impl_credentials_init(&creds, "username", doc.root());
ASSERT_STREQ("username", wf_credentials_type(&creds)); ASSERT_STREQ("username", wf_credentials_type(&creds));
ASSERT_STREQ(nullptr, wf_credentials_get(&creds, "username")); ASSERT_STREQ(nullptr, wf_credentials_get(&creds, "username"));
ASSERT_STREQ(nullptr, wf_credentials_get(&creds, "password")); ASSERT_STREQ(nullptr, wf_credentials_get(&creds, "password"));
wf_impl_credentials_cleanup(&creds); wf_impl_credentials_cleanup(&creds);
json_decref(data);
} }
TEST(Credentials, SetType) TEST(Credentials, SetType)

View File

@ -9,9 +9,32 @@ JsonDoc::JsonDoc(std::string const & text)
doc = wf_impl_json_doc_loadb(const_cast<char*>(contents.data()), contents.size()); doc = wf_impl_json_doc_loadb(const_cast<char*>(contents.data()), contents.size());
} }
JsonDoc::JsonDoc(JsonDoc && other)
{
contents = std::move(other.contents);
doc = other.doc;
other.doc = nullptr;
}
JsonDoc& JsonDoc::operator=(JsonDoc && other)
{
if (this != &other)
{
wf_impl_json_doc_dispose(doc);
contents = std::move(other.contents);
doc = other.doc;
other.doc = nullptr;
}
return *this;
}
JsonDoc::~JsonDoc() JsonDoc::~JsonDoc()
{ {
if (nullptr != doc)
{
wf_impl_json_doc_dispose(doc); wf_impl_json_doc_dispose(doc);
}
} }
wf_json const * JsonDoc::root() wf_json const * JsonDoc::root()

View File

@ -9,8 +9,12 @@ namespace webfuse_test
class JsonDoc class JsonDoc
{ {
JsonDoc(JsonDoc const&) = delete;
JsonDoc& operator=(JsonDoc const&) = delete;
public: public:
JsonDoc(std::string const & text); JsonDoc(std::string const & text);
JsonDoc(JsonDoc && other);
JsonDoc& operator=(JsonDoc && other);
~JsonDoc(); ~JsonDoc();
wf_json const * root(); wf_json const * root();
private: private:

View File

@ -1,14 +1,15 @@
#include <gtest/gtest.h>
#include "webfuse/impl/util/json_util.h" #include "webfuse/impl/util/json_util.h"
#include "webfuse/test_util/json_doc.hpp"
#include <gtest/gtest.h>
using webfuse_test::JsonDoc;
TEST(jsonrpc_util, get_int) TEST(jsonrpc_util, get_int)
{ {
json_t * object = json_object(); JsonDoc doc("{\"key\": 23}");
json_object_set_new(object, "key", json_integer(23)); int value = wf_impl_json_get_int(doc.root(), "key", 42);
int value = wf_impl_json_get_int(object, "key", 42);
ASSERT_EQ(23, value); ASSERT_EQ(23, value);
json_decref(object);
} }
TEST(jsonrpc_util, failed_to_get_null_object) TEST(jsonrpc_util, failed_to_get_null_object)
@ -20,28 +21,21 @@ TEST(jsonrpc_util, failed_to_get_null_object)
TEST(jsonrpc_util, failed_to_get_not_object) TEST(jsonrpc_util, failed_to_get_not_object)
{ {
json_t * object = json_array(); JsonDoc doc("[]");
int value = wf_impl_json_get_int(nullptr, "key", 42); int value = wf_impl_json_get_int(doc.root(), "key", 42);
ASSERT_EQ(42, value); ASSERT_EQ(42, value);
json_decref(object);
} }
TEST(jsonrpc_util, failed_to_get_invalid_key) TEST(jsonrpc_util, failed_to_get_invalid_key)
{ {
json_t * object = json_object(); JsonDoc doc("{}");
int value = wf_impl_json_get_int(object, "key", 42); int value = wf_impl_json_get_int(doc.root(), "key", 42);
ASSERT_EQ(42, value); ASSERT_EQ(42, value);
json_decref(object);
} }
TEST(jsonrpc_util, failed_to_get_invalid_value_type) TEST(jsonrpc_util, failed_to_get_invalid_value_type)
{ {
json_t * object = json_object(); JsonDoc doc("{\"key\": \"42\"}");
json_object_set_new(object, "key", json_string("42")); int value = wf_impl_json_get_int(doc.root(), "key", 42);
int value = wf_impl_json_get_int(object, "key", 42);
ASSERT_EQ(42, value); ASSERT_EQ(42, value);
json_decref(object);
} }