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