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