Skip to content

Commit d91ed5f

Browse files
authored
Merge pull request #11495 from kivaisan/improve_cellular_ut
Improve cellular unittests
2 parents 07ebd92 + da77cdc commit d91ed5f

File tree

13 files changed

+387
-17
lines changed

13 files changed

+387
-17
lines changed

UNITTESTS/features/cellular/framework/AT/at_cellularbase/at_cellularbasetest.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ class my_base : public AT_CellularBase {
5353
{
5454
return get_property(PROPERTY_AT_CGDATA);
5555
}
56+
57+
void reset_property_array()
58+
{
59+
_property_array = NULL;
60+
}
5661
};
5762

5863
// AStyle ignored as the definition is not clear due to preprocessor usage
@@ -137,3 +142,18 @@ TEST_F(TestAT_CellularBase, test_AT_CellularBase_is_supported)
137142
EXPECT_EQ(true, my_at.check_supported());
138143
EXPECT_EQ(false, my_at.check_not_supported());
139144
}
145+
146+
TEST_F(TestAT_CellularBase, test_invalid_params)
147+
{
148+
EventQueue eq;
149+
FileHandle_stub fh;
150+
ATHandler ah(&fh, eq, 0, ",");
151+
my_base my_at(ah);
152+
153+
my_at.reset_property_array(); // as array is a static variable, it might have been set in previous tests
154+
155+
my_at.set_cellular_properties(NULL);
156+
157+
// Property array not set
158+
EXPECT_EQ(0, my_at.get_property(AT_CellularBase::PROPERTY_IPV4_PDP_TYPE));
159+
}

UNITTESTS/features/cellular/framework/AT/at_cellularnetwork/at_cellularnetworktest.cpp

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,89 @@ TEST_F(TestAT_CellularNetwork, Create)
8585
delete cn;
8686
}
8787

