blob: 666145a4fade0c476f191b8695bc7eb3781d8524 [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{};
Manojkiran Eda3ca40452021-10-04 22:51:37 +053021 auto handle = pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
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
Manojkiran Eda3ca40452021-10-04 22:51:37 +053026 handle = pldm_pdr_add(repo, data.data(), data.size(), 0u, false, 1);
crgeddes2550cee2020-05-05 17:51:28 +000027 EXPECT_EQ(handle, 2u);
Manojkiran Eda3ca40452021-10-04 22:51:37 +053028 handle = pldm_pdr_add(repo, data.data(), data.size(), 0u, false, 1);
crgeddes2550cee2020-05-05 17:51:28 +000029 EXPECT_EQ(handle, 3u);
30 handle = pldm_pdr_add(repo, data.data(), data.size(), htole32(0xdeeddeedu),
Manojkiran Eda3ca40452021-10-04 22:51:37 +053031 false, 1);
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
Manojkiran Eda3ca40452021-10-04 22:51:37 +053039TEST(PDRRemoveByTerminus, testRemoveByTerminus)
40{
41 std::array<uint8_t, 10> data{};
42
43 auto repo = pldm_pdr_init();
44 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
45 pldm_pdr_remove_pdrs_by_terminus_handle(repo, 1);
46 EXPECT_EQ(pldm_pdr_get_record_count(repo), 0u);
47 pldm_pdr_destroy(repo);
48
49 repo = pldm_pdr_init();
50 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 2);
51 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
52 EXPECT_EQ(pldm_pdr_get_record_count(repo), 2u);
53 pldm_pdr_remove_pdrs_by_terminus_handle(repo, 1);
54 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
55 pldm_pdr_destroy(repo);
56
57 repo = pldm_pdr_init();
58 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
59 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 2);
60 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 2);
61 EXPECT_EQ(pldm_pdr_get_record_count(repo), 3u);
62 pldm_pdr_remove_pdrs_by_terminus_handle(repo, 2);
63 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
64 pldm_pdr_destroy(repo);
65
66 repo = pldm_pdr_init();
67 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
68 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 2);
69 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
70 EXPECT_EQ(pldm_pdr_get_record_count(repo), 3u);
71 pldm_pdr_remove_remote_pdrs(repo);
72 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
73 pldm_pdr_remove_pdrs_by_terminus_handle(repo, 1);
74 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
75 pldm_pdr_remove_pdrs_by_terminus_handle(repo, 2);
76 EXPECT_EQ(pldm_pdr_get_record_count(repo), 0u);
77 pldm_pdr_destroy(repo);
78}
79
Deepak Kodihalli4a680932020-04-22 03:31:17 -050080TEST(PDRUpdate, testRemove)
81{
82 std::array<uint8_t, 10> data{};
83
84 auto repo = pldm_pdr_init();
85 pldm_pdr_remove_remote_pdrs(repo);
crgeddes2550cee2020-05-05 17:51:28 +000086 EXPECT_EQ(pldm_pdr_get_record_count(repo), 0u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -050087 pldm_pdr_destroy(repo);
88
89 repo = pldm_pdr_init();
Manojkiran Eda3ca40452021-10-04 22:51:37 +053090 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
Deepak Kodihalli4a680932020-04-22 03:31:17 -050091 pldm_pdr_remove_remote_pdrs(repo);
crgeddes2550cee2020-05-05 17:51:28 +000092 EXPECT_EQ(pldm_pdr_get_record_count(repo), 0u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -050093 pldm_pdr_destroy(repo);
94
95 repo = pldm_pdr_init();
Manojkiran Eda3ca40452021-10-04 22:51:37 +053096 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
Deepak Kodihalli4a680932020-04-22 03:31:17 -050097 pldm_pdr_remove_remote_pdrs(repo);
crgeddes2550cee2020-05-05 17:51:28 +000098 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -050099 pldm_pdr_destroy(repo);
100
101 repo = pldm_pdr_init();
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530102 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
103 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500104 pldm_pdr_remove_remote_pdrs(repo);
crgeddes2550cee2020-05-05 17:51:28 +0000105 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500106 pldm_pdr_destroy(repo);
107
108 repo = pldm_pdr_init();
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530109 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
110 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500111 pldm_pdr_remove_remote_pdrs(repo);
crgeddes2550cee2020-05-05 17:51:28 +0000112 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500113 pldm_pdr_destroy(repo);
114
115 repo = pldm_pdr_init();
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530116 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
117 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
118 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
119 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500120 pldm_pdr_remove_remote_pdrs(repo);
crgeddes2550cee2020-05-05 17:51:28 +0000121 EXPECT_EQ(pldm_pdr_get_record_count(repo), 0u);
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530122 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
123 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
crgeddes2550cee2020-05-05 17:51:28 +0000124 EXPECT_EQ(pldm_pdr_get_record_count(repo), 2u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500125 pldm_pdr_destroy(repo);
126
127 repo = pldm_pdr_init();
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530128 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
129 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
130 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
131 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500132 pldm_pdr_remove_remote_pdrs(repo);
crgeddes2550cee2020-05-05 17:51:28 +0000133 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530134 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
135 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
crgeddes2550cee2020-05-05 17:51:28 +0000136 EXPECT_EQ(pldm_pdr_get_record_count(repo), 6u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500137 pldm_pdr_destroy(repo);
138
139 repo = pldm_pdr_init();
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530140 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
141 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
142 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
143 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500144 pldm_pdr_remove_remote_pdrs(repo);
crgeddes2550cee2020-05-05 17:51:28 +0000145 EXPECT_EQ(pldm_pdr_get_record_count(repo), 3u);
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530146 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
147 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
crgeddes2550cee2020-05-05 17:51:28 +0000148 EXPECT_EQ(pldm_pdr_get_record_count(repo), 5u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500149 pldm_pdr_destroy(repo);
150
151 repo = pldm_pdr_init();
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530152 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
153 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
154 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
155 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500156 pldm_pdr_remove_remote_pdrs(repo);
crgeddes2550cee2020-05-05 17:51:28 +0000157 EXPECT_EQ(pldm_pdr_get_record_count(repo), 3u);
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530158 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
159 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
crgeddes2550cee2020-05-05 17:51:28 +0000160 EXPECT_EQ(pldm_pdr_get_record_count(repo), 5u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500161 pldm_pdr_destroy(repo);
162
163 repo = pldm_pdr_init();
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530164 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
165 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
166 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
167 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500168 pldm_pdr_remove_remote_pdrs(repo);
crgeddes2550cee2020-05-05 17:51:28 +0000169 EXPECT_EQ(pldm_pdr_get_record_count(repo), 2u);
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530170 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
171 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
crgeddes2550cee2020-05-05 17:51:28 +0000172 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500173 pldm_pdr_destroy(repo);
174
175 repo = pldm_pdr_init();
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530176 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
177 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
178 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
179 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500180 pldm_pdr_remove_remote_pdrs(repo);
crgeddes2550cee2020-05-05 17:51:28 +0000181 EXPECT_EQ(pldm_pdr_get_record_count(repo), 3u);
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530182 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
183 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
crgeddes2550cee2020-05-05 17:51:28 +0000184 EXPECT_EQ(pldm_pdr_get_record_count(repo), 5u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500185 pldm_pdr_destroy(repo);
186
187 repo = pldm_pdr_init();
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530188 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
189 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
190 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
191 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500192 pldm_pdr_remove_remote_pdrs(repo);
crgeddes2550cee2020-05-05 17:51:28 +0000193 EXPECT_EQ(pldm_pdr_get_record_count(repo), 2u);
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530194 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
195 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
crgeddes2550cee2020-05-05 17:51:28 +0000196 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500197 pldm_pdr_destroy(repo);
198
199 repo = pldm_pdr_init();
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530200 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
201 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
202 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
203 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500204 pldm_pdr_remove_remote_pdrs(repo);
crgeddes2550cee2020-05-05 17:51:28 +0000205 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530206 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
207 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
crgeddes2550cee2020-05-05 17:51:28 +0000208 EXPECT_EQ(pldm_pdr_get_record_count(repo), 3u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500209 pldm_pdr_destroy(repo);
210
211 repo = pldm_pdr_init();
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530212 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
213 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
214 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
215 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500216 pldm_pdr_remove_remote_pdrs(repo);
crgeddes2550cee2020-05-05 17:51:28 +0000217 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530218 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
219 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
crgeddes2550cee2020-05-05 17:51:28 +0000220 EXPECT_EQ(pldm_pdr_get_record_count(repo), 3u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500221 pldm_pdr_destroy(repo);
222
223 repo = pldm_pdr_init();
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530224 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
225 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
226 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
227 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500228 pldm_pdr_remove_remote_pdrs(repo);
crgeddes2550cee2020-05-05 17:51:28 +0000229 EXPECT_EQ(pldm_pdr_get_record_count(repo), 2u);
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530230 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
231 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
crgeddes2550cee2020-05-05 17:51:28 +0000232 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500233 pldm_pdr_destroy(repo);
234
235 repo = pldm_pdr_init();
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530236 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
237 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
238 pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
239 pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500240 pldm_pdr_remove_remote_pdrs(repo);
crgeddes2550cee2020-05-05 17:51:28 +0000241 EXPECT_EQ(pldm_pdr_get_record_count(repo), 2u);
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530242 auto handle = pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
crgeddes2550cee2020-05-05 17:51:28 +0000243 EXPECT_EQ(handle, 3u);
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530244 handle = pldm_pdr_add(repo, data.data(), data.size(), 0, true, 1);
crgeddes2550cee2020-05-05 17:51:28 +0000245 EXPECT_EQ(handle, 4u);
246 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
Deepak Kodihalli4a680932020-04-22 03:31:17 -0500247 pldm_pdr_destroy(repo);
248}
249
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600250TEST(PDRAccess, testGet)
251{
252 auto repo = pldm_pdr_init();
253
254 std::array<uint32_t, 10> in{100, 345, 3, 6, 89, 0, 11, 45, 23434, 123123};
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500255 pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in.data()), sizeof(in), 1,
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530256 false, 1);
crgeddes2550cee2020-05-05 17:51:28 +0000257 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600258 EXPECT_EQ(pldm_pdr_get_repo_size(repo), sizeof(in));
259 uint32_t size{};
260 uint32_t nextRecHdl{};
261 uint8_t* outData = nullptr;
262 auto hdl = pldm_pdr_find_record(repo, 0, &outData, &size, &nextRecHdl);
263 EXPECT_NE(hdl, nullptr);
264 EXPECT_EQ(size, sizeof(in));
crgeddes2550cee2020-05-05 17:51:28 +0000265 EXPECT_EQ(nextRecHdl, 0u);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600266 EXPECT_EQ(memcmp(outData, in.data(), sizeof(in)), 0);
267 outData = nullptr;
268
269 auto hdl2 = pldm_pdr_find_record(repo, 1, &outData, &size, &nextRecHdl);
270 EXPECT_EQ(hdl, hdl2);
271 EXPECT_NE(hdl, nullptr);
272 EXPECT_EQ(size, sizeof(in));
crgeddes2550cee2020-05-05 17:51:28 +0000273 EXPECT_EQ(nextRecHdl, 0u);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600274 EXPECT_EQ(memcmp(outData, in.data(), sizeof(in)), 0);
275 outData = nullptr;
276
Lei YU7cfeb8e2020-02-27 18:24:18 +0800277 hdl = pldm_pdr_find_record(repo, htole32(0xdeaddead), &outData, &size,
278 &nextRecHdl);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600279 EXPECT_EQ(hdl, nullptr);
crgeddes2550cee2020-05-05 17:51:28 +0000280 EXPECT_EQ(size, 0u);
281 EXPECT_EQ(nextRecHdl, 0u);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600282 EXPECT_EQ(outData, nullptr);
283 outData = nullptr;
284
285 std::array<uint32_t, 10> in2{1000, 3450, 30, 60, 890,
286 0, 110, 450, 234034, 123123};
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500287 pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in2.data()), sizeof(in2), 2,
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530288 false, 1);
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500289 pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in2.data()), sizeof(in2), 3,
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530290 false, 1);
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500291 pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in2.data()), sizeof(in2), 4,
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530292 true, 1);
crgeddes2550cee2020-05-05 17:51:28 +0000293 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600294 EXPECT_EQ(pldm_pdr_get_repo_size(repo), sizeof(in2) * 4);
295 hdl = pldm_pdr_find_record(repo, 0, &outData, &size, &nextRecHdl);
296 EXPECT_NE(hdl, nullptr);
297 EXPECT_EQ(size, sizeof(in));
crgeddes2550cee2020-05-05 17:51:28 +0000298 EXPECT_EQ(nextRecHdl, 2u);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600299 EXPECT_EQ(memcmp(outData, in.data(), sizeof(in)), 0);
300 outData = nullptr;
301 hdl2 = pldm_pdr_find_record(repo, 1, &outData, &size, &nextRecHdl);
302 EXPECT_EQ(hdl, hdl2);
303 EXPECT_NE(hdl, nullptr);
304 EXPECT_EQ(size, sizeof(in));
crgeddes2550cee2020-05-05 17:51:28 +0000305 EXPECT_EQ(nextRecHdl, 2u);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600306 EXPECT_EQ(memcmp(outData, in.data(), sizeof(in)), 0);
307 outData = nullptr;
308 hdl = pldm_pdr_find_record(repo, 2, &outData, &size, &nextRecHdl);
309 EXPECT_NE(hdl, nullptr);
310 EXPECT_EQ(size, sizeof(in2));
crgeddes2550cee2020-05-05 17:51:28 +0000311 EXPECT_EQ(nextRecHdl, 3u);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600312 EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
313 outData = nullptr;
314 hdl = pldm_pdr_find_record(repo, 3, &outData, &size, &nextRecHdl);
315 EXPECT_NE(hdl, nullptr);
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500316 EXPECT_EQ(pldm_pdr_record_is_remote(hdl), false);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600317 EXPECT_EQ(size, sizeof(in2));
crgeddes2550cee2020-05-05 17:51:28 +0000318 EXPECT_EQ(nextRecHdl, 4u);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600319 EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
320 outData = nullptr;
321 hdl = pldm_pdr_find_record(repo, 4, &outData, &size, &nextRecHdl);
322 EXPECT_NE(hdl, nullptr);
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500323 EXPECT_EQ(pldm_pdr_record_is_remote(hdl), true);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600324 EXPECT_EQ(size, sizeof(in2));
crgeddes2550cee2020-05-05 17:51:28 +0000325 EXPECT_EQ(nextRecHdl, 0u);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600326 EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
327 outData = nullptr;
328
329 pldm_pdr_destroy(repo);
330}
331
332TEST(PDRAccess, testGetNext)
333{
334 auto repo = pldm_pdr_init();
335
336 std::array<uint32_t, 10> in{100, 345, 3, 6, 89, 0, 11, 45, 23434, 123123};
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500337 pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in.data()), sizeof(in), 1,
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530338 false, 1);
crgeddes2550cee2020-05-05 17:51:28 +0000339 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600340 EXPECT_EQ(pldm_pdr_get_repo_size(repo), sizeof(in));
341 uint32_t size{};
342 uint32_t nextRecHdl{};
343 uint8_t* outData = nullptr;
344 auto hdl = pldm_pdr_find_record(repo, 0, &outData, &size, &nextRecHdl);
345 EXPECT_NE(hdl, nullptr);
346 EXPECT_EQ(size, sizeof(in));
crgeddes2550cee2020-05-05 17:51:28 +0000347 EXPECT_EQ(nextRecHdl, 0u);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600348 EXPECT_EQ(memcmp(outData, in.data(), sizeof(in)), 0);
349 outData = nullptr;
350
351 std::array<uint32_t, 10> in2{1000, 3450, 30, 60, 890,
352 0, 110, 450, 234034, 123123};
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500353 pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in2.data()), sizeof(in2), 2,
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530354 false, 1);
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500355 pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in2.data()), sizeof(in2), 3,
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530356 false, 1);
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500357 pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in2.data()), sizeof(in2), 4,
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530358 false, 1);
crgeddes2550cee2020-05-05 17:51:28 +0000359 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4u);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600360 EXPECT_EQ(pldm_pdr_get_repo_size(repo), sizeof(in2) * 4);
361 hdl = pldm_pdr_get_next_record(repo, hdl, &outData, &size, &nextRecHdl);
362 EXPECT_NE(hdl, nullptr);
363 EXPECT_EQ(size, sizeof(in2));
crgeddes2550cee2020-05-05 17:51:28 +0000364 EXPECT_EQ(nextRecHdl, 3u);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600365 EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
366 outData = nullptr;
367 hdl = pldm_pdr_get_next_record(repo, hdl, &outData, &size, &nextRecHdl);
368 EXPECT_NE(hdl, nullptr);
369 EXPECT_EQ(size, sizeof(in2));
crgeddes2550cee2020-05-05 17:51:28 +0000370 EXPECT_EQ(nextRecHdl, 4u);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600371 EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
372 outData = nullptr;
373 hdl = pldm_pdr_get_next_record(repo, hdl, &outData, &size, &nextRecHdl);
374 EXPECT_NE(hdl, nullptr);
375 EXPECT_EQ(size, sizeof(in2));
crgeddes2550cee2020-05-05 17:51:28 +0000376 EXPECT_EQ(nextRecHdl, 0u);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600377 EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
378 outData = nullptr;
379
380 pldm_pdr_destroy(repo);
381}
382
383TEST(PDRAccess, testFindByType)
384{
385 auto repo = pldm_pdr_init();
386
387 std::array<uint8_t, sizeof(pldm_pdr_hdr)> data{};
388 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(data.data());
389 hdr->type = 1;
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530390 auto first = pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600391 hdr->type = 2;
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530392 auto second = pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600393 hdr->type = 3;
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530394 auto third = pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600395 hdr->type = 4;
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530396 auto fourth = pldm_pdr_add(repo, data.data(), data.size(), 0, false, 1);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600397
398 uint8_t* outData = nullptr;
399 uint32_t size{};
400 auto firstRec =
401 pldm_pdr_find_record_by_type(repo, 1, nullptr, &outData, &size);
402 EXPECT_EQ(pldm_pdr_get_record_handle(repo, firstRec), first);
403 outData = nullptr;
404 auto secondRec =
405 pldm_pdr_find_record_by_type(repo, 2, nullptr, &outData, &size);
406 EXPECT_EQ(pldm_pdr_get_record_handle(repo, secondRec), second);
407 outData = nullptr;
408 auto thirdRec =
409 pldm_pdr_find_record_by_type(repo, 3, nullptr, &outData, &size);
410 EXPECT_EQ(pldm_pdr_get_record_handle(repo, thirdRec), third);
411 outData = nullptr;
412 auto fourthRec =
413 pldm_pdr_find_record_by_type(repo, 4, nullptr, &outData, &size);
414 EXPECT_EQ(pldm_pdr_get_record_handle(repo, fourthRec), fourth);
415 outData = nullptr;
416 auto fifthRec =
417 pldm_pdr_find_record_by_type(repo, 5, nullptr, &outData, &size);
418 EXPECT_EQ(fifthRec, nullptr);
419 EXPECT_EQ(outData, nullptr);
crgeddes2550cee2020-05-05 17:51:28 +0000420 EXPECT_EQ(size, 0u);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600421
422 auto rec =
423 pldm_pdr_find_record_by_type(repo, 3, secondRec, &outData, &size);
424 EXPECT_EQ(pldm_pdr_get_record_handle(repo, rec), third);
425 outData = nullptr;
426 rec = pldm_pdr_find_record_by_type(repo, 4, secondRec, &outData, &size);
427 EXPECT_EQ(pldm_pdr_get_record_handle(repo, rec), fourth);
428 outData = nullptr;
429 rec = pldm_pdr_find_record_by_type(repo, 2, firstRec, &outData, &size);
430 EXPECT_EQ(pldm_pdr_get_record_handle(repo, rec), second);
431 outData = nullptr;
432
433 pldm_pdr_destroy(repo);
434}
Deepak Kodihallidb914672020-02-07 02:47:45 -0600435
436TEST(PDRUpdate, testAddFruRecordSet)
437{
438 auto repo = pldm_pdr_init();
439
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600440 auto handle = pldm_pdr_add_fru_record_set(repo, 1, 10, 1, 0, 100, 0);
crgeddes2550cee2020-05-05 17:51:28 +0000441 EXPECT_EQ(handle, 1u);
442 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1u);
Deepak Kodihallidb914672020-02-07 02:47:45 -0600443 EXPECT_EQ(pldm_pdr_get_repo_size(repo),
444 sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_fru_record_set));
445 uint32_t size{};
446 uint32_t nextRecHdl{};
447 uint8_t* outData = nullptr;
448 auto hdl = pldm_pdr_find_record(repo, 0, &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, 0u);
Deepak Kodihallidb914672020-02-07 02:47:45 -0600452 pldm_pdr_hdr* 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 pldm_pdr_fru_record_set* fru = reinterpret_cast<pldm_pdr_fru_record_set*>(
458 outData + 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
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600466 handle = pldm_pdr_add_fru_record_set(repo, 2, 11, 2, 1, 101, 0);
crgeddes2550cee2020-05-05 17:51:28 +0000467 EXPECT_EQ(handle, 2u);
468 EXPECT_EQ(pldm_pdr_get_record_count(repo), 2u);
Deepak Kodihallidb914672020-02-07 02:47:45 -0600469 EXPECT_EQ(pldm_pdr_get_repo_size(repo),
470 2 * (sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_fru_record_set)));
471 hdl = pldm_pdr_find_record(repo, 2, &outData, &size, &nextRecHdl);
472 EXPECT_NE(hdl, nullptr);
473 EXPECT_EQ(size, sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_fru_record_set));
crgeddes2550cee2020-05-05 17:51:28 +0000474 EXPECT_EQ(nextRecHdl, 0u);
Deepak Kodihallidb914672020-02-07 02:47:45 -0600475 hdr = reinterpret_cast<pldm_pdr_hdr*>(outData);
crgeddes2550cee2020-05-05 17:51:28 +0000476 EXPECT_EQ(hdr->version, 1u);
Deepak Kodihallidb914672020-02-07 02:47:45 -0600477 EXPECT_EQ(hdr->type, PLDM_PDR_FRU_RECORD_SET);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500478 EXPECT_EQ(hdr->length, htole16(sizeof(pldm_pdr_fru_record_set)));
479 EXPECT_EQ(hdr->record_handle, htole32(2));
Deepak Kodihallidb914672020-02-07 02:47:45 -0600480 fru = reinterpret_cast<pldm_pdr_fru_record_set*>(outData +
481 sizeof(pldm_pdr_hdr));
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500482 EXPECT_EQ(fru->terminus_handle, htole16(2));
483 EXPECT_EQ(fru->fru_rsi, htole16(11));
484 EXPECT_EQ(fru->entity_type, htole16(2));
485 EXPECT_EQ(fru->entity_instance_num, htole16(1));
486 EXPECT_EQ(fru->container_id, htole16(101));
Deepak Kodihallidb914672020-02-07 02:47:45 -0600487 outData = nullptr;
488
489 hdl = pldm_pdr_find_record(repo, 1, &outData, &size, &nextRecHdl);
490 EXPECT_NE(hdl, nullptr);
491 EXPECT_EQ(size, sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_fru_record_set));
crgeddes2550cee2020-05-05 17:51:28 +0000492 EXPECT_EQ(nextRecHdl, 2u);
Deepak Kodihallidb914672020-02-07 02:47:45 -0600493 hdr = reinterpret_cast<pldm_pdr_hdr*>(outData);
crgeddes2550cee2020-05-05 17:51:28 +0000494 EXPECT_EQ(hdr->version, 1u);
Deepak Kodihallidb914672020-02-07 02:47:45 -0600495 EXPECT_EQ(hdr->type, PLDM_PDR_FRU_RECORD_SET);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500496 EXPECT_EQ(hdr->length, htole16(sizeof(pldm_pdr_fru_record_set)));
497 EXPECT_EQ(hdr->record_handle, htole32(1));
Deepak Kodihallidb914672020-02-07 02:47:45 -0600498 fru = reinterpret_cast<pldm_pdr_fru_record_set*>(outData +
499 sizeof(pldm_pdr_hdr));
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500500 EXPECT_EQ(fru->terminus_handle, htole16(1));
501 EXPECT_EQ(fru->fru_rsi, htole16(10));
502 EXPECT_EQ(fru->entity_type, htole16(1));
503 EXPECT_EQ(fru->entity_instance_num, htole16(0));
504 EXPECT_EQ(fru->container_id, htole16(100));
Deepak Kodihallidb914672020-02-07 02:47:45 -0600505 outData = nullptr;
506
507 pldm_pdr_destroy(repo);
508}
509
510TEST(PDRUpdate, tesFindtFruRecordSet)
511{
512 auto repo = pldm_pdr_init();
513
514 uint16_t terminusHdl{};
515 uint16_t entityType{};
516 uint16_t entityInstanceNum{};
517 uint16_t containerId{};
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600518 auto first = pldm_pdr_add_fru_record_set(repo, 1, 1, 1, 0, 100, 1);
519 auto second = pldm_pdr_add_fru_record_set(repo, 1, 2, 1, 1, 100, 2);
520 auto third = pldm_pdr_add_fru_record_set(repo, 1, 3, 1, 2, 100, 3);
Deepak Kodihallidb914672020-02-07 02:47:45 -0600521 EXPECT_EQ(first, pldm_pdr_get_record_handle(
522 repo, pldm_pdr_fru_record_set_find_by_rsi(
523 repo, 1, &terminusHdl, &entityType,
524 &entityInstanceNum, &containerId)));
525 EXPECT_EQ(second, pldm_pdr_get_record_handle(
526 repo, pldm_pdr_fru_record_set_find_by_rsi(
527 repo, 2, &terminusHdl, &entityType,
528 &entityInstanceNum, &containerId)));
529 EXPECT_EQ(third, pldm_pdr_get_record_handle(
530 repo, pldm_pdr_fru_record_set_find_by_rsi(
531 repo, 3, &terminusHdl, &entityType,
532 &entityInstanceNum, &containerId)));
crgeddes2550cee2020-05-05 17:51:28 +0000533 EXPECT_EQ(terminusHdl, 1u);
534 EXPECT_EQ(entityType, 1u);
535 EXPECT_EQ(entityInstanceNum, 2u);
536 EXPECT_EQ(containerId, 100u);
Deepak Kodihallidb914672020-02-07 02:47:45 -0600537 EXPECT_EQ(nullptr, pldm_pdr_fru_record_set_find_by_rsi(
538 repo, 4, &terminusHdl, &entityType,
539 &entityInstanceNum, &containerId));
540
541 pldm_pdr_destroy(repo);
542}
Deepak Kodihalli05787052020-03-10 01:54:08 -0500543
544TEST(EntityAssociationPDR, testInit)
545{
546 auto tree = pldm_entity_association_tree_init();
547 EXPECT_NE(tree, nullptr);
548 pldm_entity_association_tree_destroy(tree);
549}
550
551TEST(EntityAssociationPDR, testBuild)
552{
553 // 1
554 // |
555 // 2--3--4
556 // |
557 // 5--6--7
558 // | |
559 // 9 8
560
561 pldm_entity entities[9]{};
562
563 entities[0].entity_type = 1;
564 entities[1].entity_type = 2;
565 entities[2].entity_type = 2;
566 entities[3].entity_type = 3;
567 entities[4].entity_type = 4;
568 entities[5].entity_type = 5;
569 entities[6].entity_type = 5;
570 entities[7].entity_type = 6;
571 entities[8].entity_type = 7;
572
573 auto tree = pldm_entity_association_tree_init();
574
George Liu64a8f0f2021-06-12 10:56:11 +0800575 auto l1 = pldm_entity_association_tree_add(
576 tree, &entities[0], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500577 EXPECT_NE(l1, nullptr);
578 auto l2a = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +0800579 tree, &entities[1], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500580 EXPECT_NE(l2a, nullptr);
581 auto l2b = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +0800582 tree, &entities[2], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500583 EXPECT_NE(l2b, nullptr);
584 auto l2c = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +0800585 tree, &entities[3], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500586 EXPECT_NE(l2c, nullptr);
587 auto l3a = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +0800588 tree, &entities[4], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500589 EXPECT_NE(l3a, nullptr);
590 auto l3b = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +0800591 tree, &entities[5], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500592 EXPECT_NE(l3b, nullptr);
593 auto l3c = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +0800594 tree, &entities[6], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500595 EXPECT_NE(l3b, nullptr);
596 auto l4a = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +0800597 tree, &entities[7], 0xFFFF, l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500598 EXPECT_NE(l4a, nullptr);
599 auto l4b = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +0800600 tree, &entities[8], 0xFFFF, l3b, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500601 EXPECT_NE(l4b, nullptr);
602
603 EXPECT_EQ(pldm_entity_is_node_parent(l1), true);
604 EXPECT_EQ(pldm_entity_is_node_parent(l2a), true);
605 EXPECT_EQ(pldm_entity_is_node_parent(l3a), true);
606 EXPECT_EQ(pldm_entity_is_node_parent(l3b), true);
607
608 EXPECT_EQ(pldm_entity_is_node_parent(l2b), false);
609 EXPECT_EQ(pldm_entity_is_node_parent(l2c), false);
610 EXPECT_EQ(pldm_entity_is_node_parent(l3c), false);
611 EXPECT_EQ(pldm_entity_is_node_parent(l4a), false);
612 EXPECT_EQ(pldm_entity_is_node_parent(l4b), false);
613
George Liub6b3cf32021-06-07 16:30:59 +0800614 EXPECT_EQ(pldm_entity_is_exist_parent(l1), false);
George Liufe77eea2021-04-28 15:08:07 +0800615
George Liub6b3cf32021-06-07 16:30:59 +0800616 pldm_entity nodeL1 = pldm_entity_extract(l1);
617 pldm_entity parentL2a = pldm_entity_get_parent(l2a);
618 pldm_entity parentL2b = pldm_entity_get_parent(l2b);
619 pldm_entity parentL2c = pldm_entity_get_parent(l2c);
620 EXPECT_EQ(pldm_entity_is_exist_parent(l2a), true);
621 EXPECT_EQ(pldm_entity_is_exist_parent(l2b), true);
622 EXPECT_EQ(pldm_entity_is_exist_parent(l2c), true);
623 EXPECT_EQ(parentL2a.entity_type, nodeL1.entity_type);
624 EXPECT_EQ(parentL2a.entity_instance_num, nodeL1.entity_instance_num);
625 EXPECT_EQ(parentL2a.entity_container_id, nodeL1.entity_container_id);
626 EXPECT_EQ(parentL2b.entity_type, nodeL1.entity_type);
627 EXPECT_EQ(parentL2b.entity_instance_num, nodeL1.entity_instance_num);
628 EXPECT_EQ(parentL2b.entity_container_id, nodeL1.entity_container_id);
629 EXPECT_EQ(parentL2c.entity_type, nodeL1.entity_type);
630 EXPECT_EQ(parentL2c.entity_instance_num, nodeL1.entity_instance_num);
631 EXPECT_EQ(parentL2c.entity_container_id, nodeL1.entity_container_id);
George Liufe77eea2021-04-28 15:08:07 +0800632
George Liub6b3cf32021-06-07 16:30:59 +0800633 pldm_entity nodeL2a = pldm_entity_extract(l2a);
634 pldm_entity parentL3a = pldm_entity_get_parent(l3a);
635 pldm_entity parentL3b = pldm_entity_get_parent(l3b);
636 pldm_entity parentL3c = pldm_entity_get_parent(l3c);
637 EXPECT_EQ(pldm_entity_is_exist_parent(l3a), true);
638 EXPECT_EQ(pldm_entity_is_exist_parent(l3b), true);
639 EXPECT_EQ(pldm_entity_is_exist_parent(l3c), true);
640 EXPECT_EQ(parentL3a.entity_type, nodeL2a.entity_type);
641 EXPECT_EQ(parentL3a.entity_instance_num, nodeL2a.entity_instance_num);
642 EXPECT_EQ(parentL3a.entity_container_id, nodeL2a.entity_container_id);
643 EXPECT_EQ(parentL3b.entity_type, nodeL2a.entity_type);
644 EXPECT_EQ(parentL3b.entity_instance_num, nodeL2a.entity_instance_num);
645 EXPECT_EQ(parentL3b.entity_container_id, nodeL2a.entity_container_id);
646 EXPECT_EQ(parentL3c.entity_type, nodeL2a.entity_type);
647 EXPECT_EQ(parentL3c.entity_instance_num, nodeL2a.entity_instance_num);
648 EXPECT_EQ(parentL3c.entity_container_id, nodeL2a.entity_container_id);
George Liufe77eea2021-04-28 15:08:07 +0800649
George Liub6b3cf32021-06-07 16:30:59 +0800650 pldm_entity nodeL3a = pldm_entity_extract(l3a);
651 pldm_entity parentL4a = pldm_entity_get_parent(l4a);
652 EXPECT_EQ(pldm_entity_is_exist_parent(l4a), true);
653 EXPECT_EQ(parentL4a.entity_type, nodeL3a.entity_type);
654 EXPECT_EQ(parentL4a.entity_instance_num, nodeL3a.entity_instance_num);
655 EXPECT_EQ(parentL4a.entity_container_id, nodeL3a.entity_container_id);
George Liufe77eea2021-04-28 15:08:07 +0800656
George Liub6b3cf32021-06-07 16:30:59 +0800657 pldm_entity nodeL3b = pldm_entity_extract(l3b);
658 pldm_entity parentL4b = pldm_entity_get_parent(l4b);
659 EXPECT_EQ(pldm_entity_is_exist_parent(l4b), true);
660 EXPECT_EQ(parentL4b.entity_type, nodeL3b.entity_type);
661 EXPECT_EQ(parentL4b.entity_instance_num, nodeL3b.entity_instance_num);
662 EXPECT_EQ(parentL4b.entity_container_id, nodeL3b.entity_container_id);
George Liufe77eea2021-04-28 15:08:07 +0800663
Deepak Kodihalli05787052020-03-10 01:54:08 -0500664 size_t num{};
665 pldm_entity* out = nullptr;
666 pldm_entity_association_tree_visit(tree, &out, &num);
crgeddes2550cee2020-05-05 17:51:28 +0000667 EXPECT_EQ(num, 9u);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500668
crgeddes2550cee2020-05-05 17:51:28 +0000669 EXPECT_EQ(out[0].entity_type, 1u);
670 EXPECT_EQ(out[0].entity_instance_num, 1u);
671 EXPECT_EQ(out[0].entity_container_id, 0u);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500672
crgeddes2550cee2020-05-05 17:51:28 +0000673 EXPECT_EQ(out[1].entity_type, 2u);
674 EXPECT_EQ(out[1].entity_instance_num, 1u);
675 EXPECT_EQ(out[1].entity_container_id, 1u);
676 EXPECT_EQ(out[2].entity_type, 2u);
677 EXPECT_EQ(out[2].entity_instance_num, 2u);
678 EXPECT_EQ(out[2].entity_container_id, 1u);
679 EXPECT_EQ(out[3].entity_type, 3u);
680 EXPECT_EQ(out[3].entity_instance_num, 1u);
681 EXPECT_EQ(out[3].entity_container_id, 1u);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500682
crgeddes2550cee2020-05-05 17:51:28 +0000683 EXPECT_EQ(out[4].entity_type, 4u);
684 EXPECT_EQ(out[4].entity_instance_num, 1u);
685 EXPECT_EQ(out[4].entity_container_id, 2u);
686 EXPECT_EQ(out[5].entity_type, 5u);
687 EXPECT_EQ(out[5].entity_instance_num, 1u);
688 EXPECT_EQ(out[5].entity_container_id, 2u);
689 EXPECT_EQ(out[6].entity_type, 5u);
690 EXPECT_EQ(out[6].entity_instance_num, 2u);
691 EXPECT_EQ(out[6].entity_container_id, 2u);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500692
crgeddes2550cee2020-05-05 17:51:28 +0000693 EXPECT_EQ(out[7].entity_type, 7u);
694 EXPECT_EQ(out[7].entity_instance_num, 1u);
695 EXPECT_EQ(out[7].entity_container_id, 4u);
696 EXPECT_EQ(out[8].entity_type, 6u);
697 EXPECT_EQ(out[8].entity_instance_num, 1u);
698 EXPECT_EQ(out[8].entity_container_id, 3u);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500699
700 free(out);
George Liud9855ab2021-05-13 09:41:31 +0800701
702 pldm_entity p1 = pldm_entity_extract(l1);
703 EXPECT_EQ(p1.entity_type, 1u);
704 EXPECT_EQ(p1.entity_instance_num, 1u);
705 EXPECT_EQ(p1.entity_container_id, 0u);
706
707 pldm_entity p2a = pldm_entity_extract(l2a);
708 EXPECT_EQ(p2a.entity_type, 2u);
709 EXPECT_EQ(p2a.entity_instance_num, 1u);
710 EXPECT_EQ(p2a.entity_container_id, 1u);
711 pldm_entity p2b = pldm_entity_extract(l2b);
712 EXPECT_EQ(p2b.entity_type, 2u);
713 EXPECT_EQ(p2b.entity_instance_num, 2u);
714 EXPECT_EQ(p2b.entity_container_id, 1u);
715 pldm_entity p2c = pldm_entity_extract(l2c);
716 EXPECT_EQ(p2c.entity_type, 3u);
717 EXPECT_EQ(p2c.entity_instance_num, 1u);
718 EXPECT_EQ(p2c.entity_container_id, 1u);
719
720 pldm_entity p3a = pldm_entity_extract(l3a);
721 EXPECT_EQ(p3a.entity_type, 4u);
722 EXPECT_EQ(p3a.entity_instance_num, 1u);
723 EXPECT_EQ(p3a.entity_container_id, 2u);
724 pldm_entity p3b = pldm_entity_extract(l3b);
725 EXPECT_EQ(p3b.entity_type, 5u);
726 EXPECT_EQ(p3b.entity_instance_num, 1u);
727 EXPECT_EQ(p3b.entity_container_id, 2u);
728 pldm_entity p3c = pldm_entity_extract(l3c);
729 EXPECT_EQ(p3c.entity_type, 5u);
730 EXPECT_EQ(p3c.entity_instance_num, 2u);
731 EXPECT_EQ(p3c.entity_container_id, 2u);
732
733 pldm_entity p4a = pldm_entity_extract(l4a);
734 EXPECT_EQ(p4a.entity_type, 6u);
735 EXPECT_EQ(p4a.entity_instance_num, 1u);
736 EXPECT_EQ(p4a.entity_container_id, 3u);
737 pldm_entity p4b = pldm_entity_extract(l4b);
738 EXPECT_EQ(p4b.entity_type, 7u);
739 EXPECT_EQ(p4b.entity_instance_num, 1u);
740 EXPECT_EQ(p4b.entity_container_id, 4u);
741
Deepak Kodihalli05787052020-03-10 01:54:08 -0500742 pldm_entity_association_tree_destroy(tree);
743}
744
745TEST(EntityAssociationPDR, testSpecialTrees)
746{
747 pldm_entity entities[3]{};
748
749 entities[0].entity_type = 1;
750 entities[1].entity_type = 2;
751 entities[2].entity_type = 1;
752
753 // A
754 auto tree = pldm_entity_association_tree_init();
755 auto node = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +0800756 tree, &entities[0], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500757 EXPECT_NE(node, nullptr);
758 size_t num{};
759 pldm_entity* out = nullptr;
760 pldm_entity_association_tree_visit(tree, &out, &num);
crgeddes2550cee2020-05-05 17:51:28 +0000761 EXPECT_EQ(num, 1u);
762 EXPECT_EQ(out[0].entity_type, 1u);
763 EXPECT_EQ(out[0].entity_instance_num, 1u);
764 EXPECT_EQ(out[0].entity_container_id, 0u);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500765 free(out);
766 pldm_entity_association_tree_destroy(tree);
767
768 // A-A-A
769 tree = pldm_entity_association_tree_init();
George Liu64a8f0f2021-06-12 10:56:11 +0800770 node = pldm_entity_association_tree_add(tree, &entities[0], 0xFFFF, nullptr,
Deepak Kodihalli05787052020-03-10 01:54:08 -0500771 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
772 EXPECT_NE(node, nullptr);
George Liu64a8f0f2021-06-12 10:56:11 +0800773 node = pldm_entity_association_tree_add(tree, &entities[1], 0xFFFF, nullptr,
Deepak Kodihalli05787052020-03-10 01:54:08 -0500774 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
775 EXPECT_NE(node, nullptr);
George Liu64a8f0f2021-06-12 10:56:11 +0800776 node = pldm_entity_association_tree_add(tree, &entities[2], 0xFFFF, nullptr,
Deepak Kodihalli05787052020-03-10 01:54:08 -0500777 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
778 EXPECT_NE(node, nullptr);
779 pldm_entity_association_tree_visit(tree, &out, &num);
crgeddes2550cee2020-05-05 17:51:28 +0000780 EXPECT_EQ(num, 3u);
781 EXPECT_EQ(out[0].entity_type, 1u);
782 EXPECT_EQ(out[0].entity_instance_num, 1u);
783 EXPECT_EQ(out[0].entity_container_id, 0u);
784 EXPECT_EQ(out[1].entity_type, 1u);
785 EXPECT_EQ(out[1].entity_instance_num, 2u);
786 EXPECT_EQ(out[1].entity_container_id, 0u);
787 EXPECT_EQ(out[2].entity_type, 2u);
788 EXPECT_EQ(out[2].entity_instance_num, 1u);
789 EXPECT_EQ(out[2].entity_container_id, 0u);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500790 free(out);
791 pldm_entity_association_tree_destroy(tree);
792
793 // A
794 // |
795 // A
796 // |
797 // A
798 tree = pldm_entity_association_tree_init();
George Liu64a8f0f2021-06-12 10:56:11 +0800799 node = pldm_entity_association_tree_add(tree, &entities[0], 0xFFFF, nullptr,
Deepak Kodihalli05787052020-03-10 01:54:08 -0500800 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
801 EXPECT_NE(node, nullptr);
802 auto node1 = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +0800803 tree, &entities[1], 0xFFFF, node, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500804 EXPECT_NE(node1, nullptr);
805 auto node2 = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +0800806 tree, &entities[2], 0xFFFF, node1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500807 EXPECT_NE(node2, nullptr);
808 pldm_entity_association_tree_visit(tree, &out, &num);
crgeddes2550cee2020-05-05 17:51:28 +0000809 EXPECT_EQ(num, 3u);
810 EXPECT_EQ(out[0].entity_type, 1u);
811 EXPECT_EQ(out[0].entity_instance_num, 1u);
812 EXPECT_EQ(out[0].entity_container_id, 0u);
813 EXPECT_EQ(out[1].entity_type, 2u);
814 EXPECT_EQ(out[1].entity_instance_num, 1u);
815 EXPECT_EQ(out[1].entity_container_id, 1u);
816 EXPECT_EQ(out[2].entity_type, 1u);
817 EXPECT_EQ(out[2].entity_instance_num, 1u);
818 EXPECT_EQ(out[2].entity_container_id, 2u);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500819 free(out);
820 pldm_entity_association_tree_destroy(tree);
821
822 // A-A
823 // |
824 // A-A
825 tree = pldm_entity_association_tree_init();
George Liu64a8f0f2021-06-12 10:56:11 +0800826 node = pldm_entity_association_tree_add(tree, &entities[0], 0xFFFF, nullptr,
Deepak Kodihalli05787052020-03-10 01:54:08 -0500827 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
828 EXPECT_NE(node, nullptr);
George Liu64a8f0f2021-06-12 10:56:11 +0800829 node = pldm_entity_association_tree_add(tree, &entities[0], 0xFFFF, nullptr,
Deepak Kodihalli05787052020-03-10 01:54:08 -0500830 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
831 EXPECT_NE(node, nullptr);
George Liu64a8f0f2021-06-12 10:56:11 +0800832 node1 = pldm_entity_association_tree_add(tree, &entities[1], 0xFFFF, node,
Deepak Kodihalli05787052020-03-10 01:54:08 -0500833 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
834 EXPECT_NE(node1, nullptr);
George Liu64a8f0f2021-06-12 10:56:11 +0800835 node2 = pldm_entity_association_tree_add(tree, &entities[2], 0xFFFF, node,
Deepak Kodihalli05787052020-03-10 01:54:08 -0500836 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
837 EXPECT_NE(node2, nullptr);
838 pldm_entity_association_tree_visit(tree, &out, &num);
crgeddes2550cee2020-05-05 17:51:28 +0000839 EXPECT_EQ(num, 4u);
840 EXPECT_EQ(out[0].entity_type, 1u);
841 EXPECT_EQ(out[0].entity_instance_num, 1u);
842 EXPECT_EQ(out[0].entity_container_id, 0u);
843 EXPECT_EQ(out[1].entity_type, 1u);
844 EXPECT_EQ(out[1].entity_instance_num, 2u);
845 EXPECT_EQ(out[1].entity_container_id, 0u);
846 EXPECT_EQ(out[2].entity_type, 2u);
847 EXPECT_EQ(out[2].entity_instance_num, 1u);
848 EXPECT_EQ(out[2].entity_container_id, 1u);
849 EXPECT_EQ(out[3].entity_type, 1u);
850 EXPECT_EQ(out[3].entity_instance_num, 1u);
851 EXPECT_EQ(out[3].entity_container_id, 1u);
Deepak Kodihalli05787052020-03-10 01:54:08 -0500852 free(out);
853 pldm_entity_association_tree_destroy(tree);
854}
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500855
856TEST(EntityAssociationPDR, testPDR)
857{
858 // e = entity type, c = container id, i = instance num
859
860 // INPUT
861 // 1(e=1)--1a(e=2)
862 // |
863 // 2(e=2)--3(e=2)--4(e=2)--5(e=3)
864 // |
865 // 6(e=4)--7(e=5)--8(e=5)--9(e=5)
866 // | |
867 // 11(e=6) 10(e=7)
868
869 // Expected OUTPUT
870 // 1(e=1,c=0,i=1)
871 // |
872 // 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)
873 // |
874 // 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)
875 // | |
876 // 10(e=6,c=3,i=1) 11(e=7,c=4,i=1)
877 pldm_entity entities[11]{};
878
879 entities[0].entity_type = 1;
880 entities[1].entity_type = 2;
881 entities[2].entity_type = 3;
882 entities[3].entity_type = 2;
883 entities[4].entity_type = 3;
884 entities[5].entity_type = 4;
885 entities[6].entity_type = 5;
886 entities[7].entity_type = 5;
887 entities[8].entity_type = 5;
888 entities[9].entity_type = 6;
889 entities[10].entity_type = 7;
890
891 auto tree = pldm_entity_association_tree_init();
892
George Liu64a8f0f2021-06-12 10:56:11 +0800893 auto l1 = pldm_entity_association_tree_add(
894 tree, &entities[0], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500895 EXPECT_NE(l1, nullptr);
896 auto l1a = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +0800897 tree, &entities[1], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500898 EXPECT_NE(l1a, nullptr);
899
900 auto l2a = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +0800901 tree, &entities[1], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500902 EXPECT_NE(l2a, nullptr);
George Liu64a8f0f2021-06-12 10:56:11 +0800903 auto l2b = pldm_entity_association_tree_add(tree, &entities[2], 0xFFFF, l1,
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500904 PLDM_ENTITY_ASSOCIAION_LOGICAL);
905 EXPECT_NE(l2b, nullptr);
906 auto l2c = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +0800907 tree, &entities[3], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500908 EXPECT_NE(l2c, nullptr);
George Liu64a8f0f2021-06-12 10:56:11 +0800909 auto l2d = pldm_entity_association_tree_add(tree, &entities[4], 0xFFFF, l1,
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500910 PLDM_ENTITY_ASSOCIAION_LOGICAL);
911 EXPECT_NE(l2d, nullptr);
912
913 auto l3a = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +0800914 tree, &entities[5], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500915 EXPECT_NE(l3a, nullptr);
916 auto l3b = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +0800917 tree, &entities[6], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500918 EXPECT_NE(l3b, nullptr);
George Liu64a8f0f2021-06-12 10:56:11 +0800919 auto l3c = pldm_entity_association_tree_add(tree, &entities[7], 0xFFFF, l2a,
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500920 PLDM_ENTITY_ASSOCIAION_LOGICAL);
921 EXPECT_NE(l3c, nullptr);
George Liu64a8f0f2021-06-12 10:56:11 +0800922 auto l3d = pldm_entity_association_tree_add(tree, &entities[8], 0xFFFF, l2a,
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500923 PLDM_ENTITY_ASSOCIAION_LOGICAL);
924 EXPECT_NE(l3d, nullptr);
925
926 auto l4a = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +0800927 tree, &entities[9], 0xFFFF, l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500928 EXPECT_NE(l4a, nullptr);
George Liu64a8f0f2021-06-12 10:56:11 +0800929 auto l4b = pldm_entity_association_tree_add(
930 tree, &entities[10], 0xFFFF, l3b, PLDM_ENTITY_ASSOCIAION_LOGICAL);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500931 EXPECT_NE(l4b, nullptr);
932
933 EXPECT_EQ(pldm_entity_get_num_children(l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL),
934 2);
935 EXPECT_EQ(pldm_entity_get_num_children(l1, PLDM_ENTITY_ASSOCIAION_LOGICAL),
936 2);
937 EXPECT_EQ(
938 pldm_entity_get_num_children(l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL), 2);
939 EXPECT_EQ(
940 pldm_entity_get_num_children(l3b, PLDM_ENTITY_ASSOCIAION_PHYSICAL), 0);
941 EXPECT_EQ(pldm_entity_get_num_children(l3b, PLDM_ENTITY_ASSOCIAION_LOGICAL),
942 1);
943
944 auto repo = pldm_pdr_init();
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530945 pldm_entity_association_pdr_add(tree, repo, false, 1);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500946
crgeddes2550cee2020-05-05 17:51:28 +0000947 EXPECT_EQ(pldm_pdr_get_record_count(repo), 6u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500948
949 uint32_t currRecHandle{};
950 uint32_t nextRecHandle{};
951 uint8_t* data = nullptr;
952 uint32_t size{};
953 uint32_t commonSize = sizeof(struct pldm_pdr_hdr) + sizeof(uint16_t) +
954 sizeof(uint8_t) + sizeof(pldm_entity) +
955 sizeof(uint8_t);
956
957 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
958 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
959 l1, PLDM_ENTITY_ASSOCIAION_LOGICAL) *
960 sizeof(pldm_entity)));
961 uint8_t* start = data;
962 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
crgeddes2550cee2020-05-05 17:51:28 +0000963 EXPECT_EQ(le32toh(hdr->record_handle), 1u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500964 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500965 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500966 start += sizeof(pldm_pdr_hdr);
967 uint16_t* containerId = reinterpret_cast<uint16_t*>(start);
crgeddes2550cee2020-05-05 17:51:28 +0000968 EXPECT_EQ(le16toh(*containerId), 1u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500969 start += sizeof(uint16_t);
970 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_LOGICAL);
971 start += sizeof(uint8_t);
972 pldm_entity* entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +0000973 EXPECT_EQ(le16toh(entity->entity_type), 1u);
974 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
975 EXPECT_EQ(le16toh(entity->entity_container_id), 0u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500976 start += sizeof(pldm_entity);
977 EXPECT_EQ(*start,
978 pldm_entity_get_num_children(l1, PLDM_ENTITY_ASSOCIAION_LOGICAL));
979 start += sizeof(uint8_t);
980 entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +0000981 EXPECT_EQ(le16toh(entity->entity_type), 3u);
982 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
983 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500984 start += sizeof(pldm_entity);
985 entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +0000986 EXPECT_EQ(le16toh(entity->entity_type), 3u);
987 EXPECT_EQ(le16toh(entity->entity_instance_num), 2u);
988 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500989 start += sizeof(pldm_entity);
990
991 currRecHandle = nextRecHandle;
992
993 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
994 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
995 l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL) *
996 sizeof(pldm_entity)));
997 start = data;
998 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
crgeddes2550cee2020-05-05 17:51:28 +0000999 EXPECT_EQ(le32toh(hdr->record_handle), 2u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001000 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -05001001 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001002 start += sizeof(pldm_pdr_hdr);
1003 containerId = reinterpret_cast<uint16_t*>(start);
crgeddes2550cee2020-05-05 17:51:28 +00001004 EXPECT_EQ(le16toh(*containerId), 1u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001005 start += sizeof(uint16_t);
1006 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1007 start += sizeof(uint8_t);
1008 entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +00001009 EXPECT_EQ(le16toh(entity->entity_type), 1u);
1010 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1011 EXPECT_EQ(le16toh(entity->entity_container_id), 0u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001012 start += sizeof(pldm_entity);
1013 EXPECT_EQ(*start, pldm_entity_get_num_children(
1014 l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL));
1015 start += sizeof(uint8_t);
1016 entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +00001017 EXPECT_EQ(le16toh(entity->entity_type), 2u);
1018 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1019 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001020 start += sizeof(pldm_entity);
1021 entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +00001022 EXPECT_EQ(le16toh(entity->entity_type), 2u);
1023 EXPECT_EQ(le16toh(entity->entity_instance_num), 2u);
1024 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001025 start += sizeof(pldm_entity);
1026
1027 currRecHandle = nextRecHandle;
1028
1029 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1030 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1031 l2a, PLDM_ENTITY_ASSOCIAION_LOGICAL) *
1032 sizeof(pldm_entity)));
1033 start = data;
1034 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
crgeddes2550cee2020-05-05 17:51:28 +00001035 EXPECT_EQ(le32toh(hdr->record_handle), 3u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001036 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -05001037 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001038 start += sizeof(pldm_pdr_hdr);
1039 containerId = reinterpret_cast<uint16_t*>(start);
crgeddes2550cee2020-05-05 17:51:28 +00001040 EXPECT_EQ(le16toh(*containerId), 2u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001041 start += sizeof(uint16_t);
1042 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_LOGICAL);
1043 start += sizeof(uint8_t);
1044 entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +00001045 EXPECT_EQ(le16toh(entity->entity_type), 2u);
1046 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1047 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001048 start += sizeof(pldm_entity);
1049 EXPECT_EQ(*start, pldm_entity_get_num_children(
1050 l2a, PLDM_ENTITY_ASSOCIAION_LOGICAL));
1051 start += sizeof(uint8_t);
1052 entity = reinterpret_cast<pldm_entity*>(start);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -05001053 EXPECT_EQ(le16toh(entity->entity_type), 5);
crgeddes2550cee2020-05-05 17:51:28 +00001054 EXPECT_EQ(le16toh(entity->entity_instance_num), 2u);
1055 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001056 start += sizeof(pldm_entity);
1057 entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +00001058 EXPECT_EQ(le16toh(entity->entity_type), 5u);
1059 EXPECT_EQ(le16toh(entity->entity_instance_num), 3u);
1060 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001061 start += sizeof(pldm_entity);
1062
1063 currRecHandle = nextRecHandle;
1064
1065 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1066 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1067 l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL) *
1068 sizeof(pldm_entity)));
1069 start = data;
1070 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
crgeddes2550cee2020-05-05 17:51:28 +00001071 EXPECT_EQ(le32toh(hdr->record_handle), 4u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001072 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -05001073 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001074 start += sizeof(pldm_pdr_hdr);
1075 containerId = reinterpret_cast<uint16_t*>(start);
crgeddes2550cee2020-05-05 17:51:28 +00001076 EXPECT_EQ(le16toh(*containerId), 2u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001077 start += sizeof(uint16_t);
1078 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1079 start += sizeof(uint8_t);
1080 entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +00001081 EXPECT_EQ(le16toh(entity->entity_type), 2u);
1082 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1083 EXPECT_EQ(le16toh(entity->entity_container_id), 1u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001084 start += sizeof(pldm_entity);
1085 EXPECT_EQ(*start, pldm_entity_get_num_children(
1086 l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL));
1087 start += sizeof(uint8_t);
1088 entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +00001089 EXPECT_EQ(le16toh(entity->entity_type), 4u);
1090 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1091 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001092 start += sizeof(pldm_entity);
1093 entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +00001094 EXPECT_EQ(le16toh(entity->entity_type), 5u);
1095 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1096 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001097 start += sizeof(pldm_entity);
1098
1099 currRecHandle = nextRecHandle;
1100
1101 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1102 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1103 l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL) *
1104 sizeof(pldm_entity)));
1105 start = data;
1106 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
crgeddes2550cee2020-05-05 17:51:28 +00001107 EXPECT_EQ(le32toh(hdr->record_handle), 5u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001108 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -05001109 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001110 start += sizeof(pldm_pdr_hdr);
1111 containerId = reinterpret_cast<uint16_t*>(start);
crgeddes2550cee2020-05-05 17:51:28 +00001112 EXPECT_EQ(le16toh(*containerId), 3u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001113 start += sizeof(uint16_t);
1114 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1115 start += sizeof(uint8_t);
1116 entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +00001117 EXPECT_EQ(le16toh(entity->entity_type), 4u);
1118 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1119 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001120 start += sizeof(pldm_entity);
1121 EXPECT_EQ(*start, pldm_entity_get_num_children(
1122 l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL));
1123 start += sizeof(uint8_t);
1124 entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +00001125 EXPECT_EQ(le16toh(entity->entity_type), 6u);
1126 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1127 EXPECT_EQ(le16toh(entity->entity_container_id), 3u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001128 start += sizeof(pldm_entity);
1129
1130 currRecHandle = nextRecHandle;
1131
1132 pldm_pdr_find_record(repo, currRecHandle, &data, &size, &nextRecHandle);
1133 EXPECT_EQ(size, commonSize + (pldm_entity_get_num_children(
1134 l3b, PLDM_ENTITY_ASSOCIAION_LOGICAL) *
1135 sizeof(pldm_entity)));
1136 start = data;
1137 hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
crgeddes2550cee2020-05-05 17:51:28 +00001138 EXPECT_EQ(le32toh(hdr->record_handle), 6u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001139 EXPECT_EQ(hdr->type, PLDM_PDR_ENTITY_ASSOCIATION);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -05001140 EXPECT_EQ(le16toh(hdr->length), size - sizeof(struct pldm_pdr_hdr));
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001141 start += sizeof(pldm_pdr_hdr);
1142 containerId = reinterpret_cast<uint16_t*>(start);
crgeddes2550cee2020-05-05 17:51:28 +00001143 EXPECT_EQ(le16toh(*containerId), 4u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001144 start += sizeof(uint16_t);
1145 EXPECT_EQ(*start, PLDM_ENTITY_ASSOCIAION_LOGICAL);
1146 start += sizeof(uint8_t);
1147 entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +00001148 EXPECT_EQ(le16toh(entity->entity_type), 5u);
1149 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1150 EXPECT_EQ(le16toh(entity->entity_container_id), 2u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001151 start += sizeof(pldm_entity);
1152 EXPECT_EQ(*start, pldm_entity_get_num_children(
1153 l3b, PLDM_ENTITY_ASSOCIAION_LOGICAL));
1154 start += sizeof(uint8_t);
1155 entity = reinterpret_cast<pldm_entity*>(start);
crgeddes2550cee2020-05-05 17:51:28 +00001156 EXPECT_EQ(le16toh(entity->entity_type), 7u);
1157 EXPECT_EQ(le16toh(entity->entity_instance_num), 1u);
1158 EXPECT_EQ(le16toh(entity->entity_container_id), 4u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001159
crgeddes2550cee2020-05-05 17:51:28 +00001160 EXPECT_EQ(nextRecHandle, 0u);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -05001161
1162 pldm_pdr_destroy(repo);
1163 pldm_entity_association_tree_destroy(tree);
1164}
Deepak Kodihalli3b28ba82020-03-30 07:39:47 -05001165
1166TEST(EntityAssociationPDR, testFind)
1167{
1168 // 1
1169 // |
1170 // 2--3--4
1171 // |
1172 // 5--6--7
1173 // | |
1174 // 8 9
1175
1176 pldm_entity entities[9]{};
1177
1178 entities[0].entity_type = 1;
1179 entities[1].entity_type = 2;
1180 entities[2].entity_type = 2;
1181 entities[3].entity_type = 3;
1182 entities[4].entity_type = 4;
1183 entities[5].entity_type = 5;
1184 entities[6].entity_type = 5;
1185 entities[7].entity_type = 6;
1186 entities[8].entity_type = 7;
1187
1188 auto tree = pldm_entity_association_tree_init();
1189
George Liu64a8f0f2021-06-12 10:56:11 +08001190 auto l1 = pldm_entity_association_tree_add(
1191 tree, &entities[0], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli3b28ba82020-03-30 07:39:47 -05001192 EXPECT_NE(l1, nullptr);
1193 auto l2a = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +08001194 tree, &entities[1], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli3b28ba82020-03-30 07:39:47 -05001195 EXPECT_NE(l2a, nullptr);
1196 auto l2b = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +08001197 tree, &entities[2], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli3b28ba82020-03-30 07:39:47 -05001198 EXPECT_NE(l2b, nullptr);
1199 auto l2c = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +08001200 tree, &entities[3], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli3b28ba82020-03-30 07:39:47 -05001201 EXPECT_NE(l2c, nullptr);
1202 auto l3a = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +08001203 tree, &entities[4], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli3b28ba82020-03-30 07:39:47 -05001204 EXPECT_NE(l3a, nullptr);
1205 auto l3b = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +08001206 tree, &entities[5], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli3b28ba82020-03-30 07:39:47 -05001207 EXPECT_NE(l3b, nullptr);
1208 auto l3c = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +08001209 tree, &entities[6], 0xFFFF, l2a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli3b28ba82020-03-30 07:39:47 -05001210 EXPECT_NE(l3c, nullptr);
1211 auto l4a = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +08001212 tree, &entities[7], 0xFFFF, l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli3b28ba82020-03-30 07:39:47 -05001213 EXPECT_NE(l4a, nullptr);
1214 auto l4b = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +08001215 tree, &entities[8], 0xFFFF, l3b, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Deepak Kodihalli3b28ba82020-03-30 07:39:47 -05001216 EXPECT_NE(l4b, nullptr);
1217
1218 pldm_entity entity{};
1219
1220 entity.entity_type = 1;
1221 entity.entity_instance_num = 1;
1222 auto result = pldm_entity_association_tree_find(tree, &entity);
1223 EXPECT_EQ(result, l1);
1224 EXPECT_EQ(entity.entity_container_id, 0);
1225
1226 entity.entity_type = 2;
1227 entity.entity_instance_num = 1;
1228 result = pldm_entity_association_tree_find(tree, &entity);
1229 EXPECT_EQ(result, l2a);
1230 EXPECT_EQ(entity.entity_container_id, 1);
1231 entity.entity_type = 2;
1232 entity.entity_instance_num = 2;
1233 result = pldm_entity_association_tree_find(tree, &entity);
1234 EXPECT_EQ(result, l2b);
1235 EXPECT_EQ(entity.entity_container_id, 1);
1236 entity.entity_type = 3;
1237 entity.entity_instance_num = 1;
1238 result = pldm_entity_association_tree_find(tree, &entity);
1239 EXPECT_EQ(result, l2c);
1240 EXPECT_EQ(entity.entity_container_id, 1);
1241
1242 entity.entity_type = 7;
1243 entity.entity_instance_num = 1;
1244 result = pldm_entity_association_tree_find(tree, &entity);
1245 EXPECT_EQ(result, l4b);
1246 EXPECT_EQ(entity.entity_container_id, 4);
1247
1248 pldm_entity_association_tree_destroy(tree);
1249}
1250
Sampa Misrac073a202021-05-08 10:56:05 -05001251TEST(EntityAssociationPDR, testCopyTree)
1252{
1253 pldm_entity entities[4]{};
1254 entities[0].entity_type = 1;
1255 entities[1].entity_type = 2;
1256 entities[2].entity_type = 2;
1257 entities[3].entity_type = 3;
1258
1259 auto orgTree = pldm_entity_association_tree_init();
1260 auto newTree = pldm_entity_association_tree_init();
George Liu64a8f0f2021-06-12 10:56:11 +08001261 auto l1 =
1262 pldm_entity_association_tree_add(orgTree, &entities[0], 0xFFFF, nullptr,
1263 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Sampa Misrac073a202021-05-08 10:56:05 -05001264 EXPECT_NE(l1, nullptr);
1265 auto l2a = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +08001266 orgTree, &entities[1], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Sampa Misrac073a202021-05-08 10:56:05 -05001267 EXPECT_NE(l2a, nullptr);
1268 auto l2b = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +08001269 orgTree, &entities[2], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Sampa Misrac073a202021-05-08 10:56:05 -05001270 EXPECT_NE(l2b, nullptr);
1271 auto l2c = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +08001272 orgTree, &entities[3], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Sampa Misrac073a202021-05-08 10:56:05 -05001273 EXPECT_NE(l2c, nullptr);
1274 size_t orgNum{};
1275 pldm_entity* orgOut = nullptr;
1276 pldm_entity_association_tree_visit(orgTree, &orgOut, &orgNum);
1277 EXPECT_EQ(orgNum, 4u);
1278
1279 pldm_entity_association_tree_copy_root(orgTree, newTree);
1280 size_t newNum{};
1281 pldm_entity* newOut = nullptr;
1282 pldm_entity_association_tree_visit(newTree, &newOut, &newNum);
1283 EXPECT_EQ(newNum, orgNum);
1284 EXPECT_EQ(newOut[0].entity_type, 1u);
1285 EXPECT_EQ(newOut[0].entity_instance_num, 1u);
1286 EXPECT_EQ(newOut[0].entity_container_id, 0u);
1287 free(orgOut);
1288 free(newOut);
1289 pldm_entity_association_tree_destroy(orgTree);
1290 pldm_entity_association_tree_destroy(newTree);
1291}
1292
Deepak Kodihalli3b28ba82020-03-30 07:39:47 -05001293TEST(EntityAssociationPDR, testExtract)
1294{
1295 std::vector<uint8_t> pdr{};
1296 pdr.resize(sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_entity_association) +
1297 sizeof(pldm_entity) * 4);
1298 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(pdr.data());
1299 hdr->type = PLDM_PDR_ENTITY_ASSOCIATION;
1300 hdr->length =
1301 htole16(sizeof(pldm_pdr_entity_association) + sizeof(pldm_entity) * 4);
1302
1303 pldm_pdr_entity_association* e =
1304 reinterpret_cast<pldm_pdr_entity_association*>(pdr.data() +
1305 sizeof(pldm_pdr_hdr));
1306 e->container_id = htole16(1);
1307 e->num_children = 5;
1308 e->container.entity_type = htole16(1);
1309 e->container.entity_instance_num = htole16(1);
1310 e->container.entity_container_id = htole16(0);
1311
1312 pldm_entity* entity = e->children;
1313 entity->entity_type = htole16(2);
1314 entity->entity_instance_num = htole16(1);
1315 entity->entity_container_id = htole16(1);
1316 ++entity;
1317 entity->entity_type = htole16(3);
1318 entity->entity_instance_num = htole16(1);
1319 entity->entity_container_id = htole16(1);
1320 ++entity;
1321 entity->entity_type = htole16(4);
1322 entity->entity_instance_num = htole16(1);
1323 entity->entity_container_id = htole16(1);
1324 ++entity;
1325 entity->entity_type = htole16(5);
1326 entity->entity_instance_num = htole16(1);
1327 entity->entity_container_id = htole16(1);
1328 ++entity;
1329 entity->entity_type = htole16(6);
1330 entity->entity_instance_num = htole16(1);
1331 entity->entity_container_id = htole16(1);
1332
1333 size_t num{};
1334 pldm_entity* out = nullptr;
1335 pldm_entity_association_pdr_extract(pdr.data(), pdr.size(), &num, &out);
crgeddes2550cee2020-05-05 17:51:28 +00001336 EXPECT_EQ(num, (unsigned)e->num_children + 1);
1337 EXPECT_EQ(out[0].entity_type, 1u);
1338 EXPECT_EQ(out[0].entity_instance_num, 1u);
1339 EXPECT_EQ(out[0].entity_container_id, 0u);
1340 EXPECT_EQ(out[1].entity_type, 2u);
1341 EXPECT_EQ(out[1].entity_instance_num, 1u);
1342 EXPECT_EQ(out[1].entity_container_id, 1u);
1343 EXPECT_EQ(out[2].entity_type, 3u);
1344 EXPECT_EQ(out[2].entity_instance_num, 1u);
1345 EXPECT_EQ(out[2].entity_container_id, 1u);
1346 EXPECT_EQ(out[3].entity_type, 4u);
1347 EXPECT_EQ(out[3].entity_instance_num, 1u);
1348 EXPECT_EQ(out[3].entity_container_id, 1u);
1349 EXPECT_EQ(out[4].entity_type, 5u);
1350 EXPECT_EQ(out[4].entity_instance_num, 1u);
1351 EXPECT_EQ(out[4].entity_container_id, 1u);
1352 EXPECT_EQ(out[5].entity_type, 6u);
1353 EXPECT_EQ(out[5].entity_instance_num, 1u);
1354 EXPECT_EQ(out[5].entity_container_id, 1u);
Deepak Kodihalli3b28ba82020-03-30 07:39:47 -05001355
1356 free(out);
1357}
George Liu6a6c3ab2021-06-22 16:12:42 +08001358
1359TEST(EntityAssociationPDR, testGetChildren)
1360{
1361 pldm_entity entities[4]{};
1362 entities[0].entity_type = 1;
1363 entities[1].entity_type = 2;
1364 entities[2].entity_type = 2;
1365 entities[3].entity_type = 3;
1366
1367 auto tree = pldm_entity_association_tree_init();
George Liu64a8f0f2021-06-12 10:56:11 +08001368 auto l1 = pldm_entity_association_tree_add(
1369 tree, &entities[0], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
George Liu6a6c3ab2021-06-22 16:12:42 +08001370 EXPECT_NE(l1, nullptr);
1371 auto l2a = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +08001372 tree, &entities[1], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
George Liu6a6c3ab2021-06-22 16:12:42 +08001373 EXPECT_NE(l2a, nullptr);
1374 auto l2b = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +08001375 tree, &entities[2], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
George Liu6a6c3ab2021-06-22 16:12:42 +08001376 EXPECT_NE(l2b, nullptr);
1377 auto l2c = pldm_entity_association_tree_add(
George Liu64a8f0f2021-06-12 10:56:11 +08001378 tree, &entities[3], 0xFFFF, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
George Liu6a6c3ab2021-06-22 16:12:42 +08001379 EXPECT_NE(l2c, nullptr);
1380
1381 pldm_entity et1;
1382 et1.entity_type = 2;
1383 et1.entity_instance_num = 1;
1384 EXPECT_EQ(true, pldm_is_current_parent_child(l1, &et1));
1385
1386 pldm_entity et2;
1387 et2.entity_type = 2;
1388 et2.entity_instance_num = 2;
1389 EXPECT_EQ(true, pldm_is_current_parent_child(l1, &et2));
1390
1391 pldm_entity et3;
1392 et3.entity_type = 2;
1393 et3.entity_instance_num = 3;
1394 EXPECT_EQ(false, pldm_is_current_parent_child(l1, &et3));
1395
1396 pldm_entity_association_tree_destroy(tree);
George Liu64a8f0f2021-06-12 10:56:11 +08001397}
1398
1399TEST(EntityAssociationPDR, testEntityInstanceNumber)
1400{
1401 pldm_entity entities[9]{};
1402
1403 entities[0].entity_type = 1;
1404 entities[1].entity_type = 2;
1405 entities[2].entity_type = 2;
1406 entities[3].entity_type = 2;
1407 entities[4].entity_type = 2;
1408 entities[5].entity_type = 2;
1409 entities[6].entity_type = 2;
1410 entities[7].entity_type = 3;
1411 entities[8].entity_type = 3;
1412
1413 auto tree = pldm_entity_association_tree_init();
1414 auto repo = pldm_pdr_init();
1415
1416 uint16_t terminusHdl{};
1417 uint16_t entityType{};
1418 uint16_t entityInstanceNum{};
1419 uint16_t containerId{};
1420
1421 auto node = pldm_entity_association_tree_add(
1422 tree, &entities[0], 0xFFFF, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1423 EXPECT_NE(node, nullptr);
1424
1425 auto l1 = pldm_entity_association_tree_add(tree, &entities[1], 63, node,
1426 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1427 auto first = pldm_pdr_add_fru_record_set(
1428 repo, 1, 1, entities[1].entity_type, entities[1].entity_instance_num,
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -06001429 entities[1].entity_container_id, 1);
George Liu64a8f0f2021-06-12 10:56:11 +08001430 EXPECT_NE(l1, nullptr);
1431 EXPECT_EQ(entities[1].entity_instance_num, 63);
1432 EXPECT_EQ(first, pldm_pdr_get_record_handle(
1433 repo, pldm_pdr_fru_record_set_find_by_rsi(
1434 repo, 1, &terminusHdl, &entityType,
1435 &entityInstanceNum, &containerId)));
1436 EXPECT_EQ(entityType, 2);
1437 EXPECT_EQ(entityInstanceNum, 63);
1438
1439 auto l2 = pldm_entity_association_tree_add(tree, &entities[2], 37, node,
1440 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1441 auto second = pldm_pdr_add_fru_record_set(
1442 repo, 1, 2, entities[2].entity_type, entities[2].entity_instance_num,
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -06001443 entities[2].entity_container_id, 2);
George Liu64a8f0f2021-06-12 10:56:11 +08001444 EXPECT_NE(l2, nullptr);
1445 EXPECT_EQ(entities[2].entity_instance_num, 37);
1446 EXPECT_EQ(second, pldm_pdr_get_record_handle(
1447 repo, pldm_pdr_fru_record_set_find_by_rsi(
1448 repo, 2, &terminusHdl, &entityType,
1449 &entityInstanceNum, &containerId)));
1450 EXPECT_EQ(entityType, 2);
1451 EXPECT_EQ(entityInstanceNum, 37);
1452
1453 auto l3 = pldm_entity_association_tree_add(tree, &entities[3], 44, node,
1454 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1455 auto third = pldm_pdr_add_fru_record_set(
1456 repo, 1, 3, entities[3].entity_type, entities[3].entity_instance_num,
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -06001457 entities[3].entity_container_id, 3);
George Liu64a8f0f2021-06-12 10:56:11 +08001458 EXPECT_NE(l3, nullptr);
1459 EXPECT_EQ(entities[3].entity_instance_num, 44);
1460 EXPECT_EQ(third, pldm_pdr_get_record_handle(
1461 repo, pldm_pdr_fru_record_set_find_by_rsi(
1462 repo, 3, &terminusHdl, &entityType,
1463 &entityInstanceNum, &containerId)));
1464 EXPECT_EQ(entityType, 2);
1465 EXPECT_EQ(entityInstanceNum, 44);
1466
1467 auto l4 = pldm_entity_association_tree_add(tree, &entities[4], 89, node,
1468 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1469 auto fourth = pldm_pdr_add_fru_record_set(
1470 repo, 1, 4, entities[4].entity_type, entities[4].entity_instance_num,
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -06001471 entities[4].entity_container_id, 4);
George Liu64a8f0f2021-06-12 10:56:11 +08001472 EXPECT_NE(l4, nullptr);
1473 EXPECT_EQ(entities[4].entity_instance_num, 89);
1474 EXPECT_EQ(fourth, pldm_pdr_get_record_handle(
1475 repo, pldm_pdr_fru_record_set_find_by_rsi(
1476 repo, 4, &terminusHdl, &entityType,
1477 &entityInstanceNum, &containerId)));
1478 EXPECT_EQ(entityType, 2);
1479 EXPECT_EQ(entityInstanceNum, 89);
1480
1481 auto l5 = pldm_entity_association_tree_add(tree, &entities[5], 0xFFFF, node,
1482 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1483 auto fifth = pldm_pdr_add_fru_record_set(
1484 repo, 1, 5, entities[5].entity_type, entities[5].entity_instance_num,
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -06001485 entities[5].entity_container_id, 5);
George Liu64a8f0f2021-06-12 10:56:11 +08001486 EXPECT_NE(l5, nullptr);
1487 EXPECT_EQ(entities[5].entity_instance_num, 90);
1488 EXPECT_EQ(fifth, pldm_pdr_get_record_handle(
1489 repo, pldm_pdr_fru_record_set_find_by_rsi(
1490 repo, 5, &terminusHdl, &entityType,
1491 &entityInstanceNum, &containerId)));
1492 EXPECT_EQ(entityType, 2);
1493 EXPECT_EQ(entityInstanceNum, 90);
1494
1495 auto l6 = pldm_entity_association_tree_add(tree, &entities[6], 90, node,
1496 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1497 EXPECT_EQ(l6, nullptr);
1498
1499 auto l7 = pldm_entity_association_tree_add(tree, &entities[7], 100, l1,
1500 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1501 auto seventh = pldm_pdr_add_fru_record_set(
1502 repo, 1, 7, entities[7].entity_type, entities[7].entity_instance_num,
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -06001503 entities[7].entity_container_id, 7);
George Liu64a8f0f2021-06-12 10:56:11 +08001504 EXPECT_NE(l7, nullptr);
1505 EXPECT_EQ(entities[7].entity_instance_num, 100);
1506 EXPECT_EQ(seventh, pldm_pdr_get_record_handle(
1507 repo, pldm_pdr_fru_record_set_find_by_rsi(
1508 repo, 7, &terminusHdl, &entityType,
1509 &entityInstanceNum, &containerId)));
1510 EXPECT_EQ(entityType, 3);
1511 EXPECT_EQ(entityInstanceNum, 100);
1512
1513 auto l8 = pldm_entity_association_tree_add(tree, &entities[8], 100, l2,
1514 PLDM_ENTITY_ASSOCIAION_PHYSICAL);
1515 auto eighth = pldm_pdr_add_fru_record_set(
1516 repo, 1, 8, entities[8].entity_type, entities[8].entity_instance_num,
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -06001517 entities[8].entity_container_id, 8);
George Liu64a8f0f2021-06-12 10:56:11 +08001518 EXPECT_NE(l8, nullptr);
1519 EXPECT_EQ(entities[8].entity_instance_num, 100);
1520 EXPECT_EQ(eighth, pldm_pdr_get_record_handle(
1521 repo, pldm_pdr_fru_record_set_find_by_rsi(
1522 repo, 8, &terminusHdl, &entityType,
1523 &entityInstanceNum, &containerId)));
1524 EXPECT_EQ(entityType, 3);
1525 EXPECT_EQ(entityInstanceNum, 100);
1526
1527 pldm_pdr_destroy(repo);
1528 pldm_entity_association_tree_destroy(tree);
1529}