]> git.ipfire.org Git - thirdparty/lldpd.git/blame - tests/check_lldp.c
Don't mix declarations and statements.
[thirdparty/lldpd.git] / tests / check_lldp.c
CommitLineData
fedf4d77
VB
1#define _GNU_SOURCE 1
2#include <stdlib.h>
654f6246 3#include <sys/socket.h>
654f6246 4#include <arpa/inet.h>
4dc61015 5#include <netinet/in.h>
fedf4d77
VB
6#include <check.h>
7#include "../src/lldpd.h"
4dc61015 8#include "common.h"
fedf4d77 9
4dc61015 10char filenameprefix[] = "lldp_send";
fedf4d77 11
0c0991d8
VB
12#define ck_assert_str_eq_n(X, Y, N) \
13 ck_assert_msg(!strncmp(X, Y, N), "Assertion '"#X"=="#Y"' failed: "#X"==\"%s\", "#Y"==\"%s\"", X, Y)
14
bc99d796
SK
15static void
16check_received_port(
17 struct lldpd_port *sport,
18 struct lldpd_port *rport)
fedf4d77 19{
bc99d796 20 ck_assert_int_eq(rport->p_id_subtype, sport->p_id_subtype);
bc99d796 21 ck_assert_int_eq(rport->p_id_len, sport->p_id_len);
0c0991d8 22 ck_assert_str_eq_n(rport->p_id, sport->p_id, sport->p_id_len);
bc99d796 23 ck_assert_str_eq(rport->p_descr, sport->p_descr);
5345fe05 24#ifdef ENABLE_DOT3
bc99d796 25 ck_assert_int_eq(rport->p_mfs, sport->p_mfs);
5345fe05 26#endif
bc99d796
SK
27 return;
28}
29
30static void
31check_received_chassis(
32 struct lldpd_chassis *schassis,
33 struct lldpd_chassis *rchassis)
34{
35 ck_assert_int_eq(rchassis->c_id_subtype, schassis->c_id_subtype);
bc99d796 36 ck_assert_int_eq(rchassis->c_id_len, schassis->c_id_len);
0c0991d8 37 ck_assert_str_eq_n(rchassis->c_id, schassis->c_id, schassis->c_id_len);
bc99d796
SK
38 ck_assert_str_eq(rchassis->c_name, schassis->c_name);
39 ck_assert_str_eq(rchassis->c_descr, schassis->c_descr);
40 ck_assert_int_eq(rchassis->c_cap_available, schassis->c_cap_available);
41 ck_assert_int_eq(rchassis->c_cap_enabled, schassis->c_cap_enabled);
42 return;
43}
44
45#ifdef ENABLE_LLDPMED
46static void
47check_received_port_med(
48 struct lldpd_port *sport,
49 struct lldpd_port *rport)
50{
51 ck_assert_int_eq(rport->p_med_cap_enabled, sport->p_med_cap_enabled);
52 ck_assert_int_eq(rport->p_med_cap_enabled, sport->p_med_cap_enabled);
53 ck_assert_int_eq(
54 rport->p_med_location[LLDPMED_LOCFORMAT_CIVIC-1].format,
55 sport->p_med_location[LLDPMED_LOCFORMAT_CIVIC-1].format);
56 ck_assert_int_eq(
57 rport->p_med_location[LLDPMED_LOCFORMAT_CIVIC-1].data_len,
58 sport->p_med_location[LLDPMED_LOCFORMAT_CIVIC-1].data_len);
0c0991d8 59 ck_assert_str_eq_n(
bc99d796 60 rport->p_med_location[LLDPMED_LOCFORMAT_CIVIC-1].data,
0c0991d8
VB
61 sport->p_med_location[LLDPMED_LOCFORMAT_CIVIC-1].data,
62 sport->p_med_location[LLDPMED_LOCFORMAT_CIVIC-1].data_len);
bc99d796
SK
63 ck_assert_int_eq(
64 rport->p_med_policy[LLDPMED_APPTYPE_SOFTPHONEVOICE-1].type,
65 sport->p_med_policy[LLDPMED_APPTYPE_SOFTPHONEVOICE-1].type);
66 ck_assert_int_eq(
67 rport->p_med_policy[LLDPMED_APPTYPE_SOFTPHONEVOICE-1].tagged,
68 sport->p_med_policy[LLDPMED_APPTYPE_SOFTPHONEVOICE-1].tagged);
69 ck_assert_int_eq(
70 rport->p_med_policy[LLDPMED_APPTYPE_SOFTPHONEVOICE-1].vid,
71 sport->p_med_policy[LLDPMED_APPTYPE_SOFTPHONEVOICE-1].vid);
72 ck_assert_int_eq(
73 rport->p_med_policy[LLDPMED_APPTYPE_SOFTPHONEVOICE-1].priority,
74 sport->p_med_policy[LLDPMED_APPTYPE_SOFTPHONEVOICE-1].priority);
75 ck_assert_int_eq(
76 rport->p_med_policy[LLDPMED_APPTYPE_SOFTPHONEVOICE-1].dscp,
77 sport->p_med_policy[LLDPMED_APPTYPE_SOFTPHONEVOICE-1].dscp);
78 ck_assert_int_eq(
79 rport->p_med_power.devicetype, sport->p_med_power.devicetype);
80 ck_assert_int_eq(rport->p_med_power.source, sport->p_med_power.source);
81 ck_assert_int_eq(rport->p_med_power.priority,
82 sport->p_med_power.priority);
83 ck_assert_int_eq(rport->p_med_power.val, sport->p_med_power.val);
84 return;
85}
86
87static void
88check_received_chassis_med(
89 struct lldpd_chassis *schassis,
90 struct lldpd_chassis *rchassis)
91{
92 ck_assert_int_eq(rchassis->c_med_cap_available,
93 schassis->c_med_cap_available);
94 ck_assert_int_eq(rchassis->c_med_type, schassis->c_med_type);
95 ck_assert_str_eq(rchassis->c_med_hw, schassis->c_med_hw);
96 ck_assert_str_eq(rchassis->c_med_fw, schassis->c_med_fw);
97 ck_assert_str_eq(rchassis->c_med_sw, schassis->c_med_sw);
98 ck_assert_str_eq(rchassis->c_med_sn, schassis->c_med_sn);
99 return;
100}
101#endif
102
31375299 103#ifdef ENABLE_DOT3
bc99d796
SK
104static void
105check_received_port_dot3(
106 struct lldpd_port *sport,
107 struct lldpd_port *rport)
108{
109 ck_assert_int_eq(rport->p_aggregid, sport->p_aggregid);
110 ck_assert_int_eq(rport->p_macphy.autoneg_support,
111 sport->p_macphy.autoneg_support);
112 ck_assert_int_eq(rport->p_macphy.autoneg_enabled,
113 sport->p_macphy.autoneg_enabled);
114 ck_assert_int_eq(rport->p_macphy.autoneg_advertised,
115 sport->p_macphy.autoneg_advertised);
116 ck_assert_int_eq(rport->p_macphy.mau_type, sport->p_macphy.mau_type);
117 return;
118}
31375299 119#endif
bc99d796
SK
120
121START_TEST (test_send_rcv_basic)
122{
123 int n;
fedf4d77 124 struct packet *pkt;
bc99d796
SK
125 struct lldpd_chassis *nchassis = NULL;
126 struct lldpd_port *nport = NULL;
fedf4d77
VB
127
128 /* Populate port and chassis */
129 hardware.h_lport.p_id_subtype = LLDP_PORTID_SUBTYPE_IFNAME;
130 hardware.h_lport.p_id = "FastEthernet 1/5";
131 hardware.h_lport.p_id_len = strlen(hardware.h_lport.p_id);
132 hardware.h_lport.p_descr = "Fake port description";
133 hardware.h_lport.p_mfs = 1516;
134 chassis.c_id_subtype = LLDP_CHASSISID_SUBTYPE_LLADDR;
135 chassis.c_id = macaddress;
136 chassis.c_id_len = ETH_ALEN;
137 chassis.c_name = "First chassis";
138 chassis.c_descr = "Chassis description";
139 chassis.c_cap_available = chassis.c_cap_enabled = LLDP_CAP_ROUTER;
140
141 /* Build packet */
142 n = lldp_send(NULL, &hardware);
9360858c
VB
143 if (n != 0) {
144 fail("unable to build packet");
145 return;
146 }
147 if (TAILQ_EMPTY(&pkts)) {
148 fail("no packets sent");
149 return;
150 }
fedf4d77 151 pkt = TAILQ_FIRST(&pkts);
fedf4d77 152 fail_unless(TAILQ_NEXT(pkt, next) == NULL, "more than one packet sent");
bc99d796
SK
153
154 /* decode the retrieved packet calling lldp_decode() */
155 fail_unless(lldp_decode(NULL, pkt->data, pkt->size, &hardware,
156 &nchassis, &nport) != -1);
157 if (!nchassis || !nport) {
158 fail("unable to decode packet");
159 return;
160 }
161 /* verify port values */
162 check_received_port(&hardware.h_lport, nport);
163 /* verify chassis values */
164 check_received_chassis(&chassis, nchassis);
fedf4d77
VB
165}
166END_TEST
167
31375299 168#ifdef ENABLE_DOT1
bc99d796
SK
169/* This test case tests send and receive of all DOT1 TLVs(2005 and 2009):
170 Port Valn ID, VLAN, Port Protocol VLAN ID, Protocol Identity,
171 VID Usage Digest, Management VID, and 802.1ax Link Aggregation TLVs */
172START_TEST (test_send_rcv_dot1_tlvs)
fedf4d77
VB
173{
174 int n;
bc99d796
SK
175 struct lldpd_vlan *rvlan, vlan1, vlan2, vlan3;
176 struct lldpd_ppvid ppvid, *rppvid;
177 struct lldpd_pi pi1, pi2, *rpi;
178 struct lldpd_chassis *nchassis = NULL;
179 struct lldpd_port *nport = NULL;
fedf4d77
VB
180 struct packet *pkt;
181
182 /* Populate port and chassis */
183 hardware.h_lport.p_id_subtype = LLDP_PORTID_SUBTYPE_LLADDR;
184 hardware.h_lport.p_id = macaddress;
185 hardware.h_lport.p_id_len = ETH_ALEN;
186 hardware.h_lport.p_descr = "Fake port description";
187 hardware.h_lport.p_mfs = 1516;
bc99d796 188 hardware.h_lport.p_pvid = 1500;
fedf4d77
VB
189 chassis.c_id_subtype = LLDP_CHASSISID_SUBTYPE_LOCAL;
190 chassis.c_id = "Chassis name";
191 chassis.c_id_len = strlen(chassis.c_id);
192 chassis.c_name = "Second chassis";
193 chassis.c_descr = "Chassis description";
194 chassis.c_cap_available = LLDP_CAP_ROUTER | LLDP_CAP_BRIDGE;
195 chassis.c_cap_enabled = LLDP_CAP_ROUTER;
bc99d796
SK
196 vlan1.v_name = "Voice"; vlan1.v_vid = 157;
197 vlan2.v_name = "Data"; vlan2.v_vid = 1247;
198 vlan3.v_name = "Control"; vlan3.v_vid = 741;
fedf4d77
VB
199 TAILQ_INSERT_TAIL(&hardware.h_lport.p_vlans, &vlan1, v_entries);
200 TAILQ_INSERT_TAIL(&hardware.h_lport.p_vlans, &vlan2, v_entries);
201 TAILQ_INSERT_TAIL(&hardware.h_lport.p_vlans, &vlan3, v_entries);
bc99d796
SK
202 ppvid.p_cap_status = 3;
203 ppvid.p_ppvid = 1500;
204 TAILQ_INSERT_TAIL(&hardware.h_lport.p_ppvids, &ppvid, p_entries);
205 pi1.p_pi = "IEEE Link Aggregration Control Protocol 802.3ad";
de5ce837 206 pi1.p_pi_len = strlen(pi1.p_pi);
bc99d796 207 pi2.p_pi = "IEEE Link Layer Discovery Protocol 802.1ab-2005";
de5ce837 208 pi2.p_pi_len = strlen(pi2.p_pi);
bc99d796
SK
209 TAILQ_INSERT_TAIL(&hardware.h_lport.p_pids, &pi1, p_entries);
210 TAILQ_INSERT_TAIL(&hardware.h_lport.p_pids, &pi2, p_entries);
fedf4d77
VB
211
212 /* Build packet */
213 n = lldp_send(NULL, &hardware);
9360858c
VB
214 if (n != 0) {
215 fail("unable to build packet");
216 return;
217 }
218 if (TAILQ_EMPTY(&pkts)) {
219 fail("no packets sent");
220 return;
221 }
fedf4d77 222 pkt = TAILQ_FIRST(&pkts);
fedf4d77 223 fail_unless(TAILQ_NEXT(pkt, next) == NULL, "more than one packet sent");
bc99d796
SK
224
225 /* decode the retrieved packet calling lldp_decode() */
226 fail_unless(lldp_decode(NULL, pkt->data, pkt->size, &hardware,
227 &nchassis, &nport) != -1);
228 if (!nchassis || !nport) {
229 fail("unable to decode packet");
230 return;
231 }
232
233 /* verify port values */
234 check_received_port(&hardware.h_lport, nport);
235 /* verify chassis values */
236 check_received_chassis(&chassis, nchassis);
237
238 if (TAILQ_EMPTY(&nport->p_vlans)) {
239 fail("no VLAN");
240 return;
241 }
242
243 rvlan = TAILQ_FIRST(&nport->p_vlans);
244 ck_assert_int_eq(rvlan->v_vid, vlan1.v_vid);
245 ck_assert_str_eq(rvlan->v_name, vlan1.v_name);
246
247 rvlan = TAILQ_NEXT(rvlan, v_entries);
248 if (!rvlan) {
249 fail("no more VLAN");
250 return;
251 }
252 ck_assert_int_eq(rvlan->v_vid, vlan2.v_vid);
253 ck_assert_str_eq(rvlan->v_name, vlan2.v_name);
254
255 rvlan = TAILQ_NEXT(rvlan, v_entries);
256 if (!rvlan) {
257 fail("no more VLAN");
258 return;
259 }
260 ck_assert_int_eq(rvlan->v_vid, vlan3.v_vid);
261 ck_assert_str_eq(rvlan->v_name, vlan3.v_name);
262
263 rvlan = TAILQ_NEXT(rvlan, v_entries);
264 fail_unless(rvlan == NULL);
265
266 ck_assert_int_eq(nport->p_pvid, hardware.h_lport.p_pvid);
267
268 if (TAILQ_EMPTY(&nport->p_ppvids)) {
269 fail("no Port Protocal VLAN ID");
270 return;
271 }
272 rppvid = TAILQ_FIRST(&nport->p_ppvids);
273 ck_assert_int_eq(rppvid->p_cap_status, ppvid.p_cap_status);
274 ck_assert_int_eq(rppvid->p_ppvid, ppvid.p_ppvid);
275
276 if (TAILQ_EMPTY(&nport->p_pids)) {
277 fail("no Protocal Identity TLV");
278 return;
279 }
280 rpi = TAILQ_FIRST(&nport->p_pids);
0c0991d8
VB
281 ck_assert_int_eq(rpi->p_pi_len, pi1.p_pi_len);
282 ck_assert_str_eq_n(rpi->p_pi, pi1.p_pi, pi1.p_pi_len);
bc99d796
SK
283
284 rpi = TAILQ_NEXT(rpi, p_entries);
285 if (!rpi) {
286 fail("no more Protocol Identity TLVs");
287 return;
288 }
0c0991d8
VB
289 ck_assert_int_eq(rpi->p_pi_len, pi2.p_pi_len);
290 ck_assert_str_eq_n(rpi->p_pi, pi2.p_pi, pi2.p_pi_len);
bc99d796
SK
291
292 rpi = TAILQ_NEXT(rpi, p_entries);
293 fail_unless(rpi == NULL);
294
295 return;
fedf4d77
VB
296}
297END_TEST
31375299 298#endif
fedf4d77 299
31375299 300#ifdef ENABLE_LLDPMED
bc99d796 301START_TEST (test_send_rcv_med)
fedf4d77
VB
302{
303 int n;
fedf4d77 304 struct packet *pkt;
bc99d796
SK
305 struct lldpd_chassis *nchassis = NULL;
306 struct lldpd_port *nport = NULL;
fedf4d77
VB
307
308 /* Populate port and chassis */
309 hardware.h_lport.p_id_subtype = LLDP_PORTID_SUBTYPE_LLADDR;
310 hardware.h_lport.p_id = macaddress;
311 hardware.h_lport.p_id_len = ETH_ALEN;
312 hardware.h_lport.p_descr = "Fake port description";
313 hardware.h_lport.p_mfs = 1516;
314 chassis.c_id_subtype = LLDP_CHASSISID_SUBTYPE_LOCAL;
315 chassis.c_id = "Chassis name";
316 chassis.c_id_len = strlen(chassis.c_id);
317 chassis.c_name = "Third chassis";
318 chassis.c_descr = "Chassis description";
319 chassis.c_cap_available = LLDP_CAP_ROUTER | LLDP_CAP_BRIDGE;
320 chassis.c_cap_enabled = LLDP_CAP_ROUTER;
321 chassis.c_med_cap_available = LLDPMED_CAP_CAP | LLDPMED_CAP_POLICY |
322 LLDPMED_CAP_LOCATION | LLDPMED_CAP_MDI_PSE |
323 LLDPMED_CAP_IV;
324 chassis.c_med_type = LLDPMED_CLASS_III;
325 chassis.c_med_hw = "hardware rev 5";
326 chassis.c_med_fw = "47b5";
327 chassis.c_med_sw = "2.6.22b5";
328 chassis.c_med_sn = "SN 47842";
329 hardware.h_lport.p_med_cap_enabled = chassis.c_med_cap_available;
330 hardware.h_lport.p_med_location[LLDPMED_LOCFORMAT_CIVIC-1].format =
331 LLDPMED_LOCFORMAT_CIVIC;
bc99d796
SK
332 hardware.h_lport.p_med_location[LLDPMED_LOCFORMAT_CIVIC-1].data = "Your favorite city";
333 hardware.h_lport.p_med_location[LLDPMED_LOCFORMAT_CIVIC-1].data_len =
334 sizeof("Your favorite city");
fedf4d77
VB
335 hardware.h_lport.p_med_policy[LLDPMED_APPTYPE_SOFTPHONEVOICE-1].type =
336 LLDPMED_APPTYPE_SOFTPHONEVOICE;
337 hardware.h_lport.p_med_policy[LLDPMED_APPTYPE_SOFTPHONEVOICE-1].tagged =
338 1;
339 hardware.h_lport.p_med_policy[LLDPMED_APPTYPE_SOFTPHONEVOICE-1].vid =
340 51;
341 hardware.h_lport.p_med_policy[LLDPMED_APPTYPE_SOFTPHONEVOICE-1].priority =
342 6;
343 hardware.h_lport.p_med_policy[LLDPMED_APPTYPE_SOFTPHONEVOICE-1].dscp =
344 46;
6d08df0e
VB
345 hardware.h_lport.p_med_power.devicetype = LLDPMED_POW_TYPE_PSE;
346 hardware.h_lport.p_med_power.source = LLDPMED_POW_SOURCE_PRIMARY;
347 hardware.h_lport.p_med_power.priority = LLDPMED_POW_PRIO_HIGH;
348 hardware.h_lport.p_med_power.val = 65;
fedf4d77
VB
349
350 /* Build packet */
351 n = lldp_send(NULL, &hardware);
9360858c
VB
352 if (n != 0) {
353 fail("unable to build packet");
354 return;
355 }
356 if (TAILQ_EMPTY(&pkts)) {
357 fail("no packets sent");
358 return;
359 }
fedf4d77 360 pkt = TAILQ_FIRST(&pkts);
fedf4d77 361 fail_unless(TAILQ_NEXT(pkt, next) == NULL, "more than one packet sent");
bc99d796
SK
362
363 /* decode the retrieved packet calling lldp_decode() */
364 fail_unless(lldp_decode(NULL, pkt->data, pkt->size, &hardware,
365 &nchassis, &nport) != -1);
366 if (!nchassis || !nport) {
367 fail("unable to decode packet");
368 return;
369 }
370 /* verify port values */
371 check_received_port(&hardware.h_lport, nport);
372 /* verify chassis values */
373 check_received_chassis(&chassis, nchassis);
374
375 /* veridfy med content */
376 check_received_port_med(&hardware.h_lport, nport);
377 check_received_chassis_med(&chassis, nchassis);
fedf4d77
VB
378}
379END_TEST
31375299 380#endif
fedf4d77 381
31375299 382#ifdef ENABLE_DOT3
bc99d796 383START_TEST (test_send_rcv_dot3)
fedf4d77
VB
384{
385 int n;
bc99d796
SK
386 struct lldpd_chassis *nchassis = NULL;
387 struct lldpd_port *nport = NULL;
fedf4d77
VB
388 struct packet *pkt;
389
390 /* Populate port and chassis */
391 hardware.h_lport.p_id_subtype = LLDP_PORTID_SUBTYPE_IFNAME;
392 hardware.h_lport.p_id = "FastEthernet 1/5";
393 hardware.h_lport.p_id_len = strlen(hardware.h_lport.p_id);
394 hardware.h_lport.p_descr = "Fake port description";
395 hardware.h_lport.p_mfs = 1516;
396 hardware.h_lport.p_aggregid = 5;
3fd015c0
VB
397 hardware.h_lport.p_macphy.autoneg_support = 1;
398 hardware.h_lport.p_macphy.autoneg_enabled = 1;
399 hardware.h_lport.p_macphy.autoneg_advertised = LLDP_DOT3_LINK_AUTONEG_10BASE_T |
fedf4d77
VB
400 LLDP_DOT3_LINK_AUTONEG_10BASET_FD | LLDP_DOT3_LINK_AUTONEG_100BASE_TX |
401 LLDP_DOT3_LINK_AUTONEG_100BASE_TXFD;
3fd015c0 402 hardware.h_lport.p_macphy.mau_type = LLDP_DOT3_MAU_100BASETXFD;
fedf4d77
VB
403 chassis.c_id_subtype = LLDP_CHASSISID_SUBTYPE_LLADDR;
404 chassis.c_id = macaddress;
405 chassis.c_id_len = ETH_ALEN;
406 chassis.c_name = "Fourth chassis";
407 chassis.c_descr = "Long chassis description";
408 chassis.c_cap_available = chassis.c_cap_enabled = LLDP_CAP_ROUTER | LLDP_CAP_WLAN;
409
410 /* Build packet */
411 n = lldp_send(NULL, &hardware);
9360858c
VB
412 if (n != 0) {
413 fail("unable to build packet");
414 return;
415 }
416 if (TAILQ_EMPTY(&pkts)) {
417 fail("no packets sent");
418 return;
419 }
fedf4d77 420 pkt = TAILQ_FIRST(&pkts);
fedf4d77 421 fail_unless(TAILQ_NEXT(pkt, next) == NULL, "more than one packet sent");
bc99d796
SK
422
423 /* decode the retrieved packet calling lldp_decode() */
424 fail_unless(lldp_decode(NULL, pkt->data, pkt->size, &hardware,
425 &nchassis, &nport) != -1);
426 if (!nchassis || !nport) {
427 fail("unable to decode packet");
428 return;
429 }
430 /* verify port values */
431 check_received_port(&hardware.h_lport, nport);
432 /* verify chassis values */
433 check_received_chassis(&chassis, nchassis);
434 /* verify dot3 values */
435 check_received_port_dot3(&hardware.h_lport, nport);
fedf4d77
VB
436}
437END_TEST
31375299 438#endif
fedf4d77 439
654f6246
VB
440START_TEST (test_recv_min)
441{
442 char pkt1[] = {
443 0x01, 0x80, 0xc2, 0x00, 0x00, 0x0e, 0x00, 0x17,
444 0xd1, 0xa8, 0x35, 0xbe, 0x88, 0xcc, 0x02, 0x07,
445 0x04, 0x00, 0x17, 0xd1, 0xa8, 0x35, 0xbf, 0x04,
446 0x07, 0x03, 0x00, 0x17, 0xd1, 0xa8, 0x36, 0x02,
447 0x06, 0x02, 0x00, 0x78, 0x00, 0x00, 0x00, 0x00,
448 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
449 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
450 0x00, 0x00, 0x00, 0x00 };
451 /* This is:
452Ethernet II, Src: Nortel_a8:35:be (00:17:d1:a8:35:be), Dst: LLDP_Multicast (01:80:c2:00:00:0e)
453 Destination: LLDP_Multicast (01:80:c2:00:00:0e)
454 Source: Nortel_a8:35:be (00:17:d1:a8:35:be)
455 Type: 802.1 Link Layer Discovery Protocol (LLDP) (0x88cc)
456Link Layer Discovery Protocol
457 Chassis Subtype = MAC address
458 0000 001. .... .... = TLV Type: Chassis Id (1)
459 .... ...0 0000 0111 = TLV Length: 7
460 Chassis Id Subtype: MAC address (4)
461 Chassis Id: Nortel_a8:35:bf (00:17:d1:a8:35:bf)
462 Port Subtype = MAC address
463 0000 010. .... .... = TLV Type: Port Id (2)
464 .... ...0 0000 0111 = TLV Length: 7
465 Port Id Subtype: MAC address (3)
466 Port Id: Nortel_a8:36:02 (00:17:d1:a8:36:02)
467 Time To Live = 120 sec
468 0000 011. .... .... = TLV Type: Time to Live (3)
469 .... ...0 0000 0010 = TLV Length: 2
470 Seconds: 120
471 End of LLDPDU
472 0000 000. .... .... = TLV Type: End of LLDPDU (0)
473 .... ...0 0000 0000 = TLV Length: 0
474 */
475 struct lldpd_chassis *nchassis = NULL;
476 struct lldpd_port *nport = NULL;
477 char mac1[] = { 0x0, 0x17, 0xd1, 0xa8, 0x35, 0xbf };
478 char mac2[] = { 0x0, 0x17, 0xd1, 0xa8, 0x36, 0x02 };
479
480 fail_unless(lldp_decode(NULL, pkt1, sizeof(pkt1), &hardware,
481 &nchassis, &nport) != -1);
9360858c
VB
482 if (!nchassis || !nport) {
483 fail("unable to decode packet");
484 return;
485 }
654f6246
VB
486 ck_assert_int_eq(nchassis->c_id_subtype,
487 LLDP_CHASSISID_SUBTYPE_LLADDR);
488 ck_assert_int_eq(nchassis->c_id_len, ETH_ALEN);
489 fail_unless(memcmp(mac1, nchassis->c_id, ETH_ALEN) == 0);
490 ck_assert_int_eq(nport->p_id_subtype,
491 LLDP_PORTID_SUBTYPE_LLADDR);
492 ck_assert_int_eq(nport->p_id_len, ETH_ALEN);
493 fail_unless(memcmp(mac2, nport->p_id, ETH_ALEN) == 0);
494 ck_assert_int_eq(nchassis->c_ttl, 120);
495 ck_assert_str_eq(nchassis->c_name, "Not received");
496 ck_assert_str_eq(nchassis->c_descr, "Not received");
497 ck_assert_str_eq(nport->p_descr, "Not received");
498}
499END_TEST
500
501START_TEST (test_recv_lldpd)
502{
503 /* This is a frame generated by lldpd */
504 char pkt1[] = {
505 0x01, 0x80, 0xc2, 0x00, 0x00, 0x0e, 0x00, 0x16,
506 0x17, 0x2f, 0xa1, 0xb6, 0x88, 0xcc, 0x02, 0x07,
507 0x04, 0x00, 0x16, 0x17, 0x2f, 0xa1, 0xb6, 0x04,
508 0x07, 0x03, 0x00, 0x16, 0x17, 0x2f, 0xa1, 0xb6,
509 0x06, 0x02, 0x00, 0x78, 0x0a, 0x1a, 0x6e, 0x61,
510 0x72, 0x75, 0x74, 0x6f, 0x2e, 0x58, 0x58, 0x58,
511 0x58, 0x58, 0x58, 0x58, 0x58, 0x58, 0x58, 0x58,
512 0x58, 0x58, 0x58, 0x58, 0x58, 0x58, 0x58, 0x58,
513 0x0c, 0x3f, 0x4c, 0x69, 0x6e, 0x75, 0x78, 0x20,
514 0x32, 0x2e, 0x36, 0x2e, 0x32, 0x39, 0x2d, 0x32,
515 0x2d, 0x61, 0x6d, 0x64, 0x36, 0x34, 0x20, 0x23,
516 0x31, 0x20, 0x53, 0x4d, 0x50, 0x20, 0x53, 0x75,
517 0x6e, 0x20, 0x4d, 0x61, 0x79, 0x20, 0x31, 0x37,
518 0x20, 0x31, 0x37, 0x3a, 0x31, 0x35, 0x3a, 0x34,
519 0x37, 0x20, 0x55, 0x54, 0x43, 0x20, 0x32, 0x30,
520 0x30, 0x39, 0x20, 0x78, 0x38, 0x36, 0x5f, 0x36,
521 0x34, 0x0e, 0x04, 0x00, 0x1c, 0x00, 0x14, 0x10,
522 0x0c, 0x05, 0x01, 0x0a, 0xee, 0x50, 0x4b, 0x02,
523 0x00, 0x00, 0x00, 0x03, 0x00, 0x08, 0x04, 0x65,
524 0x74, 0x68, 0x30, 0xfe, 0x09, 0x00, 0x12, 0x0f,
525 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x09,
526 0x00, 0x12, 0x0f, 0x01, 0x03, 0x6c, 0x03, 0x00,
527 0x10, 0xfe, 0x06, 0x00, 0x12, 0x0f, 0x04, 0x05,
528 0xdc, 0xfe, 0x07, 0x00, 0x12, 0xbb, 0x01, 0x00,
529 0x00, 0x00, 0xfe, 0x0f, 0x00, 0x12, 0xbb, 0x05,
530 0x4e, 0x44, 0x39, 0x39, 0x31, 0x37, 0x38, 0x39,
531 0x37, 0x30, 0x32, 0xfe, 0x0b, 0x00, 0x12, 0xbb,
532 0x06, 0x30, 0x38, 0x30, 0x30, 0x31, 0x32, 0x20,
533 0xfe, 0x12, 0x00, 0x12, 0xbb, 0x07, 0x32, 0x2e,
534 0x36, 0x2e, 0x32, 0x39, 0x2d, 0x32, 0x2d, 0x61,
535 0x6d, 0x64, 0x36, 0x34, 0xfe, 0x10, 0x00, 0x12,
536 0xbb, 0x08, 0x31, 0x30, 0x35, 0x38, 0x32, 0x30,
537 0x38, 0x35, 0x30, 0x30, 0x30, 0x39, 0xfe, 0x15,
538 0x00, 0x12, 0xbb, 0x09, 0x4e, 0x45, 0x43, 0x20,
539 0x43, 0x6f, 0x6d, 0x70, 0x75, 0x74, 0x65, 0x72,
540 0x73, 0x20, 0x53, 0x41, 0x53, 0xfe, 0x13, 0x00,
541 0x12, 0xbb, 0x0a, 0x50, 0x4f, 0x57, 0x45, 0x52,
542 0x4d, 0x41, 0x54, 0x45, 0x20, 0x56, 0x4c, 0x33,
543 0x35, 0x30, 0xfe, 0x0d, 0x00, 0x12, 0xbb, 0x0b,
544 0x31, 0x30, 0x30, 0x32, 0x30, 0x37, 0x31, 0x32,
545 0x30, 0x00, 0x00 };
546 /* This is:
547Ethernet II, Src: Msi_2f:a1:b6 (00:16:17:2f:a1:b6), Dst: LLDP_Multicast (01:80:c2:00:00:0e)
548 Destination: LLDP_Multicast (01:80:c2:00:00:0e)
549 Source: Msi_2f:a1:b6 (00:16:17:2f:a1:b6)
550 Type: 802.1 Link Layer Discovery Protocol (LLDP) (0x88cc)
551Link Layer Discovery Protocol
552 Chassis Subtype = MAC address
553 0000 001. .... .... = TLV Type: Chassis Id (1)
554 .... ...0 0000 0111 = TLV Length: 7
555 Chassis Id Subtype: MAC address (4)
556 Chassis Id: Msi_2f:a1:b6 (00:16:17:2f:a1:b6)
557 Port Subtype = MAC address
558 0000 010. .... .... = TLV Type: Port Id (2)
559 .... ...0 0000 0111 = TLV Length: 7
560 Port Id Subtype: MAC address (3)
561 Port Id: Msi_2f:a1:b6 (00:16:17:2f:a1:b6)
562 Time To Live = 120 sec
563 0000 011. .... .... = TLV Type: Time to Live (3)
564 .... ...0 0000 0010 = TLV Length: 2
565 Seconds: 120
566 System Name = naruto.XXXXXXXXXXXXXXXXXXX
567 0000 101. .... .... = TLV Type: System Name (5)
568 .... ...0 0001 1010 = TLV Length: 26
569 System Name = naruto.bureau.b1.p.fti.net
570 System Description = Linux 2.6.29-2-amd64 #1 SMP Sun May 17 17:15:47 UTC 2009 x86_64
571 0000 110. .... .... = TLV Type: System Description (6)
572 .... ...0 0011 1111 = TLV Length: 63
573 System Description = Linux 2.6.29-2-amd64 #1 SMP Sun May 17 17:15:47 UTC 2009 x86_64
574 Capabilities
575 0000 111. .... .... = TLV Type: System Capabilities (7)
576 .... ...0 0000 0100 = TLV Length: 4
577 Capabilities: 0x001c
578 .... .... .... .1.. = Bridge
579 .... .... .... 1... = WLAN access point
580 .... .... ...1 .... = Router
581 Enabled Capabilities: 0x0014
582 .... .... .... .1.. = Bridge
583 .... .... ...1 .... = Router
584 Management Address
585 0001 000. .... .... = TLV Type: Management Address (8)
586 .... ...0 0000 1100 = TLV Length: 12
587 Address String Length: 5
588 Address Subtype: IPv4 (1)
589 Management Address: 10.238.80.75
590 Interface Subtype: ifIndex (2)
591 Interface Number: 3
592 OID String Length: 0
593 Port Description = eth0
594 0000 100. .... .... = TLV Type: Port Description (4)
595 .... ...0 0000 0100 = TLV Length: 4
596 Port Description: eth0
597 IEEE 802.3 - Link Aggregation
598 1111 111. .... .... = TLV Type: Organization Specific (127)
599 .... ...0 0000 1001 = TLV Length: 9
600 Organization Unique Code: IEEE 802.3 (0x00120f)
601 IEEE 802.3 Subtype: Link Aggregation (0x03)
602 Aggregation Status: 0x01
603 .... ...1 = Aggregation Capability: Yes
604 .... ..0. = Aggregation Status: Not Enabled
605 Aggregated Port Id: 0
606 IEEE 802.3 - MAC/PHY Configuration/Status
607 1111 111. .... .... = TLV Type: Organization Specific (127)
608 .... ...0 0000 1001 = TLV Length: 9
609 Organization Unique Code: IEEE 802.3 (0x00120f)
610 IEEE 802.3 Subtype: MAC/PHY Configuration/Status (0x01)
611 Auto-Negotiation Support/Status: 0x03
612 .... ...1 = Auto-Negotiation: Supported
613 .... ..1. = Auto-Negotiation: Enabled
614 PMD Auto-Negotiation Advertised Capability: 0x6C03
615 .... .... .... ...1 = 1000BASE-T (full duplex mode)
616 .... .... .... ..1. = 1000BASE-T (half duplex mode)
617 .... .1.. .... .... = 100BASE-TX (full duplex mode)
618 .... 1... .... .... = 100BASE-TX (half duplex mode)
619 ..1. .... .... .... = 10BASE-T (full duplex mode)
620 .1.. .... .... .... = 10BASE-T (half duplex mode)
621 Operational MAU Type: 100BaseTXFD - 2 pair category 5 UTP, full duplex mode (0x0010)
622 IEEE 802.3 - Maximum Frame Size
623 1111 111. .... .... = TLV Type: Organization Specific (127)
624 .... ...0 0000 0110 = TLV Length: 6
625 Organization Unique Code: IEEE 802.3 (0x00120f)
626 IEEE 802.3 Subtype: Maximum Frame Size (0x04)
627 Maximum Frame Size: 1500
628 TIA - Media Capabilities
629 1111 111. .... .... = TLV Type: Organization Specific (127)
630 .... ...0 0000 0111 = TLV Length: 7
631 Organization Unique Code: TIA (0x0012bb)
632 Media Subtype: Media Capabilities (0x01)
633 Capabilities: 0x0000
634 Class Type: Type Not Defined
635 TIA - Inventory - Hardware Revision
636 1111 111. .... .... = TLV Type: Organization Specific (127)
637 .... ...0 0000 1111 = TLV Length: 15
638 Organization Unique Code: TIA (0x0012bb)
639 Media Subtype: Inventory - Hardware Revision (0x05)
640 Hardware Revision: ND991789702
641 TIA - Inventory - Firmware Revision
642 1111 111. .... .... = TLV Type: Organization Specific (127)
643 .... ...0 0000 1011 = TLV Length: 10
644 Organization Unique Code: TIA (0x0012bb)
645 Media Subtype: Inventory - Firmware Revision (0x06)
646 Firmware Revision: 080012
647 TIA - Inventory - Software Revision
648 1111 111. .... .... = TLV Type: Organization Specific (127)
649 .... ...0 0001 0010 = TLV Length: 18
650 Organization Unique Code: TIA (0x0012bb)
651 Media Subtype: Inventory - Software Revision (0x07)
652 Software Revision: 2.6.29-2-amd64
653 TIA - Inventory - Serial Number
654 1111 111. .... .... = TLV Type: Organization Specific (127)
655 .... ...0 0001 0000 = TLV Length: 16
656 Organization Unique Code: TIA (0x0012bb)
657 Media Subtype: Inventory - Serial Number (0x08)
658 Serial Number: 105820850009
659 TIA - Inventory - Manufacturer Name
660 1111 111. .... .... = TLV Type: Organization Specific (127)
661 .... ...0 0001 0101 = TLV Length: 21
662 Organization Unique Code: TIA (0x0012bb)
663 Media Subtype: Inventory - Manufacturer Name (0x09)
664 Manufacturer Name: NEC Computers SAS
665 TIA - Inventory - Model Name
666 1111 111. .... .... = TLV Type: Organization Specific (127)
667 .... ...0 0001 0011 = TLV Length: 19
668 Organization Unique Code: TIA (0x0012bb)
669 Media Subtype: Inventory - Model Name (0x0a)
670 Model Name: POWERMATE VL350
671 TIA - Inventory - Asset ID
672 1111 111. .... .... = TLV Type: Organization Specific (127)
673 .... ...0 0000 1101 = TLV Length: 13
674 Organization Unique Code: TIA (0x0012bb)
675 Media Subtype: Inventory - Asset ID (0x0b)
676 Asset ID: 100207120
677 End of LLDPDU
678 0000 000. .... .... = TLV Type: End of LLDPDU (0)
679 .... ...0 0000 0000 = TLV Length: 0
680 */
681 struct lldpd_chassis *nchassis = NULL;
682 struct lldpd_port *nport = NULL;
683 char mac1[] = { 0x00, 0x16, 0x17, 0x2f, 0xa1, 0xb6 };
684
685 fail_unless(lldp_decode(NULL, pkt1, sizeof(pkt1), &hardware,
686 &nchassis, &nport) != -1);
9360858c
VB
687 if (!nchassis || !nport) {
688 fail("unable to decode packet");
689 return;
690 }
654f6246
VB
691 ck_assert_int_eq(nchassis->c_id_subtype,
692 LLDP_CHASSISID_SUBTYPE_LLADDR);
693 ck_assert_int_eq(nchassis->c_id_len, ETH_ALEN);
694 fail_unless(memcmp(mac1, nchassis->c_id, ETH_ALEN) == 0);
695 ck_assert_int_eq(nport->p_id_subtype,
696 LLDP_PORTID_SUBTYPE_LLADDR);
697 ck_assert_int_eq(nport->p_id_len, ETH_ALEN);
698 fail_unless(memcmp(mac1, nport->p_id, ETH_ALEN) == 0);
699 ck_assert_int_eq(nchassis->c_ttl, 120);
700 ck_assert_str_eq(nchassis->c_name, "naruto.XXXXXXXXXXXXXXXXXXX");
701 ck_assert_str_eq(nchassis->c_descr,
702 "Linux 2.6.29-2-amd64 #1 SMP Sun May 17 17:15:47 UTC 2009 x86_64");
703 ck_assert_str_eq(nport->p_descr, "eth0");
704 ck_assert_int_eq(nchassis->c_cap_available,
705 LLDP_CAP_WLAN | LLDP_CAP_ROUTER | LLDP_CAP_BRIDGE);
706 ck_assert_int_eq(nchassis->c_cap_enabled,
707 LLDP_CAP_ROUTER | LLDP_CAP_BRIDGE);
e6b36c87 708 ck_assert_int_eq(nchassis->c_mgmt.tqh_first->m_addr.inet.s_addr,
654f6246 709 (u_int32_t)inet_addr("10.238.80.75"));
e6b36c87 710 ck_assert_int_eq(nchassis->c_mgmt.tqh_first->m_iface, 3);
31375299 711#ifdef ENABLE_DOT3
654f6246 712 ck_assert_int_eq(nport->p_aggregid, 0);
3fd015c0
VB
713 ck_assert_int_eq(nport->p_macphy.autoneg_enabled, 1);
714 ck_assert_int_eq(nport->p_macphy.autoneg_support, 1);
715 ck_assert_int_eq(nport->p_macphy.autoneg_advertised,
654f6246
VB
716 LLDP_DOT3_LINK_AUTONEG_1000BASE_TFD |
717 LLDP_DOT3_LINK_AUTONEG_1000BASE_T |
718 LLDP_DOT3_LINK_AUTONEG_100BASE_TXFD |
719 LLDP_DOT3_LINK_AUTONEG_100BASE_TX |
720 LLDP_DOT3_LINK_AUTONEG_10BASET_FD |
721 LLDP_DOT3_LINK_AUTONEG_10BASE_T);
3fd015c0 722 ck_assert_int_eq(nport->p_macphy.mau_type,
654f6246
VB
723 LLDP_DOT3_MAU_100BASETXFD);
724 ck_assert_int_eq(nport->p_mfs, 1500);
31375299
VB
725#endif
726#ifdef ENABLE_LLDPMED
654f6246
VB
727 ck_assert_int_eq(nchassis->c_med_type, 0);
728 ck_assert_str_eq(nchassis->c_med_hw, "ND991789702");
729 ck_assert_str_eq(nchassis->c_med_fw, "080012 "); /* Extra space */
730 ck_assert_str_eq(nchassis->c_med_sw, "2.6.29-2-amd64");
731 ck_assert_str_eq(nchassis->c_med_sn, "105820850009");
732 ck_assert_str_eq(nchassis->c_med_manuf, "NEC Computers SAS");
733 ck_assert_str_eq(nchassis->c_med_model, "POWERMATE VL350");
734 ck_assert_str_eq(nchassis->c_med_asset, "100207120");
31375299 735#endif
654f6246
VB
736}
737END_TEST
738
fedf4d77
VB
739Suite *
740lldp_suite(void)
741{
742 Suite *s = suite_create("LLDP");
5fd6695c
VB
743 TCase *tc_send = tcase_create("Send LLDP packets");
744 TCase *tc_receive = tcase_create("Receive LLDP packets");
fedf4d77
VB
745
746 /* Send tests are first run without knowing the result. The
747 result is then checked with:
748 tshark -V -T text -r tests/lldp_send_0000.pcap
749
750 If the result is correct, then, we get the packet as C
751 bytes using wireshark export to C arrays (tshark seems not
752 be able to do this).
753 */
754
4dc61015 755 tcase_add_checked_fixture(tc_send, pcap_setup, pcap_teardown);
bc99d796 756 tcase_add_test(tc_send, test_send_rcv_basic);
31375299 757#ifdef ENABLE_DOT1
bc99d796 758 tcase_add_test(tc_send, test_send_rcv_dot1_tlvs);
31375299
VB
759#endif
760#ifdef ENABLE_LLDPMED
bc99d796 761 tcase_add_test(tc_send, test_send_rcv_med);
31375299
VB
762#endif
763#ifdef ENABLE_DOT3
bc99d796 764 tcase_add_test(tc_send, test_send_rcv_dot3);
31375299 765#endif
fedf4d77
VB
766 suite_add_tcase(s, tc_send);
767
654f6246
VB
768 tcase_add_test(tc_receive, test_recv_min);
769 tcase_add_test(tc_receive, test_recv_lldpd);
fedf4d77
VB
770 suite_add_tcase(s, tc_receive);
771
772 return s;
773}
774
775int
776main()
777{
778 int number_failed;
779 Suite *s = lldp_suite ();
780 SRunner *sr = srunner_create (s);
781 srunner_set_fork_status (sr, CK_NOFORK); /* Can't fork because
782 we need to write
783 files */
784 srunner_run_all (sr, CK_ENV);
785 number_failed = srunner_ntests_failed (sr);
786 srunner_free (sr);
787 return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
788}