-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathssl_test.cpp
58 lines (43 loc) · 1.74 KB
/
ssl_test.cpp
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
#include "catch/single_include/catch.hpp"
#include "../include/twisted/reactor.hpp"
#include "../include/twisted/basic_protocol.hpp"
#include <boost/asio/read.hpp>
#include <boost/scope_exit.hpp>
#include <vector>
#include <string>
#include <future>
using boost::asio::ip::tcp;
struct echo_protocol : twisted::basic_protocol<echo_protocol> {
void on_message(const_buffer_iterator begin, const_buffer_iterator end) {
send_message(begin, end);
}
};
TEST_CASE("basic ssl send & recv test", "[ssl][reactor]") {
twisted::reactor reac;
auto fut = std::async(std::launch::async, [&]() {
reac.listen_ssl<echo_protocol>(
50000, twisted::default_ssl_options("server.crt", "server.key", "ikn1"));
reac.run();
});
boost::asio::ssl::context context(boost::asio::ssl::context::sslv23);
context.use_certificate_chain_file("server.crt");
boost::asio::io_service io_service;
boost::asio::ssl::stream<boost::asio::ip::tcp::socket> socket(io_service,
context);
BOOST_SCOPE_EXIT_TPL((&reac)(&socket)) {
reac.stop();
if (socket.lowest_layer().is_open()) {
socket.lowest_layer().close();
}
}
BOOST_SCOPE_EXIT_END
std::this_thread::sleep_for(std::chrono::milliseconds(3));
socket.lowest_layer().connect(tcp::endpoint(
boost::asio::ip::address::from_string("127.0.0.1"), 50000));
socket.handshake(boost::asio::ssl::stream_base::client);
std::string input("TEST123");
boost::asio::write(socket, boost::asio::buffer(input));
std::string buffer(input.size(), '\0');
boost::asio::read(socket, boost::asio::buffer(&buffer[0], buffer.size()));
CHECK(buffer == input);
}