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

randombit / botan / 5230455705

10 Jun 2023 02:30PM UTC coverage: 91.715% (-0.03%) from 91.746%
5230455705

push

github

randombit
Merge GH #3584 Change clang-format AllowShortFunctionsOnASingleLine config from All to Inline

77182 of 84154 relevant lines covered (91.72%)

11975295.43 hits per line

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

93.46
/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() {
36✔
47
   return Botan_Tests::Test::data_dir() + "/x509/certstor/cert1.crt";
36✔
48
}
49

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

54
class Timeout_Exception : public std::runtime_error {
55
      using std::runtime_error::runtime_error;
×
56
};
57

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

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

77
      virtual ~Peer() { cancel_timeout(); }
432✔
78

79
      net::mutable_buffer buffer() { return net::buffer(m_data, max_msg_length); }
36✔
80

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

83
      std::string message() const { return std::string(m_data); }
12✔
84

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

92
         if(bytes_transferred > 0 && m_data[bytes_transferred - 1] == '\0') {
116✔
93
            return 0;
94
         }
95

96
         return max_msg_length - bytes_transferred;
76✔
97
      }
98

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

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

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

117
               throw Timeout_Exception("timeout occured: " + message);
×
118
            }
119
         });
296✔
120
      }
296✔
121

122
      void cancel_timeout() { m_timeout_timer.cancel(); }
108✔
123

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

133
      char m_data[max_msg_length];
134
};
135

136
class Result_Wrapper {
36✔
137
   public:
138
      Result_Wrapper(std::string name) : m_result(std::move(name)) {}
144✔
139

140
      Result& result() { return m_result; }
72✔
141

142
      void expect_success(const std::string& msg, const error_code& ec) {
352✔
143
         error_code success;
352✔
144
         expect_ec(msg, success, ec);
352✔
145
      }
146

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

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

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

159
   private:
160
      Result m_result;
161
};
162

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

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

184
      void listen() {
36✔
185
         error_code ec;
36✔
186
         const auto endpoint = k_endpoints.back();
36✔
187

188
         m_acceptor.open(endpoint.protocol(), ec);
36✔
189
         m_acceptor.set_option(net::socket_base::reuse_address(true), ec);
36✔
190
         m_acceptor.bind(endpoint, ec);
36✔
191
         m_acceptor.listen(net::socket_base::max_listen_connections, ec);
36✔
192

193
         m_result.expect_success("listen", ec);
72✔
194

195
         reset_timeout("accept");
36✔
196
         m_acceptor.async_accept(std::bind(&Server::start_session, shared_from_this(), _1, _2));
72✔
197
      }
36✔
198

199
      void expect_short_read() { m_short_read_expected = true; }
4✔
200

201
      void move_before_accept() { m_move_before_accept = true; }
4✔
202

203
      Result_Wrapper result() { return m_result; }
36✔
204

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

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

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

221
         reset_timeout("handshake");
36✔
222
         m_stream->async_handshake(Botan::TLS::Connection_Side::Server,
72✔
223
                                   std::bind(&Server::handle_handshake, shared_from_this(), _1));
72✔
224
      }
36✔
225

226
      void shutdown() {
8✔
227
         error_code shutdown_ec;
8✔
228
         m_stream->shutdown(shutdown_ec);
8✔
229
         m_result.expect_success("shutdown", shutdown_ec);
16✔
230
         handle_write(error_code{});
8✔
231
      }
8✔
232

233
      void handle_handshake(const error_code& ec) {
36✔
234
         m_result.expect_success("handshake", ec);
72✔
235
         handle_write(error_code{});
36✔
236
      }
36✔
237

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

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

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

266
         m_result.expect_success("read_message", ec);
48✔
267

268
         if(message() == prepare_shutdown_no_response_message) {
36✔
269
            m_short_read_expected = true;
8✔
270
            shutdown();
8✔
271
            return;
8✔
272
         }
273

274
         if(message() == expect_short_read_message) {
28✔
275
            m_short_read_expected = true;
4✔
276
         }
277

278
         reset_timeout("send_response");
16✔
279
         net::async_write(
32✔
280
            *m_stream, buffer(bytes_transferred), std::bind(&Server::handle_write, shared_from_this(), _1));
16✔
281
      }
282

283
      void handle_shutdown(const error_code& ec) {
20✔
284
         m_result.expect_success("shutdown", ec);
40✔
285
         cancel_timeout();
20✔
286
      }
20✔
287

288
   private:
289
      tcp::acceptor m_acceptor;
290
      Result_Wrapper m_result;
