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