]>
Commit | Line | Data |
---|---|---|
eab6aa62 VB |
1 | /* -*- mode: c; c-file-style: "openbsd" -*- */ |
2 | /* | |
3 | * Copyright (c) 2015 Vincent Bernat <bernat@luffy.cx> | |
4 | * | |
5 | * Permission to use, copy, modify, and/or distribute this software for any | |
6 | * purpose with or without fee is hereby granted, provided that the above | |
7 | * copyright notice and this permission notice appear in all copies. | |
8 | * | |
9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
16 | */ | |
17 | ||
5aa86a41 VB |
18 | #include <stdlib.h> |
19 | #include <sys/socket.h> | |
20 | #include <arpa/inet.h> | |
21 | #include <netinet/in.h> | |
22 | #include <check.h> | |
5aa86a41 VB |
23 | #include "common.h" |
24 | ||
25 | char filenameprefix[] = "sonmp_send"; | |
26 | ||
27 | #ifdef ENABLE_SONMP | |
28 | ||
29 | START_TEST (test_send_sonmp) | |
30 | { | |
31 | int n; | |
32 | /* Packet we should build: | |
33 | IEEE 802.3 Ethernet | |
34 | Destination: Bay-Networks-(Synoptics)-autodiscovery (01:00:81:00:01:00) | |
35 | Source: 5e:10:8e:e7:84:ad (5e:10:8e:e7:84:ad) | |
36 | Length: 22 | |
37 | Logical-Link Control | |
38 | DSAP: SNAP (0xaa) | |
39 | IG Bit: Individual | |
40 | SSAP: SNAP (0xaa) | |
41 | CR Bit: Command | |
42 | Control field: U, func=UI (0x03) | |
43 | 000. 00.. = Command: Unnumbered Information (0x00) | |
44 | .... ..11 = Frame type: Unnumbered frame (0x03) | |
45 | Organization Code: Nortel Networks SONMP (0x000081) | |
46 | PID: SONMP segment hello (0x01a2) | |
47 | Nortel Networks / SynOptics Network Management Protocol | |
48 | NMM IP address: 172.17.142.37 (172.17.142.37) | |
49 | Segment Identifier: 0x000004 | |
50 | Chassis type: Unknown (1) | |
51 | Backplane type: ethernet, fast ethernet and gigabit ethernet (12) | |
52 | NMM state: New (3) | |
53 | Number of links: 1 | |
54 | ||
55 | IEEE 802.3 Ethernet | |
56 | Destination: Bay-Networks-(Synoptics)-autodiscovery (01:00:81:00:01:01) | |
57 | Source: 5e:10:8e:e7:84:ad (5e:10:8e:e7:84:ad) | |
58 | Length: 22 | |
59 | Logical-Link Control | |
60 | DSAP: SNAP (0xaa) | |
61 | IG Bit: Individual | |
62 | SSAP: SNAP (0xaa) | |
63 | CR Bit: Command | |
64 | Control field: U, func=UI (0x03) | |
65 | 000. 00.. = Command: Unnumbered Information (0x00) | |
66 | .... ..11 = Frame type: Unnumbered frame (0x03) | |
67 | Organization Code: Nortel Networks SONMP (0x000081) | |
68 | PID: SONMP flatnet hello (0x01a1) | |
69 | Nortel Networks / SynOptics Network Management Protocol | |
70 | NMM IP address: 172.17.142.37 (172.17.142.37) | |
71 | Segment Identifier: 0x000004 | |
72 | Chassis type: Unknown (1) | |
73 | Backplane type: ethernet, fast ethernet and gigabit ethernet (12) | |
74 | NMM state: New (3) | |
75 | Number of links: 1 | |
76 | */ | |
77 | char pkt1[] = { | |
78 | 0x01, 0x00, 0x81, 0x00, 0x01, 0x00, 0x5e, 0x10, | |
79 | 0x8e, 0xe7, 0x84, 0xad, 0x00, 0x16, 0xaa, 0xaa, | |
80 | 0x03, 0x00, 0x00, 0x81, 0x01, 0xa2, 0xac, 0x11, | |
81 | 0x8e, 0x25, 0x00, 0x00, 0x04, 0x01, 0x0c, 0x03, | |
82 | 0x01 }; | |
83 | char pkt2[] = { | |
84 | 0x01, 0x00, 0x81, 0x00, 0x01, 0x01, 0x5e, 0x10, | |
85 | 0x8e, 0xe7, 0x84, 0xad, 0x00, 0x16, 0xaa, 0xaa, | |
86 | 0x03, 0x00, 0x00, 0x81, 0x01, 0xa1, 0xac, 0x11, | |
87 | 0x8e, 0x25, 0x00, 0x00, 0x04, 0x01, 0x0c, 0x03, | |
88 | 0x01 }; | |
89 | struct packet *pkt; | |
5fd6695c VB |
90 | in_addr_t addr; |
91 | struct lldpd_mgmt *mgmt; | |
5aa86a41 VB |
92 | |
93 | /* Populate port and chassis */ | |
94 | hardware.h_lport.p_id_subtype = LLDP_PORTID_SUBTYPE_IFNAME; | |
95 | hardware.h_lport.p_id = "Not used"; | |
96 | hardware.h_lport.p_id_len = strlen(hardware.h_lport.p_id); | |
97 | chassis.c_id_subtype = LLDP_CHASSISID_SUBTYPE_LLADDR; | |
98 | chassis.c_id = macaddress; | |
4e5f34c5 | 99 | chassis.c_id_len = ETHER_ADDR_LEN; |
e6b36c87 | 100 | TAILQ_INIT(&chassis.c_mgmt); |
5fd6695c VB |
101 | addr = inet_addr("172.17.142.37"); |
102 | mgmt = lldpd_alloc_mgmt(LLDPD_AF_IPV4, | |
103 | &addr, sizeof(in_addr_t), 0); | |
e6b36c87 JV |
104 | if (mgmt == NULL) |
105 | ck_abort(); | |
106 | TAILQ_INSERT_TAIL(&chassis.c_mgmt, mgmt, m_entries); | |
5aa86a41 VB |
107 | |
108 | /* Build packet */ | |
109 | n = sonmp_send(NULL, &hardware); | |
110 | if (n != 0) { | |
111 | fail("unable to build packet"); | |
112 | return; | |
113 | } | |
114 | if (TAILQ_EMPTY(&pkts)) { | |
115 | fail("no packets sent"); | |
116 | return; | |
117 | } | |
118 | pkt = TAILQ_FIRST(&pkts); | |
119 | ck_assert_int_eq(pkt->size, sizeof(pkt1)); | |
120 | fail_unless(memcmp(pkt->data, pkt1, sizeof(pkt1)) == 0); | |
121 | pkt = TAILQ_NEXT(pkt, next); | |
122 | if (!pkt) { | |
123 | fail("need one more packet"); | |
124 | return; | |
125 | } | |
126 | ck_assert_int_eq(pkt->size, sizeof(pkt2)); | |
127 | fail_unless(memcmp(pkt->data, pkt2, sizeof(pkt2)) == 0); | |
128 | fail_unless(TAILQ_NEXT(pkt, next) == NULL, "more than two packets sent"); | |
129 | } | |
130 | END_TEST | |
131 | ||
132 | START_TEST (test_recv_sonmp) | |
133 | { | |
134 | char pkt1[] = { | |
135 | 0x01, 0x00, 0x81, 0x00, 0x01, 0x00, 0x00, 0x1b, | |
136 | 0x25, 0x08, 0x50, 0x47, 0x00, 0x13, 0xaa, 0xaa, | |
137 | 0x03, 0x00, 0x00, 0x81, 0x01, 0xa2, 0xac, 0x10, | |
138 | 0x65, 0xa8, 0x00, 0x02, 0x08, 0x38, 0x0c, 0x02, | |
139 | 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
140 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
141 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
142 | 0x00, 0x00, 0x00, 0x00 }; | |
143 | /* This is: | |
144 | IEEE 802.3 Ethernet | |
145 | Destination: Bay-Networks-(Synoptics)-autodiscovery (01:00:81:00:01:00) | |
146 | Source: Nortel_08:50:47 (00:1b:25:08:50:47) | |
147 | Length: 19 | |
148 | Trailer: 000000000000000000000000000000000000000000000000... | |
149 | Logical-Link Control | |
150 | DSAP: SNAP (0xaa) | |
151 | IG Bit: Individual | |
152 | SSAP: SNAP (0xaa) | |
153 | CR Bit: Command | |
154 | Control field: U, func=UI (0x03) | |
155 | 000. 00.. = Command: Unnumbered Information (0x00) | |
156 | .... ..11 = Frame type: Unnumbered frame (0x03) | |
157 | Organization Code: Nortel Networks SONMP (0x000081) | |
158 | PID: SONMP segment hello (0x01a2) | |
159 | Nortel Networks / SynOptics Network Management Protocol | |
160 | NMM IP address: 172.16.101.168 (172.16.101.168) | |
161 | Segment Identifier: 0x000208 | |
162 | Chassis type: Accelar 8610 L3 switch (56) | |
163 | Backplane type: ethernet, fast ethernet and gigabit ethernet (12) | |
164 | NMM state: Heartbeat (2) | |
165 | Number of links: 1 | |
166 | */ | |
167 | struct lldpd_chassis *nchassis = NULL; | |
168 | struct lldpd_port *nport = NULL; | |
169 | char cid[5]; | |
170 | in_addr_t ip; | |
171 | ||
172 | fail_unless(sonmp_decode(NULL, pkt1, sizeof(pkt1), &hardware, | |
173 | &nchassis, &nport) != -1); | |
174 | if (!nchassis || !nport) { | |
175 | fail("unable to decode packet"); | |
176 | return; | |
177 | } | |
178 | ck_assert_int_eq(nchassis->c_id_subtype, | |
179 | LLDP_CHASSISID_SUBTYPE_ADDR); | |
180 | ck_assert_int_eq(nchassis->c_id_len, 5); | |
181 | cid[0] = 1; | |
182 | ip = inet_addr("172.16.101.168"); | |
183 | memcpy(cid + 1, &ip, sizeof(in_addr_t)); | |
184 | fail_unless(memcmp(nchassis->c_id, cid, 5) == 0); | |
185 | ck_assert_str_eq(nchassis->c_name, "172.16.101.168"); | |
186 | ck_assert_str_eq(nchassis->c_descr, "Nortel Ethernet Routing 8610 L3 Switch"); | |
e6b36c87 | 187 | ck_assert_int_eq(TAILQ_FIRST(&nchassis->c_mgmt)->m_addr.inet.s_addr, |
5aa86a41 | 188 | (u_int32_t)inet_addr("172.16.101.168")); |
e6b36c87 | 189 | ck_assert_int_eq(TAILQ_FIRST(&nchassis->c_mgmt)->m_iface, 0); |
5aa86a41 VB |
190 | ck_assert_str_eq(nport->p_descr, "port 2/8"); |
191 | ck_assert_int_eq(nport->p_id_subtype, | |
192 | LLDP_PORTID_SUBTYPE_LOCAL); | |
193 | ck_assert_int_eq(nport->p_id_len, strlen("00-02-08")); | |
194 | fail_unless(memcmp(nport->p_id, | |
195 | "00-02-08", strlen("00-02-08")) == 0); | |
196 | ck_assert_int_eq(nchassis->c_cap_enabled, 0); | |
197 | } | |
198 | END_TEST | |
199 | ||
200 | #endif | |
201 | ||
202 | Suite * | |
203 | sonmp_suite(void) | |
204 | { | |
205 | Suite *s = suite_create("SONMP"); | |
206 | ||
207 | #ifdef ENABLE_SONMP | |
208 | TCase *tc_send = tcase_create("Send SONMP packets"); | |
5fd6695c VB |
209 | TCase *tc_receive = tcase_create("Receive SONMP packets"); |
210 | ||
5aa86a41 VB |
211 | tcase_add_checked_fixture(tc_send, pcap_setup, pcap_teardown); |
212 | tcase_add_test(tc_send, test_send_sonmp); | |
213 | suite_add_tcase(s, tc_send); | |
214 | ||
5aa86a41 VB |
215 | tcase_add_test(tc_receive, test_recv_sonmp); |
216 | suite_add_tcase(s, tc_receive); | |
217 | #endif | |
218 | ||
219 | return s; | |
220 | } | |
221 | ||
222 | int | |
223 | main() | |
224 | { | |
225 | int number_failed; | |
226 | Suite *s = sonmp_suite (); | |
227 | SRunner *sr = srunner_create (s); | |
228 | srunner_set_fork_status (sr, CK_NOFORK); /* Can't fork because | |
229 | we need to write | |
230 | files */ | |
231 | srunner_run_all (sr, CK_ENV); | |
232 | number_failed = srunner_ntests_failed (sr); | |
233 | srunner_free (sr); | |
234 | return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; | |
235 | } |