Skip to content

Improve cellular unittests #11495

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Sep 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ class my_base : public AT_CellularBase {
{
return get_property(PROPERTY_AT_CGDATA);
}

void reset_property_array()
{
_property_array = NULL;
}
};

// AStyle ignored as the definition is not clear due to preprocessor usage
Expand Down Expand Up @@ -137,3 +142,18 @@ TEST_F(TestAT_CellularBase, test_AT_CellularBase_is_supported)
EXPECT_EQ(true, my_at.check_supported());
EXPECT_EQ(false, my_at.check_not_supported());
}

TEST_F(TestAT_CellularBase, test_invalid_params)
{
EventQueue eq;
FileHandle_stub fh;
ATHandler ah(&fh, eq, 0, ",");
my_base my_at(ah);

my_at.reset_property_array(); // as array is a static variable, it might have been set in previous tests

my_at.set_cellular_properties(NULL);

// Property array not set
EXPECT_EQ(0, my_at.get_property(AT_CellularBase::PROPERTY_IPV4_PDP_TYPE));
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,89 @@ TEST_F(TestAT_CellularNetwork, Create)
delete cn;
}

int expected_rat = 0;
int expected_status = 0;
int expected_cellid = 0;

void status_cb_urc(nsapi_event_t ev, intptr_t ptr)
{
const cell_callback_data_t *data = (const cell_callback_data_t *)ptr;
switch (ev) {
case CellularRadioAccessTechnologyChanged:
EXPECT_EQ(NSAPI_ERROR_OK, data->error);
EXPECT_EQ(expected_rat, data->status_data);
break;
case CellularRegistrationStatusChanged:
EXPECT_EQ(NSAPI_ERROR_OK, data->error);
EXPECT_EQ(expected_status, data->status_data);
break;
case CellularCellIDChanged:
EXPECT_EQ(NSAPI_ERROR_OK, data->error);
EXPECT_EQ(expected_cellid, data->status_data);
break;
default:
if (ev == NSAPI_EVENT_CONNECTION_STATUS_CHANGE) {
EXPECT_EQ(NSAPI_STATUS_DISCONNECTED, (int)ptr);
} else {
FAIL();
}
}
}

TEST_F(TestAT_CellularNetwork, test_urc_creg)
{
EventQueue que;
FileHandle_stub fh1;
ATHandler at(&fh1, que, 0, ",");

AT_CellularNetwork cn(at);
cn.attach(status_cb_urc);

EXPECT_STREQ("+CEREG:", ATHandler_stub::urc_handlers[0].urc);
EXPECT_STREQ("+CREG:", ATHandler_stub::urc_handlers[1].urc);

// Connected to home network
expected_rat = CellularNetwork::RAT_NB1;
expected_status = CellularNetwork::RegisteredHomeNetwork;
expected_cellid = 305463233;

ATHandler_stub::int_count = 4;
ATHandler_stub::int_valid_count_table[3] = 1; // [1] STAT, Registered to home network
ATHandler_stub::int_valid_count_table[2] = 9; // [4] ACT, NB-IoT
ATHandler_stub::int_valid_count_table[1] = 1; // [5] cause_type, skipped
ATHandler_stub::int_valid_count_table[0] = 1; // [6] reject_cause, skipped

ATHandler_stub::read_string_index = 4;
ATHandler_stub::read_string_table[3] = "00C3"; // [2] LAC, 195
ATHandler_stub::read_string_table[2] = "1234FFC1"; // [3] ci, 305463233
ATHandler_stub::read_string_table[1] = "00100100"; // [7] active time
ATHandler_stub::read_string_table[0] = "01000111"; // [8] periodic-tau

ATHandler_stub::urc_handlers[0].cb();

// Disconnected
expected_rat = CellularNetwork::RAT_NB1;
expected_status = CellularNetwork::NotRegistered;
expected_cellid = 0;

ATHandler_stub::int_count = 4;
ATHandler_stub::int_valid_count_table[3] = 0; // [1] STAT, Not reqistered
ATHandler_stub::int_valid_count_table[2] = 9; // [4] ACT, NB-IoT
ATHandler_stub::int_valid_count_table[1] = 1; // [5] cause_type, skipped
ATHandler_stub::int_valid_count_table[0] = 1; // [6] reject_cause, skipped

ATHandler_stub::read_string_index = 4;
ATHandler_stub::read_string_table[3] = "0000"; // [2] LAC, 0000
ATHandler_stub::read_string_table[2] = "00000000"; // [3] ci, 000000000
ATHandler_stub::read_string_table[1] = "00100100"; // [7] active time
ATHandler_stub::read_string_table[0] = "01000111"; // [8] periodic-tau

ATHandler_stub::urc_handlers[0].cb();
ATHandler_stub::read_string_index = kRead_string_table_size;
ATHandler_stub::read_string_value = NULL;
ATHandler_stub::ssize_value = 0;
}

