Skip to content

Commit 7a7550e

Browse files
Improve doxygen and unittest coverage for API
Mainly focusing on hardening the tests for return values.
1 parent 2127bb3 commit 7a7550e

File tree

17 files changed

+485
-124
lines changed

17 files changed

+485
-124
lines changed

UNITTESTS/features/netsocket/EthernetInterface/test_EthernetInterface.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,15 @@ class TestEthernetInterface: public testing::Test {
117117
TEST_F(TestEthernetInterface, constructor_default)
118118
{
119119
EXPECT_TRUE(iface);
120+
121+
// Test that this clas presents itself correctly
122+
EXPECT_NE(nullptr, iface->ethInterface());
123+
EXPECT_NE(nullptr, iface->emacInterface());
124+
125+
EXPECT_EQ(nullptr, iface->wifiInterface());
126+
EXPECT_EQ(nullptr, iface->cellularBase());
127+
EXPECT_EQ(nullptr, iface->cellularInterface());
128+
EXPECT_EQ(nullptr, iface->meshInterface());
120129
}
121130

122131
TEST_F(TestEthernetInterface, constructor_getter)
@@ -159,11 +168,15 @@ TEST_F(TestEthernetInterface, set_network)
159168
char ipAddress[NSAPI_IPv4_SIZE] = "127.0.0.1";
160169
char netmask[NSAPI_IPv4_SIZE] = "255.255.0.0";
161170
char gateway[NSAPI_IPv4_SIZE] = "127.0.0.2";
171+
char macAddress[NSAPI_MAC_SIZE] = "";
162172

163173
const char *ipAddressArg;
164174
const char *netmaskArg;
165175
const char *gatewayArg;
166176

177+
// Before connecting return NULL
178+
EXPECT_EQ(NULL, iface->get_mac_address());
179+
167180
EXPECT_EQ(0, iface->get_ip_address());
168181
EXPECT_EQ(0, iface->get_netmask());
169182
EXPECT_EQ(0, iface->get_gateway());
@@ -192,6 +205,11 @@ TEST_F(TestEthernetInterface, set_network)
192205
EXPECT_TRUE(0 == strcmp(gateway, gatewayArg));
193206

194207
// Testing the getters makes sense now.
208+
EXPECT_CALL(*netStackIface, get_mac_address(_, _))
209+
.Times(1)
210+
.WillOnce(DoAll(SetArrayArgument<0>(macAddress, macAddress+NSAPI_MAC_SIZE), Return(macAddress)));
211+
EXPECT_EQ(std::string(macAddress), std::string(iface->get_mac_address()));
212+
195213
EXPECT_CALL(*netStackIface, get_ip_address(_, _))
196214
.Times(1)
197215
.WillOnce(DoAll(SetArgPointee<0>(*ipAddress), Return(ipAddress)));
@@ -228,6 +246,45 @@ TEST_F(TestEthernetInterface, attach)
228246
iface->attach(cb);
229247
}
230248

249+
250+
TEST_F(TestEthernetInterface, get_interface_name)
251+
{
252+
char name[100] = "eth0";
253+
EXPECT_EQ(NULL, iface->get_interface_name(name));
254+
255+
doConnect();
256+
257+
// The parameter will be an internal variable.
258+
EXPECT_CALL(*netStackIface, get_interface_name(_))
259+
.Times(1)
260+
.WillOnce(Return(name));
261+
EXPECT_EQ(std::string(name), std::string(iface->get_interface_name(name)));
262+
}
263+
264+
TEST_F(TestEthernetInterface, get_ipv6_link_local_address)
265+
{
266+
SocketAddress addr("4.3.2.1");
267+
EXPECT_EQ(NSAPI_ERROR_NO_CONNECTION, iface->get_ipv6_link_local_address(&addr));
268+
EXPECT_EQ(std::string(addr.get_ip_address()), std::string("4.3.2.1"));
269+
doConnect();
270+
271+
// The parameter will be an internal variable.
272+
EXPECT_CALL(*netStackIface, get_ipv6_link_local_address(&addr))
273+
.Times(1)
274+
.WillOnce(Return(NSAPI_ERROR_OK));
275+
EXPECT_EQ(NSAPI_ERROR_OK, iface->get_ipv6_link_local_address(&addr));
276+
}
277+
278+
TEST_F(TestEthernetInterface, set_as_default)
279+
{
280+
doConnect();
281+
282+
EXPECT_CALL(*stackMock, set_default_interface(netStackIface))
283+
.Times(1);
284+
iface->set_as_default();
285+
}
286+
287+
231288
TEST_F(TestEthernetInterface, set_dhcp)
232289
{
233290
EXPECT_EQ(NSAPI_ERROR_OK, iface->set_dhcp(false));

UNITTESTS/features/netsocket/NetworkInterface/test_NetworkInterface.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,12 @@ TEST_F(TestNetworkInterface, set_blocking)
148148
EXPECT_EQ(iface->set_blocking(true), NSAPI_ERROR_UNSUPPORTED);
149149
}
150150

