Skip to content

Commit ba3727b

Browse files
Mirela ChiricaAri Parkkila
authored andcommitted
Cellular: Unit tests for Non-IP socket
1 parent d301e13 commit ba3727b

File tree

17 files changed

+595
-116
lines changed

17 files changed

+595
-116
lines changed

UNITTESTS/features/cellular/framework/AT/at_cellulardevice/at_cellulardevicetest.cpp

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -51,26 +51,26 @@ TEST_F(TestAT_CellularDevice, test_AT_CellularDevice_get_at_handler)
5151
FileHandle_stub fh1;
5252
FileHandle_stub fh2;
5353
FileHandle_stub fh3;
54-
AT_CellularDevice dev(&fh1);
54+
AT_CellularDevice dev(&fh1); // AT fh1 ref count 1
5555

56-
EXPECT_TRUE(dev.open_network(&fh1));
56+
EXPECT_TRUE(dev.open_network(&fh1)); // AT fh1 ref count 2
5757
EXPECT_TRUE(dev.open_sms(&fh2));
5858
AT_CellularBase_stub::handler_value = AT_CellularBase_stub::handler_at_constructor_value;
5959
EXPECT_TRUE(dev.open_information(&fh3));
6060
ATHandler_stub::fh_value = &fh1;
61-
EXPECT_TRUE(dev.open_power(&fh1));
61+
EXPECT_TRUE(dev.open_power(&fh1)); // AT fh1 ref count 3
6262

6363
ATHandler_stub::fh_value = NULL;
6464

65-
AT_CellularDevice *dev2 = new AT_CellularDevice(&fh1);
66-
EXPECT_TRUE(dev2->open_information(&fh1));
67-
ATHandler *at = dev2->get_at_handler();
65+
AT_CellularDevice *dev2 = new AT_CellularDevice(&fh1); // AT fh1 ref count 4
66+
EXPECT_TRUE(dev2->open_information(&fh1)); // AT fh1 ref count 5
67+
ATHandler *at = dev2->get_at_handler(); // AT fh1 ref count 6
68+
EXPECT_TRUE(at->get_ref_count() == 6);
69+
delete dev2; // AT fh1 2 refs deleted -> ref count 4
6870
EXPECT_TRUE(at->get_ref_count() == 4);
69-
delete dev2;
70-
EXPECT_TRUE(at->get_ref_count() == 3);
71-
AT_CellularDevice dev3(&fh1);
72-
EXPECT_TRUE(dev3.release_at_handler(at) == NSAPI_ERROR_OK);
73-
EXPECT_TRUE(ATHandler_stub::ref_count == 2);
71+
AT_CellularDevice dev3(&fh1); // AT fh1 ref count 5
72+
EXPECT_TRUE(dev3.release_at_handler(at) == NSAPI_ERROR_OK); // AT fh1 ref count 4
73+
EXPECT_TRUE(ATHandler_stub::ref_count == 4);
7474
}
7575

7676
TEST_F(TestAT_CellularDevice, test_AT_CellularDevice_open_network)
@@ -129,42 +129,39 @@ TEST_F(TestAT_CellularDevice, test_AT_CellularDevice_close_network)
129129
{
130130
FileHandle_stub fh1;
131131
AT_CellularDevice dev(&fh1);
132-
ATHandler_stub::ref_count = 0;
133132

134133
EXPECT_TRUE(dev.open_network(&fh1));
135134
AT_CellularBase_stub::handler_value = AT_CellularBase_stub::handler_at_constructor_value;
136-
EXPECT_TRUE(ATHandler_stub::ref_count == 1);
135+
EXPECT_TRUE(ATHandler_stub::ref_count == 2);
137136

138137
dev.close_network();
139-
EXPECT_TRUE(ATHandler_stub::ref_count == kATHandler_destructor_ref_ount);
138+
EXPECT_TRUE(ATHANDLER_REF_COUNT_AT_DESTRUCTOR == kATHandler_destructor_ref_ount);
140139
}
141140

