~aleteoryx/muditaos

ref: ec7dcfb31ebccf270e231fd2660919444e5294b9 muditaos/module-cellular/test/unittest_parse_result.cpp -rw-r--r-- 18.1 KiB
ec7dcfb3 — Artur Śleszyński [CP-46] Extend contact info endpoint 4 years 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <string>
#define CATCH_CONFIG_MAIN

#include <catch2/catch.hpp>

#include <at/cmd/CLCC.hpp>
#include <at/cmd/CSCA.hpp>
#include <at/cmd/QECCNUM.hpp>
#include <at/cmd/CFUN.hpp>
#include <at/cmd/CPBS.hpp>
#include <at/cmd/CPBR.hpp>

#include "mock/AtCommon_channel.hpp"
#include "PhoneNumber.hpp"
#include "Result.hpp"

namespace at::cmd
{
    struct CLCCStub : public CLCC
    {
        [[nodiscard]] static auto toBool(const std::string &text) -> bool
        {
            return CLCC::toBool(text);
        }
        [[nodiscard]] static auto toUInt(const std::string &text) -> std::uint8_t
        {
            return CLCC::toUInt(text);
        }

        template <typename T>[[nodiscard]] static auto toEnum(const std::string &text) -> std::optional<T>
        {
            return CLCC::toEnum<T>(text);
        }
    };
} // namespace at::cmd

TEST_CASE("CSCA parser test")
{
    SECTION("empty failed data")
    {
        at::cmd::CSCA cmd;
        at::Result base_result; // normally returned from cmux->exec(), TODO getter for dumb result ala exe
        auto &result = cmd.parse(base_result);
        REQUIRE(!result);
        REQUIRE(result.smsCenterAddress == "");
        REQUIRE(result.smsTypeOfAddress == "");
    }

    SECTION("failing channel")
    {
        at::cmd::CSCA cmd;
        at::CSCS_badChannel channel;
        auto base = channel.cmd(cmd);
        auto resp = cmd.parse(base);
        REQUIRE(!resp);
        REQUIRE(resp.code == at::Result::Code::ERROR);
    }

    SECTION("bad data")
    {
        at::cmd::CSCA cmd(at::cmd::Modifier::Get);
        at::CSCA_emptyData channel;
        auto base = channel.cmd(cmd);
        auto resp = cmd.parse(base);
        REQUIRE(!resp);
        REQUIRE(resp.code == at::Result::Code::PARSING_ERROR);
    }

    SECTION("proper data")
    {
        at::cmd::CSCA cmd;
        at::CSCS_successChannel channel;
        auto base = channel.cmd(cmd);
        auto resp = cmd.parse(base);
        REQUIRE(resp);
        REQUIRE(resp.smsCenterAddress == channel.smsCenterAddress);
        REQUIRE(resp.smsTypeOfAddress == channel.smsTypeOfAddress);
    }

    SECTION("failing data")
    {}
}

TEST_CASE("CSCA set data")
{
    SECTION("set proper data")
    {
        const std::string play_sms_center = "+48 790 998 250";
        const int example_type_of_address = 145;
        utils::PhoneNumber nr(play_sms_center);

        const std::string expected_result =
            "AT+CSCA=\"" + nr.getView().getE164() + "\"," + std::to_string(example_type_of_address);

        at::cmd::CSCA cmd;
        cmd.set(nr.getView(), example_type_of_address);
        SECTION("We fail - `NONE` modifier set")
        {
            REQUIRE(cmd.getCmd() != expected_result);
        }

        cmd.setModifier(at::cmd::Modifier::Set);

        SECTION("Success: proper modifier and data set")
        {
            REQUIRE(cmd.getCmd() == expected_result);
        }
    }
}

