diff options
author | Eric S. Raymond <esr@thyrsus.com> | 2010-04-18 23:12:45 -0400 |
---|---|---|
committer | Eric S. Raymond <esr@thyrsus.com> | 2010-04-18 23:13:42 -0400 |
commit | 948770e120fe7028ed0f65c539b9dda129ee2a31 (patch) | |
tree | fdc61a97e48074cab72f9f8a8805904ef499ebf2 | |
parent | b7f64f8b840192601ac0b592bdee43e3eba6ebe9 (diff) | |
download | gpsd-948770e120fe7028ed0f65c539b9dda129ee2a31.tar.gz |
Add a regression test for correct behavior on EOF with empty buffer.
-rw-r--r-- | test/packet.test.chk | 9 | ||||
-rw-r--r-- | test_packet.c | 56 |
2 files changed, 57 insertions, 8 deletions
diff --git a/test/packet.test.chk b/test/packet.test.chk index 3c170be4..2f3c8bb0 100644 --- a/test/packet.test.chk +++ b/test/packet.test.chk @@ -1,4 +1,5 @@ - 1: NMEA packet with checksum (1) test succeeded. +=== Packet identification tests + === 1: NMEA packet with checksum (1) test succeeded. 2: NMEA packet with checksum (2) test succeeded. 3: NMEA packet with checksum and 4 chars of leading garbage test succeeded. 4: NMEA packet without checksum test succeeded. @@ -15,3 +16,9 @@ 15: EverMore packet 0x04, 0x10 three times at the beginning test succeeded. 16: RTCM104V3 type 1005 packet test succeeded. 17: RTCM104V3 type 1005 packet with 4th byte garbled test succeeded. +=== EOF with buffer nonempty test === +$GPVTG,308.74,T,,M,0.00,N,0.0,K*68
+$GPGGA,110534.994,4002.1425,N,07531.2585,W,0,00,50.0,172.7,M,-33.8,M,0.0,0000*7A
+packet_parse() returned 36 +packet_parse() returned 82 +packet_parse() returned 0 diff --git a/test_packet.c b/test_packet.c index f2b8a64c..d43f7b00 100644 --- a/test_packet.c +++ b/test_packet.c @@ -11,6 +11,9 @@ #ifndef S_SPLINT_S #include <unistd.h> #endif /* S_SPLINT_S */ +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> #include "gpsd.h" @@ -44,13 +47,13 @@ struct map /* *INDENT-OFF* */ /*@ -initallelements +charint -usedef @*/ -static struct map tests[] = { +static struct map singletests[] = { /* NMEA tests */ { .legend = "NMEA packet with checksum (1)", .test = "$GPVTG,308.74,T,,M,0.00,N,0.0,K*68\r\n", .testlen = 36, - 0, + .garbage_offset = 0, NMEA_PACKET, }, { @@ -238,6 +241,21 @@ static struct map tests[] = { /*@ +initallelements -charint +usedef @*/ /* *INDENT-ON* */ +/* *INDENT-OFF* */ +/*@ -initallelements +charint -usedef @*/ +static struct map runontests[] = { + /* NMEA tests */ + { + .legend = "Double NMEA packet with checksum", + .test = "$GPVTG,308.74,T,,M,0.00,N,0.0,K*68\r\n$GPGGA,110534.994,4002.1425,N,07531.2585,W,0,00,50.0,172.7,M,-33.8,M,0.0,0000*7A\r\n", + .testlen = 118, + 0, + NMEA_PACKET, + }, +}; +/*@ +initallelements -charint +usedef @*/ +/* *INDENT-ON* */ + static int packet_test(struct map *mp) { struct gps_packet_t packet; @@ -250,15 +268,15 @@ static int packet_test(struct map *mp) packet_parse(&packet); if (packet.type != mp->type) printf("%2zi: %s test FAILED (packet type %d wrong).\n", - mp - tests + 1, mp->legend, packet.type); + mp - singletests + 1, mp->legend, packet.type); else if (memcmp (mp->test + mp->garbage_offset, packet.outbuffer, packet.outbuflen)) { - printf("%2zi: %s test FAILED (data garbled).\n", mp - tests + 1, + printf("%2zi: %s test FAILED (data garbled).\n", mp - singletests + 1, mp->legend); ++failure; } else - printf("%2zi: %s test succeeded.\n", mp - tests + 1, mp->legend); + printf("%2zi: %s test succeeded.\n", mp - singletests + 1, mp->legend); #ifdef DUMPIT for (cp = packet.outbuffer; cp < packet.outbuffer + packet.outbuflen; cp++) { @@ -280,6 +298,25 @@ static int packet_test(struct map *mp) return failure; } +static void runon_test(struct map *mp) +{ + struct gps_packet_t packet; + int nullfd = open("/dev/null", O_RDONLY); + ssize_t st; + + packet_init(&packet); + /*@i@*/ memcpy(packet.inbufptr = packet.inbuffer, mp->test, mp->testlen); + packet.inbuflen = mp->testlen; + /*@ -compdef -uniondef -usedef -formatcode @*/ + (void)fputs(mp->test, stdout); + do { + st = packet_get(nullfd, &packet); + printf("packet_parse() returned %zd\n", st); + } while + (st > 0); + /*@ +compdef +uniondef +usedef +formatcode @*/ +} + int main(int argc, char *argv[]) { struct map *mp; @@ -299,9 +336,14 @@ int main(int argc, char *argv[]) } if (singletest) - failcount += packet_test(tests + singletest - 1); + failcount += packet_test(singletests + singletest - 1); else - for (mp = tests; mp < tests + sizeof(tests) / sizeof(tests[0]); mp++) + { + (void)fputs("=== Packet identification tests\n ===", stdout); + for (mp = singletests; mp < singletests + sizeof(singletests) / sizeof(singletests[0]); mp++) failcount += packet_test(mp); + (void)fputs("=== EOF with buffer nonempty test ===\n", stdout); + runon_test(&runontests[0]); + } exit(failcount > 0 ? 1 : 0); } |