1
0
mirror of https://github.com/falk-werner/webfuse synced 2024-10-27 20:34:10 +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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
49 changed files with 1650 additions and 54 deletions

View File

@ -76,6 +76,7 @@ add_library(webfuse-core STATIC
lib/webfuse/core/status.c
lib/webfuse/core/string.c
lib/webfuse/core/path.c
lib/webfuse/core/lws_log.c
)
set_target_properties(webfuse-core PROPERTIES OUTPUT_NAME webfuse-core)
@ -291,25 +292,40 @@ pkg_check_modules(GMOCK gmock)
add_executable(alltests
test/msleep.cc
test/die_if.cc
test/mock_authenticator.cc
test/mock_request.cc
test/test_container_of.cc
test/test_response_parser.cc
test/test_server.cc
test/test_timepoint.cc
test/test_timer.cc
test/test_url.cc
test/test_credentials.cc
test/test_authenticator.cc
test/test_authenticators.cc
test/test_string.cc
test/test_slist.cc
test/test_path.cc
test/test_static_filesystem.cc
test/core/test_container_of.cc
test/core/test_string.cc
test/core/test_slist.cc
test/core/test_path.cc
test/core/test_status.cc
test/core/test_message.cc
test/core/test_message_queue.cc
test/adapter/test_response_parser.cc
test/adapter/test_server.cc
test/adapter/test_timepoint.cc
test/adapter/test_timer.cc
test/adapter/test_credentials.cc
test/adapter/test_authenticator.cc
test/adapter/test_authenticators.cc
test/adapter/test_fuse_req.cc
test/adapter/jsonrpc/test_util.cc
test/adapter/jsonrpc/test_is_request.cc
test/adapter/jsonrpc/test_request.cc
test/adapter/jsonrpc/test_is_response.cc
test/adapter/jsonrpc/test_response.cc
test/adapter/jsonrpc/test_server.cc
test/adapter/jsonrpc/test_proxy.cc
test/provider/test_url.cc
test/provider/test_static_filesystem.cc
test/integration/test_integration.cc
test/integration/server.cc
test/integration/provider.cc
)
target_link_libraries(alltests PUBLIC webfuse-adapter-static webfuse-provider-static webfuse-core ${EXTRA_LIBS} ${GMOCK_LIBRARIES} ${GTEST_LIBRARIES})
target_include_directories(alltests PUBLIC lib ${GMOCK_INCLUDE_DIRS} ${GTEST_INCLUDE_DIRS})
target_include_directories(alltests PUBLIC test lib ${GMOCK_INCLUDE_DIRS} ${GTEST_INCLUDE_DIRS})
target_compile_options(alltests PUBLIC ${GMOCK_CFLAGS} ${GTEST_CFLAGS})
enable_testing()
@ -320,6 +336,7 @@ add_custom_target(coverage
COMMAND mkdir -p coverage
COMMAND lcov --capture --directory . --output-file coverage/lcov.info
COMMAND lcov --remove coverage/lcov.info '/usr/*' --output-file coverage/lcov.info
COMMAND lcov --remove coverage/lcov.info '*/test/*' --output-file coverage/lcov.info
)
add_dependencies(coverage alltests)

View File

