Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions UNITTESTS/features/netsocket/DTLSSocket/test_DTLSSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
#include "gtest/gtest.h"
#include "features/netsocket/InternetSocket.h"
#include "NetworkStack_stub.h"
#include <future>
#include <thread>
#include <chrono>

extern std::list<uint32_t> eventFlagsStubNextRetval;

Expand Down Expand Up @@ -155,20 +158,29 @@ 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)
{
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

2 changes: 2 additions & 0 deletions UNITTESTS/features/netsocket/TCPSocket/test_TCPSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions UNITTESTS/features/netsocket/UDPSocket/test_UDPSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down