1
0
mirror of https://github.com/falk-werner/webfuse-provider synced 2026-03-02 04:09:18 +00:00

chore(webfuse) Increase test coverage (#34)

* removes unnecessary code

* adds test of wf_status

* adds tests of wf_message

* adds tests of wf_message_queue

* changed branch of coverage badge to display correct results

* moves core tests into separate subdirectory

* increases coverage of timer test

* moves adapter specific tests into separate directory

* moves provider specific tests into separate directory

* adds tests of jsonrpc utilities

* adds tests of jsonrpc request

* adds test of jsonrpc response

* adds tests of jsonrpc server

* adds tests of jsonrpc proxy

* adds integration test (found some issues)

* disables problematic tests

* fixes resource leak: pending timer after cleanup proxy

* fixes order of cleanup to prevent processing pending requests after filesystem shut down

* fixes some memcheck and helgrind errors: initialization of lws_log; setup of client and server

* disabled a test

* fixes error in msleep utility

* fixes deadlock at IntegrationTest using valgrind

* removes unit test code from coverage report

* adds some integration tests

* makes badge show coverage of master

* fixes some coding style issues

* fixes eary trigger of is_connected (provider)

* fixes read error in 32 bit environments\n\ninode is always 64 bit, but variadic wf_impl_jsonrpc_proxy_invoke expects int
This commit is contained in:
Falk Werner
2019-05-19 14:33:42 +02:00
committed by GitHub
parent 9180ad3bb7
commit 07e32757f8
49 changed files with 1650 additions and 54 deletions

View File

@@ -0,0 +1,112 @@
#include <gtest/gtest.h>
#include "webfuse/adapter/impl/jsonrpc/request.h"
TEST(jsonrpc_is_request, request_with_object_params)
{
json_t * request = json_object();
json_object_set_new(request, "method", json_string("method"));
json_object_set_new(request, "params", json_object());
json_object_set_new(request, "id", json_integer(42));
ASSERT_TRUE(wf_impl_jsonrpc_is_request(request));
json_decref(request);
}
TEST(jsonrpc_is_request, request_with_array_params)
{
json_t * request = json_object();
json_object_set_new(request, "method", json_string("method"));
json_object_set_new(request, "params", json_array());
json_object_set_new(request, "id", json_integer(42));
ASSERT_TRUE(wf_impl_jsonrpc_is_request(request));
json_decref(request);
}
TEST(jsonrpc_is_request, null_request)
{
ASSERT_FALSE(wf_impl_jsonrpc_is_request(nullptr));
}
TEST(jsonrpc_is_request, invalid_request)
{
json_t * request = json_array();
json_array_append_new(request, json_string("method"));
json_array_append_new(request, json_object());
json_array_append_new(request, json_integer(42));
ASSERT_FALSE(wf_impl_jsonrpc_is_request(request));
json_decref(request);
}
TEST(jsonrpc_is_request, invalid_request_without_id)
{
json_t * request = json_object();
json_object_set_new(request, "method", json_string("method"));
json_object_set_new(request, "params", json_object());
ASSERT_FALSE(wf_impl_jsonrpc_is_request(request));
json_decref(request);
}
TEST(jsonrpc_is_request, invalid_request_due_to_invalid_id)
{
json_t * request = json_object();
json_object_set_new(request, "method", json_string("method"));
json_object_set_new(request, "params", json_object());
json_object_set_new(request, "id", json_string("42"));
ASSERT_FALSE(wf_impl_jsonrpc_is_request(request));
json_decref(request);
}
TEST(jsonrpc_is_request, invalid_request_without_method)
{
json_t * request = json_object();
json_object_set_new(request, "params", json_object());
json_object_set_new(request, "id", json_integer(42));
ASSERT_FALSE(wf_impl_jsonrpc_is_request(request));
json_decref(request);
}
TEST(jsonrpc_is_request, invalid_request_due_to_invalid_method)
{
json_t * request = json_object();
json_object_set_new(request, "method", json_integer(42));
json_object_set_new(request, "params", json_object());
json_object_set_new(request, "id", json_integer(42));
ASSERT_FALSE(wf_impl_jsonrpc_is_request(request));
json_decref(request);
}
TEST(jsonrpc_is_request, invalid_request_without_params)
{
json_t * request = json_object();
json_object_set_new(request, "method", json_string("method"));
json_object_set_new(request, "id", json_integer(42));
ASSERT_FALSE(wf_impl_jsonrpc_is_request(request));
json_decref(request);
}
TEST(jsonrpc_is_request, invalid_request_due_to_invalid_params)
{
json_t * request = json_object();
json_object_set_new(request, "methdo", json_string("method"));
json_object_set_new(request, "params", json_string("params"));
json_object_set_new(request, "id", json_integer(42));
ASSERT_FALSE(wf_impl_jsonrpc_is_request(request));
json_decref(request);
}

View File

@@ -0,0 +1,94 @@
#include <gtest/gtest.h>
#include "webfuse/adapter/impl/jsonrpc/response.h"
TEST(jsonrpc_is_response, valid_result)
{
json_t * message = json_object();
json_object_set_new(message, "result", json_object());
json_object_set_new(message, "id", json_integer(42));
ASSERT_TRUE(wf_impl_jsonrpc_is_response(message));
json_decref(message);
}
TEST(jsonrpc_is_response, valid_result_string)
{
json_t * message = json_object();
json_object_set_new(message, "result", json_string("also valid"));
json_object_set_new(message, "id", json_integer(42));
ASSERT_TRUE(wf_impl_jsonrpc_is_response(message));
json_decref(message);
}
TEST(jsonrpc_is_response, valid_error)
{
json_t * message = json_object();
json_object_set_new(message, "error", json_object());
json_object_set_new(message, "id", json_integer(42));
ASSERT_TRUE(wf_impl_jsonrpc_is_response(message));
json_decref(message);
}
TEST(jsonrpc_is_response, invalid_null)
{
ASSERT_FALSE(wf_impl_jsonrpc_is_response(nullptr));
}
TEST(jsonrpc_is_response, invalid_message)
{
json_t * message = json_array();
json_array_append_new(message, json_object());
json_array_append_new(message, json_integer(42));
ASSERT_FALSE(wf_impl_jsonrpc_is_response(message));
json_decref(message);
}
TEST(jsonrpc_is_response, invalid_missing_id)
{
json_t * message = json_object();
json_object_set_new(message, "result", json_object());
ASSERT_FALSE(wf_impl_jsonrpc_is_response(message));
json_decref(message);
}
TEST(jsonrpc_is_response, invalid_id_wrong_type)
{
json_t * message = json_object();
json_object_set_new(message, "result", json_object());
json_object_set_new(message, "id", json_string("42"));
ASSERT_FALSE(wf_impl_jsonrpc_is_response(message));
json_decref(message);
}
TEST(jsonrpc_is_response, invalid_missing_result_and_error)
{
json_t * message = json_object();
json_object_set_new(message, "id", json_integer(42));
ASSERT_FALSE(wf_impl_jsonrpc_is_response(message));
json_decref(message);
}
TEST(jsonrpc_is_response, invalid_error_wrong_type)
{
json_t * message = json_object();
json_object_set_new(message, "error", json_array());
json_object_set_new(message, "id", json_integer(42));
ASSERT_FALSE(wf_impl_jsonrpc_is_response(message));
json_decref(message);
}

View File

@@ -0,0 +1,393 @@
#include <gtest/gtest.h>
#include "webfuse/adapter/impl/jsonrpc/proxy.h"
#include "webfuse/adapter/impl/time/timeout_manager.h"
#include "msleep.hpp"
using webfuse_test::msleep;
#define WF_DEFAULT_TIMEOUT (10 * 1000)
namespace
{
struct SendContext
{
json_t * response;
bool result;
bool is_called;
explicit SendContext(bool result_ = true)
: response(nullptr)
, result(result_)
, is_called(false)
{
}
~SendContext()
{
if (nullptr != response)
{
json_decref(response);
}
}
};
bool jsonrpc_send(
json_t * request,
void * user_data)
{
SendContext * context = reinterpret_cast<SendContext*>(user_data);
context->is_called = true;
context->response = request;
json_incref(request);
return context->result;
}
struct FinishedContext
{
bool is_called;
wf_status status;
json_t * result;
FinishedContext()
: is_called(false)
, status(WF_BAD)
, result(nullptr)
{
}
~FinishedContext()
{
if (nullptr != result)
{
json_decref(result);
}
}
};
void jsonrpc_finished(
void * user_data,
wf_status status,
struct json_t const * result)
{
FinishedContext * context = reinterpret_cast<FinishedContext*>(user_data);
context->is_called = true;
context->status = status;
context->result = json_deep_copy(result);
}
}
TEST(jsonrpc_proxy, init)
{
struct wf_impl_timeout_manager timeout_manager;
wf_impl_timeout_manager_init(&timeout_manager);
SendContext context;
void * user_data = reinterpret_cast<void*>(&context);
struct wf_impl_jsonrpc_proxy proxy;
wf_impl_jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, user_data);
wf_impl_jsonrpc_proxy_cleanup(&proxy);
wf_impl_timeout_manager_cleanup(&timeout_manager);
ASSERT_FALSE(context.is_called);
}
TEST(jsonrpc_proxy, invoke)
{
struct wf_impl_timeout_manager timeout_manager;
wf_impl_timeout_manager_init(&timeout_manager);
SendContext send_context;
void * send_data = reinterpret_cast<void*>(&send_context);
struct wf_impl_jsonrpc_proxy proxy;
wf_impl_jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
FinishedContext finished_context;
void * finished_data = reinterpret_cast<void*>(&finished_context);
wf_impl_jsonrpc_proxy_invoke(&proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
ASSERT_TRUE(send_context.is_called);
ASSERT_TRUE(json_is_object(send_context.response));
json_t * method = json_object_get(send_context.response, "method");
ASSERT_TRUE(json_is_string(method));
ASSERT_STREQ("foo", json_string_value(method));
json_t * params = json_object_get(send_context.response, "params");
ASSERT_TRUE(json_is_array(params));
ASSERT_EQ(2, json_array_size(params));
ASSERT_TRUE(json_is_string(json_array_get(params, 0)));
ASSERT_STREQ("bar", json_string_value(json_array_get(params, 0)));
ASSERT_TRUE(json_is_integer(json_array_get(params, 1)));
ASSERT_EQ(42, json_integer_value(json_array_get(params, 1)));
json_t * id = json_object_get(send_context.response, "id");
ASSERT_TRUE(json_is_integer(id));
ASSERT_FALSE(finished_context.is_called);
wf_impl_jsonrpc_proxy_cleanup(&proxy);
wf_impl_timeout_manager_cleanup(&timeout_manager);
ASSERT_TRUE(finished_context.is_called);
ASSERT_FALSE(WF_GOOD == finished_context.status);
}
TEST(jsonrpc_proxy, invoke_calls_finish_if_send_fails)
{
struct wf_impl_timeout_manager timeout_manager;
wf_impl_timeout_manager_init(&timeout_manager);
SendContext send_context(false);
void * send_data = reinterpret_cast<void*>(&send_context);
struct wf_impl_jsonrpc_proxy proxy;
wf_impl_jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
FinishedContext finished_context;
void * finished_data = reinterpret_cast<void*>(&finished_context);
wf_impl_jsonrpc_proxy_invoke(&proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
ASSERT_TRUE(send_context.is_called);
ASSERT_TRUE(json_is_object(send_context.response));
ASSERT_TRUE(finished_context.is_called);
ASSERT_FALSE(WF_GOOD == finished_context.status);
wf_impl_jsonrpc_proxy_cleanup(&proxy);
wf_impl_timeout_manager_cleanup(&timeout_manager);
}
TEST(jsonrpc_proxy, invoke_fails_if_another_request_is_pending)
{
struct wf_impl_timeout_manager timeout_manager;
wf_impl_timeout_manager_init(&timeout_manager);
SendContext send_context;
void * send_data = reinterpret_cast<void*>(&send_context);
struct wf_impl_jsonrpc_proxy proxy;
wf_impl_jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
FinishedContext finished_context;
void * finished_data = reinterpret_cast<void*>(&finished_context);
wf_impl_jsonrpc_proxy_invoke(&proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
FinishedContext finished_context2;
void * finished_data2 = reinterpret_cast<void*>(&finished_context2);
wf_impl_jsonrpc_proxy_invoke(&proxy, &jsonrpc_finished, finished_data2, "foo", "");
ASSERT_TRUE(send_context.is_called);
ASSERT_TRUE(json_is_object(send_context.response));
ASSERT_FALSE(finished_context.is_called);
ASSERT_TRUE(finished_context2.is_called);
ASSERT_EQ(WF_BAD_BUSY, finished_context2.status);
wf_impl_jsonrpc_proxy_cleanup(&proxy);
wf_impl_timeout_manager_cleanup(&timeout_manager);
}
TEST(jsonrpc_proxy, invoke_fails_if_request_is_invalid)
{
struct wf_impl_timeout_manager timeout_manager;
wf_impl_timeout_manager_init(&timeout_manager);
SendContext send_context;
void * send_data = reinterpret_cast<void*>(&send_context);
struct wf_impl_jsonrpc_proxy proxy;
wf_impl_jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
FinishedContext finished_context;
void * finished_data = reinterpret_cast<void*>(&finished_context);
wf_impl_jsonrpc_proxy_invoke(&proxy, &jsonrpc_finished, finished_data, "foo", "?", "error");
ASSERT_FALSE(send_context.is_called);
ASSERT_TRUE(finished_context.is_called);
ASSERT_EQ(WF_BAD, finished_context.status);
wf_impl_jsonrpc_proxy_cleanup(&proxy);
wf_impl_timeout_manager_cleanup(&timeout_manager);
}
TEST(jsonrpc_proxy, on_result)
{
struct wf_impl_timeout_manager timeout_manager;
wf_impl_timeout_manager_init(&timeout_manager);
SendContext send_context;
void * send_data = reinterpret_cast<void*>(&send_context);
struct wf_impl_jsonrpc_proxy proxy;
wf_impl_jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
FinishedContext finished_context;
void * finished_data = reinterpret_cast<void*>(&finished_context);
wf_impl_jsonrpc_proxy_invoke(&proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
ASSERT_TRUE(send_context.is_called);
ASSERT_TRUE(json_is_object(send_context.response));
json_t * id = json_object_get(send_context.response, "id");
ASSERT_TRUE(json_is_number(id));
json_t * response = json_object();
json_object_set_new(response, "result", json_string("okay"));
json_object_set(response, "id", id);
wf_impl_jsonrpc_proxy_onresult(&proxy, response);
json_decref(response);
ASSERT_TRUE(finished_context.is_called);
ASSERT_EQ(WF_GOOD, finished_context.status);
ASSERT_TRUE(json_is_string(finished_context.result));
ASSERT_STREQ("okay", json_string_value(finished_context.result));
wf_impl_jsonrpc_proxy_cleanup(&proxy);
wf_impl_timeout_manager_cleanup(&timeout_manager);
}
TEST(jsonrpc_proxy, on_result_reject_response_with_unknown_id)
{
struct wf_impl_timeout_manager timeout_manager;
wf_impl_timeout_manager_init(&timeout_manager);
SendContext send_context;
void * send_data = reinterpret_cast<void*>(&send_context);
struct wf_impl_jsonrpc_proxy proxy;
wf_impl_jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
FinishedContext finished_context;
void * finished_data = reinterpret_cast<void*>(&finished_context);
wf_impl_jsonrpc_proxy_invoke(&proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
ASSERT_TRUE(send_context.is_called);
ASSERT_TRUE(json_is_object(send_context.response));
json_t * id = json_object_get(send_context.response, "id");
ASSERT_TRUE(json_is_number(id));
json_t * response = json_object();
json_object_set_new(response, "result", json_string("okay"));
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);
wf_impl_jsonrpc_proxy_cleanup(&proxy);
wf_impl_timeout_manager_cleanup(&timeout_manager);
}
TEST(jsonrpc_proxy, timeout)
{
struct wf_impl_timeout_manager timeout_manager;
wf_impl_timeout_manager_init(&timeout_manager);
SendContext send_context;
void * send_data = reinterpret_cast<void*>(&send_context);
struct wf_impl_jsonrpc_proxy proxy;
wf_impl_jsonrpc_proxy_init(&proxy, &timeout_manager, 0, &jsonrpc_send, send_data);
FinishedContext finished_context;
void * finished_data = reinterpret_cast<void*>(&finished_context);
wf_impl_jsonrpc_proxy_invoke(&proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
ASSERT_TRUE(send_context.is_called);
ASSERT_TRUE(json_is_object(send_context.response));
msleep(10);
wf_impl_timeout_manager_check(&timeout_manager);
ASSERT_TRUE(finished_context.is_called);
ASSERT_EQ(WF_BAD_TIMEOUT, finished_context.status);
wf_impl_jsonrpc_proxy_cleanup(&proxy);
wf_impl_timeout_manager_cleanup(&timeout_manager);
}
TEST(jsonrpc_proxy, cleanup_pending_request)
{
struct wf_impl_timeout_manager timeout_manager;
wf_impl_timeout_manager_init(&timeout_manager);
SendContext send_context;
void * send_data = reinterpret_cast<void*>(&send_context);
struct wf_impl_jsonrpc_proxy proxy;
wf_impl_jsonrpc_proxy_init(&proxy, &timeout_manager, 10, &jsonrpc_send, send_data);
FinishedContext finished_context;
void * finished_data = reinterpret_cast<void*>(&finished_context);
wf_impl_jsonrpc_proxy_invoke(&proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
ASSERT_TRUE(send_context.is_called);
ASSERT_TRUE(json_is_object(send_context.response));
ASSERT_FALSE(finished_context.is_called);
ASSERT_NE(nullptr, timeout_manager.timers);
wf_impl_jsonrpc_proxy_cleanup(&proxy);
ASSERT_TRUE(finished_context.is_called);
ASSERT_EQ(nullptr, timeout_manager.timers);
wf_impl_timeout_manager_cleanup(&timeout_manager);
}
TEST(jsonrpc_proxy, notify)
{
struct wf_impl_timeout_manager timeout_manager;
wf_impl_timeout_manager_init(&timeout_manager);
SendContext send_context;
void * send_data = reinterpret_cast<void*>(&send_context);
struct wf_impl_jsonrpc_proxy proxy;
wf_impl_jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
wf_impl_jsonrpc_proxy_notify(&proxy, "foo", "si", "bar", 42);
ASSERT_TRUE(send_context.is_called);
ASSERT_TRUE(json_is_object(send_context.response));
json_t * method = json_object_get(send_context.response, "method");
ASSERT_TRUE(json_is_string(method));
ASSERT_STREQ("foo", json_string_value(method));
json_t * params = json_object_get(send_context.response, "params");
ASSERT_TRUE(json_is_array(params));
ASSERT_EQ(2, json_array_size(params));
ASSERT_TRUE(json_is_string(json_array_get(params, 0)));
ASSERT_STREQ("bar", json_string_value(json_array_get(params, 0)));
ASSERT_TRUE(json_is_integer(json_array_get(params, 1)));
ASSERT_EQ(42, json_integer_value(json_array_get(params, 1)));
json_t * id = json_object_get(send_context.response, "id");
ASSERT_EQ(nullptr, id);
wf_impl_jsonrpc_proxy_cleanup(&proxy);
wf_impl_timeout_manager_cleanup(&timeout_manager);
}
TEST(jsonrpc_proxy, notify_dont_send_invalid_request)
{
struct wf_impl_timeout_manager timeout_manager;
wf_impl_timeout_manager_init(&timeout_manager);
SendContext send_context;
void * send_data = reinterpret_cast<void*>(&send_context);
struct wf_impl_jsonrpc_proxy proxy;
wf_impl_jsonrpc_proxy_init(&proxy, &timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
wf_impl_jsonrpc_proxy_notify(&proxy, "foo", "?");
ASSERT_FALSE(send_context.is_called);
wf_impl_jsonrpc_proxy_cleanup(&proxy);
wf_impl_timeout_manager_cleanup(&timeout_manager);
}

View File

@@ -0,0 +1,102 @@
#include <gtest/gtest.h>
#include "webfuse/adapter/impl/jsonrpc/request.h"
namespace
{
struct Context
{
json_t * response;
};
bool jsonrpc_send(
json_t * request,
void * user_data)
{
Context * context = reinterpret_cast<Context*>(user_data);
context->response = request;
json_incref(request);
return true;
}
}
TEST(jsonrpc_request, create_dispose)
{
Context context{nullptr};
void * user_data = reinterpret_cast<void*>(&context);
struct wf_impl_jsonrpc_request * request =
wf_impl_jsonrpc_request_create(42, &jsonrpc_send, user_data);
ASSERT_NE(nullptr, request);
ASSERT_EQ(user_data, wf_impl_jsonrpc_request_get_userdata(request));
wf_impl_jsonrpc_request_dispose(request);
}
TEST(jsonrpc_request, respond)
{
Context context{nullptr};
void * user_data = reinterpret_cast<void*>(&context);
struct wf_impl_jsonrpc_request * request =
wf_impl_jsonrpc_request_create(42, &jsonrpc_send, user_data);
wf_impl_jsonrpc_respond(request, json_string("okay"));
ASSERT_NE(nullptr, context.response);
json_t * response = reinterpret_cast<json_t*>(context.response);
ASSERT_TRUE(json_is_object(response));
json_t * id = json_object_get(response, "id");
ASSERT_TRUE(json_is_integer(id));
ASSERT_EQ(42, json_integer_value(id));
json_t * result = json_object_get(response, "result");
ASSERT_TRUE(json_is_string(result));
ASSERT_STREQ("okay", json_string_value(result));
ASSERT_EQ(nullptr, json_object_get(response, "error"));
json_decref(response);
}
TEST(jsonrpc_request, respond_error)
{
Context context{nullptr};
void * user_data = reinterpret_cast<void*>(&context);
struct wf_impl_jsonrpc_request * request =
wf_impl_jsonrpc_request_create(42, &jsonrpc_send, user_data);
wf_impl_jsonrpc_respond_error(request, WF_BAD);
ASSERT_NE(nullptr, context.response);
json_t * response = reinterpret_cast<json_t*>(context.response);
ASSERT_TRUE(json_is_object(response));
json_t * id = json_object_get(response, "id");
ASSERT_TRUE(json_is_integer(id));
ASSERT_EQ(42, json_integer_value(id));
ASSERT_EQ(nullptr, json_object_get(response, "result"));
json_t * err = json_object_get(response, "error");
ASSERT_TRUE(json_is_object(err));
json_t * err_code = json_object_get(err, "code");
ASSERT_TRUE(json_is_integer(err_code));
ASSERT_EQ(WF_BAD, json_integer_value(err_code));
json_t * err_message = json_object_get(err, "message");
ASSERT_TRUE(json_is_string(err_message));
ASSERT_STREQ("Bad", json_string_value(err_message));
json_decref(response);
}

View File

@@ -0,0 +1,56 @@
#include <gtest/gtest.h>
#include "webfuse/adapter/impl/jsonrpc/response.h"
TEST(json_response, init_result)
{
json_t * message = json_object();
json_object_set_new(message, "result", json_integer(47));
json_object_set_new(message, "id", json_integer(11));
struct wf_impl_jsonrpc_response response;
wf_impl_jsonrpc_response_init(&response, message);
ASSERT_EQ(WF_GOOD, response.status);
ASSERT_TRUE(json_is_integer(response.result));
ASSERT_EQ(47, json_integer_value(response.result));
ASSERT_EQ(11, response.id);
wf_impl_jsonrpc_response_cleanup(&response);
json_decref(message);
}
TEST(json_response, init_error)
{
json_t * message = json_object();
json_t * err = json_object();
json_object_set_new(err, "code", json_integer(WF_BAD_ACCESS_DENIED));
json_object_set_new(err, "message", json_string("access denied"));
json_object_set_new(message, "error", err);
json_object_set_new(message, "id", json_integer(23));
struct wf_impl_jsonrpc_response response;
wf_impl_jsonrpc_response_init(&response, message);
ASSERT_EQ(WF_BAD_ACCESS_DENIED, response.status);
ASSERT_EQ(nullptr, response.result);
ASSERT_EQ(23, response.id);
wf_impl_jsonrpc_response_cleanup(&response);
json_decref(message);
}
TEST(json_response, init_format_error)
{
json_t * message = json_object();
json_object_set_new(message, "id", json_integer(12));
struct wf_impl_jsonrpc_response response;
wf_impl_jsonrpc_response_init(&response, message);
ASSERT_EQ(WF_BAD_FORMAT, response.status);
ASSERT_EQ(nullptr, response.result);
ASSERT_EQ(12, response.id);
wf_impl_jsonrpc_response_cleanup(&response);
json_decref(message);
}

View File

@@ -0,0 +1,125 @@
#include <gtest/gtest.h>
#include "webfuse/adapter/impl/jsonrpc/server.h"
#include "webfuse/adapter/impl/jsonrpc/request.h"
namespace
{
struct Context
{
json_t * response;
bool is_called;
};
bool jsonrpc_send(
json_t * request,
void * user_data)
{
Context * context = reinterpret_cast<Context*>(user_data);
context->is_called = true;
context->response = request;
json_incref(request);
return true;
}
void sayHello(
struct wf_impl_jsonrpc_request * request,
char const * method_name,
json_t * params,
void * user_data)
{
(void) method_name;
(void) params;
(void) user_data;
json_t * result = json_string("Hello");
wf_impl_jsonrpc_respond(request, result);
}
}
TEST(jsonrpc_server, process_request)
{
struct wf_impl_jsonrpc_server server;
wf_impl_jsonrpc_server_init(&server);
wf_impl_jsonrpc_server_add(&server, "sayHello", &sayHello, nullptr);
Context context{nullptr, false};
void * user_data = reinterpret_cast<void*>(&context);
json_t * request = json_object();
json_object_set_new(request, "method", json_string("sayHello"));
json_object_set_new(request, "params", json_array());
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_NE(nullptr, context.response);
ASSERT_TRUE(json_is_object(context.response));
json_t * id = json_object_get(context.response, "id");
ASSERT_TRUE(json_is_integer(id));
ASSERT_EQ(23, json_integer_value(id));
json_t * result = json_object_get(context.response, "result");
ASSERT_TRUE(json_is_string(result));
ASSERT_STREQ("Hello", json_string_value(result));
json_decref(context.response);
json_decref(request);
wf_impl_jsonrpc_server_cleanup(&server);
}
TEST(jsonrpc_server, invoke_unknown_method)
{
struct wf_impl_jsonrpc_server server;
wf_impl_jsonrpc_server_init(&server);
wf_impl_jsonrpc_server_add(&server, "sayHello", &sayHello, nullptr);
Context context{nullptr, false};
void * user_data = reinterpret_cast<void*>(&context);
json_t * request = json_object();
json_object_set_new(request, "method", json_string("greet"));
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_NE(nullptr, context.response);
ASSERT_TRUE(json_is_object(context.response));
json_t * id = json_object_get(context.response, "id");
ASSERT_TRUE(json_is_integer(id));
ASSERT_EQ(42, json_integer_value(id));
json_t * err = json_object_get(context.response, "error");
ASSERT_TRUE(json_is_object(err));
json_t * err_code = json_object_get(err, "code");
ASSERT_TRUE(json_is_integer(err_code));
ASSERT_EQ(WF_BAD_NOTIMPLEMENTED, json_integer_value(err_code));
json_t * err_message = json_object_get(err, "message");
ASSERT_TRUE(json_is_string(err_message));
json_decref(context.response);
json_decref(request);
wf_impl_jsonrpc_server_cleanup(&server);
}
TEST(jsonrpc_server, skip_invalid_request)
{
struct wf_impl_jsonrpc_server server;
wf_impl_jsonrpc_server_init(&server);
Context context{nullptr, false};
void * user_data = reinterpret_cast<void*>(&context);
json_t * request = json_object();
json_object_set_new(request, "method", json_string("sayHello"));
json_object_set_new(request, "params", json_array());
wf_impl_jsonrpc_server_process(&server, request, &jsonrpc_send, user_data);
ASSERT_FALSE(context.is_called);
json_decref(request);
wf_impl_jsonrpc_server_cleanup(&server);
}

View File

@@ -0,0 +1,47 @@
#include <gtest/gtest.h>
#include "webfuse/adapter/impl/jsonrpc/util.h"
TEST(jsonrpc_util, get_int)
{
json_t * object = json_object();
json_object_set_new(object, "key", json_integer(23));
int value = wf_impl_json_get_int(object, "key", 42);
ASSERT_EQ(23, value);
json_decref(object);
}
TEST(jsonrpc_util, failed_to_get_null_object)
{
int value = wf_impl_json_get_int(nullptr, "key", 42);
ASSERT_EQ(42, value);
}
TEST(jsonrpc_util, failed_to_get_not_object)
{
json_t * object = json_array();
int value = wf_impl_json_get_int(nullptr, "key", 42);
ASSERT_EQ(42, value);
json_decref(object);
}
TEST(jsonrpc_util, failed_to_get_invalid_key)
{
json_t * object = json_object();
int value = wf_impl_json_get_int(object, "key", 42);
ASSERT_EQ(42, value);
json_decref(object);
}
TEST(jsonrpc_util, failed_to_get_invalid_value_type)
{
json_t * object = json_object();
json_object_set_new(object, "key", json_string("42"));
int value = wf_impl_json_get_int(object, "key", 42);
ASSERT_EQ(42, value);
json_decref(object);
}

View File

@@ -1,5 +1,5 @@
#include <gtest/gtest.h>
#include "webfuse/adapter/fuse_wrapper.h"
#include "webfuse/adapter/impl/fuse_wrapper.h"
TEST(libfuse, fuse_req_t_size)
{

View File

@@ -49,6 +49,23 @@ TEST(timer, trigger)
wf_impl_timeout_manager_cleanup(&manager);
}
TEST(timer, trigger_on_cleanup)
{
struct wf_impl_timeout_manager manager;
struct wf_impl_timer timer;
wf_impl_timeout_manager_init(&manager);
wf_impl_timer_init(&timer, &manager);
bool triggered = false;
wf_impl_timer_start(&timer, wf_impl_timepoint_in_msec(5 * 60 * 1000), &on_timeout, reinterpret_cast<void*>(&triggered));
wf_impl_timeout_manager_cleanup(&manager);
ASSERT_TRUE(triggered);
wf_impl_timer_cleanup(&timer);
}
TEST(timer, cancel)
{
struct wf_impl_timeout_manager manager;
@@ -69,6 +86,37 @@ TEST(timer, cancel)
wf_impl_timeout_manager_cleanup(&manager);
}
TEST(timer, cancel_multiple_timers)
{
static size_t const count = 5;
struct wf_impl_timeout_manager manager;
struct wf_impl_timer timer[count];
wf_impl_timeout_manager_init(&manager);
bool triggered = false;
for(size_t i = 0; i < count; i++)
{
wf_impl_timer_init(&timer[i], &manager);
wf_impl_timer_start(&timer[i], wf_impl_timepoint_in_msec(0), &on_timeout, &triggered);
}
msleep(10);
for(size_t i = 0; i < count; i++)
{
wf_impl_timer_cancel(&timer[i]);
}
wf_impl_timeout_manager_check(&manager);
ASSERT_FALSE(triggered);
for(size_t i = 0; i < count; i++)
{
wf_impl_timer_cleanup(&timer[0]);
}
wf_impl_timeout_manager_cleanup(&manager);
}
TEST(timer, multiple_timers)
{
static size_t const count = 5;

22
test/core/test_message.cc Normal file
View File

@@ -0,0 +1,22 @@
#include <gtest/gtest.h>
#include <cstring>
#include "webfuse/core/message.h"
TEST(wf_message, create)
{
json_t * value = json_object();
struct wf_message * message = wf_message_create(value);
ASSERT_NE(nullptr, message);
ASSERT_EQ(2, message->length);
ASSERT_TRUE(0 == strncmp("{}", message->data, 2));
wf_message_dispose(message);
json_decref(value);
}
TEST(wf_message, fail_to_create)
{
struct wf_message * message = wf_message_create(nullptr);
ASSERT_EQ(nullptr, message);
}

View File

@@ -0,0 +1,52 @@
#include <gtest/gtest.h>
#include "webfuse/core/message_queue.h"
#include "webfuse/core/message.h"
#include "webfuse/core/slist.h"
namespace
{
struct wf_slist_item * create_message(char const * content)
{
json_t * value = json_object();
json_object_set_new(value, "content", json_string(content));
struct wf_message * message = wf_message_create(value);
json_decref(value);
return &message->item;
}
}
TEST(wf_message_queue, cleanup_empty_list)
{
struct wf_slist queue;
wf_slist_init(&queue);
wf_message_queue_cleanup(&queue);
ASSERT_TRUE(wf_slist_empty(&queue));
}
TEST(wf_message_queue, cleanup_one_element)
{
struct wf_slist queue;
wf_slist_init(&queue);
wf_slist_append(&queue, create_message("Hello"));
wf_message_queue_cleanup(&queue);
ASSERT_TRUE(wf_slist_empty(&queue));
}
TEST(wf_message_queue, cleanup_multiple_element)
{
struct wf_slist queue;
wf_slist_init(&queue);
wf_slist_append(&queue, create_message("Hello"));
wf_slist_append(&queue, create_message("World"));
wf_slist_append(&queue, create_message("!"));
wf_message_queue_cleanup(&queue);
ASSERT_TRUE(wf_slist_empty(&queue));
}

30
test/core/test_status.cc Normal file
View File

@@ -0,0 +1,30 @@
#include <gtest/gtest.h>
#include "webfuse/core/status_intern.h"
TEST(wf_status, tostring)
{
ASSERT_STREQ("Good", wf_status_tostring(WF_GOOD));
ASSERT_STREQ("Bad", wf_status_tostring(WF_BAD));
ASSERT_STREQ("Bad (not implemented)", wf_status_tostring(WF_BAD_NOTIMPLEMENTED));
ASSERT_STREQ("Bad (busy)", wf_status_tostring(WF_BAD_BUSY));
ASSERT_STREQ("Bad (timeout)", wf_status_tostring(WF_BAD_TIMEOUT));
ASSERT_STREQ("Bad (format)", wf_status_tostring(WF_BAD_FORMAT));
ASSERT_STREQ("Bad (no entry)", wf_status_tostring(WF_BAD_NOENTRY));
ASSERT_STREQ("Bad (access denied)", wf_status_tostring(WF_BAD_ACCESS_DENIED));
ASSERT_STREQ("Bad (unknown)", wf_status_tostring(-1));
}
TEST(wf_status, to_rc)
{
ASSERT_EQ(0, wf_status_to_rc(WF_GOOD));
ASSERT_EQ(-ENOENT, wf_status_to_rc(WF_BAD));
ASSERT_EQ(-ENOSYS, wf_status_to_rc(WF_BAD_NOTIMPLEMENTED));
ASSERT_EQ(-ENOENT, wf_status_to_rc(WF_BAD_BUSY));
ASSERT_EQ(-ETIMEDOUT, wf_status_to_rc(WF_BAD_TIMEOUT));
ASSERT_EQ(-ENOENT, wf_status_to_rc(WF_BAD_FORMAT));
ASSERT_EQ(-ENOENT, wf_status_to_rc(WF_BAD_NOENTRY));
ASSERT_EQ(-EACCES, wf_status_to_rc(WF_BAD_ACCESS_DENIED));
ASSERT_EQ(-ENOENT, wf_status_to_rc(-1));
}

15
test/die_if.cc Normal file
View File

@@ -0,0 +1,15 @@
#include "die_if.hpp"
#include <cstdlib>
namespace webfuse_test
{
void die_if(bool expression)
{
if (expression)
{
exit(EXIT_FAILURE);
}
}
}

11
test/die_if.hpp Normal file
View File

@@ -0,0 +1,11 @@
#ifndef WF_TEST_DIE_IF_HPP
#define WF_TEST_DIE_IF_HPP
namespace webfuse_test
{
extern void die_if(bool expression);
}
#endif

View File

@@ -0,0 +1,85 @@
#include "integration/provider.hpp"
#include "webfuse_provider.h"
#include "webfuse/provider/impl/client.h"
#include <thread>
#include <mutex>
#include <string>
#include "msleep.hpp"
namespace webfuse_test
{
class Provider::Private
{
public:
explicit Private(char const * url)
: is_shutdown_requested(false)
{
config = wfp_client_config_create();
fs = wfp_static_filesystem_create(config);
wfp_static_filesystem_add_text(fs, "hello.txt", 0444, "Hello, World");
client = wfp_client_create(config);
wfp_client_connect(client, url);
while (!wfp_impl_client_is_connected(client))
{
wfp_client_service(client, 100);
}
thread = std::thread(Run, this);
webfuse_test::msleep(200);
}
~Private()
{
RequestShutdown();
thread.join();
wfp_client_dispose(client);
wfp_static_filesystem_dispose(fs);
wfp_client_config_dispose(config);
}
bool IsShutdownRequested()
{
std::lock_guard<std::mutex> lock(shutdown_lock);
return is_shutdown_requested;
}
private:
void RequestShutdown()
{
std::lock_guard<std::mutex> lock(shutdown_lock);
is_shutdown_requested = true;
}
static void Run(Provider::Private * context)
{
while (!context->IsShutdownRequested())
{
wfp_client_service(context->client, 100);
}
}
std::mutex shutdown_lock;
std::thread thread;
bool is_shutdown_requested;
wfp_client_config * config;
wfp_static_filesystem * fs;
public:
wfp_client * client;
};
Provider::Provider(char const * url)
: d(new Provider::Private(url))
{
}
Provider::~Provider()
{
delete d;
}
}

View File

@@ -0,0 +1,19 @@
#ifndef WF_TEST_INTEGRATION_PROVIDER
#define WF_TEST_INTEGRATION_PROVIDER
namespace webfuse_test
{
class Provider
{
public:
explicit Provider(char const * url);
~Provider();
private:
class Private;
Private * d;
};
}
#endif

View File

@@ -0,0 +1,99 @@
#include "integration/server.hpp"
#include <thread>
#include <mutex>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include "webfuse_adapter.h"
#include "webfuse/adapter/impl/server.h"
#include "msleep.hpp"
#define WF_PATH_MAX (100)
namespace webfuse_test
{
class Server::Private
{
public:
Private()
: is_shutdown_requested(false)
{
snprintf(base_dir, WF_PATH_MAX, "%s", "/tmp/webfuse_test_integration_XXXXXX");
mkdtemp(base_dir);
config = wf_server_config_create();
wf_server_config_set_port(config, 8080);
wf_server_config_set_mountpoint(config, base_dir);
server = wf_server_create(config);
while (!wf_impl_server_is_operational(server))
{
wf_server_service(server, 100);
}
thread = std::thread(Run, this);
}
~Private()
{
RequestShutdown();
thread.join();
rmdir(base_dir);
wf_server_dispose(server);
wf_server_config_dispose(config);
}
bool IsShutdownRequested()
{
std::lock_guard<std::mutex> lock(shutdown_lock);
return is_shutdown_requested;
}
private:
void RequestShutdown()
{
std::lock_guard<std::mutex> lock(shutdown_lock);
is_shutdown_requested = true;
}
static void Run(Server::Private * context)
{
while (!context->IsShutdownRequested())
{
wf_server_service(context->server, 100);
}
}
std::mutex shutdown_lock;
std::thread thread;
bool is_shutdown_requested;
public:
char base_dir[WF_PATH_MAX];
wf_server_config * config;
wf_server * server;
};
Server::Server()
: d(new Server::Private())
{
}
Server::~Server()
{
delete d;
}
char const * Server::GetBaseDir(void) const
{
return d->base_dir;
}
}

View File

@@ -0,0 +1,22 @@
#ifndef WF_TEST_INTEGRATION_SERVER_HPP
#define WF_TEST_INTEGRATION_SERVER_HPP
namespace webfuse_test
{
class Server
{
public:
Server();
~Server();
void Start(void);
void Stop(void);
char const * GetBaseDir(void) const;
private:
class Private;
Private * d;
};
}
#endif

View File

@@ -0,0 +1,156 @@
#include <gtest/gtest.h>
#include "integration/server.hpp"
#include "integration/provider.hpp"
#include <cstdio>
#include <csignal>
#include <cstring>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <jansson.h>
#include "webfuse/core/lws_log.h"
#include "die_if.hpp"
using webfuse_test::Server;
using webfuse_test::Provider;
using webfuse_test::die_if;
namespace
{
class IntegrationTest: public ::testing::Test
{
public:
IntegrationTest()
: server(nullptr)
, provider(nullptr)
{
json_object_seed(0);
wf_lwslog_disable();
}
protected:
void SetUp()
{
server = new Server();
provider = new Provider("ws://localhost:8080/");
}
void TearDown()
{
delete provider;
delete server;
}
char const * GetBaseDir() const
{
return server->GetBaseDir();
}
private:
Server * server;
Provider * provider;
};
}
TEST_F(IntegrationTest, HasMountpoint)
{
struct stat buffer;
int rc = stat(GetBaseDir(), &buffer);
ASSERT_EQ(0, rc);
ASSERT_TRUE(S_ISDIR(buffer.st_mode));
}
TEST_F(IntegrationTest, ProvidesTextFile)
{
std::string file_name = std::string(GetBaseDir()) + "/cprovider/default/hello.txt";
ASSERT_EXIT({
struct stat buffer;
int rc = stat(file_name.c_str(), &buffer);
die_if(0 != rc);
die_if(!S_ISREG(buffer.st_mode));
die_if(0444 != (buffer.st_mode & 0777));
die_if(12 != buffer.st_size);
exit(0);
}, ::testing::ExitedWithCode(0), ".*");
}
TEST_F(IntegrationTest, ReadTextFile)
{
std::string file_name = std::string(GetBaseDir()) + "/cprovider/default/hello.txt";
ASSERT_EXIT({
FILE * file = fopen(file_name.c_str(), "rb");
die_if(nullptr == file);
char buffer[13];
ssize_t count = fread(buffer, 1, 12, file);
int rc = fclose(file);
die_if(12 != count);
die_if(0 != strncmp("Hello, World", buffer, 12));
die_if(0 != rc);
exit(0);
}, ::testing::ExitedWithCode(0), ".*");
}
TEST_F(IntegrationTest, ReadDir)
{
std::string dir_name = std::string(GetBaseDir()) + "/cprovider/default";
ASSERT_EXIT({
DIR * dir = opendir(dir_name.c_str());
die_if(nullptr == dir);
bool found_self = false;
bool found_parent = false;
bool found_hello_txt = false;
bool found_other = false;
dirent * entry = readdir(dir);
while (NULL != entry)
{
if (0 == strcmp(".", entry->d_name))
{
found_self = true;
}
else if (0 == strcmp("..", entry->d_name))
{
found_parent = true;
}
else if (0 == strcmp("hello.txt", entry->d_name))
{
found_hello_txt = true;
}
else
{
found_other = true;
}
entry = readdir(dir);
}
closedir(dir);
die_if(!found_self);
die_if(!found_parent);
die_if(!found_hello_txt);
die_if(found_other);
exit(0);
}, ::testing::ExitedWithCode(0), ".*");
}

View File

@@ -10,7 +10,7 @@ void msleep(long millis)
long const msecs_per_nsec = (1000 * 1000);
long const seconds = millis / secs_per_msec;
long const nanos = (millis & secs_per_msec) * msecs_per_nsec;
long const nanos = (millis % secs_per_msec) * msecs_per_nsec;
struct timespec timeout = { seconds, nanos };
while (0 != nanosleep(&timeout, &timeout));