test_lldp.cpp 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  1. /*********************************************************************
  2. * _ _ _
  3. * _ __ | |_ _ | | __ _ | |__ ___
  4. * | '__|| __|(_)| | / _` || '_ \ / __|
  5. * | | | |_ _ | || (_| || |_) |\__ \
  6. * |_| \__|(_)|_| \__,_||_.__/ |___/
  7. *
  8. * www.rt-labs.com
  9. * Copyright 2018 rt-labs AB, Sweden.
  10. *
  11. * This software is dual-licensed under GPLv3 and a commercial
  12. * license. See the file LICENSE.md distributed with this software for
  13. * full license information.
  14. ********************************************************************/
  15. /**
  16. * @file
  17. * @brief Integration test that a LLDP frame is sent at init.
  18. *
  19. */
  20. #include "utils_for_testing.h"
  21. #include "mocks.h"
  22. #include "pf_includes.h"
  23. #include <gtest/gtest.h>
  24. #define LOCAL_PORT 1
  25. #define MAX_ETH_FRAME_SIZE 1514u
  26. #define MAX_ETH_PAYLOAD_SIZE 1500u
  27. #define LLDP_TYPE_END 0
  28. #define LLDP_TYPE_CHASSIS_ID 1
  29. #define LLDP_TYPE_PORT_ID 2
  30. #define LLDP_TYPE_TTL 3
  31. #define LLDP_TYPE_PORT_DESCRIPTION 4
  32. #define LLDP_TYPE_MANAGEMENT_ADDRESS 8
  33. #define LLDP_TYPE_ORG_SPEC 127
  34. #define TLV_BYTE_0(len, type) (((len) >> 8) | ((type) << 1))
  35. #define TLV_BYTE_1(len) (((len) >> 0) & 0xff)
  36. #define TLV_TYPE(tlv) ((tlv)[0] >> 1)
  37. #define TLV_LEN(tlv) ((size_t) (((tlv)[0] << 8) + (tlv)[1]) & 0x1FF)
  38. static const char * chassis_id_test_sample_1 =
  39. "\x53\x43\x41\x4c\x41\x4e\x43\x45\x20\x58\x2d\x32\x30\x30\x20"
  40. "\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x36\x47\x4b\x35\x20"
  41. "\x32\x30\x34\x2d\x30\x42\x41\x30\x30\x2d\x32\x42\x41\x33\x20\x20"
  42. "\x53\x56\x50\x4c\x36\x31\x34\x37\x39\x32\x30\x20\x20\x20\x20\x20"
  43. "\x20\x20\x20\x20\x20\x39\x20\x56\x20\x20\x35\x20\x20\x34\x20\x20"
  44. "\x32";
  45. static const char * port_id_test_sample_1 =
  46. "\x70\x6f\x72\x74\x2d\x30\x30\x33\x2e\x61\x62\x63\x64\x65\x66\x67"
  47. "\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70\x71\x72\x73\x74\x75\x76\x77"
  48. "\x78\x79\x7a\x2d\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c"
  49. "\x6d\x6e\x6f\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x31\x32"
  50. "\x33\x34\x35\x36\x37\x38\x39\x30\x2e\x61\x62\x63\x64\x65\x66\x67"
  51. "\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70\x71\x72\x73\x74\x75\x76\x77"
  52. "\x78\x79\x7a\x2d\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c"
  53. "\x6d\x6e\x6f\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x31\x32"
  54. "\x2e\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f"
  55. "\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x2d\x61\x62\x63\x64"
  56. "\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70\x71\x72\x73\x74"
  57. "\x75\x76\x77\x78\x79\x7a\x31\x32\x33\x2e\x61\x62\x63\x64\x65\x66"
  58. "\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70\x71\x72\x73\x74\x75\x76"
  59. "\x77\x78\x79\x7a\x2d\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b"
  60. "\x6c\x6d\x6e\x6f\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x31"
  61. "\x32\x33\x34\x35\x36\x37\x38\x39\x30";
  62. static pf_lldp_chassis_id_t fake_chassis_id (void)
  63. {
  64. pf_lldp_chassis_id_t chassis_id;
  65. memset (&chassis_id, 0, sizeof (chassis_id));
  66. snprintf (
  67. chassis_id.string,
  68. sizeof (chassis_id.string),
  69. "%s",
  70. "Chassis ID received from remote device");
  71. chassis_id.len = strlen (chassis_id.string);
  72. chassis_id.subtype = PF_LLDP_SUBTYPE_LOCALLY_ASSIGNED;
  73. chassis_id.is_valid = true;
  74. return chassis_id;
  75. }
  76. static pf_lldp_port_id_t fake_port_id (void)
  77. {
  78. pf_lldp_port_id_t port_id;
  79. memset (&port_id, 0, sizeof (port_id));
  80. snprintf (
  81. port_id.string,
  82. sizeof (port_id.string),
  83. "%s",
  84. "Port ID received from remote device");
  85. port_id.len = strlen (port_id.string);
  86. port_id.subtype = PF_LLDP_SUBTYPE_LOCALLY_ASSIGNED;
  87. port_id.is_valid = true;
  88. return port_id;
  89. }
  90. static uint16_t fake_ttl (void)
  91. {
  92. return 12345;
  93. }
  94. static pf_lldp_port_description_t fake_port_description (void)
  95. {
  96. pf_lldp_port_description_t port_description;
  97. memset (&port_description, 0, sizeof (port_description));
  98. snprintf (
  99. port_description.string,
  100. sizeof (port_description.string),
  101. "%s",
  102. "Port Description");
  103. port_description.len = strlen (port_description.string);
  104. port_description.is_valid = true;
  105. return port_description;
  106. }
  107. static pf_lldp_management_address_t fake_ipv4_management_address (void)
  108. {
  109. pf_lldp_management_address_t management_address;
  110. memset (&management_address, 0, sizeof (management_address));
  111. management_address.subtype = 1; /* IPv4 */
  112. management_address.value[0] = 192;
  113. management_address.value[1] = 168;
  114. management_address.value[2] = 10;
  115. management_address.value[3] = 102;
  116. management_address.len = 4;
  117. management_address.interface_number.subtype = 2; /* ifIndex */
  118. management_address.interface_number.value = 1;
  119. management_address.is_valid = true;
  120. return management_address;
  121. }
  122. static pf_lldp_management_address_t fake_ipv6_management_address (void)
  123. {
  124. pf_lldp_management_address_t management_address;
  125. memset (&management_address, 0, sizeof (management_address));
  126. management_address.subtype = 2; /* IPv6 */
  127. management_address.value[0] = 0x20;
  128. management_address.value[1] = 0x01;
  129. management_address.value[2] = 0x0d;
  130. management_address.value[3] = 0xd8;
  131. management_address.value[4] = 0x85;
  132. management_address.value[5] = 0xa3;
  133. management_address.value[6] = 0x00;
  134. management_address.value[7] = 0x00;
  135. management_address.value[8] = 0x00;
  136. management_address.value[9] = 0x00;
  137. management_address.value[10] = 0x8a;
  138. management_address.value[11] = 0x2e;
  139. management_address.value[12] = 0x03;
  140. management_address.value[13] = 0x70;
  141. management_address.value[14] = 0x73;
  142. management_address.value[15] = 0x34;
  143. management_address.len = 16;
  144. management_address.interface_number.subtype = 2; /* ifIndex */
  145. management_address.interface_number.value = 1;
  146. management_address.is_valid = true;
  147. return management_address;
  148. }
  149. static pf_lldp_link_status_t fake_phy_config (void)
  150. {
  151. pf_lldp_link_status_t phy_config;
  152. memset (&phy_config, 0, sizeof (phy_config));
  153. phy_config.is_autonegotiation_supported = true;
  154. phy_config.is_autonegotiation_enabled = true;
  155. phy_config.autonegotiation_advertised_capabilities =
  156. PNAL_ETH_AUTONEG_CAP_100BaseTX_HALF_DUPLEX |
  157. PNAL_ETH_AUTONEG_CAP_100BaseTX_FULL_DUPLEX;
  158. phy_config.operational_mau_type = PNAL_ETH_MAU_COPPER_100BaseTX_FULL_DUPLEX;
  159. phy_config.is_valid = true;
  160. return phy_config;
  161. }
  162. static pnet_ethaddr_t fake_mac_address (void)
  163. {
  164. pnet_ethaddr_t mac_address;
  165. memset (&mac_address, 0, sizeof (mac_address));
  166. mac_address.addr[0] = 0xab;
  167. mac_address.addr[0] = 0xcd;
  168. mac_address.addr[0] = 0xef;
  169. mac_address.addr[0] = 0x01;
  170. mac_address.addr[0] = 0x23;
  171. mac_address.addr[0] = 0x45;
  172. return mac_address;
  173. }
  174. static pf_lldp_signal_delay_t fake_port_delay (void)
  175. {
  176. pf_lldp_signal_delay_t port_delay;
  177. memset (&port_delay, 0, sizeof (port_delay));
  178. port_delay.cable_delay_local = 1;
  179. port_delay.rx_delay_local = 2;
  180. port_delay.tx_delay_local = 3;
  181. port_delay.rx_delay_remote = 4;
  182. port_delay.tx_delay_remote = 5;
  183. port_delay.is_valid = true;
  184. return port_delay;
  185. }
  186. static pf_lldp_peer_info_t fake_peer_info (void)
  187. {
  188. pf_lldp_peer_info_t peer;
  189. memset (&peer, 0, sizeof (peer));
  190. peer.chassis_id = fake_chassis_id();
  191. peer.port_id = fake_port_id();
  192. peer.ttl = fake_ttl();
  193. peer.port_description = fake_port_description();
  194. peer.management_address = fake_ipv4_management_address();
  195. peer.phy_config = fake_phy_config();
  196. peer.mac_address = fake_mac_address();
  197. peer.port_delay = fake_port_delay();
  198. return peer;
  199. }
  200. static size_t construct_packet_chassis_id (
  201. uint8_t packet[],
  202. const pf_lldp_chassis_id_t * id)
  203. {
  204. size_t size = 0;
  205. if (id->is_valid)
  206. {
  207. packet[0] = TLV_BYTE_0 (1 + id->len, LLDP_TYPE_CHASSIS_ID);
  208. packet[1] = TLV_BYTE_1 (1 + id->len);
  209. packet[2] = id->subtype;
  210. memcpy (&packet[3], id->string, id->len);
  211. size = 3 + id->len;
  212. }
  213. return size;
  214. }
  215. static size_t construct_packet_port_id (
  216. uint8_t packet[],
  217. const pf_lldp_port_id_t * id)
  218. {
  219. size_t size = 0;
  220. if (id->is_valid)
  221. {
  222. packet[0] = TLV_BYTE_0 (1 + id->len, LLDP_TYPE_PORT_ID);
  223. packet[1] = TLV_BYTE_1 (1 + id->len);
  224. packet[2] = id->subtype;
  225. memcpy (&packet[3], id->string, id->len);
  226. size = 3 + id->len;
  227. }
  228. return size;
  229. }
  230. static size_t construct_packet_ttl (uint8_t packet[], uint16_t ttl)
  231. {
  232. size_t size = 0;
  233. packet[0] = TLV_BYTE_0 (sizeof (ttl), LLDP_TYPE_TTL);
  234. packet[1] = TLV_BYTE_1 (sizeof (ttl));
  235. packet[2] = ttl >> 8;
  236. packet[3] = ttl >> 0;
  237. size = 4;
  238. return size;
  239. }
  240. static size_t construct_packet_port_description (
  241. uint8_t packet[],
  242. const pf_lldp_port_description_t * description)
  243. {
  244. size_t size = 0;
  245. if (description->is_valid)
  246. {
  247. packet[0] = TLV_BYTE_0 (description->len, LLDP_TYPE_PORT_DESCRIPTION);
  248. packet[1] = TLV_BYTE_1 (description->len);
  249. memcpy (&packet[2], description->string, description->len);
  250. size = 2 + description->len;
  251. }
  252. return size;
  253. }
  254. static size_t construct_packet_management_address (
  255. uint8_t packet[],
  256. const pf_lldp_management_address_t * address)
  257. {
  258. size_t size = 0;
  259. if (address->is_valid)
  260. {
  261. uint32_t big_endian;
  262. packet[0] =
  263. TLV_BYTE_0 (2 + address->len + 6, LLDP_TYPE_MANAGEMENT_ADDRESS);
  264. packet[1] = TLV_BYTE_1 (2 + address->len + 6);
  265. packet[2] = 1 + address->len;
  266. packet[3] = address->subtype;
  267. memcpy (&packet[4], address->value, address->len);
  268. size += 4 + address->len;
  269. big_endian = CC_TO_BE32 (address->interface_number.value);
  270. packet[0 + size] = address->interface_number.subtype;
  271. memcpy (&packet[1 + size], &big_endian, 4);
  272. packet[5 + size] = 0; /* OID string length */
  273. size += 6;
  274. }
  275. return size;
  276. }
  277. static size_t construct_packet_link_status (
  278. uint8_t packet[],
  279. const pf_lldp_link_status_t * status)
  280. {
  281. size_t size = 0;
  282. if (status->is_valid)
  283. {
  284. packet[0] = TLV_BYTE_0 (9, LLDP_TYPE_ORG_SPEC);
  285. packet[1] = TLV_BYTE_1 (9);
  286. packet[2] = 0x00; /* 802.3 OUI */
  287. packet[3] = 0x12; /* 802.3 OUI */
  288. packet[4] = 0x0F; /* 802.3 OUI */
  289. packet[5] = 1; /* subtype */
  290. packet[6] = ((uint8_t)status->is_autonegotiation_supported << 0) |
  291. ((uint8_t)status->is_autonegotiation_enabled << 1);
  292. packet[7] = status->autonegotiation_advertised_capabilities >> 8;
  293. packet[8] = status->autonegotiation_advertised_capabilities >> 0;
  294. packet[9] = status->operational_mau_type >> 8;
  295. packet[10] = status->operational_mau_type >> 0;
  296. size = 11;
  297. }
  298. return size;
  299. }
  300. static size_t construct_packet_signal_delay (
  301. uint8_t packet[],
  302. const pf_lldp_signal_delay_t * delay)
  303. {
  304. size_t size = 0;
  305. uint32_t bigendian;
  306. if (delay->is_valid)
  307. {
  308. packet[0] = TLV_BYTE_0 (24, LLDP_TYPE_ORG_SPEC);
  309. packet[1] = TLV_BYTE_1 (24);
  310. packet[2] = 0x00; /* Profinet OUI */
  311. packet[3] = 0x0E; /* Profinet OUI */
  312. packet[4] = 0xCF; /* Profinet OUI */
  313. packet[5] = 1; /* subtype */
  314. bigendian = CC_TO_BE32 (delay->rx_delay_local);
  315. memcpy (&packet[6], &bigendian, 4);
  316. bigendian = CC_TO_BE32 (delay->rx_delay_remote);
  317. memcpy (&packet[10], &bigendian, 4);
  318. bigendian = CC_TO_BE32 (delay->tx_delay_local);
  319. memcpy (&packet[14], &bigendian, 4);
  320. bigendian = CC_TO_BE32 (delay->tx_delay_remote);
  321. memcpy (&packet[18], &bigendian, 4);
  322. bigendian = CC_TO_BE32 (delay->cable_delay_local);
  323. memcpy (&packet[22], &bigendian, 4);
  324. size = 26;
  325. }
  326. return size;
  327. }
  328. static size_t construct_packet_chassis_mac (
  329. uint8_t packet[],
  330. const pnet_ethaddr_t * mac_address)
  331. {
  332. size_t size = 0;
  333. packet[0] = TLV_BYTE_0 (10, LLDP_TYPE_ORG_SPEC);
  334. packet[1] = TLV_BYTE_1 (10);
  335. packet[2] = 0x00; /* Profinet OUI */
  336. packet[3] = 0x0E; /* Profinet OUI */
  337. packet[4] = 0xCF; /* Profinet OUI */
  338. packet[5] = 5; /* subtype */
  339. memcpy (&packet[6], mac_address->addr, 6);
  340. size = 12;
  341. return size;
  342. }
  343. static size_t construct_packet_end_marker (uint8_t packet[])
  344. {
  345. size_t size;
  346. packet[0] = TLV_BYTE_0 (0, LLDP_TYPE_END);
  347. packet[1] = TLV_BYTE_1 (0);
  348. size = 2;
  349. return size;
  350. }
  351. static size_t construct_packet (
  352. uint8_t packet[MAX_ETH_PAYLOAD_SIZE],
  353. const pf_lldp_peer_info_t * data)
  354. {
  355. memset (packet, 0xff, MAX_ETH_PAYLOAD_SIZE);
  356. size_t size = 0;
  357. size += construct_packet_chassis_id (&packet[size], &data->chassis_id);
  358. size += construct_packet_port_id (&packet[size], &data->port_id);
  359. size += construct_packet_ttl (&packet[size], data->ttl);
  360. size += construct_packet_port_description (
  361. &packet[size],
  362. &data->port_description);
  363. size += construct_packet_management_address (
  364. &packet[size],
  365. &data->management_address);
  366. size += construct_packet_link_status (&packet[size], &data->phy_config);
  367. size += construct_packet_signal_delay (&packet[size], &data->port_delay);
  368. size += construct_packet_chassis_mac (&packet[size], &data->mac_address);
  369. size += construct_packet_end_marker (&packet[size]);
  370. return size;
  371. }
  372. class LldpTest : public PnetIntegrationTestBase
  373. {
  374. protected:
  375. virtual void SetUp() override
  376. {
  377. mock_init();
  378. cfg_init();
  379. appdata_init();
  380. available_modules_and_submodules_init();
  381. callcounter_reset();
  382. pnet_init_only (net, &pnet_default_cfg);
  383. /* Do not clear mock or callcounters here - we need to verify send at init
  384. * from LLDP */
  385. };
  386. };
  387. TEST_F (LldpTest, LldpInitTest)
  388. {
  389. /* Verify that LLDP has sent a frame at init */
  390. EXPECT_EQ (appdata.call_counters.state_calls, 0);
  391. EXPECT_EQ (appdata.call_counters.connect_calls, 0);
  392. EXPECT_EQ (appdata.call_counters.release_calls, 0);
  393. EXPECT_EQ (appdata.call_counters.dcontrol_calls, 0);
  394. EXPECT_EQ (appdata.call_counters.ccontrol_calls, 0);
  395. EXPECT_EQ (appdata.call_counters.read_calls, 0);
  396. EXPECT_EQ (appdata.call_counters.write_calls, 0);
  397. EXPECT_EQ (mock_os_data.eth_send_count, PNET_MAX_PHYSICAL_PORTS);
  398. }
  399. TEST_F (LldpTest, LldpGenerateAliasName)
  400. {
  401. char alias[256]; /** Terminated string */
  402. int err;
  403. err = pf_lldp_generate_alias_name (NULL, "dut", alias, 96);
  404. EXPECT_EQ (err, -1);
  405. err = pf_lldp_generate_alias_name ("port-001", NULL, alias, 96);
  406. EXPECT_EQ (err, -1);
  407. err = pf_lldp_generate_alias_name ("port-001", "dut", NULL, 96);
  408. EXPECT_EQ (err, -1);
  409. /* Test legacy PN v2.2
  410. * PortId string does not include a "."
  411. * Alias = PortId.StationName
  412. */
  413. err = pf_lldp_generate_alias_name ("port-001", "dut", alias, 13);
  414. EXPECT_EQ (err, 0);
  415. EXPECT_EQ (strcmp (alias, "port-001.dut"), 0);
  416. memset (alias, 0, sizeof (alias));
  417. err = pf_lldp_generate_alias_name ("port-001", "dut", alias, 12);
  418. EXPECT_EQ (err, -1);
  419. /* Test PN v2.3+
  420. * PortId string does include a "."
  421. * Alias = PortId
  422. */
  423. err = pf_lldp_generate_alias_name ("port-001.dut", "tester", alias, 13);
  424. EXPECT_EQ (err, 0);
  425. EXPECT_EQ (strcmp (alias, "port-001.dut"), 0);
  426. memset (alias, 0, sizeof (alias));
  427. err = pf_lldp_generate_alias_name ("port-001.dut", "tester", alias, 12);
  428. EXPECT_EQ (err, -1);
  429. /* Test data from rt-tester v2.3 style */
  430. err = pf_lldp_generate_alias_name (
  431. port_id_test_sample_1,
  432. chassis_id_test_sample_1,
  433. alias,
  434. 256);
  435. EXPECT_EQ (err, 0);
  436. EXPECT_EQ (strcmp (alias, port_id_test_sample_1), 0);
  437. memset (alias, 0, sizeof (alias));
  438. }
  439. TEST_F (LldpTest, LldpParsePacket)
  440. {
  441. uint8_t packet[MAX_ETH_PAYLOAD_SIZE];
  442. int error;
  443. size_t size;
  444. pf_lldp_peer_info_t actual;
  445. const pf_lldp_peer_info_t expected = fake_peer_info();
  446. size = construct_packet (packet, &expected);
  447. memset (&actual, 0xff, sizeof (actual));
  448. error = pf_lldp_parse_packet (packet, size, &actual);
  449. EXPECT_EQ (error, 0);
  450. EXPECT_EQ (actual.chassis_id.is_valid, expected.chassis_id.is_valid);
  451. EXPECT_EQ (actual.chassis_id.subtype, expected.chassis_id.subtype);
  452. EXPECT_EQ (actual.chassis_id.len, expected.chassis_id.len);
  453. EXPECT_STREQ (actual.chassis_id.string, expected.chassis_id.string);
  454. EXPECT_EQ (actual.port_id.is_valid, expected.port_id.is_valid);
  455. EXPECT_EQ (actual.port_id.subtype, expected.port_id.subtype);
  456. EXPECT_EQ (actual.port_id.len, expected.port_id.len);
  457. EXPECT_STREQ (actual.port_id.string, expected.port_id.string);
  458. EXPECT_EQ (actual.ttl, expected.ttl);
  459. EXPECT_EQ (
  460. actual.port_description.is_valid,
  461. expected.port_description.is_valid);
  462. EXPECT_EQ (actual.port_description.len, expected.port_description.len);
  463. EXPECT_STREQ (
  464. actual.port_description.string,
  465. expected.port_description.string);
  466. EXPECT_EQ (
  467. actual.management_address.is_valid,
  468. expected.management_address.is_valid);
  469. EXPECT_EQ (
  470. actual.management_address.subtype,
  471. expected.management_address.subtype);
  472. EXPECT_EQ (actual.management_address.len, expected.management_address.len);
  473. EXPECT_EQ (
  474. memcmp (
  475. actual.management_address.value,
  476. expected.management_address.value,
  477. 4),
  478. 0);
  479. EXPECT_EQ (
  480. actual.management_address.interface_number.subtype,
  481. expected.management_address.interface_number.subtype);
  482. EXPECT_EQ (
  483. actual.management_address.interface_number.value,
  484. expected.management_address.interface_number.value);
  485. EXPECT_EQ (actual.phy_config.is_valid, expected.phy_config.is_valid);
  486. EXPECT_EQ (
  487. actual.phy_config.is_autonegotiation_supported,
  488. expected.phy_config.is_autonegotiation_supported);
  489. EXPECT_EQ (
  490. actual.phy_config.is_autonegotiation_enabled,
  491. expected.phy_config.is_autonegotiation_enabled);
  492. EXPECT_EQ (
  493. actual.phy_config.autonegotiation_advertised_capabilities,
  494. expected.phy_config.autonegotiation_advertised_capabilities);
  495. EXPECT_EQ (
  496. actual.phy_config.operational_mau_type,
  497. expected.phy_config.operational_mau_type);
  498. EXPECT_EQ (actual.port_delay.is_valid, expected.port_delay.is_valid);
  499. EXPECT_EQ (
  500. actual.port_delay.rx_delay_local,
  501. expected.port_delay.rx_delay_local);
  502. EXPECT_EQ (
  503. actual.port_delay.rx_delay_remote,
  504. expected.port_delay.rx_delay_remote);
  505. EXPECT_EQ (
  506. actual.port_delay.tx_delay_local,
  507. expected.port_delay.tx_delay_local);
  508. EXPECT_EQ (
  509. actual.port_delay.tx_delay_remote,
  510. expected.port_delay.tx_delay_remote);
  511. EXPECT_EQ (
  512. actual.port_delay.cable_delay_local,
  513. expected.port_delay.cable_delay_local);
  514. EXPECT_EQ (memcmp (actual.mac_address.addr, expected.mac_address.addr, 6), 0);
  515. /* TODO: Add check for port status */
  516. /* Finally check that everything is as expected.
  517. * While this is the only check really needed, the checks above will make
  518. * it easier to find out exactly what went wrong.
  519. */
  520. EXPECT_EQ (memcmp (&actual, &expected, sizeof (expected)), 0);
  521. }
  522. TEST_F (LldpTest, LldpParsePacketWithTooBigChassisId)
  523. {
  524. uint8_t packet[MAX_ETH_PAYLOAD_SIZE];
  525. int error;
  526. size_t size;
  527. pf_lldp_peer_info_t actual;
  528. pf_lldp_peer_info_t peer = fake_peer_info();
  529. const size_t max_len = sizeof (peer.chassis_id.string) - 1;
  530. peer.chassis_id.len = max_len + 1;
  531. size = construct_packet (packet, &peer);
  532. memset (&actual, 0xff, sizeof (actual));
  533. error = pf_lldp_parse_packet (packet, size, &actual);
  534. EXPECT_EQ (error, 0);
  535. EXPECT_EQ (actual.chassis_id.is_valid, false);
  536. }
  537. TEST_F (LldpTest, LldpParsePacketWithTooBigPortId)
  538. {
  539. uint8_t packet[MAX_ETH_PAYLOAD_SIZE];
  540. int error;
  541. size_t size;
  542. pf_lldp_peer_info_t actual;
  543. pf_lldp_peer_info_t peer = fake_peer_info();
  544. const size_t max_len = sizeof (peer.port_id.string) - 1;
  545. peer.port_id.len = max_len + 1;
  546. size = construct_packet (packet, &peer);
  547. memset (&actual, 0xff, sizeof (actual));
  548. error = pf_lldp_parse_packet (packet, size, &actual);
  549. EXPECT_EQ (error, 0);
  550. EXPECT_EQ (actual.port_id.is_valid, false);
  551. }
  552. TEST_F (LldpTest, LldpParsePacketWithTooBigPortDescription)
  553. {
  554. uint8_t packet[MAX_ETH_PAYLOAD_SIZE];
  555. int error;
  556. size_t size;
  557. pf_lldp_peer_info_t actual;
  558. pf_lldp_peer_info_t peer = fake_peer_info();
  559. const size_t max_len = sizeof (peer.port_description.string) - 1;
  560. peer.port_description.len = max_len + 1;
  561. size = construct_packet (packet, &peer);
  562. memset (&actual, 0xff, sizeof (actual));
  563. error = pf_lldp_parse_packet (packet, size, &actual);
  564. EXPECT_EQ (error, 0);
  565. EXPECT_EQ (actual.port_description.is_valid, false);
  566. }
  567. TEST_F (LldpTest, LldpParsePacketWithTooBigManagementAddress)
  568. {
  569. uint8_t packet[MAX_ETH_PAYLOAD_SIZE];
  570. int error;
  571. size_t size;
  572. pf_lldp_peer_info_t actual;
  573. pf_lldp_peer_info_t peer = fake_peer_info();
  574. const size_t max_len = sizeof (peer.management_address.value);
  575. peer.management_address.len = max_len + 1;
  576. size = construct_packet (packet, &peer);
  577. memset (&actual, 0xff, sizeof (actual));
  578. error = pf_lldp_parse_packet (packet, size, &actual);
  579. EXPECT_EQ (error, 0);
  580. EXPECT_EQ (actual.management_address.is_valid, false);
  581. }
  582. TEST_F (LldpTest, LldpParsePacketWithMultipleManagementAddresses)
  583. {
  584. uint8_t packet[MAX_ETH_PAYLOAD_SIZE];
  585. int error;
  586. size_t size;
  587. pf_lldp_peer_info_t actual;
  588. const pf_lldp_chassis_id_t chassis_id = fake_chassis_id();
  589. const pf_lldp_port_id_t port_id = fake_port_id();
  590. const uint16_t ttl = fake_ttl();
  591. const pf_lldp_management_address_t ipv4 = fake_ipv4_management_address();
  592. const pf_lldp_management_address_t ipv6 = fake_ipv6_management_address();
  593. /* Construct LLDP packet with IPv4 and IPv6 addresses (in that order) */
  594. memset (packet, 0xff, MAX_ETH_PAYLOAD_SIZE);
  595. size = 0;
  596. size += construct_packet_chassis_id (&packet[size], &chassis_id);
  597. size += construct_packet_port_id (&packet[size], &port_id);
  598. size += construct_packet_ttl (&packet[size], ttl);
  599. size += construct_packet_management_address (&packet[size], &ipv4);
  600. size += construct_packet_management_address (&packet[size], &ipv6);
  601. size += construct_packet_end_marker (&packet[size]);
  602. memset (&actual, 0xff, sizeof (actual));
  603. error = pf_lldp_parse_packet (packet, size, &actual);
  604. /* Note that only the IPv4 address should be saved, as IPv6 addresses are
  605. * of no use for the p-net stack.
  606. */
  607. EXPECT_EQ (error, 0);
  608. EXPECT_EQ (actual.management_address.is_valid, true);
  609. EXPECT_EQ (actual.management_address.subtype, ipv4.subtype);
  610. EXPECT_EQ (actual.management_address.len, 4u);
  611. }
  612. TEST_F (LldpTest, LldpParsePacketWithInvalidManagementAddresses)
  613. {
  614. uint8_t packet[MAX_ETH_PAYLOAD_SIZE];
  615. int error;
  616. size_t size;
  617. pf_lldp_peer_info_t actual;
  618. const pf_lldp_chassis_id_t chassis_id = fake_chassis_id();
  619. const pf_lldp_port_id_t port_id = fake_port_id();
  620. const uint16_t ttl = fake_ttl();
  621. pf_lldp_management_address_t address;
  622. address = fake_ipv4_management_address();
  623. address.len = 5;
  624. /* Construct LLDP packet */
  625. memset (packet, 0xff, MAX_ETH_PAYLOAD_SIZE);
  626. size = 0;
  627. size += construct_packet_chassis_id (&packet[size], &chassis_id);
  628. size += construct_packet_port_id (&packet[size], &port_id);
  629. size += construct_packet_ttl (&packet[size], ttl);
  630. size += construct_packet_management_address (&packet[size], &address);
  631. size += construct_packet_end_marker (&packet[size]);
  632. memset (&actual, 0xff, sizeof (actual));
  633. error = pf_lldp_parse_packet (packet, size, &actual);
  634. EXPECT_EQ (error, 0);
  635. EXPECT_EQ (actual.management_address.is_valid, false);
  636. }
  637. TEST_F (LldpTest, LldpGetChassisId)
  638. {
  639. pf_lldp_chassis_id_t chassis_id;
  640. memset (&chassis_id, 0xff, sizeof (chassis_id));
  641. pf_lldp_get_chassis_id (net, &chassis_id);
  642. EXPECT_EQ (chassis_id.subtype, PF_LLDP_SUBTYPE_LOCALLY_ASSIGNED);
  643. EXPECT_STREQ (
  644. chassis_id.string,
  645. "PNET unit tests <orderid> <serial nbr> 1 P "
  646. " 0 0 0");
  647. EXPECT_EQ (
  648. chassis_id.len,
  649. strlen ("PNET unit tests <orderid> <serial nbr> "
  650. " 1 P 0 0 0"));
  651. }
  652. TEST_F (LldpTest, LldpGetPortId)
  653. {
  654. pf_lldp_port_id_t port_id;
  655. memset (&port_id, 0xff, sizeof (port_id));
  656. pf_lldp_get_port_id (net, LOCAL_PORT, &port_id);
  657. EXPECT_EQ (port_id.subtype, PF_LLDP_SUBTYPE_LOCALLY_ASSIGNED);
  658. EXPECT_STREQ (port_id.string, "port-001.12-34-00-78-90-AB");
  659. EXPECT_EQ (port_id.len, strlen ("port-001.12-34-00-78-90-AB"));
  660. }
  661. TEST_F (LldpTest, LldpGetPortDescription)
  662. {
  663. pf_lldp_port_description_t port_desc;
  664. memset (&port_desc, 0xff, sizeof (port_desc));
  665. pf_lldp_get_port_description (net, LOCAL_PORT, &port_desc);
  666. EXPECT_STREQ (port_desc.string, TEST_INTERFACE_NAME);
  667. EXPECT_EQ (port_desc.len, strlen (TEST_INTERFACE_NAME));
  668. }
  669. TEST_F (LldpTest, LldpGetManagementAddress)
  670. {
  671. pf_lldp_management_address_t man_address;
  672. mock_os_data.interface_index = 3;
  673. memset (&man_address, 0xff, sizeof (man_address));
  674. pf_lldp_get_management_address (net, &man_address);
  675. EXPECT_EQ (man_address.subtype, 1); /* IPv4 */
  676. EXPECT_EQ (man_address.value[0], 192);
  677. EXPECT_EQ (man_address.value[1], 168);
  678. EXPECT_EQ (man_address.value[2], 1);
  679. EXPECT_EQ (man_address.value[3], 171);
  680. EXPECT_EQ (man_address.len, 4u);
  681. EXPECT_EQ (man_address.interface_number.subtype, 2); /* ifIndex */
  682. EXPECT_EQ (man_address.interface_number.value, 3);
  683. }
  684. TEST_F (LldpTest, LldpGetSignalDelays)
  685. {
  686. pf_lldp_signal_delay_t delays;
  687. memset (&delays, 0xff, sizeof (delays));
  688. pf_lldp_get_signal_delays (net, LOCAL_PORT, &delays);
  689. EXPECT_EQ (delays.tx_delay_local, 0u); /* Not measured */
  690. EXPECT_EQ (delays.rx_delay_local, 0u); /* Not measured */
  691. EXPECT_EQ (delays.cable_delay_local, 0u); /* Not measured */
  692. }
  693. TEST_F (LldpTest, LldpGetLinkStatus)
  694. {
  695. pf_lldp_link_status_t link_status;
  696. mock_os_data.eth_status[LOCAL_PORT].is_autonegotiation_supported = true;
  697. mock_os_data.eth_status[LOCAL_PORT].is_autonegotiation_enabled = true;
  698. mock_os_data.eth_status[LOCAL_PORT].autonegotiation_advertised_capabilities =
  699. PNAL_ETH_AUTONEG_CAP_100BaseTX_HALF_DUPLEX |
  700. PNAL_ETH_AUTONEG_CAP_100BaseTX_FULL_DUPLEX;
  701. mock_os_data.eth_status[LOCAL_PORT].operational_mau_type =
  702. PNAL_ETH_MAU_COPPER_100BaseTX_FULL_DUPLEX;
  703. memset (&link_status, 0xff, sizeof (link_status));
  704. /* The ethernet status is updated by the background worker */
  705. pf_pdport_update_eth_status (net);
  706. pf_lldp_get_link_status (net, LOCAL_PORT, &link_status);
  707. EXPECT_EQ (link_status.is_autonegotiation_supported, true);
  708. EXPECT_EQ (link_status.is_autonegotiation_enabled, true);
  709. EXPECT_EQ (
  710. link_status.autonegotiation_advertised_capabilities,
  711. PNAL_ETH_AUTONEG_CAP_100BaseTX_HALF_DUPLEX |
  712. PNAL_ETH_AUTONEG_CAP_100BaseTX_FULL_DUPLEX);
  713. EXPECT_EQ (
  714. link_status.operational_mau_type,
  715. PNAL_ETH_MAU_COPPER_100BaseTX_FULL_DUPLEX);
  716. }
  717. TEST_F (LldpTest, LldpGetLinkStatusGivenAutonegotiationDisabled)
  718. {
  719. pf_lldp_link_status_t link_status;
  720. mock_os_data.eth_status[LOCAL_PORT].is_autonegotiation_supported = false;
  721. mock_os_data.eth_status[LOCAL_PORT].is_autonegotiation_enabled = false;
  722. mock_os_data.eth_status[LOCAL_PORT].autonegotiation_advertised_capabilities =
  723. 0x0000;
  724. mock_os_data.eth_status[LOCAL_PORT].operational_mau_type =
  725. PNAL_ETH_MAU_COPPER_100BaseTX_HALF_DUPLEX;
  726. memset (&link_status, 0xff, sizeof (link_status));
  727. pf_pdport_update_eth_status (net);
  728. pf_lldp_get_link_status (net, LOCAL_PORT, &link_status);
  729. EXPECT_EQ (link_status.is_autonegotiation_supported, false);
  730. EXPECT_EQ (link_status.is_autonegotiation_enabled, false);
  731. EXPECT_EQ (link_status.autonegotiation_advertised_capabilities, 0x0000);
  732. EXPECT_EQ (
  733. link_status.operational_mau_type,
  734. PNAL_ETH_MAU_COPPER_100BaseTX_HALF_DUPLEX);
  735. }
  736. TEST_F (LldpTest, LldpGetPeerTimestamp)
  737. {
  738. uint32_t actual;
  739. const uint32_t expected = 0x87654321;
  740. int error;
  741. pf_lldp_peer_info_t received_data;
  742. /* Nothing received */
  743. error = pf_lldp_get_peer_timestamp (net, LOCAL_PORT, &actual);
  744. EXPECT_EQ (error, -1);
  745. /* Receive fake data at fake timestamp */
  746. mock_os_data.system_uptime_10ms = expected;
  747. received_data = fake_peer_info();
  748. pf_lldp_store_peer_info (net, LOCAL_PORT, &received_data);
  749. error = pf_lldp_get_peer_timestamp (net, LOCAL_PORT, &actual);
  750. EXPECT_EQ (error, 0);
  751. EXPECT_EQ (actual, expected);
  752. }
  753. TEST_F (LldpTest, LldpGetPeerChassisId)
  754. {
  755. const pf_lldp_peer_info_t received = fake_peer_info();
  756. const pf_lldp_chassis_id_t expected = received.chassis_id;
  757. pf_lldp_chassis_id_t actual;
  758. int error;
  759. /* Nothing received */
  760. error = pf_lldp_get_peer_chassis_id (net, LOCAL_PORT, &actual);
  761. EXPECT_EQ (error, -1);
  762. /* Receive fake data */
  763. pf_lldp_store_peer_info (net, LOCAL_PORT, &received);
  764. error = pf_lldp_get_peer_chassis_id (net, LOCAL_PORT, &actual);
  765. EXPECT_EQ (error, 0);
  766. EXPECT_EQ (memcmp (&actual, &expected, sizeof (actual)), 0);
  767. }
  768. TEST_F (LldpTest, LldpGetPeerPortId)
  769. {
  770. const pf_lldp_peer_info_t received = fake_peer_info();
  771. const pf_lldp_port_id_t expected = received.port_id;
  772. pf_lldp_port_id_t actual;
  773. int error;
  774. /* Nothing received */
  775. error = pf_lldp_get_peer_port_id (net, LOCAL_PORT, &actual);
  776. EXPECT_EQ (error, -1);
  777. /* Receive fake data */
  778. pf_lldp_store_peer_info (net, LOCAL_PORT, &received);
  779. error = pf_lldp_get_peer_port_id (net, LOCAL_PORT, &actual);
  780. EXPECT_EQ (error, 0);
  781. EXPECT_EQ (memcmp (&actual, &expected, sizeof (actual)), 0);
  782. }
  783. TEST_F (LldpTest, LldpGetPeerPortDescription)
  784. {
  785. const pf_lldp_peer_info_t received = fake_peer_info();
  786. const pf_lldp_port_description_t expected = received.port_description;
  787. pf_lldp_port_description_t actual;
  788. int error;
  789. /* Nothing received */
  790. error = pf_lldp_get_peer_port_description (net, LOCAL_PORT, &actual);
  791. EXPECT_EQ (error, -1);
  792. /* Receive fake data */
  793. pf_lldp_store_peer_info (net, LOCAL_PORT, &received);
  794. error = pf_lldp_get_peer_port_description (net, LOCAL_PORT, &actual);
  795. EXPECT_EQ (error, 0);
  796. EXPECT_EQ (memcmp (&actual, &expected, sizeof (actual)), 0);
  797. }
  798. TEST_F (LldpTest, LldpGetPeerManagementAddress)
  799. {
  800. const pf_lldp_peer_info_t received = fake_peer_info();
  801. const pf_lldp_management_address_t expected = received.management_address;
  802. pf_lldp_management_address_t actual;
  803. int error;
  804. /* Nothing received */
  805. error = pf_lldp_get_peer_management_address (net, LOCAL_PORT, &actual);
  806. EXPECT_EQ (error, -1);
  807. /* Receive fake data */
  808. pf_lldp_store_peer_info (net, LOCAL_PORT, &received);
  809. error = pf_lldp_get_peer_management_address (net, LOCAL_PORT, &actual);
  810. EXPECT_EQ (error, 0);
  811. EXPECT_EQ (memcmp (&actual, &expected, sizeof (actual)), 0);
  812. }
  813. TEST_F (LldpTest, LldpGetPeerStationName)
  814. {
  815. pf_lldp_station_name_t station_name;
  816. int error;
  817. /* Nothing received */
  818. error = pf_lldp_get_peer_station_name (net, LOCAL_PORT, &station_name);
  819. EXPECT_EQ (error, -1);
  820. /* TODO: Add test case for scenario where LLDP packet has been received */
  821. }
  822. TEST_F (LldpTest, LldpGetPeerSignalDelays)
  823. {
  824. const pf_lldp_peer_info_t received = fake_peer_info();
  825. const pf_lldp_signal_delay_t expected = received.port_delay;
  826. pf_lldp_signal_delay_t actual;
  827. int error;
  828. /* Nothing received */
  829. error = pf_lldp_get_peer_signal_delays (net, LOCAL_PORT, &actual);
  830. EXPECT_EQ (error, -1);
  831. /* Receive fake data */
  832. pf_lldp_store_peer_info (net, LOCAL_PORT, &received);
  833. error = pf_lldp_get_peer_signal_delays (net, LOCAL_PORT, &actual);
  834. EXPECT_EQ (error, 0);
  835. EXPECT_EQ (memcmp (&actual, &expected, sizeof (actual)), 0);
  836. }
  837. TEST_F (LldpTest, LldpGetPeerLinkStatus)
  838. {
  839. const pf_lldp_peer_info_t received = fake_peer_info();
  840. const pf_lldp_link_status_t expected = received.phy_config;
  841. pf_lldp_link_status_t actual;
  842. int error;
  843. /* Nothing received */
  844. error = pf_lldp_get_peer_link_status (net, LOCAL_PORT, &actual);
  845. EXPECT_EQ (error, -1);
  846. /* Receive fake data */
  847. pf_lldp_store_peer_info (net, LOCAL_PORT, &received);
  848. error = pf_lldp_get_peer_link_status (net, LOCAL_PORT, &actual);
  849. EXPECT_EQ (error, 0);
  850. EXPECT_EQ (memcmp (&actual, &expected, sizeof (actual)), 0);
  851. }
  852. TEST_F (LldpTest, LldpConstructFrame)
  853. {
  854. uint8_t frame[MAX_ETH_FRAME_SIZE];
  855. const uint8_t * p;
  856. const uint8_t * tlv;
  857. size_t returned_size;
  858. size_t size;
  859. mock_os_data.interface_index = 3;
  860. returned_size = pf_lldp_construct_frame (net, LOCAL_PORT, frame);
  861. EXPECT_GT (returned_size, 14u);
  862. EXPECT_LE (returned_size, MAX_ETH_FRAME_SIZE);
  863. size = 0;
  864. /* Check destination MAC address (from LLDP standard) */
  865. p = &frame[size];
  866. EXPECT_EQ (p[0], 0x01);
  867. EXPECT_EQ (p[1], 0x80);
  868. EXPECT_EQ (p[2], 0xC2);
  869. EXPECT_EQ (p[3], 0x00);
  870. EXPECT_EQ (p[4], 0x00);
  871. EXPECT_EQ (p[5], 0x0E);
  872. size += 6;
  873. /* Check source MAC address (from app configuration) */
  874. p = &frame[size];
  875. EXPECT_EQ (p[0], 0x12);
  876. EXPECT_EQ (p[1], 0x34);
  877. EXPECT_EQ (p[2], 0x00);
  878. EXPECT_EQ (p[3], 0x78);
  879. EXPECT_EQ (p[4], 0x90);
  880. EXPECT_EQ (p[5], 0xab);
  881. size += 6;
  882. /* Check EtherType (from LLDP standard) */
  883. p = &frame[size];
  884. EXPECT_EQ (p[0], 0x88);
  885. EXPECT_EQ (p[1], 0xCC);
  886. size += 2;
  887. /* Check Chassis ID TLV */
  888. tlv = &frame[size];
  889. EXPECT_EQ (TLV_TYPE (tlv), LLDP_TYPE_CHASSIS_ID);
  890. EXPECT_GE (TLV_LEN (tlv), 2u);
  891. EXPECT_LE (TLV_LEN (tlv), 256u);
  892. size += 2 + TLV_LEN (tlv);
  893. /* Check Port ID TLV */
  894. tlv = &frame[size];
  895. EXPECT_EQ (TLV_TYPE (tlv), LLDP_TYPE_PORT_ID);
  896. EXPECT_GE (TLV_LEN (tlv), 2u);
  897. EXPECT_LE (TLV_LEN (tlv), 256u);
  898. EXPECT_EQ (tlv[2], 7u); /* Subtype */
  899. size += 2 + TLV_LEN (tlv);
  900. /* Check Time To Live TLV */
  901. tlv = &frame[size];
  902. EXPECT_EQ (TLV_TYPE (tlv), LLDP_TYPE_TTL);
  903. EXPECT_EQ (TLV_LEN (tlv), 2u);
  904. EXPECT_EQ (tlv[2], 0u); /* MSB of TTL */
  905. EXPECT_EQ (tlv[3], 20u); /* LSB of TTL */
  906. size += 2 + TLV_LEN (tlv);
  907. /* Note that the order in which TLVs below are placed in the packet
  908. * is not specified by LLDP or Profinet (except for the End TTL).
  909. */
  910. /* Check LLDP_PNIO_PORTSTATUS TLV */
  911. tlv = &frame[size];
  912. EXPECT_EQ (TLV_TYPE (tlv), LLDP_TYPE_ORG_SPEC);
  913. EXPECT_EQ (TLV_LEN (tlv), 3 + 1 + 4u);
  914. EXPECT_EQ (tlv[2], 0x00); /* Profinet OUI */
  915. EXPECT_EQ (tlv[3], 0x0E); /* Profinet OUI */
  916. EXPECT_EQ (tlv[4], 0xCF); /* Profinet OUI */
  917. EXPECT_EQ (tlv[5], 0x02); /* Subtype */
  918. size += 2 + TLV_LEN (tlv);
  919. /* Check LLDP_PNIO_CHASSIS_MAC TLV */
  920. tlv = &frame[size];
  921. EXPECT_EQ (TLV_TYPE (tlv), LLDP_TYPE_ORG_SPEC);
  922. EXPECT_EQ (TLV_LEN (tlv), 3 + 1 + 6u);
  923. EXPECT_EQ (tlv[2], 0x00); /* Profinet OUI */
  924. EXPECT_EQ (tlv[3], 0x0E); /* Profinet OUI */
  925. EXPECT_EQ (tlv[4], 0xCF); /* Profinet OUI */
  926. EXPECT_EQ (tlv[5], 0x05); /* Subtype */
  927. EXPECT_EQ (tlv[6], 0x12); /* MAC address */
  928. EXPECT_EQ (tlv[7], 0x34); /* MAC address */
  929. EXPECT_EQ (tlv[8], 0x00); /* MAC address */
  930. EXPECT_EQ (tlv[9], 0x78); /* MAC address */
  931. EXPECT_EQ (tlv[10], 0x90); /* MAC address */
  932. EXPECT_EQ (tlv[11], 0xab); /* MAC address */
  933. size += 2 + TLV_LEN (tlv);
  934. /* Check 802.3 MAC/PHY Configuration/Status TLV */
  935. tlv = &frame[size];
  936. EXPECT_EQ (TLV_TYPE (tlv), LLDP_TYPE_ORG_SPEC);
  937. EXPECT_EQ (TLV_LEN (tlv), 3 + 1 + 5u);
  938. EXPECT_EQ (tlv[2], 0x00); /* 802.3 OUI */
  939. EXPECT_EQ (tlv[3], 0x12); /* 802.3 OUI */
  940. EXPECT_EQ (tlv[4], 0x0F); /* 802.3 OUI */
  941. EXPECT_EQ (tlv[5], 0x01); /* Subtype */
  942. size += 2 + TLV_LEN (tlv);
  943. /* Check Management Address TLV */
  944. tlv = &frame[size];
  945. EXPECT_EQ (TLV_TYPE (tlv), LLDP_TYPE_MANAGEMENT_ADDRESS);
  946. EXPECT_EQ (TLV_LEN (tlv), 1 + 5 + 5 + 1u);
  947. EXPECT_EQ (tlv[2], 1u + 4u); /* Size of Subtype+Address */
  948. EXPECT_EQ (tlv[3], 1u); /* Subtype IPv4 */
  949. EXPECT_EQ (tlv[4], 192u);
  950. EXPECT_EQ (tlv[5], 168u);
  951. EXPECT_EQ (tlv[6], 1u);
  952. EXPECT_EQ (tlv[7], 171u);
  953. EXPECT_EQ (tlv[8], 2u); /* Subtype ifIndex */
  954. EXPECT_EQ (tlv[9], 0u); /* ifIndex MSB */
  955. EXPECT_EQ (tlv[10], 0u); /* ifIndex */
  956. EXPECT_EQ (tlv[11], 0u); /* ifIndex */
  957. EXPECT_EQ (tlv[12], 3u); /* ifIndex LSB */
  958. EXPECT_EQ (tlv[13], 0u); /* Size of OID */
  959. size += 2 + TLV_LEN (tlv);
  960. /* Check End Of LLDPPDU TLV */
  961. tlv = &frame[size];
  962. EXPECT_EQ (TLV_TYPE (tlv), LLDP_TYPE_END);
  963. EXPECT_EQ (TLV_LEN (tlv), 0u);
  964. size += 2 + TLV_LEN (tlv);
  965. EXPECT_EQ (size, returned_size);
  966. }