blob: bbf950da2924e8a423a50e0e0a90ce1ba9b98e78 [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
399 auto handle = pldm_pdr_add_fru_record_set(repo, 1, 10, 1, 0, 100);
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
425 handle = pldm_pdr_add_fru_record_set(repo, 2, 11, 2, 1, 101);
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{};
477 auto first = pldm_pdr_add_fru_record_set(repo, 1, 1, 1, 0, 100);
478 auto second = pldm_pdr_add_fru_record_set(repo, 1, 2, 1, 1, 100);
479 auto third = pldm_pdr_add_fru_record_set(repo, 1, 3, 1, 2, 100);
480 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
534 auto l1 = pldm_entity_association_tree_add(tree, &entities[0], nullptr,
535 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
536 EXPECT_NE(l1, nullptr);
537 auto l2a = pldm_entity_association_tree_add(
538 tree, &entities[1], l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
539 EXPECT_NE(l2a, nullptr);
540 auto l2b = pldm_entity_association_tree_add(
541 tree, &entities[2], l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
542 EXPECT_NE(l2b, nullptr);
543 auto l2c = pldm_entity_association_tree_add(
544 tree, &entities[3], l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
545 EXPECT_NE(l2c, nullptr);
546 auto l3a = pldm_entity_association_tree_add(
547 tree, &entities[4], l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
548 EXPECT_NE(l3a, nullptr);
549 auto l3b = pldm_entity_association_tree_add(
550 tree, &entities[5], l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
551 EXPECT_NE(l3b, nullptr);
552 auto l3c = pldm_entity_association_tree_add(
553 tree, &entities[6], l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
554 EXPECT_NE(l3b, nullptr);
555 auto l4a = pldm_entity_association_tree_add(
556 tree, &entities[7], l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
557 EXPECT_NE(l4a, nullptr);
558 auto l4b = pldm_entity_association_tree_add(
559 tree, &entities[8], l3b, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
560 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
573 size_t num{};
574 pldm_entity* out = nullptr;
575 pldm_entity_association_tree_visit(tree, &out, &num);
crgeddes2550cee2020-05-05 17:51:28 +0000576 EXPECT_EQ(num, 9u);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500577
crgeddes2550cee2020-05-05 17:51:28 +0000578 EXPECT_EQ(out[0].entity_type, 1u);
579 EXPECT_EQ(out[0].entity_instance_num, 1u);
580 EXPECT_EQ(out[0].entity_container_id, 0u);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500581
crgeddes2550cee2020-05-05 17:51:28 +0000582 EXPECT_EQ(out[1].entity_type, 2u);
583 EXPECT_EQ(out[1].entity_instance_num, 1u);
584 EXPECT_EQ(out[1].entity_container_id, 1u);
585 EXPECT_EQ(out[2].entity_type, 2u);
586 EXPECT_EQ(out[2].entity_instance_num, 2u);
587 EXPECT_EQ(out[2].entity_container_id, 1u);
588 EXPECT_EQ(out[3].entity_type, 3u);
589 EXPECT_EQ(out[3].entity_instance_num, 1u);
590 EXPECT_EQ(out[3].entity_container_id, 1u);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500591
crgeddes2550cee2020-05-05 17:51:28 +0000592 EXPECT_EQ(out[4].entity_type, 4u);
593 EXPECT_EQ(out[4].entity_instance_num, 1u);
594 EXPECT_EQ(out[4].entity_container_id, 2u);
595 EXPECT_EQ(out[5].entity_type, 5u);
596 EXPECT_EQ(out[5].entity_instance_num, 1u);
597 EXPECT_EQ(out[5].entity_container_id, 2u);
598 EXPECT_EQ(out[6].entity_type, 5u);
599 EXPECT_EQ(out[6].entity_instance_num, 2u);
600 EXPECT_EQ(out[6].entity_container_id, 2u);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500601
crgeddes2550cee2020-05-05 17:51:28 +0000602 EXPECT_EQ(out[7].entity_type, 7u);
603 EXPECT_EQ(out[7].entity_instance_num, 1u);
604 EXPECT_EQ(out[7].entity_container_id, 4u);
605 EXPECT_EQ(out[8].entity_type, 6u);
606 EXPECT_EQ(out[8].entity_instance_num, 1u);
607 EXPECT_EQ(out[8].entity_container_id, 3u);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500608
609 free(out);
610 pldm_entity_association_tree_destroy(tree);
611}
612
613TEST(EntityAssociationPDR, testSpecialTrees)
614{
615 pldm_entity entities[3]{};
616
617 entities[0].entity_type = 1;
618 entities[1].entity_type = 2;
619 entities[2].entity_type = 1;
620
621 // A
622 auto tree = pldm_entity_association_tree_init();
623 auto node = pldm_entity_association_tree_add(
624 tree, &entities[0], nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
625 EXPECT_NE(node, nullptr);
626 size_t num{};
627 pldm_entity* out = nullptr;
628 pldm_entity_association_tree_visit(tree, &out, &num);
crgeddes2550cee2020-05-05 17:51:28 +0000629 EXPECT_EQ(num, 1u);
630 EXPECT_EQ(out[0].entity_type, 1u);
631 EXPECT_EQ(out[0].entity_instance_num, 1u);
632 EXPECT_EQ(out[0].entity_container_id, 0u);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500633 free(out);
634 pldm_entity_association_tree_destroy(tree);
635
636 // A-A-A
637 tree = pldm_entity_association_tree_init();
638 node = pldm_entity_association_tree_add(tree, &entities[0], nullptr,
639 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
640 EXPECT_NE(node, nullptr);
641 node = pldm_entity_association_tree_add(tree, &entities[1], nullptr,
642 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
643 EXPECT_NE(node, nullptr);
644 node = pldm_entity_association_tree_add(tree, &entities[2], nullptr,
645 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
646 EXPECT_NE(node, nullptr);
647 pldm_entity_association_tree_visit(tree, &out, &num);
crgeddes2550cee2020-05-05 17:51:28 +0000648 EXPECT_EQ(num, 3u);
649 EXPECT_EQ(out[0].entity_type, 1u);
650 EXPECT_EQ(out[0].entity_instance_num, 1u);
651 EXPECT_EQ(out[0].entity_container_id, 0u);
652 EXPECT_EQ(out[1].entity_type, 1u);
653 EXPECT_EQ(out[1].entity_instance_num, 2u);
654 EXPECT_EQ(out[1].entity_container_id, 0u);
655 EXPECT_EQ(out[2].entity_type, 2u);
656 EXPECT_EQ(out[2].entity_instance_num, 1u);
657 EXPECT_EQ(out[2].entity_container_id, 0u);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500658 free(out);
659 pldm_entity_association_tree_destroy(tree);
660
661 // A
662 // |
663 // A
664 // |
665 // A
666 tree = pldm_entity_association_tree_init();
667 node = pldm_entity_association_tree_add(tree, &entities[0], nullptr,
668 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
669 EXPECT_NE(node, nullptr);
670 auto node1 = pldm_entity_association_tree_add(
671 tree, &entities[1], node, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
672 EXPECT_NE(node1, nullptr);
673 auto node2 = pldm_entity_association_tree_add(
674 tree, &entities[2], node1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
675 EXPECT_NE(node2, nullptr);
676 pldm_entity_association_tree_visit(tree, &out, &num);
crgeddes2550cee2020-05-05 17:51:28 +0000677 EXPECT_EQ(num, 3u);
678 EXPECT_EQ(out[0].entity_type, 1u);
679 EXPECT_EQ(out[0].entity_instance_num, 1u);
680 EXPECT_EQ(out[0].entity_container_id, 0u);
681 EXPECT_EQ(out[1].entity_type, 2u);
682 EXPECT_EQ(out[1].entity_instance_num, 1u);
683 EXPECT_EQ(out[1].entity_container_id, 1u);
684 EXPECT_EQ(out[2].entity_type, 1u);
685 EXPECT_EQ(out[2].entity_instance_num, 1u);
686 EXPECT_EQ(out[2].entity_container_id, 2u);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500687 free(out);
688 pldm_entity_association_tree_destroy(tree);
689
690 // A-A
691 // |
692 // A-A
693 tree = pldm_entity_association_tree_init();
694 node = pldm_entity_association_tree_add(tree, &entities[0], nullptr,
695 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
696 EXPECT_NE(node, nullptr);
697 node = pldm_entity_association_tree_add(tree, &entities[0], nullptr,
698 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
699 EXPECT_NE(node, nullptr);
700 node1 = pldm_entity_association_tree_add(tree, &entities[1], node,
701 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
702 EXPECT_NE(node1, nullptr);
703 node2 = pldm_entity_association_tree_add(tree, &entities[2], node,
704 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
705 EXPECT_NE(node2, nullptr);
706 pldm_entity_association_tree_visit(tree, &out, &num);
crgeddes2550cee2020-05-05 17:51:28 +0000707 EXPECT_EQ(num, 4u);
708 EXPECT_EQ(out[0].entity_type, 1u);
709 EXPECT_EQ(out[0].entity_instance_num, 1u);
710 EXPECT_EQ(out[0].entity_container_id, 0u);
711 EXPECT_EQ(out[1].entity_type, 1u);
712 EXPECT_EQ(out[1].entity_instance_num, 2u);
713 EXPECT_EQ(out[1].entity_container_id, 0u);
714 EXPECT_EQ(out[2].entity_type, 2u);
715 EXPECT_EQ(out[2].entity_instance_num, 1u);
716 EXPECT_EQ(out[2].entity_container_id, 1u);
717 EXPECT_EQ(out[3].entity_type, 1u);
718 EXPECT_EQ(out[3].entity_instance_num, 1u);
719 EXPECT_EQ(out[3].entity_container_id, 1u);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500720 free(out);
721 pldm_entity_association_tree_destroy(tree);
722}
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500723
724TEST(EntityAssociationPDR, testPDR)
725{
726 // e = entity type, c = container id, i = instance num
727
728 // INPUT
729 // 1(e=1)--1a(e=2)
730 // |
731 // 2(e=2)--3(e=2)--4(e=2)--5(e=3)
732 // |
733 // 6(e=4)--7(e=5)--8(e=5)--9(e=5)
734 // | |
735 // 11(e=6) 10(e=7)
736
737 // Expected OUTPUT
738 // 1(e=1,c=0,i=1)
739 // |
740 // 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)
741 // |
742 // 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)
743 // | |
744 // 10(e=6,c=3,i=1) 11(e=7,c=4,i=1)
745 pldm_entity entities[11]{};
746
747 entities[0].entity_type = 1;
748 entities[1].entity_type = 2;
749 entities[2].entity_type = 3;
750 entities[3].entity_type = 2;
751 entities[4].entity_type = 3;
752 entities[5].entity_type = 4;
753 entities[6].entity_type = 5;
754 entities[7].entity_type = 5;
755 entities[8].entity_type = 5;
756 entities[9].entity_type = 6;
757 entities[10].entity_type = 7;
758
759 auto tree = pldm_entity_association_tree_init();
760
761 auto l1 = pldm_entity_association_tree_add(tree, &entities[0], nullptr,
762 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
763 EXPECT_NE(l1, nullptr);
764 auto l1a = pldm_entity_association_tree_add(
765 tree, &entities[1], nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
766 EXPECT_NE(l1a, nullptr);
767
768 auto l2a = pldm_entity_association_tree_add(
769 tree, &entities[1], l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
770 EXPECT_NE(l2a, nullptr);
771 auto l2b = pldm_entity_association_tree_add(tree, &entities[2], l1,
772 PLDM_ENTITY_ASSOCIAION_LOGICAL);
773 EXPECT_NE(l2b, nullptr);
774 auto l2c = pldm_entity_association_tree_add(
775 tree, &entities[3], l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
776 EXPECT_NE(l2c, nullptr);
777 auto l2d = pldm_entity_association_tree_add(tree, &entities[4], l1,
778 PLDM_ENTITY_ASSOCIAION_LOGICAL);
779 EXPECT_NE(l2d, nullptr);
780
781 auto l3a = pldm_entity_association_tree_add(
782 tree, &entities[5], l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
783 EXPECT_NE(l3a, nullptr);
784 auto l3b = pldm_entity_association_tree_add(
785 tree, &entities[6], l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
786 EXPECT_NE(l3b, nullptr);
787 auto l3c = pldm_entity_association_tree_add(tree, &entities[7], l2a,
788 PLDM_ENTITY_ASSOCIAION_LOGICAL);
789 EXPECT_NE(l3c, nullptr);
790 auto l3d = pldm_entity_association_tree_add(tree, &entities[8], l2a,
791 PLDM_ENTITY_ASSOCIAION_LOGICAL);
792 EXPECT_NE(l3d, nullptr);
793
794 auto l4a = pldm_entity_association_tree_add(
795 tree, &entities[9], l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
796 EXPECT_NE(l4a, nullptr);
797 auto l4b = pldm_entity_association_tree_add(tree, &entities[10], l3b,
798 PLDM_ENTITY_ASSOCIAION_LOGICAL);
799 EXPECT_NE(l4b, nullptr);
800
801 EXPECT_EQ(pldm_entity_get_num_children(l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL),
802 2);
803 EXPECT_EQ(pldm_entity_get_num_children(l1, PLDM_ENTITY_ASSOCIAION_LOGICAL),
804 2);
805 EXPECT_EQ(
806 pldm_entity_get_num_children(l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL), 2);
807 EXPECT_EQ(
808 pldm_entity_get_num_children(l3b, PLDM_ENTITY_ASSOCIAION_PHYSICAL), 0);
809 EXPECT_EQ(pldm_entity_get_num_children(l3b, PLDM_ENTITY_ASSOCIAION_LOGICAL),
810 1);
811
812 auto repo = pldm_pdr_init();
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500813 pldm_entity_association_pdr_add(tree, repo, false);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500814
crgeddes2550cee2020-05-05 17:51:28 +0000815 EXPECT_EQ(pldm_pdr_get_record_count(repo), 6u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500816
817 uint32_t currRecHandle{};
818 uint32_t nextRecHandle{};
819 uint8_t* data = nullptr;
820 uint32_t size{};
821 uint32_t commonSize = sizeof(struct pldm_pdr_hdr) + sizeof(uint16_t) +
822 sizeof(uint8_t) + sizeof(pldm_entity) +
823 sizeof(uint8_t);
824
825 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
826 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
827 l1, PLDM_ENTITY_ASSOCIAION_LOGICAL) *
828 sizeof(pldm_entity)));
829 uint8_t* start = data;
830 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
crgeddes2550cee2020-05-05 17:51:28 +0000831 EXPECT_EQ(le32toh(hdr->record_handle), 1u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500832 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500833 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500834 start += sizeof(pldm_pdr_hdr);
835 uint16_t* containerId = reinterpret_cast<uint16_t*>(start);
crgeddes2550cee2020-05-05 17:51:28 +0000836 EXPECT_EQ(le16toh(*containerId), 1u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500837 start += sizeof(uint16_t);
838 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_LOGICAL);
839 start += sizeof(uint8_t);
840 pldm_entity* entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +0000841 EXPECT_EQ(le16toh(entity->entity_type), 1u);
842 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
843 EXPECT_EQ(le16toh(entity->entity_container_id), 0u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500844 start += sizeof(pldm_entity);
845 EXPECT_EQ(*start,
846 pldm_entity_get_num_children(l1, PLDM_ENTITY_ASSOCIAION_LOGICAL));
847 start += sizeof(uint8_t);
848 entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +0000849 EXPECT_EQ(le16toh(entity->entity_type), 3u);
850 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
851 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500852 start += sizeof(pldm_entity);
853 entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +0000854 EXPECT_EQ(le16toh(entity->entity_type), 3u);
855 EXPECT_EQ(le16toh(entity->entity_instance_num), 2u);
856 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500857 start += sizeof(pldm_entity);
858
859 currRecHandle = nextRecHandle;
860
861 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
862 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
863 l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL) *
864 sizeof(pldm_entity)));
865 start = data;
866 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
crgeddes2550cee2020-05-05 17:51:28 +0000867 EXPECT_EQ(le32toh(hdr->record_handle), 2u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500868 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500869 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500870 start += sizeof(pldm_pdr_hdr);
871 containerId = reinterpret_cast<uint16_t*>(start);
crgeddes2550cee2020-05-05 17:51:28 +0000872 EXPECT_EQ(le16toh(*containerId), 1u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500873 start += sizeof(uint16_t);
874 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
875 start += sizeof(uint8_t);
876 entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +0000877 EXPECT_EQ(le16toh(entity->entity_type), 1u);
878 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
879 EXPECT_EQ(le16toh(entity->entity_container_id), 0u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500880 start += sizeof(pldm_entity);
881 EXPECT_EQ(*start, pldm_entity_get_num_children(
882 l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL));
883 start += sizeof(uint8_t);
884 entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +0000885 EXPECT_EQ(le16toh(entity->entity_type), 2u);
886 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
887 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500888 start += sizeof(pldm_entity);
889 entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +0000890 EXPECT_EQ(le16toh(entity->entity_type), 2u);
891 EXPECT_EQ(le16toh(entity->entity_instance_num), 2u);
892 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500893 start += sizeof(pldm_entity);
894
895 currRecHandle = nextRecHandle;
896
897 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
898 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
899 l2a, PLDM_ENTITY_ASSOCIAION_LOGICAL) *
900 sizeof(pldm_entity)));
901 start = data;
902 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
crgeddes2550cee2020-05-05 17:51:28 +0000903 EXPECT_EQ(le32toh(hdr->record_handle), 3u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500904 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500905 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500906 start += sizeof(pldm_pdr_hdr);
907 containerId = reinterpret_cast<uint16_t*>(start);
crgeddes2550cee2020-05-05 17:51:28 +0000908 EXPECT_EQ(le16toh(*containerId), 2u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500909 start += sizeof(uint16_t);
910 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_LOGICAL);
911 start += sizeof(uint8_t);
912 entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +0000913 EXPECT_EQ(le16toh(entity->entity_type), 2u);
914 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
915 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500916 start += sizeof(pldm_entity);
917 EXPECT_EQ(*start, pldm_entity_get_num_children(
918 l2a, PLDM_ENTITY_ASSOCIAION_LOGICAL));
919 start += sizeof(uint8_t);
920 entity = reinterpret_cast<pldm_entity*>(start);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500921 EXPECT_EQ(le16toh(entity->entity_type), 5);
crgeddes2550cee2020-05-05 17:51:28 +0000922 EXPECT_EQ(le16toh(entity->entity_instance_num), 2u);
923 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500924 start += sizeof(pldm_entity);
925 entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +0000926 EXPECT_EQ(le16toh(entity->entity_type), 5u);
927 EXPECT_EQ(le16toh(entity->entity_instance_num), 3u);
928 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500929 start += sizeof(pldm_entity);
930
931 currRecHandle = nextRecHandle;
932
933 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
934 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
935 l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL) *
936 sizeof(pldm_entity)));
937 start = data;
938 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
crgeddes2550cee2020-05-05 17:51:28 +0000939 EXPECT_EQ(le32toh(hdr->record_handle), 4u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500940 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500941 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500942 start += sizeof(pldm_pdr_hdr);
943 containerId = reinterpret_cast<uint16_t*>(start);
crgeddes2550cee2020-05-05 17:51:28 +0000944 EXPECT_EQ(le16toh(*containerId), 2u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500945 start += sizeof(uint16_t);
946 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
947 start += sizeof(uint8_t);
948 entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +0000949 EXPECT_EQ(le16toh(entity->entity_type), 2u);
950 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
951 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500952 start += sizeof(pldm_entity);
953 EXPECT_EQ(*start, pldm_entity_get_num_children(
954 l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL));
955 start += sizeof(uint8_t);
956 entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +0000957 EXPECT_EQ(le16toh(entity->entity_type), 4u);
958 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
959 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500960 start += sizeof(pldm_entity);
961 entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +0000962 EXPECT_EQ(le16toh(entity->entity_type), 5u);
963 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
964 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500965 start += sizeof(pldm_entity);
966
967 currRecHandle = nextRecHandle;
968
969 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
970 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
971 l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL) *
972 sizeof(pldm_entity)));
973 start = data;
974 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
crgeddes2550cee2020-05-05 17:51:28 +0000975 EXPECT_EQ(le32toh(hdr->record_handle), 5u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500976 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500977 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500978 start += sizeof(pldm_pdr_hdr);
979 containerId = reinterpret_cast<uint16_t*>(start);
crgeddes2550cee2020-05-05 17:51:28 +0000980 EXPECT_EQ(le16toh(*containerId), 3u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500981 start += sizeof(uint16_t);
982 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
983 start += sizeof(uint8_t);
984 entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +0000985 EXPECT_EQ(le16toh(entity->entity_type), 4u);
986 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
987 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500988 start += sizeof(pldm_entity);
989 EXPECT_EQ(*start, pldm_entity_get_num_children(
990 l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL));
991 start += sizeof(uint8_t);
992 entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +0000993 EXPECT_EQ(le16toh(entity->entity_type), 6u);
994 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
995 EXPECT_EQ(le16toh(entity->entity_container_id), 3u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500996 start += sizeof(pldm_entity);
997
998 currRecHandle = nextRecHandle;
999
1000 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1001 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1002 l3b, PLDM_ENTITY_ASSOCIAION_LOGICAL) *
1003 sizeof(pldm_entity)));
1004 start = data;
1005 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
crgeddes2550cee2020-05-05 17:51:28 +00001006 EXPECT_EQ(le32toh(hdr->record_handle), 6u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001007 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -05001008 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001009 start += sizeof(pldm_pdr_hdr);
1010 containerId = reinterpret_cast<uint16_t*>(start);
crgeddes2550cee2020-05-05 17:51:28 +00001011 EXPECT_EQ(le16toh(*containerId), 4u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001012 start += sizeof(uint16_t);
1013 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_LOGICAL);
1014 start += sizeof(uint8_t);
1015 entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +00001016 EXPECT_EQ(le16toh(entity->entity_type), 5u);
1017 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1018 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001019 start += sizeof(pldm_entity);
1020 EXPECT_EQ(*start, pldm_entity_get_num_children(
1021 l3b, PLDM_ENTITY_ASSOCIAION_LOGICAL));
1022 start += sizeof(uint8_t);
1023 entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +00001024 EXPECT_EQ(le16toh(entity->entity_type), 7u);
1025 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1026 EXPECT_EQ(le16toh(entity->entity_container_id), 4u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001027
crgeddes2550cee2020-05-05 17:51:28 +00001028 EXPECT_EQ(nextRecHandle, 0u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001029
1030 pldm_pdr_destroy(repo);
1031 pldm_entity_association_tree_destroy(tree);
1032}
Deepak Kodihalli3b28ba82020-03-30 07:39:47 -05001033
1034TEST(EntityAssociationPDR, testFind)
1035{
1036 // 1
1037 // |
1038 // 2--3--4
1039 // |
1040 // 5--6--7
1041 // | |
1042 // 8 9
1043
1044 pldm_entity entities[9]{};
1045
1046 entities[0].entity_type = 1;
1047 entities[1].entity_type = 2;
1048 entities[2].entity_type = 2;
1049 entities[3].entity_type = 3;
1050 entities[4].entity_type = 4;
1051 entities[5].entity_type = 5;
1052 entities[6].entity_type = 5;
1053 entities[7].entity_type = 6;
1054 entities[8].entity_type = 7;
1055
1056 auto tree = pldm_entity_association_tree_init();
1057
1058 auto l1 = pldm_entity_association_tree_add(tree, &entities[0], nullptr,
1059 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1060 EXPECT_NE(l1, nullptr);
1061 auto l2a = pldm_entity_association_tree_add(
1062 tree, &entities[1], l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1063 EXPECT_NE(l2a, nullptr);
1064 auto l2b = pldm_entity_association_tree_add(
1065 tree, &entities[2], l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1066 EXPECT_NE(l2b, nullptr);
1067 auto l2c = pldm_entity_association_tree_add(
1068 tree, &entities[3], l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1069 EXPECT_NE(l2c, nullptr);
1070 auto l3a = pldm_entity_association_tree_add(
1071 tree, &entities[4], l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1072 EXPECT_NE(l3a, nullptr);
1073 auto l3b = pldm_entity_association_tree_add(
1074 tree, &entities[5], l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1075 EXPECT_NE(l3b, nullptr);
1076 auto l3c = pldm_entity_association_tree_add(
1077 tree, &entities[6], l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1078 EXPECT_NE(l3c, nullptr);
1079 auto l4a = pldm_entity_association_tree_add(
1080 tree, &entities[7], l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1081 EXPECT_NE(l4a, nullptr);
1082 auto l4b = pldm_entity_association_tree_add(
1083 tree, &entities[8], l3b, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1084 EXPECT_NE(l4b, nullptr);
1085
1086 pldm_entity entity{};
1087
1088 entity.entity_type = 1;
1089 entity.entity_instance_num = 1;
1090 auto result = pldm_entity_association_tree_find(tree, &entity);
1091 EXPECT_EQ(result, l1);
1092 EXPECT_EQ(entity.entity_container_id, 0);
1093
1094 entity.entity_type = 2;
1095 entity.entity_instance_num = 1;
1096 result = pldm_entity_association_tree_find(tree, &entity);
1097 EXPECT_EQ(result, l2a);
1098 EXPECT_EQ(entity.entity_container_id, 1);
1099 entity.entity_type = 2;
1100 entity.entity_instance_num = 2;
1101 result = pldm_entity_association_tree_find(tree, &entity);
1102 EXPECT_EQ(result, l2b);
1103 EXPECT_EQ(entity.entity_container_id, 1);
1104 entity.entity_type = 3;
1105 entity.entity_instance_num = 1;
1106 result = pldm_entity_association_tree_find(tree, &entity);
1107 EXPECT_EQ(result, l2c);
1108 EXPECT_EQ(entity.entity_container_id, 1);
1109
1110 entity.entity_type = 7;
1111 entity.entity_instance_num = 1;
1112 result = pldm_entity_association_tree_find(tree, &entity);
1113 EXPECT_EQ(result, l4b);
1114 EXPECT_EQ(entity.entity_container_id, 4);
1115
1116 pldm_entity_association_tree_destroy(tree);
1117}
1118
1119TEST(EntityAssociationPDR, testExtract)
1120{
1121 std::vector<uint8_t> pdr{};
1122 pdr.resize(sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_entity_association) +
1123 sizeof(pldm_entity) * 4);
1124 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(pdr.data());
1125 hdr->type = PLDM_PDR_ENTITY_ASSOCIATION;
1126 hdr->length =
1127 htole16(sizeof(pldm_pdr_entity_association) + sizeof(pldm_entity) * 4);
1128
1129 pldm_pdr_entity_association* e =
1130 reinterpret_cast<pldm_pdr_entity_association*>(pdr.data() +
1131 sizeof(pldm_pdr_hdr));
1132 e->container_id = htole16(1);
1133 e->num_children = 5;
1134 e->container.entity_type = htole16(1);
1135 e->container.entity_instance_num = htole16(1);
1136 e->container.entity_container_id = htole16(0);
1137
1138 pldm_entity* entity = e->children;
1139 entity->entity_type = htole16(2);
1140 entity->entity_instance_num = htole16(1);
1141 entity->entity_container_id = htole16(1);
1142 ++entity;
1143 entity->entity_type = htole16(3);
1144 entity->entity_instance_num = htole16(1);
1145 entity->entity_container_id = htole16(1);
1146 ++entity;
1147 entity->entity_type = htole16(4);
1148 entity->entity_instance_num = htole16(1);
1149 entity->entity_container_id = htole16(1);
1150 ++entity;
1151 entity->entity_type = htole16(5);
1152 entity->entity_instance_num = htole16(1);
1153 entity->entity_container_id = htole16(1);
1154 ++entity;
1155 entity->entity_type = htole16(6);
1156 entity->entity_instance_num = htole16(1);
1157 entity->entity_container_id = htole16(1);
1158
1159 size_t num{};
1160 pldm_entity* out = nullptr;
1161 pldm_entity_association_pdr_extract(pdr.data(), pdr.size(), &num, &out);
crgeddes2550cee2020-05-05 17:51:28 +00001162 EXPECT_EQ(num, (unsigned)e->num_children + 1);
1163 EXPECT_EQ(out[0].entity_type, 1u);
1164 EXPECT_EQ(out[0].entity_instance_num, 1u);
1165 EXPECT_EQ(out[0].entity_container_id, 0u);
1166 EXPECT_EQ(out[1].entity_type, 2u);
1167 EXPECT_EQ(out[1].entity_instance_num, 1u);
1168 EXPECT_EQ(out[1].entity_container_id, 1u);
1169 EXPECT_EQ(out[2].entity_type, 3u);
1170 EXPECT_EQ(out[2].entity_instance_num, 1u);
1171 EXPECT_EQ(out[2].entity_container_id, 1u);
1172 EXPECT_EQ(out[3].entity_type, 4u);
1173 EXPECT_EQ(out[3].entity_instance_num, 1u);
1174 EXPECT_EQ(out[3].entity_container_id, 1u);
1175 EXPECT_EQ(out[4].entity_type, 5u);
1176 EXPECT_EQ(out[4].entity_instance_num, 1u);
1177 EXPECT_EQ(out[4].entity_container_id, 1u);
1178 EXPECT_EQ(out[5].entity_type, 6u);
1179 EXPECT_EQ(out[5].entity_instance_num, 1u);
1180 EXPECT_EQ(out[5].entity_container_id, 1u);
Deepak Kodihalli3b28ba82020-03-30 07:39:47 -05001181
1182 free(out);
1183}