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 | |
Sampa Misra | 0db1dfa | 2019-03-19 00:15:31 -0500 | [diff] [blame] | 82 | uint16_t effecterId = 0x32; |
| 83 | uint8_t compEffecterCnt = 0x2; |
| 84 | |
| 85 | std::array<set_effecter_state_field, 8> stateField{}; |
| 86 | stateField[0] = {PLDM_REQUEST_SET, 3}; |
| 87 | stateField[1] = {PLDM_REQUEST_SET, 4}; |
| 88 | |
| 89 | uint16_t retEffecterId = 0; |
| 90 | uint8_t retCompEffecterCnt = 0; |
| 91 | |
| 92 | std::array<set_effecter_state_field, 8> retStateField{}; |
| 93 | |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 94 | memcpy(requestMsg.data() + hdrSize, &effecterId, sizeof(effecterId)); |
| 95 | memcpy(requestMsg.data() + sizeof(effecterId) + hdrSize, &compEffecterCnt, |
Sampa Misra | 0db1dfa | 2019-03-19 00:15:31 -0500 | [diff] [blame] | 96 | sizeof(compEffecterCnt)); |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 97 | memcpy(requestMsg.data() + sizeof(effecterId) + sizeof(compEffecterCnt) + |
| 98 | hdrSize, |
Sampa Misra | 0db1dfa | 2019-03-19 00:15:31 -0500 | [diff] [blame] | 99 | &stateField, sizeof(stateField)); |
| 100 | |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 101 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 102 | |
Sampa Misra | 0db1dfa | 2019-03-19 00:15:31 -0500 | [diff] [blame] | 103 | auto rc = decode_set_state_effecter_states_req( |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 104 | request, requestMsg.size() - hdrSize, &retEffecterId, |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 105 | &retCompEffecterCnt, retStateField.data()); |
Sampa Misra | 0db1dfa | 2019-03-19 00:15:31 -0500 | [diff] [blame] | 106 | |
George Liu | 684a716 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 107 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 108 | EXPECT_EQ(effecterId, retEffecterId); |
| 109 | EXPECT_EQ(retCompEffecterCnt, compEffecterCnt); |
| 110 | EXPECT_EQ(retStateField[0].set_request, stateField[0].set_request); |
| 111 | EXPECT_EQ(retStateField[0].effecter_state, stateField[0].effecter_state); |
| 112 | EXPECT_EQ(retStateField[1].set_request, stateField[1].set_request); |
| 113 | EXPECT_EQ(retStateField[1].effecter_state, stateField[1].effecter_state); |
Sampa Misra | 0db1dfa | 2019-03-19 00:15:31 -0500 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | TEST(SetStateEffecterStates, testBadDecodeRequest) |
| 117 | { |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 118 | const struct pldm_msg* msg = NULL; |
Sampa Misra | 0db1dfa | 2019-03-19 00:15:31 -0500 | [diff] [blame] | 119 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 120 | auto rc = decode_set_state_effecter_states_req(msg, sizeof(*msg), NULL, |
| 121 | NULL, NULL); |
Sampa Misra | 0db1dfa | 2019-03-19 00:15:31 -0500 | [diff] [blame] | 122 | |
George Liu | 684a716 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 123 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
Sampa Misra | 0db1dfa | 2019-03-19 00:15:31 -0500 | [diff] [blame] | 124 | } |
vkaverap | 98a2c19 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 125 | |
| 126 | TEST(SetStateEffecterStates, testBadDecodeResponse) |
| 127 | { |
| 128 | std::array<uint8_t, PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES> |
| 129 | responseMsg{}; |
| 130 | |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 131 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 132 | |
| 133 | auto rc = decode_set_state_effecter_states_resp(response, |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 134 | responseMsg.size(), NULL); |
vkaverap | 98a2c19 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 135 | |
George Liu | 684a716 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 136 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
vkaverap | 98a2c19 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 137 | } |
Sampa Misra | 7fcfb66 | 2019-05-08 13:13:53 -0500 | [diff] [blame] | 138 | |
| 139 | TEST(GetPDR, testGoodEncodeResponse) |
| 140 | { |
| 141 | uint8_t completionCode = 0; |
| 142 | uint32_t nextRecordHndl = 0x12; |
| 143 | uint32_t nextDataTransferHndl = 0x13; |
Deepak Kodihalli | e4b16ee | 2019-08-14 06:54:38 -0500 | [diff] [blame] | 144 | uint8_t transferFlag = PLDM_END; |
Sampa Misra | 7fcfb66 | 2019-05-08 13:13:53 -0500 | [diff] [blame] | 145 | uint16_t respCnt = 0x5; |
| 146 | std::vector<uint8_t> recordData{1, 2, 3, 4, 5}; |
Deepak Kodihalli | e4b16ee | 2019-08-14 06:54:38 -0500 | [diff] [blame] | 147 | uint8_t transferCRC = 6; |
Sampa Misra | 7fcfb66 | 2019-05-08 13:13:53 -0500 | [diff] [blame] | 148 | |
| 149 | // + size of record data and transfer CRC |
| 150 | std::vector<uint8_t> responseMsg(hdrSize + PLDM_GET_PDR_MIN_RESP_BYTES + |
| 151 | recordData.size() + 1); |
| 152 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 153 | |
| 154 | auto rc = encode_get_pdr_resp(0, PLDM_SUCCESS, nextRecordHndl, |
| 155 | nextDataTransferHndl, transferFlag, respCnt, |
| 156 | recordData.data(), transferCRC, response); |
| 157 | |
George Liu | 684a716 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 158 | EXPECT_EQ(rc, PLDM_SUCCESS); |
Sampa Misra | 7fcfb66 | 2019-05-08 13:13:53 -0500 | [diff] [blame] | 159 | struct pldm_get_pdr_resp* resp = |
| 160 | reinterpret_cast<struct pldm_get_pdr_resp*>(response->payload); |
| 161 | |
George Liu | 684a716 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 162 | EXPECT_EQ(completionCode, resp->completion_code); |
| 163 | EXPECT_EQ(nextRecordHndl, resp->next_record_handle); |
| 164 | EXPECT_EQ(nextDataTransferHndl, resp->next_data_transfer_handle); |
| 165 | EXPECT_EQ(transferFlag, resp->transfer_flag); |
| 166 | EXPECT_EQ(respCnt, resp->response_count); |
| 167 | EXPECT_EQ(0, |
Sampa Misra | 7fcfb66 | 2019-05-08 13:13:53 -0500 | [diff] [blame] | 168 | memcmp(recordData.data(), resp->record_data, recordData.size())); |
George Liu | 684a716 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 169 | EXPECT_EQ(*(response->payload + sizeof(pldm_get_pdr_resp) - 1 + |
Deepak Kodihalli | e4b16ee | 2019-08-14 06:54:38 -0500 | [diff] [blame] | 170 | recordData.size()), |
| 171 | transferCRC); |
| 172 | |
| 173 | transferFlag = PLDM_START_AND_END; // No CRC in this case |
| 174 | responseMsg.resize(responseMsg.size() - sizeof(transferCRC)); |
| 175 | rc = encode_get_pdr_resp(0, PLDM_SUCCESS, nextRecordHndl, |
| 176 | nextDataTransferHndl, transferFlag, respCnt, |
| 177 | recordData.data(), transferCRC, response); |
George Liu | 684a716 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 178 | EXPECT_EQ(rc, PLDM_SUCCESS); |
Sampa Misra | 7fcfb66 | 2019-05-08 13:13:53 -0500 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | TEST(GetPDR, testBadEncodeResponse) |
| 182 | { |
| 183 | uint32_t nextRecordHndl = 0x12; |
| 184 | uint32_t nextDataTransferHndl = 0x13; |
| 185 | uint8_t transferFlag = PLDM_START_AND_END; |
| 186 | uint16_t respCnt = 0x5; |
| 187 | std::vector<uint8_t> recordData{1, 2, 3, 4, 5}; |
| 188 | uint8_t transferCRC = 0; |
| 189 | |
| 190 | auto rc = encode_get_pdr_resp(0, PLDM_SUCCESS, nextRecordHndl, |
| 191 | nextDataTransferHndl, transferFlag, respCnt, |
| 192 | recordData.data(), transferCRC, nullptr); |
| 193 | |
George Liu | 684a716 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 194 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
Sampa Misra | 7fcfb66 | 2019-05-08 13:13:53 -0500 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | TEST(GetPDR, testGoodDecodeRequest) |
| 198 | { |
| 199 | std::array<uint8_t, hdrSize + PLDM_GET_PDR_REQ_BYTES> requestMsg{}; |
| 200 | |
| 201 | uint32_t recordHndl = 0x32; |
| 202 | uint32_t dataTransferHndl = 0x11; |
| 203 | uint8_t transferOpFlag = PLDM_GET_FIRSTPART; |
| 204 | uint16_t requestCnt = 0x5; |
| 205 | uint16_t recordChangeNum = 0; |
| 206 | |
| 207 | uint32_t retRecordHndl = 0; |
| 208 | uint32_t retDataTransferHndl = 0; |
| 209 | uint8_t retTransferOpFlag = 0; |
| 210 | uint16_t retRequestCnt = 0; |
| 211 | uint16_t retRecordChangeNum = 0; |
| 212 | |
| 213 | auto req = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 214 | struct pldm_get_pdr_req* request = |
| 215 | reinterpret_cast<struct pldm_get_pdr_req*>(req->payload); |
| 216 | |
| 217 | request->record_handle = recordHndl; |
| 218 | request->data_transfer_handle = dataTransferHndl; |
| 219 | request->transfer_op_flag = transferOpFlag; |
| 220 | request->request_count = requestCnt; |
| 221 | request->record_change_number = recordChangeNum; |
| 222 | |
| 223 | auto rc = decode_get_pdr_req( |
| 224 | req, requestMsg.size() - hdrSize, &retRecordHndl, &retDataTransferHndl, |
| 225 | &retTransferOpFlag, &retRequestCnt, &retRecordChangeNum); |
| 226 | |
George Liu | 684a716 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 227 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 228 | EXPECT_EQ(retRecordHndl, recordHndl); |
| 229 | EXPECT_EQ(retDataTransferHndl, dataTransferHndl); |
| 230 | EXPECT_EQ(retTransferOpFlag, transferOpFlag); |
| 231 | EXPECT_EQ(retRequestCnt, requestCnt); |
| 232 | EXPECT_EQ(retRecordChangeNum, recordChangeNum); |
Sampa Misra | 7fcfb66 | 2019-05-08 13:13:53 -0500 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | TEST(GetPDR, testBadDecodeRequest) |
| 236 | { |
| 237 | std::array<uint8_t, PLDM_GET_PDR_REQ_BYTES> requestMsg{}; |
| 238 | auto req = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 239 | |
| 240 | auto rc = decode_get_pdr_req(req, requestMsg.size(), NULL, NULL, NULL, NULL, |
| 241 | NULL); |
| 242 | |
George Liu | 684a716 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 243 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
Sampa Misra | 7fcfb66 | 2019-05-08 13:13:53 -0500 | [diff] [blame] | 244 | } |
George Liu | 820a9a5 | 2019-11-26 14:43:59 +0800 | [diff] [blame] | 245 | |
| 246 | TEST(GetPDR, testGoodEncodeRequest) |
| 247 | { |
| 248 | uint32_t record_hndl = 0; |
| 249 | uint32_t data_transfer_hndl = 0; |
| 250 | uint8_t transfer_op_flag = PLDM_GET_FIRSTPART; |
| 251 | uint16_t request_cnt = 20; |
| 252 | uint16_t record_chg_num = 0; |
| 253 | |
| 254 | std::vector<uint8_t> requestMsg(hdrSize + PLDM_GET_PDR_REQ_BYTES); |
| 255 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 256 | |
| 257 | auto rc = encode_get_pdr_req(0, record_hndl, data_transfer_hndl, |
| 258 | transfer_op_flag, request_cnt, record_chg_num, |
| 259 | request, PLDM_GET_PDR_REQ_BYTES); |
| 260 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 261 | struct pldm_get_pdr_req* req = |
| 262 | reinterpret_cast<struct pldm_get_pdr_req*>(request->payload); |
| 263 | EXPECT_EQ(record_hndl, req->record_handle); |
| 264 | EXPECT_EQ(data_transfer_hndl, req->data_transfer_handle); |
| 265 | EXPECT_EQ(transfer_op_flag, req->transfer_op_flag); |
| 266 | EXPECT_EQ(request_cnt, req->request_count); |
| 267 | EXPECT_EQ(record_chg_num, req->record_change_number); |
| 268 | } |
| 269 | |
| 270 | TEST(GetPDR, testBadEncodeRequest) |
| 271 | { |
| 272 | uint32_t record_hndl = 0; |
| 273 | uint32_t data_transfer_hndl = 0; |
| 274 | uint8_t transfer_op_flag = PLDM_GET_FIRSTPART; |
| 275 | uint16_t request_cnt = 32; |
| 276 | uint16_t record_chg_num = 0; |
| 277 | |
| 278 | std::vector<uint8_t> requestMsg(hdrSize + PLDM_GET_PDR_REQ_BYTES); |
| 279 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 280 | |
| 281 | auto rc = encode_get_pdr_req(0, record_hndl, data_transfer_hndl, |
| 282 | transfer_op_flag, request_cnt, record_chg_num, |
| 283 | nullptr, PLDM_GET_PDR_REQ_BYTES); |
| 284 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 285 | |
| 286 | rc = encode_get_pdr_req(0, record_hndl, data_transfer_hndl, |
| 287 | transfer_op_flag, request_cnt, record_chg_num, |
| 288 | request, PLDM_GET_PDR_REQ_BYTES + 1); |
| 289 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 290 | } |
| 291 | |
| 292 | TEST(GetPDR, testGoodDecodeResponse) |
| 293 | { |
| 294 | const char* recordData = "123456789"; |
| 295 | uint8_t completionCode = PLDM_SUCCESS; |
| 296 | uint32_t nextRecordHndl = 0; |
| 297 | uint32_t nextDataTransferHndl = 0; |
| 298 | uint8_t transferFlag = PLDM_END; |
| 299 | constexpr uint16_t respCnt = 9; |
| 300 | uint8_t transferCRC = 96; |
| 301 | size_t recordDataLength = 32; |
| 302 | std::array<uint8_t, hdrSize + PLDM_GET_PDR_MIN_RESP_BYTES + respCnt + |
| 303 | sizeof(transferCRC)> |
| 304 | responseMsg{}; |
| 305 | |
| 306 | uint8_t retCompletionCode = 0; |
| 307 | uint8_t retRecordData[32] = {0}; |
| 308 | uint32_t retNextRecordHndl = 0; |
| 309 | uint32_t retNextDataTransferHndl = 0; |
| 310 | uint8_t retTransferFlag = 0; |
| 311 | uint16_t retRespCnt = 0; |
| 312 | uint8_t retTransferCRC = 0; |
| 313 | |
| 314 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 315 | struct pldm_get_pdr_resp* resp = |
| 316 | reinterpret_cast<struct pldm_get_pdr_resp*>(response->payload); |
| 317 | resp->completion_code = completionCode; |
| 318 | resp->next_record_handle = htole32(nextRecordHndl); |
| 319 | resp->next_data_transfer_handle = htole32(nextDataTransferHndl); |
| 320 | resp->transfer_flag = transferFlag; |
| 321 | resp->response_count = htole16(respCnt); |
| 322 | memcpy(resp->record_data, recordData, respCnt); |
George Liu | 684a716 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 323 | response->payload[PLDM_GET_PDR_MIN_RESP_BYTES + respCnt] = transferCRC; |
George Liu | 820a9a5 | 2019-11-26 14:43:59 +0800 | [diff] [blame] | 324 | |
| 325 | auto rc = decode_get_pdr_resp( |
| 326 | response, responseMsg.size() - hdrSize, &retCompletionCode, |
| 327 | &retNextRecordHndl, &retNextDataTransferHndl, &retTransferFlag, |
| 328 | &retRespCnt, retRecordData, recordDataLength, &retTransferCRC); |
| 329 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 330 | EXPECT_EQ(retCompletionCode, completionCode); |
| 331 | EXPECT_EQ(retNextRecordHndl, nextRecordHndl); |
| 332 | EXPECT_EQ(retNextDataTransferHndl, nextDataTransferHndl); |
| 333 | EXPECT_EQ(retTransferFlag, transferFlag); |
| 334 | EXPECT_EQ(retRespCnt, respCnt); |
| 335 | EXPECT_EQ(retTransferCRC, transferCRC); |
| 336 | EXPECT_EQ(0, memcmp(recordData, resp->record_data, respCnt)); |
| 337 | } |
| 338 | |
| 339 | TEST(GetPDR, testBadDecodeResponse) |
| 340 | { |
| 341 | const char* recordData = "123456789"; |
| 342 | uint8_t completionCode = PLDM_SUCCESS; |
| 343 | uint32_t nextRecordHndl = 0; |
| 344 | uint32_t nextDataTransferHndl = 0; |
| 345 | uint8_t transferFlag = PLDM_END; |
| 346 | constexpr uint16_t respCnt = 9; |
| 347 | uint8_t transferCRC = 96; |
| 348 | size_t recordDataLength = 32; |
| 349 | std::array<uint8_t, hdrSize + PLDM_GET_PDR_MIN_RESP_BYTES + respCnt + |
| 350 | sizeof(transferCRC)> |
| 351 | responseMsg{}; |
| 352 | |
| 353 | uint8_t retCompletionCode = 0; |
George Liu | 684a716 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 354 | uint8_t retRecordData[32] = {0}; |
George Liu | 820a9a5 | 2019-11-26 14:43:59 +0800 | [diff] [blame] | 355 | uint32_t retNextRecordHndl = 0; |
| 356 | uint32_t retNextDataTransferHndl = 0; |
| 357 | uint8_t retTransferFlag = 0; |
| 358 | uint16_t retRespCnt = 0; |
| 359 | uint8_t retTransferCRC = 0; |
| 360 | |
| 361 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 362 | struct pldm_get_pdr_resp* resp = |
| 363 | reinterpret_cast<struct pldm_get_pdr_resp*>(response->payload); |
| 364 | resp->completion_code = completionCode; |
| 365 | resp->next_record_handle = htole32(nextRecordHndl); |
| 366 | resp->next_data_transfer_handle = htole32(nextDataTransferHndl); |
| 367 | resp->transfer_flag = transferFlag; |
| 368 | resp->response_count = htole16(respCnt); |
| 369 | memcpy(resp->record_data, recordData, respCnt); |
George Liu | 684a716 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 370 | response->payload[PLDM_GET_PDR_MIN_RESP_BYTES + respCnt] = transferCRC; |
George Liu | 820a9a5 | 2019-11-26 14:43:59 +0800 | [diff] [blame] | 371 | |
| 372 | auto rc = decode_get_pdr_resp(response, responseMsg.size() - hdrSize, NULL, |
| 373 | NULL, NULL, NULL, NULL, NULL, 0, NULL); |
| 374 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 375 | |
| 376 | rc = decode_get_pdr_resp( |
| 377 | response, responseMsg.size() - hdrSize - 1, &retCompletionCode, |
| 378 | &retNextRecordHndl, &retNextDataTransferHndl, &retTransferFlag, |
| 379 | &retRespCnt, retRecordData, recordDataLength, &retTransferCRC); |
| 380 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 381 | } |
George Liu | 30b859f | 2020-01-07 15:03:22 +0800 | [diff] [blame] | 382 | |
| 383 | TEST(SetNumericEffecterValue, testGoodDecodeRequest) |
| 384 | { |
| 385 | std::array<uint8_t, |
| 386 | hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 3> |
| 387 | requestMsg{}; |
| 388 | |
| 389 | uint16_t effecter_id = 32768; |
| 390 | uint8_t effecter_data_size = PLDM_EFFECTER_DATA_SIZE_UINT32; |
| 391 | uint32_t effecter_value = 123456789; |
| 392 | |
| 393 | uint16_t reteffecter_id; |
| 394 | uint8_t reteffecter_data_size; |
| 395 | uint8_t reteffecter_value[4]; |
| 396 | |
| 397 | auto req = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 398 | struct pldm_set_numeric_effecter_value_req* request = |
| 399 | reinterpret_cast<struct pldm_set_numeric_effecter_value_req*>( |
| 400 | req->payload); |
| 401 | |
| 402 | request->effecter_id = effecter_id; |
| 403 | request->effecter_data_size = effecter_data_size; |
| 404 | memcpy(request->effecter_value, &effecter_value, sizeof(effecter_value)); |
| 405 | |
| 406 | auto rc = decode_set_numeric_effecter_value_req( |
| 407 | req, requestMsg.size() - hdrSize, &reteffecter_id, |
| 408 | &reteffecter_data_size, reinterpret_cast<uint8_t*>(&reteffecter_value)); |
| 409 | |
| 410 | uint32_t value = *(reinterpret_cast<uint32_t*>(&reteffecter_value[0])); |
| 411 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 412 | EXPECT_EQ(reteffecter_id, effecter_id); |
| 413 | EXPECT_EQ(reteffecter_data_size, effecter_data_size); |
| 414 | EXPECT_EQ(value, effecter_value); |
| 415 | } |
| 416 | |
| 417 | TEST(SetNumericEffecterValue, testBadDecodeRequest) |
| 418 | { |
| 419 | std::array<uint8_t, hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES> |
| 420 | requestMsg{}; |
| 421 | |
| 422 | auto rc = decode_set_numeric_effecter_value_req( |
| 423 | NULL, requestMsg.size() - hdrSize, NULL, NULL, NULL); |
| 424 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 425 | |
| 426 | uint16_t effecter_id = 0x10; |
| 427 | uint8_t effecter_data_size = PLDM_EFFECTER_DATA_SIZE_UINT8; |
| 428 | uint8_t effecter_value = 1; |
| 429 | |
| 430 | uint16_t reteffecter_id; |
| 431 | uint8_t reteffecter_data_size; |
| 432 | uint8_t reteffecter_value[4]; |
| 433 | |
| 434 | auto req = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 435 | struct pldm_set_numeric_effecter_value_req* request = |
| 436 | reinterpret_cast<struct pldm_set_numeric_effecter_value_req*>( |
| 437 | req->payload); |
| 438 | |
| 439 | request->effecter_id = effecter_id; |
| 440 | request->effecter_data_size = effecter_data_size; |
| 441 | memcpy(request->effecter_value, &effecter_value, sizeof(effecter_value)); |
| 442 | |
| 443 | rc = decode_set_numeric_effecter_value_req( |
| 444 | req, requestMsg.size() - hdrSize - 1, &reteffecter_id, |
| 445 | &reteffecter_data_size, reinterpret_cast<uint8_t*>(&reteffecter_value)); |
| 446 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 447 | } |
| 448 | |
| 449 | TEST(SetNumericEffecterValue, testGoodEncodeRequest) |
| 450 | { |
| 451 | uint16_t effecter_id = 0; |
| 452 | uint8_t effecter_data_size = PLDM_EFFECTER_DATA_SIZE_UINT8; |
| 453 | uint8_t effecter_value = 1; |
| 454 | |
| 455 | std::vector<uint8_t> requestMsg( |
| 456 | hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES); |
| 457 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 458 | |
| 459 | auto rc = encode_set_numeric_effecter_value_req( |
| 460 | 0, effecter_id, effecter_data_size, |
| 461 | reinterpret_cast<uint8_t*>(&effecter_value), request, |
| 462 | PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES); |
| 463 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 464 | |
| 465 | struct pldm_set_numeric_effecter_value_req* req = |
| 466 | reinterpret_cast<struct pldm_set_numeric_effecter_value_req*>( |
| 467 | request->payload); |
| 468 | EXPECT_EQ(effecter_id, req->effecter_id); |
| 469 | EXPECT_EQ(effecter_data_size, req->effecter_data_size); |
| 470 | EXPECT_EQ(effecter_value, |
| 471 | *(reinterpret_cast<uint8_t*>(&req->effecter_value[0]))); |
| 472 | } |
| 473 | |
| 474 | TEST(SetNumericEffecterValue, testBadEncodeRequest) |
| 475 | { |
| 476 | std::vector<uint8_t> requestMsg( |
| 477 | hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES); |
| 478 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 479 | |
| 480 | auto rc = encode_set_numeric_effecter_value_req( |
| 481 | 0, 0, 0, NULL, NULL, PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES); |
| 482 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 483 | |
| 484 | uint16_t effecter_value; |
| 485 | rc = encode_set_numeric_effecter_value_req( |
| 486 | 0, 0, 6, reinterpret_cast<uint8_t*>(&effecter_value), request, |
| 487 | PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES); |
| 488 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 489 | } |
| 490 | |
| 491 | TEST(SetNumericEffecterValue, testGoodDecodeResponse) |
| 492 | { |
| 493 | std::array<uint8_t, hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES> |
| 494 | responseMsg{}; |
| 495 | |
| 496 | uint8_t completion_code = 0xA0; |
| 497 | |
| 498 | uint8_t retcompletion_code; |
| 499 | |
| 500 | memcpy(responseMsg.data() + hdrSize, &completion_code, |
| 501 | sizeof(completion_code)); |
| 502 | |
| 503 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 504 | |
| 505 | auto rc = decode_set_numeric_effecter_value_resp( |
| 506 | response, responseMsg.size() - hdrSize, &retcompletion_code); |
| 507 | |
| 508 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 509 | EXPECT_EQ(completion_code, retcompletion_code); |
| 510 | } |
| 511 | |
| 512 | TEST(SetNumericEffecterValue, testBadDecodeResponse) |
| 513 | { |
| 514 | std::array<uint8_t, PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES> |
| 515 | responseMsg{}; |
| 516 | |
| 517 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 518 | |
| 519 | auto rc = decode_set_numeric_effecter_value_resp(response, |
| 520 | responseMsg.size(), NULL); |
| 521 | |
| 522 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 523 | } |
| 524 | |
| 525 | TEST(SetNumericEffecterValue, testGoodEncodeResponse) |
| 526 | { |
| 527 | std::array<uint8_t, sizeof(pldm_msg_hdr) + |
| 528 | PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES> |
| 529 | responseMsg{}; |
| 530 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 531 | uint8_t completionCode = 0; |
| 532 | |
| 533 | auto rc = encode_set_numeric_effecter_value_resp( |
| 534 | 0, PLDM_SUCCESS, response, PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES); |
| 535 | |
| 536 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 537 | EXPECT_EQ(completionCode, response->payload[0]); |
| 538 | } |
| 539 | |
| 540 | TEST(SetNumericEffecterValue, testBadEncodeResponse) |
| 541 | { |
| 542 | auto rc = encode_set_numeric_effecter_value_resp( |
| 543 | 0, PLDM_SUCCESS, NULL, PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES); |
| 544 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 545 | } |