From: Vincent Bernat Date: Tue, 7 Jul 2009 12:08:06 +0000 (+0200) Subject: Add SONMP unit tests. X-Git-Tag: 0.5.0~45 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=5aa86a41befa6e158a5c7f714670369f1f43d1f2;p=thirdparty%2Flldpd.git Add SONMP unit tests. --- diff --git a/tests/Makefile.am b/tests/Makefile.am index c6940c98..6efee3c8 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,4 +1,4 @@ -TESTS = check_pack check_lldp check_cdp +TESTS = check_pack check_lldp check_cdp check_sonmp if HAVE_CHECK @@ -21,10 +21,17 @@ check_cdp_SOURCES = check_cdp.c \ check_cdp_CFLAGS = @CHECK_CFLAGS@ check_cdp_LDADD = $(top_builddir)/src/liblldpd.la @CHECK_LIBS@ +check_sonmp_SOURCES = check_sonmp.c \ + $(top_builddir)/src/lldpd.h \ + common.h common.c +check_sonmp_CFLAGS = @CHECK_CFLAGS@ +check_sonmp_LDADD = $(top_builddir)/src/liblldpd.la @CHECK_LIBS@ + if USE_SNMP check_pack_LDADD += @NETSNMP_LIB@ check_lldp_LDADD += @NETSNMP_LIB@ check_cdp_LDADD += @NETSNMP_LIB@ +check_sonmp_LDADD += @NETSNMP_LIB@ endif endif diff --git a/tests/check_sonmp.c b/tests/check_sonmp.c new file mode 100644 index 00000000..9f17dd95 --- /dev/null +++ b/tests/check_sonmp.c @@ -0,0 +1,211 @@ +#define _GNU_SOURCE 1 +#include +#include +#include +#include +#include +#include "../src/lldpd.h" +#include "common.h" + +char filenameprefix[] = "sonmp_send"; + +#ifdef ENABLE_SONMP + +START_TEST (test_send_sonmp) +{ + int n; + /* Packet we should build: +IEEE 802.3 Ethernet + Destination: Bay-Networks-(Synoptics)-autodiscovery (01:00:81:00:01:00) + Source: 5e:10:8e:e7:84:ad (5e:10:8e:e7:84:ad) + Length: 22 +Logical-Link Control + DSAP: SNAP (0xaa) + IG Bit: Individual + SSAP: SNAP (0xaa) + CR Bit: Command + Control field: U, func=UI (0x03) + 000. 00.. = Command: Unnumbered Information (0x00) + .... ..11 = Frame type: Unnumbered frame (0x03) + Organization Code: Nortel Networks SONMP (0x000081) + PID: SONMP segment hello (0x01a2) +Nortel Networks / SynOptics Network Management Protocol + NMM IP address: 172.17.142.37 (172.17.142.37) + Segment Identifier: 0x000004 + Chassis type: Unknown (1) + Backplane type: ethernet, fast ethernet and gigabit ethernet (12) + NMM state: New (3) + Number of links: 1 + +IEEE 802.3 Ethernet + Destination: Bay-Networks-(Synoptics)-autodiscovery (01:00:81:00:01:01) + Source: 5e:10:8e:e7:84:ad (5e:10:8e:e7:84:ad) + Length: 22 +Logical-Link Control + DSAP: SNAP (0xaa) + IG Bit: Individual + SSAP: SNAP (0xaa) + CR Bit: Command + Control field: U, func=UI (0x03) + 000. 00.. = Command: Unnumbered Information (0x00) + .... ..11 = Frame type: Unnumbered frame (0x03) + Organization Code: Nortel Networks SONMP (0x000081) + PID: SONMP flatnet hello (0x01a1) +Nortel Networks / SynOptics Network Management Protocol + NMM IP address: 172.17.142.37 (172.17.142.37) + Segment Identifier: 0x000004 + Chassis type: Unknown (1) + Backplane type: ethernet, fast ethernet and gigabit ethernet (12) + NMM state: New (3) + Number of links: 1 + */ + char pkt1[] = { + 0x01, 0x00, 0x81, 0x00, 0x01, 0x00, 0x5e, 0x10, + 0x8e, 0xe7, 0x84, 0xad, 0x00, 0x16, 0xaa, 0xaa, + 0x03, 0x00, 0x00, 0x81, 0x01, 0xa2, 0xac, 0x11, + 0x8e, 0x25, 0x00, 0x00, 0x04, 0x01, 0x0c, 0x03, + 0x01 }; + char pkt2[] = { + 0x01, 0x00, 0x81, 0x00, 0x01, 0x01, 0x5e, 0x10, + 0x8e, 0xe7, 0x84, 0xad, 0x00, 0x16, 0xaa, 0xaa, + 0x03, 0x00, 0x00, 0x81, 0x01, 0xa1, 0xac, 0x11, + 0x8e, 0x25, 0x00, 0x00, 0x04, 0x01, 0x0c, 0x03, + 0x01 }; + struct packet *pkt; + + /* Populate port and chassis */ + hardware.h_lport.p_id_subtype = LLDP_PORTID_SUBTYPE_IFNAME; + hardware.h_lport.p_id = "Not used"; + hardware.h_lport.p_id_len = strlen(hardware.h_lport.p_id); + chassis.c_id_subtype = LLDP_CHASSISID_SUBTYPE_LLADDR; + chassis.c_id = macaddress; + chassis.c_id_len = ETH_ALEN; + chassis.c_mgmt.s_addr = inet_addr("172.17.142.37"); + + /* Build packet */ + n = sonmp_send(NULL, &hardware); + if (n != 0) { + fail("unable to build packet"); + return; + } + if (TAILQ_EMPTY(&pkts)) { + fail("no packets sent"); + return; + } + pkt = TAILQ_FIRST(&pkts); + ck_assert_int_eq(pkt->size, sizeof(pkt1)); + fail_unless(memcmp(pkt->data, pkt1, sizeof(pkt1)) == 0); + pkt = TAILQ_NEXT(pkt, next); + if (!pkt) { + fail("need one more packet"); + return; + } + ck_assert_int_eq(pkt->size, sizeof(pkt2)); + fail_unless(memcmp(pkt->data, pkt2, sizeof(pkt2)) == 0); + fail_unless(TAILQ_NEXT(pkt, next) == NULL, "more than two packets sent"); +} +END_TEST + +START_TEST (test_recv_sonmp) +{ + char pkt1[] = { + 0x01, 0x00, 0x81, 0x00, 0x01, 0x00, 0x00, 0x1b, + 0x25, 0x08, 0x50, 0x47, 0x00, 0x13, 0xaa, 0xaa, + 0x03, 0x00, 0x00, 0x81, 0x01, 0xa2, 0xac, 0x10, + 0x65, 0xa8, 0x00, 0x02, 0x08, 0x38, 0x0c, 0x02, + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00 }; + /* This is: +IEEE 802.3 Ethernet + Destination: Bay-Networks-(Synoptics)-autodiscovery (01:00:81:00:01:00) + Source: Nortel_08:50:47 (00:1b:25:08:50:47) + Length: 19 + Trailer: 000000000000000000000000000000000000000000000000... +Logical-Link Control + DSAP: SNAP (0xaa) + IG Bit: Individual + SSAP: SNAP (0xaa) + CR Bit: Command + Control field: U, func=UI (0x03) + 000. 00.. = Command: Unnumbered Information (0x00) + .... ..11 = Frame type: Unnumbered frame (0x03) + Organization Code: Nortel Networks SONMP (0x000081) + PID: SONMP segment hello (0x01a2) +Nortel Networks / SynOptics Network Management Protocol + NMM IP address: 172.16.101.168 (172.16.101.168) + Segment Identifier: 0x000208 + Chassis type: Accelar 8610 L3 switch (56) + Backplane type: ethernet, fast ethernet and gigabit ethernet (12) + NMM state: Heartbeat (2) + Number of links: 1 + */ + struct lldpd_chassis *nchassis = NULL; + struct lldpd_port *nport = NULL; + char cid[5]; + in_addr_t ip; + + fail_unless(sonmp_decode(NULL, pkt1, sizeof(pkt1), &hardware, + &nchassis, &nport) != -1); + if (!nchassis || !nport) { + fail("unable to decode packet"); + return; + } + ck_assert_int_eq(nchassis->c_id_subtype, + LLDP_CHASSISID_SUBTYPE_ADDR); + ck_assert_int_eq(nchassis->c_id_len, 5); + cid[0] = 1; + ip = inet_addr("172.16.101.168"); + memcpy(cid + 1, &ip, sizeof(in_addr_t)); + fail_unless(memcmp(nchassis->c_id, cid, 5) == 0); + ck_assert_str_eq(nchassis->c_name, "172.16.101.168"); + ck_assert_str_eq(nchassis->c_descr, "Nortel Ethernet Routing 8610 L3 Switch"); + ck_assert_int_eq(nchassis->c_mgmt.s_addr, + (u_int32_t)inet_addr("172.16.101.168")); + ck_assert_int_eq(nchassis->c_mgmt_if, 0); + ck_assert_str_eq(nport->p_descr, "port 2/8"); + ck_assert_int_eq(nport->p_id_subtype, + LLDP_PORTID_SUBTYPE_LOCAL); + ck_assert_int_eq(nport->p_id_len, strlen("00-02-08")); + fail_unless(memcmp(nport->p_id, + "00-02-08", strlen("00-02-08")) == 0); + ck_assert_int_eq(nchassis->c_cap_enabled, 0); +} +END_TEST + +#endif + +Suite * +sonmp_suite(void) +{ + Suite *s = suite_create("SONMP"); + +#ifdef ENABLE_SONMP + TCase *tc_send = tcase_create("Send SONMP packets"); + tcase_add_checked_fixture(tc_send, pcap_setup, pcap_teardown); + tcase_add_test(tc_send, test_send_sonmp); + suite_add_tcase(s, tc_send); + + TCase *tc_receive = tcase_create("Receive SONMP packets"); + tcase_add_test(tc_receive, test_recv_sonmp); + suite_add_tcase(s, tc_receive); +#endif + + return s; +} + +int +main() +{ + int number_failed; + Suite *s = sonmp_suite (); + SRunner *sr = srunner_create (s); + srunner_set_fork_status (sr, CK_NOFORK); /* Can't fork because + we need to write + files */ + srunner_run_all (sr, CK_ENV); + number_failed = srunner_ntests_failed (sr); + srunner_free (sr); + return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; +} diff --git a/tests/common.c b/tests/common.c index 91eb811f..af4ef67e 100644 --- a/tests/common.c +++ b/tests/common.c @@ -100,7 +100,7 @@ pcap_setup() TAILQ_INIT(&hardware.h_lport.p_vlans); #endif hardware.h_mtu = 1500; - hardware.h_ifindex = 1; + hardware.h_ifindex = 4; strcpy(hardware.h_ifname, "test"); memcpy(hardware.h_lladdr, macaddress, ETH_ALEN); hardware.h_ops = &pcap_ops;