TEST_CASE("QECCNUM parser")
{
    SECTION("empty data")
    {
        at::cmd::QECCNUM cmd;
        at::Result base_result;
        auto &result = cmd.parse(base_result);
        REQUIRE(!result);
    }

    SECTION("no numbers")
    {
        at::cmd::QECCNUM cmd;
        at::GenericChannel channel(at::Result::Code::OK, {"+QECCNUM: 1", "+QECCNUM: 2"});
        auto base = channel.cmd(cmd);
        auto resp = cmd.parse(base);
        REQUIRE(!resp);
    }

    SECTION("only no sim numbers")
    {
        at::cmd::QECCNUM cmd;
        at::GenericChannel channel(at::Result::Code::OK, {"+QECCNUM: 0,112,999", "+QECCNUM: 1"});
        auto base = channel.cmd(cmd);
        auto resp = cmd.parse(base);
        REQUIRE(resp);
        REQUIRE(resp.eccNumbersNoSim == std::vector<std::string>({"112", "999"}));
        REQUIRE(resp.eccNumbersSim.empty());
    }

    SECTION("only sim numbers")
    {
        at::cmd::QECCNUM cmd;
        at::GenericChannel channel(at::Result::Code::OK, {"+QECCNUM: 1,112,998"});
        auto base = channel.cmd(cmd);
        auto resp = cmd.parse(base);
        REQUIRE(resp);
        REQUIRE(resp.eccNumbersNoSim.empty());
        REQUIRE(resp.eccNumbersSim == std::vector<std::string>({"112", "998"}));
    }

    SECTION("sim and no sim numbers")
    {
        at::cmd::QECCNUM cmd;
        at::GenericChannel channel(at::Result::Code::OK, {"+QECCNUM: 0,112,999", "+QECCNUM: 1,4564,25435,325454"});
        auto base = channel.cmd(cmd);
        auto resp = cmd.parse(base);
        REQUIRE(resp);
        REQUIRE(resp.eccNumbersNoSim == std::vector<std::string>({"112", "999"}));
        REQUIRE(resp.eccNumbersSim == std::vector<std::string>({"4564", "25435", "325454"}));
    }

    SECTION("add number")
    {
        at::cmd::QECCNUM cmdAddNoSim(
            at::cmd::QECCNUM::Mode::Add, at::cmd::QECCNUM::NumberType::WithoutSim, {"600800900", "200300500"});
        REQUIRE(cmdAddNoSim.getCmd() == "AT+QECCNUM=1,1,\"600800900\",\"200300500\"");
        at::cmd::QECCNUM cmdAddSim(
            at::cmd::QECCNUM::Mode::Add, at::cmd::QECCNUM::NumberType::WithSim, {"600800900", "112"});
        REQUIRE(cmdAddSim.getCmd() == "AT+QECCNUM=1,0,\"600800900\",\"112\"");
    }
}

TEST_CASE("CLCC parser")
{
    using namespace std::string_literals;
    SECTION("Empty data")
    {
        at::cmd::CLCC cmd;
        at::Result result;
        auto response = cmd.parse(result);
        REQUIRE(!response);
    }
    SECTION("Failing channel")
    {
        at::cmd::CLCC cmd;
        at::FailingChannel channel;
        auto base     = channel.cmd(cmd);
        auto response = cmd.parse(base);
        REQUIRE(!response);
        REQUIRE(response.code == at::Result::Code::ERROR);
    }
    SECTION("Success - one call")
    {
        at::cmd::CLCC cmd;
        at::CLCC_successChannel_oneCall channel;
        auto base     = channel.cmd(cmd);
        auto response = cmd.parse(base);
        REQUIRE(response);
        auto [idx, dir, stateOfCall, mode, multiparty, number, type, alpha, tokens] = response.getData()[0];
        REQUIRE(idx == 1);
        REQUIRE(dir == ModemCall::CallDir::MO);
        REQUIRE(stateOfCall == ModemCall::CallState::Active);
        REQUIRE(mode == ModemCall::CallMode::Voice);
        REQUIRE(multiparty == true);
        REQUIRE(number == utils::PhoneNumber(channel.number).getView());
        REQUIRE(type == "129"s);
        REQUIRE(alpha == "");
    }

    SECTION("Success - two calls")
    {
        at::cmd::CLCC cmd;
        at::CLCC_successChannel_twoCalls channel;
        auto base     = channel.cmd(cmd);
        auto response = cmd.parse(base);
        REQUIRE(response);
        SECTION("First entry")
        {
            auto [idx, dir, stateOfCall, mode, multiparty, number, type, alpha, tokens] = response.getData()[0];
            REQUIRE(idx == 1);
            REQUIRE(dir == ModemCall::CallDir::MO);
            REQUIRE(stateOfCall == ModemCall::CallState::Active);
            REQUIRE(mode == ModemCall::CallMode::Data);
            REQUIRE(multiparty == false);
            REQUIRE(number == utils::PhoneNumber(channel.number1).getView());
            REQUIRE(type == "129"s);
            REQUIRE(alpha == "");
        }

        SECTION("Second entry")
        {
            auto [idx, dir, stateOfCall, mode, multiparty, number, type, alpha, tokens] = response.getData()[1];
            REQUIRE(idx == 2);
            REQUIRE(dir == ModemCall::CallDir::MO);
            REQUIRE(stateOfCall == ModemCall::CallState::Active);
            REQUIRE(mode == ModemCall::CallMode::Voice);
            REQUIRE(multiparty == false);
            REQUIRE(number == utils::PhoneNumber(channel.number2).getView());
            REQUIRE(type == "129"s);
            REQUIRE(alpha == "");
        }
    }

    SECTION("Failed - bad channel result")
    {
        at::cmd::CLCC cmd;
        at::CSCS_badChannel channel;
        auto base = channel.cmd(cmd);
        auto resp = cmd.parse(base);
        REQUIRE(!resp);
        REQUIRE(resp.code == at::Result::Code::ERROR);
    }

    SECTION("Success - only OK data")
    {
        at::cmd::CLCC cmd;
        at::OK_Channel channel;
        auto base     = channel.cmd(cmd);
        auto response = cmd.parse(base);
        REQUIRE(response);
    }

    SECTION("Failed - only OG data")
    {
        at::cmd::CLCC cmd;
        at::OG_Channel channel;
        auto base     = channel.cmd(cmd);
        auto response = cmd.parse(base);
        REQUIRE(response.code == at::Result::Code::ERROR);
    }
}

