Skip to content

Commit c038419

Browse files
author
Cruz Monrreal
authored
Merge pull request #7333 from mirelachirica/at_handler_reading_improvements
Cellular: More unit tests for ATHandler's read routines
2 parents 45b8ec9 + 7de9770 commit c038419

File tree

7 files changed

+338
-62
lines changed

7 files changed

+338
-62
lines changed

features/cellular/UNITTESTS/at/athandler/Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ COMPONENT_NAME = ATHandler_unit
44

55
#This must be changed manually
66
SRC_FILES = \
7-
../../../framework/AT/ATHandler.cpp
7+
../../../framework/AT/ATHandler.cpp \
8+
../../../framework/common/CellularUtil.cpp
89

910
TEST_SRC_FILES = \
1011
main.cpp \
@@ -13,7 +14,6 @@ TEST_SRC_FILES = \
1314
../../stubs/AT_CellularBase_stub.cpp \
1415
../../stubs/EventQueue_stub.cpp \
1516
../../stubs/FileHandle_stub.cpp \
16-
../../stubs/CellularUtil_stub.cpp \
1717
../../stubs/us_ticker_stub.cpp \
1818
../../stubs/mbed_wait_api_stub.cpp \
1919
../../stubs/mbed_assert_stub.cpp \
@@ -22,6 +22,7 @@ TEST_SRC_FILES = \
2222
../../stubs/equeue_stub.cpp \
2323
../../stubs/Kernel.cpp \
2424
../../stubs/Thread_stub.cpp \
25+
../../stubs/randLIB_stub.cpp
2526

2627
include ../../MakefileWorker.mk
2728

features/cellular/UNITTESTS/at/athandler/athandlertest.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,11 @@ TEST(ATHandler, test_ATHandler_read_string)
177177
unit->test_ATHandler_read_string();
178178
}
179179

