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