• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.35
/src/tests/test_tls_stream_integration.cpp
1
/*
2
* TLS ASIO Stream Client-Server Interaction Test
3
* (C) 2018-2020 Jack Lloyd
4
*     2018-2020 Hannes Rantzsch, René Meusel
5
*     2022      René Meusel, Rohde & Schwarz Cybersecurity
6
*
7
* Botan is released under the Simplified BSD License (see license.txt)
8
*/
9

10
#include "tests.h"
11

12
#if defined(BOTAN_HAS_TLS) && defined(BOTAN_HAS_TLS_ASIO_STREAM) && defined(BOTAN_TARGET_OS_HAS_THREADS)
13

14
   // first version to be compatible with Networking TS (N4656) and boost::beast
15
   #include <boost/version.hpp>
16
   #if BOOST_VERSION >= 106600
17

18
      #include <functional>
19
      #include <optional>
20
      #include <thread>
21

22
      #include <botan/asio_stream.h>
23
      #include <botan/auto_rng.h>
24
      #include <botan/tls_session_manager_noop.h>
25

26
      #include <boost/asio.hpp>
27
      #include <boost/exception/exception.hpp>
28

29
      #include "../cli/tls_helpers.h"  // for Basic_Credentials_Manager
30

31
namespace {
32

33
namespace net = boost::asio;
34

35
using tcp = net::ip::tcp;
36
using error_code = boost::system::error_code;
37
using ssl_stream = Botan::TLS::Stream<net::ip::tcp::socket>;
38
using namespace std::placeholders;
39
using Result = Botan_Tests::Test::Result;
40

41
static const auto k_timeout = std::chrono::seconds(30);
42
static const auto k_endpoints = std::vector<tcp::endpoint>{tcp::endpoint{net::ip::make_address("127.0.0.1"), 8082}};
43

44
enum { max_msg_length = 512 };
45

46
static std::string server_cert() { return Botan_Tests::Test::data_dir() + "/x509/certstor/cert1.crt"; }
36✔
47

48
static std::string server_key() { return Botan_Tests::Test::data_dir() + "/x509/certstor/key01.pem"; }
36✔
49

50
class Timeout_Exception : public std::runtime_error {
51
      using std::runtime_error::runtime_error;
×
52
};
53

54
class Peer {
55
   public:
56
      Peer(std::shared_ptr<const Botan::TLS::Policy> policy, net::io_context& ioc) :
36✔
57
            m_rng(std::make_shared<Botan::AutoSeeded_RNG>()),
58
            m_credentials_manager(std::make_shared<Basic_Credentials_Manager>(true, "")),
36✔
59
            m_session_mgr(std::make_shared<Botan::TLS::Session_Manager_Noop>()),
60
            m_ctx(std::make_shared<Botan::TLS::Context>(m_credentials_manager, m_rng, m_session_mgr, policy)),
36✔
61
            m_timeout_timer(ioc) {}
108✔
62

63
      Peer(std::shared_ptr<const Botan::TLS::Policy> policy,
36✔
64
           net::io_context& ioc,
65
           const std::string& server_cert,
66
           const std::string& server_key) :
36✔
67
            m_rng(std::make_shared<Botan::AutoSeeded_RNG>()),
36✔
68
            m_credentials_manager(std::make_shared<Basic_Credentials_Manager>(server_cert, server_key)),
36✔
69
            m_session_mgr(std::make_shared<Botan::TLS::Session_Manager_Noop>()),
70
            m_ctx(std::make_shared<Botan::TLS::Context>(m_credentials_manager, m_rng, m_session_mgr, policy)),
36✔
71
            m_timeout_timer(ioc) {}
108✔
72

73
      virtual ~Peer() { cancel_timeout(); }
432✔
74

75
      net::mutable_buffer buffer() { return net::buffer(m_data, max_msg_length); }
36✔
76

77
      net::mutable_buffer buffer(size_t size) { return net::buffer(m_data, size); }
16✔
78

79
      std::string message() const { return std::string(m_data); }
12✔
80

81
      // This is a CompletionCondition for net::async_read().
82
      // Our toy protocol always expects a single \0-terminated string.
83
      std::size_t received_zero_byte(const boost::system::error_code& error, std::size_t bytes_transferred) {
152✔
84
         if(error) {
152✔
85
            return 0;
86
         }
87

88
         if(bytes_transferred > 0 && m_data[bytes_transferred - 1] == '\0') {
116✔
89
            return 0;
90
         }
91

92
         return max_msg_length - bytes_transferred;
76✔
93
      }
94

95
      void on_timeout(std::function<void(const std::string&)> cb) { m_on_timeout = std::move(cb); }
72✔
96

97
      void reset_timeout(std::string message) {
296✔
98
         m_timeout_timer.expires_after(k_timeout);
296✔
99
         m_timeout_timer.async_wait([=, this](const error_code& ec) {
2,104✔
100
            if(ec != net::error::operation_aborted)  // timer cancelled
296✔
101
            {
102
               if(m_on_timeout) {
×
103
                  m_on_timeout(message);
×
104
               }
105

106
               // hard-close the underlying transport to maximize the
107
               // probability for all participating peers to error out
108
               // of their pending I/O operations
109
               if(m_stream) {
×
110
                  m_stream->lowest_layer().close();
×
111
               }
112

113
               throw Timeout_Exception("timeout occured: " + message);
×
114
            }
115
         });
296✔
116
      }
296✔
117

118
      void cancel_timeout() { m_timeout_timer.cancel(); }
108✔
119

120
   protected:
121
      std::shared_ptr<Botan::AutoSeeded_RNG> m_rng;
122
      std::shared_ptr<Basic_Credentials_Manager> m_credentials_manager;
123
      std::shared_ptr<Botan::TLS::Session_Manager_Noop> m_session_mgr;
124
      std::shared_ptr<Botan::TLS::Context> m_ctx;
125
      std::unique_ptr<ssl_stream> m_stream;
126
      net::system_timer m_timeout_timer;
127
      std::function<void(const std::string&)> m_on_timeout;
128

129
      char m_data[max_msg_length];
130
};
131

132
class Result_Wrapper {
36✔
133
   public:
134
      Result_Wrapper(std::string name) : m_result(std::move(name)) {}
144✔
135

136
      Result& result() { return m_result; }
72✔
137

138
      void expect_success(const std::string& msg, const error_code& ec) {
352✔
139
         error_code success;
352✔
140
         expect_ec(msg, success, ec);
352✔
141
      }
142

143
      void expect_ec(const std::string& msg, const error_code& expected, const error_code& ec) {
408✔
144
         if(ec != expected) {
408✔
145
            m_result.test_failure(msg, "Unexpected error code: " + ec.message());
×
146
         } else {
147
            m_result.test_success(msg);
408✔
148
         }
149
      }
408✔
150

151
      void confirm(const std::string& msg, bool condition) { m_result.confirm(msg, condition); }
48✔
152

153
      void test_failure(const std::string& msg) { m_result.test_failure(msg); }
×
154

155
   private:
156
      Result m_result;
157
};
158

159
class Server : public Peer,
160
               public std::enable_shared_from_this<Server> {
161
   public:
162
      Server(std::shared_ptr<const Botan::TLS::Policy> policy, net::io_context& ioc, std::string test_name) :
36✔
163
            Peer(policy, ioc, server_cert(), server_key()),
36✔
164
            m_acceptor(ioc),
72✔
165
            m_result("Server (" + test_name + ")"),
36✔
166
            m_short_read_expected(false),
36✔
167
            m_move_before_accept(false) {
144✔
168
         reset_timeout("startup");
36✔
169
      }
36✔
170

171
      // Control messages
172
      // The messages below can be used by the test clients in order to configure the server's behavior during a test
173
      // case.
174
      //
175
      // Tell the server that the next read should result in a StreamTruncated error
176
      std::string expect_short_read_message = "SHORT_READ";
177
      // Prepare the server for the test case "Shutdown No Response"
178
      std::string prepare_shutdown_no_response_message = "SHUTDOWN_NOW";
179

180
      void listen() {
36✔
181
         error_code ec;
36✔
182
         const auto endpoint = k_endpoints.back();
36✔
183

184
         m_acceptor.open(endpoint.protocol(), ec);
36✔
185
         m_acceptor.set_option(net::socket_base::reuse_address(true), ec);
36✔
186
         m_acceptor.bind(endpoint, ec);
36✔
187
         m_acceptor.listen(net::socket_base::max_listen_connections, ec);
36✔
188

189
         m_result.expect_success("listen", ec);
72✔
190

191
         reset_timeout("accept");
36✔
192
         m_acceptor.async_accept(std::bind(&Server::start_session, shared_from_this(), _1, _2));
72✔
193
      }
36✔
194

195
      void expect_short_read() { m_short_read_expected = true; }
4✔
196

197
      void move_before_accept() { m_move_before_accept = true; }
4✔
198

199
      Result_Wrapper result() { return m_result; }
36✔
200

201
   private:
202
      void start_session(const error_code& ec, tcp::socket socket) {
36✔
203
         // Note: If this fails with 'Operation canceled', it likely means the timer expired and the port is taken.
204
         m_result.expect_success("accept", ec);
72✔
205

206
         // Note: If this was a real server, we should create a new session (with its own stream) for each accepted
207
         // connection. In this test we only have one connection.
208

209
         if(m_move_before_accept) {
36✔
210
            // regression test for #2635
211
            ssl_stream s(std::move(socket), m_ctx);
4✔
212
            m_stream = std::unique_ptr<ssl_stream>(new ssl_stream(std::move(s)));
4✔
213
         } else {
4✔
214
            m_stream = std::unique_ptr<ssl_stream>(new ssl_stream(std::move(socket), m_ctx));
64✔
215
         }
216

217
         reset_timeout("handshake");
36✔
218
         m_stream->async_handshake(Botan::TLS::Connection_Side::Server,
72✔
219
                                   std::bind(&Server::handle_handshake, shared_from_this(), _1));
72✔
220
      }
36✔
221

222
      void shutdown() {
8✔
223
         error_code shutdown_ec;
8✔
224
         m_stream->shutdown(shutdown_ec);
8✔
225
         m_result.expect_success("shutdown", shutdown_ec);
16✔
226
         handle_write(error_code{});
8✔
227
      }
8✔
228

229
      void handle_handshake(const error_code& ec) {
36✔
230
         m_result.expect_success("handshake", ec);
72✔
231
         handle_write(error_code{});
36✔
232
      }
36✔
233

234
      void handle_write(const error_code& ec) {
60✔
235
         m_result.expect_success("send_response", ec);
120✔
236
         reset_timeout("read_message");
60✔
237
         net::async_read(*m_stream,
60✔
238
                         buffer(),
60✔
239
                         std::bind(&Server::received_zero_byte, shared_from_this(), _1, _2),
120✔
240
                         std::bind(&Server::handle_read, shared_from_this(), _1, _2));
120✔
241
      }
60✔
242

243
      void handle_read(const error_code& ec, size_t bytes_transferred = 0) {
60✔
244
         if(m_short_read_expected) {
60✔
245
            m_result.expect_ec("received stream truncated error", Botan::TLS::StreamTruncated, ec);
16✔
246
            cancel_timeout();
16✔
247
            return;
16✔
248
         }
249

250
         if(ec) {
44✔
251
            if(m_stream->shutdown_received()) {
20✔
252
               m_result.expect_ec("received EOF after close_notify", net::error::eof, ec);
20✔
253
               reset_timeout("shutdown");
20✔
254
               m_stream->async_shutdown(std::bind(&Server::handle_shutdown, shared_from_this(), _1));
20✔
255
            } else {
256
               m_result.test_failure("Unexpected error code: " + ec.message());
×
257
               cancel_timeout();
×
258
            }
259
            return;
20✔
260
         }
261

262
         m_result.expect_success("read_message", ec);
48✔
263

264
         if(message() == prepare_shutdown_no_response_message) {
36✔
265
            m_short_read_expected = true;
8✔
266
            shutdown();
8✔
267
            return;
8✔
268
         }
269

270
         if(message() == expect_short_read_message) {
28✔
271
            m_short_read_expected = true;
4✔
272
         }
273

274
         reset_timeout("send_response");
16✔
275
         net::async_write(
32✔
276
            *m_stream, buffer(bytes_transferred), std::bind(&Server::handle_write, shared_from_this(), _1));
16✔
277
      }
278

279
      void handle_shutdown(const error_code& ec) {
20✔
280
         m_result.expect_success("shutdown", ec);
40✔
281
         cancel_timeout();
20✔
282
      }
20✔
283

284
   private:
285
      tcp::acceptor m_acceptor;
286
      Result_Wrapper m_result;
287
      bool m_short_read_expected;
288

289
      // regression test for #2635
290
      bool m_move_before_accept;
291
};
292

293
class Client : public Peer {
36✔
294
      static void accept_all(const std::vector<Botan::X509_Certificate>&,
36✔
295
                             const std::vector<std::optional<Botan::OCSP::Response>>&,
296
                             const std::vector<Botan::Certificate_Store*>&,
297
                             Botan::Usage_Type,
298
                             std::string_view,
299
                             const Botan::TLS::Policy&) {}
36✔
300

301
   public:
302
      Client(std::shared_ptr<const Botan::TLS::Policy> policy, net::io_context& ioc) : Peer(policy, ioc) {
36✔
303
         m_ctx->set_verify_callback(accept_all);
36✔
304
         m_stream = std::unique_ptr<ssl_stream>(new ssl_stream(ioc, m_ctx));
36✔
305
      }
36✔
306

307
      ssl_stream& stream() { return *m_stream; }
164✔
308

309
      void close_socket() {
24✔
310
         // Shutdown on TCP level before closing the socket for portable behavior. Otherwise the peer will see a
311
         // connection_reset error rather than EOF on Windows.
312
         // See the remark in
313
         // https://www.boost.org/doc/libs/1_68_0/doc/html/boost_asio/reference/basic_stream_socket/close/overload1.html
314
         m_stream->lowest_layer().shutdown(tcp::socket::shutdown_both);
20✔
315
         m_stream->lowest_layer().close();
24✔
316
      }
12✔
317
};
318

319
      #include <boost/asio/yield.hpp>
320

321
class TestBase {
322
   public:
323
      TestBase(net::io_context& ioc,
36✔
324
               std::shared_ptr<const Botan::TLS::Policy> client_policy,
325
               std::shared_ptr<const Botan::TLS::Policy> server_policy,
326
               const std::string& name,
327
               const std::string& config_name) :
36✔
328
            m_name(name + " (" + config_name + ")"),
72✔
329
            m_client(std::make_shared<Client>(client_policy, ioc)),
36✔
330
            m_server(std::make_shared<Server>(server_policy, ioc, m_name)),
36✔
331
            m_result(m_name) {
108✔
332
         m_client->on_timeout(
36✔
333
            [=, this](const std::string& msg) { m_result.test_failure("timeout in client during: " + msg); });
×
334
         m_server->on_timeout(
36✔
335
            [=, this](const std::string& msg) { m_result.test_failure("timeout in server during: " + msg); });
×
336

337
         m_server->listen();
36✔
338
      }
36✔
339

340
      virtual ~TestBase() = default;
108✔
341

342
      virtual void finishAsynchronousWork() {}
20✔
343

344
      void fail(const std::string& msg) { m_result.test_failure(msg); }
×
345

346
      void extend_results(std::vector<Result>& results) {
36✔
347
         results.push_back(m_result.result());
36✔
348
         results.push_back(m_server->result().result());
36✔
349
      }
36✔
350

351
   protected:
352
      //! retire client and server instances after a job well done
353
      void teardown() { m_client->cancel_timeout(); }
24✔
354

355
   protected:
356
      std::string m_name;
357

358
      std::shared_ptr<Client> m_client;
359
      std::shared_ptr<Server> m_server;
360

361
      Result_Wrapper m_result;
362
};
363

364
class Synchronous_Test : public TestBase {
16✔
365
   public:
366
      using TestBase::TestBase;
16✔
367

368
      void finishAsynchronousWork() override { m_client_thread.join(); }
×
369

370
      void run(const error_code&) {
16✔
371
         m_client_thread = std::thread([this] {
16✔
372
            try {
16✔
373
               this->run_synchronous_client();
16✔
374
            } catch(const std::exception& ex) {
×
375
               m_result.test_failure(std::string("sync client failed with: ") + ex.what());
×
376
            } catch(const boost::exception&) {
×
377
               m_result.test_failure("sync client failed with boost exception");
×
378
            } catch(...) { m_result.test_failure("sync client failed with unknown error"); }
×
379
         });
32✔
380
      }
16✔
381

382
      virtual void run_synchronous_client() = 0;
383

384
   private:
385
      std::thread m_client_thread;
386
};
387

388
/* In this test case both parties perform the handshake, exchange a message, and do a full shutdown.
389
 *
390
 * The client expects the server to echo the same message it sent. The client then initiates the shutdown. The server is
391
 * expected to receive a close_notify and complete its shutdown with an error_code Success, the client is expected to
392
 * receive a close_notify and complete its shutdown with an error_code EOF.
393
 */
394
class Test_Conversation : public TestBase,
395
                          public net::coroutine,
396
                          public std::enable_shared_from_this<Test_Conversation> {
397
   public:
398
      Test_Conversation(net::io_context& ioc,
8✔
399
                        std::string config_name,
400
                        std::shared_ptr<const Botan::TLS::Policy> client_policy,
401
                        std::shared_ptr<const Botan::TLS::Policy> server_policy,
402
                        std::string test_name = "Test Conversation") :
8✔
403
            TestBase(ioc, client_policy, server_policy, test_name, config_name) {}
24✔
404

405
      void run(const error_code& ec) {
56✔
406
         static auto test_case = &Test_Conversation::run;
56✔
407
         const std::string message("Time is an illusion. Lunchtime doubly so.");
56✔
408

409
         reenter(*this) {
56✔
410
            m_client->reset_timeout("connect");
8✔
411
            yield net::async_connect(
16✔
412
               m_client->stream().lowest_layer(), k_endpoints, std::bind(test_case, shared_from_this(), _1));
16✔
413
            m_result.expect_success("connect", ec);
16✔
414

415
            m_client->reset_timeout("handshake");
8✔
416
            yield m_client->stream().async_handshake(Botan::TLS::Connection_Side::Client,
16✔
417
                                                     std::bind(test_case, shared_from_this(), _1));
16✔
418
            m_result.expect_success("handshake", ec);
16✔
419

420
            m_client->reset_timeout("send_message");
8✔
421
            yield net::async_write(m_client->stream(),
24✔
422
                                   net::buffer(message.c_str(), message.size() + 1),  // including \0 termination
8✔
423
                                   std::bind(test_case, shared_from_this(), _1));
16✔
424
            m_result.expect_success("send_message", ec);
16✔
425

426
            m_client->reset_timeout("receive_response");
8✔
427
            yield net::async_read(m_client->stream(),
24✔
428
                                  m_client->buffer(),
8✔
429
                                  std::bind(&Client::received_zero_byte, m_client.get(), _1, _2),
8✔
430
                                  std::bind(test_case, shared_from_this(), _1));
16✔
431
            m_result.expect_success("receive_response", ec);
16✔
432
            m_result.confirm("correct message", m_client->message() == message);
16✔
433

434
            m_client->reset_timeout("shutdown");
8✔
435
            yield m_client->stream().async_shutdown(std::bind(test_case, shared_from_this(), _1));
8✔
436
            m_result.expect_success("shutdown", ec);
16✔
437

438
            m_client->reset_timeout("await close_notify");
8✔
439
            yield net::async_read(m_client->stream(), m_client->buffer(), std::bind(test_case, shared_from_this(), _1));
8✔
440
            m_result.confirm("received close_notify", m_client->stream().shutdown_received());
16✔
441
            m_result.expect_ec("closed with EOF", net::error::eof, ec);
8✔
442

443
            teardown();
64✔
444
         }
56✔
445
      }
56✔
446
};
447

448
class Test_Conversation_Sync : public Synchronous_Test {
4✔
449
   public:
450
      Test_Conversation_Sync(net::io_context& ioc,
4✔
451
                             std::string config_name,
452
                             std::shared_ptr<const Botan::TLS::Policy> client_policy,
453
                             std::shared_ptr<const Botan::TLS::Policy> server_policy) :
4✔
454
            Synchronous_Test(ioc, client_policy, server_policy, "Test Conversation Sync", config_name) {}
20✔
455

456
      void run_synchronous_client() override {
4✔
457
         const std::string message("Time is an illusion. Lunchtime doubly so.");
4✔
458
         error_code ec;
4✔
459

460
         net::connect(m_client->stream().lowest_layer(), k_endpoints, ec);
4✔
461
         m_result.expect_success("connect", ec);
8✔
462

463
         m_client->stream().handshake(Botan::TLS::Connection_Side::Client, ec);
4✔
464
         m_result.expect_success("handshake", ec);
8✔
465

466
         net::write(m_client->stream(),
4✔
467
                    net::buffer(message.c_str(), message.size() + 1),  // including \0 termination
4✔
468
                    ec);
469
         m_result.expect_success("send_message", ec);
8✔
470

471
         net::read(
8✔
472
            m_client->stream(), m_client->buffer(), std::bind(&Client::received_zero_byte, m_client.get(), _1, _2), ec);
4✔
473
         m_result.expect_success("receive_response", ec);
8✔
474
         m_result.confirm("correct message", m_client->message() == message);
8✔
475

476
         m_client->stream().shutdown(ec);
4✔
477
         m_result.expect_success("shutdown", ec);
8✔
478

479
         net::read(m_client->stream(), m_client->buffer(), ec);
4✔
480
         m_result.confirm("received close_notify", m_client->stream().shutdown_received());
8✔
481
         m_result.expect_ec("closed with EOF", net::error::eof, ec);
4✔
482

483
         teardown();
8✔
484
      }
4✔
485
};
486

487
/* In this test case the client shuts down the SSL connection, but does not wait for the server's response before
488
 * closing the socket. Accordingly, it will not receive the server's close_notify alert. Instead, the async_read
489
 * operation will be aborted. The server should be able to successfully shutdown nonetheless.
490
 */
491
class Test_Eager_Close : public TestBase,
492
                         public net::coroutine,
493
                         public std::enable_shared_from_this<Test_Eager_Close> {
494
   public:
495
      Test_Eager_Close(net::io_context& ioc,
4✔
496
                       std::string config_name,
497
                       std::shared_ptr<const Botan::TLS::Policy> client_policy,
498
                       std::shared_ptr<const Botan::TLS::Policy> server_policy) :
4✔
499
            TestBase(ioc, client_policy, server_policy, "Test Eager Close", config_name) {}
20✔
500

501
      void run(const error_code& ec) {
16✔
502
         static auto test_case = &Test_Eager_Close::run;
16✔
503
         reenter(*this) {
16✔
504
            m_client->reset_timeout("connect");
4✔
505
            yield net::async_connect(
8✔
506
               m_client->stream().lowest_layer(), k_endpoints, std::bind(test_case, shared_from_this(), _1));
8✔
507
            m_result.expect_success("connect", ec);
8✔
508

509
            m_client->reset_timeout("handshake");
4✔
510
            yield m_client->stream().async_handshake(Botan::TLS::Connection_Side::Client,
8✔
511
                                                     std::bind(test_case, shared_from_this(), _1));
8✔
512
            m_result.expect_success("handshake", ec);
8✔
513

514
            m_client->reset_timeout("shutdown");
4✔
515
            yield m_client->stream().async_shutdown(std::bind(test_case, shared_from_this(), _1));
4✔
516
            m_result.expect_success("shutdown", ec);
8✔
517

518
            m_client->close_socket();
4✔
519
            m_result.confirm("did not receive close_notify", !m_client->stream().shutdown_received());
8✔
520

521
            teardown();
20✔
522
         }
16✔
523
      }
16✔
524
};
525

526
class Test_Eager_Close_Sync : public Synchronous_Test {
4✔
527
   public:
528
      Test_Eager_Close_Sync(net::io_context& ioc,
4✔
529
                            std::string config_name,
530
                            std::shared_ptr<const Botan::TLS::Policy> client_policy,
531
                            std::shared_ptr<const Botan::TLS::Policy> server_policy) :
4✔
532
            Synchronous_Test(ioc, client_policy, server_policy, "Test Eager Close Sync", config_name) {}
20✔
533

534
      void run_synchronous_client() override {
4✔
535
         error_code ec;
4✔
536

537
         net::connect(m_client->stream().lowest_layer(), k_endpoints, ec);
4✔
538
         m_result.expect_success("connect", ec);
8✔
539

540
         m_client->stream().handshake(Botan::TLS::Connection_Side::Client, ec);
4✔
541
         m_result.expect_success("handshake", ec);
8✔
542

543
         m_client->stream().shutdown(ec);
4✔
544
         m_result.expect_success("shutdown", ec);
8✔
545

546
         m_client->close_socket();
4✔
547
         m_result.confirm("did not receive close_notify", !m_client->stream().shutdown_received());
8✔
548

549
         teardown();
4✔
550
      }
4✔
551
};
552

553
/* In this test case the client closes the socket without properly shutting down the connection.
554
 * The server should see a StreamTruncated error.
555
 */
556
class Test_Close_Without_Shutdown : public TestBase,
557
                                    public net::coroutine,
558
                                    public std::enable_shared_from_this<Test_Close_Without_Shutdown> {
559
   public:
560
      Test_Close_Without_Shutdown(net::io_context& ioc,
4✔
561
                                  std::string config_name,
562
                                  std::shared_ptr<const Botan::TLS::Policy> client_policy,
563
                                  std::shared_ptr<const Botan::TLS::Policy> server_policy) :
4✔
564
            TestBase(ioc, client_policy, server_policy, "Test Close Without Shutdown", config_name) {}
20✔
565

566
      void run(const error_code& ec) {
20✔
567
         static auto test_case = &Test_Close_Without_Shutdown::run;
20✔
568
         reenter(*this) {
20✔
569
            m_client->reset_timeout("connect");
4✔
570
            yield net::async_connect(
8✔
571
               m_client->stream().lowest_layer(), k_endpoints, std::bind(test_case, shared_from_this(), _1));
8✔
572
            m_result.expect_success("connect", ec);
8✔
573

574
            m_client->reset_timeout("handshake");
4✔
575
            yield m_client->stream().async_handshake(Botan::TLS::Connection_Side::Client,
8✔
576
                                                     std::bind(test_case, shared_from_this(), _1));
8✔
577
            m_result.expect_success("handshake", ec);
8✔
578

579
            // send the control message to configure the server to expect a short-read
580
            m_client->reset_timeout("send expect_short_read message");
4✔
581
            yield net::async_write(
8✔
582
               m_client->stream(),
4✔
583
               net::buffer(m_server->expect_short_read_message.c_str(),
4✔
584
                           m_server->expect_short_read_message.size() + 1),  // including \0 termination
4✔
585
               std::bind(test_case, shared_from_this(), _1));
8✔
586
            m_result.expect_success("send expect_short_read message", ec);
8✔
587

588
            // read the confirmation of the control message above
589
            m_client->reset_timeout("receive_response");
4✔
590
            yield net::async_read(m_client->stream(),
12✔
591
                                  m_client->buffer(),
4✔
592
                                  std::bind(&Client::received_zero_byte, m_client.get(), _1, _2),
4✔
593
                                  std::bind(test_case, shared_from_this(), _1));
8✔
594
            m_result.expect_success("receive_response", ec);
8✔
595

596
            m_client->close_socket();
4✔
597
            m_result.confirm("did not receive close_notify", !m_client->stream().shutdown_received());
8✔
598

599
            teardown();
24✔
600
         }
20✔
601
      }
20✔
602
};
603

604
class Test_Close_Without_Shutdown_Sync : public Synchronous_Test {
4✔
605
   public:
606
      Test_Close_Without_Shutdown_Sync(net::io_context& ioc,
4✔
607
                                       std::string config_name,
608
                                       std::shared_ptr<const Botan::TLS::Policy> client_policy,
609
                                       std::shared_ptr<const Botan::TLS::Policy> server_policy) :
4✔
610
            Synchronous_Test(ioc, client_policy, server_policy, "Test Close Without Shutdown Sync", config_name) {}
20✔
611

612
      void run_synchronous_client() override {
4✔
613
         error_code ec;
4✔
614
         net::connect(m_client->stream().lowest_layer(), k_endpoints, ec);
4✔
615
         m_result.expect_success("connect", ec);
8✔
616

617
         m_client->stream().handshake(Botan::TLS::Connection_Side::Client, ec);
4✔
618
         m_result.expect_success("handshake", ec);
8✔
619

620
         m_server->expect_short_read();
4✔
621

622
         m_client->close_socket();
4✔
623
         m_result.confirm("did not receive close_notify", !m_client->stream().shutdown_received());
8✔
624

625
         teardown();
4✔
626
      }
4✔
627
};
628

629
/* In this test case the server shuts down the connection but the client doesn't send the corresponding close_notify
630
 * response. Instead, it closes the socket immediately.
631
 * The server should see a short-read error.
632
 */
633
class Test_No_Shutdown_Response : public TestBase,
634
                                  public net::coroutine,
635
                                  public std::enable_shared_from_this<Test_No_Shutdown_Response> {
636
   public:
637
      Test_No_Shutdown_Response(net::io_context& ioc,
4✔
638
                                std::string config_name,
639
                                std::shared_ptr<const Botan::TLS::Policy> client_policy,
640
                                std::shared_ptr<const Botan::TLS::Policy> server_policy) :
4✔
641
            TestBase(ioc, client_policy, server_policy, "Test No Shutdown Response", config_name) {}
20✔
642

643
      void run(const error_code& ec) {
20✔
644
         static auto test_case = &Test_No_Shutdown_Response::run;
20✔
645
         reenter(*this) {
20✔
646
            m_client->reset_timeout("connect");
4✔
647
            yield net::async_connect(
8✔
648
               m_client->stream().lowest_layer(), k_endpoints, std::bind(test_case, shared_from_this(), _1));
8✔
649
            m_result.expect_success("connect", ec);
8✔
650

651
            m_client->reset_timeout("handshake");
4✔
652
            yield m_client->stream().async_handshake(Botan::TLS::Connection_Side::Client,
8✔
653
                                                     std::bind(test_case, shared_from_this(), _1));
8✔
654
            m_result.expect_success("handshake", ec);
8✔
655

656
            // send a control message to make the server shut down
657
            m_client->reset_timeout("send shutdown message");
4✔
658
            yield net::async_write(
8✔
659
               m_client->stream(),
4✔
660
               net::buffer(m_server->prepare_shutdown_no_response_message.c_str(),
4✔
661
                           m_server->prepare_shutdown_no_response_message.size() + 1),  // including \0 termination
4✔
662
               std::bind(test_case, shared_from_this(), _1));
8✔
663
            m_result.expect_success("send shutdown message", ec);
8✔
664

665
            // read the server's close-notify message
666
            m_client->reset_timeout("read close_notify");
4✔
667
            yield net::async_read(m_client->stream(), m_client->buffer(), std::bind(test_case, shared_from_this(), _1));
4✔
668
            m_result.expect_ec("read gives EOF", net::error::eof, ec);
4✔
669
            m_result.confirm("received close_notify", m_client->stream().shutdown_received());
8✔
670

671
            // close the socket rather than shutting down
672
            m_client->close_socket();
4✔
673

674
            teardown();
24✔
675
         }
20✔
676
      }
20✔
677
};
678

679
class Test_No_Shutdown_Response_Sync : public Synchronous_Test {
4✔
680
   public:
681
      Test_No_Shutdown_Response_Sync(net::io_context& ioc,
4✔
682
                                     std::string config_name,
683
                                     std::shared_ptr<const Botan::TLS::Policy> client_policy,
684
                                     std::shared_ptr<const Botan::TLS::Policy> server_policy) :
4✔
685
            Synchronous_Test(ioc, client_policy, server_policy, "Test No Shutdown Response Sync", config_name) {}
20✔
686

687
      void run_synchronous_client() override {
4✔
688
         error_code ec;
4✔
689
         net::connect(m_client->stream().lowest_layer(), k_endpoints, ec);
4✔
690
         m_result.expect_success("connect", ec);
8✔
691

692
         m_client->stream().handshake(Botan::TLS::Connection_Side::Client, ec);
4✔
693
         m_result.expect_success("handshake", ec);
8✔
694

695
         net::write(m_client->stream(),
8✔
696
                    net::buffer(m_server->prepare_shutdown_no_response_message.c_str(),
4✔
697
                                m_server->prepare_shutdown_no_response_message.size() + 1),  // including \0 termination
4✔
698
                    ec);
699
         m_result.expect_success("send expect_short_read message", ec);
8✔
700

701
         net::read(m_client->stream(), m_client->buffer(), ec);
4✔
702
         m_result.expect_ec("read gives EOF", net::error::eof, ec);
4✔
703
         m_result.confirm("received close_notify", m_client->stream().shutdown_received());
8✔
704

705
         // close the socket rather than shutting down
706
         m_client->close_socket();
4✔
707

708
         teardown();
4✔
709
      }
4✔
710
};
711

712
class Test_Conversation_With_Move : public Test_Conversation {
4✔
713
   public:
714
      Test_Conversation_With_Move(net::io_context& ioc,
4✔
715
                                  std::string config_name,
716
                                  std::shared_ptr<const Botan::TLS::Policy> client_policy,
717
                                  std::shared_ptr<const Botan::TLS::Policy> server_policy) :
4✔
718
            Test_Conversation(
719
               ioc, std::move(config_name), client_policy, server_policy, "Test Conversation With Move") {
24✔
720
         m_server->move_before_accept();
4✔
721
      }
4✔
722
};
723

724
      #include <boost/asio/unyield.hpp>
725

726
struct SystemConfiguration {
727
      std::string name;
728

729
      std::shared_ptr<Botan::TLS::Text_Policy> client_policy;
730
      std::shared_ptr<Botan::TLS::Text_Policy> server_policy;
731

732
      SystemConfiguration(std::string n, std::string cp, std::string sp) :
4✔
733
            name(std::move(n)),
8✔
734
            client_policy(std::make_shared<Botan::TLS::Text_Policy>(cp)),
4✔
735
            server_policy(std::make_shared<Botan::TLS::Text_Policy>(sp)) {}
4✔
736

737
      template <typename TestT>
738
      void run(std::vector<Result>& results) {
36✔
739
         net::io_context ioc;
36✔
740

741
         auto t = std::make_shared<TestT>(ioc, name, server_policy, client_policy);
36✔
742

743
         t->run(error_code{});
36✔
744

745
         while(true) {
746
            try {
747
               ioc.run();
36✔
748
               break;
749
            } catch(const Timeout_Exception&) { /* timeout is reported via Test::Result object */
×
750
            } catch(const boost::exception&) { t->fail("boost exception"); } catch(const std::exception& ex) {
×
751
               t->fail(ex.what());
×
752
            }
753
         }
754

755
         t->finishAsynchronousWork();
36✔
756
         t->extend_results(results);
36✔
757
      }
36✔
758
};
759

760
std::vector<SystemConfiguration> get_configurations() {
1✔
761
   return {
1✔
762
      SystemConfiguration("TLS 1.2 only", "allow_tls12=true\nallow_tls13=false", "allow_tls12=true\nallow_tls13=false"),
763
      #if defined(BOTAN_HAS_TLS_13)
764
         SystemConfiguration(
765
            "TLS 1.3 only", "allow_tls12=false\nallow_tls13=true", "allow_tls12=false\nallow_tls13=true"),
766
         SystemConfiguration("TLS 1.x server, TLS 1.2 client",
767
                             "allow_tls12=true\nallow_tls13=false",
768
                             "allow_tls12=true\nallow_tls13=true"),
769
         SystemConfiguration("TLS 1.2 server, TLS 1.x client",
770
                             "allow_tls12=true\nallow_tls13=true",
771
                             "allow_tls12=true\nallow_tls13=false"),
772
      #endif
773
   };
13✔
774
}
775

776
}  // namespace
777

