George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 1 | #include "libpldm/base.h" |
| 2 | |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 3 | #include "libpldmresponder/base.hpp" |
| 4 | |
| 5 | #include <string.h> |
| 6 | |
| 7 | #include <array> |
| 8 | |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 9 | #include <gtest/gtest.h> |
| 10 | |
| 11 | using namespace pldm::responder; |
| 12 | |
| 13 | TEST(GetPLDMTypes, testGoodRequest) |
| 14 | { |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 15 | std::array<uint8_t, sizeof(pldm_msg_hdr)> requestPayload{}; |
| 16 | auto request = reinterpret_cast<pldm_msg*>(requestPayload.data()); |
| 17 | // payload length will be 0 in this case |
| 18 | size_t requestPayloadLength = 0; |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 19 | base::Handler handler; |
| 20 | auto response = handler.getPLDMTypes(request, requestPayloadLength); |
Sridevi Ramesh | f58d9a5 | 2020-02-14 00:30:58 -0600 | [diff] [blame] | 21 | // Need to support OEM type. |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 22 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 23 | uint8_t* payload_ptr = responsePtr->payload; |
| 24 | ASSERT_EQ(payload_ptr[0], 0); |
Sridevi Ramesh | f58d9a5 | 2020-02-14 00:30:58 -0600 | [diff] [blame] | 25 | ASSERT_EQ(payload_ptr[1], 29); // 0b11101 see DSP0240 table11 |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 26 | ASSERT_EQ(payload_ptr[2], 0); |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | TEST(GetPLDMCommands, testGoodRequest) |
| 30 | { |
Sridevi Ramesh | f58d9a5 | 2020-02-14 00:30:58 -0600 | [diff] [blame] | 31 | // Need to support OEM type commands. |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 32 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_REQ_BYTES> |
| 33 | requestPayload{}; |
| 34 | auto request = reinterpret_cast<pldm_msg*>(requestPayload.data()); |
| 35 | size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 36 | base::Handler handler; |
| 37 | auto response = handler.getPLDMCommands(request, requestPayloadLength); |
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); |
John Wang | 5c4f80d | 2019-07-29 11:12:18 +0800 | [diff] [blame] | 41 | ASSERT_EQ(payload_ptr[1], 60); // 60 = 0b111100 |
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 | |
| 45 | TEST(GetPLDMCommands, testBadRequest) |
| 46 | { |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 47 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_REQ_BYTES> |
| 48 | requestPayload{}; |
| 49 | auto request = reinterpret_cast<pldm_msg*>(requestPayload.data()); |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 50 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 51 | request->payload[0] = 0xFF; |
| 52 | size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 53 | base::Handler handler; |
| 54 | auto response = handler.getPLDMCommands(request, requestPayloadLength); |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 55 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 56 | uint8_t* payload_ptr = responsePtr->payload; |
| 57 | ASSERT_EQ(payload_ptr[0], PLDM_ERROR_INVALID_PLDM_TYPE); |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 58 | } |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 59 | TEST(GetPLDMVersion, testGoodRequest) |
| 60 | { |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 61 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_REQ_BYTES> |
| 62 | requestPayload{}; |
| 63 | auto request = reinterpret_cast<pldm_msg*>(requestPayload.data()); |
| 64 | size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr); |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 65 | |
| 66 | uint8_t pldmType = PLDM_BASE; |
| 67 | uint32_t transferHandle = 0x0; |
| 68 | uint8_t flag = PLDM_GET_FIRSTPART; |
| 69 | uint8_t retFlag = PLDM_START_AND_END; |
Deepak Kodihalli | 97e0bd5 | 2019-02-21 03:54:22 -0600 | [diff] [blame] | 70 | ver32_t version = {0xF1, 0xF0, 0xF0, 0x00}; |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 71 | |
| 72 | auto rc = |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 73 | encode_get_version_req(0, transferHandle, flag, pldmType, request); |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 74 | |
| 75 | ASSERT_EQ(0, rc); |
| 76 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 77 | base::Handler handler; |
| 78 | auto response = handler.getPLDMVersion(request, requestPayloadLength); |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 79 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 80 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 81 | ASSERT_EQ(responsePtr->payload[0], 0); |
| 82 | ASSERT_EQ(0, memcmp(responsePtr->payload + sizeof(responsePtr->payload[0]), |
| 83 | &transferHandle, sizeof(transferHandle))); |
| 84 | ASSERT_EQ(0, memcmp(responsePtr->payload + sizeof(responsePtr->payload[0]) + |
| 85 | sizeof(transferHandle), |
| 86 | &retFlag, sizeof(flag))); |
| 87 | ASSERT_EQ(0, memcmp(responsePtr->payload + sizeof(responsePtr->payload[0]) + |
| 88 | sizeof(transferHandle) + sizeof(flag), |
| 89 | &version, sizeof(version))); |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 90 | } |
Sampa Misra | 94d704b | 2019-02-22 03:36:02 -0600 | [diff] [blame] | 91 | TEST(GetPLDMVersion, testBadRequest) |
| 92 | { |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 93 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_REQ_BYTES> |
| 94 | requestPayload{}; |
| 95 | auto request = reinterpret_cast<pldm_msg*>(requestPayload.data()); |
| 96 | size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr); |
Sampa Misra | 94d704b | 2019-02-22 03:36:02 -0600 | [diff] [blame] | 97 | |
| 98 | uint8_t pldmType = 7; |
| 99 | uint32_t transferHandle = 0x0; |
| 100 | uint8_t flag = PLDM_GET_FIRSTPART; |
| 101 | |
| 102 | auto rc = |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 103 | encode_get_version_req(0, transferHandle, flag, pldmType, request); |
Sampa Misra | 94d704b | 2019-02-22 03:36:02 -0600 | [diff] [blame] | 104 | |
| 105 | ASSERT_EQ(0, rc); |
| 106 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 107 | base::Handler handler; |
| 108 | auto response = handler.getPLDMVersion(request, requestPayloadLength - 1); |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 109 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
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 | ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR_INVALID_LENGTH); |
Sampa Misra | 94d704b | 2019-02-22 03:36:02 -0600 | [diff] [blame] | 112 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 113 | request = reinterpret_cast<pldm_msg*>(requestPayload.data()); |
| 114 | requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr); |
Sampa Misra | 94d704b | 2019-02-22 03:36:02 -0600 | [diff] [blame] | 115 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 116 | rc = encode_get_version_req(0, transferHandle, flag, pldmType, request); |
Sampa Misra | 94d704b | 2019-02-22 03:36:02 -0600 | [diff] [blame] | 117 | |
| 118 | ASSERT_EQ(0, rc); |
| 119 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 120 | response = handler.getPLDMVersion(request, requestPayloadLength); |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 121 | responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
Sampa Misra | 94d704b | 2019-02-22 03:36:02 -0600 | [diff] [blame] | 122 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 123 | ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR_INVALID_PLDM_TYPE); |
Sampa Misra | 94d704b | 2019-02-22 03:36:02 -0600 | [diff] [blame] | 124 | } |
John Wang | 5c4f80d | 2019-07-29 11:12:18 +0800 | [diff] [blame] | 125 | |
| 126 | TEST(GetTID, testGoodRequest) |
| 127 | { |
| 128 | std::array<uint8_t, sizeof(pldm_msg_hdr)> requestPayload{}; |
| 129 | auto request = reinterpret_cast<pldm_msg*>(requestPayload.data()); |
| 130 | size_t requestPayloadLength = 0; |
| 131 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 132 | base::Handler handler; |
| 133 | auto response = handler.getTID(request, requestPayloadLength); |
John Wang | 5c4f80d | 2019-07-29 11:12:18 +0800 | [diff] [blame] | 134 | |
| 135 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 136 | uint8_t* payload = responsePtr->payload; |
| 137 | |
| 138 | ASSERT_EQ(payload[0], 0); |
| 139 | ASSERT_EQ(payload[1], 1); |
| 140 | } |