142141
TEST_F(TestAT_CellularDevice, test_AT_CellularDevice_close_sms)
143142
{
144143
FileHandle_stub fh1;
145144
AT_CellularDevice dev(&fh1);
146-
ATHandler_stub::ref_count = 0;
147145

148146
EXPECT_TRUE(dev.open_sms(&fh1));
149147
AT_CellularBase_stub::handler_value = AT_CellularBase_stub::handler_at_constructor_value;
150-
EXPECT_TRUE(ATHandler_stub::ref_count == 1);
148+
EXPECT_TRUE(ATHandler_stub::ref_count == 2);
151149

152150
dev.close_sms();
153-
EXPECT_TRUE(ATHandler_stub::ref_count == kATHandler_destructor_ref_ount);
151+
EXPECT_TRUE(ATHANDLER_REF_COUNT_AT_DESTRUCTOR == kATHandler_destructor_ref_ount);
154152
}
155153

156154
TEST_F(TestAT_CellularDevice, test_AT_CellularDevice_close_power)
157155
{
158156
FileHandle_stub fh1;
159157
AT_CellularDevice dev(&fh1);
160-
ATHandler_stub::ref_count = 0;
161158

162159
EXPECT_TRUE(dev.open_power(&fh1));
163160
AT_CellularBase_stub::handler_value = AT_CellularBase_stub::handler_at_constructor_value;
164-
EXPECT_TRUE(ATHandler_stub::ref_count == 1);
161+
EXPECT_TRUE(ATHandler_stub::ref_count == 2);
165162

166163
dev.close_power();
167-
EXPECT_TRUE(ATHandler_stub::ref_count == kATHandler_destructor_ref_ount);
164+
EXPECT_TRUE(ATHANDLER_REF_COUNT_AT_DESTRUCTOR == kATHandler_destructor_ref_ount);
168165
}
169166

170167
TEST_F(TestAT_CellularDevice, test_AT_CellularDevice_close_information)
@@ -188,7 +185,7 @@ TEST_F(TestAT_CellularDevice, test_AT_CellularDevice_close_information)
188185
AT_CellularBase_stub::handler_value = AT_CellularBase_stub::handler_at_constructor_value;
189186

190187
dev.close_information();
191-
EXPECT_TRUE(ATHandler_stub::ref_count == kATHandler_destructor_ref_ount);
188+
EXPECT_TRUE(ATHANDLER_REF_COUNT_AT_DESTRUCTOR == kATHandler_destructor_ref_ount);
192189

193190
ATHandler_stub::fh_value = NULL;
194191
}
@@ -205,7 +202,7 @@ TEST_F(TestAT_CellularDevice, test_AT_CellularDevice_set_timeout)
205202
EXPECT_TRUE(ATHandler_stub::default_timeout == true);
206203

207204
EXPECT_TRUE(dev.open_sms(&fh1));
208-
EXPECT_TRUE(ATHandler_stub::ref_count == 1);
205+
EXPECT_TRUE(ATHandler_stub::ref_count == 2);
209206

210207
dev.set_timeout(5000);
211208
EXPECT_TRUE(ATHandler_stub::timeout == 5000);
@@ -224,7 +221,7 @@ TEST_F(TestAT_CellularDevice, test_AT_CellularDevice_modem_debug_on)
224221
EXPECT_TRUE(ATHandler_stub::debug_on == true);
225222

226223
EXPECT_TRUE(dev.open_sms(&fh1));
227-
EXPECT_TRUE(ATHandler_stub::ref_count == 1);
224+
EXPECT_TRUE(ATHandler_stub::ref_count == 2);
228225

229226
dev.modem_debug_on(true);
230227
EXPECT_TRUE(ATHandler_stub::debug_on == true);
@@ -305,20 +302,20 @@ TEST_F(TestAT_CellularDevice, test_AT_CellularDevice_create_delete_context)
305302
AT_CellularDevice *dev = new AT_CellularDevice(&fh1);
306303

