Skip to content

Commit e5c3024

Browse files
author
Teppo Järvelin
committed
Cellular: adding unit tests for new class CellularContext and changed classes.
1 parent 8a2044b commit e5c3024

22 files changed

+1122
-667
lines changed

UNITTESTS/features/cellular/framework/AT/at_cellularcontext/at_cellularcontexttest.cpp

Lines changed: 469 additions & 4 deletions
Large diffs are not rendered by default.

UNITTESTS/features/cellular/framework/AT/at_cellularcontext/unittest.cmake

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,26 @@ set(unittest-includes ${unittest-includes}
1414
# Source files
1515
set(unittest-sources
1616
../features/cellular/framework/AT/AT_CellularContext.cpp
17+
../features/cellular/framework/common/CellularUtil.cpp
1718
)
1819

1920
# Test files
2021
set(unittest-test-sources
2122
features/cellular/framework/AT/at_cellularcontext/at_cellularcontexttest.cpp
2223
stubs/ATHandler_stub.cpp
2324
stubs/AT_CellularBase_stub.cpp
25+
stubs/AT_CellularDevice_stub.cpp
26+
stubs/AT_CellularStack_stub.cpp
27+
stubs/AT_CellularNetwork_stub.cpp
28+
stubs/CellularDevice_stub.cpp
29+
stubs/CellularStateMachine_stub.cpp
30+
stubs/equeue_stub.c
2431
stubs/EventQueue_stub.cpp
2532
stubs/FileHandle_stub.cpp
33+
stubs/mbed_assert_stub.c
2634
stubs/NetworkInterface_stub.cpp
2735
stubs/NetworkStack_stub.cpp
28-
stubs/us_ticker_stub.cpp
29-
stubs/mbed_assert_stub.c
30-
stubs/CellularDevice_stub.cpp
31-
stubs/CellularStateMachine_stub.cpp
36+
stubs/randLIB_stub.cpp
3237
stubs/Semaphore_stub.cpp
33-
stubs/CellularUtil_stub.cpp
34-
stubs/equeue_stub.c
38+
stubs/us_ticker_stub.cpp
3539
)

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
* limitations under the License.
1616
*/
1717
#include "gtest/gtest.h"
18+
#include <string.h>
1819
#include "AT_CellularDevice.h"
1920
#include "ATHandler_stub.h"
2021
#include "AT_CellularBase_stub.h"
21-
#include <string.h>
2222

2323
using namespace mbed;
2424
using namespace events;
@@ -270,3 +270,40 @@ TEST_F(TestAT_CellularDevice, test_AT_CellularDevice_init_module)
270270
AT_CellularDevice dev(&fh1);
271271
EXPECT_TRUE(NSAPI_ERROR_OK == dev.init_module());
272272
}
273+
274+
TEST_F(TestAT_CellularDevice, test_AT_CellularDevice_create_delete_context)
275+
{
276+
FileHandle_stub fh1;
277+
AT_CellularDevice *dev = new AT_CellularDevice(&fh1);
278+
279+
CellularContext *ctx = dev->create_context(NULL);
280+
delete dev;
281+
282+
dev = new AT_CellularDevice(&fh1);
283+
ctx = dev->create_context(NULL);
284+
CellularContext *ctx1 = dev->create_context(&fh1);
285+
CellularContext *ctx2 = dev->create_context(&fh1);
286+
287+
EXPECT_TRUE(ctx);
288+
EXPECT_TRUE(ctx1);
289+
EXPECT_TRUE(ctx1 != ctx);
290+
EXPECT_TRUE(ctx1 != ctx2);
291+
292+
CellularContext *xx = dev->get_context_list();
293+
EXPECT_TRUE(xx);
294+
295+
dev->delete_context(ctx);
296+
dev->delete_context(ctx1);
297+
dev->delete_context(NULL);
298+
dev->delete_context(ctx2);
299+
300+
ctx = dev->create_context(NULL);
301+
ctx1 = dev->create_context(&fh1);
302+
ctx2 = dev->create_context(&fh1);
303+
EXPECT_TRUE(ctx);
304+
EXPECT_TRUE(ctx1);
305+
EXPECT_TRUE(ctx1 != ctx);
306+
EXPECT_TRUE(ctx1 != ctx2);
307+
308+
delete dev;
309+
}

