blob: 72860d7772d180ad3340bf1ae533845f320bc646 [file] [log] [blame]
Manojkiran Eda9a8e4972022-11-28 16:38:21 +05301#include <endian.h>
Manojkiran Eda9a8e4972022-11-28 16:38:21 +05302
Andrew Jeffery9c766792022-08-10 23:12:49 +09303#include <array>
Manojkiran Eda9a8e4972022-11-28 16:38:21 +05304#include <cstdint>
Andrew Jeffery5a706072023-04-05 19:45:31 +09305#include <cstdlib>
6#include <cstring>
Manojkiran Eda9a8e4972022-11-28 16:38:21 +05307#include <vector>
Andrew Jeffery9c766792022-08-10 23:12:49 +09308
9#include "libpldm/pdr.h"
10#include "libpldm/platform.h"
11
12#include <gtest/gtest.h>
13
14TEST(PDRAccess, testInit)
15{
16 auto repo = pldm_pdr_init();
17 EXPECT_EQ(pldm_pdr_get_record_count(repo), 0u);
18 EXPECT_EQ(pldm_pdr_get_repo_size(repo), 0u);
19 pldm_pdr_destroy(repo);
20}
21
22TEST(PDRUpdate, testAdd)
23{
24 auto repo = pldm_pdr_init();
25
26 std::array<uint8_t, 10> data{};
27 auto handle = pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
28 EXPECT_EQ(handle, 1u);
29 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
30 EXPECT_EQ(pldm_pdr_get_repo_size(repo), data.size());
31
32 handle = pldm_pdr_add(repo, data.data(), data.size(), 0u, false, 1);
33 EXPECT_EQ(handle, 2u);
34 handle = pldm_pdr_add(repo, data.data(), data.size(), 0u, false, 1);
35 EXPECT_EQ(handle, 3u);
36 handle = pldm_pdr_add(repo, data.data(), data.size(), htole32(0xdeeddeedu),
37 false, 1);
38 EXPECT_EQ(handle, htole32(0xdeeddeed));
39 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
40 EXPECT_EQ(pldm_pdr_get_repo_size(repo), data.size() * 4u);
41
42 pldm_pdr_destroy(repo);
43}
44
45TEST(PDRRemoveByTerminus, testRemoveByTerminus)
46{
47 std::array<uint8_t, 10> data{};
48
49 auto repo = pldm_pdr_init();
50 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
51 pldm_pdr_remove_pdrs_by_terminus_handle(repo, 1);
52 EXPECT_EQ(pldm_pdr_get_record_count(repo), 0u);
53 pldm_pdr_destroy(repo);
54
55 repo = pldm_pdr_init();
56 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 2);
57 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
58 EXPECT_EQ(pldm_pdr_get_record_count(repo), 2u);
59 pldm_pdr_remove_pdrs_by_terminus_handle(repo, 1);
60 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
61 pldm_pdr_destroy(repo);
62
63 repo = pldm_pdr_init();
64 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
65 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 2);
66 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 2);
67 EXPECT_EQ(pldm_pdr_get_record_count(repo), 3u);
68 pldm_pdr_remove_pdrs_by_terminus_handle(repo, 2);
69 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
70 pldm_pdr_destroy(repo);
71
72 repo = pldm_pdr_init();
73 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
74 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 2);
75 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
76 EXPECT_EQ(pldm_pdr_get_record_count(repo), 3u);
77 pldm_pdr_remove_remote_pdrs(repo);
78 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
79 pldm_pdr_remove_pdrs_by_terminus_handle(repo, 1);
80 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
81 pldm_pdr_remove_pdrs_by_terminus_handle(repo, 2);
82 EXPECT_EQ(pldm_pdr_get_record_count(repo), 0u);
83 pldm_pdr_destroy(repo);
84}
85
86TEST(PDRUpdate, testRemove)
87{
88 std::array<uint8_t, 10> data{};
89
90 auto repo = pldm_pdr_init();
91 pldm_pdr_remove_remote_pdrs(repo);
92 EXPECT_EQ(pldm_pdr_get_record_count(repo), 0u);
93 pldm_pdr_destroy(repo);
94
95 repo = pldm_pdr_init();
96 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
97 pldm_pdr_remove_remote_pdrs(repo);
98 EXPECT_EQ(pldm_pdr_get_record_count(repo), 0u);
99 pldm_pdr_destroy(repo);
100
101 repo = pldm_pdr_init();
102 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
103 pldm_pdr_remove_remote_pdrs(repo);
104 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
105 pldm_pdr_destroy(repo);
106
107 repo = pldm_pdr_init();
108 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
109 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
110 pldm_pdr_remove_remote_pdrs(repo);
111 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
112 pldm_pdr_destroy(repo);
113
114 repo = pldm_pdr_init();
115 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
116 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
117 pldm_pdr_remove_remote_pdrs(repo);
118 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
119 pldm_pdr_destroy(repo);
120
121 repo = pldm_pdr_init();
122 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
123 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
124 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
125 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
126 pldm_pdr_remove_remote_pdrs(repo);
127 EXPECT_EQ(pldm_pdr_get_record_count(repo), 0u);
128 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
129 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
130 EXPECT_EQ(pldm_pdr_get_record_count(repo), 2u);
131 pldm_pdr_destroy(repo);
132
133 repo = pldm_pdr_init();
134 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
135 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
136 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
137 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
138 pldm_pdr_remove_remote_pdrs(repo);
139 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
140 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
141 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
142 EXPECT_EQ(pldm_pdr_get_record_count(repo), 6u);
143 pldm_pdr_destroy(repo);
144
145 repo = pldm_pdr_init();
146 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
147 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
148 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
149 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
150 pldm_pdr_remove_remote_pdrs(repo);
151 EXPECT_EQ(pldm_pdr_get_record_count(repo), 3u);
152 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
153 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
154 EXPECT_EQ(pldm_pdr_get_record_count(repo), 5u);
155 pldm_pdr_destroy(repo);
156
157 repo = pldm_pdr_init();
158 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
159 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
160 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
161 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
162 pldm_pdr_remove_remote_pdrs(repo);
163 EXPECT_EQ(pldm_pdr_get_record_count(repo), 3u);
164 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
165 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
166 EXPECT_EQ(pldm_pdr_get_record_count(repo), 5u);
167 pldm_pdr_destroy(repo);
168
169 repo = pldm_pdr_init();
170 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
171 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
172 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
173 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
174 pldm_pdr_remove_remote_pdrs(repo);
175 EXPECT_EQ(pldm_pdr_get_record_count(repo), 2u);
176 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
177 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
178 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
179 pldm_pdr_destroy(repo);
180
181 repo = pldm_pdr_init();
182 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
183 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
184 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
185 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
186 pldm_pdr_remove_remote_pdrs(repo);
187 EXPECT_EQ(pldm_pdr_get_record_count(repo), 3u);
188 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
189 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
190 EXPECT_EQ(pldm_pdr_get_record_count(repo), 5u);
191 pldm_pdr_destroy(repo);
192
193 repo = pldm_pdr_init();
194 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
195 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
196 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
197 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
198 pldm_pdr_remove_remote_pdrs(repo);
199 EXPECT_EQ(pldm_pdr_get_record_count(repo), 2u);
200 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
201 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
202 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
203 pldm_pdr_destroy(repo);
204
205 repo = pldm_pdr_init();
206 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
207 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
208 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
209 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
210 pldm_pdr_remove_remote_pdrs(repo);
211 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
212 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
213 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
214 EXPECT_EQ(pldm_pdr_get_record_count(repo), 3u);
215 pldm_pdr_destroy(repo);
216
217 repo = pldm_pdr_init();
218 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
219 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
220 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
221 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
222 pldm_pdr_remove_remote_pdrs(repo);
223 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
224 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
225 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
226 EXPECT_EQ(pldm_pdr_get_record_count(repo), 3u);
227 pldm_pdr_destroy(repo);
228
229 repo = pldm_pdr_init();
230 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
231 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
232 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
233 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
234 pldm_pdr_remove_remote_pdrs(repo);
235 EXPECT_EQ(pldm_pdr_get_record_count(repo), 2u);
236 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
237 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
238 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
239 pldm_pdr_destroy(repo);
240
241 repo = pldm_pdr_init();
242 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
243 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
244 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
245 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
246 pldm_pdr_remove_remote_pdrs(repo);
247 EXPECT_EQ(pldm_pdr_get_record_count(repo), 2u);
248 auto handle = pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
249 EXPECT_EQ(handle, 3u);
250 handle = pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
251 EXPECT_EQ(handle, 4u);
252 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
253 pldm_pdr_destroy(repo);
254}
255
256TEST(PDRAccess, testGet)
257{
258 auto repo = pldm_pdr_init();
259
260 std::array<uint32_t, 10> in{100, 345, 3, 6, 89, 0, 11, 45, 23434, 123123};
261 pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in.data()), sizeof(in), 1,
262 false, 1);
263 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
264 EXPECT_EQ(pldm_pdr_get_repo_size(repo), sizeof(in));
265 uint32_t size{};
266 uint32_t nextRecHdl{};
267 uint8_t* outData = nullptr;
268 auto hdl = pldm_pdr_find_record(repo, 0, &outData, &size, &nextRecHdl);
269 EXPECT_NE(hdl, nullptr);
270 EXPECT_EQ(size, sizeof(in));
271 EXPECT_EQ(nextRecHdl, 0u);
272 EXPECT_EQ(memcmp(outData, in.data(), sizeof(in)), 0);
273 outData = nullptr;
274
275 auto hdl2 = pldm_pdr_find_record(repo, 1, &outData, &size, &nextRecHdl);
276 EXPECT_EQ(hdl, hdl2);
277 EXPECT_NE(hdl, nullptr);
278 EXPECT_EQ(size, sizeof(in));
279 EXPECT_EQ(nextRecHdl, 0u);
280 EXPECT_EQ(memcmp(outData, in.data(), sizeof(in)), 0);
281 outData = nullptr;
282
283 hdl = pldm_pdr_find_record(repo, htole32(0xdeaddead), &outData, &size,
284 &nextRecHdl);
285 EXPECT_EQ(hdl, nullptr);
286 EXPECT_EQ(size, 0u);
287 EXPECT_EQ(nextRecHdl, 0u);
288 EXPECT_EQ(outData, nullptr);
289 outData = nullptr;
290
291 std::array<uint32_t, 10> in2{1000, 3450, 30, 60, 890,
292 0, 110, 450, 234034, 123123};
293 pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in2.data()), sizeof(in2), 2,
294 false, 1);
295 pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in2.data()), sizeof(in2), 3,
296 false, 1);
297 pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in2.data()), sizeof(in2), 4,
298 true, 1);
299 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
300 EXPECT_EQ(pldm_pdr_get_repo_size(repo), sizeof(in2) * 4);
301 hdl = pldm_pdr_find_record(repo, 0, &outData, &size, &nextRecHdl);
302 EXPECT_NE(hdl, nullptr);
303 EXPECT_EQ(size, sizeof(in));
304 EXPECT_EQ(nextRecHdl, 2u);
305 EXPECT_EQ(memcmp(outData, in.data(), sizeof(in)), 0);
306 outData = nullptr;
307 hdl2 = pldm_pdr_find_record(repo, 1, &outData, &size, &nextRecHdl);
308 EXPECT_EQ(hdl, hdl2);
309 EXPECT_NE(hdl, nullptr);
310 EXPECT_EQ(size, sizeof(in));
311 EXPECT_EQ(nextRecHdl, 2u);
312 EXPECT_EQ(memcmp(outData, in.data(), sizeof(in)), 0);
313 outData = nullptr;
314 hdl = pldm_pdr_find_record(repo, 2, &outData, &size, &nextRecHdl);
315 EXPECT_NE(hdl, nullptr);
316 EXPECT_EQ(size, sizeof(in2));
317 EXPECT_EQ(nextRecHdl, 3u);
318 EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
319 outData = nullptr;
320 hdl = pldm_pdr_find_record(repo, 3, &outData, &size, &nextRecHdl);
321 EXPECT_NE(hdl, nullptr);
322 EXPECT_EQ(pldm_pdr_record_is_remote(hdl), false);
323 EXPECT_EQ(size, sizeof(in2));
324 EXPECT_EQ(nextRecHdl, 4u);
325 EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
326 outData = nullptr;
327 hdl = pldm_pdr_find_record(repo, 4, &outData, &size, &nextRecHdl);
328 EXPECT_NE(hdl, nullptr);
329 EXPECT_EQ(pldm_pdr_record_is_remote(hdl), true);
330 EXPECT_EQ(size, sizeof(in2));
331 EXPECT_EQ(nextRecHdl, 0u);
332 EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
333 outData = nullptr;
334
335 pldm_pdr_destroy(repo);
336}
337
338TEST(PDRAccess, testGetNext)
339{
340 auto repo = pldm_pdr_init();
341
342 std::array<uint32_t, 10> in{100, 345, 3, 6, 89, 0, 11, 45, 23434, 123123};
343 pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in.data()), sizeof(in), 1,
344 false, 1);
345 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
346 EXPECT_EQ(pldm_pdr_get_repo_size(repo), sizeof(in));
347 uint32_t size{};
348 uint32_t nextRecHdl{};
349 uint8_t* outData = nullptr;
350 auto hdl = pldm_pdr_find_record(repo, 0, &outData, &size, &nextRecHdl);
351 EXPECT_NE(hdl, nullptr);
352 EXPECT_EQ(size, sizeof(in));
353 EXPECT_EQ(nextRecHdl, 0u);
354 EXPECT_EQ(memcmp(outData, in.data(), sizeof(in)), 0);
355 outData = nullptr;
356
357 std::array<uint32_t, 10> in2{1000, 3450, 30, 60, 890,
358 0, 110, 450, 234034, 123123};
359 pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in2.data()), sizeof(in2), 2,
360 false, 1);
361 pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in2.data()), sizeof(in2), 3,
362 false, 1);
363 pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in2.data()), sizeof(in2), 4,
364 false, 1);
365 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
366 EXPECT_EQ(pldm_pdr_get_repo_size(repo), sizeof(in2) * 4);
367 hdl = pldm_pdr_get_next_record(repo, hdl, &outData, &size, &nextRecHdl);
368 EXPECT_NE(hdl, nullptr);
369 EXPECT_EQ(size, sizeof(in2));
370 EXPECT_EQ(nextRecHdl, 3u);
371 EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
372 outData = nullptr;
373 hdl = pldm_pdr_get_next_record(repo, hdl, &outData, &size, &nextRecHdl);
374 EXPECT_NE(hdl, nullptr);
375 EXPECT_EQ(size, sizeof(in2));
376 EXPECT_EQ(nextRecHdl, 4u);
377 EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
378 outData = nullptr;
379 hdl = pldm_pdr_get_next_record(repo, hdl, &outData, &size, &nextRecHdl);
380 EXPECT_NE(hdl, nullptr);
381 EXPECT_EQ(size, sizeof(in2));
382 EXPECT_EQ(nextRecHdl, 0u);
383 EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
384 outData = nullptr;
385
386 pldm_pdr_destroy(repo);
387}
388
389TEST(PDRAccess, testFindByType)
390{
391 auto repo = pldm_pdr_init();
392
393 std::array<uint8_t, sizeof(pldm_pdr_hdr)> data{};
394 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(data.data());
395 hdr->type = 1;
396 auto first = pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
397 hdr->type = 2;
398 auto second = pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
399 hdr->type = 3;
400 auto third = pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
401 hdr->type = 4;
402 auto fourth = pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
403
404 uint8_t* outData = nullptr;
405 uint32_t size{};
406 auto firstRec =
407 pldm_pdr_find_record_by_type(repo, 1, nullptr, &outData, &size);
408 EXPECT_EQ(pldm_pdr_get_record_handle(repo, firstRec), first);
409 outData = nullptr;
410 auto secondRec =
411 pldm_pdr_find_record_by_type(repo, 2, nullptr, &outData, &size);
412 EXPECT_EQ(pldm_pdr_get_record_handle(repo, secondRec), second);
413 outData = nullptr;
414 auto thirdRec =
415 pldm_pdr_find_record_by_type(repo, 3, nullptr, &outData, &size);
416 EXPECT_EQ(pldm_pdr_get_record_handle(repo, thirdRec), third);
417 outData = nullptr;
418 auto fourthRec =
419 pldm_pdr_find_record_by_type(repo, 4, nullptr, &outData, &size);
420 EXPECT_EQ(pldm_pdr_get_record_handle(repo, fourthRec), fourth);
421 outData = nullptr;
422 auto fifthRec =
423 pldm_pdr_find_record_by_type(repo, 5, nullptr, &outData, &size);
424 EXPECT_EQ(fifthRec, nullptr);
425 EXPECT_EQ(outData, nullptr);
426 EXPECT_EQ(size, 0u);
427
428 auto rec =
429 pldm_pdr_find_record_by_type(repo, 3, secondRec, &outData, &size);
430 EXPECT_EQ(pldm_pdr_get_record_handle(repo, rec), third);
431 outData = nullptr;
432 rec = pldm_pdr_find_record_by_type(repo, 4, secondRec, &outData, &size);
433 EXPECT_EQ(pldm_pdr_get_record_handle(repo, rec), fourth);
434 outData = nullptr;
435 rec = pldm_pdr_find_record_by_type(repo, 2, firstRec, &outData, &size);
436 EXPECT_EQ(pldm_pdr_get_record_handle(repo, rec), second);
437 outData = nullptr;
438
439 pldm_pdr_destroy(repo);
440}
441
442TEST(PDRUpdate, testAddFruRecordSet)
443{
444 auto repo = pldm_pdr_init();
445
446 auto handle = pldm_pdr_add_fru_record_set(repo, 1, 10, 1, 0, 100, 0);
447 EXPECT_EQ(handle, 1u);
448 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
449 EXPECT_EQ(pldm_pdr_get_repo_size(repo),
450 sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_fru_record_set));
451 uint32_t size{};
452 uint32_t nextRecHdl{};
453 uint8_t* outData = nullptr;
454 auto hdl = pldm_pdr_find_record(repo, 0, &outData, &size, &nextRecHdl);
455 EXPECT_NE(hdl, nullptr);
456 EXPECT_EQ(size, sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_fru_record_set));
457 EXPECT_EQ(nextRecHdl, 0u);
458 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(outData);
459 EXPECT_EQ(hdr->version, 1u);
460 EXPECT_EQ(hdr->type, PLDM_PDR_FRU_RECORD_SET);
461 EXPECT_EQ(hdr->length, htole16(sizeof(pldm_pdr_fru_record_set)));
462 EXPECT_EQ(hdr->record_handle, htole32(1));
463 pldm_pdr_fru_record_set* fru = reinterpret_cast<pldm_pdr_fru_record_set*>(
464 outData + sizeof(pldm_pdr_hdr));
465 EXPECT_EQ(fru->terminus_handle, htole16(1));
466 EXPECT_EQ(fru->fru_rsi, htole16(10));
467 EXPECT_EQ(fru->entity_type, htole16(1));
468 EXPECT_EQ(fru->entity_instance_num, htole16(0));
469 EXPECT_EQ(fru->container_id, htole16(100));
470 outData = nullptr;
471
472 handle = pldm_pdr_add_fru_record_set(repo, 2, 11, 2, 1, 101, 0);
473 EXPECT_EQ(handle, 2u);
474 EXPECT_EQ(pldm_pdr_get_record_count(repo), 2u);
475 EXPECT_EQ(pldm_pdr_get_repo_size(repo),
476 2 * (sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_fru_record_set)));
477 hdl = pldm_pdr_find_record(repo, 2, &outData, &size, &nextRecHdl);
478 EXPECT_NE(hdl, nullptr);
479 EXPECT_EQ(size, sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_fru_record_set));
480 EXPECT_EQ(nextRecHdl, 0u);
481 hdr = reinterpret_cast<pldm_pdr_hdr*>(outData);
482 EXPECT_EQ(hdr->version, 1u);
483 EXPECT_EQ(hdr->type, PLDM_PDR_FRU_RECORD_SET);
484 EXPECT_EQ(hdr->length, htole16(sizeof(pldm_pdr_fru_record_set)));
485 EXPECT_EQ(hdr->record_handle, htole32(2));
486 fru = reinterpret_cast<pldm_pdr_fru_record_set*>(outData +
487 sizeof(pldm_pdr_hdr));
488 EXPECT_EQ(fru->terminus_handle, htole16(2));
489 EXPECT_EQ(fru->fru_rsi, htole16(11));
490 EXPECT_EQ(fru->entity_type, htole16(2));
491 EXPECT_EQ(fru->entity_instance_num, htole16(1));
492 EXPECT_EQ(fru->container_id, htole16(101));
493 outData = nullptr;
494
495 hdl = pldm_pdr_find_record(repo, 1, &outData, &size, &nextRecHdl);
496 EXPECT_NE(hdl, nullptr);
497 EXPECT_EQ(size, sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_fru_record_set));
498 EXPECT_EQ(nextRecHdl, 2u);
499 hdr = reinterpret_cast<pldm_pdr_hdr*>(outData);
500 EXPECT_EQ(hdr->version, 1u);
501 EXPECT_EQ(hdr->type, PLDM_PDR_FRU_RECORD_SET);
502 EXPECT_EQ(hdr->length, htole16(sizeof(pldm_pdr_fru_record_set)));
503 EXPECT_EQ(hdr->record_handle, htole32(1));
504 fru = reinterpret_cast<pldm_pdr_fru_record_set*>(outData +
505 sizeof(pldm_pdr_hdr));
506 EXPECT_EQ(fru->terminus_handle, htole16(1));
507 EXPECT_EQ(fru->fru_rsi, htole16(10));
508 EXPECT_EQ(fru->entity_type, htole16(1));
509 EXPECT_EQ(fru->entity_instance_num, htole16(0));
510 EXPECT_EQ(fru->container_id, htole16(100));
511 outData = nullptr;
512
513 pldm_pdr_destroy(repo);
514}
515
516TEST(PDRUpdate, tesFindtFruRecordSet)
517{
518 auto repo = pldm_pdr_init();
519
520 uint16_t terminusHdl{};
521 uint16_t entityType{};
522 uint16_t entityInstanceNum{};
523 uint16_t containerId{};
524 auto first = pldm_pdr_add_fru_record_set(repo, 1, 1, 1, 0, 100, 1);
525 auto second = pldm_pdr_add_fru_record_set(repo, 1, 2, 1, 1, 100, 2);
526 auto third = pldm_pdr_add_fru_record_set(repo, 1, 3, 1, 2, 100, 3);
527 EXPECT_EQ(first, pldm_pdr_get_record_handle(
528 repo, pldm_pdr_fru_record_set_find_by_rsi(
529 repo, 1, &terminusHdl, &entityType,
530 &entityInstanceNum, &containerId)));
531 EXPECT_EQ(second, pldm_pdr_get_record_handle(
532 repo, pldm_pdr_fru_record_set_find_by_rsi(
533 repo, 2, &terminusHdl, &entityType,
534 &entityInstanceNum, &containerId)));
535 EXPECT_EQ(third, pldm_pdr_get_record_handle(
536 repo, pldm_pdr_fru_record_set_find_by_rsi(
537 repo, 3, &terminusHdl, &entityType,
538 &entityInstanceNum, &containerId)));
539 EXPECT_EQ(terminusHdl, 1u);
540 EXPECT_EQ(entityType, 1u);
541 EXPECT_EQ(entityInstanceNum, 2u);
542 EXPECT_EQ(containerId, 100u);
543 EXPECT_EQ(nullptr, pldm_pdr_fru_record_set_find_by_rsi(
544 repo, 4, &terminusHdl, &entityType,
545 &entityInstanceNum, &containerId));
546
547 pldm_pdr_destroy(repo);
548}
549
550TEST(EntityAssociationPDR, testInit)
551{
552 auto tree = pldm_entity_association_tree_init();
553 EXPECT_NE(tree, nullptr);
554 pldm_entity_association_tree_destroy(tree);
555}
556
557TEST(EntityAssociationPDR, testBuild)
558{
559 // 1
560 // |
561 // 2--3--4
562 // |
563 // 5--6--7
564 // | |
565 // 9 8
566
567 pldm_entity entities[9]{};
568
569 entities[0].entity_type = 1;
570 entities[1].entity_type = 2;
571 entities[2].entity_type = 2;
572 entities[3].entity_type = 3;
573 entities[4].entity_type = 4;
574 entities[5].entity_type = 5;
575 entities[6].entity_type = 5;
576 entities[7].entity_type = 6;
577 entities[8].entity_type = 7;
578
579 auto tree = pldm_entity_association_tree_init();
580
581 auto l1 = pldm_entity_association_tree_add(
582 tree, &entities[0], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
583 EXPECT_NE(l1, nullptr);
584 auto l2a = pldm_entity_association_tree_add(
585 tree, &entities[1], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
586 EXPECT_NE(l2a, nullptr);
587 auto l2b = pldm_entity_association_tree_add(
588 tree, &entities[2], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
589 EXPECT_NE(l2b, nullptr);
590 auto l2c = pldm_entity_association_tree_add(
591 tree, &entities[3], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
592 EXPECT_NE(l2c, nullptr);
593 auto l3a = pldm_entity_association_tree_add(
594 tree, &entities[4], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
595 EXPECT_NE(l3a, nullptr);
596 auto l3b = pldm_entity_association_tree_add(
597 tree, &entities[5], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
598 EXPECT_NE(l3b, nullptr);
599 auto l3c = pldm_entity_association_tree_add(
600 tree, &entities[6], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
601 EXPECT_NE(l3b, nullptr);
602 auto l4a = pldm_entity_association_tree_add(
603 tree, &entities[7], 0xFFFF, l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
604 EXPECT_NE(l4a, nullptr);
605 auto l4b = pldm_entity_association_tree_add(
606 tree, &entities[8], 0xFFFF, l3b, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
607 EXPECT_NE(l4b, nullptr);
608
609 EXPECT_EQ(pldm_entity_is_node_parent(l1), true);
610 EXPECT_EQ(pldm_entity_is_node_parent(l2a), true);
611 EXPECT_EQ(pldm_entity_is_node_parent(l3a), true);
612 EXPECT_EQ(pldm_entity_is_node_parent(l3b), true);
613
614 EXPECT_EQ(pldm_entity_is_node_parent(l2b), false);
615 EXPECT_EQ(pldm_entity_is_node_parent(l2c), false);
616 EXPECT_EQ(pldm_entity_is_node_parent(l3c), false);
617 EXPECT_EQ(pldm_entity_is_node_parent(l4a), false);
618 EXPECT_EQ(pldm_entity_is_node_parent(l4b), false);
619
620 EXPECT_EQ(pldm_entity_is_exist_parent(l1), false);
621
622 pldm_entity nodeL1 = pldm_entity_extract(l1);
623 pldm_entity parentL2a = pldm_entity_get_parent(l2a);
624 pldm_entity parentL2b = pldm_entity_get_parent(l2b);
625 pldm_entity parentL2c = pldm_entity_get_parent(l2c);
626 EXPECT_EQ(pldm_entity_is_exist_parent(l2a), true);
627 EXPECT_EQ(pldm_entity_is_exist_parent(l2b), true);
628 EXPECT_EQ(pldm_entity_is_exist_parent(l2c), true);
629 EXPECT_EQ(parentL2a.entity_type, nodeL1.entity_type);
630 EXPECT_EQ(parentL2a.entity_instance_num, nodeL1.entity_instance_num);
631 EXPECT_EQ(parentL2a.entity_container_id, nodeL1.entity_container_id);
632 EXPECT_EQ(parentL2b.entity_type, nodeL1.entity_type);
633 EXPECT_EQ(parentL2b.entity_instance_num, nodeL1.entity_instance_num);
634 EXPECT_EQ(parentL2b.entity_container_id, nodeL1.entity_container_id);
635 EXPECT_EQ(parentL2c.entity_type, nodeL1.entity_type);
636 EXPECT_EQ(parentL2c.entity_instance_num, nodeL1.entity_instance_num);
637 EXPECT_EQ(parentL2c.entity_container_id, nodeL1.entity_container_id);
638
639 pldm_entity nodeL2a = pldm_entity_extract(l2a);
640 pldm_entity parentL3a = pldm_entity_get_parent(l3a);
641 pldm_entity parentL3b = pldm_entity_get_parent(l3b);
642 pldm_entity parentL3c = pldm_entity_get_parent(l3c);
643 EXPECT_EQ(pldm_entity_is_exist_parent(l3a), true);
644 EXPECT_EQ(pldm_entity_is_exist_parent(l3b), true);
645 EXPECT_EQ(pldm_entity_is_exist_parent(l3c), true);
646 EXPECT_EQ(parentL3a.entity_type, nodeL2a.entity_type);
647 EXPECT_EQ(parentL3a.entity_instance_num, nodeL2a.entity_instance_num);
648 EXPECT_EQ(parentL3a.entity_container_id, nodeL2a.entity_container_id);
649 EXPECT_EQ(parentL3b.entity_type, nodeL2a.entity_type);
650 EXPECT_EQ(parentL3b.entity_instance_num, nodeL2a.entity_instance_num);
651 EXPECT_EQ(parentL3b.entity_container_id, nodeL2a.entity_container_id);
652 EXPECT_EQ(parentL3c.entity_type, nodeL2a.entity_type);
653 EXPECT_EQ(parentL3c.entity_instance_num, nodeL2a.entity_instance_num);
654 EXPECT_EQ(parentL3c.entity_container_id, nodeL2a.entity_container_id);
655
656 pldm_entity nodeL3a = pldm_entity_extract(l3a);
657 pldm_entity parentL4a = pldm_entity_get_parent(l4a);
658 EXPECT_EQ(pldm_entity_is_exist_parent(l4a), true);
659 EXPECT_EQ(parentL4a.entity_type, nodeL3a.entity_type);
660 EXPECT_EQ(parentL4a.entity_instance_num, nodeL3a.entity_instance_num);
661 EXPECT_EQ(parentL4a.entity_container_id, nodeL3a.entity_container_id);
662
663 pldm_entity nodeL3b = pldm_entity_extract(l3b);
664 pldm_entity parentL4b = pldm_entity_get_parent(l4b);
665 EXPECT_EQ(pldm_entity_is_exist_parent(l4b), true);
666 EXPECT_EQ(parentL4b.entity_type, nodeL3b.entity_type);
667 EXPECT_EQ(parentL4b.entity_instance_num, nodeL3b.entity_instance_num);
668 EXPECT_EQ(parentL4b.entity_container_id, nodeL3b.entity_container_id);
669
670 size_t num{};
671 pldm_entity* out = nullptr;
672 pldm_entity_association_tree_visit(tree, &out, &num);
673 EXPECT_EQ(num, 9u);
674
675 EXPECT_EQ(out[0].entity_type, 1u);
676 EXPECT_EQ(out[0].entity_instance_num, 1u);
677 EXPECT_EQ(out[0].entity_container_id, 0u);
678
679 EXPECT_EQ(out[1].entity_type, 2u);
680 EXPECT_EQ(out[1].entity_instance_num, 1u);
681 EXPECT_EQ(out[1].entity_container_id, 1u);
682 EXPECT_EQ(out[2].entity_type, 2u);
683 EXPECT_EQ(out[2].entity_instance_num, 2u);
684 EXPECT_EQ(out[2].entity_container_id, 1u);
685 EXPECT_EQ(out[3].entity_type, 3u);
686 EXPECT_EQ(out[3].entity_instance_num, 1u);
687 EXPECT_EQ(out[3].entity_container_id, 1u);
688
689 EXPECT_EQ(out[4].entity_type, 4u);
690 EXPECT_EQ(out[4].entity_instance_num, 1u);
691 EXPECT_EQ(out[4].entity_container_id, 2u);
692 EXPECT_EQ(out[5].entity_type, 5u);
693 EXPECT_EQ(out[5].entity_instance_num, 1u);
694 EXPECT_EQ(out[5].entity_container_id, 2u);
695 EXPECT_EQ(out[6].entity_type, 5u);
696 EXPECT_EQ(out[6].entity_instance_num, 2u);
697 EXPECT_EQ(out[6].entity_container_id, 2u);
698
699 EXPECT_EQ(out[7].entity_type, 7u);
700 EXPECT_EQ(out[7].entity_instance_num, 1u);
701 EXPECT_EQ(out[7].entity_container_id, 4u);
702 EXPECT_EQ(out[8].entity_type, 6u);
703 EXPECT_EQ(out[8].entity_instance_num, 1u);
704 EXPECT_EQ(out[8].entity_container_id, 3u);
705
706 free(out);
707
708 pldm_entity p1 = pldm_entity_extract(l1);
709 EXPECT_EQ(p1.entity_type, 1u);
710 EXPECT_EQ(p1.entity_instance_num, 1u);
711 EXPECT_EQ(p1.entity_container_id, 0u);
712
713 pldm_entity p2a = pldm_entity_extract(l2a);
714 EXPECT_EQ(p2a.entity_type, 2u);
715 EXPECT_EQ(p2a.entity_instance_num, 1u);
716 EXPECT_EQ(p2a.entity_container_id, 1u);
717 pldm_entity p2b = pldm_entity_extract(l2b);
718 EXPECT_EQ(p2b.entity_type, 2u);
719 EXPECT_EQ(p2b.entity_instance_num, 2u);
720 EXPECT_EQ(p2b.entity_container_id, 1u);
721 pldm_entity p2c = pldm_entity_extract(l2c);
722 EXPECT_EQ(p2c.entity_type, 3u);
723 EXPECT_EQ(p2c.entity_instance_num, 1u);
724 EXPECT_EQ(p2c.entity_container_id, 1u);
725
726 pldm_entity p3a = pldm_entity_extract(l3a);
727 EXPECT_EQ(p3a.entity_type, 4u);
728 EXPECT_EQ(p3a.entity_instance_num, 1u);
729 EXPECT_EQ(p3a.entity_container_id, 2u);
730 pldm_entity p3b = pldm_entity_extract(l3b);
731 EXPECT_EQ(p3b.entity_type, 5u);
732 EXPECT_EQ(p3b.entity_instance_num, 1u);
733 EXPECT_EQ(p3b.entity_container_id, 2u);
734 pldm_entity p3c = pldm_entity_extract(l3c);
735 EXPECT_EQ(p3c.entity_type, 5u);
736 EXPECT_EQ(p3c.entity_instance_num, 2u);
737 EXPECT_EQ(p3c.entity_container_id, 2u);
738
739 pldm_entity p4a = pldm_entity_extract(l4a);
740 EXPECT_EQ(p4a.entity_type, 6u);
741 EXPECT_EQ(p4a.entity_instance_num, 1u);
742 EXPECT_EQ(p4a.entity_container_id, 3u);
743 pldm_entity p4b = pldm_entity_extract(l4b);
744 EXPECT_EQ(p4b.entity_type, 7u);
745 EXPECT_EQ(p4b.entity_instance_num, 1u);
746 EXPECT_EQ(p4b.entity_container_id, 4u);
747
748 pldm_entity_association_tree_destroy(tree);
749}
750
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500751#if LIBPLDM_API_TESTING
752TEST(EntityAssociationPDR, findAndAddRemotePDR)
753{
754 // Tree - 1
755 //
756 // 11521(1,0)
757 // |
758 // 45 (1,1)
759 // |
760 // 64 (1,2)
761 // |
762 // ------------------
763 // | |
764 // 67(0,3) 67(1,3)
765 // | |
766 // 135(0,4) 135(0,5)
767 // | |
768 // 32903(0,6) 32903(0,7)
769
770 pldm_entity entities[9]{};
771 entities[0].entity_type = 11521;
772 entities[1].entity_type = 45;
773 entities[2].entity_type = 64;
774 entities[3].entity_type = 67;
775 entities[4].entity_type = 67;
776 entities[5].entity_type = 135;
777 entities[5].entity_container_id = 2;
778 entities[6].entity_type = 135;
779 entities[6].entity_container_id = 3;
780 entities[7].entity_type = 32903;
781 entities[8].entity_type = 32903;
782 auto tree = pldm_entity_association_tree_init();
783 auto l1 = pldm_entity_association_tree_add_entity(
784 tree, &entities[0], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_LOGICAL,
785 false, true, 0xFFFF);
786 EXPECT_NE(l1, nullptr);
787 auto l2 = pldm_entity_association_tree_add_entity(
788 tree, &entities[1], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
789 false, 0xFFFF);
790 EXPECT_NE(l2, nullptr);
791 auto l3 = pldm_entity_association_tree_add_entity(
792 tree, &entities[2], 0xFFFF, l2, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
793 true, 0xFFFF);
794 EXPECT_NE(l3, nullptr);
795 auto l4a = pldm_entity_association_tree_add_entity(
796 tree, &entities[3], 0, l3, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
797 false, 0xFFFF);
798 EXPECT_NE(l4a, nullptr);
799 auto l4b = pldm_entity_association_tree_add_entity(
800 tree, &entities[4], 1, l3, PLDM_ENTITY_ASSOCIAION_PHYSICAL, true, true,
801 0xFFFF);
802 EXPECT_NE(l4b, nullptr);
803 auto l5a = pldm_entity_association_tree_add_entity(
804 tree, &entities[5], 0, l4a, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
805 false, 0xFFFF);
806 EXPECT_NE(l5a, nullptr);
807 auto l5b = pldm_entity_association_tree_add_entity(
808 tree, &entities[6], 0, l4b, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
809 false, 0xFFFF);
810 EXPECT_NE(l5b, nullptr);
811 pldm_entity entity{};
812 entity.entity_type = 135;
813 entity.entity_instance_num = 0;
814 entity.entity_container_id = 2;
815 auto result1 = pldm_entity_association_tree_find(tree, &entity);
816 EXPECT_EQ(result1, l5a);
817 EXPECT_EQ(entities[5].entity_container_id, 2);
818 auto l6a = pldm_entity_association_tree_add_entity(
819 tree, &entities[7], 0, result1, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
820 false, 0xFFFF);
821 EXPECT_NE(l6a, nullptr);
822 entity.entity_type = 135;
823 entity.entity_instance_num = 0;
824 entity.entity_container_id = 3;
825 auto result2 = pldm_entity_association_tree_find(tree, &entity);
826 EXPECT_NE(result2, l5b);
827 EXPECT_EQ(entities[6].entity_container_id, 3);
828 auto l7a = pldm_entity_association_tree_add_entity(
829 tree, &entities[8], 0, result2, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
830 false, 0xFFFF);
831 EXPECT_EQ(l7a, nullptr);
832 pldm_entity_association_tree_destroy(tree);
833}
834#endif
835
Andrew Jeffery9c766792022-08-10 23:12:49 +0930836TEST(EntityAssociationPDR, testSpecialTrees)
837{
838 pldm_entity entities[3]{};
839
840 entities[0].entity_type = 1;
841 entities[1].entity_type = 2;
842 entities[2].entity_type = 1;
843
844 // A
845 auto tree = pldm_entity_association_tree_init();
846 auto node = pldm_entity_association_tree_add(
847 tree, &entities[0], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
848 EXPECT_NE(node, nullptr);
849 size_t num{};
850 pldm_entity* out = nullptr;
851 pldm_entity_association_tree_visit(tree, &out, &num);
852 EXPECT_EQ(num, 1u);
853 EXPECT_EQ(out[0].entity_type, 1u);
854 EXPECT_EQ(out[0].entity_instance_num, 1u);
855 EXPECT_EQ(out[0].entity_container_id, 0u);
856 free(out);
857 pldm_entity_association_tree_destroy(tree);
858
859 // A-A-A
860 tree = pldm_entity_association_tree_init();
861 node = pldm_entity_association_tree_add(tree, &entities[0], 0xFFFF, nullptr,
862 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
863 EXPECT_NE(node, nullptr);
864 node = pldm_entity_association_tree_add(tree, &entities[1], 0xFFFF, nullptr,
865 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
866 EXPECT_NE(node, nullptr);
867 node = pldm_entity_association_tree_add(tree, &entities[2], 0xFFFF, nullptr,
868 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
869 EXPECT_NE(node, nullptr);
870 pldm_entity_association_tree_visit(tree, &out, &num);
871 EXPECT_EQ(num, 3u);
872 EXPECT_EQ(out[0].entity_type, 1u);
873 EXPECT_EQ(out[0].entity_instance_num, 1u);
874 EXPECT_EQ(out[0].entity_container_id, 0u);
875 EXPECT_EQ(out[1].entity_type, 1u);
876 EXPECT_EQ(out[1].entity_instance_num, 2u);
877 EXPECT_EQ(out[1].entity_container_id, 0u);
878 EXPECT_EQ(out[2].entity_type, 2u);
879 EXPECT_EQ(out[2].entity_instance_num, 1u);
880 EXPECT_EQ(out[2].entity_container_id, 0u);
881 free(out);
882 pldm_entity_association_tree_destroy(tree);
883
884 // A
885 // |
886 // A
887 // |
888 // A
889 tree = pldm_entity_association_tree_init();
890 node = pldm_entity_association_tree_add(tree, &entities[0], 0xFFFF, nullptr,
891 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
892 EXPECT_NE(node, nullptr);
893 auto node1 = pldm_entity_association_tree_add(
894 tree, &entities[1], 0xFFFF, node, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
895 EXPECT_NE(node1, nullptr);
896 auto node2 = pldm_entity_association_tree_add(
897 tree, &entities[2], 0xFFFF, node1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
898 EXPECT_NE(node2, nullptr);
899 pldm_entity_association_tree_visit(tree, &out, &num);
900 EXPECT_EQ(num, 3u);
901 EXPECT_EQ(out[0].entity_type, 1u);
902 EXPECT_EQ(out[0].entity_instance_num, 1u);
903 EXPECT_EQ(out[0].entity_container_id, 0u);
904 EXPECT_EQ(out[1].entity_type, 2u);
905 EXPECT_EQ(out[1].entity_instance_num, 1u);
906 EXPECT_EQ(out[1].entity_container_id, 1u);
907 EXPECT_EQ(out[2].entity_type, 1u);
908 EXPECT_EQ(out[2].entity_instance_num, 1u);
909 EXPECT_EQ(out[2].entity_container_id, 2u);
910 free(out);
911 pldm_entity_association_tree_destroy(tree);
912
913 // A-A
914 // |
915 // A-A
916 tree = pldm_entity_association_tree_init();
917 node = pldm_entity_association_tree_add(tree, &entities[0], 0xFFFF, nullptr,
918 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
919 EXPECT_NE(node, nullptr);
920 node = pldm_entity_association_tree_add(tree, &entities[0], 0xFFFF, nullptr,
921 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
922 EXPECT_NE(node, nullptr);
923 node1 = pldm_entity_association_tree_add(tree, &entities[1], 0xFFFF, node,
924 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
925 EXPECT_NE(node1, nullptr);
926 node2 = pldm_entity_association_tree_add(tree, &entities[2], 0xFFFF, node,
927 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
928 EXPECT_NE(node2, nullptr);
929 pldm_entity_association_tree_visit(tree, &out, &num);
930 EXPECT_EQ(num, 4u);
931 EXPECT_EQ(out[0].entity_type, 1u);
932 EXPECT_EQ(out[0].entity_instance_num, 1u);
933 EXPECT_EQ(out[0].entity_container_id, 0u);
934 EXPECT_EQ(out[1].entity_type, 1u);
935 EXPECT_EQ(out[1].entity_instance_num, 2u);
936 EXPECT_EQ(out[1].entity_container_id, 0u);
937 EXPECT_EQ(out[2].entity_type, 2u);
938 EXPECT_EQ(out[2].entity_instance_num, 1u);
939 EXPECT_EQ(out[2].entity_container_id, 1u);
940 EXPECT_EQ(out[3].entity_type, 1u);
941 EXPECT_EQ(out[3].entity_instance_num, 1u);
942 EXPECT_EQ(out[3].entity_container_id, 1u);
943 free(out);
944 pldm_entity_association_tree_destroy(tree);
945}
946
947TEST(EntityAssociationPDR, testPDR)
948{
949 // e = entity type, c = container id, i = instance num
950
951 // INPUT
952 // 1(e=1)--1a(e=2)
953 // |
954 // 2(e=2)--3(e=2)--4(e=2)--5(e=3)
955 // |
956 // 6(e=4)--7(e=5)--8(e=5)--9(e=5)
957 // | |
958 // 11(e=6) 10(e=7)
959
960 // Expected OUTPUT
961 // 1(e=1,c=0,i=1)
962 // |
963 // 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)
964 // |
965 // 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)
966 // | |
967 // 10(e=6,c=3,i=1) 11(e=7,c=4,i=1)
968 pldm_entity entities[11]{};
969
970 entities[0].entity_type = 1;
971 entities[1].entity_type = 2;
972 entities[2].entity_type = 3;
973 entities[3].entity_type = 2;
974 entities[4].entity_type = 3;
975 entities[5].entity_type = 4;
976 entities[6].entity_type = 5;
977 entities[7].entity_type = 5;
978 entities[8].entity_type = 5;
979 entities[9].entity_type = 6;
980 entities[10].entity_type = 7;
981
982 auto tree = pldm_entity_association_tree_init();
983
984 auto l1 = pldm_entity_association_tree_add(
985 tree, &entities[0], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
986 EXPECT_NE(l1, nullptr);
987 auto l1a = pldm_entity_association_tree_add(
988 tree, &entities[1], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
989 EXPECT_NE(l1a, nullptr);
990
991 auto l2a = pldm_entity_association_tree_add(
992 tree, &entities[1], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
993 EXPECT_NE(l2a, nullptr);
994 auto l2b = pldm_entity_association_tree_add(tree, &entities[2], 0xFFFF, l1,
995 PLDM_ENTITY_ASSOCIAION_LOGICAL);
996 EXPECT_NE(l2b, nullptr);
997 auto l2c = pldm_entity_association_tree_add(
998 tree, &entities[3], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
999 EXPECT_NE(l2c, nullptr);
1000 auto l2d = pldm_entity_association_tree_add(tree, &entities[4], 0xFFFF, l1,
1001 PLDM_ENTITY_ASSOCIAION_LOGICAL);
1002 EXPECT_NE(l2d, nullptr);
1003
1004 auto l3a = pldm_entity_association_tree_add(
1005 tree, &entities[5], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1006 EXPECT_NE(l3a, nullptr);
1007 auto l3b = pldm_entity_association_tree_add(
1008 tree, &entities[6], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1009 EXPECT_NE(l3b, nullptr);
1010 auto l3c = pldm_entity_association_tree_add(tree, &entities[7], 0xFFFF, l2a,
1011 PLDM_ENTITY_ASSOCIAION_LOGICAL);
1012 EXPECT_NE(l3c, nullptr);
1013 auto l3d = pldm_entity_association_tree_add(tree, &entities[8], 0xFFFF, l2a,
1014 PLDM_ENTITY_ASSOCIAION_LOGICAL);
1015 EXPECT_NE(l3d, nullptr);
1016
1017 auto l4a = pldm_entity_association_tree_add(
1018 tree, &entities[9], 0xFFFF, l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1019 EXPECT_NE(l4a, nullptr);
1020 auto l4b = pldm_entity_association_tree_add(
1021 tree, &entities[10], 0xFFFF, l3b, PLDM_ENTITY_ASSOCIAION_LOGICAL);
1022 EXPECT_NE(l4b, nullptr);
1023
1024 EXPECT_EQ(pldm_entity_get_num_children(l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL),
1025 2);
1026 EXPECT_EQ(pldm_entity_get_num_children(l1, PLDM_ENTITY_ASSOCIAION_LOGICAL),
1027 2);
1028 EXPECT_EQ(
1029 pldm_entity_get_num_children(l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL), 2);
1030 EXPECT_EQ(
1031 pldm_entity_get_num_children(l3b, PLDM_ENTITY_ASSOCIAION_PHYSICAL), 0);
1032 EXPECT_EQ(pldm_entity_get_num_children(l3b, PLDM_ENTITY_ASSOCIAION_LOGICAL),
1033 1);
1034
1035 auto repo = pldm_pdr_init();
1036 pldm_entity_association_pdr_add(tree, repo, false, 1);
1037
1038 EXPECT_EQ(pldm_pdr_get_record_count(repo), 6u);
1039
1040 uint32_t currRecHandle{};
1041 uint32_t nextRecHandle{};
1042 uint8_t* data = nullptr;
1043 uint32_t size{};
1044 uint32_t commonSize = sizeof(struct pldm_pdr_hdr) + sizeof(uint16_t) +
1045 sizeof(uint8_t) + sizeof(pldm_entity) +
1046 sizeof(uint8_t);
1047
1048 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1049 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1050 l1, PLDM_ENTITY_ASSOCIAION_LOGICAL) *
1051 sizeof(pldm_entity)));
1052 uint8_t* start = data;
1053 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
1054 EXPECT_EQ(le32toh(hdr->record_handle), 1u);
1055 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
1056 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
1057 start += sizeof(pldm_pdr_hdr);
1058 uint16_t* containerId = reinterpret_cast<uint16_t*>(start);
1059 EXPECT_EQ(le16toh(*containerId), 1u);
1060 start += sizeof(uint16_t);
1061 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_LOGICAL);
1062 start += sizeof(uint8_t);
1063 pldm_entity* entity = reinterpret_cast<pldm_entity*>(start);
1064 EXPECT_EQ(le16toh(entity->entity_type), 1u);
1065 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1066 EXPECT_EQ(le16toh(entity->entity_container_id), 0u);
1067 start += sizeof(pldm_entity);
1068 EXPECT_EQ(*start,
1069 pldm_entity_get_num_children(l1, PLDM_ENTITY_ASSOCIAION_LOGICAL));
1070 start += sizeof(uint8_t);
1071 entity = reinterpret_cast<pldm_entity*>(start);
1072 EXPECT_EQ(le16toh(entity->entity_type), 3u);
1073 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1074 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
1075 start += sizeof(pldm_entity);
1076 entity = reinterpret_cast<pldm_entity*>(start);
1077 EXPECT_EQ(le16toh(entity->entity_type), 3u);
1078 EXPECT_EQ(le16toh(entity->entity_instance_num), 2u);
1079 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301080
1081 currRecHandle = nextRecHandle;
1082
1083 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1084 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1085 l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL) *
1086 sizeof(pldm_entity)));
1087 start = data;
1088 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
1089 EXPECT_EQ(le32toh(hdr->record_handle), 2u);
1090 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
1091 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
1092 start += sizeof(pldm_pdr_hdr);
1093 containerId = reinterpret_cast<uint16_t*>(start);
1094 EXPECT_EQ(le16toh(*containerId), 1u);
1095 start += sizeof(uint16_t);
1096 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1097 start += sizeof(uint8_t);
1098 entity = reinterpret_cast<pldm_entity*>(start);
1099 EXPECT_EQ(le16toh(entity->entity_type), 1u);
1100 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1101 EXPECT_EQ(le16toh(entity->entity_container_id), 0u);
1102 start += sizeof(pldm_entity);
1103 EXPECT_EQ(*start, pldm_entity_get_num_children(
1104 l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL));
1105 start += sizeof(uint8_t);
1106 entity = reinterpret_cast<pldm_entity*>(start);
1107 EXPECT_EQ(le16toh(entity->entity_type), 2u);
1108 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1109 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
1110 start += sizeof(pldm_entity);
1111 entity = reinterpret_cast<pldm_entity*>(start);
1112 EXPECT_EQ(le16toh(entity->entity_type), 2u);
1113 EXPECT_EQ(le16toh(entity->entity_instance_num), 2u);
1114 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301115
1116 currRecHandle = nextRecHandle;
1117
1118 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1119 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1120 l2a, PLDM_ENTITY_ASSOCIAION_LOGICAL) *
1121 sizeof(pldm_entity)));
1122 start = data;
1123 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
1124 EXPECT_EQ(le32toh(hdr->record_handle), 3u);
1125 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
1126 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
1127 start += sizeof(pldm_pdr_hdr);
1128 containerId = reinterpret_cast<uint16_t*>(start);
1129 EXPECT_EQ(le16toh(*containerId), 2u);
1130 start += sizeof(uint16_t);
1131 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_LOGICAL);
1132 start += sizeof(uint8_t);
1133 entity = reinterpret_cast<pldm_entity*>(start);
1134 EXPECT_EQ(le16toh(entity->entity_type), 2u);
1135 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1136 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
1137 start += sizeof(pldm_entity);
1138 EXPECT_EQ(*start, pldm_entity_get_num_children(
1139 l2a, PLDM_ENTITY_ASSOCIAION_LOGICAL));
1140 start += sizeof(uint8_t);
1141 entity = reinterpret_cast<pldm_entity*>(start);
1142 EXPECT_EQ(le16toh(entity->entity_type), 5);
1143 EXPECT_EQ(le16toh(entity->entity_instance_num), 2u);
1144 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
1145 start += sizeof(pldm_entity);
1146 entity = reinterpret_cast<pldm_entity*>(start);
1147 EXPECT_EQ(le16toh(entity->entity_type), 5u);
1148 EXPECT_EQ(le16toh(entity->entity_instance_num), 3u);
1149 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301150
1151 currRecHandle = nextRecHandle;
1152
1153 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1154 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1155 l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL) *
1156 sizeof(pldm_entity)));
1157 start = data;
1158 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
1159 EXPECT_EQ(le32toh(hdr->record_handle), 4u);
1160 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
1161 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
1162 start += sizeof(pldm_pdr_hdr);
1163 containerId = reinterpret_cast<uint16_t*>(start);
1164 EXPECT_EQ(le16toh(*containerId), 2u);
1165 start += sizeof(uint16_t);
1166 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1167 start += sizeof(uint8_t);
1168 entity = reinterpret_cast<pldm_entity*>(start);
1169 EXPECT_EQ(le16toh(entity->entity_type), 2u);
1170 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1171 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
1172 start += sizeof(pldm_entity);
1173 EXPECT_EQ(*start, pldm_entity_get_num_children(
1174 l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL));
1175 start += sizeof(uint8_t);
1176 entity = reinterpret_cast<pldm_entity*>(start);
1177 EXPECT_EQ(le16toh(entity->entity_type), 4u);
1178 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1179 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
1180 start += sizeof(pldm_entity);
1181 entity = reinterpret_cast<pldm_entity*>(start);
1182 EXPECT_EQ(le16toh(entity->entity_type), 5u);
1183 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1184 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301185
1186 currRecHandle = nextRecHandle;
1187
1188 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1189 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1190 l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL) *
1191 sizeof(pldm_entity)));
1192 start = data;
1193 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
1194 EXPECT_EQ(le32toh(hdr->record_handle), 5u);
1195 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
1196 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
1197 start += sizeof(pldm_pdr_hdr);
1198 containerId = reinterpret_cast<uint16_t*>(start);
1199 EXPECT_EQ(le16toh(*containerId), 3u);
1200 start += sizeof(uint16_t);
1201 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1202 start += sizeof(uint8_t);
1203 entity = reinterpret_cast<pldm_entity*>(start);
1204 EXPECT_EQ(le16toh(entity->entity_type), 4u);
1205 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1206 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
1207 start += sizeof(pldm_entity);
1208 EXPECT_EQ(*start, pldm_entity_get_num_children(
1209 l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL));
1210 start += sizeof(uint8_t);
1211 entity = reinterpret_cast<pldm_entity*>(start);
1212 EXPECT_EQ(le16toh(entity->entity_type), 6u);
1213 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1214 EXPECT_EQ(le16toh(entity->entity_container_id), 3u);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301215
1216 currRecHandle = nextRecHandle;
1217
1218 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1219 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1220 l3b, PLDM_ENTITY_ASSOCIAION_LOGICAL) *
1221 sizeof(pldm_entity)));
1222 start = data;
1223 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
1224 EXPECT_EQ(le32toh(hdr->record_handle), 6u);
1225 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
1226 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
1227 start += sizeof(pldm_pdr_hdr);
1228 containerId = reinterpret_cast<uint16_t*>(start);
1229 EXPECT_EQ(le16toh(*containerId), 4u);
1230 start += sizeof(uint16_t);
1231 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_LOGICAL);
1232 start += sizeof(uint8_t);
1233 entity = reinterpret_cast<pldm_entity*>(start);
1234 EXPECT_EQ(le16toh(entity->entity_type), 5u);
1235 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1236 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
1237 start += sizeof(pldm_entity);
1238 EXPECT_EQ(*start, pldm_entity_get_num_children(
1239 l3b, PLDM_ENTITY_ASSOCIAION_LOGICAL));
1240 start += sizeof(uint8_t);
1241 entity = reinterpret_cast<pldm_entity*>(start);
1242 EXPECT_EQ(le16toh(entity->entity_type), 7u);
1243 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1244 EXPECT_EQ(le16toh(entity->entity_container_id), 4u);
1245
1246 EXPECT_EQ(nextRecHandle, 0u);
1247
1248 pldm_pdr_destroy(repo);
1249 pldm_entity_association_tree_destroy(tree);
1250}
1251
1252TEST(EntityAssociationPDR, testFind)
1253{
1254 // 1
1255 // |
1256 // 2--3--4
1257 // |
1258 // 5--6--7
1259 // | |
1260 // 8 9
1261
1262 pldm_entity entities[9]{};
1263
1264 entities[0].entity_type = 1;
1265 entities[1].entity_type = 2;
1266 entities[2].entity_type = 2;
1267 entities[3].entity_type = 3;
1268 entities[4].entity_type = 4;
1269 entities[5].entity_type = 5;
1270 entities[6].entity_type = 5;
1271 entities[7].entity_type = 6;
1272 entities[8].entity_type = 7;
1273
1274 auto tree = pldm_entity_association_tree_init();
1275
1276 auto l1 = pldm_entity_association_tree_add(
1277 tree, &entities[0], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1278 EXPECT_NE(l1, nullptr);
1279 auto l2a = pldm_entity_association_tree_add(
1280 tree, &entities[1], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1281 EXPECT_NE(l2a, nullptr);
1282 auto l2b = pldm_entity_association_tree_add(
1283 tree, &entities[2], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1284 EXPECT_NE(l2b, nullptr);
1285 auto l2c = pldm_entity_association_tree_add(
1286 tree, &entities[3], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1287 EXPECT_NE(l2c, nullptr);
1288 auto l3a = pldm_entity_association_tree_add(
1289 tree, &entities[4], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1290 EXPECT_NE(l3a, nullptr);
1291 auto l3b = pldm_entity_association_tree_add(
1292 tree, &entities[5], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1293 EXPECT_NE(l3b, nullptr);
1294 auto l3c = pldm_entity_association_tree_add(
1295 tree, &entities[6], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1296 EXPECT_NE(l3c, nullptr);
1297 auto l4a = pldm_entity_association_tree_add(
1298 tree, &entities[7], 0xFFFF, l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1299 EXPECT_NE(l4a, nullptr);
1300 auto l4b = pldm_entity_association_tree_add(
1301 tree, &entities[8], 0xFFFF, l3b, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1302 EXPECT_NE(l4b, nullptr);
1303
1304 pldm_entity entity{};
1305
1306 entity.entity_type = 1;
1307 entity.entity_instance_num = 1;
1308 auto result = pldm_entity_association_tree_find(tree, &entity);
1309 EXPECT_EQ(result, l1);
1310 EXPECT_EQ(entity.entity_container_id, 0);
1311
1312 entity.entity_type = 2;
1313 entity.entity_instance_num = 1;
1314 result = pldm_entity_association_tree_find(tree, &entity);
1315 EXPECT_EQ(result, l2a);
1316 EXPECT_EQ(entity.entity_container_id, 1);
1317 entity.entity_type = 2;
1318 entity.entity_instance_num = 2;
1319 result = pldm_entity_association_tree_find(tree, &entity);
1320 EXPECT_EQ(result, l2b);
1321 EXPECT_EQ(entity.entity_container_id, 1);
1322 entity.entity_type = 3;
1323 entity.entity_instance_num = 1;
1324 result = pldm_entity_association_tree_find(tree, &entity);
1325 EXPECT_EQ(result, l2c);
1326 EXPECT_EQ(entity.entity_container_id, 1);
1327
1328 entity.entity_type = 7;
1329 entity.entity_instance_num = 1;
1330 result = pldm_entity_association_tree_find(tree, &entity);
1331 EXPECT_EQ(result, l4b);
1332 EXPECT_EQ(entity.entity_container_id, 4);
1333
1334 pldm_entity_association_tree_destroy(tree);
1335}
1336
1337TEST(EntityAssociationPDR, testCopyTree)
1338{
1339 pldm_entity entities[4]{};
1340 entities[0].entity_type = 1;
1341 entities[1].entity_type = 2;
1342 entities[2].entity_type = 2;
1343 entities[3].entity_type = 3;
1344
1345 auto orgTree = pldm_entity_association_tree_init();
1346 auto newTree = pldm_entity_association_tree_init();
1347 auto l1 =
1348 pldm_entity_association_tree_add(orgTree, &entities[0], 0xFFFF, nullptr,
1349 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1350 EXPECT_NE(l1, nullptr);
1351 auto l2a = pldm_entity_association_tree_add(
1352 orgTree, &entities[1], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1353 EXPECT_NE(l2a, nullptr);
1354 auto l2b = pldm_entity_association_tree_add(
1355 orgTree, &entities[2], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1356 EXPECT_NE(l2b, nullptr);
1357 auto l2c = pldm_entity_association_tree_add(
1358 orgTree, &entities[3], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1359 EXPECT_NE(l2c, nullptr);
1360 size_t orgNum{};
1361 pldm_entity* orgOut = nullptr;
1362 pldm_entity_association_tree_visit(orgTree, &orgOut, &orgNum);
1363 EXPECT_EQ(orgNum, 4u);
1364
1365 pldm_entity_association_tree_copy_root(orgTree, newTree);
1366 size_t newNum{};
1367 pldm_entity* newOut = nullptr;
1368 pldm_entity_association_tree_visit(newTree, &newOut, &newNum);
1369 EXPECT_EQ(newNum, orgNum);
1370 EXPECT_EQ(newOut[0].entity_type, 1u);
1371 EXPECT_EQ(newOut[0].entity_instance_num, 1u);
1372 EXPECT_EQ(newOut[0].entity_container_id, 0u);
1373 free(orgOut);
1374 free(newOut);
1375 pldm_entity_association_tree_destroy(orgTree);
1376 pldm_entity_association_tree_destroy(newTree);
1377}
1378
1379TEST(EntityAssociationPDR, testExtract)
1380{
1381 std::vector<uint8_t> pdr{};
1382 pdr.resize(sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_entity_association) +
1383 sizeof(pldm_entity) * 4);
1384 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(pdr.data());
1385 hdr->type = PLDM_PDR_ENTITY_ASSOCIATION;
1386 hdr->length =
1387 htole16(sizeof(pldm_pdr_entity_association) + sizeof(pldm_entity) * 4);
1388
1389 pldm_pdr_entity_association* e =
1390 reinterpret_cast<pldm_pdr_entity_association*>(pdr.data() +
1391 sizeof(pldm_pdr_hdr));
1392 e->container_id = htole16(1);
1393 e->num_children = 5;
1394 e->container.entity_type = htole16(1);
1395 e->container.entity_instance_num = htole16(1);
1396 e->container.entity_container_id = htole16(0);
1397
1398 pldm_entity* entity = e->children;
1399 entity->entity_type = htole16(2);
1400 entity->entity_instance_num = htole16(1);
1401 entity->entity_container_id = htole16(1);
1402 ++entity;
1403 entity->entity_type = htole16(3);
1404 entity->entity_instance_num = htole16(1);
1405 entity->entity_container_id = htole16(1);
1406 ++entity;
1407 entity->entity_type = htole16(4);
1408 entity->entity_instance_num = htole16(1);
1409 entity->entity_container_id = htole16(1);
1410 ++entity;
1411 entity->entity_type = htole16(5);
1412 entity->entity_instance_num = htole16(1);
1413 entity->entity_container_id = htole16(1);
1414 ++entity;
1415 entity->entity_type = htole16(6);
1416 entity->entity_instance_num = htole16(1);
1417 entity->entity_container_id = htole16(1);
1418
1419 size_t num{};
1420 pldm_entity* out = nullptr;
1421 pldm_entity_association_pdr_extract(pdr.data(), pdr.size(), &num, &out);
1422 EXPECT_EQ(num, (unsigned)e->num_children + 1);
1423 EXPECT_EQ(out[0].entity_type, 1u);
1424 EXPECT_EQ(out[0].entity_instance_num, 1u);
1425 EXPECT_EQ(out[0].entity_container_id, 0u);
1426 EXPECT_EQ(out[1].entity_type, 2u);
1427 EXPECT_EQ(out[1].entity_instance_num, 1u);
1428 EXPECT_EQ(out[1].entity_container_id, 1u);
1429 EXPECT_EQ(out[2].entity_type, 3u);
1430 EXPECT_EQ(out[2].entity_instance_num, 1u);
1431 EXPECT_EQ(out[2].entity_container_id, 1u);
1432 EXPECT_EQ(out[3].entity_type, 4u);
1433 EXPECT_EQ(out[3].entity_instance_num, 1u);
1434 EXPECT_EQ(out[3].entity_container_id, 1u);
1435 EXPECT_EQ(out[4].entity_type, 5u);
1436 EXPECT_EQ(out[4].entity_instance_num, 1u);
1437 EXPECT_EQ(out[4].entity_container_id, 1u);
1438 EXPECT_EQ(out[5].entity_type, 6u);
1439 EXPECT_EQ(out[5].entity_instance_num, 1u);
1440 EXPECT_EQ(out[5].entity_container_id, 1u);
1441
1442 free(out);
1443}
1444
1445TEST(EntityAssociationPDR, testGetChildren)
1446{
1447 pldm_entity entities[4]{};
1448 entities[0].entity_type = 1;
1449 entities[1].entity_type = 2;
1450 entities[2].entity_type = 2;
1451 entities[3].entity_type = 3;
1452
1453 auto tree = pldm_entity_association_tree_init();
1454 auto l1 = pldm_entity_association_tree_add(
1455 tree, &entities[0], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1456 EXPECT_NE(l1, nullptr);
1457 auto l2a = pldm_entity_association_tree_add(
1458 tree, &entities[1], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1459 EXPECT_NE(l2a, nullptr);
1460 auto l2b = pldm_entity_association_tree_add(
1461 tree, &entities[2], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1462 EXPECT_NE(l2b, nullptr);
1463 auto l2c = pldm_entity_association_tree_add(
1464 tree, &entities[3], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1465 EXPECT_NE(l2c, nullptr);
1466
1467 pldm_entity et1;
1468 et1.entity_type = 2;
1469 et1.entity_instance_num = 1;
1470 EXPECT_EQ(true, pldm_is_current_parent_child(l1, &et1));
1471
1472 pldm_entity et2;
1473 et2.entity_type = 2;
1474 et2.entity_instance_num = 2;
1475 EXPECT_EQ(true, pldm_is_current_parent_child(l1, &et2));
1476
1477 pldm_entity et3;
1478 et3.entity_type = 2;
1479 et3.entity_instance_num = 3;
1480 EXPECT_EQ(false, pldm_is_current_parent_child(l1, &et3));
1481
1482 pldm_entity_association_tree_destroy(tree);
1483}
1484
1485TEST(EntityAssociationPDR, testEntityInstanceNumber)
1486{
1487 pldm_entity entities[9]{};
1488
1489 entities[0].entity_type = 1;
1490 entities[1].entity_type = 2;
1491 entities[2].entity_type = 2;
1492 entities[3].entity_type = 2;
1493 entities[4].entity_type = 2;
1494 entities[5].entity_type = 2;
1495 entities[6].entity_type = 2;
1496 entities[7].entity_type = 3;
1497 entities[8].entity_type = 3;
1498
1499 auto tree = pldm_entity_association_tree_init();
1500 auto repo = pldm_pdr_init();
1501
1502 uint16_t terminusHdl{};
1503 uint16_t entityType{};
1504 uint16_t entityInstanceNum{};
1505 uint16_t containerId{};
1506
1507 auto node = pldm_entity_association_tree_add(
1508 tree, &entities[0], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1509 EXPECT_NE(node, nullptr);
1510
1511 auto l1 = pldm_entity_association_tree_add(tree, &entities[1], 63, node,
1512 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1513 auto first = pldm_pdr_add_fru_record_set(
1514 repo, 1, 1, entities[1].entity_type, entities[1].entity_instance_num,
1515 entities[1].entity_container_id, 1);
1516 EXPECT_NE(l1, nullptr);
1517 EXPECT_EQ(entities[1].entity_instance_num, 63);
1518 EXPECT_EQ(first, pldm_pdr_get_record_handle(
1519 repo, pldm_pdr_fru_record_set_find_by_rsi(
1520 repo, 1, &terminusHdl, &entityType,
1521 &entityInstanceNum, &containerId)));
1522 EXPECT_EQ(entityType, 2);
1523 EXPECT_EQ(entityInstanceNum, 63);
1524
1525 auto l2 = pldm_entity_association_tree_add(tree, &entities[2], 37, node,
1526 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1527 auto second = pldm_pdr_add_fru_record_set(
1528 repo, 1, 2, entities[2].entity_type, entities[2].entity_instance_num,
1529 entities[2].entity_container_id, 2);
1530 EXPECT_NE(l2, nullptr);
1531 EXPECT_EQ(entities[2].entity_instance_num, 37);
1532 EXPECT_EQ(second, pldm_pdr_get_record_handle(
1533 repo, pldm_pdr_fru_record_set_find_by_rsi(
1534 repo, 2, &terminusHdl, &entityType,
1535 &entityInstanceNum, &containerId)));
1536 EXPECT_EQ(entityType, 2);
1537 EXPECT_EQ(entityInstanceNum, 37);
1538
1539 auto l3 = pldm_entity_association_tree_add(tree, &entities[3], 44, node,
1540 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1541 auto third = pldm_pdr_add_fru_record_set(
1542 repo, 1, 3, entities[3].entity_type, entities[3].entity_instance_num,
1543 entities[3].entity_container_id, 3);
1544 EXPECT_NE(l3, nullptr);
1545 EXPECT_EQ(entities[3].entity_instance_num, 44);
1546 EXPECT_EQ(third, pldm_pdr_get_record_handle(
1547 repo, pldm_pdr_fru_record_set_find_by_rsi(
1548 repo, 3, &terminusHdl, &entityType,
1549 &entityInstanceNum, &containerId)));
1550 EXPECT_EQ(entityType, 2);
1551 EXPECT_EQ(entityInstanceNum, 44);
1552
1553 auto l4 = pldm_entity_association_tree_add(tree, &entities[4], 89, node,
1554 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1555 auto fourth = pldm_pdr_add_fru_record_set(
1556 repo, 1, 4, entities[4].entity_type, entities[4].entity_instance_num,
1557 entities[4].entity_container_id, 4);
1558 EXPECT_NE(l4, nullptr);
1559 EXPECT_EQ(entities[4].entity_instance_num, 89);
1560 EXPECT_EQ(fourth, pldm_pdr_get_record_handle(
1561 repo, pldm_pdr_fru_record_set_find_by_rsi(
1562 repo, 4, &terminusHdl, &entityType,
1563 &entityInstanceNum, &containerId)));
1564 EXPECT_EQ(entityType, 2);
1565 EXPECT_EQ(entityInstanceNum, 89);
1566
1567 auto l5 = pldm_entity_association_tree_add(tree, &entities[5], 0xFFFF, node,
1568 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1569 auto fifth = pldm_pdr_add_fru_record_set(
1570 repo, 1, 5, entities[5].entity_type, entities[5].entity_instance_num,
1571 entities[5].entity_container_id, 5);
1572 EXPECT_NE(l5, nullptr);
1573 EXPECT_EQ(entities[5].entity_instance_num, 90);
1574 EXPECT_EQ(fifth, pldm_pdr_get_record_handle(
1575 repo, pldm_pdr_fru_record_set_find_by_rsi(
1576 repo, 5, &terminusHdl, &entityType,
1577 &entityInstanceNum, &containerId)));
1578 EXPECT_EQ(entityType, 2);
1579 EXPECT_EQ(entityInstanceNum, 90);
1580
1581 auto l6 = pldm_entity_association_tree_add(tree, &entities[6], 90, node,
1582 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1583 EXPECT_EQ(l6, nullptr);
1584
1585 auto l7 = pldm_entity_association_tree_add(tree, &entities[7], 100, l1,
1586 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1587 auto seventh = pldm_pdr_add_fru_record_set(
1588 repo, 1, 7, entities[7].entity_type, entities[7].entity_instance_num,
1589 entities[7].entity_container_id, 7);
1590 EXPECT_NE(l7, nullptr);
1591 EXPECT_EQ(entities[7].entity_instance_num, 100);
1592 EXPECT_EQ(seventh, pldm_pdr_get_record_handle(
1593 repo, pldm_pdr_fru_record_set_find_by_rsi(
1594 repo, 7, &terminusHdl, &entityType,
1595 &entityInstanceNum, &containerId)));
1596 EXPECT_EQ(entityType, 3);
1597 EXPECT_EQ(entityInstanceNum, 100);
1598
1599 auto l8 = pldm_entity_association_tree_add(tree, &entities[8], 100, l2,
1600 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1601 auto eighth = pldm_pdr_add_fru_record_set(
1602 repo, 1, 8, entities[8].entity_type, entities[8].entity_instance_num,
1603 entities[8].entity_container_id, 8);
1604 EXPECT_NE(l8, nullptr);
1605 EXPECT_EQ(entities[8].entity_instance_num, 100);
1606 EXPECT_EQ(eighth, pldm_pdr_get_record_handle(
1607 repo, pldm_pdr_fru_record_set_find_by_rsi(
1608 repo, 8, &terminusHdl, &entityType,
1609 &entityInstanceNum, &containerId)));
1610 EXPECT_EQ(entityType, 3);
1611 EXPECT_EQ(entityInstanceNum, 100);
1612
1613 pldm_pdr_destroy(repo);
1614 pldm_entity_association_tree_destroy(tree);
1615}