291
      bool m_short_read_expected;
292

293
      // regression test for #2635
294
      bool m_move_before_accept;
295
};
296

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

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

311
      ssl_stream& stream() { return *m_stream; }
164✔
312

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

323
      #include <boost/asio/yield.hpp>
324

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

341
         m_server->listen();
36✔
342
      }
36✔
343

344
      virtual ~TestBase() = default;
108✔
345

346
      virtual void finishAsynchronousWork() {}
20✔
347

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

350
      void extend_results(std::vector<Result>& results) {
36✔
351
         results.push_back(m_result.result());
36✔
352
         results.push_back(m_server->result().result());
36✔
353
      }
36✔
354

355
   protected:
356
      //! retire client and server instances after a job well done
357
      void teardown() { m_client->cancel_timeout(); }
24✔
358

359
   protected:
360
      std::string m_name;
361

362
      std::shared_ptr<Client> m_client;
363
      std::shared_ptr<Server> m_server;
364

365
      Result_Wrapper m_result;
366
};
367

368
class Synchronous_Test : public TestBase {
16✔
369
   public:
370
      using TestBase::TestBase;
16✔
371

372
      void finishAsynchronousWork() override { m_client_thread.join(); }
×
373

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

388
      virtual void run_synchronous_client() = 0;
389

390
   private:
391
      std::thread m_client_thread;
392
};
393

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

411
      void run(const error_code& ec) {
56✔
412
         static auto test_case = &Test_Conversation::run;
56✔
413
         const std::string message("Time is an illusion. Lunchtime doubly so.");
56✔
414

415
         reenter(*this) {
56✔
416
            m_client->reset_timeout("connect");
8✔
417
            yield net::async_connect(
16✔
418
               m_client->stream().lowest_layer(), k_endpoints, std::bind(test_case, shared_from_this(), _1));
16✔
419
            m_result.expect_success("connect", ec);
16✔
420

421
            m_client->reset_timeout("handshake");
8✔
422
            yield m_client->stream().async_handshake(Botan::TLS::Connection_Side::Client,
16✔
423
                                                     std::bind(test_case, shared_from_this(), _1));
16✔
424
            m_result.expect_success("handshake", ec);
16✔
425

426
            m_client->reset_timeout("send_message");
8✔
427
            yield net::async_write(m_client->stream(),
24✔
428
                                   net::buffer(message.c_str(), message.size() + 1),  // including \0 termination
8✔
429
                                   std::bind(test_case, shared_from_this(), _1));
16✔
430
            m_result.expect_success("send_message", ec);
16✔
431

432
            m_client->reset_timeout("receive_response");
8✔
433
            yield net::async_read(m_client->stream(),
24✔
434
                                  m_client->buffer(),
8✔
435
                                  std::bind(&Client::received_zero_byte, m_client.get(), _1, _2),
8✔
436
                                  std::bind(test_case, shared_from_this(), _1));
16✔
437
            m_result.expect_success("receive_response", ec);
16✔
438
            m_result.confirm("correct message", m_client->message() == message);
16✔
439

440
            m_client->reset_timeout("shutdown");
8✔
441
            yield m_client->stream().async_shutdown(std::bind(test_case, shared_from_this(), _1));
8✔
442
            m_result.expect_success("shutdown", ec);
16✔
443

444
            m_client->reset_timeout("await close_notify");
8✔
445
            yield net::async_read(m_client->stream(), m_client->buffer(), std::bind(test_case, shared_from_this(), _1));
8✔
446
            m_result.confirm("received close_notify", m_client->stream().shutdown_received());
16✔
447
            m_result.expect_ec("closed with EOF", net::error::eof, ec);
8✔
448

449
            teardown();
64✔
450
         }
56✔
451
      }
56✔
452
};
453

