blob: b09f5424399263517895a33a6255b610533d1ca7 [file] [log] [blame]
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -06001#include <array>
2
3#include "libpldm/pdr.h"
4#include "libpldm/platform.h"
5
6#include <gtest/gtest.h>
7
8TEST(PDRAccess, testInit)
9{
10 auto repo = pldm_pdr_init();
11 EXPECT_EQ(pldm_pdr_get_record_count(repo), 0);
12 EXPECT_EQ(pldm_pdr_get_repo_size(repo), 0);
13 pldm_pdr_destroy(repo);
14}
15
16TEST(PDRUpdate, testAdd)
17{
18 auto repo = pldm_pdr_init();
19
20 std::array<uint8_t, 10> data{};
21 auto handle = pldm_pdr_add(repo, data.data(), data.size(), 0);
22 EXPECT_EQ(handle, 1);
23 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1);
24 EXPECT_EQ(pldm_pdr_get_repo_size(repo), data.size());
25
26 handle = pldm_pdr_add(repo, data.data(), data.size(), 0);
27 EXPECT_EQ(handle, 2);
28 handle = pldm_pdr_add(repo, data.data(), data.size(), 0);
29 EXPECT_EQ(handle, 3);
Lei YU7cfeb8e2020-02-27 18:24:18 +080030 handle = pldm_pdr_add(repo, data.data(), data.size(), htole32(0xdeeddeed));
Lei YU90ff3ed2020-03-02 11:16:51 +080031 EXPECT_EQ(handle, htole32(0xdeeddeed));
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -060032 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4);
33 EXPECT_EQ(pldm_pdr_get_repo_size(repo), data.size() * 4);
34
35 pldm_pdr_destroy(repo);
36}
37
38TEST(PDRAccess, testGet)
39{
40 auto repo = pldm_pdr_init();
41
42 std::array<uint32_t, 10> in{100, 345, 3, 6, 89, 0, 11, 45, 23434, 123123};
43 pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in.data()), sizeof(in), 1);
44 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1);
45 EXPECT_EQ(pldm_pdr_get_repo_size(repo), sizeof(in));
46 uint32_t size{};
47 uint32_t nextRecHdl{};
48 uint8_t* outData = nullptr;
49 auto hdl = pldm_pdr_find_record(repo, 0, &outData, &size, &nextRecHdl);
50 EXPECT_NE(hdl, nullptr);
51 EXPECT_EQ(size, sizeof(in));
52 EXPECT_EQ(nextRecHdl, 0);
53 EXPECT_EQ(memcmp(outData, in.data(), sizeof(in)), 0);
54 outData = nullptr;
55
56 auto hdl2 = pldm_pdr_find_record(repo, 1, &outData, &size, &nextRecHdl);
57 EXPECT_EQ(hdl, hdl2);
58 EXPECT_NE(hdl, nullptr);
59 EXPECT_EQ(size, sizeof(in));
60 EXPECT_EQ(nextRecHdl, 0);
61 EXPECT_EQ(memcmp(outData, in.data(), sizeof(in)), 0);
62 outData = nullptr;
63
Lei YU7cfeb8e2020-02-27 18:24:18 +080064 hdl = pldm_pdr_find_record(repo, htole32(0xdeaddead), &outData, &size,
65 &nextRecHdl);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -060066 EXPECT_EQ(hdl, nullptr);
67 EXPECT_EQ(size, 0);
68 EXPECT_EQ(nextRecHdl, 0);
69 EXPECT_EQ(outData, nullptr);
70 outData = nullptr;
71
72 std::array<uint32_t, 10> in2{1000, 3450, 30, 60, 890,
73 0, 110, 450, 234034, 123123};
74 pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in2.data()), sizeof(in2), 2);
75 pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in2.data()), sizeof(in2), 3);
76 pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in2.data()), sizeof(in2), 4);
77 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4);
78 EXPECT_EQ(pldm_pdr_get_repo_size(repo), sizeof(in2) * 4);
79 hdl = pldm_pdr_find_record(repo, 0, &outData, &size, &nextRecHdl);
80 EXPECT_NE(hdl, nullptr);
81 EXPECT_EQ(size, sizeof(in));
82 EXPECT_EQ(nextRecHdl, 2);
83 EXPECT_EQ(memcmp(outData, in.data(), sizeof(in)), 0);
84 outData = nullptr;
85 hdl2 = pldm_pdr_find_record(repo, 1, &outData, &size, &nextRecHdl);
86 EXPECT_EQ(hdl, hdl2);
87 EXPECT_NE(hdl, nullptr);
88 EXPECT_EQ(size, sizeof(in));
89 EXPECT_EQ(nextRecHdl, 2);
90 EXPECT_EQ(memcmp(outData, in.data(), sizeof(in)), 0);
91 outData = nullptr;
92 hdl = pldm_pdr_find_record(repo, 2, &outData, &size, &nextRecHdl);
93 EXPECT_NE(hdl, nullptr);
94 EXPECT_EQ(size, sizeof(in2));
95 EXPECT_EQ(nextRecHdl, 3);
96 EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
97 outData = nullptr;
98 hdl = pldm_pdr_find_record(repo, 3, &outData, &size, &nextRecHdl);
99 EXPECT_NE(hdl, nullptr);
100 EXPECT_EQ(size, sizeof(in2));
101 EXPECT_EQ(nextRecHdl, 4);
102 EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
103 outData = nullptr;
104 hdl = pldm_pdr_find_record(repo, 4, &outData, &size, &nextRecHdl);
105 EXPECT_NE(hdl, nullptr);
106 EXPECT_EQ(size, sizeof(in2));
107 EXPECT_EQ(nextRecHdl, 0);
108 EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
109 outData = nullptr;
110
111 pldm_pdr_destroy(repo);
112}
113
114TEST(PDRAccess, testGetNext)
115{
116 auto repo = pldm_pdr_init();
117
118 std::array<uint32_t, 10> in{100, 345, 3, 6, 89, 0, 11, 45, 23434, 123123};
119 pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in.data()), sizeof(in), 1);
120 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1);
121 EXPECT_EQ(pldm_pdr_get_repo_size(repo), sizeof(in));
122 uint32_t size{};
123 uint32_t nextRecHdl{};
124 uint8_t* outData = nullptr;
125 auto hdl = pldm_pdr_find_record(repo, 0, &outData, &size, &nextRecHdl);
126 EXPECT_NE(hdl, nullptr);
127 EXPECT_EQ(size, sizeof(in));
128 EXPECT_EQ(nextRecHdl, 0);
129 EXPECT_EQ(memcmp(outData, in.data(), sizeof(in)), 0);
130 outData = nullptr;
131
132 std::array<uint32_t, 10> in2{1000, 3450, 30, 60, 890,
133 0, 110, 450, 234034, 123123};
134 pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in2.data()), sizeof(in2), 2);
135 pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in2.data()), sizeof(in2), 3);
136 pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in2.data()), sizeof(in2), 4);
137 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4);
138 EXPECT_EQ(pldm_pdr_get_repo_size(repo), sizeof(in2) * 4);
139 hdl = pldm_pdr_get_next_record(repo, hdl, &outData, &size, &nextRecHdl);
140 EXPECT_NE(hdl, nullptr);
141 EXPECT_EQ(size, sizeof(in2));
142 EXPECT_EQ(nextRecHdl, 3);
143 EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
144 outData = nullptr;
145 hdl = pldm_pdr_get_next_record(repo, hdl, &outData, &size, &nextRecHdl);
146 EXPECT_NE(hdl, nullptr);
147 EXPECT_EQ(size, sizeof(in2));
148 EXPECT_EQ(nextRecHdl, 4);
149 EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
150 outData = nullptr;
151 hdl = pldm_pdr_get_next_record(repo, hdl, &outData, &size, &nextRecHdl);
152 EXPECT_NE(hdl, nullptr);
153 EXPECT_EQ(size, sizeof(in2));
154 EXPECT_EQ(nextRecHdl, 0);
155 EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
156 outData = nullptr;
157
158 pldm_pdr_destroy(repo);
159}
160
161TEST(PDRAccess, testFindByType)
162{
163 auto repo = pldm_pdr_init();
164
165 std::array<uint8_t, sizeof(pldm_pdr_hdr)> data{};
166 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(data.data());
167 hdr->type = 1;
168 auto first = pldm_pdr_add(repo, data.data(), data.size(), 0);
169 hdr->type = 2;
170 auto second = pldm_pdr_add(repo, data.data(), data.size(), 0);
171 hdr->type = 3;
172 auto third = pldm_pdr_add(repo, data.data(), data.size(), 0);
173 hdr->type = 4;
174 auto fourth = pldm_pdr_add(repo, data.data(), data.size(), 0);
175
176 uint8_t* outData = nullptr;
177 uint32_t size{};
178 auto firstRec =
179 pldm_pdr_find_record_by_type(repo, 1, nullptr, &outData, &size);
180 EXPECT_EQ(pldm_pdr_get_record_handle(repo, firstRec), first);
181 outData = nullptr;
182 auto secondRec =
183 pldm_pdr_find_record_by_type(repo, 2, nullptr, &outData, &size);
184 EXPECT_EQ(pldm_pdr_get_record_handle(repo, secondRec), second);
185 outData = nullptr;
186 auto thirdRec =
187 pldm_pdr_find_record_by_type(repo, 3, nullptr, &outData, &size);
188 EXPECT_EQ(pldm_pdr_get_record_handle(repo, thirdRec), third);
189 outData = nullptr;
190 auto fourthRec =
191 pldm_pdr_find_record_by_type(repo, 4, nullptr, &outData, &size);
192 EXPECT_EQ(pldm_pdr_get_record_handle(repo, fourthRec), fourth);
193 outData = nullptr;
194 auto fifthRec =
195 pldm_pdr_find_record_by_type(repo, 5, nullptr, &outData, &size);
196 EXPECT_EQ(fifthRec, nullptr);
197 EXPECT_EQ(outData, nullptr);
198 EXPECT_EQ(size, 0);
199
200 auto rec =
201 pldm_pdr_find_record_by_type(repo, 3, secondRec, &outData, &size);
202 EXPECT_EQ(pldm_pdr_get_record_handle(repo, rec), third);
203 outData = nullptr;
204 rec = pldm_pdr_find_record_by_type(repo, 4, secondRec, &outData, &size);
205 EXPECT_EQ(pldm_pdr_get_record_handle(repo, rec), fourth);
206 outData = nullptr;
207 rec = pldm_pdr_find_record_by_type(repo, 2, firstRec, &outData, &size);
208 EXPECT_EQ(pldm_pdr_get_record_handle(repo, rec), second);
209 outData = nullptr;
210
211 pldm_pdr_destroy(repo);
212}
Deepak Kodihallidb914672020-02-07 02:47:45 -0600213
214TEST(PDRUpdate, testAddFruRecordSet)
215{
216 auto repo = pldm_pdr_init();
217
218 auto handle = pldm_pdr_add_fru_record_set(repo, 1, 10, 1, 0, 100);
219 EXPECT_EQ(handle, 1);
220 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1);
221 EXPECT_EQ(pldm_pdr_get_repo_size(repo),
222 sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_fru_record_set));
223 uint32_t size{};
224 uint32_t nextRecHdl{};
225 uint8_t* outData = nullptr;
226 auto hdl = pldm_pdr_find_record(repo, 0, &outData, &size, &nextRecHdl);
227 EXPECT_NE(hdl, nullptr);
228 EXPECT_EQ(size, sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_fru_record_set));
229 EXPECT_EQ(nextRecHdl, 0);
230 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(outData);
231 EXPECT_EQ(hdr->version, 1);
232 EXPECT_EQ(hdr->type, PLDM_PDR_FRU_RECORD_SET);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500233 EXPECT_EQ(hdr->length, htole16(sizeof(pldm_pdr_fru_record_set)));
234 EXPECT_EQ(hdr->record_handle, htole32(1));
Deepak Kodihallidb914672020-02-07 02:47:45 -0600235 pldm_pdr_fru_record_set* fru = reinterpret_cast<pldm_pdr_fru_record_set*>(
236 outData + sizeof(pldm_pdr_hdr));
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500237 EXPECT_EQ(fru->terminus_handle, htole16(1));
238 EXPECT_EQ(fru->fru_rsi, htole16(10));
239 EXPECT_EQ(fru->entity_type, htole16(1));
240 EXPECT_EQ(fru->entity_instance_num, htole16(0));
241 EXPECT_EQ(fru->container_id, htole16(100));
Deepak Kodihallidb914672020-02-07 02:47:45 -0600242 outData = nullptr;
243
244 handle = pldm_pdr_add_fru_record_set(repo, 2, 11, 2, 1, 101);
245 EXPECT_EQ(handle, 2);
246 EXPECT_EQ(pldm_pdr_get_record_count(repo), 2);
247 EXPECT_EQ(pldm_pdr_get_repo_size(repo),
248 2 * (sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_fru_record_set)));
249 hdl = pldm_pdr_find_record(repo, 2, &outData, &size, &nextRecHdl);
250 EXPECT_NE(hdl, nullptr);
251 EXPECT_EQ(size, sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_fru_record_set));
252 EXPECT_EQ(nextRecHdl, 0);
253 hdr = reinterpret_cast<pldm_pdr_hdr*>(outData);
254 EXPECT_EQ(hdr->version, 1);
255 EXPECT_EQ(hdr->type, PLDM_PDR_FRU_RECORD_SET);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500256 EXPECT_EQ(hdr->length, htole16(sizeof(pldm_pdr_fru_record_set)));
257 EXPECT_EQ(hdr->record_handle, htole32(2));
Deepak Kodihallidb914672020-02-07 02:47:45 -0600258 fru = reinterpret_cast<pldm_pdr_fru_record_set*>(outData +
259 sizeof(pldm_pdr_hdr));
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500260 EXPECT_EQ(fru->terminus_handle, htole16(2));
261 EXPECT_EQ(fru->fru_rsi, htole16(11));
262 EXPECT_EQ(fru->entity_type, htole16(2));
263 EXPECT_EQ(fru->entity_instance_num, htole16(1));
264 EXPECT_EQ(fru->container_id, htole16(101));
Deepak Kodihallidb914672020-02-07 02:47:45 -0600265 outData = nullptr;
266
267 hdl = pldm_pdr_find_record(repo, 1, &outData, &size, &nextRecHdl);
268 EXPECT_NE(hdl, nullptr);
269 EXPECT_EQ(size, sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_fru_record_set));
270 EXPECT_EQ(nextRecHdl, 2);
271 hdr = reinterpret_cast<pldm_pdr_hdr*>(outData);
272 EXPECT_EQ(hdr->version, 1);
273 EXPECT_EQ(hdr->type, PLDM_PDR_FRU_RECORD_SET);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500274 EXPECT_EQ(hdr->length, htole16(sizeof(pldm_pdr_fru_record_set)));
275 EXPECT_EQ(hdr->record_handle, htole32(1));
Deepak Kodihallidb914672020-02-07 02:47:45 -0600276 fru = reinterpret_cast<pldm_pdr_fru_record_set*>(outData +
277 sizeof(pldm_pdr_hdr));
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500278 EXPECT_EQ(fru->terminus_handle, htole16(1));
279 EXPECT_EQ(fru->fru_rsi, htole16(10));
280 EXPECT_EQ(fru->entity_type, htole16(1));
281 EXPECT_EQ(fru->entity_instance_num, htole16(0));
282 EXPECT_EQ(fru->container_id, htole16(100));
Deepak Kodihallidb914672020-02-07 02:47:45 -0600283 outData = nullptr;
284
285 pldm_pdr_destroy(repo);
286}
287
288TEST(PDRUpdate, tesFindtFruRecordSet)
289{
290 auto repo = pldm_pdr_init();
291
292 uint16_t terminusHdl{};
293 uint16_t entityType{};
294 uint16_t entityInstanceNum{};
295 uint16_t containerId{};
296 auto first = pldm_pdr_add_fru_record_set(repo, 1, 1, 1, 0, 100);
297 auto second = pldm_pdr_add_fru_record_set(repo, 1, 2, 1, 1, 100);
298 auto third = pldm_pdr_add_fru_record_set(repo, 1, 3, 1, 2, 100);
299 EXPECT_EQ(first, pldm_pdr_get_record_handle(
300 repo, pldm_pdr_fru_record_set_find_by_rsi(
301 repo, 1, &terminusHdl, &entityType,
302 &entityInstanceNum, &containerId)));
303 EXPECT_EQ(second, pldm_pdr_get_record_handle(
304 repo, pldm_pdr_fru_record_set_find_by_rsi(
305 repo, 2, &terminusHdl, &entityType,
306 &entityInstanceNum, &containerId)));
307 EXPECT_EQ(third, pldm_pdr_get_record_handle(
308 repo, pldm_pdr_fru_record_set_find_by_rsi(
309 repo, 3, &terminusHdl, &entityType,
310 &entityInstanceNum, &containerId)));
311 EXPECT_EQ(terminusHdl, 1);
312 EXPECT_EQ(entityType, 1);
313 EXPECT_EQ(entityInstanceNum, 2);
314 EXPECT_EQ(containerId, 100);
315 EXPECT_EQ(nullptr, pldm_pdr_fru_record_set_find_by_rsi(
316 repo, 4, &terminusHdl, &entityType,
317 &entityInstanceNum, &containerId));
318
319 pldm_pdr_destroy(repo);
320}
Deepak Kodihalli05787052020-03-10 01:54:08 -0500321
322TEST(EntityAssociationPDR, testInit)
323{
324 auto tree = pldm_entity_association_tree_init();
325 EXPECT_NE(tree, nullptr);
326 pldm_entity_association_tree_destroy(tree);
327}
328
329TEST(EntityAssociationPDR, testBuild)
330{
331 // 1
332 // |
333 // 2--3--4
334 // |
335 // 5--6--7
336 // | |
337 // 9 8
338
339 pldm_entity entities[9]{};
340
341 entities[0].entity_type = 1;
342 entities[1].entity_type = 2;
343 entities[2].entity_type = 2;
344 entities[3].entity_type = 3;
345 entities[4].entity_type = 4;
346 entities[5].entity_type = 5;
347 entities[6].entity_type = 5;
348 entities[7].entity_type = 6;
349 entities[8].entity_type = 7;
350
351 auto tree = pldm_entity_association_tree_init();
352
353 auto l1 = pldm_entity_association_tree_add(tree, &entities[0], nullptr,
354 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
355 EXPECT_NE(l1, nullptr);
356 auto l2a = pldm_entity_association_tree_add(
357 tree, &entities[1], l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
358 EXPECT_NE(l2a, nullptr);
359 auto l2b = pldm_entity_association_tree_add(
360 tree, &entities[2], l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
361 EXPECT_NE(l2b, nullptr);
362 auto l2c = pldm_entity_association_tree_add(
363 tree, &entities[3], l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
364 EXPECT_NE(l2c, nullptr);
365 auto l3a = pldm_entity_association_tree_add(
366 tree, &entities[4], l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
367 EXPECT_NE(l3a, nullptr);
368 auto l3b = pldm_entity_association_tree_add(
369 tree, &entities[5], l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
370 EXPECT_NE(l3b, nullptr);
371 auto l3c = pldm_entity_association_tree_add(
372 tree, &entities[6], l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
373 EXPECT_NE(l3b, nullptr);
374 auto l4a = pldm_entity_association_tree_add(
375 tree, &entities[7], l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
376 EXPECT_NE(l4a, nullptr);
377 auto l4b = pldm_entity_association_tree_add(
378 tree, &entities[8], l3b, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
379 EXPECT_NE(l4b, nullptr);
380
381 EXPECT_EQ(pldm_entity_is_node_parent(l1), true);
382 EXPECT_EQ(pldm_entity_is_node_parent(l2a), true);
383 EXPECT_EQ(pldm_entity_is_node_parent(l3a), true);
384 EXPECT_EQ(pldm_entity_is_node_parent(l3b), true);
385
386 EXPECT_EQ(pldm_entity_is_node_parent(l2b), false);
387 EXPECT_EQ(pldm_entity_is_node_parent(l2c), false);
388 EXPECT_EQ(pldm_entity_is_node_parent(l3c), false);
389 EXPECT_EQ(pldm_entity_is_node_parent(l4a), false);
390 EXPECT_EQ(pldm_entity_is_node_parent(l4b), false);
391
392 size_t num{};
393 pldm_entity* out = nullptr;
394 pldm_entity_association_tree_visit(tree, &out, &num);
395 EXPECT_EQ(num, 9);
396
397 EXPECT_EQ(out[0].entity_type, 1);
398 EXPECT_EQ(out[0].entity_instance_num, 1);
399 EXPECT_EQ(out[0].entity_container_id, 0);
400
401 EXPECT_EQ(out[1].entity_type, 2);
402 EXPECT_EQ(out[1].entity_instance_num, 1);
403 EXPECT_EQ(out[1].entity_container_id, 1);
404 EXPECT_EQ(out[2].entity_type, 2);
405 EXPECT_EQ(out[2].entity_instance_num, 2);
406 EXPECT_EQ(out[2].entity_container_id, 1);
407 EXPECT_EQ(out[3].entity_type, 3);
408 EXPECT_EQ(out[3].entity_instance_num, 1);
409 EXPECT_EQ(out[3].entity_container_id, 1);
410
411 EXPECT_EQ(out[4].entity_type, 4);
412 EXPECT_EQ(out[4].entity_instance_num, 1);
413 EXPECT_EQ(out[4].entity_container_id, 2);
414 EXPECT_EQ(out[5].entity_type, 5);
415 EXPECT_EQ(out[5].entity_instance_num, 1);
416 EXPECT_EQ(out[5].entity_container_id, 2);
417 EXPECT_EQ(out[6].entity_type, 5);
418 EXPECT_EQ(out[6].entity_instance_num, 2);
419 EXPECT_EQ(out[6].entity_container_id, 2);
420
421 EXPECT_EQ(out[7].entity_type, 7);
422 EXPECT_EQ(out[7].entity_instance_num, 1);
423 EXPECT_EQ(out[7].entity_container_id, 4);
424 EXPECT_EQ(out[8].entity_type, 6);
425 EXPECT_EQ(out[8].entity_instance_num, 1);
426 EXPECT_EQ(out[8].entity_container_id, 3);
427
428 free(out);
429 pldm_entity_association_tree_destroy(tree);
430}
431
432TEST(EntityAssociationPDR, testSpecialTrees)
433{
434 pldm_entity entities[3]{};
435
436 entities[0].entity_type = 1;
437 entities[1].entity_type = 2;
438 entities[2].entity_type = 1;
439
440 // A
441 auto tree = pldm_entity_association_tree_init();
442 auto node = pldm_entity_association_tree_add(
443 tree, &entities[0], nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
444 EXPECT_NE(node, nullptr);
445 size_t num{};
446 pldm_entity* out = nullptr;
447 pldm_entity_association_tree_visit(tree, &out, &num);
448 EXPECT_EQ(num, 1);
449 EXPECT_EQ(out[0].entity_type, 1);
450 EXPECT_EQ(out[0].entity_instance_num, 1);
451 EXPECT_EQ(out[0].entity_container_id, 0);
452 free(out);
453 pldm_entity_association_tree_destroy(tree);
454
455 // A-A-A
456 tree = pldm_entity_association_tree_init();
457 node = pldm_entity_association_tree_add(tree, &entities[0], nullptr,
458 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
459 EXPECT_NE(node, nullptr);
460 node = pldm_entity_association_tree_add(tree, &entities[1], nullptr,
461 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
462 EXPECT_NE(node, nullptr);
463 node = pldm_entity_association_tree_add(tree, &entities[2], nullptr,
464 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
465 EXPECT_NE(node, nullptr);
466 pldm_entity_association_tree_visit(tree, &out, &num);
467 EXPECT_EQ(num, 3);
468 EXPECT_EQ(out[0].entity_type, 1);
469 EXPECT_EQ(out[0].entity_instance_num, 1);
470 EXPECT_EQ(out[0].entity_container_id, 0);
471 EXPECT_EQ(out[1].entity_type, 1);
472 EXPECT_EQ(out[1].entity_instance_num, 2);
473 EXPECT_EQ(out[1].entity_container_id, 0);
474 EXPECT_EQ(out[2].entity_type, 2);
475 EXPECT_EQ(out[2].entity_instance_num, 1);
476 EXPECT_EQ(out[2].entity_container_id, 0);
477 free(out);
478 pldm_entity_association_tree_destroy(tree);
479
480 // A
481 // |
482 // A
483 // |
484 // A
485 tree = pldm_entity_association_tree_init();
486 node = pldm_entity_association_tree_add(tree, &entities[0], nullptr,
487 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
488 EXPECT_NE(node, nullptr);
489 auto node1 = pldm_entity_association_tree_add(
490 tree, &entities[1], node, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
491 EXPECT_NE(node1, nullptr);
492 auto node2 = pldm_entity_association_tree_add(
493 tree, &entities[2], node1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
494 EXPECT_NE(node2, nullptr);
495 pldm_entity_association_tree_visit(tree, &out, &num);
496 EXPECT_EQ(num, 3);
497 EXPECT_EQ(out[0].entity_type, 1);
498 EXPECT_EQ(out[0].entity_instance_num, 1);
499 EXPECT_EQ(out[0].entity_container_id, 0);
500 EXPECT_EQ(out[1].entity_type, 2);
501 EXPECT_EQ(out[1].entity_instance_num, 1);
502 EXPECT_EQ(out[1].entity_container_id, 1);
503 EXPECT_EQ(out[2].entity_type, 1);
504 EXPECT_EQ(out[2].entity_instance_num, 1);
505 EXPECT_EQ(out[2].entity_container_id, 2);
506 free(out);
507 pldm_entity_association_tree_destroy(tree);
508
509 // A-A
510 // |
511 // A-A
512 tree = pldm_entity_association_tree_init();
513 node = pldm_entity_association_tree_add(tree, &entities[0], nullptr,
514 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
515 EXPECT_NE(node, nullptr);
516 node = pldm_entity_association_tree_add(tree, &entities[0], nullptr,
517 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
518 EXPECT_NE(node, nullptr);
519 node1 = pldm_entity_association_tree_add(tree, &entities[1], node,
520 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
521 EXPECT_NE(node1, nullptr);
522 node2 = pldm_entity_association_tree_add(tree, &entities[2], node,
523 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
524 EXPECT_NE(node2, nullptr);
525 pldm_entity_association_tree_visit(tree, &out, &num);
526 EXPECT_EQ(num, 4);
527 EXPECT_EQ(out[0].entity_type, 1);
528 EXPECT_EQ(out[0].entity_instance_num, 1);
529 EXPECT_EQ(out[0].entity_container_id, 0);
530 EXPECT_EQ(out[1].entity_type, 1);
531 EXPECT_EQ(out[1].entity_instance_num, 2);
532 EXPECT_EQ(out[1].entity_container_id, 0);
533 EXPECT_EQ(out[2].entity_type, 2);
534 EXPECT_EQ(out[2].entity_instance_num, 1);
535 EXPECT_EQ(out[2].entity_container_id, 1);
536 EXPECT_EQ(out[3].entity_type, 1);
537 EXPECT_EQ(out[3].entity_instance_num, 1);
538 EXPECT_EQ(out[3].entity_container_id, 1);
539 free(out);
540 pldm_entity_association_tree_destroy(tree);
541}
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500542
543TEST(EntityAssociationPDR, testPDR)
544{
545 // e = entity type, c = container id, i = instance num
546
547 // INPUT
548 // 1(e=1)--1a(e=2)
549 // |
550 // 2(e=2)--3(e=2)--4(e=2)--5(e=3)
551 // |
552 // 6(e=4)--7(e=5)--8(e=5)--9(e=5)
553 // | |
554 // 11(e=6) 10(e=7)
555
556 // Expected OUTPUT
557 // 1(e=1,c=0,i=1)
558 // |
559 // 2(e=2,c=1,i=1)--3(e=2,c=1,i=2)--4(e=3,c=1,i=1)--5(e=3,c=1,i=2)
560 // |
561 // 6(e=4,c=2,i=1)--7(e=5,c=2,i=1)--8(e=5,c=2,i=2)--9(e=5,c=2,i=3)
562 // | |
563 // 10(e=6,c=3,i=1) 11(e=7,c=4,i=1)
564 pldm_entity entities[11]{};
565
566 entities[0].entity_type = 1;
567 entities[1].entity_type = 2;
568 entities[2].entity_type = 3;
569 entities[3].entity_type = 2;
570 entities[4].entity_type = 3;
571 entities[5].entity_type = 4;
572 entities[6].entity_type = 5;
573 entities[7].entity_type = 5;
574 entities[8].entity_type = 5;
575 entities[9].entity_type = 6;
576 entities[10].entity_type = 7;
577
578 auto tree = pldm_entity_association_tree_init();
579
580 auto l1 = pldm_entity_association_tree_add(tree, &entities[0], nullptr,
581 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
582 EXPECT_NE(l1, nullptr);
583 auto l1a = pldm_entity_association_tree_add(
584 tree, &entities[1], nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
585 EXPECT_NE(l1a, nullptr);
586
587 auto l2a = pldm_entity_association_tree_add(
588 tree, &entities[1], l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
589 EXPECT_NE(l2a, nullptr);
590 auto l2b = pldm_entity_association_tree_add(tree, &entities[2], l1,
591 PLDM_ENTITY_ASSOCIAION_LOGICAL);
592 EXPECT_NE(l2b, nullptr);
593 auto l2c = pldm_entity_association_tree_add(
594 tree, &entities[3], l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
595 EXPECT_NE(l2c, nullptr);
596 auto l2d = pldm_entity_association_tree_add(tree, &entities[4], l1,
597 PLDM_ENTITY_ASSOCIAION_LOGICAL);
598 EXPECT_NE(l2d, nullptr);
599
600 auto l3a = pldm_entity_association_tree_add(
601 tree, &entities[5], l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
602 EXPECT_NE(l3a, nullptr);
603 auto l3b = pldm_entity_association_tree_add(
604 tree, &entities[6], l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
605 EXPECT_NE(l3b, nullptr);
606 auto l3c = pldm_entity_association_tree_add(tree, &entities[7], l2a,
607 PLDM_ENTITY_ASSOCIAION_LOGICAL);
608 EXPECT_NE(l3c, nullptr);
609 auto l3d = pldm_entity_association_tree_add(tree, &entities[8], l2a,
610 PLDM_ENTITY_ASSOCIAION_LOGICAL);
611 EXPECT_NE(l3d, nullptr);
612
613 auto l4a = pldm_entity_association_tree_add(
614 tree, &entities[9], l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
615 EXPECT_NE(l4a, nullptr);
616 auto l4b = pldm_entity_association_tree_add(tree, &entities[10], l3b,
617 PLDM_ENTITY_ASSOCIAION_LOGICAL);
618 EXPECT_NE(l4b, nullptr);
619
620 EXPECT_EQ(pldm_entity_get_num_children(l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL),
621 2);
622 EXPECT_EQ(pldm_entity_get_num_children(l1, PLDM_ENTITY_ASSOCIAION_LOGICAL),
623 2);
624 EXPECT_EQ(
625 pldm_entity_get_num_children(l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL), 2);
626 EXPECT_EQ(
627 pldm_entity_get_num_children(l3b, PLDM_ENTITY_ASSOCIAION_PHYSICAL), 0);
628 EXPECT_EQ(pldm_entity_get_num_children(l3b, PLDM_ENTITY_ASSOCIAION_LOGICAL),
629 1);
630
631 auto repo = pldm_pdr_init();
632 pldm_entity_association_pdr_add(tree, repo);
633
634 EXPECT_EQ(pldm_pdr_get_record_count(repo), 6);
635
636 uint32_t currRecHandle{};
637 uint32_t nextRecHandle{};
638 uint8_t* data = nullptr;
639 uint32_t size{};
640 uint32_t commonSize = sizeof(struct pldm_pdr_hdr) + sizeof(uint16_t) +
641 sizeof(uint8_t) + sizeof(pldm_entity) +
642 sizeof(uint8_t);
643
644 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
645 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
646 l1, PLDM_ENTITY_ASSOCIAION_LOGICAL) *
647 sizeof(pldm_entity)));
648 uint8_t* start = data;
649 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500650 EXPECT_EQ(le32toh(hdr->record_handle), 1);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500651 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500652 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500653 start += sizeof(pldm_pdr_hdr);
654 uint16_t* containerId = reinterpret_cast<uint16_t*>(start);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500655 EXPECT_EQ(le16toh(*containerId), 1);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500656 start += sizeof(uint16_t);
657 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_LOGICAL);
658 start += sizeof(uint8_t);
659 pldm_entity* entity = reinterpret_cast<pldm_entity*>(start);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500660 EXPECT_EQ(le16toh(entity->entity_type), 1);
661 EXPECT_EQ(le16toh(entity->entity_instance_num), 1);
662 EXPECT_EQ(le16toh(entity->entity_container_id), 0);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500663 start += sizeof(pldm_entity);
664 EXPECT_EQ(*start,
665 pldm_entity_get_num_children(l1, PLDM_ENTITY_ASSOCIAION_LOGICAL));
666 start += sizeof(uint8_t);
667 entity = reinterpret_cast<pldm_entity*>(start);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500668 EXPECT_EQ(le16toh(entity->entity_type), 3);
669 EXPECT_EQ(le16toh(entity->entity_instance_num), 1);
670 EXPECT_EQ(le16toh(entity->entity_container_id), 1);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500671 start += sizeof(pldm_entity);
672 entity = reinterpret_cast<pldm_entity*>(start);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500673 EXPECT_EQ(le16toh(entity->entity_type), 3);
674 EXPECT_EQ(le16toh(entity->entity_instance_num), 2);
675 EXPECT_EQ(le16toh(entity->entity_container_id), 1);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500676 start += sizeof(pldm_entity);
677
678 currRecHandle = nextRecHandle;
679
680 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
681 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
682 l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL) *
683 sizeof(pldm_entity)));
684 start = data;
685 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500686 EXPECT_EQ(le32toh(hdr->record_handle), 2);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500687 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500688 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500689 start += sizeof(pldm_pdr_hdr);
690 containerId = reinterpret_cast<uint16_t*>(start);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500691 EXPECT_EQ(le16toh(*containerId), 1);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500692 start += sizeof(uint16_t);
693 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
694 start += sizeof(uint8_t);
695 entity = reinterpret_cast<pldm_entity*>(start);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500696 EXPECT_EQ(le16toh(entity->entity_type), 1);
697 EXPECT_EQ(le16toh(entity->entity_instance_num), 1);
698 EXPECT_EQ(le16toh(entity->entity_container_id), 0);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500699 start += sizeof(pldm_entity);
700 EXPECT_EQ(*start, pldm_entity_get_num_children(
701 l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL));
702 start += sizeof(uint8_t);
703 entity = reinterpret_cast<pldm_entity*>(start);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500704 EXPECT_EQ(le16toh(entity->entity_type), 2);
705 EXPECT_EQ(le16toh(entity->entity_instance_num), 1);
706 EXPECT_EQ(le16toh(entity->entity_container_id), 1);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500707 start += sizeof(pldm_entity);
708 entity = reinterpret_cast<pldm_entity*>(start);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500709 EXPECT_EQ(le16toh(entity->entity_type), 2);
710 EXPECT_EQ(le16toh(entity->entity_instance_num), 2);
711 EXPECT_EQ(le16toh(entity->entity_container_id), 1);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500712 start += sizeof(pldm_entity);
713
714 currRecHandle = nextRecHandle;
715
716 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
717 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
718 l2a, PLDM_ENTITY_ASSOCIAION_LOGICAL) *
719 sizeof(pldm_entity)));
720 start = data;
721 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500722 EXPECT_EQ(le32toh(hdr->record_handle), 3);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500723 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500724 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500725 start += sizeof(pldm_pdr_hdr);
726 containerId = reinterpret_cast<uint16_t*>(start);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500727 EXPECT_EQ(le16toh(*containerId), 2);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500728 start += sizeof(uint16_t);
729 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_LOGICAL);
730 start += sizeof(uint8_t);
731 entity = reinterpret_cast<pldm_entity*>(start);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500732 EXPECT_EQ(le16toh(entity->entity_type), 2);
733 EXPECT_EQ(le16toh(entity->entity_instance_num), 1);
734 EXPECT_EQ(le16toh(entity->entity_container_id), 1);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500735 start += sizeof(pldm_entity);
736 EXPECT_EQ(*start, pldm_entity_get_num_children(
737 l2a, PLDM_ENTITY_ASSOCIAION_LOGICAL));
738 start += sizeof(uint8_t);
739 entity = reinterpret_cast<pldm_entity*>(start);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500740 EXPECT_EQ(le16toh(entity->entity_type), 5);
741 EXPECT_EQ(le16toh(entity->entity_instance_num), 2);
742 EXPECT_EQ(le16toh(entity->entity_container_id), 2);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500743 start += sizeof(pldm_entity);
744 entity = reinterpret_cast<pldm_entity*>(start);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500745 EXPECT_EQ(le16toh(entity->entity_type), 5);
746 EXPECT_EQ(le16toh(entity->entity_instance_num), 3);
747 EXPECT_EQ(le16toh(entity->entity_container_id), 2);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500748 start += sizeof(pldm_entity);
749
750 currRecHandle = nextRecHandle;
751
752 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
753 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
754 l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL) *
755 sizeof(pldm_entity)));
756 start = data;
757 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500758 EXPECT_EQ(le32toh(hdr->record_handle), 4);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500759 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500760 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500761 start += sizeof(pldm_pdr_hdr);
762 containerId = reinterpret_cast<uint16_t*>(start);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500763 EXPECT_EQ(le16toh(*containerId), 2);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500764 start += sizeof(uint16_t);
765 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
766 start += sizeof(uint8_t);
767 entity = reinterpret_cast<pldm_entity*>(start);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500768 EXPECT_EQ(le16toh(entity->entity_type), 2);
769 EXPECT_EQ(le16toh(entity->entity_instance_num), 1);
770 EXPECT_EQ(le16toh(entity->entity_container_id), 1);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500771 start += sizeof(pldm_entity);
772 EXPECT_EQ(*start, pldm_entity_get_num_children(
773 l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL));
774 start += sizeof(uint8_t);
775 entity = reinterpret_cast<pldm_entity*>(start);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500776 EXPECT_EQ(le16toh(entity->entity_type), 4);
777 EXPECT_EQ(le16toh(entity->entity_instance_num), 1);
778 EXPECT_EQ(le16toh(entity->entity_container_id), 2);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500779 start += sizeof(pldm_entity);
780 entity = reinterpret_cast<pldm_entity*>(start);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500781 EXPECT_EQ(le16toh(entity->entity_type), 5);
782 EXPECT_EQ(le16toh(entity->entity_instance_num), 1);
783 EXPECT_EQ(le16toh(entity->entity_container_id), 2);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500784 start += sizeof(pldm_entity);
785
786 currRecHandle = nextRecHandle;
787
788 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
789 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
790 l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL) *
791 sizeof(pldm_entity)));
792 start = data;
793 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500794 EXPECT_EQ(le32toh(hdr->record_handle), 5);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500795 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500796 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500797 start += sizeof(pldm_pdr_hdr);
798 containerId = reinterpret_cast<uint16_t*>(start);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500799 EXPECT_EQ(le16toh(*containerId), 3);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500800 start += sizeof(uint16_t);
801 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
802 start += sizeof(uint8_t);
803 entity = reinterpret_cast<pldm_entity*>(start);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500804 EXPECT_EQ(le16toh(entity->entity_type), 4);
805 EXPECT_EQ(le16toh(entity->entity_instance_num), 1);
806 EXPECT_EQ(le16toh(entity->entity_container_id), 2);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500807 start += sizeof(pldm_entity);
808 EXPECT_EQ(*start, pldm_entity_get_num_children(
809 l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL));
810 start += sizeof(uint8_t);
811 entity = reinterpret_cast<pldm_entity*>(start);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500812 EXPECT_EQ(le16toh(entity->entity_type), 6);
813 EXPECT_EQ(le16toh(entity->entity_instance_num), 1);
814 EXPECT_EQ(le16toh(entity->entity_container_id), 3);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500815 start += sizeof(pldm_entity);
816
817 currRecHandle = nextRecHandle;
818
819 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
820 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
821 l3b, PLDM_ENTITY_ASSOCIAION_LOGICAL) *
822 sizeof(pldm_entity)));
823 start = data;
824 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500825 EXPECT_EQ(le32toh(hdr->record_handle), 6);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500826 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500827 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500828 start += sizeof(pldm_pdr_hdr);
829 containerId = reinterpret_cast<uint16_t*>(start);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500830 EXPECT_EQ(le16toh(*containerId), 4);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500831 start += sizeof(uint16_t);
832 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_LOGICAL);
833 start += sizeof(uint8_t);
834 entity = reinterpret_cast<pldm_entity*>(start);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500835 EXPECT_EQ(le16toh(entity->entity_type), 5);
836 EXPECT_EQ(le16toh(entity->entity_instance_num), 1);
837 EXPECT_EQ(le16toh(entity->entity_container_id), 2);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500838 start += sizeof(pldm_entity);
839 EXPECT_EQ(*start, pldm_entity_get_num_children(
840 l3b, PLDM_ENTITY_ASSOCIAION_LOGICAL));
841 start += sizeof(uint8_t);
842 entity = reinterpret_cast<pldm_entity*>(start);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500843 EXPECT_EQ(le16toh(entity->entity_type), 7);
844 EXPECT_EQ(le16toh(entity->entity_instance_num), 1);
845 EXPECT_EQ(le16toh(entity->entity_container_id), 4);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500846
847 EXPECT_EQ(nextRecHandle, 0);
848
849 pldm_pdr_destroy(repo);
850 pldm_entity_association_tree_destroy(tree);
851}
Deepak Kodihalli3b28ba82020-03-30 07:39:47 -0500852
853TEST(EntityAssociationPDR, testFind)
854{
855 // 1
856 // |
857 // 2--3--4
858 // |
859 // 5--6--7
860 // | |
861 // 8 9
862
863 pldm_entity entities[9]{};
864
865 entities[0].entity_type = 1;
866 entities[1].entity_type = 2;
867 entities[2].entity_type = 2;
868 entities[3].entity_type = 3;
869 entities[4].entity_type = 4;
870 entities[5].entity_type = 5;
871 entities[6].entity_type = 5;
872 entities[7].entity_type = 6;
873 entities[8].entity_type = 7;
874
875 auto tree = pldm_entity_association_tree_init();
876
877 auto l1 = pldm_entity_association_tree_add(tree, &entities[0], nullptr,
878 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
879 EXPECT_NE(l1, nullptr);
880 auto l2a = pldm_entity_association_tree_add(
881 tree, &entities[1], l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
882 EXPECT_NE(l2a, nullptr);
883 auto l2b = pldm_entity_association_tree_add(
884 tree, &entities[2], l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
885 EXPECT_NE(l2b, nullptr);
886 auto l2c = pldm_entity_association_tree_add(
887 tree, &entities[3], l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
888 EXPECT_NE(l2c, nullptr);
889 auto l3a = pldm_entity_association_tree_add(
890 tree, &entities[4], l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
891 EXPECT_NE(l3a, nullptr);
892 auto l3b = pldm_entity_association_tree_add(
893 tree, &entities[5], l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
894 EXPECT_NE(l3b, nullptr);
895 auto l3c = pldm_entity_association_tree_add(
896 tree, &entities[6], l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
897 EXPECT_NE(l3c, nullptr);
898 auto l4a = pldm_entity_association_tree_add(
899 tree, &entities[7], l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
900 EXPECT_NE(l4a, nullptr);
901 auto l4b = pldm_entity_association_tree_add(
902 tree, &entities[8], l3b, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
903 EXPECT_NE(l4b, nullptr);
904
905 pldm_entity entity{};
906
907 entity.entity_type = 1;
908 entity.entity_instance_num = 1;
909 auto result = pldm_entity_association_tree_find(tree, &entity);
910 EXPECT_EQ(result, l1);
911 EXPECT_EQ(entity.entity_container_id, 0);
912
913 entity.entity_type = 2;
914 entity.entity_instance_num = 1;
915 result = pldm_entity_association_tree_find(tree, &entity);
916 EXPECT_EQ(result, l2a);
917 EXPECT_EQ(entity.entity_container_id, 1);
918 entity.entity_type = 2;
919 entity.entity_instance_num = 2;
920 result = pldm_entity_association_tree_find(tree, &entity);
921 EXPECT_EQ(result, l2b);
922 EXPECT_EQ(entity.entity_container_id, 1);
923 entity.entity_type = 3;
924 entity.entity_instance_num = 1;
925 result = pldm_entity_association_tree_find(tree, &entity);
926 EXPECT_EQ(result, l2c);
927 EXPECT_EQ(entity.entity_container_id, 1);
928
929 entity.entity_type = 7;
930 entity.entity_instance_num = 1;
931 result = pldm_entity_association_tree_find(tree, &entity);
932 EXPECT_EQ(result, l4b);
933 EXPECT_EQ(entity.entity_container_id, 4);
934
935 pldm_entity_association_tree_destroy(tree);
936}
937
938TEST(EntityAssociationPDR, testExtract)
939{
940 std::vector<uint8_t> pdr{};
941 pdr.resize(sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_entity_association) +
942 sizeof(pldm_entity) * 4);
943 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(pdr.data());
944 hdr->type = PLDM_PDR_ENTITY_ASSOCIATION;
945 hdr->length =
946 htole16(sizeof(pldm_pdr_entity_association) + sizeof(pldm_entity) * 4);
947
948 pldm_pdr_entity_association* e =
949 reinterpret_cast<pldm_pdr_entity_association*>(pdr.data() +
950 sizeof(pldm_pdr_hdr));
951 e->container_id = htole16(1);
952 e->num_children = 5;
953 e->container.entity_type = htole16(1);
954 e->container.entity_instance_num = htole16(1);
955 e->container.entity_container_id = htole16(0);
956
957 pldm_entity* entity = e->children;
958 entity->entity_type = htole16(2);
959 entity->entity_instance_num = htole16(1);
960 entity->entity_container_id = htole16(1);
961 ++entity;
962 entity->entity_type = htole16(3);
963 entity->entity_instance_num = htole16(1);
964 entity->entity_container_id = htole16(1);
965 ++entity;
966 entity->entity_type = htole16(4);
967 entity->entity_instance_num = htole16(1);
968 entity->entity_container_id = htole16(1);
969 ++entity;
970 entity->entity_type = htole16(5);
971 entity->entity_instance_num = htole16(1);
972 entity->entity_container_id = htole16(1);
973 ++entity;
974 entity->entity_type = htole16(6);
975 entity->entity_instance_num = htole16(1);
976 entity->entity_container_id = htole16(1);
977
978 size_t num{};
979 pldm_entity* out = nullptr;
980 pldm_entity_association_pdr_extract(pdr.data(), pdr.size(), &num, &out);
981 EXPECT_EQ(num, e->num_children + 1);
982 EXPECT_EQ(out[0].entity_type, 1);
983 EXPECT_EQ(out[0].entity_instance_num, 1);
984 EXPECT_EQ(out[0].entity_container_id, 0);
985 EXPECT_EQ(out[1].entity_type, 2);
986 EXPECT_EQ(out[1].entity_instance_num, 1);
987 EXPECT_EQ(out[1].entity_container_id, 1);
988 EXPECT_EQ(out[2].entity_type, 3);
989 EXPECT_EQ(out[2].entity_instance_num, 1);
990 EXPECT_EQ(out[2].entity_container_id, 1);
991 EXPECT_EQ(out[3].entity_type, 4);
992 EXPECT_EQ(out[3].entity_instance_num, 1);
993 EXPECT_EQ(out[3].entity_container_id, 1);
994 EXPECT_EQ(out[4].entity_type, 5);
995 EXPECT_EQ(out[4].entity_instance_num, 1);
996 EXPECT_EQ(out[4].entity_container_id, 1);
997 EXPECT_EQ(out[5].entity_type, 6);
998 EXPECT_EQ(out[5].entity_instance_num, 1);
999 EXPECT_EQ(out[5].entity_container_id, 1);
1000
1001 free(out);
1002}