1
0
mirror of https://github.com/falk-werner/webfuse-provider synced 2024-10-27 20:44:10 +00:00

Merge pull request #50 from falk-werner/organize_tests

Organize Tests
This commit is contained in:
Falk Werner 2020-02-24 19:38:02 +01:00 committed by GitHub
commit 3e563f00d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
74 changed files with 739 additions and 675 deletions

View File

@ -7,47 +7,48 @@ include(GoogleTest)
pkg_check_modules(GMOCK gmock)
add_executable(alltests
test/tempdir.cc
test/file_utils.cc
test/msleep.cc
test/die_if.cc
test/timeout_watcher.cc
test/fake_adapter_server.cc
test/mock_authenticator.cc
test/mock_request.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_base64.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_server_config.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_mountpoint.cc
test/adapter/test_uuid_mountpoint.cc
test/adapter/test_uuid_mountpoint_factory.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/provider/test_client_protocol.cc
test/integration/test_integration.cc
test/integration/server.cc
test/integration/provider.cc
test/webfuse/utils/tempdir.cc
test/webfuse/utils/file_utils.cc
test/webfuse/utils/msleep.cc
test/webfuse/utils/die_if.cc
test/webfuse/utils/timeout_watcher.cc
test/webfuse/utils/path.c
test/webfuse/utils/static_filesystem.c
test/webfuse/utils/ws_server.cc
test/webfuse/mocks/mock_authenticator.cc
test/webfuse/mocks/mock_request.cc
test/webfuse/mocks/mock_provider_client.cc
test/webfuse/tests/core/test_container_of.cc
test/webfuse/tests/core/test_string.cc
test/webfuse/tests/core/test_slist.cc
test/webfuse/tests/core/test_base64.cc
test/webfuse/tests/core/test_status.cc
test/webfuse/tests/core/test_message.cc
test/webfuse/tests/core/test_message_queue.cc
test/webfuse/tests/adapter/test_response_parser.cc
test/webfuse/tests/adapter/test_server.cc
test/webfuse/tests/adapter/test_server_config.cc
test/webfuse/tests/adapter/test_timepoint.cc
test/webfuse/tests/adapter/test_timer.cc
test/webfuse/tests/adapter/test_credentials.cc
test/webfuse/tests/adapter/test_authenticator.cc
test/webfuse/tests/adapter/test_authenticators.cc
test/webfuse/tests/adapter/test_mountpoint.cc
test/webfuse/tests/adapter/test_uuid_mountpoint.cc
test/webfuse/tests/adapter/test_uuid_mountpoint_factory.cc
test/webfuse/tests/adapter/test_fuse_req.cc
test/webfuse/tests/adapter/jsonrpc/test_util.cc
test/webfuse/tests/adapter/jsonrpc/test_is_request.cc
test/webfuse/tests/adapter/jsonrpc/test_request.cc
test/webfuse/tests/adapter/jsonrpc/test_is_response.cc
test/webfuse/tests/adapter/jsonrpc/test_response.cc
test/webfuse/tests/adapter/jsonrpc/test_server.cc
test/webfuse/tests/adapter/jsonrpc/test_proxy.cc
test/webfuse/tests/provider/test_url.cc
test/webfuse/tests/provider/test_client_protocol.cc
test/webfuse/tests/integration/test_integration.cc
test/webfuse/tests/integration/server.cc
test/webfuse/tests/integration/provider.cc
)
target_include_directories(alltests PRIVATE

View File

@ -6,7 +6,6 @@ add_library(webfuse-core STATIC
lib/webfuse/core/message_queue.c
lib/webfuse/core/status.c
lib/webfuse/core/string.c
lib/webfuse/core/path.c
lib/webfuse/core/base64.c
lib/webfuse/core/lws_log.c
)

View File

@ -15,7 +15,6 @@ add_library(webfuse-provider-static STATIC
lib/webfuse/provider/impl/operation/open.c
lib/webfuse/provider/impl/operation/close.c
lib/webfuse/provider/impl/operation/read.c
lib/webfuse/provider/impl/static_filesystem.c
)
set_target_properties(webfuse-provider-static PROPERTIES OUTPUT_NAME webfuse-provider)

View File

@ -227,7 +227,7 @@ Read from an open file.
Adds a filesystem.
fs provider: {"method": "add_filesytem", "params": [<name>], "id": <id>}
fs provider: {"method": "add_filesystem", "params": [<name>], "id": <id>}
webfuse daemon: {"result": {"id": <name>}, "id": <id>}
| Item | Data type | Description |

View File

@ -0,0 +1,21 @@
////////////////////////////////////////////////////////////////////////////////
/// \file protocol_names.h
/// \brief Names of websocket protocol.
////////////////////////////////////////////////////////////////////////////////
#ifndef WF_PROTOCOL_NAMES_H
#define WF_PROTOCOL_NAMES_H
//------------------------------------------------------------------------------
/// \def WF_PROTOCOL_NAME_ADAPTER_SERVER
/// \brief Name of the websocket protocol an adapter server is running.
//------------------------------------------------------------------------------
#define WF_PROTOCOL_NAME_ADAPTER_SERVER ("webfuse-adapter-server")
//------------------------------------------------------------------------------
/// \def WF_PROTOCOL_NAME_PROVIDER_CLIENT
/// \brief Name of the websocket protocol an provider client is running.
//------------------------------------------------------------------------------
#define WF_PROTOCOL_NAME_PROVIDER_CLIENT ("webfuse-provider-client")
#endif

View File

@ -1,114 +0,0 @@
////////////////////////////////////////////////////////////////////////////////
/// \file provider/static_filesystem.h
/// \brief Reference implementation of static filesystem.
///
/// This header is used by integration tests. It may be removed from the
/// library.
///
/// \todo Remove this header from library
////////////////////////////////////////////////////////////////////////////////
#ifndef WFP_STATIC_FILESYSTEM_H
#define WFP_STATIC_FILESYSTEM_H
#ifndef __cplusplus
#include <stddef.h>
#else
#include <cstddef>
using ::std::size_t;
#endif
#include <webfuse/provider/api.h>
#ifdef __cplusplus
extern "C"
{
#endif
struct wfp_client_config;
//------------------------------------------------------------------------------
/// \deprecated This will be removed. Dont use it.
//------------------------------------------------------------------------------
struct wfp_static_filesystem;
//------------------------------------------------------------------------------
/// \deprecated This will be removed. Dont use it.
//------------------------------------------------------------------------------
typedef size_t
wfp_static_filesystem_read_fn(
size_t offset,
char * buffer,
size_t buffer_size,
void * user_data);
//------------------------------------------------------------------------------
/// \deprecated This will be removed. Dont use it.
//------------------------------------------------------------------------------
typedef void
wfp_static_filesystem_get_info_fn(
void * user_data,
int * result_mode,
size_t * result_size);
//------------------------------------------------------------------------------
/// \deprecated This will be removed. Dont use it.
//------------------------------------------------------------------------------
extern WFP_API struct wfp_static_filesystem *
wfp_static_filesystem_create(
struct wfp_client_config * config);
//------------------------------------------------------------------------------
/// \deprecated This will be removed. Dont use it.
//------------------------------------------------------------------------------
extern WFP_API void
wfp_static_filesystem_dispose(
struct wfp_static_filesystem * filesystem);
//------------------------------------------------------------------------------
/// \deprecated This will be removed. Dont use it.
//------------------------------------------------------------------------------
extern WFP_API void
wfp_static_filesystem_add(
struct wfp_static_filesystem * filesystem,
char const * path,
int mode,
char const * content,
size_t length);
//------------------------------------------------------------------------------
/// \deprecated This will be removed. Dont use it.
//------------------------------------------------------------------------------
extern WFP_API void
wfp_static_filesystem_add_text(
struct wfp_static_filesystem * filesystem,
char const * path,
int mode,
char const * content);
//------------------------------------------------------------------------------
/// \deprecated This will be removed. Dont use it.
//------------------------------------------------------------------------------
extern WFP_API void
wfp_static_filesystem_add_file(
struct wfp_static_filesystem * filesystem,
char const * path,
char const * filename);
//------------------------------------------------------------------------------
/// \deprecated This will be removed. Dont use it.
//------------------------------------------------------------------------------
extern WFP_API void
wfp_static_filesystem_add_generic(
struct wfp_static_filesystem * filesystem,
char const * path,
wfp_static_filesystem_read_fn * read,
wfp_static_filesystem_get_info_fn * get_info,
void * user_data);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -7,6 +7,7 @@
#define WF_ADAPTER_H
#include <webfuse/core/status.h>
#include <webfuse/core/protocol_names.h>
#include <webfuse/adapter/api.h>
#include <webfuse/adapter/server.h>

View File

@ -7,6 +7,7 @@
#define WF_PROVIDER_H
#include <webfuse/core/status.h>
#include <webfuse/core/protocol_names.h>
#include <webfuse/provider/api.h>
#include <webfuse/provider/client.h>
@ -22,6 +23,4 @@
#include <webfuse/provider/operation/close.h>
#include <webfuse/provider/operation/read.h>
#include <webfuse/provider/static_filesystem.h>
#endif

View File

@ -38,7 +38,6 @@ static struct lws_context * wf_impl_server_context_create(
memset(server->ws_protocols, 0, sizeof(struct lws_protocols) * WF_SERVER_PROTOCOL_COUNT);
server->ws_protocols[0].name = "http";
server->ws_protocols[0].callback = lws_callback_http_dummy;
server->ws_protocols[1].name = "fs";
wf_impl_server_protocol_init_lws(&server->protocol, &server->ws_protocols[1]);
memset(&server->mount, 0, sizeof(struct lws_http_mount));

View File

@ -6,6 +6,7 @@
#include "webfuse/core/message.h"
#include "webfuse/core/util.h"
#include "webfuse/core/protocol_names.h"
#include "webfuse/adapter/impl/credentials.h"
#include "webfuse/adapter/impl/jsonrpc/request.h"
@ -121,6 +122,7 @@ void wf_impl_server_protocol_init_lws(
struct wf_server_protocol * protocol,
struct lws_protocols * lws_protocol)
{
lws_protocol->name = WF_PROTOCOL_NAME_ADAPTER_SERVER;
lws_protocol->callback = &wf_impl_server_protocol_callback;
lws_protocol->per_session_data_size = 0;
lws_protocol->user = protocol;

View File

@ -11,7 +11,6 @@
#include "webfuse/provider/impl/client_config.h"
#include "webfuse/provider/impl/client.h"
#include "webfuse/provider/impl/dirbuffer.h"
#include "webfuse/provider/impl/static_filesystem.h"
// respond
@ -241,60 +240,3 @@ void wfp_dirbuffer_add(
{
wfp_impl_dirbuffer_add(buffer, name, inode);
}
// static_filesystem
struct wfp_static_filesystem *
wfp_static_filesystem_create(
struct wfp_client_config * config)
{
return wfp_impl_static_filesystem_create(config);
}
void
wfp_static_filesystem_dispose(
struct wfp_static_filesystem * filesystem)
{
wfp_impl_static_filesystem_dispose(filesystem);
}
void
wfp_static_filesystem_add(
struct wfp_static_filesystem * filesystem,
char const * path,
int mode,
char const * content,
size_t length)
{
wfp_impl_static_filesystem_add(filesystem, path, mode, content, length);
}
void
wfp_static_filesystem_add_text(
struct wfp_static_filesystem * filesystem,
char const * path,
int mode,
char const * content)
{
wfp_impl_static_filesystem_add_text(filesystem, path, mode, content);
}
void
wfp_static_filesystem_add_file(
struct wfp_static_filesystem * filesystem,
char const * path,
char const * filename)
{
wfp_impl_static_filesystem_add_file(filesystem, path, filename);
}
void
wfp_static_filesystem_add_generic(
struct wfp_static_filesystem * filesystem,
char const * path,
wfp_static_filesystem_read_fn * read,
wfp_static_filesystem_get_info_fn * get_info,
void * user_data)
{
wfp_impl_static_filesystem_add_generic(filesystem, path, read, get_info, user_data);
}

View File

@ -35,7 +35,6 @@ struct wfp_client * wfp_impl_client_create(
wfp_impl_client_protocol_init(&client->protocol, &config->provider, config->user_data);
memset(client->protocols, 0, sizeof(struct lws_protocols) * WFP_CLIENT_PROTOCOL_COUNT);
client->protocols[0].name = WFP_CLIENT_PROTOCOL_NAME;
wfp_impl_client_protocol_init_lws(&client->protocol, &client->protocols[0]);
memset(&client->info, 0, sizeof(struct lws_context_creation_info));

View File

@ -6,7 +6,6 @@
#include <libwebsockets.h>
#include <jansson.h>
#include "webfuse/provider/impl/client_config.h"
#include "webfuse/provider/impl/provider.h"
#include "webfuse/core/util.h"
@ -14,6 +13,7 @@
#include "webfuse/core/message_queue.h"
#include "webfuse/core/container_of.h"
#include "webfuse/provider/impl/url.h"
#include "webfuse/core/protocol_names.h"
static void wfp_impl_client_protocol_respond(
json_t * response,
@ -112,7 +112,7 @@ static int wfp_impl_client_protocol_callback(
// fall-through
case LWS_CALLBACK_CLIENT_WRITEABLE:
if ((wsi == protocol->wsi) && (!wf_slist_empty(&protocol->messages)))
{
{
struct wf_slist_item * item = wf_slist_remove_first(&protocol->messages);
struct wf_message * message = wf_container_of(item, struct wf_message, item);
lws_write(wsi, (unsigned char*) message->data, message->length, LWS_WRITE_TEXT);
@ -180,6 +180,7 @@ void wfp_impl_client_protocol_init_lws(
struct wfp_client_protocol * protocol,
struct lws_protocols * lws_protocol)
{
lws_protocol->name = WF_PROTOCOL_NAME_PROVIDER_CLIENT;
lws_protocol->callback = &wfp_impl_client_protocol_callback;
lws_protocol->per_session_data_size = 0;
lws_protocol->user = protocol;
@ -203,7 +204,8 @@ void wfp_impl_client_protocol_connect(
info.host = info.address;
info.origin = info.address;
info.ssl_connection = (url_data.use_tls) ? LCCSCF_USE_SSL : 0;
info.protocol = WFP_CLIENT_PROTOCOL_NAME;
info.protocol = WF_PROTOCOL_NAME_ADAPTER_SERVER;
info.local_protocol_name = WF_PROTOCOL_NAME_PROVIDER_CLIENT;
info.pwsi = &protocol->wsi;
lws_client_connect_via_info(&info);

View File

@ -11,8 +11,6 @@ extern "C"
{
#endif
#define WFP_CLIENT_PROTOCOL_NAME ("fs")
struct wfp_client_config;
struct lws_protocols;
struct lws_context;

View File

@ -1,52 +0,0 @@
#ifndef WFP_IMPL_STATIC_FILESYSTEM_H
#define WFP_IMPL_STATIC_FILESYSTEM_H
#include "webfuse/provider/static_filesystem.h"
#ifdef __cplusplus
extern "C"
{
#endif
extern struct wfp_static_filesystem *
wfp_impl_static_filesystem_create(
struct wfp_client_config * config);
extern void
wfp_impl_static_filesystem_dispose(
struct wfp_static_filesystem * filesystem);
extern void
wfp_impl_static_filesystem_add(
struct wfp_static_filesystem * filesystem,
char const * path,
int mode,
char const * content,
size_t length);
extern void
wfp_impl_static_filesystem_add_text(
struct wfp_static_filesystem * filesystem,
char const * path,
int mode,
char const * content);
extern void
wfp_impl_static_filesystem_add_file(
struct wfp_static_filesystem * filesystem,
char const * path,
char const * filename);
extern void
wfp_impl_static_filesystem_add_generic(
struct wfp_static_filesystem * filesystem,
char const * path,
wfp_static_filesystem_read_fn * read,
wfp_static_filesystem_get_info_fn * get_info,
void * user_data);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -1,58 +0,0 @@
#include <gtest/gtest.h>
#include "webfuse/core/path.h"
TEST(wf_path, empty)
{
struct wf_path * path = wf_path_create("");
ASSERT_EQ(0, wf_path_element_count(path));
ASSERT_EQ(nullptr, wf_path_get_element(path, 0));
wf_path_dispose(path);
}
TEST(wf_path, relative_file)
{
struct wf_path * path = wf_path_create("some.file");
ASSERT_EQ(1, wf_path_element_count(path));
ASSERT_STREQ("some.file", wf_path_get_element(path, 0));
wf_path_dispose(path);
}
TEST(wf_path, absolute_file)
{
struct wf_path * path = wf_path_create("/absolute.file");
ASSERT_EQ(1, wf_path_element_count(path));
ASSERT_STREQ("absolute.file", wf_path_get_element(path, 0));
wf_path_dispose(path);
}
TEST(wf_path, nested_path)
{
struct wf_path * path = wf_path_create("/a/nested/path");
ASSERT_EQ(3, wf_path_element_count(path));
ASSERT_STREQ("a", wf_path_get_element(path, 0));
ASSERT_STREQ("nested", wf_path_get_element(path, 1));
ASSERT_STREQ("path", wf_path_get_element(path, 2));
wf_path_dispose(path);
}
TEST(wf_path, deep_nested_path)
{
struct wf_path * path = wf_path_create("/this/is/a/very/deep/nested/path/to/some/file");
ASSERT_EQ(10, wf_path_element_count(path));
ASSERT_STREQ("this", wf_path_get_element(path, 0));
ASSERT_STREQ("is", wf_path_get_element(path, 1));
ASSERT_STREQ("a", wf_path_get_element(path, 2));
ASSERT_STREQ("very", wf_path_get_element(path, 3));
ASSERT_STREQ("deep", wf_path_get_element(path, 4));
ASSERT_STREQ("nested", wf_path_get_element(path, 5));
ASSERT_STREQ("path", wf_path_get_element(path, 6));
ASSERT_STREQ("to", wf_path_get_element(path, 7));
ASSERT_STREQ("some", wf_path_get_element(path, 8));
ASSERT_STREQ("file", wf_path_get_element(path, 9));
wf_path_dispose(path);
}

View File

@ -1,24 +0,0 @@
#ifndef WF_TEST_FAKE_SERVER_HPP
#define WF_TEST_FAKE_SERVER_HPP
#include <libwebsockets.h>
namespace webfuse_test
{
class FakeAdapterServer
{
FakeAdapterServer(FakeAdapterServer const &) = delete;
FakeAdapterServer & operator=(FakeAdapterServer const &) = delete;
public:
explicit FakeAdapterServer(int port);
~FakeAdapterServer();
void waitForConnection();
private:
class Private;
Private * d;
};
}
#endif

View File

@ -1,72 +0,0 @@
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <webfuse/provider/client_protocol.h>
#include <webfuse/provider/client_config.h>
#include "fake_adapter_server.hpp"
#include <cstring>
#include <thread>
#include <atomic>
using webfuse_test::FakeAdapterServer;
using testing::_;
namespace
{
struct Context
{
lws_context * context;
std::atomic<bool> isShutdownRequested;
};
void run(Context * context)
{
while (!context->isShutdownRequested)
{
lws_service(context->context, 100);
}
}
}
TEST(client_protocol, connect)
{
FakeAdapterServer server(54321);
wfp_client_config * config = wfp_client_config_create();
wfp_client_protocol * protocol = wfp_client_protocol_create(config);
struct lws_protocols protocols[2];
memset(protocols, 0, sizeof(struct lws_protocols) * 2);
protocols[0].name = "fs";
wfp_client_protocol_init_lws(protocol, &protocols[0]);
struct lws_context_creation_info info;
memset(&info, 0, sizeof(struct lws_context_creation_info));
info.port = CONTEXT_PORT_NO_LISTEN;
info.protocols = protocols;
info.uid = -1;
info.gid = -1;
struct lws_context * context = lws_create_context(&info);
wfp_client_protocol_connect(protocol, context, "ws://localhost:54321/");
Context ctx;
ctx.context = context;
ctx.isShutdownRequested = false;
std::thread client_thread(run, &ctx);
server.waitForConnection();
ctx.isShutdownRequested = true;
client_thread.join();
lws_context_destroy(context);
wfp_client_protocol_dispose(protocol);
wfp_client_config_dispose(config);
}

View File

@ -1,61 +0,0 @@
#include <gtest/gtest.h>
#include "webfuse/provider/impl/static_filesystem.h"
#include "webfuse/provider/client_config.h"
#include "webfuse/provider/impl/client_config.h"
#include "mock_request.hpp"
using webfuse_test::request_create;
using webfuse_test::MockRequest;
using webfuse_test::GetAttrMatcher;
using webfuse_test::ReaddirMatcher;
using testing::_;
TEST(wfp_static_filesystem, has_root_dir)
{
struct wfp_client_config * config = wfp_client_config_create();
struct wfp_static_filesystem * filesystem = wfp_impl_static_filesystem_create(config);
MockRequest mock;
struct wfp_request * request = request_create(&mock, 42);
EXPECT_CALL(mock, respond(GetAttrMatcher(1, 0555, "dir"), 42)).Times(1);
config->provider.getattr(request, 1, config->user_data);
wfp_impl_static_filesystem_dispose(filesystem);
wfp_client_config_dispose(config);
}
TEST(wfp_static_filesystem, contains_default_dirs)
{
struct wfp_client_config * config = wfp_client_config_create();
struct wfp_static_filesystem * filesystem = wfp_impl_static_filesystem_create(config);
MockRequest mock;
struct wfp_request * request = request_create(&mock, 23);
char const * default_dirs[] = {".", "..", nullptr};
EXPECT_CALL(mock, respond(ReaddirMatcher(default_dirs), 23)).Times(1);
config->provider.readdir(request, 1, config->user_data);
wfp_impl_static_filesystem_dispose(filesystem);
wfp_client_config_dispose(config);
}
TEST(wfp_static_filesystem, add_text)
{
struct wfp_client_config * config = wfp_client_config_create();
struct wfp_static_filesystem * filesystem = wfp_impl_static_filesystem_create(config);
wfp_impl_static_filesystem_add_text(filesystem, "text.file", 666, "some text");
MockRequest mock;
struct wfp_request * request = request_create(&mock, 23);
char const * contained_elements[] = {"text.file", nullptr};
EXPECT_CALL(mock, respond(ReaddirMatcher(contained_elements), 23)).Times(1);
config->provider.readdir(request, 1, config->user_data);
wfp_impl_static_filesystem_dispose(filesystem);
wfp_client_config_dispose(config);
}

View File

@ -1,4 +1,4 @@
#include "mock_authenticator.hpp"
#include "webfuse/mocks/mock_authenticator.hpp"
#define WF_AUTHENTICATOR_COUNT 3

View File

@ -0,0 +1,204 @@
#include "webfuse/mocks/mock_provider_client.hpp"
#include "webfuse/provider/operation/error.h"
#include "webfuse/provider/dirbuffer.h"
extern "C"
{
using webfuse_test::IProviderClient;
using webfuse_test::ProviderClientException;
static void webfuse_test_iproviderclient_onconnected(
void * user_data)
{
auto * self = reinterpret_cast<IProviderClient*>(user_data);
self->OnConnected();
}
static void webfuse_test_iproviderclient_ondisconnected(
void * user_data)
{
auto * self = reinterpret_cast<IProviderClient*>(user_data);
self->OnDisconnected();
}
static void webfuse_test_iproviderclient_ontimer(
void * user_data)
{
auto * self = reinterpret_cast<IProviderClient*>(user_data);
self->OnTimer();
}
static void webfuse_test_iproviderclient_onlookup(
struct wfp_request * request,
ino_t parent,
char const * name,
void * user_data)
{
auto * self = reinterpret_cast<IProviderClient*>(user_data);
try
{
struct stat buffer;
self->Lookup(parent, name, &buffer);
wfp_respond_lookup(request, &buffer);
}
catch (ProviderClientException& ex)
{
wfp_respond_error(request, ex.GetErrorCode());
}
catch (...)
{
wfp_respond_error(request, WF_BAD);
}
}
static void webfuse_test_iproviderclient_ongetattr(
struct wfp_request * request,
ino_t inode,
void * user_data)
{
auto * self = reinterpret_cast<IProviderClient*>(user_data);
try
{
struct stat buffer;
memset(&buffer, 0, sizeof(struct stat));
self->GetAttr(inode, &buffer);
wfp_respond_getattr(request,&buffer);
}
catch (ProviderClientException& ex)
{
wfp_respond_error(request, ex.GetErrorCode());
}
catch (...)
{
wfp_respond_error(request, WF_BAD);
}
}
static void webfuse_test_iproviderclient_onreaddir(
struct wfp_request * request,
ino_t directory,
void * user_data)
{
auto * self = reinterpret_cast<IProviderClient*>(user_data);
wfp_dirbuffer * buffer = wfp_dirbuffer_create();
try
{
self->ReadDir(directory, buffer);
wfp_respond_readdir(request, buffer);
}
catch (ProviderClientException& ex)
{
wfp_respond_error(request, ex.GetErrorCode());
}
catch (...)
{
wfp_respond_error(request, WF_BAD);
}
wfp_dirbuffer_dispose(buffer);
}
static void webfuse_test_iproviderclient_onopen(
struct wfp_request * request,
ino_t inode,
int flags,
void * user_data)
{
auto * self = reinterpret_cast<IProviderClient*>(user_data);
try
{
uint32_t handle = 0;
self->Open(inode, flags, &handle);
wfp_respond_open(request, handle);
}
catch (ProviderClientException& ex)
{
wfp_respond_error(request, ex.GetErrorCode());
}
catch (...)
{
wfp_respond_error(request, WF_BAD);
}
}
static void webfuse_test_iproviderclient_onclose(
ino_t inode,
uint32_t handle,
int flags,
void * user_data)
{
auto * self = reinterpret_cast<IProviderClient*>(user_data);
self->Close(inode, handle, flags);
}
static void webfuse_test_iproviderclient_onread(
struct wfp_request * request,
ino_t inode,
uint32_t handle,
size_t offset,
size_t length,
void * user_data)
{
auto * self = reinterpret_cast<IProviderClient*>(user_data);
char * data = new char[length];
try
{
size_t bytes_read = 0;
self->Read(inode, handle, offset, length, data, &bytes_read);
wfp_respond_read(request, data, bytes_read);
}
catch (ProviderClientException& ex)
{
wfp_respond_error(request, ex.GetErrorCode());
}
catch (...)
{
wfp_respond_error(request, WF_BAD);
}
delete[] data;
}
}
namespace webfuse_test
{
ProviderClientException::ProviderClientException(wf_status error_code)
: runtime_error("ProviderClientException")
, error_code_(error_code)
{
}
wf_status ProviderClientException::GetErrorCode()
{
return error_code_;
}
void IProviderClient::AttachTo(wfp_client_config * config)
{
void * self = reinterpret_cast<void *>(this);
wfp_client_config_set_userdata(config, self);
wfp_client_config_set_onconnected(config, &webfuse_test_iproviderclient_onconnected);
wfp_client_config_set_ondisconnected(config, &webfuse_test_iproviderclient_ondisconnected);
wfp_client_config_set_ontimer(config, &webfuse_test_iproviderclient_ontimer);
wfp_client_config_set_onlookup(config, &webfuse_test_iproviderclient_onlookup);
wfp_client_config_set_ongetattr(config, &webfuse_test_iproviderclient_ongetattr);
wfp_client_config_set_onreaddir(config, &webfuse_test_iproviderclient_onreaddir);
wfp_client_config_set_onopen(config, &webfuse_test_iproviderclient_onopen);
wfp_client_config_set_onclose(config, &webfuse_test_iproviderclient_onclose);
wfp_client_config_set_onread(config, &webfuse_test_iproviderclient_onread);
}
}

View File

@ -0,0 +1,55 @@
#ifndef WF_MOCK_PROVIDER_CLIENT_HPP
#define WF_MOCK_PROVIDER_CLIENT_HPP
#include <gmock/gmock.h>
#include "webfuse/provider/client_config.h"
#include "webfuse/core/status.h"
#include <stdexcept>
namespace webfuse_test
{
class ProviderClientException: public std::runtime_error
{
public:
explicit ProviderClientException(wf_status error_code);
wf_status GetErrorCode();
private:
wf_status error_code_;
};
class IProviderClient
{
public:
virtual ~IProviderClient() = default;
virtual void OnConnected() = 0;
virtual void OnDisconnected() = 0;
virtual void OnTimer() = 0;
virtual void Lookup(ino_t parent, char const * name, struct stat * result) = 0;
virtual void GetAttr(ino_t inode, struct stat * buffer) = 0;
virtual void ReadDir(ino_t directory, wfp_dirbuffer * buffer) = 0;
virtual void Open(ino_t inode, int flags, uint32_t * handle) = 0;
virtual void Close(ino_t inode, uint32_t handle, int flags) = 0;
virtual void Read(ino_t inode, uint32_t handle, size_t offset, size_t length, char * buffer, size_t * bytes_read) = 0;
void AttachTo(wfp_client_config * config);
};
class MockProviderClient: public IProviderClient
{
public:
~MockProviderClient() override = default;
MOCK_METHOD0( OnConnected, void());
MOCK_METHOD0( OnDisconnected, void());
MOCK_METHOD0( OnTimer, void());
MOCK_METHOD3( Lookup, void(ino_t parent, char const * name, struct stat * result));
MOCK_METHOD2( GetAttr, void(ino_t inode, struct stat * buffer));
MOCK_METHOD2( ReadDir, void(ino_t directory, wfp_dirbuffer * buffer));
MOCK_METHOD3( Open, void(ino_t inode, int flags, uint32_t * handle));
MOCK_METHOD3( Close, void(ino_t inode, uint32_t handle, int flags));
MOCK_METHOD6( Read, void(ino_t inode, uint32_t handle, size_t offset, size_t length, char * buffer, size_t * bytes_read));
};
}
#endif

View File

@ -1,4 +1,4 @@
#include "mock_request.hpp"
#include "webfuse/mocks/mock_request.hpp"
#include <cstdlib>
namespace

View File

@ -1,7 +1,7 @@
#include <gtest/gtest.h>
#include "webfuse/adapter/impl/jsonrpc/proxy.h"
#include "webfuse/adapter/impl/time/timeout_manager.h"
#include "msleep.hpp"
#include "webfuse/utils/msleep.hpp"
using webfuse_test::msleep;

View File

@ -1,7 +1,7 @@
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "mock_authenticator.hpp"
#include "webfuse/mocks/mock_authenticator.hpp"
#include "webfuse/adapter/impl/authenticator.h"
#include "webfuse/adapter/impl/credentials.h"

View File

@ -3,7 +3,7 @@
#include "webfuse/adapter/impl/authenticators.h"
#include "webfuse/adapter/impl/credentials.h"
#include "mock_authenticator.hpp"
#include "webfuse/mocks/mock_authenticator.hpp"
using ::testing::_;
using ::testing::Return;

View File

@ -2,7 +2,7 @@
#include "webfuse/adapter/server_config.h"
#include "webfuse/adapter/impl/server_config.h"
#include "webfuse/adapter/impl/authenticator.h"
#include "tempdir.hpp"
#include "webfuse/utils/tempdir.hpp"
using webfuse_test::TempDir;

View File

@ -1,6 +1,6 @@
#include <gtest/gtest.h>
#include "msleep.hpp"
#include "webfuse/utils/msleep.hpp"
#include "webfuse/adapter/impl/time/timepoint.h"
using webfuse_test::msleep;

View File

@ -2,7 +2,7 @@
#include <cstddef>
#include "msleep.hpp"
#include "webfuse/utils/msleep.hpp"
#include "webfuse/adapter/impl/time/timer.h"
#include "webfuse/adapter/impl/time/timeout_manager.h"

View File

@ -1,7 +1,7 @@
#include <gtest/gtest.h>
#include "tempdir.hpp"
#include "file_utils.hpp"
#include "webfuse/utils/tempdir.hpp"
#include "webfuse/utils/file_utils.hpp"
#include "webfuse_adapter.h"
#include "webfuse/adapter/impl/uuid_mountpoint.h"

View File

@ -1,8 +1,8 @@
#include <gtest/gtest.h>
#include "webfuse_adapter.h"
#include "webfuse/adapter/impl/uuid_mountpoint_factory.h"
#include "tempdir.hpp"
#include "file_utils.hpp"
#include "webfuse/utils/tempdir.hpp"
#include "webfuse/utils/file_utils.hpp"
using webfuse_test::TempDir;
using webfuse_test::is_dir;

View File

@ -1,10 +1,11 @@
#include "integration/provider.hpp"
#include "webfuse/tests/integration/provider.hpp"
#include "webfuse_provider.h"
#include "webfuse/provider/impl/client.h"
#include <thread>
#include <mutex>
#include <string>
#include "msleep.hpp"
#include "webfuse/utils/msleep.hpp"
#include "webfuse/utils/static_filesystem.h"
namespace webfuse_test
{

View File

@ -1,4 +1,4 @@
#include "integration/server.hpp"
#include "webfuse/tests/integration/server.hpp"
#include <thread>
#include <mutex>
#include <cstdlib>
@ -6,7 +6,7 @@
#include <unistd.h>
#include "webfuse_adapter.h"
#include "webfuse/adapter/impl/server.h"
#include "msleep.hpp"
#include "webfuse/utils/msleep.hpp"
#define WF_PATH_MAX (100)

View File

@ -1,6 +1,6 @@
#include <gtest/gtest.h>
#include "integration/server.hpp"
#include "integration/provider.hpp"
#include "webfuse/tests/integration/server.hpp"
#include "webfuse/tests/integration/provider.hpp"
#include <cstdio>
#include <csignal>
@ -15,7 +15,7 @@
#include <jansson.h>
#include "webfuse/core/lws_log.h"
#include "die_if.hpp"
#include "webfuse/utils/die_if.hpp"
using webfuse_test::Server;
using webfuse_test::Provider;

View File

@ -0,0 +1,152 @@
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <webfuse/provider/client_protocol.h>
#include <webfuse/provider/client_config.h>
#include "webfuse/utils/ws_server.hpp"
#include "webfuse/mocks/mock_provider_client.hpp"
#include <cstring>
#include <thread>
#include <atomic>
using webfuse_test::WebsocketServer;
using webfuse_test::MockProviderClient;
using webfuse_test::IProviderClient;
using testing::_;
using testing::AtMost;
namespace
{
class ClientProtocolFixture
{
ClientProtocolFixture(ClientProtocolFixture const &) = delete;
ClientProtocolFixture& operator=(ClientProtocolFixture const &) = delete;
public:
explicit ClientProtocolFixture(IProviderClient& client)
{
config = wfp_client_config_create();
client.AttachTo(config);
protocol = wfp_client_protocol_create(config);
struct lws_protocols client_protocol;
memset(&client_protocol, 0, sizeof(struct lws_protocols));
wfp_client_protocol_init_lws(protocol, &client_protocol);
server = new WebsocketServer(54321, &client_protocol, 1);
}
~ClientProtocolFixture()
{
delete server;
wfp_client_protocol_dispose(protocol);
wfp_client_config_dispose(config);
}
void Connect()
{
wfp_client_protocol_connect(protocol, server->getContext(), "ws://localhost:54321/");
server->waitForConnection();
}
void SendToClient(json_t * request)
{
server->sendMessage(request);
}
json_t * ReceiveMessageFromClient()
{
return server->receiveMessage();
}
void AwaitAddFilesystem(std::string& filesystemName)
{
json_t * addFilesystemRequest = server->receiveMessage();
ASSERT_NE(nullptr, addFilesystemRequest);
ASSERT_TRUE(json_is_object(addFilesystemRequest));
json_t * method = json_object_get(addFilesystemRequest, "method");
ASSERT_TRUE(json_is_string(method));
ASSERT_STREQ("add_filesystem", json_string_value(method));
json_t * params = json_object_get(addFilesystemRequest, "params");
ASSERT_TRUE(json_is_array(params));
ASSERT_EQ(1, json_array_size(params));
json_t * filesystem = json_array_get(params, 0);
ASSERT_TRUE(json_is_string(filesystem));
filesystemName = json_string_value(filesystem);
json_t * id = json_object_get(addFilesystemRequest, "id");
ASSERT_TRUE(json_is_integer(id));
json_t * response = json_object();
json_t * result = json_object();
json_object_set(result, "id", filesystem);
json_object_set_new(response, "result", result);
json_object_set(response, "id", id);
server->sendMessage(response);
json_decref(addFilesystemRequest);
}
private:
WebsocketServer * server;
wfp_client_config * config;
wfp_client_protocol * protocol;
};
}
TEST(client_protocol, connect)
{
MockProviderClient provider;
ClientProtocolFixture fixture(provider);
EXPECT_CALL(provider, OnConnected()).Times(AtMost(1));
EXPECT_CALL(provider, OnDisconnected()).Times(1);
fixture.Connect();
if (HasFatalFailure()) { return; }
std::string filesystem;
fixture.AwaitAddFilesystem(filesystem);
if (HasFatalFailure()) { return; }
}
TEST(client_protocol, getattr)
{
MockProviderClient provider;
ClientProtocolFixture fixture(provider);
EXPECT_CALL(provider, OnConnected()).Times(1);
EXPECT_CALL(provider, OnDisconnected()).Times(1);
EXPECT_CALL(provider, GetAttr(1, _)).Times(1);
fixture.Connect();
if (HasFatalFailure()) { return; }
std::string filesystem;
fixture.AwaitAddFilesystem(filesystem);
if (HasFatalFailure()) { return; }
json_t * params = json_array();
json_array_append_new(params, json_string(filesystem.c_str()));
json_array_append_new(params, json_integer(1));
json_t * request = json_object();
json_object_set_new(request, "method", json_string("getattr"));
json_object_set_new(request, "params", params);
json_object_set_new(request, "id", json_integer(42));
fixture.SendToClient(request);
json_t * response = fixture.ReceiveMessageFromClient();
ASSERT_TRUE(json_is_object(response));
json_decref(response);
}

View File

@ -1,4 +1,4 @@
#include "die_if.hpp"
#include "webfuse/utils/die_if.hpp"
#include <cstdlib>
namespace webfuse_test

View File

@ -1,4 +1,4 @@
#include "file_utils.hpp"
#include "webfuse/utils/file_utils.hpp"
#include <sys/types.h>
#include <sys/stat.h>

View File

@ -1,4 +1,4 @@
#include "msleep.hpp"
#include "webfuse/utils/msleep.hpp"
#include <ctime>
namespace webfuse_test

View File

@ -1,4 +1,4 @@
#include "webfuse/core/path.h"
#include "webfuse/utils/path.h"
#include <stdlib.h>
#include <string.h>

View File

@ -1,9 +1,9 @@
#include "webfuse/provider/impl/static_filesystem.h"
#include "webfuse/utils/static_filesystem.h"
#include "webfuse/provider/client_config.h"
#include "webfuse/provider/dirbuffer.h"
#include "webfuse/provider/operation/error.h"
#include "webfuse/core/path.h"
#include "webfuse/utils/path.h"
#include "webfuse/core/util.h"
#include <sys/stat.h>
@ -41,7 +41,7 @@ struct wfp_static_filesystem
};
static struct wfp_static_filesystem_entry *
wfp_impl_static_filesystem_get_entry(
wfp_static_filesystem_get_entry(
struct wfp_static_filesystem * filesystem,
size_t inode)
{
@ -56,7 +56,7 @@ wfp_impl_static_filesystem_get_entry(
}
static struct wfp_static_filesystem_entry *
wfp_impl_static_filesystem_get_entry_by_name(
wfp_static_filesystem_get_entry_by_name(
struct wfp_static_filesystem * filesystem,
size_t parent,
char const * name)
@ -76,7 +76,7 @@ wfp_impl_static_filesystem_get_entry_by_name(
}
static struct wfp_static_filesystem_entry *
wfp_impl_static_filesystem_add_entry(
wfp_static_filesystem_add_entry(
struct wfp_static_filesystem * filesystem)
{
struct wfp_static_filesystem_entry * entry = NULL;
@ -107,7 +107,7 @@ wfp_impl_static_filesystem_add_entry(
}
static size_t
wfp_impl_static_filesystem_entry_read(
wfp_static_filesystem_entry_read(
size_t offset,
char * buffer,
size_t buffer_size,
@ -126,7 +126,7 @@ wfp_impl_static_filesystem_entry_read(
}
static void
wfp_impl_static_filesystem_entry_get_info(
wfp_static_filesystem_entry_get_info(
void * user_data,
int * result_mode,
size_t * result_size)
@ -137,62 +137,23 @@ wfp_impl_static_filesystem_entry_get_info(
}
static size_t
wfp_impl_static_filesystem_file_read(
size_t offset,
char * buffer,
size_t buffer_size,
void * user_data)
{
size_t result = 0;
struct wfp_static_filesystem_entry * entry = user_data;
char const * filename = entry->content;
FILE * file = fopen(filename, "rb");
if (NULL != file)
{
fseek(file, offset, SEEK_SET);
result = fread(buffer, buffer_size, 1, file);
fclose(file);
}
return result;
}
static void
wfp_impl_static_filesystem_file_get_info(
void * user_data,
int * result_mode,
size_t * result_size)
{
struct wfp_static_filesystem_entry * entry = user_data;
char const * filename = entry->content;
struct stat buffer;
stat(filename, &buffer);
*result_mode = (int) (buffer.st_mode & 0777);
*result_size = (size_t) buffer.st_size;
}
static size_t
wfp_impl_static_filesystem_add_dir(
wfp_static_filesystem_add_dir(
struct wfp_static_filesystem * filesystem,
size_t parent,
char const * name
)
{
struct wfp_static_filesystem_entry * entry = wfp_impl_static_filesystem_get_entry_by_name(filesystem, parent, name);
struct wfp_static_filesystem_entry * entry = wfp_static_filesystem_get_entry_by_name(filesystem, parent, name);
if (NULL == entry)
{
entry = wfp_impl_static_filesystem_add_entry(filesystem);
entry = wfp_static_filesystem_add_entry(filesystem);
entry->parent = parent;
entry->is_file = false;
entry->mode = 0555;
entry->name = strdup(name);
entry->user_data = entry;
entry->read = &wfp_impl_static_filesystem_entry_read;
entry->get_info = &wfp_impl_static_filesystem_entry_get_info;
entry->read = &wfp_static_filesystem_entry_read;
entry->get_info = &wfp_static_filesystem_entry_get_info;
entry->size = 0;
entry->content = NULL;
}
@ -201,7 +162,7 @@ wfp_impl_static_filesystem_add_dir(
}
static size_t
wfp_impl_static_filesystem_make_parent(
wfp_static_filesystem_make_parent(
struct wfp_static_filesystem * filesystem,
struct wf_path * path)
{
@ -213,7 +174,7 @@ wfp_impl_static_filesystem_make_parent(
for(size_t i = 0; i < (count - 1); i++)
{
char const * name = wf_path_get_element(path, i);
result = wfp_impl_static_filesystem_add_dir(filesystem, result, name);
result = wfp_static_filesystem_add_dir(filesystem, result, name);
}
}
@ -221,7 +182,7 @@ wfp_impl_static_filesystem_make_parent(
}
static void
wfp_impl_static_filesystem_stat(
wfp_static_filesystem_stat(
struct wfp_static_filesystem_entry * entry,
struct stat * stat
)
@ -238,19 +199,19 @@ wfp_impl_static_filesystem_stat(
stat->st_mode |= (entry->is_file) ? S_IFREG: S_IFDIR;
}
static void wfp_impl_static_filesystem_lookup(
static void wfp_static_filesystem_lookup(
struct wfp_request * request,
ino_t parent,
char const * name,
void * user_data)
{
struct wfp_static_filesystem * filesystem = user_data;
struct wfp_static_filesystem_entry * entry = wfp_impl_static_filesystem_get_entry_by_name(filesystem, parent, name);
struct wfp_static_filesystem_entry * entry = wfp_static_filesystem_get_entry_by_name(filesystem, parent, name);
if (NULL != entry)
{
struct stat stat;
wfp_impl_static_filesystem_stat(entry, &stat);
wfp_static_filesystem_stat(entry, &stat);
wfp_respond_lookup(request, &stat);
}
else
@ -260,18 +221,18 @@ static void wfp_impl_static_filesystem_lookup(
}
static void wfp_impl_static_filesystem_getattr(
static void wfp_static_filesystem_getattr(
struct wfp_request * request,
ino_t inode,
void * user_data)
{
struct wfp_static_filesystem * filesystem = user_data;
struct wfp_static_filesystem_entry * entry = wfp_impl_static_filesystem_get_entry(filesystem, inode);
struct wfp_static_filesystem_entry * entry = wfp_static_filesystem_get_entry(filesystem, inode);
if (NULL != entry)
{
struct stat stat;
wfp_impl_static_filesystem_stat(entry, &stat);
wfp_static_filesystem_stat(entry, &stat);
wfp_respond_getattr(request, &stat);
}
else
@ -280,13 +241,13 @@ static void wfp_impl_static_filesystem_getattr(
}
}
static void wfp_impl_static_filesystem_readdir(
static void wfp_static_filesystem_readdir(
struct wfp_request * request,
ino_t directory,
void * user_data)
{
struct wfp_static_filesystem * filesystem = user_data;
struct wfp_static_filesystem_entry * dir = wfp_impl_static_filesystem_get_entry(filesystem, directory);
struct wfp_static_filesystem_entry * dir = wfp_static_filesystem_get_entry(filesystem, directory);
if ((NULL != dir) && (!dir->is_file))
{
@ -312,14 +273,14 @@ static void wfp_impl_static_filesystem_readdir(
}
}
static void wfp_impl_static_filesystem_open(
static void wfp_static_filesystem_open(
struct wfp_request * request,
ino_t inode,
int flags,
void * user_data)
{
struct wfp_static_filesystem * filesystem = user_data;
struct wfp_static_filesystem_entry * entry = wfp_impl_static_filesystem_get_entry(filesystem, inode);
struct wfp_static_filesystem_entry * entry = wfp_static_filesystem_get_entry(filesystem, inode);
if ((NULL != entry) && (entry->is_file))
{
@ -338,7 +299,7 @@ static void wfp_impl_static_filesystem_open(
}
}
static void wfp_impl_static_filesystem_read(
static void wfp_static_filesystem_read(
struct wfp_request * request,
ino_t inode,
uint32_t WF_UNUSED_PARAM(handle),
@ -347,7 +308,7 @@ static void wfp_impl_static_filesystem_read(
void * user_data)
{
struct wfp_static_filesystem * filesystem = user_data;
struct wfp_static_filesystem_entry * entry = wfp_impl_static_filesystem_get_entry(filesystem, inode);
struct wfp_static_filesystem_entry * entry = wfp_static_filesystem_get_entry(filesystem, inode);
if ((NULL != entry) && (entry->is_file))
{
@ -365,7 +326,7 @@ static void wfp_impl_static_filesystem_read(
struct wfp_static_filesystem *
wfp_impl_static_filesystem_create(
wfp_static_filesystem_create(
struct wfp_client_config * config)
{
(void) config;
@ -377,21 +338,21 @@ wfp_impl_static_filesystem_create(
filesystem->size = 0;
filesystem->capacity = WFP_STATIC_FILESYSTEM_DEFAULT_CAPACITY;
wfp_impl_static_filesystem_add_dir(filesystem, 0, "<root>");
wfp_static_filesystem_add_dir(filesystem, 0, "<root>");
wfp_client_config_set_userdata(config, filesystem);
wfp_client_config_set_onlookup(config, &wfp_impl_static_filesystem_lookup);
wfp_client_config_set_ongetattr(config, &wfp_impl_static_filesystem_getattr);
wfp_client_config_set_onreaddir(config, &wfp_impl_static_filesystem_readdir);
wfp_client_config_set_onopen(config, &wfp_impl_static_filesystem_open);
wfp_client_config_set_onread(config, &wfp_impl_static_filesystem_read);
wfp_client_config_set_onlookup(config, &wfp_static_filesystem_lookup);
wfp_client_config_set_ongetattr(config, &wfp_static_filesystem_getattr);
wfp_client_config_set_onreaddir(config, &wfp_static_filesystem_readdir);
wfp_client_config_set_onopen(config, &wfp_static_filesystem_open);
wfp_client_config_set_onread(config, &wfp_static_filesystem_read);
}
return filesystem;
}
void
wfp_impl_static_filesystem_dispose(
wfp_static_filesystem_dispose(
struct wfp_static_filesystem * filesystem)
{
for(size_t i = 0; i < filesystem->size; i++)
@ -406,7 +367,7 @@ wfp_impl_static_filesystem_dispose(
}
void
wfp_impl_static_filesystem_add(
wfp_static_filesystem_add(
struct wfp_static_filesystem * filesystem,
char const * path,
int mode,
@ -416,15 +377,15 @@ wfp_impl_static_filesystem_add(
struct wf_path * path_ = wf_path_create(path);
if (NULL != path_)
{
size_t parent = wfp_impl_static_filesystem_make_parent(filesystem, path_);
struct wfp_static_filesystem_entry * entry = wfp_impl_static_filesystem_add_entry(filesystem);
size_t parent = wfp_static_filesystem_make_parent(filesystem, path_);
struct wfp_static_filesystem_entry * entry = wfp_static_filesystem_add_entry(filesystem);
entry->parent = parent;
entry->is_file = true;
entry->name = strdup(wf_path_get_filename(path_));
entry->mode = mode;
entry->size = length;
entry->get_info = &wfp_impl_static_filesystem_entry_get_info;
entry->read = &wfp_impl_static_filesystem_entry_read;
entry->get_info = &wfp_static_filesystem_entry_get_info;
entry->read = &wfp_static_filesystem_entry_read;
entry->user_data = entry;
entry->content = malloc(length);
@ -435,64 +396,12 @@ wfp_impl_static_filesystem_add(
}
void
wfp_impl_static_filesystem_add_text(
wfp_static_filesystem_add_text(
struct wfp_static_filesystem * filesystem,
char const * path,
int mode,
char const * content)
{
size_t length = strlen(content);
wfp_impl_static_filesystem_add(filesystem, path, mode, content, length);
}
void
wfp_impl_static_filesystem_add_file(
struct wfp_static_filesystem * filesystem,
char const * path,
char const * filename)
{
struct wf_path * path_ = wf_path_create(path);
if (NULL != path_)
{
size_t parent = wfp_impl_static_filesystem_make_parent(filesystem, path_);
struct wfp_static_filesystem_entry * entry = wfp_impl_static_filesystem_add_entry(filesystem);
entry->parent = parent;
entry->is_file = true;
entry->mode = 0;
entry->content = strdup(filename);
entry->size = 0;
entry->name = strdup(wf_path_get_filename(path_));
entry->get_info = &wfp_impl_static_filesystem_file_get_info;
entry->read = &wfp_impl_static_filesystem_file_read;
entry->user_data = entry;
wf_path_dispose(path_);
}
}
void
wfp_impl_static_filesystem_add_generic(
struct wfp_static_filesystem * filesystem,
char const * path,
wfp_static_filesystem_read_fn * read,
wfp_static_filesystem_get_info_fn * get_info,
void * user_data)
{
struct wf_path * path_ = wf_path_create(path);
if (NULL != path_)
{
size_t parent = wfp_impl_static_filesystem_make_parent(filesystem, path_);
struct wfp_static_filesystem_entry * entry = wfp_impl_static_filesystem_add_entry(filesystem);
entry->parent = parent;
entry->is_file = true;
entry->mode = 0;
entry->content = NULL;
entry->size = 0;
entry->name = strdup(wf_path_get_filename(path_));
entry->get_info = get_info;
entry->read = read;
entry->user_data = user_data;
wf_path_dispose(path_);
}
wfp_static_filesystem_add(filesystem, path, mode, content, length);
}

View File

@ -0,0 +1,63 @@
#ifndef WFP_STATIC_FILESYSTEM_H
#define WFP_STATIC_FILESYSTEM_H
#ifndef __cplusplus
#include <stddef.h>
#else
#include <cstddef>
using ::std::size_t;
#endif
#include <webfuse/provider/api.h>
#ifdef __cplusplus
extern "C"
{
#endif
struct wfp_client_config;
struct wfp_static_filesystem;
typedef size_t
wfp_static_filesystem_read_fn(
size_t offset,
char * buffer,
size_t buffer_size,
void * user_data);
typedef void
wfp_static_filesystem_get_info_fn(
void * user_data,
int * result_mode,
size_t * result_size);
extern WFP_API struct wfp_static_filesystem *
wfp_static_filesystem_create(
struct wfp_client_config * config);
extern WFP_API void
wfp_static_filesystem_dispose(
struct wfp_static_filesystem * filesystem);
extern WFP_API void
wfp_static_filesystem_add(
struct wfp_static_filesystem * filesystem,
char const * path,
int mode,
char const * content,
size_t length);
extern WFP_API void
wfp_static_filesystem_add_text(
struct wfp_static_filesystem * filesystem,
char const * path,
int mode,
char const * content);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -1,5 +1,5 @@
#include "webfuse/core/string.h"
#include "tempdir.hpp"
#include "webfuse/utils/tempdir.hpp"
#include <unistd.h>
#include <cstdlib>

View File

@ -1,4 +1,4 @@
#include "timeout_watcher.hpp"
#include "webfuse/utils/timeout_watcher.hpp"
#include <stdexcept>
using std::chrono::milliseconds;

View File

@ -1,7 +1,8 @@
#include "fake_adapter_server.hpp"
#include "timeout_watcher.hpp"
#include "webfuse/utils/ws_server.hpp"
#include "webfuse/utils/timeout_watcher.hpp"
#include "webfuse/core/util.h"
#include "webfuse/core/protocol_names.h"
#include <libwebsockets.h>
#include <cstring>
#include <vector>
@ -30,7 +31,7 @@ public:
extern "C"
{
static int wf_test_fake_adapter_server_callback(
static int wf_test_utils_ws_server_callback(
struct lws * wsi,
enum lws_callback_reasons reason,
void * WF_UNUSED_PARAM(user),
@ -65,7 +66,6 @@ static int wf_test_fake_adapter_server_callback(
break;
}
return 0;
}
@ -74,19 +74,25 @@ static int wf_test_fake_adapter_server_callback(
namespace webfuse_test
{
class FakeAdapterServer::Private: public IServer
class WebsocketServer::Private: public IServer
{
public:
explicit Private(int port)
Private(int port, struct lws_protocols * additionalProtocols, size_t additionalProtocolsCount)
: client_wsi(nullptr)
, message_received(false)
{
memset(ws_protocols, 0, sizeof(struct lws_protocols) * 2);
ws_protocols[0].name = "fs";
ws_protocols[0].callback = &wf_test_fake_adapter_server_callback;
ws_protocols = new struct lws_protocols[2 + additionalProtocolsCount];
memset(ws_protocols, 0, sizeof(struct lws_protocols) * (2 + additionalProtocolsCount));
ws_protocols[0].name = WF_PROTOCOL_NAME_ADAPTER_SERVER;
ws_protocols[0].callback = &wf_test_utils_ws_server_callback;
ws_protocols[0].per_session_data_size = 0;
ws_protocols[0].user = reinterpret_cast<void*>(this);
if (0 < additionalProtocolsCount)
{
memcpy(&ws_protocols[additionalProtocolsCount], additionalProtocols, sizeof(struct lws_protocols) * additionalProtocolsCount);
}
memset(&info, 0, sizeof(struct lws_context_creation_info));
info.port = port;
info.mounts = NULL;
@ -96,11 +102,18 @@ public:
info.options = LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE;
context = lws_create_context(&info);
}
virtual ~Private()
{
lws_context_destroy(context);
delete[] ws_protocols;
}
struct lws_context * getContext()
{
return context;
}
void waitForConnection()
@ -114,6 +127,43 @@ public:
}
}
void sendMessage(json_t * message)
{
char* message_text = json_dumps(message, JSON_COMPACT);
writeQueue.push(message_text);
json_decref(message);
free(message_text);
if (nullptr != client_wsi)
{
lws_callback_on_writable(client_wsi);
TimeoutWatcher watcher(DEFAULT_TIMEOUT);
while (!writeQueue.empty())
{
watcher.check();
lws_service(context, 100);
}
}
}
json_t * receiveMessage()
{
TimeoutWatcher watcher(DEFAULT_TIMEOUT);
while (recvQueue.empty())
{
watcher.check();
lws_service(context, 100);
}
std::string const & message_text = recvQueue.front();
json_t * message = json_loads(message_text.c_str(), JSON_DECODE_ANY, nullptr);
recvQueue.pop();
return message;
}
void onConnectionEstablished(struct lws * wsi) override
{
client_wsi = wsi;
@ -131,24 +181,23 @@ public:
{
if (wsi == client_wsi)
{
last_message.assign(length, *data);
message_received = true;
recvQueue.push(std::string(data, length));
}
}
void onWritable(struct lws * wsi) override
{
if (!queue.empty())
if (!writeQueue.empty())
{
std::string const & message = queue.front();
std::string const & message = writeQueue.front();
unsigned char * data = new unsigned char[LWS_PRE + message.size()];
memcpy(&data[LWS_PRE], message.c_str(), message.size());
lws_write(wsi, &data[LWS_PRE], message.size(), LWS_WRITE_TEXT);
delete[] data;
queue.pop();
if (!queue.empty())
writeQueue.pop();
if (!writeQueue.empty())
{
lws_callback_on_writable(wsi);
}
@ -161,36 +210,57 @@ private:
{
if (nullptr != client_wsi)
{
queue.push(message);
writeQueue.push(message);
lws_callback_on_writable(client_wsi);
}
}
struct lws * client_wsi;
bool message_received;
struct lws_protocols ws_protocols[2];
struct lws_protocols * ws_protocols;
struct lws_context_creation_info info;
struct lws_context * context;
std::vector<char> last_message;
std::queue<std::string> queue;
std::queue<std::string> writeQueue;
std::queue<std::string> recvQueue;
};
FakeAdapterServer::FakeAdapterServer(int port)
: d(new Private(port))
WebsocketServer::WebsocketServer(int port)
: d(new Private(port, nullptr, 0))
{
}
FakeAdapterServer::~FakeAdapterServer()
WebsocketServer::WebsocketServer(int port, struct lws_protocols * additionalProtocols, std::size_t additionalProtocolsCount)
: d(new Private(port, additionalProtocols, additionalProtocolsCount))
{
}
WebsocketServer::~WebsocketServer()
{
delete d;
}
void FakeAdapterServer::waitForConnection()
struct lws_context * WebsocketServer::getContext()
{
return d->getContext();
}
void WebsocketServer::waitForConnection()
{
d->waitForConnection();
}
void WebsocketServer::sendMessage(json_t * message)
{
d->sendMessage(message);
}
json_t * WebsocketServer::receiveMessage()
{
return d->receiveMessage();
}
}

View File

@ -0,0 +1,29 @@
#ifndef WF_TEST_UTILS_WS_SERVER_HPP
#define WF_TEST_UTILS_WS_SERVER_HPP
#include <libwebsockets.h>
#include <jansson.h>
namespace webfuse_test
{
class WebsocketServer
{
WebsocketServer(WebsocketServer const &) = delete;
WebsocketServer & operator=(WebsocketServer const &) = delete;
public:
explicit WebsocketServer(int port);
WebsocketServer(int port, struct lws_protocols * additionalProtocols, std::size_t additionalProtocolsCount);
~WebsocketServer();
struct lws_context * getContext();
void waitForConnection();
void sendMessage(json_t * message);
json_t * receiveMessage();
private:
class Private;
Private * d;
};
}
#endif