454
class Test_Conversation_Sync : public Synchronous_Test {
4✔
455
   public:
456
      Test_Conversation_Sync(net::io_context& ioc,
4✔
457
                             std::string config_name,
458
                             std::shared_ptr<const Botan::TLS::Policy> client_policy,
459
                             std::shared_ptr<const Botan::TLS::Policy> server_policy) :
4✔
460
            Synchronous_Test(ioc, client_policy, server_policy, "Test Conversation Sync", config_name) {}
20✔
461

462
      void run_synchronous_client() override {
4✔
463
         const std::string message("Time is an illusion. Lunchtime doubly so.");
4✔
464
         error_code ec;
4✔
465

466
         net::connect(m_client->stream().lowest_layer(), k_endpoints, ec);
4✔
467
         m_result.expect_success("connect", ec);
8✔
468

469
         m_client->stream().handshake(Botan::TLS::Connection_Side::Client, ec);
4✔
470
         m_result.expect_success("handshake", ec);
8✔
471

472
         net::write(m_client->stream(),
4✔
473
                    net::buffer(message.c_str(), message.size() + 1),  // including \0 termination
4✔
474
                    ec);
475
         m_result.expect_success("send_message", ec);
8✔
476

477
         net::read(
8✔
478
            m_client->stream(), m_client->buffer(), std::bind(&Client::received_zero_byte, m_client.get(), _1, _2), ec);
4✔
479
         m_result.expect_success("receive_response", ec);
8✔
480
         m_result.confirm("correct message", m_client->message() == message);
8✔
481

482
         m_client->stream().shutdown(ec);
4✔
483
         m_result.expect_success("shutdown", ec);
8✔
484

485
         net::read(m_client->stream(), m_client->buffer(), ec);
4✔
486
         m_result.confirm("received close_notify", m_client->stream().shutdown_received());
8✔
487
         m_result.expect_ec("closed with EOF", net::error::eof, ec);
4✔
488

489
         teardown();
8✔
490
      }
4✔
491
};
492

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

507
      void run(const error_code& ec) {
16✔
508
         static auto test_case = &Test_Eager_Close::run;
16✔
509
         reenter(*this) {
16✔
510
            m_client->reset_timeout("connect");
4✔
511
            yield net::async_connect(
8✔
512
               m_client->stream().lowest_layer(), k_endpoints, std::bind(test_case, shared_from_this(), _1));
8✔
513
            m_result.expect_success("connect", ec);
8✔
514

515
            m_client->reset_timeout("handshake");
4✔
516
            yield m_client->stream().async_handshake(Botan::TLS::Connection_Side::Client,
8✔
517
                                                     std::bind(test_case, shared_from_this(), _1));
8✔
518
            m_result.expect_success("handshake", ec);
8✔
519

520
            m_client->reset_timeout("shutdown");
4✔
521
            yield m_client->stream().async_shutdown(std::bind(test_case, shared_from_this(), _1));
4✔
522
            m_result.expect_success("shutdown", ec);
8✔
523

524
            m_client->close_socket();
4✔
525
            m_result.confirm("did not receive close_notify", !m_client->stream().shutdown_received());
8✔
526

527
            teardown();
20✔
528
         }
16✔
529
      }
16✔
530
};
531

532
class Test_Eager_Close_Sync : public Synchronous_Test {
4✔
533
   public:
534
      Test_Eager_Close_Sync(net::io_context& ioc,
4✔
535
                            std::string config_name,
536
                            std::shared_ptr<const Botan::TLS::Policy> client_policy,
537
                            std::shared_ptr<const Botan::TLS::Policy> server_policy) :
4✔
538
            Synchronous_Test(ioc, client_policy, server_policy, "Test Eager Close Sync", config_name) {}
20✔
539

540
      void run_synchronous_client() override {
4✔
541
         error_code ec;
4✔
542

543
         net::connect(m_client->stream().lowest_layer(), k_endpoints, ec);
4✔
544
         m_result.expect_success("connect", ec);
8✔
545

546
         m_client->stream().handshake(Botan::TLS::Connection_Side::Client, ec);
4✔
547
         m_result.expect_success("handshake", ec);
8✔
548

549
         m_client->stream().shutdown(ec);
4✔
550
         m_result.expect_success("shutdown", ec);
8✔
551

552
         m_client->close_socket();
4✔
553
         m_result.confirm("did not receive close_notify", !m_client->stream().shutdown_received());
8✔
554

555
         teardown();
4✔
556
      }
4✔
557
};
558

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

572
      void run(const error_code& ec) {
20✔
573
         static auto test_case = &Test_Close_Without_Shutdown::run;
20✔
574
         reenter(*this) {
20✔
575
            m_client->reset_timeout("connect");
4✔
576
            yield net::async_connect(
8✔
577
               m_client->stream().lowest_layer(), k_endpoints, std::bind(test_case, shared_from_this(), _1));
8✔
578
            m_result.expect_success("connect", ec);
8✔
579

580
            m_client->reset_timeout("handshake");
4✔
581
            yield m_client->stream().async_handshake(Botan::TLS::Connection_Side::Client,
8✔
582
                                                     std::bind(test_case, shared_from_this(), _1));
8✔
583
            m_result.expect_success("handshake", ec);
8✔
584

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

594
            // read the confirmation of the control message above
595
            m_client->reset_timeout("receive_response");
4✔
596
            yield net::async_read(m_client->stream(),
12✔
597
                                  m_client->buffer(),
4✔
598
                                  std::bind(&Client::received_zero_byte, m_client.get(), _1, _2),
4✔
599
                                  std::bind(test_case, shared_from_this(), _1));
8✔
600
            m_result.expect_success("receive_response", ec);
8✔
601

602
            m_client->close_socket();
4✔
603
            m_result.confirm("did not receive close_notify", !m_client->stream().shutdown_received());
8✔
604

605
            teardown();
24✔
606
         }
20✔
607
      }
