1 #include "common/utils.hpp"
2 #include "mocked_utils.hpp"
3 
4 #include <libpldm/platform.h>
5 
6 #include <gtest/gtest.h>
7 
8 using namespace pldm::utils;
9 
10 TEST(GetInventoryObjects, testForEmptyObject)
11 {
12     ObjectValueTree result =
13         DBusHandler::getInventoryObjects<GetManagedEmptyObject>();
14     EXPECT_TRUE(result.empty());
15 }
16 
17 TEST(GetInventoryObjects, testForObject)
18 {
19     std::string path = "/foo/bar";
20     std::string service = "foo.bar";
21     auto result = DBusHandler::getInventoryObjects<GetManagedObject>();
22     EXPECT_EQ(result[path].begin()->first, service);
23     auto function =
24         std::get<bool>(result[path][service][std::string("Functional")]);
25     auto model =
26         std::get<std::string>(result[path][service][std::string("Model")]);
27     EXPECT_FALSE(result.empty());
28     EXPECT_TRUE(function);
29     EXPECT_EQ(model, std::string("1234 - 00Z"));
30 }
31 
32 TEST(decodeDate, testGooduintToDate)
33 {
34     uint64_t data = 20191212115959;
35     uint16_t year = 2019;
36     uint8_t month = 12;
37     uint8_t day = 12;
38     uint8_t hours = 11;
39     uint8_t minutes = 59;
40     uint8_t seconds = 59;
41 
42     uint16_t retyear = 0;
43     uint8_t retmonth = 0;
44     uint8_t retday = 0;
45     uint8_t rethours = 0;
46     uint8_t retminutes = 0;
47     uint8_t retseconds = 0;
48 
49     auto ret = uintToDate(data, &retyear, &retmonth, &retday, &rethours,
50                           &retminutes, &retseconds);
51 
52     EXPECT_EQ(ret, true);
53     EXPECT_EQ(year, retyear);
54     EXPECT_EQ(month, retmonth);
55     EXPECT_EQ(day, retday);
56     EXPECT_EQ(hours, rethours);
57     EXPECT_EQ(minutes, retminutes);
58     EXPECT_EQ(seconds, retseconds);
59 }
60 
61 TEST(decodeDate, testBaduintToDate)
62 {
63     uint64_t data = 10191212115959;
64 
65     uint16_t retyear = 0;
66     uint8_t retmonth = 0;
67     uint8_t retday = 0;
68     uint8_t rethours = 0;
69     uint8_t retminutes = 0;
70     uint8_t retseconds = 0;
71 
72     auto ret = uintToDate(data, &retyear, &retmonth, &retday, &rethours,
73                           &retminutes, &retseconds);
74 
75     EXPECT_EQ(ret, false);
76 }
77 
78 TEST(parseEffecterData, testGoodDecodeEffecterData)
79 {
80     std::vector<uint8_t> effecterData = {1, 1, 0, 1};
81     uint8_t effecterCount = 2;
82     set_effecter_state_field stateField0 = {1, 1};
83     set_effecter_state_field stateField1 = {0, 1};
84 
85     auto effecterField = parseEffecterData(effecterData, effecterCount);
86     EXPECT_NE(effecterField, std::nullopt);
87     EXPECT_EQ(effecterCount, effecterField->size());
88 
89     std::vector<set_effecter_state_field> stateField = effecterField.value();
90     EXPECT_EQ(stateField[0].set_request, stateField0.set_request);
91     EXPECT_EQ(stateField[0].effecter_state, stateField0.effecter_state);
92     EXPECT_EQ(stateField[1].set_request, stateField1.set_request);
93     EXPECT_EQ(stateField[1].effecter_state, stateField1.effecter_state);
94 }
95 
96 TEST(parseEffecterData, testBadDecodeEffecterData)
97 {
98     std::vector<uint8_t> effecterData = {0, 1, 0, 1, 0, 1};
99     uint8_t effecterCount = 2;
100 
101     auto effecterField = parseEffecterData(effecterData, effecterCount);
102 
103     EXPECT_EQ(effecterField, std::nullopt);
104 }
105 
106 TEST(FindStateEffecterPDR, testOneMatch)
107 {
108     auto repo = pldm_pdr_init();
109     uint8_t tid = 1;
110     uint16_t entityID = 33;
111     uint16_t stateSetId = 196;
112 
113     std::vector<uint8_t> pdr(sizeof(struct pldm_state_effecter_pdr) -
114                              sizeof(uint8_t) +
115                              sizeof(struct state_effecter_possible_states));
116 
117     auto rec = reinterpret_cast<pldm_state_effecter_pdr*>(pdr.data());
118 
119     auto state =
120         reinterpret_cast<state_effecter_possible_states*>(rec->possible_states);
121 
122     rec->hdr.type = 11;
123     rec->hdr.record_handle = 1;
124     rec->entity_type = 33;
125     rec->container_id = 0;
126     rec->composite_effecter_count = 1;
127     state->state_set_id = 196;
128     state->possible_states_size = 1;
129 
130     uint32_t handle = 0;
131     ASSERT_EQ(
132         pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
133 
134     auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
135 
136     EXPECT_EQ(pdr, record[0]);
137 
138     pldm_pdr_destroy(repo);
139 }
140 
141 TEST(FindStateEffecterPDR, testNoMatch)
142 {
143     auto repo = pldm_pdr_init();
144     uint8_t tid = 1;
145     uint16_t entityID = 44;
146     uint16_t stateSetId = 196;
147 
148     std::vector<uint8_t> pdr(sizeof(struct pldm_state_effecter_pdr) -
149                              sizeof(uint8_t) +
150                              sizeof(struct state_effecter_possible_states));
151 
152     auto rec = reinterpret_cast<pldm_state_effecter_pdr*>(pdr.data());
153 
154     auto state =
155         reinterpret_cast<state_effecter_possible_states*>(rec->possible_states);
156 
157     rec->hdr.type = 11;
158     rec->hdr.record_handle = 1;
159     rec->entity_type = 33;
160     rec->container_id = 0;
161     rec->composite_effecter_count = 1;
162     state->state_set_id = 196;
163     state->possible_states_size = 1;
164 
165     uint32_t handle = 0;
166     ASSERT_EQ(
167         pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
168 
169     auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
170 
171     EXPECT_EQ(record.empty(), true);
172 
173     pldm_pdr_destroy(repo);
174 }
175 
176 TEST(FindStateEffecterPDR, testEmptyRepo)
177 {
178     auto repo = pldm_pdr_init();
179     uint8_t tid = 1;
180     uint16_t entityID = 33;
181     uint16_t stateSetId = 196;
182 
183     std::vector<uint8_t> pdr(sizeof(struct pldm_state_effecter_pdr) -
184                              sizeof(uint8_t) +
185                              sizeof(struct state_effecter_possible_states));
186 
187     auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
188 
189     EXPECT_EQ(record.empty(), true);
190 
191     pldm_pdr_destroy(repo);
192 }
193 
194 TEST(FindStateEffecterPDR, testMoreMatch)
195 {
196     auto repo = pldm_pdr_init();
197     uint8_t tid = 1;
198 
199     std::vector<uint8_t> pdr(sizeof(struct pldm_state_effecter_pdr) -
200                              sizeof(uint8_t) +
201                              sizeof(struct state_effecter_possible_states));
202 
203     auto rec = reinterpret_cast<pldm_state_effecter_pdr*>(pdr.data());
204 
205     auto state =
206         reinterpret_cast<state_effecter_possible_states*>(rec->possible_states);
207 
208     rec->hdr.type = 11;
209     rec->hdr.record_handle = 1;
210     rec->entity_type = 31;
211     rec->container_id = 0;
212     rec->composite_effecter_count = 1;
213     state->state_set_id = 129;
214     state->possible_states_size = 1;
215 
216     uint32_t handle = 0;
217     ASSERT_EQ(
218         pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
219 
220     std::vector<uint8_t> pdr_second(
221         sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
222         sizeof(struct state_effecter_possible_states));
223 
224     auto rec_second =
225         reinterpret_cast<pldm_state_effecter_pdr*>(pdr_second.data());
226 
227     auto state_second = reinterpret_cast<state_effecter_possible_states*>(
228         rec_second->possible_states);
229 
230     rec_second->hdr.type = 11;
231     rec_second->hdr.record_handle = 2;
232     rec_second->entity_type = 31;
233     rec_second->container_id = 0;
234     rec_second->composite_effecter_count = 1;
235     state_second->state_set_id = 129;
236     state_second->possible_states_size = 1;
237 
238     handle = 0;
239     ASSERT_EQ(pldm_pdr_add_check(repo, pdr_second.data(), pdr_second.size(),
240                                  false, 1, &handle),
241               0);
242 
243     uint16_t entityID_ = 31;
244     uint16_t stateSetId_ = 129;
245 
246     auto record = findStateEffecterPDR(tid, entityID_, stateSetId_, repo);
247 
248     EXPECT_EQ(pdr, record[0]);
249     EXPECT_EQ(pdr_second, record[1]);
250 
251     pldm_pdr_destroy(repo);
252 }
253 
254 TEST(FindStateEffecterPDR, testManyNoMatch)
255 {
256     auto repo = pldm_pdr_init();
257     uint8_t tid = 1;
258     uint16_t entityID = 33;
259     uint16_t stateSetId = 196;
260 
261     std::vector<uint8_t> pdr(sizeof(struct pldm_state_effecter_pdr) -
262                              sizeof(uint8_t) +
263                              sizeof(struct state_effecter_possible_states));
264 
265     auto rec = reinterpret_cast<pldm_state_effecter_pdr*>(pdr.data());
266 
267     auto state =
268         reinterpret_cast<state_effecter_possible_states*>(rec->possible_states);
269 
270     rec->hdr.type = 11;
271     rec->hdr.record_handle = 1;
272     rec->entity_type = 34;
273     rec->container_id = 0;
274     rec->composite_effecter_count = 1;
275     state->state_set_id = 198;
276     state->possible_states_size = 1;
277 
278     uint32_t handle = 0;
279     ASSERT_EQ(
280         pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
281 
282     std::vector<uint8_t> pdr_second(
283         sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
284         sizeof(struct state_effecter_possible_states));
285 
286     auto rec_second =
287         reinterpret_cast<pldm_state_effecter_pdr*>(pdr_second.data());
288 
289     auto state_second = reinterpret_cast<state_effecter_possible_states*>(
290         rec_second->possible_states);
291 
292     rec_second->hdr.type = 11;
293     rec_second->hdr.record_handle = 2;
294     rec_second->entity_type = 39;
295     rec_second->container_id = 0;
296     rec_second->composite_effecter_count = 1;
297     state_second->state_set_id = 169;
298     state_second->possible_states_size = 1;
299 
300     handle = 0;
301     ASSERT_EQ(pldm_pdr_add_check(repo, pdr_second.data(), pdr_second.size(),
302                                  false, 1, &handle),
303               0);
304 
305     auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
306 
307     EXPECT_EQ(record.empty(), true);
308 
309     pldm_pdr_destroy(repo);
310 }
311 
312 TEST(FindStateEffecterPDR, testOneMatchOneNoMatch)
313 {
314     auto repo = pldm_pdr_init();
315     uint8_t tid = 1;
316     uint16_t entityID = 67;
317     uint16_t stateSetId = 192;
318 
319     std::vector<uint8_t> pdr(sizeof(struct pldm_state_effecter_pdr) -
320                              sizeof(uint8_t) +
321                              sizeof(struct state_effecter_possible_states));
322 
323     auto rec = reinterpret_cast<pldm_state_effecter_pdr*>(pdr.data());
324 
325     auto state =
326         reinterpret_cast<state_effecter_possible_states*>(rec->possible_states);
327 
328     rec->hdr.type = 11;
329     rec->hdr.record_handle = 1;
330     rec->entity_type = 32;
331     rec->container_id = 0;
332     rec->composite_effecter_count = 1;
333     state->state_set_id = 198;
334     state->possible_states_size = 1;
335 
336     uint32_t handle = 0;
337     ASSERT_EQ(
338         pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
339 
340     std::vector<uint8_t> pdr_second(
341         sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
342         sizeof(struct state_effecter_possible_states));
343 
344     auto rec_second =
345         reinterpret_cast<pldm_state_effecter_pdr*>(pdr_second.data());
346 
347     auto state_second = reinterpret_cast<state_effecter_possible_states*>(
348         rec_second->possible_states);
349 
350     rec_second->hdr.type = 11;
351     rec_second->hdr.record_handle = 2;
352     rec_second->entity_type = 67;
353     rec_second->container_id = 0;
354     rec_second->composite_effecter_count = 1;
355     state_second->state_set_id = 192;
356     state_second->possible_states_size = 1;
357 
358     handle = 0;
359     ASSERT_EQ(pldm_pdr_add_check(repo, pdr_second.data(), pdr_second.size(),
360                                  false, 1, &handle),
361               0);
362 
363     auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
364 
365     EXPECT_EQ(pdr_second, record[0]);
366     EXPECT_EQ(record.size(), 1);
367 
368     pldm_pdr_destroy(repo);
369 }
370 
371 TEST(FindStateEffecterPDR, testOneMatchManyNoMatch)
372 {
373     auto repo = pldm_pdr_init();
374     uint8_t tid = 1;
375     uint16_t entityID = 67;
376     uint16_t stateSetId = 192;
377 
378     std::vector<uint8_t> pdr(sizeof(struct pldm_state_effecter_pdr) -
379                              sizeof(uint8_t) +
380                              sizeof(struct state_effecter_possible_states));
381 
382     auto rec = reinterpret_cast<pldm_state_effecter_pdr*>(pdr.data());
383 
384     auto state =
385         reinterpret_cast<state_effecter_possible_states*>(rec->possible_states);
386 
387     rec->hdr.type = 11;
388     rec->hdr.record_handle = 1;
389     rec->entity_type = 32;
390     rec->container_id = 0;
391     rec->composite_effecter_count = 1;
392     state->state_set_id = 198;
393     state->possible_states_size = 1;
394 
395     uint32_t handle = 0;
396     ASSERT_EQ(
397         pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
398 
399     std::vector<uint8_t> pdr_second(
400         sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
401         sizeof(struct state_effecter_possible_states));
402 
403     auto rec_second =
404         reinterpret_cast<pldm_state_effecter_pdr*>(pdr_second.data());
405 
406     auto state_second = reinterpret_cast<state_effecter_possible_states*>(
407         rec_second->possible_states);
408 
409     rec_second->hdr.type = 11;
410     rec_second->hdr.record_handle = 2;
411     rec_second->entity_type = 67;
412     rec_second->container_id = 0;
413     rec_second->composite_effecter_count = 1;
414     state_second->state_set_id = 192;
415     state_second->possible_states_size = 1;
416 
417     handle = 0;
418     ASSERT_EQ(pldm_pdr_add_check(repo, pdr_second.data(), pdr_second.size(),
419                                  false, 1, &handle),
420               0);
421 
422     std::vector<uint8_t> pdr_third(
423         sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
424         sizeof(struct state_effecter_possible_states));
425 
426     auto rec_third =
427         reinterpret_cast<pldm_state_effecter_pdr*>(pdr_third.data());
428 
429     auto state_third = reinterpret_cast<state_effecter_possible_states*>(
430         rec_third->possible_states);
431 
432     rec_third->hdr.type = 11;
433     rec_third->hdr.record_handle = 3;
434     rec_third->entity_type = 69;
435     rec_third->container_id = 0;
436     rec_third->composite_effecter_count = 1;
437     state_third->state_set_id = 199;
438     state_third->possible_states_size = 1;
439 
440     auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
441 
442     EXPECT_EQ(pdr_second, record[0]);
443     EXPECT_EQ(record.size(), 1);
444 
445     pldm_pdr_destroy(repo);
446 }
447 
448 TEST(FindStateEffecterPDR, testCompositeEffecter)
449 {
450     auto repo = pldm_pdr_init();
451     uint8_t tid = 1;
452     uint16_t entityID = 67;
453     uint16_t stateSetId = 192;
454 
455     std::vector<uint8_t> pdr(sizeof(struct pldm_state_effecter_pdr) -
456                              sizeof(uint8_t) +
457                              sizeof(struct state_effecter_possible_states) * 3);
458 
459     auto rec = reinterpret_cast<pldm_state_effecter_pdr*>(pdr.data());
460     auto state_start = rec->possible_states;
461 
462     auto state = reinterpret_cast<state_effecter_possible_states*>(state_start);
463 
464     rec->hdr.type = 11;
465     rec->hdr.record_handle = 1;
466     rec->entity_type = 67;
467     rec->container_id = 0;
468     rec->composite_effecter_count = 3;
469     state->state_set_id = 198;
470     state->possible_states_size = 1;
471 
472     state_start += state->possible_states_size + sizeof(state->state_set_id) +
473                    sizeof(state->possible_states_size);
474     state = reinterpret_cast<state_effecter_possible_states*>(state_start);
475     state->state_set_id = 193;
476     state->possible_states_size = 1;
477 
478     state_start += state->possible_states_size + sizeof(state->state_set_id) +
479                    sizeof(state->possible_states_size);
480     state = reinterpret_cast<state_effecter_possible_states*>(state_start);
481     state->state_set_id = 192;
482     state->possible_states_size = 1;
483 
484     uint32_t handle = 0;
485     ASSERT_EQ(
486         pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
487 
488     auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
489 
490     EXPECT_EQ(pdr, record[0]);
491 
492     pldm_pdr_destroy(repo);
493 }
494 
495 TEST(FindStateEffecterPDR, testNoMatchCompositeEffecter)
496 {
497     auto repo = pldm_pdr_init();
498     uint8_t tid = 1;
499     uint16_t entityID = 67;
500     uint16_t stateSetId = 192;
501 
502     std::vector<uint8_t> pdr(sizeof(struct pldm_state_effecter_pdr) -
503                              sizeof(uint8_t) +
504                              sizeof(struct state_effecter_possible_states) * 3);
505 
506     auto rec = reinterpret_cast<pldm_state_effecter_pdr*>(pdr.data());
507     auto state_start = rec->possible_states;
508 
509     auto state = reinterpret_cast<state_effecter_possible_states*>(state_start);
510 
511     rec->hdr.type = 11;
512     rec->hdr.record_handle = 1;
513     rec->entity_type = 34;
514     rec->container_id = 0;
515     rec->composite_effecter_count = 3;
516     state->state_set_id = 198;
517     state->possible_states_size = 1;
518 
519     state_start += state->possible_states_size + sizeof(state->state_set_id) +
520                    sizeof(state->possible_states_size);
521     state = reinterpret_cast<state_effecter_possible_states*>(state_start);
522     state->state_set_id = 193;
523     state->possible_states_size = 1;
524 
525     state_start += state->possible_states_size + sizeof(state->state_set_id) +
526                    sizeof(state->possible_states_size);
527     state = reinterpret_cast<state_effecter_possible_states*>(state_start);
528     state->state_set_id = 123;
529     state->possible_states_size = 1;
530 
531     uint32_t handle = 0;
532     ASSERT_EQ(
533         pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
534 
535     auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
536 
537     EXPECT_EQ(record.empty(), true);
538 
539     pldm_pdr_destroy(repo);
540 }
541 
542 TEST(FindStateSensorPDR, testOneMatch)
543 {
544     auto repo = pldm_pdr_init();
545     uint8_t tid = 1;
546     uint16_t entityID = 5;
547     uint16_t stateSetId = 1;
548 
549     std::vector<uint8_t> pdr(sizeof(struct pldm_state_sensor_pdr) -
550                              sizeof(uint8_t) +
551                              sizeof(struct state_sensor_possible_states));
552 
553     auto rec = reinterpret_cast<pldm_state_sensor_pdr*>(pdr.data());
554 
555     auto state =
556         reinterpret_cast<state_sensor_possible_states*>(rec->possible_states);
557 
558     rec->hdr.type = 4;
559     rec->hdr.record_handle = 1;
560     rec->entity_type = 5;
561     rec->container_id = 0;
562     rec->composite_sensor_count = 1;
563     state->state_set_id = 1;
564     state->possible_states_size = 1;
565 
566     uint32_t handle = 0;
567     ASSERT_EQ(
568         pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
569 
570     auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
571 
572     EXPECT_EQ(pdr, record[0]);
573 
574     pldm_pdr_destroy(repo);
575 }
576 
577 TEST(FindStateSensorPDR, testNoMatch)
578 {
579     auto repo = pldm_pdr_init();
580     uint8_t tid = 1;
581     uint16_t entityID = 5;
582     uint16_t stateSetId = 1;
583 
584     std::vector<uint8_t> pdr(sizeof(struct pldm_state_sensor_pdr) -
585                              sizeof(uint8_t) +
586                              sizeof(struct state_sensor_possible_states));
587 
588     auto rec = reinterpret_cast<pldm_state_sensor_pdr*>(pdr.data());
589 
590     auto state =
591         reinterpret_cast<state_sensor_possible_states*>(rec->possible_states);
592 
593     rec->hdr.type = 4;
594     rec->hdr.record_handle = 1;
595     rec->entity_type = 55;
596     rec->container_id = 0;
597     rec->composite_sensor_count = 1;
598     state->state_set_id = 1;
599     state->possible_states_size = 1;
600 
601     uint32_t handle = 0;
602     ASSERT_EQ(
603         pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
604 
605     auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
606 
607     EXPECT_EQ(record.empty(), true);
608 
609     pldm_pdr_destroy(repo);
610 }
611 
612 TEST(FindStateSensorPDR, testEmptyRepo)
613 {
614     auto repo = pldm_pdr_init();
615     uint8_t tid = 1;
616     uint16_t entityID = 5;
617     uint16_t stateSetId = 1;
618 
619     std::vector<uint8_t> pdr(sizeof(struct pldm_state_sensor_pdr) -
620                              sizeof(uint8_t) +
621                              sizeof(struct state_sensor_possible_states));
622 
623     auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
624 
625     EXPECT_EQ(record.empty(), true);
626 
627     pldm_pdr_destroy(repo);
628 }
629 
630 TEST(FindStateSensorPDR, testMoreMatch)
631 {
632     auto repo = pldm_pdr_init();
633     uint8_t tid = 1;
634 
635     std::vector<uint8_t> pdr(sizeof(struct pldm_state_sensor_pdr) -
636                              sizeof(uint8_t) +
637                              sizeof(struct state_sensor_possible_states));
638 
639     auto rec = reinterpret_cast<pldm_state_sensor_pdr*>(pdr.data());
640 
641     auto state =
642         reinterpret_cast<state_sensor_possible_states*>(rec->possible_states);
643 
644     rec->hdr.type = 4;
645     rec->hdr.record_handle = 1;
646     rec->entity_type = 5;
647     rec->container_id = 0;
648     rec->composite_sensor_count = 1;
649     state->state_set_id = 1;
650     state->possible_states_size = 1;
651 
652     uint32_t handle = 0;
653     ASSERT_EQ(
654         pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
655 
656     std::vector<uint8_t> pdr_second(
657         sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
658         sizeof(struct state_sensor_possible_states));
659 
660     auto rec_second =
661         reinterpret_cast<pldm_state_sensor_pdr*>(pdr_second.data());
662 
663     auto state_second = reinterpret_cast<state_sensor_possible_states*>(
664         rec_second->possible_states);
665 
666     rec_second->hdr.type = 4;
667     rec_second->hdr.record_handle = 2;
668     rec_second->entity_type = 5;
669     rec_second->container_id = 0;
670     rec_second->composite_sensor_count = 1;
671     state_second->state_set_id = 1;
672     state_second->possible_states_size = 1;
673 
674     handle = 0;
675     ASSERT_EQ(pldm_pdr_add_check(repo, pdr_second.data(), pdr_second.size(),
676                                  false, 1, &handle),
677               0);
678 
679     uint16_t entityID_ = 5;
680     uint16_t stateSetId_ = 1;
681 
682     auto record = findStateSensorPDR(tid, entityID_, stateSetId_, repo);
683 
684     EXPECT_EQ(pdr, record[0]);
685     EXPECT_EQ(pdr_second, record[1]);
686 
687     pldm_pdr_destroy(repo);
688 }
689 
690 TEST(FindStateSensorPDR, testManyNoMatch)
691 {
692     auto repo = pldm_pdr_init();
693     uint8_t tid = 1;
694     uint16_t entityID = 5;
695     uint16_t stateSetId = 1;
696 
697     std::vector<uint8_t> pdr(sizeof(struct pldm_state_sensor_pdr) -
698                              sizeof(uint8_t) +
699                              sizeof(struct state_sensor_possible_states));
700 
701     auto rec = reinterpret_cast<pldm_state_sensor_pdr*>(pdr.data());
702 
703     auto state =
704         reinterpret_cast<state_sensor_possible_states*>(rec->possible_states);
705 
706     rec->hdr.type = 4;
707     rec->hdr.record_handle = 1;
708     rec->entity_type = 56;
709     rec->container_id = 0;
710     rec->composite_sensor_count = 1;
711     state->state_set_id = 2;
712     state->possible_states_size = 1;
713 
714     uint32_t handle = 0;
715     ASSERT_EQ(
716         pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
717 
718     std::vector<uint8_t> pdr_second(
719         sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
720         sizeof(struct state_sensor_possible_states));
721 
722     auto rec_second =
723         reinterpret_cast<pldm_state_sensor_pdr*>(pdr_second.data());
724 
725     auto state_second = reinterpret_cast<state_sensor_possible_states*>(
726         rec_second->possible_states);
727 
728     rec_second->hdr.type = 4;
729     rec_second->hdr.record_handle = 2;
730     rec_second->entity_type = 66;
731     rec_second->container_id = 0;
732     rec_second->composite_sensor_count = 1;
733     state_second->state_set_id = 3;
734     state_second->possible_states_size = 1;
735 
736     handle = 0;
737     ASSERT_EQ(pldm_pdr_add_check(repo, pdr_second.data(), pdr_second.size(),
738                                  false, 1, &handle),
739               0);
740 
741     auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
742 
743     EXPECT_EQ(record.empty(), true);
744 
745     pldm_pdr_destroy(repo);
746 }
747 
748 TEST(FindStateSensorPDR, testOneMatchOneNoMatch)
749 {
750     auto repo = pldm_pdr_init();
751     uint8_t tid = 1;
752     uint16_t entityID = 5;
753     uint16_t stateSetId = 1;
754 
755     std::vector<uint8_t> pdr(sizeof(struct pldm_state_sensor_pdr) -
756                              sizeof(uint8_t) +
757                              sizeof(struct state_sensor_possible_states));
758 
759     auto rec = reinterpret_cast<pldm_state_sensor_pdr*>(pdr.data());
760 
761     auto state =
762         reinterpret_cast<state_sensor_possible_states*>(rec->possible_states);
763 
764     rec->hdr.type = 4;
765     rec->hdr.record_handle = 1;
766     rec->entity_type = 10;
767     rec->container_id = 0;
768     rec->composite_sensor_count = 1;
769     state->state_set_id = 20;
770     state->possible_states_size = 1;
771 
772     uint32_t handle = 0;
773     ASSERT_EQ(
774         pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
775 
776     std::vector<uint8_t> pdr_second(
777         sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
778         sizeof(struct state_sensor_possible_states));
779 
780     auto rec_second =
781         reinterpret_cast<pldm_state_sensor_pdr*>(pdr_second.data());
782 
783     auto state_second = reinterpret_cast<state_sensor_possible_states*>(
784         rec_second->possible_states);
785 
786     rec_second->hdr.type = 4;
787     rec_second->hdr.record_handle = 2;
788     rec_second->entity_type = 5;
789     rec_second->container_id = 0;
790     rec_second->composite_sensor_count = 1;
791     state_second->state_set_id = 1;
792     state_second->possible_states_size = 1;
793 
794     handle = 0;
795     ASSERT_EQ(pldm_pdr_add_check(repo, pdr_second.data(), pdr_second.size(),
796                                  false, 1, &handle),
797               0);
798 
799     auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
800 
801     EXPECT_EQ(pdr_second, record[0]);
802     EXPECT_EQ(record.size(), 1);
803 
804     pldm_pdr_destroy(repo);
805 }
806 
807 TEST(FindStateSensorPDR, testOneMatchManyNoMatch)
808 {
809     auto repo = pldm_pdr_init();
810     uint8_t tid = 1;
811     uint16_t entityID = 5;
812     uint16_t stateSetId = 1;
813 
814     std::vector<uint8_t> pdr(sizeof(struct pldm_state_sensor_pdr) -
815                              sizeof(uint8_t) +
816                              sizeof(struct state_sensor_possible_states));
817 
818     auto rec = reinterpret_cast<pldm_state_sensor_pdr*>(pdr.data());
819 
820     auto state =
821         reinterpret_cast<state_sensor_possible_states*>(rec->possible_states);
822 
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 
831     uint32_t handle = 0;
832     ASSERT_EQ(
833         pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
834 
835     std::vector<uint8_t> pdr_second(
836         sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
837         sizeof(struct state_sensor_possible_states));
838 
839     auto rec_second =
840         reinterpret_cast<pldm_state_sensor_pdr*>(pdr_second.data());
841 
842     auto state_second = reinterpret_cast<state_sensor_possible_states*>(
843         rec_second->possible_states);
844 
845     rec_second->hdr.type = 4;
846     rec_second->hdr.record_handle = 2;
847     rec_second->entity_type = 5;
848     rec_second->container_id = 0;
849     rec_second->composite_sensor_count = 1;
850     state_second->state_set_id = 1;
851     state_second->possible_states_size = 1;
852 
853     handle = 0;
854     ASSERT_EQ(pldm_pdr_add_check(repo, pdr_second.data(), pdr_second.size(),
855                                  false, 1, &handle),
856               0);
857 
858     std::vector<uint8_t> pdr_third(sizeof(struct pldm_state_sensor_pdr) -
859                                    sizeof(uint8_t) +
860                                    sizeof(struct state_sensor_possible_states));
861 
862     auto rec_third = reinterpret_cast<pldm_state_sensor_pdr*>(pdr_third.data());
863 
864     auto state_third = reinterpret_cast<state_sensor_possible_states*>(
865         rec_third->possible_states);
866 
867     rec_third->hdr.type = 4;
868     rec_third->hdr.record_handle = 3;
869     rec_third->entity_type = 7;
870     rec_third->container_id = 0;
871     rec_third->composite_sensor_count = 1;
872     state_third->state_set_id = 12;
873     state_third->possible_states_size = 1;
874 
875     auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
876 
877     EXPECT_EQ(pdr_second, record[0]);
878     EXPECT_EQ(record.size(), 1);
879 
880     pldm_pdr_destroy(repo);
881 }
882 
883 TEST(FindStateSensorPDR, testCompositeSensor)
884 {
885     auto repo = pldm_pdr_init();
886     uint8_t tid = 1;
887     uint16_t entityID = 5;
888     uint16_t stateSetId = 1;
889 
890     std::vector<uint8_t> pdr(sizeof(struct pldm_state_sensor_pdr) -
891                              sizeof(uint8_t) +
892                              sizeof(struct state_sensor_possible_states) * 3);
893 
894     auto rec = reinterpret_cast<pldm_state_sensor_pdr*>(pdr.data());
895     auto state_start = rec->possible_states;
896 
897     auto state = reinterpret_cast<state_sensor_possible_states*>(state_start);
898 
899     rec->hdr.type = 4;
900     rec->hdr.record_handle = 1;
901     rec->entity_type = 5;
902     rec->container_id = 0;
903     rec->composite_sensor_count = 3;
904     state->state_set_id = 2;
905     state->possible_states_size = 1;
906 
907     state_start += state->possible_states_size + sizeof(state->state_set_id) +
908                    sizeof(state->possible_states_size);
909     state = reinterpret_cast<state_sensor_possible_states*>(state_start);
910 
911     state->state_set_id = 7;
912     state->possible_states_size = 1;
913 
914     state_start += state->possible_states_size + sizeof(state->state_set_id) +
915                    sizeof(state->possible_states_size);
916     state = reinterpret_cast<state_sensor_possible_states*>(state_start);
917 
918     state->state_set_id = 1;
919     state->possible_states_size = 1;
920 
921     uint32_t handle = 0;
922     ASSERT_EQ(
923         pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
924 
925     auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
926 
927     EXPECT_EQ(pdr, record[0]);
928 
929     pldm_pdr_destroy(repo);
930 }
931 
932 TEST(FindStateSensorPDR, testNoMatchCompositeSensor)
933 {
934     auto repo = pldm_pdr_init();
935     uint8_t tid = 1;
936     uint16_t entityID = 5;
937     uint16_t stateSetId = 1;
938 
939     std::vector<uint8_t> pdr(sizeof(struct pldm_state_sensor_pdr) -
940                              sizeof(uint8_t) +
941                              sizeof(struct state_sensor_possible_states) * 3);
942 
943     auto rec = reinterpret_cast<pldm_state_sensor_pdr*>(pdr.data());
944     auto state_start = rec->possible_states;
945 
946     auto state = reinterpret_cast<state_sensor_possible_states*>(state_start);
947 
948     rec->hdr.type = 4;
949     rec->hdr.record_handle = 1;
950     rec->entity_type = 21;
951     rec->container_id = 0;
952     rec->composite_sensor_count = 3;
953     state->state_set_id = 15;
954     state->possible_states_size = 1;
955 
956     state_start += state->possible_states_size + sizeof(state->state_set_id) +
957                    sizeof(state->possible_states_size);
958     state = reinterpret_cast<state_sensor_possible_states*>(state_start);
959     state->state_set_id = 19;
960     state->possible_states_size = 1;
961 
962     state_start += state->possible_states_size + sizeof(state->state_set_id) +
963                    sizeof(state->possible_states_size);
964     state = reinterpret_cast<state_sensor_possible_states*>(state_start);
965     state->state_set_id = 39;
966     state->possible_states_size = 1;
967 
968     uint32_t handle = 0;
969     ASSERT_EQ(
970         pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
971 
972     auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
973 
974     EXPECT_EQ(record.empty(), true);
975 
976     pldm_pdr_destroy(repo);
977 }
978 
979 TEST(toString, allTestCases)
980 {
981     variable_field buffer{};
982     constexpr std::string_view str1{};
983     auto returnStr1 = toString(buffer);
984     EXPECT_EQ(returnStr1, str1);
985 
986     constexpr std::string_view str2{"0penBmc"};
987     constexpr std::array<uint8_t, 7> bufferArr1{0x30, 0x70, 0x65, 0x6e,
988                                                 0x42, 0x6d, 0x63};
989     buffer.ptr = bufferArr1.data();
990     buffer.length = bufferArr1.size();
991     auto returnStr2 = toString(buffer);
992     EXPECT_EQ(returnStr2, str2);
993 
994     constexpr std::string_view str3{"0pen mc"};
995     // \xa0 - the non-breaking space in ISO-8859-1
996     constexpr std::array<uint8_t, 7> bufferArr2{0x30, 0x70, 0x65, 0x6e,
997                                                 0xa0, 0x6d, 0x63};
998     buffer.ptr = bufferArr2.data();
999     buffer.length = bufferArr2.size();
1000     auto returnStr3 = toString(buffer);
1001     EXPECT_EQ(returnStr3, str3);
1002 }
1003 
1004 TEST(Split, allTestCases)
1005 {
1006     std::string s1 = "aa,bb,cc,dd";
1007     auto results1 = split(s1, ",");
1008     EXPECT_EQ(results1[0], "aa");
1009     EXPECT_EQ(results1[1], "bb");
1010     EXPECT_EQ(results1[2], "cc");
1011     EXPECT_EQ(results1[3], "dd");
1012 
1013     std::string s2 = "aa||bb||cc||dd";
1014     auto results2 = split(s2, "||");
1015     EXPECT_EQ(results2[0], "aa");
1016     EXPECT_EQ(results2[1], "bb");
1017     EXPECT_EQ(results2[2], "cc");
1018     EXPECT_EQ(results2[3], "dd");
1019 
1020     std::string s3 = " aa || bb||cc|| dd";
1021     auto results3 = split(s3, "||", " ");
1022     EXPECT_EQ(results3[0], "aa");
1023     EXPECT_EQ(results3[1], "bb");
1024     EXPECT_EQ(results3[2], "cc");
1025     EXPECT_EQ(results3[3], "dd");
1026 
1027     std::string s4 = "aa\\\\bb\\cc\\dd";
1028     auto results4 = split(s4, "\\");
1029     EXPECT_EQ(results4[0], "aa");
1030     EXPECT_EQ(results4[1], "bb");
1031     EXPECT_EQ(results4[2], "cc");
1032     EXPECT_EQ(results4[3], "dd");
1033 
1034     std::string s5 = "aa\\";
1035     auto results5 = split(s5, "\\");
1036     EXPECT_EQ(results5[0], "aa");
1037 }
1038