<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 "CppUTest/TestHarness.h"
#include "test_at_cellularsms.h"
#include <string.h>
#include "AT_CellularNetwork.h"
#include "EventQueue.h"
#include "ATHandler.h"
#include "ATHandler_stub.h"
#include "AT_CellularSMS.h"
#include "FileHandle_stub.h"
#include "CellularLog.h"

using namespace mbed;
using namespace events;

Test_AT_CellularSMS::Test_AT_CellularSMS()
{
    ATHandler_stub::return_given_size = false;
    ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK;
}

Test_AT_CellularSMS::~Test_AT_CellularSMS()
{
}

void Test_AT_CellularSMS::test_AT_CellularSMS_constructor()
{
    EventQueue que;
    FileHandle_stub fh1;
    ATHandler at(&fh1, que, 0, ",");

    AT_CellularSMS *sms = new AT_CellularSMS(at);

    delete sms;
}

void Test_AT_CellularSMS::test_AT_CellularSMS_initialize()
{
    EventQueue que;
    FileHandle_stub fh1;
    ATHandler at(&fh1, que, 0, ",");

    AT_CellularSMS sms(at);
    ATHandler_stub::nsapi_error_value = NSAPI_ERROR_AUTH_FAILURE;
    CHECK(NSAPI_ERROR_AUTH_FAILURE == sms.initialize(CellularSMS::CellularSMSMmodeText));
}


void Test_AT_CellularSMS::test_AT_CellularSMS_send_sms()
{
    EventQueue que;
    FileHandle_stub fh1;
    ATHandler at(&fh1, que, 0, ",");

    AT_CellularSMS sms(at);
    CHECK(NSAPI_ERROR_PARAMETER == sms.send_sms(NULL, "2", 1));

    sms.initialize(CellularSMS::CellularSMSMmodeText);
    ATHandler_stub::size_value = 1;
    CHECK(1 == sms.send_sms("1", "22", 2));

    ATHandler_stub::size_value = 2;
    CHECK(2 == sms.send_sms("1", "22", 2));

    sms.initialize(CellularSMS::CellularSMSMmodePDU);
    CHECK(2 == sms.send_sms("1", "23", 2));

    ATHandler_stub::nsapi_error_ok_counter = 1;
    ATHandler_stub::size_value = 32;
    ATHandler_stub::nsapi_error_value = NSAPI_ERROR_AUTH_FAILURE;
    CHECK(NSAPI_ERROR_AUTH_FAILURE == sms.send_sms("1", "23232323", 8));

    ATHandler_stub::nsapi_error_ok_counter = 2;
    ATHandler_stub::size_value = 32;
    ATHandler_stub::nsapi_error_value = NSAPI_ERROR_AUTH_FAILURE;
    CHECK(NSAPI_ERROR_AUTH_FAILURE == sms.send_sms("1", "23232323", 8));

    ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK;
    char table[] = "232323232323232323232323232323232323232323232323232323\
                    232323232323232323232323232323232323232323232323232323\
                    232323232323232323232323232323232323232323232323232323\
                    23232323232323232323232323232323232323\0";
    ATHandler_stub::size_value = 0;
    ATHandler_stub::return_given_size = true;
    CHECK(218 == sms.send_sms("1", table, strlen(table)));

    CHECK(218 == sms.send_sms("12", table, strlen(table)));
}


void Test_AT_CellularSMS::test_AT_CellularSMS_get_sms()
{
    EventQueue que;<commit_msg>Cellular: Fixed sms unit tests.<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 "CppUTest/TestHarness.h"
#include "test_at_cellularsms.h"
#include <string.h>
#include "AT_CellularNetwork.h"
#include "EventQueue.h"
#include "ATHandler.h"
#include "ATHandler_stub.h"
#include "AT_CellularSMS.h"
#include "FileHandle_stub.h"
#include "CellularLog.h"

using namespace mbed;
using namespace events;

Test_AT_CellularSMS::Test_AT_CellularSMS()
{
    ATHandler_stub::return_given_size = false;
    ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK;
}

Test_AT_CellularSMS::~Test_AT_CellularSMS()
{
}

void Test_AT_CellularSMS::test_AT_CellularSMS_constructor()
{
    EventQueue que;
    FileHandle_stub fh1;
    ATHandler at(&fh1, que, 0, ",");

    AT_CellularSMS *sms = new AT_CellularSMS(at);

    delete sms;
}

void Test_AT_CellularSMS::test_AT_CellularSMS_initialize()
{
    EventQueue que;
    FileHandle_stub fh1;
    ATHandler at(&fh1, que, 0, ",");

    AT_CellularSMS sms(at);
    ATHandler_stub::nsapi_error_value = NSAPI_ERROR_AUTH_FAILURE;
    CHECK(NSAPI_ERROR_AUTH_FAILURE == sms.initialize(CellularSMS::CellularSMSMmodeText));
}


void Test_AT_CellularSMS::test_AT_CellularSMS_send_sms()
{
    EventQueue que;
    FileHandle_stub fh1;
    ATHandler at(&fh1, que, 0, ",");

    AT_CellularSMS sms(at);
    LONGS_EQUAL(NSAPI_ERROR_PARAMETER, sms.send_sms(NULL, "2", 1));

    sms.initialize(CellularSMS::CellularSMSMmodeText);
    ATHandler_stub::size_value = 1;
    LONGS_EQUAL(1, sms.send_sms("1", "22", 2));

    ATHandler_stub::size_value = 2;
    LONGS_EQUAL(2, sms.send_sms("1", "22", 2));

    ATHandler_stub::return_given_size = true; // PDU mode write is much longer than than msg len
    sms.initialize(CellularSMS::CellularSMSMmodePDU);
    LONGS_EQUAL(2, sms.send_sms("1", "23", 2));;

    ATHandler_stub::nsapi_error_ok_counter = 1;
    ATHandler_stub::size_value = 32;
    ATHandler_stub::nsapi_error_value = NSAPI_ERROR_AUTH_FAILURE;
    LONGS_EQUAL(NSAPI_ERROR_AUTH_FAILURE, sms.send_sms("1", "23232323", 8));

    ATHandler_stub::nsapi_error_ok_counter = 2;
    ATHandler_stub::size_value = 32;
    ATHandler_stub::nsapi_error_value = NSAPI_ERROR_AUTH_FAILURE;
    LONGS_EQUAL(NSAPI_ERROR_AUTH_FAILURE, sms.send_sms("1", "23232323", 8));

    ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK;
    char table[] = "232323232323232323232323232323232323232323232323232323\
                    232323232323232323232323232323232323232323232323232323\
                    232323232323232323232323232323232323232323232323232323\
                    23232323232323232323232323232323232323\0";

    LONGS_EQUAL(strlen(table), sms.send_sms("1", table, strlen(table)));
    LONGS_EQUAL(strlen(table), sms.send_sms("12", table, strlen(table)));
}


void Test_AT_CellularSMS::test_AT_CellularSMS_get_sms()
{
    EventQueue que;
{'fraction_non_alphanumeric': 0.11044645462503648, 'fraction_numerical': 0.07922381091333527, 'mean_word_length': 4.81918505942275, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '4333755', 'n_tokens_mistral': 2822, 'n_tokens_neox': 2555, 'n_words': 471}