blob: 174873ce98023b01031b761731cb67204c8414ca [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
Pavithra Barithaya4d694342023-05-19 08:04:41 -0500550#ifdef LIBPLDM_API_TESTING
551TEST(PDRUpdate, testFindLastInRange)
552{
553 auto repo = pldm_pdr_init();
554
555 std::array<uint8_t, 10> data{};
556 auto handle1 = pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
557 auto handle2 = pldm_pdr_add(repo, data.data(), data.size(), 23, false, 1);
558 auto handle3 = pldm_pdr_add(repo, data.data(), data.size(), 77, false, 1);
559 auto handle4 =
560 pldm_pdr_add(repo, data.data(), data.size(), 16777325, true, 1);
561 auto handle5 =
562 pldm_pdr_add(repo, data.data(), data.size(), 16777344, true, 1);
563
564 auto rec1 = pldm_pdr_find_last_in_range(repo, 0, 100);
565 auto rec2 = pldm_pdr_find_last_in_range(repo, 16777300, 33554431);
566 EXPECT_NE(rec1, nullptr);
567 EXPECT_NE(rec2, nullptr);
568 EXPECT_NE(handle1, pldm_pdr_get_record_handle(repo, rec1));
569 EXPECT_NE(handle2, pldm_pdr_get_record_handle(repo, rec1));
570 EXPECT_EQ(handle3, pldm_pdr_get_record_handle(repo, rec1));
571 EXPECT_NE(handle4, pldm_pdr_get_record_handle(repo, rec2));
572 EXPECT_EQ(handle5, pldm_pdr_get_record_handle(repo, rec2));
573
574 pldm_pdr_destroy(repo);
575}
576#endif
577
Andrew Jeffery9c766792022-08-10 23:12:49 +0930578TEST(EntityAssociationPDR, testInit)
579{
580 auto tree = pldm_entity_association_tree_init();
581 EXPECT_NE(tree, nullptr);
582 pldm_entity_association_tree_destroy(tree);
583}
584
585TEST(EntityAssociationPDR, testBuild)
586{
587 // 1
588 // |
589 // 2--3--4
590 // |
591 // 5--6--7
592 // | |
593 // 9 8
594
595 pldm_entity entities[9]{};
596
597 entities[0].entity_type = 1;
598 entities[1].entity_type = 2;
599 entities[2].entity_type = 2;
600 entities[3].entity_type = 3;
601 entities[4].entity_type = 4;
602 entities[5].entity_type = 5;
603 entities[6].entity_type = 5;
604 entities[7].entity_type = 6;
605 entities[8].entity_type = 7;
606
607 auto tree = pldm_entity_association_tree_init();
608
609 auto l1 = pldm_entity_association_tree_add(
610 tree, &entities[0], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
611 EXPECT_NE(l1, nullptr);
612 auto l2a = pldm_entity_association_tree_add(
613 tree, &entities[1], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
614 EXPECT_NE(l2a, nullptr);
615 auto l2b = pldm_entity_association_tree_add(
616 tree, &entities[2], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
617 EXPECT_NE(l2b, nullptr);
618 auto l2c = pldm_entity_association_tree_add(
619 tree, &entities[3], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
620 EXPECT_NE(l2c, nullptr);
621 auto l3a = pldm_entity_association_tree_add(
622 tree, &entities[4], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
623 EXPECT_NE(l3a, nullptr);
624 auto l3b = pldm_entity_association_tree_add(
625 tree, &entities[5], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
626 EXPECT_NE(l3b, nullptr);
627 auto l3c = pldm_entity_association_tree_add(
628 tree, &entities[6], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
629 EXPECT_NE(l3b, nullptr);
630 auto l4a = pldm_entity_association_tree_add(
631 tree, &entities[7], 0xFFFF, l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
632 EXPECT_NE(l4a, nullptr);
633 auto l4b = pldm_entity_association_tree_add(
634 tree, &entities[8], 0xFFFF, l3b, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
635 EXPECT_NE(l4b, nullptr);
636
637 EXPECT_EQ(pldm_entity_is_node_parent(l1), true);
638 EXPECT_EQ(pldm_entity_is_node_parent(l2a), true);
639 EXPECT_EQ(pldm_entity_is_node_parent(l3a), true);
640 EXPECT_EQ(pldm_entity_is_node_parent(l3b), true);
641
642 EXPECT_EQ(pldm_entity_is_node_parent(l2b), false);
643 EXPECT_EQ(pldm_entity_is_node_parent(l2c), false);
644 EXPECT_EQ(pldm_entity_is_node_parent(l3c), false);
645 EXPECT_EQ(pldm_entity_is_node_parent(l4a), false);
646 EXPECT_EQ(pldm_entity_is_node_parent(l4b), false);
647
648 EXPECT_EQ(pldm_entity_is_exist_parent(l1), false);
649
650 pldm_entity nodeL1 = pldm_entity_extract(l1);
651 pldm_entity parentL2a = pldm_entity_get_parent(l2a);
652 pldm_entity parentL2b = pldm_entity_get_parent(l2b);
653 pldm_entity parentL2c = pldm_entity_get_parent(l2c);
654 EXPECT_EQ(pldm_entity_is_exist_parent(l2a), true);
655 EXPECT_EQ(pldm_entity_is_exist_parent(l2b), true);
656 EXPECT_EQ(pldm_entity_is_exist_parent(l2c), true);
657 EXPECT_EQ(parentL2a.entity_type, nodeL1.entity_type);
658 EXPECT_EQ(parentL2a.entity_instance_num, nodeL1.entity_instance_num);
659 EXPECT_EQ(parentL2a.entity_container_id, nodeL1.entity_container_id);
660 EXPECT_EQ(parentL2b.entity_type, nodeL1.entity_type);
661 EXPECT_EQ(parentL2b.entity_instance_num, nodeL1.entity_instance_num);
662 EXPECT_EQ(parentL2b.entity_container_id, nodeL1.entity_container_id);
663 EXPECT_EQ(parentL2c.entity_type, nodeL1.entity_type);
664 EXPECT_EQ(parentL2c.entity_instance_num, nodeL1.entity_instance_num);
665 EXPECT_EQ(parentL2c.entity_container_id, nodeL1.entity_container_id);
666
667 pldm_entity nodeL2a = pldm_entity_extract(l2a);
668 pldm_entity parentL3a = pldm_entity_get_parent(l3a);
669 pldm_entity parentL3b = pldm_entity_get_parent(l3b);
670 pldm_entity parentL3c = pldm_entity_get_parent(l3c);
671 EXPECT_EQ(pldm_entity_is_exist_parent(l3a), true);
672 EXPECT_EQ(pldm_entity_is_exist_parent(l3b), true);
673 EXPECT_EQ(pldm_entity_is_exist_parent(l3c), true);
674 EXPECT_EQ(parentL3a.entity_type, nodeL2a.entity_type);
675 EXPECT_EQ(parentL3a.entity_instance_num, nodeL2a.entity_instance_num);
676 EXPECT_EQ(parentL3a.entity_container_id, nodeL2a.entity_container_id);
677 EXPECT_EQ(parentL3b.entity_type, nodeL2a.entity_type);
678 EXPECT_EQ(parentL3b.entity_instance_num, nodeL2a.entity_instance_num);
679 EXPECT_EQ(parentL3b.entity_container_id, nodeL2a.entity_container_id);
680 EXPECT_EQ(parentL3c.entity_type, nodeL2a.entity_type);
681 EXPECT_EQ(parentL3c.entity_instance_num, nodeL2a.entity_instance_num);
682 EXPECT_EQ(parentL3c.entity_container_id, nodeL2a.entity_container_id);
683
684 pldm_entity nodeL3a = pldm_entity_extract(l3a);
685 pldm_entity parentL4a = pldm_entity_get_parent(l4a);
686 EXPECT_EQ(pldm_entity_is_exist_parent(l4a), true);
687 EXPECT_EQ(parentL4a.entity_type, nodeL3a.entity_type);
688 EXPECT_EQ(parentL4a.entity_instance_num, nodeL3a.entity_instance_num);
689 EXPECT_EQ(parentL4a.entity_container_id, nodeL3a.entity_container_id);
690
691 pldm_entity nodeL3b = pldm_entity_extract(l3b);
692 pldm_entity parentL4b = pldm_entity_get_parent(l4b);
693 EXPECT_EQ(pldm_entity_is_exist_parent(l4b), true);
694 EXPECT_EQ(parentL4b.entity_type, nodeL3b.entity_type);
695 EXPECT_EQ(parentL4b.entity_instance_num, nodeL3b.entity_instance_num);
696 EXPECT_EQ(parentL4b.entity_container_id, nodeL3b.entity_container_id);
697
698 size_t num{};
699 pldm_entity* out = nullptr;
700 pldm_entity_association_tree_visit(tree, &out, &num);
701 EXPECT_EQ(num, 9u);
702
703 EXPECT_EQ(out[0].entity_type, 1u);
704 EXPECT_EQ(out[0].entity_instance_num, 1u);
705 EXPECT_EQ(out[0].entity_container_id, 0u);
706
707 EXPECT_EQ(out[1].entity_type, 2u);
708 EXPECT_EQ(out[1].entity_instance_num, 1u);
709 EXPECT_EQ(out[1].entity_container_id, 1u);
710 EXPECT_EQ(out[2].entity_type, 2u);
711 EXPECT_EQ(out[2].entity_instance_num, 2u);
712 EXPECT_EQ(out[2].entity_container_id, 1u);
713 EXPECT_EQ(out[3].entity_type, 3u);
714 EXPECT_EQ(out[3].entity_instance_num, 1u);
715 EXPECT_EQ(out[3].entity_container_id, 1u);
716
717 EXPECT_EQ(out[4].entity_type, 4u);
718 EXPECT_EQ(out[4].entity_instance_num, 1u);
719 EXPECT_EQ(out[4].entity_container_id, 2u);
720 EXPECT_EQ(out[5].entity_type, 5u);
721 EXPECT_EQ(out[5].entity_instance_num, 1u);
722 EXPECT_EQ(out[5].entity_container_id, 2u);
723 EXPECT_EQ(out[6].entity_type, 5u);
724 EXPECT_EQ(out[6].entity_instance_num, 2u);
725 EXPECT_EQ(out[6].entity_container_id, 2u);
726
727 EXPECT_EQ(out[7].entity_type, 7u);
728 EXPECT_EQ(out[7].entity_instance_num, 1u);
729 EXPECT_EQ(out[7].entity_container_id, 4u);
730 EXPECT_EQ(out[8].entity_type, 6u);
731 EXPECT_EQ(out[8].entity_instance_num, 1u);
732 EXPECT_EQ(out[8].entity_container_id, 3u);
733
734 free(out);
735
736 pldm_entity p1 = pldm_entity_extract(l1);
737 EXPECT_EQ(p1.entity_type, 1u);
738 EXPECT_EQ(p1.entity_instance_num, 1u);
739 EXPECT_EQ(p1.entity_container_id, 0u);
740
741 pldm_entity p2a = pldm_entity_extract(l2a);
742 EXPECT_EQ(p2a.entity_type, 2u);
743 EXPECT_EQ(p2a.entity_instance_num, 1u);
744 EXPECT_EQ(p2a.entity_container_id, 1u);
745 pldm_entity p2b = pldm_entity_extract(l2b);
746 EXPECT_EQ(p2b.entity_type, 2u);
747 EXPECT_EQ(p2b.entity_instance_num, 2u);
748 EXPECT_EQ(p2b.entity_container_id, 1u);
749 pldm_entity p2c = pldm_entity_extract(l2c);
750 EXPECT_EQ(p2c.entity_type, 3u);
751 EXPECT_EQ(p2c.entity_instance_num, 1u);
752 EXPECT_EQ(p2c.entity_container_id, 1u);
753
754 pldm_entity p3a = pldm_entity_extract(l3a);
755 EXPECT_EQ(p3a.entity_type, 4u);
756 EXPECT_EQ(p3a.entity_instance_num, 1u);
757 EXPECT_EQ(p3a.entity_container_id, 2u);
758 pldm_entity p3b = pldm_entity_extract(l3b);
759 EXPECT_EQ(p3b.entity_type, 5u);
760 EXPECT_EQ(p3b.entity_instance_num, 1u);
761 EXPECT_EQ(p3b.entity_container_id, 2u);
762 pldm_entity p3c = pldm_entity_extract(l3c);
763 EXPECT_EQ(p3c.entity_type, 5u);
764 EXPECT_EQ(p3c.entity_instance_num, 2u);
765 EXPECT_EQ(p3c.entity_container_id, 2u);
766
767 pldm_entity p4a = pldm_entity_extract(l4a);
768 EXPECT_EQ(p4a.entity_type, 6u);
769 EXPECT_EQ(p4a.entity_instance_num, 1u);
770 EXPECT_EQ(p4a.entity_container_id, 3u);
771 pldm_entity p4b = pldm_entity_extract(l4b);
772 EXPECT_EQ(p4b.entity_type, 7u);
773 EXPECT_EQ(p4b.entity_instance_num, 1u);
774 EXPECT_EQ(p4b.entity_container_id, 4u);
775
776 pldm_entity_association_tree_destroy(tree);
777}
778
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500779#if LIBPLDM_API_TESTING
780TEST(EntityAssociationPDR, findAndAddRemotePDR)
781{
782 // Tree - 1
783 //
784 // 11521(1,0)
785 // |
786 // 45 (1,1)
787 // |
788 // 64 (1,2)
789 // |
790 // ------------------
791 // | |
792 // 67(0,3) 67(1,3)
793 // | |
794 // 135(0,4) 135(0,5)
795 // | |
796 // 32903(0,6) 32903(0,7)
797
798 pldm_entity entities[9]{};
799 entities[0].entity_type = 11521;
800 entities[1].entity_type = 45;
801 entities[2].entity_type = 64;
802 entities[3].entity_type = 67;
803 entities[4].entity_type = 67;
804 entities[5].entity_type = 135;
805 entities[5].entity_container_id = 2;
806 entities[6].entity_type = 135;
807 entities[6].entity_container_id = 3;
808 entities[7].entity_type = 32903;
809 entities[8].entity_type = 32903;
810 auto tree = pldm_entity_association_tree_init();
811 auto l1 = pldm_entity_association_tree_add_entity(
812 tree, &entities[0], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_LOGICAL,
813 false, true, 0xFFFF);
814 EXPECT_NE(l1, nullptr);
815 auto l2 = pldm_entity_association_tree_add_entity(
816 tree, &entities[1], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
817 false, 0xFFFF);
818 EXPECT_NE(l2, nullptr);
819 auto l3 = pldm_entity_association_tree_add_entity(
820 tree, &entities[2], 0xFFFF, l2, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
821 true, 0xFFFF);
822 EXPECT_NE(l3, nullptr);
823 auto l4a = pldm_entity_association_tree_add_entity(
824 tree, &entities[3], 0, l3, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
825 false, 0xFFFF);
826 EXPECT_NE(l4a, nullptr);
827 auto l4b = pldm_entity_association_tree_add_entity(
828 tree, &entities[4], 1, l3, PLDM_ENTITY_ASSOCIAION_PHYSICAL, true, true,
829 0xFFFF);
830 EXPECT_NE(l4b, nullptr);
831 auto l5a = pldm_entity_association_tree_add_entity(
832 tree, &entities[5], 0, l4a, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
833 false, 0xFFFF);
834 EXPECT_NE(l5a, nullptr);
835 auto l5b = pldm_entity_association_tree_add_entity(
836 tree, &entities[6], 0, l4b, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
837 false, 0xFFFF);
838 EXPECT_NE(l5b, nullptr);
839 pldm_entity entity{};
840 entity.entity_type = 135;
841 entity.entity_instance_num = 0;
842 entity.entity_container_id = 2;
843 auto result1 = pldm_entity_association_tree_find(tree, &entity);
844 EXPECT_EQ(result1, l5a);
845 EXPECT_EQ(entities[5].entity_container_id, 2);
846 auto l6a = pldm_entity_association_tree_add_entity(
847 tree, &entities[7], 0, result1, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
848 false, 0xFFFF);
849 EXPECT_NE(l6a, nullptr);
850 entity.entity_type = 135;
851 entity.entity_instance_num = 0;
852 entity.entity_container_id = 3;
853 auto result2 = pldm_entity_association_tree_find(tree, &entity);
854 EXPECT_NE(result2, l5b);
855 EXPECT_EQ(entities[6].entity_container_id, 3);
856 auto l7a = pldm_entity_association_tree_add_entity(
857 tree, &entities[8], 0, result2, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
858 false, 0xFFFF);
859 EXPECT_EQ(l7a, nullptr);
860 pldm_entity_association_tree_destroy(tree);
861}
862#endif
863
Andrew Jeffery9c766792022-08-10 23:12:49 +0930864TEST(EntityAssociationPDR, testSpecialTrees)
865{
866 pldm_entity entities[3]{};
867
868 entities[0].entity_type = 1;
869 entities[1].entity_type = 2;
870 entities[2].entity_type = 1;
871
872 // A
873 auto tree = pldm_entity_association_tree_init();
874 auto node = pldm_entity_association_tree_add(
875 tree, &entities[0], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
876 EXPECT_NE(node, nullptr);
877 size_t num{};
878 pldm_entity* out = nullptr;
879 pldm_entity_association_tree_visit(tree, &out, &num);
880 EXPECT_EQ(num, 1u);
881 EXPECT_EQ(out[0].entity_type, 1u);
882 EXPECT_EQ(out[0].entity_instance_num, 1u);
883 EXPECT_EQ(out[0].entity_container_id, 0u);
884 free(out);
885 pldm_entity_association_tree_destroy(tree);
886
887 // A-A-A
888 tree = pldm_entity_association_tree_init();
889 node = pldm_entity_association_tree_add(tree, &entities[0], 0xFFFF, nullptr,
890 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
891 EXPECT_NE(node, nullptr);
892 node = pldm_entity_association_tree_add(tree, &entities[1], 0xFFFF, nullptr,
893 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
894 EXPECT_NE(node, nullptr);
895 node = pldm_entity_association_tree_add(tree, &entities[2], 0xFFFF, nullptr,
896 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
897 EXPECT_NE(node, nullptr);
898 pldm_entity_association_tree_visit(tree, &out, &num);
899 EXPECT_EQ(num, 3u);
900 EXPECT_EQ(out[0].entity_type, 1u);
901 EXPECT_EQ(out[0].entity_instance_num, 1u);
902 EXPECT_EQ(out[0].entity_container_id, 0u);
903 EXPECT_EQ(out[1].entity_type, 1u);
904 EXPECT_EQ(out[1].entity_instance_num, 2u);
905 EXPECT_EQ(out[1].entity_container_id, 0u);
906 EXPECT_EQ(out[2].entity_type, 2u);
907 EXPECT_EQ(out[2].entity_instance_num, 1u);
908 EXPECT_EQ(out[2].entity_container_id, 0u);
909 free(out);
910 pldm_entity_association_tree_destroy(tree);
911
912 // A
913 // |
914 // A
915 // |
916 // A
917 tree = pldm_entity_association_tree_init();
918 node = pldm_entity_association_tree_add(tree, &entities[0], 0xFFFF, nullptr,
919 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
920 EXPECT_NE(node, nullptr);
921 auto node1 = pldm_entity_association_tree_add(
922 tree, &entities[1], 0xFFFF, node, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
923 EXPECT_NE(node1, nullptr);
924 auto node2 = pldm_entity_association_tree_add(
925 tree, &entities[2], 0xFFFF, node1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
926 EXPECT_NE(node2, nullptr);
927 pldm_entity_association_tree_visit(tree, &out, &num);
928 EXPECT_EQ(num, 3u);
929 EXPECT_EQ(out[0].entity_type, 1u);
930 EXPECT_EQ(out[0].entity_instance_num, 1u);
931 EXPECT_EQ(out[0].entity_container_id, 0u);
932 EXPECT_EQ(out[1].entity_type, 2u);
933 EXPECT_EQ(out[1].entity_instance_num, 1u);
934 EXPECT_EQ(out[1].entity_container_id, 1u);
935 EXPECT_EQ(out[2].entity_type, 1u);
936 EXPECT_EQ(out[2].entity_instance_num, 1u);
937 EXPECT_EQ(out[2].entity_container_id, 2u);
938 free(out);
939 pldm_entity_association_tree_destroy(tree);
940
941 // A-A
942 // |
943 // A-A
944 tree = pldm_entity_association_tree_init();
945 node = pldm_entity_association_tree_add(tree, &entities[0], 0xFFFF, nullptr,
946 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
947 EXPECT_NE(node, nullptr);
948 node = pldm_entity_association_tree_add(tree, &entities[0], 0xFFFF, nullptr,
949 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
950 EXPECT_NE(node, nullptr);
951 node1 = pldm_entity_association_tree_add(tree, &entities[1], 0xFFFF, node,
952 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
953 EXPECT_NE(node1, nullptr);
954 node2 = pldm_entity_association_tree_add(tree, &entities[2], 0xFFFF, node,
955 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
956 EXPECT_NE(node2, nullptr);
957 pldm_entity_association_tree_visit(tree, &out, &num);
958 EXPECT_EQ(num, 4u);
959 EXPECT_EQ(out[0].entity_type, 1u);
960 EXPECT_EQ(out[0].entity_instance_num, 1u);
961 EXPECT_EQ(out[0].entity_container_id, 0u);
962 EXPECT_EQ(out[1].entity_type, 1u);
963 EXPECT_EQ(out[1].entity_instance_num, 2u);
964 EXPECT_EQ(out[1].entity_container_id, 0u);
965 EXPECT_EQ(out[2].entity_type, 2u);
966 EXPECT_EQ(out[2].entity_instance_num, 1u);
967 EXPECT_EQ(out[2].entity_container_id, 1u);
968 EXPECT_EQ(out[3].entity_type, 1u);
969 EXPECT_EQ(out[3].entity_instance_num, 1u);
970 EXPECT_EQ(out[3].entity_container_id, 1u);
971 free(out);
972 pldm_entity_association_tree_destroy(tree);
973}
974
975TEST(EntityAssociationPDR, testPDR)
976{
977 // e = entity type, c = container id, i = instance num
978
979 // INPUT
980 // 1(e=1)--1a(e=2)
981 // |
982 // 2(e=2)--3(e=2)--4(e=2)--5(e=3)
983 // |
984 // 6(e=4)--7(e=5)--8(e=5)--9(e=5)
985 // | |
986 // 11(e=6) 10(e=7)
987
988 // Expected OUTPUT
989 // 1(e=1,c=0,i=1)
990 // |
991 // 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)
992 // |
993 // 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)
994 // | |
995 // 10(e=6,c=3,i=1) 11(e=7,c=4,i=1)
996 pldm_entity entities[11]{};
997
998 entities[0].entity_type = 1;
999 entities[1].entity_type = 2;
1000 entities[2].entity_type = 3;
1001 entities[3].entity_type = 2;
1002 entities[4].entity_type = 3;
1003 entities[5].entity_type = 4;
1004 entities[6].entity_type = 5;
1005 entities[7].entity_type = 5;
1006 entities[8].entity_type = 5;
1007 entities[9].entity_type = 6;
1008 entities[10].entity_type = 7;
1009
1010 auto tree = pldm_entity_association_tree_init();
1011
1012 auto l1 = pldm_entity_association_tree_add(
1013 tree, &entities[0], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1014 EXPECT_NE(l1, nullptr);
1015 auto l1a = pldm_entity_association_tree_add(
1016 tree, &entities[1], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1017 EXPECT_NE(l1a, nullptr);
1018
1019 auto l2a = pldm_entity_association_tree_add(
1020 tree, &entities[1], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1021 EXPECT_NE(l2a, nullptr);
1022 auto l2b = pldm_entity_association_tree_add(tree, &entities[2], 0xFFFF, l1,
1023 PLDM_ENTITY_ASSOCIAION_LOGICAL);
1024 EXPECT_NE(l2b, nullptr);
1025 auto l2c = pldm_entity_association_tree_add(
1026 tree, &entities[3], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1027 EXPECT_NE(l2c, nullptr);
1028 auto l2d = pldm_entity_association_tree_add(tree, &entities[4], 0xFFFF, l1,
1029 PLDM_ENTITY_ASSOCIAION_LOGICAL);
1030 EXPECT_NE(l2d, nullptr);
1031
1032 auto l3a = pldm_entity_association_tree_add(
1033 tree, &entities[5], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1034 EXPECT_NE(l3a, nullptr);
1035 auto l3b = pldm_entity_association_tree_add(
1036 tree, &entities[6], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1037 EXPECT_NE(l3b, nullptr);
1038 auto l3c = pldm_entity_association_tree_add(tree, &entities[7], 0xFFFF, l2a,
1039 PLDM_ENTITY_ASSOCIAION_LOGICAL);
1040 EXPECT_NE(l3c, nullptr);
1041 auto l3d = pldm_entity_association_tree_add(tree, &entities[8], 0xFFFF, l2a,
1042 PLDM_ENTITY_ASSOCIAION_LOGICAL);
1043 EXPECT_NE(l3d, nullptr);
1044
1045 auto l4a = pldm_entity_association_tree_add(
1046 tree, &entities[9], 0xFFFF, l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1047 EXPECT_NE(l4a, nullptr);
1048 auto l4b = pldm_entity_association_tree_add(
1049 tree, &entities[10], 0xFFFF, l3b, PLDM_ENTITY_ASSOCIAION_LOGICAL);
1050 EXPECT_NE(l4b, nullptr);
1051
1052 EXPECT_EQ(pldm_entity_get_num_children(l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL),
1053 2);
1054 EXPECT_EQ(pldm_entity_get_num_children(l1, PLDM_ENTITY_ASSOCIAION_LOGICAL),
1055 2);
1056 EXPECT_EQ(
1057 pldm_entity_get_num_children(l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL), 2);
1058 EXPECT_EQ(
1059 pldm_entity_get_num_children(l3b, PLDM_ENTITY_ASSOCIAION_PHYSICAL), 0);
1060 EXPECT_EQ(pldm_entity_get_num_children(l3b, PLDM_ENTITY_ASSOCIAION_LOGICAL),
1061 1);
1062
1063 auto repo = pldm_pdr_init();
1064 pldm_entity_association_pdr_add(tree, repo, false, 1);
1065
1066 EXPECT_EQ(pldm_pdr_get_record_count(repo), 6u);
1067
1068 uint32_t currRecHandle{};
1069 uint32_t nextRecHandle{};
1070 uint8_t* data = nullptr;
1071 uint32_t size{};
1072 uint32_t commonSize = sizeof(struct pldm_pdr_hdr) + sizeof(uint16_t) +
1073 sizeof(uint8_t) + sizeof(pldm_entity) +
1074 sizeof(uint8_t);
1075
1076 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1077 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1078 l1, PLDM_ENTITY_ASSOCIAION_LOGICAL) *
1079 sizeof(pldm_entity)));
1080 uint8_t* start = data;
1081 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
1082 EXPECT_EQ(le32toh(hdr->record_handle), 1u);
1083 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
1084 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
1085 start += sizeof(pldm_pdr_hdr);
1086 uint16_t* containerId = reinterpret_cast<uint16_t*>(start);
1087 EXPECT_EQ(le16toh(*containerId), 1u);
1088 start += sizeof(uint16_t);
1089 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_LOGICAL);
1090 start += sizeof(uint8_t);
1091 pldm_entity* entity = reinterpret_cast<pldm_entity*>(start);
1092 EXPECT_EQ(le16toh(entity->entity_type), 1u);
1093 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1094 EXPECT_EQ(le16toh(entity->entity_container_id), 0u);
1095 start += sizeof(pldm_entity);
1096 EXPECT_EQ(*start,
1097 pldm_entity_get_num_children(l1, PLDM_ENTITY_ASSOCIAION_LOGICAL));
1098 start += sizeof(uint8_t);
1099 entity = reinterpret_cast<pldm_entity*>(start);
1100 EXPECT_EQ(le16toh(entity->entity_type), 3u);
1101 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1102 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
1103 start += sizeof(pldm_entity);
1104 entity = reinterpret_cast<pldm_entity*>(start);
1105 EXPECT_EQ(le16toh(entity->entity_type), 3u);
1106 EXPECT_EQ(le16toh(entity->entity_instance_num), 2u);
1107 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301108
1109 currRecHandle = nextRecHandle;
1110
1111 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1112 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1113 l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL) *
1114 sizeof(pldm_entity)));
1115 start = data;
1116 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
1117 EXPECT_EQ(le32toh(hdr->record_handle), 2u);
1118 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
1119 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
1120 start += sizeof(pldm_pdr_hdr);
1121 containerId = reinterpret_cast<uint16_t*>(start);
1122 EXPECT_EQ(le16toh(*containerId), 1u);
1123 start += sizeof(uint16_t);
1124 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1125 start += sizeof(uint8_t);
1126 entity = reinterpret_cast<pldm_entity*>(start);
1127 EXPECT_EQ(le16toh(entity->entity_type), 1u);
1128 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1129 EXPECT_EQ(le16toh(entity->entity_container_id), 0u);
1130 start += sizeof(pldm_entity);
1131 EXPECT_EQ(*start, pldm_entity_get_num_children(
1132 l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL));
1133 start += sizeof(uint8_t);
1134 entity = reinterpret_cast<pldm_entity*>(start);
1135 EXPECT_EQ(le16toh(entity->entity_type), 2u);
1136 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1137 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
1138 start += sizeof(pldm_entity);
1139 entity = reinterpret_cast<pldm_entity*>(start);
1140 EXPECT_EQ(le16toh(entity->entity_type), 2u);
1141 EXPECT_EQ(le16toh(entity->entity_instance_num), 2u);
1142 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301143
1144 currRecHandle = nextRecHandle;
1145
1146 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1147 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1148 l2a, PLDM_ENTITY_ASSOCIAION_LOGICAL) *
1149 sizeof(pldm_entity)));
1150 start = data;
1151 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
1152 EXPECT_EQ(le32toh(hdr->record_handle), 3u);
1153 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
1154 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
1155 start += sizeof(pldm_pdr_hdr);
1156 containerId = reinterpret_cast<uint16_t*>(start);
1157 EXPECT_EQ(le16toh(*containerId), 2u);
1158 start += sizeof(uint16_t);
1159 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_LOGICAL);
1160 start += sizeof(uint8_t);
1161 entity = reinterpret_cast<pldm_entity*>(start);
1162 EXPECT_EQ(le16toh(entity->entity_type), 2u);
1163 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1164 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
1165 start += sizeof(pldm_entity);
1166 EXPECT_EQ(*start, pldm_entity_get_num_children(
1167 l2a, PLDM_ENTITY_ASSOCIAION_LOGICAL));
1168 start += sizeof(uint8_t);
1169 entity = reinterpret_cast<pldm_entity*>(start);
1170 EXPECT_EQ(le16toh(entity->entity_type), 5);
1171 EXPECT_EQ(le16toh(entity->entity_instance_num), 2u);
1172 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
1173 start += sizeof(pldm_entity);
1174 entity = reinterpret_cast<pldm_entity*>(start);
1175 EXPECT_EQ(le16toh(entity->entity_type), 5u);
1176 EXPECT_EQ(le16toh(entity->entity_instance_num), 3u);
1177 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301178
1179 currRecHandle = nextRecHandle;
1180
1181 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1182 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1183 l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL) *
1184 sizeof(pldm_entity)));
1185 start = data;
1186 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
1187 EXPECT_EQ(le32toh(hdr->record_handle), 4u);
1188 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
1189 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
1190 start += sizeof(pldm_pdr_hdr);
1191 containerId = reinterpret_cast<uint16_t*>(start);
1192 EXPECT_EQ(le16toh(*containerId), 2u);
1193 start += sizeof(uint16_t);
1194 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1195 start += sizeof(uint8_t);
1196 entity = reinterpret_cast<pldm_entity*>(start);
1197 EXPECT_EQ(le16toh(entity->entity_type), 2u);
1198 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1199 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
1200 start += sizeof(pldm_entity);
1201 EXPECT_EQ(*start, pldm_entity_get_num_children(
1202 l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL));
1203 start += sizeof(uint8_t);
1204 entity = reinterpret_cast<pldm_entity*>(start);
1205 EXPECT_EQ(le16toh(entity->entity_type), 4u);
1206 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1207 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
1208 start += sizeof(pldm_entity);
1209 entity = reinterpret_cast<pldm_entity*>(start);
1210 EXPECT_EQ(le16toh(entity->entity_type), 5u);
1211 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1212 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301213
1214 currRecHandle = nextRecHandle;
1215
1216 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1217 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1218 l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL) *
1219 sizeof(pldm_entity)));
1220 start = data;
1221 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
1222 EXPECT_EQ(le32toh(hdr->record_handle), 5u);
1223 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
1224 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
1225 start += sizeof(pldm_pdr_hdr);
1226 containerId = reinterpret_cast<uint16_t*>(start);
1227 EXPECT_EQ(le16toh(*containerId), 3u);
1228 start += sizeof(uint16_t);
1229 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1230 start += sizeof(uint8_t);
1231 entity = reinterpret_cast<pldm_entity*>(start);
1232 EXPECT_EQ(le16toh(entity->entity_type), 4u);
1233 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1234 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
1235 start += sizeof(pldm_entity);
1236 EXPECT_EQ(*start, pldm_entity_get_num_children(
1237 l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL));
1238 start += sizeof(uint8_t);
1239 entity = reinterpret_cast<pldm_entity*>(start);
1240 EXPECT_EQ(le16toh(entity->entity_type), 6u);
1241 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1242 EXPECT_EQ(le16toh(entity->entity_container_id), 3u);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301243
1244 currRecHandle = nextRecHandle;
1245
1246 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1247 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1248 l3b, PLDM_ENTITY_ASSOCIAION_LOGICAL) *
1249 sizeof(pldm_entity)));
1250 start = data;
1251 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
1252 EXPECT_EQ(le32toh(hdr->record_handle), 6u);
1253 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
1254 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
1255 start += sizeof(pldm_pdr_hdr);
1256 containerId = reinterpret_cast<uint16_t*>(start);
1257 EXPECT_EQ(le16toh(*containerId), 4u);
1258 start += sizeof(uint16_t);
1259 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_LOGICAL);
1260 start += sizeof(uint8_t);
1261 entity = reinterpret_cast<pldm_entity*>(start);
1262 EXPECT_EQ(le16toh(entity->entity_type), 5u);
1263 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1264 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
1265 start += sizeof(pldm_entity);
1266 EXPECT_EQ(*start, pldm_entity_get_num_children(
1267 l3b, PLDM_ENTITY_ASSOCIAION_LOGICAL));
1268 start += sizeof(uint8_t);
1269 entity = reinterpret_cast<pldm_entity*>(start);
1270 EXPECT_EQ(le16toh(entity->entity_type), 7u);
1271 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1272 EXPECT_EQ(le16toh(entity->entity_container_id), 4u);
1273
1274 EXPECT_EQ(nextRecHandle, 0u);
1275
1276 pldm_pdr_destroy(repo);
1277 pldm_entity_association_tree_destroy(tree);
1278}
1279
1280TEST(EntityAssociationPDR, testFind)
1281{
1282 // 1
1283 // |
1284 // 2--3--4
1285 // |
1286 // 5--6--7
1287 // | |
1288 // 8 9
1289
1290 pldm_entity entities[9]{};
1291
1292 entities[0].entity_type = 1;
1293 entities[1].entity_type = 2;
1294 entities[2].entity_type = 2;
1295 entities[3].entity_type = 3;
1296 entities[4].entity_type = 4;
1297 entities[5].entity_type = 5;
1298 entities[6].entity_type = 5;
1299 entities[7].entity_type = 6;
1300 entities[8].entity_type = 7;
1301
1302 auto tree = pldm_entity_association_tree_init();
1303
1304 auto l1 = pldm_entity_association_tree_add(
1305 tree, &entities[0], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1306 EXPECT_NE(l1, nullptr);
1307 auto l2a = pldm_entity_association_tree_add(
1308 tree, &entities[1], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1309 EXPECT_NE(l2a, nullptr);
1310 auto l2b = pldm_entity_association_tree_add(
1311 tree, &entities[2], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1312 EXPECT_NE(l2b, nullptr);
1313 auto l2c = pldm_entity_association_tree_add(
1314 tree, &entities[3], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1315 EXPECT_NE(l2c, nullptr);
1316 auto l3a = pldm_entity_association_tree_add(
1317 tree, &entities[4], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1318 EXPECT_NE(l3a, nullptr);
1319 auto l3b = pldm_entity_association_tree_add(
1320 tree, &entities[5], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1321 EXPECT_NE(l3b, nullptr);
1322 auto l3c = pldm_entity_association_tree_add(
1323 tree, &entities[6], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1324 EXPECT_NE(l3c, nullptr);
1325 auto l4a = pldm_entity_association_tree_add(
1326 tree, &entities[7], 0xFFFF, l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1327 EXPECT_NE(l4a, nullptr);
1328 auto l4b = pldm_entity_association_tree_add(
1329 tree, &entities[8], 0xFFFF, l3b, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1330 EXPECT_NE(l4b, nullptr);
1331
1332 pldm_entity entity{};
1333
1334 entity.entity_type = 1;
1335 entity.entity_instance_num = 1;
1336 auto result = pldm_entity_association_tree_find(tree, &entity);
1337 EXPECT_EQ(result, l1);
1338 EXPECT_EQ(entity.entity_container_id, 0);
1339
1340 entity.entity_type = 2;
1341 entity.entity_instance_num = 1;
1342 result = pldm_entity_association_tree_find(tree, &entity);
1343 EXPECT_EQ(result, l2a);
1344 EXPECT_EQ(entity.entity_container_id, 1);
1345 entity.entity_type = 2;
1346 entity.entity_instance_num = 2;
1347 result = pldm_entity_association_tree_find(tree, &entity);
1348 EXPECT_EQ(result, l2b);
1349 EXPECT_EQ(entity.entity_container_id, 1);
1350 entity.entity_type = 3;
1351 entity.entity_instance_num = 1;
1352 result = pldm_entity_association_tree_find(tree, &entity);
1353 EXPECT_EQ(result, l2c);
1354 EXPECT_EQ(entity.entity_container_id, 1);
1355
1356 entity.entity_type = 7;
1357 entity.entity_instance_num = 1;
1358 result = pldm_entity_association_tree_find(tree, &entity);
1359 EXPECT_EQ(result, l4b);
1360 EXPECT_EQ(entity.entity_container_id, 4);
1361
1362 pldm_entity_association_tree_destroy(tree);
1363}
1364
1365TEST(EntityAssociationPDR, testCopyTree)
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 orgTree = pldm_entity_association_tree_init();
1374 auto newTree = pldm_entity_association_tree_init();
1375 auto l1 =
1376 pldm_entity_association_tree_add(orgTree, &entities[0], 0xFFFF, nullptr,
1377 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1378 EXPECT_NE(l1, nullptr);
1379 auto l2a = pldm_entity_association_tree_add(
1380 orgTree, &entities[1], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1381 EXPECT_NE(l2a, nullptr);
1382 auto l2b = pldm_entity_association_tree_add(
1383 orgTree, &entities[2], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1384 EXPECT_NE(l2b, nullptr);
1385 auto l2c = pldm_entity_association_tree_add(
1386 orgTree, &entities[3], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1387 EXPECT_NE(l2c, nullptr);
1388 size_t orgNum{};
1389 pldm_entity* orgOut = nullptr;
1390 pldm_entity_association_tree_visit(orgTree, &orgOut, &orgNum);
1391 EXPECT_EQ(orgNum, 4u);
1392
1393 pldm_entity_association_tree_copy_root(orgTree, newTree);
1394 size_t newNum{};
1395 pldm_entity* newOut = nullptr;
1396 pldm_entity_association_tree_visit(newTree, &newOut, &newNum);
1397 EXPECT_EQ(newNum, orgNum);
1398 EXPECT_EQ(newOut[0].entity_type, 1u);
1399 EXPECT_EQ(newOut[0].entity_instance_num, 1u);
1400 EXPECT_EQ(newOut[0].entity_container_id, 0u);
1401 free(orgOut);
1402 free(newOut);
1403 pldm_entity_association_tree_destroy(orgTree);
1404 pldm_entity_association_tree_destroy(newTree);
1405}
1406
1407TEST(EntityAssociationPDR, testExtract)
1408{
1409 std::vector<uint8_t> pdr{};
1410 pdr.resize(sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_entity_association) +
1411 sizeof(pldm_entity) * 4);
1412 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(pdr.data());
1413 hdr->type = PLDM_PDR_ENTITY_ASSOCIATION;
1414 hdr->length =
1415 htole16(sizeof(pldm_pdr_entity_association) + sizeof(pldm_entity) * 4);
1416
1417 pldm_pdr_entity_association* e =
1418 reinterpret_cast<pldm_pdr_entity_association*>(pdr.data() +
1419 sizeof(pldm_pdr_hdr));
1420 e->container_id = htole16(1);
1421 e->num_children = 5;
1422 e->container.entity_type = htole16(1);
1423 e->container.entity_instance_num = htole16(1);
1424 e->container.entity_container_id = htole16(0);
1425
1426 pldm_entity* entity = e->children;
1427 entity->entity_type = htole16(2);
1428 entity->entity_instance_num = htole16(1);
1429 entity->entity_container_id = htole16(1);
1430 ++entity;
1431 entity->entity_type = htole16(3);
1432 entity->entity_instance_num = htole16(1);
1433 entity->entity_container_id = htole16(1);
1434 ++entity;
1435 entity->entity_type = htole16(4);
1436 entity->entity_instance_num = htole16(1);
1437 entity->entity_container_id = htole16(1);
1438 ++entity;
1439 entity->entity_type = htole16(5);
1440 entity->entity_instance_num = htole16(1);
1441 entity->entity_container_id = htole16(1);
1442 ++entity;
1443 entity->entity_type = htole16(6);
1444 entity->entity_instance_num = htole16(1);
1445 entity->entity_container_id = htole16(1);
1446
1447 size_t num{};
1448 pldm_entity* out = nullptr;
1449 pldm_entity_association_pdr_extract(pdr.data(), pdr.size(), &num, &out);
1450 EXPECT_EQ(num, (unsigned)e->num_children + 1);
1451 EXPECT_EQ(out[0].entity_type, 1u);
1452 EXPECT_EQ(out[0].entity_instance_num, 1u);
1453 EXPECT_EQ(out[0].entity_container_id, 0u);
1454 EXPECT_EQ(out[1].entity_type, 2u);
1455 EXPECT_EQ(out[1].entity_instance_num, 1u);
1456 EXPECT_EQ(out[1].entity_container_id, 1u);
1457 EXPECT_EQ(out[2].entity_type, 3u);
1458 EXPECT_EQ(out[2].entity_instance_num, 1u);
1459 EXPECT_EQ(out[2].entity_container_id, 1u);
1460 EXPECT_EQ(out[3].entity_type, 4u);
1461 EXPECT_EQ(out[3].entity_instance_num, 1u);
1462 EXPECT_EQ(out[3].entity_container_id, 1u);
1463 EXPECT_EQ(out[4].entity_type, 5u);
1464 EXPECT_EQ(out[4].entity_instance_num, 1u);
1465 EXPECT_EQ(out[4].entity_container_id, 1u);
1466 EXPECT_EQ(out[5].entity_type, 6u);
1467 EXPECT_EQ(out[5].entity_instance_num, 1u);
1468 EXPECT_EQ(out[5].entity_container_id, 1u);
1469
1470 free(out);
1471}
1472
1473TEST(EntityAssociationPDR, testGetChildren)
1474{
1475 pldm_entity entities[4]{};
1476 entities[0].entity_type = 1;
1477 entities[1].entity_type = 2;
1478 entities[2].entity_type = 2;
1479 entities[3].entity_type = 3;
1480
1481 auto tree = pldm_entity_association_tree_init();
1482 auto l1 = pldm_entity_association_tree_add(
1483 tree, &entities[0], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1484 EXPECT_NE(l1, nullptr);
1485 auto l2a = pldm_entity_association_tree_add(
1486 tree, &entities[1], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1487 EXPECT_NE(l2a, nullptr);
1488 auto l2b = pldm_entity_association_tree_add(
1489 tree, &entities[2], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1490 EXPECT_NE(l2b, nullptr);
1491 auto l2c = pldm_entity_association_tree_add(
1492 tree, &entities[3], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1493 EXPECT_NE(l2c, nullptr);
1494
1495 pldm_entity et1;
1496 et1.entity_type = 2;
1497 et1.entity_instance_num = 1;
1498 EXPECT_EQ(true, pldm_is_current_parent_child(l1, &et1));
1499
1500 pldm_entity et2;
1501 et2.entity_type = 2;
1502 et2.entity_instance_num = 2;
1503 EXPECT_EQ(true, pldm_is_current_parent_child(l1, &et2));
1504
1505 pldm_entity et3;
1506 et3.entity_type = 2;
1507 et3.entity_instance_num = 3;
1508 EXPECT_EQ(false, pldm_is_current_parent_child(l1, &et3));
1509
1510 pldm_entity_association_tree_destroy(tree);
1511}
1512
1513TEST(EntityAssociationPDR, testEntityInstanceNumber)
1514{
1515 pldm_entity entities[9]{};
1516
1517 entities[0].entity_type = 1;
1518 entities[1].entity_type = 2;
1519 entities[2].entity_type = 2;
1520 entities[3].entity_type = 2;
1521 entities[4].entity_type = 2;
1522 entities[5].entity_type = 2;
1523 entities[6].entity_type = 2;
1524 entities[7].entity_type = 3;
1525 entities[8].entity_type = 3;
1526
1527 auto tree = pldm_entity_association_tree_init();
1528 auto repo = pldm_pdr_init();
1529
1530 uint16_t terminusHdl{};
1531 uint16_t entityType{};
1532 uint16_t entityInstanceNum{};
1533 uint16_t containerId{};
1534
1535 auto node = pldm_entity_association_tree_add(
1536 tree, &entities[0], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1537 EXPECT_NE(node, nullptr);
1538
1539 auto l1 = pldm_entity_association_tree_add(tree, &entities[1], 63, node,
1540 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1541 auto first = pldm_pdr_add_fru_record_set(
1542 repo, 1, 1, entities[1].entity_type, entities[1].entity_instance_num,
1543 entities[1].entity_container_id, 1);
1544 EXPECT_NE(l1, nullptr);
1545 EXPECT_EQ(entities[1].entity_instance_num, 63);
1546 EXPECT_EQ(first, pldm_pdr_get_record_handle(
1547 repo, pldm_pdr_fru_record_set_find_by_rsi(
1548 repo, 1, &terminusHdl, &entityType,
1549 &entityInstanceNum, &containerId)));
1550 EXPECT_EQ(entityType, 2);
1551 EXPECT_EQ(entityInstanceNum, 63);
1552
1553 auto l2 = pldm_entity_association_tree_add(tree, &entities[2], 37, node,
1554 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1555 auto second = pldm_pdr_add_fru_record_set(
1556 repo, 1, 2, entities[2].entity_type, entities[2].entity_instance_num,
1557 entities[2].entity_container_id, 2);
1558 EXPECT_NE(l2, nullptr);
1559 EXPECT_EQ(entities[2].entity_instance_num, 37);
1560 EXPECT_EQ(second, pldm_pdr_get_record_handle(
1561 repo, pldm_pdr_fru_record_set_find_by_rsi(
1562 repo, 2, &terminusHdl, &entityType,
1563 &entityInstanceNum, &containerId)));
1564 EXPECT_EQ(entityType, 2);
1565 EXPECT_EQ(entityInstanceNum, 37);
1566
1567 auto l3 = pldm_entity_association_tree_add(tree, &entities[3], 44, node,
1568 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1569 auto third = pldm_pdr_add_fru_record_set(
1570 repo, 1, 3, entities[3].entity_type, entities[3].entity_instance_num,
1571 entities[3].entity_container_id, 3);
1572 EXPECT_NE(l3, nullptr);
1573 EXPECT_EQ(entities[3].entity_instance_num, 44);
1574 EXPECT_EQ(third, pldm_pdr_get_record_handle(
1575 repo, pldm_pdr_fru_record_set_find_by_rsi(
1576 repo, 3, &terminusHdl, &entityType,
1577 &entityInstanceNum, &containerId)));
1578 EXPECT_EQ(entityType, 2);
1579 EXPECT_EQ(entityInstanceNum, 44);
1580
1581 auto l4 = pldm_entity_association_tree_add(tree, &entities[4], 89, node,
1582 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1583 auto fourth = pldm_pdr_add_fru_record_set(
1584 repo, 1, 4, entities[4].entity_type, entities[4].entity_instance_num,
1585 entities[4].entity_container_id, 4);
1586 EXPECT_NE(l4, nullptr);
1587 EXPECT_EQ(entities[4].entity_instance_num, 89);
1588 EXPECT_EQ(fourth, pldm_pdr_get_record_handle(
1589 repo, pldm_pdr_fru_record_set_find_by_rsi(
1590 repo, 4, &terminusHdl, &entityType,
1591 &entityInstanceNum, &containerId)));
1592 EXPECT_EQ(entityType, 2);
1593 EXPECT_EQ(entityInstanceNum, 89);
1594
1595 auto l5 = pldm_entity_association_tree_add(tree, &entities[5], 0xFFFF, node,
1596 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1597 auto fifth = pldm_pdr_add_fru_record_set(
1598 repo, 1, 5, entities[5].entity_type, entities[5].entity_instance_num,
1599 entities[5].entity_container_id, 5);
1600 EXPECT_NE(l5, nullptr);
1601 EXPECT_EQ(entities[5].entity_instance_num, 90);
1602 EXPECT_EQ(fifth, pldm_pdr_get_record_handle(
1603 repo, pldm_pdr_fru_record_set_find_by_rsi(
1604 repo, 5, &terminusHdl, &entityType,
1605 &entityInstanceNum, &containerId)));
1606 EXPECT_EQ(entityType, 2);
1607 EXPECT_EQ(entityInstanceNum, 90);
1608
1609 auto l6 = pldm_entity_association_tree_add(tree, &entities[6], 90, node,
1610 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1611 EXPECT_EQ(l6, nullptr);
1612
1613 auto l7 = pldm_entity_association_tree_add(tree, &entities[7], 100, l1,
1614 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1615 auto seventh = pldm_pdr_add_fru_record_set(
1616 repo, 1, 7, entities[7].entity_type, entities[7].entity_instance_num,
1617 entities[7].entity_container_id, 7);
1618 EXPECT_NE(l7, nullptr);
1619 EXPECT_EQ(entities[7].entity_instance_num, 100);
1620 EXPECT_EQ(seventh, pldm_pdr_get_record_handle(
1621 repo, pldm_pdr_fru_record_set_find_by_rsi(
1622 repo, 7, &terminusHdl, &entityType,
1623 &entityInstanceNum, &containerId)));
1624 EXPECT_EQ(entityType, 3);
1625 EXPECT_EQ(entityInstanceNum, 100);
1626
1627 auto l8 = pldm_entity_association_tree_add(tree, &entities[8], 100, l2,
1628 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1629 auto eighth = pldm_pdr_add_fru_record_set(
1630 repo, 1, 8, entities[8].entity_type, entities[8].entity_instance_num,
1631 entities[8].entity_container_id, 8);
1632 EXPECT_NE(l8, nullptr);
1633 EXPECT_EQ(entities[8].entity_instance_num, 100);
1634 EXPECT_EQ(eighth, pldm_pdr_get_record_handle(
1635 repo, pldm_pdr_fru_record_set_find_by_rsi(
1636 repo, 8, &terminusHdl, &entityType,
1637 &entityInstanceNum, &containerId)));
1638 EXPECT_EQ(entityType, 3);
1639 EXPECT_EQ(entityInstanceNum, 100);
1640
1641 pldm_pdr_destroy(repo);
1642 pldm_entity_association_tree_destroy(tree);
1643}
Pavithra Barithaya5dc02572023-05-19 09:24:36 -05001644
Andrew Jeffery14a5fe82023-06-26 15:31:37 +09301645#ifdef LIBPLDM_API_TESTING
Pavithra Barithaya5dc02572023-05-19 09:24:36 -05001646TEST(EntityAssociationPDR, testFindContainerID)
1647{
1648 pldm_entity entities[3]{};
1649 entities[0].entity_type = 1;
1650 entities[1].entity_type = 2;
1651 entities[2].entity_type = 3;
1652 entities[1].entity_container_id = 2;
1653 entities[1].entity_instance_num = 1;
1654
1655 auto tree = pldm_entity_association_tree_init();
1656 auto l1 = pldm_entity_association_tree_add_entity(
1657 tree, &entities[0], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_LOGICAL,
1658 false, true, 0xFFFF);
1659
1660 EXPECT_NE(l1, nullptr);
1661 auto l2 = pldm_entity_association_tree_add_entity(
1662 tree, &entities[1], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
1663 false, 0xFFFF);
1664 EXPECT_NE(l2, nullptr);
1665 auto l3 = pldm_entity_association_tree_add_entity(
1666 tree, &entities[2], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
1667 true, 0xFFFF);
1668 EXPECT_NE(l3, nullptr);
1669
1670 EXPECT_EQ(pldm_entity_get_num_children(l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL),
1671 2);
1672
1673 auto repo = pldm_pdr_init();
1674 pldm_entity_association_pdr_add(tree, repo, false, 1);
1675
1676 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
1677
1678 uint16_t container_id{};
1679 pldm_pdr_find_container_id_range_exclude(repo, 1, 1, 0x01000000, 0x01FFFFFF,
1680 &container_id);
1681 EXPECT_EQ(container_id, 2);
1682
1683 uint16_t container_id1{};
1684 pldm_pdr_find_container_id_range_exclude(repo, 1, 1, 0x00000001, 0x00FFFFFF,
1685 &container_id1);
1686 EXPECT_EQ(container_id1, 0);
1687
1688 pldm_pdr_destroy(repo);
1689 pldm_entity_association_tree_destroy(tree);
1690}
Andrew Jeffery14a5fe82023-06-26 15:31:37 +09301691#endif