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

fixed unit tests; added logging support; allow to specify filesystem name

This commit is contained in:
Falk Werner
2020-11-12 20:36:05 +01:00
parent 8140afe2ab
commit c26342cefd
15 changed files with 296 additions and 88 deletions

View File

@@ -13,8 +13,10 @@
#include <libwebsockets.h>
#include <cstring>
#include <queue>
#include <sstream>
#include <thread>
#include <mutex>
using webfuse_test::WsServer;
using webfuse_test::MockProviderClient;
@@ -37,7 +39,10 @@ class ClientProtocolFixture
public:
explicit ClientProtocolFixture(IProviderClient& client, bool enableAuthentication = false)
{
server = new WsServer(WFP_PROTOCOL_NAME_ADAPTER_SERVER);
server = new WsServer(WFP_PROTOCOL_NAME_ADAPTER_SERVER,
[&](std::string const& message) mutable {
OnMessageReceived(message);
});
config = wfp_client_config_create();
client.AttachTo(config, enableAuthentication);
@@ -67,7 +72,7 @@ public:
{
TimeoutWatcher watcher(DEFAULT_TIMEOUT);
wfp_client_protocol_connect(protocol, context, server->GetUrl().c_str());
wfp_client_protocol_connect(protocol, context, server->GetUrl().c_str());
while (!server->IsConnected())
{
watcher.check();
@@ -85,15 +90,36 @@ public:
server->SendMessage(request);
}
void OnMessageReceived(std::string const message)
{
{
std::lock_guard<std::mutex> lock(mutex);
recv_queue.push(message);
}
lws_cancel_service(context);
}
std::string ReceiveMessage()
{
std::lock_guard<std::mutex> lock(mutex);
std::string result;
if (!recv_queue.empty())
{
result = recv_queue.front();
recv_queue.pop();
}
return result;
}
std::string ReceiveMessageFromClient()
{
TimeoutWatcher watcher(DEFAULT_TIMEOUT);
std::string result = server->ReceiveMessage();
std::string result = ReceiveMessage();
while (result.empty())
{
watcher.check();
lws_service(context, 0);
result = server->ReceiveMessage();
result = ReceiveMessage();
}
return result;
@@ -128,7 +154,7 @@ public:
wfp_json const * username = wfp_impl_json_object_get(credentials, "username");
ASSERT_TRUE(wfp_impl_json_is_string(username));
ASSERT_STREQ(expected_username.c_str(), wfp_impl_json_string_get(username));
wfp_json const * password = wfp_impl_json_object_get(credentials, "password");
ASSERT_TRUE(wfp_impl_json_is_string(password));
ASSERT_STREQ(expected_password.c_str(), wfp_impl_json_string_get(password));
@@ -171,6 +197,8 @@ private:
struct lws_context_creation_info info;
struct lws_protocols protocols[2];
struct lws_context * context;
std::queue<std::string> recv_queue;
std::mutex mutex;
};
@@ -217,7 +245,7 @@ TEST(client_protocol, connect_with_username_authentication)
EXPECT_CALL(provider, OnConnected()).Times(AtMost(1));
EXPECT_CALL(provider, OnDisconnected()).Times(1);
EXPECT_CALL(provider, GetCredentials(_)).Times(1).WillOnce(Invoke(GetCredentials));
fixture.Connect();
if (HasFatalFailure()) { return; }
@@ -253,4 +281,4 @@ TEST(client_protocol, getattr)
ASSERT_FALSE(response.empty());
fixture.Disconnect();
}
}

View File

@@ -101,12 +101,10 @@ public:
info.mounts = NULL;
info.protocols = protocols;
info.vhost_name = "localhost";
info.ws_ping_pong_interval = 10;
// info.ws_ping_pong_interval = 10;
info.options = LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE;
info.options |= LWS_SERVER_OPTION_EXPLICIT_VHOSTS;
context = lws_create_context(&info);
if (use_tls)
{
info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
@@ -114,9 +112,11 @@ public:
info.ssl_private_key_filepath = "server-key.pem";
}
context = lws_create_context(&info);
struct lws_vhost * vhost = lws_create_vhost(context, &info);
int port = lws_get_vhost_port(vhost);
std::ostringstream stream;
std::ostringstream stream;
stream << (use_tls ? "wss://" : "ws://") << "localhost:" << port << "/";
url = stream.str();
@@ -149,7 +149,7 @@ public:
std::string Invoke(std::string const & method, std::string const & params)
{
std::promise<std::string> response;
{
{
std::unique_lock<std::mutex> lock(mutex);
message = &response;
id++;
@@ -220,7 +220,7 @@ public:
lws_callback_on_writable(wsi);
}
}
wfp_impl_json_doc_dispose(doc);
}
}
@@ -337,4 +337,4 @@ std::string WebfuseServer::ReadDir(int inode)
return d->Invoke("readdir", params.str());
}
}
}