180+
TEST(ATHandler, test_ATHandler_read_hex_string)
181+
{
182+
unit->test_ATHandler_read_hex_string();
183+
}
184+
180185
TEST(ATHandler, test_ATHandler_read_int)
181186
{
182187
unit->test_ATHandler_read_int();

features/cellular/UNITTESTS/at/athandler/test_athandler.cpp

Lines changed: 254 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -469,97 +469,314 @@ void Test_ATHandler::test_ATHandler_read_bytes()
469469
{
470470
EventQueue que;
471471
FileHandle_stub fh1;
472+
filehandle_stub_table = NULL;
473+
filehandle_stub_table_pos = 0;
472474

473475
ATHandler at(&fh1, que, 0, ",");
474476
uint8_t buf[5];
475-
CHECK(-1 == at.read_bytes(buf, 25));
476477

477-
CHECK(-1 == at.read_bytes(buf, 5));
478+
// TEST EMPTY BUFFER
479+
// Shouldn't read any byte since buffer is empty
480+
CHECK(-1 == at.read_bytes(buf, 1));
481+
CHECK(NSAPI_ERROR_DEVICE_ERROR == at.get_last_error());
482+
// Return error due to error set to at handler by the above call on empty buffer
483+
CHECK(-1 == at.read_bytes(buf, 1));
478484

479-
char table[] = "ssssssssssssssssssssssssssssOK\r\n\0";
480-
filehandle_stub_table = table;
485+
// TEST DATA IN BUFFER
486+
at.clear_error();
487+
char table1[] = "1234512345678OK\r\n\0";
488+
filehandle_stub_table = table1;
481489
filehandle_stub_table_pos = 0;
482490
mbed_poll_stub::revents_value = POLLIN;
483-
mbed_poll_stub::int_value = strlen(table);
484-
491+
mbed_poll_stub::int_value = 1;;
485492

486-
at.clear_error();
493+
// Read 5 bytes
487494
CHECK(5 == at.read_bytes(buf, 5));
495+
CHECK(!memcmp(buf, table1, 5));
496+
// get_char triggered above should have filled in the whole reading buffer(fill_buffer())
497+
CHECK(filehandle_stub_table_pos == (strlen(table1) - 1));
498+
// Read another 8 bytes
499+
CHECK(8 == at.read_bytes(buf, 8) && !memcmp(buf, table1 + 5, 8));
500+
// Reading more than the 4 bytes left -> ERROR
501+
CHECK(-1 == at.read_bytes(buf, 5));
502+
CHECK(NSAPI_ERROR_DEVICE_ERROR == at.get_last_error());
488503
}
489504

490505
void Test_ATHandler::test_ATHandler_read_string()
491506
{
492507
EventQueue que;
493508
FileHandle_stub fh1;
509+
filehandle_stub_table = NULL;
510+
filehandle_stub_table_pos = 0;
494511

495512
ATHandler at(&fh1, que, 0, ",");
496513

514+
// *** EMPTY ***
497515
at.clear_error();
498-
char table[] = "\"s,\"OK\r\n\0";
499-
filehandle_stub_table = table;
516+
char table1[] = "";
517+
at.flush();
518+
filehandle_stub_table = table1;
500519
filehandle_stub_table_pos = 0;
501520
mbed_poll_stub::revents_value = POLLIN;
502-
mbed_poll_stub::int_value = strlen(table);
503-
504-
char buf[5];
505-
uint8_t buf2[5];
521+
mbed_poll_stub::int_value = 1;
522+
char buf1[1];
523+
// No _stop_tag set without resp_start
524+
CHECK(-1 == at.read_string(buf1, 1));
525+
at.clear_error();
526+
// Set _stop_tag to resp_stop(OKCRLF)
506527
at.resp_start();
507-
at.read_bytes(buf2, 5);
508-
CHECK(-1 == at.read_string(buf, 15));
509-
at.flush();
528+
// Device error because buffer is empty
529+
CHECK(NSAPI_ERROR_DEVICE_ERROR == at.get_last_error());
510530
at.clear_error();
531+
// Device error because empty buffer and attempt to fill_buffer by consume_char('\"')
532+
CHECK(-1 == at.read_string(buf1, 1));
511533

512-
filehandle_stub_table = table;
534+
// *** 1 BYTE ***
535+
at.clear_error();
536+
char table2[] = "s\0";
537+
at.flush();
538+
filehandle_stub_table = table2;
513539
filehandle_stub_table_pos = 0;
514-
540+
mbed_poll_stub::revents_value = POLLIN;
541+
mbed_poll_stub::int_value = 1;
542+
char buf2[1];
543+
// Set _stop_tag to resp_stop(OKCRLF)
515544
at.resp_start();
516-
at.read_bytes(buf2, 1);
517-
CHECK(1 == at.read_string(buf, 5, true));
518-
at.flush();
545+
// Device error because no CRLF and no more data to read
546+
CHECK(NSAPI_ERROR_DEVICE_ERROR == at.get_last_error());
519547
at.clear_error();
548+
CHECK(0 == at.read_string(buf2, 1));
520549

521-
char table2[] = "\"s\"OK\r\n\0";
522-
filehandle_stub_table = table2;
550+
// *** CRLF ***
551+
at.clear_error();
552+
char table3[] = "\r\ns\r\n\0";
553+
at.flush();
554+
filehandle_stub_table = table3;
523555
filehandle_stub_table_pos = 0;
524556
mbed_poll_stub::revents_value = POLLIN;
525-
mbed_poll_stub::int_value = strlen(table2);
557+
mbed_poll_stub::int_value = 1;
558+
char buf3[1];
559+
// Set _stop_tag to resp_stop(OKCRLF)
560+
at.resp_start();
561+
// OK because after CRLF matched there is more data to read ending in CRLF
562+
CHECK(NSAPI_ERROR_OK == at.get_last_error());
563+
// To read 0 bytes from: s\r\n
564+
CHECK(0 == at.read_string(buf3, 0 + 1/*for NULL*/));
565+
// To read 1 byte from: s\r\n -> read s
566+
CHECK(1 == at.read_string(buf3, 1 + 1/*for NULL*/));
526567

568+
// *** Reading more than available in buffer ***
569+
at.clear_error();
570+
char table4[] = "\"s,\"OK\r\n\0";
571+
at.flush();
572+
filehandle_stub_table = table4;
573+
filehandle_stub_table_pos = 0;
574+
mbed_poll_stub::revents_value = POLLIN;
575+
mbed_poll_stub::int_value = 1;
576+
char buf4[7];
577+
uint8_t buf5[5];
578+
// NO prefix, NO OK, NO ERROR and NO URC match, CRLF found -> return so buffer could be read
527579
at.resp_start();
528-
at.read_bytes(buf2, 1);
529-
CHECK(1 == at.read_string(buf, 5, true));
580+
// TO read 5 bytes from: "s,"OK\r\n -> read "s,"O
581+
at.read_bytes(buf5, 5);
582+
// K\r\n left to be read -> reading more than 3 + 1(for NULL) -> ERROR
583+
CHECK(-1 == at.read_string(buf4, 4 + 1/*for NULL*/));
584+
CHECK(NSAPI_ERROR_DEVICE_ERROR == at.get_last_error());
585+
586+
// *** Encountering delimiter after reading 1 byte ***
587+
at.clear_error();
530588
at.flush();
589+
filehandle_stub_table = table4;
590+
filehandle_stub_table_pos = 0;
591+
// NO prefix, NO OK, NO ERROR and NO URC match, CRLF found -> return so buffer could be read
592+
at.resp_start();
593+
// TO read 1 byte from: "s,"OK\r\n -> read "
594+
at.read_bytes(buf5, 1);
595+
// TO read max 4 from: s,"OK\r\n -> read s and stop on ,
596+
CHECK(1 == at.read_string(buf4, 4 + 1/*for NULL*/));
597+
598+
// *** Encountering delimiter as first char in buffer ***
531599
at.clear_error();
600+
at.flush();
601+
filehandle_stub_table = table4;
602+
filehandle_stub_table_pos = 0;
603+
// NO prefix, NO OK, NO ERROR and NO URC match, CRLF found -> return so buffer could be read
604+
at.resp_start();
605+
// TO read 2 bytes from: "s,"OK\r\n -> read "s
606+
at.read_bytes(buf5, 2);
607+
// TO read max 4 bytes from: ,"OK\r\n -> stop on ,
608+
CHECK(0 == at.read_string(buf4, 4 + 1/*for NULL*/));
532609

533-
char table3[] = "sss\rsss\0";
534-
filehandle_stub_table = table3;
610+
// *** Read as much as buffer size is without encountering any delimiter " or OKCRLF ***
611+
at.clear_error();
612+
char table5[] = "\"s\"OK\r\nabcd\0";
613+
at.flush();
614+
filehandle_stub_table = table5;
535615
filehandle_stub_table_pos = 0;
536616
mbed_poll_stub::revents_value = POLLIN;
537-
mbed_poll_stub::int_value = strlen(table);
617+
mbed_poll_stub::int_value = 1;
618+
// NO prefix, NO OK, NO ERROR and NO URC match, CRLF found -> return so buffer could be read
619+
at.resp_start();
620+
// TO read 1 byte from: "s"OK\r\n -> read "
621+
at.read_bytes(buf5, 1);
622+
// TO read max 1 byte from: s"OK\r\n -> read s
623+
CHECK(1 == at.read_string(buf4, 1 + 1/*for NULL*/));
624+
625+
// *** Consume " and run into OKCRLF ***
626+
// TO read max 1 byte from: "OK\r\n -> consume " and find stop tag OKCRLF
627+
CHECK(0 == at.read_string(buf4, 1 + 1/*for NULL*/));
628+
629+
// *** Try to read after stop tag was found ***
630+
// stop tag found do not read further
631+
CHECK(-1 == at.read_string(buf4, 1 + 1/*for NULL*/));
632+
633+
// *** Try to read after stop tag was found when parameter allows it ***
634+
// stop tag found but flag indicates to read despite stop_tag found
635+
CHECK(4 == at.read_string(buf4, 4 + 1/*for NULL*/, true));
636+
637+
// *** Read as much as buffer size is without encountering any delimiter " or OKCRLF ***
638+
at.clear_error();
639+
char table6[] = "sss\rsss\0";
640+
at.flush();
641+
filehandle_stub_table = table6;
642+
filehandle_stub_table_pos = 0;
643+
mbed_poll_stub::revents_value = POLLIN;
644+
mbed_poll_stub::int_value = 1;
645+
at.resp_start("s");
646+
// TO read from: ss\rsss -> read all 6 chars ss\rsss
647+
CHECK(6 == at.read_string(buf4, 6 + 1/*for NULL*/));
538648

649+
// *** Reading when buffer only has " ***
650+
at.clear_error();
651+
char table7[] = "s\"\0";
652+
at.flush();
653+
filehandle_stub_table = table7;
654+
filehandle_stub_table_pos = 0;
655+
mbed_poll_stub::revents_value = POLLIN;
656+
mbed_poll_stub::int_value = 1;
539657
at.resp_start("s");
540-
at.read_string(buf, 5, true);
658+
// TO read from buffer having only " -> consume " -> trying to read when nothing in buffer
659+
CHECK(-1 == at.read_string(buf4, 5));
660+
CHECK(NSAPI_ERROR_DEVICE_ERROR == at.get_last_error());
661+
662+
// *** Reading through partially matching stop tag ***
663+
at.clear_error();
664+
char table8[] = "\"s\"OK\rabcd\r\n\0";
541665
at.flush();
666+
filehandle_stub_table = table8;
667+
filehandle_stub_table_pos = 0;
668+
mbed_poll_stub::revents_value = POLLIN;
669+
mbed_poll_stub::int_value = 1;
670+
char buf8[9];
671+
// NO prefix, NO OK, NO ERROR and NO URC match, CRLF found -> return so buffer could be read
672+
at.resp_start();
673+
// TO read from
674+
CHECK(8 == at.read_string(buf8, 8 + 1/*for NULL*/));
675+
676+
// *** Reading through partially matching stop tag ***
542677
at.clear_error();
678+
char table9[] = "\"s\"Oabcd\r\n\0";
679+
at.flush();
680+
filehandle_stub_table = table9;
681+
filehandle_stub_table_pos = 0;
682+
mbed_poll_stub::revents_value = POLLIN;
683+
mbed_poll_stub::int_value = 1;
684+
char buf9[5];
543685

544-
char table4[] = "\"s\"\0";
545-
filehandle_stub_table = table4;
686+
// NO prefix, NO OK, NO ERROR and NO URC match, CRLF found -> return so buffer could be read
687+
at.resp_start();
688+
// TO read from
689+
CHECK(6 == at.read_string(buf9, 6 + 1/*for NULL*/));
690+
691+
// *** CRLF part of the string ***
692+
at.clear_error();
693+
char table10[] = "\"s\"\r\nOK\r\n\0";
694+
at.flush();
695+
filehandle_stub_table = table10;
546696
filehandle_stub_table_pos = 0;
547697
mbed_poll_stub::revents_value = POLLIN;
548-
mbed_poll_stub::int_value = strlen(table);
698+
mbed_poll_stub::int_value = 1;
699+
char buf10[10];
549700

550-
at.resp_start("s");
551-
at.read_string(buf, 5, true);
701+
// NO prefix, NO OK, NO ERROR and NO URC match, CRLF found -> return so buffer could be read
702+
at.resp_start();
703+
// TO read from
704+
CHECK(3 == at.read_string(buf10, 9 + 1/*for NULL*/));
705+
}
552706

707+
void Test_ATHandler::test_ATHandler_read_hex_string()
708+
{
709+
EventQueue que;
710+
FileHandle_stub fh1;
553711
filehandle_stub_table = NULL;
554712
filehandle_stub_table_pos = 0;
555-
mbed_poll_stub::revents_value = POLLOUT;
556-
mbed_poll_stub::int_value = 0;
713+
714+
ATHandler at(&fh1, que, 0, ",");
715+
716+
// *** Read up to delimiter, even length ***
717+
at.clear_error();
718+
char table1[] = "68656C6C6F,";
719+
at.flush();
720+
filehandle_stub_table = table1;
721+
filehandle_stub_table_pos = 0;
722+
mbed_poll_stub::revents_value = POLLIN;
723+
mbed_poll_stub::int_value = 1;
724+
char buf1[10];
725+
// Set _stop_tag to resp_stop(OKCRLF)
726+
at.resp_start();
727+
CHECK(5 == at.read_hex_string(buf1, 5));
728+
CHECK(!strncmp(buf1, "hello", 5));
729+
730+
// *** Read up to delimiter, odd length ***
731+
at.clear_error();
732+
char table2[] = "68656C6C6F7,";
733+
at.flush();
734+
filehandle_stub_table = table2;
735+
filehandle_stub_table_pos = 0;
736+
mbed_poll_stub::revents_value = POLLIN;
737+
mbed_poll_stub::int_value = 1;
738+
char buf2[10];
739+
// Set _stop_tag to resp_stop(OKCRLF)
740+
at.resp_start();
741+
CHECK(5 == at.read_hex_string(buf2, 6));
742+
CHECK(!strncmp(buf2, "hello", 5));
743+
744+
// *** Read with stop tag, even length ***
745+
at.clear_error();
746+
char table3[] = "6865OK\r\n";
747+
at.flush();
748+
filehandle_stub_table = table3;
749+
filehandle_stub_table_pos = 0;
750+
mbed_poll_stub::revents_value = POLLIN;
751+
mbed_poll_stub::int_value = 1;
752+
char buf3[6];
753+
// Set _stop_tag to resp_stop(OKCRLF)
754+
at.resp_start();
755+
CHECK(2 == at.read_hex_string(buf3, 2 + 1/*get to stop tag match*/));
756+
CHECK(!strncmp(buf3, "he", 2));
757+
at.resp_stop();
758+
759+
// *** Read with stop tag, odd length ***
760+
at.clear_error();
761+
char table4[] = "686OK\r\n";
762+
at.flush();
763+
filehandle_stub_table = table4;
764+
filehandle_stub_table_pos = 0;
765+
mbed_poll_stub::revents_value = POLLIN;
766+
mbed_poll_stub::int_value = 1;
767+
char buf4[6];
768+
// Set _stop_tag to resp_stop(OKCRLF)
769+
at.resp_start();
770+
CHECK(1 == at.read_hex_string(buf4, 2 + 1/*get to stop tag match*/));
771+
CHECK(!strncmp(buf4, "h", 1));
557772
}
558773

559774
void Test_ATHandler::test_ATHandler_read_int()
560775
{
561776
EventQueue que;
562777
FileHandle_stub fh1;
778+
filehandle_stub_table = NULL;
779+
filehandle_stub_table_pos = 0;
563780

564781
ATHandler at(&fh1, que, 0, ",");
565782

features/cellular/UNITTESTS/at/athandler/test_athandler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ class Test_ATHandler
8080

8181
void test_ATHandler_read_string();
8282

83+
void test_ATHandler_read_hex_string();
84+
8385
void test_ATHandler_read_int();
8486

8587
void test_ATHandler_resp_start();

0 commit comments

Comments
 (0)