Skip to content

Commit a08bea4

Browse files
committed
tests: at_cmd_parser: Add AT command parse test
Add test AT command parsing and releated util function tests. Additionally fix incorrect loop condition in several at_utils tests. Signed-off-by: Robert Lubos <[email protected]>
1 parent 525ac93 commit a08bea4

File tree

2 files changed

+115
-11
lines changed
  • tests/lib/at_cmd_parser

2 files changed

+115
-11
lines changed

tests/lib/at_cmd_parser/at_cmd_parser/src/main.c

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,90 @@ static void test_testcases_teardown(void)
522522
at_params_list_free(&test_list2);
523523
}
524524

525+
static void test_at_cmd_setup(void)
526+
{
527+
at_params_list_init(&test_list2, TEST_PARAMS2);
528+
}
529+
530+
static void test_at_cmd(void)
531+
{
532+
int ret;
533+
char tmpbuf[32];
534+
u32_t tmpbuf_len;
535+
u16_t tmpshrt;
536+
537+
static const char at_cmd_cclk[] = "AT+CCLK=\"18/12/06,22:10:00+08\"";
538+
539+
ret = at_parser_params_from_str(at_cmd_cclk, NULL, &test_list2);
540+
zassert_true(ret == 0, "at_parser_params_from_str should return 0");
541+
542+
ret = at_params_valid_count_get(&test_list2);
543+
zassert_true(ret == 2,
544+
"at_params_valid_count_get returns wrong valid count");
545+
546+
zassert_true(at_params_type_get(&test_list2, 0) == AT_PARAM_TYPE_STRING,
547+
"Param type at index 0 should be a string");
548+
zassert_true(at_params_type_get(&test_list2, 1) == AT_PARAM_TYPE_STRING,
549+
"Param type at index 1 should be a string");
550+
551+
tmpbuf_len = sizeof(tmpbuf);
552+
zassert_equal(0, at_params_string_get(&test_list2, 0,
553+
tmpbuf, &tmpbuf_len),
554+
"Get string should not fail");
555+
zassert_equal(0, memcmp("AT+CCLK", tmpbuf, tmpbuf_len),
556+
"The string in tmpbuf should equal to AT+CCLK");
557+
558+
tmpbuf_len = sizeof(tmpbuf);
559+
zassert_equal(0, at_params_string_get(&test_list2, 1,
560+
tmpbuf, &tmpbuf_len),
561+
"Get string should not fail");
562+
zassert_equal(0, memcmp("18/12/06,22:10:00+08", tmpbuf, tmpbuf_len),
563+
"The string in tmpbuf should equal to "
564+
"18/12/06,22:10:00+08");
565+
566+
static const char at_cmd_xsystemmode[] = "AT%XSYSTEMMODE=1,2,3,4";
567+
568+
ret = at_parser_params_from_str(at_cmd_xsystemmode, NULL, &test_list2);
569+
zassert_true(ret == 0, "at_parser_params_from_str should return 0");
570+
571+
ret = at_params_valid_count_get(&test_list2);
572+
zassert_true(ret == 5,
573+
"at_params_valid_count_get returns wrong valid count");
574+
575+
zassert_true(at_params_type_get(&test_list2, 0) == AT_PARAM_TYPE_STRING,
576+
"Param type at index 0 should be a string");
577+
zassert_true(at_params_type_get(&test_list2, 1) ==
578+
AT_PARAM_TYPE_NUM_SHORT,
579+
"Param type at index 1 should be a string");
580+
zassert_true(at_params_type_get(&test_list2, 2) ==
581+
AT_PARAM_TYPE_NUM_SHORT,
582+
"Param type at index 2 should be a string");
583+
zassert_true(at_params_type_get(&test_list2, 3) ==
584+
AT_PARAM_TYPE_NUM_SHORT,
585+
"Param type at index 3 should be a string");
586+
zassert_true(at_params_type_get(&test_list2, 4) ==
587+
AT_PARAM_TYPE_NUM_SHORT,
588+
"Param type at index 4 should be a string");
589+
590+
zassert_equal(0, at_params_short_get(&test_list2, 1, &tmpshrt),
591+
"Get short should not fail");
592+
zassert_equal(1, tmpshrt, "Short should be 1");
593+
zassert_equal(0, at_params_short_get(&test_list2, 2, &tmpshrt),
594+
"Get short should not fail");
595+
zassert_equal(2, tmpshrt, "Short should be 2");
596+
zassert_equal(0, at_params_short_get(&test_list2, 3, &tmpshrt),
597+
"Get short should not fail");
598+
zassert_equal(3, tmpshrt, "Short should be 3");
599+
zassert_equal(0, at_params_short_get(&test_list2, 4, &tmpshrt),
600+
"Get short should not fail");
601+
zassert_equal(4, tmpshrt, "Short should be 4");
602+
}
603+
604+
static void test_at_cmd_teardown(void)
605+
{
606+
at_params_list_free(&test_list2);
607+
}
608+
525609
void test_main(void)
526610
{
527611
ztest_test_suite(at_cmd_parser,
@@ -540,7 +624,11 @@ void test_main(void)
540624
ztest_unit_test_setup_teardown(
541625
test_testcases,
542626
test_testcases_setup,
543-
test_testcases_teardown)
627+
test_testcases_teardown),
628+
ztest_unit_test_setup_teardown(
629+
test_at_cmd,
630+
test_at_cmd_setup,
631+
test_at_cmd_teardown)
544632
);
545633

