blob: a6955a4325a6d2557d5b1dc4f0848bdb970a6746 [file] [log] [blame]
Manojkiran Eda9a8e4972022-11-28 16:38:21 +05301#include <endian.h>
Manojkiran Eda9a8e4972022-11-28 16:38:21 +05302
Andrew Jeffery9c766792022-08-10 23:12:49 +09303#include <array>
Manojkiran Eda9a8e4972022-11-28 16:38:21 +05304#include <cstdint>
Andrew Jeffery5a706072023-04-05 19:45:31 +09305#include <cstdlib>
6#include <cstring>
Manojkiran Eda9a8e4972022-11-28 16:38:21 +05307#include <vector>
Andrew Jeffery9c766792022-08-10 23:12:49 +09308
9#include "libpldm/pdr.h"
10#include "libpldm/platform.h"
11
12#include <gtest/gtest.h>
13
14TEST(PDRAccess, testInit)
15{
16 auto repo = pldm_pdr_init();
17 EXPECT_EQ(pldm_pdr_get_record_count(repo), 0u);
18 EXPECT_EQ(pldm_pdr_get_repo_size(repo), 0u);
19 pldm_pdr_destroy(repo);
20}
21
22TEST(PDRUpdate, testAdd)
23{
24 auto repo = pldm_pdr_init();
25
26 std::array<uint8_t, 10> data{};
Andrew Jefferyc79a5df2023-07-17 15:09:21 +093027 uint32_t handle = 0;
28 EXPECT_EQ(
29 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, &handle),
30 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +093031 EXPECT_EQ(handle, 1u);
32 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
33 EXPECT_EQ(pldm_pdr_get_repo_size(repo), data.size());
34
Andrew Jefferyc79a5df2023-07-17 15:09:21 +093035 handle = 0;
36 EXPECT_EQ(
37 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, &handle),
38 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +093039 EXPECT_EQ(handle, 2u);
Andrew Jefferyc79a5df2023-07-17 15:09:21 +093040
41 handle = 0;
42 EXPECT_EQ(
43 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, &handle),
44 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +093045 EXPECT_EQ(handle, 3u);
Andrew Jefferyc79a5df2023-07-17 15:09:21 +093046
47 handle = 0xdeeddeedu;
48 EXPECT_EQ(
49 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, &handle),
50 0);
51 EXPECT_EQ(handle, 0xdeeddeed);
Andrew Jeffery9c766792022-08-10 23:12:49 +093052 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
53 EXPECT_EQ(pldm_pdr_get_repo_size(repo), data.size() * 4u);
54
55 pldm_pdr_destroy(repo);
56}
57
58TEST(PDRRemoveByTerminus, testRemoveByTerminus)
59{
60 std::array<uint8_t, 10> data{};
61
62 auto repo = pldm_pdr_init();
Andrew Jefferyc79a5df2023-07-17 15:09:21 +093063
64 EXPECT_EQ(
65 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +093066 pldm_pdr_remove_pdrs_by_terminus_handle(repo, 1);
67 EXPECT_EQ(pldm_pdr_get_record_count(repo), 0u);
68 pldm_pdr_destroy(repo);
69
70 repo = pldm_pdr_init();
Andrew Jefferyc79a5df2023-07-17 15:09:21 +093071 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 2, NULL),
72 0);
73 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
74 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +093075 EXPECT_EQ(pldm_pdr_get_record_count(repo), 2u);
76 pldm_pdr_remove_pdrs_by_terminus_handle(repo, 1);
77 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
78 pldm_pdr_destroy(repo);
79
80 repo = pldm_pdr_init();
Andrew Jefferyc79a5df2023-07-17 15:09:21 +093081 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
82 0);
83 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 2, NULL),
84 0);
85 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 2, NULL),
86 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +093087 EXPECT_EQ(pldm_pdr_get_record_count(repo), 3u);
88 pldm_pdr_remove_pdrs_by_terminus_handle(repo, 2);
89 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
90 pldm_pdr_destroy(repo);
91
92 repo = pldm_pdr_init();
Andrew Jefferyc79a5df2023-07-17 15:09:21 +093093 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
94 0);
95 EXPECT_EQ(
96 pldm_pdr_add_check(repo, data.data(), data.size(), false, 2, NULL), 0);
97 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
98 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +093099 EXPECT_EQ(pldm_pdr_get_record_count(repo), 3u);
100 pldm_pdr_remove_remote_pdrs(repo);
101 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
102 pldm_pdr_remove_pdrs_by_terminus_handle(repo, 1);
103 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
104 pldm_pdr_remove_pdrs_by_terminus_handle(repo, 2);
105 EXPECT_EQ(pldm_pdr_get_record_count(repo), 0u);
106 pldm_pdr_destroy(repo);
107}
108
109TEST(PDRUpdate, testRemove)
110{
111 std::array<uint8_t, 10> data{};
112
113 auto repo = pldm_pdr_init();
114 pldm_pdr_remove_remote_pdrs(repo);
115 EXPECT_EQ(pldm_pdr_get_record_count(repo), 0u);
116 pldm_pdr_destroy(repo);
117
118 repo = pldm_pdr_init();
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930119 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
120 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930121 pldm_pdr_remove_remote_pdrs(repo);
122 EXPECT_EQ(pldm_pdr_get_record_count(repo), 0u);
123 pldm_pdr_destroy(repo);
124
125 repo = pldm_pdr_init();
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930126 EXPECT_EQ(
127 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930128 pldm_pdr_remove_remote_pdrs(repo);
129 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
130 pldm_pdr_destroy(repo);
131
132 repo = pldm_pdr_init();
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930133 EXPECT_EQ(
134 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
135 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
136 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930137 pldm_pdr_remove_remote_pdrs(repo);
138 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
139 pldm_pdr_destroy(repo);
140
141 repo = pldm_pdr_init();
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930142 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
143 0);
144 EXPECT_EQ(
145 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930146 pldm_pdr_remove_remote_pdrs(repo);
147 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
148 pldm_pdr_destroy(repo);
149
150 repo = pldm_pdr_init();
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930151 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
152 0);
153 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
154 0);
155 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
156 0);
157 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
158 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930159 pldm_pdr_remove_remote_pdrs(repo);
160 EXPECT_EQ(pldm_pdr_get_record_count(repo), 0u);
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930161 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
162 0);
163 EXPECT_EQ(
164 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930165 EXPECT_EQ(pldm_pdr_get_record_count(repo), 2u);
166 pldm_pdr_destroy(repo);
167
168 repo = pldm_pdr_init();
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930169 EXPECT_EQ(
170 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
171 EXPECT_EQ(
172 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
173 EXPECT_EQ(
174 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
175 EXPECT_EQ(
176 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930177 pldm_pdr_remove_remote_pdrs(repo);
178 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930179 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
180 0);
181 EXPECT_EQ(
182 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930183 EXPECT_EQ(pldm_pdr_get_record_count(repo), 6u);
184 pldm_pdr_destroy(repo);
185
186 repo = pldm_pdr_init();
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930187 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
188 0);
189 EXPECT_EQ(
190 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
191 EXPECT_EQ(
192 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
193 EXPECT_EQ(
194 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930195 pldm_pdr_remove_remote_pdrs(repo);
196 EXPECT_EQ(pldm_pdr_get_record_count(repo), 3u);
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930197 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
198 0);
199 EXPECT_EQ(
200 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930201 EXPECT_EQ(pldm_pdr_get_record_count(repo), 5u);
202 pldm_pdr_destroy(repo);
203
204 repo = pldm_pdr_init();
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930205 EXPECT_EQ(
206 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
207 EXPECT_EQ(
208 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
209 EXPECT_EQ(
210 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
211 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
212 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930213 pldm_pdr_remove_remote_pdrs(repo);
214 EXPECT_EQ(pldm_pdr_get_record_count(repo), 3u);
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930215 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
216 0);
217 EXPECT_EQ(
218 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930219 EXPECT_EQ(pldm_pdr_get_record_count(repo), 5u);
220 pldm_pdr_destroy(repo);
221
222 repo = pldm_pdr_init();
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930223 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
224 0);
225 EXPECT_EQ(
226 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
227 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
228 0);
229 EXPECT_EQ(
230 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930231 pldm_pdr_remove_remote_pdrs(repo);
232 EXPECT_EQ(pldm_pdr_get_record_count(repo), 2u);
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930233 EXPECT_EQ(
234 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
235 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
236 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930237 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
238 pldm_pdr_destroy(repo);
239
240 repo = pldm_pdr_init();
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930241 EXPECT_EQ(
242 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
243 EXPECT_EQ(
244 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
245 EXPECT_EQ(
246 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
247 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
248 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930249 pldm_pdr_remove_remote_pdrs(repo);
250 EXPECT_EQ(pldm_pdr_get_record_count(repo), 3u);
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930251 EXPECT_EQ(
252 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
253 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
254 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930255 EXPECT_EQ(pldm_pdr_get_record_count(repo), 5u);
256 pldm_pdr_destroy(repo);
257
258 repo = pldm_pdr_init();
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930259 EXPECT_EQ(
260 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
261 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
262 0);
263 EXPECT_EQ(
264 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
265 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
266 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930267 pldm_pdr_remove_remote_pdrs(repo);
268 EXPECT_EQ(pldm_pdr_get_record_count(repo), 2u);
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930269 EXPECT_EQ(
270 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
271 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
272 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930273 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
274 pldm_pdr_destroy(repo);
275
276 repo = pldm_pdr_init();
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930277 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
278 0);
279 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
280 0);
281 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
282 0);
283 EXPECT_EQ(
284 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930285 pldm_pdr_remove_remote_pdrs(repo);
286 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930287 EXPECT_EQ(
288 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
289 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
290 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930291 EXPECT_EQ(pldm_pdr_get_record_count(repo), 3u);
292 pldm_pdr_destroy(repo);
293
294 repo = pldm_pdr_init();
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930295 EXPECT_EQ(
296 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
297 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
298 0);
299 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
300 0);
301 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
302 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930303 pldm_pdr_remove_remote_pdrs(repo);
304 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930305 EXPECT_EQ(
306 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
307 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
308 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930309 EXPECT_EQ(pldm_pdr_get_record_count(repo), 3u);
310 pldm_pdr_destroy(repo);
311
312 repo = pldm_pdr_init();
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930313 EXPECT_EQ(
314 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
315 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
316 0);
317 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
318 0);
319 EXPECT_EQ(
320 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930321 pldm_pdr_remove_remote_pdrs(repo);
322 EXPECT_EQ(pldm_pdr_get_record_count(repo), 2u);
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930323 EXPECT_EQ(
324 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
325 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
326 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930327 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
328 pldm_pdr_destroy(repo);
329
330 repo = pldm_pdr_init();
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930331 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
332 0);
333 EXPECT_EQ(
334 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
335 EXPECT_EQ(
336 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, NULL), 0);
337 EXPECT_EQ(pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, NULL),
338 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930339 pldm_pdr_remove_remote_pdrs(repo);
340 EXPECT_EQ(pldm_pdr_get_record_count(repo), 2u);
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930341 uint32_t handle = 0;
342 EXPECT_EQ(
343 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, &handle),
344 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930345 EXPECT_EQ(handle, 3u);
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930346 handle = 0;
347 EXPECT_EQ(
348 pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, &handle),
349 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930350 EXPECT_EQ(handle, 4u);
351 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
352 pldm_pdr_destroy(repo);
353}
354
355TEST(PDRAccess, testGet)
356{
357 auto repo = pldm_pdr_init();
358
359 std::array<uint32_t, 10> in{100, 345, 3, 6, 89, 0, 11, 45, 23434, 123123};
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930360 uint32_t handle = 1;
361 EXPECT_EQ(pldm_pdr_add_check(repo, reinterpret_cast<uint8_t*>(in.data()),
362 sizeof(in), false, 1, &handle),
363 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930364 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
365 EXPECT_EQ(pldm_pdr_get_repo_size(repo), sizeof(in));
366 uint32_t size{};
367 uint32_t nextRecHdl{};
368 uint8_t* outData = nullptr;
369 auto hdl = pldm_pdr_find_record(repo, 0, &outData, &size, &nextRecHdl);
370 EXPECT_NE(hdl, nullptr);
371 EXPECT_EQ(size, sizeof(in));
372 EXPECT_EQ(nextRecHdl, 0u);
373 EXPECT_EQ(memcmp(outData, in.data(), sizeof(in)), 0);
374 outData = nullptr;
375
376 auto hdl2 = pldm_pdr_find_record(repo, 1, &outData, &size, &nextRecHdl);
377 EXPECT_EQ(hdl, hdl2);
378 EXPECT_NE(hdl, nullptr);
379 EXPECT_EQ(size, sizeof(in));
380 EXPECT_EQ(nextRecHdl, 0u);
381 EXPECT_EQ(memcmp(outData, in.data(), sizeof(in)), 0);
382 outData = nullptr;
383
384 hdl = pldm_pdr_find_record(repo, htole32(0xdeaddead), &outData, &size,
385 &nextRecHdl);
386 EXPECT_EQ(hdl, nullptr);
387 EXPECT_EQ(size, 0u);
388 EXPECT_EQ(nextRecHdl, 0u);
389 EXPECT_EQ(outData, nullptr);
390 outData = nullptr;
391
392 std::array<uint32_t, 10> in2{1000, 3450, 30, 60, 890,
393 0, 110, 450, 234034, 123123};
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930394 handle = 2;
395 EXPECT_EQ(pldm_pdr_add_check(repo, reinterpret_cast<uint8_t*>(in2.data()),
396 sizeof(in2), false, 1, &handle),
397 0);
398 handle = 3;
399 EXPECT_EQ(pldm_pdr_add_check(repo, reinterpret_cast<uint8_t*>(in2.data()),
400 sizeof(in2), false, 1, &handle),
401 0);
402 handle = 4;
403 EXPECT_EQ(pldm_pdr_add_check(repo, reinterpret_cast<uint8_t*>(in2.data()),
404 sizeof(in2), true, 1, &handle),
405 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930406 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
407 EXPECT_EQ(pldm_pdr_get_repo_size(repo), sizeof(in2) * 4);
408 hdl = pldm_pdr_find_record(repo, 0, &outData, &size, &nextRecHdl);
409 EXPECT_NE(hdl, nullptr);
410 EXPECT_EQ(size, sizeof(in));
411 EXPECT_EQ(nextRecHdl, 2u);
412 EXPECT_EQ(memcmp(outData, in.data(), sizeof(in)), 0);
413 outData = nullptr;
414 hdl2 = pldm_pdr_find_record(repo, 1, &outData, &size, &nextRecHdl);
415 EXPECT_EQ(hdl, hdl2);
416 EXPECT_NE(hdl, nullptr);
417 EXPECT_EQ(size, sizeof(in));
418 EXPECT_EQ(nextRecHdl, 2u);
419 EXPECT_EQ(memcmp(outData, in.data(), sizeof(in)), 0);
420 outData = nullptr;
421 hdl = pldm_pdr_find_record(repo, 2, &outData, &size, &nextRecHdl);
422 EXPECT_NE(hdl, nullptr);
423 EXPECT_EQ(size, sizeof(in2));
424 EXPECT_EQ(nextRecHdl, 3u);
425 EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
426 outData = nullptr;
427 hdl = pldm_pdr_find_record(repo, 3, &outData, &size, &nextRecHdl);
428 EXPECT_NE(hdl, nullptr);
429 EXPECT_EQ(pldm_pdr_record_is_remote(hdl), false);
430 EXPECT_EQ(size, sizeof(in2));
431 EXPECT_EQ(nextRecHdl, 4u);
432 EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
433 outData = nullptr;
434 hdl = pldm_pdr_find_record(repo, 4, &outData, &size, &nextRecHdl);
435 EXPECT_NE(hdl, nullptr);
436 EXPECT_EQ(pldm_pdr_record_is_remote(hdl), true);
437 EXPECT_EQ(size, sizeof(in2));
438 EXPECT_EQ(nextRecHdl, 0u);
439 EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
440 outData = nullptr;
441
442 pldm_pdr_destroy(repo);
443}
444
445TEST(PDRAccess, testGetNext)
446{
447 auto repo = pldm_pdr_init();
448
449 std::array<uint32_t, 10> in{100, 345, 3, 6, 89, 0, 11, 45, 23434, 123123};
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930450 uint32_t handle = 1;
451 EXPECT_EQ(pldm_pdr_add_check(repo, reinterpret_cast<uint8_t*>(in.data()),
452 sizeof(in), false, 1, &handle),
453 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930454 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
455 EXPECT_EQ(pldm_pdr_get_repo_size(repo), sizeof(in));
456 uint32_t size{};
457 uint32_t nextRecHdl{};
458 uint8_t* outData = nullptr;
459 auto hdl = pldm_pdr_find_record(repo, 0, &outData, &size, &nextRecHdl);
460 EXPECT_NE(hdl, nullptr);
461 EXPECT_EQ(size, sizeof(in));
462 EXPECT_EQ(nextRecHdl, 0u);
463 EXPECT_EQ(memcmp(outData, in.data(), sizeof(in)), 0);
464 outData = nullptr;
465
466 std::array<uint32_t, 10> in2{1000, 3450, 30, 60, 890,
467 0, 110, 450, 234034, 123123};
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930468 handle = 2;
469 EXPECT_EQ(pldm_pdr_add_check(repo, reinterpret_cast<uint8_t*>(in2.data()),
470 sizeof(in2), false, 1, &handle),
471 0);
472 handle = 3;
473 EXPECT_EQ(pldm_pdr_add_check(repo, reinterpret_cast<uint8_t*>(in2.data()),
474 sizeof(in2), false, 1, &handle),
475 0);
476 handle = 4;
477 EXPECT_EQ(pldm_pdr_add_check(repo, reinterpret_cast<uint8_t*>(in2.data()),
478 sizeof(in2), false, 1, &handle),
479 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930480 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
481 EXPECT_EQ(pldm_pdr_get_repo_size(repo), sizeof(in2) * 4);
482 hdl = pldm_pdr_get_next_record(repo, hdl, &outData, &size, &nextRecHdl);
483 EXPECT_NE(hdl, nullptr);
484 EXPECT_EQ(size, sizeof(in2));
485 EXPECT_EQ(nextRecHdl, 3u);
486 EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
487 outData = nullptr;
488 hdl = pldm_pdr_get_next_record(repo, hdl, &outData, &size, &nextRecHdl);
489 EXPECT_NE(hdl, nullptr);
490 EXPECT_EQ(size, sizeof(in2));
491 EXPECT_EQ(nextRecHdl, 4u);
492 EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
493 outData = nullptr;
494 hdl = pldm_pdr_get_next_record(repo, hdl, &outData, &size, &nextRecHdl);
495 EXPECT_NE(hdl, nullptr);
496 EXPECT_EQ(size, sizeof(in2));
497 EXPECT_EQ(nextRecHdl, 0u);
498 EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
499 outData = nullptr;
500
501 pldm_pdr_destroy(repo);
502}
503
504TEST(PDRAccess, testFindByType)
505{
506 auto repo = pldm_pdr_init();
507
508 std::array<uint8_t, sizeof(pldm_pdr_hdr)> data{};
509 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(data.data());
510 hdr->type = 1;
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930511 uint32_t first = 0;
512 EXPECT_EQ(
513 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, &first),
514 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930515 hdr->type = 2;
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930516 uint32_t second = 0;
517 EXPECT_EQ(
518 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, &second),
519 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930520 hdr->type = 3;
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930521 uint32_t third = 0;
522 EXPECT_EQ(
523 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, &third),
524 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930525 hdr->type = 4;
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930526 uint32_t fourth = 0;
527 EXPECT_EQ(
528 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, &fourth),
529 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930530
531 uint8_t* outData = nullptr;
532 uint32_t size{};
533 auto firstRec =
534 pldm_pdr_find_record_by_type(repo, 1, nullptr, &outData, &size);
535 EXPECT_EQ(pldm_pdr_get_record_handle(repo, firstRec), first);
536 outData = nullptr;
537 auto secondRec =
538 pldm_pdr_find_record_by_type(repo, 2, nullptr, &outData, &size);
539 EXPECT_EQ(pldm_pdr_get_record_handle(repo, secondRec), second);
540 outData = nullptr;
541 auto thirdRec =
542 pldm_pdr_find_record_by_type(repo, 3, nullptr, &outData, &size);
543 EXPECT_EQ(pldm_pdr_get_record_handle(repo, thirdRec), third);
544 outData = nullptr;
545 auto fourthRec =
546 pldm_pdr_find_record_by_type(repo, 4, nullptr, &outData, &size);
547 EXPECT_EQ(pldm_pdr_get_record_handle(repo, fourthRec), fourth);
548 outData = nullptr;
549 auto fifthRec =
550 pldm_pdr_find_record_by_type(repo, 5, nullptr, &outData, &size);
551 EXPECT_EQ(fifthRec, nullptr);
552 EXPECT_EQ(outData, nullptr);
553 EXPECT_EQ(size, 0u);
554
555 auto rec =
556 pldm_pdr_find_record_by_type(repo, 3, secondRec, &outData, &size);
557 EXPECT_EQ(pldm_pdr_get_record_handle(repo, rec), third);
558 outData = nullptr;
559 rec = pldm_pdr_find_record_by_type(repo, 4, secondRec, &outData, &size);
560 EXPECT_EQ(pldm_pdr_get_record_handle(repo, rec), fourth);
561 outData = nullptr;
562 rec = pldm_pdr_find_record_by_type(repo, 2, firstRec, &outData, &size);
563 EXPECT_EQ(pldm_pdr_get_record_handle(repo, rec), second);
564 outData = nullptr;
565
566 pldm_pdr_destroy(repo);
567}
568
569TEST(PDRUpdate, testAddFruRecordSet)
570{
571 auto repo = pldm_pdr_init();
572
Andrew Jeffery14d29842023-07-17 15:29:32 +0930573 uint32_t handle = 0;
574 EXPECT_EQ(
575 pldm_pdr_add_fru_record_set_check(repo, 1, 10, 1, 0, 100, &handle), 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930576 EXPECT_EQ(handle, 1u);
577 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
578 EXPECT_EQ(pldm_pdr_get_repo_size(repo),
579 sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_fru_record_set));
580 uint32_t size{};
581 uint32_t nextRecHdl{};
582 uint8_t* outData = nullptr;
583 auto hdl = pldm_pdr_find_record(repo, 0, &outData, &size, &nextRecHdl);
584 EXPECT_NE(hdl, nullptr);
585 EXPECT_EQ(size, sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_fru_record_set));
586 EXPECT_EQ(nextRecHdl, 0u);
587 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(outData);
588 EXPECT_EQ(hdr->version, 1u);
589 EXPECT_EQ(hdr->type, PLDM_PDR_FRU_RECORD_SET);
590 EXPECT_EQ(hdr->length, htole16(sizeof(pldm_pdr_fru_record_set)));
591 EXPECT_EQ(hdr->record_handle, htole32(1));
592 pldm_pdr_fru_record_set* fru = reinterpret_cast<pldm_pdr_fru_record_set*>(
593 outData + sizeof(pldm_pdr_hdr));
594 EXPECT_EQ(fru->terminus_handle, htole16(1));
595 EXPECT_EQ(fru->fru_rsi, htole16(10));
596 EXPECT_EQ(fru->entity_type, htole16(1));
597 EXPECT_EQ(fru->entity_instance_num, htole16(0));
598 EXPECT_EQ(fru->container_id, htole16(100));
599 outData = nullptr;
600
Andrew Jeffery14d29842023-07-17 15:29:32 +0930601 handle = 0;
602 EXPECT_EQ(
603 pldm_pdr_add_fru_record_set_check(repo, 2, 11, 2, 1, 101, &handle), 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930604 EXPECT_EQ(handle, 2u);
605 EXPECT_EQ(pldm_pdr_get_record_count(repo), 2u);
606 EXPECT_EQ(pldm_pdr_get_repo_size(repo),
607 2 * (sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_fru_record_set)));
608 hdl = pldm_pdr_find_record(repo, 2, &outData, &size, &nextRecHdl);
609 EXPECT_NE(hdl, nullptr);
610 EXPECT_EQ(size, sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_fru_record_set));
611 EXPECT_EQ(nextRecHdl, 0u);
612 hdr = reinterpret_cast<pldm_pdr_hdr*>(outData);
613 EXPECT_EQ(hdr->version, 1u);
614 EXPECT_EQ(hdr->type, PLDM_PDR_FRU_RECORD_SET);
615 EXPECT_EQ(hdr->length, htole16(sizeof(pldm_pdr_fru_record_set)));
616 EXPECT_EQ(hdr->record_handle, htole32(2));
617 fru = reinterpret_cast<pldm_pdr_fru_record_set*>(outData +
618 sizeof(pldm_pdr_hdr));
619 EXPECT_EQ(fru->terminus_handle, htole16(2));
620 EXPECT_EQ(fru->fru_rsi, htole16(11));
621 EXPECT_EQ(fru->entity_type, htole16(2));
622 EXPECT_EQ(fru->entity_instance_num, htole16(1));
623 EXPECT_EQ(fru->container_id, htole16(101));
624 outData = nullptr;
625
626 hdl = pldm_pdr_find_record(repo, 1, &outData, &size, &nextRecHdl);
627 EXPECT_NE(hdl, nullptr);
628 EXPECT_EQ(size, sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_fru_record_set));
629 EXPECT_EQ(nextRecHdl, 2u);
630 hdr = reinterpret_cast<pldm_pdr_hdr*>(outData);
631 EXPECT_EQ(hdr->version, 1u);
632 EXPECT_EQ(hdr->type, PLDM_PDR_FRU_RECORD_SET);
633 EXPECT_EQ(hdr->length, htole16(sizeof(pldm_pdr_fru_record_set)));
634 EXPECT_EQ(hdr->record_handle, htole32(1));
635 fru = reinterpret_cast<pldm_pdr_fru_record_set*>(outData +
636 sizeof(pldm_pdr_hdr));
637 EXPECT_EQ(fru->terminus_handle, htole16(1));
638 EXPECT_EQ(fru->fru_rsi, htole16(10));
639 EXPECT_EQ(fru->entity_type, htole16(1));
640 EXPECT_EQ(fru->entity_instance_num, htole16(0));
641 EXPECT_EQ(fru->container_id, htole16(100));
642 outData = nullptr;
643
644 pldm_pdr_destroy(repo);
645}
646
647TEST(PDRUpdate, tesFindtFruRecordSet)
648{
649 auto repo = pldm_pdr_init();
650
651 uint16_t terminusHdl{};
652 uint16_t entityType{};
653 uint16_t entityInstanceNum{};
654 uint16_t containerId{};
Andrew Jeffery14d29842023-07-17 15:29:32 +0930655 uint32_t first = 1;
656 EXPECT_EQ(pldm_pdr_add_fru_record_set_check(repo, 1, 1, 1, 0, 100, &first),
657 0);
658 uint32_t second = 2;
659 EXPECT_EQ(pldm_pdr_add_fru_record_set_check(repo, 1, 2, 1, 1, 100, &second),
660 0);
661 uint32_t third = 3;
662 EXPECT_EQ(pldm_pdr_add_fru_record_set_check(repo, 1, 3, 1, 2, 100, &third),
663 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930664 EXPECT_EQ(first, pldm_pdr_get_record_handle(
665 repo, pldm_pdr_fru_record_set_find_by_rsi(
666 repo, 1, &terminusHdl, &entityType,
667 &entityInstanceNum, &containerId)));
668 EXPECT_EQ(second, pldm_pdr_get_record_handle(
669 repo, pldm_pdr_fru_record_set_find_by_rsi(
670 repo, 2, &terminusHdl, &entityType,
671 &entityInstanceNum, &containerId)));
672 EXPECT_EQ(third, pldm_pdr_get_record_handle(
673 repo, pldm_pdr_fru_record_set_find_by_rsi(
674 repo, 3, &terminusHdl, &entityType,
675 &entityInstanceNum, &containerId)));
676 EXPECT_EQ(terminusHdl, 1u);
677 EXPECT_EQ(entityType, 1u);
678 EXPECT_EQ(entityInstanceNum, 2u);
679 EXPECT_EQ(containerId, 100u);
680 EXPECT_EQ(nullptr, pldm_pdr_fru_record_set_find_by_rsi(
681 repo, 4, &terminusHdl, &entityType,
682 &entityInstanceNum, &containerId));
683
684 pldm_pdr_destroy(repo);
685}
686
Pavithra Barithaya4d694342023-05-19 08:04:41 -0500687#ifdef LIBPLDM_API_TESTING
688TEST(PDRUpdate, testFindLastInRange)
689{
690 auto repo = pldm_pdr_init();
691
692 std::array<uint8_t, 10> data{};
Andrew Jefferyc79a5df2023-07-17 15:09:21 +0930693 uint32_t handle1 = 0;
694 EXPECT_EQ(
695 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, &handle1),
696 0);
697 uint32_t handle2 = 23;
698 EXPECT_EQ(
699 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, &handle2),
700 0);
701 uint32_t handle3 = 77;
702 EXPECT_EQ(
703 pldm_pdr_add_check(repo, data.data(), data.size(), false, 1, &handle3),
704 0);
705 uint32_t handle4 = 16777325;
706 EXPECT_EQ(
707 pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, &handle4),
708 0);
709 uint32_t handle5 = 16777344;
710 EXPECT_EQ(
711 pldm_pdr_add_check(repo, data.data(), data.size(), true, 1, &handle5),
712 0);
Pavithra Barithaya4d694342023-05-19 08:04:41 -0500713
714 auto rec1 = pldm_pdr_find_last_in_range(repo, 0, 100);
715 auto rec2 = pldm_pdr_find_last_in_range(repo, 16777300, 33554431);
716 EXPECT_NE(rec1, nullptr);
717 EXPECT_NE(rec2, nullptr);
718 EXPECT_NE(handle1, pldm_pdr_get_record_handle(repo, rec1));
719 EXPECT_NE(handle2, pldm_pdr_get_record_handle(repo, rec1));
720 EXPECT_EQ(handle3, pldm_pdr_get_record_handle(repo, rec1));
721 EXPECT_NE(handle4, pldm_pdr_get_record_handle(repo, rec2));
722 EXPECT_EQ(handle5, pldm_pdr_get_record_handle(repo, rec2));
723
724 pldm_pdr_destroy(repo);
725}
726#endif
727
Andrew Jeffery9c766792022-08-10 23:12:49 +0930728TEST(EntityAssociationPDR, testInit)
729{
730 auto tree = pldm_entity_association_tree_init();
731 EXPECT_NE(tree, nullptr);
732 pldm_entity_association_tree_destroy(tree);
733}
734
735TEST(EntityAssociationPDR, testBuild)
736{
737 // 1
738 // |
739 // 2--3--4
740 // |
741 // 5--6--7
742 // | |
743 // 9 8
744
745 pldm_entity entities[9]{};
746
747 entities[0].entity_type = 1;
748 entities[1].entity_type = 2;
749 entities[2].entity_type = 2;
750 entities[3].entity_type = 3;
751 entities[4].entity_type = 4;
752 entities[5].entity_type = 5;
753 entities[6].entity_type = 5;
754 entities[7].entity_type = 6;
755 entities[8].entity_type = 7;
756
757 auto tree = pldm_entity_association_tree_init();
758
759 auto l1 = pldm_entity_association_tree_add(
760 tree, &entities[0], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
761 EXPECT_NE(l1, nullptr);
762 auto l2a = pldm_entity_association_tree_add(
763 tree, &entities[1], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
764 EXPECT_NE(l2a, nullptr);
765 auto l2b = pldm_entity_association_tree_add(
766 tree, &entities[2], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
767 EXPECT_NE(l2b, nullptr);
768 auto l2c = pldm_entity_association_tree_add(
769 tree, &entities[3], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
770 EXPECT_NE(l2c, nullptr);
771 auto l3a = pldm_entity_association_tree_add(
772 tree, &entities[4], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
773 EXPECT_NE(l3a, nullptr);
774 auto l3b = pldm_entity_association_tree_add(
775 tree, &entities[5], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
776 EXPECT_NE(l3b, nullptr);
777 auto l3c = pldm_entity_association_tree_add(
778 tree, &entities[6], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
779 EXPECT_NE(l3b, nullptr);
780 auto l4a = pldm_entity_association_tree_add(
781 tree, &entities[7], 0xFFFF, l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
782 EXPECT_NE(l4a, nullptr);
783 auto l4b = pldm_entity_association_tree_add(
784 tree, &entities[8], 0xFFFF, l3b, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
785 EXPECT_NE(l4b, nullptr);
786
787 EXPECT_EQ(pldm_entity_is_node_parent(l1), true);
788 EXPECT_EQ(pldm_entity_is_node_parent(l2a), true);
789 EXPECT_EQ(pldm_entity_is_node_parent(l3a), true);
790 EXPECT_EQ(pldm_entity_is_node_parent(l3b), true);
791
792 EXPECT_EQ(pldm_entity_is_node_parent(l2b), false);
793 EXPECT_EQ(pldm_entity_is_node_parent(l2c), false);
794 EXPECT_EQ(pldm_entity_is_node_parent(l3c), false);
795 EXPECT_EQ(pldm_entity_is_node_parent(l4a), false);
796 EXPECT_EQ(pldm_entity_is_node_parent(l4b), false);
797
798 EXPECT_EQ(pldm_entity_is_exist_parent(l1), false);
799
800 pldm_entity nodeL1 = pldm_entity_extract(l1);
801 pldm_entity parentL2a = pldm_entity_get_parent(l2a);
802 pldm_entity parentL2b = pldm_entity_get_parent(l2b);
803 pldm_entity parentL2c = pldm_entity_get_parent(l2c);
804 EXPECT_EQ(pldm_entity_is_exist_parent(l2a), true);
805 EXPECT_EQ(pldm_entity_is_exist_parent(l2b), true);
806 EXPECT_EQ(pldm_entity_is_exist_parent(l2c), true);
807 EXPECT_EQ(parentL2a.entity_type, nodeL1.entity_type);
808 EXPECT_EQ(parentL2a.entity_instance_num, nodeL1.entity_instance_num);
809 EXPECT_EQ(parentL2a.entity_container_id, nodeL1.entity_container_id);
810 EXPECT_EQ(parentL2b.entity_type, nodeL1.entity_type);
811 EXPECT_EQ(parentL2b.entity_instance_num, nodeL1.entity_instance_num);
812 EXPECT_EQ(parentL2b.entity_container_id, nodeL1.entity_container_id);
813 EXPECT_EQ(parentL2c.entity_type, nodeL1.entity_type);
814 EXPECT_EQ(parentL2c.entity_instance_num, nodeL1.entity_instance_num);
815 EXPECT_EQ(parentL2c.entity_container_id, nodeL1.entity_container_id);
816
817 pldm_entity nodeL2a = pldm_entity_extract(l2a);
818 pldm_entity parentL3a = pldm_entity_get_parent(l3a);
819 pldm_entity parentL3b = pldm_entity_get_parent(l3b);
820 pldm_entity parentL3c = pldm_entity_get_parent(l3c);
821 EXPECT_EQ(pldm_entity_is_exist_parent(l3a), true);
822 EXPECT_EQ(pldm_entity_is_exist_parent(l3b), true);
823 EXPECT_EQ(pldm_entity_is_exist_parent(l3c), true);
824 EXPECT_EQ(parentL3a.entity_type, nodeL2a.entity_type);
825 EXPECT_EQ(parentL3a.entity_instance_num, nodeL2a.entity_instance_num);
826 EXPECT_EQ(parentL3a.entity_container_id, nodeL2a.entity_container_id);
827 EXPECT_EQ(parentL3b.entity_type, nodeL2a.entity_type);
828 EXPECT_EQ(parentL3b.entity_instance_num, nodeL2a.entity_instance_num);
829 EXPECT_EQ(parentL3b.entity_container_id, nodeL2a.entity_container_id);
830 EXPECT_EQ(parentL3c.entity_type, nodeL2a.entity_type);
831 EXPECT_EQ(parentL3c.entity_instance_num, nodeL2a.entity_instance_num);
832 EXPECT_EQ(parentL3c.entity_container_id, nodeL2a.entity_container_id);
833
834 pldm_entity nodeL3a = pldm_entity_extract(l3a);
835 pldm_entity parentL4a = pldm_entity_get_parent(l4a);
836 EXPECT_EQ(pldm_entity_is_exist_parent(l4a), true);
837 EXPECT_EQ(parentL4a.entity_type, nodeL3a.entity_type);
838 EXPECT_EQ(parentL4a.entity_instance_num, nodeL3a.entity_instance_num);
839 EXPECT_EQ(parentL4a.entity_container_id, nodeL3a.entity_container_id);
840
841 pldm_entity nodeL3b = pldm_entity_extract(l3b);
842 pldm_entity parentL4b = pldm_entity_get_parent(l4b);
843 EXPECT_EQ(pldm_entity_is_exist_parent(l4b), true);
844 EXPECT_EQ(parentL4b.entity_type, nodeL3b.entity_type);
845 EXPECT_EQ(parentL4b.entity_instance_num, nodeL3b.entity_instance_num);
846 EXPECT_EQ(parentL4b.entity_container_id, nodeL3b.entity_container_id);
847
848 size_t num{};
849 pldm_entity* out = nullptr;
850 pldm_entity_association_tree_visit(tree, &out, &num);
851 EXPECT_EQ(num, 9u);
852
853 EXPECT_EQ(out[0].entity_type, 1u);
854 EXPECT_EQ(out[0].entity_instance_num, 1u);
855 EXPECT_EQ(out[0].entity_container_id, 0u);
856
857 EXPECT_EQ(out[1].entity_type, 2u);
858 EXPECT_EQ(out[1].entity_instance_num, 1u);
859 EXPECT_EQ(out[1].entity_container_id, 1u);
860 EXPECT_EQ(out[2].entity_type, 2u);
861 EXPECT_EQ(out[2].entity_instance_num, 2u);
862 EXPECT_EQ(out[2].entity_container_id, 1u);
863 EXPECT_EQ(out[3].entity_type, 3u);
864 EXPECT_EQ(out[3].entity_instance_num, 1u);
865 EXPECT_EQ(out[3].entity_container_id, 1u);
866
867 EXPECT_EQ(out[4].entity_type, 4u);
868 EXPECT_EQ(out[4].entity_instance_num, 1u);
869 EXPECT_EQ(out[4].entity_container_id, 2u);
870 EXPECT_EQ(out[5].entity_type, 5u);
871 EXPECT_EQ(out[5].entity_instance_num, 1u);
872 EXPECT_EQ(out[5].entity_container_id, 2u);
873 EXPECT_EQ(out[6].entity_type, 5u);
874 EXPECT_EQ(out[6].entity_instance_num, 2u);
875 EXPECT_EQ(out[6].entity_container_id, 2u);
876
877 EXPECT_EQ(out[7].entity_type, 7u);
878 EXPECT_EQ(out[7].entity_instance_num, 1u);
879 EXPECT_EQ(out[7].entity_container_id, 4u);
880 EXPECT_EQ(out[8].entity_type, 6u);
881 EXPECT_EQ(out[8].entity_instance_num, 1u);
882 EXPECT_EQ(out[8].entity_container_id, 3u);
883
884 free(out);
885
886 pldm_entity p1 = pldm_entity_extract(l1);
887 EXPECT_EQ(p1.entity_type, 1u);
888 EXPECT_EQ(p1.entity_instance_num, 1u);
889 EXPECT_EQ(p1.entity_container_id, 0u);
890
891 pldm_entity p2a = pldm_entity_extract(l2a);
892 EXPECT_EQ(p2a.entity_type, 2u);
893 EXPECT_EQ(p2a.entity_instance_num, 1u);
894 EXPECT_EQ(p2a.entity_container_id, 1u);
895 pldm_entity p2b = pldm_entity_extract(l2b);
896 EXPECT_EQ(p2b.entity_type, 2u);
897 EXPECT_EQ(p2b.entity_instance_num, 2u);
898 EXPECT_EQ(p2b.entity_container_id, 1u);
899 pldm_entity p2c = pldm_entity_extract(l2c);
900 EXPECT_EQ(p2c.entity_type, 3u);
901 EXPECT_EQ(p2c.entity_instance_num, 1u);
902 EXPECT_EQ(p2c.entity_container_id, 1u);
903
904 pldm_entity p3a = pldm_entity_extract(l3a);
905 EXPECT_EQ(p3a.entity_type, 4u);
906 EXPECT_EQ(p3a.entity_instance_num, 1u);
907 EXPECT_EQ(p3a.entity_container_id, 2u);
908 pldm_entity p3b = pldm_entity_extract(l3b);
909 EXPECT_EQ(p3b.entity_type, 5u);
910 EXPECT_EQ(p3b.entity_instance_num, 1u);
911 EXPECT_EQ(p3b.entity_container_id, 2u);
912 pldm_entity p3c = pldm_entity_extract(l3c);
913 EXPECT_EQ(p3c.entity_type, 5u);
914 EXPECT_EQ(p3c.entity_instance_num, 2u);
915 EXPECT_EQ(p3c.entity_container_id, 2u);
916
917 pldm_entity p4a = pldm_entity_extract(l4a);
918 EXPECT_EQ(p4a.entity_type, 6u);
919 EXPECT_EQ(p4a.entity_instance_num, 1u);
920 EXPECT_EQ(p4a.entity_container_id, 3u);
921 pldm_entity p4b = pldm_entity_extract(l4b);
922 EXPECT_EQ(p4b.entity_type, 7u);
923 EXPECT_EQ(p4b.entity_instance_num, 1u);
924 EXPECT_EQ(p4b.entity_container_id, 4u);
925
926 pldm_entity_association_tree_destroy(tree);
927}
928
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500929#if LIBPLDM_API_TESTING
930TEST(EntityAssociationPDR, findAndAddRemotePDR)
931{
932 // Tree - 1
933 //
934 // 11521(1,0)
935 // |
936 // 45 (1,1)
937 // |
938 // 64 (1,2)
939 // |
940 // ------------------
941 // | |
942 // 67(0,3) 67(1,3)
943 // | |
944 // 135(0,4) 135(0,5)
945 // | |
946 // 32903(0,6) 32903(0,7)
947
948 pldm_entity entities[9]{};
949 entities[0].entity_type = 11521;
950 entities[1].entity_type = 45;
951 entities[2].entity_type = 64;
952 entities[3].entity_type = 67;
953 entities[4].entity_type = 67;
954 entities[5].entity_type = 135;
955 entities[5].entity_container_id = 2;
956 entities[6].entity_type = 135;
957 entities[6].entity_container_id = 3;
958 entities[7].entity_type = 32903;
959 entities[8].entity_type = 32903;
960 auto tree = pldm_entity_association_tree_init();
961 auto l1 = pldm_entity_association_tree_add_entity(
962 tree, &entities[0], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_LOGICAL,
963 false, true, 0xFFFF);
964 EXPECT_NE(l1, nullptr);
965 auto l2 = pldm_entity_association_tree_add_entity(
966 tree, &entities[1], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
967 false, 0xFFFF);
968 EXPECT_NE(l2, nullptr);
969 auto l3 = pldm_entity_association_tree_add_entity(
970 tree, &entities[2], 0xFFFF, l2, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
971 true, 0xFFFF);
972 EXPECT_NE(l3, nullptr);
973 auto l4a = pldm_entity_association_tree_add_entity(
974 tree, &entities[3], 0, l3, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
975 false, 0xFFFF);
976 EXPECT_NE(l4a, nullptr);
977 auto l4b = pldm_entity_association_tree_add_entity(
978 tree, &entities[4], 1, l3, PLDM_ENTITY_ASSOCIAION_PHYSICAL, true, true,
979 0xFFFF);
980 EXPECT_NE(l4b, nullptr);
981 auto l5a = pldm_entity_association_tree_add_entity(
982 tree, &entities[5], 0, l4a, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
983 false, 0xFFFF);
984 EXPECT_NE(l5a, nullptr);
985 auto l5b = pldm_entity_association_tree_add_entity(
986 tree, &entities[6], 0, l4b, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
987 false, 0xFFFF);
988 EXPECT_NE(l5b, nullptr);
989 pldm_entity entity{};
990 entity.entity_type = 135;
991 entity.entity_instance_num = 0;
992 entity.entity_container_id = 2;
993 auto result1 = pldm_entity_association_tree_find(tree, &entity);
994 EXPECT_EQ(result1, l5a);
995 EXPECT_EQ(entities[5].entity_container_id, 2);
996 auto l6a = pldm_entity_association_tree_add_entity(
997 tree, &entities[7], 0, result1, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
998 false, 0xFFFF);
999 EXPECT_NE(l6a, nullptr);
1000 entity.entity_type = 135;
1001 entity.entity_instance_num = 0;
1002 entity.entity_container_id = 3;
1003 auto result2 = pldm_entity_association_tree_find(tree, &entity);
1004 EXPECT_NE(result2, l5b);
1005 EXPECT_EQ(entities[6].entity_container_id, 3);
1006 auto l7a = pldm_entity_association_tree_add_entity(
1007 tree, &entities[8], 0, result2, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
1008 false, 0xFFFF);
1009 EXPECT_EQ(l7a, nullptr);
1010 pldm_entity_association_tree_destroy(tree);
1011}
1012#endif
1013
Andrew Jeffery9c766792022-08-10 23:12:49 +09301014TEST(EntityAssociationPDR, testSpecialTrees)
1015{
1016 pldm_entity entities[3]{};
1017
1018 entities[0].entity_type = 1;
1019 entities[1].entity_type = 2;
1020 entities[2].entity_type = 1;
1021
1022 // A
1023 auto tree = pldm_entity_association_tree_init();
1024 auto node = pldm_entity_association_tree_add(
1025 tree, &entities[0], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1026 EXPECT_NE(node, nullptr);
1027 size_t num{};
1028 pldm_entity* out = nullptr;
1029 pldm_entity_association_tree_visit(tree, &out, &num);
1030 EXPECT_EQ(num, 1u);
1031 EXPECT_EQ(out[0].entity_type, 1u);
1032 EXPECT_EQ(out[0].entity_instance_num, 1u);
1033 EXPECT_EQ(out[0].entity_container_id, 0u);
1034 free(out);
1035 pldm_entity_association_tree_destroy(tree);
1036
1037 // A-A-A
1038 tree = pldm_entity_association_tree_init();
1039 node = pldm_entity_association_tree_add(tree, &entities[0], 0xFFFF, nullptr,
1040 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1041 EXPECT_NE(node, nullptr);
1042 node = pldm_entity_association_tree_add(tree, &entities[1], 0xFFFF, nullptr,
1043 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1044 EXPECT_NE(node, nullptr);
1045 node = pldm_entity_association_tree_add(tree, &entities[2], 0xFFFF, nullptr,
1046 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1047 EXPECT_NE(node, nullptr);
1048 pldm_entity_association_tree_visit(tree, &out, &num);
1049 EXPECT_EQ(num, 3u);
1050 EXPECT_EQ(out[0].entity_type, 1u);
1051 EXPECT_EQ(out[0].entity_instance_num, 1u);
1052 EXPECT_EQ(out[0].entity_container_id, 0u);
1053 EXPECT_EQ(out[1].entity_type, 1u);
1054 EXPECT_EQ(out[1].entity_instance_num, 2u);
1055 EXPECT_EQ(out[1].entity_container_id, 0u);
1056 EXPECT_EQ(out[2].entity_type, 2u);
1057 EXPECT_EQ(out[2].entity_instance_num, 1u);
1058 EXPECT_EQ(out[2].entity_container_id, 0u);
1059 free(out);
1060 pldm_entity_association_tree_destroy(tree);
1061
1062 // A
1063 // |
1064 // A
1065 // |
1066 // A
1067 tree = pldm_entity_association_tree_init();
1068 node = pldm_entity_association_tree_add(tree, &entities[0], 0xFFFF, nullptr,
1069 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1070 EXPECT_NE(node, nullptr);
1071 auto node1 = pldm_entity_association_tree_add(
1072 tree, &entities[1], 0xFFFF, node, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1073 EXPECT_NE(node1, nullptr);
1074 auto node2 = pldm_entity_association_tree_add(
1075 tree, &entities[2], 0xFFFF, node1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1076 EXPECT_NE(node2, nullptr);
1077 pldm_entity_association_tree_visit(tree, &out, &num);
1078 EXPECT_EQ(num, 3u);
1079 EXPECT_EQ(out[0].entity_type, 1u);
1080 EXPECT_EQ(out[0].entity_instance_num, 1u);
1081 EXPECT_EQ(out[0].entity_container_id, 0u);
1082 EXPECT_EQ(out[1].entity_type, 2u);
1083 EXPECT_EQ(out[1].entity_instance_num, 1u);
1084 EXPECT_EQ(out[1].entity_container_id, 1u);
1085 EXPECT_EQ(out[2].entity_type, 1u);
1086 EXPECT_EQ(out[2].entity_instance_num, 1u);
1087 EXPECT_EQ(out[2].entity_container_id, 2u);
1088 free(out);
1089 pldm_entity_association_tree_destroy(tree);
1090
1091 // A-A
1092 // |
1093 // A-A
1094 tree = pldm_entity_association_tree_init();
1095 node = pldm_entity_association_tree_add(tree, &entities[0], 0xFFFF, nullptr,
1096 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1097 EXPECT_NE(node, nullptr);
1098 node = pldm_entity_association_tree_add(tree, &entities[0], 0xFFFF, nullptr,
1099 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1100 EXPECT_NE(node, nullptr);
1101 node1 = pldm_entity_association_tree_add(tree, &entities[1], 0xFFFF, node,
1102 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1103 EXPECT_NE(node1, nullptr);
1104 node2 = pldm_entity_association_tree_add(tree, &entities[2], 0xFFFF, node,
1105 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1106 EXPECT_NE(node2, nullptr);
1107 pldm_entity_association_tree_visit(tree, &out, &num);
1108 EXPECT_EQ(num, 4u);
1109 EXPECT_EQ(out[0].entity_type, 1u);
1110 EXPECT_EQ(out[0].entity_instance_num, 1u);
1111 EXPECT_EQ(out[0].entity_container_id, 0u);
1112 EXPECT_EQ(out[1].entity_type, 1u);
1113 EXPECT_EQ(out[1].entity_instance_num, 2u);
1114 EXPECT_EQ(out[1].entity_container_id, 0u);
1115 EXPECT_EQ(out[2].entity_type, 2u);
1116 EXPECT_EQ(out[2].entity_instance_num, 1u);
1117 EXPECT_EQ(out[2].entity_container_id, 1u);
1118 EXPECT_EQ(out[3].entity_type, 1u);
1119 EXPECT_EQ(out[3].entity_instance_num, 1u);
1120 EXPECT_EQ(out[3].entity_container_id, 1u);
1121 free(out);
1122 pldm_entity_association_tree_destroy(tree);
1123}
1124
1125TEST(EntityAssociationPDR, testPDR)
1126{
1127 // e = entity type, c = container id, i = instance num
1128
1129 // INPUT
1130 // 1(e=1)--1a(e=2)
1131 // |
1132 // 2(e=2)--3(e=2)--4(e=2)--5(e=3)
1133 // |
1134 // 6(e=4)--7(e=5)--8(e=5)--9(e=5)
1135 // | |
1136 // 11(e=6) 10(e=7)
1137
1138 // Expected OUTPUT
1139 // 1(e=1,c=0,i=1)
1140 // |
1141 // 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)
1142 // |
1143 // 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)
1144 // | |
1145 // 10(e=6,c=3,i=1) 11(e=7,c=4,i=1)
1146 pldm_entity entities[11]{};
1147
1148 entities[0].entity_type = 1;
1149 entities[1].entity_type = 2;
1150 entities[2].entity_type = 3;
1151 entities[3].entity_type = 2;
1152 entities[4].entity_type = 3;
1153 entities[5].entity_type = 4;
1154 entities[6].entity_type = 5;
1155 entities[7].entity_type = 5;
1156 entities[8].entity_type = 5;
1157 entities[9].entity_type = 6;
1158 entities[10].entity_type = 7;
1159
1160 auto tree = pldm_entity_association_tree_init();
1161
1162 auto l1 = pldm_entity_association_tree_add(
1163 tree, &entities[0], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1164 EXPECT_NE(l1, nullptr);
1165 auto l1a = pldm_entity_association_tree_add(
1166 tree, &entities[1], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1167 EXPECT_NE(l1a, nullptr);
1168
1169 auto l2a = pldm_entity_association_tree_add(
1170 tree, &entities[1], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1171 EXPECT_NE(l2a, nullptr);
1172 auto l2b = pldm_entity_association_tree_add(tree, &entities[2], 0xFFFF, l1,
1173 PLDM_ENTITY_ASSOCIAION_LOGICAL);
1174 EXPECT_NE(l2b, nullptr);
1175 auto l2c = pldm_entity_association_tree_add(
1176 tree, &entities[3], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1177 EXPECT_NE(l2c, nullptr);
1178 auto l2d = pldm_entity_association_tree_add(tree, &entities[4], 0xFFFF, l1,
1179 PLDM_ENTITY_ASSOCIAION_LOGICAL);
1180 EXPECT_NE(l2d, nullptr);
1181
1182 auto l3a = pldm_entity_association_tree_add(
1183 tree, &entities[5], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1184 EXPECT_NE(l3a, nullptr);
1185 auto l3b = pldm_entity_association_tree_add(
1186 tree, &entities[6], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1187 EXPECT_NE(l3b, nullptr);
1188 auto l3c = pldm_entity_association_tree_add(tree, &entities[7], 0xFFFF, l2a,
1189 PLDM_ENTITY_ASSOCIAION_LOGICAL);
1190 EXPECT_NE(l3c, nullptr);
1191 auto l3d = pldm_entity_association_tree_add(tree, &entities[8], 0xFFFF, l2a,
1192 PLDM_ENTITY_ASSOCIAION_LOGICAL);
1193 EXPECT_NE(l3d, nullptr);
1194
1195 auto l4a = pldm_entity_association_tree_add(
1196 tree, &entities[9], 0xFFFF, l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1197 EXPECT_NE(l4a, nullptr);
1198 auto l4b = pldm_entity_association_tree_add(
1199 tree, &entities[10], 0xFFFF, l3b, PLDM_ENTITY_ASSOCIAION_LOGICAL);
1200 EXPECT_NE(l4b, nullptr);
1201
1202 EXPECT_EQ(pldm_entity_get_num_children(l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL),
1203 2);
1204 EXPECT_EQ(pldm_entity_get_num_children(l1, PLDM_ENTITY_ASSOCIAION_LOGICAL),
1205 2);
1206 EXPECT_EQ(
1207 pldm_entity_get_num_children(l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL), 2);
1208 EXPECT_EQ(
1209 pldm_entity_get_num_children(l3b, PLDM_ENTITY_ASSOCIAION_PHYSICAL), 0);
1210 EXPECT_EQ(pldm_entity_get_num_children(l3b, PLDM_ENTITY_ASSOCIAION_LOGICAL),
1211 1);
1212
1213 auto repo = pldm_pdr_init();
Andrew Jeffery255f6542023-08-10 15:46:54 +09301214 int rc = pldm_entity_association_pdr_add_check(tree, repo, false, 1);
1215 ASSERT_EQ(rc, 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301216
1217 EXPECT_EQ(pldm_pdr_get_record_count(repo), 6u);
1218
1219 uint32_t currRecHandle{};
1220 uint32_t nextRecHandle{};
1221 uint8_t* data = nullptr;
1222 uint32_t size{};
1223 uint32_t commonSize = sizeof(struct pldm_pdr_hdr) + sizeof(uint16_t) +
1224 sizeof(uint8_t) + sizeof(pldm_entity) +
1225 sizeof(uint8_t);
1226
1227 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1228 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1229 l1, PLDM_ENTITY_ASSOCIAION_LOGICAL) *
1230 sizeof(pldm_entity)));
1231 uint8_t* start = data;
1232 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
1233 EXPECT_EQ(le32toh(hdr->record_handle), 1u);
1234 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
1235 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
1236 start += sizeof(pldm_pdr_hdr);
1237 uint16_t* containerId = reinterpret_cast<uint16_t*>(start);
1238 EXPECT_EQ(le16toh(*containerId), 1u);
1239 start += sizeof(uint16_t);
1240 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_LOGICAL);
1241 start += sizeof(uint8_t);
1242 pldm_entity* entity = reinterpret_cast<pldm_entity*>(start);
1243 EXPECT_EQ(le16toh(entity->entity_type), 1u);
1244 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1245 EXPECT_EQ(le16toh(entity->entity_container_id), 0u);
1246 start += sizeof(pldm_entity);
1247 EXPECT_EQ(*start,
1248 pldm_entity_get_num_children(l1, PLDM_ENTITY_ASSOCIAION_LOGICAL));
1249 start += sizeof(uint8_t);
1250 entity = reinterpret_cast<pldm_entity*>(start);
1251 EXPECT_EQ(le16toh(entity->entity_type), 3u);
1252 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1253 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
1254 start += sizeof(pldm_entity);
1255 entity = reinterpret_cast<pldm_entity*>(start);
1256 EXPECT_EQ(le16toh(entity->entity_type), 3u);
1257 EXPECT_EQ(le16toh(entity->entity_instance_num), 2u);
1258 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301259
1260 currRecHandle = nextRecHandle;
1261
1262 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1263 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1264 l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL) *
1265 sizeof(pldm_entity)));
1266 start = data;
1267 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
1268 EXPECT_EQ(le32toh(hdr->record_handle), 2u);
1269 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
1270 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
1271 start += sizeof(pldm_pdr_hdr);
1272 containerId = reinterpret_cast<uint16_t*>(start);
1273 EXPECT_EQ(le16toh(*containerId), 1u);
1274 start += sizeof(uint16_t);
1275 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1276 start += sizeof(uint8_t);
1277 entity = reinterpret_cast<pldm_entity*>(start);
1278 EXPECT_EQ(le16toh(entity->entity_type), 1u);
1279 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1280 EXPECT_EQ(le16toh(entity->entity_container_id), 0u);
1281 start += sizeof(pldm_entity);
1282 EXPECT_EQ(*start, pldm_entity_get_num_children(
1283 l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL));
1284 start += sizeof(uint8_t);
1285 entity = reinterpret_cast<pldm_entity*>(start);
1286 EXPECT_EQ(le16toh(entity->entity_type), 2u);
1287 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1288 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
1289 start += sizeof(pldm_entity);
1290 entity = reinterpret_cast<pldm_entity*>(start);
1291 EXPECT_EQ(le16toh(entity->entity_type), 2u);
1292 EXPECT_EQ(le16toh(entity->entity_instance_num), 2u);
1293 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301294
1295 currRecHandle = nextRecHandle;
1296
1297 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1298 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1299 l2a, PLDM_ENTITY_ASSOCIAION_LOGICAL) *
1300 sizeof(pldm_entity)));
1301 start = data;
1302 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
1303 EXPECT_EQ(le32toh(hdr->record_handle), 3u);
1304 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
1305 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
1306 start += sizeof(pldm_pdr_hdr);
1307 containerId = reinterpret_cast<uint16_t*>(start);
1308 EXPECT_EQ(le16toh(*containerId), 2u);
1309 start += sizeof(uint16_t);
1310 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_LOGICAL);
1311 start += sizeof(uint8_t);
1312 entity = reinterpret_cast<pldm_entity*>(start);
1313 EXPECT_EQ(le16toh(entity->entity_type), 2u);
1314 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1315 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
1316 start += sizeof(pldm_entity);
1317 EXPECT_EQ(*start, pldm_entity_get_num_children(
1318 l2a, PLDM_ENTITY_ASSOCIAION_LOGICAL));
1319 start += sizeof(uint8_t);
1320 entity = reinterpret_cast<pldm_entity*>(start);
1321 EXPECT_EQ(le16toh(entity->entity_type), 5);
1322 EXPECT_EQ(le16toh(entity->entity_instance_num), 2u);
1323 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
1324 start += sizeof(pldm_entity);
1325 entity = reinterpret_cast<pldm_entity*>(start);
1326 EXPECT_EQ(le16toh(entity->entity_type), 5u);
1327 EXPECT_EQ(le16toh(entity->entity_instance_num), 3u);
1328 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301329
1330 currRecHandle = nextRecHandle;
1331
1332 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1333 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1334 l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL) *
1335 sizeof(pldm_entity)));
1336 start = data;
1337 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
1338 EXPECT_EQ(le32toh(hdr->record_handle), 4u);
1339 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
1340 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
1341 start += sizeof(pldm_pdr_hdr);
1342 containerId = reinterpret_cast<uint16_t*>(start);
1343 EXPECT_EQ(le16toh(*containerId), 2u);
1344 start += sizeof(uint16_t);
1345 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1346 start += sizeof(uint8_t);
1347 entity = reinterpret_cast<pldm_entity*>(start);
1348 EXPECT_EQ(le16toh(entity->entity_type), 2u);
1349 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1350 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
1351 start += sizeof(pldm_entity);
1352 EXPECT_EQ(*start, pldm_entity_get_num_children(
1353 l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL));
1354 start += sizeof(uint8_t);
1355 entity = reinterpret_cast<pldm_entity*>(start);
1356 EXPECT_EQ(le16toh(entity->entity_type), 4u);
1357 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1358 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
1359 start += sizeof(pldm_entity);
1360 entity = reinterpret_cast<pldm_entity*>(start);
1361 EXPECT_EQ(le16toh(entity->entity_type), 5u);
1362 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1363 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301364
1365 currRecHandle = nextRecHandle;
1366
1367 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1368 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1369 l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL) *
1370 sizeof(pldm_entity)));
1371 start = data;
1372 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
1373 EXPECT_EQ(le32toh(hdr->record_handle), 5u);
1374 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
1375 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
1376 start += sizeof(pldm_pdr_hdr);
1377 containerId = reinterpret_cast<uint16_t*>(start);
1378 EXPECT_EQ(le16toh(*containerId), 3u);
1379 start += sizeof(uint16_t);
1380 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1381 start += sizeof(uint8_t);
1382 entity = reinterpret_cast<pldm_entity*>(start);
1383 EXPECT_EQ(le16toh(entity->entity_type), 4u);
1384 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1385 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
1386 start += sizeof(pldm_entity);
1387 EXPECT_EQ(*start, pldm_entity_get_num_children(
1388 l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL));
1389 start += sizeof(uint8_t);
1390 entity = reinterpret_cast<pldm_entity*>(start);
1391 EXPECT_EQ(le16toh(entity->entity_type), 6u);
1392 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1393 EXPECT_EQ(le16toh(entity->entity_container_id), 3u);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301394
1395 currRecHandle = nextRecHandle;
1396
1397 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1398 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1399 l3b, PLDM_ENTITY_ASSOCIAION_LOGICAL) *
1400 sizeof(pldm_entity)));
1401 start = data;
1402 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
1403 EXPECT_EQ(le32toh(hdr->record_handle), 6u);
1404 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
1405 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
1406 start += sizeof(pldm_pdr_hdr);
1407 containerId = reinterpret_cast<uint16_t*>(start);
1408 EXPECT_EQ(le16toh(*containerId), 4u);
1409 start += sizeof(uint16_t);
1410 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_LOGICAL);
1411 start += sizeof(uint8_t);
1412 entity = reinterpret_cast<pldm_entity*>(start);
1413 EXPECT_EQ(le16toh(entity->entity_type), 5u);
1414 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1415 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
1416 start += sizeof(pldm_entity);
1417 EXPECT_EQ(*start, pldm_entity_get_num_children(
1418 l3b, PLDM_ENTITY_ASSOCIAION_LOGICAL));
1419 start += sizeof(uint8_t);
1420 entity = reinterpret_cast<pldm_entity*>(start);
1421 EXPECT_EQ(le16toh(entity->entity_type), 7u);
1422 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1423 EXPECT_EQ(le16toh(entity->entity_container_id), 4u);
1424
1425 EXPECT_EQ(nextRecHandle, 0u);
1426
1427 pldm_pdr_destroy(repo);
1428 pldm_entity_association_tree_destroy(tree);
1429}
1430
1431TEST(EntityAssociationPDR, testFind)
1432{
1433 // 1
1434 // |
1435 // 2--3--4
1436 // |
1437 // 5--6--7
1438 // | |
1439 // 8 9
1440
1441 pldm_entity entities[9]{};
1442
1443 entities[0].entity_type = 1;
1444 entities[1].entity_type = 2;
1445 entities[2].entity_type = 2;
1446 entities[3].entity_type = 3;
1447 entities[4].entity_type = 4;
1448 entities[5].entity_type = 5;
1449 entities[6].entity_type = 5;
1450 entities[7].entity_type = 6;
1451 entities[8].entity_type = 7;
1452
1453 auto tree = pldm_entity_association_tree_init();
1454
1455 auto l1 = pldm_entity_association_tree_add(
1456 tree, &entities[0], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1457 EXPECT_NE(l1, nullptr);
1458 auto l2a = pldm_entity_association_tree_add(
1459 tree, &entities[1], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1460 EXPECT_NE(l2a, nullptr);
1461 auto l2b = pldm_entity_association_tree_add(
1462 tree, &entities[2], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1463 EXPECT_NE(l2b, nullptr);
1464 auto l2c = pldm_entity_association_tree_add(
1465 tree, &entities[3], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1466 EXPECT_NE(l2c, nullptr);
1467 auto l3a = pldm_entity_association_tree_add(
1468 tree, &entities[4], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1469 EXPECT_NE(l3a, nullptr);
1470 auto l3b = pldm_entity_association_tree_add(
1471 tree, &entities[5], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1472 EXPECT_NE(l3b, nullptr);
1473 auto l3c = pldm_entity_association_tree_add(
1474 tree, &entities[6], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1475 EXPECT_NE(l3c, nullptr);
1476 auto l4a = pldm_entity_association_tree_add(
1477 tree, &entities[7], 0xFFFF, l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1478 EXPECT_NE(l4a, nullptr);
1479 auto l4b = pldm_entity_association_tree_add(
1480 tree, &entities[8], 0xFFFF, l3b, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1481 EXPECT_NE(l4b, nullptr);
1482
1483 pldm_entity entity{};
1484
1485 entity.entity_type = 1;
1486 entity.entity_instance_num = 1;
1487 auto result = pldm_entity_association_tree_find(tree, &entity);
1488 EXPECT_EQ(result, l1);
1489 EXPECT_EQ(entity.entity_container_id, 0);
1490
1491 entity.entity_type = 2;
1492 entity.entity_instance_num = 1;
1493 result = pldm_entity_association_tree_find(tree, &entity);
1494 EXPECT_EQ(result, l2a);
1495 EXPECT_EQ(entity.entity_container_id, 1);
1496 entity.entity_type = 2;
1497 entity.entity_instance_num = 2;
1498 result = pldm_entity_association_tree_find(tree, &entity);
1499 EXPECT_EQ(result, l2b);
1500 EXPECT_EQ(entity.entity_container_id, 1);
1501 entity.entity_type = 3;
1502 entity.entity_instance_num = 1;
1503 result = pldm_entity_association_tree_find(tree, &entity);
1504 EXPECT_EQ(result, l2c);
1505 EXPECT_EQ(entity.entity_container_id, 1);
1506
1507 entity.entity_type = 7;
1508 entity.entity_instance_num = 1;
1509 result = pldm_entity_association_tree_find(tree, &entity);
1510 EXPECT_EQ(result, l4b);
1511 EXPECT_EQ(entity.entity_container_id, 4);
1512
1513 pldm_entity_association_tree_destroy(tree);
1514}
1515
1516TEST(EntityAssociationPDR, testCopyTree)
1517{
1518 pldm_entity entities[4]{};
1519 entities[0].entity_type = 1;
1520 entities[1].entity_type = 2;
1521 entities[2].entity_type = 2;
1522 entities[3].entity_type = 3;
1523
1524 auto orgTree = pldm_entity_association_tree_init();
1525 auto newTree = pldm_entity_association_tree_init();
1526 auto l1 =
1527 pldm_entity_association_tree_add(orgTree, &entities[0], 0xFFFF, nullptr,
1528 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1529 EXPECT_NE(l1, nullptr);
1530 auto l2a = pldm_entity_association_tree_add(
1531 orgTree, &entities[1], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1532 EXPECT_NE(l2a, nullptr);
1533 auto l2b = pldm_entity_association_tree_add(
1534 orgTree, &entities[2], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1535 EXPECT_NE(l2b, nullptr);
1536 auto l2c = pldm_entity_association_tree_add(
1537 orgTree, &entities[3], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1538 EXPECT_NE(l2c, nullptr);
1539 size_t orgNum{};
1540 pldm_entity* orgOut = nullptr;
1541 pldm_entity_association_tree_visit(orgTree, &orgOut, &orgNum);
1542 EXPECT_EQ(orgNum, 4u);
1543
1544 pldm_entity_association_tree_copy_root(orgTree, newTree);
1545 size_t newNum{};
1546 pldm_entity* newOut = nullptr;
1547 pldm_entity_association_tree_visit(newTree, &newOut, &newNum);
1548 EXPECT_EQ(newNum, orgNum);
1549 EXPECT_EQ(newOut[0].entity_type, 1u);
1550 EXPECT_EQ(newOut[0].entity_instance_num, 1u);
1551 EXPECT_EQ(newOut[0].entity_container_id, 0u);
1552 free(orgOut);
1553 free(newOut);
1554 pldm_entity_association_tree_destroy(orgTree);
1555 pldm_entity_association_tree_destroy(newTree);
1556}
1557
1558TEST(EntityAssociationPDR, testExtract)
1559{
1560 std::vector<uint8_t> pdr{};
1561 pdr.resize(sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_entity_association) +
1562 sizeof(pldm_entity) * 4);
1563 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(pdr.data());
1564 hdr->type = PLDM_PDR_ENTITY_ASSOCIATION;
1565 hdr->length =
1566 htole16(sizeof(pldm_pdr_entity_association) + sizeof(pldm_entity) * 4);
1567
1568 pldm_pdr_entity_association* e =
1569 reinterpret_cast<pldm_pdr_entity_association*>(pdr.data() +
1570 sizeof(pldm_pdr_hdr));
1571 e->container_id = htole16(1);
1572 e->num_children = 5;
1573 e->container.entity_type = htole16(1);
1574 e->container.entity_instance_num = htole16(1);
1575 e->container.entity_container_id = htole16(0);
1576
1577 pldm_entity* entity = e->children;
1578 entity->entity_type = htole16(2);
1579 entity->entity_instance_num = htole16(1);
1580 entity->entity_container_id = htole16(1);
1581 ++entity;
1582 entity->entity_type = htole16(3);
1583 entity->entity_instance_num = htole16(1);
1584 entity->entity_container_id = htole16(1);
1585 ++entity;
1586 entity->entity_type = htole16(4);
1587 entity->entity_instance_num = htole16(1);
1588 entity->entity_container_id = htole16(1);
1589 ++entity;
1590 entity->entity_type = htole16(5);
1591 entity->entity_instance_num = htole16(1);
1592 entity->entity_container_id = htole16(1);
1593 ++entity;
1594 entity->entity_type = htole16(6);
1595 entity->entity_instance_num = htole16(1);
1596 entity->entity_container_id = htole16(1);
1597
1598 size_t num{};
1599 pldm_entity* out = nullptr;
1600 pldm_entity_association_pdr_extract(pdr.data(), pdr.size(), &num, &out);
1601 EXPECT_EQ(num, (unsigned)e->num_children + 1);
1602 EXPECT_EQ(out[0].entity_type, 1u);
1603 EXPECT_EQ(out[0].entity_instance_num, 1u);
1604 EXPECT_EQ(out[0].entity_container_id, 0u);
1605 EXPECT_EQ(out[1].entity_type, 2u);
1606 EXPECT_EQ(out[1].entity_instance_num, 1u);
1607 EXPECT_EQ(out[1].entity_container_id, 1u);
1608 EXPECT_EQ(out[2].entity_type, 3u);
1609 EXPECT_EQ(out[2].entity_instance_num, 1u);
1610 EXPECT_EQ(out[2].entity_container_id, 1u);
1611 EXPECT_EQ(out[3].entity_type, 4u);
1612 EXPECT_EQ(out[3].entity_instance_num, 1u);
1613 EXPECT_EQ(out[3].entity_container_id, 1u);
1614 EXPECT_EQ(out[4].entity_type, 5u);
1615 EXPECT_EQ(out[4].entity_instance_num, 1u);
1616 EXPECT_EQ(out[4].entity_container_id, 1u);
1617 EXPECT_EQ(out[5].entity_type, 6u);
1618 EXPECT_EQ(out[5].entity_instance_num, 1u);
1619 EXPECT_EQ(out[5].entity_container_id, 1u);
1620
1621 free(out);
1622}
1623
1624TEST(EntityAssociationPDR, testGetChildren)
1625{
1626 pldm_entity entities[4]{};
1627 entities[0].entity_type = 1;
1628 entities[1].entity_type = 2;
1629 entities[2].entity_type = 2;
1630 entities[3].entity_type = 3;
1631
1632 auto tree = pldm_entity_association_tree_init();
1633 auto l1 = pldm_entity_association_tree_add(
1634 tree, &entities[0], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1635 EXPECT_NE(l1, nullptr);
1636 auto l2a = pldm_entity_association_tree_add(
1637 tree, &entities[1], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1638 EXPECT_NE(l2a, nullptr);
1639 auto l2b = pldm_entity_association_tree_add(
1640 tree, &entities[2], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1641 EXPECT_NE(l2b, nullptr);
1642 auto l2c = pldm_entity_association_tree_add(
1643 tree, &entities[3], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1644 EXPECT_NE(l2c, nullptr);
1645
1646 pldm_entity et1;
1647 et1.entity_type = 2;
1648 et1.entity_instance_num = 1;
1649 EXPECT_EQ(true, pldm_is_current_parent_child(l1, &et1));
1650
1651 pldm_entity et2;
1652 et2.entity_type = 2;
1653 et2.entity_instance_num = 2;
1654 EXPECT_EQ(true, pldm_is_current_parent_child(l1, &et2));
1655
1656 pldm_entity et3;
1657 et3.entity_type = 2;
1658 et3.entity_instance_num = 3;
1659 EXPECT_EQ(false, pldm_is_current_parent_child(l1, &et3));
1660
1661 pldm_entity_association_tree_destroy(tree);
1662}
1663
1664TEST(EntityAssociationPDR, testEntityInstanceNumber)
1665{
1666 pldm_entity entities[9]{};
1667
1668 entities[0].entity_type = 1;
1669 entities[1].entity_type = 2;
1670 entities[2].entity_type = 2;
1671 entities[3].entity_type = 2;
1672 entities[4].entity_type = 2;
1673 entities[5].entity_type = 2;
1674 entities[6].entity_type = 2;
1675 entities[7].entity_type = 3;
1676 entities[8].entity_type = 3;
1677
1678 auto tree = pldm_entity_association_tree_init();
1679 auto repo = pldm_pdr_init();
1680
1681 uint16_t terminusHdl{};
1682 uint16_t entityType{};
1683 uint16_t entityInstanceNum{};
1684 uint16_t containerId{};
1685
1686 auto node = pldm_entity_association_tree_add(
1687 tree, &entities[0], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1688 EXPECT_NE(node, nullptr);
1689
1690 auto l1 = pldm_entity_association_tree_add(tree, &entities[1], 63, node,
1691 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery14d29842023-07-17 15:29:32 +09301692 uint32_t first = 1;
1693 EXPECT_EQ(pldm_pdr_add_fru_record_set_check(
1694 repo, 1, 1, entities[1].entity_type,
1695 entities[1].entity_instance_num,
1696 entities[1].entity_container_id, &first),
1697 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301698 EXPECT_NE(l1, nullptr);
1699 EXPECT_EQ(entities[1].entity_instance_num, 63);
1700 EXPECT_EQ(first, pldm_pdr_get_record_handle(
1701 repo, pldm_pdr_fru_record_set_find_by_rsi(
1702 repo, 1, &terminusHdl, &entityType,
1703 &entityInstanceNum, &containerId)));
1704 EXPECT_EQ(entityType, 2);
1705 EXPECT_EQ(entityInstanceNum, 63);
1706
1707 auto l2 = pldm_entity_association_tree_add(tree, &entities[2], 37, node,
1708 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery14d29842023-07-17 15:29:32 +09301709 uint32_t second = 2;
1710 EXPECT_EQ(pldm_pdr_add_fru_record_set_check(
1711 repo, 1, 2, entities[2].entity_type,
1712 entities[2].entity_instance_num,
1713 entities[2].entity_container_id, &second),
1714 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301715 EXPECT_NE(l2, nullptr);
1716 EXPECT_EQ(entities[2].entity_instance_num, 37);
1717 EXPECT_EQ(second, pldm_pdr_get_record_handle(
1718 repo, pldm_pdr_fru_record_set_find_by_rsi(
1719 repo, 2, &terminusHdl, &entityType,
1720 &entityInstanceNum, &containerId)));
1721 EXPECT_EQ(entityType, 2);
1722 EXPECT_EQ(entityInstanceNum, 37);
1723
1724 auto l3 = pldm_entity_association_tree_add(tree, &entities[3], 44, node,
1725 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery14d29842023-07-17 15:29:32 +09301726 uint32_t third = 3;
1727 EXPECT_EQ(pldm_pdr_add_fru_record_set_check(
1728 repo, 1, 3, entities[3].entity_type,
1729 entities[3].entity_instance_num,
1730 entities[3].entity_container_id, &third),
1731 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301732 EXPECT_NE(l3, nullptr);
1733 EXPECT_EQ(entities[3].entity_instance_num, 44);
1734 EXPECT_EQ(third, pldm_pdr_get_record_handle(
1735 repo, pldm_pdr_fru_record_set_find_by_rsi(
1736 repo, 3, &terminusHdl, &entityType,
1737 &entityInstanceNum, &containerId)));
1738 EXPECT_EQ(entityType, 2);
1739 EXPECT_EQ(entityInstanceNum, 44);
1740
1741 auto l4 = pldm_entity_association_tree_add(tree, &entities[4], 89, node,
1742 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery14d29842023-07-17 15:29:32 +09301743 uint32_t fourth = 4;
1744 EXPECT_EQ(pldm_pdr_add_fru_record_set_check(
1745 repo, 1, 4, entities[4].entity_type,
1746 entities[4].entity_instance_num,
1747 entities[4].entity_container_id, &fourth),
1748 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301749 EXPECT_NE(l4, nullptr);
1750 EXPECT_EQ(entities[4].entity_instance_num, 89);
1751 EXPECT_EQ(fourth, pldm_pdr_get_record_handle(
1752 repo, pldm_pdr_fru_record_set_find_by_rsi(
1753 repo, 4, &terminusHdl, &entityType,
1754 &entityInstanceNum, &containerId)));
1755 EXPECT_EQ(entityType, 2);
1756 EXPECT_EQ(entityInstanceNum, 89);
1757
1758 auto l5 = pldm_entity_association_tree_add(tree, &entities[5], 0xFFFF, node,
1759 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery14d29842023-07-17 15:29:32 +09301760 uint32_t fifth = 5;
1761 EXPECT_EQ(pldm_pdr_add_fru_record_set_check(
1762 repo, 1, 5, entities[5].entity_type,
1763 entities[5].entity_instance_num,
1764 entities[5].entity_container_id, &fifth),
1765 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301766 EXPECT_NE(l5, nullptr);
1767 EXPECT_EQ(entities[5].entity_instance_num, 90);
1768 EXPECT_EQ(fifth, pldm_pdr_get_record_handle(
1769 repo, pldm_pdr_fru_record_set_find_by_rsi(
1770 repo, 5, &terminusHdl, &entityType,
1771 &entityInstanceNum, &containerId)));
1772 EXPECT_EQ(entityType, 2);
1773 EXPECT_EQ(entityInstanceNum, 90);
1774
1775 auto l6 = pldm_entity_association_tree_add(tree, &entities[6], 90, node,
1776 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1777 EXPECT_EQ(l6, nullptr);
1778
1779 auto l7 = pldm_entity_association_tree_add(tree, &entities[7], 100, l1,
1780 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery14d29842023-07-17 15:29:32 +09301781 uint32_t seventh = 7;
1782 EXPECT_EQ(pldm_pdr_add_fru_record_set_check(
1783 repo, 1, 7, entities[7].entity_type,
1784 entities[7].entity_instance_num,
1785 entities[7].entity_container_id, &seventh),
1786 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301787 EXPECT_NE(l7, nullptr);
1788 EXPECT_EQ(entities[7].entity_instance_num, 100);
1789 EXPECT_EQ(seventh, pldm_pdr_get_record_handle(
1790 repo, pldm_pdr_fru_record_set_find_by_rsi(
1791 repo, 7, &terminusHdl, &entityType,
1792 &entityInstanceNum, &containerId)));
1793 EXPECT_EQ(entityType, 3);
1794 EXPECT_EQ(entityInstanceNum, 100);
1795
1796 auto l8 = pldm_entity_association_tree_add(tree, &entities[8], 100, l2,
1797 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery14d29842023-07-17 15:29:32 +09301798 uint32_t eighth = 8;
1799 EXPECT_EQ(pldm_pdr_add_fru_record_set_check(
1800 repo, 1, 8, entities[8].entity_type,
1801 entities[8].entity_instance_num,
1802 entities[8].entity_container_id, &eighth),
1803 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301804 EXPECT_NE(l8, nullptr);
1805 EXPECT_EQ(entities[8].entity_instance_num, 100);
1806 EXPECT_EQ(eighth, pldm_pdr_get_record_handle(
1807 repo, pldm_pdr_fru_record_set_find_by_rsi(
1808 repo, 8, &terminusHdl, &entityType,
1809 &entityInstanceNum, &containerId)));
1810 EXPECT_EQ(entityType, 3);
1811 EXPECT_EQ(entityInstanceNum, 100);
1812
1813 pldm_pdr_destroy(repo);
1814 pldm_entity_association_tree_destroy(tree);
1815}
Pavithra Barithaya5dc02572023-05-19 09:24:36 -05001816
Andrew Jeffery14a5fe82023-06-26 15:31:37 +09301817#ifdef LIBPLDM_API_TESTING
Pavithra Barithaya8cf70452023-06-22 04:36:19 -05001818TEST(EntityAssociationPDR, testFindChildContainerID)
Pavithra Barithaya5dc02572023-05-19 09:24:36 -05001819{
1820 pldm_entity entities[3]{};
1821 entities[0].entity_type = 1;
1822 entities[1].entity_type = 2;
1823 entities[2].entity_type = 3;
1824 entities[1].entity_container_id = 2;
1825 entities[1].entity_instance_num = 1;
1826
1827 auto tree = pldm_entity_association_tree_init();
1828 auto l1 = pldm_entity_association_tree_add_entity(
1829 tree, &entities[0], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_LOGICAL,
1830 false, true, 0xFFFF);
1831
1832 EXPECT_NE(l1, nullptr);
1833 auto l2 = pldm_entity_association_tree_add_entity(
1834 tree, &entities[1], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
1835 false, 0xFFFF);
1836 EXPECT_NE(l2, nullptr);
1837 auto l3 = pldm_entity_association_tree_add_entity(
1838 tree, &entities[2], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL, false,
1839 true, 0xFFFF);
1840 EXPECT_NE(l3, nullptr);
1841
1842 EXPECT_EQ(pldm_entity_get_num_children(l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL),
1843 2);
1844
1845 auto repo = pldm_pdr_init();
Andrew Jeffery255f6542023-08-10 15:46:54 +09301846 int rc = pldm_entity_association_pdr_add_check(tree, repo, false, 1);
1847 ASSERT_EQ(rc, 0);
Pavithra Barithaya5dc02572023-05-19 09:24:36 -05001848
1849 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
1850
1851 uint16_t container_id{};
Pavithra Barithaya8cf70452023-06-22 04:36:19 -05001852 pldm_pdr_find_child_container_id_index_range_exclude(
1853 repo, 1, 1, 0, 0x01000000, 0x01FFFFFF, &container_id);
Pavithra Barithaya5dc02572023-05-19 09:24:36 -05001854 EXPECT_EQ(container_id, 2);
1855
1856 uint16_t container_id1{};
Pavithra Barithaya8cf70452023-06-22 04:36:19 -05001857 pldm_pdr_find_child_container_id_index_range_exclude(
1858 repo, 1, 1, 0, 0x00000001, 0x00FFFFFF, &container_id1);
Pavithra Barithaya5dc02572023-05-19 09:24:36 -05001859 EXPECT_EQ(container_id1, 0);
1860
1861 pldm_pdr_destroy(repo);
1862 pldm_entity_association_tree_destroy(tree);
1863}
Andrew Jeffery14a5fe82023-06-26 15:31:37 +09301864#endif