Skip to content

Commit 8691b83

Browse files
Test PerfdataWriterConnection TLS connections as localhost
Also add a test case that tests host name verification fails with a different host name than on the certificate (here "127.0.0.1" instead of "localhost").
1 parent 8a7caed commit 8691b83

2 files changed

Lines changed: 36 additions & 3 deletions

File tree

test/perfdata-perfdatatargetfixture.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class PerfdataWriterTargetFixture
8282
void ResetStream()
8383
{
8484
if (std::holds_alternative<Shared<AsioTlsStream>::Ptr>(m_Stream)) {
85-
m_Stream = Shared<AsioTlsStream>::Make(IoEngine::Get().GetIoContext(), *m_SslContext);
85+
m_Stream = Shared<AsioTlsStream>::Make(IoEngine::Get().GetIoContext(), *m_SslContext, "localhost");
8686
} else {
8787
m_Stream = Shared<AsioTcpStream>::Make(IoEngine::Get().GetIoContext());
8888
}

test/perfdata-perfdatawriterconnection.cpp

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: GPL-3.0-or-later
33

44
#include "perfdata/perfdatawriterconnection.hpp"
5+
#include "test/base-testloggerfixture.hpp"
56
#include "test/perfdata-perfdatatargetfixture.hpp"
67
#include "test/remote-certificate-fixture.hpp"
78
#include "test/test-ctest.hpp"
@@ -10,17 +11,21 @@
1011

1112
using namespace icinga;
1213

13-
class TlsPerfdataWriterFixture : public CertificateFixture, public PerfdataWriterTargetFixture
14+
class TlsPerfdataWriterFixture : public CertificateFixture, public PerfdataWriterTargetFixture, public TestLoggerFixture
1415
{
1516
public:
1617
TlsPerfdataWriterFixture() : PerfdataWriterTargetFixture(MakeContext("server"))
1718
{
1819
m_PdwSslContext = MakeContext("client");
1920

20-
m_Conn = new PerfdataWriterConnection{"Test", "test", "127.0.0.1", std::to_string(GetPort()), m_PdwSslContext};
21+
ResetConnection();
2122
}
2223

2324
auto& GetConnection() { return *m_Conn; }
25+
void ResetConnection(String host = "localhost")
26+
{
27+
m_Conn = new PerfdataWriterConnection{"Test", "test", std::move(host), std::to_string(GetPort()), m_PdwSslContext};
28+
}
2429

2530
static inline const std::vector<String> RequiredCerts{"client", "server"};
2631

@@ -66,6 +71,34 @@ BOOST_AUTO_TEST_CASE(connection_refused)
6671
REQUIRE_JOINS_WITHIN(timeoutThread, 1s);
6772
}
6873

74+
/* This tests that the connection attempt fails when the host can not be verified correctly.
75+
*/
76+
BOOST_AUTO_TEST_CASE(tls_verify_host)
77+
{
78+
/* The certificates are created with CN=localhost, so 127.0.0.1 should still connect
79+
* correctly, but fail the host name verification of the certificate.
80+
*/
81+
ResetConnection("127.0.0.1");
82+
83+
std::promise<void> p;
84+
TestThread timeoutThread{[&]() {
85+
Accept();
86+
Handshake();
87+
auto f = p.get_future();
88+
GetConnection().CancelAfterTimeout(f, 200ms);
89+
}};
90+
91+
BOOST_REQUIRE_EXCEPTION(
92+
GetConnection().Send(boost::asio::const_buffer{"foobar", 7}), std::exception, [](const std::exception& ex) {
93+
std::cout << "exception raised: " << ex.what() << std::endl;
94+
return true;
95+
}
96+
);
97+
98+
REQUIRE_JOINS_WITHIN(timeoutThread, 10s);
99+
REQUIRE_LOG_MESSAGE("Error while connecting .*?: [hH]ostname mismatch", 10s);
100+
}
101+
69102
/* The PerfdataWriterConnection connects automatically when sending the first data.
70103
* In case of http we also need to support disconnecting and reconnecting.
71104
*/

0 commit comments

Comments
 (0)