778
namespace Botan_Tests {
779

780
class Tls_Stream_Integration_Tests final : public Test {
×
781
   public:
782
      std::vector<Test::Result> run() override {
1✔
783
         std::vector<Test::Result> results;
1✔
784

785
         auto configs = get_configurations();
1✔
786
         for(auto& config : configs) {
5✔
787
            config.run<Test_Conversation>(results);
4✔
788
            config.run<Test_Eager_Close>(results);
4✔
789
            config.run<Test_Close_Without_Shutdown>(results);
4✔
790
            config.run<Test_No_Shutdown_Response>(results);
4✔
791
            config.run<Test_Conversation_Sync>(results);
4✔
792
            config.run<Test_Eager_Close_Sync>(results);
4✔
793
            config.run<Test_Close_Without_Shutdown_Sync>(results);
4✔
794
            config.run<Test_No_Shutdown_Response_Sync>(results);
4✔
795
            config.run<Test_Conversation_With_Move>(results);
4✔
796
         }
797

798
         return results;
1✔
799
      }
1✔
800
};
801

802
BOTAN_REGISTER_TEST("tls", "tls_stream_integration", Tls_Stream_Integration_Tests);
803

804
}  // namespace Botan_Tests
805

806
   #endif  // BOOST_VERSION
807
#endif     // BOTAN_HAS_TLS && BOTAN_HAS_BOOST_ASIO
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc