George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 1 | #include "oem/ibm/libpldm/host.h" |
| 2 | |
George Liu | 9008d28 | 2020-03-12 11:20:35 +0800 | [diff] [blame] | 3 | #include <string.h> |
| 4 | |
| 5 | #include <array> |
| 6 | |
George Liu | 9008d28 | 2020-03-12 11:20:35 +0800 | [diff] [blame] | 7 | #include <gtest/gtest.h> |
| 8 | |
| 9 | constexpr auto hdrSize = sizeof(pldm_msg_hdr); |
| 10 | |
| 11 | TEST(GetAlertStatus, testGoodEncodeRequest) |
| 12 | { |
| 13 | std::array<uint8_t, hdrSize + PLDM_GET_ALERT_STATUS_REQ_BYTES> requestMsg{}; |
| 14 | |
| 15 | uint8_t versionId = 0x0; |
| 16 | |
| 17 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 18 | auto rc = encode_get_alert_status_req(0, versionId, request, |
| 19 | PLDM_GET_ALERT_STATUS_REQ_BYTES); |
| 20 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 21 | EXPECT_EQ(versionId, request->payload[0]); |
| 22 | } |
| 23 | |
| 24 | TEST(GetAlertStatus, testBadEncodeRequest) |
| 25 | { |
| 26 | std::array<uint8_t, hdrSize + PLDM_GET_ALERT_STATUS_REQ_BYTES> requestMsg{}; |
| 27 | |
| 28 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 29 | auto rc = encode_get_alert_status_req(0, 0x0, request, |
| 30 | PLDM_GET_ALERT_STATUS_REQ_BYTES + 1); |
| 31 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 32 | } |
| 33 | |
| 34 | TEST(GetAlertStatus, testGoodDecodeResponse) |
| 35 | { |
| 36 | uint8_t completionCode = PLDM_SUCCESS; |
| 37 | uint32_t rack_entry = 0xFF000030; |
| 38 | uint32_t pri_cec_node = 0x00008030; |
| 39 | std::array<uint8_t, hdrSize + PLDM_GET_ALERT_STATUS_RESP_BYTES> |
| 40 | responseMsg{}; |
| 41 | |
| 42 | uint8_t retCompletionCode = 0; |
| 43 | uint32_t retRack_entry = 0; |
| 44 | uint32_t retPri_cec_node = 0; |
| 45 | |
| 46 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 47 | struct pldm_get_alert_status_resp* resp = |
| 48 | reinterpret_cast<struct pldm_get_alert_status_resp*>(response->payload); |
| 49 | resp->completion_code = completionCode; |
| 50 | resp->rack_entry = htole32(rack_entry); |
| 51 | resp->pri_cec_node = htole32(pri_cec_node); |
| 52 | |
| 53 | auto rc = decode_get_alert_status_resp( |
| 54 | response, responseMsg.size() - hdrSize, &retCompletionCode, |
| 55 | &retRack_entry, &retPri_cec_node); |
| 56 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 57 | EXPECT_EQ(retCompletionCode, completionCode); |
| 58 | EXPECT_EQ(retRack_entry, rack_entry); |
| 59 | EXPECT_EQ(retPri_cec_node, pri_cec_node); |
| 60 | } |
| 61 | |
| 62 | TEST(GetAlertStatus, testBadDecodeResponse) |
| 63 | { |
| 64 | auto rc = decode_get_alert_status_resp(NULL, 0, NULL, NULL, NULL); |
| 65 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 66 | |
| 67 | uint8_t completionCode = PLDM_SUCCESS; |
| 68 | uint32_t rack_entry = 0xFF000030; |
| 69 | uint32_t pri_cec_node = 0x00008030; |
| 70 | std::array<uint8_t, hdrSize + PLDM_GET_ALERT_STATUS_RESP_BYTES> |
| 71 | responseMsg{}; |
| 72 | |
| 73 | uint8_t retCompletionCode = 0; |
| 74 | uint32_t retRack_entry = 0; |
| 75 | uint32_t retPri_cec_node = 0; |
| 76 | |
| 77 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 78 | struct pldm_get_alert_status_resp* resp = |
| 79 | reinterpret_cast<struct pldm_get_alert_status_resp*>(response->payload); |
| 80 | resp->completion_code = completionCode; |
| 81 | resp->rack_entry = htole32(rack_entry); |
| 82 | resp->pri_cec_node = htole32(pri_cec_node); |
| 83 | |
| 84 | rc = decode_get_alert_status_resp( |
| 85 | response, responseMsg.size() - hdrSize + 1, &retCompletionCode, |
| 86 | &retRack_entry, &retPri_cec_node); |
| 87 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 88 | } |
George Liu | 81caca5 | 2020-03-12 12:14:11 +0800 | [diff] [blame] | 89 | |
| 90 | TEST(GetAlertStatus, testGoodEncodeResponse) |
| 91 | { |
| 92 | uint8_t completionCode = 0; |
| 93 | uint32_t rack_entry = 0xFF000030; |
| 94 | uint32_t pri_cec_node = 0x00008030; |
| 95 | |
| 96 | std::vector<uint8_t> responseMsg(hdrSize + |
| 97 | PLDM_GET_ALERT_STATUS_RESP_BYTES); |
| 98 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 99 | |
| 100 | auto rc = |
| 101 | encode_get_alert_status_resp(0, PLDM_SUCCESS, rack_entry, pri_cec_node, |
| 102 | response, responseMsg.size() - hdrSize); |
| 103 | |
| 104 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 105 | struct pldm_get_alert_status_resp* resp = |
| 106 | reinterpret_cast<struct pldm_get_alert_status_resp*>(response->payload); |
| 107 | |
| 108 | EXPECT_EQ(completionCode, resp->completion_code); |
| 109 | EXPECT_EQ(rack_entry, le32toh(resp->rack_entry)); |
| 110 | EXPECT_EQ(pri_cec_node, le32toh(resp->pri_cec_node)); |
| 111 | } |
| 112 | |
| 113 | TEST(GetAlertStatus, testBadEncodeResponse) |
| 114 | { |
| 115 | uint32_t rack_entry = 0xFF000030; |
| 116 | uint32_t pri_cec_node = 0x00008030; |
| 117 | |
| 118 | std::vector<uint8_t> responseMsg(hdrSize + |
| 119 | PLDM_GET_ALERT_STATUS_RESP_BYTES); |
| 120 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 121 | |
| 122 | auto rc = encode_get_alert_status_resp(0, PLDM_SUCCESS, rack_entry, |
| 123 | pri_cec_node, response, |
| 124 | responseMsg.size() - hdrSize + 1); |
| 125 | |
| 126 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 127 | } |
| 128 | |
| 129 | TEST(GetAlertStatus, testGoodDecodeRequest) |
| 130 | { |
| 131 | std::array<uint8_t, hdrSize + PLDM_GET_ALERT_STATUS_REQ_BYTES> requestMsg{}; |
| 132 | |
| 133 | uint8_t versionId = 0x0; |
| 134 | uint8_t retVersionId; |
| 135 | |
| 136 | auto req = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 137 | |
| 138 | req->payload[0] = versionId; |
| 139 | |
| 140 | auto rc = decode_get_alert_status_req(req, requestMsg.size() - hdrSize, |
| 141 | &retVersionId); |
| 142 | |
| 143 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 144 | EXPECT_EQ(retVersionId, versionId); |
| 145 | } |
| 146 | |
| 147 | TEST(GetAlertStatus, testBadDecodeRequest) |
| 148 | { |
| 149 | std::array<uint8_t, hdrSize + PLDM_GET_ALERT_STATUS_REQ_BYTES> requestMsg{}; |
| 150 | |
| 151 | uint8_t versionId = 0x0; |
| 152 | uint8_t retVersionId; |
| 153 | |
| 154 | auto req = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 155 | |
| 156 | req->payload[0] = versionId; |
| 157 | |
| 158 | auto rc = decode_get_alert_status_req(req, requestMsg.size() - hdrSize + 1, |
| 159 | &retVersionId); |
| 160 | |
| 161 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 162 | } |