blob: 8c572fcdc19bd5d5629069ed1c9ed3dd5d39350b [file] [log] [blame]
Manojkiran Eda9a8e4972022-11-28 16:38:21 +05301#include <endian.h>
2#include <stdlib.h>
3#include <string.h>
4
Andrew Jeffery9c766792022-08-10 23:12:49 +09305#include <array>
Manojkiran Eda9a8e4972022-11-28 16:38:21 +05306#include <cstdint>
7#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);
995 start += sizeof(pldm_entity);
996
997 currRecHandle = nextRecHandle;
998
999 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1000 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1001 l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL) *
1002 sizeof(pldm_entity)));
1003 start = data;
1004 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
1005 EXPECT_EQ(le32toh(hdr->record_handle), 2u);
1006 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
1007 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
1008 start += sizeof(pldm_pdr_hdr);
1009 containerId = reinterpret_cast<uint16_t*>(start);
1010 EXPECT_EQ(le16toh(*containerId), 1u);
1011 start += sizeof(uint16_t);
1012 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1013 start += sizeof(uint8_t);
1014 entity = reinterpret_cast<pldm_entity*>(start);
1015 EXPECT_EQ(le16toh(entity->entity_type), 1u);
1016 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1017 EXPECT_EQ(le16toh(entity->entity_container_id), 0u);
1018 start += sizeof(pldm_entity);
1019 EXPECT_EQ(*start, pldm_entity_get_num_children(
1020 l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL));
1021 start += sizeof(uint8_t);
1022 entity = reinterpret_cast<pldm_entity*>(start);
1023 EXPECT_EQ(le16toh(entity->entity_type), 2u);
1024 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1025 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
1026 start += sizeof(pldm_entity);
1027 entity = reinterpret_cast<pldm_entity*>(start);
1028 EXPECT_EQ(le16toh(entity->entity_type), 2u);
1029 EXPECT_EQ(le16toh(entity->entity_instance_num), 2u);
1030 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
1031 start += sizeof(pldm_entity);
1032
1033 currRecHandle = nextRecHandle;
1034
1035 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1036 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1037 l2a, PLDM_ENTITY_ASSOCIAION_LOGICAL) *
1038 sizeof(pldm_entity)));
1039 start = data;
1040 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
1041 EXPECT_EQ(le32toh(hdr->record_handle), 3u);
1042 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
1043 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
1044 start += sizeof(pldm_pdr_hdr);
1045 containerId = reinterpret_cast<uint16_t*>(start);
1046 EXPECT_EQ(le16toh(*containerId), 2u);
1047 start += sizeof(uint16_t);
1048 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_LOGICAL);
1049 start += sizeof(uint8_t);
1050 entity = reinterpret_cast<pldm_entity*>(start);
1051 EXPECT_EQ(le16toh(entity->entity_type), 2u);
1052 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1053 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
1054 start += sizeof(pldm_entity);
1055 EXPECT_EQ(*start, pldm_entity_get_num_children(
1056 l2a, PLDM_ENTITY_ASSOCIAION_LOGICAL));
1057 start += sizeof(uint8_t);
1058 entity = reinterpret_cast<pldm_entity*>(start);
1059 EXPECT_EQ(le16toh(entity->entity_type), 5);
1060 EXPECT_EQ(le16toh(entity->entity_instance_num), 2u);
1061 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
1062 start += sizeof(pldm_entity);
1063 entity = reinterpret_cast<pldm_entity*>(start);
1064 EXPECT_EQ(le16toh(entity->entity_type), 5u);
1065 EXPECT_EQ(le16toh(entity->entity_instance_num), 3u);
1066 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
1067 start += sizeof(pldm_entity);
1068
1069 currRecHandle = nextRecHandle;
1070
1071 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1072 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1073 l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL) *
1074 sizeof(pldm_entity)));
1075 start = data;
1076 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
1077 EXPECT_EQ(le32toh(hdr->record_handle), 4u);
1078 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
1079 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
1080 start += sizeof(pldm_pdr_hdr);
1081 containerId = reinterpret_cast<uint16_t*>(start);
1082 EXPECT_EQ(le16toh(*containerId), 2u);
1083 start += sizeof(uint16_t);
1084 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1085 start += sizeof(uint8_t);
1086 entity = reinterpret_cast<pldm_entity*>(start);
1087 EXPECT_EQ(le16toh(entity->entity_type), 2u);
1088 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1089 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
1090 start += sizeof(pldm_entity);
1091 EXPECT_EQ(*start, pldm_entity_get_num_children(
1092 l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL));
1093 start += sizeof(uint8_t);
1094 entity = reinterpret_cast<pldm_entity*>(start);
1095 EXPECT_EQ(le16toh(entity->entity_type), 4u);
1096 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1097 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
1098 start += sizeof(pldm_entity);
1099 entity = reinterpret_cast<pldm_entity*>(start);
1100 EXPECT_EQ(le16toh(entity->entity_type), 5u);
1101 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1102 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
1103 start += sizeof(pldm_entity);
1104
1105 currRecHandle = nextRecHandle;
1106
1107 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1108 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1109 l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL) *
1110 sizeof(pldm_entity)));
1111 start = data;
1112 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
1113 EXPECT_EQ(le32toh(hdr->record_handle), 5u);
1114 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
1115 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
1116 start += sizeof(pldm_pdr_hdr);
1117 containerId = reinterpret_cast<uint16_t*>(start);
1118 EXPECT_EQ(le16toh(*containerId), 3u);
1119 start += sizeof(uint16_t);
1120 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1121 start += sizeof(uint8_t);
1122 entity = reinterpret_cast<pldm_entity*>(start);
1123 EXPECT_EQ(le16toh(entity->entity_type), 4u);
1124 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1125 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
1126 start += sizeof(pldm_entity);
1127 EXPECT_EQ(*start, pldm_entity_get_num_children(
1128 l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL));
1129 start += sizeof(uint8_t);
1130 entity = reinterpret_cast<pldm_entity*>(start);
1131 EXPECT_EQ(le16toh(entity->entity_type), 6u);
1132 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1133 EXPECT_EQ(le16toh(entity->entity_container_id), 3u);
1134 start += sizeof(pldm_entity);
1135
1136 currRecHandle = nextRecHandle;
1137
1138 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1139 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1140 l3b, PLDM_ENTITY_ASSOCIAION_LOGICAL) *
1141 sizeof(pldm_entity)));
1142 start = data;
1143 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
1144 EXPECT_EQ(le32toh(hdr->record_handle), 6u);
1145 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
1146 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
1147 start += sizeof(pldm_pdr_hdr);
1148 containerId = reinterpret_cast<uint16_t*>(start);
1149 EXPECT_EQ(le16toh(*containerId), 4u);
1150 start += sizeof(uint16_t);
1151 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_LOGICAL);
1152 start += sizeof(uint8_t);
1153 entity = reinterpret_cast<pldm_entity*>(start);
1154 EXPECT_EQ(le16toh(entity->entity_type), 5u);
1155 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1156 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
1157 start += sizeof(pldm_entity);
1158 EXPECT_EQ(*start, pldm_entity_get_num_children(
1159 l3b, PLDM_ENTITY_ASSOCIAION_LOGICAL));
1160 start += sizeof(uint8_t);
1161 entity = reinterpret_cast<pldm_entity*>(start);
1162 EXPECT_EQ(le16toh(entity->entity_type), 7u);
1163 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1164 EXPECT_EQ(le16toh(entity->entity_container_id), 4u);
1165
1166 EXPECT_EQ(nextRecHandle, 0u);
1167
1168 pldm_pdr_destroy(repo);
1169 pldm_entity_association_tree_destroy(tree);
1170}
1171
1172TEST(EntityAssociationPDR, testFind)
1173{
1174 // 1
1175 // |
1176 // 2--3--4
1177 // |
1178 // 5--6--7
1179 // | |
1180 // 8 9
1181
1182 pldm_entity entities[9]{};
1183
1184 entities[0].entity_type = 1;
1185 entities[1].entity_type = 2;
1186 entities[2].entity_type = 2;
1187 entities[3].entity_type = 3;
1188 entities[4].entity_type = 4;
1189 entities[5].entity_type = 5;
1190 entities[6].entity_type = 5;
1191 entities[7].entity_type = 6;
1192 entities[8].entity_type = 7;
1193
1194 auto tree = pldm_entity_association_tree_init();
1195
1196 auto l1 = pldm_entity_association_tree_add(
1197 tree, &entities[0], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1198 EXPECT_NE(l1, nullptr);
1199 auto l2a = pldm_entity_association_tree_add(
1200 tree, &entities[1], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1201 EXPECT_NE(l2a, nullptr);
1202 auto l2b = pldm_entity_association_tree_add(
1203 tree, &entities[2], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1204 EXPECT_NE(l2b, nullptr);
1205 auto l2c = pldm_entity_association_tree_add(
1206 tree, &entities[3], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1207 EXPECT_NE(l2c, nullptr);
1208 auto l3a = pldm_entity_association_tree_add(
1209 tree, &entities[4], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1210 EXPECT_NE(l3a, nullptr);
1211 auto l3b = pldm_entity_association_tree_add(
1212 tree, &entities[5], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1213 EXPECT_NE(l3b, nullptr);
1214 auto l3c = pldm_entity_association_tree_add(
1215 tree, &entities[6], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1216 EXPECT_NE(l3c, nullptr);
1217 auto l4a = pldm_entity_association_tree_add(
1218 tree, &entities[7], 0xFFFF, l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1219 EXPECT_NE(l4a, nullptr);
1220 auto l4b = pldm_entity_association_tree_add(
1221 tree, &entities[8], 0xFFFF, l3b, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1222 EXPECT_NE(l4b, nullptr);
1223
1224 pldm_entity entity{};
1225
1226 entity.entity_type = 1;
1227 entity.entity_instance_num = 1;
1228 auto result = pldm_entity_association_tree_find(tree, &entity);
1229 EXPECT_EQ(result, l1);
1230 EXPECT_EQ(entity.entity_container_id, 0);
1231
1232 entity.entity_type = 2;
1233 entity.entity_instance_num = 1;
1234 result = pldm_entity_association_tree_find(tree, &entity);
1235 EXPECT_EQ(result, l2a);
1236 EXPECT_EQ(entity.entity_container_id, 1);
1237 entity.entity_type = 2;
1238 entity.entity_instance_num = 2;
1239 result = pldm_entity_association_tree_find(tree, &entity);
1240 EXPECT_EQ(result, l2b);
1241 EXPECT_EQ(entity.entity_container_id, 1);
1242 entity.entity_type = 3;
1243 entity.entity_instance_num = 1;
1244 result = pldm_entity_association_tree_find(tree, &entity);
1245 EXPECT_EQ(result, l2c);
1246 EXPECT_EQ(entity.entity_container_id, 1);
1247
1248 entity.entity_type = 7;
1249 entity.entity_instance_num = 1;
1250 result = pldm_entity_association_tree_find(tree, &entity);
1251 EXPECT_EQ(result, l4b);
1252 EXPECT_EQ(entity.entity_container_id, 4);
1253
1254 pldm_entity_association_tree_destroy(tree);
1255}
1256
1257TEST(EntityAssociationPDR, testCopyTree)
1258{
1259 pldm_entity entities[4]{};
1260 entities[0].entity_type = 1;
1261 entities[1].entity_type = 2;
1262 entities[2].entity_type = 2;
1263 entities[3].entity_type = 3;
1264
1265 auto orgTree = pldm_entity_association_tree_init();
1266 auto newTree = pldm_entity_association_tree_init();
1267 auto l1 =
1268 pldm_entity_association_tree_add(orgTree, &entities[0], 0xFFFF, nullptr,
1269 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1270 EXPECT_NE(l1, nullptr);
1271 auto l2a = pldm_entity_association_tree_add(
1272 orgTree, &entities[1], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1273 EXPECT_NE(l2a, nullptr);
1274 auto l2b = pldm_entity_association_tree_add(
1275 orgTree, &entities[2], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1276 EXPECT_NE(l2b, nullptr);
1277 auto l2c = pldm_entity_association_tree_add(
1278 orgTree, &entities[3], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1279 EXPECT_NE(l2c, nullptr);
1280 size_t orgNum{};
1281 pldm_entity* orgOut = nullptr;
1282 pldm_entity_association_tree_visit(orgTree, &orgOut, &orgNum);
1283 EXPECT_EQ(orgNum, 4u);
1284
1285 pldm_entity_association_tree_copy_root(orgTree, newTree);
1286 size_t newNum{};
1287 pldm_entity* newOut = nullptr;
1288 pldm_entity_association_tree_visit(newTree, &newOut, &newNum);
1289 EXPECT_EQ(newNum, orgNum);
1290 EXPECT_EQ(newOut[0].entity_type, 1u);
1291 EXPECT_EQ(newOut[0].entity_instance_num, 1u);
1292 EXPECT_EQ(newOut[0].entity_container_id, 0u);
1293 free(orgOut);
1294 free(newOut);
1295 pldm_entity_association_tree_destroy(orgTree);
1296 pldm_entity_association_tree_destroy(newTree);
1297}
1298
1299TEST(EntityAssociationPDR, testExtract)
1300{
1301 std::vector<uint8_t> pdr{};
1302 pdr.resize(sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_entity_association) +
1303 sizeof(pldm_entity) * 4);
1304 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(pdr.data());
1305 hdr->type = PLDM_PDR_ENTITY_ASSOCIATION;
1306 hdr->length =
1307 htole16(sizeof(pldm_pdr_entity_association) + sizeof(pldm_entity) * 4);
1308
1309 pldm_pdr_entity_association* e =
1310 reinterpret_cast<pldm_pdr_entity_association*>(pdr.data() +
1311 sizeof(pldm_pdr_hdr));
1312 e->container_id = htole16(1);
1313 e->num_children = 5;
1314 e->container.entity_type = htole16(1);
1315 e->container.entity_instance_num = htole16(1);
1316 e->container.entity_container_id = htole16(0);
1317
1318 pldm_entity* entity = e->children;
1319 entity->entity_type = htole16(2);
1320 entity->entity_instance_num = htole16(1);
1321 entity->entity_container_id = htole16(1);
1322 ++entity;
1323 entity->entity_type = htole16(3);
1324 entity->entity_instance_num = htole16(1);
1325 entity->entity_container_id = htole16(1);
1326 ++entity;
1327 entity->entity_type = htole16(4);
1328 entity->entity_instance_num = htole16(1);
1329 entity->entity_container_id = htole16(1);
1330 ++entity;
1331 entity->entity_type = htole16(5);
1332 entity->entity_instance_num = htole16(1);
1333 entity->entity_container_id = htole16(1);
1334 ++entity;
1335 entity->entity_type = htole16(6);
1336 entity->entity_instance_num = htole16(1);
1337 entity->entity_container_id = htole16(1);
1338
1339 size_t num{};
1340 pldm_entity* out = nullptr;
1341 pldm_entity_association_pdr_extract(pdr.data(), pdr.size(), &num, &out);
1342 EXPECT_EQ(num, (unsigned)e->num_children + 1);
1343 EXPECT_EQ(out[0].entity_type, 1u);
1344 EXPECT_EQ(out[0].entity_instance_num, 1u);
1345 EXPECT_EQ(out[0].entity_container_id, 0u);
1346 EXPECT_EQ(out[1].entity_type, 2u);
1347 EXPECT_EQ(out[1].entity_instance_num, 1u);
1348 EXPECT_EQ(out[1].entity_container_id, 1u);
1349 EXPECT_EQ(out[2].entity_type, 3u);
1350 EXPECT_EQ(out[2].entity_instance_num, 1u);
1351 EXPECT_EQ(out[2].entity_container_id, 1u);
1352 EXPECT_EQ(out[3].entity_type, 4u);
1353 EXPECT_EQ(out[3].entity_instance_num, 1u);
1354 EXPECT_EQ(out[3].entity_container_id, 1u);
1355 EXPECT_EQ(out[4].entity_type, 5u);
1356 EXPECT_EQ(out[4].entity_instance_num, 1u);
1357 EXPECT_EQ(out[4].entity_container_id, 1u);
1358 EXPECT_EQ(out[5].entity_type, 6u);
1359 EXPECT_EQ(out[5].entity_instance_num, 1u);
1360 EXPECT_EQ(out[5].entity_container_id, 1u);
1361
1362 free(out);
1363}
1364
1365TEST(EntityAssociationPDR, testGetChildren)
1366{
1367 pldm_entity entities[4]{};
1368 entities[0].entity_type = 1;
1369 entities[1].entity_type = 2;
1370 entities[2].entity_type = 2;
1371 entities[3].entity_type = 3;
1372
1373 auto tree = pldm_entity_association_tree_init();
1374 auto l1 = pldm_entity_association_tree_add(
1375 tree, &entities[0], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1376 EXPECT_NE(l1, nullptr);
1377 auto l2a = pldm_entity_association_tree_add(
1378 tree, &entities[1], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1379 EXPECT_NE(l2a, nullptr);
1380 auto l2b = pldm_entity_association_tree_add(
1381 tree, &entities[2], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1382 EXPECT_NE(l2b, nullptr);
1383 auto l2c = pldm_entity_association_tree_add(
1384 tree, &entities[3], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1385 EXPECT_NE(l2c, nullptr);
1386
1387 pldm_entity et1;
1388 et1.entity_type = 2;
1389 et1.entity_instance_num = 1;
1390 EXPECT_EQ(true, pldm_is_current_parent_child(l1, &et1));
1391
1392 pldm_entity et2;
1393 et2.entity_type = 2;
1394 et2.entity_instance_num = 2;
1395 EXPECT_EQ(true, pldm_is_current_parent_child(l1, &et2));
1396
1397 pldm_entity et3;
1398 et3.entity_type = 2;
1399 et3.entity_instance_num = 3;
1400 EXPECT_EQ(false, pldm_is_current_parent_child(l1, &et3));
1401
1402 pldm_entity_association_tree_destroy(tree);
1403}
1404
1405TEST(EntityAssociationPDR, testEntityInstanceNumber)
1406{
1407 pldm_entity entities[9]{};
1408
1409 entities[0].entity_type = 1;
1410 entities[1].entity_type = 2;
1411 entities[2].entity_type = 2;
1412 entities[3].entity_type = 2;
1413 entities[4].entity_type = 2;
1414 entities[5].entity_type = 2;
1415 entities[6].entity_type = 2;
1416 entities[7].entity_type = 3;
1417 entities[8].entity_type = 3;
1418
1419 auto tree = pldm_entity_association_tree_init();
1420 auto repo = pldm_pdr_init();
1421
1422 uint16_t terminusHdl{};
1423 uint16_t entityType{};
1424 uint16_t entityInstanceNum{};
1425 uint16_t containerId{};
1426
1427 auto node = pldm_entity_association_tree_add(
1428 tree, &entities[0], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1429 EXPECT_NE(node, nullptr);
1430
1431 auto l1 = pldm_entity_association_tree_add(tree, &entities[1], 63, node,
1432 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1433 auto first = pldm_pdr_add_fru_record_set(
1434 repo, 1, 1, entities[1].entity_type, entities[1].entity_instance_num,
1435 entities[1].entity_container_id, 1);
1436 EXPECT_NE(l1, nullptr);
1437 EXPECT_EQ(entities[1].entity_instance_num, 63);
1438 EXPECT_EQ(first, pldm_pdr_get_record_handle(
1439 repo, pldm_pdr_fru_record_set_find_by_rsi(
1440 repo, 1, &terminusHdl, &entityType,
1441 &entityInstanceNum, &containerId)));
1442 EXPECT_EQ(entityType, 2);
1443 EXPECT_EQ(entityInstanceNum, 63);
1444
1445 auto l2 = pldm_entity_association_tree_add(tree, &entities[2], 37, node,
1446 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1447 auto second = pldm_pdr_add_fru_record_set(
1448 repo, 1, 2, entities[2].entity_type, entities[2].entity_instance_num,
1449 entities[2].entity_container_id, 2);
1450 EXPECT_NE(l2, nullptr);
1451 EXPECT_EQ(entities[2].entity_instance_num, 37);
1452 EXPECT_EQ(second, pldm_pdr_get_record_handle(
1453 repo, pldm_pdr_fru_record_set_find_by_rsi(
1454 repo, 2, &terminusHdl, &entityType,
1455 &entityInstanceNum, &containerId)));
1456 EXPECT_EQ(entityType, 2);
1457 EXPECT_EQ(entityInstanceNum, 37);
1458
1459 auto l3 = pldm_entity_association_tree_add(tree, &entities[3], 44, node,
1460 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1461 auto third = pldm_pdr_add_fru_record_set(
1462 repo, 1, 3, entities[3].entity_type, entities[3].entity_instance_num,
1463 entities[3].entity_container_id, 3);
1464 EXPECT_NE(l3, nullptr);
1465 EXPECT_EQ(entities[3].entity_instance_num, 44);
1466 EXPECT_EQ(third, pldm_pdr_get_record_handle(
1467 repo, pldm_pdr_fru_record_set_find_by_rsi(
1468 repo, 3, &terminusHdl, &entityType,
1469 &entityInstanceNum, &containerId)));
1470 EXPECT_EQ(entityType, 2);
1471 EXPECT_EQ(entityInstanceNum, 44);
1472
1473 auto l4 = pldm_entity_association_tree_add(tree, &entities[4], 89, node,
1474 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1475 auto fourth = pldm_pdr_add_fru_record_set(
1476 repo, 1, 4, entities[4].entity_type, entities[4].entity_instance_num,
1477 entities[4].entity_container_id, 4);
1478 EXPECT_NE(l4, nullptr);
1479 EXPECT_EQ(entities[4].entity_instance_num, 89);
1480 EXPECT_EQ(fourth, pldm_pdr_get_record_handle(
1481 repo, pldm_pdr_fru_record_set_find_by_rsi(
1482 repo, 4, &terminusHdl, &entityType,
1483 &entityInstanceNum, &containerId)));
1484 EXPECT_EQ(entityType, 2);
1485 EXPECT_EQ(entityInstanceNum, 89);
1486
1487 auto l5 = pldm_entity_association_tree_add(tree, &entities[5], 0xFFFF, node,
1488 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1489 auto fifth = pldm_pdr_add_fru_record_set(
1490 repo, 1, 5, entities[5].entity_type, entities[5].entity_instance_num,
1491 entities[5].entity_container_id, 5);
1492 EXPECT_NE(l5, nullptr);
1493 EXPECT_EQ(entities[5].entity_instance_num, 90);
1494 EXPECT_EQ(fifth, pldm_pdr_get_record_handle(
1495 repo, pldm_pdr_fru_record_set_find_by_rsi(
1496 repo, 5, &terminusHdl, &entityType,
1497 &entityInstanceNum, &containerId)));
1498 EXPECT_EQ(entityType, 2);
1499 EXPECT_EQ(entityInstanceNum, 90);
1500
1501 auto l6 = pldm_entity_association_tree_add(tree, &entities[6], 90, node,
1502 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1503 EXPECT_EQ(l6, nullptr);
1504
1505 auto l7 = pldm_entity_association_tree_add(tree, &entities[7], 100, l1,
1506 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1507 auto seventh = pldm_pdr_add_fru_record_set(
1508 repo, 1, 7, entities[7].entity_type, entities[7].entity_instance_num,
1509 entities[7].entity_container_id, 7);
1510 EXPECT_NE(l7, nullptr);
1511 EXPECT_EQ(entities[7].entity_instance_num, 100);
1512 EXPECT_EQ(seventh, pldm_pdr_get_record_handle(
1513 repo, pldm_pdr_fru_record_set_find_by_rsi(
1514 repo, 7, &terminusHdl, &entityType,
1515 &entityInstanceNum, &containerId)));
1516 EXPECT_EQ(entityType, 3);
1517 EXPECT_EQ(entityInstanceNum, 100);
1518
1519 auto l8 = pldm_entity_association_tree_add(tree, &entities[8], 100, l2,
1520 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1521 auto eighth = pldm_pdr_add_fru_record_set(
1522 repo, 1, 8, entities[8].entity_type, entities[8].entity_instance_num,
1523 entities[8].entity_container_id, 8);
1524 EXPECT_NE(l8, nullptr);
1525 EXPECT_EQ(entities[8].entity_instance_num, 100);
1526 EXPECT_EQ(eighth, pldm_pdr_get_record_handle(
1527 repo, pldm_pdr_fru_record_set_find_by_rsi(
1528 repo, 8, &terminusHdl, &entityType,
1529 &entityInstanceNum, &containerId)));
1530 EXPECT_EQ(entityType, 3);
1531 EXPECT_EQ(entityInstanceNum, 100);
1532
1533 pldm_pdr_destroy(repo);
1534 pldm_entity_association_tree_destroy(tree);
1535}