summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Habets <thomas@habets.se>2012-09-23 23:53:00 +0100
committerThomas Habets <thomas@habets.se>2015-04-22 20:21:58 +0100
commitc7e5d01dcde34e844a87cb6c488e289c58005d78 (patch)
tree65872849a7e8c94cf5c3919cd62be097de63785c
parent4f3d75e9a70023bf45ba810dcba5673475e29d98 (diff)
downloadarping-c7e5d01dcde34e844a87cb6c488e289c58005d78.tar.gz
Initial unit test.
-rw-r--r--src/Makefile.am7
-rw-r--r--src/arping.c17
-rw-r--r--src/arping.h17
-rw-r--r--src/arping_main.c38
-rw-r--r--src/arping_test.c113
-rw-r--r--src/mock_libpcap.c37
6 files changed, 219 insertions, 10 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index 8eaa519..c0ad7e9 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -3,6 +3,11 @@
include $(top_srcdir)/Makefile.am.common
sbin_PROGRAMS = arping
-arping_SOURCES = arping.c unix.c
+arping_SOURCES = arping.c arping_main.c unix.c
arping_LDADD = $(LIBOBJS)
+
+TESTS=arping_test
+check_PROGRAMS=arping_test
+arping_test_SOURCES=arping.c arping_test.c unix.c mock_libpcap.c findif_linux.c
+arping_test_LDADD=-lcheck -lpthread
diff --git a/src/arping.c b/src/arping.c
index ec2f440..b073f37 100644
--- a/src/arping.c
+++ b/src/arping.c
@@ -15,7 +15,7 @@
*
*/
/*
- * Copyright (C) 2000-2014 Thomas Habets <thomas@habets.se>
+ * Copyright (C) 2000-2015 Thomas Habets <thomas@habets.se>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -143,7 +143,7 @@ static char *target = "huh? bug in arping?";
* Ping IP mode: cmdline target
* Ping MAC mode: 255.255.255.255, override with -T
*/
-static uint32_t dstip;
+uint32_t dstip;
/*
* Ping IP mode: ethxmas, override with -t
@@ -151,7 +151,7 @@ static uint32_t dstip;
*/
static uint8_t dstmac[ETH_ALEN];
-static uint32_t srcip; /* autodetected, override with -S/-b/-0 */
+uint32_t srcip; /* autodetected, override with -S/-b/-0 */
static uint8_t srcmac[ETH_ALEN]; /* autodetected, override with -s */
static int16_t vlan_tag = -1; /* 802.1Q tag to add to packets. -V */
@@ -168,8 +168,8 @@ static int finddup = 0; /* finddup mode. -d */
static int dupfound = 0; /* set to 1 if dup found */
static char lastreplymac[ETH_ALEN]; /* if last different from this then dup */
-static unsigned int numsent = 0; /* packets sent */
-static unsigned int numrecvd = 0; /* packets received */
+unsigned int numsent = 0; /* packets sent */
+unsigned int numrecvd = 0; /* packets received */
static unsigned int max_replies = UINT_MAX; /* exit after -C replies */
static unsigned int numdots = 0; /* dots that should be printed */
static const char* timestamp_type = NULL; /* Incoming packet measurement ts type (-m) */
@@ -960,7 +960,7 @@ pingip_send()
* \param h packet metadata
* \param packet packet data
*/
-static void
+void
pingip_recv(const char *unused, struct pcap_pkthdr *h, uint8_t *packet)
{
const unsigned char *pkt_srcmac;
@@ -1095,7 +1095,7 @@ pingip_recv(const char *unused, struct pcap_pkthdr *h, uint8_t *packet)
* \param h packet metadata
* \param packet packet data
*/
-static void
+void
pingmac_recv(const char *unused, struct pcap_pkthdr *h, uint8_t *packet)
{
const unsigned char *pkt_dstmac;
@@ -1346,7 +1346,8 @@ ping_recv(pcap_t *pcap, uint32_t packetwait, pcap_handler func)
/**
*
*/
-int main(int argc, char **argv)
+int
+arping_main(int argc, char **argv)
{
char ebuf[LIBNET_ERRBUF_SIZE + PCAP_ERRBUF_SIZE];
char *cp;
diff --git a/src/arping.h b/src/arping.h
index cb3f225..91ee42d 100644
--- a/src/arping.h
+++ b/src/arping.h
@@ -1,6 +1,6 @@
/* arping/src/arping.h
*
- * Copyright (C) 2000-2011 Thomas Habets <thomas@habets.se>
+ * Copyright (C) 2000-2015 Thomas Habets <thomas@habets.se>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -25,9 +25,24 @@
#include <inttypes.h>
#endif
+/* Forward declarations */
+struct pcap_pkthdr;
+
extern int verbose;
+extern uint32_t srcip;
+extern uint32_t dstip;
+extern unsigned int numrecvd;
+extern unsigned int numsent;
+
+const char *
+arping_lookupdev(uint32_t srcip, uint32_t dstip, char *ebuf);
void do_signal_init();
void do_libnet_init(const char *ifname, int recursive);
void sigint(int);
const char *arping_lookupdev_default(uint32_t srcip, uint32_t dstip,
char *ebuf);
+int arping_main(int argc, char **argv);
+
+
+void pingip_recv(const char *unused, struct pcap_pkthdr *h, uint8_t *packet);
+void pingmac_recv(const char *unused, struct pcap_pkthdr *h, uint8_t *packet);
diff --git a/src/arping_main.c b/src/arping_main.c
new file mode 100644
index 0000000..c6a04a3
--- /dev/null
+++ b/src/arping_main.c
@@ -0,0 +1,38 @@
+/* arping/src/arping_main.c
+ *
+ * Copyright (C) 2015 Thomas Habets <thomas@habets.se>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include"arping.h"
+
+/**
+ * main() wrapping arping_main() for testing purposes.
+ */
+int
+main(int argc, char **argv)
+{
+ return arping_main(argc, argv);
+}
+/* ---- Emacs Variables ----
+ * Local Variables:
+ * c-basic-offset: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/arping_test.c b/src/arping_test.c
new file mode 100644
index 0000000..a09e80b
--- /dev/null
+++ b/src/arping_test.c
@@ -0,0 +1,113 @@
+/* arping/src/arping_test.c
+ *
+ * Copyright (C) 2015 Thomas Habets <thomas@habets.se>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+#include<inttypes.h>
+#include<stdlib.h>
+
+#include<check.h>
+#include<pcap.h>
+
+#include"arping.h"
+
+static int numtests = 0;
+static void* test_registry[1024];
+
+#define MYTEST(a) static void a(int);__attribute__((constructor)) static void cons_##a() { test_registry[numtests++] = a;} START_TEST(a)
+
+// Received uninteresting packet, should not record anything.
+MYTEST(pingip_uninteresting_packet)
+{
+ struct pcap_pkthdr pkthdr;
+ uint8_t packet[128];
+
+ int prev_numrecvd = numrecvd;
+
+ pingip_recv(NULL, &pkthdr, packet);
+
+ fail_unless(prev_numrecvd == numrecvd);
+} END_TEST
+
+// Received reply that actually matches. Things should happen.
+MYTEST(pingip_interesting_packet)
+{
+ struct pcap_pkthdr pkthdr;
+ uint8_t packet[] = {
+ 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, // dst
+ 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, // src
+ 0x00, 0x00, // type
+ 0x00, 0x01, // hardware
+ 0x08, 0x00, // protocol
+ 0x06, 0x04, // lengths
+ 0x00, 0x02, // operator
+
+ 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, // sender
+ 0x12, 0x34, 0x56, 0x78, // sender protocol address
+
+ 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, // receiver
+ 0x87, 0x65, 0x43, 0x21, // receiver protocol address
+
+ 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x6f, 0xa8, 0x58, 0x63,
+ };
+ int prev_numrecvd = numrecvd;
+
+ dstip = htonl(0x12345678);
+
+ pingip_recv(NULL, &pkthdr, packet);
+ fail_unless(numrecvd == prev_numrecvd + 1,
+ "numrecvd not incremented");
+
+ pingip_recv(NULL, &pkthdr, packet);
+ fail_unless(numrecvd == prev_numrecvd + 2,
+ "numrecvd not incremented second time");
+} END_TEST
+
+static Suite*
+arping_suite(void)
+{
+ int c;
+ Suite* s = suite_create("Arping");
+
+ /* Core test case */
+ //tcase_add_checked_fixture (tc_core, setup, teardown);
+ TCase *tc_core = tcase_create("Receiving");
+ for (c = 0; c < numtests; c++) {
+ tcase_add_test(tc_core, test_registry[c]);
+ }
+ suite_add_tcase(s, tc_core);
+ return s;
+}
+
+int
+main()
+{
+ int number_failed;
+ Suite *s = arping_suite();
+ SRunner *sr = srunner_create (s);
+ srunner_run_all (sr, CK_NORMAL);
+ number_failed = srunner_ntests_failed (sr);
+ srunner_free (sr);
+ return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
+}
+/* ---- Emacs Variables ----
+ * Local Variables:
+ * c-basic-offset: 8
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/mock_libpcap.c b/src/mock_libpcap.c
new file mode 100644
index 0000000..f30f482
--- /dev/null
+++ b/src/mock_libpcap.c
@@ -0,0 +1,37 @@
+#include<pcap.h>
+
+int
+pcap_setfilter(pcap_t *pcap, struct bpf_program *prog)
+{
+}
+
+int
+pcap_dispatch(pcap_t *pcap, int num, pcap_handler handler, u_char *packet)
+{
+}
+
+int
+pcap_compile(pcap_t *pcap, struct bpf_program *prog, const char *x, int y,
+ bpf_u_int32 z)
+{
+}
+
+pcap_t*
+pcap_open_live(const char *ifname, int a, int b, int c, char *d)
+{
+}
+
+int
+pcap_setnonblock(pcap_t *pcap, int a, char *b)
+{
+}
+
+int
+pcap_get_selectable_fd(pcap_t *pcap)
+{
+}
+
+char*
+pcap_lookupdev(char *ifname)
+{
+}