blob: d9083455aba5f1b8113308b7bacbbde2a566ed93 [file] [log] [blame]
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -06001#include <array>
2
3#include "libpldm/pdr.h"
4#include "libpldm/platform.h"
5
6#include <gtest/gtest.h>
7
8TEST(PDRAccess, testInit)
9{
10 auto repo = pldm_pdr_init();
crgeddes2550cee2020-05-05 17:51:28 +000011 EXPECT_EQ(pldm_pdr_get_record_count(repo), 0u);
12 EXPECT_EQ(pldm_pdr_get_repo_size(repo), 0u);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -060013 pldm_pdr_destroy(repo);
14}
15
16TEST(PDRUpdate, testAdd)
17{
18 auto repo = pldm_pdr_init();
19
20 std::array<uint8_t, 10> data{};
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050021 auto handle = pldm_pdr_add(repo, data.data(), data.size(), 0, false);
crgeddes2550cee2020-05-05 17:51:28 +000022 EXPECT_EQ(handle, 1u);
23 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -060024 EXPECT_EQ(pldm_pdr_get_repo_size(repo), data.size());
25
crgeddes2550cee2020-05-05 17:51:28 +000026 handle = pldm_pdr_add(repo, data.data(), data.size(), 0u, false);
27 EXPECT_EQ(handle, 2u);
28 handle = pldm_pdr_add(repo, data.data(), data.size(), 0u, false);
29 EXPECT_EQ(handle, 3u);
30 handle = pldm_pdr_add(repo, data.data(), data.size(), htole32(0xdeeddeedu),
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050031 false);
Lei YU90ff3ed2020-03-02 11:16:51 +080032 EXPECT_EQ(handle, htole32(0xdeeddeed));
crgeddes2550cee2020-05-05 17:51:28 +000033 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
34 EXPECT_EQ(pldm_pdr_get_repo_size(repo), data.size() * 4u);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -060035
36 pldm_pdr_destroy(repo);
37}
38
Deepak Kodihalli4a680932020-04-22 03:31:17 -050039TEST(PDRUpdate, testRemove)
40{
41 std::array<uint8_t, 10> data{};
42
43 auto repo = pldm_pdr_init();
44 pldm_pdr_remove_remote_pdrs(repo);
crgeddes2550cee2020-05-05 17:51:28 +000045 EXPECT_EQ(pldm_pdr_get_record_count(repo), 0u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -050046 pldm_pdr_destroy(repo);
47
48 repo = pldm_pdr_init();
49 pldm_pdr_add(repo, data.data(), data.size(), 0, true);
50 pldm_pdr_remove_remote_pdrs(repo);
crgeddes2550cee2020-05-05 17:51:28 +000051 EXPECT_EQ(pldm_pdr_get_record_count(repo), 0u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -050052 pldm_pdr_destroy(repo);
53
54 repo = pldm_pdr_init();
55 pldm_pdr_add(repo, data.data(), data.size(), 0, false);
56 pldm_pdr_remove_remote_pdrs(repo);
crgeddes2550cee2020-05-05 17:51:28 +000057 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -050058 pldm_pdr_destroy(repo);
59
60 repo = pldm_pdr_init();
61 pldm_pdr_add(repo, data.data(), data.size(), 0, false);
62 pldm_pdr_add(repo, data.data(), data.size(), 0, true);
63 pldm_pdr_remove_remote_pdrs(repo);
crgeddes2550cee2020-05-05 17:51:28 +000064 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -050065 pldm_pdr_destroy(repo);
66
67 repo = pldm_pdr_init();
68 pldm_pdr_add(repo, data.data(), data.size(), 0, true);
69 pldm_pdr_add(repo, data.data(), data.size(), 0, false);
70 pldm_pdr_remove_remote_pdrs(repo);
crgeddes2550cee2020-05-05 17:51:28 +000071 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -050072 pldm_pdr_destroy(repo);
73
74 repo = pldm_pdr_init();
75 pldm_pdr_add(repo, data.data(), data.size(), 0, true);
76 pldm_pdr_add(repo, data.data(), data.size(), 0, true);
77 pldm_pdr_add(repo, data.data(), data.size(), 0, true);
78 pldm_pdr_add(repo, data.data(), data.size(), 0, true);
79 pldm_pdr_remove_remote_pdrs(repo);
crgeddes2550cee2020-05-05 17:51:28 +000080 EXPECT_EQ(pldm_pdr_get_record_count(repo), 0u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -050081 pldm_pdr_add(repo, data.data(), data.size(), 0, true);
82 pldm_pdr_add(repo, data.data(), data.size(), 0, false);
crgeddes2550cee2020-05-05 17:51:28 +000083 EXPECT_EQ(pldm_pdr_get_record_count(repo), 2u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -050084 pldm_pdr_destroy(repo);
85
86 repo = pldm_pdr_init();
87 pldm_pdr_add(repo, data.data(), data.size(), 0, false);
88 pldm_pdr_add(repo, data.data(), data.size(), 0, false);
89 pldm_pdr_add(repo, data.data(), data.size(), 0, false);
90 pldm_pdr_add(repo, data.data(), data.size(), 0, false);
91 pldm_pdr_remove_remote_pdrs(repo);
crgeddes2550cee2020-05-05 17:51:28 +000092 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -050093 pldm_pdr_add(repo, data.data(), data.size(), 0, true);
94 pldm_pdr_add(repo, data.data(), data.size(), 0, false);
crgeddes2550cee2020-05-05 17:51:28 +000095 EXPECT_EQ(pldm_pdr_get_record_count(repo), 6u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -050096 pldm_pdr_destroy(repo);
97
98 repo = pldm_pdr_init();
99 pldm_pdr_add(repo, data.data(), data.size(), 0, true);
100 pldm_pdr_add(repo, data.data(), data.size(), 0, false);
101 pldm_pdr_add(repo, data.data(), data.size(), 0, false);
102 pldm_pdr_add(repo, data.data(), data.size(), 0, false);
103 pldm_pdr_remove_remote_pdrs(repo);
crgeddes2550cee2020-05-05 17:51:28 +0000104 EXPECT_EQ(pldm_pdr_get_record_count(repo), 3u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500105 pldm_pdr_add(repo, data.data(), data.size(), 0, true);
106 pldm_pdr_add(repo, data.data(), data.size(), 0, false);
crgeddes2550cee2020-05-05 17:51:28 +0000107 EXPECT_EQ(pldm_pdr_get_record_count(repo), 5u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500108 pldm_pdr_destroy(repo);
109
110 repo = pldm_pdr_init();
111 pldm_pdr_add(repo, data.data(), data.size(), 0, false);
112 pldm_pdr_add(repo, data.data(), data.size(), 0, false);
113 pldm_pdr_add(repo, data.data(), data.size(), 0, false);
114 pldm_pdr_add(repo, data.data(), data.size(), 0, true);
115 pldm_pdr_remove_remote_pdrs(repo);
crgeddes2550cee2020-05-05 17:51:28 +0000116 EXPECT_EQ(pldm_pdr_get_record_count(repo), 3u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500117 pldm_pdr_add(repo, data.data(), data.size(), 0, true);
118 pldm_pdr_add(repo, data.data(), data.size(), 0, false);
crgeddes2550cee2020-05-05 17:51:28 +0000119 EXPECT_EQ(pldm_pdr_get_record_count(repo), 5u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500120 pldm_pdr_destroy(repo);
121
122 repo = pldm_pdr_init();
123 pldm_pdr_add(repo, data.data(), data.size(), 0, true);
124 pldm_pdr_add(repo, data.data(), data.size(), 0, false);
125 pldm_pdr_add(repo, data.data(), data.size(), 0, true);
126 pldm_pdr_add(repo, data.data(), data.size(), 0, false);
127 pldm_pdr_remove_remote_pdrs(repo);
crgeddes2550cee2020-05-05 17:51:28 +0000128 EXPECT_EQ(pldm_pdr_get_record_count(repo), 2u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500129 pldm_pdr_add(repo, data.data(), data.size(), 0, false);
130 pldm_pdr_add(repo, data.data(), data.size(), 0, true);
crgeddes2550cee2020-05-05 17:51:28 +0000131 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500132 pldm_pdr_destroy(repo);
133
134 repo = pldm_pdr_init();
135 pldm_pdr_add(repo, data.data(), data.size(), 0, false);
136 pldm_pdr_add(repo, data.data(), data.size(), 0, false);
137 pldm_pdr_add(repo, data.data(), data.size(), 0, false);
138 pldm_pdr_add(repo, data.data(), data.size(), 0, true);
139 pldm_pdr_remove_remote_pdrs(repo);
crgeddes2550cee2020-05-05 17:51:28 +0000140 EXPECT_EQ(pldm_pdr_get_record_count(repo), 3u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500141 pldm_pdr_add(repo, data.data(), data.size(), 0, false);
142 pldm_pdr_add(repo, data.data(), data.size(), 0, true);
crgeddes2550cee2020-05-05 17:51:28 +0000143 EXPECT_EQ(pldm_pdr_get_record_count(repo), 5u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500144 pldm_pdr_destroy(repo);
145
146 repo = pldm_pdr_init();
147 pldm_pdr_add(repo, data.data(), data.size(), 0, false);
148 pldm_pdr_add(repo, data.data(), data.size(), 0, true);
149 pldm_pdr_add(repo, data.data(), data.size(), 0, false);
150 pldm_pdr_add(repo, data.data(), data.size(), 0, true);
151 pldm_pdr_remove_remote_pdrs(repo);
crgeddes2550cee2020-05-05 17:51:28 +0000152 EXPECT_EQ(pldm_pdr_get_record_count(repo), 2u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500153 pldm_pdr_add(repo, data.data(), data.size(), 0, false);
154 pldm_pdr_add(repo, data.data(), data.size(), 0, true);
crgeddes2550cee2020-05-05 17:51:28 +0000155 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500156 pldm_pdr_destroy(repo);
157
158 repo = pldm_pdr_init();
159 pldm_pdr_add(repo, data.data(), data.size(), 0, true);
160 pldm_pdr_add(repo, data.data(), data.size(), 0, true);
161 pldm_pdr_add(repo, data.data(), data.size(), 0, true);
162 pldm_pdr_add(repo, data.data(), data.size(), 0, false);
163 pldm_pdr_remove_remote_pdrs(repo);
crgeddes2550cee2020-05-05 17:51:28 +0000164 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500165 pldm_pdr_add(repo, data.data(), data.size(), 0, false);
166 pldm_pdr_add(repo, data.data(), data.size(), 0, true);
crgeddes2550cee2020-05-05 17:51:28 +0000167 EXPECT_EQ(pldm_pdr_get_record_count(repo), 3u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500168 pldm_pdr_destroy(repo);
169
170 repo = pldm_pdr_init();
171 pldm_pdr_add(repo, data.data(), data.size(), 0, false);
172 pldm_pdr_add(repo, data.data(), data.size(), 0, true);
173 pldm_pdr_add(repo, data.data(), data.size(), 0, true);
174 pldm_pdr_add(repo, data.data(), data.size(), 0, true);
175 pldm_pdr_remove_remote_pdrs(repo);
crgeddes2550cee2020-05-05 17:51:28 +0000176 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500177 pldm_pdr_add(repo, data.data(), data.size(), 0, false);
178 pldm_pdr_add(repo, data.data(), data.size(), 0, true);
crgeddes2550cee2020-05-05 17:51:28 +0000179 EXPECT_EQ(pldm_pdr_get_record_count(repo), 3u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500180 pldm_pdr_destroy(repo);
181
182 repo = pldm_pdr_init();
183 pldm_pdr_add(repo, data.data(), data.size(), 0, false);
184 pldm_pdr_add(repo, data.data(), data.size(), 0, true);
185 pldm_pdr_add(repo, data.data(), data.size(), 0, true);
186 pldm_pdr_add(repo, data.data(), data.size(), 0, false);
187 pldm_pdr_remove_remote_pdrs(repo);
crgeddes2550cee2020-05-05 17:51:28 +0000188 EXPECT_EQ(pldm_pdr_get_record_count(repo), 2u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500189 pldm_pdr_add(repo, data.data(), data.size(), 0, false);
190 pldm_pdr_add(repo, data.data(), data.size(), 0, true);
crgeddes2550cee2020-05-05 17:51:28 +0000191 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500192 pldm_pdr_destroy(repo);
193
194 repo = pldm_pdr_init();
195 pldm_pdr_add(repo, data.data(), data.size(), 0, true);
196 pldm_pdr_add(repo, data.data(), data.size(), 0, false);
197 pldm_pdr_add(repo, data.data(), data.size(), 0, false);
198 pldm_pdr_add(repo, data.data(), data.size(), 0, true);
199 pldm_pdr_remove_remote_pdrs(repo);
crgeddes2550cee2020-05-05 17:51:28 +0000200 EXPECT_EQ(pldm_pdr_get_record_count(repo), 2u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500201 auto handle = pldm_pdr_add(repo, data.data(), data.size(), 0, false);
crgeddes2550cee2020-05-05 17:51:28 +0000202 EXPECT_EQ(handle, 3u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500203 handle = pldm_pdr_add(repo, data.data(), data.size(), 0, true);
crgeddes2550cee2020-05-05 17:51:28 +0000204 EXPECT_EQ(handle, 4u);
205 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500206 pldm_pdr_destroy(repo);
207}
208
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600209TEST(PDRAccess, testGet)
210{
211 auto repo = pldm_pdr_init();
212
213 std::array<uint32_t, 10> in{100, 345, 3, 6, 89, 0, 11, 45, 23434, 123123};
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500214 pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in.data()), sizeof(in), 1,
215 false);
crgeddes2550cee2020-05-05 17:51:28 +0000216 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600217 EXPECT_EQ(pldm_pdr_get_repo_size(repo), sizeof(in));
218 uint32_t size{};
219 uint32_t nextRecHdl{};
220 uint8_t* outData = nullptr;
221 auto hdl = pldm_pdr_find_record(repo, 0, &outData, &size, &nextRecHdl);
222 EXPECT_NE(hdl, nullptr);
223 EXPECT_EQ(size, sizeof(in));
crgeddes2550cee2020-05-05 17:51:28 +0000224 EXPECT_EQ(nextRecHdl, 0u);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600225 EXPECT_EQ(memcmp(outData, in.data(), sizeof(in)), 0);
226 outData = nullptr;
227
228 auto hdl2 = pldm_pdr_find_record(repo, 1, &outData, &size, &nextRecHdl);
229 EXPECT_EQ(hdl, hdl2);
230 EXPECT_NE(hdl, nullptr);
231 EXPECT_EQ(size, sizeof(in));
crgeddes2550cee2020-05-05 17:51:28 +0000232 EXPECT_EQ(nextRecHdl, 0u);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600233 EXPECT_EQ(memcmp(outData, in.data(), sizeof(in)), 0);
234 outData = nullptr;
235
Lei YU7cfeb8e2020-02-27 18:24:18 +0800236 hdl = pldm_pdr_find_record(repo, htole32(0xdeaddead), &outData, &size,
237 &nextRecHdl);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600238 EXPECT_EQ(hdl, nullptr);
crgeddes2550cee2020-05-05 17:51:28 +0000239 EXPECT_EQ(size, 0u);
240 EXPECT_EQ(nextRecHdl, 0u);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600241 EXPECT_EQ(outData, nullptr);
242 outData = nullptr;
243
244 std::array<uint32_t, 10> in2{1000, 3450, 30, 60, 890,
245 0, 110, 450, 234034, 123123};
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500246 pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in2.data()), sizeof(in2), 2,
247 false);
248 pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in2.data()), sizeof(in2), 3,
249 false);
250 pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in2.data()), sizeof(in2), 4,
251 true);
crgeddes2550cee2020-05-05 17:51:28 +0000252 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600253 EXPECT_EQ(pldm_pdr_get_repo_size(repo), sizeof(in2) * 4);
254 hdl = pldm_pdr_find_record(repo, 0, &outData, &size, &nextRecHdl);
255 EXPECT_NE(hdl, nullptr);
256 EXPECT_EQ(size, sizeof(in));
crgeddes2550cee2020-05-05 17:51:28 +0000257 EXPECT_EQ(nextRecHdl, 2u);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600258 EXPECT_EQ(memcmp(outData, in.data(), sizeof(in)), 0);
259 outData = nullptr;
260 hdl2 = pldm_pdr_find_record(repo, 1, &outData, &size, &nextRecHdl);
261 EXPECT_EQ(hdl, hdl2);
262 EXPECT_NE(hdl, nullptr);
263 EXPECT_EQ(size, sizeof(in));
crgeddes2550cee2020-05-05 17:51:28 +0000264 EXPECT_EQ(nextRecHdl, 2u);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600265 EXPECT_EQ(memcmp(outData, in.data(), sizeof(in)), 0);
266 outData = nullptr;
267 hdl = pldm_pdr_find_record(repo, 2, &outData, &size, &nextRecHdl);
268 EXPECT_NE(hdl, nullptr);
269 EXPECT_EQ(size, sizeof(in2));
crgeddes2550cee2020-05-05 17:51:28 +0000270 EXPECT_EQ(nextRecHdl, 3u);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600271 EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
272 outData = nullptr;
273 hdl = pldm_pdr_find_record(repo, 3, &outData, &size, &nextRecHdl);
274 EXPECT_NE(hdl, nullptr);
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500275 EXPECT_EQ(pldm_pdr_record_is_remote(hdl), false);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600276 EXPECT_EQ(size, sizeof(in2));
crgeddes2550cee2020-05-05 17:51:28 +0000277 EXPECT_EQ(nextRecHdl, 4u);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600278 EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
279 outData = nullptr;
280 hdl = pldm_pdr_find_record(repo, 4, &outData, &size, &nextRecHdl);
281 EXPECT_NE(hdl, nullptr);
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500282 EXPECT_EQ(pldm_pdr_record_is_remote(hdl), true);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600283 EXPECT_EQ(size, sizeof(in2));
crgeddes2550cee2020-05-05 17:51:28 +0000284 EXPECT_EQ(nextRecHdl, 0u);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600285 EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
286 outData = nullptr;
287
288 pldm_pdr_destroy(repo);
289}
290
291TEST(PDRAccess, testGetNext)
292{
293 auto repo = pldm_pdr_init();
294
295 std::array<uint32_t, 10> in{100, 345, 3, 6, 89, 0, 11, 45, 23434, 123123};
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500296 pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in.data()), sizeof(in), 1,
297 false);
crgeddes2550cee2020-05-05 17:51:28 +0000298 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600299 EXPECT_EQ(pldm_pdr_get_repo_size(repo), sizeof(in));
300 uint32_t size{};
301 uint32_t nextRecHdl{};
302 uint8_t* outData = nullptr;
303 auto hdl = pldm_pdr_find_record(repo, 0, &outData, &size, &nextRecHdl);
304 EXPECT_NE(hdl, nullptr);
305 EXPECT_EQ(size, sizeof(in));
crgeddes2550cee2020-05-05 17:51:28 +0000306 EXPECT_EQ(nextRecHdl, 0u);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600307 EXPECT_EQ(memcmp(outData, in.data(), sizeof(in)), 0);
308 outData = nullptr;
309
310 std::array<uint32_t, 10> in2{1000, 3450, 30, 60, 890,
311 0, 110, 450, 234034, 123123};
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500312 pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in2.data()), sizeof(in2), 2,
313 false);
314 pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in2.data()), sizeof(in2), 3,
315 false);
316 pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in2.data()), sizeof(in2), 4,
317 false);
crgeddes2550cee2020-05-05 17:51:28 +0000318 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600319 EXPECT_EQ(pldm_pdr_get_repo_size(repo), sizeof(in2) * 4);
320 hdl = pldm_pdr_get_next_record(repo, hdl, &outData, &size, &nextRecHdl);
321 EXPECT_NE(hdl, nullptr);
322 EXPECT_EQ(size, sizeof(in2));
crgeddes2550cee2020-05-05 17:51:28 +0000323 EXPECT_EQ(nextRecHdl, 3u);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600324 EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
325 outData = nullptr;
326 hdl = pldm_pdr_get_next_record(repo, hdl, &outData, &size, &nextRecHdl);
327 EXPECT_NE(hdl, nullptr);
328 EXPECT_EQ(size, sizeof(in2));
crgeddes2550cee2020-05-05 17:51:28 +0000329 EXPECT_EQ(nextRecHdl, 4u);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600330 EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
331 outData = nullptr;
332 hdl = pldm_pdr_get_next_record(repo, hdl, &outData, &size, &nextRecHdl);
333 EXPECT_NE(hdl, nullptr);
334 EXPECT_EQ(size, sizeof(in2));
crgeddes2550cee2020-05-05 17:51:28 +0000335 EXPECT_EQ(nextRecHdl, 0u);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600336 EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
337 outData = nullptr;
338
339 pldm_pdr_destroy(repo);
340}
341
342TEST(PDRAccess, testFindByType)
343{
344 auto repo = pldm_pdr_init();
345
346 std::array<uint8_t, sizeof(pldm_pdr_hdr)> data{};
347 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(data.data());
348 hdr->type = 1;
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500349 auto first = pldm_pdr_add(repo, data.data(), data.size(), 0, false);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600350 hdr->type = 2;
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500351 auto second = pldm_pdr_add(repo, data.data(), data.size(), 0, false);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600352 hdr->type = 3;
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500353 auto third = pldm_pdr_add(repo, data.data(), data.size(), 0, false);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600354 hdr->type = 4;
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500355 auto fourth = pldm_pdr_add(repo, data.data(), data.size(), 0, false);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600356
357 uint8_t* outData = nullptr;
358 uint32_t size{};
359 auto firstRec =
360 pldm_pdr_find_record_by_type(repo, 1, nullptr, &outData, &size);
361 EXPECT_EQ(pldm_pdr_get_record_handle(repo, firstRec), first);
362 outData = nullptr;
363 auto secondRec =
364 pldm_pdr_find_record_by_type(repo, 2, nullptr, &outData, &size);
365 EXPECT_EQ(pldm_pdr_get_record_handle(repo, secondRec), second);
366 outData = nullptr;
367 auto thirdRec =
368 pldm_pdr_find_record_by_type(repo, 3, nullptr, &outData, &size);
369 EXPECT_EQ(pldm_pdr_get_record_handle(repo, thirdRec), third);
370 outData = nullptr;
371 auto fourthRec =
372 pldm_pdr_find_record_by_type(repo, 4, nullptr, &outData, &size);
373 EXPECT_EQ(pldm_pdr_get_record_handle(repo, fourthRec), fourth);
374 outData = nullptr;
375 auto fifthRec =
376 pldm_pdr_find_record_by_type(repo, 5, nullptr, &outData, &size);
377 EXPECT_EQ(fifthRec, nullptr);
378 EXPECT_EQ(outData, nullptr);
crgeddes2550cee2020-05-05 17:51:28 +0000379 EXPECT_EQ(size, 0u);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600380
381 auto rec =
382 pldm_pdr_find_record_by_type(repo, 3, secondRec, &outData, &size);
383 EXPECT_EQ(pldm_pdr_get_record_handle(repo, rec), third);
384 outData = nullptr;
385 rec = pldm_pdr_find_record_by_type(repo, 4, secondRec, &outData, &size);
386 EXPECT_EQ(pldm_pdr_get_record_handle(repo, rec), fourth);
387 outData = nullptr;
388 rec = pldm_pdr_find_record_by_type(repo, 2, firstRec, &outData, &size);
389 EXPECT_EQ(pldm_pdr_get_record_handle(repo, rec), second);
390 outData = nullptr;
391
392 pldm_pdr_destroy(repo);
393}
Deepak Kodihallidb914672020-02-07 02:47:45 -0600394
395TEST(PDRUpdate, testAddFruRecordSet)
396{
397 auto repo = pldm_pdr_init();
398
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600399 auto handle = pldm_pdr_add_fru_record_set(repo, 1, 10, 1, 0, 100, 0);
crgeddes2550cee2020-05-05 17:51:28 +0000400 EXPECT_EQ(handle, 1u);
401 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
Deepak Kodihallidb914672020-02-07 02:47:45 -0600402 EXPECT_EQ(pldm_pdr_get_repo_size(repo),
403 sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_fru_record_set));
404 uint32_t size{};
405 uint32_t nextRecHdl{};
406 uint8_t* outData = nullptr;
407 auto hdl = pldm_pdr_find_record(repo, 0, &outData, &size, &nextRecHdl);
408 EXPECT_NE(hdl, nullptr);
409 EXPECT_EQ(size, sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_fru_record_set));
crgeddes2550cee2020-05-05 17:51:28 +0000410 EXPECT_EQ(nextRecHdl, 0u);
Deepak Kodihallidb914672020-02-07 02:47:45 -0600411 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(outData);
crgeddes2550cee2020-05-05 17:51:28 +0000412 EXPECT_EQ(hdr->version, 1u);
Deepak Kodihallidb914672020-02-07 02:47:45 -0600413 EXPECT_EQ(hdr->type, PLDM_PDR_FRU_RECORD_SET);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500414 EXPECT_EQ(hdr->length, htole16(sizeof(pldm_pdr_fru_record_set)));
415 EXPECT_EQ(hdr->record_handle, htole32(1));
Deepak Kodihallidb914672020-02-07 02:47:45 -0600416 pldm_pdr_fru_record_set* fru = reinterpret_cast<pldm_pdr_fru_record_set*>(
417 outData + sizeof(pldm_pdr_hdr));
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500418 EXPECT_EQ(fru->terminus_handle, htole16(1));
419 EXPECT_EQ(fru->fru_rsi, htole16(10));
420 EXPECT_EQ(fru->entity_type, htole16(1));
421 EXPECT_EQ(fru->entity_instance_num, htole16(0));
422 EXPECT_EQ(fru->container_id, htole16(100));
Deepak Kodihallidb914672020-02-07 02:47:45 -0600423 outData = nullptr;
424
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600425 handle = pldm_pdr_add_fru_record_set(repo, 2, 11, 2, 1, 101, 0);
crgeddes2550cee2020-05-05 17:51:28 +0000426 EXPECT_EQ(handle, 2u);
427 EXPECT_EQ(pldm_pdr_get_record_count(repo), 2u);
Deepak Kodihallidb914672020-02-07 02:47:45 -0600428 EXPECT_EQ(pldm_pdr_get_repo_size(repo),
429 2 * (sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_fru_record_set)));
430 hdl = pldm_pdr_find_record(repo, 2, &outData, &size, &nextRecHdl);
431 EXPECT_NE(hdl, nullptr);
432 EXPECT_EQ(size, sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_fru_record_set));
crgeddes2550cee2020-05-05 17:51:28 +0000433 EXPECT_EQ(nextRecHdl, 0u);
Deepak Kodihallidb914672020-02-07 02:47:45 -0600434 hdr = reinterpret_cast<pldm_pdr_hdr*>(outData);
crgeddes2550cee2020-05-05 17:51:28 +0000435 EXPECT_EQ(hdr->version, 1u);
Deepak Kodihallidb914672020-02-07 02:47:45 -0600436 EXPECT_EQ(hdr->type, PLDM_PDR_FRU_RECORD_SET);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500437 EXPECT_EQ(hdr->length, htole16(sizeof(pldm_pdr_fru_record_set)));
438 EXPECT_EQ(hdr->record_handle, htole32(2));
Deepak Kodihallidb914672020-02-07 02:47:45 -0600439 fru = reinterpret_cast<pldm_pdr_fru_record_set*>(outData +
440 sizeof(pldm_pdr_hdr));
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500441 EXPECT_EQ(fru->terminus_handle, htole16(2));
442 EXPECT_EQ(fru->fru_rsi, htole16(11));
443 EXPECT_EQ(fru->entity_type, htole16(2));
444 EXPECT_EQ(fru->entity_instance_num, htole16(1));
445 EXPECT_EQ(fru->container_id, htole16(101));
Deepak Kodihallidb914672020-02-07 02:47:45 -0600446 outData = nullptr;
447
448 hdl = pldm_pdr_find_record(repo, 1, &outData, &size, &nextRecHdl);
449 EXPECT_NE(hdl, nullptr);
450 EXPECT_EQ(size, sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_fru_record_set));
crgeddes2550cee2020-05-05 17:51:28 +0000451 EXPECT_EQ(nextRecHdl, 2u);
Deepak Kodihallidb914672020-02-07 02:47:45 -0600452 hdr = reinterpret_cast<pldm_pdr_hdr*>(outData);
crgeddes2550cee2020-05-05 17:51:28 +0000453 EXPECT_EQ(hdr->version, 1u);
Deepak Kodihallidb914672020-02-07 02:47:45 -0600454 EXPECT_EQ(hdr->type, PLDM_PDR_FRU_RECORD_SET);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500455 EXPECT_EQ(hdr->length, htole16(sizeof(pldm_pdr_fru_record_set)));
456 EXPECT_EQ(hdr->record_handle, htole32(1));
Deepak Kodihallidb914672020-02-07 02:47:45 -0600457 fru = reinterpret_cast<pldm_pdr_fru_record_set*>(outData +
458 sizeof(pldm_pdr_hdr));
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500459 EXPECT_EQ(fru->terminus_handle, htole16(1));
460 EXPECT_EQ(fru->fru_rsi, htole16(10));
461 EXPECT_EQ(fru->entity_type, htole16(1));
462 EXPECT_EQ(fru->entity_instance_num, htole16(0));
463 EXPECT_EQ(fru->container_id, htole16(100));
Deepak Kodihallidb914672020-02-07 02:47:45 -0600464 outData = nullptr;
465
466 pldm_pdr_destroy(repo);
467}
468
469TEST(PDRUpdate, tesFindtFruRecordSet)
470{
471 auto repo = pldm_pdr_init();
472
473 uint16_t terminusHdl{};
474 uint16_t entityType{};
475 uint16_t entityInstanceNum{};
476 uint16_t containerId{};
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600477 auto first = pldm_pdr_add_fru_record_set(repo, 1, 1, 1, 0, 100, 1);
478 auto second = pldm_pdr_add_fru_record_set(repo, 1, 2, 1, 1, 100, 2);
479 auto third = pldm_pdr_add_fru_record_set(repo, 1, 3, 1, 2, 100, 3);
Deepak Kodihallidb914672020-02-07 02:47:45 -0600480 EXPECT_EQ(first, pldm_pdr_get_record_handle(
481 repo, pldm_pdr_fru_record_set_find_by_rsi(
482 repo, 1, &terminusHdl, &entityType,
483 &entityInstanceNum, &containerId)));
484 EXPECT_EQ(second, pldm_pdr_get_record_handle(
485 repo, pldm_pdr_fru_record_set_find_by_rsi(
486 repo, 2, &terminusHdl, &entityType,
487 &entityInstanceNum, &containerId)));
488 EXPECT_EQ(third, pldm_pdr_get_record_handle(
489 repo, pldm_pdr_fru_record_set_find_by_rsi(
490 repo, 3, &terminusHdl, &entityType,
491 &entityInstanceNum, &containerId)));
crgeddes2550cee2020-05-05 17:51:28 +0000492 EXPECT_EQ(terminusHdl, 1u);
493 EXPECT_EQ(entityType, 1u);
494 EXPECT_EQ(entityInstanceNum, 2u);
495 EXPECT_EQ(containerId, 100u);
Deepak Kodihallidb914672020-02-07 02:47:45 -0600496 EXPECT_EQ(nullptr, pldm_pdr_fru_record_set_find_by_rsi(
497 repo, 4, &terminusHdl, &entityType,
498 &entityInstanceNum, &containerId));
499
500 pldm_pdr_destroy(repo);
501}
Deepak Kodihalli05787052020-03-10 01:54:08 -0500502
503TEST(EntityAssociationPDR, testInit)
504{
505 auto tree = pldm_entity_association_tree_init();
506 EXPECT_NE(tree, nullptr);
507 pldm_entity_association_tree_destroy(tree);
508}
509
510TEST(EntityAssociationPDR, testBuild)
511{
512 // 1
513 // |
514 // 2--3--4
515 // |
516 // 5--6--7
517 // | |
518 // 9 8
519
520 pldm_entity entities[9]{};
521
522 entities[0].entity_type = 1;
523 entities[1].entity_type = 2;
524 entities[2].entity_type = 2;
525 entities[3].entity_type = 3;
526 entities[4].entity_type = 4;
527 entities[5].entity_type = 5;
528 entities[6].entity_type = 5;
529 entities[7].entity_type = 6;
530 entities[8].entity_type = 7;
531
532 auto tree = pldm_entity_association_tree_init();
533
George Liu64a8f0f2021-06-12 10:56:11 +0800534 auto l1 = pldm_entity_association_tree_add(
535 tree, &entities[0], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500536 EXPECT_NE(l1, nullptr);
537 auto l2a = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +0800538 tree, &entities[1], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500539 EXPECT_NE(l2a, nullptr);
540 auto l2b = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +0800541 tree, &entities[2], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500542 EXPECT_NE(l2b, nullptr);
543 auto l2c = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +0800544 tree, &entities[3], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500545 EXPECT_NE(l2c, nullptr);
546 auto l3a = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +0800547 tree, &entities[4], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500548 EXPECT_NE(l3a, nullptr);
549 auto l3b = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +0800550 tree, &entities[5], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500551 EXPECT_NE(l3b, nullptr);
552 auto l3c = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +0800553 tree, &entities[6], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500554 EXPECT_NE(l3b, nullptr);
555 auto l4a = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +0800556 tree, &entities[7], 0xFFFF, l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500557 EXPECT_NE(l4a, nullptr);
558 auto l4b = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +0800559 tree, &entities[8], 0xFFFF, l3b, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500560 EXPECT_NE(l4b, nullptr);
561
562 EXPECT_EQ(pldm_entity_is_node_parent(l1), true);
563 EXPECT_EQ(pldm_entity_is_node_parent(l2a), true);
564 EXPECT_EQ(pldm_entity_is_node_parent(l3a), true);
565 EXPECT_EQ(pldm_entity_is_node_parent(l3b), true);
566
567 EXPECT_EQ(pldm_entity_is_node_parent(l2b), false);
568 EXPECT_EQ(pldm_entity_is_node_parent(l2c), false);
569 EXPECT_EQ(pldm_entity_is_node_parent(l3c), false);
570 EXPECT_EQ(pldm_entity_is_node_parent(l4a), false);
571 EXPECT_EQ(pldm_entity_is_node_parent(l4b), false);
572
George Liub6b3cf32021-06-07 16:30:59 +0800573 EXPECT_EQ(pldm_entity_is_exist_parent(l1), false);
George Liufe77eea2021-04-28 15:08:07 +0800574
George Liub6b3cf32021-06-07 16:30:59 +0800575 pldm_entity nodeL1 = pldm_entity_extract(l1);
576 pldm_entity parentL2a = pldm_entity_get_parent(l2a);
577 pldm_entity parentL2b = pldm_entity_get_parent(l2b);
578 pldm_entity parentL2c = pldm_entity_get_parent(l2c);
579 EXPECT_EQ(pldm_entity_is_exist_parent(l2a), true);
580 EXPECT_EQ(pldm_entity_is_exist_parent(l2b), true);
581 EXPECT_EQ(pldm_entity_is_exist_parent(l2c), true);
582 EXPECT_EQ(parentL2a.entity_type, nodeL1.entity_type);
583 EXPECT_EQ(parentL2a.entity_instance_num, nodeL1.entity_instance_num);
584 EXPECT_EQ(parentL2a.entity_container_id, nodeL1.entity_container_id);
585 EXPECT_EQ(parentL2b.entity_type, nodeL1.entity_type);
586 EXPECT_EQ(parentL2b.entity_instance_num, nodeL1.entity_instance_num);
587 EXPECT_EQ(parentL2b.entity_container_id, nodeL1.entity_container_id);
588 EXPECT_EQ(parentL2c.entity_type, nodeL1.entity_type);
589 EXPECT_EQ(parentL2c.entity_instance_num, nodeL1.entity_instance_num);
590 EXPECT_EQ(parentL2c.entity_container_id, nodeL1.entity_container_id);
George Liufe77eea2021-04-28 15:08:07 +0800591
George Liub6b3cf32021-06-07 16:30:59 +0800592 pldm_entity nodeL2a = pldm_entity_extract(l2a);
593 pldm_entity parentL3a = pldm_entity_get_parent(l3a);
594 pldm_entity parentL3b = pldm_entity_get_parent(l3b);
595 pldm_entity parentL3c = pldm_entity_get_parent(l3c);
596 EXPECT_EQ(pldm_entity_is_exist_parent(l3a), true);
597 EXPECT_EQ(pldm_entity_is_exist_parent(l3b), true);
598 EXPECT_EQ(pldm_entity_is_exist_parent(l3c), true);
599 EXPECT_EQ(parentL3a.entity_type, nodeL2a.entity_type);
600 EXPECT_EQ(parentL3a.entity_instance_num, nodeL2a.entity_instance_num);
601 EXPECT_EQ(parentL3a.entity_container_id, nodeL2a.entity_container_id);
602 EXPECT_EQ(parentL3b.entity_type, nodeL2a.entity_type);
603 EXPECT_EQ(parentL3b.entity_instance_num, nodeL2a.entity_instance_num);
604 EXPECT_EQ(parentL3b.entity_container_id, nodeL2a.entity_container_id);
605 EXPECT_EQ(parentL3c.entity_type, nodeL2a.entity_type);
606 EXPECT_EQ(parentL3c.entity_instance_num, nodeL2a.entity_instance_num);
607 EXPECT_EQ(parentL3c.entity_container_id, nodeL2a.entity_container_id);
George Liufe77eea2021-04-28 15:08:07 +0800608
George Liub6b3cf32021-06-07 16:30:59 +0800609 pldm_entity nodeL3a = pldm_entity_extract(l3a);
610 pldm_entity parentL4a = pldm_entity_get_parent(l4a);
611 EXPECT_EQ(pldm_entity_is_exist_parent(l4a), true);
612 EXPECT_EQ(parentL4a.entity_type, nodeL3a.entity_type);
613 EXPECT_EQ(parentL4a.entity_instance_num, nodeL3a.entity_instance_num);
614 EXPECT_EQ(parentL4a.entity_container_id, nodeL3a.entity_container_id);
George Liufe77eea2021-04-28 15:08:07 +0800615
George Liub6b3cf32021-06-07 16:30:59 +0800616 pldm_entity nodeL3b = pldm_entity_extract(l3b);
617 pldm_entity parentL4b = pldm_entity_get_parent(l4b);
618 EXPECT_EQ(pldm_entity_is_exist_parent(l4b), true);
619 EXPECT_EQ(parentL4b.entity_type, nodeL3b.entity_type);
620 EXPECT_EQ(parentL4b.entity_instance_num, nodeL3b.entity_instance_num);
621 EXPECT_EQ(parentL4b.entity_container_id, nodeL3b.entity_container_id);
George Liufe77eea2021-04-28 15:08:07 +0800622
Deepak Kodihalli05787052020-03-10 01:54:08 -0500623 size_t num{};
624 pldm_entity* out = nullptr;
625 pldm_entity_association_tree_visit(tree, &out, &num);
crgeddes2550cee2020-05-05 17:51:28 +0000626 EXPECT_EQ(num, 9u);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500627
crgeddes2550cee2020-05-05 17:51:28 +0000628 EXPECT_EQ(out[0].entity_type, 1u);
629 EXPECT_EQ(out[0].entity_instance_num, 1u);
630 EXPECT_EQ(out[0].entity_container_id, 0u);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500631
crgeddes2550cee2020-05-05 17:51:28 +0000632 EXPECT_EQ(out[1].entity_type, 2u);
633 EXPECT_EQ(out[1].entity_instance_num, 1u);
634 EXPECT_EQ(out[1].entity_container_id, 1u);
635 EXPECT_EQ(out[2].entity_type, 2u);
636 EXPECT_EQ(out[2].entity_instance_num, 2u);
637 EXPECT_EQ(out[2].entity_container_id, 1u);
638 EXPECT_EQ(out[3].entity_type, 3u);
639 EXPECT_EQ(out[3].entity_instance_num, 1u);
640 EXPECT_EQ(out[3].entity_container_id, 1u);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500641
crgeddes2550cee2020-05-05 17:51:28 +0000642 EXPECT_EQ(out[4].entity_type, 4u);
643 EXPECT_EQ(out[4].entity_instance_num, 1u);
644 EXPECT_EQ(out[4].entity_container_id, 2u);
645 EXPECT_EQ(out[5].entity_type, 5u);
646 EXPECT_EQ(out[5].entity_instance_num, 1u);
647 EXPECT_EQ(out[5].entity_container_id, 2u);
648 EXPECT_EQ(out[6].entity_type, 5u);
649 EXPECT_EQ(out[6].entity_instance_num, 2u);
650 EXPECT_EQ(out[6].entity_container_id, 2u);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500651
crgeddes2550cee2020-05-05 17:51:28 +0000652 EXPECT_EQ(out[7].entity_type, 7u);
653 EXPECT_EQ(out[7].entity_instance_num, 1u);
654 EXPECT_EQ(out[7].entity_container_id, 4u);
655 EXPECT_EQ(out[8].entity_type, 6u);
656 EXPECT_EQ(out[8].entity_instance_num, 1u);
657 EXPECT_EQ(out[8].entity_container_id, 3u);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500658
659 free(out);
George Liud9855ab2021-05-13 09:41:31 +0800660
661 pldm_entity p1 = pldm_entity_extract(l1);
662 EXPECT_EQ(p1.entity_type, 1u);
663 EXPECT_EQ(p1.entity_instance_num, 1u);
664 EXPECT_EQ(p1.entity_container_id, 0u);
665
666 pldm_entity p2a = pldm_entity_extract(l2a);
667 EXPECT_EQ(p2a.entity_type, 2u);
668 EXPECT_EQ(p2a.entity_instance_num, 1u);
669 EXPECT_EQ(p2a.entity_container_id, 1u);
670 pldm_entity p2b = pldm_entity_extract(l2b);
671 EXPECT_EQ(p2b.entity_type, 2u);
672 EXPECT_EQ(p2b.entity_instance_num, 2u);
673 EXPECT_EQ(p2b.entity_container_id, 1u);
674 pldm_entity p2c = pldm_entity_extract(l2c);
675 EXPECT_EQ(p2c.entity_type, 3u);
676 EXPECT_EQ(p2c.entity_instance_num, 1u);
677 EXPECT_EQ(p2c.entity_container_id, 1u);
678
679 pldm_entity p3a = pldm_entity_extract(l3a);
680 EXPECT_EQ(p3a.entity_type, 4u);
681 EXPECT_EQ(p3a.entity_instance_num, 1u);
682 EXPECT_EQ(p3a.entity_container_id, 2u);
683 pldm_entity p3b = pldm_entity_extract(l3b);
684 EXPECT_EQ(p3b.entity_type, 5u);
685 EXPECT_EQ(p3b.entity_instance_num, 1u);
686 EXPECT_EQ(p3b.entity_container_id, 2u);
687 pldm_entity p3c = pldm_entity_extract(l3c);
688 EXPECT_EQ(p3c.entity_type, 5u);
689 EXPECT_EQ(p3c.entity_instance_num, 2u);
690 EXPECT_EQ(p3c.entity_container_id, 2u);
691
692 pldm_entity p4a = pldm_entity_extract(l4a);
693 EXPECT_EQ(p4a.entity_type, 6u);
694 EXPECT_EQ(p4a.entity_instance_num, 1u);
695 EXPECT_EQ(p4a.entity_container_id, 3u);
696 pldm_entity p4b = pldm_entity_extract(l4b);
697 EXPECT_EQ(p4b.entity_type, 7u);
698 EXPECT_EQ(p4b.entity_instance_num, 1u);
699 EXPECT_EQ(p4b.entity_container_id, 4u);
700
Deepak Kodihalli05787052020-03-10 01:54:08 -0500701 pldm_entity_association_tree_destroy(tree);
702}
703
704TEST(EntityAssociationPDR, testSpecialTrees)
705{
706 pldm_entity entities[3]{};
707
708 entities[0].entity_type = 1;
709 entities[1].entity_type = 2;
710 entities[2].entity_type = 1;
711
712 // A
713 auto tree = pldm_entity_association_tree_init();
714 auto node = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +0800715 tree, &entities[0], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500716 EXPECT_NE(node, nullptr);
717 size_t num{};
718 pldm_entity* out = nullptr;
719 pldm_entity_association_tree_visit(tree, &out, &num);
crgeddes2550cee2020-05-05 17:51:28 +0000720 EXPECT_EQ(num, 1u);
721 EXPECT_EQ(out[0].entity_type, 1u);
722 EXPECT_EQ(out[0].entity_instance_num, 1u);
723 EXPECT_EQ(out[0].entity_container_id, 0u);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500724 free(out);
725 pldm_entity_association_tree_destroy(tree);
726
727 // A-A-A
728 tree = pldm_entity_association_tree_init();
George Liu64a8f0f2021-06-12 10:56:11 +0800729 node = pldm_entity_association_tree_add(tree, &entities[0], 0xFFFF, nullptr,
Deepak Kodihalli05787052020-03-10 01:54:08 -0500730 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
731 EXPECT_NE(node, nullptr);
George Liu64a8f0f2021-06-12 10:56:11 +0800732 node = pldm_entity_association_tree_add(tree, &entities[1], 0xFFFF, nullptr,
Deepak Kodihalli05787052020-03-10 01:54:08 -0500733 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
734 EXPECT_NE(node, nullptr);
George Liu64a8f0f2021-06-12 10:56:11 +0800735 node = pldm_entity_association_tree_add(tree, &entities[2], 0xFFFF, nullptr,
Deepak Kodihalli05787052020-03-10 01:54:08 -0500736 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
737 EXPECT_NE(node, nullptr);
738 pldm_entity_association_tree_visit(tree, &out, &num);
crgeddes2550cee2020-05-05 17:51:28 +0000739 EXPECT_EQ(num, 3u);
740 EXPECT_EQ(out[0].entity_type, 1u);
741 EXPECT_EQ(out[0].entity_instance_num, 1u);
742 EXPECT_EQ(out[0].entity_container_id, 0u);
743 EXPECT_EQ(out[1].entity_type, 1u);
744 EXPECT_EQ(out[1].entity_instance_num, 2u);
745 EXPECT_EQ(out[1].entity_container_id, 0u);
746 EXPECT_EQ(out[2].entity_type, 2u);
747 EXPECT_EQ(out[2].entity_instance_num, 1u);
748 EXPECT_EQ(out[2].entity_container_id, 0u);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500749 free(out);
750 pldm_entity_association_tree_destroy(tree);
751
752 // A
753 // |
754 // A
755 // |
756 // A
757 tree = pldm_entity_association_tree_init();
George Liu64a8f0f2021-06-12 10:56:11 +0800758 node = pldm_entity_association_tree_add(tree, &entities[0], 0xFFFF, nullptr,
Deepak Kodihalli05787052020-03-10 01:54:08 -0500759 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
760 EXPECT_NE(node, nullptr);
761 auto node1 = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +0800762 tree, &entities[1], 0xFFFF, node, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500763 EXPECT_NE(node1, nullptr);
764 auto node2 = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +0800765 tree, &entities[2], 0xFFFF, node1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500766 EXPECT_NE(node2, nullptr);
767 pldm_entity_association_tree_visit(tree, &out, &num);
crgeddes2550cee2020-05-05 17:51:28 +0000768 EXPECT_EQ(num, 3u);
769 EXPECT_EQ(out[0].entity_type, 1u);
770 EXPECT_EQ(out[0].entity_instance_num, 1u);
771 EXPECT_EQ(out[0].entity_container_id, 0u);
772 EXPECT_EQ(out[1].entity_type, 2u);
773 EXPECT_EQ(out[1].entity_instance_num, 1u);
774 EXPECT_EQ(out[1].entity_container_id, 1u);
775 EXPECT_EQ(out[2].entity_type, 1u);
776 EXPECT_EQ(out[2].entity_instance_num, 1u);
777 EXPECT_EQ(out[2].entity_container_id, 2u);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500778 free(out);
779 pldm_entity_association_tree_destroy(tree);
780
781 // A-A
782 // |
783 // A-A
784 tree = pldm_entity_association_tree_init();
George Liu64a8f0f2021-06-12 10:56:11 +0800785 node = pldm_entity_association_tree_add(tree, &entities[0], 0xFFFF, nullptr,
Deepak Kodihalli05787052020-03-10 01:54:08 -0500786 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
787 EXPECT_NE(node, nullptr);
George Liu64a8f0f2021-06-12 10:56:11 +0800788 node = pldm_entity_association_tree_add(tree, &entities[0], 0xFFFF, nullptr,
Deepak Kodihalli05787052020-03-10 01:54:08 -0500789 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
790 EXPECT_NE(node, nullptr);
George Liu64a8f0f2021-06-12 10:56:11 +0800791 node1 = pldm_entity_association_tree_add(tree, &entities[1], 0xFFFF, node,
Deepak Kodihalli05787052020-03-10 01:54:08 -0500792 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
793 EXPECT_NE(node1, nullptr);
George Liu64a8f0f2021-06-12 10:56:11 +0800794 node2 = pldm_entity_association_tree_add(tree, &entities[2], 0xFFFF, node,
Deepak Kodihalli05787052020-03-10 01:54:08 -0500795 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
796 EXPECT_NE(node2, nullptr);
797 pldm_entity_association_tree_visit(tree, &out, &num);
crgeddes2550cee2020-05-05 17:51:28 +0000798 EXPECT_EQ(num, 4u);
799 EXPECT_EQ(out[0].entity_type, 1u);
800 EXPECT_EQ(out[0].entity_instance_num, 1u);
801 EXPECT_EQ(out[0].entity_container_id, 0u);
802 EXPECT_EQ(out[1].entity_type, 1u);
803 EXPECT_EQ(out[1].entity_instance_num, 2u);
804 EXPECT_EQ(out[1].entity_container_id, 0u);
805 EXPECT_EQ(out[2].entity_type, 2u);
806 EXPECT_EQ(out[2].entity_instance_num, 1u);
807 EXPECT_EQ(out[2].entity_container_id, 1u);
808 EXPECT_EQ(out[3].entity_type, 1u);
809 EXPECT_EQ(out[3].entity_instance_num, 1u);
810 EXPECT_EQ(out[3].entity_container_id, 1u);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500811 free(out);
812 pldm_entity_association_tree_destroy(tree);
813}
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500814
815TEST(EntityAssociationPDR, testPDR)
816{
817 // e = entity type, c = container id, i = instance num
818
819 // INPUT
820 // 1(e=1)--1a(e=2)
821 // |
822 // 2(e=2)--3(e=2)--4(e=2)--5(e=3)
823 // |
824 // 6(e=4)--7(e=5)--8(e=5)--9(e=5)
825 // | |
826 // 11(e=6) 10(e=7)
827
828 // Expected OUTPUT
829 // 1(e=1,c=0,i=1)
830 // |
831 // 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)
832 // |
833 // 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)
834 // | |
835 // 10(e=6,c=3,i=1) 11(e=7,c=4,i=1)
836 pldm_entity entities[11]{};
837
838 entities[0].entity_type = 1;
839 entities[1].entity_type = 2;
840 entities[2].entity_type = 3;
841 entities[3].entity_type = 2;
842 entities[4].entity_type = 3;
843 entities[5].entity_type = 4;
844 entities[6].entity_type = 5;
845 entities[7].entity_type = 5;
846 entities[8].entity_type = 5;
847 entities[9].entity_type = 6;
848 entities[10].entity_type = 7;
849
850 auto tree = pldm_entity_association_tree_init();
851
George Liu64a8f0f2021-06-12 10:56:11 +0800852 auto l1 = pldm_entity_association_tree_add(
853 tree, &entities[0], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500854 EXPECT_NE(l1, nullptr);
855 auto l1a = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +0800856 tree, &entities[1], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500857 EXPECT_NE(l1a, nullptr);
858
859 auto l2a = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +0800860 tree, &entities[1], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500861 EXPECT_NE(l2a, nullptr);
George Liu64a8f0f2021-06-12 10:56:11 +0800862 auto l2b = pldm_entity_association_tree_add(tree, &entities[2], 0xFFFF, l1,
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500863 PLDM_ENTITY_ASSOCIAION_LOGICAL);
864 EXPECT_NE(l2b, nullptr);
865 auto l2c = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +0800866 tree, &entities[3], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500867 EXPECT_NE(l2c, nullptr);
George Liu64a8f0f2021-06-12 10:56:11 +0800868 auto l2d = pldm_entity_association_tree_add(tree, &entities[4], 0xFFFF, l1,
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500869 PLDM_ENTITY_ASSOCIAION_LOGICAL);
870 EXPECT_NE(l2d, nullptr);
871
872 auto l3a = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +0800873 tree, &entities[5], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500874 EXPECT_NE(l3a, nullptr);
875 auto l3b = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +0800876 tree, &entities[6], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500877 EXPECT_NE(l3b, nullptr);
George Liu64a8f0f2021-06-12 10:56:11 +0800878 auto l3c = pldm_entity_association_tree_add(tree, &entities[7], 0xFFFF, l2a,
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500879 PLDM_ENTITY_ASSOCIAION_LOGICAL);
880 EXPECT_NE(l3c, nullptr);
George Liu64a8f0f2021-06-12 10:56:11 +0800881 auto l3d = pldm_entity_association_tree_add(tree, &entities[8], 0xFFFF, l2a,
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500882 PLDM_ENTITY_ASSOCIAION_LOGICAL);
883 EXPECT_NE(l3d, nullptr);
884
885 auto l4a = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +0800886 tree, &entities[9], 0xFFFF, l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500887 EXPECT_NE(l4a, nullptr);
George Liu64a8f0f2021-06-12 10:56:11 +0800888 auto l4b = pldm_entity_association_tree_add(
889 tree, &entities[10], 0xFFFF, l3b, PLDM_ENTITY_ASSOCIAION_LOGICAL);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500890 EXPECT_NE(l4b, nullptr);
891
892 EXPECT_EQ(pldm_entity_get_num_children(l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL),
893 2);
894 EXPECT_EQ(pldm_entity_get_num_children(l1, PLDM_ENTITY_ASSOCIAION_LOGICAL),
895 2);
896 EXPECT_EQ(
897 pldm_entity_get_num_children(l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL), 2);
898 EXPECT_EQ(
899 pldm_entity_get_num_children(l3b, PLDM_ENTITY_ASSOCIAION_PHYSICAL), 0);
900 EXPECT_EQ(pldm_entity_get_num_children(l3b, PLDM_ENTITY_ASSOCIAION_LOGICAL),
901 1);
902
903 auto repo = pldm_pdr_init();
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500904 pldm_entity_association_pdr_add(tree, repo, false);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500905
crgeddes2550cee2020-05-05 17:51:28 +0000906 EXPECT_EQ(pldm_pdr_get_record_count(repo), 6u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500907
908 uint32_t currRecHandle{};
909 uint32_t nextRecHandle{};
910 uint8_t* data = nullptr;
911 uint32_t size{};
912 uint32_t commonSize = sizeof(struct pldm_pdr_hdr) + sizeof(uint16_t) +
913 sizeof(uint8_t) + sizeof(pldm_entity) +
914 sizeof(uint8_t);
915
916 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
917 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
918 l1, PLDM_ENTITY_ASSOCIAION_LOGICAL) *
919 sizeof(pldm_entity)));
920 uint8_t* start = data;
921 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
crgeddes2550cee2020-05-05 17:51:28 +0000922 EXPECT_EQ(le32toh(hdr->record_handle), 1u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500923 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500924 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500925 start += sizeof(pldm_pdr_hdr);
926 uint16_t* containerId = reinterpret_cast<uint16_t*>(start);
crgeddes2550cee2020-05-05 17:51:28 +0000927 EXPECT_EQ(le16toh(*containerId), 1u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500928 start += sizeof(uint16_t);
929 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_LOGICAL);
930 start += sizeof(uint8_t);
931 pldm_entity* entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +0000932 EXPECT_EQ(le16toh(entity->entity_type), 1u);
933 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
934 EXPECT_EQ(le16toh(entity->entity_container_id), 0u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500935 start += sizeof(pldm_entity);
936 EXPECT_EQ(*start,
937 pldm_entity_get_num_children(l1, PLDM_ENTITY_ASSOCIAION_LOGICAL));
938 start += sizeof(uint8_t);
939 entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +0000940 EXPECT_EQ(le16toh(entity->entity_type), 3u);
941 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
942 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500943 start += sizeof(pldm_entity);
944 entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +0000945 EXPECT_EQ(le16toh(entity->entity_type), 3u);
946 EXPECT_EQ(le16toh(entity->entity_instance_num), 2u);
947 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500948 start += sizeof(pldm_entity);
949
950 currRecHandle = nextRecHandle;
951
952 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
953 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
954 l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL) *
955 sizeof(pldm_entity)));
956 start = data;
957 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
crgeddes2550cee2020-05-05 17:51:28 +0000958 EXPECT_EQ(le32toh(hdr->record_handle), 2u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500959 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500960 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500961 start += sizeof(pldm_pdr_hdr);
962 containerId = reinterpret_cast<uint16_t*>(start);
crgeddes2550cee2020-05-05 17:51:28 +0000963 EXPECT_EQ(le16toh(*containerId), 1u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500964 start += sizeof(uint16_t);
965 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
966 start += sizeof(uint8_t);
967 entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +0000968 EXPECT_EQ(le16toh(entity->entity_type), 1u);
969 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
970 EXPECT_EQ(le16toh(entity->entity_container_id), 0u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500971 start += sizeof(pldm_entity);
972 EXPECT_EQ(*start, pldm_entity_get_num_children(
973 l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL));
974 start += sizeof(uint8_t);
975 entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +0000976 EXPECT_EQ(le16toh(entity->entity_type), 2u);
977 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
978 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500979 start += sizeof(pldm_entity);
980 entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +0000981 EXPECT_EQ(le16toh(entity->entity_type), 2u);
982 EXPECT_EQ(le16toh(entity->entity_instance_num), 2u);
983 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500984 start += sizeof(pldm_entity);
985
986 currRecHandle = nextRecHandle;
987
988 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
989 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
990 l2a, PLDM_ENTITY_ASSOCIAION_LOGICAL) *
991 sizeof(pldm_entity)));
992 start = data;
993 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
crgeddes2550cee2020-05-05 17:51:28 +0000994 EXPECT_EQ(le32toh(hdr->record_handle), 3u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500995 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500996 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500997 start += sizeof(pldm_pdr_hdr);
998 containerId = reinterpret_cast<uint16_t*>(start);
crgeddes2550cee2020-05-05 17:51:28 +0000999 EXPECT_EQ(le16toh(*containerId), 2u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001000 start += sizeof(uint16_t);
1001 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_LOGICAL);
1002 start += sizeof(uint8_t);
1003 entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +00001004 EXPECT_EQ(le16toh(entity->entity_type), 2u);
1005 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1006 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001007 start += sizeof(pldm_entity);
1008 EXPECT_EQ(*start, pldm_entity_get_num_children(
1009 l2a, PLDM_ENTITY_ASSOCIAION_LOGICAL));
1010 start += sizeof(uint8_t);
1011 entity = reinterpret_cast<pldm_entity*>(start);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -05001012 EXPECT_EQ(le16toh(entity->entity_type), 5);
crgeddes2550cee2020-05-05 17:51:28 +00001013 EXPECT_EQ(le16toh(entity->entity_instance_num), 2u);
1014 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001015 start += sizeof(pldm_entity);
1016 entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +00001017 EXPECT_EQ(le16toh(entity->entity_type), 5u);
1018 EXPECT_EQ(le16toh(entity->entity_instance_num), 3u);
1019 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001020 start += sizeof(pldm_entity);
1021
1022 currRecHandle = nextRecHandle;
1023
1024 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1025 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1026 l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL) *
1027 sizeof(pldm_entity)));
1028 start = data;
1029 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
crgeddes2550cee2020-05-05 17:51:28 +00001030 EXPECT_EQ(le32toh(hdr->record_handle), 4u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001031 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -05001032 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001033 start += sizeof(pldm_pdr_hdr);
1034 containerId = reinterpret_cast<uint16_t*>(start);
crgeddes2550cee2020-05-05 17:51:28 +00001035 EXPECT_EQ(le16toh(*containerId), 2u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001036 start += sizeof(uint16_t);
1037 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1038 start += sizeof(uint8_t);
1039 entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +00001040 EXPECT_EQ(le16toh(entity->entity_type), 2u);
1041 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1042 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001043 start += sizeof(pldm_entity);
1044 EXPECT_EQ(*start, pldm_entity_get_num_children(
1045 l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL));
1046 start += sizeof(uint8_t);
1047 entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +00001048 EXPECT_EQ(le16toh(entity->entity_type), 4u);
1049 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1050 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001051 start += sizeof(pldm_entity);
1052 entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +00001053 EXPECT_EQ(le16toh(entity->entity_type), 5u);
1054 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1055 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001056 start += sizeof(pldm_entity);
1057
1058 currRecHandle = nextRecHandle;
1059
1060 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1061 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1062 l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL) *
1063 sizeof(pldm_entity)));
1064 start = data;
1065 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
crgeddes2550cee2020-05-05 17:51:28 +00001066 EXPECT_EQ(le32toh(hdr->record_handle), 5u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001067 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -05001068 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001069 start += sizeof(pldm_pdr_hdr);
1070 containerId = reinterpret_cast<uint16_t*>(start);
crgeddes2550cee2020-05-05 17:51:28 +00001071 EXPECT_EQ(le16toh(*containerId), 3u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001072 start += sizeof(uint16_t);
1073 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1074 start += sizeof(uint8_t);
1075 entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +00001076 EXPECT_EQ(le16toh(entity->entity_type), 4u);
1077 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1078 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001079 start += sizeof(pldm_entity);
1080 EXPECT_EQ(*start, pldm_entity_get_num_children(
1081 l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL));
1082 start += sizeof(uint8_t);
1083 entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +00001084 EXPECT_EQ(le16toh(entity->entity_type), 6u);
1085 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1086 EXPECT_EQ(le16toh(entity->entity_container_id), 3u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001087 start += sizeof(pldm_entity);
1088
1089 currRecHandle = nextRecHandle;
1090
1091 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1092 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1093 l3b, PLDM_ENTITY_ASSOCIAION_LOGICAL) *
1094 sizeof(pldm_entity)));
1095 start = data;
1096 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
crgeddes2550cee2020-05-05 17:51:28 +00001097 EXPECT_EQ(le32toh(hdr->record_handle), 6u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001098 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -05001099 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001100 start += sizeof(pldm_pdr_hdr);
1101 containerId = reinterpret_cast<uint16_t*>(start);
crgeddes2550cee2020-05-05 17:51:28 +00001102 EXPECT_EQ(le16toh(*containerId), 4u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001103 start += sizeof(uint16_t);
1104 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_LOGICAL);
1105 start += sizeof(uint8_t);
1106 entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +00001107 EXPECT_EQ(le16toh(entity->entity_type), 5u);
1108 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1109 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001110 start += sizeof(pldm_entity);
1111 EXPECT_EQ(*start, pldm_entity_get_num_children(
1112 l3b, PLDM_ENTITY_ASSOCIAION_LOGICAL));
1113 start += sizeof(uint8_t);
1114 entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +00001115 EXPECT_EQ(le16toh(entity->entity_type), 7u);
1116 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1117 EXPECT_EQ(le16toh(entity->entity_container_id), 4u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001118
crgeddes2550cee2020-05-05 17:51:28 +00001119 EXPECT_EQ(nextRecHandle, 0u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001120
1121 pldm_pdr_destroy(repo);
1122 pldm_entity_association_tree_destroy(tree);
1123}
Deepak Kodihalli3b28ba82020-03-30 07:39:47 -05001124
1125TEST(EntityAssociationPDR, testFind)
1126{
1127 // 1
1128 // |
1129 // 2--3--4
1130 // |
1131 // 5--6--7
1132 // | |
1133 // 8 9
1134
1135 pldm_entity entities[9]{};
1136
1137 entities[0].entity_type = 1;
1138 entities[1].entity_type = 2;
1139 entities[2].entity_type = 2;
1140 entities[3].entity_type = 3;
1141 entities[4].entity_type = 4;
1142 entities[5].entity_type = 5;
1143 entities[6].entity_type = 5;
1144 entities[7].entity_type = 6;
1145 entities[8].entity_type = 7;
1146
1147 auto tree = pldm_entity_association_tree_init();
1148
George Liu64a8f0f2021-06-12 10:56:11 +08001149 auto l1 = pldm_entity_association_tree_add(
1150 tree, &entities[0], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli3b28ba82020-03-30 07:39:47 -05001151 EXPECT_NE(l1, nullptr);
1152 auto l2a = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +08001153 tree, &entities[1], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli3b28ba82020-03-30 07:39:47 -05001154 EXPECT_NE(l2a, nullptr);
1155 auto l2b = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +08001156 tree, &entities[2], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli3b28ba82020-03-30 07:39:47 -05001157 EXPECT_NE(l2b, nullptr);
1158 auto l2c = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +08001159 tree, &entities[3], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli3b28ba82020-03-30 07:39:47 -05001160 EXPECT_NE(l2c, nullptr);
1161 auto l3a = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +08001162 tree, &entities[4], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli3b28ba82020-03-30 07:39:47 -05001163 EXPECT_NE(l3a, nullptr);
1164 auto l3b = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +08001165 tree, &entities[5], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli3b28ba82020-03-30 07:39:47 -05001166 EXPECT_NE(l3b, nullptr);
1167 auto l3c = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +08001168 tree, &entities[6], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli3b28ba82020-03-30 07:39:47 -05001169 EXPECT_NE(l3c, nullptr);
1170 auto l4a = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +08001171 tree, &entities[7], 0xFFFF, l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli3b28ba82020-03-30 07:39:47 -05001172 EXPECT_NE(l4a, nullptr);
1173 auto l4b = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +08001174 tree, &entities[8], 0xFFFF, l3b, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli3b28ba82020-03-30 07:39:47 -05001175 EXPECT_NE(l4b, nullptr);
1176
1177 pldm_entity entity{};
1178
1179 entity.entity_type = 1;
1180 entity.entity_instance_num = 1;
1181 auto result = pldm_entity_association_tree_find(tree, &entity);
1182 EXPECT_EQ(result, l1);
1183 EXPECT_EQ(entity.entity_container_id, 0);
1184
1185 entity.entity_type = 2;
1186 entity.entity_instance_num = 1;
1187 result = pldm_entity_association_tree_find(tree, &entity);
1188 EXPECT_EQ(result, l2a);
1189 EXPECT_EQ(entity.entity_container_id, 1);
1190 entity.entity_type = 2;
1191 entity.entity_instance_num = 2;
1192 result = pldm_entity_association_tree_find(tree, &entity);
1193 EXPECT_EQ(result, l2b);
1194 EXPECT_EQ(entity.entity_container_id, 1);
1195 entity.entity_type = 3;
1196 entity.entity_instance_num = 1;
1197 result = pldm_entity_association_tree_find(tree, &entity);
1198 EXPECT_EQ(result, l2c);
1199 EXPECT_EQ(entity.entity_container_id, 1);
1200
1201 entity.entity_type = 7;
1202 entity.entity_instance_num = 1;
1203 result = pldm_entity_association_tree_find(tree, &entity);
1204 EXPECT_EQ(result, l4b);
1205 EXPECT_EQ(entity.entity_container_id, 4);
1206
1207 pldm_entity_association_tree_destroy(tree);
1208}
1209
Sampa Misrac073a202021-05-08 10:56:05 -05001210TEST(EntityAssociationPDR, testCopyTree)
1211{
1212 pldm_entity entities[4]{};
1213 entities[0].entity_type = 1;
1214 entities[1].entity_type = 2;
1215 entities[2].entity_type = 2;
1216 entities[3].entity_type = 3;
1217
1218 auto orgTree = pldm_entity_association_tree_init();
1219 auto newTree = pldm_entity_association_tree_init();
George Liu64a8f0f2021-06-12 10:56:11 +08001220 auto l1 =
1221 pldm_entity_association_tree_add(orgTree, &entities[0], 0xFFFF, nullptr,
1222 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Sampa Misrac073a202021-05-08 10:56:05 -05001223 EXPECT_NE(l1, nullptr);
1224 auto l2a = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +08001225 orgTree, &entities[1], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Sampa Misrac073a202021-05-08 10:56:05 -05001226 EXPECT_NE(l2a, nullptr);
1227 auto l2b = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +08001228 orgTree, &entities[2], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Sampa Misrac073a202021-05-08 10:56:05 -05001229 EXPECT_NE(l2b, nullptr);
1230 auto l2c = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +08001231 orgTree, &entities[3], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Sampa Misrac073a202021-05-08 10:56:05 -05001232 EXPECT_NE(l2c, nullptr);
1233 size_t orgNum{};
1234 pldm_entity* orgOut = nullptr;
1235 pldm_entity_association_tree_visit(orgTree, &orgOut, &orgNum);
1236 EXPECT_EQ(orgNum, 4u);
1237
1238 pldm_entity_association_tree_copy_root(orgTree, newTree);
1239 size_t newNum{};
1240 pldm_entity* newOut = nullptr;
1241 pldm_entity_association_tree_visit(newTree, &newOut, &newNum);
1242 EXPECT_EQ(newNum, orgNum);
1243 EXPECT_EQ(newOut[0].entity_type, 1u);
1244 EXPECT_EQ(newOut[0].entity_instance_num, 1u);
1245 EXPECT_EQ(newOut[0].entity_container_id, 0u);
1246 free(orgOut);
1247 free(newOut);
1248 pldm_entity_association_tree_destroy(orgTree);
1249 pldm_entity_association_tree_destroy(newTree);
1250}
1251
Deepak Kodihalli3b28ba82020-03-30 07:39:47 -05001252TEST(EntityAssociationPDR, testExtract)
1253{
1254 std::vector<uint8_t> pdr{};
1255 pdr.resize(sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_entity_association) +
1256 sizeof(pldm_entity) * 4);
1257 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(pdr.data());
1258 hdr->type = PLDM_PDR_ENTITY_ASSOCIATION;
1259 hdr->length =
1260 htole16(sizeof(pldm_pdr_entity_association) + sizeof(pldm_entity) * 4);
1261
1262 pldm_pdr_entity_association* e =
1263 reinterpret_cast<pldm_pdr_entity_association*>(pdr.data() +
1264 sizeof(pldm_pdr_hdr));
1265 e->container_id = htole16(1);
1266 e->num_children = 5;
1267 e->container.entity_type = htole16(1);
1268 e->container.entity_instance_num = htole16(1);
1269 e->container.entity_container_id = htole16(0);
1270
1271 pldm_entity* entity = e->children;
1272 entity->entity_type = htole16(2);
1273 entity->entity_instance_num = htole16(1);
1274 entity->entity_container_id = htole16(1);
1275 ++entity;
1276 entity->entity_type = htole16(3);
1277 entity->entity_instance_num = htole16(1);
1278 entity->entity_container_id = htole16(1);
1279 ++entity;
1280 entity->entity_type = htole16(4);
1281 entity->entity_instance_num = htole16(1);
1282 entity->entity_container_id = htole16(1);
1283 ++entity;
1284 entity->entity_type = htole16(5);
1285 entity->entity_instance_num = htole16(1);
1286 entity->entity_container_id = htole16(1);
1287 ++entity;
1288 entity->entity_type = htole16(6);
1289 entity->entity_instance_num = htole16(1);
1290 entity->entity_container_id = htole16(1);
1291
1292 size_t num{};
1293 pldm_entity* out = nullptr;
1294 pldm_entity_association_pdr_extract(pdr.data(), pdr.size(), &num, &out);
crgeddes2550cee2020-05-05 17:51:28 +00001295 EXPECT_EQ(num, (unsigned)e->num_children + 1);
1296 EXPECT_EQ(out[0].entity_type, 1u);
1297 EXPECT_EQ(out[0].entity_instance_num, 1u);
1298 EXPECT_EQ(out[0].entity_container_id, 0u);
1299 EXPECT_EQ(out[1].entity_type, 2u);
1300 EXPECT_EQ(out[1].entity_instance_num, 1u);
1301 EXPECT_EQ(out[1].entity_container_id, 1u);
1302 EXPECT_EQ(out[2].entity_type, 3u);
1303 EXPECT_EQ(out[2].entity_instance_num, 1u);
1304 EXPECT_EQ(out[2].entity_container_id, 1u);
1305 EXPECT_EQ(out[3].entity_type, 4u);
1306 EXPECT_EQ(out[3].entity_instance_num, 1u);
1307 EXPECT_EQ(out[3].entity_container_id, 1u);
1308 EXPECT_EQ(out[4].entity_type, 5u);
1309 EXPECT_EQ(out[4].entity_instance_num, 1u);
1310 EXPECT_EQ(out[4].entity_container_id, 1u);
1311 EXPECT_EQ(out[5].entity_type, 6u);
1312 EXPECT_EQ(out[5].entity_instance_num, 1u);
1313 EXPECT_EQ(out[5].entity_container_id, 1u);
Deepak Kodihalli3b28ba82020-03-30 07:39:47 -05001314
1315 free(out);
1316}
George Liu6a6c3ab2021-06-22 16:12:42 +08001317
1318TEST(EntityAssociationPDR, testGetChildren)
1319{
1320 pldm_entity entities[4]{};
1321 entities[0].entity_type = 1;
1322 entities[1].entity_type = 2;
1323 entities[2].entity_type = 2;
1324 entities[3].entity_type = 3;
1325
1326 auto tree = pldm_entity_association_tree_init();
George Liu64a8f0f2021-06-12 10:56:11 +08001327 auto l1 = pldm_entity_association_tree_add(
1328 tree, &entities[0], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
George Liu6a6c3ab2021-06-22 16:12:42 +08001329 EXPECT_NE(l1, nullptr);
1330 auto l2a = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +08001331 tree, &entities[1], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
George Liu6a6c3ab2021-06-22 16:12:42 +08001332 EXPECT_NE(l2a, nullptr);
1333 auto l2b = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +08001334 tree, &entities[2], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
George Liu6a6c3ab2021-06-22 16:12:42 +08001335 EXPECT_NE(l2b, nullptr);
1336 auto l2c = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +08001337 tree, &entities[3], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
George Liu6a6c3ab2021-06-22 16:12:42 +08001338 EXPECT_NE(l2c, nullptr);
1339
1340 pldm_entity et1;
1341 et1.entity_type = 2;
1342 et1.entity_instance_num = 1;
1343 EXPECT_EQ(true, pldm_is_current_parent_child(l1, &et1));
1344
1345 pldm_entity et2;
1346 et2.entity_type = 2;
1347 et2.entity_instance_num = 2;
1348 EXPECT_EQ(true, pldm_is_current_parent_child(l1, &et2));
1349
1350 pldm_entity et3;
1351 et3.entity_type = 2;
1352 et3.entity_instance_num = 3;
1353 EXPECT_EQ(false, pldm_is_current_parent_child(l1, &et3));
1354
1355 pldm_entity_association_tree_destroy(tree);
George Liu64a8f0f2021-06-12 10:56:11 +08001356}
1357
1358TEST(EntityAssociationPDR, testEntityInstanceNumber)
1359{
1360 pldm_entity entities[9]{};
1361
1362 entities[0].entity_type = 1;
1363 entities[1].entity_type = 2;
1364 entities[2].entity_type = 2;
1365 entities[3].entity_type = 2;
1366 entities[4].entity_type = 2;
1367 entities[5].entity_type = 2;
1368 entities[6].entity_type = 2;
1369 entities[7].entity_type = 3;
1370 entities[8].entity_type = 3;
1371
1372 auto tree = pldm_entity_association_tree_init();
1373 auto repo = pldm_pdr_init();
1374
1375 uint16_t terminusHdl{};
1376 uint16_t entityType{};
1377 uint16_t entityInstanceNum{};
1378 uint16_t containerId{};
1379
1380 auto node = pldm_entity_association_tree_add(
1381 tree, &entities[0], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1382 EXPECT_NE(node, nullptr);
1383
1384 auto l1 = pldm_entity_association_tree_add(tree, &entities[1], 63, node,
1385 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1386 auto first = pldm_pdr_add_fru_record_set(
1387 repo, 1, 1, entities[1].entity_type, entities[1].entity_instance_num,
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -06001388 entities[1].entity_container_id, 1);
George Liu64a8f0f2021-06-12 10:56:11 +08001389 EXPECT_NE(l1, nullptr);
1390 EXPECT_EQ(entities[1].entity_instance_num, 63);
1391 EXPECT_EQ(first, pldm_pdr_get_record_handle(
1392 repo, pldm_pdr_fru_record_set_find_by_rsi(
1393 repo, 1, &terminusHdl, &entityType,
1394 &entityInstanceNum, &containerId)));
1395 EXPECT_EQ(entityType, 2);
1396 EXPECT_EQ(entityInstanceNum, 63);
1397
1398 auto l2 = pldm_entity_association_tree_add(tree, &entities[2], 37, node,
1399 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1400 auto second = pldm_pdr_add_fru_record_set(
1401 repo, 1, 2, entities[2].entity_type, entities[2].entity_instance_num,
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -06001402 entities[2].entity_container_id, 2);
George Liu64a8f0f2021-06-12 10:56:11 +08001403 EXPECT_NE(l2, nullptr);
1404 EXPECT_EQ(entities[2].entity_instance_num, 37);
1405 EXPECT_EQ(second, pldm_pdr_get_record_handle(
1406 repo, pldm_pdr_fru_record_set_find_by_rsi(
1407 repo, 2, &terminusHdl, &entityType,
1408 &entityInstanceNum, &containerId)));
1409 EXPECT_EQ(entityType, 2);
1410 EXPECT_EQ(entityInstanceNum, 37);
1411
1412 auto l3 = pldm_entity_association_tree_add(tree, &entities[3], 44, node,
1413 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1414 auto third = pldm_pdr_add_fru_record_set(
1415 repo, 1, 3, entities[3].entity_type, entities[3].entity_instance_num,
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -06001416 entities[3].entity_container_id, 3);
George Liu64a8f0f2021-06-12 10:56:11 +08001417 EXPECT_NE(l3, nullptr);
1418 EXPECT_EQ(entities[3].entity_instance_num, 44);
1419 EXPECT_EQ(third, pldm_pdr_get_record_handle(
1420 repo, pldm_pdr_fru_record_set_find_by_rsi(
1421 repo, 3, &terminusHdl, &entityType,
1422 &entityInstanceNum, &containerId)));
1423 EXPECT_EQ(entityType, 2);
1424 EXPECT_EQ(entityInstanceNum, 44);
1425
1426 auto l4 = pldm_entity_association_tree_add(tree, &entities[4], 89, node,
1427 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1428 auto fourth = pldm_pdr_add_fru_record_set(
1429 repo, 1, 4, entities[4].entity_type, entities[4].entity_instance_num,
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -06001430 entities[4].entity_container_id, 4);
George Liu64a8f0f2021-06-12 10:56:11 +08001431 EXPECT_NE(l4, nullptr);
1432 EXPECT_EQ(entities[4].entity_instance_num, 89);
1433 EXPECT_EQ(fourth, pldm_pdr_get_record_handle(
1434 repo, pldm_pdr_fru_record_set_find_by_rsi(
1435 repo, 4, &terminusHdl, &entityType,
1436 &entityInstanceNum, &containerId)));
1437 EXPECT_EQ(entityType, 2);
1438 EXPECT_EQ(entityInstanceNum, 89);
1439
1440 auto l5 = pldm_entity_association_tree_add(tree, &entities[5], 0xFFFF, node,
1441 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1442 auto fifth = pldm_pdr_add_fru_record_set(
1443 repo, 1, 5, entities[5].entity_type, entities[5].entity_instance_num,
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -06001444 entities[5].entity_container_id, 5);
George Liu64a8f0f2021-06-12 10:56:11 +08001445 EXPECT_NE(l5, nullptr);
1446 EXPECT_EQ(entities[5].entity_instance_num, 90);
1447 EXPECT_EQ(fifth, pldm_pdr_get_record_handle(
1448 repo, pldm_pdr_fru_record_set_find_by_rsi(
1449 repo, 5, &terminusHdl, &entityType,
1450 &entityInstanceNum, &containerId)));
1451 EXPECT_EQ(entityType, 2);
1452 EXPECT_EQ(entityInstanceNum, 90);
1453
1454 auto l6 = pldm_entity_association_tree_add(tree, &entities[6], 90, node,
1455 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1456 EXPECT_EQ(l6, nullptr);
1457
1458 auto l7 = pldm_entity_association_tree_add(tree, &entities[7], 100, l1,
1459 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1460 auto seventh = pldm_pdr_add_fru_record_set(
1461 repo, 1, 7, entities[7].entity_type, entities[7].entity_instance_num,
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -06001462 entities[7].entity_container_id, 7);
George Liu64a8f0f2021-06-12 10:56:11 +08001463 EXPECT_NE(l7, nullptr);
1464 EXPECT_EQ(entities[7].entity_instance_num, 100);
1465 EXPECT_EQ(seventh, pldm_pdr_get_record_handle(
1466 repo, pldm_pdr_fru_record_set_find_by_rsi(
1467 repo, 7, &terminusHdl, &entityType,
1468 &entityInstanceNum, &containerId)));
1469 EXPECT_EQ(entityType, 3);
1470 EXPECT_EQ(entityInstanceNum, 100);
1471
1472 auto l8 = pldm_entity_association_tree_add(tree, &entities[8], 100, l2,
1473 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1474 auto eighth = pldm_pdr_add_fru_record_set(
1475 repo, 1, 8, entities[8].entity_type, entities[8].entity_instance_num,
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -06001476 entities[8].entity_container_id, 8);
George Liu64a8f0f2021-06-12 10:56:11 +08001477 EXPECT_NE(l8, nullptr);
1478 EXPECT_EQ(entities[8].entity_instance_num, 100);
1479 EXPECT_EQ(eighth, pldm_pdr_get_record_handle(
1480 repo, pldm_pdr_fru_record_set_find_by_rsi(
1481 repo, 8, &terminusHdl, &entityType,
1482 &entityInstanceNum, &containerId)));
1483 EXPECT_EQ(entityType, 3);
1484 EXPECT_EQ(entityInstanceNum, 100);
1485
1486 pldm_pdr_destroy(repo);
1487 pldm_entity_association_tree_destroy(tree);
1488}