546634
ztest_run_test_suite(at_cmd_parser);

tests/lib/at_cmd_parser/at_utils/src/main.c

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ char *string_return = "mfw_nrf9160_0.7.0-23.prealpha";
1313

1414
static void test_notification_detection(void)
1515
{
16-
for (char c = 0; c < sizeof(c); ++c) {
16+
for (char c = 0; c < 127; ++c) {
1717
if ((c == '%') || (c == '+')) {
1818
continue;
1919
}
@@ -28,9 +28,23 @@ static void test_notification_detection(void)
2828
"Notification char was not detected");
2929
}
3030

31+
static void test_command_detection(void)
32+
{
33+
zassert_true(is_command("AT+"), "Command was not detected");
34+
zassert_true(is_command("AT%"), "Command was not detected");
35+
zassert_true(is_command("AT#"), "Command was not detected");
36+
zassert_true(is_command("at+"), "Command was not detected");
37+
zassert_true(is_command("at%"), "Command was not detected");
38+
zassert_true(is_command("at#"), "Command was not detected");
39+
zassert_false(is_command("AT"), "Should fail, command too short");
40+
zassert_false(is_command("BT+"), "Should fail, invalid string");
41+
zassert_false(is_command("AB+"), "Should fail, invalid string");
42+
zassert_false(is_command("AT$"), "Should fail, invalid string");
43+
}
44+
3145
static void test_valid_notification_char_detection(void)
3246
{
33-
for (char c = 0; c < sizeof(c); ++c) {
47+
for (char c = 0; c < 127; ++c) {
3448
if ((c >= 'A') && (c <= 'Z')) {
3549
continue;
3650
}
@@ -56,7 +70,7 @@ static void test_valid_notification_char_detection(void)
5670

5771
static void test_string_termination(void)
5872
{
59-
for (char c = 1; c < sizeof(c); ++c) {
73+
for (char c = 1; c < 127; ++c) {
6074
zassert_false(is_terminated(c), "String termination detected");
6175
}
6276

@@ -66,21 +80,22 @@ static void test_string_termination(void)
6680

6781
static void test_string_separator(void)
6882
{
69-
for (char c = 0; c < sizeof(c); ++c) {
70-
if ((c == ':') || (c == ',')) {
83+
for (char c = 0; c < 127; ++c) {
84+
if ((c == ':') || (c == ',') || (c == '=')) {
7185
continue;
7286
}
7387

7488
zassert_false(is_separator(c), "Separator detected");
7589
}
7690

91+
zassert_true(is_separator('='), "Separator not detected");
7792
zassert_true(is_separator(':'), "Separator not detected");
7893
zassert_true(is_separator(','), "Separator not detected");
7994
}
8095

8196
static void test_lfcr(void)
8297
{
83-
for (char c = 0; c < sizeof(c); ++c) {
98+
for (char c = 0; c < 127; ++c) {
8499
if ((c == '\r') || (c == '\n')) {
85100
continue;
86101
}
@@ -94,7 +109,7 @@ static void test_lfcr(void)
94109

95110
static void test_dblquote(void)
96111
{
97-
for (char c = 0; c < sizeof(c); ++c) {
112+
for (char c = 0; c < 127; ++c) {
98113
if (c == '"') {
99114
continue;
100115
}
@@ -107,7 +122,7 @@ static void test_dblquote(void)
107122

108123
static void test_array_detection(void)
109124
{
110-
for (char c = 0; c < sizeof(c); ++c) {
125+
for (char c = 0; c < 127; ++c) {
111126
if ((c == '(') || (c == ')')) {
112127
continue;
113128
}
@@ -122,8 +137,8 @@ static void test_array_detection(void)
122137

123138
static void test_number_detection(void)
124139
{
125-
for (char c = 0; c < sizeof(c); ++c) {
126-
if ((c <= 0) && (c >= 9)) {
140+
for (char c = 0; c < 127; ++c) {
141+
if ((c >= '0') && (c <= '9')) {
127142
continue;
128143
}
129144

@@ -146,6 +161,7 @@ void test_main(void)
146161
{
147162
ztest_test_suite(at_cmd_parser,
148163
ztest_unit_test(test_notification_detection),
164+
ztest_unit_test(test_command_detection),
149165
ztest_unit_test(test_valid_notification_char_detection),
150166
ztest_unit_test(test_string_termination),
151167
ztest_unit_test(test_string_separator),

0 commit comments

Comments
 (0)