TEST_CASE("CLCC set data")
{
    using namespace std::literals;
    at::cmd::CLCC cmd;
    SECTION("None modifier set")
    {
        constexpr auto expectedResult = "AT+CLCC"sv;
        REQUIRE(cmd.getCmd() == expectedResult);
    }

    SECTION("Get modifier set")
    {
        constexpr auto expectedResult = "AT+CLCC?"sv;
        cmd.setModifier(at::cmd::Modifier::Get);
        REQUIRE(cmd.getCmd() == expectedResult);
    }
}

TEST_CASE("CLCC conversion methods")
{
    SECTION("String to bool")
    {
        SECTION("Proper data - true")
        {
            REQUIRE(at::cmd::CLCCStub::toBool("1") == true);
        }
        SECTION("Proper data - true")
        {
            REQUIRE(at::cmd::CLCCStub::toBool("0") == false);
        }
        SECTION("Invaild data")
        {
            REQUIRE_THROWS(at::cmd::CLCCStub::toBool("-1") == false);
        }
    }
    SECTION("String to uint32")
    {
        REQUIRE(at::cmd::CLCCStub::toUInt("1") == 1);
        REQUIRE(at::cmd::CLCCStub::toUInt("32") == 32);
        REQUIRE(at::cmd::CLCCStub::toUInt("255") == std::numeric_limits<std::uint8_t>::max());
        REQUIRE_THROWS(at::cmd::CLCCStub::toUInt("256"));
    }
    SECTION("String to enum")
    {
        SECTION("Call dir")
        {
            REQUIRE(at::cmd::CLCCStub::toEnum<ModemCall::CallDir>("-1") == std::nullopt);
            REQUIRE(at::cmd::CLCCStub::toEnum<ModemCall::CallDir>("0").value() == ModemCall::CallDir::MO);
            REQUIRE(at::cmd::CLCCStub::toEnum<ModemCall::CallDir>("1").value() == ModemCall::CallDir::MT);
            REQUIRE(at::cmd::CLCCStub::toEnum<ModemCall::CallDir>("2") == std::nullopt);
        }
        SECTION("Call state")
        {
            REQUIRE(at::cmd::CLCCStub::toEnum<ModemCall::CallState>("-1") == std::nullopt);
            REQUIRE(at::cmd::CLCCStub::toEnum<ModemCall::CallState>("0").value() == ModemCall::CallState::Active);
            REQUIRE(at::cmd::CLCCStub::toEnum<ModemCall::CallState>("5").value() == ModemCall::CallState::Waiting);
            REQUIRE(at::cmd::CLCCStub::toEnum<ModemCall::CallState>("6") == std::nullopt);
        }
        SECTION("Call mode")
        {
            REQUIRE(at::cmd::CLCCStub::toEnum<ModemCall::CallMode>("-1") == std::nullopt);
            REQUIRE(at::cmd::CLCCStub::toEnum<ModemCall::CallMode>("0").value() == ModemCall::CallMode::Voice);
            REQUIRE(at::cmd::CLCCStub::toEnum<ModemCall::CallMode>("2").value() == ModemCall::CallMode::FAX);
            REQUIRE(at::cmd::CLCCStub::toEnum<ModemCall::CallMode>("3") == std::nullopt);
        }
    }
}

