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