TEST_F(TestAT_CellularNetwork, test_AT_CellularNetwork_set_registration)
{
EventQueue que;
Expand Down Expand Up @@ -684,3 +767,43 @@ TEST_F(TestAT_CellularNetwork, test_AT_CellularNetwork_set_packet_domain_event_r
EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == cn.set_packet_domain_event_reporting(true));
EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == cn.set_packet_domain_event_reporting(false));
}

TEST_F(TestAT_CellularNetwork, test_AT_CellularNetwork_is_active_context)
{
EventQueue que;
FileHandle_stub fh1;
ATHandler at(&fh1, que, 0, ",");

AT_CellularNetwork cn(at);

// No contexts
int active_contexts = -1;
EXPECT_FALSE(cn.is_active_context(&active_contexts));
EXPECT_EQ(0, active_contexts);

// Active contexts
ATHandler_stub::resp_info_true_counter = 2;
ATHandler_stub::int_count = 4;
ATHandler_stub::int_valid_count_table[3] = 0; // ctx 0
ATHandler_stub::int_valid_count_table[2] = 0; // ctx 0 inactive
ATHandler_stub::int_valid_count_table[1] = 1; // ctx 1
ATHandler_stub::int_valid_count_table[0] = 1; // ctx 1 active

EXPECT_TRUE(cn.is_active_context(&active_contexts));
EXPECT_EQ(1, active_contexts);

ATHandler_stub::resp_info_true_counter = 2;
ATHandler_stub::int_count = 4;
EXPECT_FALSE(cn.is_active_context(&active_contexts, 0));
EXPECT_EQ(1, active_contexts);

ATHandler_stub::resp_info_true_counter = 2;
ATHandler_stub::int_count = 4;
EXPECT_TRUE(cn.is_active_context(&active_contexts, 1));
EXPECT_EQ(1, active_contexts);

ATHandler_stub::resp_info_true_counter = 2;
ATHandler_stub::int_count = 4;
EXPECT_TRUE(cn.is_active_context(NULL, 1));
EXPECT_EQ(1, active_contexts);
}
90 changes: 90 additions & 0 deletions UNITTESTS/features/cellular/framework/common/list/listtest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright (c) 2018, Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "gtest/gtest.h"
#include "CellularList.h"

using namespace mbed;

// AStyle ignored as the definition is not clear due to preprocessor usage
// *INDENT-OFF*
class Testlist : public testing::Test {
protected:

void SetUp()
{
}

void TearDown()
{
}
};
// *INDENT-ON*

struct entry_t {
int i;
entry_t *next;
};

TEST_F(Testlist, test_list_int)
{
CellularList<entry_t> list;
EXPECT_TRUE(NULL == list.get_head());

entry_t *first = list.add_new();
first->i = 1;
EXPECT_TRUE(NULL != first);

entry_t *second = list.add_new();
first->i = 2;
EXPECT_TRUE(NULL != second);

entry_t *third = list.add_new();
first->i = 3;
EXPECT_TRUE(NULL != third);

EXPECT_EQ(3, list.get_head()->i);

list.delete_last(); // Deletes first
EXPECT_EQ(3, list.get_head()->i);

list.delete_all();
EXPECT_TRUE(NULL == list.get_head());
}

TEST_F(Testlist, delete_last_until_empty)
{
CellularList<entry_t> list;
list.add_new();
list.add_new();
list.delete_last();
list.delete_last();
EXPECT_TRUE(NULL == list.get_head());
}

TEST_F(Testlist, empty_list_delete_last)
{
CellularList<entry_t> list;
list.delete_last();
EXPECT_TRUE(NULL == list.get_head());
}

TEST_F(Testlist, empty_list_delete_all)
{
CellularList<entry_t> list;
list.delete_all();
EXPECT_TRUE(NULL == list.get_head());
}
19 changes: 19 additions & 0 deletions UNITTESTS/features/cellular/framework/common/list/unittest.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

####################
# UNIT TESTS
####################

# Add test specific include paths
set(unittest-includes ${unittest-includes}
features/cellular/framework/common/util
../features/cellular/framework/common
)

