From 4dc61015f57aed2ac4a382676354be8f64a6a0c5 Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Tue, 7 Jul 2009 10:17:33 +0200 Subject: [PATCH] Move pcap stuff into a separate file to allow sharing with future tests. --- tests/Makefile.am | 3 +- tests/check_lldp.c | 157 ++------------------------------------------- tests/common.c | 132 +++++++++++++++++++++++++++++++++++++ tests/common.h | 44 +++++++++++++ 4 files changed, 182 insertions(+), 154 deletions(-) create mode 100644 tests/common.c create mode 100644 tests/common.h diff --git a/tests/Makefile.am b/tests/Makefile.am index 567c8370..10a072cc 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -10,7 +10,8 @@ check_pack_CFLAGS = @CHECK_CFLAGS@ check_pack_LDADD = $(top_builddir)/src/liblldpd.la @CHECK_LIBS@ check_lldp_SOURCES = check_lldp.c \ - $(top_builddir)/src/lldpd.h + $(top_builddir)/src/lldpd.h \ + common.h common.c check_lldp_CFLAGS = @CHECK_CFLAGS@ check_lldp_LDADD = $(top_builddir)/src/liblldpd.la @CHECK_LIBS@ diff --git a/tests/check_lldp.c b/tests/check_lldp.c index 42ebb133..33cbf05e 100644 --- a/tests/check_lldp.c +++ b/tests/check_lldp.c @@ -1,162 +1,13 @@ #define _GNU_SOURCE 1 #include -#include -#include -#include -#include #include -#include #include -#include -#include +#include #include #include "../src/lldpd.h" +#include "common.h" -int dump = -1; -char *filename = NULL; -struct packet { - TAILQ_ENTRY(packet) next; - int size; - char data[]; -}; -TAILQ_HEAD(, packet) pkts; -char *buffer[] = { NULL }; -char macaddress[ETH_ALEN] = { 0x5e, 0x10, 0x8e, 0xe7, 0x84, 0xad }; -struct lldpd_hardware hardware; -struct lldpd_chassis chassis; - -/* See: - * http://wiki.wireshark.org/Development/LibpcapFileFormat - */ -struct pcap_hdr { - u_int32_t magic_number; /* magic number */ - u_int16_t version_major; /* major version number */ - u_int16_t version_minor; /* minor version number */ - u_int32_t thiszone; /* GMT to local correction */ - u_int32_t sigfigs; /* accuracy of timestamps */ - u_int32_t snaplen; /* max length of captured packets, in octets */ - u_int32_t network; /* data link type */ -}; -struct pcaprec_hdr { - u_int32_t ts_sec; /* timestamp seconds */ - u_int32_t ts_usec; /* timestamp microseconds */ - u_int32_t incl_len; /* number of octets of packet saved in file */ - u_int32_t orig_len; /* actual length of packet */ -}; - -int -pcap_send(struct lldpd *cfg, struct lldpd_hardware *hardware, - char *buffer, size_t size) -{ - struct pcaprec_hdr hdr; - struct packet *pkt; - int n; - - /* Write pcap record header */ - hdr.ts_sec = time(NULL); - hdr.ts_usec = 0; - hdr.incl_len = hdr.orig_len = size; - n = write(dump, &hdr, sizeof(hdr)); - if (n == 1) { - fail("unable to write pcap record header to %s", filename); - return -1; - } - - /* Write data */ - n = write(dump, buffer, size); - if (n == -1) { - fail("unable to write pcap data to %s", filename); - return -1; - } - - /* Append to list of packets */ - pkt = (struct packet *)malloc(size + sizeof(TAILQ_HEAD(,packet)) + sizeof(int)); - if (!pkt) { - fail("unable to allocate packet"); - return -1; - } - memcpy(pkt->data, buffer, size); - pkt->size = size; - TAILQ_INSERT_TAIL(&pkts, pkt, next); - return 0; -} - -struct lldpd_ops fake_ops = { - .send = pcap_send, - .recv = NULL, /* Won't be used */ - .cleanup = NULL, /* Won't be used */ -}; - - -void -setup() -{ - static int serial = 0; - struct pcap_hdr hdr; - int n; - /* Prepare packet buffer */ - TAILQ_INIT(&pkts); - /* Open a new dump file */ - n = asprintf(&filename, "lldp_send_%04d.pcap", serial++); - if (n == -1) { - fail("unable to compute filename"); - return; - } - dump = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644); - if (dump == -1) { - fail("unable to open %s", filename); - return; - } - /* Write a PCAP header */ - hdr.magic_number = 0xa1b2c3d4; - hdr.version_major = 2; - hdr.version_minor = 4; - hdr.thiszone = 0; - hdr.sigfigs = 0; - hdr.snaplen = 65535; - hdr.network = 1; - n = write(dump, &hdr, sizeof(hdr)); - if (n == -1) { - fail("unable to write pcap header to %s", filename); - return; - } - /* Prepare hardware */ - memset(&hardware, 0, sizeof(struct lldpd_hardware)); - TAILQ_INIT(&hardware.h_rports); -#ifdef ENABLE_DOT1 - TAILQ_INIT(&hardware.h_lport.p_vlans); -#endif - hardware.h_mtu = 1500; - hardware.h_ifindex = 1; - strcpy(hardware.h_ifname, "test"); - memcpy(hardware.h_lladdr, macaddress, ETH_ALEN); - hardware.h_ops = &fake_ops; - /* Prepare chassis */ - memset(&chassis, 0, sizeof(struct lldpd_chassis)); - hardware.h_lport.p_chassis = &chassis; - chassis.c_ttl = 180; -} - -void -teardown() -{ - struct packet *npkt, *pkt; - for (pkt = TAILQ_FIRST(&pkts); - pkt != NULL; - pkt = npkt) { - npkt = TAILQ_NEXT(pkt, next); - TAILQ_REMOVE(&pkts, pkt, next); - free(pkt); - } - if (dump != -1) { - close(dump); - dump = -1; - } - if (filename) { - free(filename); - filename = NULL; - } -} +char filenameprefix[] = "lldp_send"; START_TEST (test_send_basic) { @@ -1572,7 +1423,7 @@ lldp_suite(void) */ TCase *tc_send = tcase_create("Send LLDP packets"); - tcase_add_checked_fixture(tc_send, setup, teardown); + tcase_add_checked_fixture(tc_send, pcap_setup, pcap_teardown); tcase_add_test(tc_send, test_send_basic); #ifdef ENABLE_DOT1 tcase_add_test(tc_send, test_send_vlan); diff --git a/tests/common.c b/tests/common.c new file mode 100644 index 00000000..91eb811f --- /dev/null +++ b/tests/common.c @@ -0,0 +1,132 @@ +#define _GNU_SOURCE 1 +#include +#include +#include +#include +#include +#include +#include +#include +#include "../src/lldpd.h" +#include "common.h" + +int dump = -1; +char *filename = NULL; +struct pkts_t pkts; +char macaddress[ETH_ALEN] = { 0x5e, 0x10, 0x8e, 0xe7, 0x84, 0xad }; +struct lldpd_hardware hardware; +struct lldpd_chassis chassis; + +int +pcap_send(struct lldpd *cfg, struct lldpd_hardware *hardware, + char *buffer, size_t size) +{ + struct pcaprec_hdr hdr; + struct packet *pkt; + int n; + + /* Write pcap record header */ + hdr.ts_sec = time(NULL); + hdr.ts_usec = 0; + hdr.incl_len = hdr.orig_len = size; + n = write(dump, &hdr, sizeof(hdr)); + if (n == 1) { + fail("unable to write pcap record header to %s", filename); + return -1; + } + + /* Write data */ + n = write(dump, buffer, size); + if (n == -1) { + fail("unable to write pcap data to %s", filename); + return -1; + } + + /* Append to list of packets */ + pkt = (struct packet *)malloc(size + sizeof(TAILQ_HEAD(,packet)) + sizeof(int)); + if (!pkt) { + fail("unable to allocate packet"); + return -1; + } + memcpy(pkt->data, buffer, size); + pkt->size = size; + TAILQ_INSERT_TAIL(&pkts, pkt, next); + return 0; +} + +struct lldpd_ops pcap_ops = { + .send = pcap_send, + .recv = NULL, /* Won't be used */ + .cleanup = NULL, /* Won't be used */ +}; + + +void +pcap_setup() +{ + static int serial = 0; + struct pcap_hdr hdr; + int n; + /* Prepare packet buffer */ + TAILQ_INIT(&pkts); + /* Open a new dump file */ + n = asprintf(&filename, "%s_%04d.pcap", filenameprefix, serial++); + if (n == -1) { + fail("unable to compute filename"); + return; + } + dump = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644); + if (dump == -1) { + fail("unable to open %s", filename); + return; + } + /* Write a PCAP header */ + hdr.magic_number = 0xa1b2c3d4; + hdr.version_major = 2; + hdr.version_minor = 4; + hdr.thiszone = 0; + hdr.sigfigs = 0; + hdr.snaplen = 65535; + hdr.network = 1; + n = write(dump, &hdr, sizeof(hdr)); + if (n == -1) { + fail("unable to write pcap header to %s", filename); + return; + } + /* Prepare hardware */ + memset(&hardware, 0, sizeof(struct lldpd_hardware)); + TAILQ_INIT(&hardware.h_rports); +#ifdef ENABLE_DOT1 + TAILQ_INIT(&hardware.h_lport.p_vlans); +#endif + hardware.h_mtu = 1500; + hardware.h_ifindex = 1; + strcpy(hardware.h_ifname, "test"); + memcpy(hardware.h_lladdr, macaddress, ETH_ALEN); + hardware.h_ops = &pcap_ops; + /* Prepare chassis */ + memset(&chassis, 0, sizeof(struct lldpd_chassis)); + hardware.h_lport.p_chassis = &chassis; + chassis.c_ttl = 180; +} + +void +pcap_teardown() +{ + struct packet *npkt, *pkt; + for (pkt = TAILQ_FIRST(&pkts); + pkt != NULL; + pkt = npkt) { + npkt = TAILQ_NEXT(pkt, next); + TAILQ_REMOVE(&pkts, pkt, next); + free(pkt); + } + if (dump != -1) { + close(dump); + dump = -1; + } + if (filename) { + free(filename); + filename = NULL; + } +} diff --git a/tests/common.h b/tests/common.h new file mode 100644 index 00000000..48cbf251 --- /dev/null +++ b/tests/common.h @@ -0,0 +1,44 @@ +#ifndef _COMMON_H +#define _COMMON_H + +#include "../src/lldpd.h" + +/* See: + * http://wiki.wireshark.org/Development/LibpcapFileFormat + */ +struct pcap_hdr { + u_int32_t magic_number; /* magic number */ + u_int16_t version_major; /* major version number */ + u_int16_t version_minor; /* minor version number */ + u_int32_t thiszone; /* GMT to local correction */ + u_int32_t sigfigs; /* accuracy of timestamps */ + u_int32_t snaplen; /* max length of captured packets, in octets */ + u_int32_t network; /* data link type */ +}; +struct pcaprec_hdr { + u_int32_t ts_sec; /* timestamp seconds */ + u_int32_t ts_usec; /* timestamp microseconds */ + u_int32_t incl_len; /* number of octets of packet saved in file */ + u_int32_t orig_len; /* actual length of packet */ +}; + +struct packet { + TAILQ_ENTRY(packet) next; + int size; + char data[]; +}; +TAILQ_HEAD(pkts_t, packet); + +extern int dump; /* Dump file descriptor in pcap format */ +extern char filenameprefix[]; /* Prefix for filename dumping */ +extern char *filename; /* Filename we are dumping to */ +extern char macaddress[]; /* MAC address we use to send */ +extern struct pkts_t pkts; /* List of sent packets */ +extern struct lldpd_hardware hardware; +extern struct lldpd_chassis chassis; + +int pcap_send(struct lldpd *, struct lldpd_hardware *, char *, size_t); +void pcap_setup(); +void pcap_teardown(); + +#endif -- 2.39.5