307304
ATHandler *at = dev->get_at_handler();
308-
EXPECT_TRUE(at->get_ref_count() == 1);
305+
EXPECT_TRUE(at->get_ref_count() == 2);
309306
EXPECT_TRUE(dev->release_at_handler(at) == NSAPI_ERROR_OK);
310307

311308
CellularContext *ctx = dev->create_context(NULL);
312309
delete dev;
313310

314311
dev = new AT_CellularDevice(&fh1);
315312
at = dev->get_at_handler();
316-
EXPECT_TRUE(at->get_ref_count() == 1);
313+
EXPECT_TRUE(at->get_ref_count() == 2);
317314
ctx = dev->create_context(NULL);
318315
CellularContext *ctx1 = dev->create_context(&fh1);
319-
EXPECT_TRUE(at->get_ref_count() == 3);
320-
CellularContext *ctx2 = dev->create_context(&fh1);
321316
EXPECT_TRUE(at->get_ref_count() == 4);
317+
CellularContext *ctx2 = dev->create_context(&fh1);
318+
EXPECT_TRUE(at->get_ref_count() == 5);
322319

323320
EXPECT_TRUE(ctx);
324321
EXPECT_TRUE(ctx1);
@@ -329,20 +326,20 @@ TEST_F(TestAT_CellularDevice, test_AT_CellularDevice_create_delete_context)
329326
EXPECT_TRUE(xx);
330327

331328
dev->delete_context(ctx);
332-
EXPECT_TRUE(at->get_ref_count() == 3);
329+
EXPECT_TRUE(at->get_ref_count() == 4);
333330
dev->delete_context(ctx1);
334-
EXPECT_TRUE(at->get_ref_count() == 2);
331+
EXPECT_TRUE(at->get_ref_count() == 3);
335332
dev->delete_context(NULL);
336-
EXPECT_TRUE(at->get_ref_count() == 2);
333+
EXPECT_TRUE(at->get_ref_count() == 3);
337334
dev->delete_context(ctx2);
338-
EXPECT_TRUE(at->get_ref_count() == 1);
335+
EXPECT_TRUE(at->get_ref_count() == 2);
339336

