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