void c_netuser::send_token_bynet(const std::string &ip_address, int port) { DBG_MTX(dbg_mtx, "START"); boost::system::error_code ec; ip::address addr = ip::address::from_string(ip_address, ec); //if(ec) { ///< boost error - not needed // throw std::runtime_error("bad ip"); //} if (!addr.is_v6()) { std::string msg = addr.to_string(); msg += ec.message()+" : is not valid IPv6 address"; throw std::invalid_argument(msg); } ip::tcp::endpoint server_endpoint(addr, port); ip::tcp::socket socket_(m_io_service); socket_.connect(server_endpoint, ec); if (ec) { DBG_MTX(dbg_mtx,"EC = " << ec); throw std::runtime_error("send_token_bynet -- fail to connect"); } DBG_MTX(dbg_mtx, "getting remote public key"); send_public_key_req(socket_); ed_key remote_public_key(get_public_key_resp(socket_)); DBG_MTX(dbg_mtx, "remote public key " << remote_public_key); std::string packet = get_token_packet(serialization::Json, remote_public_key); if (packet == "fail") { DBG_MTX(dbg_mtx,"stop sending token -- empty wallet"); return; } send_coin(socket_, packet); socket_.close(); }
//---------------------------------------------------------------------------- TEST_F(FiberTest, SSLconnectDisconnectFiberFromClient) { boost::log::core::get()->set_filter(boost::log::trivial::severity >= boost::log::trivial::info); Wait(); typedef boost::asio::ssl::stream<fiber> ssl_fiber_t; boost::asio::ssl::context ctx(boost::asio::ssl::context::tlsv12); std::function<bool(bool, boost::asio::ssl::verify_context&)> verify_callback = [](bool preverified, boost::asio::ssl::verify_context& ctx) { BOOST_LOG_TRIVIAL(debug) << "------------------------------" << std::endl; X509_STORE_CTX* cts = ctx.native_handle(); X509* cert = X509_STORE_CTX_get_current_cert(ctx.native_handle()); char subject_name[256]; auto err = X509_STORE_CTX_get_error(ctx.native_handle()); auto depth_err = X509_STORE_CTX_get_error_depth(ctx.native_handle()); BOOST_LOG_TRIVIAL(debug) << "Error " << X509_verify_cert_error_string(err) << std::endl; BOOST_LOG_TRIVIAL(debug) << "Depth " << depth_err << std::endl; X509* issuer = cts->current_issuer; if (issuer) { X509_NAME_oneline(X509_get_subject_name(issuer), subject_name, 256); BOOST_LOG_TRIVIAL(debug) << "Issuer " << subject_name << "\n"; } X509_NAME_oneline(X509_get_subject_name(cert), subject_name, 256); BOOST_LOG_TRIVIAL(debug) << "Verifying " << subject_name << "\n"; BOOST_LOG_TRIVIAL(debug) << "------------------------------" << std::endl; return preverified; }; ctx.set_verify_depth(100); // Set the mutual authentication ctx.set_verify_mode(boost::asio::ssl::verify_peer | boost::asio::ssl::verify_fail_if_no_peer_cert); // Set the callback to verify the cetificate chains of the peer ctx.set_verify_callback(boost::bind(verify_callback, _1, _2)); // Load the file containing the trusted certificate authorities SSL_CTX_load_verify_locations(ctx.native_handle(), "./certs/trusted/ca.crt", NULL); // The certificate used by the local peer ctx.use_certificate_chain_file("./certs/certificate.crt"); // The private key used by the local peer ctx.use_private_key_file("./certs/private.key", boost::asio::ssl::context::pem); // The Diffie-Hellman parameter file ctx.use_tmp_dh_file("./certs/dh4096.pem"); // Force a specific cipher suite SSL_CTX_set_cipher_list(ctx.native_handle(), "DHE-RSA-AES256-GCM-SHA384"); fiber_acceptor fib_acceptor(io_service_server_); ssl_fiber_t ssl_fiber_client(io_service_client_, ctx); ssl_fiber_t ssl_fiber_server(io_service_server_, ctx); std::promise<bool> client_closed; std::promise<bool> server_closed; uint32_t buffer_s = 1; uint32_t buffer_c = 0; auto sent = [this, &server_closed, &ssl_fiber_server, &buffer_s]( const boost::system::error_code& ec, size_t length) { EXPECT_EQ(ec.value(), 0) << "Sent handler should not be in error"; BOOST_LOG_TRIVIAL(debug) << "Server sent: " << buffer_s << std::endl; server_closed.set_value(true); }; auto async_handshaked_s = [this, &ssl_fiber_server, &buffer_s, &sent]( const boost::system::error_code& ec) { EXPECT_EQ(ec.value(), 0) << "Handshaked handler should not be in error"; buffer_s = 10; boost::asio::async_write(ssl_fiber_server, boost::asio::buffer(&buffer_s, sizeof(buffer_s)), boost::bind<void>(sent, _1, _2)); }; auto async_accept_s = [this, &ssl_fiber_server, &async_handshaked_s]( const boost::system::error_code& ec) { EXPECT_EQ(ec.value(), 0) << "Accept handler should not be in error"; ssl_fiber_server.async_handshake(boost::asio::ssl::stream_base::server, boost::bind<void>(async_handshaked_s, _1)); }; auto received = [this, &client_closed, &ssl_fiber_client, &buffer_c, &buffer_s](const boost::system::error_code& ec, size_t length) { EXPECT_EQ(ec.value(), 0) << "Received handler should not be in error " << ec.message(); EXPECT_EQ(buffer_s, buffer_c); BOOST_LOG_TRIVIAL(debug) << "client received: " << buffer_c << std::endl; ssl_fiber_client.next_layer().close(); client_closed.set_value(true); }; auto async_handshaked_c = [this, &ssl_fiber_client, &buffer_c, &received]( const boost::system::error_code& ec) { EXPECT_EQ(ec.value(), 0) << "Handshaked handler should not be in error"; boost::asio::async_read(ssl_fiber_client, boost::asio::buffer(&buffer_c, sizeof(buffer_c)), boost::bind<void>(received, _1, _2)); }; auto async_connect_c = [this, &ssl_fiber_client, &async_handshaked_c]( const boost::system::error_code& ec) { EXPECT_EQ(ec.value(), 0) << "Connect handler should not be in error"; ssl_fiber_client.async_handshake(boost::asio::ssl::stream_base::client, boost::bind<void>(async_handshaked_c, _1)); }; boost::system::error_code acceptor_ec; fiber_endpoint server_endpoint(boost::asio::fiber::stream_fiber<socket>::v1(), demux_server_, 1); fib_acceptor.open(server_endpoint.protocol(), acceptor_ec); fib_acceptor.bind(server_endpoint, acceptor_ec); fib_acceptor.listen(boost::asio::socket_base::max_connections, acceptor_ec); fib_acceptor.async_accept(ssl_fiber_server.next_layer(), boost::bind<void>(async_accept_s, _1)); fiber_endpoint client_endpoint(boost::asio::fiber::stream_fiber<socket>::v1(), demux_client_, 1); ssl_fiber_client.next_layer().async_connect( client_endpoint, boost::bind<void>(async_connect_c, _1)); client_closed.get_future().wait(); server_closed.get_future().wait(); boost::system::error_code ec; ssl_fiber_server.next_layer().close(ec); fib_acceptor.close(ec); }
//----------------------------------------------------------------------------- TEST_F(FiberTest, tooSmallReceiveBuffer) { boost::log::core::get()->set_filter(boost::log::trivial::severity >= boost::log::trivial::info); Wait(); std::array<uint8_t, 5> buffer_client; std::array<uint8_t, 2> buffer_server; boost::recursive_mutex received_mutex; uint8_t count = 0; size_t received = 0; size_t sent = 0; fiber_acceptor fib_acceptor(io_service_server_); fiber fib_server(io_service_server_); fiber fib_client(io_service_client_); std::promise<bool> client_closed; std::promise<bool> server_closed; auto void_handler_receive = [&](const boost::system::error_code& ec, size_t s) { boost::recursive_mutex::scoped_lock lock(received_mutex); received += s; ++count; if (count == 4) { ASSERT_EQ(received, 5) << "Not received all data"; ASSERT_EQ(fib_server.is_open(), false) << "Server fiber not closed"; server_closed.set_value(true); } }; auto async_accept_h1 = [&, this](const boost::system::error_code& ec) { ASSERT_EQ(ec.value(), 0); boost::asio::async_read(fib_server, boost::asio::buffer(buffer_server), boost::bind<void>(void_handler_receive, _1, _2)); boost::asio::async_read(fib_server, boost::asio::buffer(buffer_server), boost::bind<void>(void_handler_receive, _1, _2)); boost::asio::async_read(fib_server, boost::asio::buffer(buffer_server), boost::bind<void>(void_handler_receive, _1, _2)); boost::asio::async_read(fib_server, boost::asio::buffer(buffer_server), boost::bind<void>(void_handler_receive, _1, _2)); }; auto void_handler_send = [&](const boost::system::error_code& ec, size_t s) { EXPECT_EQ(ec.value(), 0); if (ec) { client_closed.set_value(false); return; } sent += s; fib_client.close(); client_closed.set_value(true); }; auto async_connect_h1 = [&, this](const boost::system::error_code& ec) { ASSERT_EQ(ec.value(), 0); buffer_client[0] = 'a'; buffer_client[1] = 'b'; buffer_client[2] = 'c'; buffer_client[3] = 'd'; buffer_client[4] = 'e'; boost::asio::async_write(fib_client, boost::asio::buffer(buffer_client), boost::bind<void>(void_handler_send, _1, _2)); }; boost::system::error_code acceptor_ec; fiber_endpoint server_endpoint(boost::asio::fiber::stream_fiber<socket>::v1(), demux_server_, 1); fib_acceptor.open(server_endpoint.protocol(), acceptor_ec); fib_acceptor.bind(server_endpoint, acceptor_ec); fib_acceptor.listen(boost::asio::socket_base::max_connections, acceptor_ec); fib_acceptor.async_accept(fib_server, boost::bind<void>(async_accept_h1, _1)); fiber_endpoint client_endpoint(boost::asio::fiber::stream_fiber<socket>::v1(), demux_client_, 1); fib_client.async_connect(client_endpoint, boost::bind<void>(async_connect_h1, _1)); client_closed.get_future().wait(); server_closed.get_future().wait(); { boost::recursive_mutex::scoped_lock lock(received_mutex); } EXPECT_EQ(received, 5); EXPECT_EQ(sent, 5); boost::system::error_code close_fib_acceptor_ec; fib_acceptor.close(close_fib_acceptor_ec); }
////----------------------------------------------------------------------------- TEST_F(FiberTest, exchangePacketsFiveClients) { boost::log::core::get()->set_filter(boost::log::trivial::severity >= boost::log::trivial::info); Wait(); typedef std::array<uint8_t, 5> buffer_type; struct TestConnection { TestConnection(boost::asio::io_service& io_service_server, boost::asio::io_service& io_service_client) : fib_server(io_service_server), fib_client(io_service_client) {} fiber fib_server; fiber fib_client; buffer_type buffer_server; buffer_type buffer_client; std::promise<bool> server_closed; std::promise<bool> client_closed; }; uint32_t connection_number = 5; int32_t packet_number = 5 * 100; fiber_acceptor fib_acceptor(io_service_server_); std::list<TestConnection> test_connections; boost::recursive_mutex server_received_mutex; int32_t server_received = 0; std::atomic<bool> finished(false); std::function<void(const boost::system::error_code&, std::size_t, std::promise<bool>&, fiber&, buffer_type&)> async_receive_h1; std::function<void(const boost::system::error_code&, std::size_t, std::promise<bool>&, fiber&, buffer_type&)> async_send_h1; async_receive_h1 = [&](const boost::system::error_code& ec, std::size_t, std::promise<bool>& closed, fiber& fib, buffer_type& buffer_server) { if (ec) { boost::recursive_mutex::scoped_lock lock_server_received( server_received_mutex); ASSERT_EQ(packet_number, server_received); return; } ASSERT_EQ(buffer_server[0], 'a'); ASSERT_EQ(buffer_server[1], 'b'); ASSERT_EQ(buffer_server[2], 'c'); ASSERT_EQ(buffer_server[3], 'd'); ASSERT_EQ(buffer_server[4], 'e'); buffer_server[0] = '1'; buffer_server[1] = '2'; buffer_server[2] = '3'; buffer_server[3] = '4'; buffer_server[4] = '5'; boost::recursive_mutex::scoped_lock lock(server_received_mutex); if (++server_received < packet_number - 4) { boost::asio::async_write( fib, boost::asio::buffer(buffer_server), boost::bind<void>(async_send_h1, _1, _2, boost::ref(closed), boost::ref(fib), boost::ref(buffer_server))); } else if (server_received < packet_number) { fib.close(); closed.set_value(true); } else { finished = true; fib.close(); closed.set_value(true); } }; async_send_h1 = [&](const boost::system::error_code& ec, std::size_t, std::promise<bool>& closed, fiber& fib, buffer_type& buffer_server) { ASSERT_EQ(ec.value(), 0); boost::asio::async_read( fib, boost::asio::buffer(buffer_server), boost::bind<void>(async_receive_h1, _1, _2, boost::ref(closed), boost::ref(fib), boost::ref(buffer_server))); }; auto async_accept_h = [&, this](const boost::system::error_code& ec, std::promise<bool>& closed, fiber& fib, buffer_type& buffer_server) { EXPECT_EQ(ec.value(), 0); buffer_server[0] = '1'; buffer_server[1] = '2'; buffer_server[2] = '3'; buffer_server[3] = '4'; buffer_server[4] = '5'; boost::asio::async_read( fib, boost::asio::buffer(buffer_server), boost::bind<void>(async_receive_h1, _1, _2, boost::ref(closed), boost::ref(fib), boost::ref(buffer_server))); }; std::function<void(const boost::system::error_code&, std::size_t, std::promise<bool>&, fiber&, buffer_type&)> async_receive_h2; std::function<void(const boost::system::error_code&, std::size_t, std::promise<bool>&, fiber&, buffer_type&)> async_send_h2; async_receive_h2 = [&](const boost::system::error_code& ec, std::size_t, std::promise<bool>& closed, fiber& fib, buffer_type& buffer_client) { if (!ec) { ASSERT_EQ(buffer_client[0], '1'); ASSERT_EQ(buffer_client[1], '2'); ASSERT_EQ(buffer_client[2], '3'); ASSERT_EQ(buffer_client[3], '4'); ASSERT_EQ(buffer_client[4], '5'); buffer_client[0] = 'a'; buffer_client[1] = 'b'; buffer_client[2] = 'c'; buffer_client[3] = 'd'; buffer_client[4] = 'e'; boost::asio::async_write( fib, boost::asio::buffer(buffer_client), boost::bind<void>(async_send_h2, _1, _2, boost::ref(closed), boost::ref(fib), boost::ref(buffer_client))); } else { fib.close(); closed.set_value(true); } }; async_send_h2 = [&](const boost::system::error_code& ec, std::size_t, std::promise<bool>& closed, fiber& fib, buffer_type& buffer_client) { EXPECT_EQ(ec.value(), 0); boost::asio::async_read( fib, boost::asio::buffer(buffer_client), boost::bind<void>(async_receive_h2, _1, _2, boost::ref(closed), boost::ref(fib), boost::ref(buffer_client))); }; auto async_connect_h = [&](std::promise<bool>& closed, fiber& fib, buffer_type& buffer_client, const boost::system::error_code& ec) { ASSERT_EQ(ec.value(), 0); buffer_client[0] = 'a'; buffer_client[1] = 'b'; buffer_client[2] = 'c'; buffer_client[3] = 'd'; buffer_client[4] = 'e'; boost::asio::async_write( fib, boost::asio::buffer(buffer_client), boost::bind<void>(async_send_h2, _1, _2, boost::ref(closed), boost::ref(fib), boost::ref(buffer_client))); }; boost::system::error_code acceptor_ec; fiber_endpoint server_endpoint(boost::asio::fiber::stream_fiber<socket>::v1(), demux_server_, 1); fib_acceptor.open(server_endpoint.protocol(), acceptor_ec); fib_acceptor.bind(server_endpoint, acceptor_ec); fib_acceptor.listen(boost::asio::socket_base::max_connections, acceptor_ec); fiber_endpoint client_endpoint(boost::asio::fiber::stream_fiber<socket>::v1(), demux_client_, 1); for (std::size_t i = 0; i < connection_number; ++i) { test_connections.emplace_front(io_service_server_, io_service_client_); auto& test_connection = test_connections.front(); fib_acceptor.async_accept( test_connection.fib_server, boost::bind<void>(async_accept_h, _1, boost::ref(test_connection.server_closed), boost::ref(test_connection.fib_server), boost::ref(test_connection.buffer_server))); test_connection.fib_client.async_connect( client_endpoint, boost::bind<void>(async_connect_h, boost::ref(test_connection.client_closed), boost::ref(test_connection.fib_client), boost::ref(test_connection.buffer_client), _1)); } for (auto& test_connection : test_connections) { test_connection.client_closed.get_future().wait(); test_connection.server_closed.get_future().wait(); } ASSERT_EQ(packet_number, server_received); boost::system::error_code close_fib_acceptor_ec; fib_acceptor.close(close_fib_acceptor_ec); ASSERT_EQ(finished, true) << "Test did not finish completely"; }
//----------------------------------------------------------------------------- TEST_F(FiberTest, exchangePackets) { boost::log::core::get()->set_filter(boost::log::trivial::severity >= boost::log::trivial::info); Wait(); const int32_t packet_number = 1000; std::promise<bool> client_closed; std::promise<bool> server_closed; fiber_acceptor fib_acceptor(io_service_server_); fiber fib_server(io_service_server_); fiber fib_client(io_service_client_); uint8_t buffer_client[5] = {0}; uint8_t buffer_server[5] = {0}; bool result_server = true; int server_received = 0; std::function<void(const boost::system::error_code & ec, std::size_t)> async_receive_h1; std::function<void(const boost::system::error_code & ec, std::size_t)> async_send_h1; async_receive_h1 = [&, this](const boost::system::error_code& ec, std::size_t) { ASSERT_EQ(ec.value(), 0); result_server &= (buffer_server[0] == 'a'); result_server &= (buffer_server[1] == 'b'); result_server &= (buffer_server[2] == 'c'); result_server &= (buffer_server[3] == 'd'); result_server &= (buffer_server[4] == 'e'); buffer_server[0] = '1'; buffer_server[1] = '2'; buffer_server[2] = '3'; buffer_server[3] = '4'; buffer_server[4] = '5'; if (++server_received < packet_number) { boost::asio::async_write(fib_server, boost::asio::buffer(buffer_server), boost::bind<void>(async_send_h1, _1, _2)); } else { fib_server.close(); server_closed.set_value(true); } }; async_send_h1 = [&, this](const boost::system::error_code& ec, std::size_t) { ASSERT_EQ(ec.value(), 0); boost::asio::async_read(fib_server, boost::asio::buffer(buffer_server), boost::bind<void>(async_receive_h1, _1, _2)); }; auto async_accept_h = [&, this](const boost::system::error_code& ec) { ASSERT_EQ(ec.value(), 0); buffer_server[0] = '1'; buffer_server[1] = '2'; buffer_server[2] = '3'; buffer_server[3] = '4'; buffer_server[4] = '5'; boost::asio::async_read(fib_server, boost::asio::buffer(buffer_server), boost::bind<void>(async_receive_h1, _1, _2)); }; std::function<void(const boost::system::error_code & ec, std::size_t)> async_receive_h2; std::function<void(const boost::system::error_code & ec, std::size_t)> async_send_h2; async_receive_h2 = [&, this](const boost::system::error_code& ec, std::size_t) { if (!ec) { buffer_client[0] = 'a'; buffer_client[1] = 'b'; buffer_client[2] = 'c'; buffer_client[3] = 'd'; buffer_client[4] = 'e'; boost::asio::async_write(fib_client, boost::asio::buffer(buffer_client), boost::bind<void>(async_send_h2, _1, _2)); } else { fib_client.close(); client_closed.set_value(true); } }; async_send_h2 = [&, this](const boost::system::error_code& ec, std::size_t) { ASSERT_EQ(ec.value(), 0); boost::asio::async_read(fib_client, boost::asio::buffer(buffer_client), boost::bind<void>(async_receive_h2, _1, _2)); }; auto async_connect_h = [&, this](const boost::system::error_code& ec) { ASSERT_EQ(ec.value(), 0); buffer_client[0] = 'a'; buffer_client[1] = 'b'; buffer_client[2] = 'c'; buffer_client[3] = 'd'; buffer_client[4] = 'e'; boost::asio::async_write(fib_client, boost::asio::buffer(buffer_client), boost::bind<void>(async_send_h2, _1, _2)); }; boost::system::error_code acceptor_ec; fiber_endpoint server_endpoint(boost::asio::fiber::stream_fiber<socket>::v1(), demux_server_, 1); fib_acceptor.open(server_endpoint.protocol(), acceptor_ec); fib_acceptor.bind(server_endpoint, acceptor_ec); fib_acceptor.listen(boost::asio::socket_base::max_connections, acceptor_ec); fib_acceptor.async_accept(fib_server, boost::bind<void>(async_accept_h, _1)); fiber_endpoint client_endpoint(boost::asio::fiber::stream_fiber<socket>::v1(), demux_client_, 1); fib_client.async_connect(client_endpoint, boost::bind<void>(async_connect_h, _1)); client_closed.get_future().wait(); server_closed.get_future().wait(); ASSERT_EQ(true, result_server); ASSERT_EQ(packet_number, server_received); boost::system::error_code close_fib_acceptor_ec; fib_acceptor.close(close_fib_acceptor_ec); }