diff --git a/test/webfuse/tests/adapter/test_client.cc b/test/webfuse/tests/adapter/test_client.cc index abafa52..78cf7b2 100644 --- a/test/webfuse/tests/adapter/test_client.cc +++ b/test/webfuse/tests/adapter/test_client.cc @@ -87,6 +87,74 @@ TEST(AdapterClient, Connect) 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) { TimeoutWatcher watcher(TIMEOUT); diff --git a/test/webfuse/utils/ws_server2.cc b/test/webfuse/utils/ws_server2.cc index 7cb28f1..21d4a6e 100644 --- a/test/webfuse/utils/ws_server2.cc +++ b/test/webfuse/utils/ws_server2.cc @@ -70,7 +70,7 @@ static int wf_test_utils_ws_server_callback( namespace webfuse_test { -class WsServer2::Private : IServer +class WsServer2::Private : public IServer { Private(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 OnWritable(struct lws * wsi) override; -private: + void SendMessage(char const * message); void SendMessage(json_t * message); +private: static void Run(Private * self); IIvokationHandler & handler_; @@ -127,6 +128,17 @@ std::string const & WsServer2::GetUrl() const return d->GetUrl(); } +void WsServer2::SendMessage(char const * message) +{ + d->SendMessage(message); +} + +void WsServer2::SendMessage(json_t * message) +{ + d->SendMessage(message); +} + + WsServer2::Private::Private( IIvokationHandler & handler, std::string const & protocol, @@ -249,8 +261,7 @@ void WsServer2::Private::OnWritable(struct lws * wsi) } } - -void WsServer2::Private::SendMessage(json_t * message) +void WsServer2::Private::SendMessage(char const * message) { lws * wsi = nullptr; @@ -259,10 +270,7 @@ void WsServer2::Private::SendMessage(json_t * message) if (nullptr != wsi_) { - char* message_text = json_dumps(message, JSON_COMPACT); - writeQueue.push(message_text); - json_decref(message); - free(message_text); + writeQueue.push(message); 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) wsi; diff --git a/test/webfuse/utils/ws_server2.hpp b/test/webfuse/utils/ws_server2.hpp index b09c573..b29ffb1 100644 --- a/test/webfuse/utils/ws_server2.hpp +++ b/test/webfuse/utils/ws_server2.hpp @@ -27,6 +27,8 @@ public: virtual ~WsServer2(); bool IsConnected(); std::string const & GetUrl() const; + void SendMessage(char const * message); + void SendMessage(json_t * message); private: class Private; Private * d;