Skip to content

Commit 72233b0

Browse files
michalpasztamobicaSeppo Takalo
authored andcommitted
unittests: Add tests for NetworkStack class.
Improved the stubs for event queue and nsapi_dns, to allow checking if callback are handled correctly. This involves some memory allocation and deallocation. The NetworkStackWrapper is not covered as it seems to be deprecated code.
1 parent aa90f5b commit 72233b0

File tree

5 files changed

+280
-7
lines changed

5 files changed

+280
-7
lines changed
Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
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 "features/netsocket/NetworkStack.h"
20+
#include "netsocket/nsapi_dns.h"
21+
#include "events/EventQueue.h"
22+
#include <string>
23+
24+
// Control nsapi stub return value. See stubs/nsapi_dns_stub.cpp
25+
extern nsapi_error_t nsapi_stub_return_value;
26+
27+
// For testing async calls which require callback
28+
static bool callback_is_called;
29+
static SocketAddress address_received;
30+
static nsapi_error_t result_received;
31+
static void my_callback(nsapi_error_t result, SocketAddress *address)
32+
{
33+
result_received = result;
34+
address_received = *address;
35+
callback_is_called = true;
36+
}
37+
38+
static bool noarg_callback_is_called;
39+
static void noarg_callback(void)
40+
{
41+
noarg_callback_is_called = true;
42+
}
43+
44+
extern call_in_callback_cb_t callin_callback;
45+
46+
namespace mbed {
47+
extern events::EventQueue *mbed_shared_queue_stub;
48+
}
49+
50+
// NetworkStack is an abstract class we need to provide a child class for tests.
51+
class NetworkStackChild : public NetworkStack {
52+
virtual nsapi_error_t socket_open(nsapi_socket_t *handle, nsapi_protocol_t proto)
53+
{
54+
return NSAPI_ERROR_OK;
55+
}
56+
virtual nsapi_error_t socket_close(nsapi_socket_t handle)
57+
{
58+
return NSAPI_ERROR_OK;
59+
}
60+
virtual nsapi_error_t socket_bind(nsapi_socket_t handle, const SocketAddress &address)
61+
{
62+
return NSAPI_ERROR_OK;
63+
}
64+
virtual nsapi_error_t socket_listen(nsapi_socket_t handle, int backlog)
65+
{
66+
return NSAPI_ERROR_OK;
67+
}
68+
virtual nsapi_error_t socket_connect(nsapi_socket_t handle, const SocketAddress &address)
69+
{
70+
return NSAPI_ERROR_OK;
71+
}
72+
virtual nsapi_error_t socket_accept(nsapi_socket_t server,
73+
nsapi_socket_t *handle, SocketAddress *address = 0)
74+
{
75+
return NSAPI_ERROR_OK;
76+
}
77+
virtual nsapi_size_or_error_t socket_send(nsapi_socket_t handle,
78+
const void *data, nsapi_size_t size)
79+
{
80+
return NSAPI_ERROR_OK;
81+
}
82+
virtual nsapi_size_or_error_t socket_recv(nsapi_socket_t handle,
83+
void *data, nsapi_size_t size)
84+
{
85+
return NSAPI_ERROR_OK;
86+
}
87+
virtual nsapi_size_or_error_t socket_sendto(nsapi_socket_t handle, const SocketAddress &address,
88+
const void *data, nsapi_size_t size)
89+
{
90+
return NSAPI_ERROR_OK;
91+
}
92+
virtual nsapi_size_or_error_t socket_recvfrom(nsapi_socket_t handle, SocketAddress *address,
93+
void *buffer, nsapi_size_t size)
94+
{
95+
return NSAPI_ERROR_OK;
96+
}
97+
virtual void socket_attach(nsapi_socket_t handle, void (*callback)(void *), void *data)
98+
{
99+
}
100+
public:
101+
std::string ip_address;
102+
const char *get_ip_address()
103+
{
104+
return ip_address.c_str();
105+
106+
}
107+
};
108+
109+
class TestNetworkStack : public testing::Test {
110+
protected:
111+
NetworkStackChild *stack;
112+
113+
virtual void SetUp()
114+
{
115+
stack = new NetworkStackChild();
116+
address_received = SocketAddress();
117+
result_received = NSAPI_ERROR_OK;
118+
callback_is_called = false;
119+
noarg_callback_is_called = false;
120+
mbed::mbed_shared_queue_stub = 0;
121+
}
122+
123+
virtual void TearDown()
124+
{
125+
delete stack;
126+
if (mbed::mbed_shared_queue_stub)
127+
{
128+
delete mbed::mbed_shared_queue_stub;
129+
mbed::mbed_shared_queue_stub = 0;
130+
}
131+
}
132+
133+
void SetUpQueue()
134+
{
135+
mbed::mbed_shared_queue_stub = new events::EventQueue();
136+
}
137+
};
138+
139+
TEST_F(TestNetworkStack, constructor)
140+
{
141+
EXPECT_TRUE(stack);
142+
}
143+
144+
TEST_F(TestNetworkStack, get_ip_address_default)
145+
{
146+
EXPECT_EQ(stack->NetworkStack::get_ip_address(), (char*)NULL);
147+
}
148+
149+
/* gethostbyname */
150+
151+
TEST_F(TestNetworkStack, gethostbyname)
152+
{
153+
SocketAddress a;
154+
stack->ip_address = std::string("127.0.0.1");
155+
EXPECT_EQ(stack->gethostbyname("host", &a, NSAPI_UNSPEC), NSAPI_ERROR_DNS_FAILURE);
156+
}
157+
158+
TEST_F(TestNetworkStack, gethostbyname_simple_address)
159+
{
160+
SocketAddress a;
161+
EXPECT_EQ(stack->gethostbyname("127.0.0.1", &a, NSAPI_UNSPEC), NSAPI_ERROR_OK);
162+
}
163+
164+
TEST_F(TestNetworkStack, gethostbyname_simple_address_right_version)
165+
{
166+
SocketAddress a;
167+
EXPECT_EQ(stack->gethostbyname("127.0.0.1", &a, NSAPI_IPv4), NSAPI_ERROR_OK);
168+
}
169+
170+
TEST_F(TestNetworkStack, gethostbyname_simple_address_wrong_version)
171+
{
172+
SocketAddress a;
173+
EXPECT_EQ(stack->gethostbyname("127.0.0.1", &a, NSAPI_IPv6), NSAPI_ERROR_DNS_FAILURE);
174+
}
175+
176+
TEST_F(TestNetworkStack, gethostbyname_empty_host)
177+
{
178+
SocketAddress a;
179+
EXPECT_EQ(stack->gethostbyname("", &a, NSAPI_UNSPEC), NSAPI_ERROR_PARAMETER);
180+
}
181+
182+
/* gethostbyname_async */
183+
184+
TEST_F(TestNetworkStack, gethostbyname_async_delay)
185+
{
186+
SocketAddress a;
187+
stack->ip_address = std::string("127.0.0.1");
188+
SetUpQueue();
189+
EXPECT_EQ(stack->gethostbyname_async("localhost", mbed::callback(my_callback), NSAPI_UNSPEC), NSAPI_ERROR_DNS_FAILURE);
190+
EXPECT_EQ(callin_callback(1, mbed::callback(noarg_callback)), NSAPI_ERROR_OK);
191+
EXPECT_TRUE(noarg_callback_is_called);
192+
EXPECT_FALSE(callback_is_called);
193+
}
194+
195+
TEST_F(TestNetworkStack, gethostbyname_async)
196+
{
197+
SocketAddress a;
198+
stack->ip_address = std::string("127.0.0.1");
199+
SetUpQueue();
200+
EXPECT_EQ(stack->gethostbyname_async("localhost", mbed::callback(my_callback), NSAPI_UNSPEC), NSAPI_ERROR_DNS_FAILURE);
201+
EXPECT_EQ(callin_callback(0, mbed::callback(noarg_callback)), NSAPI_ERROR_OK);
202+
EXPECT_TRUE(noarg_callback_is_called);
203+
EXPECT_FALSE(callback_is_called);
204+
}
205+
206+
TEST_F(TestNetworkStack, gethostbyname_async_eventqueue_simple_address)
207+
{
208+
SocketAddress a;
209+
stack->ip_address = std::string("127.0.0.1");
210+
EXPECT_EQ(stack->gethostbyname_async("127.0.0.1", mbed::callback(my_callback), NSAPI_IPv6), NSAPI_ERROR_DNS_FAILURE);
211+
EXPECT_FALSE(callback_is_called);
212+
EXPECT_EQ(stack->gethostbyname_async("127.0.0.1", mbed::callback(my_callback), NSAPI_IPv4), NSAPI_ERROR_OK);
213+
EXPECT_TRUE(callback_is_called);
214+
EXPECT_EQ(result_received, NSAPI_ERROR_OK);
215+
EXPECT_EQ(std::string(address_received.get_ip_address()), stack->ip_address);
216+
}
217+
218+
TEST_F(TestNetworkStack, gethostbyname_async_empty_host)
219+
{
220+
SocketAddress a;
221+
EXPECT_EQ(stack->gethostbyname_async("", mbed::callback(my_callback), NSAPI_UNSPEC), NSAPI_ERROR_PARAMETER);
222+
}
223+
224+
TEST_F(TestNetworkStack, getstackopt)
225+
{
226+
EXPECT_EQ(stack->getstackopt(0,0,0,0), NSAPI_ERROR_UNSUPPORTED);
227+
}
228+
229+
TEST_F(TestNetworkStack, setstackopt)
230+
{
231+
EXPECT_EQ(stack->setstackopt(0,0,0,0), NSAPI_ERROR_UNSUPPORTED);
232+
}
233+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
####################
3+
# UNIT TESTS
4+
####################
5+
6+
# Unit test suite name
7+
set(TEST_SUITE_NAME "features_netsocket_NetworkStack")
8+
9+
# Source files
10+
set(unittest-sources
11+
../features/netsocket/SocketAddress.cpp
12+
../features/netsocket/NetworkStack.cpp
13+
../features/netsocket/NetworkInterface.cpp
14+
../features/frameworks/nanostack-libservice/source/libip4string/ip4tos.c
15+
../features/frameworks/nanostack-libservice/source/libip4string/stoip4.c
16+
)
17+
18+
# Test files
19+
set(unittest-test-sources
20+
stubs/Mutex_stub.cpp
21+
stubs/mbed_assert_stub.c
22+
stubs/equeue_stub.c
23+
stubs/EventQueue_stub.cpp
24+
stubs/mbed_shared_queues_stub.cpp
25+
stubs/nsapi_dns_stub.cpp
26+
stubs/EventFlags_stub.cpp
27+
features/netsocket/NetworkStack/test_NetworkStack.cpp
28+
)

