George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 1 | #include "libpldm/base.h" |
| 2 | |
Sagar Srinivas | a6a8ccd | 2021-04-01 07:58:33 -0500 | [diff] [blame] | 3 | #include "common/utils.hpp" |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 4 | #include "libpldmresponder/base.hpp" |
| 5 | |
| 6 | #include <string.h> |
| 7 | |
Sagar Srinivas | a6a8ccd | 2021-04-01 07:58:33 -0500 | [diff] [blame] | 8 | #include <sdeventplus/event.hpp> |
| 9 | |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 10 | #include <array> |
| 11 | |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 12 | #include <gtest/gtest.h> |
| 13 | |
| 14 | using namespace pldm::responder; |
| 15 | |
Sagar Srinivas | a6a8ccd | 2021-04-01 07:58:33 -0500 | [diff] [blame] | 16 | class TestBaseCommands : public testing::Test |
| 17 | { |
| 18 | protected: |
| 19 | TestBaseCommands() : |
| 20 | requester(pldm::utils::DBusHandler::getBus(), "/abc/def"), |
| 21 | event(sdeventplus::Event::get_default()) |
| 22 | {} |
| 23 | |
| 24 | uint8_t mctpEid = 0; |
| 25 | Requester requester; |
| 26 | sdeventplus::Event event; |
| 27 | }; |
| 28 | |
| 29 | TEST_F(TestBaseCommands, testPLDMTypesGoodRequest) |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 30 | { |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 31 | std::array<uint8_t, sizeof(pldm_msg_hdr)> requestPayload{}; |
| 32 | auto request = reinterpret_cast<pldm_msg*>(requestPayload.data()); |
| 33 | // payload length will be 0 in this case |
| 34 | size_t requestPayloadLength = 0; |
Sagar Srinivas | 79669c9 | 2021-04-28 15:43:30 -0500 | [diff] [blame] | 35 | base::Handler handler(mctpEid, requester, event, nullptr, nullptr); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 36 | auto response = handler.getPLDMTypes(request, requestPayloadLength); |
Sridevi Ramesh | f58d9a5 | 2020-02-14 00:30:58 -0600 | [diff] [blame] | 37 | // Need to support OEM type. |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 38 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 39 | uint8_t* payload_ptr = responsePtr->payload; |
| 40 | ASSERT_EQ(payload_ptr[0], 0); |
Sridevi Ramesh | f58d9a5 | 2020-02-14 00:30:58 -0600 | [diff] [blame] | 41 | ASSERT_EQ(payload_ptr[1], 29); // 0b11101 see DSP0240 table11 |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 42 | ASSERT_EQ(payload_ptr[2], 0); |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 43 | } |
| 44 | |
Sagar Srinivas | a6a8ccd | 2021-04-01 07:58:33 -0500 | [diff] [blame] | 45 | TEST_F(TestBaseCommands, testGetPLDMCommandsGoodRequest) |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 46 | { |
Sridevi Ramesh | f58d9a5 | 2020-02-14 00:30:58 -0600 | [diff] [blame] | 47 | // Need to support OEM type commands. |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 48 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_REQ_BYTES> |
| 49 | requestPayload{}; |
| 50 | auto request = reinterpret_cast<pldm_msg*>(requestPayload.data()); |
| 51 | size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr); |
Sagar Srinivas | 79669c9 | 2021-04-28 15:43:30 -0500 | [diff] [blame] | 52 | base::Handler handler(mctpEid, requester, event, nullptr, nullptr); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 53 | auto response = handler.getPLDMCommands(request, requestPayloadLength); |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 54 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 55 | uint8_t* payload_ptr = responsePtr->payload; |
| 56 | ASSERT_EQ(payload_ptr[0], 0); |
John Wang | 5c4f80d | 2019-07-29 11:12:18 +0800 | [diff] [blame] | 57 | ASSERT_EQ(payload_ptr[1], 60); // 60 = 0b111100 |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 58 | ASSERT_EQ(payload_ptr[2], 0); |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 59 | } |
| 60 | |
Sagar Srinivas | a6a8ccd | 2021-04-01 07:58:33 -0500 | [diff] [blame] | 61 | TEST_F(TestBaseCommands, testGetPLDMCommandsBadRequest) |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 62 | { |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 63 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_REQ_BYTES> |
| 64 | requestPayload{}; |
| 65 | auto request = reinterpret_cast<pldm_msg*>(requestPayload.data()); |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 66 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 67 | request->payload[0] = 0xFF; |
| 68 | size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr); |
Sagar Srinivas | 79669c9 | 2021-04-28 15:43:30 -0500 | [diff] [blame] | 69 | base::Handler handler(mctpEid, requester, event, nullptr, nullptr); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 70 | auto response = handler.getPLDMCommands(request, requestPayloadLength); |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 71 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 72 | uint8_t* payload_ptr = responsePtr->payload; |
| 73 | ASSERT_EQ(payload_ptr[0], PLDM_ERROR_INVALID_PLDM_TYPE); |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 74 | } |
Sagar Srinivas | a6a8ccd | 2021-04-01 07:58:33 -0500 | [diff] [blame] | 75 | |
| 76 | TEST_F(TestBaseCommands, testGetPLDMVersionGoodRequest) |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 77 | { |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 78 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_REQ_BYTES> |
| 79 | requestPayload{}; |
| 80 | auto request = reinterpret_cast<pldm_msg*>(requestPayload.data()); |
| 81 | size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr); |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 82 | |
| 83 | uint8_t pldmType = PLDM_BASE; |
| 84 | uint32_t transferHandle = 0x0; |
| 85 | uint8_t flag = PLDM_GET_FIRSTPART; |
| 86 | uint8_t retFlag = PLDM_START_AND_END; |
Manojkiran Eda | cb45467 | 2022-06-29 11:05:42 +0530 | [diff] [blame] | 87 | ver32_t version = {0x00, 0xF0, 0xF0, 0xF1}; |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 88 | |
| 89 | auto rc = |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 90 | encode_get_version_req(0, transferHandle, flag, pldmType, request); |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 91 | |
| 92 | ASSERT_EQ(0, rc); |
| 93 | |
Sagar Srinivas | 79669c9 | 2021-04-28 15:43:30 -0500 | [diff] [blame] | 94 | base::Handler handler(mctpEid, requester, event, nullptr, nullptr); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 95 | auto response = handler.getPLDMVersion(request, requestPayloadLength); |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 96 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 97 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 98 | ASSERT_EQ(responsePtr->payload[0], 0); |
| 99 | ASSERT_EQ(0, memcmp(responsePtr->payload + sizeof(responsePtr->payload[0]), |
| 100 | &transferHandle, sizeof(transferHandle))); |
| 101 | ASSERT_EQ(0, memcmp(responsePtr->payload + sizeof(responsePtr->payload[0]) + |
| 102 | sizeof(transferHandle), |
| 103 | &retFlag, sizeof(flag))); |
| 104 | ASSERT_EQ(0, memcmp(responsePtr->payload + sizeof(responsePtr->payload[0]) + |
| 105 | sizeof(transferHandle) + sizeof(flag), |
| 106 | &version, sizeof(version))); |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 107 | } |
Sagar Srinivas | a6a8ccd | 2021-04-01 07:58:33 -0500 | [diff] [blame] | 108 | |
| 109 | TEST_F(TestBaseCommands, testGetPLDMVersionBadRequest) |
Sampa Misra | 94d704b | 2019-02-22 03:36:02 -0600 | [diff] [blame] | 110 | { |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 111 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_REQ_BYTES> |
| 112 | requestPayload{}; |
| 113 | auto request = reinterpret_cast<pldm_msg*>(requestPayload.data()); |
| 114 | size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr); |
Sampa Misra | 94d704b | 2019-02-22 03:36:02 -0600 | [diff] [blame] | 115 | |
| 116 | uint8_t pldmType = 7; |
| 117 | uint32_t transferHandle = 0x0; |
| 118 | uint8_t flag = PLDM_GET_FIRSTPART; |
| 119 | |
| 120 | auto rc = |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 121 | encode_get_version_req(0, transferHandle, flag, pldmType, request); |
Sampa Misra | 94d704b | 2019-02-22 03:36:02 -0600 | [diff] [blame] | 122 | |
| 123 | ASSERT_EQ(0, rc); |
| 124 | |
Sagar Srinivas | 79669c9 | 2021-04-28 15:43:30 -0500 | [diff] [blame] | 125 | base::Handler handler(mctpEid, requester, event, nullptr, nullptr); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 126 | auto response = handler.getPLDMVersion(request, requestPayloadLength - 1); |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 127 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
Sampa Misra | 94d704b | 2019-02-22 03:36:02 -0600 | [diff] [blame] | 128 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 129 | ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR_INVALID_LENGTH); |
Sampa Misra | 94d704b | 2019-02-22 03:36:02 -0600 | [diff] [blame] | 130 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 131 | request = reinterpret_cast<pldm_msg*>(requestPayload.data()); |
| 132 | requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr); |
Sampa Misra | 94d704b | 2019-02-22 03:36:02 -0600 | [diff] [blame] | 133 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 134 | rc = encode_get_version_req(0, transferHandle, flag, pldmType, request); |
Sampa Misra | 94d704b | 2019-02-22 03:36:02 -0600 | [diff] [blame] | 135 | |
| 136 | ASSERT_EQ(0, rc); |
| 137 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 138 | response = handler.getPLDMVersion(request, requestPayloadLength); |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 139 | responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
Sampa Misra | 94d704b | 2019-02-22 03:36:02 -0600 | [diff] [blame] | 140 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 141 | ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR_INVALID_PLDM_TYPE); |
Sampa Misra | 94d704b | 2019-02-22 03:36:02 -0600 | [diff] [blame] | 142 | } |
John Wang | 5c4f80d | 2019-07-29 11:12:18 +0800 | [diff] [blame] | 143 | |
Sagar Srinivas | a6a8ccd | 2021-04-01 07:58:33 -0500 | [diff] [blame] | 144 | TEST_F(TestBaseCommands, testGetTIDGoodRequest) |
John Wang | 5c4f80d | 2019-07-29 11:12:18 +0800 | [diff] [blame] | 145 | { |
| 146 | std::array<uint8_t, sizeof(pldm_msg_hdr)> requestPayload{}; |
| 147 | auto request = reinterpret_cast<pldm_msg*>(requestPayload.data()); |
| 148 | size_t requestPayloadLength = 0; |
| 149 | |
Sagar Srinivas | 79669c9 | 2021-04-28 15:43:30 -0500 | [diff] [blame] | 150 | base::Handler handler(mctpEid, requester, event, nullptr, nullptr); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 151 | auto response = handler.getTID(request, requestPayloadLength); |
John Wang | 5c4f80d | 2019-07-29 11:12:18 +0800 | [diff] [blame] | 152 | |
| 153 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 154 | uint8_t* payload = responsePtr->payload; |
| 155 | |
| 156 | ASSERT_EQ(payload[0], 0); |
| 157 | ASSERT_EQ(payload[1], 1); |
| 158 | } |