View File

@@ -31,7 +31,10 @@ class WsServer::Private : IServer
Private(Private const &) = delete;
Private & operator=(Private const &) = delete;
public:
Private(std::string const & protocol, int port);
Private(
std::string const & protocol,
std::function<void(std::string const &)> handleMessage,
int port);
~Private();
bool IsConnected();
std::string GetUrl() const;
@@ -45,6 +48,7 @@ public:
private:
static void run(Private * self);
std::string protocol_;
std::function<void(std::string const &)> handleMessage_;
int port_;
bool is_connected;
bool is_shutdown_requested;
@@ -55,7 +59,6 @@ private:
std::thread context;
std::mutex mutex;
std::queue<std::string> writeQueue;
std::queue<std::string> recvQueue;
};
}
@@ -107,8 +110,11 @@ static int wfp_test_utils_ws_server_callback(
namespace webfuse_test
{
WsServer::WsServer(std::string const & protocol, int port)
: d(new Private(protocol, port))
WsServer::WsServer(
std::string const & protocol,
std::function<void(std::string const &)> handleMessage,
int port)
: d(new Private(protocol, handleMessage, port))
{
}
@@ -128,19 +134,18 @@ void WsServer::SendMessage(std::string const & message)
d->SendMessage(message);
}
std::string WsServer::ReceiveMessage()
{
return d->ReceiveMessage();
}
std::string WsServer::GetUrl() const
{
return d->GetUrl();
}
WsServer::Private::Private(std::string const & protocol, int port)
WsServer::Private::Private(
std::string const & protocol,
std::function<void(std::string const &)> handleMessage,
int port)
: protocol_(protocol)
, handleMessage_(handleMessage)
, port_(port)
, is_connected(false)
, is_shutdown_requested(false)
@@ -160,7 +165,7 @@ WsServer::Private::Private(std::string const & protocol, int port)
info.mounts = NULL;
info.protocols =ws_protocols;
info.vhost_name = "localhost";
info.ws_ping_pong_interval = 10;
// info.ws_ping_pong_interval = 10;
info.options = LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE;
info.options |= LWS_SERVER_OPTION_EXPLICIT_VHOSTS;
@@ -270,25 +275,16 @@ void WsServer::Private::SendMessage(std::string const & message)
void WsServer::Private::OnMessageReceived(struct lws * wsi, char const * data, size_t length)
{
std::unique_lock<std::mutex> lock(mutex);
if (wsi == wsi_)
bool handleMessage;
{
recvQueue.push(std::string(data, length));
}
}
std::string WsServer::Private::ReceiveMessage()
{
std::unique_lock<std::mutex> lock(mutex);
std::string message;
if (!recvQueue.empty())
{
message = recvQueue.front();
recvQueue.pop();
std::unique_lock<std::mutex> lock(mutex);
handleMessage = (wsi == wsi_);
}
return message;
if (handleMessage)
{
handleMessage_(std::string(data, length));
}
}
std::string WsServer::Private::GetUrl() const
@@ -299,4 +295,4 @@ std::string WsServer::Private::GetUrl() const
}
}
}

View File

@@ -2,6 +2,7 @@
#define WFP_TEST_UTILS_WS_SERVER_HPP
#include <string>
#include <functional>
namespace webfuse_test
{
@@ -11,12 +12,14 @@ class WsServer
WsServer(WsServer const &) = delete;
WsServer & operator=(WsServer const &) = delete;
public:
WsServer(std::string const & protocol, int port = 0);
WsServer(
std::string const & protocol,
std::function<void(std::string const &)> handleMessage,
int port = 0);
~WsServer();
bool IsConnected();
std::string GetUrl() const;
void SendMessage(std::string const & message);
std::string ReceiveMessage();
private:
class Private;
Private * d;