UNITTESTS/stubs/equeue_stub.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717

1818
#include "equeue.h"
19+
#include <stdlib.h>
1920

2021
int equeue_create(equeue_t *queue, size_t size)
2122
{
@@ -47,19 +48,14 @@ int equeue_call(equeue_t *queue, void (*cb)(void *), void *data)
4748
return 0;
4849
}
4950

50-
int equeue_call_in(equeue_t *queue, int ms, void (*cb)(void *), void *data)
51-
{
52-
return 0;
53-
}
54-
5551
int equeue_call_every(equeue_t *queue, int ms, void (*cb)(void *), void *data)
5652
{
5753
return 0;
5854
}
5955

6056
void *equeue_alloc(equeue_t *queue, size_t size)
6157
{
62-
return NULL;
58+
return malloc(size);
6359
}
6460

6561
void equeue_dealloc(equeue_t *queue, void *event)
@@ -84,6 +80,12 @@ void equeue_event_dtor(void *event, void (*dtor)(void *))
8480

8581
int equeue_post(equeue_t *queue, void (*cb)(void *), void *event)
8682
{
83+
if (cb)
84+
{
85+
cb(event);
86+
free(event);
87+
return 1; //Fake ID for calling cancel
88+
}
8789
return 0;
8890
}
8991