# Source files
set(unittest-sources
)

# Test files
set(unittest-test-sources
features/cellular/framework/common/list/listtest.cpp
)
41 changes: 41 additions & 0 deletions UNITTESTS/features/cellular/framework/common/util/utiltest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,47 @@ TEST_F(Testutil, test_util_binary_str_to_uint)
EXPECT_TRUE(0 == binary_str_to_uint(binary_str, 0));
}

TEST_F(Testutil, hex_to_char)
{
char output;

// 0
hex_to_char("00", output);
EXPECT_EQ((char)0x00, output);

// <128
hex_to_char("10", output);
EXPECT_EQ((char)0x10, output);

// =128
hex_to_char("80", output);
EXPECT_EQ((char)0x80, output);

// >128
hex_to_char("FF", output);
EXPECT_EQ((char)0xFF, output);

// Null -> output is not modified
hex_to_char(NULL, output);
EXPECT_EQ((char)0xFF, output);
}

TEST_F(Testutil, hex_str_to_char_str)
{
char input[] = "0165AABBCC";
char output[32];
EXPECT_EQ(5, hex_str_to_char_str(input, strlen(input), output));
EXPECT_EQ((char)0x01, output[0]);
EXPECT_EQ((char)0x65, output[1]);
EXPECT_EQ((char)0xAA, output[2]);
EXPECT_EQ((char)0xBB, output[3]);
EXPECT_EQ((char)0xCC, output[4]);

// NULL params
EXPECT_EQ(0, hex_str_to_char_str(NULL, 2, output));
EXPECT_EQ(0, hex_str_to_char_str(input, strlen(input), NULL));
}

TEST_F(Testutil, test_util_uint_to_binary_string)
{
char str[33];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,35 @@ TEST_F(TestCellularDevice, test_shutdown)

delete dev;
}

TEST_F(TestCellularDevice, test_timeout_array)
{
FileHandle_stub fh1;
myCellularDevice *dev = new myCellularDevice(&fh1);
EXPECT_TRUE(dev);

CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_OK;

// Max size
uint16_t set_timeouts[CELLULAR_RETRY_ARRAY_SIZE + 1];
for (int i = 0; i < CELLULAR_RETRY_ARRAY_SIZE; i++) {
set_timeouts[i] = i + 100;
}
dev->set_retry_timeout_array(set_timeouts, CELLULAR_RETRY_ARRAY_SIZE);

uint16_t verify_timeouts[CELLULAR_RETRY_ARRAY_SIZE + 1];
for (int i = 0; i < CELLULAR_RETRY_ARRAY_SIZE; i++) {
verify_timeouts[i] = i + 100;
}
dev->verify_timeout_array(verify_timeouts, CELLULAR_RETRY_ARRAY_SIZE);

// Empty
dev->set_retry_timeout_array(NULL, 0);
dev->verify_timeout_array(NULL, 0);

// Oversize (returns only CELLULAR_RETRY_ARRAY_SIZE)
dev->set_retry_timeout_array(set_timeouts, CELLULAR_RETRY_ARRAY_SIZE + 1);
dev->verify_timeout_array(verify_timeouts, CELLULAR_RETRY_ARRAY_SIZE);

delete dev;
}
6 changes: 6 additions & 0 deletions UNITTESTS/stubs/ATHandler_stub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ int ATHandler_stub::int_valid_count_table[kRead_int_table_size];
int ATHandler_stub::int_count = kRead_int_table_size;
bool ATHandler_stub::process_oob_urc = false;

std::vector<ATHandler_stub::urc_handler> ATHandler_stub::urc_handlers;

int ATHandler_stub::read_string_index = kRead_string_table_size;
const char *ATHandler_stub::read_string_table[kRead_string_table_size];
int ATHandler_stub::resp_stop_success_count = kResp_stop_count_default;
Expand Down Expand Up @@ -111,6 +113,7 @@ bool ATHandler::get_debug() const

ATHandler::~ATHandler()
{
ATHandler_stub::urc_handlers.clear();
}

void ATHandler::inc_ref_count()
Expand Down Expand Up @@ -149,6 +152,9 @@ void ATHandler::set_urc_handler(const char *urc, mbed::Callback<void()> cb)
return;
}

ATHandler_stub::urc_handler handler = { .urc = urc, .cb = cb };
ATHandler_stub::urc_handlers.push_back(handler);

if (ATHandler_stub::call_immediately) {
cb();
}
Expand Down
Loading