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

randombit / botan / 5378005141

26 Jun 2023 12:53PM UTC coverage: 91.741% (-0.001%) from 91.742%
5378005141

push

github

randombit
Disable pylint too-many-return-statements

78183 of 85221 relevant lines covered (91.74%)

12352794.33 hits per line

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

98.72
/src/tests/unit_asio_stream.cpp
1
/*
2
* TLS ASIO Stream Unit Tests
3
* (C) 2018-2020 Jack Lloyd
4
*     2018-2020 Hannes Rantzsch, Tim Oesterreich, Rene Meusel
5
*
6
* Botan is released under the Simplified BSD License (see license.txt)
7
*/
8

9
#include "tests.h"
10

11
#if defined(BOTAN_HAS_TLS) && defined(BOTAN_HAS_TLS_ASIO_STREAM)
12

13
   #include <botan/asio_stream.h>
14
   #include <botan/tls_callbacks.h>
15
   #include <botan/tls_session_manager_noop.h>
16

17
   // The boost::beast::test::stream we use is available starting from boost
18
   // version 1.68, so we cannot run these tests with a smaller version.
19
   #include <boost/version.hpp>
20
   #if BOOST_VERSION >= 106800
21

22
      // boost::beast::test::stream's include path has been changed in boost version
23
      // 1.70.
24
      #if BOOST_VERSION < 107000
25
         #include <boost/beast/experimental/test/stream.hpp>
26
      #else
27
         #include <boost/beast/_experimental/test/stream.hpp>
28
      #endif
29

30
      #include <boost/bind.hpp>
31
      #include <utility>
32

