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