1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
|
/*
* TLS ASIO Stream Unit Tests
* (C) 2018-2019 Jack Lloyd
* 2018-2019 Hannes Rantzsch, Tim Oesterreich, Rene Meusel
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#include "tests.h"
#if defined(BOTAN_HAS_TLS) && defined(BOTAN_HAS_BOOST_ASIO)
#include <botan/asio_stream.h>
#include <botan/tls_callbacks.h>
// first boost version to include boost/beast/experimental/test/stream.hpp
#include <boost/version.hpp>
#if BOOST_VERSION >= 106800
#include <boost/beast/experimental/test/stream.hpp>
#include <boost/bind.hpp>
namespace Botan_Tests {
namespace net = boost::asio;
using error_code = boost::system::error_code;
constexpr uint8_t TEST_DATA[] = "The story so far: In the beginning the Universe was created. "
"This has made a lot of people very angry and been widely regarded as a bad move.";
constexpr std::size_t TEST_DATA_SIZE = 142;
static_assert(sizeof(TEST_DATA) == TEST_DATA_SIZE, "size of TEST_DATA must match TEST_DATA_SIZE");
/**
* Mocked Botan::TLS::Channel. Pretends to perform TLS operations and triggers appropriate callbacks in StreamCore.
*/
class MockChannel
{
public:
MockChannel(Botan::TLS::Callbacks& core)
: m_callbacks(core)
, m_bytes_till_complete_record(TEST_DATA_SIZE)
, m_active(false) {}
public:
std::size_t received_data(const uint8_t[], std::size_t buf_size)
{
if(m_bytes_till_complete_record <= buf_size)
{
m_callbacks.tls_record_received(0, TEST_DATA, TEST_DATA_SIZE);
m_active = true; // claim to be active once a full record has been received (for handshake test)
return 0;
}
m_bytes_till_complete_record -= buf_size;
return m_bytes_till_complete_record;
}
void send(const uint8_t buf[], std::size_t buf_size) { m_callbacks.tls_emit_data(buf, buf_size); }
bool is_active() { return m_active; }
protected:
Botan::TLS::Callbacks& m_callbacks;
std::size_t m_bytes_till_complete_record; // number of bytes still to read before tls record is completed
bool m_active;
};
class ThrowingMockChannel : public MockChannel
{
public:
static boost::system::error_code expected_ec()
{
return Botan::TLS::Alert::UNEXPECTED_MESSAGE;
}
ThrowingMockChannel(Botan::TLS::Callbacks& core) : MockChannel(core)
{
}
std::size_t received_data(const uint8_t[], std::size_t)
{
throw Botan::TLS::Unexpected_Message("test_error");
}
void send(const uint8_t[], std::size_t)
{
throw Botan::TLS::Unexpected_Message("test_error");
}
};
using TestStream = boost::beast::test::stream;
using FailCount = boost::beast::test::fail_count;
class AsioStream : public Botan::TLS::Stream<TestStream, MockChannel>
{
public:
template <typename... Args>
AsioStream(Botan::TLS::Context& context, Args&& ... args)
: Stream(context, args...)
{
m_native_handle = std::unique_ptr<MockChannel>(new MockChannel(m_core));
}
virtual ~AsioStream() = default;
};
class ThrowingAsioStream : public Botan::TLS::Stream<TestStream, ThrowingMockChannel>
{
public:
template <typename... Args>
ThrowingAsioStream(Botan::TLS::Context& context, Args&& ... args)
: Stream(context, args...)
{
m_native_handle = std::unique_ptr<ThrowingMockChannel>(new ThrowingMockChannel(m_core));
}
virtual ~ThrowingAsioStream() = default;
};
/**
* Synchronous tests for Botan::Stream.
*
* This test validates the asynchronous behavior Botan::Stream, including its utility classes StreamCore and Async_*_Op.
* The stream's channel, i.e. TLS_Client or TLS_Server, is mocked and pretends to perform TLS operations (noop) and
* provides the test data to the stream.
* The underlying network socket, claiming it read / wrote a number of bytes.
*/
class Asio_Stream_Tests final : public Test
{
Botan::Credentials_Manager m_credentials_manager;
Botan::Null_RNG m_rng;
Botan::TLS::Session_Manager_Noop m_session_manager;
Botan::TLS::Default_Policy m_policy;
Botan::TLS::Context get_context()
{
return Botan::TLS::Context(m_credentials_manager, m_rng, m_session_manager, m_policy);
}
// use memcmp to check if the data in a is a prefix of the data in b
bool contains(const void* a, const void* b, const std::size_t size) { return memcmp(a, b, size) == 0; }
boost::string_view test_data() const
{
return boost::string_view(reinterpret_cast<const char*>(TEST_DATA), TEST_DATA_SIZE);
}
void test_sync_handshake(std::vector<Test::Result>& results)
{
net::io_context ioc;
auto ctx = get_context();
AsioStream ssl(ctx, ioc, test_data());
ssl.handshake(Botan::TLS::CLIENT);
Test::Result result("sync TLS handshake");
result.test_eq("feeds data into channel until active", ssl.native_handle()->is_active(), true);
results.push_back(result);
}
void test_sync_handshake_error(std::vector<Test::Result>& results)
{
net::io_context ioc;
// fail right away
FailCount fc{0, net::error::eof};
TestStream remote{ioc};
auto ctx = get_context();
AsioStream ssl(ctx, ioc, fc);
ssl.next_layer().connect(remote);
// mimic handshake initialization
ssl.native_handle()->send(TEST_DATA, TEST_DATA_SIZE);
error_code ec;
ssl.handshake(Botan::TLS::CLIENT, ec);
Test::Result result("sync TLS handshake error");
result.test_eq("does not activate channel", ssl.native_handle()->is_active(), false);
result.confirm("propagates error code", ec == net::error::eof);
results.push_back(result);
}
void test_sync_handshake_throw(std::vector<Test::Result>& results)
{
net::io_context ioc;
TestStream remote{ioc};
auto ctx = get_context();
ThrowingAsioStream ssl(ctx, ioc, test_data());
ssl.next_layer().connect(remote);
error_code ec;
ssl.handshake(Botan::TLS::CLIENT, ec);
Test::Result result("sync TLS handshake error");
result.test_eq("does not activate channel", ssl.native_handle()->is_active(), false);
result.confirm("propagates error code", ec == ThrowingMockChannel::expected_ec());
results.push_back(result);
}
void test_async_handshake(std::vector<Test::Result>& results)
{
net::io_context ioc;
TestStream remote{ioc};
auto ctx = get_context();
AsioStream ssl(ctx, ioc, test_data());
ssl.next_layer().connect(remote);
// mimic handshake initialization
ssl.native_handle()->send(TEST_DATA, TEST_DATA_SIZE);
Test::Result result("async TLS handshake");
auto handler = [&](const error_code&)
{
result.confirm("reads from socket", ssl.next_layer().nread() > 0);
result.confirm("writes from socket", ssl.next_layer().nwrite() > 0);
result.test_eq("feeds data into channel until active", ssl.native_handle()->is_active(), true);
};
ssl.async_handshake(Botan::TLS::CLIENT, handler);
ssl.next_layer().close_remote();
ioc.run();
results.push_back(result);
}
void test_async_handshake_error(std::vector<Test::Result>& results)
{
net::io_context ioc;
// fail right away
FailCount fc{0, net::error::eof};
TestStream remote{ioc};
auto ctx = get_context();
AsioStream ssl(ctx, ioc, fc);
ssl.next_layer().connect(remote);
// mimic handshake initialization
ssl.native_handle()->send(TEST_DATA, TEST_DATA_SIZE);
Test::Result result("async TLS handshake error");
auto handler = [&](const error_code &ec)
{
result.test_eq("does not activate channel", ssl.native_handle()->is_active(), false);
result.confirm("propagates error code", ec == net::error::eof);
};
ssl.async_handshake(Botan::TLS::CLIENT, handler);
ioc.run();
results.push_back(result);
}
void test_async_handshake_throw(std::vector<Test::Result>& results)
{
net::io_context ioc;
TestStream remote{ioc};
auto ctx = get_context();
ThrowingAsioStream ssl(ctx, ioc, test_data());
ssl.next_layer().connect(remote);
Test::Result result("async TLS handshake throw");
auto handler = [&](const error_code &ec)
{
result.test_eq("does not activate channel", ssl.native_handle()->is_active(), false);
result.confirm("propagates error code", ec == ThrowingMockChannel::expected_ec());
};
ssl.async_handshake(Botan::TLS::CLIENT, handler);
ioc.run();
results.push_back(result);
}
void test_sync_read_some_success(std::vector<Test::Result>& results)
{
net::io_context ioc;
auto ctx = get_context();
AsioStream ssl(ctx, ioc, test_data());
const std::size_t buf_size = 128;
uint8_t buf[buf_size];
error_code ec;
auto bytes_transferred = net::read(ssl, net::mutable_buffer(buf, sizeof(buf)), ec);
Test::Result result("sync read_some success");
result.confirm("reads the correct data", contains(buf, TEST_DATA, buf_size));
result.test_eq("reads the correct amount of data", bytes_transferred, buf_size);
result.confirm("does not report an error", !ec);
results.push_back(result);
}
void test_sync_read_some_buffer_sequence(std::vector<Test::Result>& results)
{
net::io_context ioc;
auto ctx = get_context();
AsioStream ssl(ctx, ioc, test_data());
error_code ec;
std::vector<net::mutable_buffer> data;
uint8_t buf1[TEST_DATA_SIZE/2];
uint8_t buf2[TEST_DATA_SIZE/2];
data.emplace_back(net::mutable_buffer(buf1, TEST_DATA_SIZE/2));
data.emplace_back(net::mutable_buffer(buf2, TEST_DATA_SIZE/2));
auto bytes_transferred = net::read(ssl, data, ec);
Test::Result result("sync read_some buffer sequence");
result.confirm("reads the correct data",
contains(buf1, TEST_DATA, TEST_DATA_SIZE/2) &&
contains(buf2, TEST_DATA+TEST_DATA_SIZE/2, TEST_DATA_SIZE/2));
result.test_eq("reads the correct amount of data", bytes_transferred, TEST_DATA_SIZE);
result.confirm("does not report an error", !ec);
results.push_back(result);
}
void test_sync_read_some_error(std::vector<Test::Result>& results)
{
net::io_context ioc;
// fail right away
FailCount fc{0, net::error::eof};
TestStream remote{ioc};
auto ctx = get_context();
AsioStream ssl(ctx, ioc, fc);
ssl.next_layer().connect(remote);
uint8_t buf[128];
error_code ec;
auto bytes_transferred = net::read(ssl, net::mutable_buffer(buf, sizeof(buf)), ec);
Test::Result result("sync read_some error");
result.test_eq("didn't transfer anything", bytes_transferred, 0);
result.confirm("propagates error code", ec == net::error::eof);
results.push_back(result);
}
void test_sync_read_some_throw(std::vector<Test::Result>& results)
{
net::io_context ioc;
TestStream remote{ioc};
auto ctx = get_context();
ThrowingAsioStream ssl(ctx, ioc, test_data());
ssl.next_layer().connect(remote);
uint8_t buf[128];
error_code ec;
auto bytes_transferred = net::read(ssl, net::mutable_buffer(buf, sizeof(buf)), ec);
Test::Result result("sync read_some throw");
result.test_eq("didn't transfer anything", bytes_transferred, 0);
result.confirm("propagates error code", ec == ThrowingMockChannel::expected_ec());
results.push_back(result);
}
void test_sync_read_zero_buffer(std::vector<Test::Result>& results)
{
net::io_context ioc;
auto ctx = get_context();
AsioStream ssl(ctx, ioc);
const std::size_t buf_size = 128;
uint8_t buf[buf_size];
error_code ec;
auto bytes_transferred = net::read(ssl, net::mutable_buffer(buf, std::size_t(0)), ec);
Test::Result result("sync read_some into zero-size buffer");
result.test_eq("reads the correct amount of data", bytes_transferred, 0);
// This relies on an implementation detail of TestStream: A "real" asio::tcp::stream
// would block here. TestStream sets error_code::eof.
result.confirm("does not report an error", !ec);
results.push_back(result);
}
void test_async_read_some_success(std::vector<Test::Result>& results)
{
net::io_context ioc;
TestStream remote{ioc};
auto ctx = get_context();
AsioStream ssl(ctx, ioc, test_data());
uint8_t data[TEST_DATA_SIZE];
Test::Result result("async read_some success");
auto read_handler = [&](const error_code &ec, std::size_t bytes_transferred)
{
result.confirm("reads the correct data", contains(data, TEST_DATA, TEST_DATA_SIZE));
result.test_eq("reads the correct amount of data", bytes_transferred, TEST_DATA_SIZE);
result.confirm("does not report an error", !ec);
};
net::mutable_buffer buf {data, TEST_DATA_SIZE};
net::async_read(ssl, buf, read_handler);
ssl.next_layer().close_remote();
ioc.run();
results.push_back(result);
}
void test_async_read_some_buffer_sequence(std::vector<Test::Result>& results)
{
net::io_context ioc;
auto ctx = get_context();
AsioStream ssl(ctx, ioc, test_data());
std::vector<net::mutable_buffer> data;
uint8_t buf1[TEST_DATA_SIZE/2];
uint8_t buf2[TEST_DATA_SIZE/2];
data.emplace_back(net::mutable_buffer(buf1, TEST_DATA_SIZE/2));
data.emplace_back(net::mutable_buffer(buf2, TEST_DATA_SIZE/2));
Test::Result result("async read_some buffer sequence");
auto read_handler = [&](const error_code &ec, std::size_t bytes_transferred)
{
result.confirm("reads the correct data",
contains(buf1, TEST_DATA, TEST_DATA_SIZE/2) &&
contains(buf2, TEST_DATA+TEST_DATA_SIZE/2, TEST_DATA_SIZE/2));
result.test_eq("reads the correct amount of data", bytes_transferred, TEST_DATA_SIZE);
result.confirm("does not report an error", !ec);
};
net::async_read(ssl, data, read_handler);
ssl.next_layer().close_remote();
ioc.run();
results.push_back(result);
}
void test_async_read_some_error(std::vector<Test::Result>& results)
{
net::io_context ioc;
// fail right away
FailCount fc{0, net::error::eof};
auto ctx = get_context();
AsioStream ssl(ctx, ioc, fc);
uint8_t data[TEST_DATA_SIZE];
Test::Result result("async read_some error");
auto read_handler = [&](const error_code &ec, std::size_t bytes_transferred)
{
result.test_eq("didn't transfer anything", bytes_transferred, 0);
result.confirm("propagates error code", ec == net::error::eof);
};
net::mutable_buffer buf {data, TEST_DATA_SIZE};
net::async_read(ssl, buf, read_handler);
ssl.next_layer().close_remote();
ioc.run();
results.push_back(result);
}
void test_async_read_some_throw(std::vector<Test::Result>& results)
{
net::io_context ioc;
auto ctx = get_context();
ThrowingAsioStream ssl(ctx, ioc, test_data());
uint8_t data[TEST_DATA_SIZE];
Test::Result result("async read_some throw");
auto read_handler = [&](const error_code &ec, std::size_t bytes_transferred)
{
result.test_eq("didn't transfer anything", bytes_transferred, 0);
result.confirm("propagates error code", ec == ThrowingMockChannel::expected_ec());
};
net::mutable_buffer buf {data, TEST_DATA_SIZE};
net::async_read(ssl, buf, read_handler);
ssl.next_layer().close_remote();
ioc.run();
results.push_back(result);
}
void test_async_read_zero_buffer(std::vector<Test::Result>& results)
{
net::io_context ioc;
TestStream remote{ioc};
auto ctx = get_context();
AsioStream ssl(ctx, ioc);
uint8_t data[TEST_DATA_SIZE];
Test::Result result("async read_some into zero-size buffer");
auto read_handler = [&](const error_code &ec, std::size_t bytes_transferred)
{
result.test_eq("reads the correct amount of data", bytes_transferred, 0);
// This relies on an implementation detail of TestStream: A "real" asio::tcp::stream
// would block here. TestStream sets error_code::eof.
result.confirm("does not report an error", !ec);
};
net::mutable_buffer buf {data, std::size_t(0)};
net::async_read(ssl, buf, read_handler);
ssl.next_layer().close_remote();
ioc.run();
results.push_back(result);
}
void test_sync_write_some_success(std::vector<Test::Result>& results)
{
net::io_context ioc;
TestStream remote{ioc};
auto ctx = get_context();
AsioStream ssl(ctx, ioc);
ssl.next_layer().connect(remote);
error_code ec;
auto bytes_transferred = net::write(ssl, net::const_buffer(TEST_DATA, TEST_DATA_SIZE), ec);
Test::Result result("sync write_some success");
result.confirm("writes the correct data", remote.str() == test_data());
result.test_eq("writes the correct amount of data", bytes_transferred, TEST_DATA_SIZE);
result.confirm("does not report an error", !ec);
results.push_back(result);
}
void test_sync_write_some_buffer_sequence(std::vector<Test::Result>& results)
{
net::io_context ioc;
TestStream remote{ioc};
auto ctx = get_context();
AsioStream ssl(ctx, ioc);
ssl.next_layer().connect(remote);
error_code ec;
// this should be Botan::TLS::MAX_PLAINTEXT_SIZE + 1024 + 1
std::array<uint8_t, 17 * 1024 + 1> random_data;
random_data.fill('4'); // chosen by fair dice roll
random_data.back() = '5';
std::vector<net::const_buffer> data;
data.emplace_back(net::const_buffer(random_data.data(), 1));
for(std::size_t i = 1; i < random_data.size(); i += 1024)
{
data.emplace_back(net::const_buffer(random_data.data() + i, 1024));
}
auto bytes_transferred = net::write(ssl, data, ec);
Test::Result result("sync write_some buffer sequence");
result.confirm("[precondition] MAX_PLAINTEXT_SIZE is still smaller than random_data.size()",
Botan::TLS::MAX_PLAINTEXT_SIZE < random_data.size());
result.confirm("writes the correct data",
contains(remote.buffer().data().data(), random_data.data(), random_data.size()));
result.test_eq("writes the correct amount of data", bytes_transferred, random_data.size());
result.test_eq("correct number of writes", ssl.next_layer().nwrite(), 2);
result.confirm("does not report an error", !ec);
results.push_back(result);
}
void test_sync_write_some_error(std::vector<Test::Result>& results)
{
net::io_context ioc;
// fail right away
FailCount fc{0, net::error::eof};
TestStream remote{ioc};
auto ctx = get_context();
AsioStream ssl(ctx, ioc, fc);
ssl.next_layer().connect(remote);
error_code ec;
auto bytes_transferred = net::write(ssl, net::const_buffer(TEST_DATA, TEST_DATA_SIZE), ec);
Test::Result result("sync write_some error");
result.test_eq("didn't transfer anything", bytes_transferred, 0);
result.confirm("propagates error code", ec == net::error::eof);
results.push_back(result);
}
void test_sync_write_some_throw(std::vector<Test::Result>& results)
{
net::io_context ioc;
TestStream remote{ioc};
auto ctx = get_context();
ThrowingAsioStream ssl(ctx, ioc);
ssl.next_layer().connect(remote);
error_code ec;
auto bytes_transferred = net::write(ssl, net::const_buffer(TEST_DATA, TEST_DATA_SIZE), ec);
Test::Result result("sync write_some throw");
result.test_eq("didn't transfer anything", bytes_transferred, 0);
result.confirm("propagates error code", ec == ThrowingMockChannel::expected_ec());
results.push_back(result);
}
void test_async_write_some_success(std::vector<Test::Result>& results)
{
net::io_context ioc;
TestStream remote{ioc};
auto ctx = get_context();
AsioStream ssl(ctx, ioc);
ssl.next_layer().connect(remote);
Test::Result result("async write_some success");
auto write_handler = [&](const error_code &ec, std::size_t bytes_transferred)
{
result.confirm("writes the correct data", remote.str() == test_data());
result.test_eq("writes the correct amount of data", bytes_transferred, TEST_DATA_SIZE);
result.confirm("does not report an error", !ec);
};
net::async_write(ssl, net::const_buffer(TEST_DATA, TEST_DATA_SIZE), write_handler);
ioc.run();
results.push_back(result);
}
void test_async_write_some_buffer_sequence(std::vector<Test::Result>& results)
{
net::io_context ioc;
TestStream remote{ioc};
auto ctx = get_context();
AsioStream ssl(ctx, ioc);
ssl.next_layer().connect(remote);
// this should be Botan::TLS::MAX_PLAINTEXT_SIZE + 1024 + 1
std::array<uint8_t, 17 * 1024 + 1> random_data;
random_data.fill('4'); // chosen by fair dice roll
random_data.back() = '5';
std::vector<net::const_buffer> src;
src.emplace_back(net::const_buffer(random_data.data(), 1));
for(std::size_t i = 1; i < random_data.size(); i += 1024)
{
src.emplace_back(net::const_buffer(random_data.data() + i, 1024));
}
Test::Result result("async write_some buffer sequence");
result.confirm("[precondition] MAX_PLAINTEXT_SIZE is still smaller than random_data.size()",
Botan::TLS::MAX_PLAINTEXT_SIZE < random_data.size());
auto write_handler = [&](const error_code &ec, std::size_t bytes_transferred)
{
result.confirm("writes the correct data",
contains(remote.buffer().data().data(), random_data.data(), random_data.size()));
result.test_eq("writes the correct amount of data", bytes_transferred, random_data.size());
result.test_eq("correct number of writes", ssl.next_layer().nwrite(), 2);
result.confirm("does not report an error", !ec);
};
net::async_write(ssl, src, write_handler);
ioc.run();
results.push_back(result);
}
void test_async_write_some_error(std::vector<Test::Result>& results)
{
net::io_context ioc;
// fail right away
FailCount fc{0, net::error::eof};
TestStream remote{ioc};
auto ctx = get_context();
AsioStream ssl(ctx, ioc, fc);
ssl.next_layer().connect(remote);
Test::Result result("async write_some error");
auto write_handler = [&](const error_code &ec, std::size_t bytes_transferred)
{
result.test_eq("committed some bytes to the core", bytes_transferred, TEST_DATA_SIZE);
result.confirm("propagates error code", ec == net::error::eof);
};
net::async_write(ssl, net::const_buffer(TEST_DATA, TEST_DATA_SIZE), write_handler);
ioc.run();
results.push_back(result);
}
void test_async_write_throw(std::vector<Test::Result>& results)
{
net::io_context ioc;
TestStream remote{ioc};
auto ctx = get_context();
ThrowingAsioStream ssl(ctx, ioc);
ssl.next_layer().connect(remote);
Test::Result result("async write_some throw");
auto write_handler = [&](const error_code &ec, std::size_t bytes_transferred)
{
result.test_eq("didn't transfer anything", bytes_transferred, 0);
result.confirm("propagates error code", ec == ThrowingMockChannel::expected_ec());
};
net::async_write(ssl, net::const_buffer(TEST_DATA, TEST_DATA_SIZE), write_handler);
ioc.run();
results.push_back(result);
}
public:
std::vector<Test::Result> run() override
{
std::vector<Test::Result> results;
test_sync_handshake(results);
test_sync_handshake_error(results);
test_sync_handshake_throw(results);
test_async_handshake(results);
test_async_handshake_error(results);
test_async_handshake_throw(results);
test_sync_read_some_success(results);
test_sync_read_some_buffer_sequence(results);
test_sync_read_some_error(results);
test_sync_read_some_throw(results);
test_sync_read_zero_buffer(results);
test_async_read_some_success(results);
test_async_read_some_buffer_sequence(results);
test_async_read_some_error(results);
test_async_read_some_throw(results);
test_async_read_zero_buffer(results);
test_sync_write_some_success(results);
test_sync_write_some_buffer_sequence(results);
test_sync_write_some_error(results);
test_sync_write_some_throw(results);
test_async_write_some_success(results);
test_async_write_some_buffer_sequence(results);
test_async_write_some_error(results);
test_async_write_throw(results);
return results;
}
};
BOTAN_REGISTER_TEST("tls_asio_stream", Asio_Stream_Tests);
} // namespace Botan_Tests
#endif // BOOST_VERSION
#endif // BOTAN_HAS_TLS && BOTAN_HAS_BOOST_ASIO
|