340337
ctx = dev->create_context(NULL);
341-
EXPECT_TRUE(at->get_ref_count() == 2);
342-
ctx1 = dev->create_context(&fh1);
343338
EXPECT_TRUE(at->get_ref_count() == 3);
344-
ctx2 = dev->create_context(&fh1);
339+
ctx1 = dev->create_context(&fh1);
345340
EXPECT_TRUE(at->get_ref_count() == 4);
341+
ctx2 = dev->create_context(&fh1);
342+
EXPECT_TRUE(at->get_ref_count() == 5);
346343
EXPECT_TRUE(dev->release_at_handler(at) == NSAPI_ERROR_OK);
347344
EXPECT_TRUE(ctx);
348345
EXPECT_TRUE(ctx1);
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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/cellular/CellularNonIPSocket.h"
20+
#include "CellularContext_stub.h"
21+
22+
using namespace mbed;
23+
24+
// Control the rtos EventFlags stub. See EventFlags_stub.cpp
25+
extern std::list<uint32_t> eventFlagsStubNextRetval;
26+
27+
static bool callback_is_called;
28+
static void my_callback()
29+
{
30+
callback_is_called = true;
31+
}
32+
33+
class TestCellularNonIPSocket : public testing::Test {
34+
protected:
35+
CellularNonIPSocket *socket;
36+
ControlPlane_netif_stub *cp_netif;
37+
CellularContext_stub cellular_context;
38+
nsapi_size_t dataSize;
39+
char dataBuf[10];
40+
41+
virtual void SetUp()
42+
{
43+
socket = new CellularNonIPSocket();
44+
cp_netif = NULL;
45+
dataSize = 10;
46+
}
47+
48+
virtual void TearDown()
49+
{
50+
delete socket;
51+
}
52+
};
53+
54+
TEST_F(TestCellularNonIPSocket, open_null_cp_netif)
55+
{
56+
EXPECT_EQ(socket->open(static_cast<ControlPlane_netif *>(NULL)), NSAPI_ERROR_PARAMETER);
57+
}
58+
59+
TEST_F(TestCellularNonIPSocket, open_null_context)
60+
{
61+
EXPECT_EQ(socket->open(static_cast<CellularContext *>(NULL)), NSAPI_ERROR_PARAMETER);
62+
}
63+
64+
TEST_F(TestCellularNonIPSocket, open_context)
65+
{
66+
EXPECT_EQ(socket->open((CellularContext *)&cellular_context), NSAPI_ERROR_OK);
67+
}
68+
69+
TEST_F(TestCellularNonIPSocket, open_cp_netif)
70+
{
71+
cp_netif = cellular_context.get_cp_netif();
72+
EXPECT_EQ(socket->open((ControlPlane_netif *)cp_netif), NSAPI_ERROR_OK);
73+
}
74+
75+
TEST_F(TestCellularNonIPSocket, open_twice)
76+
{
77+
EXPECT_EQ(socket->open((CellularContext *)&cellular_context), NSAPI_ERROR_OK);
78+
EXPECT_EQ(socket->open((CellularContext *)&cellular_context), NSAPI_ERROR_PARAMETER);
79+
}
80+
81+
TEST_F(TestCellularNonIPSocket, close)
82+
{
83+
socket->open((CellularContext *)&cellular_context);
84+
EXPECT_EQ(socket->close(), NSAPI_ERROR_OK);
85+
}
86+
87+
TEST_F(TestCellularNonIPSocket, close_no_open)
88+
{
89+
EXPECT_EQ(socket->close(), NSAPI_ERROR_NO_SOCKET);
90+
}
91+
92+
TEST_F(TestCellularNonIPSocket, sigio)
93+
{
94+
callback_is_called = false;
95+
socket->open((CellularContext *)&cellular_context);
96+
socket->sigio(mbed::callback(my_callback));
97+
socket->close(); // Trigger event;
98+
EXPECT_EQ(callback_is_called, true);
99+
}
100+
101+
/* send */
102+
103+
TEST_F(TestCellularNonIPSocket, send_no_open)
104+
{
105+
EXPECT_EQ(socket->send((char *)dataBuf, dataSize), NSAPI_ERROR_NO_SOCKET);
106+
}
107+
108+
TEST_F(TestCellularNonIPSocket, send_error_would_block)
109+
{
110+
socket->open((CellularContext *)&cellular_context);
111+
cp_netif = cellular_context.get_cp_netif();
112+
cp_netif->return_value = NSAPI_ERROR_WOULD_BLOCK;
113+
eventFlagsStubNextRetval.push_back(osFlagsError); // Break the wait loop
114+
EXPECT_EQ(socket->send(dataBuf, dataSize), NSAPI_ERROR_WOULD_BLOCK);
115+
}
116+
117+
TEST_F(TestCellularNonIPSocket, send_error_other)
118+
{
119+
socket->open((CellularContext *)&cellular_context);
120+
cp_netif = cellular_context.get_cp_netif();
121+
cp_netif->return_value = NSAPI_ERROR_NO_MEMORY;
122+
EXPECT_EQ(socket->send(dataBuf, dataSize), NSAPI_ERROR_NO_MEMORY);
123+
}
124+
125+
TEST_F(TestCellularNonIPSocket, send_error_no_timeout)
126+
{
127+
socket->open((CellularContext *)&cellular_context);
128+
cp_netif = cellular_context.get_cp_netif();
129+
cp_netif->return_value = NSAPI_ERROR_WOULD_BLOCK;
130+
socket->set_blocking(false);
131+
EXPECT_EQ(socket->send(dataBuf, dataSize), NSAPI_ERROR_WOULD_BLOCK);
132+
}
133+
134+
TEST_F(TestCellularNonIPSocket, send)
135+
{
136+
socket->open((CellularContext *)&cellular_context);
137+
cp_netif = cellular_context.get_cp_netif();
138+
cp_netif->return_value = dataSize;
139+
EXPECT_EQ(socket->send(dataBuf, dataSize), dataSize);
140+
}
141+
142+
TEST_F(TestCellularNonIPSocket, recv)
143+
{
144+
EXPECT_EQ(socket->recv(&dataBuf, dataSize), NSAPI_ERROR_NO_SOCKET);
145+
146+
socket->open((CellularContext *)&cellular_context);
147+
cp_netif = cellular_context.get_cp_netif();
148+
149+
cp_netif->return_value = 100;
150+
EXPECT_EQ(socket->recv(&dataBuf, dataSize), 100);
151+
152+
cp_netif->return_value = NSAPI_ERROR_WOULD_BLOCK;
153+
eventFlagsStubNextRetval.push_back(0);
154+
eventFlagsStubNextRetval.push_back(osFlagsError); // Break the wait loop
155+
EXPECT_EQ(socket->recv(&dataBuf, dataSize), NSAPI_ERROR_WOULD_BLOCK);
156+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
####################
3+
# UNIT TESTS
4+
####################
5+
6+
# Add test specific include paths
7+
set(unittest-includes ${unittest-includes}
8+
../features/netsocket/cellular
9+
)
10+
11+
set(unittest-sources
12+
../features/netsocket/cellular/CellularNonIPSocket.cpp
13+
)
14+
15+
set(unittest-test-sources
16+
features/netsocket/cellular/CellularNonIPSocket/test_CellularNonIPSocket.cpp
17+
stubs/NetworkInterface_stub.cpp
18+
stubs/NetworkStack_stub.cpp
19+
stubs/EventFlags_stub.cpp
20+
stubs/Mutex_stub.cpp
21+
)

UNITTESTS/stubs/ATHandler_stub.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@
2626
#ifndef __AT_HANDLER_STUB_H__
2727
#define __AT_HANDLER_STUB_H__
2828

29+
#define ATHANDLER_REF_COUNT_AT_DESTRUCTOR -909
30+
2931
static const int kRead_string_table_size = 100;
3032
static const int kRead_int_table_size = 100;
3133
static const int kResp_stop_count_default = 100;
3234
// set reference count to -909 to separate it from zero so we can test that ATHandler is really deleted.
33-
static const int kATHandler_destructor_ref_ount = -909;
35+
static const int kATHandler_destructor_ref_ount = ATHANDLER_REF_COUNT_AT_DESTRUCTOR;
3436
static const int kATHandler_urc_table_max_size = 10;
3537
static const int kATHandler_urc_string_max_size = 16;
3638

UNITTESTS/stubs/AT_CellularContext_stub.cpp

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

2222
AT_CellularContext::AT_CellularContext(ATHandler &at, CellularDevice *device, const char *apn, bool cp_req, bool nonip_req) :
2323
AT_CellularBase(at), _is_blocking(true), _is_connected(false),
24-
_current_op(OP_INVALID), _device(device), _nw(0), _fh(0), _cp_req(cp_req), _nonip_req(nonip_req), _cp_in_use(false), _cp_netif(NULL)
24+
_current_op(OP_INVALID), _device(device), _nw(0), _fh(0), _cp_req(cp_req), _nonip_req(nonip_req), _cp_in_use(false)
2525
{
2626
_stack = NULL;
2727
_pdp_type = DEFAULT_PDP_TYPE;
@@ -36,6 +36,7 @@ AT_CellularContext::AT_CellularContext(ATHandler &at, CellularDevice *device, co
3636
_cid = -1;
3737
_new_context_set = false;
3838
_next = NULL;
39+
_cp_netif = NULL;
3940
}
4041

4142
AT_CellularContext::~AT_CellularContext()

0 commit comments

Comments
 (0)