blob: cfa6fecf637812c7e9340ca665822f7ad5eabb17 [file] [log] [blame]
Deepak Kodihallid130e1a2020-06-17 05:55:32 -05001#include "common/utils.hpp"
Riya Dixit754041d2024-02-20 06:15:49 -06002#include "mocked_utils.hpp"
George Liu6492f522020-06-16 10:34:05 +08003
George Liuc453e162022-12-21 17:16:23 +08004#include <libpldm/platform.h>
Gilbert Chen6c7fed42022-02-22 15:40:17 +00005#include <linux/mctp.h>
George Liuc453e162022-12-21 17:16:23 +08006
George Liu83409572019-12-24 18:42:54 +08007#include <gtest/gtest.h>
8
9using namespace pldm::utils;
10
Riya Dixit754041d2024-02-20 06:15:49 -060011TEST(GetInventoryObjects, testForEmptyObject)
12{
13 ObjectValueTree result =
14 DBusHandler::getInventoryObjects<GetManagedEmptyObject>();
15 EXPECT_TRUE(result.empty());
16}
17
18TEST(GetInventoryObjects, testForObject)
19{
20 std::string path = "/foo/bar";
21 std::string service = "foo.bar";
22 auto result = DBusHandler::getInventoryObjects<GetManagedObject>();
23 EXPECT_EQ(result[path].begin()->first, service);
24 auto function =
25 std::get<bool>(result[path][service][std::string("Functional")]);
26 auto model =
27 std::get<std::string>(result[path][service][std::string("Model")]);
28 EXPECT_FALSE(result.empty());
29 EXPECT_TRUE(function);
30 EXPECT_EQ(model, std::string("1234 - 00Z"));
31}
32
Manojkiran Edacd4cd452024-04-23 08:53:17 +053033TEST(printBuffer, testprintBufferGoodPath)
34{
35 std::vector<uint8_t> buffer = {10, 12, 14, 25, 233};
36 std::ostringstream localString;
37 auto coutBuffer = std::cout.rdbuf();
38 std::cout.rdbuf(localString.rdbuf());
39 printBuffer(false, buffer);
40 std::cout.rdbuf(coutBuffer);
41 EXPECT_EQ(localString.str(), "Rx: 0a 0c 0e 19 e9 \n");
42 localString.str("");
43 localString.clear();
44 std::cerr << localString.str() << std::endl;
45 buffer = {12, 0, 200, 12, 255};
46 std::cout.rdbuf(localString.rdbuf());
47 printBuffer(true, buffer);
48 std::cout.rdbuf(coutBuffer);
49 EXPECT_EQ(localString.str(), "Tx: 0c 00 c8 0c ff \n");
50}
51
52TEST(printBuffer, testprintBufferBadPath)
53{
54 std::vector<uint8_t> buffer = {};
55 std::ostringstream localString;
56 auto coutBuffer = std::cout.rdbuf();
57 std::cout.rdbuf(localString.rdbuf());
58 printBuffer(false, buffer);
59 EXPECT_EQ(localString.str(), "");
60 printBuffer(true, buffer);
61 std::cout.rdbuf(coutBuffer);
62 EXPECT_EQ(localString.str(), "");
63}
64
George Liu83409572019-12-24 18:42:54 +080065TEST(decodeDate, testGooduintToDate)
66{
67 uint64_t data = 20191212115959;
68 uint16_t year = 2019;
69 uint8_t month = 12;
70 uint8_t day = 12;
71 uint8_t hours = 11;
72 uint8_t minutes = 59;
73 uint8_t seconds = 59;
74
75 uint16_t retyear = 0;
76 uint8_t retmonth = 0;
77 uint8_t retday = 0;
78 uint8_t rethours = 0;
79 uint8_t retminutes = 0;
80 uint8_t retseconds = 0;
81
82 auto ret = uintToDate(data, &retyear, &retmonth, &retday, &rethours,
83 &retminutes, &retseconds);
84
85 EXPECT_EQ(ret, true);
86 EXPECT_EQ(year, retyear);
87 EXPECT_EQ(month, retmonth);
88 EXPECT_EQ(day, retday);
89 EXPECT_EQ(hours, rethours);
90 EXPECT_EQ(minutes, retminutes);
91 EXPECT_EQ(seconds, retseconds);
92}
93
94TEST(decodeDate, testBaduintToDate)
95{
96 uint64_t data = 10191212115959;
97
98 uint16_t retyear = 0;
99 uint8_t retmonth = 0;
100 uint8_t retday = 0;
101 uint8_t rethours = 0;
102 uint8_t retminutes = 0;
103 uint8_t retseconds = 0;
104
105 auto ret = uintToDate(data, &retyear, &retmonth, &retday, &rethours,
106 &retminutes, &retseconds);
107
108 EXPECT_EQ(ret, false);
109}
110
George Liuba4c1fb2020-02-05 14:13:30 +0800111TEST(parseEffecterData, testGoodDecodeEffecterData)
George Liu83409572019-12-24 18:42:54 +0800112{
George Liuba4c1fb2020-02-05 14:13:30 +0800113 std::vector<uint8_t> effecterData = {1, 1, 0, 1};
114 uint8_t effecterCount = 2;
George Liu83409572019-12-24 18:42:54 +0800115 set_effecter_state_field stateField0 = {1, 1};
George Liuba4c1fb2020-02-05 14:13:30 +0800116 set_effecter_state_field stateField1 = {0, 1};
George Liu83409572019-12-24 18:42:54 +0800117
George Liuba4c1fb2020-02-05 14:13:30 +0800118 auto effecterField = parseEffecterData(effecterData, effecterCount);
119 EXPECT_NE(effecterField, std::nullopt);
120 EXPECT_EQ(effecterCount, effecterField->size());
George Liu83409572019-12-24 18:42:54 +0800121
George Liuba4c1fb2020-02-05 14:13:30 +0800122 std::vector<set_effecter_state_field> stateField = effecterField.value();
George Liu83409572019-12-24 18:42:54 +0800123 EXPECT_EQ(stateField[0].set_request, stateField0.set_request);
124 EXPECT_EQ(stateField[0].effecter_state, stateField0.effecter_state);
125 EXPECT_EQ(stateField[1].set_request, stateField1.set_request);
126 EXPECT_EQ(stateField[1].effecter_state, stateField1.effecter_state);
George Liu83409572019-12-24 18:42:54 +0800127}
128
George Liuba4c1fb2020-02-05 14:13:30 +0800129TEST(parseEffecterData, testBadDecodeEffecterData)
George Liu83409572019-12-24 18:42:54 +0800130{
George Liuba4c1fb2020-02-05 14:13:30 +0800131 std::vector<uint8_t> effecterData = {0, 1, 0, 1, 0, 1};
132 uint8_t effecterCount = 2;
George Liu83409572019-12-24 18:42:54 +0800133
George Liuba4c1fb2020-02-05 14:13:30 +0800134 auto effecterField = parseEffecterData(effecterData, effecterCount);
George Liu83409572019-12-24 18:42:54 +0800135
George Liuba4c1fb2020-02-05 14:13:30 +0800136 EXPECT_EQ(effecterField, std::nullopt);
George Liu83409572019-12-24 18:42:54 +0800137}
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500138
139TEST(FindStateEffecterPDR, testOneMatch)
140{
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500141 auto repo = pldm_pdr_init();
142 uint8_t tid = 1;
143 uint16_t entityID = 33;
144 uint16_t stateSetId = 196;
145
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400146 std::vector<uint8_t> pdr(
147 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
148 sizeof(struct state_effecter_possible_states));
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500149
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530150 auto rec = new (pdr.data()) pldm_state_effecter_pdr;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500151
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530152 auto state = new (rec->possible_states) state_effecter_possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500153
154 rec->hdr.type = 11;
155 rec->hdr.record_handle = 1;
156 rec->entity_type = 33;
157 rec->container_id = 0;
158 rec->composite_effecter_count = 1;
159 state->state_set_id = 196;
160 state->possible_states_size = 1;
161
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930162 uint32_t handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000163 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500164
165 auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
166
167 EXPECT_EQ(pdr, record[0]);
168
169 pldm_pdr_destroy(repo);
170}
171
172TEST(FindStateEffecterPDR, testNoMatch)
173{
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500174 auto repo = pldm_pdr_init();
175 uint8_t tid = 1;
176 uint16_t entityID = 44;
177 uint16_t stateSetId = 196;
178
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400179 std::vector<uint8_t> pdr(
180 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
181 sizeof(struct state_effecter_possible_states));
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500182
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530183 auto rec = new (pdr.data()) pldm_state_effecter_pdr;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500184
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530185 auto state = new (rec->possible_states) state_effecter_possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500186
187 rec->hdr.type = 11;
188 rec->hdr.record_handle = 1;
189 rec->entity_type = 33;
190 rec->container_id = 0;
191 rec->composite_effecter_count = 1;
192 state->state_set_id = 196;
193 state->possible_states_size = 1;
194
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930195 uint32_t handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000196 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500197
198 auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
199
200 EXPECT_EQ(record.empty(), true);
201
202 pldm_pdr_destroy(repo);
203}
204
205TEST(FindStateEffecterPDR, testEmptyRepo)
206{
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500207 auto repo = pldm_pdr_init();
208 uint8_t tid = 1;
209 uint16_t entityID = 33;
210 uint16_t stateSetId = 196;
211
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400212 std::vector<uint8_t> pdr(
213 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
214 sizeof(struct state_effecter_possible_states));
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500215
216 auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
217
218 EXPECT_EQ(record.empty(), true);
219
220 pldm_pdr_destroy(repo);
221}
222
223TEST(FindStateEffecterPDR, testMoreMatch)
224{
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500225 auto repo = pldm_pdr_init();
226 uint8_t tid = 1;
227
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400228 std::vector<uint8_t> pdr(
229 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
230 sizeof(struct state_effecter_possible_states));
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500231
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530232 auto rec = new (pdr.data()) pldm_state_effecter_pdr;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500233
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530234 auto state = new (rec->possible_states) state_effecter_possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500235
236 rec->hdr.type = 11;
237 rec->hdr.record_handle = 1;
238 rec->entity_type = 31;
239 rec->container_id = 0;
240 rec->composite_effecter_count = 1;
241 state->state_set_id = 129;
242 state->possible_states_size = 1;
243
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930244 uint32_t handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000245 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500246
247 std::vector<uint8_t> pdr_second(
248 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
249 sizeof(struct state_effecter_possible_states));
250
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530251 auto rec_second = new (pdr_second.data()) pldm_state_effecter_pdr;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500252
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530253 auto state_second = new (rec_second->possible_states)
254 state_effecter_possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500255
256 rec_second->hdr.type = 11;
257 rec_second->hdr.record_handle = 2;
258 rec_second->entity_type = 31;
259 rec_second->container_id = 0;
260 rec_second->composite_effecter_count = 1;
261 state_second->state_set_id = 129;
262 state_second->possible_states_size = 1;
263
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930264 handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000265 ASSERT_EQ(pldm_pdr_add(repo, pdr_second.data(), pdr_second.size(), false, 1,
266 &handle),
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930267 0);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500268
269 uint16_t entityID_ = 31;
270 uint16_t stateSetId_ = 129;
271
272 auto record = findStateEffecterPDR(tid, entityID_, stateSetId_, repo);
273
274 EXPECT_EQ(pdr, record[0]);
275 EXPECT_EQ(pdr_second, record[1]);
276
277 pldm_pdr_destroy(repo);
278}
279
280TEST(FindStateEffecterPDR, testManyNoMatch)
281{
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500282 auto repo = pldm_pdr_init();
283 uint8_t tid = 1;
284 uint16_t entityID = 33;
285 uint16_t stateSetId = 196;
286
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400287 std::vector<uint8_t> pdr(
288 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
289 sizeof(struct state_effecter_possible_states));
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500290
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530291 auto rec = new (pdr.data()) pldm_state_effecter_pdr;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500292
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530293 auto state = new (rec->possible_states) state_effecter_possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500294
295 rec->hdr.type = 11;
296 rec->hdr.record_handle = 1;
297 rec->entity_type = 34;
298 rec->container_id = 0;
299 rec->composite_effecter_count = 1;
300 state->state_set_id = 198;
301 state->possible_states_size = 1;
302
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930303 uint32_t handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000304 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500305
306 std::vector<uint8_t> pdr_second(
307 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
308 sizeof(struct state_effecter_possible_states));
309
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530310 auto rec_second = new (pdr_second.data()) pldm_state_effecter_pdr;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500311
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530312 auto state_second = new (rec_second->possible_states)
313 state_effecter_possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500314
315 rec_second->hdr.type = 11;
316 rec_second->hdr.record_handle = 2;
317 rec_second->entity_type = 39;
318 rec_second->container_id = 0;
319 rec_second->composite_effecter_count = 1;
320 state_second->state_set_id = 169;
321 state_second->possible_states_size = 1;
322
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930323 handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000324 ASSERT_EQ(pldm_pdr_add(repo, pdr_second.data(), pdr_second.size(), false, 1,
325 &handle),
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930326 0);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500327
328 auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
329
330 EXPECT_EQ(record.empty(), true);
331
332 pldm_pdr_destroy(repo);
333}
334
335TEST(FindStateEffecterPDR, testOneMatchOneNoMatch)
336{
337 auto repo = pldm_pdr_init();
338 uint8_t tid = 1;
339 uint16_t entityID = 67;
340 uint16_t stateSetId = 192;
341
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400342 std::vector<uint8_t> pdr(
343 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
344 sizeof(struct state_effecter_possible_states));
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500345
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530346 auto rec = new (pdr.data()) pldm_state_effecter_pdr;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500347
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530348 auto state = new (rec->possible_states) state_effecter_possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500349
350 rec->hdr.type = 11;
351 rec->hdr.record_handle = 1;
352 rec->entity_type = 32;
353 rec->container_id = 0;
354 rec->composite_effecter_count = 1;
355 state->state_set_id = 198;
356 state->possible_states_size = 1;
357
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930358 uint32_t handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000359 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500360
361 std::vector<uint8_t> pdr_second(
362 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
363 sizeof(struct state_effecter_possible_states));
364
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530365 auto rec_second = new (pdr_second.data()) pldm_state_effecter_pdr;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500366
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530367 auto state_second = new (rec_second->possible_states)
368 state_effecter_possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500369
370 rec_second->hdr.type = 11;
371 rec_second->hdr.record_handle = 2;
372 rec_second->entity_type = 67;
373 rec_second->container_id = 0;
374 rec_second->composite_effecter_count = 1;
375 state_second->state_set_id = 192;
376 state_second->possible_states_size = 1;
377
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930378 handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000379 ASSERT_EQ(pldm_pdr_add(repo, pdr_second.data(), pdr_second.size(), false, 1,
380 &handle),
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930381 0);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500382
383 auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
384
385 EXPECT_EQ(pdr_second, record[0]);
386 EXPECT_EQ(record.size(), 1);
387
388 pldm_pdr_destroy(repo);
389}
390
391TEST(FindStateEffecterPDR, testOneMatchManyNoMatch)
392{
393 auto repo = pldm_pdr_init();
394 uint8_t tid = 1;
395 uint16_t entityID = 67;
396 uint16_t stateSetId = 192;
397
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400398 std::vector<uint8_t> pdr(
399 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
400 sizeof(struct state_effecter_possible_states));
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500401
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530402 auto rec = new (pdr.data()) pldm_state_effecter_pdr;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500403
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530404 auto state = new (rec->possible_states) state_effecter_possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500405
406 rec->hdr.type = 11;
407 rec->hdr.record_handle = 1;
408 rec->entity_type = 32;
409 rec->container_id = 0;
410 rec->composite_effecter_count = 1;
411 state->state_set_id = 198;
412 state->possible_states_size = 1;
413
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930414 uint32_t handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000415 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500416
417 std::vector<uint8_t> pdr_second(
418 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
419 sizeof(struct state_effecter_possible_states));
420
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530421 auto rec_second = new (pdr_second.data()) pldm_state_effecter_pdr;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500422
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530423 auto state_second = new (rec_second->possible_states)
424 state_effecter_possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500425
426 rec_second->hdr.type = 11;
427 rec_second->hdr.record_handle = 2;
428 rec_second->entity_type = 67;
429 rec_second->container_id = 0;
430 rec_second->composite_effecter_count = 1;
431 state_second->state_set_id = 192;
432 state_second->possible_states_size = 1;
433
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930434 handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000435 ASSERT_EQ(pldm_pdr_add(repo, pdr_second.data(), pdr_second.size(), false, 1,
436 &handle),
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930437 0);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500438
439 std::vector<uint8_t> pdr_third(
440 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
441 sizeof(struct state_effecter_possible_states));
442
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530443 auto rec_third = new (pdr_third.data()) pldm_state_effecter_pdr;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500444
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530445 auto state_third = new (rec_third->possible_states)
446 state_effecter_possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500447
448 rec_third->hdr.type = 11;
449 rec_third->hdr.record_handle = 3;
450 rec_third->entity_type = 69;
451 rec_third->container_id = 0;
452 rec_third->composite_effecter_count = 1;
453 state_third->state_set_id = 199;
454 state_third->possible_states_size = 1;
455
456 auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
457
458 EXPECT_EQ(pdr_second, record[0]);
459 EXPECT_EQ(record.size(), 1);
460
461 pldm_pdr_destroy(repo);
462}
463
464TEST(FindStateEffecterPDR, testCompositeEffecter)
465{
466 auto repo = pldm_pdr_init();
467 uint8_t tid = 1;
468 uint16_t entityID = 67;
469 uint16_t stateSetId = 192;
470
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400471 std::vector<uint8_t> pdr(
472 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
473 sizeof(struct state_effecter_possible_states) * 3);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500474
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530475 auto rec = new (pdr.data()) pldm_state_effecter_pdr;
Chicago Duana7aacc32020-06-10 18:03:38 +0800476 auto state_start = rec->possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500477
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530478 auto state = new (state_start) state_effecter_possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500479
480 rec->hdr.type = 11;
481 rec->hdr.record_handle = 1;
482 rec->entity_type = 67;
483 rec->container_id = 0;
484 rec->composite_effecter_count = 3;
485 state->state_set_id = 198;
486 state->possible_states_size = 1;
487
Chicago Duana7aacc32020-06-10 18:03:38 +0800488 state_start += state->possible_states_size + sizeof(state->state_set_id) +
489 sizeof(state->possible_states_size);
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530490 state = new (state_start) state_effecter_possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500491 state->state_set_id = 193;
492 state->possible_states_size = 1;
493
Chicago Duana7aacc32020-06-10 18:03:38 +0800494 state_start += state->possible_states_size + sizeof(state->state_set_id) +
495 sizeof(state->possible_states_size);
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530496 state = new (state_start) state_effecter_possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500497 state->state_set_id = 192;
498 state->possible_states_size = 1;
499
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930500 uint32_t handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000501 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500502
503 auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
504
505 EXPECT_EQ(pdr, record[0]);
506
507 pldm_pdr_destroy(repo);
508}
509
510TEST(FindStateEffecterPDR, testNoMatchCompositeEffecter)
511{
512 auto repo = pldm_pdr_init();
513 uint8_t tid = 1;
514 uint16_t entityID = 67;
515 uint16_t stateSetId = 192;
516
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400517 std::vector<uint8_t> pdr(
518 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
519 sizeof(struct state_effecter_possible_states) * 3);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500520
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530521 auto rec = new (pdr.data()) pldm_state_effecter_pdr;
Chicago Duana7aacc32020-06-10 18:03:38 +0800522 auto state_start = rec->possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500523
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530524 auto state = new (state_start) state_effecter_possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500525
526 rec->hdr.type = 11;
527 rec->hdr.record_handle = 1;
528 rec->entity_type = 34;
529 rec->container_id = 0;
530 rec->composite_effecter_count = 3;
531 state->state_set_id = 198;
532 state->possible_states_size = 1;
533
Chicago Duana7aacc32020-06-10 18:03:38 +0800534 state_start += state->possible_states_size + sizeof(state->state_set_id) +
535 sizeof(state->possible_states_size);
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530536 state = new (state_start) state_effecter_possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500537 state->state_set_id = 193;
538 state->possible_states_size = 1;
539
Chicago Duana7aacc32020-06-10 18:03:38 +0800540 state_start += state->possible_states_size + sizeof(state->state_set_id) +
541 sizeof(state->possible_states_size);
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530542 state = new (state_start) state_effecter_possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500543 state->state_set_id = 123;
544 state->possible_states_size = 1;
545
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930546 uint32_t handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000547 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500548
549 auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
550
551 EXPECT_EQ(record.empty(), true);
552
553 pldm_pdr_destroy(repo);
554}
Chicago Duan738e4d82020-05-28 16:39:19 +0800555
556TEST(FindStateSensorPDR, testOneMatch)
557{
Chicago Duan738e4d82020-05-28 16:39:19 +0800558 auto repo = pldm_pdr_init();
559 uint8_t tid = 1;
560 uint16_t entityID = 5;
561 uint16_t stateSetId = 1;
562
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400563 std::vector<uint8_t> pdr(
564 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
565 sizeof(struct state_sensor_possible_states));
Chicago Duan738e4d82020-05-28 16:39:19 +0800566
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530567 auto rec = new (pdr.data()) pldm_state_sensor_pdr;
Chicago Duan738e4d82020-05-28 16:39:19 +0800568
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530569 auto state = new (rec->possible_states) state_sensor_possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800570
571 rec->hdr.type = 4;
572 rec->hdr.record_handle = 1;
573 rec->entity_type = 5;
574 rec->container_id = 0;
575 rec->composite_sensor_count = 1;
576 state->state_set_id = 1;
577 state->possible_states_size = 1;
578
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930579 uint32_t handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000580 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Chicago Duan738e4d82020-05-28 16:39:19 +0800581
582 auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
583
584 EXPECT_EQ(pdr, record[0]);
585
586 pldm_pdr_destroy(repo);
587}
588
589TEST(FindStateSensorPDR, testNoMatch)
590{
Chicago Duan738e4d82020-05-28 16:39:19 +0800591 auto repo = pldm_pdr_init();
592 uint8_t tid = 1;
593 uint16_t entityID = 5;
594 uint16_t stateSetId = 1;
595
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400596 std::vector<uint8_t> pdr(
597 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
598 sizeof(struct state_sensor_possible_states));
Chicago Duan738e4d82020-05-28 16:39:19 +0800599
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530600 auto rec = new (pdr.data()) pldm_state_sensor_pdr;
Chicago Duan738e4d82020-05-28 16:39:19 +0800601
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530602 auto state = new (rec->possible_states) state_sensor_possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800603
604 rec->hdr.type = 4;
605 rec->hdr.record_handle = 1;
606 rec->entity_type = 55;
607 rec->container_id = 0;
608 rec->composite_sensor_count = 1;
609 state->state_set_id = 1;
610 state->possible_states_size = 1;
611
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930612 uint32_t handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000613 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Chicago Duan738e4d82020-05-28 16:39:19 +0800614
615 auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
616
617 EXPECT_EQ(record.empty(), true);
618
619 pldm_pdr_destroy(repo);
620}
621
622TEST(FindStateSensorPDR, testEmptyRepo)
623{
Chicago Duan738e4d82020-05-28 16:39:19 +0800624 auto repo = pldm_pdr_init();
625 uint8_t tid = 1;
626 uint16_t entityID = 5;
627 uint16_t stateSetId = 1;
628
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400629 std::vector<uint8_t> pdr(
630 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
631 sizeof(struct state_sensor_possible_states));
Chicago Duan738e4d82020-05-28 16:39:19 +0800632
633 auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
634
635 EXPECT_EQ(record.empty(), true);
636
637 pldm_pdr_destroy(repo);
638}
639
640TEST(FindStateSensorPDR, testMoreMatch)
641{
Chicago Duan738e4d82020-05-28 16:39:19 +0800642 auto repo = pldm_pdr_init();
643 uint8_t tid = 1;
644
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400645 std::vector<uint8_t> pdr(
646 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
647 sizeof(struct state_sensor_possible_states));
Chicago Duan738e4d82020-05-28 16:39:19 +0800648
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530649 auto rec = new (pdr.data()) pldm_state_sensor_pdr;
Chicago Duan738e4d82020-05-28 16:39:19 +0800650
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530651 auto state = new (rec->possible_states) state_sensor_possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800652
653 rec->hdr.type = 4;
654 rec->hdr.record_handle = 1;
655 rec->entity_type = 5;
656 rec->container_id = 0;
657 rec->composite_sensor_count = 1;
658 state->state_set_id = 1;
659 state->possible_states_size = 1;
660
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930661 uint32_t handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000662 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Chicago Duan738e4d82020-05-28 16:39:19 +0800663
664 std::vector<uint8_t> pdr_second(
665 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
666 sizeof(struct state_sensor_possible_states));
667
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530668 auto rec_second = new (pdr_second.data()) pldm_state_sensor_pdr;
Chicago Duan738e4d82020-05-28 16:39:19 +0800669
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530670 auto state_second = new (rec_second->possible_states)
671 state_sensor_possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800672
673 rec_second->hdr.type = 4;
674 rec_second->hdr.record_handle = 2;
675 rec_second->entity_type = 5;
676 rec_second->container_id = 0;
677 rec_second->composite_sensor_count = 1;
678 state_second->state_set_id = 1;
679 state_second->possible_states_size = 1;
680
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930681 handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000682 ASSERT_EQ(pldm_pdr_add(repo, pdr_second.data(), pdr_second.size(), false, 1,
683 &handle),
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930684 0);
Chicago Duan738e4d82020-05-28 16:39:19 +0800685
686 uint16_t entityID_ = 5;
687 uint16_t stateSetId_ = 1;
688
689 auto record = findStateSensorPDR(tid, entityID_, stateSetId_, repo);
690
691 EXPECT_EQ(pdr, record[0]);
692 EXPECT_EQ(pdr_second, record[1]);
693
694 pldm_pdr_destroy(repo);
695}
696
697TEST(FindStateSensorPDR, testManyNoMatch)
698{
Chicago Duan738e4d82020-05-28 16:39:19 +0800699 auto repo = pldm_pdr_init();
700 uint8_t tid = 1;
701 uint16_t entityID = 5;
702 uint16_t stateSetId = 1;
703
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400704 std::vector<uint8_t> pdr(
705 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
706 sizeof(struct state_sensor_possible_states));
Chicago Duan738e4d82020-05-28 16:39:19 +0800707
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530708 auto rec = new (pdr.data()) pldm_state_sensor_pdr;
Chicago Duan738e4d82020-05-28 16:39:19 +0800709
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530710 auto state = new (rec->possible_states) state_sensor_possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800711
712 rec->hdr.type = 4;
713 rec->hdr.record_handle = 1;
714 rec->entity_type = 56;
715 rec->container_id = 0;
716 rec->composite_sensor_count = 1;
717 state->state_set_id = 2;
718 state->possible_states_size = 1;
719
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930720 uint32_t handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000721 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Chicago Duan738e4d82020-05-28 16:39:19 +0800722
723 std::vector<uint8_t> pdr_second(
724 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
725 sizeof(struct state_sensor_possible_states));
726
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530727 auto rec_second = new (pdr_second.data()) pldm_state_sensor_pdr;
Chicago Duan738e4d82020-05-28 16:39:19 +0800728
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530729 auto state_second = new (rec_second->possible_states)
730 state_sensor_possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800731
732 rec_second->hdr.type = 4;
733 rec_second->hdr.record_handle = 2;
734 rec_second->entity_type = 66;
735 rec_second->container_id = 0;
736 rec_second->composite_sensor_count = 1;
737 state_second->state_set_id = 3;
738 state_second->possible_states_size = 1;
739
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930740 handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000741 ASSERT_EQ(pldm_pdr_add(repo, pdr_second.data(), pdr_second.size(), false, 1,
742 &handle),
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930743 0);
Chicago Duan738e4d82020-05-28 16:39:19 +0800744
745 auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
746
747 EXPECT_EQ(record.empty(), true);
748
749 pldm_pdr_destroy(repo);
750}
751
752TEST(FindStateSensorPDR, testOneMatchOneNoMatch)
753{
754 auto repo = pldm_pdr_init();
755 uint8_t tid = 1;
756 uint16_t entityID = 5;
757 uint16_t stateSetId = 1;
758
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400759 std::vector<uint8_t> pdr(
760 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
761 sizeof(struct state_sensor_possible_states));
Chicago Duan738e4d82020-05-28 16:39:19 +0800762
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530763 auto rec = new (pdr.data()) pldm_state_sensor_pdr;
Chicago Duan738e4d82020-05-28 16:39:19 +0800764
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530765 auto state = new (rec->possible_states) state_sensor_possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800766
767 rec->hdr.type = 4;
768 rec->hdr.record_handle = 1;
769 rec->entity_type = 10;
770 rec->container_id = 0;
771 rec->composite_sensor_count = 1;
772 state->state_set_id = 20;
773 state->possible_states_size = 1;
774
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930775 uint32_t handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000776 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Chicago Duan738e4d82020-05-28 16:39:19 +0800777
778 std::vector<uint8_t> pdr_second(
779 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
780 sizeof(struct state_sensor_possible_states));
781
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530782 auto rec_second = new (pdr_second.data()) pldm_state_sensor_pdr;
Chicago Duan738e4d82020-05-28 16:39:19 +0800783
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530784 auto state_second = new (rec_second->possible_states)
785 state_sensor_possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800786
787 rec_second->hdr.type = 4;
788 rec_second->hdr.record_handle = 2;
789 rec_second->entity_type = 5;
790 rec_second->container_id = 0;
791 rec_second->composite_sensor_count = 1;
792 state_second->state_set_id = 1;
793 state_second->possible_states_size = 1;
794
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930795 handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000796 ASSERT_EQ(pldm_pdr_add(repo, pdr_second.data(), pdr_second.size(), false, 1,
797 &handle),
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930798 0);
Chicago Duan738e4d82020-05-28 16:39:19 +0800799
800 auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
801
802 EXPECT_EQ(pdr_second, record[0]);
803 EXPECT_EQ(record.size(), 1);
804
805 pldm_pdr_destroy(repo);
806}
807
808TEST(FindStateSensorPDR, testOneMatchManyNoMatch)
809{
810 auto repo = pldm_pdr_init();
811 uint8_t tid = 1;
812 uint16_t entityID = 5;
813 uint16_t stateSetId = 1;
814
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400815 std::vector<uint8_t> pdr(
816 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
817 sizeof(struct state_sensor_possible_states));
Chicago Duan738e4d82020-05-28 16:39:19 +0800818
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530819 auto rec = new (pdr.data()) pldm_state_sensor_pdr;
Chicago Duan738e4d82020-05-28 16:39:19 +0800820
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530821 auto state = new (rec->possible_states) state_sensor_possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800822
823 rec->hdr.type = 4;
824 rec->hdr.record_handle = 1;
825 rec->entity_type = 6;
826 rec->container_id = 0;
827 rec->composite_sensor_count = 1;
828 state->state_set_id = 9;
829 state->possible_states_size = 1;
830
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930831 uint32_t handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000832 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Chicago Duan738e4d82020-05-28 16:39:19 +0800833
834 std::vector<uint8_t> pdr_second(
835 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
836 sizeof(struct state_sensor_possible_states));
837
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530838 auto rec_second = new (pdr_second.data()) pldm_state_sensor_pdr;
Chicago Duan738e4d82020-05-28 16:39:19 +0800839
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530840 auto state_second = new (rec_second->possible_states)
841 state_sensor_possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800842
843 rec_second->hdr.type = 4;
844 rec_second->hdr.record_handle = 2;
845 rec_second->entity_type = 5;
846 rec_second->container_id = 0;
847 rec_second->composite_sensor_count = 1;
848 state_second->state_set_id = 1;
849 state_second->possible_states_size = 1;
850
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930851 handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000852 ASSERT_EQ(pldm_pdr_add(repo, pdr_second.data(), pdr_second.size(), false, 1,
853 &handle),
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930854 0);
Chicago Duan738e4d82020-05-28 16:39:19 +0800855
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400856 std::vector<uint8_t> pdr_third(
857 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
858 sizeof(struct state_sensor_possible_states));
Chicago Duan738e4d82020-05-28 16:39:19 +0800859
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530860 auto rec_third = new (pdr_third.data()) pldm_state_sensor_pdr;
Chicago Duan738e4d82020-05-28 16:39:19 +0800861
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530862 auto state_third = new (rec_third->possible_states)
863 state_sensor_possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800864
865 rec_third->hdr.type = 4;
866 rec_third->hdr.record_handle = 3;
867 rec_third->entity_type = 7;
868 rec_third->container_id = 0;
869 rec_third->composite_sensor_count = 1;
870 state_third->state_set_id = 12;
871 state_third->possible_states_size = 1;
872
873 auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
874
875 EXPECT_EQ(pdr_second, record[0]);
876 EXPECT_EQ(record.size(), 1);
877
878 pldm_pdr_destroy(repo);
879}
880
881TEST(FindStateSensorPDR, testCompositeSensor)
882{
883 auto repo = pldm_pdr_init();
884 uint8_t tid = 1;
885 uint16_t entityID = 5;
886 uint16_t stateSetId = 1;
887
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400888 std::vector<uint8_t> pdr(
889 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
890 sizeof(struct state_sensor_possible_states) * 3);
Chicago Duan738e4d82020-05-28 16:39:19 +0800891
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530892 auto rec = new (pdr.data()) pldm_state_sensor_pdr;
Chicago Duana7aacc32020-06-10 18:03:38 +0800893 auto state_start = rec->possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800894
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530895 auto state = new (state_start) state_sensor_possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800896
897 rec->hdr.type = 4;
898 rec->hdr.record_handle = 1;
899 rec->entity_type = 5;
900 rec->container_id = 0;
901 rec->composite_sensor_count = 3;
902 state->state_set_id = 2;
903 state->possible_states_size = 1;
904
Chicago Duana7aacc32020-06-10 18:03:38 +0800905 state_start += state->possible_states_size + sizeof(state->state_set_id) +
906 sizeof(state->possible_states_size);
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530907 state = new (state_start) state_sensor_possible_states;
Chicago Duana7aacc32020-06-10 18:03:38 +0800908
Chicago Duan738e4d82020-05-28 16:39:19 +0800909 state->state_set_id = 7;
910 state->possible_states_size = 1;
911
Chicago Duana7aacc32020-06-10 18:03:38 +0800912 state_start += state->possible_states_size + sizeof(state->state_set_id) +
913 sizeof(state->possible_states_size);
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530914 state = new (state_start) state_sensor_possible_states;
Chicago Duana7aacc32020-06-10 18:03:38 +0800915
Chicago Duan738e4d82020-05-28 16:39:19 +0800916 state->state_set_id = 1;
917 state->possible_states_size = 1;
918
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930919 uint32_t handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000920 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Chicago Duan738e4d82020-05-28 16:39:19 +0800921
922 auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
923
924 EXPECT_EQ(pdr, record[0]);
925
926 pldm_pdr_destroy(repo);
927}
928
929TEST(FindStateSensorPDR, testNoMatchCompositeSensor)
930{
931 auto repo = pldm_pdr_init();
932 uint8_t tid = 1;
933 uint16_t entityID = 5;
934 uint16_t stateSetId = 1;
935
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400936 std::vector<uint8_t> pdr(
937 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
938 sizeof(struct state_sensor_possible_states) * 3);
Chicago Duan738e4d82020-05-28 16:39:19 +0800939
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530940 auto rec = new (pdr.data()) pldm_state_sensor_pdr;
Chicago Duana7aacc32020-06-10 18:03:38 +0800941 auto state_start = rec->possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800942
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530943 auto state = new (state_start) state_sensor_possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800944
945 rec->hdr.type = 4;
946 rec->hdr.record_handle = 1;
947 rec->entity_type = 21;
948 rec->container_id = 0;
949 rec->composite_sensor_count = 3;
950 state->state_set_id = 15;
951 state->possible_states_size = 1;
952
Chicago Duana7aacc32020-06-10 18:03:38 +0800953 state_start += state->possible_states_size + sizeof(state->state_set_id) +
954 sizeof(state->possible_states_size);
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530955 state = new (state_start) state_sensor_possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800956 state->state_set_id = 19;
957 state->possible_states_size = 1;
958
Chicago Duana7aacc32020-06-10 18:03:38 +0800959 state_start += state->possible_states_size + sizeof(state->state_set_id) +
960 sizeof(state->possible_states_size);
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530961 state = new (state_start) state_sensor_possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800962 state->state_set_id = 39;
963 state->possible_states_size = 1;
964
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930965 uint32_t handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000966 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Chicago Duan738e4d82020-05-28 16:39:19 +0800967
968 auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
969
970 EXPECT_EQ(record.empty(), true);
971
972 pldm_pdr_destroy(repo);
Chicago Duana7aacc32020-06-10 18:03:38 +0800973}
Tom Joseph54922072021-06-19 02:45:46 -0700974
975TEST(toString, allTestCases)
976{
977 variable_field buffer{};
978 constexpr std::string_view str1{};
979 auto returnStr1 = toString(buffer);
980 EXPECT_EQ(returnStr1, str1);
981
982 constexpr std::string_view str2{"0penBmc"};
983 constexpr std::array<uint8_t, 7> bufferArr1{0x30, 0x70, 0x65, 0x6e,
984 0x42, 0x6d, 0x63};
985 buffer.ptr = bufferArr1.data();
986 buffer.length = bufferArr1.size();
987 auto returnStr2 = toString(buffer);
988 EXPECT_EQ(returnStr2, str2);
989
990 constexpr std::string_view str3{"0pen mc"};
991 // \xa0 - the non-breaking space in ISO-8859-1
992 constexpr std::array<uint8_t, 7> bufferArr2{0x30, 0x70, 0x65, 0x6e,
993 0xa0, 0x6d, 0x63};
994 buffer.ptr = bufferArr2.data();
995 buffer.length = bufferArr2.size();
996 auto returnStr3 = toString(buffer);
997 EXPECT_EQ(returnStr3, str3);
George Liu872f0f62021-11-25 16:26:16 +0800998}
999
1000TEST(Split, allTestCases)
1001{
1002 std::string s1 = "aa,bb,cc,dd";
1003 auto results1 = split(s1, ",");
1004 EXPECT_EQ(results1[0], "aa");
1005 EXPECT_EQ(results1[1], "bb");
1006 EXPECT_EQ(results1[2], "cc");
1007 EXPECT_EQ(results1[3], "dd");
1008
1009 std::string s2 = "aa||bb||cc||dd";
1010 auto results2 = split(s2, "||");
1011 EXPECT_EQ(results2[0], "aa");
1012 EXPECT_EQ(results2[1], "bb");
1013 EXPECT_EQ(results2[2], "cc");
1014 EXPECT_EQ(results2[3], "dd");
1015
1016 std::string s3 = " aa || bb||cc|| dd";
1017 auto results3 = split(s3, "||", " ");
1018 EXPECT_EQ(results3[0], "aa");
1019 EXPECT_EQ(results3[1], "bb");
1020 EXPECT_EQ(results3[2], "cc");
1021 EXPECT_EQ(results3[3], "dd");
1022
1023 std::string s4 = "aa\\\\bb\\cc\\dd";
1024 auto results4 = split(s4, "\\");
1025 EXPECT_EQ(results4[0], "aa");
1026 EXPECT_EQ(results4[1], "bb");
1027 EXPECT_EQ(results4[2], "cc");
1028 EXPECT_EQ(results4[3], "dd");
1029
1030 std::string s5 = "aa\\";
1031 auto results5 = split(s5, "\\");
1032 EXPECT_EQ(results5[0], "aa");
Manojkiran Eda3ca40452021-10-04 22:51:37 +05301033}
Gilbert Chen6c7fed42022-02-22 15:40:17 +00001034
1035TEST(ValidEID, allTestCases)
1036{
1037 auto rc = isValidEID(MCTP_ADDR_NULL);
1038 EXPECT_EQ(rc, false);
1039 rc = isValidEID(MCTP_ADDR_ANY);
1040 EXPECT_EQ(rc, false);
1041 rc = isValidEID(1);
1042 EXPECT_EQ(rc, false);
1043 rc = isValidEID(2);
1044 EXPECT_EQ(rc, false);
1045 rc = isValidEID(3);
1046 EXPECT_EQ(rc, false);
1047 rc = isValidEID(4);
1048 EXPECT_EQ(rc, false);
1049 rc = isValidEID(5);
1050 EXPECT_EQ(rc, false);
1051 rc = isValidEID(6);
1052 EXPECT_EQ(rc, false);
1053 rc = isValidEID(7);
1054 EXPECT_EQ(rc, false);
1055 rc = isValidEID(MCTP_START_VALID_EID);
1056 EXPECT_EQ(rc, true);
1057 rc = isValidEID(254);
1058 EXPECT_EQ(rc, true);
1059}
Thu Nguyenb8cf46b2024-06-15 02:44:35 +00001060
1061TEST(TrimNameForDbus, goodTest)
1062{
1063 std::string name = "Name with space";
1064 std::string_view expectedName = "Name_with__space";
1065 std::string_view result = trimNameForDbus(name);
1066 EXPECT_EQ(expectedName, result);
Pavithra Barithaya5d86cdf2024-08-20 15:08:46 +05301067 name = "Name 1\0"; // NOLINT(bugprone-string-literal-with-embedded-nul)
Thu Nguyenb8cf46b2024-06-15 02:44:35 +00001068 expectedName = "Name_1";
1069 result = trimNameForDbus(name);
1070 EXPECT_EQ(expectedName, result);
1071}
Thu Nguyena34a64b2022-03-31 08:56:39 +07001072
1073TEST(dbusPropValuesToDouble, goodTest)
1074{
1075 double value = 0;
1076 bool ret =
1077 dbusPropValuesToDouble("uint8_t", static_cast<uint8_t>(0x12), &value);
1078 EXPECT_EQ(true, ret);
1079 EXPECT_EQ(0x12, value);
1080 ret =
1081 dbusPropValuesToDouble("int16_t", static_cast<int16_t>(0x1234), &value);
1082 EXPECT_EQ(true, ret);
1083 EXPECT_EQ(0x1234, value);
1084 ret = dbusPropValuesToDouble("uint16_t", static_cast<uint16_t>(0x8234),
1085 &value);
1086 EXPECT_EQ(true, ret);
1087 EXPECT_EQ(0x8234, value);
1088 ret = dbusPropValuesToDouble("int32_t", static_cast<int32_t>(0x12345678),
1089 &value);
1090 EXPECT_EQ(true, ret);
1091 EXPECT_EQ(0x12345678, value);
1092 ret = dbusPropValuesToDouble("uint32_t", static_cast<uint32_t>(0x82345678),
1093 &value);
1094 EXPECT_EQ(true, ret);
1095 EXPECT_EQ(0x82345678, value);
1096 ret = dbusPropValuesToDouble(
1097 "int64_t", static_cast<int64_t>(0x1234567898765432), &value);
1098 EXPECT_EQ(true, ret);
1099 EXPECT_EQ(0x1234567898765432, value);
1100 ret = dbusPropValuesToDouble(
1101 "uint64_t", static_cast<uint64_t>(0x8234567898765432), &value);
1102 EXPECT_EQ(true, ret);
1103 EXPECT_EQ(0x8234567898765432, value);
1104 ret = dbusPropValuesToDouble("double", static_cast<double>(1234.5678),
1105 &value);
1106 EXPECT_EQ(true, ret);
1107 EXPECT_EQ(1234.5678, value);
1108}
1109
1110TEST(dbusPropValuesToDouble, badTest)
1111{
1112 double value = std::numeric_limits<double>::quiet_NaN();
1113 /* Type and Data variant are different */
1114 bool ret =
1115 dbusPropValuesToDouble("uint8_t", static_cast<uint16_t>(0x12), &value);
1116 EXPECT_EQ(false, ret);
1117 /* Unsupported Types */
1118 ret = dbusPropValuesToDouble("string", static_cast<std::string>("hello"),
1119 &value);
1120 EXPECT_EQ(false, ret);
1121 ret = dbusPropValuesToDouble("bool", static_cast<bool>(true), &value);
1122 EXPECT_EQ(false, ret);
1123 ret = dbusPropValuesToDouble("vector<uint8_t>",
1124 static_cast<std::string>("hello"), &value);
1125 EXPECT_EQ(false, ret);
1126 ret = dbusPropValuesToDouble("vector<string>",
1127 static_cast<std::string>("hello"), &value);
1128 EXPECT_EQ(false, ret);
1129 /* Support Type but Data Type is unsupported */
1130 ret = dbusPropValuesToDouble("double", static_cast<std::string>("hello"),
1131 &value);
1132 EXPECT_EQ(false, ret);
1133 /* Null pointer */
1134 ret = dbusPropValuesToDouble("double", static_cast<std::string>("hello"),
1135 nullptr);
1136 EXPECT_EQ(false, ret);
1137}
Dung Caob6d39432024-06-05 03:46:47 +00001138
1139TEST(FruFieldValuestring, goodTest)
1140{
1141 std::vector<uint8_t> data = {0x41, 0x6d, 0x70, 0x65, 0x72, 0x65};
1142 std::string expectedString = "Ampere";
1143 auto result = fruFieldValuestring(data.data(), data.size());
1144 EXPECT_EQ(expectedString, result);
1145}
1146
1147TEST(FruFieldValuestring, BadTest)
1148{
1149 std::vector<uint8_t> data = {0x41, 0x6d, 0x70, 0x65, 0x72, 0x65};
1150 auto result = fruFieldValuestring(data.data(), 0);
1151 EXPECT_EQ(std::nullopt, result);
1152 result = fruFieldValuestring(nullptr, data.size());
1153 EXPECT_EQ(std::nullopt, result);
1154}
1155
1156TEST(fruFieldParserU32, goodTest)
1157{
1158 std::vector<uint8_t> data = {0x10, 0x12, 0x14, 0x25};
1159 uint32_t expectedU32 = 0x25141210;
1160 auto result = fruFieldParserU32(data.data(), data.size());
1161 EXPECT_EQ(expectedU32, result.value());
1162}
1163
1164TEST(fruFieldParserU32, BadTest)
1165{
1166 std::vector<uint8_t> data = {0x10, 0x12, 0x14, 0x25};
1167 auto result = fruFieldParserU32(data.data(), data.size() - 1);
1168 EXPECT_EQ(std::nullopt, result);
1169 result = fruFieldParserU32(nullptr, data.size());
1170 EXPECT_EQ(std::nullopt, result);
1171}