blob: 6b99d5e3b918ec1c46e34e7348210a5b6ad90e35 [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
751TEST(EntityAssociationPDR, testSpecialTrees)
752{
753 pldm_entity entities[3]{};
754
755 entities[0].entity_type = 1;
756 entities[1].entity_type = 2;
757 entities[2].entity_type = 1;
758
759 // A
760 auto tree = pldm_entity_association_tree_init();
761 auto node = pldm_entity_association_tree_add(
762 tree, &entities[0], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
763 EXPECT_NE(node, nullptr);
764 size_t num{};
765 pldm_entity* out = nullptr;
766 pldm_entity_association_tree_visit(tree, &out, &num);
767 EXPECT_EQ(num, 1u);
768 EXPECT_EQ(out[0].entity_type, 1u);
769 EXPECT_EQ(out[0].entity_instance_num, 1u);
770 EXPECT_EQ(out[0].entity_container_id, 0u);
771 free(out);
772 pldm_entity_association_tree_destroy(tree);
773
774 // A-A-A
775 tree = pldm_entity_association_tree_init();
776 node = pldm_entity_association_tree_add(tree, &entities[0], 0xFFFF, nullptr,
777 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
778 EXPECT_NE(node, nullptr);
779 node = pldm_entity_association_tree_add(tree, &entities[1], 0xFFFF, nullptr,
780 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
781 EXPECT_NE(node, nullptr);
782 node = pldm_entity_association_tree_add(tree, &entities[2], 0xFFFF, nullptr,
783 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
784 EXPECT_NE(node, nullptr);
785 pldm_entity_association_tree_visit(tree, &out, &num);
786 EXPECT_EQ(num, 3u);
787 EXPECT_EQ(out[0].entity_type, 1u);
788 EXPECT_EQ(out[0].entity_instance_num, 1u);
789 EXPECT_EQ(out[0].entity_container_id, 0u);
790 EXPECT_EQ(out[1].entity_type, 1u);
791 EXPECT_EQ(out[1].entity_instance_num, 2u);
792 EXPECT_EQ(out[1].entity_container_id, 0u);
793 EXPECT_EQ(out[2].entity_type, 2u);
794 EXPECT_EQ(out[2].entity_instance_num, 1u);
795 EXPECT_EQ(out[2].entity_container_id, 0u);
796 free(out);
797 pldm_entity_association_tree_destroy(tree);
798
799 // A
800 // |
801 // A
802 // |
803 // A
804 tree = pldm_entity_association_tree_init();
805 node = pldm_entity_association_tree_add(tree, &entities[0], 0xFFFF, nullptr,
806 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
807 EXPECT_NE(node, nullptr);
808 auto node1 = pldm_entity_association_tree_add(
809 tree, &entities[1], 0xFFFF, node, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
810 EXPECT_NE(node1, nullptr);
811 auto node2 = pldm_entity_association_tree_add(
812 tree, &entities[2], 0xFFFF, node1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
813 EXPECT_NE(node2, nullptr);
814 pldm_entity_association_tree_visit(tree, &out, &num);
815 EXPECT_EQ(num, 3u);
816 EXPECT_EQ(out[0].entity_type, 1u);
817 EXPECT_EQ(out[0].entity_instance_num, 1u);
818 EXPECT_EQ(out[0].entity_container_id, 0u);
819 EXPECT_EQ(out[1].entity_type, 2u);
820 EXPECT_EQ(out[1].entity_instance_num, 1u);
821 EXPECT_EQ(out[1].entity_container_id, 1u);
822 EXPECT_EQ(out[2].entity_type, 1u);
823 EXPECT_EQ(out[2].entity_instance_num, 1u);
824 EXPECT_EQ(out[2].entity_container_id, 2u);
825 free(out);
826 pldm_entity_association_tree_destroy(tree);
827
828 // A-A
829 // |
830 // A-A
831 tree = pldm_entity_association_tree_init();
832 node = pldm_entity_association_tree_add(tree, &entities[0], 0xFFFF, nullptr,
833 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
834 EXPECT_NE(node, nullptr);
835 node = pldm_entity_association_tree_add(tree, &entities[0], 0xFFFF, nullptr,
836 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
837 EXPECT_NE(node, nullptr);
838 node1 = pldm_entity_association_tree_add(tree, &entities[1], 0xFFFF, node,
839 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
840 EXPECT_NE(node1, nullptr);
841 node2 = pldm_entity_association_tree_add(tree, &entities[2], 0xFFFF, node,
842 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
843 EXPECT_NE(node2, nullptr);
844 pldm_entity_association_tree_visit(tree, &out, &num);
845 EXPECT_EQ(num, 4u);
846 EXPECT_EQ(out[0].entity_type, 1u);
847 EXPECT_EQ(out[0].entity_instance_num, 1u);
848 EXPECT_EQ(out[0].entity_container_id, 0u);
849 EXPECT_EQ(out[1].entity_type, 1u);
850 EXPECT_EQ(out[1].entity_instance_num, 2u);
851 EXPECT_EQ(out[1].entity_container_id, 0u);
852 EXPECT_EQ(out[2].entity_type, 2u);
853 EXPECT_EQ(out[2].entity_instance_num, 1u);
854 EXPECT_EQ(out[2].entity_container_id, 1u);
855 EXPECT_EQ(out[3].entity_type, 1u);
856 EXPECT_EQ(out[3].entity_instance_num, 1u);
857 EXPECT_EQ(out[3].entity_container_id, 1u);
858 free(out);
859 pldm_entity_association_tree_destroy(tree);
860}
861
862TEST(EntityAssociationPDR, testPDR)
863{
864 // e = entity type, c = container id, i = instance num
865
866 // INPUT
867 // 1(e=1)--1a(e=2)
868 // |
869 // 2(e=2)--3(e=2)--4(e=2)--5(e=3)
870 // |
871 // 6(e=4)--7(e=5)--8(e=5)--9(e=5)
872 // | |
873 // 11(e=6) 10(e=7)
874
875 // Expected OUTPUT
876 // 1(e=1,c=0,i=1)
877 // |
878 // 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)
879 // |
880 // 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)
881 // | |
882 // 10(e=6,c=3,i=1) 11(e=7,c=4,i=1)
883 pldm_entity entities[11]{};
884
885 entities[0].entity_type = 1;
886 entities[1].entity_type = 2;
887 entities[2].entity_type = 3;
888 entities[3].entity_type = 2;
889 entities[4].entity_type = 3;
890 entities[5].entity_type = 4;
891 entities[6].entity_type = 5;
892 entities[7].entity_type = 5;
893 entities[8].entity_type = 5;
894 entities[9].entity_type = 6;
895 entities[10].entity_type = 7;
896
897 auto tree = pldm_entity_association_tree_init();
898
899 auto l1 = pldm_entity_association_tree_add(
900 tree, &entities[0], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
901 EXPECT_NE(l1, nullptr);
902 auto l1a = pldm_entity_association_tree_add(
903 tree, &entities[1], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
904 EXPECT_NE(l1a, nullptr);
905
906 auto l2a = pldm_entity_association_tree_add(
907 tree, &entities[1], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
908 EXPECT_NE(l2a, nullptr);
909 auto l2b = pldm_entity_association_tree_add(tree, &entities[2], 0xFFFF, l1,
910 PLDM_ENTITY_ASSOCIAION_LOGICAL);
911 EXPECT_NE(l2b, nullptr);
912 auto l2c = pldm_entity_association_tree_add(
913 tree, &entities[3], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
914 EXPECT_NE(l2c, nullptr);
915 auto l2d = pldm_entity_association_tree_add(tree, &entities[4], 0xFFFF, l1,
916 PLDM_ENTITY_ASSOCIAION_LOGICAL);
917 EXPECT_NE(l2d, nullptr);
918
919 auto l3a = pldm_entity_association_tree_add(
920 tree, &entities[5], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
921 EXPECT_NE(l3a, nullptr);
922 auto l3b = pldm_entity_association_tree_add(
923 tree, &entities[6], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
924 EXPECT_NE(l3b, nullptr);
925 auto l3c = pldm_entity_association_tree_add(tree, &entities[7], 0xFFFF, l2a,
926 PLDM_ENTITY_ASSOCIAION_LOGICAL);
927 EXPECT_NE(l3c, nullptr);
928 auto l3d = pldm_entity_association_tree_add(tree, &entities[8], 0xFFFF, l2a,
929 PLDM_ENTITY_ASSOCIAION_LOGICAL);
930 EXPECT_NE(l3d, nullptr);
931
932 auto l4a = pldm_entity_association_tree_add(
933 tree, &entities[9], 0xFFFF, l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
934 EXPECT_NE(l4a, nullptr);
935 auto l4b = pldm_entity_association_tree_add(
936 tree, &entities[10], 0xFFFF, l3b, PLDM_ENTITY_ASSOCIAION_LOGICAL);
937 EXPECT_NE(l4b, nullptr);
938
939 EXPECT_EQ(pldm_entity_get_num_children(l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL),
940 2);
941 EXPECT_EQ(pldm_entity_get_num_children(l1, PLDM_ENTITY_ASSOCIAION_LOGICAL),
942 2);
943 EXPECT_EQ(
944 pldm_entity_get_num_children(l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL), 2);
945 EXPECT_EQ(
946 pldm_entity_get_num_children(l3b, PLDM_ENTITY_ASSOCIAION_PHYSICAL), 0);
947 EXPECT_EQ(pldm_entity_get_num_children(l3b, PLDM_ENTITY_ASSOCIAION_LOGICAL),
948 1);
949
950 auto repo = pldm_pdr_init();
951 pldm_entity_association_pdr_add(tree, repo, false, 1);
952
953 EXPECT_EQ(pldm_pdr_get_record_count(repo), 6u);
954
955 uint32_t currRecHandle{};
956 uint32_t nextRecHandle{};
957 uint8_t* data = nullptr;
958 uint32_t size{};
959 uint32_t commonSize = sizeof(struct pldm_pdr_hdr) + sizeof(uint16_t) +
960 sizeof(uint8_t) + sizeof(pldm_entity) +
961 sizeof(uint8_t);
962
963 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
964 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
965 l1, PLDM_ENTITY_ASSOCIAION_LOGICAL) *
966 sizeof(pldm_entity)));
967 uint8_t* start = data;
968 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
969 EXPECT_EQ(le32toh(hdr->record_handle), 1u);
970 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
971 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
972 start += sizeof(pldm_pdr_hdr);
973 uint16_t* containerId = reinterpret_cast<uint16_t*>(start);
974 EXPECT_EQ(le16toh(*containerId), 1u);
975 start += sizeof(uint16_t);
976 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_LOGICAL);
977 start += sizeof(uint8_t);
978 pldm_entity* entity = reinterpret_cast<pldm_entity*>(start);
979 EXPECT_EQ(le16toh(entity->entity_type), 1u);
980 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
981 EXPECT_EQ(le16toh(entity->entity_container_id), 0u);
982 start += sizeof(pldm_entity);
983 EXPECT_EQ(*start,
984 pldm_entity_get_num_children(l1, PLDM_ENTITY_ASSOCIAION_LOGICAL));
985 start += sizeof(uint8_t);
986 entity = reinterpret_cast<pldm_entity*>(start);
987 EXPECT_EQ(le16toh(entity->entity_type), 3u);
988 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
989 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
990 start += sizeof(pldm_entity);
991 entity = reinterpret_cast<pldm_entity*>(start);
992 EXPECT_EQ(le16toh(entity->entity_type), 3u);
993 EXPECT_EQ(le16toh(entity->entity_instance_num), 2u);
994 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930995
996 currRecHandle = nextRecHandle;
997
998 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
999 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1000 l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL) *
1001 sizeof(pldm_entity)));
1002 start = data;
1003 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
1004 EXPECT_EQ(le32toh(hdr->record_handle), 2u);
1005 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
1006 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
1007 start += sizeof(pldm_pdr_hdr);
1008 containerId = reinterpret_cast<uint16_t*>(start);
1009 EXPECT_EQ(le16toh(*containerId), 1u);
1010 start += sizeof(uint16_t);
1011 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1012 start += sizeof(uint8_t);
1013 entity = reinterpret_cast<pldm_entity*>(start);
1014 EXPECT_EQ(le16toh(entity->entity_type), 1u);
1015 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1016 EXPECT_EQ(le16toh(entity->entity_container_id), 0u);
1017 start += sizeof(pldm_entity);
1018 EXPECT_EQ(*start, pldm_entity_get_num_children(
1019 l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL));
1020 start += sizeof(uint8_t);
1021 entity = reinterpret_cast<pldm_entity*>(start);
1022 EXPECT_EQ(le16toh(entity->entity_type), 2u);
1023 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1024 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
1025 start += sizeof(pldm_entity);
1026 entity = reinterpret_cast<pldm_entity*>(start);
1027 EXPECT_EQ(le16toh(entity->entity_type), 2u);
1028 EXPECT_EQ(le16toh(entity->entity_instance_num), 2u);
1029 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301030
1031 currRecHandle = nextRecHandle;
1032
1033 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1034 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1035 l2a, PLDM_ENTITY_ASSOCIAION_LOGICAL) *
1036 sizeof(pldm_entity)));
1037 start = data;
1038 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
1039 EXPECT_EQ(le32toh(hdr->record_handle), 3u);
1040 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
1041 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
1042 start += sizeof(pldm_pdr_hdr);
1043 containerId = reinterpret_cast<uint16_t*>(start);
1044 EXPECT_EQ(le16toh(*containerId), 2u);
1045 start += sizeof(uint16_t);
1046 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_LOGICAL);
1047 start += sizeof(uint8_t);
1048 entity = reinterpret_cast<pldm_entity*>(start);
1049 EXPECT_EQ(le16toh(entity->entity_type), 2u);
1050 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1051 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
1052 start += sizeof(pldm_entity);
1053 EXPECT_EQ(*start, pldm_entity_get_num_children(
1054 l2a, PLDM_ENTITY_ASSOCIAION_LOGICAL));
1055 start += sizeof(uint8_t);
1056 entity = reinterpret_cast<pldm_entity*>(start);
1057 EXPECT_EQ(le16toh(entity->entity_type), 5);
1058 EXPECT_EQ(le16toh(entity->entity_instance_num), 2u);
1059 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
1060 start += sizeof(pldm_entity);
1061 entity = reinterpret_cast<pldm_entity*>(start);
1062 EXPECT_EQ(le16toh(entity->entity_type), 5u);
1063 EXPECT_EQ(le16toh(entity->entity_instance_num), 3u);
1064 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301065
1066 currRecHandle = nextRecHandle;
1067
1068 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1069 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1070 l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL) *
1071 sizeof(pldm_entity)));
1072 start = data;
1073 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
1074 EXPECT_EQ(le32toh(hdr->record_handle), 4u);
1075 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
1076 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
1077 start += sizeof(pldm_pdr_hdr);
1078 containerId = reinterpret_cast<uint16_t*>(start);
1079 EXPECT_EQ(le16toh(*containerId), 2u);
1080 start += sizeof(uint16_t);
1081 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1082 start += sizeof(uint8_t);
1083 entity = reinterpret_cast<pldm_entity*>(start);
1084 EXPECT_EQ(le16toh(entity->entity_type), 2u);
1085 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1086 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
1087 start += sizeof(pldm_entity);
1088 EXPECT_EQ(*start, pldm_entity_get_num_children(
1089 l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL));
1090 start += sizeof(uint8_t);
1091 entity = reinterpret_cast<pldm_entity*>(start);
1092 EXPECT_EQ(le16toh(entity->entity_type), 4u);
1093 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1094 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
1095 start += sizeof(pldm_entity);
1096 entity = reinterpret_cast<pldm_entity*>(start);
1097 EXPECT_EQ(le16toh(entity->entity_type), 5u);
1098 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1099 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301100
1101 currRecHandle = nextRecHandle;
1102
1103 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1104 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1105 l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL) *
1106 sizeof(pldm_entity)));
1107 start = data;
1108 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
1109 EXPECT_EQ(le32toh(hdr->record_handle), 5u);
1110 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
1111 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
1112 start += sizeof(pldm_pdr_hdr);
1113 containerId = reinterpret_cast<uint16_t*>(start);
1114 EXPECT_EQ(le16toh(*containerId), 3u);
1115 start += sizeof(uint16_t);
1116 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1117 start += sizeof(uint8_t);
1118 entity = reinterpret_cast<pldm_entity*>(start);
1119 EXPECT_EQ(le16toh(entity->entity_type), 4u);
1120 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1121 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
1122 start += sizeof(pldm_entity);
1123 EXPECT_EQ(*start, pldm_entity_get_num_children(
1124 l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL));
1125 start += sizeof(uint8_t);
1126 entity = reinterpret_cast<pldm_entity*>(start);
1127 EXPECT_EQ(le16toh(entity->entity_type), 6u);
1128 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1129 EXPECT_EQ(le16toh(entity->entity_container_id), 3u);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301130
1131 currRecHandle = nextRecHandle;
1132
1133 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1134 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1135 l3b, PLDM_ENTITY_ASSOCIAION_LOGICAL) *
1136 sizeof(pldm_entity)));
1137 start = data;
1138 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
1139 EXPECT_EQ(le32toh(hdr->record_handle), 6u);
1140 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
1141 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
1142 start += sizeof(pldm_pdr_hdr);
1143 containerId = reinterpret_cast<uint16_t*>(start);
1144 EXPECT_EQ(le16toh(*containerId), 4u);
1145 start += sizeof(uint16_t);
1146 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_LOGICAL);
1147 start += sizeof(uint8_t);
1148 entity = reinterpret_cast<pldm_entity*>(start);
1149 EXPECT_EQ(le16toh(entity->entity_type), 5u);
1150 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1151 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
1152 start += sizeof(pldm_entity);
1153 EXPECT_EQ(*start, pldm_entity_get_num_children(
1154 l3b, PLDM_ENTITY_ASSOCIAION_LOGICAL));
1155 start += sizeof(uint8_t);
1156 entity = reinterpret_cast<pldm_entity*>(start);
1157 EXPECT_EQ(le16toh(entity->entity_type), 7u);
1158 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1159 EXPECT_EQ(le16toh(entity->entity_container_id), 4u);
1160
1161 EXPECT_EQ(nextRecHandle, 0u);
1162
1163 pldm_pdr_destroy(repo);
1164 pldm_entity_association_tree_destroy(tree);
1165}
1166
1167TEST(EntityAssociationPDR, testFind)
1168{
1169 // 1
1170 // |
1171 // 2--3--4
1172 // |
1173 // 5--6--7
1174 // | |
1175 // 8 9
1176
1177 pldm_entity entities[9]{};
1178
1179 entities[0].entity_type = 1;
1180 entities[1].entity_type = 2;
1181 entities[2].entity_type = 2;
1182 entities[3].entity_type = 3;
1183 entities[4].entity_type = 4;
1184 entities[5].entity_type = 5;
1185 entities[6].entity_type = 5;
1186 entities[7].entity_type = 6;
1187 entities[8].entity_type = 7;
1188
1189 auto tree = pldm_entity_association_tree_init();
1190
1191 auto l1 = pldm_entity_association_tree_add(
1192 tree, &entities[0], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1193 EXPECT_NE(l1, nullptr);
1194 auto l2a = pldm_entity_association_tree_add(
1195 tree, &entities[1], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1196 EXPECT_NE(l2a, nullptr);
1197 auto l2b = pldm_entity_association_tree_add(
1198 tree, &entities[2], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1199 EXPECT_NE(l2b, nullptr);
1200 auto l2c = pldm_entity_association_tree_add(
1201 tree, &entities[3], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1202 EXPECT_NE(l2c, nullptr);
1203 auto l3a = pldm_entity_association_tree_add(
1204 tree, &entities[4], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1205 EXPECT_NE(l3a, nullptr);
1206 auto l3b = pldm_entity_association_tree_add(
1207 tree, &entities[5], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1208 EXPECT_NE(l3b, nullptr);
1209 auto l3c = pldm_entity_association_tree_add(
1210 tree, &entities[6], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1211 EXPECT_NE(l3c, nullptr);
1212 auto l4a = pldm_entity_association_tree_add(
1213 tree, &entities[7], 0xFFFF, l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1214 EXPECT_NE(l4a, nullptr);
1215 auto l4b = pldm_entity_association_tree_add(
1216 tree, &entities[8], 0xFFFF, l3b, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1217 EXPECT_NE(l4b, nullptr);
1218
1219 pldm_entity entity{};
1220
1221 entity.entity_type = 1;
1222 entity.entity_instance_num = 1;
1223 auto result = pldm_entity_association_tree_find(tree, &entity);
1224 EXPECT_EQ(result, l1);
1225 EXPECT_EQ(entity.entity_container_id, 0);
1226
1227 entity.entity_type = 2;
1228 entity.entity_instance_num = 1;
1229 result = pldm_entity_association_tree_find(tree, &entity);
1230 EXPECT_EQ(result, l2a);
1231 EXPECT_EQ(entity.entity_container_id, 1);
1232 entity.entity_type = 2;
1233 entity.entity_instance_num = 2;
1234 result = pldm_entity_association_tree_find(tree, &entity);
1235 EXPECT_EQ(result, l2b);
1236 EXPECT_EQ(entity.entity_container_id, 1);
1237 entity.entity_type = 3;
1238 entity.entity_instance_num = 1;
1239 result = pldm_entity_association_tree_find(tree, &entity);
1240 EXPECT_EQ(result, l2c);
1241 EXPECT_EQ(entity.entity_container_id, 1);
1242
1243 entity.entity_type = 7;
1244 entity.entity_instance_num = 1;
1245 result = pldm_entity_association_tree_find(tree, &entity);
1246 EXPECT_EQ(result, l4b);
1247 EXPECT_EQ(entity.entity_container_id, 4);
1248
1249 pldm_entity_association_tree_destroy(tree);
1250}
1251
1252TEST(EntityAssociationPDR, testCopyTree)
1253{
1254 pldm_entity entities[4]{};
1255 entities[0].entity_type = 1;
1256 entities[1].entity_type = 2;
1257 entities[2].entity_type = 2;
1258 entities[3].entity_type = 3;
1259
1260 auto orgTree = pldm_entity_association_tree_init();
1261 auto newTree = pldm_entity_association_tree_init();
1262 auto l1 =
1263 pldm_entity_association_tree_add(orgTree, &entities[0], 0xFFFF, nullptr,
1264 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1265 EXPECT_NE(l1, nullptr);
1266 auto l2a = pldm_entity_association_tree_add(
1267 orgTree, &entities[1], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1268 EXPECT_NE(l2a, nullptr);
1269 auto l2b = pldm_entity_association_tree_add(
1270 orgTree, &entities[2], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1271 EXPECT_NE(l2b, nullptr);
1272 auto l2c = pldm_entity_association_tree_add(
1273 orgTree, &entities[3], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1274 EXPECT_NE(l2c, nullptr);
1275 size_t orgNum{};
1276 pldm_entity* orgOut = nullptr;
1277 pldm_entity_association_tree_visit(orgTree, &orgOut, &orgNum);
1278 EXPECT_EQ(orgNum, 4u);
1279
1280 pldm_entity_association_tree_copy_root(orgTree, newTree);
1281 size_t newNum{};
1282 pldm_entity* newOut = nullptr;
1283 pldm_entity_association_tree_visit(newTree, &newOut, &newNum);
1284 EXPECT_EQ(newNum, orgNum);
1285 EXPECT_EQ(newOut[0].entity_type, 1u);
1286 EXPECT_EQ(newOut[0].entity_instance_num, 1u);
1287 EXPECT_EQ(newOut[0].entity_container_id, 0u);
1288 free(orgOut);
1289 free(newOut);
1290 pldm_entity_association_tree_destroy(orgTree);
1291 pldm_entity_association_tree_destroy(newTree);
1292}
1293
1294TEST(EntityAssociationPDR, testExtract)
1295{
1296 std::vector<uint8_t> pdr{};
1297 pdr.resize(sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_entity_association) +
1298 sizeof(pldm_entity) * 4);
1299 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(pdr.data());
1300 hdr->type = PLDM_PDR_ENTITY_ASSOCIATION;
1301 hdr->length =
1302 htole16(sizeof(pldm_pdr_entity_association) + sizeof(pldm_entity) * 4);
1303
1304 pldm_pdr_entity_association* e =
1305 reinterpret_cast<pldm_pdr_entity_association*>(pdr.data() +
1306 sizeof(pldm_pdr_hdr));
1307 e->container_id = htole16(1);
1308 e->num_children = 5;
1309 e->container.entity_type = htole16(1);
1310 e->container.entity_instance_num = htole16(1);
1311 e->container.entity_container_id = htole16(0);
1312
1313 pldm_entity* entity = e->children;
1314 entity->entity_type = htole16(2);
1315 entity->entity_instance_num = htole16(1);
1316 entity->entity_container_id = htole16(1);
1317 ++entity;
1318 entity->entity_type = htole16(3);
1319 entity->entity_instance_num = htole16(1);
1320 entity->entity_container_id = htole16(1);
1321 ++entity;
1322 entity->entity_type = htole16(4);
1323 entity->entity_instance_num = htole16(1);
1324 entity->entity_container_id = htole16(1);
1325 ++entity;
1326 entity->entity_type = htole16(5);
1327 entity->entity_instance_num = htole16(1);
1328 entity->entity_container_id = htole16(1);
1329 ++entity;
1330 entity->entity_type = htole16(6);
1331 entity->entity_instance_num = htole16(1);
1332 entity->entity_container_id = htole16(1);
1333
1334 size_t num{};
1335 pldm_entity* out = nullptr;
1336 pldm_entity_association_pdr_extract(pdr.data(), pdr.size(), &num, &out);
1337 EXPECT_EQ(num, (unsigned)e->num_children + 1);
1338 EXPECT_EQ(out[0].entity_type, 1u);
1339 EXPECT_EQ(out[0].entity_instance_num, 1u);
1340 EXPECT_EQ(out[0].entity_container_id, 0u);
1341 EXPECT_EQ(out[1].entity_type, 2u);
1342 EXPECT_EQ(out[1].entity_instance_num, 1u);
1343 EXPECT_EQ(out[1].entity_container_id, 1u);
1344 EXPECT_EQ(out[2].entity_type, 3u);
1345 EXPECT_EQ(out[2].entity_instance_num, 1u);
1346 EXPECT_EQ(out[2].entity_container_id, 1u);
1347 EXPECT_EQ(out[3].entity_type, 4u);
1348 EXPECT_EQ(out[3].entity_instance_num, 1u);
1349 EXPECT_EQ(out[3].entity_container_id, 1u);
1350 EXPECT_EQ(out[4].entity_type, 5u);
1351 EXPECT_EQ(out[4].entity_instance_num, 1u);
1352 EXPECT_EQ(out[4].entity_container_id, 1u);
1353 EXPECT_EQ(out[5].entity_type, 6u);
1354 EXPECT_EQ(out[5].entity_instance_num, 1u);
1355 EXPECT_EQ(out[5].entity_container_id, 1u);
1356
1357 free(out);
1358}
1359
1360TEST(EntityAssociationPDR, testGetChildren)
1361{
1362 pldm_entity entities[4]{};
1363 entities[0].entity_type = 1;
1364 entities[1].entity_type = 2;
1365 entities[2].entity_type = 2;
1366 entities[3].entity_type = 3;
1367
1368 auto tree = pldm_entity_association_tree_init();
1369 auto l1 = pldm_entity_association_tree_add(
1370 tree, &entities[0], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1371 EXPECT_NE(l1, nullptr);
1372 auto l2a = pldm_entity_association_tree_add(
1373 tree, &entities[1], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1374 EXPECT_NE(l2a, nullptr);
1375 auto l2b = pldm_entity_association_tree_add(
1376 tree, &entities[2], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1377 EXPECT_NE(l2b, nullptr);
1378 auto l2c = pldm_entity_association_tree_add(
1379 tree, &entities[3], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1380 EXPECT_NE(l2c, nullptr);
1381
1382 pldm_entity et1;
1383 et1.entity_type = 2;
1384 et1.entity_instance_num = 1;
1385 EXPECT_EQ(true, pldm_is_current_parent_child(l1, &et1));
1386
1387 pldm_entity et2;
1388 et2.entity_type = 2;
1389 et2.entity_instance_num = 2;
1390 EXPECT_EQ(true, pldm_is_current_parent_child(l1, &et2));
1391
1392 pldm_entity et3;
1393 et3.entity_type = 2;
1394 et3.entity_instance_num = 3;
1395 EXPECT_EQ(false, pldm_is_current_parent_child(l1, &et3));
1396
1397 pldm_entity_association_tree_destroy(tree);
1398}
1399
1400TEST(EntityAssociationPDR, testEntityInstanceNumber)
1401{
1402 pldm_entity entities[9]{};
1403
1404 entities[0].entity_type = 1;
1405 entities[1].entity_type = 2;
1406 entities[2].entity_type = 2;
1407 entities[3].entity_type = 2;
1408 entities[4].entity_type = 2;
1409 entities[5].entity_type = 2;
1410 entities[6].entity_type = 2;
1411 entities[7].entity_type = 3;
1412 entities[8].entity_type = 3;
1413
1414 auto tree = pldm_entity_association_tree_init();
1415 auto repo = pldm_pdr_init();
1416
1417 uint16_t terminusHdl{};
1418 uint16_t entityType{};
1419 uint16_t entityInstanceNum{};
1420 uint16_t containerId{};
1421
1422 auto node = pldm_entity_association_tree_add(
1423 tree, &entities[0], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1424 EXPECT_NE(node, nullptr);
1425
1426 auto l1 = pldm_entity_association_tree_add(tree, &entities[1], 63, node,
1427 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1428 auto first = pldm_pdr_add_fru_record_set(
1429 repo, 1, 1, entities[1].entity_type, entities[1].entity_instance_num,
1430 entities[1].entity_container_id, 1);
1431 EXPECT_NE(l1, nullptr);
1432 EXPECT_EQ(entities[1].entity_instance_num, 63);
1433 EXPECT_EQ(first, pldm_pdr_get_record_handle(
1434 repo, pldm_pdr_fru_record_set_find_by_rsi(
1435 repo, 1, &terminusHdl, &entityType,
1436 &entityInstanceNum, &containerId)));
1437 EXPECT_EQ(entityType, 2);
1438 EXPECT_EQ(entityInstanceNum, 63);
1439
1440 auto l2 = pldm_entity_association_tree_add(tree, &entities[2], 37, node,
1441 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1442 auto second = pldm_pdr_add_fru_record_set(
1443 repo, 1, 2, entities[2].entity_type, entities[2].entity_instance_num,
1444 entities[2].entity_container_id, 2);
1445 EXPECT_NE(l2, nullptr);
1446 EXPECT_EQ(entities[2].entity_instance_num, 37);
1447 EXPECT_EQ(second, pldm_pdr_get_record_handle(
1448 repo, pldm_pdr_fru_record_set_find_by_rsi(
1449 repo, 2, &terminusHdl, &entityType,
1450 &entityInstanceNum, &containerId)));
1451 EXPECT_EQ(entityType, 2);
1452 EXPECT_EQ(entityInstanceNum, 37);
1453
1454 auto l3 = pldm_entity_association_tree_add(tree, &entities[3], 44, node,
1455 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1456 auto third = pldm_pdr_add_fru_record_set(
1457 repo, 1, 3, entities[3].entity_type, entities[3].entity_instance_num,
1458 entities[3].entity_container_id, 3);
1459 EXPECT_NE(l3, nullptr);
1460 EXPECT_EQ(entities[3].entity_instance_num, 44);
1461 EXPECT_EQ(third, pldm_pdr_get_record_handle(
1462 repo, pldm_pdr_fru_record_set_find_by_rsi(
1463 repo, 3, &terminusHdl, &entityType,
1464 &entityInstanceNum, &containerId)));
1465 EXPECT_EQ(entityType, 2);
1466 EXPECT_EQ(entityInstanceNum, 44);
1467
1468 auto l4 = pldm_entity_association_tree_add(tree, &entities[4], 89, node,
1469 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1470 auto fourth = pldm_pdr_add_fru_record_set(
1471 repo, 1, 4, entities[4].entity_type, entities[4].entity_instance_num,
1472 entities[4].entity_container_id, 4);
1473 EXPECT_NE(l4, nullptr);
1474 EXPECT_EQ(entities[4].entity_instance_num, 89);
1475 EXPECT_EQ(fourth, pldm_pdr_get_record_handle(
1476 repo, pldm_pdr_fru_record_set_find_by_rsi(
1477 repo, 4, &terminusHdl, &entityType,
1478 &entityInstanceNum, &containerId)));
1479 EXPECT_EQ(entityType, 2);
1480 EXPECT_EQ(entityInstanceNum, 89);
1481
1482 auto l5 = pldm_entity_association_tree_add(tree, &entities[5], 0xFFFF, node,
1483 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1484 auto fifth = pldm_pdr_add_fru_record_set(
1485 repo, 1, 5, entities[5].entity_type, entities[5].entity_instance_num,
1486 entities[5].entity_container_id, 5);
1487 EXPECT_NE(l5, nullptr);
1488 EXPECT_EQ(entities[5].entity_instance_num, 90);
1489 EXPECT_EQ(fifth, pldm_pdr_get_record_handle(
1490 repo, pldm_pdr_fru_record_set_find_by_rsi(
1491 repo, 5, &terminusHdl, &entityType,
1492 &entityInstanceNum, &containerId)));
1493 EXPECT_EQ(entityType, 2);
1494 EXPECT_EQ(entityInstanceNum, 90);
1495
1496 auto l6 = pldm_entity_association_tree_add(tree, &entities[6], 90, node,
1497 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1498 EXPECT_EQ(l6, nullptr);
1499
1500 auto l7 = pldm_entity_association_tree_add(tree, &entities[7], 100, l1,
1501 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1502 auto seventh = pldm_pdr_add_fru_record_set(
1503 repo, 1, 7, entities[7].entity_type, entities[7].entity_instance_num,
1504 entities[7].entity_container_id, 7);
1505 EXPECT_NE(l7, nullptr);
1506 EXPECT_EQ(entities[7].entity_instance_num, 100);
1507 EXPECT_EQ(seventh, pldm_pdr_get_record_handle(
1508 repo, pldm_pdr_fru_record_set_find_by_rsi(
1509 repo, 7, &terminusHdl, &entityType,
1510 &entityInstanceNum, &containerId)));
1511 EXPECT_EQ(entityType, 3);
1512 EXPECT_EQ(entityInstanceNum, 100);
1513
1514 auto l8 = pldm_entity_association_tree_add(tree, &entities[8], 100, l2,
1515 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1516 auto eighth = pldm_pdr_add_fru_record_set(
1517 repo, 1, 8, entities[8].entity_type, entities[8].entity_instance_num,
1518 entities[8].entity_container_id, 8);
1519 EXPECT_NE(l8, nullptr);
1520 EXPECT_EQ(entities[8].entity_instance_num, 100);
1521 EXPECT_EQ(eighth, pldm_pdr_get_record_handle(
1522 repo, pldm_pdr_fru_record_set_find_by_rsi(
1523 repo, 8, &terminusHdl, &entityType,
1524 &entityInstanceNum, &containerId)));
1525 EXPECT_EQ(entityType, 3);
1526 EXPECT_EQ(entityInstanceNum, 100);
1527
1528 pldm_pdr_destroy(repo);
1529 pldm_entity_association_tree_destroy(tree);
1530}