blob: f98f21f73e637314e023ba7bdd65bcdb558166fd [file] [log] [blame]
Manojkiran Eda9a8e4972022-11-28 16:38:21 +05301#include <endian.h>
Andrew Jefferyefb40062023-11-10 13:48:39 +10302#include <libpldm/pdr.h>
3#include <libpldm/platform.h>
Manojkiran Eda9a8e4972022-11-28 16:38:21 +05304
Andrew Jeffery9c766792022-08-10 23:12:49 +09305#include <array>
Manojkiran Eda9a8e4972022-11-28 16:38:21 +05306#include <cstdint>
Andrew Jeffery5a706072023-04-05 19:45:31 +09307#include <cstdlib>
8#include <cstring>
Manojkiran Eda9a8e4972022-11-28 16:38:21 +05309#include <vector>
Andrew Jeffery9c766792022-08-10 23:12:49 +093010
Andrew Jeffery9c766792022-08-10 23:12:49 +093011#include <gtest/gtest.h>
12
13TEST(PDRAccess, testInit)
14{
15 auto repo = pldm_pdr_init();
16 EXPECT_EQ(pldm_pdr_get_record_count(repo), 0u);
17 EXPECT_EQ(pldm_pdr_get_repo_size(repo), 0u);
18 pldm_pdr_destroy(repo);
19}
20
21TEST(PDRUpdate, testAdd)
22{
23 auto repo = pldm_pdr_init();
24
25 std::array<uint8_t, 10> data{};
Andrew Jefferyc79a5df2023-07-17 15:09:21 +093026 uint32_t handle = 0;
27 EXPECT_EQ(
28 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, &handle),
29 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +093030 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
Andrew Jefferyc79a5df2023-07-17 15:09:21 +093034 handle = 0;
35 EXPECT_EQ(
36 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, &handle),
37 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +093038 EXPECT_EQ(handle, 2u);
Andrew Jefferyc79a5df2023-07-17 15:09:21 +093039
40 handle = 0;
41 EXPECT_EQ(
42 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, &handle),
43 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +093044 EXPECT_EQ(handle, 3u);
Andrew Jefferyc79a5df2023-07-17 15:09:21 +093045
46 handle = 0xdeeddeedu;
47 EXPECT_EQ(
48 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, &handle),
49 0);
50 EXPECT_EQ(handle, 0xdeeddeed);
Andrew Jeffery9c766792022-08-10 23:12:49 +093051 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
52 EXPECT_EQ(pldm_pdr_get_repo_size(repo), data.size() * 4u);
53
54 pldm_pdr_destroy(repo);
55}
56
57TEST(PDRRemoveByTerminus, testRemoveByTerminus)
58{
59 std::array<uint8_t, 10> data{};
60
61 auto repo = pldm_pdr_init();
Andrew Jefferyc79a5df2023-07-17 15:09:21 +093062
63 EXPECT_EQ(
64 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +093065 pldm_pdr_remove_pdrs_by_terminus_handle(repo, 1);
66 EXPECT_EQ(pldm_pdr_get_record_count(repo), 0u);
67 pldm_pdr_destroy(repo);
68
69 repo = pldm_pdr_init();
Andrew Jefferyc79a5df2023-07-17 15:09:21 +093070 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 2, NULL),
71 0);
72 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
73 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +093074 EXPECT_EQ(pldm_pdr_get_record_count(repo), 2u);
75 pldm_pdr_remove_pdrs_by_terminus_handle(repo, 1);
76 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
77 pldm_pdr_destroy(repo);
78
79 repo = pldm_pdr_init();
Andrew Jefferyc79a5df2023-07-17 15:09:21 +093080 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
81 0);
82 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 2, NULL),
83 0);
84 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 2, NULL),
85 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +093086 EXPECT_EQ(pldm_pdr_get_record_count(repo), 3u);
87 pldm_pdr_remove_pdrs_by_terminus_handle(repo, 2);
88 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
89 pldm_pdr_destroy(repo);
90
91 repo = pldm_pdr_init();
Andrew Jefferyc79a5df2023-07-17 15:09:21 +093092 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
93 0);
94 EXPECT_EQ(
95 pldm_pdr_add_check(repo, data.data(), data.size(), false, 2, NULL), 0);
96 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
97 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +093098 EXPECT_EQ(pldm_pdr_get_record_count(repo), 3u);
99 pldm_pdr_remove_remote_pdrs(repo);
100 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
101 pldm_pdr_remove_pdrs_by_terminus_handle(repo, 1);
102 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
103 pldm_pdr_remove_pdrs_by_terminus_handle(repo, 2);
104 EXPECT_EQ(pldm_pdr_get_record_count(repo), 0u);
105 pldm_pdr_destroy(repo);
106}
107
108TEST(PDRUpdate, testRemove)
109{
110 std::array<uint8_t, 10> data{};
111
112 auto repo = pldm_pdr_init();
113 pldm_pdr_remove_remote_pdrs(repo);
114 EXPECT_EQ(pldm_pdr_get_record_count(repo), 0u);
115 pldm_pdr_destroy(repo);
116
117 repo = pldm_pdr_init();
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930118 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
119 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930120 pldm_pdr_remove_remote_pdrs(repo);
121 EXPECT_EQ(pldm_pdr_get_record_count(repo), 0u);
122 pldm_pdr_destroy(repo);
123
124 repo = pldm_pdr_init();
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930125 EXPECT_EQ(
126 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930127 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();
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930132 EXPECT_EQ(
133 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
134 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
135 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930136 pldm_pdr_remove_remote_pdrs(repo);
137 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
138 pldm_pdr_destroy(repo);
139
140 repo = pldm_pdr_init();
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930141 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
142 0);
143 EXPECT_EQ(
144 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930145 pldm_pdr_remove_remote_pdrs(repo);
146 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
147 pldm_pdr_destroy(repo);
148
149 repo = pldm_pdr_init();
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930150 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
151 0);
152 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
153 0);
154 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
155 0);
156 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
157 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930158 pldm_pdr_remove_remote_pdrs(repo);
159 EXPECT_EQ(pldm_pdr_get_record_count(repo), 0u);
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930160 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
161 0);
162 EXPECT_EQ(
163 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930164 EXPECT_EQ(pldm_pdr_get_record_count(repo), 2u);
165 pldm_pdr_destroy(repo);
166
167 repo = pldm_pdr_init();
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930168 EXPECT_EQ(
169 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
170 EXPECT_EQ(
171 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
172 EXPECT_EQ(
173 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
174 EXPECT_EQ(
175 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930176 pldm_pdr_remove_remote_pdrs(repo);
177 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930178 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
179 0);
180 EXPECT_EQ(
181 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930182 EXPECT_EQ(pldm_pdr_get_record_count(repo), 6u);
183 pldm_pdr_destroy(repo);
184
185 repo = pldm_pdr_init();
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930186 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
187 0);
188 EXPECT_EQ(
189 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
190 EXPECT_EQ(
191 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
192 EXPECT_EQ(
193 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930194 pldm_pdr_remove_remote_pdrs(repo);
195 EXPECT_EQ(pldm_pdr_get_record_count(repo), 3u);
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930196 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
197 0);
198 EXPECT_EQ(
199 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930200 EXPECT_EQ(pldm_pdr_get_record_count(repo), 5u);
201 pldm_pdr_destroy(repo);
202
203 repo = pldm_pdr_init();
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930204 EXPECT_EQ(
205 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
206 EXPECT_EQ(
207 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
208 EXPECT_EQ(
209 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
210 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
211 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930212 pldm_pdr_remove_remote_pdrs(repo);
213 EXPECT_EQ(pldm_pdr_get_record_count(repo), 3u);
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930214 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
215 0);
216 EXPECT_EQ(
217 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930218 EXPECT_EQ(pldm_pdr_get_record_count(repo), 5u);
219 pldm_pdr_destroy(repo);
220
221 repo = pldm_pdr_init();
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930222 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
223 0);
224 EXPECT_EQ(
225 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
226 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
227 0);
228 EXPECT_EQ(
229 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930230 pldm_pdr_remove_remote_pdrs(repo);
231 EXPECT_EQ(pldm_pdr_get_record_count(repo), 2u);
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930232 EXPECT_EQ(
233 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
234 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
235 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930236 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
237 pldm_pdr_destroy(repo);
238
239 repo = pldm_pdr_init();
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930240 EXPECT_EQ(
241 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
242 EXPECT_EQ(
243 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
244 EXPECT_EQ(
245 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
246 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
247 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930248 pldm_pdr_remove_remote_pdrs(repo);
249 EXPECT_EQ(pldm_pdr_get_record_count(repo), 3u);
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930250 EXPECT_EQ(
251 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
252 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
253 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930254 EXPECT_EQ(pldm_pdr_get_record_count(repo), 5u);
255 pldm_pdr_destroy(repo);
256
257 repo = pldm_pdr_init();
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930258 EXPECT_EQ(
259 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
260 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
261 0);
262 EXPECT_EQ(
263 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
264 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
265 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930266 pldm_pdr_remove_remote_pdrs(repo);
267 EXPECT_EQ(pldm_pdr_get_record_count(repo), 2u);
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930268 EXPECT_EQ(
269 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
270 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
271 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930272 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
273 pldm_pdr_destroy(repo);
274
275 repo = pldm_pdr_init();
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930276 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
277 0);
278 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
279 0);
280 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
281 0);
282 EXPECT_EQ(
283 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930284 pldm_pdr_remove_remote_pdrs(repo);
285 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930286 EXPECT_EQ(
287 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
288 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
289 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930290 EXPECT_EQ(pldm_pdr_get_record_count(repo), 3u);
291 pldm_pdr_destroy(repo);
292
293 repo = pldm_pdr_init();
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930294 EXPECT_EQ(
295 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
296 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
297 0);
298 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
299 0);
300 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
301 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930302 pldm_pdr_remove_remote_pdrs(repo);
303 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930304 EXPECT_EQ(
305 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
306 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
307 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930308 EXPECT_EQ(pldm_pdr_get_record_count(repo), 3u);
309 pldm_pdr_destroy(repo);
310
311 repo = pldm_pdr_init();
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930312 EXPECT_EQ(
313 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
314 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
315 0);
316 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
317 0);
318 EXPECT_EQ(
319 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930320 pldm_pdr_remove_remote_pdrs(repo);
321 EXPECT_EQ(pldm_pdr_get_record_count(repo), 2u);
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930322 EXPECT_EQ(
323 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
324 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
325 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930326 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
327 pldm_pdr_destroy(repo);
328
329 repo = pldm_pdr_init();
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930330 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
331 0);
332 EXPECT_EQ(
333 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
334 EXPECT_EQ(
335 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
336 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
337 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930338 pldm_pdr_remove_remote_pdrs(repo);
339 EXPECT_EQ(pldm_pdr_get_record_count(repo), 2u);
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930340 uint32_t handle = 0;
341 EXPECT_EQ(
342 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, &handle),
343 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930344 EXPECT_EQ(handle, 3u);
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930345 handle = 0;
346 EXPECT_EQ(
347 pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, &handle),
348 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930349 EXPECT_EQ(handle, 4u);
350 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
351 pldm_pdr_destroy(repo);
352}
353
354TEST(PDRAccess, testGet)
355{
356 auto repo = pldm_pdr_init();
357
358 std::array<uint32_t, 10> in{100, 345, 3, 6, 89, 0, 11, 45, 23434, 123123};
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930359 uint32_t handle = 1;
360 EXPECT_EQ(pldm_pdr_add_check(repo, reinterpret_cast<uint8_t*>(in.data()),
361 sizeof(in), false, 1, &handle),
362 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930363 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
364 EXPECT_EQ(pldm_pdr_get_repo_size(repo), sizeof(in));
365 uint32_t size{};
366 uint32_t nextRecHdl{};
367 uint8_t* outData = nullptr;
368 auto hdl = pldm_pdr_find_record(repo, 0, &outData, &size, &nextRecHdl);
369 EXPECT_NE(hdl, nullptr);
370 EXPECT_EQ(size, sizeof(in));
371 EXPECT_EQ(nextRecHdl, 0u);
372 EXPECT_EQ(memcmp(outData, in.data(), sizeof(in)), 0);
373 outData = nullptr;
374
375 auto hdl2 = pldm_pdr_find_record(repo, 1, &outData, &size, &nextRecHdl);
376 EXPECT_EQ(hdl, hdl2);
377 EXPECT_NE(hdl, nullptr);
378 EXPECT_EQ(size, sizeof(in));
379 EXPECT_EQ(nextRecHdl, 0u);
380 EXPECT_EQ(memcmp(outData, in.data(), sizeof(in)), 0);
381 outData = nullptr;
382
383 hdl = pldm_pdr_find_record(repo, htole32(0xdeaddead), &outData, &size,
384 &nextRecHdl);
385 EXPECT_EQ(hdl, nullptr);
386 EXPECT_EQ(size, 0u);
387 EXPECT_EQ(nextRecHdl, 0u);
388 EXPECT_EQ(outData, nullptr);
389 outData = nullptr;
390
391 std::array<uint32_t, 10> in2{1000, 3450, 30, 60, 890,
392 0, 110, 450, 234034, 123123};
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930393 handle = 2;
394 EXPECT_EQ(pldm_pdr_add_check(repo, reinterpret_cast<uint8_t*>(in2.data()),
395 sizeof(in2), false, 1, &handle),
396 0);
397 handle = 3;
398 EXPECT_EQ(pldm_pdr_add_check(repo, reinterpret_cast<uint8_t*>(in2.data()),
399 sizeof(in2), false, 1, &handle),
400 0);
401 handle = 4;
402 EXPECT_EQ(pldm_pdr_add_check(repo, reinterpret_cast<uint8_t*>(in2.data()),
403 sizeof(in2), true, 1, &handle),
404 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930405 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
406 EXPECT_EQ(pldm_pdr_get_repo_size(repo), sizeof(in2) * 4);
407 hdl = pldm_pdr_find_record(repo, 0, &outData, &size, &nextRecHdl);
408 EXPECT_NE(hdl, nullptr);
409 EXPECT_EQ(size, sizeof(in));
410 EXPECT_EQ(nextRecHdl, 2u);
411 EXPECT_EQ(memcmp(outData, in.data(), sizeof(in)), 0);
412 outData = nullptr;
413 hdl2 = pldm_pdr_find_record(repo, 1, &outData, &size, &nextRecHdl);
414 EXPECT_EQ(hdl, hdl2);
415 EXPECT_NE(hdl, nullptr);
416 EXPECT_EQ(size, sizeof(in));
417 EXPECT_EQ(nextRecHdl, 2u);
418 EXPECT_EQ(memcmp(outData, in.data(), sizeof(in)), 0);
419 outData = nullptr;
420 hdl = pldm_pdr_find_record(repo, 2, &outData, &size, &nextRecHdl);
421 EXPECT_NE(hdl, nullptr);
422 EXPECT_EQ(size, sizeof(in2));
423 EXPECT_EQ(nextRecHdl, 3u);
424 EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
425 outData = nullptr;
426 hdl = pldm_pdr_find_record(repo, 3, &outData, &size, &nextRecHdl);
427 EXPECT_NE(hdl, nullptr);
428 EXPECT_EQ(pldm_pdr_record_is_remote(hdl), false);
429 EXPECT_EQ(size, sizeof(in2));
430 EXPECT_EQ(nextRecHdl, 4u);
431 EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
432 outData = nullptr;
433 hdl = pldm_pdr_find_record(repo, 4, &outData, &size, &nextRecHdl);
434 EXPECT_NE(hdl, nullptr);
435 EXPECT_EQ(pldm_pdr_record_is_remote(hdl), true);
436 EXPECT_EQ(size, sizeof(in2));
437 EXPECT_EQ(nextRecHdl, 0u);
438 EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
439 outData = nullptr;
440
441 pldm_pdr_destroy(repo);
442}
443
444TEST(PDRAccess, testGetNext)
445{
446 auto repo = pldm_pdr_init();
447
448 std::array<uint32_t, 10> in{100, 345, 3, 6, 89, 0, 11, 45, 23434, 123123};
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930449 uint32_t handle = 1;
450 EXPECT_EQ(pldm_pdr_add_check(repo, reinterpret_cast<uint8_t*>(in.data()),
451 sizeof(in), false, 1, &handle),
452 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930453 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
454 EXPECT_EQ(pldm_pdr_get_repo_size(repo), sizeof(in));
455 uint32_t size{};
456 uint32_t nextRecHdl{};
457 uint8_t* outData = nullptr;
458 auto hdl = pldm_pdr_find_record(repo, 0, &outData, &size, &nextRecHdl);
459 EXPECT_NE(hdl, nullptr);
460 EXPECT_EQ(size, sizeof(in));
461 EXPECT_EQ(nextRecHdl, 0u);
462 EXPECT_EQ(memcmp(outData, in.data(), sizeof(in)), 0);
463 outData = nullptr;
464
465 std::array<uint32_t, 10> in2{1000, 3450, 30, 60, 890,
466 0, 110, 450, 234034, 123123};
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930467 handle = 2;
468 EXPECT_EQ(pldm_pdr_add_check(repo, reinterpret_cast<uint8_t*>(in2.data()),
469 sizeof(in2), false, 1, &handle),
470 0);
471 handle = 3;
472 EXPECT_EQ(pldm_pdr_add_check(repo, reinterpret_cast<uint8_t*>(in2.data()),
473 sizeof(in2), false, 1, &handle),
474 0);
475 handle = 4;
476 EXPECT_EQ(pldm_pdr_add_check(repo, reinterpret_cast<uint8_t*>(in2.data()),
477 sizeof(in2), false, 1, &handle),
478 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930479 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
480 EXPECT_EQ(pldm_pdr_get_repo_size(repo), sizeof(in2) * 4);
481 hdl = pldm_pdr_get_next_record(repo, hdl, &outData, &size, &nextRecHdl);
482 EXPECT_NE(hdl, nullptr);
483 EXPECT_EQ(size, sizeof(in2));
484 EXPECT_EQ(nextRecHdl, 3u);
485 EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
486 outData = nullptr;
487 hdl = pldm_pdr_get_next_record(repo, hdl, &outData, &size, &nextRecHdl);
488 EXPECT_NE(hdl, nullptr);
489 EXPECT_EQ(size, sizeof(in2));
490 EXPECT_EQ(nextRecHdl, 4u);
491 EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
492 outData = nullptr;
493 hdl = pldm_pdr_get_next_record(repo, hdl, &outData, &size, &nextRecHdl);
494 EXPECT_NE(hdl, nullptr);
495 EXPECT_EQ(size, sizeof(in2));
496 EXPECT_EQ(nextRecHdl, 0u);
497 EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
498 outData = nullptr;
499
500 pldm_pdr_destroy(repo);
501}
502
503TEST(PDRAccess, testFindByType)
504{
505 auto repo = pldm_pdr_init();
506
507 std::array<uint8_t, sizeof(pldm_pdr_hdr)> data{};
508 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(data.data());
509 hdr->type = 1;
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930510 uint32_t first = 0;
511 EXPECT_EQ(
512 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, &first),
513 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930514 hdr->type = 2;
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930515 uint32_t second = 0;
516 EXPECT_EQ(
517 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, &second),
518 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930519 hdr->type = 3;
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930520 uint32_t third = 0;
521 EXPECT_EQ(
522 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, &third),
523 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930524 hdr->type = 4;
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930525 uint32_t fourth = 0;
526 EXPECT_EQ(
527 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, &fourth),
528 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930529
530 uint8_t* outData = nullptr;
531 uint32_t size{};
532 auto firstRec =
533 pldm_pdr_find_record_by_type(repo, 1, nullptr, &outData, &size);
534 EXPECT_EQ(pldm_pdr_get_record_handle(repo, firstRec), first);
535 outData = nullptr;
536 auto secondRec =
537 pldm_pdr_find_record_by_type(repo, 2, nullptr, &outData, &size);
538 EXPECT_EQ(pldm_pdr_get_record_handle(repo, secondRec), second);
539 outData = nullptr;
540 auto thirdRec =
541 pldm_pdr_find_record_by_type(repo, 3, nullptr, &outData, &size);
542 EXPECT_EQ(pldm_pdr_get_record_handle(repo, thirdRec), third);
543 outData = nullptr;
544 auto fourthRec =
545 pldm_pdr_find_record_by_type(repo, 4, nullptr, &outData, &size);
546 EXPECT_EQ(pldm_pdr_get_record_handle(repo, fourthRec), fourth);
547 outData = nullptr;
548 auto fifthRec =
549 pldm_pdr_find_record_by_type(repo, 5, nullptr, &outData, &size);
550 EXPECT_EQ(fifthRec, nullptr);
551 EXPECT_EQ(outData, nullptr);
552 EXPECT_EQ(size, 0u);
553
554 auto rec =
555 pldm_pdr_find_record_by_type(repo, 3, secondRec, &outData, &size);
556 EXPECT_EQ(pldm_pdr_get_record_handle(repo, rec), third);
557 outData = nullptr;
558 rec = pldm_pdr_find_record_by_type(repo, 4, secondRec, &outData, &size);
559 EXPECT_EQ(pldm_pdr_get_record_handle(repo, rec), fourth);
560 outData = nullptr;
561 rec = pldm_pdr_find_record_by_type(repo, 2, firstRec, &outData, &size);
562 EXPECT_EQ(pldm_pdr_get_record_handle(repo, rec), second);
563 outData = nullptr;
564
565 pldm_pdr_destroy(repo);
566}
567
568TEST(PDRUpdate, testAddFruRecordSet)
569{
570 auto repo = pldm_pdr_init();
571
Andrew Jeffery14d29842023-07-17 15:29:32 +0930572 uint32_t handle = 0;
573 EXPECT_EQ(
574 pldm_pdr_add_fru_record_set_check(repo, 1, 10, 1, 0, 100, &handle), 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930575 EXPECT_EQ(handle, 1u);
576 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
577 EXPECT_EQ(pldm_pdr_get_repo_size(repo),
578 sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_fru_record_set));
579 uint32_t size{};
580 uint32_t nextRecHdl{};
581 uint8_t* outData = nullptr;
582 auto hdl = pldm_pdr_find_record(repo, 0, &outData, &size, &nextRecHdl);
583 EXPECT_NE(hdl, nullptr);
584 EXPECT_EQ(size, sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_fru_record_set));
585 EXPECT_EQ(nextRecHdl, 0u);
586 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(outData);
587 EXPECT_EQ(hdr->version, 1u);
588 EXPECT_EQ(hdr->type, PLDM_PDR_FRU_RECORD_SET);
589 EXPECT_EQ(hdr->length, htole16(sizeof(pldm_pdr_fru_record_set)));
590 EXPECT_EQ(hdr->record_handle, htole32(1));
591 pldm_pdr_fru_record_set* fru = reinterpret_cast<pldm_pdr_fru_record_set*>(
592 outData + sizeof(pldm_pdr_hdr));
593 EXPECT_EQ(fru->terminus_handle, htole16(1));
594 EXPECT_EQ(fru->fru_rsi, htole16(10));
595 EXPECT_EQ(fru->entity_type, htole16(1));
596 EXPECT_EQ(fru->entity_instance_num, htole16(0));
597 EXPECT_EQ(fru->container_id, htole16(100));
598 outData = nullptr;
599
Andrew Jeffery14d29842023-07-17 15:29:32 +0930600 handle = 0;
601 EXPECT_EQ(
602 pldm_pdr_add_fru_record_set_check(repo, 2, 11, 2, 1, 101, &handle), 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930603 EXPECT_EQ(handle, 2u);
604 EXPECT_EQ(pldm_pdr_get_record_count(repo), 2u);
605 EXPECT_EQ(pldm_pdr_get_repo_size(repo),
606 2 * (sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_fru_record_set)));
607 hdl = pldm_pdr_find_record(repo, 2, &outData, &size, &nextRecHdl);
608 EXPECT_NE(hdl, nullptr);
609 EXPECT_EQ(size, sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_fru_record_set));
610 EXPECT_EQ(nextRecHdl, 0u);
611 hdr = reinterpret_cast<pldm_pdr_hdr*>(outData);
612 EXPECT_EQ(hdr->version, 1u);
613 EXPECT_EQ(hdr->type, PLDM_PDR_FRU_RECORD_SET);
614 EXPECT_EQ(hdr->length, htole16(sizeof(pldm_pdr_fru_record_set)));
615 EXPECT_EQ(hdr->record_handle, htole32(2));
616 fru = reinterpret_cast<pldm_pdr_fru_record_set*>(outData +
617 sizeof(pldm_pdr_hdr));
618 EXPECT_EQ(fru->terminus_handle, htole16(2));
619 EXPECT_EQ(fru->fru_rsi, htole16(11));
620 EXPECT_EQ(fru->entity_type, htole16(2));
621 EXPECT_EQ(fru->entity_instance_num, htole16(1));
622 EXPECT_EQ(fru->container_id, htole16(101));
623 outData = nullptr;
624
625 hdl = pldm_pdr_find_record(repo, 1, &outData, &size, &nextRecHdl);
626 EXPECT_NE(hdl, nullptr);
627 EXPECT_EQ(size, sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_fru_record_set));
628 EXPECT_EQ(nextRecHdl, 2u);
629 hdr = reinterpret_cast<pldm_pdr_hdr*>(outData);
630 EXPECT_EQ(hdr->version, 1u);
631 EXPECT_EQ(hdr->type, PLDM_PDR_FRU_RECORD_SET);
632 EXPECT_EQ(hdr->length, htole16(sizeof(pldm_pdr_fru_record_set)));
633 EXPECT_EQ(hdr->record_handle, htole32(1));
634 fru = reinterpret_cast<pldm_pdr_fru_record_set*>(outData +
635 sizeof(pldm_pdr_hdr));
636 EXPECT_EQ(fru->terminus_handle, htole16(1));
637 EXPECT_EQ(fru->fru_rsi, htole16(10));
638 EXPECT_EQ(fru->entity_type, htole16(1));
639 EXPECT_EQ(fru->entity_instance_num, htole16(0));
640 EXPECT_EQ(fru->container_id, htole16(100));
641 outData = nullptr;
642
643 pldm_pdr_destroy(repo);
644}
645
646TEST(PDRUpdate, tesFindtFruRecordSet)
647{
648 auto repo = pldm_pdr_init();
649
650 uint16_t terminusHdl{};
651 uint16_t entityType{};
652 uint16_t entityInstanceNum{};
653 uint16_t containerId{};
Andrew Jeffery14d29842023-07-17 15:29:32 +0930654 uint32_t first = 1;
655 EXPECT_EQ(pldm_pdr_add_fru_record_set_check(repo, 1, 1, 1, 0, 100, &first),
656 0);
657 uint32_t second = 2;
658 EXPECT_EQ(pldm_pdr_add_fru_record_set_check(repo, 1, 2, 1, 1, 100, &second),
659 0);
660 uint32_t third = 3;
661 EXPECT_EQ(pldm_pdr_add_fru_record_set_check(repo, 1, 3, 1, 2, 100, &third),
662 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930663 EXPECT_EQ(first, pldm_pdr_get_record_handle(
664 repo, pldm_pdr_fru_record_set_find_by_rsi(
665 repo, 1, &terminusHdl, &entityType,
666 &entityInstanceNum, &containerId)));
667 EXPECT_EQ(second, pldm_pdr_get_record_handle(
668 repo, pldm_pdr_fru_record_set_find_by_rsi(
669 repo, 2, &terminusHdl, &entityType,
670 &entityInstanceNum, &containerId)));
671 EXPECT_EQ(third, pldm_pdr_get_record_handle(
672 repo, pldm_pdr_fru_record_set_find_by_rsi(
673 repo, 3, &terminusHdl, &entityType,
674 &entityInstanceNum, &containerId)));
675 EXPECT_EQ(terminusHdl, 1u);
676 EXPECT_EQ(entityType, 1u);
677 EXPECT_EQ(entityInstanceNum, 2u);
678 EXPECT_EQ(containerId, 100u);
679 EXPECT_EQ(nullptr, pldm_pdr_fru_record_set_find_by_rsi(
680 repo, 4, &terminusHdl, &entityType,
681 &entityInstanceNum, &containerId));
682
683 pldm_pdr_destroy(repo);
684}
685
Pavithra Barithaya4d694342023-05-19 08:04:41 -0500686#ifdef LIBPLDM_API_TESTING
687TEST(PDRUpdate, testFindLastInRange)
688{
689 auto repo = pldm_pdr_init();
690
691 std::array<uint8_t, 10> data{};
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930692 uint32_t handle1 = 0;
693 EXPECT_EQ(
694 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, &handle1),
695 0);
696 uint32_t handle2 = 23;
697 EXPECT_EQ(
698 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, &handle2),
699 0);
700 uint32_t handle3 = 77;
701 EXPECT_EQ(
702 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, &handle3),
703 0);
704 uint32_t handle4 = 16777325;
705 EXPECT_EQ(
706 pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, &handle4),
707 0);
708 uint32_t handle5 = 16777344;
709 EXPECT_EQ(
710 pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, &handle5),
711 0);
Pavithra Barithaya4d694342023-05-19 08:04:41 -0500712
713 auto rec1 = pldm_pdr_find_last_in_range(repo, 0, 100);
714 auto rec2 = pldm_pdr_find_last_in_range(repo, 16777300, 33554431);
715 EXPECT_NE(rec1, nullptr);
716 EXPECT_NE(rec2, nullptr);
717 EXPECT_NE(handle1, pldm_pdr_get_record_handle(repo, rec1));
718 EXPECT_NE(handle2, pldm_pdr_get_record_handle(repo, rec1));
719 EXPECT_EQ(handle3, pldm_pdr_get_record_handle(repo, rec1));
720 EXPECT_NE(handle4, pldm_pdr_get_record_handle(repo, rec2));
721 EXPECT_EQ(handle5, pldm_pdr_get_record_handle(repo, rec2));
722
723 pldm_pdr_destroy(repo);
724}
725#endif
726
Andrew Jeffery9c766792022-08-10 23:12:49 +0930727TEST(EntityAssociationPDR, testInit)
728{
729 auto tree = pldm_entity_association_tree_init();
730 EXPECT_NE(tree, nullptr);
731 pldm_entity_association_tree_destroy(tree);
732}
733
734TEST(EntityAssociationPDR, testBuild)
735{
736 // 1
737 // |
738 // 2--3--4
739 // |
740 // 5--6--7
741 // | |
742 // 9 8
743
744 pldm_entity entities[9]{};
745
746 entities[0].entity_type = 1;
747 entities[1].entity_type = 2;
748 entities[2].entity_type = 2;
749 entities[3].entity_type = 3;
750 entities[4].entity_type = 4;
751 entities[5].entity_type = 5;
752 entities[6].entity_type = 5;
753 entities[7].entity_type = 6;
754 entities[8].entity_type = 7;
755
756 auto tree = pldm_entity_association_tree_init();
757
758 auto l1 = pldm_entity_association_tree_add(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -0600759 tree, &entities[0], 0xffff, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930760 EXPECT_NE(l1, nullptr);
761 auto l2a = pldm_entity_association_tree_add(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -0600762 tree, &entities[1], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930763 EXPECT_NE(l2a, nullptr);
764 auto l2b = pldm_entity_association_tree_add(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -0600765 tree, &entities[2], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930766 EXPECT_NE(l2b, nullptr);
767 auto l2c = pldm_entity_association_tree_add(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -0600768 tree, &entities[3], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930769 EXPECT_NE(l2c, nullptr);
770 auto l3a = pldm_entity_association_tree_add(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -0600771 tree, &entities[4], 0xffff, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930772 EXPECT_NE(l3a, nullptr);
773 auto l3b = pldm_entity_association_tree_add(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -0600774 tree, &entities[5], 0xffff, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930775 EXPECT_NE(l3b, nullptr);
776 auto l3c = pldm_entity_association_tree_add(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -0600777 tree, &entities[6], 0xffff, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930778 EXPECT_NE(l3b, nullptr);
779 auto l4a = pldm_entity_association_tree_add(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -0600780 tree, &entities[7], 0xffff, l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930781 EXPECT_NE(l4a, nullptr);
782 auto l4b = pldm_entity_association_tree_add(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -0600783 tree, &entities[8], 0xffff, l3b, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930784 EXPECT_NE(l4b, nullptr);
785
786 EXPECT_EQ(pldm_entity_is_node_parent(l1), true);
787 EXPECT_EQ(pldm_entity_is_node_parent(l2a), true);
788 EXPECT_EQ(pldm_entity_is_node_parent(l3a), true);
789 EXPECT_EQ(pldm_entity_is_node_parent(l3b), true);
790
791 EXPECT_EQ(pldm_entity_is_node_parent(l2b), false);
792 EXPECT_EQ(pldm_entity_is_node_parent(l2c), false);
793 EXPECT_EQ(pldm_entity_is_node_parent(l3c), false);
794 EXPECT_EQ(pldm_entity_is_node_parent(l4a), false);
795 EXPECT_EQ(pldm_entity_is_node_parent(l4b), false);
796
797 EXPECT_EQ(pldm_entity_is_exist_parent(l1), false);
798
799 pldm_entity nodeL1 = pldm_entity_extract(l1);
800 pldm_entity parentL2a = pldm_entity_get_parent(l2a);
801 pldm_entity parentL2b = pldm_entity_get_parent(l2b);
802 pldm_entity parentL2c = pldm_entity_get_parent(l2c);
803 EXPECT_EQ(pldm_entity_is_exist_parent(l2a), true);
804 EXPECT_EQ(pldm_entity_is_exist_parent(l2b), true);
805 EXPECT_EQ(pldm_entity_is_exist_parent(l2c), true);
806 EXPECT_EQ(parentL2a.entity_type, nodeL1.entity_type);
807 EXPECT_EQ(parentL2a.entity_instance_num, nodeL1.entity_instance_num);
808 EXPECT_EQ(parentL2a.entity_container_id, nodeL1.entity_container_id);
809 EXPECT_EQ(parentL2b.entity_type, nodeL1.entity_type);
810 EXPECT_EQ(parentL2b.entity_instance_num, nodeL1.entity_instance_num);
811 EXPECT_EQ(parentL2b.entity_container_id, nodeL1.entity_container_id);
812 EXPECT_EQ(parentL2c.entity_type, nodeL1.entity_type);
813 EXPECT_EQ(parentL2c.entity_instance_num, nodeL1.entity_instance_num);
814 EXPECT_EQ(parentL2c.entity_container_id, nodeL1.entity_container_id);
815
816 pldm_entity nodeL2a = pldm_entity_extract(l2a);
817 pldm_entity parentL3a = pldm_entity_get_parent(l3a);
818 pldm_entity parentL3b = pldm_entity_get_parent(l3b);
819 pldm_entity parentL3c = pldm_entity_get_parent(l3c);
820 EXPECT_EQ(pldm_entity_is_exist_parent(l3a), true);
821 EXPECT_EQ(pldm_entity_is_exist_parent(l3b), true);
822 EXPECT_EQ(pldm_entity_is_exist_parent(l3c), true);
823 EXPECT_EQ(parentL3a.entity_type, nodeL2a.entity_type);
824 EXPECT_EQ(parentL3a.entity_instance_num, nodeL2a.entity_instance_num);
825 EXPECT_EQ(parentL3a.entity_container_id, nodeL2a.entity_container_id);
826 EXPECT_EQ(parentL3b.entity_type, nodeL2a.entity_type);
827 EXPECT_EQ(parentL3b.entity_instance_num, nodeL2a.entity_instance_num);
828 EXPECT_EQ(parentL3b.entity_container_id, nodeL2a.entity_container_id);
829 EXPECT_EQ(parentL3c.entity_type, nodeL2a.entity_type);
830 EXPECT_EQ(parentL3c.entity_instance_num, nodeL2a.entity_instance_num);
831 EXPECT_EQ(parentL3c.entity_container_id, nodeL2a.entity_container_id);
832
833 pldm_entity nodeL3a = pldm_entity_extract(l3a);
834 pldm_entity parentL4a = pldm_entity_get_parent(l4a);
835 EXPECT_EQ(pldm_entity_is_exist_parent(l4a), true);
836 EXPECT_EQ(parentL4a.entity_type, nodeL3a.entity_type);
837 EXPECT_EQ(parentL4a.entity_instance_num, nodeL3a.entity_instance_num);
838 EXPECT_EQ(parentL4a.entity_container_id, nodeL3a.entity_container_id);
839
840 pldm_entity nodeL3b = pldm_entity_extract(l3b);
841 pldm_entity parentL4b = pldm_entity_get_parent(l4b);
842 EXPECT_EQ(pldm_entity_is_exist_parent(l4b), true);
843 EXPECT_EQ(parentL4b.entity_type, nodeL3b.entity_type);
844 EXPECT_EQ(parentL4b.entity_instance_num, nodeL3b.entity_instance_num);
845 EXPECT_EQ(parentL4b.entity_container_id, nodeL3b.entity_container_id);
846
847 size_t num{};
848 pldm_entity* out = nullptr;
849 pldm_entity_association_tree_visit(tree, &out, &num);
850 EXPECT_EQ(num, 9u);
851
852 EXPECT_EQ(out[0].entity_type, 1u);
853 EXPECT_EQ(out[0].entity_instance_num, 1u);
854 EXPECT_EQ(out[0].entity_container_id, 0u);
855
856 EXPECT_EQ(out[1].entity_type, 2u);
857 EXPECT_EQ(out[1].entity_instance_num, 1u);
858 EXPECT_EQ(out[1].entity_container_id, 1u);
859 EXPECT_EQ(out[2].entity_type, 2u);
860 EXPECT_EQ(out[2].entity_instance_num, 2u);
861 EXPECT_EQ(out[2].entity_container_id, 1u);
862 EXPECT_EQ(out[3].entity_type, 3u);
863 EXPECT_EQ(out[3].entity_instance_num, 1u);
864 EXPECT_EQ(out[3].entity_container_id, 1u);
865
866 EXPECT_EQ(out[4].entity_type, 4u);
867 EXPECT_EQ(out[4].entity_instance_num, 1u);
868 EXPECT_EQ(out[4].entity_container_id, 2u);
869 EXPECT_EQ(out[5].entity_type, 5u);
870 EXPECT_EQ(out[5].entity_instance_num, 1u);
871 EXPECT_EQ(out[5].entity_container_id, 2u);
872 EXPECT_EQ(out[6].entity_type, 5u);
873 EXPECT_EQ(out[6].entity_instance_num, 2u);
874 EXPECT_EQ(out[6].entity_container_id, 2u);
875
876 EXPECT_EQ(out[7].entity_type, 7u);
877 EXPECT_EQ(out[7].entity_instance_num, 1u);
878 EXPECT_EQ(out[7].entity_container_id, 4u);
879 EXPECT_EQ(out[8].entity_type, 6u);
880 EXPECT_EQ(out[8].entity_instance_num, 1u);
881 EXPECT_EQ(out[8].entity_container_id, 3u);
882
883 free(out);
884
885 pldm_entity p1 = pldm_entity_extract(l1);
886 EXPECT_EQ(p1.entity_type, 1u);
887 EXPECT_EQ(p1.entity_instance_num, 1u);
888 EXPECT_EQ(p1.entity_container_id, 0u);
889
890 pldm_entity p2a = pldm_entity_extract(l2a);
891 EXPECT_EQ(p2a.entity_type, 2u);
892 EXPECT_EQ(p2a.entity_instance_num, 1u);
893 EXPECT_EQ(p2a.entity_container_id, 1u);
894 pldm_entity p2b = pldm_entity_extract(l2b);
895 EXPECT_EQ(p2b.entity_type, 2u);
896 EXPECT_EQ(p2b.entity_instance_num, 2u);
897 EXPECT_EQ(p2b.entity_container_id, 1u);
898 pldm_entity p2c = pldm_entity_extract(l2c);
899 EXPECT_EQ(p2c.entity_type, 3u);
900 EXPECT_EQ(p2c.entity_instance_num, 1u);
901 EXPECT_EQ(p2c.entity_container_id, 1u);
902
903 pldm_entity p3a = pldm_entity_extract(l3a);
904 EXPECT_EQ(p3a.entity_type, 4u);
905 EXPECT_EQ(p3a.entity_instance_num, 1u);
906 EXPECT_EQ(p3a.entity_container_id, 2u);
907 pldm_entity p3b = pldm_entity_extract(l3b);
908 EXPECT_EQ(p3b.entity_type, 5u);
909 EXPECT_EQ(p3b.entity_instance_num, 1u);
910 EXPECT_EQ(p3b.entity_container_id, 2u);
911 pldm_entity p3c = pldm_entity_extract(l3c);
912 EXPECT_EQ(p3c.entity_type, 5u);
913 EXPECT_EQ(p3c.entity_instance_num, 2u);
914 EXPECT_EQ(p3c.entity_container_id, 2u);
915
916 pldm_entity p4a = pldm_entity_extract(l4a);
917 EXPECT_EQ(p4a.entity_type, 6u);
918 EXPECT_EQ(p4a.entity_instance_num, 1u);
919 EXPECT_EQ(p4a.entity_container_id, 3u);
920 pldm_entity p4b = pldm_entity_extract(l4b);
921 EXPECT_EQ(p4b.entity_type, 7u);
922 EXPECT_EQ(p4b.entity_instance_num, 1u);
923 EXPECT_EQ(p4b.entity_container_id, 4u);
924
925 pldm_entity_association_tree_destroy(tree);
926}
927
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500928#if LIBPLDM_API_TESTING
929TEST(EntityAssociationPDR, findAndAddRemotePDR)
930{
931 // Tree - 1
932 //
933 // 11521(1,0)
934 // |
935 // 45 (1,1)
936 // |
937 // 64 (1,2)
938 // |
939 // ------------------
940 // | |
941 // 67(0,3) 67(1,3)
942 // | |
943 // 135(0,4) 135(0,5)
944 // | |
945 // 32903(0,6) 32903(0,7)
946
947 pldm_entity entities[9]{};
948 entities[0].entity_type = 11521;
949 entities[1].entity_type = 45;
950 entities[2].entity_type = 64;
951 entities[3].entity_type = 67;
952 entities[4].entity_type = 67;
953 entities[5].entity_type = 135;
954 entities[5].entity_container_id = 2;
955 entities[6].entity_type = 135;
956 entities[6].entity_container_id = 3;
957 entities[7].entity_type = 32903;
958 entities[8].entity_type = 32903;
959 auto tree = pldm_entity_association_tree_init();
960 auto l1 = pldm_entity_association_tree_add_entity(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -0600961 tree, &entities[0], 0xffff, nullptr, PLDM_ENTITY_ASSOCIAION_LOGICAL,
962 false, true, 0xffff);
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500963 EXPECT_NE(l1, nullptr);
964 auto l2 = pldm_entity_association_tree_add_entity(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -0600965 tree, &entities[1], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
966 false, 0xffff);
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500967 EXPECT_NE(l2, nullptr);
968 auto l3 = pldm_entity_association_tree_add_entity(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -0600969 tree, &entities[2], 0xffff, l2, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
970 true, 0xffff);
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500971 EXPECT_NE(l3, nullptr);
972 auto l4a = pldm_entity_association_tree_add_entity(
973 tree, &entities[3], 0, l3, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -0600974 false, 0xffff);
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500975 EXPECT_NE(l4a, nullptr);
976 auto l4b = pldm_entity_association_tree_add_entity(
977 tree, &entities[4], 1, l3, PLDM_ENTITY_ASSOCIAION_PHYSICAL, true, true,
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -0600978 0xffff);
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500979 EXPECT_NE(l4b, nullptr);
980 auto l5a = pldm_entity_association_tree_add_entity(
981 tree, &entities[5], 0, l4a, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -0600982 false, 0xffff);
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500983 EXPECT_NE(l5a, nullptr);
984 auto l5b = pldm_entity_association_tree_add_entity(
985 tree, &entities[6], 0, l4b, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -0600986 false, 0xffff);
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500987 EXPECT_NE(l5b, nullptr);
988 pldm_entity entity{};
989 entity.entity_type = 135;
990 entity.entity_instance_num = 0;
991 entity.entity_container_id = 2;
992 auto result1 = pldm_entity_association_tree_find(tree, &entity);
993 EXPECT_EQ(result1, l5a);
994 EXPECT_EQ(entities[5].entity_container_id, 2);
995 auto l6a = pldm_entity_association_tree_add_entity(
996 tree, &entities[7], 0, result1, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -0600997 false, 0xffff);
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500998 EXPECT_NE(l6a, nullptr);
999 entity.entity_type = 135;
1000 entity.entity_instance_num = 0;
1001 entity.entity_container_id = 3;
1002 auto result2 = pldm_entity_association_tree_find(tree, &entity);
1003 EXPECT_NE(result2, l5b);
1004 EXPECT_EQ(entities[6].entity_container_id, 3);
1005 auto l7a = pldm_entity_association_tree_add_entity(
1006 tree, &entities[8], 0, result2, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001007 false, 0xffff);
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -05001008 EXPECT_EQ(l7a, nullptr);
1009 pldm_entity_association_tree_destroy(tree);
1010}
1011#endif
1012
Andrew Jeffery9c766792022-08-10 23:12:49 +09301013TEST(EntityAssociationPDR, testSpecialTrees)
1014{
1015 pldm_entity entities[3]{};
1016
1017 entities[0].entity_type = 1;
1018 entities[1].entity_type = 2;
1019 entities[2].entity_type = 1;
1020
1021 // A
1022 auto tree = pldm_entity_association_tree_init();
1023 auto node = pldm_entity_association_tree_add(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001024 tree, &entities[0], 0xffff, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301025 EXPECT_NE(node, nullptr);
1026 size_t num{};
1027 pldm_entity* out = nullptr;
1028 pldm_entity_association_tree_visit(tree, &out, &num);
1029 EXPECT_EQ(num, 1u);
1030 EXPECT_EQ(out[0].entity_type, 1u);
1031 EXPECT_EQ(out[0].entity_instance_num, 1u);
1032 EXPECT_EQ(out[0].entity_container_id, 0u);
1033 free(out);
1034 pldm_entity_association_tree_destroy(tree);
1035
1036 // A-A-A
1037 tree = pldm_entity_association_tree_init();
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001038 node = pldm_entity_association_tree_add(tree, &entities[0], 0xffff, nullptr,
Andrew Jeffery9c766792022-08-10 23:12:49 +09301039 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1040 EXPECT_NE(node, nullptr);
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001041 node = pldm_entity_association_tree_add(tree, &entities[1], 0xffff, nullptr,
Andrew Jeffery9c766792022-08-10 23:12:49 +09301042 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1043 EXPECT_NE(node, nullptr);
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001044 node = pldm_entity_association_tree_add(tree, &entities[2], 0xffff, nullptr,
Andrew Jeffery9c766792022-08-10 23:12:49 +09301045 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1046 EXPECT_NE(node, 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, 1u);
1053 EXPECT_EQ(out[1].entity_instance_num, 2u);
1054 EXPECT_EQ(out[1].entity_container_id, 0u);
1055 EXPECT_EQ(out[2].entity_type, 2u);
1056 EXPECT_EQ(out[2].entity_instance_num, 1u);
1057 EXPECT_EQ(out[2].entity_container_id, 0u);
1058 free(out);
1059 pldm_entity_association_tree_destroy(tree);
1060
1061 // A
1062 // |
1063 // A
1064 // |
1065 // A
1066 tree = pldm_entity_association_tree_init();
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001067 node = pldm_entity_association_tree_add(tree, &entities[0], 0xffff, nullptr,
Andrew Jeffery9c766792022-08-10 23:12:49 +09301068 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1069 EXPECT_NE(node, nullptr);
1070 auto node1 = pldm_entity_association_tree_add(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001071 tree, &entities[1], 0xffff, node, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301072 EXPECT_NE(node1, nullptr);
1073 auto node2 = pldm_entity_association_tree_add(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001074 tree, &entities[2], 0xffff, node1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301075 EXPECT_NE(node2, nullptr);
1076 pldm_entity_association_tree_visit(tree, &out, &num);
1077 EXPECT_EQ(num, 3u);
1078 EXPECT_EQ(out[0].entity_type, 1u);
1079 EXPECT_EQ(out[0].entity_instance_num, 1u);
1080 EXPECT_EQ(out[0].entity_container_id, 0u);
1081 EXPECT_EQ(out[1].entity_type, 2u);
1082 EXPECT_EQ(out[1].entity_instance_num, 1u);
1083 EXPECT_EQ(out[1].entity_container_id, 1u);
1084 EXPECT_EQ(out[2].entity_type, 1u);
1085 EXPECT_EQ(out[2].entity_instance_num, 1u);
1086 EXPECT_EQ(out[2].entity_container_id, 2u);
1087 free(out);
1088 pldm_entity_association_tree_destroy(tree);
1089
1090 // A-A
1091 // |
1092 // A-A
1093 tree = pldm_entity_association_tree_init();
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001094 node = pldm_entity_association_tree_add(tree, &entities[0], 0xffff, nullptr,
Andrew Jeffery9c766792022-08-10 23:12:49 +09301095 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1096 EXPECT_NE(node, nullptr);
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001097 node = pldm_entity_association_tree_add(tree, &entities[0], 0xffff, nullptr,
Andrew Jeffery9c766792022-08-10 23:12:49 +09301098 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1099 EXPECT_NE(node, nullptr);
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001100 node1 = pldm_entity_association_tree_add(tree, &entities[1], 0xffff, node,
Andrew Jeffery9c766792022-08-10 23:12:49 +09301101 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1102 EXPECT_NE(node1, nullptr);
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001103 node2 = pldm_entity_association_tree_add(tree, &entities[2], 0xffff, node,
Andrew Jeffery9c766792022-08-10 23:12:49 +09301104 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1105 EXPECT_NE(node2, nullptr);
1106 pldm_entity_association_tree_visit(tree, &out, &num);
1107 EXPECT_EQ(num, 4u);
1108 EXPECT_EQ(out[0].entity_type, 1u);
1109 EXPECT_EQ(out[0].entity_instance_num, 1u);
1110 EXPECT_EQ(out[0].entity_container_id, 0u);
1111 EXPECT_EQ(out[1].entity_type, 1u);
1112 EXPECT_EQ(out[1].entity_instance_num, 2u);
1113 EXPECT_EQ(out[1].entity_container_id, 0u);
1114 EXPECT_EQ(out[2].entity_type, 2u);
1115 EXPECT_EQ(out[2].entity_instance_num, 1u);
1116 EXPECT_EQ(out[2].entity_container_id, 1u);
1117 EXPECT_EQ(out[3].entity_type, 1u);
1118 EXPECT_EQ(out[3].entity_instance_num, 1u);
1119 EXPECT_EQ(out[3].entity_container_id, 1u);
1120 free(out);
1121 pldm_entity_association_tree_destroy(tree);
1122}
1123
1124TEST(EntityAssociationPDR, testPDR)
1125{
1126 // e = entity type, c = container id, i = instance num
1127
1128 // INPUT
1129 // 1(e=1)--1a(e=2)
1130 // |
1131 // 2(e=2)--3(e=2)--4(e=2)--5(e=3)
1132 // |
1133 // 6(e=4)--7(e=5)--8(e=5)--9(e=5)
1134 // | |
1135 // 11(e=6) 10(e=7)
1136
1137 // Expected OUTPUT
1138 // 1(e=1,c=0,i=1)
1139 // |
1140 // 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)
1141 // |
1142 // 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)
1143 // | |
1144 // 10(e=6,c=3,i=1) 11(e=7,c=4,i=1)
1145 pldm_entity entities[11]{};
1146
1147 entities[0].entity_type = 1;
1148 entities[1].entity_type = 2;
1149 entities[2].entity_type = 3;
1150 entities[3].entity_type = 2;
1151 entities[4].entity_type = 3;
1152 entities[5].entity_type = 4;
1153 entities[6].entity_type = 5;
1154 entities[7].entity_type = 5;
1155 entities[8].entity_type = 5;
1156 entities[9].entity_type = 6;
1157 entities[10].entity_type = 7;
1158
1159 auto tree = pldm_entity_association_tree_init();
1160
1161 auto l1 = pldm_entity_association_tree_add(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001162 tree, &entities[0], 0xffff, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301163 EXPECT_NE(l1, nullptr);
1164 auto l1a = pldm_entity_association_tree_add(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001165 tree, &entities[1], 0xffff, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301166 EXPECT_NE(l1a, nullptr);
1167
1168 auto l2a = pldm_entity_association_tree_add(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001169 tree, &entities[1], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301170 EXPECT_NE(l2a, nullptr);
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001171 auto l2b = pldm_entity_association_tree_add(tree, &entities[2], 0xffff, l1,
Andrew Jeffery9c766792022-08-10 23:12:49 +09301172 PLDM_ENTITY_ASSOCIAION_LOGICAL);
1173 EXPECT_NE(l2b, nullptr);
1174 auto l2c = pldm_entity_association_tree_add(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001175 tree, &entities[3], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301176 EXPECT_NE(l2c, nullptr);
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001177 auto l2d = pldm_entity_association_tree_add(tree, &entities[4], 0xffff, l1,
Andrew Jeffery9c766792022-08-10 23:12:49 +09301178 PLDM_ENTITY_ASSOCIAION_LOGICAL);
1179 EXPECT_NE(l2d, nullptr);
1180
1181 auto l3a = pldm_entity_association_tree_add(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001182 tree, &entities[5], 0xffff, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301183 EXPECT_NE(l3a, nullptr);
1184 auto l3b = pldm_entity_association_tree_add(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001185 tree, &entities[6], 0xffff, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301186 EXPECT_NE(l3b, nullptr);
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001187 auto l3c = pldm_entity_association_tree_add(tree, &entities[7], 0xffff, l2a,
Andrew Jeffery9c766792022-08-10 23:12:49 +09301188 PLDM_ENTITY_ASSOCIAION_LOGICAL);
1189 EXPECT_NE(l3c, nullptr);
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001190 auto l3d = pldm_entity_association_tree_add(tree, &entities[8], 0xffff, l2a,
Andrew Jeffery9c766792022-08-10 23:12:49 +09301191 PLDM_ENTITY_ASSOCIAION_LOGICAL);
1192 EXPECT_NE(l3d, nullptr);
1193
1194 auto l4a = pldm_entity_association_tree_add(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001195 tree, &entities[9], 0xffff, l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301196 EXPECT_NE(l4a, nullptr);
1197 auto l4b = pldm_entity_association_tree_add(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001198 tree, &entities[10], 0xffff, l3b, PLDM_ENTITY_ASSOCIAION_LOGICAL);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301199 EXPECT_NE(l4b, nullptr);
1200
1201 EXPECT_EQ(pldm_entity_get_num_children(l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL),
1202 2);
1203 EXPECT_EQ(pldm_entity_get_num_children(l1, PLDM_ENTITY_ASSOCIAION_LOGICAL),
1204 2);
1205 EXPECT_EQ(
1206 pldm_entity_get_num_children(l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL), 2);
1207 EXPECT_EQ(
1208 pldm_entity_get_num_children(l3b, PLDM_ENTITY_ASSOCIAION_PHYSICAL), 0);
1209 EXPECT_EQ(pldm_entity_get_num_children(l3b, PLDM_ENTITY_ASSOCIAION_LOGICAL),
1210 1);
1211
1212 auto repo = pldm_pdr_init();
Andrew Jeffery255f6542023-08-10 15:46:54 +09301213 int rc = pldm_entity_association_pdr_add_check(tree, repo, false, 1);
1214 ASSERT_EQ(rc, 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301215
1216 EXPECT_EQ(pldm_pdr_get_record_count(repo), 6u);
1217
1218 uint32_t currRecHandle{};
1219 uint32_t nextRecHandle{};
1220 uint8_t* data = nullptr;
1221 uint32_t size{};
1222 uint32_t commonSize = sizeof(struct pldm_pdr_hdr) + sizeof(uint16_t) +
1223 sizeof(uint8_t) + sizeof(pldm_entity) +
1224 sizeof(uint8_t);
1225
1226 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1227 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1228 l1, PLDM_ENTITY_ASSOCIAION_LOGICAL) *
1229 sizeof(pldm_entity)));
1230 uint8_t* start = data;
1231 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
1232 EXPECT_EQ(le32toh(hdr->record_handle), 1u);
1233 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
1234 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
1235 start += sizeof(pldm_pdr_hdr);
1236 uint16_t* containerId = reinterpret_cast<uint16_t*>(start);
1237 EXPECT_EQ(le16toh(*containerId), 1u);
1238 start += sizeof(uint16_t);
1239 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_LOGICAL);
1240 start += sizeof(uint8_t);
1241 pldm_entity* entity = reinterpret_cast<pldm_entity*>(start);
1242 EXPECT_EQ(le16toh(entity->entity_type), 1u);
1243 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1244 EXPECT_EQ(le16toh(entity->entity_container_id), 0u);
1245 start += sizeof(pldm_entity);
1246 EXPECT_EQ(*start,
1247 pldm_entity_get_num_children(l1, PLDM_ENTITY_ASSOCIAION_LOGICAL));
1248 start += sizeof(uint8_t);
1249 entity = reinterpret_cast<pldm_entity*>(start);
1250 EXPECT_EQ(le16toh(entity->entity_type), 3u);
1251 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1252 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
1253 start += sizeof(pldm_entity);
1254 entity = reinterpret_cast<pldm_entity*>(start);
1255 EXPECT_EQ(le16toh(entity->entity_type), 3u);
1256 EXPECT_EQ(le16toh(entity->entity_instance_num), 2u);
1257 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301258
1259 currRecHandle = nextRecHandle;
1260
1261 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1262 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1263 l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL) *
1264 sizeof(pldm_entity)));
1265 start = data;
1266 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
1267 EXPECT_EQ(le32toh(hdr->record_handle), 2u);
1268 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
1269 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
1270 start += sizeof(pldm_pdr_hdr);
1271 containerId = reinterpret_cast<uint16_t*>(start);
1272 EXPECT_EQ(le16toh(*containerId), 1u);
1273 start += sizeof(uint16_t);
1274 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1275 start += sizeof(uint8_t);
1276 entity = reinterpret_cast<pldm_entity*>(start);
1277 EXPECT_EQ(le16toh(entity->entity_type), 1u);
1278 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1279 EXPECT_EQ(le16toh(entity->entity_container_id), 0u);
1280 start += sizeof(pldm_entity);
1281 EXPECT_EQ(*start, pldm_entity_get_num_children(
1282 l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL));
1283 start += sizeof(uint8_t);
1284 entity = reinterpret_cast<pldm_entity*>(start);
1285 EXPECT_EQ(le16toh(entity->entity_type), 2u);
1286 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1287 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
1288 start += sizeof(pldm_entity);
1289 entity = reinterpret_cast<pldm_entity*>(start);
1290 EXPECT_EQ(le16toh(entity->entity_type), 2u);
1291 EXPECT_EQ(le16toh(entity->entity_instance_num), 2u);
1292 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301293
1294 currRecHandle = nextRecHandle;
1295
1296 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1297 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1298 l2a, PLDM_ENTITY_ASSOCIAION_LOGICAL) *
1299 sizeof(pldm_entity)));
1300 start = data;
1301 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
1302 EXPECT_EQ(le32toh(hdr->record_handle), 3u);
1303 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
1304 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
1305 start += sizeof(pldm_pdr_hdr);
1306 containerId = reinterpret_cast<uint16_t*>(start);
1307 EXPECT_EQ(le16toh(*containerId), 2u);
1308 start += sizeof(uint16_t);
1309 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_LOGICAL);
1310 start += sizeof(uint8_t);
1311 entity = reinterpret_cast<pldm_entity*>(start);
1312 EXPECT_EQ(le16toh(entity->entity_type), 2u);
1313 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1314 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
1315 start += sizeof(pldm_entity);
1316 EXPECT_EQ(*start, pldm_entity_get_num_children(
1317 l2a, PLDM_ENTITY_ASSOCIAION_LOGICAL));
1318 start += sizeof(uint8_t);
1319 entity = reinterpret_cast<pldm_entity*>(start);
1320 EXPECT_EQ(le16toh(entity->entity_type), 5);
1321 EXPECT_EQ(le16toh(entity->entity_instance_num), 2u);
1322 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
1323 start += sizeof(pldm_entity);
1324 entity = reinterpret_cast<pldm_entity*>(start);
1325 EXPECT_EQ(le16toh(entity->entity_type), 5u);
1326 EXPECT_EQ(le16toh(entity->entity_instance_num), 3u);
1327 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301328
1329 currRecHandle = nextRecHandle;
1330
1331 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1332 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1333 l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL) *
1334 sizeof(pldm_entity)));
1335 start = data;
1336 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
1337 EXPECT_EQ(le32toh(hdr->record_handle), 4u);
1338 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
1339 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
1340 start += sizeof(pldm_pdr_hdr);
1341 containerId = reinterpret_cast<uint16_t*>(start);
1342 EXPECT_EQ(le16toh(*containerId), 2u);
1343 start += sizeof(uint16_t);
1344 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1345 start += sizeof(uint8_t);
1346 entity = reinterpret_cast<pldm_entity*>(start);
1347 EXPECT_EQ(le16toh(entity->entity_type), 2u);
1348 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1349 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
1350 start += sizeof(pldm_entity);
1351 EXPECT_EQ(*start, pldm_entity_get_num_children(
1352 l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL));
1353 start += sizeof(uint8_t);
1354 entity = reinterpret_cast<pldm_entity*>(start);
1355 EXPECT_EQ(le16toh(entity->entity_type), 4u);
1356 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1357 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
1358 start += sizeof(pldm_entity);
1359 entity = reinterpret_cast<pldm_entity*>(start);
1360 EXPECT_EQ(le16toh(entity->entity_type), 5u);
1361 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1362 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301363
1364 currRecHandle = nextRecHandle;
1365
1366 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1367 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1368 l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL) *
1369 sizeof(pldm_entity)));
1370 start = data;
1371 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
1372 EXPECT_EQ(le32toh(hdr->record_handle), 5u);
1373 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
1374 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
1375 start += sizeof(pldm_pdr_hdr);
1376 containerId = reinterpret_cast<uint16_t*>(start);
1377 EXPECT_EQ(le16toh(*containerId), 3u);
1378 start += sizeof(uint16_t);
1379 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1380 start += sizeof(uint8_t);
1381 entity = reinterpret_cast<pldm_entity*>(start);
1382 EXPECT_EQ(le16toh(entity->entity_type), 4u);
1383 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1384 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
1385 start += sizeof(pldm_entity);
1386 EXPECT_EQ(*start, pldm_entity_get_num_children(
1387 l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL));
1388 start += sizeof(uint8_t);
1389 entity = reinterpret_cast<pldm_entity*>(start);
1390 EXPECT_EQ(le16toh(entity->entity_type), 6u);
1391 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1392 EXPECT_EQ(le16toh(entity->entity_container_id), 3u);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301393
1394 currRecHandle = nextRecHandle;
1395
1396 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1397 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1398 l3b, PLDM_ENTITY_ASSOCIAION_LOGICAL) *
1399 sizeof(pldm_entity)));
1400 start = data;
1401 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
1402 EXPECT_EQ(le32toh(hdr->record_handle), 6u);
1403 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
1404 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
1405 start += sizeof(pldm_pdr_hdr);
1406 containerId = reinterpret_cast<uint16_t*>(start);
1407 EXPECT_EQ(le16toh(*containerId), 4u);
1408 start += sizeof(uint16_t);
1409 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_LOGICAL);
1410 start += sizeof(uint8_t);
1411 entity = reinterpret_cast<pldm_entity*>(start);
1412 EXPECT_EQ(le16toh(entity->entity_type), 5u);
1413 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1414 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
1415 start += sizeof(pldm_entity);
1416 EXPECT_EQ(*start, pldm_entity_get_num_children(
1417 l3b, PLDM_ENTITY_ASSOCIAION_LOGICAL));
1418 start += sizeof(uint8_t);
1419 entity = reinterpret_cast<pldm_entity*>(start);
1420 EXPECT_EQ(le16toh(entity->entity_type), 7u);
1421 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1422 EXPECT_EQ(le16toh(entity->entity_container_id), 4u);
1423
1424 EXPECT_EQ(nextRecHandle, 0u);
1425
1426 pldm_pdr_destroy(repo);
1427 pldm_entity_association_tree_destroy(tree);
1428}
1429
1430TEST(EntityAssociationPDR, testFind)
1431{
1432 // 1
1433 // |
1434 // 2--3--4
1435 // |
1436 // 5--6--7
1437 // | |
1438 // 8 9
1439
1440 pldm_entity entities[9]{};
1441
1442 entities[0].entity_type = 1;
1443 entities[1].entity_type = 2;
1444 entities[2].entity_type = 2;
1445 entities[3].entity_type = 3;
1446 entities[4].entity_type = 4;
1447 entities[5].entity_type = 5;
1448 entities[6].entity_type = 5;
1449 entities[7].entity_type = 6;
1450 entities[8].entity_type = 7;
1451
1452 auto tree = pldm_entity_association_tree_init();
1453
1454 auto l1 = pldm_entity_association_tree_add(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001455 tree, &entities[0], 0xffff, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301456 EXPECT_NE(l1, nullptr);
1457 auto l2a = pldm_entity_association_tree_add(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001458 tree, &entities[1], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301459 EXPECT_NE(l2a, nullptr);
1460 auto l2b = pldm_entity_association_tree_add(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001461 tree, &entities[2], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301462 EXPECT_NE(l2b, nullptr);
1463 auto l2c = pldm_entity_association_tree_add(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001464 tree, &entities[3], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301465 EXPECT_NE(l2c, nullptr);
1466 auto l3a = pldm_entity_association_tree_add(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001467 tree, &entities[4], 0xffff, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301468 EXPECT_NE(l3a, nullptr);
1469 auto l3b = pldm_entity_association_tree_add(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001470 tree, &entities[5], 0xffff, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301471 EXPECT_NE(l3b, nullptr);
1472 auto l3c = pldm_entity_association_tree_add(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001473 tree, &entities[6], 0xffff, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301474 EXPECT_NE(l3c, nullptr);
1475 auto l4a = pldm_entity_association_tree_add(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001476 tree, &entities[7], 0xffff, l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301477 EXPECT_NE(l4a, nullptr);
1478 auto l4b = pldm_entity_association_tree_add(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001479 tree, &entities[8], 0xffff, l3b, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301480 EXPECT_NE(l4b, nullptr);
1481
1482 pldm_entity entity{};
1483
1484 entity.entity_type = 1;
1485 entity.entity_instance_num = 1;
1486 auto result = pldm_entity_association_tree_find(tree, &entity);
1487 EXPECT_EQ(result, l1);
1488 EXPECT_EQ(entity.entity_container_id, 0);
1489
1490 entity.entity_type = 2;
1491 entity.entity_instance_num = 1;
1492 result = pldm_entity_association_tree_find(tree, &entity);
1493 EXPECT_EQ(result, l2a);
1494 EXPECT_EQ(entity.entity_container_id, 1);
1495 entity.entity_type = 2;
1496 entity.entity_instance_num = 2;
1497 result = pldm_entity_association_tree_find(tree, &entity);
1498 EXPECT_EQ(result, l2b);
1499 EXPECT_EQ(entity.entity_container_id, 1);
1500 entity.entity_type = 3;
1501 entity.entity_instance_num = 1;
1502 result = pldm_entity_association_tree_find(tree, &entity);
1503 EXPECT_EQ(result, l2c);
1504 EXPECT_EQ(entity.entity_container_id, 1);
1505
1506 entity.entity_type = 7;
1507 entity.entity_instance_num = 1;
1508 result = pldm_entity_association_tree_find(tree, &entity);
1509 EXPECT_EQ(result, l4b);
1510 EXPECT_EQ(entity.entity_container_id, 4);
1511
1512 pldm_entity_association_tree_destroy(tree);
1513}
1514
1515TEST(EntityAssociationPDR, testCopyTree)
1516{
1517 pldm_entity entities[4]{};
1518 entities[0].entity_type = 1;
1519 entities[1].entity_type = 2;
1520 entities[2].entity_type = 2;
1521 entities[3].entity_type = 3;
1522
1523 auto orgTree = pldm_entity_association_tree_init();
1524 auto newTree = pldm_entity_association_tree_init();
1525 auto l1 =
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001526 pldm_entity_association_tree_add(orgTree, &entities[0], 0xffff, nullptr,
Andrew Jeffery9c766792022-08-10 23:12:49 +09301527 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1528 EXPECT_NE(l1, nullptr);
1529 auto l2a = pldm_entity_association_tree_add(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001530 orgTree, &entities[1], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301531 EXPECT_NE(l2a, nullptr);
1532 auto l2b = pldm_entity_association_tree_add(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001533 orgTree, &entities[2], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301534 EXPECT_NE(l2b, nullptr);
1535 auto l2c = pldm_entity_association_tree_add(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001536 orgTree, &entities[3], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301537 EXPECT_NE(l2c, nullptr);
1538 size_t orgNum{};
1539 pldm_entity* orgOut = nullptr;
1540 pldm_entity_association_tree_visit(orgTree, &orgOut, &orgNum);
1541 EXPECT_EQ(orgNum, 4u);
1542
1543 pldm_entity_association_tree_copy_root(orgTree, newTree);
1544 size_t newNum{};
1545 pldm_entity* newOut = nullptr;
1546 pldm_entity_association_tree_visit(newTree, &newOut, &newNum);
1547 EXPECT_EQ(newNum, orgNum);
1548 EXPECT_EQ(newOut[0].entity_type, 1u);
1549 EXPECT_EQ(newOut[0].entity_instance_num, 1u);
1550 EXPECT_EQ(newOut[0].entity_container_id, 0u);
1551 free(orgOut);
1552 free(newOut);
1553 pldm_entity_association_tree_destroy(orgTree);
1554 pldm_entity_association_tree_destroy(newTree);
1555}
1556
1557TEST(EntityAssociationPDR, testExtract)
1558{
1559 std::vector<uint8_t> pdr{};
1560 pdr.resize(sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_entity_association) +
1561 sizeof(pldm_entity) * 4);
1562 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(pdr.data());
1563 hdr->type = PLDM_PDR_ENTITY_ASSOCIATION;
1564 hdr->length =
1565 htole16(sizeof(pldm_pdr_entity_association) + sizeof(pldm_entity) * 4);
1566
1567 pldm_pdr_entity_association* e =
1568 reinterpret_cast<pldm_pdr_entity_association*>(pdr.data() +
1569 sizeof(pldm_pdr_hdr));
1570 e->container_id = htole16(1);
1571 e->num_children = 5;
1572 e->container.entity_type = htole16(1);
1573 e->container.entity_instance_num = htole16(1);
1574 e->container.entity_container_id = htole16(0);
1575
1576 pldm_entity* entity = e->children;
1577 entity->entity_type = htole16(2);
1578 entity->entity_instance_num = htole16(1);
1579 entity->entity_container_id = htole16(1);
1580 ++entity;
1581 entity->entity_type = htole16(3);
1582 entity->entity_instance_num = htole16(1);
1583 entity->entity_container_id = htole16(1);
1584 ++entity;
1585 entity->entity_type = htole16(4);
1586 entity->entity_instance_num = htole16(1);
1587 entity->entity_container_id = htole16(1);
1588 ++entity;
1589 entity->entity_type = htole16(5);
1590 entity->entity_instance_num = htole16(1);
1591 entity->entity_container_id = htole16(1);
1592 ++entity;
1593 entity->entity_type = htole16(6);
1594 entity->entity_instance_num = htole16(1);
1595 entity->entity_container_id = htole16(1);
1596
1597 size_t num{};
1598 pldm_entity* out = nullptr;
1599 pldm_entity_association_pdr_extract(pdr.data(), pdr.size(), &num, &out);
1600 EXPECT_EQ(num, (unsigned)e->num_children + 1);
1601 EXPECT_EQ(out[0].entity_type, 1u);
1602 EXPECT_EQ(out[0].entity_instance_num, 1u);
1603 EXPECT_EQ(out[0].entity_container_id, 0u);
1604 EXPECT_EQ(out[1].entity_type, 2u);
1605 EXPECT_EQ(out[1].entity_instance_num, 1u);
1606 EXPECT_EQ(out[1].entity_container_id, 1u);
1607 EXPECT_EQ(out[2].entity_type, 3u);
1608 EXPECT_EQ(out[2].entity_instance_num, 1u);
1609 EXPECT_EQ(out[2].entity_container_id, 1u);
1610 EXPECT_EQ(out[3].entity_type, 4u);
1611 EXPECT_EQ(out[3].entity_instance_num, 1u);
1612 EXPECT_EQ(out[3].entity_container_id, 1u);
1613 EXPECT_EQ(out[4].entity_type, 5u);
1614 EXPECT_EQ(out[4].entity_instance_num, 1u);
1615 EXPECT_EQ(out[4].entity_container_id, 1u);
1616 EXPECT_EQ(out[5].entity_type, 6u);
1617 EXPECT_EQ(out[5].entity_instance_num, 1u);
1618 EXPECT_EQ(out[5].entity_container_id, 1u);
1619
1620 free(out);
1621}
1622
1623TEST(EntityAssociationPDR, testGetChildren)
1624{
1625 pldm_entity entities[4]{};
1626 entities[0].entity_type = 1;
1627 entities[1].entity_type = 2;
1628 entities[2].entity_type = 2;
1629 entities[3].entity_type = 3;
1630
1631 auto tree = pldm_entity_association_tree_init();
1632 auto l1 = pldm_entity_association_tree_add(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001633 tree, &entities[0], 0xffff, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301634 EXPECT_NE(l1, nullptr);
1635 auto l2a = pldm_entity_association_tree_add(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001636 tree, &entities[1], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301637 EXPECT_NE(l2a, nullptr);
1638 auto l2b = pldm_entity_association_tree_add(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001639 tree, &entities[2], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301640 EXPECT_NE(l2b, nullptr);
1641 auto l2c = pldm_entity_association_tree_add(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001642 tree, &entities[3], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301643 EXPECT_NE(l2c, nullptr);
1644
1645 pldm_entity et1;
1646 et1.entity_type = 2;
1647 et1.entity_instance_num = 1;
1648 EXPECT_EQ(true, pldm_is_current_parent_child(l1, &et1));
1649
1650 pldm_entity et2;
1651 et2.entity_type = 2;
1652 et2.entity_instance_num = 2;
1653 EXPECT_EQ(true, pldm_is_current_parent_child(l1, &et2));
1654
1655 pldm_entity et3;
1656 et3.entity_type = 2;
1657 et3.entity_instance_num = 3;
1658 EXPECT_EQ(false, pldm_is_current_parent_child(l1, &et3));
1659
1660 pldm_entity_association_tree_destroy(tree);
1661}
1662
1663TEST(EntityAssociationPDR, testEntityInstanceNumber)
1664{
1665 pldm_entity entities[9]{};
1666
1667 entities[0].entity_type = 1;
1668 entities[1].entity_type = 2;
1669 entities[2].entity_type = 2;
1670 entities[3].entity_type = 2;
1671 entities[4].entity_type = 2;
1672 entities[5].entity_type = 2;
1673 entities[6].entity_type = 2;
1674 entities[7].entity_type = 3;
1675 entities[8].entity_type = 3;
1676
1677 auto tree = pldm_entity_association_tree_init();
1678 auto repo = pldm_pdr_init();
1679
1680 uint16_t terminusHdl{};
1681 uint16_t entityType{};
1682 uint16_t entityInstanceNum{};
1683 uint16_t containerId{};
1684
1685 auto node = pldm_entity_association_tree_add(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001686 tree, &entities[0], 0xffff, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301687 EXPECT_NE(node, nullptr);
1688
1689 auto l1 = pldm_entity_association_tree_add(tree, &entities[1], 63, node,
1690 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery14d29842023-07-17 15:29:32 +09301691 uint32_t first = 1;
1692 EXPECT_EQ(pldm_pdr_add_fru_record_set_check(
1693 repo, 1, 1, entities[1].entity_type,
1694 entities[1].entity_instance_num,
1695 entities[1].entity_container_id, &first),
1696 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301697 EXPECT_NE(l1, nullptr);
1698 EXPECT_EQ(entities[1].entity_instance_num, 63);
1699 EXPECT_EQ(first, pldm_pdr_get_record_handle(
1700 repo, pldm_pdr_fru_record_set_find_by_rsi(
1701 repo, 1, &terminusHdl, &entityType,
1702 &entityInstanceNum, &containerId)));
1703 EXPECT_EQ(entityType, 2);
1704 EXPECT_EQ(entityInstanceNum, 63);
1705
1706 auto l2 = pldm_entity_association_tree_add(tree, &entities[2], 37, node,
1707 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery14d29842023-07-17 15:29:32 +09301708 uint32_t second = 2;
1709 EXPECT_EQ(pldm_pdr_add_fru_record_set_check(
1710 repo, 1, 2, entities[2].entity_type,
1711 entities[2].entity_instance_num,
1712 entities[2].entity_container_id, &second),
1713 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301714 EXPECT_NE(l2, nullptr);
1715 EXPECT_EQ(entities[2].entity_instance_num, 37);
1716 EXPECT_EQ(second, pldm_pdr_get_record_handle(
1717 repo, pldm_pdr_fru_record_set_find_by_rsi(
1718 repo, 2, &terminusHdl, &entityType,
1719 &entityInstanceNum, &containerId)));
1720 EXPECT_EQ(entityType, 2);
1721 EXPECT_EQ(entityInstanceNum, 37);
1722
1723 auto l3 = pldm_entity_association_tree_add(tree, &entities[3], 44, node,
1724 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery14d29842023-07-17 15:29:32 +09301725 uint32_t third = 3;
1726 EXPECT_EQ(pldm_pdr_add_fru_record_set_check(
1727 repo, 1, 3, entities[3].entity_type,
1728 entities[3].entity_instance_num,
1729 entities[3].entity_container_id, &third),
1730 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301731 EXPECT_NE(l3, nullptr);
1732 EXPECT_EQ(entities[3].entity_instance_num, 44);
1733 EXPECT_EQ(third, pldm_pdr_get_record_handle(
1734 repo, pldm_pdr_fru_record_set_find_by_rsi(
1735 repo, 3, &terminusHdl, &entityType,
1736 &entityInstanceNum, &containerId)));
1737 EXPECT_EQ(entityType, 2);
1738 EXPECT_EQ(entityInstanceNum, 44);
1739
1740 auto l4 = pldm_entity_association_tree_add(tree, &entities[4], 89, node,
1741 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery14d29842023-07-17 15:29:32 +09301742 uint32_t fourth = 4;
1743 EXPECT_EQ(pldm_pdr_add_fru_record_set_check(
1744 repo, 1, 4, entities[4].entity_type,
1745 entities[4].entity_instance_num,
1746 entities[4].entity_container_id, &fourth),
1747 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301748 EXPECT_NE(l4, nullptr);
1749 EXPECT_EQ(entities[4].entity_instance_num, 89);
1750 EXPECT_EQ(fourth, pldm_pdr_get_record_handle(
1751 repo, pldm_pdr_fru_record_set_find_by_rsi(
1752 repo, 4, &terminusHdl, &entityType,
1753 &entityInstanceNum, &containerId)));
1754 EXPECT_EQ(entityType, 2);
1755 EXPECT_EQ(entityInstanceNum, 89);
1756
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001757 auto l5 = pldm_entity_association_tree_add(tree, &entities[5], 0xffff, node,
Andrew Jeffery9c766792022-08-10 23:12:49 +09301758 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery14d29842023-07-17 15:29:32 +09301759 uint32_t fifth = 5;
1760 EXPECT_EQ(pldm_pdr_add_fru_record_set_check(
1761 repo, 1, 5, entities[5].entity_type,
1762 entities[5].entity_instance_num,
1763 entities[5].entity_container_id, &fifth),
1764 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301765 EXPECT_NE(l5, nullptr);
1766 EXPECT_EQ(entities[5].entity_instance_num, 90);
1767 EXPECT_EQ(fifth, pldm_pdr_get_record_handle(
1768 repo, pldm_pdr_fru_record_set_find_by_rsi(
1769 repo, 5, &terminusHdl, &entityType,
1770 &entityInstanceNum, &containerId)));
1771 EXPECT_EQ(entityType, 2);
1772 EXPECT_EQ(entityInstanceNum, 90);
1773
1774 auto l6 = pldm_entity_association_tree_add(tree, &entities[6], 90, node,
1775 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1776 EXPECT_EQ(l6, nullptr);
1777
1778 auto l7 = pldm_entity_association_tree_add(tree, &entities[7], 100, l1,
1779 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery14d29842023-07-17 15:29:32 +09301780 uint32_t seventh = 7;
1781 EXPECT_EQ(pldm_pdr_add_fru_record_set_check(
1782 repo, 1, 7, entities[7].entity_type,
1783 entities[7].entity_instance_num,
1784 entities[7].entity_container_id, &seventh),
1785 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301786 EXPECT_NE(l7, nullptr);
1787 EXPECT_EQ(entities[7].entity_instance_num, 100);
1788 EXPECT_EQ(seventh, pldm_pdr_get_record_handle(
1789 repo, pldm_pdr_fru_record_set_find_by_rsi(
1790 repo, 7, &terminusHdl, &entityType,
1791 &entityInstanceNum, &containerId)));
1792 EXPECT_EQ(entityType, 3);
1793 EXPECT_EQ(entityInstanceNum, 100);
1794
1795 auto l8 = pldm_entity_association_tree_add(tree, &entities[8], 100, l2,
1796 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery14d29842023-07-17 15:29:32 +09301797 uint32_t eighth = 8;
1798 EXPECT_EQ(pldm_pdr_add_fru_record_set_check(
1799 repo, 1, 8, entities[8].entity_type,
1800 entities[8].entity_instance_num,
1801 entities[8].entity_container_id, &eighth),
1802 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301803 EXPECT_NE(l8, nullptr);
1804 EXPECT_EQ(entities[8].entity_instance_num, 100);
1805 EXPECT_EQ(eighth, pldm_pdr_get_record_handle(
1806 repo, pldm_pdr_fru_record_set_find_by_rsi(
1807 repo, 8, &terminusHdl, &entityType,
1808 &entityInstanceNum, &containerId)));
1809 EXPECT_EQ(entityType, 3);
1810 EXPECT_EQ(entityInstanceNum, 100);
1811
1812 pldm_pdr_destroy(repo);
1813 pldm_entity_association_tree_destroy(tree);
1814}
Pavithra Barithaya5dc02572023-05-19 09:24:36 -05001815
Andrew Jeffery14a5fe82023-06-26 15:31:37 +09301816#ifdef LIBPLDM_API_TESTING
Pavithra Barithaya8cf70452023-06-22 04:36:19 -05001817TEST(EntityAssociationPDR, testFindChildContainerID)
Pavithra Barithaya5dc02572023-05-19 09:24:36 -05001818{
1819 pldm_entity entities[3]{};
1820 entities[0].entity_type = 1;
1821 entities[1].entity_type = 2;
1822 entities[2].entity_type = 3;
1823 entities[1].entity_container_id = 2;
1824 entities[1].entity_instance_num = 1;
1825
1826 auto tree = pldm_entity_association_tree_init();
1827 auto l1 = pldm_entity_association_tree_add_entity(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001828 tree, &entities[0], 0xffff, nullptr, PLDM_ENTITY_ASSOCIAION_LOGICAL,
1829 false, true, 0xffff);
Pavithra Barithaya5dc02572023-05-19 09:24:36 -05001830
1831 EXPECT_NE(l1, nullptr);
1832 auto l2 = pldm_entity_association_tree_add_entity(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001833 tree, &entities[1], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
1834 false, 0xffff);
Pavithra Barithaya5dc02572023-05-19 09:24:36 -05001835 EXPECT_NE(l2, nullptr);
1836 auto l3 = pldm_entity_association_tree_add_entity(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001837 tree, &entities[2], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
1838 true, 0xffff);
Pavithra Barithaya5dc02572023-05-19 09:24:36 -05001839 EXPECT_NE(l3, nullptr);
1840
1841 EXPECT_EQ(pldm_entity_get_num_children(l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL),
1842 2);
1843
1844 auto repo = pldm_pdr_init();
Andrew Jeffery255f6542023-08-10 15:46:54 +09301845 int rc = pldm_entity_association_pdr_add_check(tree, repo, false, 1);
1846 ASSERT_EQ(rc, 0);
Pavithra Barithaya5dc02572023-05-19 09:24:36 -05001847
1848 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
1849
1850 uint16_t container_id{};
Pavithra Barithaya8cf70452023-06-22 04:36:19 -05001851 pldm_pdr_find_child_container_id_index_range_exclude(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001852 repo, 1, 1, 0, 0x01000000, 0x01ffffff, &container_id);
Pavithra Barithaya5dc02572023-05-19 09:24:36 -05001853 EXPECT_EQ(container_id, 2);
1854
1855 uint16_t container_id1{};
Pavithra Barithaya8cf70452023-06-22 04:36:19 -05001856 pldm_pdr_find_child_container_id_index_range_exclude(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001857 repo, 1, 1, 0, 0x00000001, 0x00ffffff, &container_id1);
Pavithra Barithaya5dc02572023-05-19 09:24:36 -05001858 EXPECT_EQ(container_id1, 0);
1859
1860 pldm_pdr_destroy(repo);
1861 pldm_entity_association_tree_destroy(tree);
1862}
Andrew Jeffery14a5fe82023-06-26 15:31:37 +09301863#endif
Pavithra Barithaya39eb3222023-11-17 03:30:27 -06001864
1865TEST(EntityAssociationPDR, testNodeAddCheck)
1866{
1867 // pldm_entity entities[4]{};
1868 pldm_entity* entities = (pldm_entity*)malloc(sizeof(pldm_entity) * 4);
1869 entities[0].entity_type = 1;
1870 entities[1].entity_type = 2;
1871 entities[2].entity_type = 2;
1872 entities[3].entity_type = 3;
1873
1874 auto tree = pldm_entity_association_tree_init();
1875
1876 auto l1 = pldm_entity_association_tree_add(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001877 tree, &entities[0], 0xffff, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Pavithra Barithaya39eb3222023-11-17 03:30:27 -06001878 EXPECT_NE(l1, nullptr);
1879 auto l2a = pldm_entity_association_tree_add(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001880 tree, &entities[1], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Pavithra Barithaya39eb3222023-11-17 03:30:27 -06001881 EXPECT_NE(l2a, nullptr);
1882 auto l2b = pldm_entity_association_tree_add(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001883 tree, &entities[2], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Pavithra Barithaya39eb3222023-11-17 03:30:27 -06001884 EXPECT_NE(l2b, nullptr);
1885 auto l2c = pldm_entity_association_tree_add(
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001886 tree, &entities[3], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Pavithra Barithaya39eb3222023-11-17 03:30:27 -06001887 EXPECT_NE(l2c, nullptr);
1888
1889 auto repo = pldm_pdr_init();
1890
1891 EXPECT_EQ(pldm_entity_association_pdr_add_from_node_with_record_handle(
1892 l1, repo, &entities, 4, false, 1, 0),
1893 0);
1894 EXPECT_EQ(pldm_entity_association_pdr_add_from_node_with_record_handle(
1895 l1, repo, &entities, 4, false, 1, 2),
1896 0);
1897 EXPECT_EQ(pldm_entity_association_pdr_add_from_node_with_record_handle(
1898 l1, repo, &entities, 4, false, 1, 23),
1899 0);
1900 EXPECT_EQ(pldm_entity_association_pdr_add_from_node_with_record_handle(
1901 l1, repo, &entities, 4, false, 1, 34),
1902 0);
1903
1904 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
1905
1906 uint8_t* outData = nullptr;
1907 uint32_t size{};
1908 uint32_t nextRecHdl{};
1909 auto hdl = pldm_pdr_find_record(repo, 0, &outData, &size, &nextRecHdl);
1910 EXPECT_NE(hdl, nullptr);
1911
1912 outData = nullptr;
1913 auto hdl1 = pldm_pdr_find_record(repo, 2, &outData, &size, &nextRecHdl);
1914 EXPECT_NE(hdl1, nullptr);
1915 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(outData);
1916 EXPECT_EQ(hdr->record_handle, 2);
1917
1918 outData = nullptr;
1919 auto hdl2 = pldm_pdr_find_record(repo, 23, &outData, &size, &nextRecHdl);
1920 EXPECT_NE(hdl2, nullptr);
1921 pldm_pdr_hdr* hdr1 = reinterpret_cast<pldm_pdr_hdr*>(outData);
1922 EXPECT_EQ(hdr1->record_handle, 23);
1923
1924 outData = nullptr;
1925 auto hdl3 = pldm_pdr_find_record(repo, 3, &outData, &size, &nextRecHdl);
1926 EXPECT_EQ(hdl3, nullptr);
1927
1928 free(entities);
1929 pldm_pdr_destroy(repo);
1930 pldm_entity_association_tree_destroy(tree);
1931}
Varsha Kaverappa37552b92024-02-12 05:06:06 -06001932
Andrew Jeffery1111a972024-06-03 13:39:18 +09301933#ifdef LIBPLDM_API_TESTING
Varsha Kaverappa37552b92024-02-12 05:06:06 -06001934TEST(EntityAssociationPDR, testAddContainedEntityRemotePDR)
1935{
1936 // pldm_entity entities[5]{};
1937 pldm_entity* entities = (pldm_entity*)malloc(sizeof(pldm_entity) * 5);
1938 entities[0].entity_type = 1;
1939 entities[1].entity_type = 2;
1940 entities[2].entity_type = 3;
1941 entities[3].entity_type = 4;
1942 entities[4].entity_type = 5;
1943
1944 auto tree = pldm_entity_association_tree_init();
1945 auto l1 = pldm_entity_association_tree_add(
1946 tree, &entities[0], 0xffff, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1947 EXPECT_NE(l1, nullptr);
1948 auto l2a = pldm_entity_association_tree_add(
1949 tree, &entities[1], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1950 EXPECT_NE(l2a, nullptr);
1951 auto l2b = pldm_entity_association_tree_add(
1952 tree, &entities[2], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1953 EXPECT_NE(l2b, nullptr);
1954 auto l2c = pldm_entity_association_tree_add(
1955 tree, &entities[3], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1956 EXPECT_NE(l2c, nullptr);
1957 auto l3a = pldm_entity_association_tree_add(
1958 tree, &entities[4], 0xffff, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1959 EXPECT_NE(l3a, nullptr);
1960
1961 auto repo = pldm_pdr_init();
1962
1963 EXPECT_EQ(pldm_entity_association_pdr_add_from_node_with_record_handle(
1964 l1, repo, &entities, 5, false, 1, 0),
1965 0);
1966 EXPECT_EQ(pldm_entity_association_pdr_add_from_node_with_record_handle(
1967 l1, repo, &entities, 5, false, 1, 2),
1968 0);
1969 EXPECT_EQ(pldm_entity_association_pdr_add_from_node_with_record_handle(
1970 l1, repo, &entities, 5, false, 1, 23),
1971 0);
1972 EXPECT_EQ(pldm_entity_association_pdr_add_from_node_with_record_handle(
1973 l1, repo, &entities, 5, false, 1, 34),
1974 0);
1975 EXPECT_EQ(pldm_entity_association_pdr_add_from_node_with_record_handle(
1976 l2a, repo, &entities, 5, false, 1, 3),
1977 0);
1978
1979 pldm_entity entity1[1];
1980 entity1[0].entity_type = 2;
1981
1982 EXPECT_EQ(pldm_entity_association_pdr_add_contained_entity_to_remote_pdr(
1983 repo, &entity1[0], 2),
1984 0);
1985
1986 free(entities);
1987 pldm_pdr_destroy(repo);
1988 pldm_entity_association_tree_destroy(tree);
1989}
Andrew Jeffery1111a972024-06-03 13:39:18 +09301990#endif
Varsha Kaverappa37552b92024-02-12 05:06:06 -06001991
Andrew Jeffery1111a972024-06-03 13:39:18 +09301992#ifdef LIBPLDM_API_TESTING
Varsha Kaverappa37552b92024-02-12 05:06:06 -06001993TEST(EntityAssociationPDR, testAddContainedEntityNew)
1994{
1995 // pldm_entity entities[5]{};
1996 pldm_entity* entities = (pldm_entity*)malloc(sizeof(pldm_entity) * 5);
1997 entities[0].entity_type = 1;
1998 entities[1].entity_type = 2;
1999 entities[2].entity_type = 3;
2000 entities[3].entity_type = 4;
2001 entities[4].entity_type = 5;
2002
2003 auto tree = pldm_entity_association_tree_init();
2004 auto l1 = pldm_entity_association_tree_add(
2005 tree, &entities[0], 0xffff, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
2006 EXPECT_NE(l1, nullptr);
2007 auto l2a = pldm_entity_association_tree_add(
2008 tree, &entities[1], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
2009 EXPECT_NE(l2a, nullptr);
2010 auto l2b = pldm_entity_association_tree_add(
2011 tree, &entities[2], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
2012 EXPECT_NE(l2b, nullptr);
2013 auto l2c = pldm_entity_association_tree_add(
2014 tree, &entities[3], 0xffff, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
2015 EXPECT_NE(l2c, nullptr);
2016 auto l3a = pldm_entity_association_tree_add(
2017 tree, &entities[4], 0xffff, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
2018 EXPECT_NE(l3a, nullptr);
2019
2020 auto repo = pldm_pdr_init();
2021
2022 EXPECT_EQ(pldm_entity_association_pdr_add_from_node_with_record_handle(
2023 l1, repo, &entities, 5, false, 1, 0),
2024 0);
2025 EXPECT_EQ(pldm_entity_association_pdr_add_from_node_with_record_handle(
2026 l1, repo, &entities, 5, false, 1, 2),
2027 0);
2028 EXPECT_EQ(pldm_entity_association_pdr_add_from_node_with_record_handle(
2029 l1, repo, &entities, 5, false, 1, 23),
2030 0);
2031 EXPECT_EQ(pldm_entity_association_pdr_add_from_node_with_record_handle(
2032 l1, repo, &entities, 5, false, 1, 34),
2033 0);
2034 EXPECT_EQ(pldm_entity_association_pdr_add_from_node_with_record_handle(
2035 l2a, repo, &entities, 5, false, 1, 3),
2036 0);
2037
2038 uint32_t updated_record_handle = 0;
2039
2040 pldm_entity entity2[1]{};
2041 entity2[0].entity_type = 4;
2042
2043 pldm_entity entity3[1]{};
2044 entity3[0].entity_type = 4;
2045
2046 EXPECT_EQ(pldm_entity_association_pdr_create_new(
2047 repo, 34, &entity2[0], &entity3[0], &updated_record_handle),
2048 0);
2049
2050 EXPECT_EQ(updated_record_handle, 35);
2051
2052 free(entities);
2053 pldm_pdr_destroy(repo);
2054 pldm_entity_association_tree_destroy(tree);
2055}
Andrew Jeffery1111a972024-06-03 13:39:18 +09302056#endif