Skip to content

Commit

Permalink
Implement Tls test
Browse files Browse the repository at this point in the history
  • Loading branch information
Iandiehard committed Jun 2, 2024
1 parent 3885eea commit 68eed82
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 17 deletions.
10 changes: 0 additions & 10 deletions .github/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,6 @@ apt-get install g++-11
BOOST_MAJOR_VERSION="1"
BOOST_MINOR_VERSION="79"

# Jfrog boost link is broken
# wget "https://boostorg.jfrog.io/artifactory/main/release/${BOOST_MAJOR_VERSION}.${BOOST_MINOR_VERSION}.0/source/boost_${BOOST_MAJOR_VERSION}_${BOOST_MINOR_VERSION}_0.tar.gz"
wget "https://sourceforge.net/projects/boost/files/boost/${BOOST_MAJOR_VERSION}.${BOOST_MINOR_VERSION}.0/boost_${BOOST_MAJOR_VERSION}_${BOOST_MINOR_VERSION}_0.tar.gz"
mkdir boost
tar -zxvf boost_${BOOST_MAJOR_VERSION}_${BOOST_MINOR_VERSION}_0.tar.gz -C boost
cd boost/boost_${BOOST_MAJOR_VERSION}_${BOOST_MINOR_VERSION}_0 || exit
./bootstrap.sh
./b2 variant=release install
cd ../../

# Install DLT daemon (needed for logging)
DLT_MAJOR_VERSION="2"
DLT_MINOR_VERSION="18"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class TcpMessage final {
/**
* @brief Type alias for underlying buffer
*/
using BufferType = std::vector<uint8_t>;
using BufferType = std::vector<std::uint8_t>;

/**
* @brief Type alias of IP address type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,27 @@
namespace boost_support {
namespace server {
namespace tls {

namespace detail {
/**
* @brief Template type for Tls version
* @tparam CipherSuite
* The supported cipher suites in corresponding tls version
*/
template<typename CipherSuite>
struct TlsVersion {
std::initializer_list<CipherSuite> cipher_suites{};
};
} // namespace detail

/**
* @brief Strong type for TLS version 1.2
*/
using TlsVersion12 = TlsVersion<Tls12CipherSuites>;
using TlsVersion12 = detail::TlsVersion<Tls12CipherSuites>;

/**
* @brief Strong type for TLS version 1.3
*/
using TlsVersion13 = TlsVersion<Tls13CipherSuites>;
using TlsVersion13 = detail::TlsVersion<Tls13CipherSuites>;

} // namespace tls
} // namespace server
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ core_type::Result<void> TlsClient<TlsVersion>::Transmit(MessageConstPtr tcp_mess

template class TlsClient<TlsVersion13>;
template class TlsClient<TlsVersion12>;

} // namespace tls
} // namespace client
} // namespace boost_support
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <boost/asio.hpp>

#include "boost-support/common/logger.h"
#include "boost-support/server/tls/tls_version.h"
#include "boost-support/socket/tls/tls_context.h"
#include "boost-support/socket/tls/tls_socket.h"

Expand Down Expand Up @@ -81,8 +82,8 @@ class TlsAcceptor<TlsVersion>::TlsAcceptorImpl final {
tls_server.emplace(TlsSocket{std::move(accepted_socket), tls_context_});
common::logger::LibBoostLogger::GetLibBoostLogger().GetLogger().LogDebug(
__FILE__, __LINE__, __func__, [&endpoint](std::stringstream &msg) {
msg << "Tls socket connection received from client "
<< "<" << endpoint.address().to_string() << "," << endpoint.port() << ">";
msg << "Tls socket connection received from client " << "<" << endpoint.address().to_string() << ","
<< endpoint.port() << ">";
});
} else {
common::logger::LibBoostLogger::GetLibBoostLogger().GetLogger().LogError(
Expand Down Expand Up @@ -130,6 +131,9 @@ std::optional<TlsServer> TlsAcceptor<TlsVersion>::GetTlsServer() noexcept {
return tls_acceptor_impl_->GetTlsServer();
}

template class TlsAcceptor<TlsVersion13>;
template class TlsAcceptor<TlsVersion12>;

} // namespace tls
} // namespace server
} // namespace boost_support
2 changes: 1 addition & 1 deletion diag-client-lib/lib/doip-client/common/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
*/
#ifndef DIAGNOSTIC_CLIENT_LIB_LIB_DOIP_CLIENT_COMMON_LOGGER_H
#define DIAGNOSTIC_CLIENT_LIB_LIB_DOIP_CLIENT_COMMON_LOGGER_H

