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

added test to connect adapter client via TLS

This commit is contained in:
Falk Werner 2020-06-14 19:35:50 +02:00
parent 71956c4574
commit 8de8ec0003
3 changed files with 56 additions and 7 deletions

View File

@ -2,7 +2,7 @@
#include <gmock/gmock.h>
#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<wf_client_tlsconfig*>(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);

View File

@ -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);

View File

@ -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;