[Git][NTPsec/ntpsec][master] TESTFRAME: First unit test for packet_dump().

Eric S. Raymond gitlab at mg.gitlab.com
Thu Sep 15 19:09:45 UTC 2016


Eric S. Raymond pushed to branch master at NTPsec / ntpsec


Commits:
bb3ff4cd by Eric S. Raymond at 2016-09-15T15:09:06-04:00
TESTFRAME: First unit test for packet_dump().

- - - - -


6 changed files:

- include/ntpd.h
- libntp/pktvis.c
- ntpd/ntp_intercept.c
- tests/common/tests_main.c
- + tests/libntp/ntpvis.c
- tests/wscript


Changes:

=====================================
include/ntpd.h
=====================================
--- a/include/ntpd.h
+++ b/include/ntpd.h
@@ -240,7 +240,7 @@ extern	void	record_timing_stats (const char *);
 extern	char *	fstostr(time_t);	/* NTP timescale seconds */
 
 /* ntpvis.c */
-void packet_dump(char *, size_t, sockaddr_u *, struct pkt *, size_t);
+void packet_dump(char *, size_t, struct pkt *, size_t);
 size_t packet_undump(char *, int len, char *);
 
 /*


=====================================
libntp/pktvis.c
=====================================
--- a/libntp/pktvis.c
+++ b/libntp/pktvis.c
@@ -31,18 +31,14 @@ static char *lfpdump(l_fp *fp)
     return buf;
 }
 
-void packet_dump(char *buf, size_t buflen,
-			sockaddr_u *dest, struct pkt *pkt, size_t len)
+void packet_dump(char *buf, size_t buflen, struct pkt *pkt, size_t len)
 {
     size_t i;
     /*
-     * Format is three tokens: source address, packet, MAC token. 
-     *
      * FIXME: struct pkt fields are in network byte order. Need to
-     * add htonl()/ntohl() calls here.
+     * add htonl()/ntohl() calls here  for comprehensibility.
      */
-    snprintf(buf, buflen, "%s %d:%d:%d:%d:%u:%u:%u:%s:%s:%s:%s ",
-	   socktoa(dest),
+    snprintf(buf, buflen, "%d:%d:%d:%d:%u:%u:%u:%s:%s:%s:%s:",
 	   pkt->li_vn_mode, pkt->stratum, pkt->ppoll, pkt->precision,
 	   pkt->rootdelay, pkt->rootdisp,
 	   pkt->refid,


=====================================
ntpd/ntp_intercept.c
=====================================
--- a/ntpd/ntp_intercept.c
+++ b/ntpd/ntp_intercept.c
@@ -661,7 +661,11 @@ void intercept_sendpkt(const char *legend,
       return;
     }
 
-    packet_dump(pkt_dump, sizeof(pkt_dump), dest, (struct pkt*)pkt, len);
+    strlcpy(pkt_dump, socktoa(dest), sizeof(pkt_dump));
+    strlcat(pkt_dump, " ", sizeof(pkt_dump));
+    packet_dump(pkt_dump + strlen(pkt_dump), sizeof(pkt_dump) - strlen(pkt_dump),
+		(struct pkt*)pkt, len);
+    strlcat(pkt_dump, " ", sizeof(pkt_dump));
     snprintf(newpacket, sizeof(newpacket), "sendpkt %s %s\n", legend, pkt_dump);
 
     if (mode == replay)


=====================================
tests/common/tests_main.c
=====================================
--- a/tests/common/tests_main.c
+++ b/tests/common/tests_main.c
@@ -52,6 +52,7 @@ static void RunAllTests(void)
 	RUN_TEST_GROUP(modetoa);
 	RUN_TEST_GROUP(msyslog);
 	RUN_TEST_GROUP(netof);
+	RUN_TEST_GROUP(ntpvis);
 	RUN_TEST_GROUP(numtoa);
 	RUN_TEST_GROUP(numtohost);
 	RUN_TEST_GROUP(prettydate);


=====================================
tests/libntp/ntpvis.c
=====================================
--- /dev/null
+++ b/tests/libntp/ntpvis.c
@@ -0,0 +1,42 @@
+#include "config.h"
+#include "ntp_stdlib.h"
+
+#include "unity.h"
+#include "unity_fixture.h"
+
+TEST_GROUP(ntpvis);
+
+TEST_SETUP(ntpvis) {}
+
+TEST_TEAR_DOWN(ntpvis) {}
+
+#include "ntpd.h"
+
+static struct pkt ExamplePacket1 = {
+    .li_vn_mode = 6,
+    .stratum = 2,
+    .ppoll = 3,
+    .precision = -21,
+    .rootdelay = 0,
+    .rootdisp = 0,
+    .refid = 0x47506300,	/* big-endian 'GPS' */
+#ifdef __unused__
+    .reftime = 0,
+    .org = 0,
+    .rec = 0,
+    .xmt = 0,
+#endif
+    .exten = {0},
+};
+static char *ExampleDump1 = "6:2:3:-21:0:0:1196450560:0:0:0:0:nomac";
+
+TEST(ntpvis, PacketDump) {
+    char buf[BUFSIZ];
+
+    packet_dump(buf, sizeof(buf), &ExamplePacket1, LEN_PKT_NOMAC);
+    TEST_ASSERT_TRUE(strcmp(buf, ExampleDump1) == 0);
+}
+
+TEST_GROUP_RUNNER(ntpvis) {
+    RUN_TEST_CASE(ntpvis, PacketDump);
+}


=====================================
tests/wscript
=====================================
--- a/tests/wscript
+++ b/tests/wscript
@@ -67,6 +67,7 @@ def build(ctx):
 		"libntp/modetoa.c",
 		"libntp/msyslog.c",
 		"libntp/netof.c",
+		"libntp/ntpvis.c",
 		"libntp/numtoa.c",
 		"libntp/numtohost.c",
 		"libntp/prettydate.c",



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/bb3ff4cd7ba3135fcd7eea8e8c56cbac28c0c816
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ntpsec.org/pipermail/vc/attachments/20160915/d4c0c96f/attachment.html>


More information about the vc mailing list