33
namespace Botan_Tests {
34

35
namespace net = boost::asio;
36
using error_code = boost::system::error_code;
37

38
constexpr uint8_t TEST_DATA[] =
39
   "The story so far: In the beginning the Universe was created. "
40
   "This has made a lot of people very angry and been widely regarded as a bad move.";
41
constexpr std::size_t TEST_DATA_SIZE = 142;
42
static_assert(sizeof(TEST_DATA) == TEST_DATA_SIZE, "size of TEST_DATA must match TEST_DATA_SIZE");
43

44
/**
45
 * Mocked Botan::TLS::Channel. Pretends to perform TLS operations and triggers appropriate callbacks in StreamCore.
46
 */
47
class MockChannel {
36✔
48
   public:
49
      MockChannel(std::shared_ptr<Botan::TLS::Callbacks> core) :
24✔
50
            m_callbacks(std::move(core)), m_bytes_till_complete_record(TEST_DATA_SIZE), m_active(false) {}
24✔
51

52
   public:
53
      std::size_t received_data(std::span<const uint8_t> data) {
6✔
54
         if(m_bytes_till_complete_record <= data.size()) {
6✔
55
            m_callbacks->tls_record_received(0, TEST_DATA);
6✔
56
            m_active = true;  // claim to be active once a full record has been received (for handshake test)
6✔
57
            return 0;
6✔
58
         }
59
         m_bytes_till_complete_record -= data.size();
×
60
         return m_bytes_till_complete_record;
×
61
      }
62

63
      void send(std::span<const uint8_t> buf) { m_callbacks->tls_emit_data(buf); }
43✔
64

65
      bool is_active() const { return m_active; }
10✔
66

67
   private:
68
      std::shared_ptr<Botan::TLS::Callbacks> m_callbacks;
69
      std::size_t m_bytes_till_complete_record;  // number of bytes still to read before tls record is completed
70
      bool m_active;
71
};
72

73
class ThrowingMockChannel : public MockChannel {
12✔
74
   public:
75
      static boost::system::error_code expected_ec() { return Botan::TLS::Alert::UnexpectedMessage; }
12✔
76

77
      ThrowingMockChannel(std::shared_ptr<Botan::TLS::Callbacks> core) : MockChannel(std::move(core)) {}
6✔
78

79
      std::size_t received_data(std::span<const uint8_t>) { throw Botan::TLS::Unexpected_Message("test_error"); }
4✔
80

81
      void send(std::span<const uint8_t>) { throw Botan::TLS::Unexpected_Message("test_error"); }
2✔
82
};
83

84
// Unfortunately, boost::beast::test::stream keeps lowest_layer_type private and
85
// only friends boost::asio::ssl::stream. We need to make our own.
86
class TestStream : public boost::beast::test::stream {
25✔
87
   public:
88
      using boost::beast::test::stream::stream;
25✔
89
      using lowest_layer_type = boost::beast::test::stream;
90
};
91

92
using FailCount = boost::beast::test::fail_count;
93

94
class AsioStream : public Botan::TLS::Stream<TestStream, MockChannel> {
×
95
   public:
96
      template <typename... Args>
97
      AsioStream(std::shared_ptr<Botan::TLS::Context> context, Args&&... args) : Stream(context, args...) {
18✔
98
         m_native_handle = std::make_unique<MockChannel>(m_core);
18✔
99
      }
18✔
100
};
101

102
class ThrowingAsioStream : public Botan::TLS::Stream<TestStream, ThrowingMockChannel> {
12✔
103
   public:
104
      template <typename... Args>
105
      ThrowingAsioStream(std::shared_ptr<Botan::TLS::Context> context, Args&&... args) : Stream(context, args...) {
6✔
106
         m_native_handle = std::make_unique<ThrowingMockChannel>(m_core);
6✔
107
      }
6✔
108
};
109

110
/**
111
 * Synchronous tests for Botan::Stream.
112
 *
113
 * This test validates the asynchronous behavior Botan::Stream, including its utility classes StreamCore and Async_*_Op.
114
 * The stream's channel, i.e. TLS_Client or TLS_Server, is mocked and pretends to perform TLS operations (noop) and
115
 * provides the test data to the stream.
116
 * The underlying network socket, claiming it read / wrote a number of bytes.
117
 */
118
class Asio_Stream_Tests final : public Test {
×
119
      std::shared_ptr<Botan::TLS::Context> get_context() {
25✔
120
         return std::make_shared<Botan::TLS::Context>(std::make_shared<Botan::Credentials_Manager>(),
50✔
121
                                                      std::make_shared<Botan::Null_RNG>(),
25✔
122
                                                      std::make_shared<Botan::TLS::Session_Manager_Noop>(),
×
123
                                                      std::make_shared<Botan::TLS::Default_Policy>());
50✔
124
      }
125

126
      // use memcmp to check if the data in a is a prefix of the data in b
127
      bool contains(const void* a, const void* b, const std::size_t size) { return memcmp(a, b, size) == 0; }
8✔
128

129
      boost::string_view test_data() const {
12✔
130
         return boost::string_view(reinterpret_cast<const char*>(TEST_DATA), TEST_DATA_SIZE);
12✔
131
      }
132

133
      void test_sync_handshake(std::vector<Test::Result>& results) {
1✔
134
         net::io_context ioc;
1✔
135
         auto ctx = get_context();
1✔
136
         AsioStream ssl(ctx, ioc, test_data());
1✔
137

138
         ssl.handshake(Botan::TLS::Connection_Side::Client);
1✔
139

140
         Test::Result result("sync TLS handshake");
1✔
141
         result.test_eq("feeds data into channel until active", ssl.native_handle()->is_active(), true);
1✔
142
         results.push_back(result);
1✔
143
      }
2✔
144

145
      void test_sync_handshake_error(std::vector<Test::Result>& results) {
1✔
146
         net::io_context ioc;
1✔
147
         // fail right away
148
         FailCount fc{0, net::error::no_recovery};
1✔
149
         TestStream remote{ioc};
1✔
150

151
         auto ctx = get_context();
1✔
152
         AsioStream ssl(ctx, ioc, fc);
1✔
153
         ssl.next_layer().connect(remote);
1✔
154

155
         // mimic handshake initialization
156
         ssl.native_handle()->send(TEST_DATA);
1✔
157

158
         error_code ec;
1✔
159
         ssl.handshake(Botan::TLS::Connection_Side::Client, ec);
1✔
160

161
         Test::Result result("sync TLS handshake error");
1✔
162
         result.test_eq("does not activate channel", ssl.native_handle()->is_active(), false);
1✔
163
         result.confirm("propagates error code", ec == net::error::no_recovery);
3✔
164
         results.push_back(result);
1✔
165
      }
3✔
166

167
      void test_sync_handshake_throw(std::vector<Test::Result>& results) {
1✔
168
         net::io_context ioc;
1✔
169
         TestStream remote{ioc};
1✔
170

171
         auto ctx = get_context();
1✔
172
         ThrowingAsioStream ssl(ctx, ioc, test_data());
1✔
173
         ssl.next_layer().connect(remote);
1✔
174

175
         error_code ec;
1✔
176
         ssl.handshake(Botan::TLS::Connection_Side::Client, ec);
1✔
177

178
         Test::Result result("sync TLS handshake error");
1✔
179
         result.test_eq("does not activate channel", ssl.native_handle()->is_active(), false);
1✔
180
         result.confirm("propagates error code", ec == ThrowingMockChannel::expected_ec());
3✔
181
         results.push_back(result);
1✔
182
      }
2✔
183

184
      void test_async_handshake(std::vector<Test::Result>& results) {
1✔
185
         net::io_context ioc;
1✔
186
         TestStream remote{ioc};
1✔
187

188
         auto ctx = get_context();
1✔
189
         AsioStream ssl(ctx, ioc, test_data());
1✔
190
         ssl.next_layer().connect(remote);
1✔
191

192
         // mimic handshake initialization
193
         ssl.native_handle()->send(TEST_DATA);
1✔
194

195
         Test::Result result("async TLS handshake");
1✔
196

197
         auto handler = [&](const error_code&) {
2✔
198
            result.confirm("reads from socket", ssl.next_layer().nread() > 0);
2✔
199
            result.confirm("writes from socket", ssl.next_layer().nwrite() > 0);
2✔
200
            result.test_eq("feeds data into channel until active", ssl.native_handle()->is_active(), true);
1✔
201
         };
1✔
202

203
         ssl.async_handshake(Botan::TLS::Connection_Side::Client, handler);
1✔
204

205
         ssl.next_layer().close_remote();
1✔
206
         ioc.run();
1✔
207
         results.push_back(result);
1✔
208
      }
2✔
209

210
      void test_async_handshake_error(std::vector<Test::Result>& results) {
1✔
211
         net::io_context ioc;
1✔
212
         // fail right away
213
         FailCount fc{0, net::error::no_recovery};
1✔
214
         TestStream remote{ioc};
1✔
215

216
         auto ctx = get_context();
1✔
217
         AsioStream ssl(ctx, ioc, fc);
1✔
218
         ssl.next_layer().connect(remote);
1✔
219

220
         // mimic handshake initialization
221
         ssl.native_handle()->send(TEST_DATA);
1✔
222

223
         Test::Result result("async TLS handshake error");
1✔
224

225
         auto handler = [&](const error_code& ec) {
2✔
226
            result.test_eq("does not activate channel", ssl.native_handle()->is_active(), false);
1✔
227
            result.confirm("propagates error code", ec == net::error::no_recovery);
3✔
228
         };
1✔
229

230
         ssl.async_handshake(Botan::TLS::Connection_Side::Client, handler);
1✔
231

232
         ioc.run();
1✔
233
         results.push_back(result);
1✔
234
      }
2✔
235

236
      void test_async_handshake_throw(std::vector<Test::Result>& results) {
1✔
237
         net::io_context ioc;
1✔
238
         TestStream remote{ioc};
1✔
239

240
         auto ctx = get_context();
1✔
241
         ThrowingAsioStream ssl(ctx, ioc, test_data());
1✔
242
         ssl.next_layer().connect(remote);
1✔
243

244
         Test::Result result("async TLS handshake throw");
1✔
245

246
         auto handler = [&](const error_code& ec) {
2✔
247
            result.test_eq("does not activate channel", ssl.native_handle()->is_active(), false);
1✔
248
            result.confirm("propagates error code", ec == ThrowingMockChannel::expected_ec());
3✔
249
         };
1✔
250

251
         ssl.async_handshake(Botan::TLS::Connection_Side::Client, handler);
1✔
252

253
         ioc.run();
1✔
254
         results.push_back(result);
1✔
255
      }
2✔
256

257
      void test_sync_read_some_success(std::vector<Test::Result>& results) {
1✔
258
         net::io_context ioc;
1✔
259

260
         auto ctx = get_context();
1✔
261
         AsioStream ssl(ctx, ioc, test_data());
1✔
262

263
         const std::size_t buf_size = 128;
1✔
264
         uint8_t buf[buf_size];
1✔
265
         error_code ec;
1✔
266

267
         auto bytes_transferred = net::read(ssl, net::mutable_buffer(buf, sizeof(buf)), ec);
1✔
268

269
         Test::Result result("sync read_some success");
1✔
270
         result.confirm("reads the correct data", contains(buf, TEST_DATA, buf_size));
2✔
271
         result.test_eq("reads the correct amount of data", bytes_transferred, buf_size);
1✔
272
         result.confirm("does not report an error", !ec);
2✔
273

274
         results.push_back(result);
1✔
275
      }
2✔
276

277
      void test_sync_read_some_buffer_sequence(std::vector<Test::Result>& results) {
1✔
278
         net::io_context ioc;
1✔
279

280
         auto ctx = get_context();
1✔
281
         AsioStream ssl(ctx, ioc, test_data());
1✔
282
         error_code ec;
1✔
283

284
         std::vector<net::mutable_buffer> data;
1✔
285
         uint8_t buf1[TEST_DATA_SIZE / 2];
1✔
286
         uint8_t buf2[TEST_DATA_SIZE / 2];
1✔
287
         data.emplace_back(net::mutable_buffer(buf1, TEST_DATA_SIZE / 2));
1✔
288
         data.emplace_back(net::mutable_buffer(buf2, TEST_DATA_SIZE / 2));
1✔
289

290
         auto bytes_transferred = net::read(ssl, data, ec);
1✔
291

292
         Test::Result result("sync read_some buffer sequence");
1✔
293

294
         result.confirm("reads the correct data",
2✔
295
                        contains(buf1, TEST_DATA, TEST_DATA_SIZE / 2) &&
1✔
296
                           contains(buf2, TEST_DATA + TEST_DATA_SIZE / 2, TEST_DATA_SIZE / 2));
1✔
297
         result.test_eq("reads the correct amount of data", bytes_transferred, TEST_DATA_SIZE);
1✔
298
         result.confirm("does not report an error", !ec);
2✔
299

300
         results.push_back(result);
1✔
301
      }
3✔
302

303
      void test_sync_read_some_error(std::vector<Test::Result>& results) {
1✔
304
         net::io_context ioc;
1✔
305
         // fail right away
306
         FailCount fc{0, net::error::no_recovery};
1✔
307
         TestStream remote{ioc};
1✔
308

309
         auto ctx = get_context();
1✔
310
         AsioStream ssl(ctx, ioc, fc);
1✔
311
         ssl.next_layer().connect(remote);
1✔
312

313
         uint8_t buf[128];
1✔
314
         error_code ec;
1✔
315

316
         auto bytes_transferred = net::read(ssl, net::mutable_buffer(buf, sizeof(buf)), ec);
1✔
317

318
         Test::Result result("sync read_some error");
1✔
319
         result.test_eq("didn't transfer anything", bytes_transferred, 0);
1✔
320
         result.confirm("propagates error code", ec == net::error::no_recovery);
3✔
321

322
         results.push_back(result);
1✔
323
      }
2✔
324

325
      void test_sync_read_some_throw(std::vector<Test::Result>& results) {
1✔
326
         net::io_context ioc;
1✔
327
         TestStream remote{ioc};
1✔
328

329
         auto ctx = get_context();
1✔
330
         ThrowingAsioStream ssl(ctx, ioc, test_data());
1✔
331
         ssl.next_layer().connect(remote);
1✔
332

333
         uint8_t buf[128];
1✔
334
         error_code ec;
1✔
335

336
         auto bytes_transferred = net::read(ssl, net::mutable_buffer(buf, sizeof(buf)), ec);
1✔
337

338
         Test::Result result("sync read_some throw");
1✔
339
         result.test_eq("didn't transfer anything", bytes_transferred, 0);
1✔
340
         result.confirm("propagates error code", ec == ThrowingMockChannel::expected_ec());
3✔
341

342
         results.push_back(result);
1✔
343
      }
2✔
344

345
      void test_sync_read_zero_buffer(std::vector<Test::Result>& results) {
1✔
346
         net::io_context ioc;
1✔
347

348
         auto ctx = get_context();
1✔
349
         AsioStream ssl(ctx, ioc);
1✔
350

351
         const std::size_t buf_size = 128;
1✔
352
         uint8_t buf[buf_size];
1✔
353
         error_code ec;
1✔
354

355
         auto bytes_transferred = net::read(ssl, net::mutable_buffer(buf, std::size_t(0)), ec);
1✔
356

357
         Test::Result result("sync read_some into zero-size buffer");
1✔
358
         result.test_eq("reads the correct amount of data", bytes_transferred, 0);
1✔
359
         // This relies on an implementation detail of TestStream: A "real" asio::tcp::stream
360
         // would block here. TestStream sets error_code::eof.
361
         result.confirm("does not report an error", !ec);
2✔
362

363
         results.push_back(result);
1✔
364
      }
2✔
365

366
      void test_async_read_some_success(std::vector<Test::Result>& results) {
1✔
367
         net::io_context ioc;
1✔
368
         TestStream remote{ioc};
1✔
369

370
         auto ctx = get_context();
1✔
371
         AsioStream ssl(ctx, ioc, test_data());
1✔
372
         uint8_t data[TEST_DATA_SIZE];
1✔
373

374
         Test::Result result("async read_some success");
1✔
375

376
         auto read_handler = [&](const error_code& ec, std::size_t bytes_transferred) {
2✔
377
            result.confirm("reads the correct data", contains(data, TEST_DATA, TEST_DATA_SIZE));
2✔
378
            result.test_eq("reads the correct amount of data", bytes_transferred, TEST_DATA_SIZE);
1✔
379
            result.confirm("does not report an error", !ec);
2✔
380
         };
1✔
381

382
         net::mutable_buffer buf{data, TEST_DATA_SIZE};
1✔
383
         net::async_read(ssl, buf, read_handler);
1✔
384

385
         ssl.next_layer().close_remote();
1✔
386
         ioc.run();
1✔
387
         results.push_back(result);
1✔
388
      }
2✔
389

390
      void test_async_read_some_buffer_sequence(std::vector<Test::Result>& results) {
1✔
391
         net::io_context ioc;
1✔
392
         auto ctx = get_context();
1✔
393
         AsioStream ssl(ctx, ioc, test_data());
1✔
394

395
         std::vector<net::mutable_buffer> data;
1✔
396
         uint8_t buf1[TEST_DATA_SIZE / 2];
1✔
397
         uint8_t buf2[TEST_DATA_SIZE / 2];
1✔
398
         data.emplace_back(net::mutable_buffer(buf1, TEST_DATA_SIZE / 2));
1✔
399
         data.emplace_back(net::mutable_buffer(buf2, TEST_DATA_SIZE / 2));
1✔
400

401
         Test::Result result("async read_some buffer sequence");
1✔
402

403
         auto read_handler = [&](const error_code& ec, std::size_t bytes_transferred) {
2✔
404
            result.confirm("reads the correct data",
2✔
405
                           contains(buf1, TEST_DATA, TEST_DATA_SIZE / 2) &&
1✔
406
                              contains(buf2, TEST_DATA + TEST_DATA_SIZE / 2, TEST_DATA_SIZE / 2));
1✔
407
            result.test_eq("reads the correct amount of data", bytes_transferred, TEST_DATA_SIZE);
1✔
408
            result.confirm("does not report an error", !ec);
2✔
409
         };
1✔
410

411
         net::async_read(ssl, data, read_handler);
1✔
412

413
         ssl.next_layer().close_remote();
1✔
414
         ioc.run();
1✔
415
         results.push_back(result);
1✔
416
      }
3✔
417

418
      void test_async_read_some_error(std::vector<Test::Result>& results) {
1✔
419
         net::io_context ioc;
1✔
420
         // fail right away
421
         FailCount fc{0, net::error::no_recovery};
1✔
422
         auto ctx = get_context();
1✔
423
         AsioStream ssl(ctx, ioc, fc);
1✔
424
         uint8_t data[TEST_DATA_SIZE];
1✔
425

426
         Test::Result result("async read_some error");
1✔
427

428
         auto read_handler = [&](const error_code& ec, std::size_t bytes_transferred) {
2✔
429
            result.test_eq("didn't transfer anything", bytes_transferred, 0);
1✔
430
            result.confirm("propagates error code", ec == net::error::no_recovery);
3✔
431
         };
1✔
432

433
         net::mutable_buffer buf{data, TEST_DATA_SIZE};
1✔
434
         net::async_read(ssl, buf, read_handler);
1✔
435

436
         ssl.next_layer().close_remote();
1✔
437
         ioc.run();
1✔
438
         results.push_back(result);
1✔
439
      }
2✔
440

441
      void test_async_read_some_throw(std::vector<Test::Result>& results) {
1✔
442
         net::io_context ioc;
1✔
443
         auto ctx = get_context();
1✔
444
         ThrowingAsioStream ssl(ctx, ioc, test_data());
1✔
445
         uint8_t data[TEST_DATA_SIZE];
1✔
446

447
         Test::Result result("async read_some throw");
1✔
448

449
         auto read_handler = [&](const error_code& ec, std::size_t bytes_transferred) {
2✔
450
            result.test_eq("didn't transfer anything", bytes_transferred, 0);
1✔
451
            result.confirm("propagates error code", ec == ThrowingMockChannel::expected_ec());
3✔
452
         };
1✔
453

454
         net::mutable_buffer buf{data, TEST_DATA_SIZE};
1✔
455
         net::async_read(ssl, buf, read_handler);
1✔
456

457
         ssl.next_layer().close_remote();
1✔
458
         ioc.run();
1✔
459
         results.push_back(result);
1✔
460
      }
2✔
461

462
      void test_async_read_zero_buffer(std::vector<Test::Result>& results) {
1✔
463
         net::io_context ioc;
1✔
464
         TestStream remote{ioc};
1✔
465

466
         auto ctx = get_context();
1✔
467
         AsioStream ssl(ctx, ioc);
1✔
468
         uint8_t data[TEST_DATA_SIZE];
1✔
469

470
         Test::Result result("async read_some into zero-size buffer");
1✔
471

472
         auto read_handler = [&](const error_code& ec, std::size_t bytes_transferred) {
2✔
473
            result.test_eq("reads the correct amount of data", bytes_transferred, 0);
1✔
474
            // This relies on an implementation detail of TestStream: A "real" asio::tcp::stream
475
            // would block here. TestStream sets error_code::eof.
476
            result.confirm("does not report an error", !ec);
2✔
477
         };
1✔
478

479
         net::mutable_buffer buf{data, std::size_t(0)};
1✔
480
         net::async_read(ssl, buf, read_handler);
1✔
481

482
         ssl.next_layer().close_remote();
1✔
483
         ioc.run();
1✔
484
         results.push_back(result);
1✔
485
      }
2✔
486

487
      void test_sync_write_some_success(std::vector<Test::Result>& results) {
1✔
488
         net::io_context ioc;
1✔
489
         TestStream remote{ioc};
1✔
490

491
         auto ctx = get_context();
1✔
492
         AsioStream ssl(ctx, ioc);
1✔
493
         ssl.next_layer().connect(remote);
1✔
494
         error_code ec;
1✔
495

496
         auto bytes_transferred = net::write(ssl, net::const_buffer(TEST_DATA, TEST_DATA_SIZE), ec);
1✔
497

498
         Test::Result result("sync write_some success");
1✔
499
         result.confirm("writes the correct data", remote.str() == test_data());
3✔
500
         result.test_eq("writes the correct amount of data", bytes_transferred, TEST_DATA_SIZE);
1✔
501
         result.confirm("does not report an error", !ec);
2✔
502

503
         results.push_back(result);
1✔
504
      }
2✔
505

506
      void test_sync_no_handshake(std::vector<Test::Result>& results) {
1✔
507
         net::io_context ioc;
1✔
508
         TestStream remote{ioc};
1✔
509

510
         auto ctx = get_context();
1✔
511
         Botan::TLS::Stream<TestStream> ssl(ctx, ioc);  // Note that we're not using MockChannel here
1✔
512
         ssl.next_layer().connect(remote);
1✔
513
         error_code ec;
1✔
514

515
         net::write(ssl, net::const_buffer(TEST_DATA, TEST_DATA_SIZE), ec);
1✔
516

517
         Test::Result result("sync write_some without handshake fails gracefully");
1✔
518
         result.confirm("reports an error", ec.failed());
2✔
519

520
         results.push_back(result);
1✔
521
      }
2✔
522

523
      void test_sync_write_some_buffer_sequence(std::vector<Test::Result>& results) {
1✔
524
         net::io_context ioc;
1✔
525
         TestStream remote{ioc};
1✔
526

527
         auto ctx = get_context();
1✔
528
         AsioStream ssl(ctx, ioc);
1✔
529
         ssl.next_layer().connect(remote);
1✔
530
         error_code ec;
1✔
531

532
         // this should be Botan::TLS::MAX_PLAINTEXT_SIZE + 1024 + 1
533
         std::array<uint8_t, 17 * 1024 + 1> random_data;
1✔
534
         random_data.fill('4');  // chosen by fair dice roll
1✔
535
         random_data.back() = '5';
1✔
536

537
         std::vector<net::const_buffer> data;
1✔
538
         data.emplace_back(net::const_buffer(random_data.data(), 1));
1✔
539
         for(std::size_t i = 1; i < random_data.size(); i += 1024) {
18✔
540
            data.emplace_back(net::const_buffer(random_data.data() + i, 1024));
17✔
541
         }
542

543
         auto bytes_transferred = net::write(ssl, data, ec);
1✔
544

545
         Test::Result result("sync write_some buffer sequence");
1✔
546

547
         result.confirm("[precondition] MAX_PLAINTEXT_SIZE is still smaller than random_data.size()",
2✔
548
                        Botan::TLS::MAX_PLAINTEXT_SIZE < random_data.size());
549

550
         result.confirm("writes the correct data",
2✔
551
                        contains(remote.buffer().data().data(), random_data.data(), random_data.size()));
1✔
552
         result.test_eq("writes the correct amount of data", bytes_transferred, random_data.size());
1✔
553
         result.test_eq("correct number of writes", ssl.next_layer().nwrite(), 2);
1✔
554
         result.confirm("does not report an error", !ec);
2✔
555

556
         results.push_back(result);
1✔
557
      }
3✔
558

559
      void test_sync_write_some_error(std::vector<Test::Result>& results) {
1✔
560
         net::io_context ioc;
1✔
561
         // fail right away
562
         FailCount fc{0, net::error::no_recovery};
1✔
563
         TestStream remote{ioc};
1✔
564

565
         auto ctx = get_context();
1✔
566
         AsioStream ssl(ctx, ioc, fc);
1✔
567
         ssl.next_layer().connect(remote);
1✔
568

569
         error_code ec;
1✔
570

571
         auto bytes_transferred = net::write(ssl, net::const_buffer(TEST_DATA, TEST_DATA_SIZE), ec);
1✔
572

573
         Test::Result result("sync write_some error");
1✔
574
         result.test_eq("didn't transfer anything", bytes_transferred, 0);
1✔
575
         result.confirm("propagates error code", ec == net::error::no_recovery);
3✔
576

577
         results.push_back(result);
1✔
578
      }
2✔
579

580
      void test_sync_write_some_throw(std::vector<Test::Result>& results) {
1✔
581
         net::io_context ioc;
1✔
582
         TestStream remote{ioc};
1✔
583

584
         auto ctx = get_context();
1✔
585
         ThrowingAsioStream ssl(ctx, ioc);
1✔
586
         ssl.next_layer().connect(remote);
1✔
587
         error_code ec;
1✔
588

589
         auto bytes_transferred = net::write(ssl, net::const_buffer(TEST_DATA, TEST_DATA_SIZE), ec);
1✔
590

591
         Test::Result result("sync write_some throw");
1✔
592
         result.test_eq("didn't transfer anything", bytes_transferred, 0);
1✔
593
         result.confirm("propagates error code", ec == ThrowingMockChannel::expected_ec());
3✔
594

595
         results.push_back(result);
1✔
596
      }
2✔
597

598
      void test_async_write_some_success(std::vector<Test::Result>& results) {
1✔
599
         net::io_context ioc;
1✔
600
         TestStream remote{ioc};
1✔
601

602
         auto ctx = get_context();
1✔
603
         AsioStream ssl(ctx, ioc);
1✔
604
         ssl.next_layer().connect(remote);
1✔
605

606
         Test::Result result("async write_some success");
1✔
607

608
         auto write_handler = [&](const error_code& ec, std::size_t bytes_transferred) {
2✔
609
            result.confirm("writes the correct data", remote.str() == test_data());
3✔
610
            result.test_eq("writes the correct amount of data", bytes_transferred, TEST_DATA_SIZE);
1✔
611
            result.confirm("does not report an error", !ec);
2✔
612
         };
1✔
613

614
         net::async_write(ssl, net::const_buffer(TEST_DATA, TEST_DATA_SIZE), write_handler);
1✔
615

616
         ioc.run();
1✔
617
         results.push_back(result);
1✔
618
      }
2✔
619

620
      void test_async_write_some_buffer_sequence(std::vector<Test::Result>& results) {
1✔
621
         net::io_context ioc;
1✔
622
         TestStream remote{ioc};
1✔
623

624
         auto ctx = get_context();
1✔
625
         AsioStream ssl(ctx, ioc);
1✔
626
         ssl.next_layer().connect(remote);
1✔
627

628
         // this should be Botan::TLS::MAX_PLAINTEXT_SIZE + 1024 + 1
629
         std::array<uint8_t, 17 * 1024 + 1> random_data;
1✔
630
         random_data.fill('4');  // chosen by fair dice roll
1✔
631
         random_data.back() = '5';
1✔
632

633
         std::vector<net::const_buffer> src;
1✔
634
         src.emplace_back(net::const_buffer(random_data.data(), 1));
1✔
635
         for(std::size_t i = 1; i < random_data.size(); i += 1024) {
18✔
636
            src.emplace_back(net::const_buffer(random_data.data() + i, 1024));
17✔
637
         }
638

639
         Test::Result result("async write_some buffer sequence");
1✔
640

641
         result.confirm("[precondition] MAX_PLAINTEXT_SIZE is still smaller than random_data.size()",
2✔
642
                        Botan::TLS::MAX_PLAINTEXT_SIZE < random_data.size());
643

644
         auto write_handler = [&](const error_code& ec, std::size_t bytes_transferred) {
2✔
645
            result.confirm("writes the correct data",
2✔
646
                           contains(remote.buffer().data().data(), random_data.data(), random_data.size()));
1✔
647
            result.test_eq("writes the correct amount of data", bytes_transferred, random_data.size());
1✔
648
            result.test_eq("correct number of writes", ssl.next_layer().nwrite(), 2);
1✔
649
            result.confirm("does not report an error", !ec);
2✔
650
         };
1✔
651

652
         net::async_write(ssl, src, write_handler);
1✔
653

654
         ioc.run();
1✔
655
         results.push_back(result);
1✔
656
      }
3✔
657

658
      void test_async_write_some_error(std::vector<Test::Result>& results) {
1✔
659
         net::io_context ioc;
1✔
660
         // fail right away
661
         FailCount fc{0, net::error::no_recovery};
1✔
662
         TestStream remote{ioc};
1✔
663

664
         auto ctx = get_context();
1✔
665
         AsioStream ssl(ctx, ioc, fc);
1✔
666
         ssl.next_layer().connect(remote);
1✔
667

668
         Test::Result result("async write_some error");
1✔
669

670
         auto write_handler = [&](const error_code& ec, std::size_t bytes_transferred) {
2✔
671
            result.test_eq("committed some bytes to the core", bytes_transferred, TEST_DATA_SIZE);
1✔
672
            result.confirm("propagates error code", ec == net::error::no_recovery);
3✔
673
         };
1✔
674

675
         net::async_write(ssl, net::const_buffer(TEST_DATA, TEST_DATA_SIZE), write_handler);
1✔
676

677
         ioc.run();
1✔
678
         results.push_back(result);
1✔
679
      }
2✔
680

681
      void test_async_write_throw(std::vector<Test::Result>& results) {
1✔
682
         net::io_context ioc;
1✔
683
         TestStream remote{ioc};
1✔
684

685
         auto ctx = get_context();
1✔
686
         ThrowingAsioStream ssl(ctx, ioc);
1✔
687
         ssl.next_layer().connect(remote);
1✔
688

689
         Test::Result result("async write_some throw");
1✔
690

691
         auto write_handler = [&](const error_code& ec, std::size_t bytes_transferred) {
2✔
692
            result.test_eq("didn't transfer anything", bytes_transferred, 0);
1✔
693
            result.confirm("propagates error code", ec == ThrowingMockChannel::expected_ec());
3✔
694
         };
1✔
695

696
         net::async_write(ssl, net::const_buffer(TEST_DATA, TEST_DATA_SIZE), write_handler);
1✔
697

698
         ioc.run();
1✔
699
         results.push_back(result);
1✔
700
      }
2✔
701

702
   public:
703
      std::vector<Test::Result> run() override {
1✔
704
         std::vector<Test::Result> results;
1✔
705

706
         test_sync_no_handshake(results);
1✔
707

708
         test_sync_handshake(results);
1✔
709
         test_sync_handshake_error(results);
1✔
710
         test_sync_handshake_throw(results);
1✔
711

712
         test_async_handshake(results);
1✔
713
         test_async_handshake_error(results);
1✔
714
         test_async_handshake_throw(results);
1✔
715

716
         test_sync_read_some_success(results);
1✔
717
         test_sync_read_some_buffer_sequence(results);
1✔
718
         test_sync_read_some_error(results);
1✔
719
         test_sync_read_some_throw(results);
1✔
720
         test_sync_read_zero_buffer(results);
1✔
721

722
         test_async_read_some_success(results);
1✔
723
         test_async_read_some_buffer_sequence(results);
1✔
724
         test_async_read_some_error(results);
1✔
725
         test_async_read_some_throw(results);
1✔
726
         test_async_read_zero_buffer(results);
1✔
727

728
         test_sync_write_some_success(results);
1✔
729
         test_sync_write_some_buffer_sequence(results);
1✔
730
         test_sync_write_some_error(results);
1✔
731
         test_sync_write_some_throw(results);
1✔
732

733
         test_async_write_some_success(results);
1✔
734
         test_async_write_some_buffer_sequence(results);
1✔
735
         test_async_write_some_error(results);
1✔
736
         test_async_write_throw(results);
1✔
737

738
         return results;
1✔
739
      }
×
740
};
741

742
BOTAN_REGISTER_TEST("tls", "tls_asio_stream", Asio_Stream_Tests);
743

744
}  // namespace Botan_Tests
745

746
   #endif  // BOOST_VERSION
747
#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