20✔
608
};
609

610
class Test_Close_Without_Shutdown_Sync : public Synchronous_Test {
4✔
611
   public:
612
      Test_Close_Without_Shutdown_Sync(net::io_context& ioc,
4✔
613
                                       std::string config_name,
614
                                       std::shared_ptr<const Botan::TLS::Policy> client_policy,
615
                                       std::shared_ptr<const Botan::TLS::Policy> server_policy) :
4✔
616
            Synchronous_Test(ioc, client_policy, server_policy, "Test Close Without Shutdown Sync", config_name) {}
20✔
617

618
      void run_synchronous_client() override {
4✔
619
         error_code ec;
4✔
620
         net::connect(m_client->stream().lowest_layer(), k_endpoints, ec);
4✔
621
         m_result.expect_success("connect", ec);
8✔
622

623
         m_client->stream().handshake(Botan::TLS::Connection_Side::Client, ec);
4✔
624
         m_result.expect_success("handshake", ec);
8✔
625

626
         m_server->expect_short_read();
4✔
627

628
         m_client->close_socket();
4✔
629
         m_result.confirm("did not receive close_notify", !m_client->stream().shutdown_received());
8✔
630

631
         teardown();
4✔
632
      }
4✔
633
};
634

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

649
      void run(const error_code& ec) {
20✔
650
         static auto test_case = &Test_No_Shutdown_Response::run;
20✔
651
         reenter(*this) {
20✔
652
            m_client->reset_timeout("connect");
4✔
653
            yield net::async_connect(
8✔
654
               m_client->stream().lowest_layer(), k_endpoints, std::bind(test_case, shared_from_this(), _1));
8✔
655
            m_result.expect_success("connect", ec);
8✔
656

657
            m_client->reset_timeout("handshake");
4✔
658
            yield m_client->stream().async_handshake(Botan::TLS::Connection_Side::Client,
8✔
659
                                                     std::bind(test_case, shared_from_this(), _1));
8✔
660
            m_result.expect_success("handshake", ec);
8✔
661

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

671
            // read the server's close-notify message
672
            m_client->reset_timeout("read close_notify");
4✔
673
            yield net::async_read(m_client->stream(), m_client->buffer(), std::bind(test_case, shared_from_this(), _1));
4✔
674
            m_result.expect_ec("read gives EOF", net::error::eof, ec);
4✔
675
            m_result.confirm("received close_notify", m_client->stream().shutdown_received());
8✔
676

677
            // close the socket rather than shutting down
678
            m_client->close_socket();
4✔
679

680
            teardown();
24✔
681
         }
20✔
682
      }
20✔
683
};
684

685
class Test_No_Shutdown_Response_Sync : public Synchronous_Test {
4✔
686
   public:
687
      Test_No_Shutdown_Response_Sync(net::io_context& ioc,
4✔
688
                                     std::string config_name,
689
                                     std::shared_ptr<const Botan::TLS::Policy> client_policy,
690
                                     std::shared_ptr<const Botan::TLS::Policy> server_policy) :
4✔
691
            Synchronous_Test(ioc, client_policy, server_policy, "Test No Shutdown Response Sync", config_name) {}
20✔
692

693
      void run_synchronous_client() override {
4✔
694
         error_code ec;
4✔
695
         net::connect(m_client->stream().lowest_layer(), k_endpoints, ec);
4✔
696
         m_result.expect_success("connect", ec);
8✔
697

698
         m_client->stream().handshake(Botan::TLS::Connection_Side::Client, ec);
4✔
699
         m_result.expect_success("handshake", ec);
8✔
700

701
         net::write(m_client->stream(),
8✔
702
                    net::buffer(m_server->prepare_shutdown_no_response_message.c_str(),
4✔
703
                                m_server->prepare_shutdown_no_response_message.size() + 1),  // including \0 termination
4✔
704
                    ec);
705
         m_result.expect_success("send expect_short_read message", ec);
8✔
706

707
         net::read(m_client->stream(), m_client->buffer(), ec);
4✔
708
         m_result.expect_ec("read gives EOF", net::error::eof, ec);
4✔
709
         m_result.confirm("received close_notify", m_client->stream().shutdown_received());
8✔
710

711
         // close the socket rather than shutting down
712
         m_client->close_socket();
4✔
713

714
         teardown();
4✔
715
      }
4✔
716
};
717