88+
int expected_rat = 0;
89+
int expected_status = 0;
90+
int expected_cellid = 0;
91+
92+
void status_cb_urc(nsapi_event_t ev, intptr_t ptr)
93+
{
94+
const cell_callback_data_t *data = (const cell_callback_data_t *)ptr;
95+
switch (ev) {
96+
case CellularRadioAccessTechnologyChanged:
97+
EXPECT_EQ(NSAPI_ERROR_OK, data->error);
98+
EXPECT_EQ(expected_rat, data->status_data);
99+
break;
100+
case CellularRegistrationStatusChanged:
101+
EXPECT_EQ(NSAPI_ERROR_OK, data->error);
102+
EXPECT_EQ(expected_status, data->status_data);
103+
break;
104+
case CellularCellIDChanged:
105+
EXPECT_EQ(NSAPI_ERROR_OK, data->error);
106+
EXPECT_EQ(expected_cellid, data->status_data);
107+
break;
108+
default:
109+
if (ev == NSAPI_EVENT_CONNECTION_STATUS_CHANGE) {
110+
EXPECT_EQ(NSAPI_STATUS_DISCONNECTED, (int)ptr);
111+
} else {
112+
FAIL();
113+
}
114+
}
115+
}
116+
117+
TEST_F(TestAT_CellularNetwork, test_urc_creg)
118+
{
119+
EventQueue que;
120+
FileHandle_stub fh1;
121+
ATHandler at(&fh1, que, 0, ",");
122+
123+
AT_CellularNetwork cn(at);
124+
cn.attach(status_cb_urc);
125+
126+
EXPECT_STREQ("+CEREG:", ATHandler_stub::urc_handlers[0].urc);
127+
EXPECT_STREQ("+CREG:", ATHandler_stub::urc_handlers[1].urc);
128+
129+
// Connected to home network
130+
expected_rat = CellularNetwork::RAT_NB1;
131+
expected_status = CellularNetwork::RegisteredHomeNetwork;
132+
expected_cellid = 305463233;
133+
134+
ATHandler_stub::int_count = 4;
135+
ATHandler_stub::int_valid_count_table[3] = 1; // [1] STAT, Registered to home network
136+
ATHandler_stub::int_valid_count_table[2] = 9; // [4] ACT, NB-IoT
137+
ATHandler_stub::int_valid_count_table[1] = 1; // [5] cause_type, skipped
138+
ATHandler_stub::int_valid_count_table[0] = 1; // [6] reject_cause, skipped
139+
140+
ATHandler_stub::read_string_index = 4;
141+
ATHandler_stub::read_string_table[3] = "00C3"; // [2] LAC, 195
142+
ATHandler_stub::read_string_table[2] = "1234FFC1"; // [3] ci, 305463233
143+
ATHandler_stub::read_string_table[1] = "00100100"; // [7] active time
144+
ATHandler_stub::read_string_table[0] = "01000111"; // [8] periodic-tau
145+
146+
ATHandler_stub::urc_handlers[0].cb();
147+
148+
// Disconnected
149+
expected_rat = CellularNetwork::RAT_NB1;
150+
expected_status = CellularNetwork::NotRegistered;
151+
expected_cellid = 0;
152+
153+
ATHandler_stub::int_count = 4;
154+
ATHandler_stub::int_valid_count_table[3] = 0; // [1] STAT, Not reqistered
155+
ATHandler_stub::int_valid_count_table[2] = 9; // [4] ACT, NB-IoT
156+
ATHandler_stub::int_valid_count_table[1] = 1; // [5] cause_type, skipped
157+
ATHandler_stub::int_valid_count_table[0] = 1; // [6] reject_cause, skipped
158+
159+
ATHandler_stub::read_string_index = 4;
160+
ATHandler_stub::read_string_table[3] = "0000"; // [2] LAC, 0000
161+
ATHandler_stub::read_string_table[2] = "00000000"; // [3] ci, 000000000
162+
ATHandler_stub::read_string_table[1] = "00100100"; // [7] active time
163+
ATHandler_stub::read_string_table[0] = "01000111"; // [8] periodic-tau
164+
165+
ATHandler_stub::urc_handlers[0].cb();
166+
ATHandler_stub::read_string_index = kRead_string_table_size;
167+
ATHandler_stub::read_string_value = NULL;
168+
ATHandler_stub::ssize_value = 0;
169+
}
170+
88171
TEST_F(TestAT_CellularNetwork, test_AT_CellularNetwork_set_registration)
89172
{
90173
EventQueue que;
@@ -684,3 +767,43 @@ TEST_F(TestAT_CellularNetwork, test_AT_CellularNetwork_set_packet_domain_event_r
684767
EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == cn.set_packet_domain_event_reporting(true));
685768
EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == cn.set_packet_domain_event_reporting(false));
686769
}
770+
771+
TEST_F(TestAT_CellularNetwork, test_AT_CellularNetwork_is_active_context)
772+
{
773+
EventQueue que;
774+
FileHandle_stub fh1;
775+
ATHandler at(&fh1, que, 0, ",");
776+
777+
AT_CellularNetwork cn(at);
778+
779+
// No contexts
780+
int active_contexts = -1;
781+
EXPECT_FALSE(cn.is_active_context(&active_contexts));
782+
EXPECT_EQ(0, active_contexts);
783+
784+
// Active contexts
785+
ATHandler_stub::resp_info_true_counter = 2;
786+
ATHandler_stub::int_count = 4;
787+
ATHandler_stub::int_valid_count_table[3] = 0; // ctx 0
788+
ATHandler_stub::int_valid_count_table[2] = 0; // ctx 0 inactive
789+
ATHandler_stub::int_valid_count_table[1] = 1; // ctx 1
790+
ATHandler_stub::int_valid_count_table[0] = 1; // ctx 1 active
791+
792+
EXPECT_TRUE(cn.is_active_context(&active_contexts));
793+
EXPECT_EQ(1, active_contexts);
794+
795+
ATHandler_stub::resp_info_true_counter = 2;
796+
ATHandler_stub::int_count = 4;
797+
EXPECT_FALSE(cn.is_active_context(&active_contexts, 0));
798+
EXPECT_EQ(1, active_contexts);
799+
800+
ATHandler_stub::resp_info_true_counter = 2;
801+
ATHandler_stub::int_count = 4;
802+
EXPECT_TRUE(cn.is_active_context(&active_contexts, 1));
803+
EXPECT_EQ(1, active_contexts);
804+
805+
ATHandler_stub::resp_info_true_counter = 2;
806+
ATHandler_stub::int_count = 4;
807+
EXPECT_TRUE(cn.is_active_context(NULL, 1));
808+
EXPECT_EQ(1, active_contexts);
809+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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 "CellularList.h"
19+
20+
using namespace mbed;
21+
22+
// AStyle ignored as the definition is not clear due to preprocessor usage
23+
// *INDENT-OFF*
24+
class Testlist : public testing::Test {
25+
protected:
26+
27+
void SetUp()
28+
{
29+
}
30+
31+
void TearDown()
32+
{
33+
}
34+
};
35+
// *INDENT-ON*
36+
37+
struct entry_t {
38+
int i;
39+
entry_t *next;
40+
};
41+
42+
TEST_F(Testlist, test_list_int)
43+
{
44+
CellularList<entry_t> list;
45+
EXPECT_TRUE(NULL == list.get_head());
46+
47+
entry_t *first = list.add_new();
48+
first->i = 1;
49+
EXPECT_TRUE(NULL != first);
50+
51+
entry_t *second = list.add_new();
52+
first->i = 2;
53+
EXPECT_TRUE(NULL != second);
54+
55+
entry_t *third = list.add_new();
56+
first->i = 3;
57+
EXPECT_TRUE(NULL != third);
58+
59+
EXPECT_EQ(3, list.get_head()->i);
60+
61+
list.delete_last(); // Deletes first
62+
EXPECT_EQ(3, list.get_head()->i);
63+
64+
list.delete_all();
65+
EXPECT_TRUE(NULL == list.get_head());
66+
}
67+
68+
TEST_F(Testlist, delete_last_until_empty)
69+
{
70+
CellularList<entry_t> list;
71+
list.add_new();
72+
list.add_new();
73+
list.delete_last();
74+
list.delete_last();
75+
EXPECT_TRUE(NULL == list.get_head());
76+
}
77+
78+
TEST_F(Testlist, empty_list_delete_last)
79+
{
80+
CellularList<entry_t> list;
81+
list.delete_last();
82+
EXPECT_TRUE(NULL == list.get_head());
83+
}
84+
85+
TEST_F(Testlist, empty_list_delete_all)
86+
{
87+
CellularList<entry_t> list;
88+
list.delete_all();
89+
EXPECT_TRUE(NULL == list.get_head());
90+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
####################
3+
# UNIT TESTS
4+
####################
5+
6+
# Add test specific include paths
7+
set(unittest-includes ${unittest-includes}
8+
features/cellular/framework/common/util
9+
../features/cellular/framework/common
10+
)
11+
12+
# Source files
13+
set(unittest-sources
14+
)
15+
16+
# Test files
17+
set(unittest-test-sources
18+
features/cellular/framework/common/list/listtest.cpp
19+
)

UNITTESTS/features/cellular/framework/common/util/utiltest.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,47 @@ TEST_F(Testutil, test_util_binary_str_to_uint)
5353
EXPECT_TRUE(0 == binary_str_to_uint(binary_str, 0));
5454
}
5555

56+
TEST_F(Testutil, hex_to_char)
57+
{
58+
char output;
59+
60+
// 0
61+
hex_to_char("00", output);
62+
EXPECT_EQ((char)0x00, output);
63+
64+
// <128
65+
hex_to_char("10", output);
66+
EXPECT_EQ((char)0x10, output);
67+
68+
// =128
69+
hex_to_char("80", output);
70+
EXPECT_EQ((char)0x80, output);
71+
72+
// >128
73+
hex_to_char("FF", output);
74+
EXPECT_EQ((char)0xFF, output);
75+
76+
// Null -> output is not modified
77+
hex_to_char(NULL, output);
78+
EXPECT_EQ((char)0xFF, output);
79+
}
80+
81+
TEST_F(Testutil, hex_str_to_char_str)
82+
{
83+
char input[] = "0165AABBCC";
84+
char output[32];
85+
EXPECT_EQ(5, hex_str_to_char_str(input, strlen(input), output));
86+
EXPECT_EQ((char)0x01, output[0]);
87+
EXPECT_EQ((char)0x65, output[1]);
88+
EXPECT_EQ((char)0xAA, output[2]);
89+
EXPECT_EQ((char)0xBB, output[3]);
90+
EXPECT_EQ((char)0xCC, output[4]);
91+
92+
// NULL params
93+
EXPECT_EQ(0, hex_str_to_char_str(NULL, 2, output));
94+
EXPECT_EQ(0, hex_str_to_char_str(input, strlen(input), NULL));
95+
}
96+
5697
TEST_F(Testutil, test_util_uint_to_binary_string)
5798
{
5899
char str[33];

UNITTESTS/features/cellular/framework/device/cellulardevice/cellulardevicetest.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,35 @@ TEST_F(TestCellularDevice, test_shutdown)
240240

241241
delete dev;
242242
}
243+
244+
TEST_F(TestCellularDevice, test_timeout_array)
245+
{
246+
FileHandle_stub fh1;
247+
myCellularDevice *dev = new myCellularDevice(&fh1);
248+
EXPECT_TRUE(dev);
249+
250+
CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_OK;
251+
252+
// Max size
253+
uint16_t set_timeouts[CELLULAR_RETRY_ARRAY_SIZE + 1];
254+
for (int i = 0; i < CELLULAR_RETRY_ARRAY_SIZE; i++) {
255+
set_timeouts[i] = i + 100;
256+
}
257+
dev->set_retry_timeout_array(set_timeouts, CELLULAR_RETRY_ARRAY_SIZE);
258+
259+
uint16_t verify_timeouts[CELLULAR_RETRY_ARRAY_SIZE + 1];
260+
for (int i = 0; i < CELLULAR_RETRY_ARRAY_SIZE; i++) {
261+
verify_timeouts[i] = i + 100;
262+
}
263+
dev->verify_timeout_array(verify_timeouts, CELLULAR_RETRY_ARRAY_SIZE);
264+
265+
// Empty
266+
dev->set_retry_timeout_array(NULL, 0);
267+
dev->verify_timeout_array(NULL, 0);
268+
269+
// Oversize (returns only CELLULAR_RETRY_ARRAY_SIZE)
270+
dev->set_retry_timeout_array(set_timeouts, CELLULAR_RETRY_ARRAY_SIZE + 1);
271+
dev->verify_timeout_array(verify_timeouts, CELLULAR_RETRY_ARRAY_SIZE);
272+
273+
delete dev;
274+
}

UNITTESTS/stubs/ATHandler_stub.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ int ATHandler_stub::int_valid_count_table[kRead_int_table_size];
5454
int ATHandler_stub::int_count = kRead_int_table_size;
5555
bool ATHandler_stub::process_oob_urc = false;
5656

57+
std::vector<ATHandler_stub::urc_handler> ATHandler_stub::urc_handlers;
58+
5759
int ATHandler_stub::read_string_index = kRead_string_table_size;
5860
const char *ATHandler_stub::read_string_table[kRead_string_table_size];
5961
int ATHandler_stub::resp_stop_success_count = kResp_stop_count_default;
@@ -111,6 +113,7 @@ bool ATHandler::get_debug() const
111113

112114
ATHandler::~ATHandler()
113115
{
116+
ATHandler_stub::urc_handlers.clear();
114117
}
115118

116119
void ATHandler::inc_ref_count()
@@ -149,6 +152,9 @@ void ATHandler::set_urc_handler(const char *urc, mbed::Callback<void()> cb)
149152
return;
150153
}
151154

155+
ATHandler_stub::urc_handler handler = { .urc = urc, .cb = cb };
156+
ATHandler_stub::urc_handlers.push_back(handler);
157+
152158
if (ATHandler_stub::call_immediately) {
153159
cb();
154160
}

0 commit comments

Comments
 (0)