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

added unit tests

This commit is contained in:
Falk Werner 2020-06-15 16:58:04 +02:00
parent 4e42c856c7
commit 6bafbdd43a
3 changed files with 94 additions and 8 deletions

View File

@ -87,6 +87,74 @@ TEST(AdapterClient, Connect)
ASSERT_TRUE(watcher.waitUntil([&]() mutable { return disconnected; })); ASSERT_TRUE(watcher.waitUntil([&]() mutable { return disconnected; }));
} }
TEST(AdapterClient, IgnoreNonJsonMessage)
{
TimeoutWatcher watcher(TIMEOUT);
MockInvokationHander handler;
WsServer2 server(handler, WF_PROTOCOL_NAME_PROVIDER_SERVER);
EXPECT_CALL(handler, Invoke(_,_)).Times(0);
MockAdapterClientCallback callback;
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_INIT, nullptr)).Times(1);
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_CREATED, nullptr)).Times(1);
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_GET_TLS_CONFIG, _)).Times(1);
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_CLEANUP, nullptr)).Times(1);
bool connected = false;
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_CONNECTED, nullptr)).Times(1)
.WillOnce(Invoke([&] (wf_client *, int, void *) mutable { connected = true; }));
bool disconnected = false;
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_DISCONNECTED, nullptr)).Times(1)
.WillOnce(Invoke([&] (wf_client *, int, void *) mutable { disconnected = true; }));
AdapterClient client(callback.GetCallbackFn(), callback.GetUserData(), server.GetUrl());
client.Connect();
ASSERT_TRUE(watcher.waitUntil([&]() mutable { return connected; }));
server.SendMessage("brummni");
client.Disconnect();
ASSERT_TRUE(watcher.waitUntil([&]() mutable { return disconnected; }));
}
TEST(AdapterClient, IgnoreInvalidJsonMessage)
{
TimeoutWatcher watcher(TIMEOUT);
MockInvokationHander handler;
WsServer2 server(handler, WF_PROTOCOL_NAME_PROVIDER_SERVER);
EXPECT_CALL(handler, Invoke(_,_)).Times(0);
MockAdapterClientCallback callback;
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_INIT, nullptr)).Times(1);
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_CREATED, nullptr)).Times(1);
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_GET_TLS_CONFIG, _)).Times(1);
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_CLEANUP, nullptr)).Times(1);
bool connected = false;
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_CONNECTED, nullptr)).Times(1)
.WillOnce(Invoke([&] (wf_client *, int, void *) mutable { connected = true; }));
bool disconnected = false;
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_DISCONNECTED, nullptr)).Times(1)
.WillOnce(Invoke([&] (wf_client *, int, void *) mutable { disconnected = true; }));
AdapterClient client(callback.GetCallbackFn(), callback.GetUserData(), server.GetUrl());
client.Connect();
ASSERT_TRUE(watcher.waitUntil([&]() mutable { return connected; }));
json_t * invalid_request = json_object();
server.SendMessage(invalid_request);
client.Disconnect();
ASSERT_TRUE(watcher.waitUntil([&]() mutable { return disconnected; }));
}
TEST(AdapterClient, ConnectWithTls) TEST(AdapterClient, ConnectWithTls)
{ {
TimeoutWatcher watcher(TIMEOUT); TimeoutWatcher watcher(TIMEOUT);

View File

@ -70,7 +70,7 @@ static int wf_test_utils_ws_server_callback(
namespace webfuse_test namespace webfuse_test
{ {
class WsServer2::Private : IServer class WsServer2::Private : public IServer
{ {
Private(Private const &) = delete; Private(Private const &) = delete;
Private & operator=(Private const &) = delete; Private & operator=(Private const &) = delete;
@ -84,8 +84,9 @@ public:
void OnMessageReceived(struct lws * wsi, char const * data, size_t length) override; void OnMessageReceived(struct lws * wsi, char const * data, size_t length) override;
void OnWritable(struct lws * wsi) override; void OnWritable(struct lws * wsi) override;
private: void SendMessage(char const * message);
void SendMessage(json_t * message); void SendMessage(json_t * message);
private:
static void Run(Private * self); static void Run(Private * self);
IIvokationHandler & handler_; IIvokationHandler & handler_;
@ -127,6 +128,17 @@ std::string const & WsServer2::GetUrl() const
return d->GetUrl(); return d->GetUrl();
} }
void WsServer2::SendMessage(char const * message)
{
d->SendMessage(message);
}
void WsServer2::SendMessage(json_t * message)
{
d->SendMessage(message);
}
WsServer2::Private::Private( WsServer2::Private::Private(
IIvokationHandler & handler, IIvokationHandler & handler,
std::string const & protocol, std::string const & protocol,
@ -249,8 +261,7 @@ void WsServer2::Private::OnWritable(struct lws * wsi)
} }
} }
void WsServer2::Private::SendMessage(char const * message)
void WsServer2::Private::SendMessage(json_t * message)
{ {
lws * wsi = nullptr; lws * wsi = nullptr;
@ -259,10 +270,7 @@ void WsServer2::Private::SendMessage(json_t * message)
if (nullptr != wsi_) if (nullptr != wsi_)
{ {
char* message_text = json_dumps(message, JSON_COMPACT); writeQueue.push(message);
writeQueue.push(message_text);
json_decref(message);
free(message_text);
wsi = wsi_; wsi = wsi_;
} }
} }
@ -273,6 +281,14 @@ void WsServer2::Private::SendMessage(json_t * message)
} }
} }
void WsServer2::Private::SendMessage(json_t * message)
{
char* message_text = json_dumps(message, JSON_COMPACT);
SendMessage(message_text);
json_decref(message);
free(message_text);
}
void WsServer2::Private::OnMessageReceived(struct lws * wsi, char const * data, size_t length) void WsServer2::Private::OnMessageReceived(struct lws * wsi, char const * data, size_t length)
{ {
(void) wsi; (void) wsi;

View File

@ -27,6 +27,8 @@ public:
virtual ~WsServer2(); virtual ~WsServer2();
bool IsConnected(); bool IsConnected();
std::string const & GetUrl() const; std::string const & GetUrl() const;
void SendMessage(char const * message);
void SendMessage(json_t * message);
private: private:
class Private; class Private;
Private * d; Private * d;