~aleteoryx/muditaos

ref: df6d7848f55a94cfeb999363169a6ccf297ce3e4 muditaos/module-services/service-cellular/tests/unittest_ussdHandler.cpp -rw-r--r-- 4.7 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <catch2/catch.hpp>

#include <module-services/service-cellular/src/ussd/USSDHandler.hpp>

TEST_CASE("ussd handler")
{
    using namespace cellular::ussd;

    USSDHandler handler;
    auto onSendCodeCalled               = 0;
    auto onAbortSessionCalled           = 0;
    auto onOpenPushSessionCalled        = 0;
    auto onRequestOpenPushSessionCalled = 0;
    auto onRequestAbortSessionCalled    = 0;

    auto onTimerStartCalled = 0;
    auto onTimerStopCalled  = 0;

    handler.onSendUssdCode = [&onSendCodeCalled](std::string) -> bool {
        onSendCodeCalled++;
        return true;
    };

    handler.onAbortSession = [&onAbortSessionCalled]([[maybe_unused]] bool ussdSessionTimeout) -> bool {
        onAbortSessionCalled++;
        return true;
    };

    handler.onOpenPushSession = [&onOpenPushSessionCalled]() -> bool {
        onOpenPushSessionCalled++;
        return true;
    };

    handler.onRequestOpenPushSession = [&onRequestOpenPushSessionCalled]() -> bool {
        onRequestOpenPushSessionCalled++;
        return true;
    };

    handler.onRequestAbortSession = [&onRequestAbortSessionCalled]() -> bool {
        onRequestAbortSessionCalled++;
        return true;
    };

    handler.onTimerStart = [&onTimerStartCalled]() { onTimerStartCalled++; };

    handler.onTimerStop = [&onTimerStopCalled]() { onTimerStopCalled++; };

    SECTION("HandleUSSDRequest - ok pull session request")
    {
        handler.handleUSSDRequest(cellular::USSDMessage::RequestType::pullSessionRequest);
        REQUIRE(onTimerStartCalled == 1);
        REQUIRE(onSendCodeCalled == 1);

        REQUIRE(onAbortSessionCalled == 0);
        REQUIRE(onOpenPushSessionCalled == 0);
        REQUIRE(onRequestOpenPushSessionCalled == 0);
        REQUIRE(onTimerStopCalled == 0);
        REQUIRE(onRequestAbortSessionCalled == 0);
    }

    SECTION("HandleUSSDRequest - ok abort session request")
    {
        handler.handleUSSDRequest(cellular::USSDMessage::RequestType::abortSession);

        REQUIRE(onAbortSessionCalled == 1);
        REQUIRE(onRequestOpenPushSessionCalled == 1);
        REQUIRE(onTimerStopCalled == 1);

        REQUIRE(onOpenPushSessionCalled == 0);
        REQUIRE(onTimerStartCalled == 0);
        REQUIRE(onSendCodeCalled == 0);
        REQUIRE(onRequestAbortSessionCalled == 0);
    }

    SECTION("HandleUSSDRequest - ok push session request")
    {
        handler.handleUSSDRequest(cellular::USSDMessage::RequestType::pushSessionRequest);
        REQUIRE(onTimerStopCalled == 1);
        REQUIRE(onOpenPushSessionCalled == 1);

        REQUIRE(onSendCodeCalled == 0);
        REQUIRE(onAbortSessionCalled == 0);
        REQUIRE(onRequestOpenPushSessionCalled == 0);
        REQUIRE(onTimerStartCalled == 0);
        REQUIRE(onRequestAbortSessionCalled == 0);
    }

    SECTION("HandleUSSDRequest - failed abort session request")
    {
        handler.onAbortSession = [&onAbortSessionCalled]([[maybe_unused]] bool ussdSessionTimeout) -> bool {
            onAbortSessionCalled++;
            return false;
        };
        handler.handleUSSDRequest(cellular::USSDMessage::RequestType::abortSession);

        REQUIRE(onAbortSessionCalled == 1);
        REQUIRE(onRequestOpenPushSessionCalled == 0);
        REQUIRE(onRequestAbortSessionCalled == 1);

        REQUIRE(onOpenPushSessionCalled == 0);
        REQUIRE(onTimerStartCalled == 0);
        REQUIRE(onSendCodeCalled == 0);
        REQUIRE(onTimerStopCalled == 0);
    }

    SECTION("setUSSDTimer - pullSessionRequest")
    {
        handler.handleUSSDRequest(cellular::USSDMessage::RequestType::pullSessionRequest);

        REQUIRE(onTimerStartCalled == 1);
        REQUIRE(onTimerStopCalled == 0);

        onTimerStartCalled = 0;
        for (std::uint32_t i = 0; i < pullResponseTimeout; i++) {
            handler.handleUSSDTimer();
        }
        REQUIRE(onTimerStartCalled == 0);
        REQUIRE(onTimerStopCalled == 0);

        handler.handleUSSDTimer();
        REQUIRE(onTimerStopCalled == 1);
    }

    SECTION("setUSSDTimer - abortSession")
    {
        handler.handleUSSDRequest(cellular::USSDMessage::RequestType::abortSession);

        REQUIRE(onTimerStartCalled == 0);
        REQUIRE(onTimerStopCalled == 1);

        handler.handleUSSDTimer();
        REQUIRE(onTimerStopCalled == 2);
    }

    SECTION("setUSSDTimer - pushSession")
    {
        handler.handleUSSDRequest(cellular::USSDMessage::RequestType::abortSession);

        REQUIRE(onTimerStartCalled == 0);
        REQUIRE(onTimerStopCalled == 1);

        handler.handleUSSDTimer();
        REQUIRE(onTimerStopCalled == 2);
    }
}