UNITTESTS/features/cellular/framework/AT/at_cellulardevice/unittest.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
# Add test specific include paths
77
set(unittest-includes ${unittest-includes}
8-
features/cellular/framework/common/util
8+
../features/cellular/framework/common/util
99
../features/cellular/framework/common
1010
../features/cellular/framework/AT
1111
../features/cellular/framework/device
Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
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+
#include "gtest/gtest.h"
18+
#include <string.h>
19+
20+
#include "CellularDevice.h"
21+
#include "FileHandle_stub.h"
22+
#include "myCellularDevice.h"
23+
#include "CellularStateMachine_stub.h"
24+
#include "CellularSIM.h"
25+
26+
using namespace mbed;
27+
28+
// AStyle ignored as the definition is not clear due to preprocessor usage
29+
// *INDENT-OFF*
30+
class TestCellularDevice : public testing::Test {
31+
protected:
32+
33+
void SetUp()
34+
{
35+
CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_OK;
36+
CellularStateMachine_stub::get_current_target_state = STATE_INIT;
37+
CellularStateMachine_stub::get_current_current_state = STATE_INIT;
38+
CellularStateMachine_stub::bool_value = false;
39+
}
40+
41+
void TearDown()
42+
{
43+
}
44+
};
45+
46+
// *INDENT-ON*
47+
TEST_F(TestCellularDevice, test_create_delete)
48+
{
49+
FileHandle_stub fh1;
50+
51+
CellularDevice *dev = new myCellularDevice(&fh1);
52+
EXPECT_TRUE(dev);
53+
delete dev;
54+
dev = NULL;
55+
56+
CellularDevice *dev1 = CellularDevice::get_default_instance();
57+
EXPECT_TRUE(dev1);
58+
}
59+
60+
TEST_F(TestCellularDevice, test_set_sim_pin)
61+
{
62+
FileHandle_stub fh1;
63+
CellularDevice *dev = new myCellularDevice(&fh1);
64+
EXPECT_TRUE(dev);
65+
66+
// MAX_PIN_SIZE = 8; so let's try a longer one
67+
dev->set_sim_pin("123480375462074360276407");
68+
dev->set_sim_pin(NULL);
69+
dev->set_sim_pin("1234");
70+
delete dev;
71+
}
72+
73+
TEST_F(TestCellularDevice, test_set_plmn)
74+
{
75+
FileHandle_stub fh1;
76+
CellularDevice *dev = new myCellularDevice(&fh1);
77+
EXPECT_TRUE(dev);
78+
79+
80+
// MAX_PLMN_SIZE = 16; so let's try a longer one
81+
dev->set_plmn("123480327465023746502734602736073264076340");
82+
dev->set_plmn("1234");
83+
dev->set_plmn(NULL);
84+
delete dev;
85+
}
86+
87+
TEST_F(TestCellularDevice, test_set_device_ready)
88+
{
89+
FileHandle_stub fh1;
90+
CellularDevice *dev = new myCellularDevice(&fh1);
91+
EXPECT_TRUE(dev);
92+
93+
CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_NO_MEMORY;
94+
ASSERT_EQ(dev->set_device_ready(), NSAPI_ERROR_NO_MEMORY);
95+
96+
CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_OK;
97+
ASSERT_EQ(dev->set_device_ready(), NSAPI_ERROR_OK);
98+
99+
CellularStateMachine_stub::get_current_current_state = STATE_SIM_PIN;
100+
CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_OK;
101+
ASSERT_EQ(dev->set_device_ready(), NSAPI_ERROR_ALREADY);
102+
103+
CellularStateMachine_stub::bool_value = true;
104+
CellularStateMachine_stub::get_current_target_state = STATE_SIM_PIN;
105+
CellularStateMachine_stub::get_current_current_state = STATE_POWER_ON;
106+
ASSERT_EQ(dev->set_device_ready(), NSAPI_ERROR_IN_PROGRESS);
107+
delete dev;
108+
}
109+
110+
TEST_F(TestCellularDevice, test_set_sim_ready)
111+
{
112+
FileHandle_stub fh1;
113+
CellularDevice *dev = new myCellularDevice(&fh1);
114+
EXPECT_TRUE(dev);
115+
116+
CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_NO_MEMORY;
117+
ASSERT_EQ(dev->set_sim_ready(), NSAPI_ERROR_NO_MEMORY);
118+
119+
CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_OK;
120+
ASSERT_EQ(dev->set_sim_ready(), NSAPI_ERROR_OK);
121+
122+
CellularStateMachine_stub::get_current_current_state = STATE_MANUAL_REGISTERING_NETWORK;
123+
CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_OK;
124+
ASSERT_EQ(dev->set_sim_ready(), NSAPI_ERROR_ALREADY);
125+
126+
CellularStateMachine_stub::bool_value = true;
127+
CellularStateMachine_stub::get_current_target_state = STATE_MANUAL_REGISTERING_NETWORK;
128+
CellularStateMachine_stub::get_current_current_state = STATE_POWER_ON;
129+
ASSERT_EQ(dev->set_sim_ready(), NSAPI_ERROR_IN_PROGRESS);
130+
delete dev;
131+
}
132+
133+
TEST_F(TestCellularDevice, test_register_to_network)
134+
{
135+
FileHandle_stub fh1;
136+
CellularDevice *dev = new myCellularDevice(&fh1);
137+
EXPECT_TRUE(dev);
138+
139+
CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_NO_MEMORY;
140+
ASSERT_EQ(dev->register_to_network(), NSAPI_ERROR_NO_MEMORY);
141+
142+
CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_OK;
143+
ASSERT_EQ(dev->register_to_network(), NSAPI_ERROR_OK);
144+
145+
CellularStateMachine_stub::get_current_current_state = STATE_ATTACHING_NETWORK;
146+
CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_OK;
147+
ASSERT_EQ(dev->register_to_network(), NSAPI_ERROR_ALREADY);
148+
149+
CellularStateMachine_stub::bool_value = true;
150+
CellularStateMachine_stub::get_current_target_state = STATE_ATTACHING_NETWORK;
151+
CellularStateMachine_stub::get_current_current_state = STATE_POWER_ON;
152+
ASSERT_EQ(dev->register_to_network(), NSAPI_ERROR_IN_PROGRESS);
153+
delete dev;
154+
}
155+
156+
TEST_F(TestCellularDevice, test_attach_to_network)
157+
{
158+
FileHandle_stub fh1;
159+
CellularDevice *dev = new myCellularDevice(&fh1);
160+
EXPECT_TRUE(dev);
161+
162+
CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_NO_MEMORY;
163+
ASSERT_EQ(dev->attach_to_network(), NSAPI_ERROR_NO_MEMORY);
164+
165+
CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_OK;
166+
ASSERT_EQ(dev->attach_to_network(), NSAPI_ERROR_OK);
167+
168+
CellularStateMachine_stub::get_current_current_state = STATE_ATTACHING_NETWORK;
169+
CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_OK;
170+
ASSERT_EQ(dev->attach_to_network(), NSAPI_ERROR_ALREADY);
171+
172+
CellularStateMachine_stub::bool_value = true;
173+
CellularStateMachine_stub::get_current_target_state = STATE_ATTACHING_NETWORK;
174+
CellularStateMachine_stub::get_current_current_state = STATE_POWER_ON;
175+
ASSERT_EQ(dev->attach_to_network(), NSAPI_ERROR_IN_PROGRESS);
176+
delete dev;
177+
}
178+
179+
TEST_F(TestCellularDevice, test_get_context_list)
180+
{
181+
FileHandle_stub fh1;
182+
183+
CellularDevice *dev = new myCellularDevice(&fh1);
184+
EXPECT_TRUE(dev);
185+
CellularContext *ctx = dev->create_context();
186+
EXPECT_TRUE(dev->get_context_list());
187+
delete dev;
188+
dev = NULL;
189+
190+
dev = new myCellularDevice(&fh1);
191+
EXPECT_TRUE(dev);
192+
EXPECT_FALSE(dev->get_context_list());
193+
}
194+
195+
TEST_F(TestCellularDevice, test_stop)
196+
{
197+
FileHandle_stub fh1;
198+
CellularDevice *dev = new myCellularDevice(&fh1);
199+
EXPECT_TRUE(dev);
200+
201+
CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_OK;
202+
ASSERT_EQ(dev->attach_to_network(), NSAPI_ERROR_OK);
203+
204+
dev->stop();
205+
delete dev;
206+
}
207+
208+
TEST_F(TestCellularDevice, test_cellular_callback)
209+
{
210+
FileHandle_stub fh1;
211+
myCellularDevice *dev = new myCellularDevice(&fh1);
212+
EXPECT_TRUE(dev);
213+
214+
ASSERT_EQ(dev->attach_to_network(), NSAPI_ERROR_OK);
215+
216+
cell_callback_data_t data;
217+
dev->cellular_callback((nsapi_event_t)CellularRegistrationStatusChanged, (intptr_t)&data);
218+
219+
data.error = NSAPI_ERROR_OK;
220+
dev->set_plmn("1234");
221+
dev->cellular_callback((nsapi_event_t)CellularDeviceReady, (intptr_t)&data);
222+
223+
dev->set_sim_pin("1234");
224+
data.status_data = CellularSIM::SimStatePinNeeded;
225+
dev->cellular_callback((nsapi_event_t)CellularSIMStatusChanged, (intptr_t)&data);
226+
227+
CellularContext *ctx = dev->create_context();
228+
dev->cellular_callback(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, NSAPI_STATUS_DISCONNECTED);
229+
230+
delete dev;
231+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
####################
3+
# UNIT TESTS
4+
####################
5+
6+
# Add test specific include paths
7+
set(unittest-includes ${unittest-includes}
8+
/features/cellular/framework/device/cellulardevice
9+
../features/cellular/framework/device
10+
../features/cellular/framework/common
11+
)
12+
13+
# Source files
14+
set(unittest-sources
15+
../features/cellular/framework/device/CellularDevice.cpp
16+
)
17+
18+
# Test files
19+
set(unittest-test-sources
20+
features/cellular/framework/device/cellulardevice/cellulardevicetest.cpp
21+
stubs/FileHandle_stub.cpp
22+
stubs/CellularStateMachine_stub.cpp
23+
stubs/EventQueue_stub.cpp
24+
stubs/mbed_assert_stub.c
25+
stubs/UARTSerial_stub.cpp
26+
stubs/SerialBase_stub.cpp
27+
stubs/ATHandler_stub.cpp
28+
stubs/AT_CellularNetwork_stub.cpp
29+
stubs/AT_CellularBase_stub.cpp
30+
stubs/AT_CellularContext_stub.cpp
31+
stubs/Semaphore_stub.cpp
32+
stubs/NetworkInterface_stub.cpp
33+
)
34+
35+
# defines
36+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DMDMRTS=PTC0 -DMDMCTS=PTC1 -DMDMTXD=NC -DMDMRXD=NC -DMBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE=115200 -DCELLULAR_DEVICE=myCellularDevice -DDEVICE_SERIAL_FC=1")
37+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DMDMRTS=PTC0 -DMDMCTS=PTC1 -DMDMTXD=NC -DMDMRXD=NC -DMBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE=115200 -DCELLULAR_DEVICE=myCellularDevice -DDEVICE_SERIAL_FC=1")

UNITTESTS/stubs/ATHandler_stub.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ ssize_t ATHandler::read_string(char *buf, size_t size, bool read_even_stop_tag)
170170
if (ATHandler_stub::read_string_index == kRead_string_table_size) {
171171
if (ATHandler_stub::read_string_value && ATHandler_stub::ssize_value >= 0) {
172172
memcpy(buf, ATHandler_stub::read_string_value, ATHandler_stub::ssize_value + 1);
173+
buf[ATHandler_stub::ssize_value] = '\0';
173174
}
174175
return ATHandler_stub::ssize_value;
175176
}
@@ -179,6 +180,7 @@ ssize_t ATHandler::read_string(char *buf, size_t size, bool read_even_stop_tag)
179180
const char *tmp = ATHandler_stub::read_string_table[ATHandler_stub::read_string_index];
180181
ssize_t len = strlen(tmp);
181182
memcpy(buf, tmp, len + 1);
183+
buf[len] = '\0';
182184
return len;
183185
}
184186

0 commit comments

Comments
 (0)