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