Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 1 | #include "libpldmresponder/effecters.hpp" |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 2 | #include "libpldmresponder/pdr.hpp" |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 3 | #include "libpldmresponder/pdr_utils.hpp" |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 4 | #include "libpldmresponder/platform.hpp" |
| 5 | |
| 6 | #include <iostream> |
| 7 | |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 8 | #include <gmock/gmock-matchers.h> |
| 9 | #include <gmock/gmock.h> |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 10 | #include <gtest/gtest.h> |
| 11 | |
| 12 | using namespace pldm::responder; |
| 13 | |
| 14 | TEST(getPDR, testGoodPath) |
| 15 | { |
| 16 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES> |
| 17 | requestPayload{}; |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 18 | auto req = reinterpret_cast<pldm_msg*>(requestPayload.data()); |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 19 | size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr); |
| 20 | |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 21 | struct pldm_get_pdr_req* request = |
| 22 | reinterpret_cast<struct pldm_get_pdr_req*>(req->payload); |
| 23 | request->request_count = 100; |
| 24 | |
| 25 | pdr_utils::RepoInterface& pdrRepo = |
| 26 | pdr::getRepo("./pdr_jsons/state_effecter/good"); |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 27 | ASSERT_EQ(pdrRepo.empty(), false); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 28 | platform::Handler handler; |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 29 | auto response = handler.getPDR(req, requestPayloadLength); |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 30 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 31 | |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 32 | struct pldm_get_pdr_resp* resp = |
| 33 | reinterpret_cast<struct pldm_get_pdr_resp*>(responsePtr->payload); |
| 34 | ASSERT_EQ(PLDM_SUCCESS, resp->completion_code); |
| 35 | ASSERT_EQ(2, resp->next_record_handle); |
| 36 | ASSERT_EQ(true, resp->response_count != 0); |
| 37 | |
| 38 | pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(resp->record_data); |
| 39 | ASSERT_EQ(hdr->record_handle, 1); |
| 40 | ASSERT_EQ(hdr->version, 1); |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | TEST(getPDR, testShortRead) |
| 44 | { |
| 45 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES> |
| 46 | requestPayload{}; |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 47 | auto req = reinterpret_cast<pldm_msg*>(requestPayload.data()); |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 48 | size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr); |
| 49 | |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 50 | struct pldm_get_pdr_req* request = |
| 51 | reinterpret_cast<struct pldm_get_pdr_req*>(req->payload); |
| 52 | request->request_count = 1; |
| 53 | |
| 54 | pdr_utils::RepoInterface& pdrRepo = |
| 55 | pdr::getRepo("./pdr_jsons/state_effecter/good"); |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 56 | ASSERT_EQ(pdrRepo.empty(), false); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 57 | platform::Handler handler; |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 58 | auto response = handler.getPDR(req, requestPayloadLength); |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 59 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 60 | struct pldm_get_pdr_resp* resp = |
| 61 | reinterpret_cast<struct pldm_get_pdr_resp*>(responsePtr->payload); |
| 62 | ASSERT_EQ(PLDM_SUCCESS, resp->completion_code); |
| 63 | ASSERT_EQ(1, resp->response_count); |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | TEST(getPDR, testBadRecordHandle) |
| 67 | { |
| 68 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES> |
| 69 | requestPayload{}; |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 70 | auto req = reinterpret_cast<pldm_msg*>(requestPayload.data()); |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 71 | size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr); |
| 72 | |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 73 | struct pldm_get_pdr_req* request = |
| 74 | reinterpret_cast<struct pldm_get_pdr_req*>(req->payload); |
| 75 | request->record_handle = 100000; |
| 76 | request->request_count = 1; |
| 77 | |
| 78 | pdr_utils::RepoInterface& pdrRepo = |
| 79 | pdr::getRepo("./pdr_jsons/state_effecter/good"); |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 80 | ASSERT_EQ(pdrRepo.empty(), false); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 81 | platform::Handler handler; |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 82 | auto response = handler.getPDR(req, requestPayloadLength); |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 83 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 84 | |
| 85 | ASSERT_EQ(responsePtr->payload[0], PLDM_PLATFORM_INVALID_RECORD_HANDLE); |
| 86 | } |
| 87 | |
| 88 | TEST(getPDR, testNoNextRecord) |
| 89 | { |
| 90 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES> |
| 91 | requestPayload{}; |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 92 | auto req = reinterpret_cast<pldm_msg*>(requestPayload.data()); |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 93 | size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr); |
| 94 | |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 95 | struct pldm_get_pdr_req* request = |
| 96 | reinterpret_cast<struct pldm_get_pdr_req*>(req->payload); |
| 97 | request->record_handle = 1; |
| 98 | |
| 99 | pdr_utils::RepoInterface& pdrRepo = |
| 100 | pdr::getRepo("./pdr_jsons/state_effecter/good"); |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 101 | ASSERT_EQ(pdrRepo.empty(), false); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 102 | platform::Handler handler; |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 103 | auto response = handler.getPDR(req, requestPayloadLength); |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 104 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 105 | struct pldm_get_pdr_resp* resp = |
| 106 | reinterpret_cast<struct pldm_get_pdr_resp*>(responsePtr->payload); |
| 107 | ASSERT_EQ(PLDM_SUCCESS, resp->completion_code); |
| 108 | ASSERT_EQ(2, resp->next_record_handle); |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | TEST(getPDR, testFindPDR) |
| 112 | { |
| 113 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES> |
| 114 | requestPayload{}; |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 115 | auto req = reinterpret_cast<pldm_msg*>(requestPayload.data()); |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 116 | size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr); |
| 117 | |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 118 | struct pldm_get_pdr_req* request = |
| 119 | reinterpret_cast<struct pldm_get_pdr_req*>(req->payload); |
| 120 | request->request_count = 100; |
| 121 | |
| 122 | pdr_utils::RepoInterface& pdrRepo = |
| 123 | pdr::getRepo("./pdr_jsons/state_effecter/good"); |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 124 | ASSERT_EQ(pdrRepo.empty(), false); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 125 | platform::Handler handler; |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 126 | auto response = handler.getPDR(req, requestPayloadLength); |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 127 | |
| 128 | // Let's try to find a PDR of type stateEffecter (= 11) and entity type = |
| 129 | // 100 |
| 130 | bool found = false; |
| 131 | uint32_t handle = 0; // start asking for PDRs from recordHandle 0 |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 132 | while (!found) |
| 133 | { |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 134 | request->record_handle = handle; |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 135 | platform::Handler handler; |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 136 | auto response = handler.getPDR(req, requestPayloadLength); |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 137 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 138 | struct pldm_get_pdr_resp* resp = |
| 139 | reinterpret_cast<struct pldm_get_pdr_resp*>(responsePtr->payload); |
| 140 | ASSERT_EQ(PLDM_SUCCESS, resp->completion_code); |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 141 | |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 142 | handle = resp->next_record_handle; // point to the next pdr in case |
| 143 | // current is not what we want |
| 144 | |
| 145 | pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(resp->record_data); |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 146 | std::cerr << "PDR next record handle " << handle << "\n"; |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 147 | std::cerr << "PDR type " << hdr->type << "\n"; |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 148 | if (hdr->type == PLDM_STATE_EFFECTER_PDR) |
| 149 | { |
| 150 | pldm_state_effecter_pdr* pdr = |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 151 | reinterpret_cast<pldm_state_effecter_pdr*>(resp->record_data); |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 152 | std::cerr << "PDR entity type " << pdr->entity_type << "\n"; |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 153 | if (pdr->entity_type == 100) |
| 154 | { |
| 155 | found = true; |
| 156 | // Rest of the PDR can be accessed as need be |
| 157 | break; |
| 158 | } |
| 159 | } |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 160 | if (!resp->next_record_handle) // no more records |
Deepak Kodihalli | 557dfb0 | 2019-05-12 13:11:17 +0530 | [diff] [blame] | 161 | { |
| 162 | break; |
| 163 | } |
| 164 | } |
| 165 | ASSERT_EQ(found, true); |
| 166 | } |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 167 | |
| 168 | namespace pldm |
| 169 | { |
| 170 | |
| 171 | namespace responder |
| 172 | { |
| 173 | |
| 174 | class MockdBusHandler |
| 175 | { |
| 176 | public: |
| 177 | MOCK_CONST_METHOD4(setDbusProperty, |
| 178 | int(const std::string&, const std::string&, |
| 179 | const std::string&, |
| 180 | const std::variant<std::string>&)); |
| 181 | }; |
| 182 | } // namespace responder |
| 183 | } // namespace pldm |
| 184 | |
| 185 | using ::testing::_; |
| 186 | using ::testing::Return; |
| 187 | |
| 188 | TEST(setStateEffecterStatesHandler, testGoodRequest) |
| 189 | { |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 190 | pdr_utils::Repo pdrRepo = pdr::getRepoByType( |
| 191 | "./pdr_jsons/state_effecter/good", PLDM_STATE_EFFECTER_PDR); |
| 192 | pdr_utils::PdrEntry e; |
| 193 | auto record1 = pdr::getRecordByHandle(pdrRepo, 1, e); |
| 194 | ASSERT_NE(record1, nullptr); |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 195 | pldm_state_effecter_pdr* pdr = |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 196 | reinterpret_cast<pldm_state_effecter_pdr*>(e.data); |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 197 | EXPECT_EQ(pdr->hdr.type, PLDM_STATE_EFFECTER_PDR); |
| 198 | |
| 199 | std::vector<set_effecter_state_field> stateField; |
| 200 | stateField.push_back({PLDM_REQUEST_SET, 1}); |
| 201 | stateField.push_back({PLDM_REQUEST_SET, 1}); |
| 202 | |
| 203 | auto bootProgressInf = "xyz.openbmc_project.State.OperatingSystem.Status"; |
| 204 | auto bootProgressProp = "OperatingSystemState"; |
| 205 | std::string objPath = "/foo/bar"; |
| 206 | std::variant<std::string> value{"xyz.openbmc_project.State.OperatingSystem." |
| 207 | "Status.OSStatus.Standby"}; |
| 208 | |
| 209 | MockdBusHandler handlerObj; |
| 210 | EXPECT_CALL(handlerObj, setDbusProperty(objPath, bootProgressProp, |
| 211 | bootProgressInf, value)) |
| 212 | .Times(2); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 213 | platform::Handler handler; |
| 214 | auto rc = handler.setStateEffecterStatesHandler<MockdBusHandler>( |
| 215 | handlerObj, 0x1, stateField); |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 216 | ASSERT_EQ(rc, 0); |
| 217 | } |
| 218 | |
| 219 | TEST(setStateEffecterStatesHandler, testBadRequest) |
| 220 | { |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 221 | pdr_utils::Repo pdrRepo = pdr::getRepoByType( |
| 222 | "./pdr_jsons/state_effecter/good", PLDM_STATE_EFFECTER_PDR); |
| 223 | pdr_utils::PdrEntry e; |
| 224 | auto record1 = pdr::getRecordByHandle(pdrRepo, 1, e); |
| 225 | ASSERT_NE(record1, nullptr); |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 226 | pldm_state_effecter_pdr* pdr = |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 227 | reinterpret_cast<pldm_state_effecter_pdr*>(e.data); |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 228 | EXPECT_EQ(pdr->hdr.type, PLDM_STATE_EFFECTER_PDR); |
| 229 | |
| 230 | std::vector<set_effecter_state_field> stateField; |
| 231 | stateField.push_back({PLDM_REQUEST_SET, 3}); |
| 232 | stateField.push_back({PLDM_REQUEST_SET, 4}); |
| 233 | |
| 234 | MockdBusHandler handlerObj; |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 235 | platform::Handler handler; |
| 236 | auto rc = handler.setStateEffecterStatesHandler<MockdBusHandler>( |
| 237 | handlerObj, 0x1, stateField); |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 238 | ASSERT_EQ(rc, PLDM_PLATFORM_SET_EFFECTER_UNSUPPORTED_SENSORSTATE); |
| 239 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 240 | rc = handler.setStateEffecterStatesHandler<MockdBusHandler>(handlerObj, 0x9, |
| 241 | stateField); |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 242 | ASSERT_EQ(rc, PLDM_PLATFORM_INVALID_EFFECTER_ID); |
| 243 | |
| 244 | stateField.push_back({PLDM_REQUEST_SET, 4}); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 245 | rc = handler.setStateEffecterStatesHandler<MockdBusHandler>(handlerObj, 0x1, |
| 246 | stateField); |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 247 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 248 | |
| 249 | std::vector<set_effecter_state_field> newStateField; |
| 250 | newStateField.push_back({PLDM_REQUEST_SET, 1}); |
| 251 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 252 | rc = handler.setStateEffecterStatesHandler<MockdBusHandler>(handlerObj, 0x2, |
| 253 | newStateField); |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 254 | ASSERT_EQ(rc, PLDM_PLATFORM_INVALID_STATE_VALUE); |
| 255 | } |