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:
112
test/adapter/jsonrpc/test_is_request.cc
Normal file
112
test/adapter/jsonrpc/test_is_request.cc
Normal 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);
|
||||
}
|
||||
94
test/adapter/jsonrpc/test_is_response.cc
Normal file
94
test/adapter/jsonrpc/test_is_response.cc
Normal 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);
|
||||
}
|
||||
393
test/adapter/jsonrpc/test_proxy.cc
Normal file
393
test/adapter/jsonrpc/test_proxy.cc
Normal 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);
|
||||
}
|
||||
102
test/adapter/jsonrpc/test_request.cc
Normal file
102
test/adapter/jsonrpc/test_request.cc
Normal 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);
|
||||
}
|
||||
56
test/adapter/jsonrpc/test_response.cc
Normal file
56
test/adapter/jsonrpc/test_response.cc
Normal 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);
|
||||
}
|
||||
125
test/adapter/jsonrpc/test_server.cc
Normal file
125
test/adapter/jsonrpc/test_server.cc
Normal 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);
|
||||
}
|
||||
47
test/adapter/jsonrpc/test_util.cc
Normal file
47
test/adapter/jsonrpc/test_util.cc
Normal 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);
|
||||
}
|
||||
63
test/adapter/test_authenticator.cc
Normal file
63
test/adapter/test_authenticator.cc
Normal file
@@ -0,0 +1,63 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <gmock/gmock.h>
|
||||
|
||||
#include "mock_authenticator.hpp"
|
||||
|
||||
#include "webfuse/adapter/impl/authenticator.h"
|
||||
#include "webfuse/adapter/impl/credentials.h"
|
||||
|
||||
using ::testing::Return;
|
||||
using ::testing::_;
|
||||
using ::webfuse_test::Authenticator;
|
||||
using ::webfuse_test::MockAuthenticator;
|
||||
using ::webfuse_test::set_authenticator;
|
||||
using ::webfuse_test::authenticate;
|
||||
|
||||
|
||||
TEST(Authenticator, Authenticate)
|
||||
{
|
||||
MockAuthenticator mock;
|
||||
set_authenticator(&mock);
|
||||
|
||||
struct wf_credentials creds;
|
||||
wf_impl_credentials_init(&creds, "username", nullptr);
|
||||
char dummy[] = "usr_data";
|
||||
void * user_data = reinterpret_cast<void*>(dummy);
|
||||
|
||||
EXPECT_CALL(mock, authenticate(&creds, user_data))
|
||||
.Times(1)
|
||||
.WillRepeatedly(Return(true));
|
||||
|
||||
struct wf_impl_authenticator * authenticator = wf_impl_authenticator_create(
|
||||
"username",
|
||||
&authenticate,
|
||||
user_data);
|
||||
|
||||
bool result = wf_impl_authenticator_autenticate(authenticator, &creds);
|
||||
ASSERT_TRUE(result);
|
||||
|
||||
wf_impl_authenticator_dispose(authenticator);
|
||||
wf_impl_credentials_cleanup(&creds);
|
||||
}
|
||||
|
||||
TEST(Authenticator, SkipAuthenticationWithWrongType)
|
||||
{
|
||||
MockAuthenticator mock;
|
||||
set_authenticator(&mock);
|
||||
|
||||
struct wf_credentials creds;
|
||||
wf_impl_credentials_init(&creds, "username", nullptr);
|
||||
EXPECT_CALL(mock, authenticate(_, _))
|
||||
.Times(0);
|
||||
|
||||
struct wf_impl_authenticator * authenticator = wf_impl_authenticator_create(
|
||||
"certificate",
|
||||
&authenticate,
|
||||
nullptr);
|
||||
|
||||
bool result = wf_impl_authenticator_autenticate(authenticator, &creds);
|
||||
ASSERT_FALSE(result);
|
||||
|
||||
wf_impl_authenticator_dispose(authenticator);
|
||||
wf_impl_credentials_cleanup(&creds);
|
||||
}
|
||||
154
test/adapter/test_authenticators.cc
Normal file
154
test/adapter/test_authenticators.cc
Normal file
@@ -0,0 +1,154 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <gmock/gmock.h>
|
||||
|
||||
#include "webfuse/adapter/impl/authenticators.h"
|
||||
#include "webfuse/adapter/impl/credentials.h"
|
||||
#include "mock_authenticator.hpp"
|
||||
|
||||
using ::testing::_;
|
||||
using ::testing::Return;
|
||||
using ::webfuse_test::MockAuthenticator;
|
||||
using ::webfuse_test::set_authenticator;
|
||||
using ::webfuse_test::authenticate;
|
||||
using ::webfuse_test::authenticate_1;
|
||||
using ::webfuse_test::authenticate_2;
|
||||
|
||||
|
||||
TEST(Authenticators, CloneEmpty)
|
||||
{
|
||||
struct wf_impl_authenticators authenticators;
|
||||
struct wf_impl_authenticators clone;
|
||||
|
||||
wf_impl_authenticators_init(&authenticators);
|
||||
ASSERT_EQ(nullptr, authenticators.first);
|
||||
|
||||
wf_impl_authenticators_clone(&authenticators, &clone);
|
||||
ASSERT_EQ(nullptr, clone.first);
|
||||
|
||||
wf_impl_authenticators_cleanup(&authenticators);
|
||||
wf_impl_authenticators_cleanup(&clone);
|
||||
}
|
||||
|
||||
TEST(Authenticators, Clone)
|
||||
{
|
||||
struct wf_impl_authenticators authenticators;
|
||||
struct wf_impl_authenticators clone;
|
||||
|
||||
wf_impl_authenticators_init(&authenticators);
|
||||
wf_impl_authenticators_add(&authenticators, "username", &authenticate, nullptr);
|
||||
ASSERT_NE(nullptr, authenticators.first);
|
||||
|
||||
wf_impl_authenticators_clone(&authenticators, &clone);
|
||||
ASSERT_NE(nullptr, clone.first);
|
||||
ASSERT_NE(nullptr, authenticators.first);
|
||||
ASSERT_NE(authenticators.first, clone.first);
|
||||
|
||||
wf_impl_authenticators_cleanup(&authenticators);
|
||||
wf_impl_authenticators_cleanup(&clone);
|
||||
}
|
||||
|
||||
TEST(Authenticators, Move)
|
||||
{
|
||||
struct wf_impl_authenticators authenticators;
|
||||
struct wf_impl_authenticators clone;
|
||||
|
||||
wf_impl_authenticators_init(&authenticators);
|
||||
wf_impl_authenticators_add(&authenticators, "username", &authenticate, nullptr);
|
||||
ASSERT_NE(nullptr, authenticators.first);
|
||||
|
||||
wf_impl_authenticators_move(&authenticators, &clone);
|
||||
ASSERT_NE(nullptr, clone.first);
|
||||
ASSERT_EQ(nullptr, authenticators.first);
|
||||
ASSERT_NE(authenticators.first, clone.first);
|
||||
|
||||
wf_impl_authenticators_cleanup(&authenticators);
|
||||
wf_impl_authenticators_cleanup(&clone);
|
||||
}
|
||||
|
||||
TEST(Authenticators, AuthenticateWithoutAuthenticators)
|
||||
{
|
||||
struct wf_credentials creds;
|
||||
wf_impl_credentials_init(&creds, "username", nullptr);
|
||||
|
||||
struct wf_impl_authenticators authenticators;
|
||||
wf_impl_authenticators_init(&authenticators);
|
||||
|
||||
bool result = wf_impl_authenticators_authenticate(&authenticators, &creds);
|
||||
ASSERT_TRUE(result);
|
||||
|
||||
result = wf_impl_authenticators_authenticate(&authenticators, nullptr);
|
||||
ASSERT_TRUE(result);
|
||||
|
||||
wf_impl_authenticators_cleanup(&authenticators);
|
||||
wf_impl_credentials_cleanup(&creds);
|
||||
}
|
||||
|
||||
TEST(Authenticators, FailToAuthenticateWithoutCredentials)
|
||||
{
|
||||
MockAuthenticator mock;
|
||||
set_authenticator(&mock);
|
||||
|
||||
struct wf_impl_authenticators authenticators;
|
||||
wf_impl_authenticators_init(&authenticators);
|
||||
wf_impl_authenticators_add(&authenticators, "username", &authenticate, nullptr);
|
||||
|
||||
bool result = wf_impl_authenticators_authenticate(&authenticators, nullptr);
|
||||
ASSERT_FALSE(result);
|
||||
|
||||
wf_impl_authenticators_cleanup(&authenticators);
|
||||
}
|
||||
|
||||
TEST(Authenticators, AuthenticateWithMultipleCredentials)
|
||||
{
|
||||
struct wf_credentials creds;
|
||||
wf_impl_credentials_init(&creds, "username", nullptr);
|
||||
|
||||
MockAuthenticator username_mock;
|
||||
set_authenticator(1, &username_mock);
|
||||
EXPECT_CALL(username_mock, authenticate(&creds, nullptr))
|
||||
.Times(1)
|
||||
.WillRepeatedly(Return(true));
|
||||
|
||||
MockAuthenticator certificate_mock;
|
||||
set_authenticator(2, &certificate_mock);
|
||||
EXPECT_CALL(certificate_mock, authenticate(_, _))
|
||||
.Times(0);
|
||||
|
||||
struct wf_impl_authenticators authenticators;
|
||||
wf_impl_authenticators_init(&authenticators);
|
||||
wf_impl_authenticators_add(&authenticators, "username", &authenticate_1, nullptr);
|
||||
wf_impl_authenticators_add(&authenticators, "certificate", &authenticate_2, nullptr);
|
||||
|
||||
bool result = wf_impl_authenticators_authenticate(&authenticators, &creds);
|
||||
ASSERT_TRUE(result);
|
||||
|
||||
wf_impl_authenticators_cleanup(&authenticators);
|
||||
wf_impl_credentials_cleanup(&creds);
|
||||
}
|
||||
|
||||
TEST(Authenticators, FailedAuthenticateWithWrongType)
|
||||
{
|
||||
struct wf_credentials creds;
|
||||
wf_impl_credentials_init(&creds, "token", nullptr);
|
||||
|
||||
MockAuthenticator username_mock;
|
||||
set_authenticator(1, &username_mock);
|
||||
EXPECT_CALL(username_mock, authenticate(&creds, nullptr))
|
||||
.Times(0);
|
||||
|
||||
MockAuthenticator certificate_mock;
|
||||
set_authenticator(2, &certificate_mock);
|
||||
EXPECT_CALL(certificate_mock, authenticate(_, _))
|
||||
.Times(0);
|
||||
|
||||
struct wf_impl_authenticators authenticators;
|
||||
wf_impl_authenticators_init(&authenticators);
|
||||
wf_impl_authenticators_add(&authenticators, "username", &authenticate_1, nullptr);
|
||||
wf_impl_authenticators_add(&authenticators, "certificate", &authenticate_2, nullptr);
|
||||
|
||||
bool result = wf_impl_authenticators_authenticate(&authenticators, &creds);
|
||||
ASSERT_FALSE(result);
|
||||
|
||||
wf_impl_authenticators_cleanup(&authenticators);
|
||||
wf_impl_credentials_cleanup(&creds);
|
||||
}
|
||||
70
test/adapter/test_credentials.cc
Normal file
70
test/adapter/test_credentials.cc
Normal file
@@ -0,0 +1,70 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "webfuse/adapter/impl/credentials.h"
|
||||
#include <jansson.h>
|
||||
|
||||
TEST(Credentials, Type)
|
||||
{
|
||||
struct wf_credentials creds;
|
||||
|
||||
wf_impl_credentials_init(&creds, "test", nullptr);
|
||||
ASSERT_STREQ("test", wf_impl_credentials_type(&creds));
|
||||
wf_impl_credentials_cleanup(&creds);
|
||||
}
|
||||
|
||||
TEST(Credentials, Get)
|
||||
{
|
||||
struct wf_credentials creds;
|
||||
json_t * data = json_object();
|
||||
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);
|
||||
ASSERT_STREQ("username", wf_impl_credentials_type(&creds));
|
||||
ASSERT_STREQ("bob", wf_impl_credentials_get(&creds, "username"));
|
||||
ASSERT_STREQ("<secret>", wf_impl_credentials_get(&creds, "password"));
|
||||
|
||||
wf_impl_credentials_cleanup(&creds);
|
||||
json_decref(data);
|
||||
}
|
||||
|
||||
TEST(Credentials, FailedToGetNonexistingValue)
|
||||
{
|
||||
struct wf_credentials creds;
|
||||
json_t * data = json_object();
|
||||
|
||||
wf_impl_credentials_init(&creds, "username", data);
|
||||
ASSERT_STREQ("username", wf_impl_credentials_type(&creds));
|
||||
ASSERT_STREQ(nullptr, wf_impl_credentials_get(&creds, "username"));
|
||||
ASSERT_STREQ(nullptr, wf_impl_credentials_get(&creds, "password"));
|
||||
|
||||
wf_impl_credentials_cleanup(&creds);
|
||||
json_decref(data);
|
||||
}
|
||||
|
||||
TEST(Credentials, FailedToGetWithoutData)
|
||||
{
|
||||
struct wf_credentials creds;
|
||||
|
||||
wf_impl_credentials_init(&creds, "username", nullptr);
|
||||
ASSERT_STREQ("username", wf_impl_credentials_type(&creds));
|
||||
ASSERT_STREQ(nullptr, wf_impl_credentials_get(&creds, "username"));
|
||||
ASSERT_STREQ(nullptr, wf_impl_credentials_get(&creds, "password"));
|
||||
|
||||
wf_impl_credentials_cleanup(&creds);
|
||||
}
|
||||
|
||||
TEST(Credentials, FailedToGetWrongDataType)
|
||||
{
|
||||
struct wf_credentials creds;
|
||||
json_t * data = json_string("invalid_creds");
|
||||
|
||||
wf_impl_credentials_init(&creds, "username", data);
|
||||
ASSERT_STREQ("username", wf_impl_credentials_type(&creds));
|
||||
ASSERT_STREQ(nullptr, wf_impl_credentials_get(&creds, "username"));
|
||||
ASSERT_STREQ(nullptr, wf_impl_credentials_get(&creds, "password"));
|
||||
|
||||
wf_impl_credentials_cleanup(&creds);
|
||||
json_decref(data);
|
||||
}
|
||||
|
||||
7
test/adapter/test_fuse_req.cc
Normal file
7
test/adapter/test_fuse_req.cc
Normal file
@@ -0,0 +1,7 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "webfuse/adapter/impl/fuse_wrapper.h"
|
||||
|
||||
TEST(libfuse, fuse_req_t_size)
|
||||
{
|
||||
ASSERT_EQ(sizeof(void*), sizeof(fuse_req_t));
|
||||
}
|
||||
54
test/adapter/test_response_parser.cc
Normal file
54
test/adapter/test_response_parser.cc
Normal file
@@ -0,0 +1,54 @@
|
||||
#include <string>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "webfuse/adapter/impl/jsonrpc/response.h"
|
||||
|
||||
|
||||
static void response_parse_str(
|
||||
std::string const & buffer,
|
||||
struct wf_impl_jsonrpc_response * response)
|
||||
{
|
||||
json_t * message = json_loadb(buffer.c_str(), buffer.size(), 0, nullptr);
|
||||
if (nullptr != message)
|
||||
{
|
||||
wf_impl_jsonrpc_response_init(response, message);
|
||||
json_decref(message);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(response_parser, test)
|
||||
{
|
||||
struct wf_impl_jsonrpc_response response;
|
||||
|
||||
// no object
|
||||
response_parse_str("[]", &response);
|
||||
ASSERT_NE(WF_GOOD, response.status);
|
||||
ASSERT_EQ(-1, response.id);
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
|
||||
// empty
|
||||
response_parse_str("{}", &response);
|
||||
ASSERT_NE(WF_GOOD, response.status);
|
||||
ASSERT_EQ(-1, response.id);
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
|
||||
// no data
|
||||
response_parse_str("{\"id\":42}", &response);
|
||||
ASSERT_NE(WF_GOOD, response.status);
|
||||
ASSERT_EQ(42, response.id);
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
|
||||
// custom error code
|
||||
response_parse_str("{\"error\":{\"code\": 42}, \"id\": 42}", &response);
|
||||
ASSERT_NE(WF_GOOD, response.status);
|
||||
ASSERT_EQ(42, response.status);
|
||||
ASSERT_EQ(42, response.id);
|
||||
ASSERT_EQ(nullptr, response.result);
|
||||
|
||||
// valid response
|
||||
response_parse_str("{\"result\": true, \"id\": 42}", &response);
|
||||
ASSERT_EQ(WF_GOOD, response.status);
|
||||
ASSERT_EQ(42, response.id);
|
||||
ASSERT_NE(nullptr, response.result);
|
||||
json_decref(response.result);
|
||||
}
|
||||
25
test/adapter/test_server.cc
Normal file
25
test/adapter/test_server.cc
Normal file
@@ -0,0 +1,25 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <cstdlib>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "webfuse/adapter/server.h"
|
||||
#include "webfuse/adapter/server_config.h"
|
||||
|
||||
|
||||
TEST(server, create_dispose)
|
||||
{
|
||||
mkdir("test", 0700);
|
||||
|
||||
struct wf_server_config * config = wf_server_config_create();
|
||||
wf_server_config_set_mountpoint(config, "test");
|
||||
struct wf_server * server = wf_server_create(config);
|
||||
ASSERT_NE(nullptr, server);
|
||||
|
||||
wf_server_dispose(server);
|
||||
wf_server_config_dispose(config);
|
||||
|
||||
rmdir("test");
|
||||
}
|
||||
36
test/adapter/test_timepoint.cc
Normal file
36
test/adapter/test_timepoint.cc
Normal file
@@ -0,0 +1,36 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "msleep.hpp"
|
||||
#include "webfuse/adapter/impl/time/timepoint.h"
|
||||
|
||||
using webfuse_test::msleep;
|
||||
|
||||
TEST(timepoint, now)
|
||||
{
|
||||
wf_impl_timepoint start = wf_impl_timepoint_now();
|
||||
msleep(42);
|
||||
wf_impl_timepoint end = wf_impl_timepoint_now();
|
||||
|
||||
ASSERT_LT(start, end);
|
||||
ASSERT_LT(end, start + 500);
|
||||
}
|
||||
|
||||
TEST(timepoint, in_msec)
|
||||
{
|
||||
wf_impl_timepoint now = wf_impl_timepoint_now();
|
||||
wf_impl_timepoint later = wf_impl_timepoint_in_msec(42);
|
||||
|
||||
ASSERT_LT(now, later);
|
||||
ASSERT_LT(later, now + 500);
|
||||
}
|
||||
|
||||
TEST(wf_impl_timepoint, elapsed)
|
||||
{
|
||||
wf_impl_timepoint now;
|
||||
|
||||
now = wf_impl_timepoint_now();
|
||||
ASSERT_TRUE(wf_impl_timepoint_is_elapsed(now - 1));
|
||||
|
||||
now =wf_impl_timepoint_now();
|
||||
ASSERT_FALSE(wf_impl_timepoint_is_elapsed(now + 500));
|
||||
}
|
||||
149
test/adapter/test_timer.cc
Normal file
149
test/adapter/test_timer.cc
Normal file
@@ -0,0 +1,149 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
#include "msleep.hpp"
|
||||
#include "webfuse/adapter/impl/time/timer.h"
|
||||
#include "webfuse/adapter/impl/time/timeout_manager.h"
|
||||
|
||||
using std::size_t;
|
||||
using webfuse_test::msleep;
|
||||
|
||||
namespace
|
||||
{
|
||||
void on_timeout(struct wf_impl_timer * timer)
|
||||
{
|
||||
bool * triggered = reinterpret_cast<bool*>(timer->user_data);
|
||||
*triggered = true;
|
||||
}
|
||||
}
|
||||
|
||||
TEST(timer, init)
|
||||
{
|
||||
struct wf_impl_timeout_manager manager;
|
||||
struct wf_impl_timer timer;
|
||||
|
||||
wf_impl_timeout_manager_init(&manager);
|
||||
wf_impl_timer_init(&timer, &manager);
|
||||
|
||||
wf_impl_timer_cleanup(&timer);
|
||||
wf_impl_timeout_manager_cleanup(&manager);
|
||||
}
|
||||
|
||||
TEST(timer, trigger)
|
||||
{
|
||||
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(250), &on_timeout, reinterpret_cast<void*>(&triggered));
|
||||
msleep(500);
|
||||
wf_impl_timeout_manager_check(&manager);
|
||||
|
||||
ASSERT_TRUE(triggered);
|
||||
|
||||
wf_impl_timer_cleanup(&timer);
|
||||
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;
|
||||
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(250), &on_timeout, &triggered);
|
||||
msleep(500);
|
||||
wf_impl_timer_cancel(&timer);
|
||||
wf_impl_timeout_manager_check(&manager);
|
||||
|
||||
ASSERT_FALSE(triggered);
|
||||
|
||||
wf_impl_timer_cleanup(&timer);
|
||||
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;
|
||||
struct wf_impl_timeout_manager manager;
|
||||
struct wf_impl_timer timer[count];
|
||||
bool triggered[count];
|
||||
|
||||
wf_impl_timeout_manager_init(&manager);
|
||||
|
||||
for(size_t i = 0; i < count; i++)
|
||||
{
|
||||
wf_impl_timer_init(&timer[i], &manager);
|
||||
triggered[i] = false;
|
||||
wf_impl_timer_start(&timer[i], wf_impl_timepoint_in_msec(300 - (50 * i)), &on_timeout, &triggered[i]);
|
||||
}
|
||||
|
||||
for(size_t i = 0; i < count; i++)
|
||||
{
|
||||
msleep(100);
|
||||
wf_impl_timeout_manager_check(&manager);
|
||||
}
|
||||
|
||||
for(size_t i = 0; i < count; i++)
|
||||
{
|
||||
ASSERT_TRUE(triggered[i]);
|
||||
wf_impl_timer_cleanup(&timer[i]);
|
||||
}
|
||||
|
||||
wf_impl_timeout_manager_cleanup(&manager);
|
||||
}
|
||||
Reference in New Issue
Block a user