|
2 | 2 | // SPDX-License-Identifier: GPL-3.0-or-later |
3 | 3 |
|
4 | 4 | #include "perfdata/perfdatawriterconnection.hpp" |
| 5 | +#include "test/base-testloggerfixture.hpp" |
5 | 6 | #include "test/perfdata-perfdatatargetfixture.hpp" |
6 | 7 | #include "test/remote-certificate-fixture.hpp" |
7 | 8 | #include "test/test-ctest.hpp" |
|
10 | 11 |
|
11 | 12 | using namespace icinga; |
12 | 13 |
|
13 | | -class TlsPerfdataWriterFixture : public CertificateFixture, public PerfdataWriterTargetFixture |
| 14 | +class TlsPerfdataWriterFixture : public CertificateFixture, public PerfdataWriterTargetFixture, public TestLoggerFixture |
14 | 15 | { |
15 | 16 | public: |
16 | 17 | TlsPerfdataWriterFixture() : PerfdataWriterTargetFixture(MakeContext("server")) |
17 | 18 | { |
18 | 19 | m_PdwSslContext = MakeContext("client"); |
19 | 20 |
|
20 | | - m_Conn = new PerfdataWriterConnection{"Test", "test", "127.0.0.1", std::to_string(GetPort()), m_PdwSslContext}; |
| 21 | + ResetConnection(); |
21 | 22 | } |
22 | 23 |
|
23 | 24 | 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 | + } |
24 | 29 |
|
25 | 30 | static inline const std::vector<String> RequiredCerts{"client", "server"}; |
26 | 31 |
|
@@ -66,6 +71,34 @@ BOOST_AUTO_TEST_CASE(connection_refused) |
66 | 71 | REQUIRE_JOINS_WITHIN(timeoutThread, 1s); |
67 | 72 | } |
68 | 73 |
|
| 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 | + |
69 | 102 | /* The PerfdataWriterConnection connects automatically when sending the first data. |
70 | 103 | * In case of http we also need to support disconnecting and reconnecting. |
71 | 104 | */ |
|
0 commit comments