blob: 19988235e3cec20541e2b73fb1633ae738aacf54 [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();
11 EXPECT_EQ(pldm_pdr_get_record_count(repo), 0);
12 EXPECT_EQ(pldm_pdr_get_repo_size(repo), 0);
13 pldm_pdr_destroy(repo);
14}
15
16TEST(PDRUpdate, testAdd)
17{
18 auto repo = pldm_pdr_init();
19
20 std::array<uint8_t, 10> data{};
21 auto handle = pldm_pdr_add(repo, data.data(), data.size(), 0);
22 EXPECT_EQ(handle, 1);
23 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1);
24 EXPECT_EQ(pldm_pdr_get_repo_size(repo), data.size());
25
26 handle = pldm_pdr_add(repo, data.data(), data.size(), 0);
27 EXPECT_EQ(handle, 2);
28 handle = pldm_pdr_add(repo, data.data(), data.size(), 0);
29 EXPECT_EQ(handle, 3);
Lei YU7cfeb8e2020-02-27 18:24:18 +080030 handle = pldm_pdr_add(repo, data.data(), data.size(), htole32(0xdeeddeed));
Lei YU90ff3ed2020-03-02 11:16:51 +080031 EXPECT_EQ(handle, htole32(0xdeeddeed));
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -060032 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4);
33 EXPECT_EQ(pldm_pdr_get_repo_size(repo), data.size() * 4);
34
35 pldm_pdr_destroy(repo);
36}
37
38TEST(PDRAccess, testGet)
39{
40 auto repo = pldm_pdr_init();
41
42 std::array<uint32_t, 10> in{100, 345, 3, 6, 89, 0, 11, 45, 23434, 123123};
43 pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in.data()), sizeof(in), 1);
44 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1);
45 EXPECT_EQ(pldm_pdr_get_repo_size(repo), sizeof(in));
46 uint32_t size{};
47 uint32_t nextRecHdl{};
48 uint8_t* outData = nullptr;
49 auto hdl = pldm_pdr_find_record(repo, 0, &outData, &size, &nextRecHdl);
50 EXPECT_NE(hdl, nullptr);
51 EXPECT_EQ(size, sizeof(in));
52 EXPECT_EQ(nextRecHdl, 0);
53 EXPECT_EQ(memcmp(outData, in.data(), sizeof(in)), 0);
54 outData = nullptr;
55
56 auto hdl2 = pldm_pdr_find_record(repo, 1, &outData, &size, &nextRecHdl);
57 EXPECT_EQ(hdl, hdl2);
58 EXPECT_NE(hdl, nullptr);
59 EXPECT_EQ(size, sizeof(in));
60 EXPECT_EQ(nextRecHdl, 0);
61 EXPECT_EQ(memcmp(outData, in.data(), sizeof(in)), 0);
62 outData = nullptr;
63
Lei YU7cfeb8e2020-02-27 18:24:18 +080064 hdl = pldm_pdr_find_record(repo, htole32(0xdeaddead), &outData, &size,
65 &nextRecHdl);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -060066 EXPECT_EQ(hdl, nullptr);
67 EXPECT_EQ(size, 0);
68 EXPECT_EQ(nextRecHdl, 0);
69 EXPECT_EQ(outData, nullptr);
70 outData = nullptr;
71
72 std::array<uint32_t, 10> in2{1000, 3450, 30, 60, 890,
73 0, 110, 450, 234034, 123123};
74 pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in2.data()), sizeof(in2), 2);
75 pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in2.data()), sizeof(in2), 3);
76 pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in2.data()), sizeof(in2), 4);
77 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4);
78 EXPECT_EQ(pldm_pdr_get_repo_size(repo), sizeof(in2) * 4);
79 hdl = pldm_pdr_find_record(repo, 0, &outData, &size, &nextRecHdl);
80 EXPECT_NE(hdl, nullptr);
81 EXPECT_EQ(size, sizeof(in));
82 EXPECT_EQ(nextRecHdl, 2);
83 EXPECT_EQ(memcmp(outData, in.data(), sizeof(in)), 0);
84 outData = nullptr;
85 hdl2 = pldm_pdr_find_record(repo, 1, &outData, &size, &nextRecHdl);
86 EXPECT_EQ(hdl, hdl2);
87 EXPECT_NE(hdl, nullptr);
88 EXPECT_EQ(size, sizeof(in));
89 EXPECT_EQ(nextRecHdl, 2);
90 EXPECT_EQ(memcmp(outData, in.data(), sizeof(in)), 0);
91 outData = nullptr;
92 hdl = pldm_pdr_find_record(repo, 2, &outData, &size, &nextRecHdl);
93 EXPECT_NE(hdl, nullptr);
94 EXPECT_EQ(size, sizeof(in2));
95 EXPECT_EQ(nextRecHdl, 3);
96 EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
97 outData = nullptr;
98 hdl = pldm_pdr_find_record(repo, 3, &outData, &size, &nextRecHdl);
99 EXPECT_NE(hdl, nullptr);
100 EXPECT_EQ(size, sizeof(in2));
101 EXPECT_EQ(nextRecHdl, 4);
102 EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
103 outData = nullptr;
104 hdl = pldm_pdr_find_record(repo, 4, &outData, &size, &nextRecHdl);
105 EXPECT_NE(hdl, nullptr);
106 EXPECT_EQ(size, sizeof(in2));
107 EXPECT_EQ(nextRecHdl, 0);
108 EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
109 outData = nullptr;
110
111 pldm_pdr_destroy(repo);
112}
113
114TEST(PDRAccess, testGetNext)
115{
116 auto repo = pldm_pdr_init();
117
118 std::array<uint32_t, 10> in{100, 345, 3, 6, 89, 0, 11, 45, 23434, 123123};
119 pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in.data()), sizeof(in), 1);
120 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1);
121 EXPECT_EQ(pldm_pdr_get_repo_size(repo), sizeof(in));
122 uint32_t size{};
123 uint32_t nextRecHdl{};
124 uint8_t* outData = nullptr;
125 auto hdl = pldm_pdr_find_record(repo, 0, &outData, &size, &nextRecHdl);
126 EXPECT_NE(hdl, nullptr);
127 EXPECT_EQ(size, sizeof(in));
128 EXPECT_EQ(nextRecHdl, 0);
129 EXPECT_EQ(memcmp(outData, in.data(), sizeof(in)), 0);
130 outData = nullptr;
131
132 std::array<uint32_t, 10> in2{1000, 3450, 30, 60, 890,
133 0, 110, 450, 234034, 123123};
134 pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in2.data()), sizeof(in2), 2);
135 pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in2.data()), sizeof(in2), 3);
136 pldm_pdr_add(repo, reinterpret_cast<uint8_t*>(in2.data()), sizeof(in2), 4);
137 EXPECT_EQ(pldm_pdr_get_record_count(repo), 4);
138 EXPECT_EQ(pldm_pdr_get_repo_size(repo), sizeof(in2) * 4);
139 hdl = pldm_pdr_get_next_record(repo, hdl, &outData, &size, &nextRecHdl);
140 EXPECT_NE(hdl, nullptr);
141 EXPECT_EQ(size, sizeof(in2));
142 EXPECT_EQ(nextRecHdl, 3);
143 EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
144 outData = nullptr;
145 hdl = pldm_pdr_get_next_record(repo, hdl, &outData, &size, &nextRecHdl);
146 EXPECT_NE(hdl, nullptr);
147 EXPECT_EQ(size, sizeof(in2));
148 EXPECT_EQ(nextRecHdl, 4);
149 EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
150 outData = nullptr;
151 hdl = pldm_pdr_get_next_record(repo, hdl, &outData, &size, &nextRecHdl);
152 EXPECT_NE(hdl, nullptr);
153 EXPECT_EQ(size, sizeof(in2));
154 EXPECT_EQ(nextRecHdl, 0);
155 EXPECT_EQ(memcmp(outData, in2.data(), sizeof(in2)), 0);
156 outData = nullptr;
157
158 pldm_pdr_destroy(repo);
159}
160
161TEST(PDRAccess, testFindByType)
162{
163 auto repo = pldm_pdr_init();
164
165 std::array<uint8_t, sizeof(pldm_pdr_hdr)> data{};
166 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(data.data());
167 hdr->type = 1;
168 auto first = pldm_pdr_add(repo, data.data(), data.size(), 0);
169 hdr->type = 2;
170 auto second = pldm_pdr_add(repo, data.data(), data.size(), 0);
171 hdr->type = 3;
172 auto third = pldm_pdr_add(repo, data.data(), data.size(), 0);
173 hdr->type = 4;
174 auto fourth = pldm_pdr_add(repo, data.data(), data.size(), 0);
175
176 uint8_t* outData = nullptr;
177 uint32_t size{};
178 auto firstRec =
179 pldm_pdr_find_record_by_type(repo, 1, nullptr, &outData, &size);
180 EXPECT_EQ(pldm_pdr_get_record_handle(repo, firstRec), first);
181 outData = nullptr;
182 auto secondRec =
183 pldm_pdr_find_record_by_type(repo, 2, nullptr, &outData, &size);
184 EXPECT_EQ(pldm_pdr_get_record_handle(repo, secondRec), second);
185 outData = nullptr;
186 auto thirdRec =
187 pldm_pdr_find_record_by_type(repo, 3, nullptr, &outData, &size);
188 EXPECT_EQ(pldm_pdr_get_record_handle(repo, thirdRec), third);
189 outData = nullptr;
190 auto fourthRec =
191 pldm_pdr_find_record_by_type(repo, 4, nullptr, &outData, &size);
192 EXPECT_EQ(pldm_pdr_get_record_handle(repo, fourthRec), fourth);
193 outData = nullptr;
194 auto fifthRec =
195 pldm_pdr_find_record_by_type(repo, 5, nullptr, &outData, &size);
196 EXPECT_EQ(fifthRec, nullptr);
197 EXPECT_EQ(outData, nullptr);
198 EXPECT_EQ(size, 0);
199
200 auto rec =
201 pldm_pdr_find_record_by_type(repo, 3, secondRec, &outData, &size);
202 EXPECT_EQ(pldm_pdr_get_record_handle(repo, rec), third);
203 outData = nullptr;
204 rec = pldm_pdr_find_record_by_type(repo, 4, secondRec, &outData, &size);
205 EXPECT_EQ(pldm_pdr_get_record_handle(repo, rec), fourth);
206 outData = nullptr;
207 rec = pldm_pdr_find_record_by_type(repo, 2, firstRec, &outData, &size);
208 EXPECT_EQ(pldm_pdr_get_record_handle(repo, rec), second);
209 outData = nullptr;
210
211 pldm_pdr_destroy(repo);
212}
Deepak Kodihallidb914672020-02-07 02:47:45 -0600213
214TEST(PDRUpdate, testAddFruRecordSet)
215{
216 auto repo = pldm_pdr_init();
217
218 auto handle = pldm_pdr_add_fru_record_set(repo, 1, 10, 1, 0, 100);
219 EXPECT_EQ(handle, 1);
220 EXPECT_EQ(pldm_pdr_get_record_count(repo), 1);
221 EXPECT_EQ(pldm_pdr_get_repo_size(repo),
222 sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_fru_record_set));
223 uint32_t size{};
224 uint32_t nextRecHdl{};
225 uint8_t* outData = nullptr;
226 auto hdl = pldm_pdr_find_record(repo, 0, &outData, &size, &nextRecHdl);
227 EXPECT_NE(hdl, nullptr);
228 EXPECT_EQ(size, sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_fru_record_set));
229 EXPECT_EQ(nextRecHdl, 0);
230 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(outData);
231 EXPECT_EQ(hdr->version, 1);
232 EXPECT_EQ(hdr->type, PLDM_PDR_FRU_RECORD_SET);
233 EXPECT_EQ(hdr->length, sizeof(pldm_pdr_fru_record_set));
234 EXPECT_EQ(hdr->record_handle, 1);
235 pldm_pdr_fru_record_set* fru = reinterpret_cast<pldm_pdr_fru_record_set*>(
236 outData + sizeof(pldm_pdr_hdr));
237 EXPECT_EQ(fru->terminus_handle, 1);
238 EXPECT_EQ(fru->fru_rsi, 10);
239 EXPECT_EQ(fru->entity_type, 1);
240 EXPECT_EQ(fru->entity_instance_num, 0);
241 EXPECT_EQ(fru->container_id, 100);
242 outData = nullptr;
243
244 handle = pldm_pdr_add_fru_record_set(repo, 2, 11, 2, 1, 101);
245 EXPECT_EQ(handle, 2);
246 EXPECT_EQ(pldm_pdr_get_record_count(repo), 2);
247 EXPECT_EQ(pldm_pdr_get_repo_size(repo),
248 2 * (sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_fru_record_set)));
249 hdl = pldm_pdr_find_record(repo, 2, &outData, &size, &nextRecHdl);
250 EXPECT_NE(hdl, nullptr);
251 EXPECT_EQ(size, sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_fru_record_set));
252 EXPECT_EQ(nextRecHdl, 0);
253 hdr = reinterpret_cast<pldm_pdr_hdr*>(outData);
254 EXPECT_EQ(hdr->version, 1);
255 EXPECT_EQ(hdr->type, PLDM_PDR_FRU_RECORD_SET);
256 EXPECT_EQ(hdr->length, sizeof(pldm_pdr_fru_record_set));
257 EXPECT_EQ(hdr->record_handle, 2);
258 fru = reinterpret_cast<pldm_pdr_fru_record_set*>(outData +
259 sizeof(pldm_pdr_hdr));
260 EXPECT_EQ(fru->terminus_handle, 2);
261 EXPECT_EQ(fru->fru_rsi, 11);
262 EXPECT_EQ(fru->entity_type, 2);
263 EXPECT_EQ(fru->entity_instance_num, 1);
264 EXPECT_EQ(fru->container_id, 101);
265 outData = nullptr;
266
267 hdl = pldm_pdr_find_record(repo, 1, &outData, &size, &nextRecHdl);
268 EXPECT_NE(hdl, nullptr);
269 EXPECT_EQ(size, sizeof(pldm_pdr_hdr) + sizeof(pldm_pdr_fru_record_set));
270 EXPECT_EQ(nextRecHdl, 2);
271 hdr = reinterpret_cast<pldm_pdr_hdr*>(outData);
272 EXPECT_EQ(hdr->version, 1);
273 EXPECT_EQ(hdr->type, PLDM_PDR_FRU_RECORD_SET);
274 EXPECT_EQ(hdr->length, sizeof(pldm_pdr_fru_record_set));
275 EXPECT_EQ(hdr->record_handle, 1);
276 fru = reinterpret_cast<pldm_pdr_fru_record_set*>(outData +
277 sizeof(pldm_pdr_hdr));
278 EXPECT_EQ(fru->terminus_handle, 1);
279 EXPECT_EQ(fru->fru_rsi, 10);
280 EXPECT_EQ(fru->entity_type, 1);
281 EXPECT_EQ(fru->entity_instance_num, 0);
282 EXPECT_EQ(fru->container_id, 100);
283 outData = nullptr;
284
285 pldm_pdr_destroy(repo);
286}
287
288TEST(PDRUpdate, tesFindtFruRecordSet)
289{
290 auto repo = pldm_pdr_init();
291
292 uint16_t terminusHdl{};
293 uint16_t entityType{};
294 uint16_t entityInstanceNum{};
295 uint16_t containerId{};
296 auto first = pldm_pdr_add_fru_record_set(repo, 1, 1, 1, 0, 100);
297 auto second = pldm_pdr_add_fru_record_set(repo, 1, 2, 1, 1, 100);
298 auto third = pldm_pdr_add_fru_record_set(repo, 1, 3, 1, 2, 100);
299 EXPECT_EQ(first, pldm_pdr_get_record_handle(
300 repo, pldm_pdr_fru_record_set_find_by_rsi(
301 repo, 1, &terminusHdl, &entityType,
302 &entityInstanceNum, &containerId)));
303 EXPECT_EQ(second, pldm_pdr_get_record_handle(
304 repo, pldm_pdr_fru_record_set_find_by_rsi(
305 repo, 2, &terminusHdl, &entityType,
306 &entityInstanceNum, &containerId)));
307 EXPECT_EQ(third, pldm_pdr_get_record_handle(
308 repo, pldm_pdr_fru_record_set_find_by_rsi(
309 repo, 3, &terminusHdl, &entityType,
310 &entityInstanceNum, &containerId)));
311 EXPECT_EQ(terminusHdl, 1);
312 EXPECT_EQ(entityType, 1);
313 EXPECT_EQ(entityInstanceNum, 2);
314 EXPECT_EQ(containerId, 100);
315 EXPECT_EQ(nullptr, pldm_pdr_fru_record_set_find_by_rsi(
316 repo, 4, &terminusHdl, &entityType,
317 &entityInstanceNum, &containerId));
318
319 pldm_pdr_destroy(repo);
320}