@ -3,8 +3,6 @@
#include "webfuse/adapter/impl/jsonrpc/response.h"
#define WF_DEFAULT_TIMEOUT (10 * 1000)
static void wf_impl_jsonrpc_proxy_timeout(
struct wf_impl_timer * timer)
{
@ -53,8 +51,9 @@ static json_t * wf_impl_jsonrpc_request_create(
break;
default:
fprintf(stderr, "fatal: unknown param_type '%c'\n", *param_type);
exit(EXIT_FAILURE);
break;
json_decref(params);
json_decref(request);
return NULL;
}
}
@ -71,10 +70,12 @@ static json_t * wf_impl_jsonrpc_request_create(
void wf_impl_jsonrpc_proxy_init(
struct wf_impl_jsonrpc_proxy * proxy,
struct wf_impl_timeout_manager * timeout_manager,
int timeout,
wf_impl_jsonrpc_send_fn * send,
void * user_data)
{
proxy->send = send;
proxy->timeout = timeout;
proxy->user_data = user_data;
proxy->request.is_pending = false;
@ -84,13 +85,21 @@ void wf_impl_jsonrpc_proxy_init(
void wf_impl_jsonrpc_proxy_cleanup(
struct wf_impl_jsonrpc_proxy * proxy)
{
wf_impl_timer_cleanup(&proxy->request.timer);
if (proxy->request.is_pending)
{
proxy->request.finished(proxy->request.user_data, WF_BAD, NULL);
void * user_data = proxy->request.user_data;
wf_impl_jsonrpc_proxy_finished_fn * finished = proxy->request.finished;
proxy->request.is_pending = false;
proxy->request.finished = NULL;
proxy->request.user_data = NULL;
proxy->request.id = 0;
wf_impl_timer_cancel(&proxy->request.timer);
finished(user_data, WF_BAD, NULL);
}
wf_impl_timer_cleanup(&proxy->request.timer);
}
void wf_impl_jsonrpc_proxy_invoke(
@ -108,25 +117,29 @@ void wf_impl_jsonrpc_proxy_invoke(
proxy->request.finished = finished;
proxy->request.user_data = user_data;
proxy->request.id = 42;
wf_impl_timer_start(&proxy->request.timer, wf_impl_timepoint_in_msec(WF_DEFAULT_TIMEOUT),
wf_impl_timer_start(&proxy->request.timer, wf_impl_timepoint_in_msec(proxy->timeout),
&wf_impl_jsonrpc_proxy_timeout, proxy);
va_list args;
va_start(args, param_info);
json_t * request = wf_impl_jsonrpc_request_create(method_name, proxy->request.id, param_info, args);
va_end(args);
bool const is_send = ((NULL != request) && (proxy->send(request, proxy->user_data)));
if (!is_send)
{
proxy->request.is_pending = false;
proxy->request.finished = NULL;
proxy->request.user_data = NULL;
proxy->request.id = 0;
wf_impl_timer_cancel(&proxy->request.timer);
finished(user_data, WF_BAD, NULL);
}
if (NULL != request)
{
if (!proxy->send(request, proxy->user_data))
{
proxy->request.is_pending = false;
proxy->request.finished = NULL;
proxy->request.user_data = NULL;
proxy->request.id = 0;
wf_impl_timer_cancel(&proxy->request.timer);
finished(user_data, WF_BAD, NULL);
}
json_decref(request);
}
}

View File

@ -39,6 +39,7 @@ struct wf_impl_jsonrpc_request
struct wf_impl_jsonrpc_proxy
{
struct wf_impl_jsonrpc_request request;
int timeout;
wf_impl_jsonrpc_send_fn * send;
void * user_data;
};
@ -46,6 +47,7 @@ struct wf_impl_jsonrpc_proxy
extern void wf_impl_jsonrpc_proxy_init(
struct wf_impl_jsonrpc_proxy * proxy,
struct wf_impl_timeout_manager * manager,
int timeout,
wf_impl_jsonrpc_send_fn * send,
void * user_data);

View File

@ -93,7 +93,7 @@ void wf_impl_operation_read(
{
int const length = (size <= WF_MAX_READ_LENGTH) ? (int) size : WF_MAX_READ_LENGTH;
int handle = (file_info->fh & INT_MAX);
wf_impl_jsonrpc_proxy_invoke(rpc, &wf_impl_operation_read_finished, request, "read", "siiii", user_data->name, inode, handle, (int) offset, length);
wf_impl_jsonrpc_proxy_invoke(rpc, &wf_impl_operation_read_finished, request, "read", "siiii", user_data->name, (int) inode, handle, (int) offset, length);
}
else
{

View File

@ -10,8 +10,8 @@
#include "webfuse/adapter/impl/server_config.h"
#include "webfuse/adapter/impl/server_protocol.h"
#include "webfuse/core/lws_log.h"
#define WF_DISABLE_LWS_LOG 0
#define WF_SERVER_PROTOCOL_COUNT 3
struct wf_server
@ -33,7 +33,7 @@ static bool wf_impl_server_tls_enabled(
static struct lws_context * wf_impl_server_context_create(
struct wf_server * server)
{
lws_set_log_level(WF_DISABLE_LWS_LOG, NULL);
wf_lwslog_disable();
memset(server->ws_protocols, 0, sizeof(struct lws_protocols) * WF_SERVER_PROTOCOL_COUNT);
server->ws_protocols[0].name = "http";
@ -125,6 +125,12 @@ void wf_impl_server_dispose(
free(server);
}
bool wf_impl_server_is_operational(
struct wf_server * server)
{
return server->protocol.is_operational;
}
void wf_impl_server_service(
struct wf_server * server,
int timeout_ms)

View File

@ -1,6 +1,10 @@
#ifndef WF_ADAPTER_IMPL_SERVER_H
#define WF_ADAPTER_IMPL_SERVER_H
#ifndef __cplusplus
#include <stdbool.h>
#endif
#ifdef __cplusplus
extern "C"
{
@ -15,6 +19,9 @@ extern struct wf_server * wf_impl_server_create(
extern void wf_impl_server_dispose(
struct wf_server * server);
extern bool wf_impl_server_is_operational(
struct wf_server * server);
extern void wf_impl_server_service(
struct wf_server * server,
int timeout_ms);

View File

@ -30,6 +30,9 @@ static int wf_impl_server_protocol_callback(
switch (reason)
{
case LWS_CALLBACK_PROTOCOL_INIT:
protocol->is_operational = true;
break;
case LWS_CALLBACK_ESTABLISHED:
session = wf_impl_session_manager_add(
&protocol->session_manager,
@ -206,6 +209,7 @@ void wf_impl_server_protocol_init(
char * mount_point)
{
protocol->mount_point = strdup(mount_point);
protocol->is_operational = false;
wf_impl_timeout_manager_init(&protocol->timeout_manager);
wf_impl_session_manager_init(&protocol->session_manager);
@ -220,6 +224,8 @@ void wf_impl_server_protocol_cleanup(
struct wf_server_protocol * protocol)
{
free(protocol->mount_point);
protocol->is_operational = false;
wf_impl_jsonrpc_server_cleanup(&protocol->server);
wf_impl_timeout_manager_cleanup(&protocol->timeout_manager);
wf_impl_authenticators_cleanup(&protocol->authenticators);

View File

@ -7,6 +7,10 @@
#include "webfuse/adapter/impl/session_manager.h"
#include "webfuse/adapter/impl/jsonrpc/server.h"
#ifndef __cplusplus
#include <stdbool.h>
#endif
#ifdef __cplusplus
extern "C"
{
@ -21,6 +25,7 @@ struct wf_server_protocol
struct wf_impl_authenticators authenticators;
struct wf_impl_session_manager session_manager;
struct wf_impl_jsonrpc_server server;
bool is_operational;
};
extern void wf_impl_server_protocol_init(

View File

@ -13,6 +13,8 @@
#include <stddef.h>
#include <stdlib.h>
#define WF_DEFAULT_TIMEOUT (10 * 1000)
static bool wf_impl_session_send(
json_t * request,
void * user_data)
@ -55,7 +57,7 @@ struct wf_impl_session * wf_impl_session_create(
session->is_authenticated = false;
session->authenticators = authenticators;
session->server = server;
wf_impl_jsonrpc_proxy_init(&session->rpc, timeout_manager, &wf_impl_session_send, session);
wf_impl_jsonrpc_proxy_init(&session->rpc, timeout_manager, WF_DEFAULT_TIMEOUT, &wf_impl_session_send, session);
wf_slist_init(&session->messages);
}
@ -79,10 +81,10 @@ static void wf_impl_session_dispose_filesystems(
void wf_impl_session_dispose(
struct wf_impl_session * session)
{
wf_impl_session_dispose_filesystems(&session->filesystems);
wf_impl_jsonrpc_proxy_cleanup(&session->rpc);
wf_message_queue_cleanup(&session->messages);
wf_impl_session_dispose_filesystems(&session->filesystems);
session->is_authenticated = false;
session->wsi = NULL;
session->authenticators = NULL;

View File

@ -0,0 +1,18 @@
#include "webfuse/core/lws_log.h"
#include <stdbool.h>
#include <libwebsockets.h>
#define WF_LWSLOG_DISABLE 0
static bool wf_lwslog_is_diabled = false;
void wf_lwslog_disable(void)
{
if (!wf_lwslog_is_diabled)
{
lws_set_log_level(WF_LWSLOG_DISABLE, NULL);
wf_lwslog_is_diabled = true;
}
}

View File

@ -0,0 +1,15 @@
#ifndef WF_LWS_LOG_H
#define WF_LWS_LOG_H
#ifdef __cplusplus
extern "C"
{
#endif
extern void wf_lwslog_disable(void);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -27,11 +27,6 @@ void wf_slist_append(
item->next = NULL;
list->last->next = item;
list->last = item;
if (NULL == list->head.next)
{
list->head.next = item;
}
}
struct wf_slist_item * wf_slist_remove_first(

View File

@ -23,7 +23,7 @@ char const * wf_status_tostring(wf_status status)
{
case WF_GOOD: return "Good";
case WF_BAD: return "Bad";
case WF_BAD_NOTIMPLEMENTED: return "Bad (not implelemted)";
case WF_BAD_NOTIMPLEMENTED: return "Bad (not implemented)";
case WF_BAD_TIMEOUT: return "Bad (timeout)";
case WF_BAD_BUSY: return "Bad (busy)";
case WF_BAD_FORMAT: return "Bad (format)";

View File

@ -10,9 +10,9 @@
#include "webfuse/provider/impl/client_protocol.h"
#include "webfuse/provider/impl/client_config.h"
#include "webfuse/provider/impl/url.h"
#include "webfuse/core/lws_log.h"
#define WFP_PROTOCOL ("fs")
#define WFP_DISABLE_LWS_LOG 0
#define WFP_CLIENT_PROTOCOL_COUNT 2
struct wfp_client
@ -29,7 +29,7 @@ struct wfp_client
struct wfp_client * wfp_impl_client_create(
struct wfp_client_config * config)
{
lws_set_log_level(WFP_DISABLE_LWS_LOG, NULL);
wf_lwslog_disable();
struct wfp_client * client = malloc(sizeof(struct wfp_client));
if (NULL != client)
@ -99,6 +99,12 @@ void wfp_impl_client_disconnect(
// ToDo: implement me
}
bool wfp_impl_client_is_connected(
struct wfp_client * client)
{
return client->protocol.is_connected;
}
void wfp_impl_client_service(
struct wfp_client * client,
int timeout_ms)

View File

@ -1,6 +1,10 @@
#ifndef WF_PROVIDER_IMPL_CLIENT_H
#define WF_PROVIDER_IMPL_CLIENT_H
#ifndef __cplusplus
#include <stdbool.h>
#endif
#ifdef __cplusplus
extern "C"
{
@ -27,13 +31,12 @@ extern void wfp_impl_client_connect(
extern void wfp_impl_client_disconnect(
struct wfp_client * client);
extern void wfp_impl_client_settimeout(
struct wfp_client * client,
unsigned int timepoint);
extern void wfp_impl_client_dispose(
struct wfp_client * client);
extern bool wfp_impl_client_is_connected(
struct wfp_client * client);
extern void wfp_impl_client_service(
struct wfp_client * client,
int timeout_ms);

View File

@ -35,6 +35,15 @@ static void wfp_impl_client_protocol_process_request(
json_t * request = json_loadb(message, length, 0, NULL);
if (NULL != request)
{
// FIXME: is_connected should be invoked, when filesystem added
if ((!protocol->is_connected) && (NULL != json_object_get(request, "result")))
{
protocol->is_connected = true;
protocol->provider.connected(protocol->user_data);
}
struct wfp_impl_invokation_context context =
{
.provider = &protocol->provider,
@ -84,13 +93,15 @@ static int wfp_impl_client_protocol_callback(
{
case LWS_CALLBACK_CLIENT_ESTABLISHED:
wfp_impl_client_protocol_add_filesystem(protocol);
protocol->provider.connected(protocol->user_data);
// Defer is_connected until response received
break;
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
protocol->is_connected = false;
protocol->provider.disconnected(protocol->user_data);
break;
case LWS_CALLBACK_CLIENT_CLOSED:
protocol->provider.connected(protocol->user_data);
protocol->is_connected = false;
protocol->provider.disconnected(protocol->user_data);
break;
case LWS_CALLBACK_CLIENT_RECEIVE:
wfp_impl_client_protocol_process_request(protocol, in, len);
@ -126,6 +137,7 @@ void wfp_impl_client_protocol_init(
struct wfp_provider const * provider,
void * user_data)
{
protocol->is_connected = false;
wf_slist_init(&protocol->messages);
protocol->wsi = NULL;

View File

@ -16,6 +16,7 @@ struct lws_protocols;
struct wfp_client_protocol
{
bool is_connected;
struct wfp_request request;
struct wfp_provider provider;
void * user_data;

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));