Sampa Misra | 0db1dfa | 2019-03-19 00:15:31 -0500 | [diff] [blame] | 1 | #include <string.h> |
| 2 | |
| 3 | #include <array> |
| 4 | |
| 5 | #include "libpldm/base.h" |
| 6 | #include "libpldm/platform.h" |
| 7 | |
| 8 | #include <gtest/gtest.h> |
| 9 | |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 10 | constexpr auto hdrSize = sizeof(pldm_msg_hdr); |
| 11 | |
Sampa Misra | 0db1dfa | 2019-03-19 00:15:31 -0500 | [diff] [blame] | 12 | TEST(SetStateEffecterStates, testEncodeResponse) |
| 13 | { |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 14 | std::array<uint8_t, |
| 15 | sizeof(pldm_msg_hdr) + PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES> |
| 16 | responseMsg{}; |
| 17 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
Sampa Misra | 0db1dfa | 2019-03-19 00:15:31 -0500 | [diff] [blame] | 18 | uint8_t completionCode = 0; |
| 19 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 20 | auto rc = encode_set_state_effecter_states_resp(0, PLDM_SUCCESS, response); |
Sampa Misra | 0db1dfa | 2019-03-19 00:15:31 -0500 | [diff] [blame] | 21 | |
George Liu | 684a716 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 22 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 23 | EXPECT_EQ(completionCode, response->payload[0]); |
Sampa Misra | 0db1dfa | 2019-03-19 00:15:31 -0500 | [diff] [blame] | 24 | } |
| 25 | |
vkaverap | 98a2c19 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 26 | TEST(SetStateEffecterStates, testEncodeRequest) |
| 27 | { |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 28 | std::array<uint8_t, |
| 29 | sizeof(pldm_msg_hdr) + PLDM_SET_STATE_EFFECTER_STATES_REQ_BYTES> |
| 30 | requestMsg{}; |
| 31 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 32 | |
vkaverap | 98a2c19 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 33 | uint16_t effecterId = 0x0A; |
| 34 | uint8_t compEffecterCnt = 0x2; |
| 35 | std::array<set_effecter_state_field, 8> stateField{}; |
| 36 | stateField[0] = {PLDM_REQUEST_SET, 2}; |
| 37 | stateField[1] = {PLDM_REQUEST_SET, 3}; |
| 38 | |
vkaverap | 98a2c19 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 39 | auto rc = encode_set_state_effecter_states_req( |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 40 | 0, effecterId, compEffecterCnt, stateField.data(), request); |
vkaverap | 98a2c19 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 41 | |
George Liu | 684a716 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 42 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 43 | EXPECT_EQ(effecterId, request->payload[0]); |
| 44 | EXPECT_EQ(compEffecterCnt, request->payload[sizeof(effecterId)]); |
| 45 | EXPECT_EQ(stateField[0].set_request, |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 46 | request->payload[sizeof(effecterId) + sizeof(compEffecterCnt)]); |
George Liu | 684a716 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 47 | EXPECT_EQ(stateField[0].effecter_state, |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 48 | request->payload[sizeof(effecterId) + sizeof(compEffecterCnt) + |
| 49 | sizeof(stateField[0].set_request)]); |
George Liu | 684a716 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 50 | EXPECT_EQ(stateField[1].set_request, |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 51 | request->payload[sizeof(effecterId) + sizeof(compEffecterCnt) + |
| 52 | sizeof(stateField[0])]); |
George Liu | 684a716 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 53 | EXPECT_EQ(stateField[1].effecter_state, |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 54 | request->payload[sizeof(effecterId) + sizeof(compEffecterCnt) + |
| 55 | sizeof(stateField[0]) + |
| 56 | sizeof(stateField[1].set_request)]); |
vkaverap | 98a2c19 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | TEST(SetStateEffecterStates, testGoodDecodeResponse) |
| 60 | { |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 61 | std::array<uint8_t, hdrSize + PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES> |
vkaverap | 98a2c19 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 62 | responseMsg{}; |
| 63 | |
vkaverap | 98a2c19 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 64 | uint8_t retcompletion_code = 0; |
| 65 | |
George Liu | 684a716 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 66 | responseMsg[hdrSize] = PLDM_SUCCESS; |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 67 | |
| 68 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
vkaverap | 98a2c19 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 69 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 70 | auto rc = decode_set_state_effecter_states_resp( |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 71 | response, responseMsg.size() - hdrSize, &retcompletion_code); |
vkaverap | 98a2c19 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 72 | |
George Liu | 684a716 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 73 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 74 | EXPECT_EQ(PLDM_SUCCESS, retcompletion_code); |
vkaverap | 98a2c19 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 75 | } |
| 76 | |
Sampa Misra | 0db1dfa | 2019-03-19 00:15:31 -0500 | [diff] [blame] | 77 | TEST(SetStateEffecterStates, testGoodDecodeRequest) |
| 78 | { |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 79 | std::array<uint8_t, hdrSize + PLDM_SET_STATE_EFFECTER_STATES_REQ_BYTES> |
| 80 | requestMsg{}; |
Sampa Misra | 0db1dfa | 2019-03-19 00:15:31 -0500 | [diff] [blame] | 81 | |
Lei YU | 065cf76 | 2020-02-28 15:31:28 +0800 | [diff] [blame] | 82 | uint16_t effecterId = 0x32; |
| 83 | uint16_t effecterIdLE = htole16(effecterId); |
Sampa Misra | 0db1dfa | 2019-03-19 00:15:31 -0500 | [diff] [blame] | 84 | uint8_t compEffecterCnt = 0x2; |
| 85 | |
| 86 | std::array<set_effecter_state_field, 8> stateField{}; |
| 87 | stateField[0] = {PLDM_REQUEST_SET, 3}; |
| 88 | stateField[1] = {PLDM_REQUEST_SET, 4}; |
| 89 | |
| 90 | uint16_t retEffecterId = 0; |
| 91 | uint8_t retCompEffecterCnt = 0; |
| 92 | |
| 93 | std::array<set_effecter_state_field, 8> retStateField{}; |
| 94 | |
Lei YU | 065cf76 | 2020-02-28 15:31:28 +0800 | [diff] [blame] | 95 | memcpy(requestMsg.data() + hdrSize, &effecterIdLE, sizeof(effecterIdLE)); |
| 96 | memcpy(requestMsg.data() + sizeof(effecterIdLE) + hdrSize, &compEffecterCnt, |
Sampa Misra | 0db1dfa | 2019-03-19 00:15:31 -0500 | [diff] [blame] | 97 | sizeof(compEffecterCnt)); |
Lei YU | 065cf76 | 2020-02-28 15:31:28 +0800 | [diff] [blame] | 98 | memcpy(requestMsg.data() + sizeof(effecterIdLE) + sizeof(compEffecterCnt) + |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 99 | hdrSize, |
Sampa Misra | 0db1dfa | 2019-03-19 00:15:31 -0500 | [diff] [blame] | 100 | &stateField, sizeof(stateField)); |
| 101 | |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 102 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 103 | |
Sampa Misra | 0db1dfa | 2019-03-19 00:15:31 -0500 | [diff] [blame] | 104 | auto rc = decode_set_state_effecter_states_req( |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 105 | request, requestMsg.size() - hdrSize, &retEffecterId, |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 106 | &retCompEffecterCnt, retStateField.data()); |
Sampa Misra | 0db1dfa | 2019-03-19 00:15:31 -0500 | [diff] [blame] | 107 | |
George Liu | 684a716 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 108 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 109 | EXPECT_EQ(effecterId, retEffecterId); |
| 110 | EXPECT_EQ(retCompEffecterCnt, compEffecterCnt); |
| 111 | EXPECT_EQ(retStateField[0].set_request, stateField[0].set_request); |
| 112 | EXPECT_EQ(retStateField[0].effecter_state, stateField[0].effecter_state); |
| 113 | EXPECT_EQ(retStateField[1].set_request, stateField[1].set_request); |
| 114 | EXPECT_EQ(retStateField[1].effecter_state, stateField[1].effecter_state); |
Sampa Misra | 0db1dfa | 2019-03-19 00:15:31 -0500 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | TEST(SetStateEffecterStates, testBadDecodeRequest) |
| 118 | { |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 119 | const struct pldm_msg* msg = NULL; |
Sampa Misra | 0db1dfa | 2019-03-19 00:15:31 -0500 | [diff] [blame] | 120 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 121 | auto rc = decode_set_state_effecter_states_req(msg, sizeof(*msg), NULL, |
| 122 | NULL, NULL); |
Sampa Misra | 0db1dfa | 2019-03-19 00:15:31 -0500 | [diff] [blame] | 123 | |
George Liu | 684a716 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 124 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
Sampa Misra | 0db1dfa | 2019-03-19 00:15:31 -0500 | [diff] [blame] | 125 | } |
vkaverap | 98a2c19 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 126 | |
| 127 | TEST(SetStateEffecterStates, testBadDecodeResponse) |
| 128 | { |
| 129 | std::array<uint8_t, PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES> |
| 130 | responseMsg{}; |
| 131 | |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 132 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 133 | |
| 134 | auto rc = decode_set_state_effecter_states_resp(response, |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 135 | responseMsg.size(), NULL); |
vkaverap | 98a2c19 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 136 | |
George Liu | 684a716 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 137 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
vkaverap | 98a2c19 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 138 | } |
Sampa Misra | 7fcfb66 | 2019-05-08 13:13:53 -0500 | [diff] [blame] | 139 | |
| 140 | TEST(GetPDR, testGoodEncodeResponse) |
| 141 | { |
| 142 | uint8_t completionCode = 0; |
| 143 | uint32_t nextRecordHndl = 0x12; |
| 144 | uint32_t nextDataTransferHndl = 0x13; |
Deepak Kodihalli | e4b16ee | 2019-08-14 06:54:38 -0500 | [diff] [blame] | 145 | uint8_t transferFlag = PLDM_END; |
Sampa Misra | 7fcfb66 | 2019-05-08 13:13:53 -0500 | [diff] [blame] | 146 | uint16_t respCnt = 0x5; |
| 147 | std::vector<uint8_t> recordData{1, 2, 3, 4, 5}; |
Deepak Kodihalli | e4b16ee | 2019-08-14 06:54:38 -0500 | [diff] [blame] | 148 | uint8_t transferCRC = 6; |
Sampa Misra | 7fcfb66 | 2019-05-08 13:13:53 -0500 | [diff] [blame] | 149 | |
| 150 | // + size of record data and transfer CRC |
| 151 | std::vector<uint8_t> responseMsg(hdrSize + PLDM_GET_PDR_MIN_RESP_BYTES + |
| 152 | recordData.size() + 1); |
| 153 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 154 | |
| 155 | auto rc = encode_get_pdr_resp(0, PLDM_SUCCESS, nextRecordHndl, |
| 156 | nextDataTransferHndl, transferFlag, respCnt, |
| 157 | recordData.data(), transferCRC, response); |
| 158 | |
George Liu | 684a716 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 159 | EXPECT_EQ(rc, PLDM_SUCCESS); |
Sampa Misra | 7fcfb66 | 2019-05-08 13:13:53 -0500 | [diff] [blame] | 160 | struct pldm_get_pdr_resp* resp = |
| 161 | reinterpret_cast<struct pldm_get_pdr_resp*>(response->payload); |
| 162 | |
George Liu | 684a716 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 163 | EXPECT_EQ(completionCode, resp->completion_code); |
Lei YU | 065cf76 | 2020-02-28 15:31:28 +0800 | [diff] [blame] | 164 | EXPECT_EQ(nextRecordHndl, le32toh(resp->next_record_handle)); |
| 165 | EXPECT_EQ(nextDataTransferHndl, le32toh(resp->next_data_transfer_handle)); |
George Liu | 684a716 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 166 | EXPECT_EQ(transferFlag, resp->transfer_flag); |
Lei YU | 065cf76 | 2020-02-28 15:31:28 +0800 | [diff] [blame] | 167 | EXPECT_EQ(respCnt, le16toh(resp->response_count)); |
George Liu | 684a716 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 168 | EXPECT_EQ(0, |
Sampa Misra | 7fcfb66 | 2019-05-08 13:13:53 -0500 | [diff] [blame] | 169 | memcmp(recordData.data(), resp->record_data, recordData.size())); |
George Liu | 684a716 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 170 | EXPECT_EQ(*(response->payload + sizeof(pldm_get_pdr_resp) - 1 + |
Deepak Kodihalli | e4b16ee | 2019-08-14 06:54:38 -0500 | [diff] [blame] | 171 | recordData.size()), |
| 172 | transferCRC); |
| 173 | |
| 174 | transferFlag = PLDM_START_AND_END; // No CRC in this case |
| 175 | responseMsg.resize(responseMsg.size() - sizeof(transferCRC)); |
| 176 | rc = encode_get_pdr_resp(0, PLDM_SUCCESS, nextRecordHndl, |
| 177 | nextDataTransferHndl, transferFlag, respCnt, |
| 178 | recordData.data(), transferCRC, response); |
George Liu | 684a716 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 179 | EXPECT_EQ(rc, PLDM_SUCCESS); |
Sampa Misra | 7fcfb66 | 2019-05-08 13:13:53 -0500 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | TEST(GetPDR, testBadEncodeResponse) |
| 183 | { |
| 184 | uint32_t nextRecordHndl = 0x12; |
| 185 | uint32_t nextDataTransferHndl = 0x13; |
| 186 | uint8_t transferFlag = PLDM_START_AND_END; |
| 187 | uint16_t respCnt = 0x5; |
| 188 | std::vector<uint8_t> recordData{1, 2, 3, 4, 5}; |
| 189 | uint8_t transferCRC = 0; |
| 190 | |
| 191 | auto rc = encode_get_pdr_resp(0, PLDM_SUCCESS, nextRecordHndl, |
| 192 | nextDataTransferHndl, transferFlag, respCnt, |
| 193 | recordData.data(), transferCRC, nullptr); |
| 194 | |
George Liu | 684a716 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 195 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
Sampa Misra | 7fcfb66 | 2019-05-08 13:13:53 -0500 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | TEST(GetPDR, testGoodDecodeRequest) |
| 199 | { |
| 200 | std::array<uint8_t, hdrSize + PLDM_GET_PDR_REQ_BYTES> requestMsg{}; |
| 201 | |
| 202 | uint32_t recordHndl = 0x32; |
| 203 | uint32_t dataTransferHndl = 0x11; |
| 204 | uint8_t transferOpFlag = PLDM_GET_FIRSTPART; |
| 205 | uint16_t requestCnt = 0x5; |
Lei YU | 065cf76 | 2020-02-28 15:31:28 +0800 | [diff] [blame] | 206 | uint16_t recordChangeNum = 0x01; |
Sampa Misra | 7fcfb66 | 2019-05-08 13:13:53 -0500 | [diff] [blame] | 207 | |
| 208 | uint32_t retRecordHndl = 0; |
| 209 | uint32_t retDataTransferHndl = 0; |
| 210 | uint8_t retTransferOpFlag = 0; |
| 211 | uint16_t retRequestCnt = 0; |
| 212 | uint16_t retRecordChangeNum = 0; |
| 213 | |
| 214 | auto req = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 215 | struct pldm_get_pdr_req* request = |
| 216 | reinterpret_cast<struct pldm_get_pdr_req*>(req->payload); |
| 217 | |
Lei YU | 065cf76 | 2020-02-28 15:31:28 +0800 | [diff] [blame] | 218 | request->record_handle = htole32(recordHndl); |
| 219 | request->data_transfer_handle = htole32(dataTransferHndl); |
Sampa Misra | 7fcfb66 | 2019-05-08 13:13:53 -0500 | [diff] [blame] | 220 | request->transfer_op_flag = transferOpFlag; |
Lei YU | 065cf76 | 2020-02-28 15:31:28 +0800 | [diff] [blame] | 221 | request->request_count = htole16(requestCnt); |
| 222 | request->record_change_number = htole16(recordChangeNum); |
Sampa Misra | 7fcfb66 | 2019-05-08 13:13:53 -0500 | [diff] [blame] | 223 | |
| 224 | auto rc = decode_get_pdr_req( |
| 225 | req, requestMsg.size() - hdrSize, &retRecordHndl, &retDataTransferHndl, |
| 226 | &retTransferOpFlag, &retRequestCnt, &retRecordChangeNum); |
| 227 | |
George Liu | 684a716 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 228 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 229 | EXPECT_EQ(retRecordHndl, recordHndl); |
| 230 | EXPECT_EQ(retDataTransferHndl, dataTransferHndl); |
| 231 | EXPECT_EQ(retTransferOpFlag, transferOpFlag); |
| 232 | EXPECT_EQ(retRequestCnt, requestCnt); |
| 233 | EXPECT_EQ(retRecordChangeNum, recordChangeNum); |
Sampa Misra | 7fcfb66 | 2019-05-08 13:13:53 -0500 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | TEST(GetPDR, testBadDecodeRequest) |
| 237 | { |
| 238 | std::array<uint8_t, PLDM_GET_PDR_REQ_BYTES> requestMsg{}; |
| 239 | auto req = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 240 | |
| 241 | auto rc = decode_get_pdr_req(req, requestMsg.size(), NULL, NULL, NULL, NULL, |
| 242 | NULL); |
| 243 | |
George Liu | 684a716 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 244 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
Sampa Misra | 7fcfb66 | 2019-05-08 13:13:53 -0500 | [diff] [blame] | 245 | } |
George Liu | 820a9a5 | 2019-11-26 14:43:59 +0800 | [diff] [blame] | 246 | |
| 247 | TEST(GetPDR, testGoodEncodeRequest) |
| 248 | { |
| 249 | uint32_t record_hndl = 0; |
| 250 | uint32_t data_transfer_hndl = 0; |
| 251 | uint8_t transfer_op_flag = PLDM_GET_FIRSTPART; |
| 252 | uint16_t request_cnt = 20; |
| 253 | uint16_t record_chg_num = 0; |
| 254 | |
| 255 | std::vector<uint8_t> requestMsg(hdrSize + PLDM_GET_PDR_REQ_BYTES); |
| 256 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 257 | |
| 258 | auto rc = encode_get_pdr_req(0, record_hndl, data_transfer_hndl, |
| 259 | transfer_op_flag, request_cnt, record_chg_num, |
| 260 | request, PLDM_GET_PDR_REQ_BYTES); |
| 261 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 262 | struct pldm_get_pdr_req* req = |
| 263 | reinterpret_cast<struct pldm_get_pdr_req*>(request->payload); |
Lei YU | 065cf76 | 2020-02-28 15:31:28 +0800 | [diff] [blame] | 264 | EXPECT_EQ(record_hndl, le32toh(req->record_handle)); |
| 265 | EXPECT_EQ(data_transfer_hndl, le32toh(req->data_transfer_handle)); |
George Liu | 820a9a5 | 2019-11-26 14:43:59 +0800 | [diff] [blame] | 266 | EXPECT_EQ(transfer_op_flag, req->transfer_op_flag); |
Lei YU | 065cf76 | 2020-02-28 15:31:28 +0800 | [diff] [blame] | 267 | EXPECT_EQ(request_cnt, le16toh(req->request_count)); |
| 268 | EXPECT_EQ(record_chg_num, le16toh(req->record_change_number)); |
George Liu | 820a9a5 | 2019-11-26 14:43:59 +0800 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | TEST(GetPDR, testBadEncodeRequest) |
| 272 | { |
| 273 | uint32_t record_hndl = 0; |
| 274 | uint32_t data_transfer_hndl = 0; |
| 275 | uint8_t transfer_op_flag = PLDM_GET_FIRSTPART; |
| 276 | uint16_t request_cnt = 32; |
| 277 | uint16_t record_chg_num = 0; |
| 278 | |
| 279 | std::vector<uint8_t> requestMsg(hdrSize + PLDM_GET_PDR_REQ_BYTES); |
| 280 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 281 | |
| 282 | auto rc = encode_get_pdr_req(0, record_hndl, data_transfer_hndl, |
| 283 | transfer_op_flag, request_cnt, record_chg_num, |
| 284 | nullptr, PLDM_GET_PDR_REQ_BYTES); |
| 285 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 286 | |
| 287 | rc = encode_get_pdr_req(0, record_hndl, data_transfer_hndl, |
| 288 | transfer_op_flag, request_cnt, record_chg_num, |
| 289 | request, PLDM_GET_PDR_REQ_BYTES + 1); |
| 290 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 291 | } |
| 292 | |
| 293 | TEST(GetPDR, testGoodDecodeResponse) |
| 294 | { |
| 295 | const char* recordData = "123456789"; |
| 296 | uint8_t completionCode = PLDM_SUCCESS; |
| 297 | uint32_t nextRecordHndl = 0; |
| 298 | uint32_t nextDataTransferHndl = 0; |
| 299 | uint8_t transferFlag = PLDM_END; |
| 300 | constexpr uint16_t respCnt = 9; |
| 301 | uint8_t transferCRC = 96; |
| 302 | size_t recordDataLength = 32; |
| 303 | std::array<uint8_t, hdrSize + PLDM_GET_PDR_MIN_RESP_BYTES + respCnt + |
| 304 | sizeof(transferCRC)> |
| 305 | responseMsg{}; |
| 306 | |
| 307 | uint8_t retCompletionCode = 0; |
| 308 | uint8_t retRecordData[32] = {0}; |
| 309 | uint32_t retNextRecordHndl = 0; |
| 310 | uint32_t retNextDataTransferHndl = 0; |
| 311 | uint8_t retTransferFlag = 0; |
| 312 | uint16_t retRespCnt = 0; |
| 313 | uint8_t retTransferCRC = 0; |
| 314 | |
| 315 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 316 | struct pldm_get_pdr_resp* resp = |
| 317 | reinterpret_cast<struct pldm_get_pdr_resp*>(response->payload); |
| 318 | resp->completion_code = completionCode; |
| 319 | resp->next_record_handle = htole32(nextRecordHndl); |
| 320 | resp->next_data_transfer_handle = htole32(nextDataTransferHndl); |
| 321 | resp->transfer_flag = transferFlag; |
| 322 | resp->response_count = htole16(respCnt); |
| 323 | memcpy(resp->record_data, recordData, respCnt); |
George Liu | 684a716 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 324 | response->payload[PLDM_GET_PDR_MIN_RESP_BYTES + respCnt] = transferCRC; |
George Liu | 820a9a5 | 2019-11-26 14:43:59 +0800 | [diff] [blame] | 325 | |
| 326 | auto rc = decode_get_pdr_resp( |
| 327 | response, responseMsg.size() - hdrSize, &retCompletionCode, |
| 328 | &retNextRecordHndl, &retNextDataTransferHndl, &retTransferFlag, |
| 329 | &retRespCnt, retRecordData, recordDataLength, &retTransferCRC); |
| 330 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 331 | EXPECT_EQ(retCompletionCode, completionCode); |
| 332 | EXPECT_EQ(retNextRecordHndl, nextRecordHndl); |
| 333 | EXPECT_EQ(retNextDataTransferHndl, nextDataTransferHndl); |
| 334 | EXPECT_EQ(retTransferFlag, transferFlag); |
| 335 | EXPECT_EQ(retRespCnt, respCnt); |
| 336 | EXPECT_EQ(retTransferCRC, transferCRC); |
| 337 | EXPECT_EQ(0, memcmp(recordData, resp->record_data, respCnt)); |
| 338 | } |
| 339 | |
| 340 | TEST(GetPDR, testBadDecodeResponse) |
| 341 | { |
| 342 | const char* recordData = "123456789"; |
| 343 | uint8_t completionCode = PLDM_SUCCESS; |
| 344 | uint32_t nextRecordHndl = 0; |
| 345 | uint32_t nextDataTransferHndl = 0; |
| 346 | uint8_t transferFlag = PLDM_END; |
| 347 | constexpr uint16_t respCnt = 9; |
| 348 | uint8_t transferCRC = 96; |
| 349 | size_t recordDataLength = 32; |
| 350 | std::array<uint8_t, hdrSize + PLDM_GET_PDR_MIN_RESP_BYTES + respCnt + |
| 351 | sizeof(transferCRC)> |
| 352 | responseMsg{}; |
| 353 | |
| 354 | uint8_t retCompletionCode = 0; |
George Liu | 684a716 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 355 | uint8_t retRecordData[32] = {0}; |
George Liu | 820a9a5 | 2019-11-26 14:43:59 +0800 | [diff] [blame] | 356 | uint32_t retNextRecordHndl = 0; |
| 357 | uint32_t retNextDataTransferHndl = 0; |
| 358 | uint8_t retTransferFlag = 0; |
| 359 | uint16_t retRespCnt = 0; |
| 360 | uint8_t retTransferCRC = 0; |
| 361 | |
| 362 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 363 | struct pldm_get_pdr_resp* resp = |
| 364 | reinterpret_cast<struct pldm_get_pdr_resp*>(response->payload); |
| 365 | resp->completion_code = completionCode; |
| 366 | resp->next_record_handle = htole32(nextRecordHndl); |
| 367 | resp->next_data_transfer_handle = htole32(nextDataTransferHndl); |
| 368 | resp->transfer_flag = transferFlag; |
| 369 | resp->response_count = htole16(respCnt); |
| 370 | memcpy(resp->record_data, recordData, respCnt); |
George Liu | 684a716 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 371 | response->payload[PLDM_GET_PDR_MIN_RESP_BYTES + respCnt] = transferCRC; |
George Liu | 820a9a5 | 2019-11-26 14:43:59 +0800 | [diff] [blame] | 372 | |
| 373 | auto rc = decode_get_pdr_resp(response, responseMsg.size() - hdrSize, NULL, |
| 374 | NULL, NULL, NULL, NULL, NULL, 0, NULL); |
| 375 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 376 | |
| 377 | rc = decode_get_pdr_resp( |
| 378 | response, responseMsg.size() - hdrSize - 1, &retCompletionCode, |
| 379 | &retNextRecordHndl, &retNextDataTransferHndl, &retTransferFlag, |
| 380 | &retRespCnt, retRecordData, recordDataLength, &retTransferCRC); |
| 381 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 382 | } |
George Liu | 30b859f | 2020-01-07 15:03:22 +0800 | [diff] [blame] | 383 | |
| 384 | TEST(SetNumericEffecterValue, testGoodDecodeRequest) |
| 385 | { |
| 386 | std::array<uint8_t, |
| 387 | hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 3> |
| 388 | requestMsg{}; |
| 389 | |
| 390 | uint16_t effecter_id = 32768; |
| 391 | uint8_t effecter_data_size = PLDM_EFFECTER_DATA_SIZE_UINT32; |
| 392 | uint32_t effecter_value = 123456789; |
| 393 | |
| 394 | uint16_t reteffecter_id; |
| 395 | uint8_t reteffecter_data_size; |
| 396 | uint8_t reteffecter_value[4]; |
| 397 | |
| 398 | auto req = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 399 | struct pldm_set_numeric_effecter_value_req* request = |
| 400 | reinterpret_cast<struct pldm_set_numeric_effecter_value_req*>( |
| 401 | req->payload); |
| 402 | |
Lei YU | 065cf76 | 2020-02-28 15:31:28 +0800 | [diff] [blame] | 403 | request->effecter_id = htole16(effecter_id); |
George Liu | 30b859f | 2020-01-07 15:03:22 +0800 | [diff] [blame] | 404 | request->effecter_data_size = effecter_data_size; |
Lei YU | 065cf76 | 2020-02-28 15:31:28 +0800 | [diff] [blame] | 405 | uint32_t effecter_value_le = htole32(effecter_value); |
| 406 | memcpy(request->effecter_value, &effecter_value_le, |
| 407 | sizeof(effecter_value_le)); |
George Liu | 30b859f | 2020-01-07 15:03:22 +0800 | [diff] [blame] | 408 | |
| 409 | auto rc = decode_set_numeric_effecter_value_req( |
| 410 | req, requestMsg.size() - hdrSize, &reteffecter_id, |
Lei YU | 065cf76 | 2020-02-28 15:31:28 +0800 | [diff] [blame] | 411 | &reteffecter_data_size, reteffecter_value); |
George Liu | 30b859f | 2020-01-07 15:03:22 +0800 | [diff] [blame] | 412 | |
George Liu | da75abe | 2020-04-09 13:36:13 +0800 | [diff] [blame] | 413 | uint32_t value = *(reinterpret_cast<uint32_t*>(reteffecter_value)); |
George Liu | 30b859f | 2020-01-07 15:03:22 +0800 | [diff] [blame] | 414 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 415 | EXPECT_EQ(reteffecter_id, effecter_id); |
| 416 | EXPECT_EQ(reteffecter_data_size, effecter_data_size); |
| 417 | EXPECT_EQ(value, effecter_value); |
| 418 | } |
| 419 | |
| 420 | TEST(SetNumericEffecterValue, testBadDecodeRequest) |
| 421 | { |
| 422 | std::array<uint8_t, hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES> |
| 423 | requestMsg{}; |
| 424 | |
| 425 | auto rc = decode_set_numeric_effecter_value_req( |
| 426 | NULL, requestMsg.size() - hdrSize, NULL, NULL, NULL); |
| 427 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 428 | |
| 429 | uint16_t effecter_id = 0x10; |
| 430 | uint8_t effecter_data_size = PLDM_EFFECTER_DATA_SIZE_UINT8; |
| 431 | uint8_t effecter_value = 1; |
| 432 | |
| 433 | uint16_t reteffecter_id; |
| 434 | uint8_t reteffecter_data_size; |
| 435 | uint8_t reteffecter_value[4]; |
| 436 | |
| 437 | auto req = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 438 | struct pldm_set_numeric_effecter_value_req* request = |
| 439 | reinterpret_cast<struct pldm_set_numeric_effecter_value_req*>( |
| 440 | req->payload); |
| 441 | |
| 442 | request->effecter_id = effecter_id; |
| 443 | request->effecter_data_size = effecter_data_size; |
| 444 | memcpy(request->effecter_value, &effecter_value, sizeof(effecter_value)); |
| 445 | |
| 446 | rc = decode_set_numeric_effecter_value_req( |
| 447 | req, requestMsg.size() - hdrSize - 1, &reteffecter_id, |
| 448 | &reteffecter_data_size, reinterpret_cast<uint8_t*>(&reteffecter_value)); |
| 449 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 450 | } |
| 451 | |
| 452 | TEST(SetNumericEffecterValue, testGoodEncodeRequest) |
| 453 | { |
| 454 | uint16_t effecter_id = 0; |
George Liu | ab74950 | 2020-04-15 13:16:35 +0800 | [diff] [blame] | 455 | uint8_t effecter_data_size = PLDM_EFFECTER_DATA_SIZE_UINT16; |
| 456 | uint16_t effecter_value = 65534; |
George Liu | 30b859f | 2020-01-07 15:03:22 +0800 | [diff] [blame] | 457 | |
| 458 | std::vector<uint8_t> requestMsg( |
George Liu | ab74950 | 2020-04-15 13:16:35 +0800 | [diff] [blame] | 459 | hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 1); |
George Liu | 30b859f | 2020-01-07 15:03:22 +0800 | [diff] [blame] | 460 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 461 | |
| 462 | auto rc = encode_set_numeric_effecter_value_req( |
| 463 | 0, effecter_id, effecter_data_size, |
| 464 | reinterpret_cast<uint8_t*>(&effecter_value), request, |
George Liu | ab74950 | 2020-04-15 13:16:35 +0800 | [diff] [blame] | 465 | PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 1); |
George Liu | 30b859f | 2020-01-07 15:03:22 +0800 | [diff] [blame] | 466 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 467 | |
| 468 | struct pldm_set_numeric_effecter_value_req* req = |
| 469 | reinterpret_cast<struct pldm_set_numeric_effecter_value_req*>( |
| 470 | request->payload); |
| 471 | EXPECT_EQ(effecter_id, req->effecter_id); |
| 472 | EXPECT_EQ(effecter_data_size, req->effecter_data_size); |
George Liu | ab74950 | 2020-04-15 13:16:35 +0800 | [diff] [blame] | 473 | uint16_t* val = (uint16_t*)req->effecter_value; |
| 474 | *val = le16toh(*val); |
| 475 | EXPECT_EQ(effecter_value, *val); |
George Liu | 30b859f | 2020-01-07 15:03:22 +0800 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | TEST(SetNumericEffecterValue, testBadEncodeRequest) |
| 479 | { |
| 480 | std::vector<uint8_t> requestMsg( |
| 481 | hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES); |
| 482 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 483 | |
| 484 | auto rc = encode_set_numeric_effecter_value_req( |
| 485 | 0, 0, 0, NULL, NULL, PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES); |
| 486 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 487 | |
| 488 | uint16_t effecter_value; |
| 489 | rc = encode_set_numeric_effecter_value_req( |
| 490 | 0, 0, 6, reinterpret_cast<uint8_t*>(&effecter_value), request, |
| 491 | PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES); |
| 492 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 493 | } |
| 494 | |
| 495 | TEST(SetNumericEffecterValue, testGoodDecodeResponse) |
| 496 | { |
| 497 | std::array<uint8_t, hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES> |
| 498 | responseMsg{}; |
| 499 | |
| 500 | uint8_t completion_code = 0xA0; |
| 501 | |
| 502 | uint8_t retcompletion_code; |
| 503 | |
| 504 | memcpy(responseMsg.data() + hdrSize, &completion_code, |
| 505 | sizeof(completion_code)); |
| 506 | |
| 507 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 508 | |
| 509 | auto rc = decode_set_numeric_effecter_value_resp( |
| 510 | response, responseMsg.size() - hdrSize, &retcompletion_code); |
| 511 | |
| 512 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 513 | EXPECT_EQ(completion_code, retcompletion_code); |
| 514 | } |
| 515 | |
| 516 | TEST(SetNumericEffecterValue, testBadDecodeResponse) |
| 517 | { |
| 518 | std::array<uint8_t, PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES> |
| 519 | responseMsg{}; |
| 520 | |
| 521 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 522 | |
| 523 | auto rc = decode_set_numeric_effecter_value_resp(response, |
| 524 | responseMsg.size(), NULL); |
| 525 | |
| 526 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 527 | } |
| 528 | |
| 529 | TEST(SetNumericEffecterValue, testGoodEncodeResponse) |
| 530 | { |
| 531 | std::array<uint8_t, sizeof(pldm_msg_hdr) + |
| 532 | PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES> |
| 533 | responseMsg{}; |
| 534 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 535 | uint8_t completionCode = 0; |
| 536 | |
| 537 | auto rc = encode_set_numeric_effecter_value_resp( |
| 538 | 0, PLDM_SUCCESS, response, PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES); |
| 539 | |
| 540 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 541 | EXPECT_EQ(completionCode, response->payload[0]); |
| 542 | } |
| 543 | |
| 544 | TEST(SetNumericEffecterValue, testBadEncodeResponse) |
| 545 | { |
| 546 | auto rc = encode_set_numeric_effecter_value_resp( |
| 547 | 0, PLDM_SUCCESS, NULL, PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES); |
| 548 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 549 | } |
Jolie Ku | 3557bad | 2020-03-02 16:22:57 +0800 | [diff] [blame] | 550 | |
| 551 | TEST(GetStateSensorReadings, testGoodEncodeResponse) |
| 552 | { |
George Liu | 80237ef | 2020-07-10 15:16:39 +0800 | [diff] [blame] | 553 | std::array<uint8_t, hdrSize + |
| 554 | PLDM_GET_STATE_SENSOR_READINGS_MIN_RESP_BYTES + |
| 555 | sizeof(get_sensor_state_field) * 2> |
Jolie Ku | 3557bad | 2020-03-02 16:22:57 +0800 | [diff] [blame] | 556 | responseMsg{}; |
| 557 | |
| 558 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 559 | uint8_t completionCode = 0; |
| 560 | uint8_t comp_sensorCnt = 0x2; |
| 561 | |
| 562 | std::array<get_sensor_state_field, 2> stateField{}; |
George Liu | 9d15502 | 2020-07-10 15:06:35 +0800 | [diff] [blame] | 563 | stateField[0] = {PLDM_SENSOR_ENABLED, PLDM_SENSOR_NORMAL, |
| 564 | PLDM_SENSOR_WARNING, PLDM_SENSOR_UNKNOWN}; |
| 565 | stateField[1] = {PLDM_SENSOR_FAILED, PLDM_SENSOR_UPPERFATAL, |
| 566 | PLDM_SENSOR_UPPERCRITICAL, PLDM_SENSOR_FATAL}; |
Jolie Ku | 3557bad | 2020-03-02 16:22:57 +0800 | [diff] [blame] | 567 | |
| 568 | auto rc = encode_get_state_sensor_readings_resp( |
| 569 | 0, PLDM_SUCCESS, comp_sensorCnt, stateField.data(), response); |
| 570 | |
| 571 | struct pldm_get_state_sensor_readings_resp* resp = |
| 572 | reinterpret_cast<struct pldm_get_state_sensor_readings_resp*>( |
| 573 | response->payload); |
| 574 | |
| 575 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 576 | EXPECT_EQ(completionCode, resp->completion_code); |
| 577 | EXPECT_EQ(comp_sensorCnt, resp->comp_sensor_count); |
| 578 | EXPECT_EQ(stateField[0].sensor_op_state, resp->field->sensor_op_state); |
| 579 | EXPECT_EQ(stateField[0].present_state, resp->field->present_state); |
| 580 | EXPECT_EQ(stateField[0].previous_state, resp->field->previous_state); |
| 581 | EXPECT_EQ(stateField[0].event_state, resp->field->event_state); |
| 582 | EXPECT_EQ(stateField[1].sensor_op_state, resp->field[1].sensor_op_state); |
| 583 | EXPECT_EQ(stateField[1].present_state, resp->field[1].present_state); |
| 584 | EXPECT_EQ(stateField[1].previous_state, resp->field[1].previous_state); |
| 585 | EXPECT_EQ(stateField[1].event_state, resp->field[1].event_state); |
| 586 | } |
| 587 | |
| 588 | TEST(GetStateSensorReadings, testBadEncodeResponse) |
| 589 | { |
| 590 | auto rc = encode_get_state_sensor_readings_resp(0, PLDM_SUCCESS, 0, nullptr, |
| 591 | nullptr); |
| 592 | |
| 593 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 594 | } |
| 595 | |
| 596 | TEST(GetStateSensorReadings, testGoodDecodeResponse) |
| 597 | { |
George Liu | 80237ef | 2020-07-10 15:16:39 +0800 | [diff] [blame] | 598 | std::array<uint8_t, hdrSize + |
| 599 | PLDM_GET_STATE_SENSOR_READINGS_MIN_RESP_BYTES + |
| 600 | sizeof(get_sensor_state_field) * 2> |
Jolie Ku | 3557bad | 2020-03-02 16:22:57 +0800 | [diff] [blame] | 601 | responseMsg{}; |
| 602 | |
| 603 | uint8_t completionCode = 0; |
| 604 | uint8_t comp_sensorCnt = 2; |
| 605 | |
| 606 | std::array<get_sensor_state_field, 2> stateField{}; |
George Liu | 9d15502 | 2020-07-10 15:06:35 +0800 | [diff] [blame] | 607 | stateField[0] = {PLDM_SENSOR_DISABLED, PLDM_SENSOR_UNKNOWN, |
| 608 | PLDM_SENSOR_UNKNOWN, PLDM_SENSOR_UNKNOWN}; |
| 609 | stateField[1] = {PLDM_SENSOR_ENABLED, PLDM_SENSOR_LOWERFATAL, |
| 610 | PLDM_SENSOR_LOWERCRITICAL, PLDM_SENSOR_WARNING}; |
Jolie Ku | 3557bad | 2020-03-02 16:22:57 +0800 | [diff] [blame] | 611 | |
| 612 | uint8_t retcompletion_code = 0; |
George Liu | 38eaa27 | 2021-01-21 19:30:15 +0800 | [diff] [blame] | 613 | uint8_t retcomp_sensorCnt = 0; |
Jolie Ku | 3557bad | 2020-03-02 16:22:57 +0800 | [diff] [blame] | 614 | std::array<get_sensor_state_field, 2> retstateField{}; |
| 615 | |
| 616 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 617 | struct pldm_get_state_sensor_readings_resp* resp = |
| 618 | reinterpret_cast<struct pldm_get_state_sensor_readings_resp*>( |
| 619 | response->payload); |
| 620 | |
| 621 | resp->completion_code = completionCode; |
| 622 | resp->comp_sensor_count = comp_sensorCnt; |
| 623 | memcpy(resp->field, &stateField, |
| 624 | (sizeof(get_sensor_state_field) * comp_sensorCnt)); |
| 625 | |
| 626 | auto rc = decode_get_state_sensor_readings_resp( |
| 627 | response, responseMsg.size() - hdrSize, &retcompletion_code, |
| 628 | &retcomp_sensorCnt, retstateField.data()); |
| 629 | |
| 630 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 631 | EXPECT_EQ(completionCode, retcompletion_code); |
| 632 | EXPECT_EQ(comp_sensorCnt, retcomp_sensorCnt); |
| 633 | EXPECT_EQ(stateField[0].sensor_op_state, retstateField[0].sensor_op_state); |
| 634 | EXPECT_EQ(stateField[0].present_state, retstateField[0].present_state); |
| 635 | EXPECT_EQ(stateField[0].previous_state, retstateField[0].previous_state); |
| 636 | EXPECT_EQ(stateField[0].event_state, retstateField[0].event_state); |
| 637 | EXPECT_EQ(stateField[1].sensor_op_state, retstateField[1].sensor_op_state); |
| 638 | EXPECT_EQ(stateField[1].present_state, retstateField[1].present_state); |
| 639 | EXPECT_EQ(stateField[1].previous_state, retstateField[1].previous_state); |
| 640 | EXPECT_EQ(stateField[1].event_state, retstateField[1].event_state); |
| 641 | } |
| 642 | |
| 643 | TEST(GetStateSensorReadings, testBadDecodeResponse) |
| 644 | { |
George Liu | 80237ef | 2020-07-10 15:16:39 +0800 | [diff] [blame] | 645 | std::array<uint8_t, hdrSize + |
| 646 | PLDM_GET_STATE_SENSOR_READINGS_MIN_RESP_BYTES + |
| 647 | sizeof(get_sensor_state_field) * 2> |
Jolie Ku | 3557bad | 2020-03-02 16:22:57 +0800 | [diff] [blame] | 648 | responseMsg{}; |
| 649 | |
| 650 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 651 | |
| 652 | auto rc = decode_get_state_sensor_readings_resp( |
| 653 | response, responseMsg.size() - hdrSize, nullptr, nullptr, nullptr); |
| 654 | |
| 655 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 656 | |
| 657 | uint8_t completionCode = 0; |
| 658 | uint8_t comp_sensorCnt = 1; |
| 659 | |
| 660 | std::array<get_sensor_state_field, 1> stateField{}; |
George Liu | 9d15502 | 2020-07-10 15:06:35 +0800 | [diff] [blame] | 661 | stateField[0] = {PLDM_SENSOR_ENABLED, PLDM_SENSOR_UPPERFATAL, |
| 662 | PLDM_SENSOR_UPPERCRITICAL, PLDM_SENSOR_WARNING}; |
Jolie Ku | 3557bad | 2020-03-02 16:22:57 +0800 | [diff] [blame] | 663 | |
| 664 | uint8_t retcompletion_code = 0; |
| 665 | uint8_t retcomp_sensorCnt = 0; |
| 666 | std::array<get_sensor_state_field, 1> retstateField{}; |
| 667 | |
| 668 | struct pldm_get_state_sensor_readings_resp* resp = |
| 669 | reinterpret_cast<struct pldm_get_state_sensor_readings_resp*>( |
| 670 | response->payload); |
| 671 | |
| 672 | resp->completion_code = completionCode; |
| 673 | resp->comp_sensor_count = comp_sensorCnt; |
| 674 | memcpy(resp->field, &stateField, |
| 675 | (sizeof(get_sensor_state_field) * comp_sensorCnt)); |
| 676 | |
| 677 | rc = decode_get_state_sensor_readings_resp( |
George Liu | 38eaa27 | 2021-01-21 19:30:15 +0800 | [diff] [blame] | 678 | response, responseMsg.size() - hdrSize + 1, &retcompletion_code, |
Jolie Ku | 3557bad | 2020-03-02 16:22:57 +0800 | [diff] [blame] | 679 | &retcomp_sensorCnt, retstateField.data()); |
| 680 | |
| 681 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 682 | } |
| 683 | |
| 684 | TEST(GetStateSensorReadings, testGoodEncodeRequest) |
| 685 | { |
| 686 | std::array<uint8_t, hdrSize + PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES> |
| 687 | requestMsg{}; |
| 688 | |
| 689 | uint16_t sensorId = 0xAB; |
| 690 | bitfield8_t sensorRearm; |
| 691 | sensorRearm.byte = 0x03; |
| 692 | |
| 693 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 694 | auto rc = encode_get_state_sensor_readings_req(0, sensorId, sensorRearm, 0, |
| 695 | request); |
| 696 | |
| 697 | struct pldm_get_state_sensor_readings_req* req = |
| 698 | reinterpret_cast<struct pldm_get_state_sensor_readings_req*>( |
| 699 | request->payload); |
| 700 | |
| 701 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 702 | EXPECT_EQ(sensorId, le16toh(req->sensor_id)); |
| 703 | EXPECT_EQ(sensorRearm.byte, req->sensor_rearm.byte); |
| 704 | } |
| 705 | |
| 706 | TEST(GetStateSensorReadings, testBadEncodeRequest) |
| 707 | { |
| 708 | bitfield8_t sensorRearm; |
| 709 | sensorRearm.byte = 0x0; |
| 710 | |
| 711 | auto rc = |
| 712 | encode_get_state_sensor_readings_req(0, 0, sensorRearm, 0, nullptr); |
| 713 | |
| 714 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 715 | } |
| 716 | |
| 717 | TEST(GetStateSensorReadings, testGoodDecodeRequest) |
| 718 | { |
| 719 | std::array<uint8_t, hdrSize + PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES> |
| 720 | requestMsg{}; |
| 721 | |
| 722 | uint16_t sensorId = 0xCD; |
| 723 | bitfield8_t sensorRearm; |
| 724 | sensorRearm.byte = 0x10; |
| 725 | |
| 726 | uint16_t retsensorId; |
| 727 | bitfield8_t retsensorRearm; |
| 728 | uint8_t retreserved; |
| 729 | |
| 730 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 731 | |
| 732 | struct pldm_get_state_sensor_readings_req* req = |
| 733 | reinterpret_cast<struct pldm_get_state_sensor_readings_req*>( |
| 734 | request->payload); |
| 735 | |
| 736 | req->sensor_id = htole16(sensorId); |
| 737 | req->sensor_rearm.byte = sensorRearm.byte; |
| 738 | |
| 739 | auto rc = decode_get_state_sensor_readings_req( |
| 740 | request, requestMsg.size() - hdrSize, &retsensorId, &retsensorRearm, |
| 741 | &retreserved); |
| 742 | |
| 743 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 744 | EXPECT_EQ(sensorId, retsensorId); |
| 745 | EXPECT_EQ(sensorRearm.byte, retsensorRearm.byte); |
| 746 | EXPECT_EQ(0, retreserved); |
| 747 | } |
| 748 | |
| 749 | TEST(GetStateSensorReadings, testBadDecodeRequest) |
| 750 | { |
| 751 | std::array<uint8_t, hdrSize + PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES> |
| 752 | requestMsg{}; |
| 753 | |
| 754 | auto rc = decode_get_state_sensor_readings_req( |
| 755 | nullptr, requestMsg.size() - hdrSize, nullptr, nullptr, nullptr); |
| 756 | |
| 757 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 758 | uint16_t sensorId = 0x11; |
| 759 | bitfield8_t sensorRearm; |
| 760 | sensorRearm.byte = 0x04; |
| 761 | |
| 762 | uint16_t retsensorId; |
| 763 | bitfield8_t retsensorRearm; |
| 764 | uint8_t retreserved; |
| 765 | |
| 766 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 767 | |
| 768 | struct pldm_get_state_sensor_readings_req* req = |
| 769 | reinterpret_cast<struct pldm_get_state_sensor_readings_req*>( |
| 770 | request->payload); |
| 771 | |
| 772 | req->sensor_id = htole16(sensorId); |
| 773 | req->sensor_rearm.byte = sensorRearm.byte; |
| 774 | |
| 775 | rc = decode_get_state_sensor_readings_req( |
| 776 | request, requestMsg.size() - hdrSize - 1, &retsensorId, &retsensorRearm, |
| 777 | &retreserved); |
| 778 | |
| 779 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 780 | } |
Zahed Hossain | d4abab1 | 2020-02-06 03:36:43 -0600 | [diff] [blame] | 781 | |
| 782 | TEST(PlatformEventMessage, testGoodStateSensorDecodeRequest) |
| 783 | { |
| 784 | std::array<uint8_t, |
| 785 | hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES + |
| 786 | PLDM_PLATFORM_EVENT_MESSAGE_STATE_SENSOR_STATE_REQ_BYTES> |
| 787 | requestMsg{}; |
| 788 | |
| 789 | uint8_t retFormatVersion = 0; |
| 790 | uint8_t retTid = 0; |
| 791 | uint8_t retEventClass = 0; |
| 792 | size_t retEventDataOffset = 0; |
| 793 | |
| 794 | auto req = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 795 | struct pldm_platform_event_message_req* request = |
| 796 | reinterpret_cast<struct pldm_platform_event_message_req*>(req->payload); |
| 797 | |
| 798 | uint8_t formatVersion = 0x01; |
| 799 | uint8_t tid = 0x02; |
| 800 | // Sensor Event |
| 801 | uint8_t eventClass = 0x00; |
| 802 | |
| 803 | request->format_version = formatVersion; |
| 804 | request->tid = tid; |
| 805 | request->event_class = eventClass; |
| 806 | size_t eventDataOffset = |
| 807 | sizeof(formatVersion) + sizeof(tid) + sizeof(eventClass); |
| 808 | |
| 809 | auto rc = decode_platform_event_message_req( |
| 810 | req, requestMsg.size() - hdrSize, &retFormatVersion, &retTid, |
| 811 | &retEventClass, &retEventDataOffset); |
| 812 | |
| 813 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 814 | EXPECT_EQ(retFormatVersion, formatVersion); |
| 815 | EXPECT_EQ(retTid, tid); |
| 816 | EXPECT_EQ(retEventClass, eventClass); |
| 817 | EXPECT_EQ(retEventDataOffset, eventDataOffset); |
| 818 | } |
| 819 | |
| 820 | TEST(PlatformEventMessage, testBadDecodeRequest) |
| 821 | { |
| 822 | const struct pldm_msg* msg = NULL; |
| 823 | std::array<uint8_t, |
| 824 | hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES + |
| 825 | PLDM_PLATFORM_EVENT_MESSAGE_STATE_SENSOR_STATE_REQ_BYTES - 1> |
| 826 | requestMsg{}; |
| 827 | auto req = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 828 | uint8_t retFormatVersion; |
| 829 | uint8_t retTid = 0; |
| 830 | uint8_t retEventClass = 0; |
| 831 | size_t retEventDataOffset; |
| 832 | |
| 833 | auto rc = decode_platform_event_message_req(msg, sizeof(*msg), NULL, NULL, |
| 834 | NULL, NULL); |
| 835 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 836 | |
| 837 | rc = decode_platform_event_message_req( |
| 838 | req, |
| 839 | requestMsg.size() - hdrSize - |
| 840 | PLDM_PLATFORM_EVENT_MESSAGE_STATE_SENSOR_STATE_REQ_BYTES, |
| 841 | &retFormatVersion, &retTid, &retEventClass, &retEventDataOffset); |
| 842 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 843 | } |
| 844 | |
| 845 | TEST(PlatformEventMessage, testGoodEncodeResponse) |
| 846 | { |
| 847 | std::array<uint8_t, |
| 848 | hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES + |
| 849 | PLDM_PLATFORM_EVENT_MESSAGE_STATE_SENSOR_STATE_REQ_BYTES - 1> |
| 850 | responseMsg{}; |
| 851 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 852 | uint8_t completionCode = 0; |
| 853 | uint8_t instanceId = 0x01; |
Pavithra Barithaya | 2ea1f07 | 2020-04-03 09:30:23 -0500 | [diff] [blame] | 854 | uint8_t platformEventStatus = 0x01; |
Zahed Hossain | d4abab1 | 2020-02-06 03:36:43 -0600 | [diff] [blame] | 855 | |
| 856 | auto rc = encode_platform_event_message_resp(instanceId, PLDM_SUCCESS, |
Pavithra Barithaya | 2ea1f07 | 2020-04-03 09:30:23 -0500 | [diff] [blame] | 857 | platformEventStatus, response); |
Zahed Hossain | d4abab1 | 2020-02-06 03:36:43 -0600 | [diff] [blame] | 858 | |
| 859 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 860 | EXPECT_EQ(completionCode, response->payload[0]); |
Pavithra Barithaya | 2ea1f07 | 2020-04-03 09:30:23 -0500 | [diff] [blame] | 861 | EXPECT_EQ(platformEventStatus, response->payload[1]); |
Zahed Hossain | d4abab1 | 2020-02-06 03:36:43 -0600 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | TEST(PlatformEventMessage, testBadEncodeResponse) |
| 865 | { |
| 866 | auto rc = encode_platform_event_message_resp(0, PLDM_SUCCESS, 1, NULL); |
| 867 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 868 | } |
Zahed Hossain | 1c86171 | 2020-03-04 08:55:19 -0600 | [diff] [blame] | 869 | |
Pavithra Barithaya | 35f2b2c | 2020-04-08 01:35:56 -0500 | [diff] [blame] | 870 | TEST(PlatformEventMessage, testGoodEncodeRequest) |
| 871 | { |
| 872 | uint8_t formatVersion = 0x01; |
| 873 | uint8_t Tid = 0x03; |
| 874 | uint8_t eventClass = 0x00; |
| 875 | uint8_t eventData = 34; |
| 876 | |
| 877 | std::array<uint8_t, hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES + |
| 878 | sizeof(eventData)> |
| 879 | requestMsg{}; |
| 880 | |
| 881 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 882 | auto rc = encode_platform_event_message_req( |
| 883 | 0, formatVersion, Tid, eventClass, |
Christian Geddes | 3bdb3c2 | 2020-05-01 14:55:39 -0500 | [diff] [blame] | 884 | reinterpret_cast<uint8_t*>(&eventData), sizeof(eventData), request, |
| 885 | sizeof(eventData) + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES); |
Pavithra Barithaya | 35f2b2c | 2020-04-08 01:35:56 -0500 | [diff] [blame] | 886 | |
| 887 | struct pldm_platform_event_message_req* req = |
| 888 | reinterpret_cast<struct pldm_platform_event_message_req*>( |
| 889 | request->payload); |
| 890 | |
| 891 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 892 | EXPECT_EQ(formatVersion, req->format_version); |
| 893 | EXPECT_EQ(Tid, req->tid); |
| 894 | EXPECT_EQ(eventClass, req->event_class); |
| 895 | EXPECT_EQ(0, memcmp(&eventData, req->event_data, sizeof(eventData))); |
| 896 | } |
| 897 | |
| 898 | TEST(PlatformEventMessage, testBadEncodeRequest) |
| 899 | { |
| 900 | uint8_t Tid = 0x03; |
| 901 | uint8_t eventClass = 0x00; |
| 902 | uint8_t eventData = 34; |
Christian Geddes | 3bdb3c2 | 2020-05-01 14:55:39 -0500 | [diff] [blame] | 903 | size_t sz_eventData = sizeof(eventData); |
| 904 | size_t payloadLen = |
| 905 | sz_eventData + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES; |
Pavithra Barithaya | 35f2b2c | 2020-04-08 01:35:56 -0500 | [diff] [blame] | 906 | uint8_t formatVersion = 0x01; |
| 907 | |
| 908 | std::array<uint8_t, hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES + |
| 909 | sizeof(eventData)> |
| 910 | requestMsg{}; |
| 911 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
Christian Geddes | 3bdb3c2 | 2020-05-01 14:55:39 -0500 | [diff] [blame] | 912 | |
Pavithra Barithaya | 35f2b2c | 2020-04-08 01:35:56 -0500 | [diff] [blame] | 913 | auto rc = encode_platform_event_message_req( |
| 914 | 0, formatVersion, Tid, eventClass, |
Christian Geddes | 3bdb3c2 | 2020-05-01 14:55:39 -0500 | [diff] [blame] | 915 | reinterpret_cast<uint8_t*>(&eventData), sz_eventData, nullptr, |
| 916 | payloadLen); |
Pavithra Barithaya | 35f2b2c | 2020-04-08 01:35:56 -0500 | [diff] [blame] | 917 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 918 | rc = encode_platform_event_message_req( |
| 919 | 0, 0, Tid, eventClass, reinterpret_cast<uint8_t*>(&eventData), |
Christian Geddes | 3bdb3c2 | 2020-05-01 14:55:39 -0500 | [diff] [blame] | 920 | sz_eventData, request, payloadLen); |
Pavithra Barithaya | 35f2b2c | 2020-04-08 01:35:56 -0500 | [diff] [blame] | 921 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 922 | rc = encode_platform_event_message_req(0, formatVersion, Tid, eventClass, |
Christian Geddes | 3bdb3c2 | 2020-05-01 14:55:39 -0500 | [diff] [blame] | 923 | nullptr, 0, request, payloadLen); |
Pavithra Barithaya | 35f2b2c | 2020-04-08 01:35:56 -0500 | [diff] [blame] | 924 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
Christian Geddes | 3bdb3c2 | 2020-05-01 14:55:39 -0500 | [diff] [blame] | 925 | rc = encode_platform_event_message_req( |
| 926 | 0, formatVersion, Tid, eventClass, |
| 927 | reinterpret_cast<uint8_t*>(&eventData), sz_eventData, request, 0); |
| 928 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
Pavithra Barithaya | 35f2b2c | 2020-04-08 01:35:56 -0500 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | TEST(PlatformEventMessage, testGoodDecodeResponse) |
| 932 | { |
| 933 | std::array<uint8_t, hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_RESP_BYTES> |
| 934 | responseMsg{}; |
| 935 | |
| 936 | uint8_t completionCode = PLDM_SUCCESS; |
| 937 | uint8_t platformEventStatus = 0x01; |
| 938 | |
| 939 | uint8_t retcompletionCode; |
| 940 | uint8_t retplatformEventStatus; |
| 941 | |
| 942 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 943 | struct pldm_platform_event_message_resp* resp = |
| 944 | reinterpret_cast<struct pldm_platform_event_message_resp*>( |
| 945 | response->payload); |
| 946 | |
| 947 | resp->completion_code = completionCode; |
| 948 | resp->platform_event_status = platformEventStatus; |
| 949 | |
| 950 | auto rc = decode_platform_event_message_resp( |
| 951 | response, responseMsg.size() - hdrSize, &retcompletionCode, |
| 952 | &retplatformEventStatus); |
| 953 | |
| 954 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 955 | EXPECT_EQ(completionCode, retcompletionCode); |
| 956 | EXPECT_EQ(platformEventStatus, retplatformEventStatus); |
| 957 | } |
| 958 | |
| 959 | TEST(PlatformEventMessage, testBadDecodeResponse) |
| 960 | { |
| 961 | std::array<uint8_t, hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_RESP_BYTES> |
| 962 | responseMsg{}; |
| 963 | |
| 964 | uint8_t completionCode = PLDM_SUCCESS; |
| 965 | uint8_t platformEventStatus = 0x01; |
| 966 | |
| 967 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 968 | struct pldm_platform_event_message_resp* resp = |
| 969 | reinterpret_cast<struct pldm_platform_event_message_resp*>( |
| 970 | response->payload); |
| 971 | resp->completion_code = completionCode; |
| 972 | resp->platform_event_status = platformEventStatus; |
| 973 | |
| 974 | auto rc = decode_platform_event_message_resp( |
| 975 | nullptr, responseMsg.size() - hdrSize, nullptr, nullptr); |
| 976 | |
| 977 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 978 | |
| 979 | rc = decode_platform_event_message_resp( |
| 980 | response, responseMsg.size() - hdrSize - 1, &completionCode, |
| 981 | &platformEventStatus); |
| 982 | |
| 983 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 984 | } |
| 985 | |
Zahed Hossain | 1c86171 | 2020-03-04 08:55:19 -0600 | [diff] [blame] | 986 | TEST(PlatformEventMessage, testGoodSensorEventDataDecodeRequest) |
| 987 | { |
| 988 | std::array<uint8_t, PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH + |
| 989 | PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES> |
| 990 | eventDataArr{}; |
| 991 | uint16_t sensorId = 0x1234; |
| 992 | uint8_t sensorEventClassType = PLDM_SENSOR_OP_STATE; |
| 993 | |
| 994 | struct pldm_sensor_event_data* eventData = |
| 995 | (struct pldm_sensor_event_data*)eventDataArr.data(); |
| 996 | eventData->sensor_id = sensorId; |
| 997 | eventData->sensor_event_class_type = sensorEventClassType; |
| 998 | |
| 999 | size_t retSensorOpDataOffset; |
| 1000 | uint16_t retSensorId = 0; |
| 1001 | uint8_t retSensorEventClassType; |
| 1002 | size_t sensorOpDataOffset = sizeof(sensorId) + sizeof(sensorEventClassType); |
| 1003 | auto rc = decode_sensor_event_data( |
| 1004 | reinterpret_cast<uint8_t*>(eventData), eventDataArr.size(), |
| 1005 | &retSensorId, &retSensorEventClassType, &retSensorOpDataOffset); |
| 1006 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 1007 | EXPECT_EQ(retSensorId, sensorId); |
| 1008 | EXPECT_EQ(retSensorEventClassType, sensorEventClassType); |
| 1009 | EXPECT_EQ(retSensorOpDataOffset, sensorOpDataOffset); |
| 1010 | } |
| 1011 | |
| 1012 | TEST(PlatformEventMessage, testBadSensorEventDataDecodeRequest) |
| 1013 | { |
| 1014 | |
| 1015 | std::array<uint8_t, PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH + |
| 1016 | PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES> |
| 1017 | eventDataArr{}; |
| 1018 | |
| 1019 | struct pldm_sensor_event_data* eventData = |
| 1020 | (struct pldm_sensor_event_data*)eventDataArr.data(); |
| 1021 | |
| 1022 | size_t retSensorOpDataOffset; |
| 1023 | uint16_t retSensorId = 0; |
| 1024 | uint8_t retSensorEventClassType; |
| 1025 | auto rc = decode_sensor_event_data(NULL, eventDataArr.size(), &retSensorId, |
| 1026 | &retSensorEventClassType, |
| 1027 | &retSensorOpDataOffset); |
| 1028 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 1029 | |
| 1030 | rc = decode_sensor_event_data( |
| 1031 | reinterpret_cast<uint8_t*>(eventDataArr.data()), |
| 1032 | eventDataArr.size() - |
| 1033 | PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH, |
| 1034 | &retSensorId, &retSensorEventClassType, &retSensorOpDataOffset); |
| 1035 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 1036 | |
| 1037 | eventData->sensor_event_class_type = PLDM_SENSOR_OP_STATE; |
| 1038 | |
| 1039 | rc = decode_sensor_event_data( |
| 1040 | reinterpret_cast<uint8_t*>(eventDataArr.data()), eventDataArr.size(), |
| 1041 | &retSensorId, &retSensorEventClassType, &retSensorOpDataOffset); |
| 1042 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 1043 | |
| 1044 | eventData->sensor_event_class_type = PLDM_STATE_SENSOR_STATE; |
| 1045 | rc = decode_sensor_event_data( |
| 1046 | reinterpret_cast<uint8_t*>(eventDataArr.data()), eventDataArr.size(), |
| 1047 | &retSensorId, &retSensorEventClassType, &retSensorOpDataOffset); |
| 1048 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 1049 | |
| 1050 | eventData->sensor_event_class_type = PLDM_NUMERIC_SENSOR_STATE; |
| 1051 | rc = decode_sensor_event_data( |
| 1052 | reinterpret_cast<uint8_t*>(eventDataArr.data()), |
| 1053 | eventDataArr.size() + 1, &retSensorId, &retSensorEventClassType, |
| 1054 | &retSensorOpDataOffset); |
| 1055 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 1056 | } |
| 1057 | |
| 1058 | TEST(PlatformEventMessage, testGoodSensorOpEventDataDecodeRequest) |
| 1059 | { |
| 1060 | std::array<uint8_t, PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH> |
| 1061 | eventDataArr{}; |
| 1062 | |
| 1063 | struct pldm_sensor_event_sensor_op_state* sensorData = |
| 1064 | (struct pldm_sensor_event_sensor_op_state*)eventDataArr.data(); |
| 1065 | uint8_t presentState = PLDM_SENSOR_ENABLED; |
| 1066 | uint8_t previousState = PLDM_SENSOR_INITIALIZING; |
| 1067 | sensorData->present_op_state = presentState; |
| 1068 | sensorData->previous_op_state = previousState; |
| 1069 | |
| 1070 | uint8_t retPresentState; |
| 1071 | uint8_t retPreviousState; |
| 1072 | auto rc = decode_sensor_op_data(reinterpret_cast<uint8_t*>(sensorData), |
| 1073 | eventDataArr.size(), &retPresentState, |
| 1074 | &retPreviousState); |
| 1075 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 1076 | EXPECT_EQ(retPresentState, presentState); |
| 1077 | EXPECT_EQ(retPreviousState, previousState); |
| 1078 | } |
| 1079 | |
| 1080 | TEST(PlatformEventMessage, testBadSensorOpEventDataDecodeRequest) |
| 1081 | { |
| 1082 | uint8_t presentOpState; |
| 1083 | uint8_t previousOpState; |
| 1084 | size_t sensorDataLength = PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH; |
| 1085 | auto rc = decode_sensor_op_data(NULL, sensorDataLength, &presentOpState, |
| 1086 | &previousOpState); |
| 1087 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 1088 | |
| 1089 | std::array<uint8_t, PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH> |
| 1090 | sensorData{}; |
| 1091 | rc = decode_sensor_op_data(reinterpret_cast<uint8_t*>(sensorData.data()), |
| 1092 | sensorDataLength + 1, &presentOpState, |
| 1093 | &previousOpState); |
| 1094 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 1095 | |
| 1096 | rc = decode_sensor_op_data(reinterpret_cast<uint8_t*>(sensorData.data()), |
| 1097 | sensorDataLength, nullptr, &previousOpState); |
| 1098 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 1099 | } |
| 1100 | |
| 1101 | TEST(PlatformEventMessage, testGoodSensorStateEventDataDecodeRequest) |
| 1102 | { |
| 1103 | std::array<uint8_t, PLDM_SENSOR_EVENT_STATE_SENSOR_STATE_DATA_LENGTH> |
| 1104 | eventDataArr{}; |
| 1105 | |
| 1106 | struct pldm_sensor_event_state_sensor_state* sensorData = |
| 1107 | (struct pldm_sensor_event_state_sensor_state*)eventDataArr.data(); |
| 1108 | uint8_t sensorOffset = 0x02; |
| 1109 | uint8_t eventState = PLDM_SENSOR_SHUTTINGDOWN; |
| 1110 | uint8_t previousEventState = PLDM_SENSOR_INTEST; |
| 1111 | sensorData->sensor_offset = sensorOffset; |
| 1112 | sensorData->event_state = eventState; |
| 1113 | sensorData->previous_event_state = previousEventState; |
| 1114 | |
| 1115 | uint8_t retSensorOffset; |
| 1116 | uint8_t retEventState; |
| 1117 | uint8_t retPreviousState; |
| 1118 | auto rc = decode_state_sensor_data(reinterpret_cast<uint8_t*>(sensorData), |
| 1119 | eventDataArr.size(), &retSensorOffset, |
| 1120 | &retEventState, &retPreviousState); |
| 1121 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 1122 | EXPECT_EQ(retSensorOffset, sensorOffset); |
| 1123 | EXPECT_EQ(retEventState, eventState); |
| 1124 | EXPECT_EQ(retPreviousState, previousEventState); |
| 1125 | } |
| 1126 | |
| 1127 | TEST(PlatformEventMessage, testBadStateSensorEventDataDecodeRequest) |
| 1128 | { |
| 1129 | uint8_t sensorOffset; |
| 1130 | uint8_t eventState; |
| 1131 | uint8_t previousEventState; |
| 1132 | size_t sensorDataLength = PLDM_SENSOR_EVENT_STATE_SENSOR_STATE_DATA_LENGTH; |
| 1133 | auto rc = decode_state_sensor_data(NULL, sensorDataLength, &sensorOffset, |
| 1134 | &eventState, &previousEventState); |
| 1135 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 1136 | |
| 1137 | std::array<uint8_t, PLDM_SENSOR_EVENT_STATE_SENSOR_STATE_DATA_LENGTH> |
| 1138 | sensorData{}; |
| 1139 | rc = decode_state_sensor_data(reinterpret_cast<uint8_t*>(sensorData.data()), |
| 1140 | sensorDataLength - 1, &sensorOffset, |
| 1141 | &eventState, &previousEventState); |
| 1142 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 1143 | |
| 1144 | rc = decode_state_sensor_data(reinterpret_cast<uint8_t*>(sensorData.data()), |
| 1145 | sensorDataLength, &sensorOffset, nullptr, |
| 1146 | &previousEventState); |
| 1147 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 1148 | } |
| 1149 | |
| 1150 | TEST(PlatformEventMessage, testGoodNumericSensorEventDataDecodeRequest) |
| 1151 | { |
| 1152 | std::array<uint8_t, PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH> |
| 1153 | eventDataArr{}; |
| 1154 | struct pldm_sensor_event_numeric_sensor_state* sensorData = |
| 1155 | (struct pldm_sensor_event_numeric_sensor_state*)eventDataArr.data(); |
| 1156 | |
| 1157 | size_t sensorDataLength = |
| 1158 | PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_32BIT_DATA_LENGTH; |
| 1159 | uint8_t eventState = PLDM_SENSOR_SHUTTINGDOWN; |
| 1160 | uint8_t previousEventState = PLDM_SENSOR_INTEST; |
| 1161 | uint8_t sensorDataSize = PLDM_SENSOR_DATA_SIZE_UINT32; |
| 1162 | uint32_t presentReading = 305441741; |
| 1163 | sensorData->event_state = eventState; |
| 1164 | sensorData->previous_event_state = previousEventState; |
| 1165 | sensorData->sensor_data_size = sensorDataSize; |
| 1166 | sensorData->present_reading[3] = |
| 1167 | static_cast<uint8_t>(htole32(presentReading) & (0x000000ff)); |
| 1168 | sensorData->present_reading[2] = |
| 1169 | static_cast<uint8_t>((htole32(presentReading) & (0x0000ff00)) >> 8); |
| 1170 | sensorData->present_reading[1] = |
| 1171 | static_cast<uint8_t>((htole32(presentReading) & (0x00ff0000)) >> 16); |
| 1172 | sensorData->present_reading[0] = |
| 1173 | static_cast<uint8_t>((htole32(presentReading) & (0xff000000)) >> 24); |
| 1174 | |
| 1175 | uint8_t retEventState; |
| 1176 | uint8_t retPreviousEventState; |
| 1177 | uint8_t retSensorDataSize; |
| 1178 | uint32_t retPresentReading; |
| 1179 | |
| 1180 | auto rc = decode_numeric_sensor_data( |
| 1181 | reinterpret_cast<uint8_t*>(sensorData), sensorDataLength, |
| 1182 | &retEventState, &retPreviousEventState, &retSensorDataSize, |
| 1183 | &retPresentReading); |
| 1184 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 1185 | EXPECT_EQ(retEventState, eventState); |
| 1186 | EXPECT_EQ(retPreviousEventState, previousEventState); |
| 1187 | EXPECT_EQ(retSensorDataSize, sensorDataSize); |
| 1188 | EXPECT_EQ(retPresentReading, presentReading); |
| 1189 | |
| 1190 | int16_t presentReadingNew = -31432; |
| 1191 | sensorData->present_reading[1] = |
| 1192 | static_cast<uint8_t>(htole16(presentReadingNew) & (0x000000ff)); |
| 1193 | sensorData->present_reading[0] = |
| 1194 | static_cast<uint8_t>((htole16(presentReadingNew) & (0x0000ff00)) >> 8); |
| 1195 | sensorDataSize = PLDM_SENSOR_DATA_SIZE_SINT16; |
| 1196 | sensorData->sensor_data_size = sensorDataSize; |
| 1197 | sensorDataLength = PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_16BIT_DATA_LENGTH; |
| 1198 | |
| 1199 | rc = decode_numeric_sensor_data(reinterpret_cast<uint8_t*>(sensorData), |
| 1200 | sensorDataLength, &retEventState, |
| 1201 | &retPreviousEventState, &retSensorDataSize, |
| 1202 | &retPresentReading); |
| 1203 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 1204 | EXPECT_EQ(retEventState, eventState); |
| 1205 | EXPECT_EQ(retPreviousEventState, previousEventState); |
| 1206 | EXPECT_EQ(retSensorDataSize, sensorDataSize); |
| 1207 | EXPECT_EQ(static_cast<int16_t>(retPresentReading), presentReadingNew); |
| 1208 | } |
| 1209 | |
| 1210 | TEST(PlatformEventMessage, testBadNumericSensorEventDataDecodeRequest) |
| 1211 | { |
| 1212 | uint8_t eventState; |
| 1213 | uint8_t previousEventState; |
| 1214 | uint8_t sensorDataSize; |
| 1215 | uint32_t presentReading; |
| 1216 | size_t sensorDataLength = |
| 1217 | PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH; |
| 1218 | auto rc = decode_numeric_sensor_data(NULL, sensorDataLength, &eventState, |
| 1219 | &previousEventState, &sensorDataSize, |
| 1220 | &presentReading); |
| 1221 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 1222 | |
| 1223 | std::array<uint8_t, PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH> |
| 1224 | sensorData{}; |
| 1225 | rc = decode_numeric_sensor_data( |
| 1226 | reinterpret_cast<uint8_t*>(sensorData.data()), sensorDataLength - 1, |
| 1227 | &eventState, &previousEventState, &sensorDataSize, &presentReading); |
| 1228 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 1229 | |
| 1230 | struct pldm_sensor_event_numeric_sensor_state* numericSensorData = |
| 1231 | (struct pldm_sensor_event_numeric_sensor_state*)sensorData.data(); |
| 1232 | numericSensorData->sensor_data_size = PLDM_SENSOR_DATA_SIZE_UINT8; |
| 1233 | rc = decode_numeric_sensor_data( |
| 1234 | reinterpret_cast<uint8_t*>(sensorData.data()), sensorDataLength, |
| 1235 | &eventState, &previousEventState, &sensorDataSize, &presentReading); |
| 1236 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 1237 | |
| 1238 | numericSensorData->sensor_data_size = PLDM_SENSOR_DATA_SIZE_UINT16; |
| 1239 | rc = decode_numeric_sensor_data( |
| 1240 | reinterpret_cast<uint8_t*>(sensorData.data()), sensorDataLength, |
| 1241 | &eventState, &previousEventState, &sensorDataSize, &presentReading); |
| 1242 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 1243 | |
| 1244 | numericSensorData->sensor_data_size = PLDM_SENSOR_DATA_SIZE_UINT32; |
| 1245 | rc = decode_numeric_sensor_data( |
| 1246 | reinterpret_cast<uint8_t*>(sensorData.data()), sensorDataLength - 1, |
| 1247 | &eventState, &previousEventState, &sensorDataSize, &presentReading); |
| 1248 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 1249 | } |
Jolie Ku | 6787f17 | 2020-03-19 11:15:53 +0800 | [diff] [blame] | 1250 | |
| 1251 | TEST(GetNumericEffecterValue, testGoodEncodeRequest) |
| 1252 | { |
| 1253 | std::vector<uint8_t> requestMsg(hdrSize + |
| 1254 | PLDM_GET_NUMERIC_EFFECTER_VALUE_REQ_BYTES); |
| 1255 | |
| 1256 | uint16_t effecter_id = 0xAB01; |
| 1257 | |
| 1258 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 1259 | |
| 1260 | auto rc = encode_get_numeric_effecter_value_req(0, effecter_id, request); |
| 1261 | |
| 1262 | struct pldm_get_numeric_effecter_value_req* req = |
| 1263 | reinterpret_cast<struct pldm_get_numeric_effecter_value_req*>( |
| 1264 | request->payload); |
| 1265 | |
| 1266 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 1267 | EXPECT_EQ(effecter_id, le16toh(req->effecter_id)); |
| 1268 | } |
| 1269 | |
| 1270 | TEST(GetNumericEffecterValue, testBadEncodeRequest) |
| 1271 | { |
| 1272 | std::vector<uint8_t> requestMsg( |
| 1273 | hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES); |
| 1274 | |
| 1275 | auto rc = encode_get_numeric_effecter_value_req(0, 0, nullptr); |
| 1276 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 1277 | } |
| 1278 | |
| 1279 | TEST(GetNumericEffecterValue, testGoodDecodeRequest) |
| 1280 | { |
| 1281 | std::array<uint8_t, hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_REQ_BYTES> |
| 1282 | requestMsg{}; |
| 1283 | |
| 1284 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 1285 | struct pldm_get_numeric_effecter_value_req* req = |
| 1286 | reinterpret_cast<struct pldm_get_numeric_effecter_value_req*>( |
| 1287 | request->payload); |
| 1288 | |
| 1289 | uint16_t effecter_id = 0x12AB; |
| 1290 | req->effecter_id = htole16(effecter_id); |
| 1291 | |
| 1292 | uint16_t reteffecter_id; |
| 1293 | |
| 1294 | auto rc = decode_get_numeric_effecter_value_req( |
| 1295 | request, requestMsg.size() - hdrSize, &reteffecter_id); |
| 1296 | |
| 1297 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 1298 | EXPECT_EQ(effecter_id, reteffecter_id); |
| 1299 | } |
| 1300 | |
| 1301 | TEST(GetNumericEffecterValue, testBadDecodeRequest) |
| 1302 | { |
| 1303 | std::array<uint8_t, hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_REQ_BYTES> |
| 1304 | requestMsg{}; |
| 1305 | |
| 1306 | auto rc = decode_get_numeric_effecter_value_req( |
| 1307 | nullptr, requestMsg.size() - hdrSize, nullptr); |
| 1308 | |
| 1309 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 1310 | |
| 1311 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 1312 | struct pldm_set_numeric_effecter_value_req* req = |
| 1313 | reinterpret_cast<struct pldm_set_numeric_effecter_value_req*>( |
| 1314 | request->payload); |
| 1315 | |
| 1316 | uint16_t effecter_id = 0x1A; |
| 1317 | req->effecter_id = htole16(effecter_id); |
| 1318 | uint16_t reteffecter_id; |
| 1319 | |
| 1320 | rc = decode_get_numeric_effecter_value_req( |
| 1321 | request, requestMsg.size() - hdrSize - 1, &reteffecter_id); |
| 1322 | |
| 1323 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 1324 | } |
| 1325 | |
| 1326 | TEST(GetNumericEffecterValue, testGoodEncodeResponse) |
| 1327 | { |
| 1328 | uint8_t completionCode = 0; |
| 1329 | uint8_t effecter_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT32; |
| 1330 | uint8_t effecter_operState = EFFECTER_OPER_STATE_ENABLED_NOUPDATEPENDING; |
| 1331 | uint32_t pendingValue = 0x12345678; |
Jolie Ku | 3cdbcfb | 2020-04-17 10:30:59 +0800 | [diff] [blame] | 1332 | uint32_t presentValue = 0xABCDEF11; |
Jolie Ku | 6787f17 | 2020-03-19 11:15:53 +0800 | [diff] [blame] | 1333 | |
| 1334 | std::array<uint8_t, |
| 1335 | hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 6> |
| 1336 | responseMsg{}; |
| 1337 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 1338 | |
Jolie Ku | 6787f17 | 2020-03-19 11:15:53 +0800 | [diff] [blame] | 1339 | auto rc = encode_get_numeric_effecter_value_resp( |
| 1340 | 0, completionCode, effecter_dataSize, effecter_operState, |
Jolie Ku | 3cdbcfb | 2020-04-17 10:30:59 +0800 | [diff] [blame] | 1341 | reinterpret_cast<uint8_t*>(&pendingValue), |
| 1342 | reinterpret_cast<uint8_t*>(&presentValue), response, |
Jolie Ku | 6787f17 | 2020-03-19 11:15:53 +0800 | [diff] [blame] | 1343 | responseMsg.size() - hdrSize); |
| 1344 | |
| 1345 | struct pldm_get_numeric_effecter_value_resp* resp = |
| 1346 | reinterpret_cast<struct pldm_get_numeric_effecter_value_resp*>( |
| 1347 | response->payload); |
| 1348 | |
Jolie Ku | 3cdbcfb | 2020-04-17 10:30:59 +0800 | [diff] [blame] | 1349 | uint32_t* val_pending = (uint32_t*)(&resp->pending_and_present_values[0]); |
| 1350 | *val_pending = le32toh(*val_pending); |
| 1351 | uint32_t* val_present = (uint32_t*)(&resp->pending_and_present_values[4]); |
| 1352 | *val_present = le32toh(*val_present); |
| 1353 | |
Jolie Ku | 6787f17 | 2020-03-19 11:15:53 +0800 | [diff] [blame] | 1354 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 1355 | EXPECT_EQ(effecter_dataSize, resp->effecter_data_size); |
| 1356 | EXPECT_EQ(effecter_operState, resp->effecter_oper_state); |
Jolie Ku | 3cdbcfb | 2020-04-17 10:30:59 +0800 | [diff] [blame] | 1357 | EXPECT_EQ(pendingValue, *val_pending); |
| 1358 | EXPECT_EQ(presentValue, *val_present); |
Jolie Ku | 6787f17 | 2020-03-19 11:15:53 +0800 | [diff] [blame] | 1359 | } |
| 1360 | |
| 1361 | TEST(GetNumericEffecterValue, testBadEncodeResponse) |
| 1362 | { |
| 1363 | std::array<uint8_t, |
| 1364 | hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 2> |
| 1365 | responseMsg{}; |
| 1366 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 1367 | |
| 1368 | uint8_t pendingValue = 0x01; |
| 1369 | uint8_t presentValue = 0x02; |
| 1370 | |
| 1371 | auto rc = encode_get_numeric_effecter_value_resp( |
| 1372 | 0, PLDM_SUCCESS, 0, 0, nullptr, nullptr, nullptr, |
| 1373 | responseMsg.size() - hdrSize); |
| 1374 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 1375 | |
| 1376 | rc = encode_get_numeric_effecter_value_resp( |
| 1377 | 0, PLDM_SUCCESS, 6, 9, reinterpret_cast<uint8_t*>(&pendingValue), |
| 1378 | reinterpret_cast<uint8_t*>(&presentValue), response, |
| 1379 | responseMsg.size() - hdrSize); |
| 1380 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 1381 | |
| 1382 | uint8_t effecter_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT8; |
| 1383 | uint8_t effecter_operState = EFFECTER_OPER_STATE_FAILED; |
| 1384 | |
| 1385 | rc = encode_get_numeric_effecter_value_resp( |
| 1386 | 0, PLDM_SUCCESS, effecter_dataSize, effecter_operState, |
| 1387 | reinterpret_cast<uint8_t*>(&pendingValue), |
| 1388 | reinterpret_cast<uint8_t*>(&presentValue), response, |
| 1389 | responseMsg.size() - hdrSize); |
| 1390 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 1391 | } |
| 1392 | |
| 1393 | TEST(GetNumericEffecterValue, testGoodDecodeResponse) |
| 1394 | { |
| 1395 | std::array<uint8_t, |
| 1396 | hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 2> |
| 1397 | responseMsg{}; |
| 1398 | |
| 1399 | uint8_t completionCode = 0; |
| 1400 | uint8_t effecter_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT16; |
| 1401 | uint8_t effecter_operState = EFFECTER_OPER_STATE_ENABLED_NOUPDATEPENDING; |
| 1402 | uint16_t pendingValue = 0x4321; |
| 1403 | uint16_t presentValue = 0xDCBA; |
| 1404 | |
| 1405 | uint8_t retcompletionCode; |
| 1406 | uint8_t reteffecter_dataSize; |
| 1407 | uint8_t reteffecter_operState; |
| 1408 | uint8_t retpendingValue[2]; |
| 1409 | uint8_t retpresentValue[2]; |
| 1410 | |
| 1411 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 1412 | struct pldm_get_numeric_effecter_value_resp* resp = |
| 1413 | reinterpret_cast<struct pldm_get_numeric_effecter_value_resp*>( |
| 1414 | response->payload); |
| 1415 | |
| 1416 | resp->completion_code = completionCode; |
| 1417 | resp->effecter_data_size = effecter_dataSize; |
| 1418 | resp->effecter_oper_state = effecter_operState; |
| 1419 | |
| 1420 | uint16_t pendingValue_le = htole16(pendingValue); |
| 1421 | memcpy(resp->pending_and_present_values, &pendingValue_le, |
| 1422 | sizeof(pendingValue_le)); |
| 1423 | uint16_t presentValue_le = htole16(presentValue); |
| 1424 | memcpy(&resp->pending_and_present_values[2], &presentValue_le, |
| 1425 | sizeof(presentValue_le)); |
| 1426 | |
| 1427 | auto rc = decode_get_numeric_effecter_value_resp( |
| 1428 | response, responseMsg.size() - hdrSize, &retcompletionCode, |
| 1429 | &reteffecter_dataSize, &reteffecter_operState, retpendingValue, |
| 1430 | retpresentValue); |
| 1431 | |
Jolie Ku | 6787f17 | 2020-03-19 11:15:53 +0800 | [diff] [blame] | 1432 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 1433 | EXPECT_EQ(completionCode, retcompletionCode); |
| 1434 | EXPECT_EQ(effecter_dataSize, reteffecter_dataSize); |
| 1435 | EXPECT_EQ(effecter_operState, reteffecter_operState); |
Jolie Ku | 3cdbcfb | 2020-04-17 10:30:59 +0800 | [diff] [blame] | 1436 | EXPECT_EQ(pendingValue, *(reinterpret_cast<uint16_t*>(retpendingValue))); |
| 1437 | EXPECT_EQ(presentValue, *(reinterpret_cast<uint16_t*>(retpresentValue))); |
Jolie Ku | 6787f17 | 2020-03-19 11:15:53 +0800 | [diff] [blame] | 1438 | } |
| 1439 | |
| 1440 | TEST(GetNumericEffecterValue, testBadDecodeResponse) |
| 1441 | { |
| 1442 | std::array<uint8_t, |
| 1443 | hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 6> |
| 1444 | responseMsg{}; |
| 1445 | |
| 1446 | auto rc = decode_get_numeric_effecter_value_resp( |
| 1447 | nullptr, responseMsg.size() - hdrSize, nullptr, nullptr, nullptr, |
| 1448 | nullptr, nullptr); |
| 1449 | |
| 1450 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 1451 | |
| 1452 | uint8_t completionCode = 0; |
| 1453 | uint8_t effecter_dataSize = PLDM_EFFECTER_DATA_SIZE_SINT16; |
| 1454 | uint8_t effecter_operState = EFFECTER_OPER_STATE_DISABLED; |
| 1455 | uint16_t pendingValue = 0x5678; |
| 1456 | uint16_t presentValue = 0xCDEF; |
| 1457 | |
| 1458 | uint8_t retcompletionCode; |
| 1459 | uint8_t reteffecter_dataSize; |
| 1460 | uint8_t reteffecter_operState; |
| 1461 | uint8_t retpendingValue[2]; |
| 1462 | uint8_t retpresentValue[2]; |
| 1463 | |
| 1464 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 1465 | struct pldm_get_numeric_effecter_value_resp* resp = |
| 1466 | reinterpret_cast<struct pldm_get_numeric_effecter_value_resp*>( |
| 1467 | response->payload); |
| 1468 | |
| 1469 | resp->completion_code = completionCode; |
| 1470 | resp->effecter_data_size = effecter_dataSize; |
| 1471 | resp->effecter_oper_state = effecter_operState; |
| 1472 | |
| 1473 | uint16_t pendingValue_le = htole16(pendingValue); |
| 1474 | memcpy(resp->pending_and_present_values, &pendingValue_le, |
| 1475 | sizeof(pendingValue_le)); |
| 1476 | uint16_t presentValue_le = htole16(presentValue); |
| 1477 | memcpy(&resp->pending_and_present_values[2], &presentValue_le, |
| 1478 | sizeof(presentValue_le)); |
| 1479 | |
| 1480 | rc = decode_get_numeric_effecter_value_resp( |
| 1481 | response, responseMsg.size() - hdrSize, &retcompletionCode, |
| 1482 | &reteffecter_dataSize, &reteffecter_operState, retpendingValue, |
| 1483 | retpresentValue); |
| 1484 | |
| 1485 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 1486 | } |
Zahed Hossain | 9be087c | 2020-04-02 02:26:41 -0500 | [diff] [blame] | 1487 | |
| 1488 | TEST(PldmPDRRepositoryChgEventEvent, testGoodDecodeRequest) |
| 1489 | { |
| 1490 | const uint8_t eventDataFormat = FORMAT_IS_PDR_HANDLES; |
| 1491 | const uint8_t numberOfChangeRecords = 2; |
| 1492 | uint8_t eventDataOperation1 = PLDM_RECORDS_DELETED; |
| 1493 | const uint8_t numberOfChangeEntries1 = 2; |
| 1494 | std::array<uint32_t, numberOfChangeEntries1> changeRecordArr1{ |
| 1495 | {0x00000000, 0x12345678}}; |
| 1496 | uint8_t eventDataOperation2 = PLDM_RECORDS_ADDED; |
| 1497 | const uint8_t numberOfChangeEntries2 = 5; |
| 1498 | std::array<uint32_t, numberOfChangeEntries2> changeRecordArr2{ |
| 1499 | {0x01234567, 0x11223344, 0x45678901, 0x21222324, 0x98765432}}; |
| 1500 | std::array<uint8_t, PLDM_PDR_REPOSITORY_CHG_EVENT_MIN_LENGTH + |
| 1501 | PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH * |
| 1502 | numberOfChangeRecords + |
| 1503 | (numberOfChangeEntries1 + numberOfChangeEntries2) * |
| 1504 | sizeof(uint32_t)> |
| 1505 | eventDataArr{}; |
| 1506 | |
| 1507 | struct pldm_pdr_repository_chg_event_data* eventData = |
| 1508 | reinterpret_cast<struct pldm_pdr_repository_chg_event_data*>( |
| 1509 | eventDataArr.data()); |
| 1510 | eventData->event_data_format = eventDataFormat; |
| 1511 | eventData->number_of_change_records = numberOfChangeRecords; |
| 1512 | struct pldm_pdr_repository_change_record_data* changeRecord1 = |
| 1513 | reinterpret_cast<struct pldm_pdr_repository_change_record_data*>( |
| 1514 | eventData->change_records); |
| 1515 | changeRecord1->event_data_operation = eventDataOperation1; |
| 1516 | changeRecord1->number_of_change_entries = numberOfChangeEntries1; |
| 1517 | memcpy(changeRecord1->change_entry, &changeRecordArr1[0], |
| 1518 | changeRecordArr1.size() * sizeof(uint32_t)); |
| 1519 | struct pldm_pdr_repository_change_record_data* changeRecord2 = |
| 1520 | reinterpret_cast<struct pldm_pdr_repository_change_record_data*>( |
| 1521 | eventData->change_records + |
| 1522 | PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH + |
| 1523 | (changeRecordArr1.size() * sizeof(uint32_t))); |
| 1524 | changeRecord2->event_data_operation = eventDataOperation2; |
| 1525 | changeRecord2->number_of_change_entries = numberOfChangeEntries2; |
| 1526 | memcpy(changeRecord2->change_entry, &changeRecordArr2[0], |
| 1527 | changeRecordArr2.size() * sizeof(uint32_t)); |
| 1528 | |
| 1529 | uint8_t retEventDataFormat{}; |
| 1530 | uint8_t retNumberOfChangeRecords{}; |
| 1531 | size_t retChangeRecordDataOffset{0}; |
| 1532 | auto rc = decode_pldm_pdr_repository_chg_event_data( |
| 1533 | reinterpret_cast<const uint8_t*>(eventData), eventDataArr.size(), |
| 1534 | &retEventDataFormat, &retNumberOfChangeRecords, |
| 1535 | &retChangeRecordDataOffset); |
| 1536 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 1537 | EXPECT_EQ(retEventDataFormat, FORMAT_IS_PDR_HANDLES); |
| 1538 | EXPECT_EQ(retNumberOfChangeRecords, numberOfChangeRecords); |
| 1539 | |
| 1540 | const uint8_t* changeRecordData = |
| 1541 | reinterpret_cast<const uint8_t*>(changeRecord1); |
| 1542 | size_t changeRecordDataSize = |
| 1543 | eventDataArr.size() - PLDM_PDR_REPOSITORY_CHG_EVENT_MIN_LENGTH; |
| 1544 | uint8_t retEventDataOperation; |
| 1545 | uint8_t retNumberOfChangeEntries; |
| 1546 | size_t retChangeEntryDataOffset; |
| 1547 | |
| 1548 | rc = decode_pldm_pdr_repository_change_record_data( |
| 1549 | reinterpret_cast<const uint8_t*>(changeRecordData), |
| 1550 | changeRecordDataSize, &retEventDataOperation, &retNumberOfChangeEntries, |
| 1551 | &retChangeEntryDataOffset); |
| 1552 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 1553 | EXPECT_EQ(retEventDataOperation, eventDataOperation1); |
| 1554 | EXPECT_EQ(retNumberOfChangeEntries, numberOfChangeEntries1); |
| 1555 | changeRecordData += retChangeEntryDataOffset; |
| 1556 | EXPECT_EQ(0, memcmp(changeRecordData, &changeRecordArr1[0], |
| 1557 | sizeof(uint32_t) * retNumberOfChangeEntries)); |
| 1558 | |
| 1559 | changeRecordData += sizeof(uint32_t) * retNumberOfChangeEntries; |
| 1560 | changeRecordDataSize -= sizeof(uint32_t) * retNumberOfChangeEntries - |
| 1561 | PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH; |
| 1562 | rc = decode_pldm_pdr_repository_change_record_data( |
| 1563 | reinterpret_cast<const uint8_t*>(changeRecordData), |
| 1564 | changeRecordDataSize, &retEventDataOperation, &retNumberOfChangeEntries, |
| 1565 | &retChangeEntryDataOffset); |
| 1566 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 1567 | EXPECT_EQ(retEventDataOperation, eventDataOperation2); |
| 1568 | EXPECT_EQ(retNumberOfChangeEntries, numberOfChangeEntries2); |
| 1569 | changeRecordData += retChangeEntryDataOffset; |
| 1570 | EXPECT_EQ(0, memcmp(changeRecordData, &changeRecordArr2[0], |
| 1571 | sizeof(uint32_t) * retNumberOfChangeEntries)); |
| 1572 | } |
| 1573 | |
| 1574 | TEST(PldmPDRRepositoryChgEventEvent, testBadDecodeRequest) |
| 1575 | { |
| 1576 | uint8_t eventDataFormat{}; |
| 1577 | uint8_t numberOfChangeRecords{}; |
| 1578 | size_t changeRecordDataOffset{}; |
| 1579 | auto rc = decode_pldm_pdr_repository_chg_event_data( |
| 1580 | NULL, 0, &eventDataFormat, &numberOfChangeRecords, |
| 1581 | &changeRecordDataOffset); |
| 1582 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 1583 | |
| 1584 | std::array<uint8_t, 2> eventData{}; |
| 1585 | rc = decode_pldm_pdr_repository_chg_event_data( |
| 1586 | reinterpret_cast<const uint8_t*>(eventData.data()), 0, &eventDataFormat, |
| 1587 | &numberOfChangeRecords, &changeRecordDataOffset); |
| 1588 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 1589 | |
| 1590 | uint8_t eventDataOperation{}; |
| 1591 | uint8_t numberOfChangeEntries{}; |
| 1592 | size_t changeEntryDataOffset{}; |
| 1593 | rc = decode_pldm_pdr_repository_change_record_data( |
| 1594 | NULL, 0, &eventDataOperation, &numberOfChangeEntries, |
| 1595 | &changeEntryDataOffset); |
| 1596 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 1597 | |
| 1598 | std::array<uint8_t, 2> changeRecord{}; |
| 1599 | rc = decode_pldm_pdr_repository_change_record_data( |
| 1600 | reinterpret_cast<const uint8_t*>(changeRecord.data()), 0, |
| 1601 | &eventDataOperation, &numberOfChangeEntries, &changeEntryDataOffset); |
| 1602 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 1603 | } |
Jolie Ku | f798c8f | 2020-04-14 11:18:06 +0800 | [diff] [blame] | 1604 | |
| 1605 | TEST(GetSensorReading, testGoodEncodeRequest) |
| 1606 | { |
| 1607 | std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_REQ_BYTES> |
| 1608 | requestMsg{}; |
| 1609 | |
| 1610 | uint16_t sensorId = 0x1234; |
| 1611 | bool8_t rearmEventState = 0x01; |
| 1612 | |
| 1613 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 1614 | auto rc = |
| 1615 | encode_get_sensor_reading_req(0, sensorId, rearmEventState, request); |
| 1616 | |
| 1617 | struct pldm_get_sensor_reading_req* req = |
| 1618 | reinterpret_cast<struct pldm_get_sensor_reading_req*>(request->payload); |
| 1619 | |
| 1620 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 1621 | EXPECT_EQ(sensorId, le16toh(req->sensor_id)); |
| 1622 | EXPECT_EQ(rearmEventState, req->rearm_event_state); |
| 1623 | } |
| 1624 | |
| 1625 | TEST(GetSensorReading, testBadEncodeRequest) |
| 1626 | { |
| 1627 | auto rc = encode_get_sensor_reading_req(0, 0, 0, nullptr); |
| 1628 | |
| 1629 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 1630 | } |
| 1631 | |
| 1632 | TEST(GetSensorReading, testGoodDecodeRequest) |
| 1633 | { |
| 1634 | std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_REQ_BYTES> |
| 1635 | requestMsg{}; |
| 1636 | |
| 1637 | uint16_t sensorId = 0xABCD; |
| 1638 | bool8_t rearmEventState = 0xA; |
| 1639 | |
| 1640 | uint16_t retsensorId; |
| 1641 | bool8_t retrearmEventState; |
| 1642 | |
| 1643 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 1644 | |
| 1645 | struct pldm_get_sensor_reading_req* req = |
| 1646 | reinterpret_cast<struct pldm_get_sensor_reading_req*>(request->payload); |
| 1647 | |
| 1648 | req->sensor_id = htole16(sensorId); |
| 1649 | req->rearm_event_state = rearmEventState; |
| 1650 | |
| 1651 | auto rc = |
| 1652 | decode_get_sensor_reading_req(request, requestMsg.size() - hdrSize, |
| 1653 | &retsensorId, &retrearmEventState); |
| 1654 | |
| 1655 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 1656 | EXPECT_EQ(sensorId, retsensorId); |
| 1657 | EXPECT_EQ(rearmEventState, retrearmEventState); |
| 1658 | } |
| 1659 | |
| 1660 | TEST(GetSensorReading, testBadDecodeRequest) |
| 1661 | { |
| 1662 | std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_REQ_BYTES> |
| 1663 | requestMsg{}; |
| 1664 | |
| 1665 | auto rc = decode_get_sensor_reading_req( |
| 1666 | nullptr, requestMsg.size() - hdrSize, nullptr, nullptr); |
| 1667 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 1668 | |
| 1669 | uint16_t sensorId = 0xABCD; |
| 1670 | bool8_t rearmEventState = 0xA; |
| 1671 | |
| 1672 | uint16_t retsensorId; |
| 1673 | bool8_t retrearmEventState; |
| 1674 | |
| 1675 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 1676 | |
| 1677 | struct pldm_get_sensor_reading_req* req = |
| 1678 | reinterpret_cast<struct pldm_get_sensor_reading_req*>(request->payload); |
| 1679 | |
| 1680 | req->sensor_id = htole16(sensorId); |
| 1681 | req->rearm_event_state = rearmEventState; |
| 1682 | |
| 1683 | rc = decode_get_sensor_reading_req(request, requestMsg.size() - hdrSize - 1, |
| 1684 | &retsensorId, &retrearmEventState); |
| 1685 | |
| 1686 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 1687 | } |
| 1688 | |
| 1689 | TEST(GetSensorReading, testGoodEncodeResponse) |
| 1690 | { |
| 1691 | std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_MIN_RESP_BYTES> |
| 1692 | responseMsg{}; |
| 1693 | |
| 1694 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 1695 | |
| 1696 | uint8_t completionCode = 0; |
| 1697 | uint8_t sensor_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT8; |
| 1698 | uint8_t sensor_operationalState = PLDM_SENSOR_ENABLED; |
| 1699 | uint8_t sensor_event_messageEnable = PLDM_NO_EVENT_GENERATION; |
George Liu | 9d15502 | 2020-07-10 15:06:35 +0800 | [diff] [blame] | 1700 | uint8_t presentState = PLDM_SENSOR_NORMAL; |
| 1701 | uint8_t previousState = PLDM_SENSOR_WARNING; |
| 1702 | uint8_t eventState = PLDM_SENSOR_UPPERWARNING; |
Jolie Ku | f798c8f | 2020-04-14 11:18:06 +0800 | [diff] [blame] | 1703 | uint8_t presentReading = 0x21; |
| 1704 | |
| 1705 | auto rc = encode_get_sensor_reading_resp( |
| 1706 | 0, completionCode, sensor_dataSize, sensor_operationalState, |
| 1707 | sensor_event_messageEnable, presentState, previousState, eventState, |
| 1708 | reinterpret_cast<uint8_t*>(&presentReading), response, |
| 1709 | responseMsg.size() - hdrSize); |
| 1710 | |
| 1711 | struct pldm_get_sensor_reading_resp* resp = |
| 1712 | reinterpret_cast<struct pldm_get_sensor_reading_resp*>( |
| 1713 | response->payload); |
| 1714 | |
| 1715 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 1716 | EXPECT_EQ(completionCode, resp->completion_code); |
| 1717 | EXPECT_EQ(sensor_dataSize, resp->sensor_data_size); |
| 1718 | EXPECT_EQ(sensor_operationalState, resp->sensor_operational_state); |
| 1719 | EXPECT_EQ(sensor_event_messageEnable, resp->sensor_event_message_enable); |
| 1720 | EXPECT_EQ(presentState, resp->present_state); |
| 1721 | EXPECT_EQ(previousState, resp->previous_state); |
| 1722 | EXPECT_EQ(eventState, resp->event_state); |
| 1723 | EXPECT_EQ(presentReading, |
| 1724 | *(reinterpret_cast<uint8_t*>(&resp->present_reading[0]))); |
| 1725 | } |
| 1726 | |
| 1727 | TEST(GetSensorReading, testBadEncodeResponse) |
| 1728 | { |
| 1729 | std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 3> |
| 1730 | responseMsg{}; |
| 1731 | |
| 1732 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 1733 | |
| 1734 | uint8_t presentReading = 0x1; |
| 1735 | |
| 1736 | auto rc = encode_get_sensor_reading_resp(0, PLDM_SUCCESS, 0, 0, 0, 0, 0, 0, |
| 1737 | nullptr, nullptr, |
| 1738 | responseMsg.size() - hdrSize); |
| 1739 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 1740 | |
| 1741 | rc = encode_get_sensor_reading_resp( |
| 1742 | 0, PLDM_SUCCESS, 6, 1, 1, 1, 1, 1, |
| 1743 | reinterpret_cast<uint8_t*>(&presentReading), response, |
| 1744 | responseMsg.size() - hdrSize); |
| 1745 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 1746 | |
| 1747 | uint8_t sensor_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT8; |
| 1748 | |
| 1749 | rc = encode_get_sensor_reading_resp( |
| 1750 | 0, PLDM_SUCCESS, sensor_dataSize, 1, 1, 1, 1, 1, |
| 1751 | reinterpret_cast<uint8_t*>(&presentReading), response, |
| 1752 | responseMsg.size() - hdrSize); |
| 1753 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 1754 | } |
| 1755 | |
| 1756 | TEST(GetSensorReading, testGoodDecodeResponse) |
| 1757 | { |
| 1758 | std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 3> |
| 1759 | responseMsg{}; |
| 1760 | |
| 1761 | uint8_t completionCode = 0; |
| 1762 | uint8_t sensor_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT32; |
| 1763 | uint8_t sensor_operationalState = PLDM_SENSOR_STATUSUNKOWN; |
| 1764 | uint8_t sensor_event_messageEnable = PLDM_EVENTS_ENABLED; |
George Liu | 9d15502 | 2020-07-10 15:06:35 +0800 | [diff] [blame] | 1765 | uint8_t presentState = PLDM_SENSOR_CRITICAL; |
| 1766 | uint8_t previousState = PLDM_SENSOR_UPPERCRITICAL; |
| 1767 | uint8_t eventState = PLDM_SENSOR_WARNING; |
Jolie Ku | f798c8f | 2020-04-14 11:18:06 +0800 | [diff] [blame] | 1768 | uint32_t presentReading = 0xABCDEF11; |
| 1769 | |
| 1770 | uint8_t retcompletionCode; |
| 1771 | uint8_t retsensor_dataSize = PLDM_SENSOR_DATA_SIZE_UINT32; |
| 1772 | uint8_t retsensor_operationalState; |
| 1773 | uint8_t retsensor_event_messageEnable; |
| 1774 | uint8_t retpresentState; |
| 1775 | uint8_t retpreviousState; |
| 1776 | uint8_t reteventState; |
| 1777 | uint8_t retpresentReading[4]; |
| 1778 | |
| 1779 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 1780 | struct pldm_get_sensor_reading_resp* resp = |
| 1781 | reinterpret_cast<struct pldm_get_sensor_reading_resp*>( |
| 1782 | response->payload); |
| 1783 | |
| 1784 | resp->completion_code = completionCode; |
| 1785 | resp->sensor_data_size = sensor_dataSize; |
| 1786 | resp->sensor_operational_state = sensor_operationalState; |
| 1787 | resp->sensor_event_message_enable = sensor_event_messageEnable; |
| 1788 | resp->present_state = presentState; |
| 1789 | resp->previous_state = previousState; |
| 1790 | resp->event_state = eventState; |
| 1791 | |
| 1792 | uint32_t presentReading_le = htole32(presentReading); |
| 1793 | memcpy(resp->present_reading, &presentReading_le, |
| 1794 | sizeof(presentReading_le)); |
| 1795 | |
| 1796 | auto rc = decode_get_sensor_reading_resp( |
| 1797 | response, responseMsg.size() - hdrSize, &retcompletionCode, |
| 1798 | &retsensor_dataSize, &retsensor_operationalState, |
| 1799 | &retsensor_event_messageEnable, &retpresentState, &retpreviousState, |
| 1800 | &reteventState, retpresentReading); |
| 1801 | |
| 1802 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 1803 | EXPECT_EQ(completionCode, retcompletionCode); |
| 1804 | EXPECT_EQ(sensor_dataSize, retsensor_dataSize); |
| 1805 | EXPECT_EQ(sensor_operationalState, retsensor_operationalState); |
| 1806 | EXPECT_EQ(sensor_event_messageEnable, retsensor_event_messageEnable); |
| 1807 | EXPECT_EQ(presentState, retpresentState); |
| 1808 | EXPECT_EQ(previousState, retpreviousState); |
| 1809 | EXPECT_EQ(eventState, reteventState); |
| 1810 | EXPECT_EQ(presentReading, |
| 1811 | *(reinterpret_cast<uint32_t*>(retpresentReading))); |
| 1812 | } |
| 1813 | |
| 1814 | TEST(GetSensorReading, testBadDecodeResponse) |
| 1815 | { |
| 1816 | std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 1> |
| 1817 | responseMsg{}; |
| 1818 | |
| 1819 | auto rc = decode_get_sensor_reading_resp( |
| 1820 | nullptr, responseMsg.size() - hdrSize, nullptr, nullptr, nullptr, |
| 1821 | nullptr, nullptr, nullptr, nullptr, nullptr); |
| 1822 | |
| 1823 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 1824 | |
| 1825 | uint8_t completionCode = 0; |
| 1826 | uint8_t sensor_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT8; |
George Liu | 9d15502 | 2020-07-10 15:06:35 +0800 | [diff] [blame] | 1827 | uint8_t sensor_operationalState = PLDM_SENSOR_INTEST; |
Jolie Ku | f798c8f | 2020-04-14 11:18:06 +0800 | [diff] [blame] | 1828 | uint8_t sensor_event_messageEnable = PLDM_EVENTS_DISABLED; |
George Liu | 9d15502 | 2020-07-10 15:06:35 +0800 | [diff] [blame] | 1829 | uint8_t presentState = PLDM_SENSOR_FATAL; |
| 1830 | uint8_t previousState = PLDM_SENSOR_UPPERFATAL; |
| 1831 | uint8_t eventState = PLDM_SENSOR_WARNING; |
Jolie Ku | f798c8f | 2020-04-14 11:18:06 +0800 | [diff] [blame] | 1832 | uint8_t presentReading = 0xA; |
| 1833 | |
| 1834 | uint8_t retcompletionCode; |
| 1835 | uint8_t retsensor_dataSize = PLDM_SENSOR_DATA_SIZE_SINT16; |
| 1836 | uint8_t retsensor_operationalState; |
| 1837 | uint8_t retsensor_event_messageEnable; |
| 1838 | uint8_t retpresent_state; |
| 1839 | uint8_t retprevious_state; |
| 1840 | uint8_t retevent_state; |
| 1841 | uint8_t retpresentReading; |
| 1842 | |
| 1843 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 1844 | struct pldm_get_sensor_reading_resp* resp = |
| 1845 | reinterpret_cast<struct pldm_get_sensor_reading_resp*>( |
| 1846 | response->payload); |
| 1847 | |
| 1848 | resp->completion_code = completionCode; |
| 1849 | resp->sensor_data_size = sensor_dataSize; |
| 1850 | resp->sensor_operational_state = sensor_operationalState; |
| 1851 | resp->sensor_event_message_enable = sensor_event_messageEnable; |
| 1852 | resp->present_state = presentState; |
| 1853 | resp->previous_state = previousState; |
| 1854 | resp->event_state = eventState; |
| 1855 | resp->present_reading[0] = presentReading; |
| 1856 | |
| 1857 | rc = decode_get_sensor_reading_resp( |
| 1858 | response, responseMsg.size() - hdrSize, &retcompletionCode, |
| 1859 | &retsensor_dataSize, &retsensor_operationalState, |
| 1860 | &retsensor_event_messageEnable, &retpresent_state, &retprevious_state, |
| 1861 | &retevent_state, reinterpret_cast<uint8_t*>(&retpresentReading)); |
| 1862 | |
| 1863 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 1864 | } |
Sagar Srinivas | 535bccd | 2021-03-24 12:18:26 -0500 | [diff] [blame] | 1865 | |
| 1866 | TEST(SetEventReceiver, testGoodEncodeRequest) |
| 1867 | { |
| 1868 | uint8_t eventMessageGlobalEnable = |
| 1869 | PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE; |
| 1870 | uint8_t transportProtocolType = PLDM_TRANSPORT_PROTOCOL_TYPE_MCTP; |
| 1871 | uint8_t eventReceiverAddressInfo = 0x08; |
| 1872 | uint16_t heartbeatTimer = 0x78; |
| 1873 | |
| 1874 | std::vector<uint8_t> requestMsg(hdrSize + |
| 1875 | PLDM_SET_EVENT_RECEIVER_REQ_BYTES); |
| 1876 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 1877 | |
| 1878 | auto rc = encode_set_event_receiver_req( |
| 1879 | 0, eventMessageGlobalEnable, transportProtocolType, |
| 1880 | eventReceiverAddressInfo, heartbeatTimer, request); |
| 1881 | |
| 1882 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 1883 | struct pldm_set_event_receiver_req* req = |
| 1884 | reinterpret_cast<struct pldm_set_event_receiver_req*>(request->payload); |
| 1885 | EXPECT_EQ(eventMessageGlobalEnable, req->event_message_global_enable); |
| 1886 | EXPECT_EQ(transportProtocolType, req->transport_protocol_type); |
| 1887 | EXPECT_EQ(eventReceiverAddressInfo, req->event_receiver_address_info); |
| 1888 | EXPECT_EQ(heartbeatTimer, le16toh(req->heartbeat_timer)); |
| 1889 | } |
| 1890 | |
| 1891 | TEST(SetEventReceiver, testBadEncodeRequest) |
| 1892 | { |
| 1893 | uint8_t eventMessageGlobalEnable = |
| 1894 | PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE; |
| 1895 | uint8_t transportProtocolType = PLDM_TRANSPORT_PROTOCOL_TYPE_MCTP; |
| 1896 | uint8_t eventReceiverAddressInfo = 0x08; |
| 1897 | uint16_t heartbeatTimer = 0; |
| 1898 | |
| 1899 | std::vector<uint8_t> requestMsg(hdrSize + |
| 1900 | PLDM_SET_EVENT_RECEIVER_REQ_BYTES); |
| 1901 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 1902 | |
| 1903 | auto rc = encode_set_event_receiver_req( |
| 1904 | 0, eventMessageGlobalEnable, transportProtocolType, |
| 1905 | eventReceiverAddressInfo, heartbeatTimer, request); |
| 1906 | |
| 1907 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 1908 | } |
| 1909 | |
| 1910 | TEST(SetEventReceiver, testGoodDecodeResponse) |
| 1911 | { |
| 1912 | std::array<uint8_t, hdrSize + PLDM_SET_EVENT_RECEIVER_RESP_BYTES> |
| 1913 | responseMsg{}; |
| 1914 | |
| 1915 | uint8_t retcompletion_code = 0; |
| 1916 | responseMsg[hdrSize] = PLDM_SUCCESS; |
| 1917 | |
| 1918 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 1919 | auto rc = decode_set_event_receiver_resp( |
| 1920 | response, responseMsg.size() - sizeof(pldm_msg_hdr), |
| 1921 | &retcompletion_code); |
| 1922 | |
| 1923 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 1924 | EXPECT_EQ(PLDM_SUCCESS, retcompletion_code); |
| 1925 | } |
| 1926 | |
| 1927 | TEST(SetEventReceiver, testBadDecodeResponse) |
| 1928 | { |
| 1929 | std::array<uint8_t, hdrSize + PLDM_SET_EVENT_RECEIVER_RESP_BYTES> |
| 1930 | responseMsg{}; |
| 1931 | uint8_t retcompletion_code = 0; |
| 1932 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 1933 | |
| 1934 | auto rc = decode_set_event_receiver_resp( |
| 1935 | response, responseMsg.size() - sizeof(pldm_msg_hdr), NULL); |
| 1936 | |
| 1937 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 1938 | |
| 1939 | rc = decode_set_event_receiver_resp( |
| 1940 | nullptr, responseMsg.size() - sizeof(pldm_msg_hdr), |
| 1941 | &retcompletion_code); |
| 1942 | |
| 1943 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 1944 | } |
Sridevi Ramesh | 7418752 | 2021-04-08 09:50:22 -0500 | [diff] [blame^] | 1945 | |
| 1946 | TEST(SetEventReceiver, testGoodEncodeResponse) |
| 1947 | { |
| 1948 | std::array<uint8_t, |
| 1949 | sizeof(pldm_msg_hdr) + PLDM_SET_EVENT_RECEIVER_RESP_BYTES> |
| 1950 | responseMsg{}; |
| 1951 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 1952 | uint8_t completionCode = 0; |
| 1953 | |
| 1954 | auto rc = encode_set_event_receiver_resp(0, PLDM_SUCCESS, response); |
| 1955 | |
| 1956 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 1957 | EXPECT_EQ(completionCode, response->payload[0]); |
| 1958 | } |
| 1959 | |
| 1960 | TEST(SetEventReceiver, testBadEncodeResponse) |
| 1961 | { |
| 1962 | auto rc = encode_set_event_receiver_resp(0, PLDM_SUCCESS, NULL); |
| 1963 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 1964 | } |
| 1965 | |
| 1966 | TEST(SetEventReceiver, testGoodDecodeRequest) |
| 1967 | { |
| 1968 | |
| 1969 | std::array<uint8_t, hdrSize + PLDM_SET_EVENT_RECEIVER_REQ_BYTES> |
| 1970 | requestMsg{}; |
| 1971 | |
| 1972 | uint8_t eventMessageGlobalEnable = |
| 1973 | PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE; |
| 1974 | uint8_t transportProtocolType = PLDM_TRANSPORT_PROTOCOL_TYPE_MCTP; |
| 1975 | uint8_t eventReceiverAddressInfo = 0x08; |
| 1976 | uint16_t heartbeatTimer = 0x78; |
| 1977 | |
| 1978 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 1979 | struct pldm_set_event_receiver_req* req = |
| 1980 | reinterpret_cast<struct pldm_set_event_receiver_req*>(request->payload); |
| 1981 | |
| 1982 | req->event_message_global_enable = eventMessageGlobalEnable; |
| 1983 | req->transport_protocol_type = transportProtocolType; |
| 1984 | req->event_receiver_address_info = eventReceiverAddressInfo; |
| 1985 | req->heartbeat_timer = htole16(heartbeatTimer); |
| 1986 | |
| 1987 | uint8_t reteventMessageGlobalEnable; |
| 1988 | uint8_t rettransportProtocolType; |
| 1989 | uint8_t reteventReceiverAddressInfo; |
| 1990 | uint16_t retheartbeatTimer; |
| 1991 | auto rc = decode_set_event_receiver_req( |
| 1992 | request, requestMsg.size() - hdrSize, &reteventMessageGlobalEnable, |
| 1993 | &rettransportProtocolType, &reteventReceiverAddressInfo, |
| 1994 | &retheartbeatTimer); |
| 1995 | |
| 1996 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 1997 | EXPECT_EQ(eventMessageGlobalEnable, reteventMessageGlobalEnable); |
| 1998 | EXPECT_EQ(transportProtocolType, rettransportProtocolType); |
| 1999 | EXPECT_EQ(eventReceiverAddressInfo, reteventReceiverAddressInfo); |
| 2000 | EXPECT_EQ(heartbeatTimer, retheartbeatTimer); |
| 2001 | } |
| 2002 | |
| 2003 | TEST(SetEventReceiver, testBadDecodeRequest) |
| 2004 | { |
| 2005 | std::array<uint8_t, hdrSize + PLDM_SET_EVENT_RECEIVER_REQ_BYTES> |
| 2006 | requestMsg{}; |
| 2007 | |
| 2008 | auto rc = decode_set_event_receiver_req(NULL, requestMsg.size() - hdrSize, |
| 2009 | NULL, NULL, NULL, NULL); |
| 2010 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 2011 | |
| 2012 | uint8_t eventMessageGlobalEnable = |
| 2013 | PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE; |
| 2014 | uint8_t transportProtocolType = PLDM_TRANSPORT_PROTOCOL_TYPE_MCTP; |
| 2015 | uint8_t eventReceiverAddressInfo = 0x08; |
| 2016 | uint16_t heartbeatTimer = 0x78; |
| 2017 | |
| 2018 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 2019 | struct pldm_set_event_receiver_req* req = |
| 2020 | reinterpret_cast<struct pldm_set_event_receiver_req*>(request->payload); |
| 2021 | |
| 2022 | req->event_message_global_enable = eventMessageGlobalEnable; |
| 2023 | req->transport_protocol_type = transportProtocolType; |
| 2024 | req->event_receiver_address_info = eventReceiverAddressInfo; |
| 2025 | req->heartbeat_timer = htole16(heartbeatTimer); |
| 2026 | |
| 2027 | uint8_t reteventMessageGlobalEnable; |
| 2028 | uint8_t rettransportProtocolType; |
| 2029 | uint8_t reteventReceiverAddressInfo; |
| 2030 | uint16_t retheartbeatTimer; |
| 2031 | rc = decode_set_event_receiver_req( |
| 2032 | request, requestMsg.size() - hdrSize - 1, &reteventMessageGlobalEnable, |
| 2033 | &rettransportProtocolType, &reteventReceiverAddressInfo, |
| 2034 | &retheartbeatTimer); |
| 2035 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 2036 | } |