]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
Move pcap stuff into a separate file to allow sharing with future tests.
authorVincent Bernat <bernat@luffy.cx>
Tue, 7 Jul 2009 08:17:33 +0000 (10:17 +0200)
committerVincent Bernat <bernat@luffy.cx>
Tue, 7 Jul 2009 13:23:39 +0000 (15:23 +0200)
tests/Makefile.am
tests/check_lldp.c
tests/common.c [new file with mode: 0644]
tests/common.h [new file with mode: 0644]

index 567c8370157220d9681f44e1224cbd8df612bd34..10a072cc2e5a86d7e38bc438e307acf8c5bc81d4 100644 (file)
@@ -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@
 
index 42ebb13352b148895ef73827e6c28c6966e177a0..33cbf05eec20d406809df674fb62682b08bf32df 100644 (file)
 #define _GNU_SOURCE 1
 #include <stdlib.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/stat.h>
 #include <sys/socket.h>
-#include <netinet/in.h>
 #include <arpa/inet.h>
-#include <fcntl.h>
-#include <time.h>
+#include <netinet/in.h>
 #include <check.h>
 #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 (file)
index 0000000..91eb811
--- /dev/null
@@ -0,0 +1,132 @@
+#define _GNU_SOURCE 1
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <time.h>
+#include <fcntl.h>
+#include <check.h>
+#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 (file)
index 0000000..48cbf25
--- /dev/null
@@ -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