Skip to content

Commit

Permalink
wsd: test: support SSL in classic tests
Browse files Browse the repository at this point in the history
Add support for configuring the SSL certificates
when running the test binary as well as setting
up the SSL context.

Also adds the SSL socket headers with proper
compile-time guards for when SSL is disabled.

Change-Id: I99992639a66a64871f8ff8a2b2105279ead63ca1
Signed-off-by: Ashod Nakashian <[email protected]>
  • Loading branch information
Ashod committed Jan 13, 2021
1 parent feac95a commit 06c5c90
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
4 changes: 2 additions & 2 deletions net/Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@

#include <SigUtil.hpp>
#include "ServerSocket.hpp"
#if !MOBILEAPP
#include "SslSocket.hpp"
#if !MOBILEAPP && ENABLE_SSL
#include <net/SslSocket.hpp>
#endif
#include "WebSocketHandler.hpp"

Expand Down
3 changes: 3 additions & 0 deletions net/Ssl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class SslContext

static void uninitialize();

/// Returns true iff the SslContext has been initialized.
static bool isInitialized() { return !!Instance; }

static SSL* newSsl()
{
return SSL_new(Instance->_ctx);
Expand Down
33 changes: 32 additions & 1 deletion test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@
#include <helpers.hpp>
#include <Unit.hpp>
#include <wsd/LOOLWSD.hpp>

#if ENABLE_SSL
#include <Ssl.hpp>
#include <SslSocket.hpp>
#endif
#include <Log.hpp>

#include "common/Protocol.hpp"
Expand Down Expand Up @@ -73,6 +76,7 @@ static bool IsDebugrun = false;
int main(int argc, char** argv)
{
bool verbose = false;
std::string cert_path = "/etc/loolwsd/";
for (int i = 1; i < argc; ++i)
{
const std::string arg(argv[i]);
Expand All @@ -84,11 +88,38 @@ int main(int argc, char** argv)
{
IsDebugrun = true;
}
else if (arg == "--cert-path" && ++i < argc)
{
cert_path = argv[i];
}
}

const char* loglevel = verbose ? "trace" : "warning";
Log::initialize("tst", loglevel, true, false, {});

#if ENABLE_SSL
try
{
// The most likely place. If not found, SSL will be disabled in the tests.
const std::string ssl_cert_file_path = cert_path + "/cert.pem";
const std::string ssl_key_file_path = cert_path + "/key.pem";
const std::string ssl_ca_file_path = cert_path + "/ca-chain.cert.pem";
const std::string ssl_cipher_list = "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH";

// Initialize the non-blocking socket SSL.
SslContext::initialize(ssl_cert_file_path, ssl_key_file_path, ssl_ca_file_path,
ssl_cipher_list);
}
catch (const std::exception& ex)
{
LOG_ERR("Exception while initializing SslContext: " << ex.what());
}

if (!SslContext::isInitialized())
LOG_ERR("Failed to initialize SSL. Set the path to the certificates via --cert-path. "
"HTTPS tests will be disabled in unit-tests.");
#endif

return runClientTests(true, verbose)? 0: 1;
}

Expand Down
4 changes: 3 additions & 1 deletion wsd/Admin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
#include <Util.hpp>

#include <net/Socket.hpp>
#include <net/SslSocket.hpp>
#if ENABLE_SSL
#include <SslSocket.hpp>
#endif
#include <net/WebSocketHandler.hpp>

#include <common/SigUtil.hpp>
Expand Down

0 comments on commit 06c5c90

Please sign in to comment.