Skip to content

Commit 51dedfd

Browse files
michalpasztamobicaSeppo Takalo
authored andcommitted
unittests: Added NetworkInterface unit tests
Most functions are empty or simply return "UNSUPPORTED", but it is still worth covering this functions with unit tests to have better control of unwanted changes.
1 parent d740d69 commit 51dedfd

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

UNITTESTS/features/netsocket/NetworkInterface/test_NetworkInterface.cpp

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "gtest/gtest.h"
1919
#include "features/netsocket/NetworkInterface.h"
20+
#include "NetworkStack_stub.h"
2021

2122
class stubNetworkInterface : public NetworkInterface {
2223
virtual nsapi_error_t connect()
@@ -29,8 +30,10 @@ class stubNetworkInterface : public NetworkInterface {
2930
};
3031
virtual NetworkStack *get_stack()
3132
{
32-
return NULL;
33+
return &stack;
3334
};
35+
public:
36+
NetworkStackstub stack;
3437
};
3538

3639
class TestNetworkInterface : public testing::Test {
@@ -53,8 +56,79 @@ TEST_F(TestNetworkInterface, constructor)
5356
EXPECT_TRUE(iface);
5457
}
5558

59+
// get_default_instance is tested along with the implementations of NetworkInterface.
60+
5661
TEST_F(TestNetworkInterface, get_mac_address)
5762
{
5863
char *n = 0;
5964
EXPECT_EQ(iface->get_mac_address(), n);
6065
}
66+
67+
TEST_F(TestNetworkInterface, get_ip_address)
68+
{
69+
char *n = 0;
70+
EXPECT_EQ(iface->get_ip_address(), n);
71+
}
72+
73+
TEST_F(TestNetworkInterface, get_netmask)
74+
{
75+
char *n = 0;
76+
EXPECT_EQ(iface->get_netmask(), n);
77+
}
78+
79+
TEST_F(TestNetworkInterface, get_gateway)
80+
{
81+
char *n = 0;
82+
EXPECT_EQ(iface->get_gateway(), n);
83+
}
84+
85+
TEST_F(TestNetworkInterface, set_network)
86+
{
87+
EXPECT_EQ(iface->set_network("127.0.0.1", "255.255.0.0", "127.0.0.1"), NSAPI_ERROR_UNSUPPORTED);
88+
}
89+
90+
TEST_F(TestNetworkInterface, set_dhcp)
91+
{
92+
EXPECT_EQ(iface->set_dhcp(true), NSAPI_ERROR_OK);
93+
EXPECT_EQ(iface->set_dhcp(false), NSAPI_ERROR_UNSUPPORTED);
94+
}
95+
96+
TEST_F(TestNetworkInterface, gethostbyname)
97+
{
98+
SocketAddress a;
99+
EXPECT_EQ(iface->gethostbyname("host", &a, NSAPI_UNSPEC), NSAPI_ERROR_OK);
100+
}
101+
102+
103+
static bool callback_is_called;
104+
static void my_callback(nsapi_error_t result, SocketAddress *address)
105+
{
106+
(void)result;
107+
(void)address;
108+
callback_is_called = true;
109+
}
110+
111+
TEST_F(TestNetworkInterface, gethostbyname_async)
112+
{
113+
SocketAddress a;
114+
EXPECT_EQ(iface->gethostbyname_async("host", mbed::callback(my_callback), NSAPI_UNSPEC), NSAPI_ERROR_OK);
115+
EXPECT_EQ(iface->gethostbyname_async_cancel(1), NSAPI_ERROR_OK);
116+
}
117+
118+
TEST_F(TestNetworkInterface, add_dns_server)
119+
{
120+
SocketAddress a("127.0.0.1", 1024);
121+
EXPECT_EQ(iface->add_dns_server(a), NSAPI_ERROR_OK);
122+
}
123+
124+
TEST_F(TestNetworkInterface, get_connection_status)
125+
{
126+
EXPECT_EQ(iface->get_connection_status(), NSAPI_ERROR_UNSUPPORTED);
127+
}
128+
129+
TEST_F(TestNetworkInterface, set_blocking)
130+
{
131+
EXPECT_EQ(iface->set_blocking(true), NSAPI_ERROR_UNSUPPORTED);
132+
}
133+
134+
// No way to test attach as it doesn't do or return anything.

UNITTESTS/features/netsocket/NetworkInterface/unittest.cmake

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,21 @@
55

66
# Source files
77
set(unittest-sources
8+
../features/netsocket/SocketAddress.cpp
9+
../features/netsocket/NetworkStack.cpp
810
../features/netsocket/NetworkInterface.cpp
11+
../features/frameworks/nanostack-libservice/source/libip4string/ip4tos.c
12+
../features/frameworks/nanostack-libservice/source/libip4string/stoip4.c
913
)
1014

1115
# Test files
1216
set(unittest-test-sources
17+
stubs/Mutex_stub.cpp
18+
stubs/mbed_assert_stub.c
19+
stubs/equeue_stub.c
20+
stubs/EventQueue_stub.cpp
21+
stubs/mbed_shared_queues_stub.cpp
22+
stubs/nsapi_dns_stub.cpp
23+
stubs/EventFlags_stub.cpp
1324
features/netsocket/NetworkInterface/test_NetworkInterface.cpp
1425
)

0 commit comments

Comments
 (0)