| #include "common/utils.hpp" |
| |
| #include <libpldm/platform.h> |
| |
| #include <gtest/gtest.h> |
| |
| using namespace pldm::utils; |
| |
| TEST(decodeDate, testGooduintToDate) |
| { |
| uint64_t data = 20191212115959; |
| uint16_t year = 2019; |
| uint8_t month = 12; |
| uint8_t day = 12; |
| uint8_t hours = 11; |
| uint8_t minutes = 59; |
| uint8_t seconds = 59; |
| |
| uint16_t retyear = 0; |
| uint8_t retmonth = 0; |
| uint8_t retday = 0; |
| uint8_t rethours = 0; |
| uint8_t retminutes = 0; |
| uint8_t retseconds = 0; |
| |
| auto ret = uintToDate(data, &retyear, &retmonth, &retday, &rethours, |
| &retminutes, &retseconds); |
| |
| EXPECT_EQ(ret, true); |
| EXPECT_EQ(year, retyear); |
| EXPECT_EQ(month, retmonth); |
| EXPECT_EQ(day, retday); |
| EXPECT_EQ(hours, rethours); |
| EXPECT_EQ(minutes, retminutes); |
| EXPECT_EQ(seconds, retseconds); |
| } |
| |
| TEST(decodeDate, testBaduintToDate) |
| { |
| uint64_t data = 10191212115959; |
| |
| uint16_t retyear = 0; |
| uint8_t retmonth = 0; |
| uint8_t retday = 0; |
| uint8_t rethours = 0; |
| uint8_t retminutes = 0; |
| uint8_t retseconds = 0; |
| |
| auto ret = uintToDate(data, &retyear, &retmonth, &retday, &rethours, |
| &retminutes, &retseconds); |
| |
| EXPECT_EQ(ret, false); |
| } |
| |
| TEST(parseEffecterData, testGoodDecodeEffecterData) |
| { |
| std::vector<uint8_t> effecterData = {1, 1, 0, 1}; |
| uint8_t effecterCount = 2; |
| set_effecter_state_field stateField0 = {1, 1}; |
| set_effecter_state_field stateField1 = {0, 1}; |
| |
| auto effecterField = parseEffecterData(effecterData, effecterCount); |
| EXPECT_NE(effecterField, std::nullopt); |
| EXPECT_EQ(effecterCount, effecterField->size()); |
| |
| std::vector<set_effecter_state_field> stateField = effecterField.value(); |
| EXPECT_EQ(stateField[0].set_request, stateField0.set_request); |
| EXPECT_EQ(stateField[0].effecter_state, stateField0.effecter_state); |
| EXPECT_EQ(stateField[1].set_request, stateField1.set_request); |
| EXPECT_EQ(stateField[1].effecter_state, stateField1.effecter_state); |
| } |
| |
| TEST(parseEffecterData, testBadDecodeEffecterData) |
| { |
| std::vector<uint8_t> effecterData = {0, 1, 0, 1, 0, 1}; |
| uint8_t effecterCount = 2; |
| |
| auto effecterField = parseEffecterData(effecterData, effecterCount); |
| |
| EXPECT_EQ(effecterField, std::nullopt); |
| } |
| |
| TEST(FindStateEffecterPDR, testOneMatch) |
| { |
| auto repo = pldm_pdr_init(); |
| uint8_t tid = 1; |
| uint16_t entityID = 33; |
| uint16_t stateSetId = 196; |
| |
| std::vector<uint8_t> pdr(sizeof(struct pldm_state_effecter_pdr) - |
| sizeof(uint8_t) + |
| sizeof(struct state_effecter_possible_states)); |
| |
| auto rec = reinterpret_cast<pldm_state_effecter_pdr*>(pdr.data()); |
| |
| auto state = |
| reinterpret_cast<state_effecter_possible_states*>(rec->possible_states); |
| |
| rec->hdr.type = 11; |
| rec->hdr.record_handle = 1; |
| rec->entity_type = 33; |
| rec->container_id = 0; |
| rec->composite_effecter_count = 1; |
| state->state_set_id = 196; |
| state->possible_states_size = 1; |
| |
| uint32_t handle = 0; |
| ASSERT_EQ( |
| pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0); |
| |
| auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo); |
| |
| EXPECT_EQ(pdr, record[0]); |
| |
| pldm_pdr_destroy(repo); |
| } |
| |
| TEST(FindStateEffecterPDR, testNoMatch) |
| { |
| auto repo = pldm_pdr_init(); |
| uint8_t tid = 1; |
| uint16_t entityID = 44; |
| uint16_t stateSetId = 196; |
| |
| std::vector<uint8_t> pdr(sizeof(struct pldm_state_effecter_pdr) - |
| sizeof(uint8_t) + |
| sizeof(struct state_effecter_possible_states)); |
| |
| auto rec = reinterpret_cast<pldm_state_effecter_pdr*>(pdr.data()); |
| |
| auto state = |
| reinterpret_cast<state_effecter_possible_states*>(rec->possible_states); |
| |
| rec->hdr.type = 11; |
| rec->hdr.record_handle = 1; |
| rec->entity_type = 33; |
| rec->container_id = 0; |
| rec->composite_effecter_count = 1; |
| state->state_set_id = 196; |
| state->possible_states_size = 1; |
| |
| uint32_t handle = 0; |
| ASSERT_EQ( |
| pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0); |
| |
| auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo); |
| |
| EXPECT_EQ(record.empty(), true); |
| |
| pldm_pdr_destroy(repo); |
| } |
| |
| TEST(FindStateEffecterPDR, testEmptyRepo) |
| { |
| auto repo = pldm_pdr_init(); |
| uint8_t tid = 1; |
| uint16_t entityID = 33; |
| uint16_t stateSetId = 196; |
| |
| std::vector<uint8_t> pdr(sizeof(struct pldm_state_effecter_pdr) - |
| sizeof(uint8_t) + |
| sizeof(struct state_effecter_possible_states)); |
| |
| auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo); |
| |
| EXPECT_EQ(record.empty(), true); |
| |
| pldm_pdr_destroy(repo); |
| } |
| |
| TEST(FindStateEffecterPDR, testMoreMatch) |
| { |
| auto repo = pldm_pdr_init(); |
| uint8_t tid = 1; |
| |
| std::vector<uint8_t> pdr(sizeof(struct pldm_state_effecter_pdr) - |
| sizeof(uint8_t) + |
| sizeof(struct state_effecter_possible_states)); |
| |
| auto rec = reinterpret_cast<pldm_state_effecter_pdr*>(pdr.data()); |
| |
| auto state = |
| reinterpret_cast<state_effecter_possible_states*>(rec->possible_states); |
| |
| rec->hdr.type = 11; |
| rec->hdr.record_handle = 1; |
| rec->entity_type = 31; |
| rec->container_id = 0; |
| rec->composite_effecter_count = 1; |
| state->state_set_id = 129; |
| state->possible_states_size = 1; |
| |
| uint32_t handle = 0; |
| ASSERT_EQ( |
| pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0); |
| |
| std::vector<uint8_t> pdr_second( |
| sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) + |
| sizeof(struct state_effecter_possible_states)); |
| |
| auto rec_second = |
| reinterpret_cast<pldm_state_effecter_pdr*>(pdr_second.data()); |
| |
| auto state_second = reinterpret_cast<state_effecter_possible_states*>( |
| rec_second->possible_states); |
| |
| rec_second->hdr.type = 11; |
| rec_second->hdr.record_handle = 2; |
| rec_second->entity_type = 31; |
| rec_second->container_id = 0; |
| rec_second->composite_effecter_count = 1; |
| state_second->state_set_id = 129; |
| state_second->possible_states_size = 1; |
| |
| handle = 0; |
| ASSERT_EQ(pldm_pdr_add_check(repo, pdr_second.data(), pdr_second.size(), |
| false, 1, &handle), |
| 0); |
| |
| uint16_t entityID_ = 31; |
| uint16_t stateSetId_ = 129; |
| |
| auto record = findStateEffecterPDR(tid, entityID_, stateSetId_, repo); |
| |
| EXPECT_EQ(pdr, record[0]); |
| EXPECT_EQ(pdr_second, record[1]); |
| |
| pldm_pdr_destroy(repo); |
| } |
| |
| TEST(FindStateEffecterPDR, testManyNoMatch) |
| { |
| auto repo = pldm_pdr_init(); |
| uint8_t tid = 1; |
| uint16_t entityID = 33; |
| uint16_t stateSetId = 196; |
| |
| std::vector<uint8_t> pdr(sizeof(struct pldm_state_effecter_pdr) - |
| sizeof(uint8_t) + |
| sizeof(struct state_effecter_possible_states)); |
| |
| auto rec = reinterpret_cast<pldm_state_effecter_pdr*>(pdr.data()); |
| |
| auto state = |
| reinterpret_cast<state_effecter_possible_states*>(rec->possible_states); |
| |
| rec->hdr.type = 11; |
| rec->hdr.record_handle = 1; |
| rec->entity_type = 34; |
| rec->container_id = 0; |
| rec->composite_effecter_count = 1; |
| state->state_set_id = 198; |
| state->possible_states_size = 1; |
| |
| uint32_t handle = 0; |
| ASSERT_EQ( |
| pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0); |
| |
| std::vector<uint8_t> pdr_second( |
| sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) + |
| sizeof(struct state_effecter_possible_states)); |
| |
| auto rec_second = |
| reinterpret_cast<pldm_state_effecter_pdr*>(pdr_second.data()); |
| |
| auto state_second = reinterpret_cast<state_effecter_possible_states*>( |
| rec_second->possible_states); |
| |
| rec_second->hdr.type = 11; |
| rec_second->hdr.record_handle = 2; |
| rec_second->entity_type = 39; |
| rec_second->container_id = 0; |
| rec_second->composite_effecter_count = 1; |
| state_second->state_set_id = 169; |
| state_second->possible_states_size = 1; |
| |
| handle = 0; |
| ASSERT_EQ(pldm_pdr_add_check(repo, pdr_second.data(), pdr_second.size(), |
| false, 1, &handle), |
| 0); |
| |
| auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo); |
| |
| EXPECT_EQ(record.empty(), true); |
| |
| pldm_pdr_destroy(repo); |
| } |
| |
| TEST(FindStateEffecterPDR, testOneMatchOneNoMatch) |
| { |
| auto repo = pldm_pdr_init(); |
| uint8_t tid = 1; |
| uint16_t entityID = 67; |
| uint16_t stateSetId = 192; |
| |
| std::vector<uint8_t> pdr(sizeof(struct pldm_state_effecter_pdr) - |
| sizeof(uint8_t) + |
| sizeof(struct state_effecter_possible_states)); |
| |
| auto rec = reinterpret_cast<pldm_state_effecter_pdr*>(pdr.data()); |
| |
| auto state = |
| reinterpret_cast<state_effecter_possible_states*>(rec->possible_states); |
| |
| rec->hdr.type = 11; |
| rec->hdr.record_handle = 1; |
| rec->entity_type = 32; |
| rec->container_id = 0; |
| rec->composite_effecter_count = 1; |
| state->state_set_id = 198; |
| state->possible_states_size = 1; |
| |
| uint32_t handle = 0; |
| ASSERT_EQ( |
| pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0); |
| |
| std::vector<uint8_t> pdr_second( |
| sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) + |
| sizeof(struct state_effecter_possible_states)); |
| |
| auto rec_second = |
| reinterpret_cast<pldm_state_effecter_pdr*>(pdr_second.data()); |
| |
| auto state_second = reinterpret_cast<state_effecter_possible_states*>( |
| rec_second->possible_states); |
| |
| rec_second->hdr.type = 11; |
| rec_second->hdr.record_handle = 2; |
| rec_second->entity_type = 67; |
| rec_second->container_id = 0; |
| rec_second->composite_effecter_count = 1; |
| state_second->state_set_id = 192; |
| state_second->possible_states_size = 1; |
| |
| handle = 0; |
| ASSERT_EQ(pldm_pdr_add_check(repo, pdr_second.data(), pdr_second.size(), |
| false, 1, &handle), |
| 0); |
| |
| auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo); |
| |
| EXPECT_EQ(pdr_second, record[0]); |
| EXPECT_EQ(record.size(), 1); |
| |
| pldm_pdr_destroy(repo); |
| } |
| |
| TEST(FindStateEffecterPDR, testOneMatchManyNoMatch) |
| { |
| auto repo = pldm_pdr_init(); |
| uint8_t tid = 1; |
| uint16_t entityID = 67; |
| uint16_t stateSetId = 192; |
| |
| std::vector<uint8_t> pdr(sizeof(struct pldm_state_effecter_pdr) - |
| sizeof(uint8_t) + |
| sizeof(struct state_effecter_possible_states)); |
| |
| auto rec = reinterpret_cast<pldm_state_effecter_pdr*>(pdr.data()); |
| |
| auto state = |
| reinterpret_cast<state_effecter_possible_states*>(rec->possible_states); |
| |
| rec->hdr.type = 11; |
| rec->hdr.record_handle = 1; |
| rec->entity_type = 32; |
| rec->container_id = 0; |
| rec->composite_effecter_count = 1; |
| state->state_set_id = 198; |
| state->possible_states_size = 1; |
| |
| uint32_t handle = 0; |
| ASSERT_EQ( |
| pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0); |
| |
| std::vector<uint8_t> pdr_second( |
| sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) + |
| sizeof(struct state_effecter_possible_states)); |
| |
| auto rec_second = |
| reinterpret_cast<pldm_state_effecter_pdr*>(pdr_second.data()); |
| |
| auto state_second = reinterpret_cast<state_effecter_possible_states*>( |
| rec_second->possible_states); |
| |
| rec_second->hdr.type = 11; |
| rec_second->hdr.record_handle = 2; |
| rec_second->entity_type = 67; |
| rec_second->container_id = 0; |
| rec_second->composite_effecter_count = 1; |
| state_second->state_set_id = 192; |
| state_second->possible_states_size = 1; |
| |
| handle = 0; |
| ASSERT_EQ(pldm_pdr_add_check(repo, pdr_second.data(), pdr_second.size(), |
| false, 1, &handle), |
| 0); |
| |
| std::vector<uint8_t> pdr_third( |
| sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) + |
| sizeof(struct state_effecter_possible_states)); |
| |
| auto rec_third = |
| reinterpret_cast<pldm_state_effecter_pdr*>(pdr_third.data()); |
| |
| auto state_third = reinterpret_cast<state_effecter_possible_states*>( |
| rec_third->possible_states); |
| |
| rec_third->hdr.type = 11; |
| rec_third->hdr.record_handle = 3; |
| rec_third->entity_type = 69; |
| rec_third->container_id = 0; |
| rec_third->composite_effecter_count = 1; |
| state_third->state_set_id = 199; |
| state_third->possible_states_size = 1; |
| |
| auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo); |
| |
| EXPECT_EQ(pdr_second, record[0]); |
| EXPECT_EQ(record.size(), 1); |
| |
| pldm_pdr_destroy(repo); |
| } |
| |
| TEST(FindStateEffecterPDR, testCompositeEffecter) |
| { |
| auto repo = pldm_pdr_init(); |
| uint8_t tid = 1; |
| uint16_t entityID = 67; |
| uint16_t stateSetId = 192; |
| |
| std::vector<uint8_t> pdr(sizeof(struct pldm_state_effecter_pdr) - |
| sizeof(uint8_t) + |
| sizeof(struct state_effecter_possible_states) * 3); |
| |
| auto rec = reinterpret_cast<pldm_state_effecter_pdr*>(pdr.data()); |
| auto state_start = rec->possible_states; |
| |
| auto state = reinterpret_cast<state_effecter_possible_states*>(state_start); |
| |
| rec->hdr.type = 11; |
| rec->hdr.record_handle = 1; |
| rec->entity_type = 67; |
| rec->container_id = 0; |
| rec->composite_effecter_count = 3; |
| state->state_set_id = 198; |
| state->possible_states_size = 1; |
| |
| state_start += state->possible_states_size + sizeof(state->state_set_id) + |
| sizeof(state->possible_states_size); |
| state = reinterpret_cast<state_effecter_possible_states*>(state_start); |
| state->state_set_id = 193; |
| state->possible_states_size = 1; |
| |
| state_start += state->possible_states_size + sizeof(state->state_set_id) + |
| sizeof(state->possible_states_size); |
| state = reinterpret_cast<state_effecter_possible_states*>(state_start); |
| state->state_set_id = 192; |
| state->possible_states_size = 1; |
| |
| uint32_t handle = 0; |
| ASSERT_EQ( |
| pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0); |
| |
| auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo); |
| |
| EXPECT_EQ(pdr, record[0]); |
| |
| pldm_pdr_destroy(repo); |
| } |
| |
| TEST(FindStateEffecterPDR, testNoMatchCompositeEffecter) |
| { |
| auto repo = pldm_pdr_init(); |
| uint8_t tid = 1; |
| uint16_t entityID = 67; |
| uint16_t stateSetId = 192; |
| |
| std::vector<uint8_t> pdr(sizeof(struct pldm_state_effecter_pdr) - |
| sizeof(uint8_t) + |
| sizeof(struct state_effecter_possible_states) * 3); |
| |
| auto rec = reinterpret_cast<pldm_state_effecter_pdr*>(pdr.data()); |
| auto state_start = rec->possible_states; |
| |
| auto state = reinterpret_cast<state_effecter_possible_states*>(state_start); |
| |
| rec->hdr.type = 11; |
| rec->hdr.record_handle = 1; |
| rec->entity_type = 34; |
| rec->container_id = 0; |
| rec->composite_effecter_count = 3; |
| state->state_set_id = 198; |
| state->possible_states_size = 1; |
| |
| state_start += state->possible_states_size + sizeof(state->state_set_id) + |
| sizeof(state->possible_states_size); |
| state = reinterpret_cast<state_effecter_possible_states*>(state_start); |
| state->state_set_id = 193; |
| state->possible_states_size = 1; |
| |
| state_start += state->possible_states_size + sizeof(state->state_set_id) + |
| sizeof(state->possible_states_size); |
| state = reinterpret_cast<state_effecter_possible_states*>(state_start); |
| state->state_set_id = 123; |
| state->possible_states_size = 1; |
| |
| uint32_t handle = 0; |
| ASSERT_EQ( |
| pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0); |
| |
| auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo); |
| |
| EXPECT_EQ(record.empty(), true); |
| |
| pldm_pdr_destroy(repo); |
| } |
| |
| TEST(FindStateSensorPDR, testOneMatch) |
| { |
| auto repo = pldm_pdr_init(); |
| uint8_t tid = 1; |
| uint16_t entityID = 5; |
| uint16_t stateSetId = 1; |
| |
| std::vector<uint8_t> pdr(sizeof(struct pldm_state_sensor_pdr) - |
| sizeof(uint8_t) + |
| sizeof(struct state_sensor_possible_states)); |
| |
| auto rec = reinterpret_cast<pldm_state_sensor_pdr*>(pdr.data()); |
| |
| auto state = |
| reinterpret_cast<state_sensor_possible_states*>(rec->possible_states); |
| |
| rec->hdr.type = 4; |
| rec->hdr.record_handle = 1; |
| rec->entity_type = 5; |
| rec->container_id = 0; |
| rec->composite_sensor_count = 1; |
| state->state_set_id = 1; |
| state->possible_states_size = 1; |
| |
| uint32_t handle = 0; |
| ASSERT_EQ( |
| pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0); |
| |
| auto record = findStateSensorPDR(tid, entityID, stateSetId, repo); |
| |
| EXPECT_EQ(pdr, record[0]); |
| |
| pldm_pdr_destroy(repo); |
| } |
| |
| TEST(FindStateSensorPDR, testNoMatch) |
| { |
| auto repo = pldm_pdr_init(); |
| uint8_t tid = 1; |
| uint16_t entityID = 5; |
| uint16_t stateSetId = 1; |
| |
| std::vector<uint8_t> pdr(sizeof(struct pldm_state_sensor_pdr) - |
| sizeof(uint8_t) + |
| sizeof(struct state_sensor_possible_states)); |
| |
| auto rec = reinterpret_cast<pldm_state_sensor_pdr*>(pdr.data()); |
| |
| auto state = |
| reinterpret_cast<state_sensor_possible_states*>(rec->possible_states); |
| |
| rec->hdr.type = 4; |
| rec->hdr.record_handle = 1; |
| rec->entity_type = 55; |
| rec->container_id = 0; |
| rec->composite_sensor_count = 1; |
| state->state_set_id = 1; |
| state->possible_states_size = 1; |
| |
| uint32_t handle = 0; |
| ASSERT_EQ( |
| pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0); |
| |
| auto record = findStateSensorPDR(tid, entityID, stateSetId, repo); |
| |
| EXPECT_EQ(record.empty(), true); |
| |
| pldm_pdr_destroy(repo); |
| } |
| |
| TEST(FindStateSensorPDR, testEmptyRepo) |
| { |
| auto repo = pldm_pdr_init(); |
| uint8_t tid = 1; |
| uint16_t entityID = 5; |
| uint16_t stateSetId = 1; |
| |
| std::vector<uint8_t> pdr(sizeof(struct pldm_state_sensor_pdr) - |
| sizeof(uint8_t) + |
| sizeof(struct state_sensor_possible_states)); |
| |
| auto record = findStateSensorPDR(tid, entityID, stateSetId, repo); |
| |
| EXPECT_EQ(record.empty(), true); |
| |
| pldm_pdr_destroy(repo); |
| } |
| |
| TEST(FindStateSensorPDR, testMoreMatch) |
| { |
| auto repo = pldm_pdr_init(); |
| uint8_t tid = 1; |
| |
| std::vector<uint8_t> pdr(sizeof(struct pldm_state_sensor_pdr) - |
| sizeof(uint8_t) + |
| sizeof(struct state_sensor_possible_states)); |
| |
| auto rec = reinterpret_cast<pldm_state_sensor_pdr*>(pdr.data()); |
| |
| auto state = |
| reinterpret_cast<state_sensor_possible_states*>(rec->possible_states); |
| |
| rec->hdr.type = 4; |
| rec->hdr.record_handle = 1; |
| rec->entity_type = 5; |
| rec->container_id = 0; |
| rec->composite_sensor_count = 1; |
| state->state_set_id = 1; |
| state->possible_states_size = 1; |
| |
| uint32_t handle = 0; |
| ASSERT_EQ( |
| pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0); |
| |
| std::vector<uint8_t> pdr_second( |
| sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) + |
| sizeof(struct state_sensor_possible_states)); |
| |
| auto rec_second = |
| reinterpret_cast<pldm_state_sensor_pdr*>(pdr_second.data()); |
| |
| auto state_second = reinterpret_cast<state_sensor_possible_states*>( |
| rec_second->possible_states); |
| |
| rec_second->hdr.type = 4; |
| rec_second->hdr.record_handle = 2; |
| rec_second->entity_type = 5; |
| rec_second->container_id = 0; |
| rec_second->composite_sensor_count = 1; |
| state_second->state_set_id = 1; |
| state_second->possible_states_size = 1; |
| |
| handle = 0; |
| ASSERT_EQ(pldm_pdr_add_check(repo, pdr_second.data(), pdr_second.size(), |
| false, 1, &handle), |
| 0); |
| |
| uint16_t entityID_ = 5; |
| uint16_t stateSetId_ = 1; |
| |
| auto record = findStateSensorPDR(tid, entityID_, stateSetId_, repo); |
| |
| EXPECT_EQ(pdr, record[0]); |
| EXPECT_EQ(pdr_second, record[1]); |
| |
| pldm_pdr_destroy(repo); |
| } |
| |
| TEST(FindStateSensorPDR, testManyNoMatch) |
| { |
| auto repo = pldm_pdr_init(); |
| uint8_t tid = 1; |
| uint16_t entityID = 5; |
| uint16_t stateSetId = 1; |
| |
| std::vector<uint8_t> pdr(sizeof(struct pldm_state_sensor_pdr) - |
| sizeof(uint8_t) + |
| sizeof(struct state_sensor_possible_states)); |
| |
| auto rec = reinterpret_cast<pldm_state_sensor_pdr*>(pdr.data()); |
| |
| auto state = |
| reinterpret_cast<state_sensor_possible_states*>(rec->possible_states); |
| |
| rec->hdr.type = 4; |
| rec->hdr.record_handle = 1; |
| rec->entity_type = 56; |
| rec->container_id = 0; |
| rec->composite_sensor_count = 1; |
| state->state_set_id = 2; |
| state->possible_states_size = 1; |
| |
| uint32_t handle = 0; |
| ASSERT_EQ( |
| pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0); |
| |
| std::vector<uint8_t> pdr_second( |
| sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) + |
| sizeof(struct state_sensor_possible_states)); |
| |
| auto rec_second = |
| reinterpret_cast<pldm_state_sensor_pdr*>(pdr_second.data()); |
| |
| auto state_second = reinterpret_cast<state_sensor_possible_states*>( |
| rec_second->possible_states); |
| |
| rec_second->hdr.type = 4; |
| rec_second->hdr.record_handle = 2; |
| rec_second->entity_type = 66; |
| rec_second->container_id = 0; |
| rec_second->composite_sensor_count = 1; |
| state_second->state_set_id = 3; |
| state_second->possible_states_size = 1; |
| |
| handle = 0; |
| ASSERT_EQ(pldm_pdr_add_check(repo, pdr_second.data(), pdr_second.size(), |
| false, 1, &handle), |
| 0); |
| |
| auto record = findStateSensorPDR(tid, entityID, stateSetId, repo); |
| |
| EXPECT_EQ(record.empty(), true); |
| |
| pldm_pdr_destroy(repo); |
| } |
| |
| TEST(FindStateSensorPDR, testOneMatchOneNoMatch) |
| { |
| auto repo = pldm_pdr_init(); |
| uint8_t tid = 1; |
| uint16_t entityID = 5; |
| uint16_t stateSetId = 1; |
| |
| std::vector<uint8_t> pdr(sizeof(struct pldm_state_sensor_pdr) - |
| sizeof(uint8_t) + |
| sizeof(struct state_sensor_possible_states)); |
| |
| auto rec = reinterpret_cast<pldm_state_sensor_pdr*>(pdr.data()); |
| |
| auto state = |
| reinterpret_cast<state_sensor_possible_states*>(rec->possible_states); |
| |
| rec->hdr.type = 4; |
| rec->hdr.record_handle = 1; |
| rec->entity_type = 10; |
| rec->container_id = 0; |
| rec->composite_sensor_count = 1; |
| state->state_set_id = 20; |
| state->possible_states_size = 1; |
| |
| uint32_t handle = 0; |
| ASSERT_EQ( |
| pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0); |
| |
| std::vector<uint8_t> pdr_second( |
| sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) + |
| sizeof(struct state_sensor_possible_states)); |
| |
| auto rec_second = |
| reinterpret_cast<pldm_state_sensor_pdr*>(pdr_second.data()); |
| |
| auto state_second = reinterpret_cast<state_sensor_possible_states*>( |
| rec_second->possible_states); |
| |
| rec_second->hdr.type = 4; |
| rec_second->hdr.record_handle = 2; |
| rec_second->entity_type = 5; |
| rec_second->container_id = 0; |
| rec_second->composite_sensor_count = 1; |
| state_second->state_set_id = 1; |
| state_second->possible_states_size = 1; |
| |
| handle = 0; |
| ASSERT_EQ(pldm_pdr_add_check(repo, pdr_second.data(), pdr_second.size(), |
| false, 1, &handle), |
| 0); |
| |
| auto record = findStateSensorPDR(tid, entityID, stateSetId, repo); |
| |
| EXPECT_EQ(pdr_second, record[0]); |
| EXPECT_EQ(record.size(), 1); |
| |
| pldm_pdr_destroy(repo); |
| } |
| |
| TEST(FindStateSensorPDR, testOneMatchManyNoMatch) |
| { |
| auto repo = pldm_pdr_init(); |
| uint8_t tid = 1; |
| uint16_t entityID = 5; |
| uint16_t stateSetId = 1; |
| |
| std::vector<uint8_t> pdr(sizeof(struct pldm_state_sensor_pdr) - |
| sizeof(uint8_t) + |
| sizeof(struct state_sensor_possible_states)); |
| |
| auto rec = reinterpret_cast<pldm_state_sensor_pdr*>(pdr.data()); |
| |
| auto state = |
| reinterpret_cast<state_sensor_possible_states*>(rec->possible_states); |
| |
| rec->hdr.type = 4; |
| rec->hdr.record_handle = 1; |
| rec->entity_type = 6; |
| rec->container_id = 0; |
| rec->composite_sensor_count = 1; |
| state->state_set_id = 9; |
| state->possible_states_size = 1; |
| |
| uint32_t handle = 0; |
| ASSERT_EQ( |
| pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0); |
| |
| std::vector<uint8_t> pdr_second( |
| sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) + |
| sizeof(struct state_sensor_possible_states)); |
| |
| auto rec_second = |
| reinterpret_cast<pldm_state_sensor_pdr*>(pdr_second.data()); |
| |
| auto state_second = reinterpret_cast<state_sensor_possible_states*>( |
| rec_second->possible_states); |
| |
| rec_second->hdr.type = 4; |
| rec_second->hdr.record_handle = 2; |
| rec_second->entity_type = 5; |
| rec_second->container_id = 0; |
| rec_second->composite_sensor_count = 1; |
| state_second->state_set_id = 1; |
| state_second->possible_states_size = 1; |
| |
| handle = 0; |
| ASSERT_EQ(pldm_pdr_add_check(repo, pdr_second.data(), pdr_second.size(), |
| false, 1, &handle), |
| 0); |
| |
| std::vector<uint8_t> pdr_third(sizeof(struct pldm_state_sensor_pdr) - |
| sizeof(uint8_t) + |
| sizeof(struct state_sensor_possible_states)); |
| |
| auto rec_third = reinterpret_cast<pldm_state_sensor_pdr*>(pdr_third.data()); |
| |
| auto state_third = reinterpret_cast<state_sensor_possible_states*>( |
| rec_third->possible_states); |
| |
| rec_third->hdr.type = 4; |
| rec_third->hdr.record_handle = 3; |
| rec_third->entity_type = 7; |
| rec_third->container_id = 0; |
| rec_third->composite_sensor_count = 1; |
| state_third->state_set_id = 12; |
| state_third->possible_states_size = 1; |
| |
| auto record = findStateSensorPDR(tid, entityID, stateSetId, repo); |
| |
| EXPECT_EQ(pdr_second, record[0]); |
| EXPECT_EQ(record.size(), 1); |
| |
| pldm_pdr_destroy(repo); |
| } |
| |
| TEST(FindStateSensorPDR, testCompositeSensor) |
| { |
| auto repo = pldm_pdr_init(); |
| uint8_t tid = 1; |
| uint16_t entityID = 5; |
| uint16_t stateSetId = 1; |
| |
| std::vector<uint8_t> pdr(sizeof(struct pldm_state_sensor_pdr) - |
| sizeof(uint8_t) + |
| sizeof(struct state_sensor_possible_states) * 3); |
| |
| auto rec = reinterpret_cast<pldm_state_sensor_pdr*>(pdr.data()); |
| auto state_start = rec->possible_states; |
| |
| auto state = reinterpret_cast<state_sensor_possible_states*>(state_start); |
| |
| rec->hdr.type = 4; |
| rec->hdr.record_handle = 1; |
| rec->entity_type = 5; |
| rec->container_id = 0; |
| rec->composite_sensor_count = 3; |
| state->state_set_id = 2; |
| state->possible_states_size = 1; |
| |
| state_start += state->possible_states_size + sizeof(state->state_set_id) + |
| sizeof(state->possible_states_size); |
| state = reinterpret_cast<state_sensor_possible_states*>(state_start); |
| |
| state->state_set_id = 7; |
| state->possible_states_size = 1; |
| |
| state_start += state->possible_states_size + sizeof(state->state_set_id) + |
| sizeof(state->possible_states_size); |
| state = reinterpret_cast<state_sensor_possible_states*>(state_start); |
| |
| state->state_set_id = 1; |
| state->possible_states_size = 1; |
| |
| uint32_t handle = 0; |
| ASSERT_EQ( |
| pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0); |
| |
| auto record = findStateSensorPDR(tid, entityID, stateSetId, repo); |
| |
| EXPECT_EQ(pdr, record[0]); |
| |
| pldm_pdr_destroy(repo); |
| } |
| |
| TEST(FindStateSensorPDR, testNoMatchCompositeSensor) |
| { |
| auto repo = pldm_pdr_init(); |
| uint8_t tid = 1; |
| uint16_t entityID = 5; |
| uint16_t stateSetId = 1; |
| |
| std::vector<uint8_t> pdr(sizeof(struct pldm_state_sensor_pdr) - |
| sizeof(uint8_t) + |
| sizeof(struct state_sensor_possible_states) * 3); |
| |
| auto rec = reinterpret_cast<pldm_state_sensor_pdr*>(pdr.data()); |
| auto state_start = rec->possible_states; |
| |
| auto state = reinterpret_cast<state_sensor_possible_states*>(state_start); |
| |
| rec->hdr.type = 4; |
| rec->hdr.record_handle = 1; |
| rec->entity_type = 21; |
| rec->container_id = 0; |
| rec->composite_sensor_count = 3; |
| state->state_set_id = 15; |
| state->possible_states_size = 1; |
| |
| state_start += state->possible_states_size + sizeof(state->state_set_id) + |
| sizeof(state->possible_states_size); |
| state = reinterpret_cast<state_sensor_possible_states*>(state_start); |
| state->state_set_id = 19; |
| state->possible_states_size = 1; |
| |
| state_start += state->possible_states_size + sizeof(state->state_set_id) + |
| sizeof(state->possible_states_size); |
| state = reinterpret_cast<state_sensor_possible_states*>(state_start); |
| state->state_set_id = 39; |
| state->possible_states_size = 1; |
| |
| uint32_t handle = 0; |
| ASSERT_EQ( |
| pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0); |
| |
| auto record = findStateSensorPDR(tid, entityID, stateSetId, repo); |
| |
| EXPECT_EQ(record.empty(), true); |
| |
| pldm_pdr_destroy(repo); |
| } |
| |
| TEST(toString, allTestCases) |
| { |
| variable_field buffer{}; |
| constexpr std::string_view str1{}; |
| auto returnStr1 = toString(buffer); |
| EXPECT_EQ(returnStr1, str1); |
| |
| constexpr std::string_view str2{"0penBmc"}; |
| constexpr std::array<uint8_t, 7> bufferArr1{0x30, 0x70, 0x65, 0x6e, |
| 0x42, 0x6d, 0x63}; |
| buffer.ptr = bufferArr1.data(); |
| buffer.length = bufferArr1.size(); |
| auto returnStr2 = toString(buffer); |
| EXPECT_EQ(returnStr2, str2); |
| |
| constexpr std::string_view str3{"0pen mc"}; |
| // \xa0 - the non-breaking space in ISO-8859-1 |
| constexpr std::array<uint8_t, 7> bufferArr2{0x30, 0x70, 0x65, 0x6e, |
| 0xa0, 0x6d, 0x63}; |
| buffer.ptr = bufferArr2.data(); |
| buffer.length = bufferArr2.size(); |
| auto returnStr3 = toString(buffer); |
| EXPECT_EQ(returnStr3, str3); |
| } |
| |
| TEST(Split, allTestCases) |
| { |
| std::string s1 = "aa,bb,cc,dd"; |
| auto results1 = split(s1, ","); |
| EXPECT_EQ(results1[0], "aa"); |
| EXPECT_EQ(results1[1], "bb"); |
| EXPECT_EQ(results1[2], "cc"); |
| EXPECT_EQ(results1[3], "dd"); |
| |
| std::string s2 = "aa||bb||cc||dd"; |
| auto results2 = split(s2, "||"); |
| EXPECT_EQ(results2[0], "aa"); |
| EXPECT_EQ(results2[1], "bb"); |
| EXPECT_EQ(results2[2], "cc"); |
| EXPECT_EQ(results2[3], "dd"); |
| |
| std::string s3 = " aa || bb||cc|| dd"; |
| auto results3 = split(s3, "||", " "); |
| EXPECT_EQ(results3[0], "aa"); |
| EXPECT_EQ(results3[1], "bb"); |
| EXPECT_EQ(results3[2], "cc"); |
| EXPECT_EQ(results3[3], "dd"); |
| |
| std::string s4 = "aa\\\\bb\\cc\\dd"; |
| auto results4 = split(s4, "\\"); |
| EXPECT_EQ(results4[0], "aa"); |
| EXPECT_EQ(results4[1], "bb"); |
| EXPECT_EQ(results4[2], "cc"); |
| EXPECT_EQ(results4[3], "dd"); |
| |
| std::string s5 = "aa\\"; |
| auto results5 = split(s5, "\\"); |
| EXPECT_EQ(results5[0], "aa"); |
| } |
| |
| TEST(utf16BeToUtf8, testAsciiConversion) |
| { |
| // "Temp" in UTF-16BE: 0x0054, 0x0065, 0x006D, 0x0070, 0x0000 |
| const uint8_t u16Data[] = {0x00, 'T', 0x00, 'e', 0x00, |
| 'm', 0x00, 'p', 0x00, 0x00}; |
| size_t bytesConsumed = 0; |
| std::string result = utf16BeToUtf8(u16Data, sizeof(u16Data), bytesConsumed); |
| EXPECT_EQ(result, "Temp"); |
| EXPECT_EQ(bytesConsumed, 10); |
| } |
| |
| TEST(utf16BeToUtf8, testMultibyteAndSurrogates) |
| { |
| // U+00E9 ('é' -> UTF-8 0xC3 0xA9) = 0x00E9 |
| // U+4E2D ('中' -> UTF-8 0xE4 0xB8 0xAD) = 0x4E2D |
| // U+1F600 (😀 -> UTF-8 0xF0 0x9F 0x98 0x80) = High surrogate 0xD83D, Low |
| // surrogate 0xDE00 Null terminator = 0x0000 |
| const uint8_t u16Data[] = { |
| 0x00, 0xE9, // é |
| 0x4E, 0x2D, // 中 |
| 0xD8, 0x3D, 0xDE, 0x00, // 😀 |
| 0x00, 0x00 // null |
| }; |
| size_t bytesConsumed = 0; |
| std::string result = utf16BeToUtf8(u16Data, sizeof(u16Data), bytesConsumed); |
| EXPECT_EQ(result, "é中😀"); |
| EXPECT_EQ(bytesConsumed, sizeof(u16Data)); |
| } |
| |
| TEST(utf16BeToUtf8, testTruncationAndNullPtr) |
| { |
| size_t bytesConsumed = 0; |
| std::string resultNull = utf16BeToUtf8(nullptr, 10, bytesConsumed); |
| EXPECT_EQ(resultNull, ""); |
| EXPECT_EQ(bytesConsumed, 0); |
| |
| // Buffer with maxBytes < 2 |
| const uint8_t u16Data[] = {0x00, 'A', 0x00, 'B', 0x00, 'C'}; |
| std::string result0 = utf16BeToUtf8(u16Data, 0, bytesConsumed); |
| EXPECT_EQ(result0, ""); |
| EXPECT_EQ(bytesConsumed, 0); |
| |
| std::string result1 = utf16BeToUtf8(u16Data, 1, bytesConsumed); |
| EXPECT_EQ(result1, ""); |
| EXPECT_EQ(bytesConsumed, 0); |
| |
| // Odd byte count (3 bytes: consumes 1 UTF-16 code unit = 2 bytes) |
| std::string result3 = utf16BeToUtf8(u16Data, 3, bytesConsumed); |
| EXPECT_EQ(result3, "A"); |
| EXPECT_EQ(bytesConsumed, 2); |
| |
| // Buffer without null terminator, truncated at 4 bytes (2 characters) |
| std::string result4 = utf16BeToUtf8(u16Data, 4, bytesConsumed); |
| EXPECT_EQ(result4, "AB"); |
| EXPECT_EQ(bytesConsumed, 4); |
| } |
| |
| TEST(utf16BeToUtf8, testEmptyUtf16String) |
| { |
| // Empty UTF-16 string containing only null terminator: 0x0000 |
| const uint8_t u16Data[] = {0x00, 0x00}; |
| size_t bytesConsumed = 0; |
| std::string result = utf16BeToUtf8(u16Data, sizeof(u16Data), bytesConsumed); |
| EXPECT_EQ(result, ""); |
| EXPECT_EQ(bytesConsumed, 2); |
| } |
| |
| TEST(utf16BeToUtf8, testUnpairedSurrogates) |
| { |
| // Unpaired high surrogate at end of buffer |
| const uint8_t u16High[] = {0xD8, 0x00}; |
| size_t bytesConsumed = 0; |
| std::string resultHigh = utf16BeToUtf8(u16High, sizeof(u16High), |
| bytesConsumed); |
| EXPECT_EQ(resultHigh, "\xEF\xBF\xBD"); // U+FFFD Replacement Character |
| EXPECT_EQ(bytesConsumed, 2); |
| |
| // Unpaired high surrogate followed by non-low surrogate |
| const uint8_t u16HighFollowedByAscii[] = {0xD8, 0x00, 0x00, |
| 'A', 0x00, 0x00}; |
| bytesConsumed = 0; |
| std::string resultHighAscii = utf16BeToUtf8( |
| u16HighFollowedByAscii, sizeof(u16HighFollowedByAscii), bytesConsumed); |
| EXPECT_EQ(resultHighAscii, "\xEF\xBF\xBD" |
| "A"); |
| EXPECT_EQ(bytesConsumed, 6); |
| |
| // Unpaired low surrogate |
| const uint8_t u16Low[] = {0xDC, 0x00, 0x00, 0x00}; |
| bytesConsumed = 0; |
| std::string resultLow = utf16BeToUtf8(u16Low, sizeof(u16Low), |
| bytesConsumed); |
| EXPECT_EQ(resultLow, "\xEF\xBF\xBD"); |
| EXPECT_EQ(bytesConsumed, 4); |
| } |
| |
| #pragma GCC diagnostic push |
| #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| #include <codecvt> |
| #include <locale> |
| |
| TEST(utf16BeToUtf8, testMatchesLegacyWstringConvert) |
| { |
| auto legacyConvert = [](const std::u16string& u16) { |
| return std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, |
| char16_t>{} |
| .to_bytes(u16); |
| }; |
| |
| auto toUtf16BeBytes = [](const std::u16string& str) { |
| std::vector<uint8_t> bytes; |
| for (char16_t c : str) |
| { |
| bytes.push_back(static_cast<uint8_t>((c >> 8) & 0xFF)); |
| bytes.push_back(static_cast<uint8_t>(c & 0xFF)); |
| } |
| bytes.push_back(0); |
| bytes.push_back(0); |
| return bytes; |
| }; |
| |
| const std::vector<std::u16string> testCases = { |
| u"", |
| u"A", |
| u"Hello World", |
| u"Sensor_0_Volt", |
| u"Caf\u00E9 \u00C5ngstr\u00F6m", |
| u"\u041F\u0440\u0438\u0432\u0435\u0442 \u03A9\u03BC\u03AD\u03B3\u03B1", // Cyrillic & Greek |
| u"\u6E29\u5EA6\u4F20\u611F\u5668", // Chinese |
| u"\u96FB\u5727\u30BB\u30F3\u30B5\u30FC", // Japanese |
| u"\U0001F600 \U0001F680 \u26A1", // Emojis (surrogates + BMP) |
| u"\u0001 \u007F \u0080 \u07FF \u0800 \uD7FF \uE000 \uFFFF", // Boundaries |
| u"\U00010000 \U00010348 \U0010FFFF", // SMP / boundary surrogates |
| }; |
| |
| for (const auto& u16Str : testCases) |
| { |
| std::string expected = legacyConvert(u16Str); |
| auto wireBytes = toUtf16BeBytes(u16Str); |
| size_t bytesConsumed = 0; |
| std::string actual = utf16BeToUtf8(wireBytes.data(), wireBytes.size(), |
| bytesConsumed); |
| EXPECT_EQ(actual, expected); |
| EXPECT_EQ(bytesConsumed, wireBytes.size()); |
| } |
| } |
| #pragma GCC diagnostic pop |