From 22b6b0fddbf19b5f825e92fe0eb8bf3abbcd4810 Mon Sep 17 00:00:00 2001 From: Dominika Maziec Date: Fri, 27 Sep 2019 12:42:41 +0200 Subject: [PATCH 1/4] unitests update for tcp and udp --- .../features/netsocket/SocketAddress/test_SocketAddress.cpp | 6 +++++- UNITTESTS/features/netsocket/TCPSocket/test_TCPSocket.cpp | 2 ++ UNITTESTS/features/netsocket/UDPSocket/test_UDPSocket.cpp | 3 +++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/UNITTESTS/features/netsocket/SocketAddress/test_SocketAddress.cpp b/UNITTESTS/features/netsocket/SocketAddress/test_SocketAddress.cpp index d9abf4f656f..553606cf625 100644 --- a/UNITTESTS/features/netsocket/SocketAddress/test_SocketAddress.cpp +++ b/UNITTESTS/features/netsocket/SocketAddress/test_SocketAddress.cpp @@ -162,5 +162,9 @@ TEST_F(TestSocketAddress, bool_operator_ip6_true) EXPECT_TRUE(addr ? true : false); } - +TEST_F(TestSocketAddress, bool_operator_ip6_false) +{ + SocketAddress addr("0:0:0:0:0:0:0:0",80); + EXPECT_FALSE(addr ? true : false); +} diff --git a/UNITTESTS/features/netsocket/TCPSocket/test_TCPSocket.cpp b/UNITTESTS/features/netsocket/TCPSocket/test_TCPSocket.cpp index 40fcf6e1959..24e672370ea 100644 --- a/UNITTESTS/features/netsocket/TCPSocket/test_TCPSocket.cpp +++ b/UNITTESTS/features/netsocket/TCPSocket/test_TCPSocket.cpp @@ -77,6 +77,8 @@ TEST_F(TestTCPSocket, connect) stack.return_value = NSAPI_ERROR_OK; const SocketAddress a("127.0.0.1", 1024); EXPECT_EQ(socket->connect(a), NSAPI_ERROR_OK); + EXPECT_EQ(socket->setsockopt(NSAPI_SOCKET,NSAPI_BIND_TO_DEVICE, "12345", 5), NSAPI_ERROR_UNSUPPORTED); + EXPECT_EQ(socket->connect("127.0.0.1", 1024), NSAPI_ERROR_OK); } TEST_F(TestTCPSocket, connect_no_open) diff --git a/UNITTESTS/features/netsocket/UDPSocket/test_UDPSocket.cpp b/UNITTESTS/features/netsocket/UDPSocket/test_UDPSocket.cpp index f746d010fc0..900abf13a53 100644 --- a/UNITTESTS/features/netsocket/UDPSocket/test_UDPSocket.cpp +++ b/UNITTESTS/features/netsocket/UDPSocket/test_UDPSocket.cpp @@ -74,6 +74,9 @@ TEST_F(TestUDPSocket, sendto_addr_port) stack.return_value = NSAPI_ERROR_OK; EXPECT_EQ(socket->sendto("127.0.0.1", 0, 0, 0), 0); + + EXPECT_EQ(socket->setsockopt(NSAPI_SOCKET,NSAPI_BIND_TO_DEVICE,"12324",5), NSAPI_ERROR_UNSUPPORTED); + EXPECT_EQ(socket->sendto("127.0.0.1", 0, 0, 0), 0); } TEST_F(TestUDPSocket, connect) From 8c3194ac4e7363f3190f4612f1d90f8681871614 Mon Sep 17 00:00:00 2001 From: Michal Paszta Date: Wed, 2 Oct 2019 15:48:59 +0200 Subject: [PATCH 2/4] [unittests] Improve coverage For the following classes: * DTLSSocket * NetworkInterface * TLSSocketWrapper --- .../netsocket/DTLSSocket/test_DTLSSocket.cpp | 6 +++ .../test_NetworkInterface.cpp | 12 ++++-- .../test_TLSSocketWrapper.cpp | 41 +++++++++++++++++++ 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/UNITTESTS/features/netsocket/DTLSSocket/test_DTLSSocket.cpp b/UNITTESTS/features/netsocket/DTLSSocket/test_DTLSSocket.cpp index cc4bdc2aecd..f00c5055246 100644 --- a/UNITTESTS/features/netsocket/DTLSSocket/test_DTLSSocket.cpp +++ b/UNITTESTS/features/netsocket/DTLSSocket/test_DTLSSocket.cpp @@ -54,6 +54,12 @@ TEST_F(TestDTLSSocket, constructor) EXPECT_TRUE(socket); } +TEST_F(TestDTLSSocket, connect_no_socket) +{ + EXPECT_TRUE(socket); + EXPECT_EQ(socket->connect("127.0.0.1", 1024), NSAPI_ERROR_NO_SOCKET); +} + /* connect */ TEST_F(TestDTLSSocket, connect) diff --git a/UNITTESTS/features/netsocket/NetworkInterface/test_NetworkInterface.cpp b/UNITTESTS/features/netsocket/NetworkInterface/test_NetworkInterface.cpp index 5dc2575b308..dec2c52cb5f 100644 --- a/UNITTESTS/features/netsocket/NetworkInterface/test_NetworkInterface.cpp +++ b/UNITTESTS/features/netsocket/NetworkInterface/test_NetworkInterface.cpp @@ -64,6 +64,7 @@ class TestNetworkInterface : public testing::Test { TEST_F(TestNetworkInterface, constructor) { EXPECT_TRUE(iface); + iface->set_as_default(); //Empty function. Just trigger it, so it doesn't obscure coverage reports. } // get_default_instance is tested along with the implementations of NetworkInterface. @@ -92,6 +93,12 @@ TEST_F(TestNetworkInterface, get_gateway) EXPECT_EQ(iface->get_gateway(), n); } +TEST_F(TestNetworkInterface, get_interface_name) +{ + char *n = 0; + EXPECT_EQ(iface->get_interface_name(n), n); +} + TEST_F(TestNetworkInterface, set_network) { EXPECT_EQ(iface->set_network("127.0.0.1", "255.255.0.0", "127.0.0.1"), NSAPI_ERROR_UNSUPPORTED); @@ -168,7 +175,7 @@ TEST_F(TestNetworkInterface, add_event_listener) TEST_F(TestNetworkInterface, remove_event_listener) { - // Add two callback and check that both are called + // Add two callbacks and check that both are called callback_is_called = false; second_callback_called = false; iface->add_event_listener(my_iface_callback); @@ -215,7 +222,6 @@ TEST_F(TestNetworkInterface, correct_event_listener_per_interface) EXPECT_EQ(second_callback_called, true); iface->remove_event_listener(my_iface_callback); - iface2->remove_event_listener(my_iface_callback2); - + // Do not call iface2->remove_event_listener, so the destructor can take care of this. delete iface2; } diff --git a/UNITTESTS/features/netsocket/TLSSocketWrapper/test_TLSSocketWrapper.cpp b/UNITTESTS/features/netsocket/TLSSocketWrapper/test_TLSSocketWrapper.cpp index 1d62f948d16..1de1e86bac0 100644 --- a/UNITTESTS/features/netsocket/TLSSocketWrapper/test_TLSSocketWrapper.cpp +++ b/UNITTESTS/features/netsocket/TLSSocketWrapper/test_TLSSocketWrapper.cpp @@ -105,6 +105,20 @@ TEST_F(TestTLSSocketWrapper, constructor_hostname) delete wrapper2; } +TEST_F(TestTLSSocketWrapper, no_socket) +{ + TLSSocketWrapper *wrapperTmp = new TLSSocketWrapper(NULL); + const SocketAddress a("127.0.0.1", 1024); + EXPECT_EQ(wrapperTmp->connect(a), NSAPI_ERROR_NO_SOCKET); + EXPECT_EQ(wrapperTmp->bind(a), NSAPI_ERROR_NO_SOCKET); + EXPECT_EQ(wrapperTmp->setsockopt(0, 0, 0, 0), NSAPI_ERROR_NO_SOCKET); + EXPECT_EQ(wrapperTmp->getsockopt(0, 0, 0, 0), NSAPI_ERROR_NO_SOCKET); + EXPECT_EQ(wrapperTmp->send(dataBuf, dataSize), NSAPI_ERROR_NO_SOCKET); + EXPECT_EQ(wrapperTmp->recv(dataBuf, dataSize), NSAPI_ERROR_NO_SOCKET); + EXPECT_EQ(wrapperTmp->close(), NSAPI_ERROR_NO_SOCKET); + delete wrapperTmp; +} + /* connect */ TEST_F(TestTLSSocketWrapper, connect) @@ -194,6 +208,11 @@ TEST_F(TestTLSSocketWrapper, connect_handshake_fail_ssl_handshake_in_progress) mbedtls_stub.retArray[2] = MBEDTLS_ERR_SSL_WANT_READ; // mbedtls_ssl_handshake error const SocketAddress a("127.0.0.1", 1024); EXPECT_EQ(wrapper->connect(a), NSAPI_ERROR_IN_PROGRESS); + + // Check that send will fail in this situation. + mbedtls_stub.retArray[3] = MBEDTLS_ERR_SSL_WANT_READ; // mbedtls_ssl_handshake error + eventFlagsStubNextRetval.push_back(osFlagsError); // Break the wait loop + EXPECT_EQ(wrapper->send(dataBuf, dataSize), NSAPI_ERROR_WOULD_BLOCK); } TEST_F(TestTLSSocketWrapper, connect_handshake_fail_ssl_get_verify_result) @@ -243,6 +262,17 @@ TEST_F(TestTLSSocketWrapper, send_error_would_block) EXPECT_EQ(wrapper->send(dataBuf, dataSize), NSAPI_ERROR_WOULD_BLOCK); } +TEST_F(TestTLSSocketWrapper, send_device_error) +{ + transport->open((NetworkStack *)&stack); + mbedtls_stub.useCounter = true; + mbedtls_stub.retArray[3] = MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE; // mbedtls_ssl_write + eventFlagsStubNextRetval.push_back(osFlagsError); // Break the wait loop + const SocketAddress a("127.0.0.1", 1024); + EXPECT_EQ(wrapper->connect(a), NSAPI_ERROR_OK); + EXPECT_EQ(wrapper->send(dataBuf, dataSize), NSAPI_ERROR_DEVICE_ERROR); +} + TEST_F(TestTLSSocketWrapper, send_to) { transport->open((NetworkStack *)&stack); @@ -294,6 +324,17 @@ TEST_F(TestTLSSocketWrapper, recv_would_block) EXPECT_EQ(wrapper->recv(dataBuf, dataSize), NSAPI_ERROR_WOULD_BLOCK); } +TEST_F(TestTLSSocketWrapper, recv_device_error) +{ + transport->open((NetworkStack *)&stack); + mbedtls_stub.useCounter = true; + mbedtls_stub.retArray[3] = MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE; // mbedtls_ssl_write + const SocketAddress a("127.0.0.1", 1024); + EXPECT_EQ(wrapper->connect(a), NSAPI_ERROR_OK); + eventFlagsStubNextRetval.push_back(osFlagsError); // Break the wait loop + EXPECT_EQ(wrapper->recv(dataBuf, dataSize), NSAPI_ERROR_DEVICE_ERROR); +} + TEST_F(TestTLSSocketWrapper, recv_from_no_socket) { SocketAddress a("127.0.0.1", 1024); From 1706843d7dea840ae252c2c109a63b7c1bd7056d Mon Sep 17 00:00:00 2001 From: Michal Paszta Date: Wed, 2 Oct 2019 15:50:49 +0200 Subject: [PATCH 3/4] [unittests] test for InternetSocket::close blocking Close should not take place in case there is someone reading or writing to the socket. --- .../InternetSocket/test_InternetSocket.cpp | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/UNITTESTS/features/netsocket/InternetSocket/test_InternetSocket.cpp b/UNITTESTS/features/netsocket/InternetSocket/test_InternetSocket.cpp index 8a757ce9252..c06f6b587c7 100644 --- a/UNITTESTS/features/netsocket/InternetSocket/test_InternetSocket.cpp +++ b/UNITTESTS/features/netsocket/InternetSocket/test_InternetSocket.cpp @@ -18,6 +18,9 @@ #include "gtest/gtest.h" #include "features/netsocket/InternetSocket.h" #include "NetworkStack_stub.h" +#include +#include +#include extern std::list eventFlagsStubNextRetval; @@ -155,10 +158,19 @@ TEST_F(TestInternetSocket, close_during_read) { stack.return_value = NSAPI_ERROR_OK; socket->open((NetworkStack *)&stack); - // when c++11 is available use something like the code below to test the blocking behavior - // socket->add_reader(); - // std::async(c[](){std::this_thread::sleep_for(1ms); socket->rem_reader()}); + // Simulate the blocking behavior by adding a reader. + socket->add_reader(); + // The reader will be removed after we attempt to close the socket. + auto delay = std::chrono::milliseconds(2); + auto fut = std::async(std::launch::async, [&](){std::this_thread::sleep_for(delay); socket->rem_reader();}); + + // close() will block until the other thread calls rem_reader() + auto start = std::chrono::system_clock::now(); EXPECT_EQ(socket->close(), NSAPI_ERROR_OK); + auto end = std::chrono::system_clock::now(); + + auto diff = end - start; + EXPECT_LE(delay, end - start); } TEST_F(TestInternetSocket, modify_multicast_group) @@ -166,9 +178,6 @@ TEST_F(TestInternetSocket, modify_multicast_group) SocketAddress a("127.0.0.1", 1024); stack.return_value = NSAPI_ERROR_OK; socket->open((NetworkStack *)&stack); - // when c++11 is available use something like the code below to test the blocking behavior - // socket->add_reader(); - // std::async(c[](){std::this_thread::sleep_for(1ms); socket->rem_reader()}); EXPECT_EQ(socket->join_multicast_group(a), NSAPI_ERROR_UNSUPPORTED); EXPECT_EQ(socket->leave_multicast_group(a), NSAPI_ERROR_UNSUPPORTED); } From 66c4b131e2f9bfae76d4034fa50a9aa634da29b4 Mon Sep 17 00:00:00 2001 From: Michal Paszta Date: Wed, 2 Oct 2019 16:27:05 +0200 Subject: [PATCH 4/4] Astyle fixes --- .../netsocket/InternetSocket/test_InternetSocket.cpp | 5 ++++- .../features/netsocket/SocketAddress/test_SocketAddress.cpp | 2 +- UNITTESTS/features/netsocket/TCPSocket/test_TCPSocket.cpp | 2 +- UNITTESTS/features/netsocket/UDPSocket/test_UDPSocket.cpp | 2 +- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/UNITTESTS/features/netsocket/InternetSocket/test_InternetSocket.cpp b/UNITTESTS/features/netsocket/InternetSocket/test_InternetSocket.cpp index c06f6b587c7..f176818dd15 100644 --- a/UNITTESTS/features/netsocket/InternetSocket/test_InternetSocket.cpp +++ b/UNITTESTS/features/netsocket/InternetSocket/test_InternetSocket.cpp @@ -162,7 +162,10 @@ TEST_F(TestInternetSocket, close_during_read) socket->add_reader(); // The reader will be removed after we attempt to close the socket. auto delay = std::chrono::milliseconds(2); - auto fut = std::async(std::launch::async, [&](){std::this_thread::sleep_for(delay); socket->rem_reader();}); + auto fut = std::async(std::launch::async, [&]() { + std::this_thread::sleep_for(delay); + socket->rem_reader(); + }); // close() will block until the other thread calls rem_reader() auto start = std::chrono::system_clock::now(); diff --git a/UNITTESTS/features/netsocket/SocketAddress/test_SocketAddress.cpp b/UNITTESTS/features/netsocket/SocketAddress/test_SocketAddress.cpp index 553606cf625..cc3588a7e6f 100644 --- a/UNITTESTS/features/netsocket/SocketAddress/test_SocketAddress.cpp +++ b/UNITTESTS/features/netsocket/SocketAddress/test_SocketAddress.cpp @@ -164,7 +164,7 @@ TEST_F(TestSocketAddress, bool_operator_ip6_true) TEST_F(TestSocketAddress, bool_operator_ip6_false) { - SocketAddress addr("0:0:0:0:0:0:0:0",80); + SocketAddress addr("0:0:0:0:0:0:0:0", 80); EXPECT_FALSE(addr ? true : false); } diff --git a/UNITTESTS/features/netsocket/TCPSocket/test_TCPSocket.cpp b/UNITTESTS/features/netsocket/TCPSocket/test_TCPSocket.cpp index 24e672370ea..be38eb1d57d 100644 --- a/UNITTESTS/features/netsocket/TCPSocket/test_TCPSocket.cpp +++ b/UNITTESTS/features/netsocket/TCPSocket/test_TCPSocket.cpp @@ -77,7 +77,7 @@ TEST_F(TestTCPSocket, connect) stack.return_value = NSAPI_ERROR_OK; const SocketAddress a("127.0.0.1", 1024); EXPECT_EQ(socket->connect(a), NSAPI_ERROR_OK); - EXPECT_EQ(socket->setsockopt(NSAPI_SOCKET,NSAPI_BIND_TO_DEVICE, "12345", 5), NSAPI_ERROR_UNSUPPORTED); + EXPECT_EQ(socket->setsockopt(NSAPI_SOCKET, NSAPI_BIND_TO_DEVICE, "12345", 5), NSAPI_ERROR_UNSUPPORTED); EXPECT_EQ(socket->connect("127.0.0.1", 1024), NSAPI_ERROR_OK); } diff --git a/UNITTESTS/features/netsocket/UDPSocket/test_UDPSocket.cpp b/UNITTESTS/features/netsocket/UDPSocket/test_UDPSocket.cpp index 900abf13a53..b7f560c6e29 100644 --- a/UNITTESTS/features/netsocket/UDPSocket/test_UDPSocket.cpp +++ b/UNITTESTS/features/netsocket/UDPSocket/test_UDPSocket.cpp @@ -75,7 +75,7 @@ TEST_F(TestUDPSocket, sendto_addr_port) stack.return_value = NSAPI_ERROR_OK; EXPECT_EQ(socket->sendto("127.0.0.1", 0, 0, 0), 0); - EXPECT_EQ(socket->setsockopt(NSAPI_SOCKET,NSAPI_BIND_TO_DEVICE,"12324",5), NSAPI_ERROR_UNSUPPORTED); + EXPECT_EQ(socket->setsockopt(NSAPI_SOCKET, NSAPI_BIND_TO_DEVICE, "12324", 5), NSAPI_ERROR_UNSUPPORTED); EXPECT_EQ(socket->sendto("127.0.0.1", 0, 0, 0), 0); }