~aleteoryx/muditaos

ref: df6d7848f55a94cfeb999363169a6ccf297ce3e4 muditaos/module-services/service-cellular/tests/unittest_simcard.cpp -rw-r--r-- 3.4 KiB
df6d7848 — Lefucjusz [BH-2056] Add bedside lamp settings 1 year, 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <catch2/catch.hpp>
#include <service-cellular/src/SimCard.hpp>

#include <at/Cmd.hpp>
#include <at/Result.hpp>
#include <at/ATFactory.hpp>

#include <module-cellular/test/mock/AtCommon_channel.hpp>

namespace at
{
    class QPINC_Channel : public ChannelMock
    {
      public:
        Result::Code code;
        at::EquipmentErrorCode err;
        const std::string pinAttempts;
        const std::string pukAttempts;

        QPINC_Channel(Result::Code code          = Result::Code::OK,
                      at::EquipmentErrorCode err = at::EquipmentErrorCode::NoInformation,
                      int pinAttempts            = 2,
                      int pukAttempts            = 1)
            : code(code), err(err), pinAttempts(std::to_string(pinAttempts)), pukAttempts(std::to_string(pukAttempts))
        {}

        auto cmd(const at::Cmd &at) -> Result override
        {
            const auto &cmd = at.getCmd();
            if (cmd.rfind("AT+QPINC=", 0) == 0)
                return Result(Result::Code::OK, {"+QPINC:0," + pinAttempts + "," + pukAttempts, "OK"});
            Result result(code, {});
            result.errorCode = err;
            return result;
        }
    };
} // namespace at

TEST_CASE("SimCard functionality test")
{
    SECTION("simCodeToString()")
    {
        cellular::api::SimCode v{1, 2, 3, 4};
        cellular::api::SimCode empty;
        REQUIRE(cellular::internal::simCodeToString(v) == "1234");
        REQUIRE(cellular::internal::simCodeToString(empty).empty());
    }

    SECTION("Check Channel mockup sanity")
    {
        auto channel = at::QPINC_Channel();

        auto resp = channel.cmd(at::factory(at::AT::QPINC));
        REQUIRE(resp.code == at::Result::Code::OK);

        at::response::qpinc::AttemptsCounters ret;
        REQUIRE(at::response::parseQPINC(resp, ret));

        REQUIRE(resp.code == at::Result::Code::OK);
        REQUIRE(ret.PinCounter == 2);
        REQUIRE(ret.PukCounter == 1);
    }

    SECTION("Unlock with PIN - sucessful")
    {
        cellular::service::SimCard simCard;

        auto mockChannel = at::QPINC_Channel();
        simCard.setChannel(&mockChannel);

        bool event             = false;
        simCard.onUnhandledCME = [&event](unsigned int code) { event = true; };

        auto result = simCard.handlePinUnlock({});
        REQUIRE(result);
        REQUIRE(!event);
    }

    SECTION("Unlock with PIN - incorrect password")
    {
        cellular::service::SimCard simCard;

        auto mockChannel = at::QPINC_Channel(at::Result::Code::ERROR, at::EquipmentErrorCode::IncorrectPassword);
        simCard.setChannel(&mockChannel);

        bool event             = false;
        simCard.onUnhandledCME = [&event](unsigned int code) { event = true; };

        auto result = simCard.handlePinUnlock({});
        REQUIRE(!result);
        REQUIRE(!event);
    }

    SECTION("Unlock with PIN - unhandled CME")
    {
        cellular::service::SimCard simCard;

        auto mockChannel = at::QPINC_Channel(at::Result::Code::ERROR);
        simCard.setChannel(&mockChannel);

        bool event             = false;
        simCard.onUnhandledCME = [&event](unsigned int code) { event = true; };

        auto result = simCard.handlePinUnlock({});
        REQUIRE(!result);
        REQUIRE(event);
    }
}