Skip to content
Snippets Groups Projects
Commit bb3ff4cd authored by Eric S. Raymond's avatar Eric S. Raymond
Browse files

TESTFRAME: First unit test for packet_dump().

parent 6bd4b034
No related branches found
No related tags found
No related merge requests found
......@@ -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 *);
/*
......
......@@ -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,
......
......@@ -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)
......
......@@ -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);
......
#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);
}
......@@ -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",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment