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