TEST_CASE("CFUN parser")
{
    SECTION("Empty data")
    {
        at::cmd::CFUN cmd;
        at::Result result;
        auto response = cmd.parse(result);
        REQUIRE(!response);
    }

    SECTION("Failing channel")
    {
        at::cmd::CFUN cmd;
        at::FailingChannel channel;
        auto base     = channel.cmd(cmd);
        auto response = cmd.parse(base);
        REQUIRE(!response);
        REQUIRE(response.code == at::Result::Code::ERROR);
    }

    SECTION("Success - valid token")
    {
        at::cmd::CFUN cmd;
        at::CFUN_successChannel channel;
        auto base     = channel.cmd(cmd);
        auto response = cmd.parse(base);
        REQUIRE(response);
        REQUIRE(response.functionality == at::cfun::Functionality::Full);
    }

    SECTION("Failed - invalid token")
    {
        at::cmd::CFUN cmd;
        at::CFUN_invalidTokenChannel channel;
        auto base     = channel.cmd(cmd);
        auto response = cmd.parse(base);
        REQUIRE(!response);
    }
}

TEST_CASE("CFUN set data")
{
    at::cmd::CFUN cmd;
    SECTION("None modifier set")
    {
        constexpr auto expectedResult = "AT+CFUN";
        REQUIRE(cmd.getCmd() == expectedResult);
    }

    SECTION("Get modifier set")
    {
        constexpr auto expectedResult = "AT+CFUN?";
        cmd.setModifier(at::cmd::Modifier::Get);
        REQUIRE(cmd.getCmd() == expectedResult);
    }

    SECTION("Set modifier set")
    {
        constexpr auto expectedResult = "AT+CFUN=";
        cmd.setModifier(at::cmd::Modifier::Set);
        REQUIRE(cmd.getCmd() == expectedResult);
    }

    SECTION("Set modifier set functionality only")
    {
        constexpr auto expectedResult = "AT+CFUN=1";
        cmd.setModifier(at::cmd::Modifier::Set);
        cmd.set(at::cfun::Functionality::Full);
        REQUIRE(cmd.getCmd() == expectedResult);
    }

    SECTION("Set modifier set functionality and reset")
    {
        constexpr auto expectedResult = "AT+CFUN=4,1";
        cmd.setModifier(at::cmd::Modifier::Set);
        cmd.set(at::cfun::Functionality::DisableRF, at::cfun::Reset::ResetTheME);
        REQUIRE(cmd.getCmd() == expectedResult);
    }
}

TEST_CASE("CPBS parser")
{
    SECTION("Empty data")
    {
        at::cmd::CPBS cmd;
        at::Result result;
        auto response = cmd.parse(result);
        REQUIRE(!response);
    }

    SECTION("Failing channel")
    {
        at::cmd::CPBS cmd;
        at::FailingChannel channel;
        auto base     = channel.cmd(cmd);
        auto response = cmd.parse(base);
        REQUIRE(!response);
        REQUIRE(response.code == at::Result::Code::ERROR);
    }

    SECTION("Success - valid token")
    {
        at::cmd::CPBS cmd;
        at::CPBS_successChannel channel;
        auto base     = channel.cmd(cmd);
        auto response = cmd.parse(base);
        REQUIRE(response.storage == "\"SM\"");
        REQUIRE(response.used == 2);
        REQUIRE(response.total == 500);
    }

    SECTION("Failed - to little tokens")
    {
        at::cmd::CPBS cmd;
        at::CPBS_toLittleTokens channel;
        auto base     = channel.cmd(cmd);
        auto response = cmd.parse(base);
        REQUIRE(!response);
        REQUIRE(response.code == at::Result::Code::PARSING_ERROR);
    }

    SECTION("Failed - to many tokens")
    {
        at::cmd::CPBS cmd;
        at::CPBS_toManyTokens channel;
        auto base     = channel.cmd(cmd);
        auto response = cmd.parse(base);
        REQUIRE(!response);
        REQUIRE(response.code == at::Result::Code::PARSING_ERROR);
    }
}