151+
TEST_F(TestNetworkInterface, get_ipv6_link_local_address)
152+
{
153+
SocketAddress a;
154+
EXPECT_EQ(iface->get_ipv6_link_local_address(&a), NSAPI_ERROR_UNSUPPORTED);
155+
}
156+
151157
void my_iface_callback(nsapi_event_t e, intptr_t i)
152158
{
153159
(void)e;
Lines changed: 292 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,292 @@
1+
/*
2+
* Copyright (c) 2018, Arm Limited and affiliates
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "gtest/gtest.h"
19+
#include "gmock/gmock.h"
20+
21+
#include "OnboardNetworkStack_mock.h"
22+
23+
#include "features/netsocket/PPPInterface.h"
24+
#include <iostream>
25+
#include "FileHandle_stub.h"
26+
27+
28+
OnboardNetworkStack &OnboardNetworkStack::get_default_instance()
29+
{
30+
return OnboardNetworkStackMock::get_instance();
31+
}
32+
33+
using ::testing::_;
34+
using ::testing::Return;
35+
using ::testing::ReturnArg;
36+
using ::testing::SaveArg;
37+
using ::testing::SaveArgPointee;
38+
using ::testing::SetArrayArgument;
39+
using ::testing::SetArgPointee;
40+
using ::testing::SetArgReferee;
41+
42+
class MockPPP : public PPP {
43+
public:
44+
MOCK_METHOD0(power_up, bool());
45+
MOCK_METHOD0(power_down, void());
46+
MOCK_METHOD0(get_mtu_size, uint32_t());
47+
MOCK_CONST_METHOD0(get_align_preference, uint32_t());
48+
MOCK_CONST_METHOD2(get_ifname, void(char *name, uint8_t size));
49+
MOCK_METHOD2(link_out, bool(net_stack_mem_buf_t *buf, nsapi_ip_stack_t ip_stack));
50+
MOCK_METHOD1(set_stream, void(mbed::FileHandle *stream));
51+
MOCK_METHOD1(set_ip_stack, void(nsapi_ip_stack_t ip_stack));
52+
MOCK_METHOD2(set_credentials, void(const char *uname, const char *password));
53+
MOCK_METHOD1(get_ip_address, const nsapi_addr_t *(nsapi_version_t version));
54+
MOCK_METHOD0(get_netmask, const nsapi_addr_t *());
55+
MOCK_METHOD0(get_gateway, const nsapi_addr_t *());
56+
MOCK_METHOD1(get_dns_server, const nsapi_addr_t *(uint8_t index));
57+
MOCK_METHOD1(set_link_input_cb, void(ppp_link_input_cb_t input_cb));
58+
MOCK_METHOD1(set_link_state_cb, void(ppp_link_state_change_cb_t state_cb));
59+
MOCK_METHOD1(set_memory_manager, void(NetStackMemoryManager &mem_mngr));
60+
61+
static MockPPP &get_instance()
62+
{
63+
static MockPPP pppMock1;
64+
return pppMock1;
65+
}
66+
};
67+
68+
MBED_WEAK PPP &PPP::get_default_instance()
69+
{
70+
return MockPPP::get_instance();
71+
}
72+
73+
class TestPPPInterface: public testing::Test {
74+
protected:
75+
PPPInterface *iface;
76+
OnboardNetworkStackMock *stackMock;
77+
MockPPP *pppMock;
78+
OnboardNetworkStackMock::InterfaceMock *netStackIface;
79+
virtual void SetUp()
80+
{
81+
stackMock = &OnboardNetworkStackMock::get_instance();
82+
pppMock = &MockPPP::get_instance();
83+
netStackIface = &OnboardNetworkStackMock::InterfaceMock::get_instance();
84+
iface = new PPPInterface(MockPPP::get_instance(), OnboardNetworkStackMock::get_instance());
85+
}
86+
87+
virtual void TearDown()
88+
{
89+
// Do not delete the mocks pointers, as they point to statically allocated singletons.
90+
delete iface;
91+
}
92+
93+
/* Enclose the heavily-used connection procedure to improve code redability */
94+
void doConnect(bool blocking = true)
95+
{
96+
EXPECT_CALL(*pppMock, set_stream(_));
97+
EXPECT_CALL(*pppMock, set_ip_stack(_));
98+
EXPECT_CALL(*pppMock, set_credentials(_, _));
99+
EXPECT_CALL(*stackMock, add_ppp_interface(testing::Ref(*pppMock), true, _))
100+
.Times(1)
101+
.WillOnce(DoAll(SetArgPointee<2>(netStackIface), Return(NSAPI_ERROR_OK)));
102+
EXPECT_CALL(*netStackIface, attach(_))
103+
.Times(1)
104+
.RetiresOnSaturation();
105+
EXPECT_CALL(*netStackIface, bringup(false, NULL, NULL, NULL, DEFAULT_STACK, blocking))
106+
.Times(1)
107+
.WillOnce(Return(NSAPI_ERROR_OK));
108+
EXPECT_EQ(NSAPI_ERROR_OK, iface->connect());
109+
}
110+
111+
static void cb(nsapi_event_t ev, intptr_t ptr)
112+
{
113+
114+
}
115+
};
116+
117+
TEST_F(TestPPPInterface, constructor_default)
118+
{
119+
EXPECT_TRUE(iface);
120+
// Test that this clas presents itself correctly
121+
EXPECT_NE(nullptr, iface->pppInterface());
122+
123+
EXPECT_EQ(nullptr, iface->emacInterface());
124+
EXPECT_EQ(nullptr, iface->ethInterface());
125+
EXPECT_EQ(nullptr, iface->wifiInterface());
126+
EXPECT_EQ(nullptr, iface->cellularBase());
127+
EXPECT_EQ(nullptr, iface->cellularInterface());
128+
EXPECT_EQ(nullptr, iface->meshInterface());
129+
}
130+
131+
TEST_F(TestPPPInterface, connect)
132+
{
133+
mbed::FileHandle_stub handle;
134+
nsapi_ip_stack_t stack = IPV4_STACK;
135+
iface->set_credentials("uname", "passwd");
136+
iface->set_stream(&handle);
137+
iface->set_ip_stack(stack);
138+
139+
EXPECT_CALL(*pppMock, set_stream(&handle));
140+
EXPECT_CALL(*pppMock, set_ip_stack(stack));
141+
EXPECT_CALL(*pppMock, set_credentials("uname", "passwd"));
142+
EXPECT_CALL(*stackMock, add_ppp_interface(testing::Ref(*pppMock), true, _))
143+
.Times(1)
144+
.WillOnce(DoAll(SetArgPointee<2>(netStackIface), Return(NSAPI_ERROR_OK)));
145+
EXPECT_CALL(*netStackIface, attach(_))
146+
.Times(1)
147+
.RetiresOnSaturation();
148+
EXPECT_CALL(*netStackIface, bringup(false, NULL, NULL, NULL, stack, true))
149+
.Times(1)
150+
.WillOnce(Return(NSAPI_ERROR_OK));
151+
EXPECT_EQ(NSAPI_ERROR_OK, iface->connect());
152+
}
153+
154+
TEST_F(TestPPPInterface, connect_failure)
155+
{
156+
EXPECT_CALL(*pppMock, set_stream(_));
157+
EXPECT_CALL(*pppMock, set_ip_stack(_));
158+
EXPECT_CALL(*pppMock, set_credentials(_, _));
159+
EXPECT_CALL(*stackMock, add_ppp_interface(testing::Ref(*pppMock), true, _))
160+
.Times(1)
161+
.WillOnce(DoAll(SetArgPointee<2>(netStackIface), Return(NSAPI_ERROR_NO_MEMORY)));
162+
EXPECT_EQ(NSAPI_ERROR_NO_MEMORY, iface->connect());
163+
}
164+
165+
TEST_F(TestPPPInterface, disconnect_without_connecting)
166+
{
167+
EXPECT_EQ(NSAPI_ERROR_NO_CONNECTION, iface->disconnect());
168+
}
169+
170+
TEST_F(TestPPPInterface, disconnect)
171+
{
172+
doConnect();
173+
174+
EXPECT_CALL(*netStackIface, bringdown())
175+
.Times(1)
176+
.WillOnce(Return(NSAPI_ERROR_OK));
177+
EXPECT_EQ(NSAPI_ERROR_OK, iface->disconnect());
178+
}
179+
180+
TEST_F(TestPPPInterface, set_network)
181+
{
182+
char ipAddress[NSAPI_IPv4_SIZE] = "127.0.0.1";
183+
char netmask[NSAPI_IPv4_SIZE] = "255.255.0.0";
184+
char gateway[NSAPI_IPv4_SIZE] = "127.0.0.2";
185+
186+
const char *ipAddressArg;
187+
const char *netmaskArg;
188+
const char *gatewayArg;
189+
190+
EXPECT_EQ(0, iface->get_ip_address());
191+
EXPECT_EQ(0, iface->get_netmask());
192+
EXPECT_EQ(0, iface->get_gateway());
193+
194+
EXPECT_CALL(*pppMock, set_stream(_));
195+
EXPECT_CALL(*pppMock, set_ip_stack(_));
196+
EXPECT_CALL(*pppMock, set_credentials(_, _));
197+
198+
// Set the network data
199+
EXPECT_EQ(NSAPI_ERROR_OK, iface->set_network(ipAddress, netmask, gateway));
200+
201+
// Now the bringup should have different arguments. We can't use doConnect method.
202+
EXPECT_CALL(*stackMock, add_ppp_interface(testing::Ref(*pppMock), true, _))
203+
.Times(1)
204+
.WillOnce(DoAll(SetArgPointee<2>(netStackIface), Return(NSAPI_ERROR_OK)));
205+
EXPECT_CALL(*netStackIface, attach(_))
206+
.Times(1)
207+
.RetiresOnSaturation();
208+
// Do not put the expected char * arguments, as they are pointers and would not match
209+
EXPECT_CALL(*netStackIface, bringup(false, _, _, _, DEFAULT_STACK, true))
210+
.Times(1)
211+
.WillOnce(DoAll(SaveArg<1>(&ipAddressArg),
212+
SaveArg<2>(&netmaskArg),
213+
SaveArg<3>(&gatewayArg),
214+
Return(NSAPI_ERROR_OK)));
215+
EXPECT_EQ(NSAPI_ERROR_OK, iface->connect());
216+
// Check the contents of the stored pointer arguments.
217+
EXPECT_TRUE(0 == strcmp(ipAddress, ipAddressArg));
218+
EXPECT_TRUE(0 == strcmp(netmask, netmaskArg));
219+
EXPECT_TRUE(0 == strcmp(gateway, gatewayArg));
220+
221+
// Testing the getters makes sense now.
222+
EXPECT_CALL(*netStackIface, get_ip_address(_, _))
223+
.Times(1)
224+
.WillOnce(DoAll(SetArgPointee<0>(*ipAddress), Return(ipAddress)));
225+
EXPECT_EQ(std::string(ipAddress), std::string(iface->get_ip_address()));
226+
227+
EXPECT_CALL(*netStackIface, get_netmask(_, _))
228+
.Times(1)
229+
.WillOnce(DoAll(SetArgPointee<0>(*netmask), Return(netmask)));
230+
EXPECT_EQ(std::string(netmask), std::string(iface->get_netmask()));
231+
232+
EXPECT_CALL(*netStackIface, get_gateway(_, _))
233+
.Times(1)
234+
.WillOnce(DoAll(SetArgPointee<0>(*gateway), Return(gateway)));
235+
EXPECT_EQ(std::string(gateway), std::string(iface->get_gateway()));
236+
}
237+
238+
TEST_F(TestPPPInterface, get_connection_status)
239+
{
240+
EXPECT_EQ(NSAPI_STATUS_DISCONNECTED, iface->get_connection_status());
241+
242+
doConnect();
243+
244+
EXPECT_CALL(*netStackIface, get_connection_status())
245+
.Times(1)
246+
.WillOnce(Return(NSAPI_STATUS_LOCAL_UP));
247+
EXPECT_EQ(NSAPI_STATUS_LOCAL_UP, iface->get_connection_status());
248+
}
249+
250+
TEST_F(TestPPPInterface, get_interface_name)
251+
{
252+
char name[100];
253+
EXPECT_EQ(NULL, iface->get_interface_name(name));
254+
255+
doConnect();
256+
257+
EXPECT_EQ(NULL, iface->get_interface_name(name));
258+
259+
EXPECT_CALL(*netStackIface, get_interface_name(name))
260+
.Times(1)
261+
.WillOnce(Return(name));
262+
EXPECT_EQ(name, iface->get_interface_name(name));
263+
}
264+
265+
TEST_F(TestPPPInterface, set_as_default)
266+
{
267+
doConnect();
268+
269+
EXPECT_CALL(*stackMock, set_default_interface(netStackIface))
270+
.Times(1);
271+
iface->set_as_default();
272+
}
273+
274+
TEST_F(TestPPPInterface, attach)
275+
{
276+
doConnect();
277+
EXPECT_CALL(*netStackIface, attach(_)) // TODO: check that the correct function is passed.
278+
.Times(1);
279+
iface->attach(cb);
280+
}
281+
282+
TEST_F(TestPPPInterface, set_dhcp)
283+
{
284+
EXPECT_EQ(NSAPI_ERROR_UNSUPPORTED, iface->set_dhcp(false));
285+
doConnect(true);
286+
}
287+
288+
TEST_F(TestPPPInterface, set_blocking)
289+
{
290+
EXPECT_EQ(NSAPI_ERROR_OK, iface->set_blocking(false));
291+
doConnect(false);
292+
}

0 commit comments

Comments
 (0)