George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 1 | #include "libpldm/platform.h" |
| 2 | |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 3 | #include "libpldmresponder/pdr_utils.hpp" |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 4 | #include "libpldmresponder/platform.hpp" |
Deepak Kodihalli | c6e8fb5 | 2019-05-02 08:35:31 -0500 | [diff] [blame] | 5 | |
Deepak Kodihalli | c6e8fb5 | 2019-05-02 08:35:31 -0500 | [diff] [blame] | 6 | #include <gtest/gtest.h> |
| 7 | |
| 8 | using namespace pldm::responder; |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 9 | using namespace pldm::responder::platform; |
| 10 | using namespace pldm::responder::pdr; |
| 11 | using namespace pldm::responder::pdr_utils; |
Deepak Kodihalli | c6e8fb5 | 2019-05-02 08:35:31 -0500 | [diff] [blame] | 12 | |
George Liu | 456c9a2 | 2020-01-13 11:36:22 +0800 | [diff] [blame] | 13 | TEST(GeneratePDRByStateEffecter, testGoodJson) |
Deepak Kodihalli | c6e8fb5 | 2019-05-02 08:35:31 -0500 | [diff] [blame] | 14 | { |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 15 | auto inPDRRepo = pldm_pdr_init(); |
| 16 | auto outPDRRepo = pldm_pdr_init(); |
| 17 | Repo outRepo(outPDRRepo); |
TOM JOSEPH | d4d97a5 | 2020-03-23 14:36:34 +0530 | [diff] [blame] | 18 | Handler handler("./pdr_jsons/state_effecter/good", "./event_jsons/good", |
Tom Joseph | 33e9c7e | 2020-06-11 22:09:52 +0530 | [diff] [blame] | 19 | inPDRRepo, nullptr, nullptr); |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 20 | Repo inRepo(inPDRRepo); |
| 21 | getRepoByType(inRepo, outRepo, PLDM_STATE_EFFECTER_PDR); |
Deepak Kodihalli | c6e8fb5 | 2019-05-02 08:35:31 -0500 | [diff] [blame] | 22 | |
| 23 | // 2 entries |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 24 | ASSERT_EQ(outRepo.getRecordCount(), 2); |
Deepak Kodihalli | c6e8fb5 | 2019-05-02 08:35:31 -0500 | [diff] [blame] | 25 | |
| 26 | // Check first PDR |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 27 | pdr_utils::PdrEntry e; |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 28 | auto record1 = pdr::getRecordByHandle(outRepo, 1, e); |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 29 | ASSERT_NE(record1, nullptr); |
Deepak Kodihalli | c6e8fb5 | 2019-05-02 08:35:31 -0500 | [diff] [blame] | 30 | pldm_state_effecter_pdr* pdr = |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 31 | reinterpret_cast<pldm_state_effecter_pdr*>(e.data); |
Deepak Kodihalli | c6e8fb5 | 2019-05-02 08:35:31 -0500 | [diff] [blame] | 32 | |
| 33 | ASSERT_EQ(pdr->hdr.record_handle, 1); |
| 34 | ASSERT_EQ(pdr->hdr.version, 1); |
| 35 | ASSERT_EQ(pdr->hdr.type, PLDM_STATE_EFFECTER_PDR); |
| 36 | ASSERT_EQ(pdr->hdr.record_change_num, 0); |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 37 | ASSERT_EQ(pdr->hdr.length, 23); |
Deepak Kodihalli | c6e8fb5 | 2019-05-02 08:35:31 -0500 | [diff] [blame] | 38 | |
| 39 | ASSERT_EQ(pdr->terminus_handle, 0); |
| 40 | ASSERT_EQ(pdr->effecter_id, 1); |
| 41 | ASSERT_EQ(pdr->entity_type, 33); |
| 42 | ASSERT_EQ(pdr->entity_instance, 0); |
| 43 | ASSERT_EQ(pdr->container_id, 0); |
| 44 | ASSERT_EQ(pdr->effecter_semantic_id, 0); |
| 45 | ASSERT_EQ(pdr->effecter_init, PLDM_NO_INIT); |
| 46 | ASSERT_EQ(pdr->has_description_pdr, false); |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 47 | ASSERT_EQ(pdr->composite_effecter_count, 2); |
Deepak Kodihalli | c6e8fb5 | 2019-05-02 08:35:31 -0500 | [diff] [blame] | 48 | state_effecter_possible_states* states = |
| 49 | reinterpret_cast<state_effecter_possible_states*>(pdr->possible_states); |
| 50 | ASSERT_EQ(states->state_set_id, 196); |
| 51 | ASSERT_EQ(states->possible_states_size, 1); |
| 52 | bitfield8_t bf1{}; |
| 53 | bf1.byte = 2; |
| 54 | ASSERT_EQ(states->states[0].byte, bf1.byte); |
| 55 | |
George Liu | 1ec85d4 | 2020-02-12 16:05:32 +0800 | [diff] [blame] | 56 | const auto& [dbusMappings1, dbusValMaps1] = |
| 57 | handler.getDbusObjMaps(pdr->effecter_id); |
| 58 | ASSERT_EQ(dbusMappings1[0].objectPath, "/foo/bar"); |
Deepak Kodihalli | d078296 | 2019-05-07 16:36:47 +0530 | [diff] [blame] | 59 | |
Deepak Kodihalli | c6e8fb5 | 2019-05-02 08:35:31 -0500 | [diff] [blame] | 60 | // Check second PDR |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 61 | auto record2 = pdr::getRecordByHandle(outRepo, 2, e); |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 62 | ASSERT_NE(record2, nullptr); |
| 63 | pdr = reinterpret_cast<pldm_state_effecter_pdr*>(e.data); |
Deepak Kodihalli | c6e8fb5 | 2019-05-02 08:35:31 -0500 | [diff] [blame] | 64 | |
| 65 | ASSERT_EQ(pdr->hdr.record_handle, 2); |
| 66 | ASSERT_EQ(pdr->hdr.version, 1); |
| 67 | ASSERT_EQ(pdr->hdr.type, PLDM_STATE_EFFECTER_PDR); |
| 68 | ASSERT_EQ(pdr->hdr.record_change_num, 0); |
| 69 | ASSERT_EQ(pdr->hdr.length, 24); |
| 70 | |
| 71 | ASSERT_EQ(pdr->terminus_handle, 0); |
| 72 | ASSERT_EQ(pdr->effecter_id, 2); |
| 73 | ASSERT_EQ(pdr->entity_type, 100); |
| 74 | ASSERT_EQ(pdr->entity_instance, 0); |
| 75 | ASSERT_EQ(pdr->container_id, 0); |
| 76 | ASSERT_EQ(pdr->effecter_semantic_id, 0); |
| 77 | ASSERT_EQ(pdr->effecter_init, PLDM_NO_INIT); |
| 78 | ASSERT_EQ(pdr->has_description_pdr, false); |
| 79 | ASSERT_EQ(pdr->composite_effecter_count, 2); |
| 80 | states = |
| 81 | reinterpret_cast<state_effecter_possible_states*>(pdr->possible_states); |
| 82 | ASSERT_EQ(states->state_set_id, 197); |
| 83 | ASSERT_EQ(states->possible_states_size, 1); |
| 84 | bf1.byte = 2; |
| 85 | ASSERT_EQ(states->states[0].byte, bf1.byte); |
| 86 | states = reinterpret_cast<state_effecter_possible_states*>( |
| 87 | pdr->possible_states + sizeof(state_effecter_possible_states)); |
| 88 | ASSERT_EQ(states->state_set_id, 198); |
| 89 | ASSERT_EQ(states->possible_states_size, 2); |
| 90 | bitfield8_t bf2[2]; |
| 91 | bf2[0].byte = 38; |
| 92 | bf2[1].byte = 128; |
| 93 | ASSERT_EQ(states->states[0].byte, bf2[0].byte); |
| 94 | ASSERT_EQ(states->states[1].byte, bf2[1].byte); |
Deepak Kodihalli | d078296 | 2019-05-07 16:36:47 +0530 | [diff] [blame] | 95 | |
George Liu | 1ec85d4 | 2020-02-12 16:05:32 +0800 | [diff] [blame] | 96 | const auto& [dbusMappings2, dbusValMaps2] = |
| 97 | handler.getDbusObjMaps(pdr->effecter_id); |
| 98 | ASSERT_EQ(dbusMappings2[0].objectPath, "/foo/bar"); |
| 99 | ASSERT_EQ(dbusMappings2[1].objectPath, "/foo/bar/baz"); |
Deepak Kodihalli | d078296 | 2019-05-07 16:36:47 +0530 | [diff] [blame] | 100 | |
George Liu | 1ec85d4 | 2020-02-12 16:05:32 +0800 | [diff] [blame] | 101 | ASSERT_THROW(handler.getDbusObjMaps(0xDEAD), std::exception); |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 102 | |
| 103 | pldm_pdr_destroy(inPDRRepo); |
| 104 | pldm_pdr_destroy(outPDRRepo); |
Deepak Kodihalli | c6e8fb5 | 2019-05-02 08:35:31 -0500 | [diff] [blame] | 105 | } |
| 106 | |
George Liu | 456c9a2 | 2020-01-13 11:36:22 +0800 | [diff] [blame] | 107 | TEST(GeneratePDRByNumericEffecter, testGoodJson) |
| 108 | { |
| 109 | auto inPDRRepo = pldm_pdr_init(); |
| 110 | auto outPDRRepo = pldm_pdr_init(); |
| 111 | Repo outRepo(outPDRRepo); |
Tom Joseph | 33e9c7e | 2020-06-11 22:09:52 +0530 | [diff] [blame] | 112 | Handler handler("./pdr_jsons/state_effecter/good", "", inPDRRepo, nullptr, |
| 113 | nullptr); |
George Liu | 456c9a2 | 2020-01-13 11:36:22 +0800 | [diff] [blame] | 114 | Repo inRepo(inPDRRepo); |
| 115 | getRepoByType(inRepo, outRepo, PLDM_NUMERIC_EFFECTER_PDR); |
| 116 | |
| 117 | // 1 entries |
| 118 | ASSERT_EQ(outRepo.getRecordCount(), 1); |
| 119 | |
| 120 | // Check first PDR |
| 121 | pdr_utils::PdrEntry e; |
| 122 | auto record = pdr::getRecordByHandle(outRepo, 3, e); |
| 123 | ASSERT_NE(record, nullptr); |
| 124 | |
| 125 | pldm_numeric_effecter_value_pdr* pdr = |
| 126 | reinterpret_cast<pldm_numeric_effecter_value_pdr*>(e.data); |
| 127 | EXPECT_EQ(pdr->hdr.record_handle, 3); |
| 128 | EXPECT_EQ(pdr->hdr.version, 1); |
| 129 | EXPECT_EQ(pdr->hdr.type, PLDM_NUMERIC_EFFECTER_PDR); |
| 130 | EXPECT_EQ(pdr->hdr.record_change_num, 0); |
| 131 | EXPECT_EQ(pdr->hdr.length, |
| 132 | sizeof(pldm_numeric_effecter_value_pdr) - sizeof(pldm_pdr_hdr)); |
| 133 | |
| 134 | EXPECT_EQ(pdr->effecter_id, 3); |
| 135 | EXPECT_EQ(pdr->effecter_data_size, 4); |
| 136 | |
| 137 | const auto& [dbusMappings, dbusValMaps] = |
| 138 | handler.getDbusObjMaps(pdr->effecter_id); |
| 139 | EXPECT_EQ(dbusMappings[0].objectPath, "/foo/bar"); |
| 140 | EXPECT_EQ(dbusMappings[0].interface, "xyz.openbmc_project.Foo.Bar"); |
| 141 | EXPECT_EQ(dbusMappings[0].propertyName, "propertyName"); |
| 142 | EXPECT_EQ(dbusMappings[0].propertyType, "uint64_t"); |
| 143 | |
| 144 | pldm_pdr_destroy(inPDRRepo); |
| 145 | pldm_pdr_destroy(outPDRRepo); |
| 146 | } |
| 147 | |
Deepak Kodihalli | c6e8fb5 | 2019-05-02 08:35:31 -0500 | [diff] [blame] | 148 | TEST(GeneratePDR, testNoJson) |
| 149 | { |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 150 | auto pdrRepo = pldm_pdr_init(); |
| 151 | |
TOM JOSEPH | d4d97a5 | 2020-03-23 14:36:34 +0530 | [diff] [blame] | 152 | ASSERT_THROW(Handler("./pdr_jsons/not_there", "./event_jsons/not_there", |
Tom Joseph | 33e9c7e | 2020-06-11 22:09:52 +0530 | [diff] [blame] | 153 | pdrRepo, nullptr, nullptr), |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 154 | std::exception); |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 155 | |
| 156 | pldm_pdr_destroy(pdrRepo); |
Deepak Kodihalli | c6e8fb5 | 2019-05-02 08:35:31 -0500 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | TEST(GeneratePDR, testMalformedJson) |
| 160 | { |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 161 | auto inPDRRepo = pldm_pdr_init(); |
| 162 | auto outPDRRepo = pldm_pdr_init(); |
| 163 | Repo outRepo(outPDRRepo); |
TOM JOSEPH | d4d97a5 | 2020-03-23 14:36:34 +0530 | [diff] [blame] | 164 | Handler handler("./pdr_jsons/state_effecter/good", "./event_jsons/good", |
Tom Joseph | 33e9c7e | 2020-06-11 22:09:52 +0530 | [diff] [blame] | 165 | inPDRRepo, nullptr, nullptr); |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 166 | Repo inRepo(inPDRRepo); |
| 167 | getRepoByType(inRepo, outRepo, PLDM_STATE_EFFECTER_PDR); |
| 168 | |
| 169 | ASSERT_EQ(outRepo.getRecordCount(), 2); |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 170 | ASSERT_THROW(pdr_utils::readJson("./pdr_jsons/state_effecter/malformed"), |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 171 | std::exception); |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 172 | |
| 173 | pldm_pdr_destroy(inPDRRepo); |
| 174 | pldm_pdr_destroy(outPDRRepo); |
Deepak Kodihalli | c6e8fb5 | 2019-05-02 08:35:31 -0500 | [diff] [blame] | 175 | } |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 176 | |
| 177 | TEST(findStateEffecterId, goodJson) |
| 178 | { |
| 179 | auto inPDRRepo = pldm_pdr_init(); |
Tom Joseph | 33e9c7e | 2020-06-11 22:09:52 +0530 | [diff] [blame] | 180 | Handler handler("./pdr_jsons/state_effecter/good", "", inPDRRepo, nullptr, |
| 181 | nullptr); |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 182 | uint16_t entityType = 33; |
| 183 | uint16_t entityInstance = 0; |
| 184 | uint16_t containerId = 0; |
| 185 | uint16_t stateSetId = 196; |
| 186 | auto effecterId = findStateEffecterId(inPDRRepo, entityType, entityInstance, |
| 187 | containerId, stateSetId); |
| 188 | ASSERT_EQ(effecterId, 1); |
| 189 | stateSetId = 300; |
| 190 | effecterId = findStateEffecterId(inPDRRepo, entityType, entityInstance, |
| 191 | containerId, stateSetId); |
| 192 | ASSERT_EQ(effecterId, PLDM_INVALID_EFFECTER_ID); |
| 193 | pldm_pdr_destroy(inPDRRepo); |
| 194 | } |