TEST_CASE("CPBS set data")
{
    at::cmd::CPBS cmd;
    SECTION("None modifier set")
    {
        constexpr auto expectedResult = "AT+CPBS";
        REQUIRE(cmd.getCmd() == expectedResult);
    }

    SECTION("Get modifier set")
    {
        constexpr auto expectedResult = "AT+CPBS?";
        cmd.setModifier(at::cmd::Modifier::Get);
        REQUIRE(cmd.getCmd() == expectedResult);
    }

    SECTION("Set modifier set")
    {
        constexpr auto expectedResult = "AT+CPBS=";
        cmd.setModifier(at::cmd::Modifier::Set);
        REQUIRE(cmd.getCmd() == expectedResult);
    }

    SECTION("Set commnad")
    {
        constexpr auto expectedResult = "AT+CPBS=\"SM\"";
        cmd                           = at::cmd::CPBS(at::cmd::Modifier::Set);
        cmd.set();
        REQUIRE(cmd.getCmd() == expectedResult);
    }
}

TEST_CASE("CPBR parser")
{
    SECTION("Empty data")
    {
        at::cmd::CPBR cmd;
        at::Result result;
        auto response = cmd.parse(result);
        REQUIRE(!response);
    }

    SECTION("Failing channel")
    {
        at::cmd::CPBR cmd;
        at::FailingChannel channel;
        auto base     = channel.cmd(cmd);
        auto response = cmd.parse(base);
        REQUIRE(!response);
        REQUIRE(response.code == at::Result::Code::ERROR);
    }

    SECTION("Success - valid token")
    {
        at::cmd::CPBR cmd;
        at::CPBR_successChannel channel;
        auto base     = channel.cmd(cmd);
        auto response = cmd.parse(base);

        REQUIRE(response.contacts.size() == 2);

        REQUIRE(response.contacts[0].index == 1);
        REQUIRE(response.contacts[0].number == "123456789");
        REQUIRE(response.contacts[0].type == at::cpbr::ContactType::National);
        REQUIRE(response.contacts[0].name == "Mock1");

        REQUIRE(response.contacts[1].index == 2);
        REQUIRE(response.contacts[1].number == "+48123456789");
        REQUIRE(response.contacts[1].type == at::cpbr::ContactType::International);
        REQUIRE(response.contacts[1].name == "Mock2");
    }

    SECTION("Failed - to little tokens")
    {
        at::cmd::CPBR cmd;
        at::CPBR_toLittleTokens channel;
        auto base     = channel.cmd(cmd);
        auto response = cmd.parse(base);
        REQUIRE(!response);
        REQUIRE(response.code == at::Result::Code::PARSING_ERROR);
    }

    SECTION("Failed - to many tokens")
    {
        at::cmd::CPBR cmd;
        at::CPBR_toManyTokens channel;
        auto base     = channel.cmd(cmd);
        auto response = cmd.parse(base);
        REQUIRE(!response);
        REQUIRE(response.code == at::Result::Code::PARSING_ERROR);
    }

    SECTION("Failed - invalid type")
    {
        at::cmd::CPBR cmd;
        at::CPBR_invalidType channel;
        auto base     = channel.cmd(cmd);
        auto response = cmd.parse(base);
        REQUIRE(!response);
        REQUIRE(response.code == at::Result::Code::PARSING_ERROR);
    }
}

TEST_CASE("CPBR set data")
{
    at::cmd::CPBR cmd;
    SECTION("None modifier set")
    {
        constexpr auto expectedResult = "AT+CPBR";
        REQUIRE(cmd.getCmd() == expectedResult);
    }

    SECTION("Get modifier set")
    {
        constexpr auto expectedResult = "AT+CPBR?";
        cmd.setModifier(at::cmd::Modifier::Get);
        REQUIRE(cmd.getCmd() == expectedResult);
    }

    SECTION("Set modifier set")
    {
        constexpr auto expectedResult = "AT+CPBR=";
        cmd.setModifier(at::cmd::Modifier::Set);
        REQUIRE(cmd.getCmd() == expectedResult);
    }

    SECTION("Set commnad")
    {
        constexpr auto expectedResult = "AT+CPBR=1,5";
        cmd                           = at::cmd::CPBR(at::cmd::Modifier::Set);
        cmd.setSimContactsReadRange(1, 5);
        REQUIRE(cmd.getCmd() == expectedResult);
    }
}