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