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 | |
| 22 | ASSERT_EQ(rc, PLDM_SUCCESS); |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 23 | ASSERT_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 | |
| 42 | ASSERT_EQ(rc, PLDM_SUCCESS); |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 43 | ASSERT_EQ(effecterId, request->payload[0]); |
| 44 | ASSERT_EQ(compEffecterCnt, request->payload[sizeof(effecterId)]); |
| 45 | ASSERT_EQ(stateField[0].set_request, |
| 46 | request->payload[sizeof(effecterId) + sizeof(compEffecterCnt)]); |
| 47 | ASSERT_EQ(stateField[0].effecter_state, |
| 48 | request->payload[sizeof(effecterId) + sizeof(compEffecterCnt) + |
| 49 | sizeof(stateField[0].set_request)]); |
| 50 | ASSERT_EQ(stateField[1].set_request, |
| 51 | request->payload[sizeof(effecterId) + sizeof(compEffecterCnt) + |
| 52 | sizeof(stateField[0])]); |
| 53 | ASSERT_EQ(stateField[1].effecter_state, |
| 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 completion_code = 0xA0; |
| 65 | |
| 66 | uint8_t retcompletion_code = 0; |
| 67 | |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 68 | memcpy(responseMsg.data() + hdrSize, &completion_code, |
| 69 | sizeof(completion_code)); |
| 70 | |
| 71 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
vkaverap | 98a2c19 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 72 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 73 | auto rc = decode_set_state_effecter_states_resp( |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 74 | response, responseMsg.size() - hdrSize, &retcompletion_code); |
vkaverap | 98a2c19 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 75 | |
| 76 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 77 | ASSERT_EQ(completion_code, retcompletion_code); |
| 78 | } |
| 79 | |
Sampa Misra | 0db1dfa | 2019-03-19 00:15:31 -0500 | [diff] [blame] | 80 | TEST(SetStateEffecterStates, testGoodDecodeRequest) |
| 81 | { |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 82 | std::array<uint8_t, hdrSize + PLDM_SET_STATE_EFFECTER_STATES_REQ_BYTES> |
| 83 | requestMsg{}; |
Sampa Misra | 0db1dfa | 2019-03-19 00:15:31 -0500 | [diff] [blame] | 84 | |
Sampa Misra | 0db1dfa | 2019-03-19 00:15:31 -0500 | [diff] [blame] | 85 | uint16_t effecterId = 0x32; |
| 86 | uint8_t compEffecterCnt = 0x2; |
| 87 | |
| 88 | std::array<set_effecter_state_field, 8> stateField{}; |
| 89 | stateField[0] = {PLDM_REQUEST_SET, 3}; |
| 90 | stateField[1] = {PLDM_REQUEST_SET, 4}; |
| 91 | |
| 92 | uint16_t retEffecterId = 0; |
| 93 | uint8_t retCompEffecterCnt = 0; |
| 94 | |
| 95 | std::array<set_effecter_state_field, 8> retStateField{}; |
| 96 | |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 97 | memcpy(requestMsg.data() + hdrSize, &effecterId, sizeof(effecterId)); |
| 98 | memcpy(requestMsg.data() + sizeof(effecterId) + hdrSize, &compEffecterCnt, |
Sampa Misra | 0db1dfa | 2019-03-19 00:15:31 -0500 | [diff] [blame] | 99 | sizeof(compEffecterCnt)); |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 100 | memcpy(requestMsg.data() + sizeof(effecterId) + sizeof(compEffecterCnt) + |
| 101 | hdrSize, |
Sampa Misra | 0db1dfa | 2019-03-19 00:15:31 -0500 | [diff] [blame] | 102 | &stateField, sizeof(stateField)); |
| 103 | |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 104 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 105 | |
Sampa Misra | 0db1dfa | 2019-03-19 00:15:31 -0500 | [diff] [blame] | 106 | auto rc = decode_set_state_effecter_states_req( |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 107 | request, requestMsg.size() - hdrSize, &retEffecterId, |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 108 | &retCompEffecterCnt, retStateField.data()); |
Sampa Misra | 0db1dfa | 2019-03-19 00:15:31 -0500 | [diff] [blame] | 109 | |
| 110 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 111 | ASSERT_EQ(effecterId, retEffecterId); |
| 112 | ASSERT_EQ(retCompEffecterCnt, compEffecterCnt); |
| 113 | ASSERT_EQ(retStateField[0].set_request, stateField[0].set_request); |
| 114 | ASSERT_EQ(retStateField[0].effecter_state, stateField[0].effecter_state); |
| 115 | ASSERT_EQ(retStateField[1].set_request, stateField[1].set_request); |
| 116 | ASSERT_EQ(retStateField[1].effecter_state, stateField[1].effecter_state); |
| 117 | } |
| 118 | |
| 119 | TEST(SetStateEffecterStates, testBadDecodeRequest) |
| 120 | { |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 121 | const struct pldm_msg* msg = NULL; |
Sampa Misra | 0db1dfa | 2019-03-19 00:15:31 -0500 | [diff] [blame] | 122 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 123 | auto rc = decode_set_state_effecter_states_req(msg, sizeof(*msg), NULL, |
| 124 | NULL, NULL); |
Sampa Misra | 0db1dfa | 2019-03-19 00:15:31 -0500 | [diff] [blame] | 125 | |
| 126 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 127 | } |
vkaverap | 98a2c19 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 128 | |
| 129 | TEST(SetStateEffecterStates, testBadDecodeResponse) |
| 130 | { |
| 131 | std::array<uint8_t, PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES> |
| 132 | responseMsg{}; |
| 133 | |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 134 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 135 | |
| 136 | auto rc = decode_set_state_effecter_states_resp(response, |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 137 | responseMsg.size(), NULL); |
vkaverap | 98a2c19 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 138 | |
| 139 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 140 | } |
Sampa Misra | 7fcfb66 | 2019-05-08 13:13:53 -0500 | [diff] [blame] | 141 | |
| 142 | TEST(GetPDR, testGoodEncodeResponse) |
| 143 | { |
| 144 | uint8_t completionCode = 0; |
| 145 | uint32_t nextRecordHndl = 0x12; |
| 146 | uint32_t nextDataTransferHndl = 0x13; |
| 147 | uint8_t transferFlag = PLDM_START_AND_END; |
| 148 | uint16_t respCnt = 0x5; |
| 149 | std::vector<uint8_t> recordData{1, 2, 3, 4, 5}; |
| 150 | uint8_t transferCRC = 0; |
| 151 | |
| 152 | // + size of record data and transfer CRC |
| 153 | std::vector<uint8_t> responseMsg(hdrSize + PLDM_GET_PDR_MIN_RESP_BYTES + |
| 154 | recordData.size() + 1); |
| 155 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 156 | |
| 157 | auto rc = encode_get_pdr_resp(0, PLDM_SUCCESS, nextRecordHndl, |
| 158 | nextDataTransferHndl, transferFlag, respCnt, |
| 159 | recordData.data(), transferCRC, response); |
| 160 | |
| 161 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 162 | struct pldm_get_pdr_resp* resp = |
| 163 | reinterpret_cast<struct pldm_get_pdr_resp*>(response->payload); |
| 164 | |
| 165 | ASSERT_EQ(completionCode, resp->completion_code); |
| 166 | ASSERT_EQ(nextRecordHndl, resp->next_record_handle); |
| 167 | ASSERT_EQ(nextDataTransferHndl, resp->next_data_transfer_handle); |
| 168 | ASSERT_EQ(transferFlag, resp->transfer_flag); |
| 169 | ASSERT_EQ(respCnt, resp->response_count); |
| 170 | ASSERT_EQ(0, |
| 171 | memcmp(recordData.data(), resp->record_data, recordData.size())); |
| 172 | } |
| 173 | |
| 174 | TEST(GetPDR, testBadEncodeResponse) |
| 175 | { |
| 176 | uint32_t nextRecordHndl = 0x12; |
| 177 | uint32_t nextDataTransferHndl = 0x13; |
| 178 | uint8_t transferFlag = PLDM_START_AND_END; |
| 179 | uint16_t respCnt = 0x5; |
| 180 | std::vector<uint8_t> recordData{1, 2, 3, 4, 5}; |
| 181 | uint8_t transferCRC = 0; |
| 182 | |
| 183 | auto rc = encode_get_pdr_resp(0, PLDM_SUCCESS, nextRecordHndl, |
| 184 | nextDataTransferHndl, transferFlag, respCnt, |
| 185 | recordData.data(), transferCRC, nullptr); |
| 186 | |
| 187 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 188 | } |
| 189 | |
| 190 | TEST(GetPDR, testGoodDecodeRequest) |
| 191 | { |
| 192 | std::array<uint8_t, hdrSize + PLDM_GET_PDR_REQ_BYTES> requestMsg{}; |
| 193 | |
| 194 | uint32_t recordHndl = 0x32; |
| 195 | uint32_t dataTransferHndl = 0x11; |
| 196 | uint8_t transferOpFlag = PLDM_GET_FIRSTPART; |
| 197 | uint16_t requestCnt = 0x5; |
| 198 | uint16_t recordChangeNum = 0; |
| 199 | |
| 200 | uint32_t retRecordHndl = 0; |
| 201 | uint32_t retDataTransferHndl = 0; |
| 202 | uint8_t retTransferOpFlag = 0; |
| 203 | uint16_t retRequestCnt = 0; |
| 204 | uint16_t retRecordChangeNum = 0; |
| 205 | |
| 206 | auto req = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 207 | struct pldm_get_pdr_req* request = |
| 208 | reinterpret_cast<struct pldm_get_pdr_req*>(req->payload); |
| 209 | |
| 210 | request->record_handle = recordHndl; |
| 211 | request->data_transfer_handle = dataTransferHndl; |
| 212 | request->transfer_op_flag = transferOpFlag; |
| 213 | request->request_count = requestCnt; |
| 214 | request->record_change_number = recordChangeNum; |
| 215 | |
| 216 | auto rc = decode_get_pdr_req( |
| 217 | req, requestMsg.size() - hdrSize, &retRecordHndl, &retDataTransferHndl, |
| 218 | &retTransferOpFlag, &retRequestCnt, &retRecordChangeNum); |
| 219 | |
| 220 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 221 | ASSERT_EQ(retRecordHndl, recordHndl); |
| 222 | ASSERT_EQ(retDataTransferHndl, dataTransferHndl); |
| 223 | ASSERT_EQ(retTransferOpFlag, transferOpFlag); |
| 224 | ASSERT_EQ(retRequestCnt, requestCnt); |
| 225 | ASSERT_EQ(retRecordChangeNum, recordChangeNum); |
| 226 | } |
| 227 | |
| 228 | TEST(GetPDR, testBadDecodeRequest) |
| 229 | { |
| 230 | std::array<uint8_t, PLDM_GET_PDR_REQ_BYTES> requestMsg{}; |
| 231 | auto req = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 232 | |
| 233 | auto rc = decode_get_pdr_req(req, requestMsg.size(), NULL, NULL, NULL, NULL, |
| 234 | NULL); |
| 235 | |
| 236 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 237 | } |