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