From 68eed82dde1b90b2ac4b587535e733656fd0ce21 Mon Sep 17 00:00:00 2001 From: Avijit Dey Date: Fri, 29 Mar 2024 12:14:17 +0100 Subject: [PATCH] Implement Tls test --- .github/setup.sh | 10 -- .../boost-support/message/tcp/tcp_message.h | 2 +- .../boost-support/server/tls/tls_version.h | 12 +- .../boost-support/client/tls/tls_client.cpp | 1 + .../boost-support/server/tls/tls_acceptor.cpp | 8 +- .../lib/doip-client/common/logger.h | 2 +- test/component/test_cases/tls_test.cpp | 134 ++++++++++++++++++ 7 files changed, 152 insertions(+), 17 deletions(-) create mode 100644 test/component/test_cases/tls_test.cpp diff --git a/.github/setup.sh b/.github/setup.sh index eb1342c4..facf0486 100644 --- a/.github/setup.sh +++ b/.github/setup.sh @@ -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" diff --git a/diag-client-lib/lib/boost-support/include/boost-support/message/tcp/tcp_message.h b/diag-client-lib/lib/boost-support/include/boost-support/message/tcp/tcp_message.h index 9098e565..cf453f06 100644 --- a/diag-client-lib/lib/boost-support/include/boost-support/message/tcp/tcp_message.h +++ b/diag-client-lib/lib/boost-support/include/boost-support/message/tcp/tcp_message.h @@ -46,7 +46,7 @@ class TcpMessage final { /** * @brief Type alias for underlying buffer */ - using BufferType = std::vector; + using BufferType = std::vector; /** * @brief Type alias of IP address type diff --git a/diag-client-lib/lib/boost-support/include/boost-support/server/tls/tls_version.h b/diag-client-lib/lib/boost-support/include/boost-support/server/tls/tls_version.h index 0a91fb38..0ec3563e 100644 --- a/diag-client-lib/lib/boost-support/include/boost-support/server/tls/tls_version.h +++ b/diag-client-lib/lib/boost-support/include/boost-support/server/tls/tls_version.h @@ -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 struct TlsVersion { std::initializer_list cipher_suites{}; }; +} // namespace detail /** * @brief Strong type for TLS version 1.2 */ -using TlsVersion12 = TlsVersion; +using TlsVersion12 = detail::TlsVersion; /** * @brief Strong type for TLS version 1.3 */ -using TlsVersion13 = TlsVersion; +using TlsVersion13 = detail::TlsVersion; } // namespace tls } // namespace server diff --git a/diag-client-lib/lib/boost-support/src/boost-support/client/tls/tls_client.cpp b/diag-client-lib/lib/boost-support/src/boost-support/client/tls/tls_client.cpp index 108af460..14d7be31 100644 --- a/diag-client-lib/lib/boost-support/src/boost-support/client/tls/tls_client.cpp +++ b/diag-client-lib/lib/boost-support/src/boost-support/client/tls/tls_client.cpp @@ -247,6 +247,7 @@ core_type::Result TlsClient::Transmit(MessageConstPtr tcp_mess template class TlsClient; template class TlsClient; + } // namespace tls } // namespace client } // namespace boost_support diff --git a/diag-client-lib/lib/boost-support/src/boost-support/server/tls/tls_acceptor.cpp b/diag-client-lib/lib/boost-support/src/boost-support/server/tls/tls_acceptor.cpp index b1685b42..28e709c0 100644 --- a/diag-client-lib/lib/boost-support/src/boost-support/server/tls/tls_acceptor.cpp +++ b/diag-client-lib/lib/boost-support/src/boost-support/server/tls/tls_acceptor.cpp @@ -12,6 +12,7 @@ #include #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" @@ -81,8 +82,8 @@ class TlsAcceptor::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( @@ -130,6 +131,9 @@ std::optional TlsAcceptor::GetTlsServer() noexcept { return tls_acceptor_impl_->GetTlsServer(); } +template class TlsAcceptor; +template class TlsAcceptor; + } // namespace tls } // namespace server } // namespace boost_support diff --git a/diag-client-lib/lib/doip-client/common/logger.h b/diag-client-lib/lib/doip-client/common/logger.h index e63f56fa..cfb1bd9c 100644 --- a/diag-client-lib/lib/doip-client/common/logger.h +++ b/diag-client-lib/lib/doip-client/common/logger.h @@ -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 diff --git a/test/component/test_cases/tls_test.cpp b/test/component/test_cases/tls_test.cpp new file mode 100644 index 00000000..a77d4ba4 --- /dev/null +++ b/test/component/test_cases/tls_test.cpp @@ -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 +#include + +#include +#include + +#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 + auto CreateServerWithExpectation(Functor expectation_functor) noexcept -> std::future { + return std::async(std::launch::async, [this, expectation_functor = std::move(expectation_functor)]() { + std::optional 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 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 const kTestData{1u, 2u, 3u, 4u, 5u, 6u}; + + std::future 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(kTlsServerIpAddress, kTlsServerTcpPortNum, kTestData)) + .HasValue()); +} + +TEST_F(Tls12Fixture, SendDataFromServerToClient) {} + +} // namespace test_cases +} // namespace component +} // namespace test