]> git.ipfire.org Git - thirdparty/lldpd.git/blob - tests/check_cdp.c
tests: display tests/test-suite.log on errors
[thirdparty/lldpd.git] / tests / check_cdp.c
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
18 #include <stdlib.h>
19 #include <sys/socket.h>
20 #include <arpa/inet.h>
21 #include <netinet/in.h>
22 #include <check.h>
23 #include "common.h"
24
25 char filenameprefix[] = "cdp_send";
26
27 #ifdef ENABLE_CDP
28
29 START_TEST (test_send_cdpv1)
30 {
31 int n;
32 /* Packet we should build:
33 IEEE 802.3 Ethernet
34 Destination: CDP/VTP/DTP/PAgP/UDLD (01:00:0c:cc:cc:cc)
35 Source: 5e:10:8e:e7:84:ad (5e:10:8e:e7:84:ad)
36 Length: 106
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: Cisco (0x00000c)
46 PID: CDP (0x2000)
47 Cisco Discovery Protocol
48 Version: 1
49 TTL: 180 seconds
50 Checksum: 0x3af7 [correct]
51 [Good: True]
52 [Bad : False]
53 Device ID: First chassis
54 Type: Device ID (0x0001)
55 Length: 17
56 Device ID: First chassis
57 Addresses
58 Type: Addresses (0x0002)
59 Length: 17
60 Number of addresses: 1
61 IP address: 172.17.142.37
62 Protocol type: NLPID
63 Protocol length: 1
64 Protocol: IP
65 Address length: 4
66 IP address: 172.17.142.37
67 Port ID: FastEthernet 1/5
68 Type: Port ID (0x0003)
69 Length: 20
70 Sent through Interface: FastEthernet 1/5
71 Capabilities
72 Type: Capabilities (0x0004)
73 Length: 8
74 Capabilities: 0x00000011
75 .... .... .... .... .... .... .... ...1 = Is a Router
76 .... .... .... .... .... .... .... ..0. = Not a Transparent Bridge
77 .... .... .... .... .... .... .... .0.. = Not a Source Route Bridge
78 .... .... .... .... .... .... .... 0... = Not a Switch
79 .... .... .... .... .... .... ...1 .... = Is a Host
80 .... .... .... .... .... .... ..0. .... = Not IGMP capable
81 .... .... .... .... .... .... .0.. .... = Not a Repeater
82 Software Version
83 Type: Software version (0x0005)
84 Length: 23
85 Software Version: Chassis description
86 Platform: Linux
87 Type: Platform (0x0006)
88 Length: 9
89 Platform: Linux
90 */
91 char pkt1[] = {
92 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc, 0x5e, 0x10,
93 0x8e, 0xe7, 0x84, 0xad, 0x00, 0x6a, 0xaa, 0xaa,
94 0x03, 0x00, 0x00, 0x0c, 0x20, 0x00, 0x01, 0xb4,
95 0x3a, 0xf7, 0x00, 0x01, 0x00, 0x11, 0x46, 0x69,
96 0x72, 0x73, 0x74, 0x20, 0x63, 0x68, 0x61, 0x73,
97 0x73, 0x69, 0x73, 0x00, 0x02, 0x00, 0x11, 0x00,
98 0x00, 0x00, 0x01, 0x01, 0x01, 0xcc, 0x00, 0x04,
99 0xac, 0x11, 0x8e, 0x25, 0x00, 0x03, 0x00, 0x14,
100 0x46, 0x61, 0x73, 0x74, 0x45, 0x74, 0x68, 0x65,
101 0x72, 0x6e, 0x65, 0x74, 0x20, 0x31, 0x2f, 0x35,
102 0x00, 0x04, 0x00, 0x08, 0x00, 0x00, 0x00, 0x11,
103 0x00, 0x05, 0x00, 0x17, 0x43, 0x68, 0x61, 0x73,
104 0x73, 0x69, 0x73, 0x20, 0x64, 0x65, 0x73, 0x63,
105 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x00,
106 0x06, 0x00, 0x09, 0x4c, 0x69, 0x6e, 0x75, 0x78 };
107 struct packet *pkt;
108 in_addr_t addr;
109 struct lldpd_mgmt *mgmt;
110 struct lldpd cfg = {
111 .g_config = {
112 .c_ttl = 180,
113 .c_platform = "Linux"
114 }
115 };
116
117 /* Populate port and chassis */
118 hardware.h_lport.p_id_subtype = LLDP_PORTID_SUBTYPE_IFNAME;
119 hardware.h_lport.p_id = "Not used";
120 hardware.h_lport.p_id_len = strlen(hardware.h_lport.p_id);
121 hardware.h_lport.p_descr = "FastEthernet 1/5";
122 chassis.c_id_subtype = LLDP_CHASSISID_SUBTYPE_LLADDR;
123 chassis.c_id = macaddress;
124 chassis.c_id_len = ETHER_ADDR_LEN;
125 chassis.c_name = "First chassis";
126 chassis.c_descr = "Chassis description";
127 chassis.c_cap_available = chassis.c_cap_enabled = LLDP_CAP_ROUTER;
128 TAILQ_INIT(&chassis.c_mgmt);
129 addr = inet_addr("172.17.142.37");
130 mgmt = lldpd_alloc_mgmt(LLDPD_AF_IPV4,
131 &addr, sizeof(in_addr_t), 0);
132 if (mgmt == NULL)
133 ck_abort();
134 TAILQ_INSERT_TAIL(&chassis.c_mgmt, mgmt, m_entries);
135
136 /* Build packet */
137 n = cdpv1_send(&cfg, &hardware);
138 if (n != 0) {
139 fail("unable to build packet");
140 return;
141 }
142 if (TAILQ_EMPTY(&pkts)) {
143 fail("no packets sent");
144 return;
145 }
146 pkt = TAILQ_FIRST(&pkts);
147 ck_assert_int_eq(pkt->size, sizeof(pkt1));
148 fail_unless(memcmp(pkt->data, pkt1, sizeof(pkt1)) == 0);
149 fail_unless(TAILQ_NEXT(pkt, next) == NULL, "more than one packet sent");
150 }
151 END_TEST
152
153 START_TEST (test_send_cdpv2)
154 {
155 int n;
156 /* Packet we should build:
157 IEEE 802.3 Ethernet
158 Destination: CDP/VTP/DTP/PAgP/UDLD (01:00:0c:cc:cc:cc)
159 Source: 5e:10:8e:e7:84:ad (5e:10:8e:e7:84:ad)
160 the factory default)
161 Length: 111
162 Logical-Link Control
163 DSAP: SNAP (0xaa)
164 IG Bit: Individual
165 SSAP: SNAP (0xaa)
166 CR Bit: Command
167 Control field: U, func=UI (0x03)
168 000. 00.. = Command: Unnumbered Information (0x00)
169 .... ..11 = Frame type: Unnumbered frame (0x03)
170 Organization Code: Cisco (0x00000c)
171 PID: CDP (0x2000)
172 Cisco Discovery Protocol
173 Version: 2
174 TTL: 180 seconds
175 Checksum: 0x5926 [correct]
176 [Good: True]
177 [Bad : False]
178 Device ID: Second chassis
179 Type: Device ID (0x0001)
180 Length: 18
181 Device ID: Second chassis
182 Addresses
183 Type: Addresses (0x0002)
184 Length: 26
185 Number of addresses: 2
186 IP address: 172.17.142.36
187 Protocol type: NLPID
188 Protocol length: 1
189 Protocol: IP
190 Address length: 4
191 IP address: 172.17.142.36
192 IP address: 172.17.142.38
193 Protocol type: NLPID
194 Protocol length: 1
195 Protocol: IP
196 Address length: 4
197 IP address: 172.17.142.38
198 Port ID: Gigabit Ethernet 5/8
199 Type: Port ID (0x0003)
200 Length: 24
201 Sent through Interface: Gigabit Ethernet 5/8
202 Capabilities
203 Type: Capabilities (0x0004)
204 Length: 8
205 Capabilities: 0x00000019
206 .... .... .... .... .... .... .... ...1 = Is a Router
207 .... .... .... .... .... .... .... ..0. = Not a Transparent Bridge
208 .... .... .... .... .... .... .... .0.. = Not a Source Route Bridge
209 .... .... .... .... .... .... .... 1... = Is a Switch
210 .... .... .... .... .... .... ...1 .... = Is a Host
211 .... .... .... .... .... .... ..0. .... = Not IGMP capable
212 .... .... .... .... .... .... .0.. .... = Not a Repeater
213 Software Version
214 Type: Software version (0x0005)
215 Length: 23
216 Software Version: Chassis description
217 Platform: Linux
218 Type: Platform (0x0006)
219 Length: 9
220 Platform: Linux
221 */
222 char pkt1[] = {
223 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc, 0x5e, 0x10,
224 0x8e, 0xe7, 0x84, 0xad, 0x00, 0x78, 0xaa, 0xaa,
225 0x03, 0x00, 0x00, 0x0c, 0x20, 0x00, 0x02, 0xb4,
226 0xc8, 0x67, 0x00, 0x01, 0x00, 0x12, 0x53, 0x65,
227 0x63, 0x6f, 0x6e, 0x64, 0x20, 0x63, 0x68, 0x61,
228 0x73, 0x73, 0x69, 0x73, 0x00, 0x02, 0x00, 0x1a,
229 0x00, 0x00, 0x00, 0x02, 0x01, 0x01, 0xcc, 0x00,
230 0x04, 0xac, 0x11, 0x8e, 0x24, 0x01, 0x01, 0xcc,
231 0x00, 0x04, 0xac, 0x11, 0x8e, 0x26, 0x00, 0x03,
232 0x00, 0x18, 0x47, 0x69, 0x67, 0x61, 0x62, 0x69,
233 0x74, 0x20, 0x45, 0x74, 0x68, 0x65, 0x72, 0x6e,
234 0x65, 0x74, 0x20, 0x35, 0x2f, 0x38, 0x00, 0x04,
235 0x00, 0x08, 0x00, 0x00, 0x00, 0x19, 0x00, 0x05,
236 0x00, 0x17, 0x43, 0x68, 0x61, 0x73, 0x73, 0x69,
237 0x73, 0x20, 0x64, 0x65, 0x73, 0x63, 0x72, 0x69,
238 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x00, 0x06, 0x00,
239 0x09, 0x4c, 0x69, 0x6e, 0x75, 0x78 };
240 struct packet *pkt;
241 in_addr_t addr1;
242 in_addr_t addr2;
243 struct lldpd_mgmt *mgmt1;
244 struct lldpd_mgmt *mgmt2;
245 struct lldpd cfg = {
246 .g_config = {
247 .c_ttl = 180,
248 .c_platform = "Linux"
249 }
250 };
251
252 /* Populate port and chassis */
253 hardware.h_lport.p_id_subtype = LLDP_PORTID_SUBTYPE_LLADDR;
254 hardware.h_lport.p_id = macaddress;
255 hardware.h_lport.p_id_len = ETHER_ADDR_LEN;
256 hardware.h_lport.p_descr = "Gigabit Ethernet 5/8";
257 chassis.c_id_subtype = LLDP_CHASSISID_SUBTYPE_LLADDR;
258 chassis.c_id = macaddress;
259 chassis.c_id_len = ETHER_ADDR_LEN;
260 chassis.c_name = "Second chassis";
261 chassis.c_descr = "Chassis description";
262 chassis.c_cap_available = chassis.c_cap_enabled =
263 LLDP_CAP_ROUTER | LLDP_CAP_BRIDGE;
264 TAILQ_INIT(&chassis.c_mgmt);
265 addr1 = inet_addr("172.17.142.36");
266 addr2 = inet_addr("172.17.142.38");
267 mgmt1 = lldpd_alloc_mgmt(LLDPD_AF_IPV4,
268 &addr1, sizeof(in_addr_t), 0);
269 mgmt2 = lldpd_alloc_mgmt(LLDPD_AF_IPV4,
270 &addr2, sizeof(in_addr_t), 0);
271 if (mgmt1 == NULL || mgmt2 == NULL)
272 ck_abort();
273 TAILQ_INSERT_TAIL(&chassis.c_mgmt, mgmt1, m_entries);
274 TAILQ_INSERT_TAIL(&chassis.c_mgmt, mgmt2, m_entries);
275
276 /* Build packet */
277 n = cdpv2_send(&cfg, &hardware);
278 if (n != 0) {
279 fail("unable to build packet");
280 return;
281 }
282 if (TAILQ_EMPTY(&pkts)) {
283 fail("no packets sent");
284 return;
285 }
286 pkt = TAILQ_FIRST(&pkts);
287 ck_assert_int_eq(pkt->size, sizeof(pkt1));
288 fail_unless(memcmp(pkt->data, pkt1, sizeof(pkt1)) == 0);
289 fail_unless(TAILQ_NEXT(pkt, next) == NULL, "more than one packet sent");
290 }
291 END_TEST
292
293 START_TEST (test_recv_cdpv1)
294 {
295 char pkt1[] = {
296 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc, 0x00, 0xe0,
297 0x1e, 0xd5, 0xd5, 0x15, 0x01, 0x1e, 0xaa, 0xaa,
298 0x03, 0x00, 0x00, 0x0c, 0x20, 0x00, 0x01, 0xb4,
299 0xdf, 0xf0, 0x00, 0x01, 0x00, 0x06, 0x52, 0x31,
300 0x00, 0x02, 0x00, 0x11, 0x00, 0x00, 0x00, 0x01,
301 0x01, 0x01, 0xcc, 0x00, 0x04, 0xc0, 0xa8, 0x0a,
302 0x01, 0x00, 0x03, 0x00, 0x0d, 0x45, 0x74, 0x68,
303 0x65, 0x72, 0x6e, 0x65, 0x74, 0x30, 0x00, 0x04,
304 0x00, 0x08, 0x00, 0x00, 0x00, 0x11, 0x00, 0x05,
305 0x00, 0xd8, 0x43, 0x69, 0x73, 0x63, 0x6f, 0x20,
306 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74,
307 0x77, 0x6f, 0x72, 0x6b, 0x20, 0x4f, 0x70, 0x65,
308 0x72, 0x61, 0x74, 0x69, 0x6e, 0x67, 0x20, 0x53,
309 0x79, 0x73, 0x74, 0x65, 0x6d, 0x20, 0x53, 0x6f,
310 0x66, 0x74, 0x77, 0x61, 0x72, 0x65, 0x20, 0x0a,
311 0x49, 0x4f, 0x53, 0x20, 0x28, 0x74, 0x6d, 0x29,
312 0x20, 0x31, 0x36, 0x30, 0x30, 0x20, 0x53, 0x6f,
313 0x66, 0x74, 0x77, 0x61, 0x72, 0x65, 0x20, 0x28,
314 0x43, 0x31, 0x36, 0x30, 0x30, 0x2d, 0x4e, 0x59,
315 0x2d, 0x4c, 0x29, 0x2c, 0x20, 0x56, 0x65, 0x72,
316 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x31, 0x31, 0x2e,
317 0x32, 0x28, 0x31, 0x32, 0x29, 0x50, 0x2c, 0x20,
318 0x52, 0x45, 0x4c, 0x45, 0x41, 0x53, 0x45, 0x20,
319 0x53, 0x4f, 0x46, 0x54, 0x57, 0x41, 0x52, 0x45,
320 0x20, 0x28, 0x66, 0x63, 0x31, 0x29, 0x0a, 0x43,
321 0x6f, 0x70, 0x79, 0x72, 0x69, 0x67, 0x68, 0x74,
322 0x20, 0x28, 0x63, 0x29, 0x20, 0x31, 0x39, 0x38,
323 0x36, 0x2d, 0x31, 0x39, 0x39, 0x38, 0x20, 0x62,
324 0x79, 0x20, 0x63, 0x69, 0x73, 0x63, 0x6f, 0x20,
325 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x73, 0x2c,
326 0x20, 0x49, 0x6e, 0x63, 0x2e, 0x0a, 0x43, 0x6f,
327 0x6d, 0x70, 0x69, 0x6c, 0x65, 0x64, 0x20, 0x54,
328 0x75, 0x65, 0x20, 0x30, 0x33, 0x2d, 0x4d, 0x61,
329 0x72, 0x2d, 0x39, 0x38, 0x20, 0x30, 0x36, 0x3a,
330 0x33, 0x33, 0x20, 0x62, 0x79, 0x20, 0x64, 0x73,
331 0x63, 0x68, 0x77, 0x61, 0x72, 0x74, 0x00, 0x06,
332 0x00, 0x0e, 0x63, 0x69, 0x73, 0x63, 0x6f, 0x20,
333 0x31, 0x36, 0x30, 0x31 };
334 /* This is:
335 IEEE 802.3 Ethernet
336 Destination: CDP/VTP/DTP/PAgP/UDLD (01:00:0c:cc:cc:cc)
337 Source: Cisco_d5:d5:15 (00:e0:1e:d5:d5:15)
338 Length: 286
339 Logical-Link Control
340 DSAP: SNAP (0xaa)
341 IG Bit: Individual
342 SSAP: SNAP (0xaa)
343 CR Bit: Command
344 Control field: U, func=UI (0x03)
345 000. 00.. = Command: Unnumbered Information (0x00)
346 .... ..11 = Frame type: Unnumbered frame (0x03)
347 Organization Code: Cisco (0x00000c)
348 PID: CDP (0x2000)
349 Cisco Discovery Protocol
350 Version: 1
351 TTL: 180 seconds
352 Checksum: 0xdff0 [correct]
353 [Good: True]
354 [Bad : False]
355 Device ID: R1
356 Type: Device ID (0x0001)
357 Length: 6
358 Device ID: R1
359 Addresses
360 Type: Addresses (0x0002)
361 Length: 17
362 Number of addresses: 1
363 IP address: 192.168.10.1
364 Protocol type: NLPID
365 Protocol length: 1
366 Protocol: IP
367 Address length: 4
368 IP address: 192.168.10.1
369 Port ID: Ethernet0
370 Type: Port ID (0x0003)
371 Length: 13
372 Sent through Interface: Ethernet0
373 Capabilities
374 Type: Capabilities (0x0004)
375 Length: 8
376 Capabilities: 0x00000011
377 .... .... .... .... .... .... .... ...1 = Is a Router
378 .... .... .... .... .... .... .... ..0. = Not a Transparent Bridge
379 .... .... .... .... .... .... .... .0.. = Not a Source Route Bridge
380 .... .... .... .... .... .... .... 0... = Not a Switch
381 .... .... .... .... .... .... ...1 .... = Is a Host
382 .... .... .... .... .... .... ..0. .... = Not IGMP capable
383 .... .... .... .... .... .... .0.. .... = Not a Repeater
384 Software Version
385 Type: Software version (0x0005)
386 Length: 216
387 Software Version: Cisco Internetwork Operating System Software
388 IOS (tm) 1600 Software (C1600-NY-L), Version 11.2(12)P, RELEASE SOFTWARE (fc1)
389 Copyright (c) 1986-1998 by cisco Systems, Inc.
390 Compiled Tue 03-Mar-98 06:33 by dschwart
391 Platform: cisco 1601
392 Type: Platform (0x0006)
393 Length: 14
394 Platform: cisco 1601
395 */
396 struct lldpd_chassis *nchassis = NULL;
397 struct lldpd_port *nport = NULL;
398
399 fail_unless(cdpv1_guess(pkt1, sizeof(pkt1)));
400 fail_unless(cdp_decode(NULL, pkt1, sizeof(pkt1), &hardware,
401 &nchassis, &nport) != -1);
402 if (!nchassis || !nport) {
403 fail("unable to decode packet");
404 return;
405 }
406 ck_assert_int_eq(nchassis->c_id_subtype,
407 LLDP_CHASSISID_SUBTYPE_LOCAL);
408 ck_assert_int_eq(nchassis->c_id_len, 2);
409 fail_unless(memcmp(nchassis->c_id, "R1", 2) == 0);
410 ck_assert_str_eq(nchassis->c_name, "R1");
411 ck_assert_int_eq(TAILQ_FIRST(&nchassis->c_mgmt)->m_addr.inet.s_addr,
412 (u_int32_t)inet_addr("192.168.10.1"));
413 ck_assert_int_eq(TAILQ_FIRST(&nchassis->c_mgmt)->m_iface, 0);
414 ck_assert_int_eq(nport->p_id_subtype,
415 LLDP_PORTID_SUBTYPE_IFNAME);
416 ck_assert_int_eq(nport->p_id_len, strlen("Ethernet0"));
417 fail_unless(memcmp(nport->p_id,
418 "Ethernet0", strlen("Ethernet0")) == 0);
419 ck_assert_str_eq(nport->p_descr, "Ethernet0");
420 ck_assert_int_eq(nchassis->c_cap_enabled, LLDP_CAP_ROUTER);
421 ck_assert_str_eq(nchassis->c_descr,
422 "cisco 1601 running on\n"
423 "Cisco Internetwork Operating System Software \n"
424 "IOS (tm) 1600 Software (C1600-NY-L), Version 11.2(12)P, RELEASE SOFTWARE (fc1)\n"
425 "Copyright (c) 1986-1998 by cisco Systems, Inc.\n"
426 "Compiled Tue 03-Mar-98 06:33 by dschwart");
427 }
428 END_TEST
429
430 START_TEST (test_recv_cdpv2)
431 {
432 char pkt1[] = {
433 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc, 0xca, 0x00,
434 0x68, 0x46, 0x00, 0x00, 0x01, 0x30, 0xaa, 0xaa,
435 0x03, 0x00, 0x00, 0x0c, 0x20, 0x00, 0x02, 0xb4,
436 0x54, 0x27, 0x00, 0x01, 0x00, 0x0f, 0x72, 0x74,
437 0x62, 0x67, 0x36, 0x74, 0x65, 0x73, 0x74, 0x30,
438 0x31, 0x00, 0x05, 0x00, 0xd3, 0x43, 0x69, 0x73,
439 0x63, 0x6f, 0x20, 0x49, 0x6e, 0x74, 0x65, 0x72,
440 0x6e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x20,
441 0x4f, 0x70, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6e,
442 0x67, 0x20, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d,
443 0x20, 0x53, 0x6f, 0x66, 0x74, 0x77, 0x61, 0x72,
444 0x65, 0x20, 0x0a, 0x49, 0x4f, 0x53, 0x20, 0x28,
445 0x74, 0x6d, 0x29, 0x20, 0x37, 0x32, 0x30, 0x30,
446 0x20, 0x53, 0x6f, 0x66, 0x74, 0x77, 0x61, 0x72,
447 0x65, 0x20, 0x28, 0x43, 0x37, 0x32, 0x30, 0x30,
448 0x2d, 0x50, 0x2d, 0x4d, 0x29, 0x2c, 0x20, 0x56,
449 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x31,
450 0x32, 0x2e, 0x32, 0x28, 0x34, 0x36, 0x29, 0x2c,
451 0x20, 0x52, 0x45, 0x4c, 0x45, 0x41, 0x53, 0x45,
452 0x20, 0x53, 0x4f, 0x46, 0x54, 0x57, 0x41, 0x52,
453 0x45, 0x20, 0x28, 0x66, 0x63, 0x31, 0x29, 0x0a,
454 0x43, 0x6f, 0x70, 0x79, 0x72, 0x69, 0x67, 0x68,
455 0x74, 0x20, 0x28, 0x63, 0x29, 0x20, 0x31, 0x39,
456 0x38, 0x36, 0x2d, 0x32, 0x30, 0x30, 0x37, 0x20,
457 0x62, 0x79, 0x20, 0x63, 0x69, 0x73, 0x63, 0x6f,
458 0x20, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x73,
459 0x2c, 0x20, 0x49, 0x6e, 0x63, 0x2e, 0x0a, 0x43,
460 0x6f, 0x6d, 0x70, 0x69, 0x6c, 0x65, 0x64, 0x20,
461 0x54, 0x68, 0x75, 0x20, 0x32, 0x36, 0x2d, 0x41,
462 0x70, 0x72, 0x2d, 0x30, 0x37, 0x20, 0x32, 0x31,
463 0x3a, 0x35, 0x36, 0x20, 0x62, 0x79, 0x20, 0x70,
464 0x77, 0x61, 0x64, 0x65, 0x00, 0x06, 0x00, 0x11,
465 0x63, 0x69, 0x73, 0x63, 0x6f, 0x20, 0x37, 0x32,
466 0x30, 0x36, 0x56, 0x58, 0x52, 0x00, 0x02, 0x00,
467 0x11, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0xcc,
468 0x00, 0x04, 0xac, 0x42, 0x37, 0x03, 0x00, 0x03,
469 0x00, 0x13, 0x46, 0x61, 0x73, 0x74, 0x45, 0x74,
470 0x68, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x30, 0x2f,
471 0x30, 0x00, 0x04, 0x00, 0x08, 0x00, 0x00, 0x00,
472 0x00, 0x00, 0x0b, 0x00, 0x05, 0x00 };
473 /* This is:
474 IEEE 802.3 Ethernet
475 Destination: CDP/VTP/DTP/PAgP/UDLD (01:00:0c:cc:cc:cc)
476 Source: ca:00:68:46:00:00 (ca:00:68:46:00:00)
477 Length: 304
478 Logical-Link Control
479 DSAP: SNAP (0xaa)
480 IG Bit: Individual
481 SSAP: SNAP (0xaa)
482 CR Bit: Command
483 Control field: U, func=UI (0x03)
484 000. 00.. = Command: Unnumbered Information (0x00)
485 .... ..11 = Frame type: Unnumbered frame (0x03)
486 Organization Code: Cisco (0x00000c)
487 PID: CDP (0x2000)
488 Cisco Discovery Protocol
489 Version: 2
490 TTL: 180 seconds
491 Checksum: 0x5427 [correct]
492 [Good: True]
493 [Bad : False]
494 Device ID: rtbg6test01
495 Type: Device ID (0x0001)
496 Length: 15
497 Device ID: rtbg6test01
498 Software Version
499 Type: Software version (0x0005)
500 Length: 211
501 Software Version: Cisco Internetwork Operating System Software
502 IOS (tm) 7200 Software (C7200-P-M), Version 12.2(46), RELEASE SOFTWARE (fc1)
503 Copyright (c) 1986-2007 by cisco Systems, Inc.
504 Compiled Thu 26-Apr-07 21:56 by pwade
505 Platform: cisco 7206VXR
506 Type: Platform (0x0006)
507 Length: 17
508 Platform: cisco 7206VXR
509 Addresses
510 Type: Addresses (0x0002)
511 Length: 17
512 Number of addresses: 1
513 IP address: 172.66.55.3
514 Protocol type: NLPID
515 Protocol length: 1
516 Protocol: IP
517 Address length: 4
518 IP address: 172.66.55.3
519 Port ID: FastEthernet0/0
520 Type: Port ID (0x0003)
521 Length: 19
522 Sent through Interface: FastEthernet0/0
523 Capabilities
524 Type: Capabilities (0x0004)
525 Length: 8
526 Capabilities: 0x00000000
527 .... .... .... .... .... .... .... ...0 = Not a Router
528 .... .... .... .... .... .... .... ..0. = Not a Transparent Bridge
529 .... .... .... .... .... .... .... .0.. = Not a Source Route Bridge
530 .... .... .... .... .... .... .... 0... = Not a Switch
531 .... .... .... .... .... .... ...0 .... = Not a Host
532 .... .... .... .... .... .... ..0. .... = Not IGMP capable
533 .... .... .... .... .... .... .0.. .... = Not a Repeater
534 Duplex: Half
535 Type: Duplex (0x000b)
536 Length: 5
537 Duplex: Half
538 */
539 struct lldpd_chassis *nchassis = NULL;
540 struct lldpd_port *nport = NULL;
541
542 fail_unless(cdpv2_guess(pkt1, sizeof(pkt1)));
543 fail_unless(cdp_decode(NULL, pkt1, sizeof(pkt1), &hardware,
544 &nchassis, &nport) != -1);
545 if (!nchassis || !nport) {
546 fail("unable to decode packet");
547 return;
548 }
549 ck_assert_int_eq(nchassis->c_id_subtype,
550 LLDP_CHASSISID_SUBTYPE_LOCAL);
551 ck_assert_int_eq(nchassis->c_id_len, strlen("rtbg6test01"));
552 fail_unless(memcmp(nchassis->c_id,
553 "rtbg6test01", strlen("rtbg6test01")) == 0);
554 ck_assert_str_eq(nchassis->c_name, "rtbg6test01");
555 ck_assert_int_eq(TAILQ_FIRST(&nchassis->c_mgmt)->m_addr.inet.s_addr,
556 (u_int32_t)inet_addr("172.66.55.3"));
557 ck_assert_int_eq(TAILQ_FIRST(&nchassis->c_mgmt)->m_iface, 0);
558 ck_assert_int_eq(nport->p_id_subtype,
559 LLDP_PORTID_SUBTYPE_IFNAME);
560 ck_assert_int_eq(nport->p_id_len, strlen("FastEthernet0/0"));
561 fail_unless(memcmp(nport->p_id,
562 "FastEthernet0/0", strlen("FastEthernet0/0")) == 0);
563 ck_assert_str_eq(nport->p_descr, "FastEthernet0/0");
564 ck_assert_int_eq(nchassis->c_cap_enabled, LLDP_CAP_STATION);
565 ck_assert_str_eq(nchassis->c_descr,
566 "cisco 7206VXR running on\n"
567 "Cisco Internetwork Operating System Software \n"
568 "IOS (tm) 7200 Software (C7200-P-M), Version 12.2(46), RELEASE SOFTWARE (fc1)\n"
569 "Copyright (c) 1986-2007 by cisco Systems, Inc.\n"
570 "Compiled Thu 26-Apr-07 21:56 by pwade");
571 }
572 END_TEST
573
574 #endif
575
576 Suite *
577 cdp_suite(void)
578 {
579 Suite *s = suite_create("CDP");
580
581 #ifdef ENABLE_CDP
582 TCase *tc_send = tcase_create("Send CDP packets");
583 TCase *tc_receive = tcase_create("Receive CDP packets");
584
585 tcase_add_checked_fixture(tc_send, pcap_setup, pcap_teardown);
586 tcase_add_test(tc_send, test_send_cdpv1);
587 tcase_add_test(tc_send, test_send_cdpv2);
588 suite_add_tcase(s, tc_send);
589
590 tcase_add_test(tc_receive, test_recv_cdpv1);
591 tcase_add_test(tc_receive, test_recv_cdpv2);
592 suite_add_tcase(s, tc_receive);
593 #endif
594
595 return s;
596 }
597
598 int
599 main()
600 {
601 int number_failed;
602 Suite *s = cdp_suite ();
603 SRunner *sr = srunner_create (s);
604 srunner_set_fork_status (sr, CK_NOFORK); /* Can't fork because
605 we need to write
606 files */
607 srunner_run_all (sr, CK_ENV);
608 number_failed = srunner_ntests_failed (sr);
609 srunner_free (sr);
610 return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
611 }