@@ -1222,17 +1222,21 @@ TEST_F(TCPv4Tests, send_and_receive_between_secure_ports_untrusted_server)
1222
1222
// destination that does not read or does it so slowly.
1223
1223
TEST_F (TCPv4Tests, secure_non_blocking_send)
1224
1224
{
1225
+ eprosima::fastdds::dds::Log::SetVerbosity (eprosima::fastdds::dds::Log::Kind::Info);
1226
+
1225
1227
uint16_t port = g_default_port;
1226
1228
uint32_t msg_size = eprosima::fastdds::rtps::s_minimumSocketBuffer;
1227
1229
// Create a TCP Server transport
1228
1230
using TLSOptions = TCPTransportDescriptor::TLSConfig::TLSOptions;
1229
1231
using TLSVerifyMode = TCPTransportDescriptor::TLSConfig::TLSVerifyMode;
1230
- using TLSHSRole = TCPTransportDescriptor::TLSConfig::TLSHandShakeRole;
1231
1232
TCPv4TransportDescriptor senderDescriptor;
1232
1233
senderDescriptor.add_listener_port (port);
1234
+ senderDescriptor.apply_security = true ;
1233
1235
senderDescriptor.sendBufferSize = msg_size;
1234
- senderDescriptor.tls_config .handshake_role = TLSHSRole::CLIENT;
1235
- senderDescriptor.tls_config .verify_file = " ca.crt" ;
1236
+ senderDescriptor.tls_config .password = " fastddspwd" ;
1237
+ senderDescriptor.tls_config .cert_chain_file = " fastdds.crt" ;
1238
+ senderDescriptor.tls_config .private_key_file = " fastdds.key" ;
1239
+ senderDescriptor.tls_config .tmp_dh_file = " dh_params.pem" ;
1236
1240
senderDescriptor.tls_config .verify_mode = TLSVerifyMode::VERIFY_PEER;
1237
1241
senderDescriptor.tls_config .add_option (TLSOptions::DEFAULT_WORKAROUNDS);
1238
1242
senderDescriptor.tls_config .add_option (TLSOptions::SINGLE_DH_USE);
@@ -1249,8 +1253,8 @@ TEST_F(TCPv4Tests, secure_non_blocking_send)
1249
1253
// saturate the reception socket of the datareader. This saturation requires
1250
1254
// preventing the datareader from reading from the socket, what inevitably
1251
1255
// happens continuously if instantiating and connecting the receiver transport.
1252
- // Hence, a raw socket is opened and connected to the server. There won't be read
1253
- // calls on that socket .
1256
+ // Hence, a raw socket is opened and connected to the server. Read calls on that
1257
+ // socket are controlled .
1254
1258
Locator_t serverLoc;
1255
1259
serverLoc.kind = LOCATOR_KIND_TCPv4;
1256
1260
IPLocator::setIPv4 (serverLoc, 127 , 0 , 0 , 1 );
@@ -1263,13 +1267,7 @@ TEST_F(TCPv4Tests, secure_non_blocking_send)
1263
1267
{
1264
1268
return preverified;
1265
1269
});
1266
- ssl_context.set_password_callback ([](std::size_t , asio::ssl::context_base::password_purpose)
1267
- {
1268
- return " fastddspwd" ;
1269
- });
1270
- ssl_context.use_certificate_chain_file (" fastdds.crt" );
1271
- ssl_context.use_private_key_file (" fastdds.key" , asio::ssl::context::pem);
1272
- ssl_context.use_tmp_dh_file (" dh_params.pem" );
1270
+ ssl_context.load_verify_file (" ca.crt" );
1273
1271
1274
1272
uint32_t options = 0 ;
1275
1273
options |= asio::ssl::context::default_workarounds;
@@ -1278,8 +1276,19 @@ TEST_F(TCPv4Tests, secure_non_blocking_send)
1278
1276
options |= asio::ssl::context::no_compression;
1279
1277
ssl_context.set_options (options);
1280
1278
1281
- // TCPChannelResourceSecure::connect() like connection
1282
1279
asio::io_service io_service;
1280
+ auto ioServiceFunction = [&]()
1281
+ {
1282
+ #if ASIO_VERSION >= 101200
1283
+ asio::executor_work_guard<asio::io_service::executor_type> work (io_service.get_executor ());
1284
+ #else
1285
+ io_service::work work (io_service_);
1286
+ #endif // if ASIO_VERSION >= 101200
1287
+ io_service.run ();
1288
+ };
1289
+ std::thread ioServiceThread (ioServiceFunction);
1290
+
1291
+ // TCPChannelResourceSecure::connect() like connection
1283
1292
asio::ip::tcp::resolver resolver (io_service);
1284
1293
auto endpoints = resolver.resolve (
1285
1294
IPLocator::ip_to_string (serverLoc),
@@ -1300,7 +1309,7 @@ TEST_F(TCPv4Tests, secure_non_blocking_send)
1300
1309
)
1301
1310
{
1302
1311
ASSERT_TRUE (!ec);
1303
- asio::ssl::stream_base::handshake_type role = asio::ssl::stream_base::server ;
1312
+ asio::ssl::stream_base::handshake_type role = asio::ssl::stream_base::client ;
1304
1313
secure_socket->async_handshake (role,
1305
1314
[](const std::error_code& ec)
1306
1315
{
@@ -1316,24 +1325,40 @@ TEST_F(TCPv4Tests, secure_non_blocking_send)
1316
1325
a connection. This channel will not be present in the server's channel_resources_ map
1317
1326
as communication lacks most of the discovery messages using a raw socket as participant.
1318
1327
*/
1328
+ // auto sender_unbound_channel_resources = senderTransportUnderTest.get_unbound_channel_resources();
1319
1329
auto sender_unbound_channel_resources = senderTransportUnderTest.get_unbound_channel_resources ();
1320
1330
ASSERT_TRUE (sender_unbound_channel_resources.size () == 1 );
1321
1331
auto sender_channel_resource =
1322
1332
std::static_pointer_cast<TCPChannelResourceBasic>(sender_unbound_channel_resources[0 ]);
1323
1333
1324
1334
// Prepare the message
1325
1335
asio::error_code ec;
1326
- std::vector<octet> message (msg_size, 0 );
1336
+ std::vector<octet> message (msg_size * 2 , 0 );
1327
1337
const octet* data = message.data ();
1328
1338
size_t size = message.size ();
1329
1339
1330
- // Send the message with no header
1331
- for (int i = 0 ; i < 5 ; i++)
1332
- {
1333
- sender_channel_resource->send (nullptr , 0 , data, size, ec);
1334
- }
1340
+ // Send the message with no header. Since TCP actually allocates twice the size of the buffer requested
1341
+ // it should be able to send a message of msg_size*2.
1342
+ size_t bytes_sent = sender_channel_resource->send (nullptr , 0 , data, size, ec);
1343
+ ASSERT_EQ (bytes_sent, size);
1344
+
1345
+ // Now wait until the receive buffer is flushed (send buffer will be empty too)
1346
+ std::vector<octet> buffer (size, 0 );
1347
+ size_t bytes_read = 0 ;
1348
+ bytes_read = asio::read (*secure_socket, asio::buffer (buffer.data (), size), asio::transfer_exactly (size), ec);
1349
+ ASSERT_EQ (ec, asio::error_code ());
1350
+ ASSERT_EQ (bytes_read, size);
1351
+
1352
+ // Now try to send a message that is bigger than the buffer size: (msg_size*2 + 1) + bytes_in_send_buffer(0) > 2*sendBufferSize
1353
+ message.resize (msg_size * 2 + 1 );
1354
+ data = message.data ();
1355
+ size = message.size ();
1356
+ bytes_sent = sender_channel_resource->send (nullptr , 0 , data, size, ec);
1357
+ ASSERT_EQ (bytes_sent, 0u );
1335
1358
1336
1359
secure_socket->lowest_layer ().close (ec);
1360
+ io_service.stop ();
1361
+ ioServiceThread.join ();
1337
1362
}
1338
1363
#endif // ifndef _WIN32
1339
1364
@@ -1769,7 +1794,7 @@ TEST_F(TCPv4Tests, client_announced_local_port_uniqueness)
1769
1794
}
1770
1795
1771
1796
#ifndef _WIN32
1772
- // The primary purpose of this test is to check the non-blocking behavior of a secure socket sending data to a
1797
+ // The primary purpose of this test is to check the non-blocking behavior of a socket sending data to a
1773
1798
// destination that does not read or does it so slowly.
1774
1799
TEST_F (TCPv4Tests, non_blocking_send)
1775
1800
{
@@ -1790,8 +1815,8 @@ TEST_F(TCPv4Tests, non_blocking_send)
1790
1815
// saturate the reception socket of the datareader. This saturation requires
1791
1816
// preventing the datareader from reading from the socket, what inevitably
1792
1817
// happens continuously if instantiating and connecting the receiver transport.
1793
- // Hence, a raw socket is opened and connected to the server. There won't be read
1794
- // calls on that socket .
1818
+ // Hence, a raw socket is opened and connected to the server. Read calls on that
1819
+ // socket are controlled .
1795
1820
Locator_t serverLoc;
1796
1821
serverLoc.kind = LOCATOR_KIND_TCPv4;
1797
1822
IPLocator::setIPv4 (serverLoc, 127 , 0 , 0 , 1 );
@@ -1836,15 +1861,26 @@ TEST_F(TCPv4Tests, non_blocking_send)
1836
1861
1837
1862
// Prepare the message
1838
1863
asio::error_code ec;
1839
- std::vector<octet> message (msg_size, 0 );
1864
+ std::vector<octet> message (msg_size * 2 , 0 );
1840
1865
const octet* data = message.data ();
1841
1866
size_t size = message.size ();
1842
1867
1843
- // Send the message with no header
1844
- for (int i = 0 ; i < 5 ; i++)
1845
- {
1846
- sender_channel_resource->send (nullptr , 0 , data, size, ec);
1847
- }
1868
+ // Send the message with no header. Since TCP actually allocates twice the size of the buffer requested
1869
+ // it should be able to send a message of msg_size*2.
1870
+ size_t bytes_sent = sender_channel_resource->send (nullptr , 0 , data, size, ec);
1871
+ ASSERT_EQ (bytes_sent, size);
1872
+
1873
+ // Now wait until the receive buffer is flushed (send buffer will be empty too)
1874
+ std::vector<octet> buffer (size, 0 );
1875
+ size_t bytes_read = asio::read (socket, asio::buffer (buffer, size), asio::transfer_exactly (size), ec);
1876
+ ASSERT_EQ (bytes_read, size);
1877
+
1878
+ // Now try to send a message that is bigger than the buffer size: (msg_size*2 + 1) + bytes_in_send_buffer(0) > 2*sendBufferSize
1879
+ message.resize (msg_size * 2 + 1 );
1880
+ data = message.data ();
1881
+ size = message.size ();
1882
+ bytes_sent = sender_channel_resource->send (nullptr , 0 , data, size, ec);
1883
+ ASSERT_EQ (bytes_sent, 0u );
1848
1884
1849
1885
socket.shutdown (asio::ip::tcp::socket::shutdown_both);
1850
1886
socket.cancel ();
0 commit comments