Expand Down
134 changes: 134 additions & 0 deletions test/component/test_cases/tls_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/* Diagnostic Client library
* Copyright (C) 2024 Avijit Dey
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include <future>
#include <optional>

#include "boost-support/client/tls/tls_cipher_list.h"
#include "boost-support/client/tls/tls_client.h"
#include "boost-support/client/tls/tls_version.h"
#include "boost-support/server/tls/tls_acceptor.h"
#include "boost-support/server/tls/tls_cipher_list.h"
#include "boost-support/server/tls/tls_server.h"
#include "boost-support/server/tls/tls_version.h"
#include "component_test.h"

namespace test {
namespace component {
namespace test_cases {

// Tls Server Tcp Ip Address
constexpr std::string_view kTlsServerIpAddress{"172.16.25.128"};
// Tls Server port number
constexpr std::uint16_t kTlsServerTcpPortNum{3496U};
// Tls client Tcp Ip Address
constexpr std::string_view kTlsClientIpAddress{"172.16.25.127"};
// Tls client port number
constexpr std::uint16_t kTlsClientTcpPortNum{3496U};
// Certificate path
constexpr std::string_view kCertificatePath{};
// Private key path
constexpr std::string_view kPrivateKeyPath{};
// CA certificate path
constexpr std::string_view kCACertificatePath{};

/*!
* @brief Test fixture to test tls 1.2
*/
class Tls12Fixture : public component::ComponentTest {
public:
// Type Alias of acceptor
using TlsAcceptor = boost_support::server::tls::TlsAcceptor12;
// Type Alias of server
using TlsServer = boost_support::server::tls::TlsServer;
// Type Alias of client
using TlsClient = boost_support::client::tls::TlsClient12;
// Type Alias of tls server cipher suites version 1.2
using TlsServerCipherSuite = boost_support::server::tls::Tls12CipherSuites;
// Type Alias of tls server version
using TlsServerVersion = boost_support::server::tls::TlsVersion12;
// Type Alias of tls client cipher suites version 1.2
using TlsClientCipherSuite = boost_support::client::tls::Tls12CipherSuites;
// Type Alias of tls client version
using TlsClientVersion = boost_support::client::tls::TlsVersion12;

protected:
Tls12Fixture()
: tls_acceptor_{kTlsServerIpAddress,
kTlsServerTcpPortNum,
1u,
TlsServerVersion{{TlsServerCipherSuite::TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
TlsServerCipherSuite ::TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256}},
kCertificatePath,
kPrivateKeyPath},
tls_server_{},
tls_client_{kTlsClientIpAddress, kTlsClientTcpPortNum, kCACertificatePath,
TlsClientVersion{{TlsClientCipherSuite::TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
TlsClientCipherSuite::TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256}}} {}

void SetUp() override { tls_client_.Initialize(); }

void TearDown() override {
if (tls_server_.has_value()) { tls_server_->DeInitialize(); }
tls_client_.DeInitialize();
}

template<typename Functor>
auto CreateServerWithExpectation(Functor expectation_functor) noexcept -> std::future<bool> {
return std::async(std::launch::async, [this, expectation_functor = std::move(expectation_functor)]() {
std::optional<TlsServer> server{tls_acceptor_.GetTlsServer()};
if (server.has_value()) {
tls_server_.emplace(std::move(server).value());
tls_server_->Initialize();
// Set Expectation
expectation_functor();
}
return tls_server_.has_value();
});
}

protected:
// Tls acceptor
TlsAcceptor tls_acceptor_;
// Tls Server
std::optional<TlsServer> tls_server_;
// Tls client with Tls version 1.2
TlsClient tls_client_;
};

/**
* @brief Verify that sending of data from tls client to server works.
*/
TEST_F(Tls12Fixture, SendDataFromClientToServer) {
std::vector<std::uint8_t> const kTestData{1u, 2u, 3u, 4u, 5u, 6u};

std::future<bool> is_server_created{CreateServerWithExpectation([this, &kTestData]() {
// Create expectation that Receive Handler is invoked with same data
tls_server_->SetReadHandler([&kTestData](TlsServer::MessagePtr message) {
EXPECT_THAT(kTestData, ::testing::ElementsAreArray(message->GetPayload()));
});
})};

ASSERT_TRUE(is_server_created.get());
// Try connecting to server and verify
EXPECT_TRUE(tls_client_.ConnectToHost(kTlsServerIpAddress, kTlsServerTcpPortNum).HasValue());
EXPECT_TRUE(tls_client_.IsConnectedToHost());
// Send test data to tls server
EXPECT_TRUE(
tls_client_.Transmit(std::make_unique<TlsClient::Message>(kTlsServerIpAddress, kTlsServerTcpPortNum, kTestData))
.HasValue());
}

TEST_F(Tls12Fixture, SendDataFromServerToClient) {}

} // namespace test_cases
} // namespace component
} // namespace test

0 comments on commit 68eed82

Please sign in to comment.