xref: /openbmc/libpldm/tests/dsp/pdr.cpp (revision 9e566597)
1 #include <endian.h>
2 #include <libpldm/pdr.h>
3 #include <libpldm/platform.h>
4 
5 #include <array>
6 #include <cstdint>
7 #include <cstdlib>
8 #include <cstring>
9 #include <vector>
10 
11 #include <gtest/gtest.h>
12 
TEST(PDRAccess,testInit)13 TEST(PDRAccess, testInit)
14 {
15     auto repo = pldm_pdr_init();
16     EXPECT_EQ(pldm_pdr_get_record_count(repo), 0u);
17     EXPECT_EQ(pldm_pdr_get_repo_size(repo), 0u);
18     pldm_pdr_destroy(repo);
19 }
20 
TEST(PDRUpdate,testAdd)21 TEST(PDRUpdate, testAdd)
22 {
23     auto repo = pldm_pdr_init();
24 
25     std::array<uint8_t, 10> data{};
26     uint32_t handle = 0;
27     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, &handle),
28               0);
29     EXPECT_EQ(handle, 1u);
30     EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
31     EXPECT_EQ(pldm_pdr_get_repo_size(repo), data.size());
32 
33     handle = 0;
34     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, &handle),
35               0);
36     EXPECT_EQ(handle, 2u);
37 
38     handle = 0;
39     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, &handle),
40               0);
41     EXPECT_EQ(handle, 3u);
42 
43     handle = 0xdeeddeedu;
44     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, &handle),
45               0);
46     EXPECT_EQ(handle, 0xdeeddeed);
47     EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
48     EXPECT_EQ(pldm_pdr_get_repo_size(repo), data.size() * 4u);
49 
50     pldm_pdr_destroy(repo);
51 }
52 
TEST(PDRRemoveByTerminus,testRemoveByTerminus)53 TEST(PDRRemoveByTerminus, testRemoveByTerminus)
54 {
55     std::array<uint8_t, 10> data{};
56 
57     auto repo = pldm_pdr_init();
58 
59     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, NULL), 0);
60     pldm_pdr_remove_pdrs_by_terminus_handle(repo, 1);
61     EXPECT_EQ(pldm_pdr_get_record_count(repo), 0u);
62     pldm_pdr_destroy(repo);
63 
64     repo = pldm_pdr_init();
65     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 2, NULL), 0);
66     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 1, NULL), 0);
67     EXPECT_EQ(pldm_pdr_get_record_count(repo), 2u);
68     pldm_pdr_remove_pdrs_by_terminus_handle(repo, 1);
69     EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
70     pldm_pdr_destroy(repo);
71 
72     repo = pldm_pdr_init();
73     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 1, NULL), 0);
74     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 2, NULL), 0);
75     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 2, NULL), 0);
76     EXPECT_EQ(pldm_pdr_get_record_count(repo), 3u);
77     pldm_pdr_remove_pdrs_by_terminus_handle(repo, 2);
78     EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
79     pldm_pdr_destroy(repo);
80 
81     repo = pldm_pdr_init();
82     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 1, NULL), 0);
83     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 2, NULL), 0);
84     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 1, NULL), 0);
85     EXPECT_EQ(pldm_pdr_get_record_count(repo), 3u);
86     pldm_pdr_remove_remote_pdrs(repo);
87     EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
88     pldm_pdr_remove_pdrs_by_terminus_handle(repo, 1);
89     EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
90     pldm_pdr_remove_pdrs_by_terminus_handle(repo, 2);
91     EXPECT_EQ(pldm_pdr_get_record_count(repo), 0u);
92     pldm_pdr_destroy(repo);
93 }
94 
TEST(PDRUpdate,testRemove)95 TEST(PDRUpdate, testRemove)
96 {
97     std::array<uint8_t, 10> data{};
98 
99     auto repo = pldm_pdr_init();
100     pldm_pdr_remove_remote_pdrs(repo);
101     EXPECT_EQ(pldm_pdr_get_record_count(repo), 0u);
102     pldm_pdr_destroy(repo);
103 
104     repo = pldm_pdr_init();
105     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 1, NULL), 0);
106     pldm_pdr_remove_remote_pdrs(repo);
107     EXPECT_EQ(pldm_pdr_get_record_count(repo), 0u);
108     pldm_pdr_destroy(repo);
109 
110     repo = pldm_pdr_init();
111     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, NULL), 0);
112     pldm_pdr_remove_remote_pdrs(repo);
113     EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
114     pldm_pdr_destroy(repo);
115 
116     repo = pldm_pdr_init();
117     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, NULL), 0);
118     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 1, NULL), 0);
119     pldm_pdr_remove_remote_pdrs(repo);
120     EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
121     pldm_pdr_destroy(repo);
122 
123     repo = pldm_pdr_init();
124     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 1, NULL), 0);
125     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, NULL), 0);
126     pldm_pdr_remove_remote_pdrs(repo);
127     EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
128     pldm_pdr_destroy(repo);
129 
130     repo = pldm_pdr_init();
131     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 1, NULL), 0);
132     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 1, NULL), 0);
133     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 1, NULL), 0);
134     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 1, NULL), 0);
135     pldm_pdr_remove_remote_pdrs(repo);
136     EXPECT_EQ(pldm_pdr_get_record_count(repo), 0u);
137     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 1, NULL), 0);
138     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, NULL), 0);
139     EXPECT_EQ(pldm_pdr_get_record_count(repo), 2u);
140     pldm_pdr_destroy(repo);
141 
142     repo = pldm_pdr_init();
143     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, NULL), 0);
144     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, NULL), 0);
145     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, NULL), 0);
146     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, NULL), 0);
147     pldm_pdr_remove_remote_pdrs(repo);
148     EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
149     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 1, NULL), 0);
150     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, NULL), 0);
151     EXPECT_EQ(pldm_pdr_get_record_count(repo), 6u);
152     pldm_pdr_destroy(repo);
153 
154     repo = pldm_pdr_init();
155     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 1, NULL), 0);
156     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, NULL), 0);
157     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, NULL), 0);
158     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, NULL), 0);
159     pldm_pdr_remove_remote_pdrs(repo);
160     EXPECT_EQ(pldm_pdr_get_record_count(repo), 3u);
161     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 1, NULL), 0);
162     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, NULL), 0);
163     EXPECT_EQ(pldm_pdr_get_record_count(repo), 5u);
164     pldm_pdr_destroy(repo);
165 
166     repo = pldm_pdr_init();
167     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, NULL), 0);
168     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, NULL), 0);
169     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, NULL), 0);
170     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 1, NULL), 0);
171     pldm_pdr_remove_remote_pdrs(repo);
172     EXPECT_EQ(pldm_pdr_get_record_count(repo), 3u);
173     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 1, NULL), 0);
174     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, NULL), 0);
175     EXPECT_EQ(pldm_pdr_get_record_count(repo), 5u);
176     pldm_pdr_destroy(repo);
177 
178     repo = pldm_pdr_init();
179     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 1, NULL), 0);
180     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, NULL), 0);
181     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 1, NULL), 0);
182     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, NULL), 0);
183     pldm_pdr_remove_remote_pdrs(repo);
184     EXPECT_EQ(pldm_pdr_get_record_count(repo), 2u);
185     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, NULL), 0);
186     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 1, NULL), 0);
187     EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
188     pldm_pdr_destroy(repo);
189 
190     repo = pldm_pdr_init();
191     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, NULL), 0);
192     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, NULL), 0);
193     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, NULL), 0);
194     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 1, NULL), 0);
195     pldm_pdr_remove_remote_pdrs(repo);
196     EXPECT_EQ(pldm_pdr_get_record_count(repo), 3u);
197     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, NULL), 0);
198     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 1, NULL), 0);
199     EXPECT_EQ(pldm_pdr_get_record_count(repo), 5u);
200     pldm_pdr_destroy(repo);
201 
202     repo = pldm_pdr_init();
203     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, NULL), 0);
204     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 1, NULL), 0);
205     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, NULL), 0);
206     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 1, NULL), 0);
207     pldm_pdr_remove_remote_pdrs(repo);
208     EXPECT_EQ(pldm_pdr_get_record_count(repo), 2u);
209     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, NULL), 0);
210     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 1, NULL), 0);
211     EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
212     pldm_pdr_destroy(repo);
213 
214     repo = pldm_pdr_init();
215     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 1, NULL), 0);
216     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 1, NULL), 0);
217     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 1, NULL), 0);
218     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, NULL), 0);
219     pldm_pdr_remove_remote_pdrs(repo);
220     EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
221     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, NULL), 0);
222     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 1, NULL), 0);
223     EXPECT_EQ(pldm_pdr_get_record_count(repo), 3u);
224     pldm_pdr_destroy(repo);
225 
226     repo = pldm_pdr_init();
227     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, NULL), 0);
228     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 1, NULL), 0);
229     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 1, NULL), 0);
230     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 1, NULL), 0);
231     pldm_pdr_remove_remote_pdrs(repo);
232     EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
233     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, NULL), 0);
234     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 1, NULL), 0);
235     EXPECT_EQ(pldm_pdr_get_record_count(repo), 3u);
236     pldm_pdr_destroy(repo);
237 
238     repo = pldm_pdr_init();
239     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, NULL), 0);
240     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 1, NULL), 0);
241     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 1, NULL), 0);
242     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, NULL), 0);
243     pldm_pdr_remove_remote_pdrs(repo);
244     EXPECT_EQ(pldm_pdr_get_record_count(repo), 2u);
245     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, NULL), 0);
246     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 1, NULL), 0);
247     EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
248     pldm_pdr_destroy(repo);
249 
250     repo = pldm_pdr_init();
251     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 1, NULL), 0);
252     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, NULL), 0);
253     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, NULL), 0);
254     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 1, NULL), 0);
255     pldm_pdr_remove_remote_pdrs(repo);
256     EXPECT_EQ(pldm_pdr_get_record_count(repo), 2u);
257     uint32_t handle = 0;
258     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, &handle),
259               0);
260     EXPECT_EQ(handle, 3u);
261     handle = 0;
262     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 1, &handle),
263               0);
264     EXPECT_EQ(handle, 4u);
265     EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
266     pldm_pdr_destroy(repo);
267 }
268 
TEST(PDRAccess,testGet)269 TEST(PDRAccess, testGet)
270 {
271     auto repo = pldm_pdr_init();
272 
273     std::array<uint32_t, 10> in{100, 345, 3, 6, 89, 0, 11, 45, 23434, 123123};
274     uint32_t handle = 1;
275     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
276     EXPECT_EQ(pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in.data()),
277                            sizeof(in), false, 1, &handle),
278               0);
279     EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
280     EXPECT_EQ(pldm_pdr_get_repo_size(repo), sizeof(in));
281     uint32_t size{};
282     uint32_t nextRecHdl{};
283     uint8_t* outData = nullptr;
284     auto hdl = pldm_pdr_find_record(repo, 0, &outData, &size, &nextRecHdl);
285     EXPECT_NE(hdl, nullptr);
286     EXPECT_EQ(size, sizeof(in));
287     EXPECT_EQ(nextRecHdl, 0u);
288     EXPECT_EQ(memcmp(outData, in.data(), sizeof(in)), 0);
289     outData = nullptr;
290 
291     auto hdl2 = pldm_pdr_find_record(repo, 1, &outData, &size, &nextRecHdl);
292     EXPECT_EQ(hdl, hdl2);
293     EXPECT_NE(hdl, nullptr);
294     EXPECT_EQ(size, sizeof(in));
295     EXPECT_EQ(nextRecHdl, 0u);
296     EXPECT_EQ(memcmp(outData, in.data(), sizeof(in)), 0);
297     outData = nullptr;
298 
299     hdl = pldm_pdr_find_record(repo, htole32(0xdeaddead), &outData, &size,
300                                &nextRecHdl);
301     EXPECT_EQ(hdl, nullptr);
302     EXPECT_EQ(size, 0u);
303     EXPECT_EQ(nextRecHdl, 0u);
304     EXPECT_EQ(outData, nullptr);
305     outData = nullptr;
306 
307     std::array<uint32_t, 10> in2{1000, 3450, 30,  60,     890,
308                                  0,    110,  450, 234034, 123123};
309     handle = 2;
310     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
311     EXPECT_EQ(pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in2.data()),
312                            sizeof(in2), false, 1, &handle),
313               0);
314     handle = 3;
315     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
316     EXPECT_EQ(pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in2.data()),
317                            sizeof(in2), false, 1, &handle),
318               0);
319     handle = 4;
320     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
321     EXPECT_EQ(pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in2.data()),
322                            sizeof(in2), true, 1, &handle),
323               0);
324     EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
325     EXPECT_EQ(pldm_pdr_get_repo_size(repo), sizeof(in2) * 4);
326     hdl = pldm_pdr_find_record(repo, 0, &outData, &size, &nextRecHdl);
327     EXPECT_NE(hdl, nullptr);
328     EXPECT_EQ(size, sizeof(in));
329     EXPECT_EQ(nextRecHdl, 2u);
330     EXPECT_EQ(memcmp(outData, in.data(), sizeof(in)), 0);
331     outData = nullptr;
332     hdl2 = pldm_pdr_find_record(repo, 1, &outData, &size, &nextRecHdl);
333     EXPECT_EQ(hdl, hdl2);
334     EXPECT_NE(hdl, nullptr);
335     EXPECT_EQ(size, sizeof(in));
336     EXPECT_EQ(nextRecHdl, 2u);
337     EXPECT_EQ(memcmp(outData, in.data(), sizeof(in)), 0);
338     outData = nullptr;
339     hdl = pldm_pdr_find_record(repo, 2, &outData, &size, &nextRecHdl);
340     EXPECT_NE(hdl, nullptr);
341     EXPECT_EQ(size, sizeof(in2));
342     EXPECT_EQ(nextRecHdl, 3u);
343     EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
344     outData = nullptr;
345     hdl = pldm_pdr_find_record(repo, 3, &outData, &size, &nextRecHdl);
346     EXPECT_NE(hdl, nullptr);
347     EXPECT_EQ(pldm_pdr_record_is_remote(hdl), false);
348     EXPECT_EQ(size, sizeof(in2));
349     EXPECT_EQ(nextRecHdl, 4u);
350     EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
351     outData = nullptr;
352     hdl = pldm_pdr_find_record(repo, 4, &outData, &size, &nextRecHdl);
353     EXPECT_NE(hdl, nullptr);
354     EXPECT_EQ(pldm_pdr_record_is_remote(hdl), true);
355     EXPECT_EQ(size, sizeof(in2));
356     EXPECT_EQ(nextRecHdl, 0u);
357     EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
358     outData = nullptr;
359 
360     pldm_pdr_destroy(repo);
361 }
362 
TEST(PDRAccess,testGetNext)363 TEST(PDRAccess, testGetNext)
364 {
365     auto repo = pldm_pdr_init();
366 
367     std::array<uint32_t, 10> in{100, 345, 3, 6, 89, 0, 11, 45, 23434, 123123};
368     uint32_t handle = 1;
369     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
370     EXPECT_EQ(pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in.data()),
371                            sizeof(in), false, 1, &handle),
372               0);
373     EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
374     EXPECT_EQ(pldm_pdr_get_repo_size(repo), sizeof(in));
375     uint32_t size{};
376     uint32_t nextRecHdl{};
377     uint8_t* outData = nullptr;
378     auto hdl = pldm_pdr_find_record(repo, 0, &outData, &size, &nextRecHdl);
379     EXPECT_NE(hdl, nullptr);
380     EXPECT_EQ(size, sizeof(in));
381     EXPECT_EQ(nextRecHdl, 0u);
382     EXPECT_EQ(memcmp(outData, in.data(), sizeof(in)), 0);
383     outData = nullptr;
384 
385     std::array<uint32_t, 10> in2{1000, 3450, 30,  60,     890,
386                                  0,    110,  450, 234034, 123123};
387     handle = 2;
388     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
389     EXPECT_EQ(pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in2.data()),
390                            sizeof(in2), false, 1, &handle),
391               0);
392     handle = 3;
393     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
394     EXPECT_EQ(pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in2.data()),
395                            sizeof(in2), false, 1, &handle),
396               0);
397     handle = 4;
398     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
399     EXPECT_EQ(pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in2.data()),
400                            sizeof(in2), false, 1, &handle),
401               0);
402     EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
403     EXPECT_EQ(pldm_pdr_get_repo_size(repo), sizeof(in2) * 4);
404     hdl = pldm_pdr_get_next_record(repo, hdl, &outData, &size, &nextRecHdl);
405     EXPECT_NE(hdl, nullptr);
406     EXPECT_EQ(size, sizeof(in2));
407     EXPECT_EQ(nextRecHdl, 3u);
408     EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
409     outData = nullptr;
410     hdl = pldm_pdr_get_next_record(repo, hdl, &outData, &size, &nextRecHdl);
411     EXPECT_NE(hdl, nullptr);
412     EXPECT_EQ(size, sizeof(in2));
413     EXPECT_EQ(nextRecHdl, 4u);
414     EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
415     outData = nullptr;
416     hdl = pldm_pdr_get_next_record(repo, hdl, &outData, &size, &nextRecHdl);
417     EXPECT_NE(hdl, nullptr);
418     EXPECT_EQ(size, sizeof(in2));
419     EXPECT_EQ(nextRecHdl, 0u);
420     EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
421     outData = nullptr;
422 
423     pldm_pdr_destroy(repo);
424 }
425 
TEST(PDRAccess,testFindByType)426 TEST(PDRAccess, testFindByType)
427 {
428     auto repo = pldm_pdr_init();
429 
430     std::array<uint8_t, sizeof(pldm_pdr_hdr)> data{};
431     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
432     pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(data.data());
433     hdr->type = 1;
434     uint32_t first = 0;
435     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, &first),
436               0);
437     hdr->type = 2;
438     uint32_t second = 0;
439     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, &second),
440               0);
441     hdr->type = 3;
442     uint32_t third = 0;
443     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, &third),
444               0);
445     hdr->type = 4;
446     uint32_t fourth = 0;
447     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, &fourth),
448               0);
449 
450     uint8_t* outData = nullptr;
451     uint32_t size{};
452     auto firstRec =
453         pldm_pdr_find_record_by_type(repo, 1, nullptr, &outData, &size);
454     EXPECT_EQ(pldm_pdr_get_record_handle(repo, firstRec), first);
455     outData = nullptr;
456     auto secondRec =
457         pldm_pdr_find_record_by_type(repo, 2, nullptr, &outData, &size);
458     EXPECT_EQ(pldm_pdr_get_record_handle(repo, secondRec), second);
459     outData = nullptr;
460     auto thirdRec =
461         pldm_pdr_find_record_by_type(repo, 3, nullptr, &outData, &size);
462     EXPECT_EQ(pldm_pdr_get_record_handle(repo, thirdRec), third);
463     outData = nullptr;
464     auto fourthRec =
465         pldm_pdr_find_record_by_type(repo, 4, nullptr, &outData, &size);
466     EXPECT_EQ(pldm_pdr_get_record_handle(repo, fourthRec), fourth);
467     outData = nullptr;
468     auto fifthRec =
469         pldm_pdr_find_record_by_type(repo, 5, nullptr, &outData, &size);
470     EXPECT_EQ(fifthRec, nullptr);
471     EXPECT_EQ(outData, nullptr);
472     EXPECT_EQ(size, 0u);
473 
474     auto rec =
475         pldm_pdr_find_record_by_type(repo, 3, secondRec, &outData, &size);
476     EXPECT_EQ(pldm_pdr_get_record_handle(repo, rec), third);
477     outData = nullptr;
478     rec = pldm_pdr_find_record_by_type(repo, 4, secondRec, &outData, &size);
479     EXPECT_EQ(pldm_pdr_get_record_handle(repo, rec), fourth);
480     outData = nullptr;
481     rec = pldm_pdr_find_record_by_type(repo, 2, firstRec, &outData, &size);
482     EXPECT_EQ(pldm_pdr_get_record_handle(repo, rec), second);
483     outData = nullptr;
484 
485     pldm_pdr_destroy(repo);
486 }
487 
TEST(PDRUpdate,testAddFruRecordSet)488 TEST(PDRUpdate, testAddFruRecordSet)
489 {
490     auto repo = pldm_pdr_init();
491 
492     uint32_t handle = 0;
493     EXPECT_EQ(pldm_pdr_add_fru_record_set(repo, 1, 10, 1, 0, 100, &handle), 0);
494     EXPECT_EQ(handle, 1u);
495     EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
496     EXPECT_EQ(pldm_pdr_get_repo_size(repo),
497               sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_fru_record_set));
498     uint32_t size{};
499     uint32_t nextRecHdl{};
500     uint8_t* outData = nullptr;
501     auto hdl = pldm_pdr_find_record(repo, 0, &outData, &size, &nextRecHdl);
502     EXPECT_NE(hdl, nullptr);
503     EXPECT_EQ(size, sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_fru_record_set));
504     EXPECT_EQ(nextRecHdl, 0u);
505     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
506     pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(outData);
507     EXPECT_EQ(hdr->version, 1u);
508     EXPECT_EQ(hdr->type, PLDM_PDR_FRU_RECORD_SET);
509     EXPECT_EQ(hdr->length, htole16(sizeof(pldm_pdr_fru_record_set)));
510     EXPECT_EQ(hdr->record_handle, htole32(1));
511     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
512     pldm_pdr_fru_record_set* fru = reinterpret_cast<pldm_pdr_fru_record_set*>(
513         outData + sizeof(pldm_pdr_hdr));
514     EXPECT_EQ(fru->terminus_handle, htole16(1));
515     EXPECT_EQ(fru->fru_rsi, htole16(10));
516     EXPECT_EQ(fru->entity_type, htole16(1));
517     EXPECT_EQ(fru->entity_instance_num, htole16(0));
518     EXPECT_EQ(fru->container_id, htole16(100));
519     outData = nullptr;
520 
521     handle = 0;
522     EXPECT_EQ(pldm_pdr_add_fru_record_set(repo, 2, 11, 2, 1, 101, &handle), 0);
523     EXPECT_EQ(handle, 2u);
524     EXPECT_EQ(pldm_pdr_get_record_count(repo), 2u);
525     EXPECT_EQ(pldm_pdr_get_repo_size(repo),
526               2 * (sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_fru_record_set)));
527     hdl = pldm_pdr_find_record(repo, 2, &outData, &size, &nextRecHdl);
528     EXPECT_NE(hdl, nullptr);
529     EXPECT_EQ(size, sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_fru_record_set));
530     EXPECT_EQ(nextRecHdl, 0u);
531     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
532     hdr = reinterpret_cast<pldm_pdr_hdr*>(outData);
533     EXPECT_EQ(hdr->version, 1u);
534     EXPECT_EQ(hdr->type, PLDM_PDR_FRU_RECORD_SET);
535     EXPECT_EQ(hdr->length, htole16(sizeof(pldm_pdr_fru_record_set)));
536     EXPECT_EQ(hdr->record_handle, htole32(2));
537     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
538     fru = reinterpret_cast<pldm_pdr_fru_record_set*>(outData +
539                                                      sizeof(pldm_pdr_hdr));
540     EXPECT_EQ(fru->terminus_handle, htole16(2));
541     EXPECT_EQ(fru->fru_rsi, htole16(11));
542     EXPECT_EQ(fru->entity_type, htole16(2));
543     EXPECT_EQ(fru->entity_instance_num, htole16(1));
544     EXPECT_EQ(fru->container_id, htole16(101));
545     outData = nullptr;
546 
547     hdl = pldm_pdr_find_record(repo, 1, &outData, &size, &nextRecHdl);
548     EXPECT_NE(hdl, nullptr);
549     EXPECT_EQ(size, sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_fru_record_set));
550     EXPECT_EQ(nextRecHdl, 2u);
551     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
552     hdr = reinterpret_cast<pldm_pdr_hdr*>(outData);
553     EXPECT_EQ(hdr->version, 1u);
554     EXPECT_EQ(hdr->type, PLDM_PDR_FRU_RECORD_SET);
555     EXPECT_EQ(hdr->length, htole16(sizeof(pldm_pdr_fru_record_set)));
556     EXPECT_EQ(hdr->record_handle, htole32(1));
557     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
558     fru = reinterpret_cast<pldm_pdr_fru_record_set*>(outData +
559                                                      sizeof(pldm_pdr_hdr));
560     EXPECT_EQ(fru->terminus_handle, htole16(1));
561     EXPECT_EQ(fru->fru_rsi, htole16(10));
562     EXPECT_EQ(fru->entity_type, htole16(1));
563     EXPECT_EQ(fru->entity_instance_num, htole16(0));
564     EXPECT_EQ(fru->container_id, htole16(100));
565     outData = nullptr;
566 
567     pldm_pdr_destroy(repo);
568 }
569 
TEST(PDRUpdate,tesFindtFruRecordSet)570 TEST(PDRUpdate, tesFindtFruRecordSet)
571 {
572     auto repo = pldm_pdr_init();
573 
574     uint16_t terminusHdl{};
575     uint16_t entityType{};
576     uint16_t entityInstanceNum{};
577     uint16_t containerId{};
578     uint32_t first = 1;
579     EXPECT_EQ(pldm_pdr_add_fru_record_set(repo, 1, 1, 1, 0, 100, &first), 0);
580     uint32_t second = 2;
581     EXPECT_EQ(pldm_pdr_add_fru_record_set(repo, 1, 2, 1, 1, 100, &second), 0);
582     uint32_t third = 3;
583     EXPECT_EQ(pldm_pdr_add_fru_record_set(repo, 1, 3, 1, 2, 100, &third), 0);
584     EXPECT_EQ(first, pldm_pdr_get_record_handle(
585                          repo, pldm_pdr_fru_record_set_find_by_rsi(
586                                    repo, 1, &terminusHdl, &entityType,
587                                    &entityInstanceNum, &containerId)));
588     EXPECT_EQ(second, pldm_pdr_get_record_handle(
589                           repo, pldm_pdr_fru_record_set_find_by_rsi(
590                                     repo, 2, &terminusHdl, &entityType,
591                                     &entityInstanceNum, &containerId)));
592     EXPECT_EQ(third, pldm_pdr_get_record_handle(
593                          repo, pldm_pdr_fru_record_set_find_by_rsi(
594                                    repo, 3, &terminusHdl, &entityType,
595                                    &entityInstanceNum, &containerId)));
596     EXPECT_EQ(terminusHdl, 1u);
597     EXPECT_EQ(entityType, 1u);
598     EXPECT_EQ(entityInstanceNum, 2u);
599     EXPECT_EQ(containerId, 100u);
600     EXPECT_EQ(nullptr, pldm_pdr_fru_record_set_find_by_rsi(
601                            repo, 4, &terminusHdl, &entityType,
602                            &entityInstanceNum, &containerId));
603 
604     pldm_pdr_destroy(repo);
605 }
606 
607 #ifdef LIBPLDM_API_TESTING
TEST(PDRUpdate,testFindLastInRange)608 TEST(PDRUpdate, testFindLastInRange)
609 {
610     auto repo = pldm_pdr_init();
611 
612     std::array<uint8_t, 10> data{};
613     uint32_t handle1 = 0;
614     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, &handle1),
615               0);
616     uint32_t handle2 = 23;
617     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, &handle2),
618               0);
619     uint32_t handle3 = 77;
620     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), false, 1, &handle3),
621               0);
622     uint32_t handle4 = 16777325;
623     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 1, &handle4),
624               0);
625     uint32_t handle5 = 16777344;
626     EXPECT_EQ(pldm_pdr_add(repo, data.data(), data.size(), true, 1, &handle5),
627               0);
628 
629     auto rec1 = pldm_pdr_find_last_in_range(repo, 0, 100);
630     auto rec2 = pldm_pdr_find_last_in_range(repo, 16777300, 33554431);
631     EXPECT_NE(rec1, nullptr);
632     EXPECT_NE(rec2, nullptr);
633     EXPECT_NE(handle1, pldm_pdr_get_record_handle(repo, rec1));
634     EXPECT_NE(handle2, pldm_pdr_get_record_handle(repo, rec1));
635     EXPECT_EQ(handle3, pldm_pdr_get_record_handle(repo, rec1));
636     EXPECT_NE(handle4, pldm_pdr_get_record_handle(repo, rec2));
637     EXPECT_EQ(handle5, pldm_pdr_get_record_handle(repo, rec2));
638 
639     pldm_pdr_destroy(repo);
640 }
641 #endif
642 
643 #ifdef LIBPLDM_API_TESTING
TEST(PDRAccess,testGetTerminusHandle)644 TEST(PDRAccess, testGetTerminusHandle)
645 {
646 
647     auto repo = pldm_pdr_init();
648 
649     pldm_pdr_hdr hdr;
650 
651     hdr.type = 1;
652     uint16_t firstTerminusHandle = 1;
653     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
654     EXPECT_EQ(pldm_pdr_add(repo, reinterpret_cast<const uint8_t*>(&hdr),
655                            sizeof(hdr), false, firstTerminusHandle, NULL),
656               0);
657 
658     hdr.type = 2;
659     uint16_t secondTerminusHandle = 2;
660     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
661     EXPECT_EQ(pldm_pdr_add(repo, reinterpret_cast<const uint8_t*>(&hdr),
662                            sizeof(hdr), true, secondTerminusHandle, NULL),
663               0);
664 
665     hdr.type = 3;
666     uint16_t thirdTerminusHandle = 3;
667     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
668     EXPECT_EQ(pldm_pdr_add(repo, reinterpret_cast<const uint8_t*>(&hdr),
669                            sizeof(hdr), true, thirdTerminusHandle, NULL),
670               0);
671 
672     uint8_t* outData = nullptr;
673     uint32_t size{};
674 
675     auto firstRec =
676         pldm_pdr_find_record_by_type(repo, 1, nullptr, &outData, &size);
677     EXPECT_EQ(pldm_pdr_get_terminus_handle(repo, firstRec),
678               firstTerminusHandle);
679     outData = nullptr;
680 
681     auto secondRec =
682         pldm_pdr_find_record_by_type(repo, 2, nullptr, &outData, &size);
683     EXPECT_EQ(pldm_pdr_get_terminus_handle(repo, secondRec),
684               secondTerminusHandle);
685     outData = nullptr;
686 
687     auto thirdRec =
688         pldm_pdr_find_record_by_type(repo, 3, nullptr, &outData, &size);
689     EXPECT_EQ(pldm_pdr_get_terminus_handle(repo, thirdRec),
690               thirdTerminusHandle);
691     outData = nullptr;
692 
693     pldm_pdr_destroy(repo);
694 }
695 #endif
696 
TEST(EntityAssociationPDR,testInit)697 TEST(EntityAssociationPDR, testInit)
698 {
699     auto tree = pldm_entity_association_tree_init();
700     EXPECT_NE(tree, nullptr);
701     pldm_entity_association_tree_destroy(tree);
702 }
703 
TEST(EntityAssociationPDR,testBuild)704 TEST(EntityAssociationPDR, testBuild)
705 {
706     //        1
707     //        |
708     //        2--3--4
709     //        |
710     //        5--6--7
711     //        |  |
712     //        9  8
713 
714     pldm_entity entities[9]{};
715 
716     entities[0].entity_type = 1;
717     entities[1].entity_type = 2;
718     entities[2].entity_type = 2;
719     entities[3].entity_type = 3;
720     entities[4].entity_type = 4;
721     entities[5].entity_type = 5;
722     entities[6].entity_type = 5;
723     entities[7].entity_type = 6;
724     entities[8].entity_type = 7;
725 
726     auto tree = pldm_entity_association_tree_init();
727 
728     auto l1 = pldm_entity_association_tree_add(
729         tree, &entities[0], 0xffff, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
730     EXPECT_NE(l1, nullptr);
731     auto l2a = pldm_entity_association_tree_add(
732         tree, &entities[1], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
733     EXPECT_NE(l2a, nullptr);
734     auto l2b = pldm_entity_association_tree_add(
735         tree, &entities[2], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
736     EXPECT_NE(l2b, nullptr);
737     auto l2c = pldm_entity_association_tree_add(
738         tree, &entities[3], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
739     EXPECT_NE(l2c, nullptr);
740     auto l3a = pldm_entity_association_tree_add(
741         tree, &entities[4], 0xffff, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
742     EXPECT_NE(l3a, nullptr);
743     auto l3b = pldm_entity_association_tree_add(
744         tree, &entities[5], 0xffff, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
745     EXPECT_NE(l3b, nullptr);
746     auto l3c = pldm_entity_association_tree_add(
747         tree, &entities[6], 0xffff, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
748     EXPECT_NE(l3b, nullptr);
749     auto l4a = pldm_entity_association_tree_add(
750         tree, &entities[7], 0xffff, l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
751     EXPECT_NE(l4a, nullptr);
752     auto l4b = pldm_entity_association_tree_add(
753         tree, &entities[8], 0xffff, l3b, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
754     EXPECT_NE(l4b, nullptr);
755 
756     EXPECT_EQ(pldm_entity_is_node_parent(l1), true);
757     EXPECT_EQ(pldm_entity_is_node_parent(l2a), true);
758     EXPECT_EQ(pldm_entity_is_node_parent(l3a), true);
759     EXPECT_EQ(pldm_entity_is_node_parent(l3b), true);
760 
761     EXPECT_EQ(pldm_entity_is_node_parent(l2b), false);
762     EXPECT_EQ(pldm_entity_is_node_parent(l2c), false);
763     EXPECT_EQ(pldm_entity_is_node_parent(l3c), false);
764     EXPECT_EQ(pldm_entity_is_node_parent(l4a), false);
765     EXPECT_EQ(pldm_entity_is_node_parent(l4b), false);
766 
767     EXPECT_EQ(pldm_entity_is_exist_parent(l1), false);
768 
769     pldm_entity nodeL1 = pldm_entity_extract(l1);
770     pldm_entity parentL2a = pldm_entity_get_parent(l2a);
771     pldm_entity parentL2b = pldm_entity_get_parent(l2b);
772     pldm_entity parentL2c = pldm_entity_get_parent(l2c);
773     EXPECT_EQ(pldm_entity_is_exist_parent(l2a), true);
774     EXPECT_EQ(pldm_entity_is_exist_parent(l2b), true);
775     EXPECT_EQ(pldm_entity_is_exist_parent(l2c), true);
776     EXPECT_EQ(parentL2a.entity_type, nodeL1.entity_type);
777     EXPECT_EQ(parentL2a.entity_instance_num, nodeL1.entity_instance_num);
778     EXPECT_EQ(parentL2a.entity_container_id, nodeL1.entity_container_id);
779     EXPECT_EQ(parentL2b.entity_type, nodeL1.entity_type);
780     EXPECT_EQ(parentL2b.entity_instance_num, nodeL1.entity_instance_num);
781     EXPECT_EQ(parentL2b.entity_container_id, nodeL1.entity_container_id);
782     EXPECT_EQ(parentL2c.entity_type, nodeL1.entity_type);
783     EXPECT_EQ(parentL2c.entity_instance_num, nodeL1.entity_instance_num);
784     EXPECT_EQ(parentL2c.entity_container_id, nodeL1.entity_container_id);
785 
786     pldm_entity nodeL2a = pldm_entity_extract(l2a);
787     pldm_entity parentL3a = pldm_entity_get_parent(l3a);
788     pldm_entity parentL3b = pldm_entity_get_parent(l3b);
789     pldm_entity parentL3c = pldm_entity_get_parent(l3c);
790     EXPECT_EQ(pldm_entity_is_exist_parent(l3a), true);
791     EXPECT_EQ(pldm_entity_is_exist_parent(l3b), true);
792     EXPECT_EQ(pldm_entity_is_exist_parent(l3c), true);
793     EXPECT_EQ(parentL3a.entity_type, nodeL2a.entity_type);
794     EXPECT_EQ(parentL3a.entity_instance_num, nodeL2a.entity_instance_num);
795     EXPECT_EQ(parentL3a.entity_container_id, nodeL2a.entity_container_id);
796     EXPECT_EQ(parentL3b.entity_type, nodeL2a.entity_type);
797     EXPECT_EQ(parentL3b.entity_instance_num, nodeL2a.entity_instance_num);
798     EXPECT_EQ(parentL3b.entity_container_id, nodeL2a.entity_container_id);
799     EXPECT_EQ(parentL3c.entity_type, nodeL2a.entity_type);
800     EXPECT_EQ(parentL3c.entity_instance_num, nodeL2a.entity_instance_num);
801     EXPECT_EQ(parentL3c.entity_container_id, nodeL2a.entity_container_id);
802 
803     pldm_entity nodeL3a = pldm_entity_extract(l3a);
804     pldm_entity parentL4a = pldm_entity_get_parent(l4a);
805     EXPECT_EQ(pldm_entity_is_exist_parent(l4a), true);
806     EXPECT_EQ(parentL4a.entity_type, nodeL3a.entity_type);
807     EXPECT_EQ(parentL4a.entity_instance_num, nodeL3a.entity_instance_num);
808     EXPECT_EQ(parentL4a.entity_container_id, nodeL3a.entity_container_id);
809 
810     pldm_entity nodeL3b = pldm_entity_extract(l3b);
811     pldm_entity parentL4b = pldm_entity_get_parent(l4b);
812     EXPECT_EQ(pldm_entity_is_exist_parent(l4b), true);
813     EXPECT_EQ(parentL4b.entity_type, nodeL3b.entity_type);
814     EXPECT_EQ(parentL4b.entity_instance_num, nodeL3b.entity_instance_num);
815     EXPECT_EQ(parentL4b.entity_container_id, nodeL3b.entity_container_id);
816 
817     size_t num{};
818     pldm_entity* out = nullptr;
819     pldm_entity_association_tree_visit(tree, &out, &num);
820     EXPECT_EQ(num, 9u);
821 
822     EXPECT_EQ(out[0].entity_type, 1u);
823     EXPECT_EQ(out[0].entity_instance_num, 1u);
824     EXPECT_EQ(out[0].entity_container_id, 0u);
825 
826     EXPECT_EQ(out[1].entity_type, 2u);
827     EXPECT_EQ(out[1].entity_instance_num, 1u);
828     EXPECT_EQ(out[1].entity_container_id, 1u);
829     EXPECT_EQ(out[2].entity_type, 2u);
830     EXPECT_EQ(out[2].entity_instance_num, 2u);
831     EXPECT_EQ(out[2].entity_container_id, 1u);
832     EXPECT_EQ(out[3].entity_type, 3u);
833     EXPECT_EQ(out[3].entity_instance_num, 1u);
834     EXPECT_EQ(out[3].entity_container_id, 1u);
835 
836     EXPECT_EQ(out[4].entity_type, 4u);
837     EXPECT_EQ(out[4].entity_instance_num, 1u);
838     EXPECT_EQ(out[4].entity_container_id, 2u);
839     EXPECT_EQ(out[5].entity_type, 5u);
840     EXPECT_EQ(out[5].entity_instance_num, 1u);
841     EXPECT_EQ(out[5].entity_container_id, 2u);
842     EXPECT_EQ(out[6].entity_type, 5u);
843     EXPECT_EQ(out[6].entity_instance_num, 2u);
844     EXPECT_EQ(out[6].entity_container_id, 2u);
845 
846     EXPECT_EQ(out[7].entity_type, 7u);
847     EXPECT_EQ(out[7].entity_instance_num, 1u);
848     EXPECT_EQ(out[7].entity_container_id, 4u);
849     EXPECT_EQ(out[8].entity_type, 6u);
850     EXPECT_EQ(out[8].entity_instance_num, 1u);
851     EXPECT_EQ(out[8].entity_container_id, 3u);
852 
853     free(out);
854 
855     pldm_entity p1 = pldm_entity_extract(l1);
856     EXPECT_EQ(p1.entity_type, 1u);
857     EXPECT_EQ(p1.entity_instance_num, 1u);
858     EXPECT_EQ(p1.entity_container_id, 0u);
859 
860     pldm_entity p2a = pldm_entity_extract(l2a);
861     EXPECT_EQ(p2a.entity_type, 2u);
862     EXPECT_EQ(p2a.entity_instance_num, 1u);
863     EXPECT_EQ(p2a.entity_container_id, 1u);
864     pldm_entity p2b = pldm_entity_extract(l2b);
865     EXPECT_EQ(p2b.entity_type, 2u);
866     EXPECT_EQ(p2b.entity_instance_num, 2u);
867     EXPECT_EQ(p2b.entity_container_id, 1u);
868     pldm_entity p2c = pldm_entity_extract(l2c);
869     EXPECT_EQ(p2c.entity_type, 3u);
870     EXPECT_EQ(p2c.entity_instance_num, 1u);
871     EXPECT_EQ(p2c.entity_container_id, 1u);
872 
873     pldm_entity p3a = pldm_entity_extract(l3a);
874     EXPECT_EQ(p3a.entity_type, 4u);
875     EXPECT_EQ(p3a.entity_instance_num, 1u);
876     EXPECT_EQ(p3a.entity_container_id, 2u);
877     pldm_entity p3b = pldm_entity_extract(l3b);
878     EXPECT_EQ(p3b.entity_type, 5u);
879     EXPECT_EQ(p3b.entity_instance_num, 1u);
880     EXPECT_EQ(p3b.entity_container_id, 2u);
881     pldm_entity p3c = pldm_entity_extract(l3c);
882     EXPECT_EQ(p3c.entity_type, 5u);
883     EXPECT_EQ(p3c.entity_instance_num, 2u);
884     EXPECT_EQ(p3c.entity_container_id, 2u);
885 
886     pldm_entity p4a = pldm_entity_extract(l4a);
887     EXPECT_EQ(p4a.entity_type, 6u);
888     EXPECT_EQ(p4a.entity_instance_num, 1u);
889     EXPECT_EQ(p4a.entity_container_id, 3u);
890     pldm_entity p4b = pldm_entity_extract(l4b);
891     EXPECT_EQ(p4b.entity_type, 7u);
892     EXPECT_EQ(p4b.entity_instance_num, 1u);
893     EXPECT_EQ(p4b.entity_container_id, 4u);
894 
895     pldm_entity_association_tree_destroy(tree);
896 }
897 
898 #if LIBPLDM_API_TESTING
TEST(EntityAssociationPDR,findAndAddRemotePDR)899 TEST(EntityAssociationPDR, findAndAddRemotePDR)
900 {
901     //         Tree - 1
902     //
903     //        11521(1,0)
904     //             |
905     //          45 (1,1)
906     //             |
907     //          64 (1,2)
908     //             |
909     //    ------------------
910     //    |                 |
911     //  67(0,3)           67(1,3)
912     //    |                 |
913     // 135(0,4)          135(0,5)
914     //    |                 |
915     // 32903(0,6)         32903(0,7)
916 
917     pldm_entity entities[9]{};
918     entities[0].entity_type = 11521;
919     entities[1].entity_type = 45;
920     entities[2].entity_type = 64;
921     entities[3].entity_type = 67;
922     entities[4].entity_type = 67;
923     entities[5].entity_type = 135;
924     entities[5].entity_container_id = 2;
925     entities[6].entity_type = 135;
926     entities[6].entity_container_id = 3;
927     entities[7].entity_type = 32903;
928     entities[8].entity_type = 32903;
929     auto tree = pldm_entity_association_tree_init();
930     auto l1 = pldm_entity_association_tree_add_entity(
931         tree, &entities[0], 0xffff, nullptr, PLDM_ENTITY_ASSOCIAION_LOGICAL,
932         false, true, 0xffff);
933     EXPECT_NE(l1, nullptr);
934     auto l2 = pldm_entity_association_tree_add_entity(
935         tree, &entities[1], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
936         false, 0xffff);
937     EXPECT_NE(l2, nullptr);
938     auto l3 = pldm_entity_association_tree_add_entity(
939         tree, &entities[2], 0xffff, l2, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
940         true, 0xffff);
941     EXPECT_NE(l3, nullptr);
942     auto l4a = pldm_entity_association_tree_add_entity(
943         tree, &entities[3], 0, l3, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
944         false, 0xffff);
945     EXPECT_NE(l4a, nullptr);
946     auto l4b = pldm_entity_association_tree_add_entity(
947         tree, &entities[4], 1, l3, PLDM_ENTITY_ASSOCIAION_PHYSICAL, true, true,
948         0xffff);
949     EXPECT_NE(l4b, nullptr);
950     auto l5a = pldm_entity_association_tree_add_entity(
951         tree, &entities[5], 0, l4a, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
952         false, 0xffff);
953     EXPECT_NE(l5a, nullptr);
954     auto l5b = pldm_entity_association_tree_add_entity(
955         tree, &entities[6], 0, l4b, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
956         false, 0xffff);
957     EXPECT_NE(l5b, nullptr);
958     pldm_entity entity{};
959     entity.entity_type = 135;
960     entity.entity_instance_num = 0;
961     entity.entity_container_id = 2;
962     auto result1 = pldm_entity_association_tree_find(tree, &entity);
963     EXPECT_EQ(result1, l5a);
964     EXPECT_EQ(entities[5].entity_container_id, 2);
965     auto l6a = pldm_entity_association_tree_add_entity(
966         tree, &entities[7], 0, result1, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
967         false, 0xffff);
968     EXPECT_NE(l6a, nullptr);
969     entity.entity_type = 135;
970     entity.entity_instance_num = 0;
971     entity.entity_container_id = 3;
972     auto result2 = pldm_entity_association_tree_find(tree, &entity);
973     EXPECT_NE(result2, l5b);
974     EXPECT_EQ(entities[6].entity_container_id, 3);
975     auto l7a = pldm_entity_association_tree_add_entity(
976         tree, &entities[8], 0, result2, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
977         false, 0xffff);
978     EXPECT_EQ(l7a, nullptr);
979     pldm_entity_association_tree_destroy(tree);
980 }
981 #endif
982 
TEST(EntityAssociationPDR,testSpecialTrees)983 TEST(EntityAssociationPDR, testSpecialTrees)
984 {
985     pldm_entity entities[3]{};
986 
987     entities[0].entity_type = 1;
988     entities[1].entity_type = 2;
989     entities[2].entity_type = 1;
990 
991     // A
992     auto tree = pldm_entity_association_tree_init();
993     auto node = pldm_entity_association_tree_add(
994         tree, &entities[0], 0xffff, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
995     EXPECT_NE(node, nullptr);
996     size_t num{};
997     pldm_entity* out = nullptr;
998     pldm_entity_association_tree_visit(tree, &out, &num);
999     EXPECT_EQ(num, 1u);
1000     EXPECT_EQ(out[0].entity_type, 1u);
1001     EXPECT_EQ(out[0].entity_instance_num, 1u);
1002     EXPECT_EQ(out[0].entity_container_id, 0u);
1003     free(out);
1004     pldm_entity_association_tree_destroy(tree);
1005 
1006     // A-A-A
1007     tree = pldm_entity_association_tree_init();
1008     node = pldm_entity_association_tree_add(tree, &entities[0], 0xffff, nullptr,
1009                                             PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1010     EXPECT_NE(node, nullptr);
1011     node = pldm_entity_association_tree_add(tree, &entities[1], 0xffff, nullptr,
1012                                             PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1013     EXPECT_NE(node, nullptr);
1014     node = pldm_entity_association_tree_add(tree, &entities[2], 0xffff, nullptr,
1015                                             PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1016     EXPECT_NE(node, nullptr);
1017     pldm_entity_association_tree_visit(tree, &out, &num);
1018     EXPECT_EQ(num, 3u);
1019     EXPECT_EQ(out[0].entity_type, 1u);
1020     EXPECT_EQ(out[0].entity_instance_num, 1u);
1021     EXPECT_EQ(out[0].entity_container_id, 0u);
1022     EXPECT_EQ(out[1].entity_type, 1u);
1023     EXPECT_EQ(out[1].entity_instance_num, 2u);
1024     EXPECT_EQ(out[1].entity_container_id, 0u);
1025     EXPECT_EQ(out[2].entity_type, 2u);
1026     EXPECT_EQ(out[2].entity_instance_num, 1u);
1027     EXPECT_EQ(out[2].entity_container_id, 0u);
1028     free(out);
1029     pldm_entity_association_tree_destroy(tree);
1030 
1031     // A
1032     // |
1033     // A
1034     // |
1035     // A
1036     tree = pldm_entity_association_tree_init();
1037     node = pldm_entity_association_tree_add(tree, &entities[0], 0xffff, nullptr,
1038                                             PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1039     EXPECT_NE(node, nullptr);
1040     auto node1 = pldm_entity_association_tree_add(
1041         tree, &entities[1], 0xffff, node, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1042     EXPECT_NE(node1, nullptr);
1043     auto node2 = pldm_entity_association_tree_add(
1044         tree, &entities[2], 0xffff, node1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1045     EXPECT_NE(node2, nullptr);
1046     pldm_entity_association_tree_visit(tree, &out, &num);
1047     EXPECT_EQ(num, 3u);
1048     EXPECT_EQ(out[0].entity_type, 1u);
1049     EXPECT_EQ(out[0].entity_instance_num, 1u);
1050     EXPECT_EQ(out[0].entity_container_id, 0u);
1051     EXPECT_EQ(out[1].entity_type, 2u);
1052     EXPECT_EQ(out[1].entity_instance_num, 1u);
1053     EXPECT_EQ(out[1].entity_container_id, 1u);
1054     EXPECT_EQ(out[2].entity_type, 1u);
1055     EXPECT_EQ(out[2].entity_instance_num, 1u);
1056     EXPECT_EQ(out[2].entity_container_id, 2u);
1057     free(out);
1058     pldm_entity_association_tree_destroy(tree);
1059 
1060     // A-A
1061     //   |
1062     //   A-A
1063     tree = pldm_entity_association_tree_init();
1064     node = pldm_entity_association_tree_add(tree, &entities[0], 0xffff, nullptr,
1065                                             PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1066     EXPECT_NE(node, nullptr);
1067     node = pldm_entity_association_tree_add(tree, &entities[0], 0xffff, nullptr,
1068                                             PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1069     EXPECT_NE(node, nullptr);
1070     node1 = pldm_entity_association_tree_add(tree, &entities[1], 0xffff, node,
1071                                              PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1072     EXPECT_NE(node1, nullptr);
1073     node2 = pldm_entity_association_tree_add(tree, &entities[2], 0xffff, node,
1074                                              PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1075     EXPECT_NE(node2, nullptr);
1076     pldm_entity_association_tree_visit(tree, &out, &num);
1077     EXPECT_EQ(num, 4u);
1078     EXPECT_EQ(out[0].entity_type, 1u);
1079     EXPECT_EQ(out[0].entity_instance_num, 1u);
1080     EXPECT_EQ(out[0].entity_container_id, 0u);
1081     EXPECT_EQ(out[1].entity_type, 1u);
1082     EXPECT_EQ(out[1].entity_instance_num, 2u);
1083     EXPECT_EQ(out[1].entity_container_id, 0u);
1084     EXPECT_EQ(out[2].entity_type, 2u);
1085     EXPECT_EQ(out[2].entity_instance_num, 1u);
1086     EXPECT_EQ(out[2].entity_container_id, 1u);
1087     EXPECT_EQ(out[3].entity_type, 1u);
1088     EXPECT_EQ(out[3].entity_instance_num, 1u);
1089     EXPECT_EQ(out[3].entity_container_id, 1u);
1090     free(out);
1091     pldm_entity_association_tree_destroy(tree);
1092 }
1093 
TEST(EntityAssociationPDR,testPDR)1094 TEST(EntityAssociationPDR, testPDR)
1095 {
1096     // e = entity type, c = container id, i = instance num
1097 
1098     //        INPUT
1099     //        1(e=1)--1a(e=2)
1100     //        |
1101     //        2(e=2)--3(e=2)--4(e=2)--5(e=3)
1102     //        |
1103     //        6(e=4)--7(e=5)--8(e=5)--9(e=5)
1104     //        |       |
1105     //        11(e=6) 10(e=7)
1106 
1107     //        Expected OUTPUT
1108     //        1(e=1,c=0,i=1)
1109     //        |
1110     //        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)
1111     //        |
1112     //        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)
1113     //        |               |
1114     //        10(e=6,c=3,i=1) 11(e=7,c=4,i=1)
1115     pldm_entity entities[11]{};
1116 
1117     entities[0].entity_type = 1;
1118     entities[1].entity_type = 2;
1119     entities[2].entity_type = 3;
1120     entities[3].entity_type = 2;
1121     entities[4].entity_type = 3;
1122     entities[5].entity_type = 4;
1123     entities[6].entity_type = 5;
1124     entities[7].entity_type = 5;
1125     entities[8].entity_type = 5;
1126     entities[9].entity_type = 6;
1127     entities[10].entity_type = 7;
1128 
1129     auto tree = pldm_entity_association_tree_init();
1130 
1131     auto l1 = pldm_entity_association_tree_add(
1132         tree, &entities[0], 0xffff, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1133     EXPECT_NE(l1, nullptr);
1134     auto l1a = pldm_entity_association_tree_add(
1135         tree, &entities[1], 0xffff, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1136     EXPECT_NE(l1a, nullptr);
1137 
1138     auto l2a = pldm_entity_association_tree_add(
1139         tree, &entities[1], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1140     EXPECT_NE(l2a, nullptr);
1141     auto l2b = pldm_entity_association_tree_add(tree, &entities[2], 0xffff, l1,
1142                                                 PLDM_ENTITY_ASSOCIAION_LOGICAL);
1143     EXPECT_NE(l2b, nullptr);
1144     auto l2c = pldm_entity_association_tree_add(
1145         tree, &entities[3], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1146     EXPECT_NE(l2c, nullptr);
1147     auto l2d = pldm_entity_association_tree_add(tree, &entities[4], 0xffff, l1,
1148                                                 PLDM_ENTITY_ASSOCIAION_LOGICAL);
1149     EXPECT_NE(l2d, nullptr);
1150 
1151     auto l3a = pldm_entity_association_tree_add(
1152         tree, &entities[5], 0xffff, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1153     EXPECT_NE(l3a, nullptr);
1154     auto l3b = pldm_entity_association_tree_add(
1155         tree, &entities[6], 0xffff, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1156     EXPECT_NE(l3b, nullptr);
1157     auto l3c = pldm_entity_association_tree_add(tree, &entities[7], 0xffff, l2a,
1158                                                 PLDM_ENTITY_ASSOCIAION_LOGICAL);
1159     EXPECT_NE(l3c, nullptr);
1160     auto l3d = pldm_entity_association_tree_add(tree, &entities[8], 0xffff, l2a,
1161                                                 PLDM_ENTITY_ASSOCIAION_LOGICAL);
1162     EXPECT_NE(l3d, nullptr);
1163 
1164     auto l4a = pldm_entity_association_tree_add(
1165         tree, &entities[9], 0xffff, l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1166     EXPECT_NE(l4a, nullptr);
1167     auto l4b = pldm_entity_association_tree_add(
1168         tree, &entities[10], 0xffff, l3b, PLDM_ENTITY_ASSOCIAION_LOGICAL);
1169     EXPECT_NE(l4b, nullptr);
1170 
1171     EXPECT_EQ(pldm_entity_get_num_children(l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL),
1172               2);
1173     EXPECT_EQ(pldm_entity_get_num_children(l1, PLDM_ENTITY_ASSOCIAION_LOGICAL),
1174               2);
1175     EXPECT_EQ(
1176         pldm_entity_get_num_children(l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL), 2);
1177     EXPECT_EQ(
1178         pldm_entity_get_num_children(l3b, PLDM_ENTITY_ASSOCIAION_PHYSICAL), 0);
1179     EXPECT_EQ(pldm_entity_get_num_children(l3b, PLDM_ENTITY_ASSOCIAION_LOGICAL),
1180               1);
1181 
1182     auto repo = pldm_pdr_init();
1183     int rc = pldm_entity_association_pdr_add(tree, repo, false, 1);
1184     ASSERT_EQ(rc, 0);
1185 
1186     EXPECT_EQ(pldm_pdr_get_record_count(repo), 6u);
1187 
1188     uint32_t currRecHandle{};
1189     uint32_t nextRecHandle{};
1190     uint8_t* data = nullptr;
1191     uint32_t size{};
1192     uint32_t commonSize = sizeof(struct pldm_pdr_hdr) + sizeof(uint16_t) +
1193                           sizeof(uint8_t) + sizeof(pldm_entity) +
1194                           sizeof(uint8_t);
1195 
1196     pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1197     EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1198                                       l1, PLDM_ENTITY_ASSOCIAION_LOGICAL) *
1199                                   sizeof(pldm_entity)));
1200     uint8_t* start = data;
1201     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1202     pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
1203     EXPECT_EQ(le32toh(hdr->record_handle), 1u);
1204     EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
1205     EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
1206     start += sizeof(pldm_pdr_hdr);
1207     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1208     uint16_t* containerId = reinterpret_cast<uint16_t*>(start);
1209     EXPECT_EQ(le16toh(*containerId), 1u);
1210     start += sizeof(uint16_t);
1211     EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_LOGICAL);
1212     start += sizeof(uint8_t);
1213     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1214     pldm_entity* entity = reinterpret_cast<pldm_entity*>(start);
1215     EXPECT_EQ(le16toh(entity->entity_type), 1u);
1216     EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1217     EXPECT_EQ(le16toh(entity->entity_container_id), 0u);
1218     start += sizeof(pldm_entity);
1219     EXPECT_EQ(*start,
1220               pldm_entity_get_num_children(l1, PLDM_ENTITY_ASSOCIAION_LOGICAL));
1221     start += sizeof(uint8_t);
1222     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1223     entity = reinterpret_cast<pldm_entity*>(start);
1224     EXPECT_EQ(le16toh(entity->entity_type), 3u);
1225     EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1226     EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
1227     start += sizeof(pldm_entity);
1228     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1229     entity = reinterpret_cast<pldm_entity*>(start);
1230     EXPECT_EQ(le16toh(entity->entity_type), 3u);
1231     EXPECT_EQ(le16toh(entity->entity_instance_num), 2u);
1232     EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
1233 
1234     currRecHandle = nextRecHandle;
1235 
1236     pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1237     EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1238                                       l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL) *
1239                                   sizeof(pldm_entity)));
1240     start = data;
1241     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1242     hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
1243     EXPECT_EQ(le32toh(hdr->record_handle), 2u);
1244     EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
1245     EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
1246     start += sizeof(pldm_pdr_hdr);
1247     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1248     containerId = reinterpret_cast<uint16_t*>(start);
1249     EXPECT_EQ(le16toh(*containerId), 1u);
1250     start += sizeof(uint16_t);
1251     EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1252     start += sizeof(uint8_t);
1253     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1254     entity = reinterpret_cast<pldm_entity*>(start);
1255     EXPECT_EQ(le16toh(entity->entity_type), 1u);
1256     EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1257     EXPECT_EQ(le16toh(entity->entity_container_id), 0u);
1258     start += sizeof(pldm_entity);
1259     EXPECT_EQ(*start, pldm_entity_get_num_children(
1260                           l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL));
1261     start += sizeof(uint8_t);
1262     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1263     entity = reinterpret_cast<pldm_entity*>(start);
1264     EXPECT_EQ(le16toh(entity->entity_type), 2u);
1265     EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1266     EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
1267     start += sizeof(pldm_entity);
1268     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1269     entity = reinterpret_cast<pldm_entity*>(start);
1270     EXPECT_EQ(le16toh(entity->entity_type), 2u);
1271     EXPECT_EQ(le16toh(entity->entity_instance_num), 2u);
1272     EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
1273 
1274     currRecHandle = nextRecHandle;
1275 
1276     pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1277     EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1278                                       l2a, PLDM_ENTITY_ASSOCIAION_LOGICAL) *
1279                                   sizeof(pldm_entity)));
1280     start = data;
1281     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1282     hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
1283     EXPECT_EQ(le32toh(hdr->record_handle), 3u);
1284     EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
1285     EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
1286     start += sizeof(pldm_pdr_hdr);
1287     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1288     containerId = reinterpret_cast<uint16_t*>(start);
1289     EXPECT_EQ(le16toh(*containerId), 2u);
1290     start += sizeof(uint16_t);
1291     EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_LOGICAL);
1292     start += sizeof(uint8_t);
1293     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1294     entity = reinterpret_cast<pldm_entity*>(start);
1295     EXPECT_EQ(le16toh(entity->entity_type), 2u);
1296     EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1297     EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
1298     start += sizeof(pldm_entity);
1299     EXPECT_EQ(*start, pldm_entity_get_num_children(
1300                           l2a, PLDM_ENTITY_ASSOCIAION_LOGICAL));
1301     start += sizeof(uint8_t);
1302     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1303     entity = reinterpret_cast<pldm_entity*>(start);
1304     EXPECT_EQ(le16toh(entity->entity_type), 5);
1305     EXPECT_EQ(le16toh(entity->entity_instance_num), 2u);
1306     EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
1307     start += sizeof(pldm_entity);
1308     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1309     entity = reinterpret_cast<pldm_entity*>(start);
1310     EXPECT_EQ(le16toh(entity->entity_type), 5u);
1311     EXPECT_EQ(le16toh(entity->entity_instance_num), 3u);
1312     EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
1313 
1314     currRecHandle = nextRecHandle;
1315 
1316     pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1317     EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1318                                       l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL) *
1319                                   sizeof(pldm_entity)));
1320     start = data;
1321     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1322     hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
1323     EXPECT_EQ(le32toh(hdr->record_handle), 4u);
1324     EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
1325     EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
1326     start += sizeof(pldm_pdr_hdr);
1327     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1328     containerId = reinterpret_cast<uint16_t*>(start);
1329     EXPECT_EQ(le16toh(*containerId), 2u);
1330     start += sizeof(uint16_t);
1331     EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1332     start += sizeof(uint8_t);
1333     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1334     entity = reinterpret_cast<pldm_entity*>(start);
1335     EXPECT_EQ(le16toh(entity->entity_type), 2u);
1336     EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1337     EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
1338     start += sizeof(pldm_entity);
1339     EXPECT_EQ(*start, pldm_entity_get_num_children(
1340                           l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL));
1341     start += sizeof(uint8_t);
1342     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1343     entity = reinterpret_cast<pldm_entity*>(start);
1344     EXPECT_EQ(le16toh(entity->entity_type), 4u);
1345     EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1346     EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
1347     start += sizeof(pldm_entity);
1348     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1349     entity = reinterpret_cast<pldm_entity*>(start);
1350     EXPECT_EQ(le16toh(entity->entity_type), 5u);
1351     EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1352     EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
1353 
1354     currRecHandle = nextRecHandle;
1355 
1356     pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1357     EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1358                                       l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL) *
1359                                   sizeof(pldm_entity)));
1360     start = data;
1361     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1362     hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
1363     EXPECT_EQ(le32toh(hdr->record_handle), 5u);
1364     EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
1365     EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
1366     start += sizeof(pldm_pdr_hdr);
1367     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1368     containerId = reinterpret_cast<uint16_t*>(start);
1369     EXPECT_EQ(le16toh(*containerId), 3u);
1370     start += sizeof(uint16_t);
1371     EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1372     start += sizeof(uint8_t);
1373     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1374     entity = reinterpret_cast<pldm_entity*>(start);
1375     EXPECT_EQ(le16toh(entity->entity_type), 4u);
1376     EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1377     EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
1378     start += sizeof(pldm_entity);
1379     EXPECT_EQ(*start, pldm_entity_get_num_children(
1380                           l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL));
1381     start += sizeof(uint8_t);
1382     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1383     entity = reinterpret_cast<pldm_entity*>(start);
1384     EXPECT_EQ(le16toh(entity->entity_type), 6u);
1385     EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1386     EXPECT_EQ(le16toh(entity->entity_container_id), 3u);
1387 
1388     currRecHandle = nextRecHandle;
1389 
1390     pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1391     EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1392                                       l3b, PLDM_ENTITY_ASSOCIAION_LOGICAL) *
1393                                   sizeof(pldm_entity)));
1394     start = data;
1395     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1396     hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
1397     EXPECT_EQ(le32toh(hdr->record_handle), 6u);
1398     EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
1399     EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
1400     start += sizeof(pldm_pdr_hdr);
1401     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1402     containerId = reinterpret_cast<uint16_t*>(start);
1403     EXPECT_EQ(le16toh(*containerId), 4u);
1404     start += sizeof(uint16_t);
1405     EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_LOGICAL);
1406     start += sizeof(uint8_t);
1407     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1408     entity = reinterpret_cast<pldm_entity*>(start);
1409     EXPECT_EQ(le16toh(entity->entity_type), 5u);
1410     EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1411     EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
1412     start += sizeof(pldm_entity);
1413     EXPECT_EQ(*start, pldm_entity_get_num_children(
1414                           l3b, PLDM_ENTITY_ASSOCIAION_LOGICAL));
1415     start += sizeof(uint8_t);
1416     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1417     entity = reinterpret_cast<pldm_entity*>(start);
1418     EXPECT_EQ(le16toh(entity->entity_type), 7u);
1419     EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1420     EXPECT_EQ(le16toh(entity->entity_container_id), 4u);
1421 
1422     EXPECT_EQ(nextRecHandle, 0u);
1423 
1424     pldm_pdr_destroy(repo);
1425     pldm_entity_association_tree_destroy(tree);
1426 }
1427 
TEST(EntityAssociationPDR,testFind)1428 TEST(EntityAssociationPDR, testFind)
1429 {
1430     //        1
1431     //        |
1432     //        2--3--4
1433     //        |
1434     //        5--6--7
1435     //        |  |
1436     //        8  9
1437 
1438     pldm_entity entities[9]{};
1439 
1440     entities[0].entity_type = 1;
1441     entities[1].entity_type = 2;
1442     entities[2].entity_type = 2;
1443     entities[3].entity_type = 3;
1444     entities[4].entity_type = 4;
1445     entities[5].entity_type = 5;
1446     entities[6].entity_type = 5;
1447     entities[7].entity_type = 6;
1448     entities[8].entity_type = 7;
1449 
1450     auto tree = pldm_entity_association_tree_init();
1451 
1452     auto l1 = pldm_entity_association_tree_add(
1453         tree, &entities[0], 0xffff, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1454     EXPECT_NE(l1, nullptr);
1455     auto l2a = pldm_entity_association_tree_add(
1456         tree, &entities[1], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1457     EXPECT_NE(l2a, nullptr);
1458     auto l2b = pldm_entity_association_tree_add(
1459         tree, &entities[2], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1460     EXPECT_NE(l2b, nullptr);
1461     auto l2c = pldm_entity_association_tree_add(
1462         tree, &entities[3], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1463     EXPECT_NE(l2c, nullptr);
1464     auto l3a = pldm_entity_association_tree_add(
1465         tree, &entities[4], 0xffff, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1466     EXPECT_NE(l3a, nullptr);
1467     auto l3b = pldm_entity_association_tree_add(
1468         tree, &entities[5], 0xffff, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1469     EXPECT_NE(l3b, nullptr);
1470     auto l3c = pldm_entity_association_tree_add(
1471         tree, &entities[6], 0xffff, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1472     EXPECT_NE(l3c, nullptr);
1473     auto l4a = pldm_entity_association_tree_add(
1474         tree, &entities[7], 0xffff, l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1475     EXPECT_NE(l4a, nullptr);
1476     auto l4b = pldm_entity_association_tree_add(
1477         tree, &entities[8], 0xffff, l3b, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1478     EXPECT_NE(l4b, nullptr);
1479 
1480     pldm_entity entity{};
1481 
1482     entity.entity_type = 1;
1483     entity.entity_instance_num = 1;
1484     auto result = pldm_entity_association_tree_find(tree, &entity);
1485     EXPECT_EQ(result, l1);
1486     EXPECT_EQ(entity.entity_container_id, 0);
1487 
1488     entity.entity_type = 2;
1489     entity.entity_instance_num = 1;
1490     result = pldm_entity_association_tree_find(tree, &entity);
1491     EXPECT_EQ(result, l2a);
1492     EXPECT_EQ(entity.entity_container_id, 1);
1493     entity.entity_type = 2;
1494     entity.entity_instance_num = 2;
1495     result = pldm_entity_association_tree_find(tree, &entity);
1496     EXPECT_EQ(result, l2b);
1497     EXPECT_EQ(entity.entity_container_id, 1);
1498     entity.entity_type = 3;
1499     entity.entity_instance_num = 1;
1500     result = pldm_entity_association_tree_find(tree, &entity);
1501     EXPECT_EQ(result, l2c);
1502     EXPECT_EQ(entity.entity_container_id, 1);
1503 
1504     entity.entity_type = 7;
1505     entity.entity_instance_num = 1;
1506     result = pldm_entity_association_tree_find(tree, &entity);
1507     EXPECT_EQ(result, l4b);
1508     EXPECT_EQ(entity.entity_container_id, 4);
1509 
1510     pldm_entity_association_tree_destroy(tree);
1511 }
1512 
1513 #ifdef LIBPLDM_API_TESTING
TEST(EntityAssociationPDR,testCopyTree)1514 TEST(EntityAssociationPDR, testCopyTree)
1515 {
1516     pldm_entity entities[4]{};
1517     int rc;
1518 
1519     entities[0].entity_type = 1;
1520     entities[1].entity_type = 2;
1521     entities[2].entity_type = 2;
1522     entities[3].entity_type = 3;
1523 
1524     auto orgTree = pldm_entity_association_tree_init();
1525     auto newTree = pldm_entity_association_tree_init();
1526     auto l1 =
1527         pldm_entity_association_tree_add(orgTree, &entities[0], 0xffff, nullptr,
1528                                          PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1529     EXPECT_NE(l1, nullptr);
1530     auto l2a = pldm_entity_association_tree_add(
1531         orgTree, &entities[1], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1532     EXPECT_NE(l2a, nullptr);
1533     auto l2b = pldm_entity_association_tree_add(
1534         orgTree, &entities[2], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1535     EXPECT_NE(l2b, nullptr);
1536     auto l2c = pldm_entity_association_tree_add(
1537         orgTree, &entities[3], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1538     EXPECT_NE(l2c, nullptr);
1539     size_t orgNum{};
1540     pldm_entity* orgOut = nullptr;
1541     pldm_entity_association_tree_visit(orgTree, &orgOut, &orgNum);
1542     EXPECT_EQ(orgNum, 4u);
1543 
1544     rc = pldm_entity_association_tree_copy_root_check(orgTree, newTree);
1545     ASSERT_EQ(rc, 0);
1546     size_t newNum{};
1547     pldm_entity* newOut = nullptr;
1548     pldm_entity_association_tree_visit(newTree, &newOut, &newNum);
1549     EXPECT_EQ(newNum, orgNum);
1550     EXPECT_EQ(newOut[0].entity_type, 1u);
1551     EXPECT_EQ(newOut[0].entity_instance_num, 1u);
1552     EXPECT_EQ(newOut[0].entity_container_id, 0u);
1553     free(orgOut);
1554     free(newOut);
1555     pldm_entity_association_tree_destroy(orgTree);
1556     pldm_entity_association_tree_destroy(newTree);
1557 }
1558 #endif
1559 
TEST(EntityAssociationPDR,testExtract)1560 TEST(EntityAssociationPDR, testExtract)
1561 {
1562     std::vector<uint8_t> pdr{};
1563     pdr.resize(sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_entity_association) +
1564                sizeof(pldm_entity) * 5);
1565     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1566     pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(pdr.data());
1567     hdr->type = PLDM_PDR_ENTITY_ASSOCIATION;
1568     hdr->length =
1569         htole16(sizeof(pldm_pdr_entity_association) + sizeof(pldm_entity) * 5);
1570 
1571     pldm_pdr_entity_association* e =
1572         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1573         reinterpret_cast<pldm_pdr_entity_association*>(pdr.data() +
1574                                                        sizeof(pldm_pdr_hdr));
1575     e->container_id = htole16(1);
1576     e->num_children = 5;
1577     e->container.entity_type = htole16(1);
1578     e->container.entity_instance_num = htole16(1);
1579     e->container.entity_container_id = htole16(0);
1580 
1581     pldm_entity* entity = e->children;
1582     entity->entity_type = htole16(2);
1583     entity->entity_instance_num = htole16(1);
1584     entity->entity_container_id = htole16(1);
1585     ++entity;
1586     entity->entity_type = htole16(3);
1587     entity->entity_instance_num = htole16(1);
1588     entity->entity_container_id = htole16(1);
1589     ++entity;
1590     entity->entity_type = htole16(4);
1591     entity->entity_instance_num = htole16(1);
1592     entity->entity_container_id = htole16(1);
1593     ++entity;
1594     entity->entity_type = htole16(5);
1595     entity->entity_instance_num = htole16(1);
1596     entity->entity_container_id = htole16(1);
1597     ++entity;
1598     entity->entity_type = htole16(6);
1599     entity->entity_instance_num = htole16(1);
1600     entity->entity_container_id = htole16(1);
1601 
1602     size_t num{};
1603     pldm_entity* out = nullptr;
1604     pldm_entity_association_pdr_extract(pdr.data(), pdr.size(), &num, &out);
1605     EXPECT_EQ(num, (unsigned)e->num_children + 1);
1606     EXPECT_EQ(out[0].entity_type, 1u);
1607     EXPECT_EQ(out[0].entity_instance_num, 1u);
1608     EXPECT_EQ(out[0].entity_container_id, 0u);
1609     EXPECT_EQ(out[1].entity_type, 2u);
1610     EXPECT_EQ(out[1].entity_instance_num, 1u);
1611     EXPECT_EQ(out[1].entity_container_id, 1u);
1612     EXPECT_EQ(out[2].entity_type, 3u);
1613     EXPECT_EQ(out[2].entity_instance_num, 1u);
1614     EXPECT_EQ(out[2].entity_container_id, 1u);
1615     EXPECT_EQ(out[3].entity_type, 4u);
1616     EXPECT_EQ(out[3].entity_instance_num, 1u);
1617     EXPECT_EQ(out[3].entity_container_id, 1u);
1618     EXPECT_EQ(out[4].entity_type, 5u);
1619     EXPECT_EQ(out[4].entity_instance_num, 1u);
1620     EXPECT_EQ(out[4].entity_container_id, 1u);
1621     EXPECT_EQ(out[5].entity_type, 6u);
1622     EXPECT_EQ(out[5].entity_instance_num, 1u);
1623     EXPECT_EQ(out[5].entity_container_id, 1u);
1624 
1625     free(out);
1626 }
1627 
TEST(EntityAssociationPDR,testGetChildren)1628 TEST(EntityAssociationPDR, testGetChildren)
1629 {
1630     pldm_entity entities[4]{};
1631     entities[0].entity_type = 1;
1632     entities[1].entity_type = 2;
1633     entities[2].entity_type = 2;
1634     entities[3].entity_type = 3;
1635 
1636     auto tree = pldm_entity_association_tree_init();
1637     auto l1 = pldm_entity_association_tree_add(
1638         tree, &entities[0], 0xffff, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1639     EXPECT_NE(l1, nullptr);
1640     auto l2a = pldm_entity_association_tree_add(
1641         tree, &entities[1], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1642     EXPECT_NE(l2a, nullptr);
1643     auto l2b = pldm_entity_association_tree_add(
1644         tree, &entities[2], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1645     EXPECT_NE(l2b, nullptr);
1646     auto l2c = pldm_entity_association_tree_add(
1647         tree, &entities[3], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1648     EXPECT_NE(l2c, nullptr);
1649 
1650     pldm_entity et1;
1651     et1.entity_type = 2;
1652     et1.entity_instance_num = 1;
1653     EXPECT_EQ(true, pldm_is_current_parent_child(l1, &et1));
1654 
1655     pldm_entity et2;
1656     et2.entity_type = 2;
1657     et2.entity_instance_num = 2;
1658     EXPECT_EQ(true, pldm_is_current_parent_child(l1, &et2));
1659 
1660     pldm_entity et3;
1661     et3.entity_type = 2;
1662     et3.entity_instance_num = 3;
1663     EXPECT_EQ(false, pldm_is_current_parent_child(l1, &et3));
1664 
1665     pldm_entity_association_tree_destroy(tree);
1666 }
1667 
TEST(EntityAssociationPDR,testEntityInstanceNumber)1668 TEST(EntityAssociationPDR, testEntityInstanceNumber)
1669 {
1670     pldm_entity entities[9]{};
1671 
1672     entities[0].entity_type = 1;
1673     entities[1].entity_type = 2;
1674     entities[2].entity_type = 2;
1675     entities[3].entity_type = 2;
1676     entities[4].entity_type = 2;
1677     entities[5].entity_type = 2;
1678     entities[6].entity_type = 2;
1679     entities[7].entity_type = 3;
1680     entities[8].entity_type = 3;
1681 
1682     auto tree = pldm_entity_association_tree_init();
1683     auto repo = pldm_pdr_init();
1684 
1685     uint16_t terminusHdl{};
1686     uint16_t entityType{};
1687     uint16_t entityInstanceNum{};
1688     uint16_t containerId{};
1689 
1690     auto node = pldm_entity_association_tree_add(
1691         tree, &entities[0], 0xffff, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1692     EXPECT_NE(node, nullptr);
1693 
1694     auto l1 = pldm_entity_association_tree_add(tree, &entities[1], 63, node,
1695                                                PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1696     uint32_t first = 1;
1697     EXPECT_EQ(pldm_pdr_add_fru_record_set(repo, 1, 1, entities[1].entity_type,
1698                                           entities[1].entity_instance_num,
1699                                           entities[1].entity_container_id,
1700                                           &first),
1701               0);
1702     EXPECT_NE(l1, nullptr);
1703     EXPECT_EQ(entities[1].entity_instance_num, 63);
1704     EXPECT_EQ(first, pldm_pdr_get_record_handle(
1705                          repo, pldm_pdr_fru_record_set_find_by_rsi(
1706                                    repo, 1, &terminusHdl, &entityType,
1707                                    &entityInstanceNum, &containerId)));
1708     EXPECT_EQ(entityType, 2);
1709     EXPECT_EQ(entityInstanceNum, 63);
1710 
1711     auto l2 = pldm_entity_association_tree_add(tree, &entities[2], 37, node,
1712                                                PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1713     uint32_t second = 2;
1714     EXPECT_EQ(pldm_pdr_add_fru_record_set(repo, 1, 2, entities[2].entity_type,
1715                                           entities[2].entity_instance_num,
1716                                           entities[2].entity_container_id,
1717                                           &second),
1718               0);
1719     EXPECT_NE(l2, nullptr);
1720     EXPECT_EQ(entities[2].entity_instance_num, 37);
1721     EXPECT_EQ(second, pldm_pdr_get_record_handle(
1722                           repo, pldm_pdr_fru_record_set_find_by_rsi(
1723                                     repo, 2, &terminusHdl, &entityType,
1724                                     &entityInstanceNum, &containerId)));
1725     EXPECT_EQ(entityType, 2);
1726     EXPECT_EQ(entityInstanceNum, 37);
1727 
1728     auto l3 = pldm_entity_association_tree_add(tree, &entities[3], 44, node,
1729                                                PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1730     uint32_t third = 3;
1731     EXPECT_EQ(pldm_pdr_add_fru_record_set(repo, 1, 3, entities[3].entity_type,
1732                                           entities[3].entity_instance_num,
1733                                           entities[3].entity_container_id,
1734                                           &third),
1735               0);
1736     EXPECT_NE(l3, nullptr);
1737     EXPECT_EQ(entities[3].entity_instance_num, 44);
1738     EXPECT_EQ(third, pldm_pdr_get_record_handle(
1739                          repo, pldm_pdr_fru_record_set_find_by_rsi(
1740                                    repo, 3, &terminusHdl, &entityType,
1741                                    &entityInstanceNum, &containerId)));
1742     EXPECT_EQ(entityType, 2);
1743     EXPECT_EQ(entityInstanceNum, 44);
1744 
1745     auto l4 = pldm_entity_association_tree_add(tree, &entities[4], 89, node,
1746                                                PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1747     uint32_t fourth = 4;
1748     EXPECT_EQ(pldm_pdr_add_fru_record_set(repo, 1, 4, entities[4].entity_type,
1749                                           entities[4].entity_instance_num,
1750                                           entities[4].entity_container_id,
1751                                           &fourth),
1752               0);
1753     EXPECT_NE(l4, nullptr);
1754     EXPECT_EQ(entities[4].entity_instance_num, 89);
1755     EXPECT_EQ(fourth, pldm_pdr_get_record_handle(
1756                           repo, pldm_pdr_fru_record_set_find_by_rsi(
1757                                     repo, 4, &terminusHdl, &entityType,
1758                                     &entityInstanceNum, &containerId)));
1759     EXPECT_EQ(entityType, 2);
1760     EXPECT_EQ(entityInstanceNum, 89);
1761 
1762     auto l5 = pldm_entity_association_tree_add(tree, &entities[5], 0xffff, node,
1763                                                PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1764     uint32_t fifth = 5;
1765     EXPECT_EQ(pldm_pdr_add_fru_record_set(repo, 1, 5, entities[5].entity_type,
1766                                           entities[5].entity_instance_num,
1767                                           entities[5].entity_container_id,
1768                                           &fifth),
1769               0);
1770     EXPECT_NE(l5, nullptr);
1771     EXPECT_EQ(entities[5].entity_instance_num, 90);
1772     EXPECT_EQ(fifth, pldm_pdr_get_record_handle(
1773                          repo, pldm_pdr_fru_record_set_find_by_rsi(
1774                                    repo, 5, &terminusHdl, &entityType,
1775                                    &entityInstanceNum, &containerId)));
1776     EXPECT_EQ(entityType, 2);
1777     EXPECT_EQ(entityInstanceNum, 90);
1778 
1779     auto l6 = pldm_entity_association_tree_add(tree, &entities[6], 90, node,
1780                                                PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1781     EXPECT_EQ(l6, nullptr);
1782 
1783     auto l7 = pldm_entity_association_tree_add(tree, &entities[7], 100, l1,
1784                                                PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1785     uint32_t seventh = 7;
1786     EXPECT_EQ(pldm_pdr_add_fru_record_set(repo, 1, 7, entities[7].entity_type,
1787                                           entities[7].entity_instance_num,
1788                                           entities[7].entity_container_id,
1789                                           &seventh),
1790               0);
1791     EXPECT_NE(l7, nullptr);
1792     EXPECT_EQ(entities[7].entity_instance_num, 100);
1793     EXPECT_EQ(seventh, pldm_pdr_get_record_handle(
1794                            repo, pldm_pdr_fru_record_set_find_by_rsi(
1795                                      repo, 7, &terminusHdl, &entityType,
1796                                      &entityInstanceNum, &containerId)));
1797     EXPECT_EQ(entityType, 3);
1798     EXPECT_EQ(entityInstanceNum, 100);
1799 
1800     auto l8 = pldm_entity_association_tree_add(tree, &entities[8], 100, l2,
1801                                                PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1802     uint32_t eighth = 8;
1803     EXPECT_EQ(pldm_pdr_add_fru_record_set(repo, 1, 8, entities[8].entity_type,
1804                                           entities[8].entity_instance_num,
1805                                           entities[8].entity_container_id,
1806                                           &eighth),
1807               0);
1808     EXPECT_NE(l8, nullptr);
1809     EXPECT_EQ(entities[8].entity_instance_num, 100);
1810     EXPECT_EQ(eighth, pldm_pdr_get_record_handle(
1811                           repo, pldm_pdr_fru_record_set_find_by_rsi(
1812                                     repo, 8, &terminusHdl, &entityType,
1813                                     &entityInstanceNum, &containerId)));
1814     EXPECT_EQ(entityType, 3);
1815     EXPECT_EQ(entityInstanceNum, 100);
1816 
1817     pldm_pdr_destroy(repo);
1818     pldm_entity_association_tree_destroy(tree);
1819 }
1820 
1821 #ifdef LIBPLDM_API_TESTING
TEST(EntityAssociationPDR,testFindChildContainerID)1822 TEST(EntityAssociationPDR, testFindChildContainerID)
1823 {
1824     pldm_entity entities[3]{};
1825     entities[0].entity_type = 1;
1826     entities[1].entity_type = 2;
1827     entities[2].entity_type = 3;
1828     entities[1].entity_container_id = 2;
1829     entities[1].entity_instance_num = 1;
1830 
1831     auto tree = pldm_entity_association_tree_init();
1832     auto l1 = pldm_entity_association_tree_add_entity(
1833         tree, &entities[0], 0xffff, nullptr, PLDM_ENTITY_ASSOCIAION_LOGICAL,
1834         false, true, 0xffff);
1835 
1836     EXPECT_NE(l1, nullptr);
1837     auto l2 = pldm_entity_association_tree_add_entity(
1838         tree, &entities[1], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
1839         false, 0xffff);
1840     EXPECT_NE(l2, nullptr);
1841     auto l3 = pldm_entity_association_tree_add_entity(
1842         tree, &entities[2], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
1843         true, 0xffff);
1844     EXPECT_NE(l3, nullptr);
1845 
1846     EXPECT_EQ(pldm_entity_get_num_children(l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL),
1847               2);
1848 
1849     auto repo = pldm_pdr_init();
1850     int rc = pldm_entity_association_pdr_add(tree, repo, false, 1);
1851     ASSERT_EQ(rc, 0);
1852 
1853     EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
1854 
1855     uint16_t container_id{};
1856     pldm_pdr_find_child_container_id_index_range_exclude(
1857         repo, 1, 1, 0, 0x01000000, 0x01ffffff, &container_id);
1858     EXPECT_EQ(container_id, 2);
1859 
1860     uint16_t container_id1{};
1861     pldm_pdr_find_child_container_id_index_range_exclude(
1862         repo, 1, 1, 0, 0x00000001, 0x00ffffff, &container_id1);
1863     EXPECT_EQ(container_id1, 0);
1864 
1865     pldm_pdr_destroy(repo);
1866     pldm_entity_association_tree_destroy(tree);
1867 }
1868 #endif
1869 
TEST(EntityAssociationPDR,testNodeAddCheck)1870 TEST(EntityAssociationPDR, testNodeAddCheck)
1871 {
1872     // pldm_entity entities[4]{};
1873     pldm_entity* entities = (pldm_entity*)malloc(sizeof(pldm_entity) * 4);
1874     entities[0].entity_type = 1;
1875     entities[1].entity_type = 2;
1876     entities[2].entity_type = 2;
1877     entities[3].entity_type = 3;
1878 
1879     auto tree = pldm_entity_association_tree_init();
1880 
1881     auto l1 = pldm_entity_association_tree_add(
1882         tree, &entities[0], 0xffff, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1883     EXPECT_NE(l1, nullptr);
1884     auto l2a = pldm_entity_association_tree_add(
1885         tree, &entities[1], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1886     EXPECT_NE(l2a, nullptr);
1887     auto l2b = pldm_entity_association_tree_add(
1888         tree, &entities[2], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1889     EXPECT_NE(l2b, nullptr);
1890     auto l2c = pldm_entity_association_tree_add(
1891         tree, &entities[3], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1892     EXPECT_NE(l2c, nullptr);
1893 
1894     auto repo = pldm_pdr_init();
1895 
1896     EXPECT_EQ(pldm_entity_association_pdr_add_from_node_with_record_handle(
1897                   l1, repo, &entities, 4, false, 1, 0),
1898               0);
1899     EXPECT_EQ(pldm_entity_association_pdr_add_from_node_with_record_handle(
1900                   l1, repo, &entities, 4, false, 1, 2),
1901               0);
1902     EXPECT_EQ(pldm_entity_association_pdr_add_from_node_with_record_handle(
1903                   l1, repo, &entities, 4, false, 1, 23),
1904               0);
1905     EXPECT_EQ(pldm_entity_association_pdr_add_from_node_with_record_handle(
1906                   l1, repo, &entities, 4, false, 1, 34),
1907               0);
1908 
1909     EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
1910 
1911     uint8_t* outData = nullptr;
1912     uint32_t size{};
1913     uint32_t nextRecHdl{};
1914     auto hdl = pldm_pdr_find_record(repo, 0, &outData, &size, &nextRecHdl);
1915     EXPECT_NE(hdl, nullptr);
1916 
1917     outData = nullptr;
1918     auto hdl1 = pldm_pdr_find_record(repo, 2, &outData, &size, &nextRecHdl);
1919     EXPECT_NE(hdl1, nullptr);
1920     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1921     pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(outData);
1922     EXPECT_EQ(hdr->record_handle, 2);
1923 
1924     outData = nullptr;
1925     auto hdl2 = pldm_pdr_find_record(repo, 23, &outData, &size, &nextRecHdl);
1926     EXPECT_NE(hdl2, nullptr);
1927     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1928     pldm_pdr_hdr* hdr1 = reinterpret_cast<pldm_pdr_hdr*>(outData);
1929     EXPECT_EQ(hdr1->record_handle, 23);
1930 
1931     outData = nullptr;
1932     auto hdl3 = pldm_pdr_find_record(repo, 3, &outData, &size, &nextRecHdl);
1933     EXPECT_EQ(hdl3, nullptr);
1934 
1935     free(entities);
1936     pldm_pdr_destroy(repo);
1937     pldm_entity_association_tree_destroy(tree);
1938 }
1939 
1940 #ifdef LIBPLDM_API_TESTING
TEST(EntityAssociationPDR,testAddContainedEntityRemotePDR)1941 TEST(EntityAssociationPDR, testAddContainedEntityRemotePDR)
1942 {
1943     // pldm_entity entities[5]{};
1944     pldm_entity* entities = (pldm_entity*)malloc(sizeof(pldm_entity) * 5);
1945     entities[0].entity_type = 1;
1946     entities[1].entity_type = 2;
1947     entities[2].entity_type = 3;
1948     entities[3].entity_type = 4;
1949     entities[4].entity_type = 5;
1950 
1951     auto tree = pldm_entity_association_tree_init();
1952     auto l1 = pldm_entity_association_tree_add(
1953         tree, &entities[0], 0xffff, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1954     EXPECT_NE(l1, nullptr);
1955     auto l2a = pldm_entity_association_tree_add(
1956         tree, &entities[1], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1957     EXPECT_NE(l2a, nullptr);
1958     auto l2b = pldm_entity_association_tree_add(
1959         tree, &entities[2], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1960     EXPECT_NE(l2b, nullptr);
1961     auto l2c = pldm_entity_association_tree_add(
1962         tree, &entities[3], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1963     EXPECT_NE(l2c, nullptr);
1964     auto l3a = pldm_entity_association_tree_add(
1965         tree, &entities[4], 0xffff, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1966     EXPECT_NE(l3a, nullptr);
1967 
1968     auto repo = pldm_pdr_init();
1969 
1970     EXPECT_EQ(pldm_entity_association_pdr_add_from_node_with_record_handle(
1971                   l1, repo, &entities, 5, false, 1, 0),
1972               0);
1973     EXPECT_EQ(pldm_entity_association_pdr_add_from_node_with_record_handle(
1974                   l1, repo, &entities, 5, false, 1, 2),
1975               0);
1976     EXPECT_EQ(pldm_entity_association_pdr_add_from_node_with_record_handle(
1977                   l1, repo, &entities, 5, false, 1, 23),
1978               0);
1979     EXPECT_EQ(pldm_entity_association_pdr_add_from_node_with_record_handle(
1980                   l1, repo, &entities, 5, false, 1, 34),
1981               0);
1982     EXPECT_EQ(pldm_entity_association_pdr_add_from_node_with_record_handle(
1983                   l2a, repo, &entities, 5, false, 1, 3),
1984               0);
1985 
1986     pldm_entity entity1[1];
1987     entity1[0].entity_type = 2;
1988 
1989     EXPECT_EQ(pldm_entity_association_pdr_add_contained_entity_to_remote_pdr(
1990                   repo, &entity1[0], 2),
1991               0);
1992 
1993     free(entities);
1994     pldm_pdr_destroy(repo);
1995     pldm_entity_association_tree_destroy(tree);
1996 }
1997 #endif
1998 
1999 #ifdef LIBPLDM_API_TESTING
TEST(EntityAssociationPDR,testAddContainedEntityNew)2000 TEST(EntityAssociationPDR, testAddContainedEntityNew)
2001 {
2002     // pldm_entity entities[5]{};
2003     pldm_entity* entities = (pldm_entity*)malloc(sizeof(pldm_entity) * 5);
2004     entities[0].entity_type = 1;
2005     entities[1].entity_type = 2;
2006     entities[2].entity_type = 3;
2007     entities[3].entity_type = 4;
2008     entities[4].entity_type = 5;
2009 
2010     auto tree = pldm_entity_association_tree_init();
2011     auto l1 = pldm_entity_association_tree_add(
2012         tree, &entities[0], 0xffff, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
2013     EXPECT_NE(l1, nullptr);
2014     auto l2a = pldm_entity_association_tree_add(
2015         tree, &entities[1], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
2016     EXPECT_NE(l2a, nullptr);
2017     auto l2b = pldm_entity_association_tree_add(
2018         tree, &entities[2], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
2019     EXPECT_NE(l2b, nullptr);
2020     auto l2c = pldm_entity_association_tree_add(
2021         tree, &entities[3], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
2022     EXPECT_NE(l2c, nullptr);
2023     auto l3a = pldm_entity_association_tree_add(
2024         tree, &entities[4], 0xffff, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
2025     EXPECT_NE(l3a, nullptr);
2026 
2027     auto repo = pldm_pdr_init();
2028 
2029     EXPECT_EQ(pldm_entity_association_pdr_add_from_node_with_record_handle(
2030                   l1, repo, &entities, 5, false, 1, 0),
2031               0);
2032     EXPECT_EQ(pldm_entity_association_pdr_add_from_node_with_record_handle(
2033                   l1, repo, &entities, 5, false, 1, 2),
2034               0);
2035     EXPECT_EQ(pldm_entity_association_pdr_add_from_node_with_record_handle(
2036                   l1, repo, &entities, 5, false, 1, 23),
2037               0);
2038     EXPECT_EQ(pldm_entity_association_pdr_add_from_node_with_record_handle(
2039                   l1, repo, &entities, 5, false, 1, 34),
2040               0);
2041     EXPECT_EQ(pldm_entity_association_pdr_add_from_node_with_record_handle(
2042                   l2a, repo, &entities, 5, false, 1, 3),
2043               0);
2044 
2045     uint32_t updated_record_handle = 0;
2046 
2047     pldm_entity entity2[1]{};
2048     entity2[0].entity_type = 4;
2049 
2050     pldm_entity entity3[1]{};
2051     entity3[0].entity_type = 4;
2052 
2053     EXPECT_EQ(pldm_entity_association_pdr_create_new(
2054                   repo, 34, &entity2[0], &entity3[0], &updated_record_handle),
2055               0);
2056 
2057     EXPECT_EQ(updated_record_handle, 35);
2058 
2059     free(entities);
2060     pldm_pdr_destroy(repo);
2061     pldm_entity_association_tree_destroy(tree);
2062 }
2063 #endif
2064 
2065 #ifdef LIBPLDM_API_TESTING
TEST(EntityAssociationPDR,testRemoveContainedEntity)2066 TEST(EntityAssociationPDR, testRemoveContainedEntity)
2067 {
2068     struct pldm_entity entities[4] = {
2069         {.entity_type = 1, .entity_instance_num = 1, .entity_container_id = 2},
2070         {.entity_type = 2, .entity_instance_num = 1, .entity_container_id = 2},
2071         {.entity_type = 3, .entity_instance_num = 3, .entity_container_id = 3},
2072         {.entity_type = 4, .entity_instance_num = 1, .entity_container_id = 2}};
2073     struct pldm_entity* base = entities;
2074 
2075     auto tree = pldm_entity_association_tree_init();
2076     auto l1 = pldm_entity_association_tree_add_entity(
2077         tree, &entities[0], 0xffff, nullptr, PLDM_ENTITY_ASSOCIAION_LOGICAL,
2078         false, true, 0xffff);
2079 
2080     EXPECT_NE(l1, nullptr);
2081     auto l2 = pldm_entity_association_tree_add_entity(
2082         tree, &entities[1], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
2083         false, 0xffff);
2084     EXPECT_NE(l2, nullptr);
2085     auto l3 = pldm_entity_association_tree_add_entity(
2086         tree, &entities[2], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
2087         true, 0xffff);
2088     EXPECT_NE(l3, nullptr);
2089     auto l4 = pldm_entity_association_tree_add_entity(
2090         tree, &entities[3], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
2091         true, 0xffff);
2092     EXPECT_NE(l4, nullptr);
2093 
2094     EXPECT_EQ(pldm_entity_get_num_children(l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL),
2095               3);
2096 
2097     auto repo = pldm_pdr_init();
2098 
2099     EXPECT_EQ(pldm_entity_association_pdr_add_from_node_with_record_handle(
2100                   l1, repo, &base, 4, false, 1, 3),
2101               0);
2102 
2103     EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
2104 
2105     uint32_t removed_record_handle{};
2106     struct pldm_entity entity[1] = {
2107         {.entity_type = 2, .entity_instance_num = 1, .entity_container_id = 2}};
2108 
2109     EXPECT_EQ(pldm_entity_association_pdr_remove_contained_entity(
2110                   repo, entity, false, &removed_record_handle),
2111               0);
2112     EXPECT_EQ(removed_record_handle, 3);
2113 
2114     pldm_pdr_destroy(repo);
2115     pldm_entity_association_tree_destroy(tree);
2116 }
2117 #endif
2118 
2119 #ifdef LIBPLDM_API_TESTING
TEST(PDRUpdate,testRemoveFruRecord)2120 TEST(PDRUpdate, testRemoveFruRecord)
2121 {
2122     auto repo = pldm_pdr_init();
2123 
2124     uint32_t record_handle = 1;
2125     int rc = pldm_pdr_add_fru_record_set(repo, 1, 1, 1, 0, 100, &record_handle);
2126     EXPECT_EQ(rc, 0);
2127     record_handle = 2;
2128     rc = pldm_pdr_add_fru_record_set(repo, 1, 2, 1, 1, 100, &record_handle);
2129     EXPECT_EQ(rc, 0);
2130     record_handle = 3;
2131     rc = pldm_pdr_add_fru_record_set(repo, 1, 3, 1, 2, 100, &record_handle);
2132     EXPECT_EQ(rc, 0);
2133     EXPECT_EQ(pldm_pdr_get_record_count(repo), 3);
2134 
2135     uint32_t removed_record_handle{};
2136     rc = pldm_pdr_remove_fru_record_set_by_rsi(repo, 2, false,
2137                                                &removed_record_handle);
2138     EXPECT_EQ(rc, 0);
2139     EXPECT_EQ(removed_record_handle, 2);
2140     EXPECT_EQ(pldm_pdr_get_record_count(repo), 2);
2141 
2142     uint16_t terminusHdl{};
2143     uint16_t entityType{};
2144     uint16_t entityInstanceNum{};
2145     uint16_t containerId{};
2146     auto record = pldm_pdr_fru_record_set_find_by_rsi(
2147         repo, 1, &terminusHdl, &entityType, &entityInstanceNum, &containerId);
2148     EXPECT_NE(record, nullptr);
2149     record_handle = pldm_pdr_get_record_handle(repo, record);
2150     EXPECT_EQ(record_handle, 1);
2151 
2152     record = pldm_pdr_fru_record_set_find_by_rsi(
2153         repo, 3, &terminusHdl, &entityType, &entityInstanceNum, &containerId);
2154     EXPECT_NE(record, nullptr);
2155     record_handle = pldm_pdr_get_record_handle(repo, record);
2156     EXPECT_EQ(record_handle, 3);
2157 
2158     record = pldm_pdr_fru_record_set_find_by_rsi(
2159         repo, 2, &terminusHdl, &entityType, &entityInstanceNum, &containerId);
2160     EXPECT_EQ(record, nullptr);
2161 
2162     rc = pldm_pdr_remove_fru_record_set_by_rsi(repo, 1, false,
2163                                                &removed_record_handle);
2164     EXPECT_EQ(rc, 0);
2165     EXPECT_EQ(removed_record_handle, 1);
2166 
2167     // remove the same record again
2168     removed_record_handle = 5;
2169     rc = pldm_pdr_remove_fru_record_set_by_rsi(repo, 1, false,
2170                                                &removed_record_handle);
2171     EXPECT_EQ(rc, 0);
2172     EXPECT_NE(removed_record_handle, 1);
2173     EXPECT_EQ(removed_record_handle, 5);
2174 
2175     rc = pldm_pdr_remove_fru_record_set_by_rsi(repo, 3, false,
2176                                                &removed_record_handle);
2177     EXPECT_EQ(rc, 0);
2178     EXPECT_EQ(removed_record_handle, 3);
2179 
2180     EXPECT_EQ(pldm_pdr_get_record_count(repo), 0);
2181 
2182     pldm_pdr_destroy(repo);
2183 }
2184 #endif
2185