<commit_before>/*
 * 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 <string.h>
#include "AT_CellularNetwork.h"
#include "events/EventQueue.h"
#include "ATHandler.h"
#include "AT_CellularDevice.h"
#include "FileHandle_stub.h"
#include "CellularLog.h"
#include "ATHandler_stub.h"
#include "AT_CellularStack.h"

using namespace mbed;
using namespace events;

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

    void SetUp()
    {
        ATHandler_stub::int_count = kRead_int_table_size;
        ATHandler_stub::read_string_index = kRead_string_table_size;
        ATHandler_stub::resp_stop_success_count = kResp_stop_count_default;
        ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK;
        ATHandler_stub::int_value = -1;
    }

    void TearDown()
    {
    }
};


class my_AT_CN : public AT_CellularNetwork {
public:
    my_AT_CN(ATHandler &atHandler) : AT_CellularNetwork(atHandler) {}
    virtual ~my_AT_CN() {}
    virtual nsapi_error_t set_access_technology_impl(RadioAccessTechnology op_rat)
    {
        return NSAPI_ERROR_OK;
    }
};

class my_AT_CNipv6 : public AT_CellularNetwork {
public:
    my_AT_CNipv6(ATHandler &atHandler) : AT_CellularNetwork(atHandler) {}
    virtual ~my_AT_CNipv6() {}
    virtual nsapi_error_t set_access_technology_impl(RadioAccessTechnology op_rat)
    {
        return NSAPI_ERROR_OK;
    }
};

static int network_cb_count;
static void network_cb(nsapi_event_t ev, intptr_t intptr)
{
    network_cb_count++;
}

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

    AT_CellularNetwork *cn = new AT_CellularNetwork(at);
    EXPECT_TRUE(cn != NULL);
    delete cn;
}

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

    AT_CellularNetwork cn(at);

    ATHandler_stub::nsapi_error_value = NSAPI_ERROR_DEVICE_ERROR;
    EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == cn.set_registration());

    ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK;
    EXPECT_TRUE(NSAPI_ERROR_OK == cn.set_registration());

    ATHandler_stub::nsapi_error_value = NSAPI_ERROR_DEVICE_ERROR;
    EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == cn.set_registration("12345"));

    ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK;
    EXPECT_TRUE(NSAPI_ERROR_OK == cn.set_registration("12345"));
}

TEST_F(TestAT_CellularNetwork, test_AT_CellularNetwork_get_registration_params)
{
    EventQueue que;
    FileHandle_stub fh1;
    ATHandler at(&fh1, que, 0, ",");
<commit_msg>Add UT for CEREG URC handler<commit_after>/*
 * 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 <string.h>
#include "AT_CellularNetwork.h"
#include "events/EventQueue.h"
#include "ATHandler.h"
#include "AT_CellularDevice.h"
#include "FileHandle_stub.h"
#include "CellularLog.h"
#include "ATHandler_stub.h"
#include "AT_CellularStack.h"

using namespace mbed;
using namespace events;

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

    void SetUp()
    {
        ATHandler_stub::int_count = kRead_int_table_size;
        ATHandler_stub::read_string_index = kRead_string_table_size;
        ATHandler_stub::resp_stop_success_count = kResp_stop_count_default;
        ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK;
        ATHandler_stub::int_value = -1;
    }

    void TearDown()
    {
    }
};


class my_AT_CN : public AT_CellularNetwork {
public:
    my_AT_CN(ATHandler &atHandler) : AT_CellularNetwork(atHandler) {}
    virtual ~my_AT_CN() {}
    virtual nsapi_error_t set_access_technology_impl(RadioAccessTechnology op_rat)
    {
        return NSAPI_ERROR_OK;
    }
};

class my_AT_CNipv6 : public AT_CellularNetwork {
public:
    my_AT_CNipv6(ATHandler &atHandler) : AT_CellularNetwork(atHandler) {}
    virtual ~my_AT_CNipv6() {}
    virtual nsapi_error_t set_access_technology_impl(RadioAccessTechnology op_rat)
    {
        return NSAPI_ERROR_OK;
    }
};

static int network_cb_count;
static void network_cb(nsapi_event_t ev, intptr_t intptr)
{
    network_cb_count++;
}

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

    AT_CellularNetwork *cn = new AT_CellularNetwork(at);
    EXPECT_TRUE(cn != NULL);
    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;
    FileHandle_stub fh1;
    ATHandler at(&fh1, que, 0, ",");

    AT_CellularNetwork cn(at);

    ATHandler_stub::nsapi_error_value = NSAPI_ERROR_DEVICE_ERROR;
    EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == cn.set_registration());

    ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK;
    EXPECT_TRUE(NSAPI_ERROR_OK == cn.set_registration());

    ATHandler_stub::nsapi_error_value = NSAPI_ERROR_DEVICE_ERROR;
    EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == cn.set_registration("12345"));

    ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK;
    EXPECT_TRUE(NSAPI_ERROR_OK == cn.set_registration("12345"));
}

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

{'fraction_non_alphanumeric': 0.10387882509466789, 'fraction_numerical': 0.021492170709241632, 'mean_word_length': 4.392935982339956, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3114031', 'n_tokens_mistral': 3508, 'n_tokens_neox': 3405, 'n_words': 676}