718
class Test_Conversation_With_Move : public Test_Conversation {
4✔
719
   public:
720
      Test_Conversation_With_Move(net::io_context& ioc,
4✔
721
                                  std::string config_name,
722
                                  std::shared_ptr<const Botan::TLS::Policy> client_policy,
723
                                  std::shared_ptr<const Botan::TLS::Policy> server_policy) :
4✔
724
            Test_Conversation(
725
               ioc, std::move(config_name), client_policy, server_policy, "Test Conversation With Move") {
24✔
726
         m_server->move_before_accept();
4✔
727
      }
4✔
728
};
729

730
      #include <boost/asio/unyield.hpp>
731

732
struct SystemConfiguration {
733
      std::string name;
734

735
      std::shared_ptr<Botan::TLS::Text_Policy> client_policy;
736
      std::shared_ptr<Botan::TLS::Text_Policy> server_policy;
737

738
      SystemConfiguration(std::string n, std::string cp, std::string sp) :
4✔
739
            name(std::move(n)),
8✔
740
            client_policy(std::make_shared<Botan::TLS::Text_Policy>(cp)),
4✔
741
            server_policy(std::make_shared<Botan::TLS::Text_Policy>(sp)) {}
4✔
742

743
      template <typename TestT>
744
      void run(std::vector<Result>& results) {
36✔
745
         net::io_context ioc;
36✔
746

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

749
         t->run(error_code{});
36✔
750

751
         while(true) {
752
            try {
753
               ioc.run();
36✔
754
               break;
755
            } catch(const Timeout_Exception&) { /* timeout is reported via Test::Result object */
×
756
            } catch(const boost::exception&) {
×
757
               t->fail("boost exception");
×
758
            } catch(const std::exception& ex) {
×
759
               t->fail(ex.what());
×
760
            }
761
         }
762

763
         t->finishAsynchronousWork();
36✔
764
         t->extend_results(results);
36✔
765
      }
36✔
766
};
767

768
std::vector<SystemConfiguration> get_configurations() {
1✔
769
   return {
1✔
770
      SystemConfiguration("TLS 1.2 only", "allow_tls12=true\nallow_tls13=false", "allow_tls12=true\nallow_tls13=false"),
771
      #if defined(BOTAN_HAS_TLS_13)
772
         SystemConfiguration(
773
            "TLS 1.3 only", "allow_tls12=false\nallow_tls13=true", "allow_tls12=false\nallow_tls13=true"),
774
         SystemConfiguration("TLS 1.x server, TLS 1.2 client",
775
                             "allow_tls12=true\nallow_tls13=false",
776
                             "allow_tls12=true\nallow_tls13=true"),
777
         SystemConfiguration("TLS 1.2 server, TLS 1.x client",
778
                             "allow_tls12=true\nallow_tls13=true",
779
                             "allow_tls12=true\nallow_tls13=false"),
780
      #endif
781
   };
13✔
782
}
783

784
}  // namespace
785

786
namespace Botan_Tests {
787

788
class Tls_Stream_Integration_Tests final : public Test {
×
789
   public:
790
      std::vector<Test::Result> run() override {
1✔
791
         std::vector<Test::Result> results;
1✔
792

793
         auto configs = get_configurations();
1✔
794
         for(auto& config : configs) {
5✔
795
            config.run<Test_Conversation>(results);
4✔
796
            config.run<Test_Eager_Close>(results);
4✔
797
            config.run<Test_Close_Without_Shutdown>(results);
4✔
798
            config.run<Test_No_Shutdown_Response>(results);
4✔
799
            config.run<Test_Conversation_Sync>(results);
4✔
800
            config.run<Test_Eager_Close_Sync>(results);
4✔
801
            config.run<Test_Close_Without_Shutdown_Sync>(results);
4✔
802
            config.run<Test_No_Shutdown_Response_Sync>(results);
4✔
803
            config.run<Test_Conversation_With_Move>(results);
4✔
804
         }
805

806
         return results;
1✔
807
      }
1✔
808
};
809

810
BOTAN_REGISTER_TEST("tls", "tls_stream_integration", Tls_Stream_Integration_Tests);
811

812
}  // namespace Botan_Tests
813

814
   #endif  // BOOST_VERSION
815
#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