diff --git a/cmake/unit_tests.cmake b/cmake/unit_tests.cmake index c4f779e..1ff1be5 100644 --- a/cmake/unit_tests.cmake +++ b/cmake/unit_tests.cmake @@ -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 diff --git a/cmake/webfuse_core.cmake b/cmake/webfuse_core.cmake index b162f40..52c7681 100644 --- a/cmake/webfuse_core.cmake +++ b/cmake/webfuse_core.cmake @@ -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 ) diff --git a/cmake/webfuse_provider.cmake b/cmake/webfuse_provider.cmake index b42f1f4..5079cb1 100644 --- a/cmake/webfuse_provider.cmake +++ b/cmake/webfuse_provider.cmake @@ -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) diff --git a/doc/protocol.md b/doc/protocol.md index 5245b3d..32eaa9d 100644 --- a/doc/protocol.md +++ b/doc/protocol.md @@ -227,7 +227,7 @@ Read from an open file. Adds a filesystem. - fs provider: {"method": "add_filesytem", "params": [], "id": } + fs provider: {"method": "add_filesystem", "params": [], "id": } webfuse daemon: {"result": {"id": }, "id": } | Item | Data type | Description | diff --git a/include/webfuse/core/protocol_names.h b/include/webfuse/core/protocol_names.h new file mode 100644 index 0000000..4de8c4a --- /dev/null +++ b/include/webfuse/core/protocol_names.h @@ -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 diff --git a/include/webfuse/provider/static_filesystem.h b/include/webfuse/provider/static_filesystem.h deleted file mode 100644 index e736976..0000000 --- a/include/webfuse/provider/static_filesystem.h +++ /dev/null @@ -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 -#else -#include -using ::std::size_t; -#endif - -#include - -#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 diff --git a/include/webfuse_adapter.h b/include/webfuse_adapter.h index af37c14..1ff048f 100644 --- a/include/webfuse_adapter.h +++ b/include/webfuse_adapter.h @@ -7,6 +7,7 @@ #define WF_ADAPTER_H #include +#include #include #include diff --git a/include/webfuse_provider.h b/include/webfuse_provider.h index 5882c60..7af3934 100644 --- a/include/webfuse_provider.h +++ b/include/webfuse_provider.h @@ -7,6 +7,7 @@ #define WF_PROVIDER_H #include +#include #include #include @@ -22,6 +23,4 @@ #include #include -#include - #endif diff --git a/lib/webfuse/adapter/impl/server.c b/lib/webfuse/adapter/impl/server.c index ad3e5c2..bf32b85 100644 --- a/lib/webfuse/adapter/impl/server.c +++ b/lib/webfuse/adapter/impl/server.c @@ -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)); diff --git a/lib/webfuse/adapter/impl/server_protocol.c b/lib/webfuse/adapter/impl/server_protocol.c index 02efd80..74d4631 100644 --- a/lib/webfuse/adapter/impl/server_protocol.c +++ b/lib/webfuse/adapter/impl/server_protocol.c @@ -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; diff --git a/lib/webfuse/provider/api.c b/lib/webfuse/provider/api.c index 933516b..0c0e88a 100644 --- a/lib/webfuse/provider/api.c +++ b/lib/webfuse/provider/api.c @@ -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); -} diff --git a/lib/webfuse/provider/impl/client.c b/lib/webfuse/provider/impl/client.c index 8b3c06b..4ddae92 100644 --- a/lib/webfuse/provider/impl/client.c +++ b/lib/webfuse/provider/impl/client.c @@ -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)); diff --git a/lib/webfuse/provider/impl/client_protocol.c b/lib/webfuse/provider/impl/client_protocol.c index 6d5ba85..59945bd 100644 --- a/lib/webfuse/provider/impl/client_protocol.c +++ b/lib/webfuse/provider/impl/client_protocol.c @@ -6,7 +6,6 @@ #include #include - #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); diff --git a/lib/webfuse/provider/impl/client_protocol.h b/lib/webfuse/provider/impl/client_protocol.h index d197559..d6f537d 100644 --- a/lib/webfuse/provider/impl/client_protocol.h +++ b/lib/webfuse/provider/impl/client_protocol.h @@ -11,8 +11,6 @@ extern "C" { #endif -#define WFP_CLIENT_PROTOCOL_NAME ("fs") - struct wfp_client_config; struct lws_protocols; struct lws_context; diff --git a/lib/webfuse/provider/impl/static_filesystem.h b/lib/webfuse/provider/impl/static_filesystem.h deleted file mode 100644 index a18835d..0000000 --- a/lib/webfuse/provider/impl/static_filesystem.h +++ /dev/null @@ -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 diff --git a/test/core/test_path.cc b/test/core/test_path.cc deleted file mode 100644 index 93783ec..0000000 --- a/test/core/test_path.cc +++ /dev/null @@ -1,58 +0,0 @@ -#include -#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); -} \ No newline at end of file diff --git a/test/fake_adapter_server.hpp b/test/fake_adapter_server.hpp deleted file mode 100644 index 9cc51f0..0000000 --- a/test/fake_adapter_server.hpp +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef WF_TEST_FAKE_SERVER_HPP -#define WF_TEST_FAKE_SERVER_HPP - -#include - -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 diff --git a/test/provider/test_client_protocol.cc b/test/provider/test_client_protocol.cc deleted file mode 100644 index 4e78f8e..0000000 --- a/test/provider/test_client_protocol.cc +++ /dev/null @@ -1,72 +0,0 @@ -#include -#include - -#include -#include -#include "fake_adapter_server.hpp" - -#include -#include -#include - -using webfuse_test::FakeAdapterServer; -using testing::_; - -namespace -{ - -struct Context -{ - lws_context * context; - std::atomic 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); -} \ No newline at end of file diff --git a/test/provider/test_static_filesystem.cc b/test/provider/test_static_filesystem.cc deleted file mode 100644 index e7db28e..0000000 --- a/test/provider/test_static_filesystem.cc +++ /dev/null @@ -1,61 +0,0 @@ -#include - -#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); -} diff --git a/test/mock_authenticator.cc b/test/webfuse/mocks/mock_authenticator.cc similarity index 94% rename from test/mock_authenticator.cc rename to test/webfuse/mocks/mock_authenticator.cc index f2decab..35648ed 100644 --- a/test/mock_authenticator.cc +++ b/test/webfuse/mocks/mock_authenticator.cc @@ -1,4 +1,4 @@ -#include "mock_authenticator.hpp" +#include "webfuse/mocks/mock_authenticator.hpp" #define WF_AUTHENTICATOR_COUNT 3 diff --git a/test/mock_authenticator.hpp b/test/webfuse/mocks/mock_authenticator.hpp similarity index 100% rename from test/mock_authenticator.hpp rename to test/webfuse/mocks/mock_authenticator.hpp diff --git a/test/webfuse/mocks/mock_provider_client.cc b/test/webfuse/mocks/mock_provider_client.cc new file mode 100644 index 0000000..a624ba3 --- /dev/null +++ b/test/webfuse/mocks/mock_provider_client.cc @@ -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(user_data); + self->OnConnected(); +} + +static void webfuse_test_iproviderclient_ondisconnected( + void * user_data) +{ + auto * self = reinterpret_cast(user_data); + self->OnDisconnected(); +} + +static void webfuse_test_iproviderclient_ontimer( + void * user_data) +{ + auto * self = reinterpret_cast(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(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(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(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(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(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(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(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); +} + +} \ No newline at end of file diff --git a/test/webfuse/mocks/mock_provider_client.hpp b/test/webfuse/mocks/mock_provider_client.hpp new file mode 100644 index 0000000..45fdf91 --- /dev/null +++ b/test/webfuse/mocks/mock_provider_client.hpp @@ -0,0 +1,55 @@ +#ifndef WF_MOCK_PROVIDER_CLIENT_HPP +#define WF_MOCK_PROVIDER_CLIENT_HPP + +#include +#include "webfuse/provider/client_config.h" +#include "webfuse/core/status.h" +#include + +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 diff --git a/test/mock_request.cc b/test/webfuse/mocks/mock_request.cc similarity index 95% rename from test/mock_request.cc rename to test/webfuse/mocks/mock_request.cc index efd8334..c2dd441 100644 --- a/test/mock_request.cc +++ b/test/webfuse/mocks/mock_request.cc @@ -1,4 +1,4 @@ -#include "mock_request.hpp" +#include "webfuse/mocks/mock_request.hpp" #include namespace diff --git a/test/mock_request.hpp b/test/webfuse/mocks/mock_request.hpp similarity index 100% rename from test/mock_request.hpp rename to test/webfuse/mocks/mock_request.hpp diff --git a/test/adapter/jsonrpc/test_is_request.cc b/test/webfuse/tests/adapter/jsonrpc/test_is_request.cc similarity index 100% rename from test/adapter/jsonrpc/test_is_request.cc rename to test/webfuse/tests/adapter/jsonrpc/test_is_request.cc diff --git a/test/adapter/jsonrpc/test_is_response.cc b/test/webfuse/tests/adapter/jsonrpc/test_is_response.cc similarity index 100% rename from test/adapter/jsonrpc/test_is_response.cc rename to test/webfuse/tests/adapter/jsonrpc/test_is_response.cc diff --git a/test/adapter/jsonrpc/test_proxy.cc b/test/webfuse/tests/adapter/jsonrpc/test_proxy.cc similarity index 99% rename from test/adapter/jsonrpc/test_proxy.cc rename to test/webfuse/tests/adapter/jsonrpc/test_proxy.cc index 1355c4e..8f99312 100644 --- a/test/adapter/jsonrpc/test_proxy.cc +++ b/test/webfuse/tests/adapter/jsonrpc/test_proxy.cc @@ -1,7 +1,7 @@ #include #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; diff --git a/test/adapter/jsonrpc/test_request.cc b/test/webfuse/tests/adapter/jsonrpc/test_request.cc similarity index 100% rename from test/adapter/jsonrpc/test_request.cc rename to test/webfuse/tests/adapter/jsonrpc/test_request.cc diff --git a/test/adapter/jsonrpc/test_response.cc b/test/webfuse/tests/adapter/jsonrpc/test_response.cc similarity index 100% rename from test/adapter/jsonrpc/test_response.cc rename to test/webfuse/tests/adapter/jsonrpc/test_response.cc diff --git a/test/adapter/jsonrpc/test_server.cc b/test/webfuse/tests/adapter/jsonrpc/test_server.cc similarity index 100% rename from test/adapter/jsonrpc/test_server.cc rename to test/webfuse/tests/adapter/jsonrpc/test_server.cc diff --git a/test/adapter/jsonrpc/test_util.cc b/test/webfuse/tests/adapter/jsonrpc/test_util.cc similarity index 100% rename from test/adapter/jsonrpc/test_util.cc rename to test/webfuse/tests/adapter/jsonrpc/test_util.cc diff --git a/test/adapter/test_authenticator.cc b/test/webfuse/tests/adapter/test_authenticator.cc similarity index 97% rename from test/adapter/test_authenticator.cc rename to test/webfuse/tests/adapter/test_authenticator.cc index 93505a9..07e06e5 100644 --- a/test/adapter/test_authenticator.cc +++ b/test/webfuse/tests/adapter/test_authenticator.cc @@ -1,7 +1,7 @@ #include #include -#include "mock_authenticator.hpp" +#include "webfuse/mocks/mock_authenticator.hpp" #include "webfuse/adapter/impl/authenticator.h" #include "webfuse/adapter/impl/credentials.h" diff --git a/test/adapter/test_authenticators.cc b/test/webfuse/tests/adapter/test_authenticators.cc similarity index 99% rename from test/adapter/test_authenticators.cc rename to test/webfuse/tests/adapter/test_authenticators.cc index 3227e57..6cb8064 100644 --- a/test/adapter/test_authenticators.cc +++ b/test/webfuse/tests/adapter/test_authenticators.cc @@ -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; diff --git a/test/adapter/test_credentials.cc b/test/webfuse/tests/adapter/test_credentials.cc similarity index 100% rename from test/adapter/test_credentials.cc rename to test/webfuse/tests/adapter/test_credentials.cc diff --git a/test/adapter/test_fuse_req.cc b/test/webfuse/tests/adapter/test_fuse_req.cc similarity index 100% rename from test/adapter/test_fuse_req.cc rename to test/webfuse/tests/adapter/test_fuse_req.cc diff --git a/test/adapter/test_mountpoint.cc b/test/webfuse/tests/adapter/test_mountpoint.cc similarity index 100% rename from test/adapter/test_mountpoint.cc rename to test/webfuse/tests/adapter/test_mountpoint.cc diff --git a/test/adapter/test_response_parser.cc b/test/webfuse/tests/adapter/test_response_parser.cc similarity index 100% rename from test/adapter/test_response_parser.cc rename to test/webfuse/tests/adapter/test_response_parser.cc diff --git a/test/adapter/test_server.cc b/test/webfuse/tests/adapter/test_server.cc similarity index 100% rename from test/adapter/test_server.cc rename to test/webfuse/tests/adapter/test_server.cc diff --git a/test/adapter/test_server_config.cc b/test/webfuse/tests/adapter/test_server_config.cc similarity index 99% rename from test/adapter/test_server_config.cc rename to test/webfuse/tests/adapter/test_server_config.cc index cb176d6..f85aa92 100644 --- a/test/adapter/test_server_config.cc +++ b/test/webfuse/tests/adapter/test_server_config.cc @@ -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; diff --git a/test/adapter/test_timepoint.cc b/test/webfuse/tests/adapter/test_timepoint.cc similarity index 95% rename from test/adapter/test_timepoint.cc rename to test/webfuse/tests/adapter/test_timepoint.cc index 85de35d..8b0db97 100644 --- a/test/adapter/test_timepoint.cc +++ b/test/webfuse/tests/adapter/test_timepoint.cc @@ -1,6 +1,6 @@ #include -#include "msleep.hpp" +#include "webfuse/utils/msleep.hpp" #include "webfuse/adapter/impl/time/timepoint.h" using webfuse_test::msleep; diff --git a/test/adapter/test_timer.cc b/test/webfuse/tests/adapter/test_timer.cc similarity index 99% rename from test/adapter/test_timer.cc rename to test/webfuse/tests/adapter/test_timer.cc index b838147..d554f2d 100644 --- a/test/adapter/test_timer.cc +++ b/test/webfuse/tests/adapter/test_timer.cc @@ -2,7 +2,7 @@ #include -#include "msleep.hpp" +#include "webfuse/utils/msleep.hpp" #include "webfuse/adapter/impl/time/timer.h" #include "webfuse/adapter/impl/time/timeout_manager.h" diff --git a/test/adapter/test_uuid_mountpoint.cc b/test/webfuse/tests/adapter/test_uuid_mountpoint.cc similarity index 96% rename from test/adapter/test_uuid_mountpoint.cc rename to test/webfuse/tests/adapter/test_uuid_mountpoint.cc index 8bff760..7549b84 100644 --- a/test/adapter/test_uuid_mountpoint.cc +++ b/test/webfuse/tests/adapter/test_uuid_mountpoint.cc @@ -1,7 +1,7 @@ #include -#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" diff --git a/test/adapter/test_uuid_mountpoint_factory.cc b/test/webfuse/tests/adapter/test_uuid_mountpoint_factory.cc similarity index 96% rename from test/adapter/test_uuid_mountpoint_factory.cc rename to test/webfuse/tests/adapter/test_uuid_mountpoint_factory.cc index b097ae0..dca941f 100644 --- a/test/adapter/test_uuid_mountpoint_factory.cc +++ b/test/webfuse/tests/adapter/test_uuid_mountpoint_factory.cc @@ -1,8 +1,8 @@ #include #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; diff --git a/test/core/test_base64.cc b/test/webfuse/tests/core/test_base64.cc similarity index 100% rename from test/core/test_base64.cc rename to test/webfuse/tests/core/test_base64.cc diff --git a/test/core/test_container_of.cc b/test/webfuse/tests/core/test_container_of.cc similarity index 100% rename from test/core/test_container_of.cc rename to test/webfuse/tests/core/test_container_of.cc diff --git a/test/core/test_message.cc b/test/webfuse/tests/core/test_message.cc similarity index 100% rename from test/core/test_message.cc rename to test/webfuse/tests/core/test_message.cc diff --git a/test/core/test_message_queue.cc b/test/webfuse/tests/core/test_message_queue.cc similarity index 100% rename from test/core/test_message_queue.cc rename to test/webfuse/tests/core/test_message_queue.cc diff --git a/test/core/test_slist.cc b/test/webfuse/tests/core/test_slist.cc similarity index 100% rename from test/core/test_slist.cc rename to test/webfuse/tests/core/test_slist.cc diff --git a/test/core/test_status.cc b/test/webfuse/tests/core/test_status.cc similarity index 100% rename from test/core/test_status.cc rename to test/webfuse/tests/core/test_status.cc diff --git a/test/core/test_string.cc b/test/webfuse/tests/core/test_string.cc similarity index 100% rename from test/core/test_string.cc rename to test/webfuse/tests/core/test_string.cc diff --git a/test/integration/provider.cc b/test/webfuse/tests/integration/provider.cc similarity index 92% rename from test/integration/provider.cc rename to test/webfuse/tests/integration/provider.cc index 3ee4285..5142ca8 100644 --- a/test/integration/provider.cc +++ b/test/webfuse/tests/integration/provider.cc @@ -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 #include #include -#include "msleep.hpp" +#include "webfuse/utils/msleep.hpp" +#include "webfuse/utils/static_filesystem.h" namespace webfuse_test { diff --git a/test/integration/provider.hpp b/test/webfuse/tests/integration/provider.hpp similarity index 100% rename from test/integration/provider.hpp rename to test/webfuse/tests/integration/provider.hpp diff --git a/test/integration/server.cc b/test/webfuse/tests/integration/server.cc similarity index 95% rename from test/integration/server.cc rename to test/webfuse/tests/integration/server.cc index 0659efe..1360934 100644 --- a/test/integration/server.cc +++ b/test/webfuse/tests/integration/server.cc @@ -1,4 +1,4 @@ -#include "integration/server.hpp" +#include "webfuse/tests/integration/server.hpp" #include #include #include @@ -6,7 +6,7 @@ #include #include "webfuse_adapter.h" #include "webfuse/adapter/impl/server.h" -#include "msleep.hpp" +#include "webfuse/utils/msleep.hpp" #define WF_PATH_MAX (100) diff --git a/test/integration/server.hpp b/test/webfuse/tests/integration/server.hpp similarity index 100% rename from test/integration/server.hpp rename to test/webfuse/tests/integration/server.hpp diff --git a/test/integration/test_integration.cc b/test/webfuse/tests/integration/test_integration.cc similarity index 96% rename from test/integration/test_integration.cc rename to test/webfuse/tests/integration/test_integration.cc index 8bd21c6..203856c 100644 --- a/test/integration/test_integration.cc +++ b/test/webfuse/tests/integration/test_integration.cc @@ -1,6 +1,6 @@ #include -#include "integration/server.hpp" -#include "integration/provider.hpp" +#include "webfuse/tests/integration/server.hpp" +#include "webfuse/tests/integration/provider.hpp" #include #include @@ -15,7 +15,7 @@ #include #include "webfuse/core/lws_log.h" -#include "die_if.hpp" +#include "webfuse/utils/die_if.hpp" using webfuse_test::Server; using webfuse_test::Provider; diff --git a/test/webfuse/tests/provider/test_client_protocol.cc b/test/webfuse/tests/provider/test_client_protocol.cc new file mode 100644 index 0000000..ff7be74 --- /dev/null +++ b/test/webfuse/tests/provider/test_client_protocol.cc @@ -0,0 +1,152 @@ +#include +#include + +#include +#include +#include "webfuse/utils/ws_server.hpp" +#include "webfuse/mocks/mock_provider_client.hpp" + +#include +#include +#include + +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); +} \ No newline at end of file diff --git a/test/provider/test_url.cc b/test/webfuse/tests/provider/test_url.cc similarity index 100% rename from test/provider/test_url.cc rename to test/webfuse/tests/provider/test_url.cc diff --git a/test/die_if.cc b/test/webfuse/utils/die_if.cc similarity index 79% rename from test/die_if.cc rename to test/webfuse/utils/die_if.cc index 3eff5ae..bd8dd70 100644 --- a/test/die_if.cc +++ b/test/webfuse/utils/die_if.cc @@ -1,4 +1,4 @@ -#include "die_if.hpp" +#include "webfuse/utils/die_if.hpp" #include namespace webfuse_test diff --git a/test/die_if.hpp b/test/webfuse/utils/die_if.hpp similarity index 100% rename from test/die_if.hpp rename to test/webfuse/utils/die_if.hpp diff --git a/test/file_utils.cc b/test/webfuse/utils/file_utils.cc similarity index 94% rename from test/file_utils.cc rename to test/webfuse/utils/file_utils.cc index 0d3ff81..7a3d29c 100644 --- a/test/file_utils.cc +++ b/test/webfuse/utils/file_utils.cc @@ -1,4 +1,4 @@ -#include "file_utils.hpp" +#include "webfuse/utils/file_utils.hpp" #include #include diff --git a/test/file_utils.hpp b/test/webfuse/utils/file_utils.hpp similarity index 100% rename from test/file_utils.hpp rename to test/webfuse/utils/file_utils.hpp diff --git a/test/msleep.cc b/test/webfuse/utils/msleep.cc similarity index 90% rename from test/msleep.cc rename to test/webfuse/utils/msleep.cc index 5f76e49..659bb81 100644 --- a/test/msleep.cc +++ b/test/webfuse/utils/msleep.cc @@ -1,4 +1,4 @@ -#include "msleep.hpp" +#include "webfuse/utils/msleep.hpp" #include namespace webfuse_test diff --git a/test/msleep.hpp b/test/webfuse/utils/msleep.hpp similarity index 100% rename from test/msleep.hpp rename to test/webfuse/utils/msleep.hpp diff --git a/lib/webfuse/core/path.c b/test/webfuse/utils/path.c similarity index 98% rename from lib/webfuse/core/path.c rename to test/webfuse/utils/path.c index 36689f0..3cb222e 100644 --- a/lib/webfuse/core/path.c +++ b/test/webfuse/utils/path.c @@ -1,4 +1,4 @@ -#include "webfuse/core/path.h" +#include "webfuse/utils/path.h" #include #include diff --git a/lib/webfuse/core/path.h b/test/webfuse/utils/path.h similarity index 100% rename from lib/webfuse/core/path.h rename to test/webfuse/utils/path.h diff --git a/lib/webfuse/provider/impl/static_filesystem.c b/test/webfuse/utils/static_filesystem.c similarity index 61% rename from lib/webfuse/provider/impl/static_filesystem.c rename to test/webfuse/utils/static_filesystem.c index 83c3b92..62b507a 100644 --- a/lib/webfuse/provider/impl/static_filesystem.c +++ b/test/webfuse/utils/static_filesystem.c @@ -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 @@ -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, ""); + wfp_static_filesystem_add_dir(filesystem, 0, ""); 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); } diff --git a/test/webfuse/utils/static_filesystem.h b/test/webfuse/utils/static_filesystem.h new file mode 100644 index 0000000..bf23b88 --- /dev/null +++ b/test/webfuse/utils/static_filesystem.h @@ -0,0 +1,63 @@ +#ifndef WFP_STATIC_FILESYSTEM_H +#define WFP_STATIC_FILESYSTEM_H + +#ifndef __cplusplus +#include +#else +#include +using ::std::size_t; +#endif + +#include + +#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 diff --git a/test/tempdir.cc b/test/webfuse/utils/tempdir.cc similarity index 92% rename from test/tempdir.cc rename to test/webfuse/utils/tempdir.cc index 040aed1..31dce25 100644 --- a/test/tempdir.cc +++ b/test/webfuse/utils/tempdir.cc @@ -1,5 +1,5 @@ #include "webfuse/core/string.h" -#include "tempdir.hpp" +#include "webfuse/utils/tempdir.hpp" #include #include diff --git a/test/tempdir.hpp b/test/webfuse/utils/tempdir.hpp similarity index 100% rename from test/tempdir.hpp rename to test/webfuse/utils/tempdir.hpp diff --git a/test/timeout_watcher.cc b/test/webfuse/utils/timeout_watcher.cc similarity index 93% rename from test/timeout_watcher.cc rename to test/webfuse/utils/timeout_watcher.cc index 7e3bb52..8d9add7 100644 --- a/test/timeout_watcher.cc +++ b/test/webfuse/utils/timeout_watcher.cc @@ -1,4 +1,4 @@ -#include "timeout_watcher.hpp" +#include "webfuse/utils/timeout_watcher.hpp" #include using std::chrono::milliseconds; diff --git a/test/timeout_watcher.hpp b/test/webfuse/utils/timeout_watcher.hpp similarity index 100% rename from test/timeout_watcher.hpp rename to test/webfuse/utils/timeout_watcher.hpp diff --git a/test/fake_adapter_server.cc b/test/webfuse/utils/ws_server.cc similarity index 55% rename from test/fake_adapter_server.cc rename to test/webfuse/utils/ws_server.cc index 72a1831..48e2588 100644 --- a/test/fake_adapter_server.cc +++ b/test/webfuse/utils/ws_server.cc @@ -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 #include #include @@ -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(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 last_message; - std::queue queue; + std::queue writeQueue; + std::queue recvQueue; }; -FakeAdapterServer::FakeAdapterServer(int port) -: d(new Private(port)) +WebsocketServer::WebsocketServer(int port) +: d(new Private(port, nullptr, 0)) +{ + +} + +WebsocketServer::WebsocketServer(int port, struct lws_protocols * additionalProtocols, std::size_t additionalProtocolsCount) +: d(new Private(port, additionalProtocols, additionalProtocolsCount)) { } -FakeAdapterServer::~FakeAdapterServer() +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(); +} + + } \ No newline at end of file diff --git a/test/webfuse/utils/ws_server.hpp b/test/webfuse/utils/ws_server.hpp new file mode 100644 index 0000000..e9d825a --- /dev/null +++ b/test/webfuse/utils/ws_server.hpp @@ -0,0 +1,29 @@ +#ifndef WF_TEST_UTILS_WS_SERVER_HPP +#define WF_TEST_UTILS_WS_SERVER_HPP + +#include +#include + +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