diff --git a/test/webfuse/tests/adapter/test_client.cc b/test/webfuse/tests/adapter/test_client.cc index 8d39f6c..1002187 100644 --- a/test/webfuse/tests/adapter/test_client.cc +++ b/test/webfuse/tests/adapter/test_client.cc @@ -2,7 +2,7 @@ #include #include "webfuse/utils/adapter_client.hpp" - +#include "webfuse/adapter/client_tlsconfig.h" #include "webfuse/adapter/credentials.h" #include "webfuse/core/protocol_names.h" #include "webfuse/utils/ws_server2.hpp" @@ -87,6 +87,44 @@ TEST(AdapterClient, Connect) ASSERT_TRUE(watcher.waitUntil([&]() mutable { return disconnected; })); } +TEST(AdapterClient, ConnectWithTls) +{ + TimeoutWatcher watcher(TIMEOUT); + + MockInvokationHander handler; + WsServer2 server(handler, WF_PROTOCOL_NAME_PROVIDER_SERVER, 0, true); + 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) + .WillOnce(Invoke([](wf_client *, int, void * arg) { + auto * tls = reinterpret_cast(arg); + wf_client_tlsconfig_set_keypath (tls, "client-key.pem"); + wf_client_tlsconfig_set_certpath(tls, "client-cert.pem"); + wf_client_tlsconfig_set_cafilepath(tls, "server-cert.pem"); + })); + 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; })); + + client.Disconnect(); + ASSERT_TRUE(watcher.waitUntil([&]() mutable { return disconnected; })); +} + + TEST(AdapterClient, Authenticate) { TimeoutWatcher watcher(TIMEOUT); diff --git a/test/webfuse/utils/ws_server2.cc b/test/webfuse/utils/ws_server2.cc index df1b772..7cb28f1 100644 --- a/test/webfuse/utils/ws_server2.cc +++ b/test/webfuse/utils/ws_server2.cc @@ -75,7 +75,7 @@ class WsServer2::Private : IServer Private(Private const &) = delete; Private & operator=(Private const &) = delete; public: - Private(IIvokationHandler & handler, std::string const & protocol, int port); + Private(IIvokationHandler & handler, std::string const & protocol, int port, bool enable_tls); ~Private(); bool IsConnected(); std::string const & GetUrl() const; @@ -105,8 +105,9 @@ private: WsServer2::WsServer2( IIvokationHandler& handler, std::string const & protocol, - int port) -: d(new WsServer2::Private(handler, protocol, port)) + int port, + bool enable_tls) +: d(new WsServer2::Private(handler, protocol, port, enable_tls)) { } @@ -129,7 +130,8 @@ std::string const & WsServer2::GetUrl() const WsServer2::Private::Private( IIvokationHandler & handler, std::string const & protocol, - int port) + int port, + bool enable_tls) : handler_(handler) , protocol_(protocol) , is_connected(false) @@ -154,11 +156,19 @@ WsServer2::Private::Private( info.options = LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE; info.options |= LWS_SERVER_OPTION_EXPLICIT_VHOSTS; + if (enable_tls) + { + info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT; + info.ssl_cert_filepath = "server-cert.pem"; + info.ssl_private_key_filepath = "server-key.pem"; + } + ws_context = lws_create_context(&info); std::ostringstream stream; struct lws_vhost * vhost = lws_create_vhost(ws_context, &info); - stream << "ws://localhost:" << lws_get_vhost_port(vhost) << "/"; + stream << (enable_tls ? "wss://" : "ws://") + << "localhost:" << lws_get_vhost_port(vhost) << "/"; url = stream.str(); context = std::thread(&Run, this); diff --git a/test/webfuse/utils/ws_server2.hpp b/test/webfuse/utils/ws_server2.hpp index e19c47d..b09c573 100644 --- a/test/webfuse/utils/ws_server2.hpp +++ b/test/webfuse/utils/ws_server2.hpp @@ -22,7 +22,8 @@ public: WsServer2( IIvokationHandler& handler, std::string const & protocol, - int port = 0); + int port = 0, + bool enable_tls = false); virtual ~WsServer2(); bool IsConnected(); std::string const & GetUrl() const;