@@ -102,3 +104,8 @@ void equeue_chain(equeue_t *queue, equeue_t *target)
102104
{
103105

104106
}
107+
108+
int equeue_call_in(equeue_t *q, int ms, void (*cb)(void*), void *data) {
109+
// The stub does not implement the delay mechanism.
110+
return equeue_post(q, cb, data);
111+
}

UNITTESTS/stubs/mbed_shared_queues_stub.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ using namespace events;
2121

2222
namespace mbed {
2323

24+
EventQueue *mbed_shared_queue_stub;
2425
EventQueue *mbed_event_queue()
2526
{
26-
return 0;
27+
return mbed_shared_queue_stub;
2728
}
2829
} // namespace mbed

UNITTESTS/stubs/nsapi_dns_stub.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020

2121
nsapi_error_t nsapi_stub_return_value = NSAPI_ERROR_DNS_FAILURE;
22+
NetworkStack::hostbyname_cb_t query_callback;
23+
call_in_callback_cb_t callin_callback;
2224

2325
nsapi_error_t nsapi_dns_query(NetworkStack *stack, const char *host,
2426
SocketAddress *addr, nsapi_version_t version)
@@ -30,6 +32,8 @@ nsapi_error_t nsapi_dns_query_async(NetworkStack *stack, const char *host,
3032
NetworkStack::hostbyname_cb_t callback, call_in_callback_cb_t call_in_cb,
3133
nsapi_version_t version)
3234
{
35+
query_callback = callback;
36+
callin_callback = call_in_cb;
3337
return nsapi_stub_return_value;
3438
}
3539

0 commit comments

Comments
 (0)