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
TEST(GetNumPadBytesTest,NoPaddingNeeded)11 TEST(GetNumPadBytesTest, NoPaddingNeeded)
12 {
13 EXPECT_EQ(getNumPadBytes(0), 0);
14 EXPECT_EQ(getNumPadBytes(4), 0);
15 EXPECT_EQ(getNumPadBytes(8), 0);
16 EXPECT_EQ(getNumPadBytes(12), 0);
17 }
18
TEST(GetNumPadBytesTest,OneBytePadding)19 TEST(GetNumPadBytesTest, OneBytePadding)
20 {
21 EXPECT_EQ(getNumPadBytes(3), 1);
22 EXPECT_EQ(getNumPadBytes(7), 1);
23 EXPECT_EQ(getNumPadBytes(11), 1);
24 }
25
TEST(GetNumPadBytesTest,TwoBytesPadding)26 TEST(GetNumPadBytesTest, TwoBytesPadding)
27 {
28 EXPECT_EQ(getNumPadBytes(2), 2);
29 EXPECT_EQ(getNumPadBytes(6), 2);
30 EXPECT_EQ(getNumPadBytes(10), 2);
31 }
32
TEST(GetNumPadBytesTest,ThreeBytesPadding)33 TEST(GetNumPadBytesTest, ThreeBytesPadding)
34 {
35 EXPECT_EQ(getNumPadBytes(1), 3);
36 EXPECT_EQ(getNumPadBytes(5), 3);
37 EXPECT_EQ(getNumPadBytes(9), 3);
38 }
39
TEST(GetNumPadBytesTest,LargeValues)40 TEST(GetNumPadBytesTest, LargeValues)
41 {
42 EXPECT_EQ(getNumPadBytes(1001), 3);
43 EXPECT_EQ(getNumPadBytes(1024), 0);
44 EXPECT_EQ(getNumPadBytes(65535), 1);
45 }
46
TEST(GetInventoryObjects,testForEmptyObject)47 TEST(GetInventoryObjects, testForEmptyObject)
48 {
49 ObjectValueTree result =
50 DBusHandler::getInventoryObjects<GetManagedEmptyObject>();
51 EXPECT_TRUE(result.empty());
52 }
53
TEST(GetInventoryObjects,testForObject)54 TEST(GetInventoryObjects, testForObject)
55 {
56 std::string path = "/foo/bar";
57 std::string service = "foo.bar";
58 auto result = DBusHandler::getInventoryObjects<GetManagedObject>();
59 EXPECT_EQ(result[path].begin()->first, service);
60 auto function =
61 std::get<bool>(result[path][service][std::string("Functional")]);
62 auto model =
63 std::get<std::string>(result[path][service][std::string("Model")]);
64 EXPECT_FALSE(result.empty());
65 EXPECT_TRUE(function);
66 EXPECT_EQ(model, std::string("1234 - 00Z"));
67 }
68
TEST(printBuffer,testprintBufferGoodPath)69 TEST(printBuffer, testprintBufferGoodPath)
70 {
71 std::vector<uint8_t> buffer = {10, 12, 14, 25, 233};
72 std::ostringstream localString;
73 auto coutBuffer = std::cout.rdbuf();
74 std::cout.rdbuf(localString.rdbuf());
75 printBuffer(false, buffer);
76 std::cout.rdbuf(coutBuffer);
77 EXPECT_EQ(localString.str(), "Rx: 0a 0c 0e 19 e9 \n");
78 localString.str("");
79 localString.clear();
80 std::cerr << localString.str() << std::endl;
81 buffer = {12, 0, 200, 12, 255};
82 std::cout.rdbuf(localString.rdbuf());
83 printBuffer(true, buffer);
84 std::cout.rdbuf(coutBuffer);
85 EXPECT_EQ(localString.str(), "Tx: 0c 00 c8 0c ff \n");
86 }
87
TEST(printBuffer,testprintBufferBadPath)88 TEST(printBuffer, testprintBufferBadPath)
89 {
90 std::vector<uint8_t> buffer = {};
91 std::ostringstream localString;
92 auto coutBuffer = std::cout.rdbuf();
93 std::cout.rdbuf(localString.rdbuf());
94 printBuffer(false, buffer);
95 EXPECT_EQ(localString.str(), "");
96 printBuffer(true, buffer);
97 std::cout.rdbuf(coutBuffer);
98 EXPECT_EQ(localString.str(), "");
99 }
100
TEST(decodeDate,testGooduintToDate)101 TEST(decodeDate, testGooduintToDate)
102 {
103 uint64_t data = 20191212115959;
104 uint16_t year = 2019;
105 uint8_t month = 12;
106 uint8_t day = 12;
107 uint8_t hours = 11;
108 uint8_t minutes = 59;
109 uint8_t seconds = 59;
110
111 uint16_t retyear = 0;
112 uint8_t retmonth = 0;
113 uint8_t retday = 0;
114 uint8_t rethours = 0;
115 uint8_t retminutes = 0;
116 uint8_t retseconds = 0;
117
118 auto ret = uintToDate(data, &retyear, &retmonth, &retday, &rethours,
119 &retminutes, &retseconds);
120
121 EXPECT_EQ(ret, true);
122 EXPECT_EQ(year, retyear);
123 EXPECT_EQ(month, retmonth);
124 EXPECT_EQ(day, retday);
125 EXPECT_EQ(hours, rethours);
126 EXPECT_EQ(minutes, retminutes);
127 EXPECT_EQ(seconds, retseconds);
128 }
129
TEST(decodeDate,testBaduintToDate)130 TEST(decodeDate, testBaduintToDate)
131 {
132 uint64_t data = 10191212115959;
133
134 uint16_t retyear = 0;
135 uint8_t retmonth = 0;
136 uint8_t retday = 0;
137 uint8_t rethours = 0;
138 uint8_t retminutes = 0;
139 uint8_t retseconds = 0;
140
141 auto ret = uintToDate(data, &retyear, &retmonth, &retday, &rethours,
142 &retminutes, &retseconds);
143
144 EXPECT_EQ(ret, false);
145 }
146
TEST(parseEffecterData,testGoodDecodeEffecterData)147 TEST(parseEffecterData, testGoodDecodeEffecterData)
148 {
149 std::vector<uint8_t> effecterData = {1, 1, 0, 1};
150 uint8_t effecterCount = 2;
151 set_effecter_state_field stateField0 = {1, 1};
152 set_effecter_state_field stateField1 = {0, 1};
153
154 auto effecterField = parseEffecterData(effecterData, effecterCount);
155 EXPECT_NE(effecterField, std::nullopt);
156 EXPECT_EQ(effecterCount, effecterField->size());
157
158 std::vector<set_effecter_state_field> stateField = effecterField.value();
159 EXPECT_EQ(stateField[0].set_request, stateField0.set_request);
160 EXPECT_EQ(stateField[0].effecter_state, stateField0.effecter_state);
161 EXPECT_EQ(stateField[1].set_request, stateField1.set_request);
162 EXPECT_EQ(stateField[1].effecter_state, stateField1.effecter_state);
163 }
164
TEST(parseEffecterData,testBadDecodeEffecterData)165 TEST(parseEffecterData, testBadDecodeEffecterData)
166 {
167 std::vector<uint8_t> effecterData = {0, 1, 0, 1, 0, 1};
168 uint8_t effecterCount = 2;
169
170 auto effecterField = parseEffecterData(effecterData, effecterCount);
171
172 EXPECT_EQ(effecterField, std::nullopt);
173 }
174
TEST(FindStateEffecterPDR,testOneMatch)175 TEST(FindStateEffecterPDR, testOneMatch)
176 {
177 auto repo = pldm_pdr_init();
178 uint8_t tid = 1;
179 uint16_t entityID = 33;
180 uint16_t stateSetId = 196;
181
182 std::vector<uint8_t> pdr(
183 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
184 sizeof(struct state_effecter_possible_states));
185
186 auto rec = new (pdr.data()) pldm_state_effecter_pdr;
187
188 auto state = new (rec->possible_states) state_effecter_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(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
200
201 auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
202
203 EXPECT_EQ(pdr, record[0]);
204
205 pldm_pdr_destroy(repo);
206 }
207
TEST(FindStateEffecterPDR,testNoMatch)208 TEST(FindStateEffecterPDR, testNoMatch)
209 {
210 auto repo = pldm_pdr_init();
211 uint8_t tid = 1;
212 uint16_t entityID = 44;
213 uint16_t stateSetId = 196;
214
215 std::vector<uint8_t> pdr(
216 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
217 sizeof(struct state_effecter_possible_states));
218
219 auto rec = new (pdr.data()) pldm_state_effecter_pdr;
220
221 auto state = new (rec->possible_states) state_effecter_possible_states;
222
223 rec->hdr.type = 11;
224 rec->hdr.record_handle = 1;
225 rec->entity_type = 33;
226 rec->container_id = 0;
227 rec->composite_effecter_count = 1;
228 state->state_set_id = 196;
229 state->possible_states_size = 1;
230
231 uint32_t handle = 0;
232 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
233
234 auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
235
236 EXPECT_EQ(record.empty(), true);
237
238 pldm_pdr_destroy(repo);
239 }
240
TEST(FindStateEffecterPDR,testEmptyRepo)241 TEST(FindStateEffecterPDR, testEmptyRepo)
242 {
243 auto repo = pldm_pdr_init();
244 uint8_t tid = 1;
245 uint16_t entityID = 33;
246 uint16_t stateSetId = 196;
247
248 std::vector<uint8_t> pdr(
249 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
250 sizeof(struct state_effecter_possible_states));
251
252 auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
253
254 EXPECT_EQ(record.empty(), true);
255
256 pldm_pdr_destroy(repo);
257 }
258
TEST(FindStateEffecterPDR,testMoreMatch)259 TEST(FindStateEffecterPDR, testMoreMatch)
260 {
261 auto repo = pldm_pdr_init();
262 uint8_t tid = 1;
263
264 std::vector<uint8_t> pdr(
265 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
266 sizeof(struct state_effecter_possible_states));
267
268 auto rec = new (pdr.data()) pldm_state_effecter_pdr;
269
270 auto state = new (rec->possible_states) state_effecter_possible_states;
271
272 rec->hdr.type = 11;
273 rec->hdr.record_handle = 1;
274 rec->entity_type = 31;
275 rec->container_id = 0;
276 rec->composite_effecter_count = 1;
277 state->state_set_id = 129;
278 state->possible_states_size = 1;
279
280 uint32_t handle = 0;
281 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
282
283 std::vector<uint8_t> pdr_second(
284 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
285 sizeof(struct state_effecter_possible_states));
286
287 auto rec_second = new (pdr_second.data()) pldm_state_effecter_pdr;
288
289 auto state_second = new (rec_second->possible_states)
290 state_effecter_possible_states;
291
292 rec_second->hdr.type = 11;
293 rec_second->hdr.record_handle = 2;
294 rec_second->entity_type = 31;
295 rec_second->container_id = 0;
296 rec_second->composite_effecter_count = 1;
297 state_second->state_set_id = 129;
298 state_second->possible_states_size = 1;
299
300 handle = 0;
301 ASSERT_EQ(pldm_pdr_add(repo, pdr_second.data(), pdr_second.size(), false, 1,
302 &handle),
303 0);
304
305 uint16_t entityID_ = 31;
306 uint16_t stateSetId_ = 129;
307
308 auto record = findStateEffecterPDR(tid, entityID_, stateSetId_, repo);
309
310 EXPECT_EQ(pdr, record[0]);
311 EXPECT_EQ(pdr_second, record[1]);
312
313 pldm_pdr_destroy(repo);
314 }
315
TEST(FindStateEffecterPDR,testManyNoMatch)316 TEST(FindStateEffecterPDR, testManyNoMatch)
317 {
318 auto repo = pldm_pdr_init();
319 uint8_t tid = 1;
320 uint16_t entityID = 33;
321 uint16_t stateSetId = 196;
322
323 std::vector<uint8_t> pdr(
324 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
325 sizeof(struct state_effecter_possible_states));
326
327 auto rec = new (pdr.data()) pldm_state_effecter_pdr;
328
329 auto state = new (rec->possible_states) state_effecter_possible_states;
330
331 rec->hdr.type = 11;
332 rec->hdr.record_handle = 1;
333 rec->entity_type = 34;
334 rec->container_id = 0;
335 rec->composite_effecter_count = 1;
336 state->state_set_id = 198;
337 state->possible_states_size = 1;
338
339 uint32_t handle = 0;
340 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
341
342 std::vector<uint8_t> pdr_second(
343 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
344 sizeof(struct state_effecter_possible_states));
345
346 auto rec_second = new (pdr_second.data()) pldm_state_effecter_pdr;
347
348 auto state_second = new (rec_second->possible_states)
349 state_effecter_possible_states;
350
351 rec_second->hdr.type = 11;
352 rec_second->hdr.record_handle = 2;
353 rec_second->entity_type = 39;
354 rec_second->container_id = 0;
355 rec_second->composite_effecter_count = 1;
356 state_second->state_set_id = 169;
357 state_second->possible_states_size = 1;
358
359 handle = 0;
360 ASSERT_EQ(pldm_pdr_add(repo, pdr_second.data(), pdr_second.size(), false, 1,
361 &handle),
362 0);
363
364 auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
365
366 EXPECT_EQ(record.empty(), true);
367
368 pldm_pdr_destroy(repo);
369 }
370
TEST(FindStateEffecterPDR,testOneMatchOneNoMatch)371 TEST(FindStateEffecterPDR, testOneMatchOneNoMatch)
372 {
373 auto repo = pldm_pdr_init();
374 uint8_t tid = 1;
375 uint16_t entityID = 67;
376 uint16_t stateSetId = 192;
377
378 std::vector<uint8_t> pdr(
379 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
380 sizeof(struct state_effecter_possible_states));
381
382 auto rec = new (pdr.data()) pldm_state_effecter_pdr;
383
384 auto state = new (rec->possible_states) state_effecter_possible_states;
385
386 rec->hdr.type = 11;
387 rec->hdr.record_handle = 1;
388 rec->entity_type = 32;
389 rec->container_id = 0;
390 rec->composite_effecter_count = 1;
391 state->state_set_id = 198;
392 state->possible_states_size = 1;
393
394 uint32_t handle = 0;
395 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
396
397 std::vector<uint8_t> pdr_second(
398 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
399 sizeof(struct state_effecter_possible_states));
400
401 auto rec_second = new (pdr_second.data()) pldm_state_effecter_pdr;
402
403 auto state_second = new (rec_second->possible_states)
404 state_effecter_possible_states;
405
406 rec_second->hdr.type = 11;
407 rec_second->hdr.record_handle = 2;
408 rec_second->entity_type = 67;
409 rec_second->container_id = 0;
410 rec_second->composite_effecter_count = 1;
411 state_second->state_set_id = 192;
412 state_second->possible_states_size = 1;
413
414 handle = 0;
415 ASSERT_EQ(pldm_pdr_add(repo, pdr_second.data(), pdr_second.size(), false, 1,
416 &handle),
417 0);
418
419 auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
420
421 EXPECT_EQ(pdr_second, record[0]);
422 EXPECT_EQ(record.size(), 1);
423
424 pldm_pdr_destroy(repo);
425 }
426
TEST(FindStateEffecterPDR,testOneMatchManyNoMatch)427 TEST(FindStateEffecterPDR, testOneMatchManyNoMatch)
428 {
429 auto repo = pldm_pdr_init();
430 uint8_t tid = 1;
431 uint16_t entityID = 67;
432 uint16_t stateSetId = 192;
433
434 std::vector<uint8_t> pdr(
435 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
436 sizeof(struct state_effecter_possible_states));
437
438 auto rec = new (pdr.data()) pldm_state_effecter_pdr;
439
440 auto state = new (rec->possible_states) state_effecter_possible_states;
441
442 rec->hdr.type = 11;
443 rec->hdr.record_handle = 1;
444 rec->entity_type = 32;
445 rec->container_id = 0;
446 rec->composite_effecter_count = 1;
447 state->state_set_id = 198;
448 state->possible_states_size = 1;
449
450 uint32_t handle = 0;
451 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
452
453 std::vector<uint8_t> pdr_second(
454 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
455 sizeof(struct state_effecter_possible_states));
456
457 auto rec_second = new (pdr_second.data()) pldm_state_effecter_pdr;
458
459 auto state_second = new (rec_second->possible_states)
460 state_effecter_possible_states;
461
462 rec_second->hdr.type = 11;
463 rec_second->hdr.record_handle = 2;
464 rec_second->entity_type = 67;
465 rec_second->container_id = 0;
466 rec_second->composite_effecter_count = 1;
467 state_second->state_set_id = 192;
468 state_second->possible_states_size = 1;
469
470 handle = 0;
471 ASSERT_EQ(pldm_pdr_add(repo, pdr_second.data(), pdr_second.size(), false, 1,
472 &handle),
473 0);
474
475 std::vector<uint8_t> pdr_third(
476 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
477 sizeof(struct state_effecter_possible_states));
478
479 auto rec_third = new (pdr_third.data()) pldm_state_effecter_pdr;
480
481 auto state_third = new (rec_third->possible_states)
482 state_effecter_possible_states;
483
484 rec_third->hdr.type = 11;
485 rec_third->hdr.record_handle = 3;
486 rec_third->entity_type = 69;
487 rec_third->container_id = 0;
488 rec_third->composite_effecter_count = 1;
489 state_third->state_set_id = 199;
490 state_third->possible_states_size = 1;
491
492 auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
493
494 EXPECT_EQ(pdr_second, record[0]);
495 EXPECT_EQ(record.size(), 1);
496
497 pldm_pdr_destroy(repo);
498 }
499
TEST(FindStateEffecterPDR,testCompositeEffecter)500 TEST(FindStateEffecterPDR, testCompositeEffecter)
501 {
502 auto repo = pldm_pdr_init();
503 uint8_t tid = 1;
504 uint16_t entityID = 67;
505 uint16_t stateSetId = 192;
506
507 std::vector<uint8_t> pdr(
508 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
509 sizeof(struct state_effecter_possible_states) * 3);
510
511 auto rec = new (pdr.data()) pldm_state_effecter_pdr;
512 auto state_start = rec->possible_states;
513
514 auto state = new (state_start) state_effecter_possible_states;
515
516 rec->hdr.type = 11;
517 rec->hdr.record_handle = 1;
518 rec->entity_type = 67;
519 rec->container_id = 0;
520 rec->composite_effecter_count = 3;
521 state->state_set_id = 198;
522 state->possible_states_size = 1;
523
524 state_start += state->possible_states_size + sizeof(state->state_set_id) +
525 sizeof(state->possible_states_size);
526 state = new (state_start) state_effecter_possible_states;
527 state->state_set_id = 193;
528 state->possible_states_size = 1;
529
530 state_start += state->possible_states_size + sizeof(state->state_set_id) +
531 sizeof(state->possible_states_size);
532 state = new (state_start) state_effecter_possible_states;
533 state->state_set_id = 192;
534 state->possible_states_size = 1;
535
536 uint32_t handle = 0;
537 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
538
539 auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
540
541 EXPECT_EQ(pdr, record[0]);
542
543 pldm_pdr_destroy(repo);
544 }
545
TEST(FindStateEffecterPDR,testNoMatchCompositeEffecter)546 TEST(FindStateEffecterPDR, testNoMatchCompositeEffecter)
547 {
548 auto repo = pldm_pdr_init();
549 uint8_t tid = 1;
550 uint16_t entityID = 67;
551 uint16_t stateSetId = 192;
552
553 std::vector<uint8_t> pdr(
554 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
555 sizeof(struct state_effecter_possible_states) * 3);
556
557 auto rec = new (pdr.data()) pldm_state_effecter_pdr;
558 auto state_start = rec->possible_states;
559
560 auto state = new (state_start) state_effecter_possible_states;
561
562 rec->hdr.type = 11;
563 rec->hdr.record_handle = 1;
564 rec->entity_type = 34;
565 rec->container_id = 0;
566 rec->composite_effecter_count = 3;
567 state->state_set_id = 198;
568 state->possible_states_size = 1;
569
570 state_start += state->possible_states_size + sizeof(state->state_set_id) +
571 sizeof(state->possible_states_size);
572 state = new (state_start) state_effecter_possible_states;
573 state->state_set_id = 193;
574 state->possible_states_size = 1;
575
576 state_start += state->possible_states_size + sizeof(state->state_set_id) +
577 sizeof(state->possible_states_size);
578 state = new (state_start) state_effecter_possible_states;
579 state->state_set_id = 123;
580 state->possible_states_size = 1;
581
582 uint32_t handle = 0;
583 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
584
585 auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
586
587 EXPECT_EQ(record.empty(), true);
588
589 pldm_pdr_destroy(repo);
590 }
591
TEST(FindStateSensorPDR,testOneMatch)592 TEST(FindStateSensorPDR, testOneMatch)
593 {
594 auto repo = pldm_pdr_init();
595 uint8_t tid = 1;
596 uint16_t entityID = 5;
597 uint16_t stateSetId = 1;
598
599 std::vector<uint8_t> pdr(
600 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
601 sizeof(struct state_sensor_possible_states));
602
603 auto rec = new (pdr.data()) pldm_state_sensor_pdr;
604
605 auto state = new (rec->possible_states) state_sensor_possible_states;
606
607 rec->hdr.type = 4;
608 rec->hdr.record_handle = 1;
609 rec->entity_type = 5;
610 rec->container_id = 0;
611 rec->composite_sensor_count = 1;
612 state->state_set_id = 1;
613 state->possible_states_size = 1;
614
615 uint32_t handle = 0;
616 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
617
618 auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
619
620 EXPECT_EQ(pdr, record[0]);
621
622 pldm_pdr_destroy(repo);
623 }
624
TEST(FindStateSensorPDR,testNoMatch)625 TEST(FindStateSensorPDR, testNoMatch)
626 {
627 auto repo = pldm_pdr_init();
628 uint8_t tid = 1;
629 uint16_t entityID = 5;
630 uint16_t stateSetId = 1;
631
632 std::vector<uint8_t> pdr(
633 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
634 sizeof(struct state_sensor_possible_states));
635
636 auto rec = new (pdr.data()) pldm_state_sensor_pdr;
637
638 auto state = new (rec->possible_states) state_sensor_possible_states;
639
640 rec->hdr.type = 4;
641 rec->hdr.record_handle = 1;
642 rec->entity_type = 55;
643 rec->container_id = 0;
644 rec->composite_sensor_count = 1;
645 state->state_set_id = 1;
646 state->possible_states_size = 1;
647
648 uint32_t handle = 0;
649 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
650
651 auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
652
653 EXPECT_EQ(record.empty(), true);
654
655 pldm_pdr_destroy(repo);
656 }
657
TEST(FindStateSensorPDR,testEmptyRepo)658 TEST(FindStateSensorPDR, testEmptyRepo)
659 {
660 auto repo = pldm_pdr_init();
661 uint8_t tid = 1;
662 uint16_t entityID = 5;
663 uint16_t stateSetId = 1;
664
665 std::vector<uint8_t> pdr(
666 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
667 sizeof(struct state_sensor_possible_states));
668
669 auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
670
671 EXPECT_EQ(record.empty(), true);
672
673 pldm_pdr_destroy(repo);
674 }
675
TEST(FindStateSensorPDR,testMoreMatch)676 TEST(FindStateSensorPDR, testMoreMatch)
677 {
678 auto repo = pldm_pdr_init();
679 uint8_t tid = 1;
680
681 std::vector<uint8_t> pdr(
682 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
683 sizeof(struct state_sensor_possible_states));
684
685 auto rec = new (pdr.data()) pldm_state_sensor_pdr;
686
687 auto state = new (rec->possible_states) state_sensor_possible_states;
688
689 rec->hdr.type = 4;
690 rec->hdr.record_handle = 1;
691 rec->entity_type = 5;
692 rec->container_id = 0;
693 rec->composite_sensor_count = 1;
694 state->state_set_id = 1;
695 state->possible_states_size = 1;
696
697 uint32_t handle = 0;
698 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
699
700 std::vector<uint8_t> pdr_second(
701 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
702 sizeof(struct state_sensor_possible_states));
703
704 auto rec_second = new (pdr_second.data()) pldm_state_sensor_pdr;
705
706 auto state_second = new (rec_second->possible_states)
707 state_sensor_possible_states;
708
709 rec_second->hdr.type = 4;
710 rec_second->hdr.record_handle = 2;
711 rec_second->entity_type = 5;
712 rec_second->container_id = 0;
713 rec_second->composite_sensor_count = 1;
714 state_second->state_set_id = 1;
715 state_second->possible_states_size = 1;
716
717 handle = 0;
718 ASSERT_EQ(pldm_pdr_add(repo, pdr_second.data(), pdr_second.size(), false, 1,
719 &handle),
720 0);
721
722 uint16_t entityID_ = 5;
723 uint16_t stateSetId_ = 1;
724
725 auto record = findStateSensorPDR(tid, entityID_, stateSetId_, repo);
726
727 EXPECT_EQ(pdr, record[0]);
728 EXPECT_EQ(pdr_second, record[1]);
729
730 pldm_pdr_destroy(repo);
731 }
732
TEST(FindStateSensorPDR,testManyNoMatch)733 TEST(FindStateSensorPDR, testManyNoMatch)
734 {
735 auto repo = pldm_pdr_init();
736 uint8_t tid = 1;
737 uint16_t entityID = 5;
738 uint16_t stateSetId = 1;
739
740 std::vector<uint8_t> pdr(
741 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
742 sizeof(struct state_sensor_possible_states));
743
744 auto rec = new (pdr.data()) pldm_state_sensor_pdr;
745
746 auto state = new (rec->possible_states) state_sensor_possible_states;
747
748 rec->hdr.type = 4;
749 rec->hdr.record_handle = 1;
750 rec->entity_type = 56;
751 rec->container_id = 0;
752 rec->composite_sensor_count = 1;
753 state->state_set_id = 2;
754 state->possible_states_size = 1;
755
756 uint32_t handle = 0;
757 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
758
759 std::vector<uint8_t> pdr_second(
760 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
761 sizeof(struct state_sensor_possible_states));
762
763 auto rec_second = new (pdr_second.data()) pldm_state_sensor_pdr;
764
765 auto state_second = new (rec_second->possible_states)
766 state_sensor_possible_states;
767
768 rec_second->hdr.type = 4;
769 rec_second->hdr.record_handle = 2;
770 rec_second->entity_type = 66;
771 rec_second->container_id = 0;
772 rec_second->composite_sensor_count = 1;
773 state_second->state_set_id = 3;
774 state_second->possible_states_size = 1;
775
776 handle = 0;
777 ASSERT_EQ(pldm_pdr_add(repo, pdr_second.data(), pdr_second.size(), false, 1,
778 &handle),
779 0);
780
781 auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
782
783 EXPECT_EQ(record.empty(), true);
784
785 pldm_pdr_destroy(repo);
786 }
787
TEST(FindStateSensorPDR,testOneMatchOneNoMatch)788 TEST(FindStateSensorPDR, testOneMatchOneNoMatch)
789 {
790 auto repo = pldm_pdr_init();
791 uint8_t tid = 1;
792 uint16_t entityID = 5;
793 uint16_t stateSetId = 1;
794
795 std::vector<uint8_t> pdr(
796 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
797 sizeof(struct state_sensor_possible_states));
798
799 auto rec = new (pdr.data()) pldm_state_sensor_pdr;
800
801 auto state = new (rec->possible_states) state_sensor_possible_states;
802
803 rec->hdr.type = 4;
804 rec->hdr.record_handle = 1;
805 rec->entity_type = 10;
806 rec->container_id = 0;
807 rec->composite_sensor_count = 1;
808 state->state_set_id = 20;
809 state->possible_states_size = 1;
810
811 uint32_t handle = 0;
812 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
813
814 std::vector<uint8_t> pdr_second(
815 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
816 sizeof(struct state_sensor_possible_states));
817
818 auto rec_second = new (pdr_second.data()) pldm_state_sensor_pdr;
819
820 auto state_second = new (rec_second->possible_states)
821 state_sensor_possible_states;
822
823 rec_second->hdr.type = 4;
824 rec_second->hdr.record_handle = 2;
825 rec_second->entity_type = 5;
826 rec_second->container_id = 0;
827 rec_second->composite_sensor_count = 1;
828 state_second->state_set_id = 1;
829 state_second->possible_states_size = 1;
830
831 handle = 0;
832 ASSERT_EQ(pldm_pdr_add(repo, pdr_second.data(), pdr_second.size(), false, 1,
833 &handle),
834 0);
835
836 auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
837
838 EXPECT_EQ(pdr_second, record[0]);
839 EXPECT_EQ(record.size(), 1);
840
841 pldm_pdr_destroy(repo);
842 }
843
TEST(FindStateSensorPDR,testOneMatchManyNoMatch)844 TEST(FindStateSensorPDR, testOneMatchManyNoMatch)
845 {
846 auto repo = pldm_pdr_init();
847 uint8_t tid = 1;
848 uint16_t entityID = 5;
849 uint16_t stateSetId = 1;
850
851 std::vector<uint8_t> pdr(
852 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
853 sizeof(struct state_sensor_possible_states));
854
855 auto rec = new (pdr.data()) pldm_state_sensor_pdr;
856
857 auto state = new (rec->possible_states) state_sensor_possible_states;
858
859 rec->hdr.type = 4;
860 rec->hdr.record_handle = 1;
861 rec->entity_type = 6;
862 rec->container_id = 0;
863 rec->composite_sensor_count = 1;
864 state->state_set_id = 9;
865 state->possible_states_size = 1;
866
867 uint32_t handle = 0;
868 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
869
870 std::vector<uint8_t> pdr_second(
871 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
872 sizeof(struct state_sensor_possible_states));
873
874 auto rec_second = new (pdr_second.data()) pldm_state_sensor_pdr;
875
876 auto state_second = new (rec_second->possible_states)
877 state_sensor_possible_states;
878
879 rec_second->hdr.type = 4;
880 rec_second->hdr.record_handle = 2;
881 rec_second->entity_type = 5;
882 rec_second->container_id = 0;
883 rec_second->composite_sensor_count = 1;
884 state_second->state_set_id = 1;
885 state_second->possible_states_size = 1;
886
887 handle = 0;
888 ASSERT_EQ(pldm_pdr_add(repo, pdr_second.data(), pdr_second.size(), false, 1,
889 &handle),
890 0);
891
892 std::vector<uint8_t> pdr_third(
893 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
894 sizeof(struct state_sensor_possible_states));
895
896 auto rec_third = new (pdr_third.data()) pldm_state_sensor_pdr;
897
898 auto state_third = new (rec_third->possible_states)
899 state_sensor_possible_states;
900
901 rec_third->hdr.type = 4;
902 rec_third->hdr.record_handle = 3;
903 rec_third->entity_type = 7;
904 rec_third->container_id = 0;
905 rec_third->composite_sensor_count = 1;
906 state_third->state_set_id = 12;
907 state_third->possible_states_size = 1;
908
909 auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
910
911 EXPECT_EQ(pdr_second, record[0]);
912 EXPECT_EQ(record.size(), 1);
913
914 pldm_pdr_destroy(repo);
915 }
916
TEST(FindStateSensorPDR,testCompositeSensor)917 TEST(FindStateSensorPDR, testCompositeSensor)
918 {
919 auto repo = pldm_pdr_init();
920 uint8_t tid = 1;
921 uint16_t entityID = 5;
922 uint16_t stateSetId = 1;
923
924 std::vector<uint8_t> pdr(
925 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
926 sizeof(struct state_sensor_possible_states) * 3);
927
928 auto rec = new (pdr.data()) pldm_state_sensor_pdr;
929 auto state_start = rec->possible_states;
930
931 auto state = new (state_start) state_sensor_possible_states;
932
933 rec->hdr.type = 4;
934 rec->hdr.record_handle = 1;
935 rec->entity_type = 5;
936 rec->container_id = 0;
937 rec->composite_sensor_count = 3;
938 state->state_set_id = 2;
939 state->possible_states_size = 1;
940
941 state_start += state->possible_states_size + sizeof(state->state_set_id) +
942 sizeof(state->possible_states_size);
943 state = new (state_start) state_sensor_possible_states;
944
945 state->state_set_id = 7;
946 state->possible_states_size = 1;
947
948 state_start += state->possible_states_size + sizeof(state->state_set_id) +
949 sizeof(state->possible_states_size);
950 state = new (state_start) state_sensor_possible_states;
951
952 state->state_set_id = 1;
953 state->possible_states_size = 1;
954
955 uint32_t handle = 0;
956 ASSERT_EQ(pldm_pdr_add(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
TEST(FindStateSensorPDR,testNoMatchCompositeSensor)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(
973 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
974 sizeof(struct state_sensor_possible_states) * 3);
975
976 auto rec = new (pdr.data()) pldm_state_sensor_pdr;
977 auto state_start = rec->possible_states;
978
979 auto state = new (state_start) state_sensor_possible_states;
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 = new (state_start) state_sensor_possible_states;
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 = new (state_start) state_sensor_possible_states;
998 state->state_set_id = 39;
999 state->possible_states_size = 1;
1000
1001 uint32_t handle = 0;
1002 ASSERT_EQ(pldm_pdr_add(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
TEST(toString,allTestCases)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
TEST(Split,allTestCases)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
TEST(ValidEID,allTestCases)1071 TEST(ValidEID, allTestCases)
1072 {
1073 auto rc = isValidEID(MCTP_ADDR_NULL);
1074 EXPECT_EQ(rc, false);
1075 rc = isValidEID(MCTP_ADDR_ANY);
1076 EXPECT_EQ(rc, false);
1077 rc = isValidEID(1);
1078 EXPECT_EQ(rc, false);
1079 rc = isValidEID(2);
1080 EXPECT_EQ(rc, false);
1081 rc = isValidEID(3);
1082 EXPECT_EQ(rc, false);
1083 rc = isValidEID(4);
1084 EXPECT_EQ(rc, false);
1085 rc = isValidEID(5);
1086 EXPECT_EQ(rc, false);
1087 rc = isValidEID(6);
1088 EXPECT_EQ(rc, false);
1089 rc = isValidEID(7);
1090 EXPECT_EQ(rc, false);
1091 rc = isValidEID(MCTP_START_VALID_EID);
1092 EXPECT_EQ(rc, true);
1093 rc = isValidEID(254);
1094 EXPECT_EQ(rc, true);
1095 }
1096
TEST(TrimNameForDbus,goodTest)1097 TEST(TrimNameForDbus, goodTest)
1098 {
1099 std::string name = "Name with space";
1100 std::string_view expectedName = "Name_with__space";
1101 std::string_view result = trimNameForDbus(name);
1102 EXPECT_EQ(expectedName, result);
1103 name = "Name 1\0"; // NOLINT(bugprone-string-literal-with-embedded-nul)
1104 expectedName = "Name_1";
1105 result = trimNameForDbus(name);
1106 EXPECT_EQ(expectedName, result);
1107 }
1108
TEST(dbusPropValuesToDouble,goodTest)1109 TEST(dbusPropValuesToDouble, goodTest)
1110 {
1111 double value = 0;
1112 bool ret =
1113 dbusPropValuesToDouble("uint8_t", static_cast<uint8_t>(0x12), &value);
1114 EXPECT_EQ(true, ret);
1115 EXPECT_EQ(0x12, value);
1116 ret =
1117 dbusPropValuesToDouble("int16_t", static_cast<int16_t>(0x1234), &value);
1118 EXPECT_EQ(true, ret);
1119 EXPECT_EQ(0x1234, value);
1120 ret = dbusPropValuesToDouble("uint16_t", static_cast<uint16_t>(0x8234),
1121 &value);
1122 EXPECT_EQ(true, ret);
1123 EXPECT_EQ(0x8234, value);
1124 ret = dbusPropValuesToDouble("int32_t", static_cast<int32_t>(0x12345678),
1125 &value);
1126 EXPECT_EQ(true, ret);
1127 EXPECT_EQ(0x12345678, value);
1128 ret = dbusPropValuesToDouble("uint32_t", static_cast<uint32_t>(0x82345678),
1129 &value);
1130 EXPECT_EQ(true, ret);
1131 EXPECT_EQ(0x82345678, value);
1132 ret = dbusPropValuesToDouble(
1133 "int64_t", static_cast<int64_t>(0x1234567898765432), &value);
1134 EXPECT_EQ(true, ret);
1135 EXPECT_EQ(0x1234567898765432, value);
1136 ret = dbusPropValuesToDouble(
1137 "uint64_t", static_cast<uint64_t>(0x8234567898765432), &value);
1138 EXPECT_EQ(true, ret);
1139 EXPECT_EQ(0x8234567898765432, value);
1140 ret = dbusPropValuesToDouble("double", static_cast<double>(1234.5678),
1141 &value);
1142 EXPECT_EQ(true, ret);
1143 EXPECT_EQ(1234.5678, value);
1144 }
1145
TEST(dbusPropValuesToDouble,badTest)1146 TEST(dbusPropValuesToDouble, badTest)
1147 {
1148 double value = std::numeric_limits<double>::quiet_NaN();
1149 /* Type and Data variant are different */
1150 bool ret =
1151 dbusPropValuesToDouble("uint8_t", static_cast<uint16_t>(0x12), &value);
1152 EXPECT_EQ(false, ret);
1153 /* Unsupported Types */
1154 ret = dbusPropValuesToDouble("string", static_cast<std::string>("hello"),
1155 &value);
1156 EXPECT_EQ(false, ret);
1157 ret = dbusPropValuesToDouble("bool", static_cast<bool>(true), &value);
1158 EXPECT_EQ(false, ret);
1159 ret = dbusPropValuesToDouble("vector<uint8_t>",
1160 static_cast<std::string>("hello"), &value);
1161 EXPECT_EQ(false, ret);
1162 ret = dbusPropValuesToDouble("vector<string>",
1163 static_cast<std::string>("hello"), &value);
1164 EXPECT_EQ(false, ret);
1165 /* Support Type but Data Type is unsupported */
1166 ret = dbusPropValuesToDouble("double", static_cast<std::string>("hello"),
1167 &value);
1168 EXPECT_EQ(false, ret);
1169 /* Null pointer */
1170 ret = dbusPropValuesToDouble("double", static_cast<std::string>("hello"),
1171 nullptr);
1172 EXPECT_EQ(false, ret);
1173 }
1174
TEST(FruFieldValuestring,goodTest)1175 TEST(FruFieldValuestring, goodTest)
1176 {
1177 std::vector<uint8_t> data = {0x41, 0x6d, 0x70, 0x65, 0x72, 0x65};
1178 std::string expectedString = "Ampere";
1179 auto result = fruFieldValuestring(data.data(), data.size());
1180 EXPECT_EQ(expectedString, result);
1181 }
1182
TEST(FruFieldValuestring,BadTest)1183 TEST(FruFieldValuestring, BadTest)
1184 {
1185 std::vector<uint8_t> data = {0x41, 0x6d, 0x70, 0x65, 0x72, 0x65};
1186 auto result = fruFieldValuestring(data.data(), 0);
1187 EXPECT_EQ(std::nullopt, result);
1188 result = fruFieldValuestring(nullptr, data.size());
1189 EXPECT_EQ(std::nullopt, result);
1190 }
1191
TEST(fruFieldParserU32,goodTest)1192 TEST(fruFieldParserU32, goodTest)
1193 {
1194 std::vector<uint8_t> data = {0x10, 0x12, 0x14, 0x25};
1195 uint32_t expectedU32 = 0x25141210;
1196 auto result = fruFieldParserU32(data.data(), data.size());
1197 EXPECT_EQ(expectedU32, result.value());
1198 }
1199
TEST(fruFieldParserU32,BadTest)1200 TEST(fruFieldParserU32, BadTest)
1201 {
1202 std::vector<uint8_t> data = {0x10, 0x12, 0x14, 0x25};
1203 auto result = fruFieldParserU32(data.data(), data.size() - 1);
1204 EXPECT_EQ(std::nullopt, result);
1205 result = fruFieldParserU32(nullptr, data.size());
1206 EXPECT_EQ(std::nullopt, result);
1207 }
1208