gokulsanker | 138ceba | 2021-04-05 13:25:25 +0530 | [diff] [blame] | 1 | #include "libpldm/base.h"
|
| 2 | #include "libpldm/firmware_update.h"
|
| 3 |
|
| 4 | #include <gtest/gtest.h>
|
| 5 |
|
| 6 | constexpr auto hdrSize = sizeof(pldm_msg_hdr);
|
| 7 |
|
| 8 | TEST(QueryDeviceIdentifiers, goodPathEncodeRequest)
|
| 9 | {
|
| 10 | std::array<uint8_t, sizeof(pldm_msg_hdr)> requestMsg{};
|
| 11 | auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
|
| 12 |
|
| 13 | uint8_t instanceId = 0x01;
|
| 14 |
|
| 15 | auto rc = encode_query_device_identifiers_req(
|
| 16 | instanceId, PLDM_QUERY_DEVICE_IDENTIFIERS_REQ_BYTES, requestPtr);
|
| 17 | EXPECT_EQ(rc, PLDM_SUCCESS);
|
| 18 | EXPECT_EQ(requestPtr->hdr.request, PLDM_REQUEST);
|
| 19 | EXPECT_EQ(requestPtr->hdr.instance_id, instanceId);
|
| 20 | EXPECT_EQ(requestPtr->hdr.type, PLDM_FWUP);
|
| 21 | EXPECT_EQ(requestPtr->hdr.command, PLDM_QUERY_DEVICE_IDENTIFIERS);
|
| 22 | }
|
gokulsanker | eca3e19 | 2021-04-05 14:57:41 +0530 | [diff] [blame] | 23 |
|
| 24 | TEST(QueryDeviceIdentifiers, goodPathDecodeResponse)
|
| 25 | {
|
| 26 | // descriptorDataLen is not fixed here taking it as 6
|
| 27 | constexpr uint8_t descriptorDataLen = 6;
|
| 28 | std::array<uint8_t, hdrSize +
|
| 29 | sizeof(struct pldm_query_device_identifiers_resp) +
|
| 30 | descriptorDataLen>
|
| 31 | responseMsg{};
|
| 32 | auto inResp = reinterpret_cast<struct pldm_query_device_identifiers_resp*>(
|
| 33 | responseMsg.data() + hdrSize);
|
| 34 |
|
| 35 | inResp->completion_code = PLDM_SUCCESS;
|
| 36 | inResp->device_identifiers_len = htole32(descriptorDataLen);
|
| 37 | inResp->descriptor_count = 1;
|
| 38 |
|
| 39 | // filling descriptor data
|
| 40 | std::fill_n(responseMsg.data() + hdrSize +
|
| 41 | sizeof(struct pldm_query_device_identifiers_resp),
|
| 42 | descriptorDataLen, 0xFF);
|
| 43 |
|
| 44 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
|
| 45 | uint8_t completionCode = PLDM_SUCCESS;
|
| 46 | uint32_t deviceIdentifiersLen = 0;
|
| 47 | uint8_t descriptorCount = 0;
|
| 48 | uint8_t* outDescriptorData = nullptr;
|
| 49 |
|
| 50 | auto rc = decode_query_device_identifiers_resp(
|
| 51 | response, responseMsg.size() - hdrSize, &completionCode,
|
| 52 | &deviceIdentifiersLen, &descriptorCount, &outDescriptorData);
|
| 53 |
|
| 54 | EXPECT_EQ(rc, PLDM_SUCCESS);
|
| 55 | EXPECT_EQ(completionCode, PLDM_SUCCESS);
|
| 56 | EXPECT_EQ(deviceIdentifiersLen, inResp->device_identifiers_len);
|
| 57 | EXPECT_EQ(descriptorCount, inResp->descriptor_count);
|
| 58 | EXPECT_EQ(true,
|
| 59 | std::equal(outDescriptorData,
|
| 60 | outDescriptorData + deviceIdentifiersLen,
|
| 61 | responseMsg.begin() + hdrSize +
|
| 62 | sizeof(struct pldm_query_device_identifiers_resp),
|
| 63 | responseMsg.end()));
|
| 64 | }
|
gokulsanker | 981fbfb | 2021-04-05 15:17:25 +0530 | [diff] [blame] | 65 |
|
| 66 | TEST(GetFirmwareParameters, goodPathEncodeRequest)
|
| 67 | {
|
| 68 | std::array<uint8_t, sizeof(pldm_msg_hdr)> requestMsg{};
|
| 69 | auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
|
| 70 | uint8_t instanceId = 0x01;
|
| 71 |
|
| 72 | auto rc = encode_get_firmware_parameters_req(
|
| 73 | instanceId, PLDM_GET_FIRMWARE_PARAMETERS_REQ_BYTES, requestPtr);
|
| 74 | EXPECT_EQ(rc, PLDM_SUCCESS);
|
| 75 | EXPECT_EQ(requestPtr->hdr.request, PLDM_REQUEST);
|
| 76 | EXPECT_EQ(requestPtr->hdr.instance_id, instanceId);
|
| 77 | EXPECT_EQ(requestPtr->hdr.type, PLDM_FWUP);
|
| 78 | EXPECT_EQ(requestPtr->hdr.command, PLDM_GET_FIRMWARE_PARAMETERS);
|
| 79 | }
|
gokulsanker | 22fbb34 | 2021-04-05 15:55:06 +0530 | [diff] [blame] | 80 |
|
| 81 | TEST(GetFirmwareParameters, goodPathDecodeResponseComponentSetInfo)
|
| 82 | {
|
| 83 | // Random value for capabilities during update
|
| 84 | constexpr uint32_t capabilitiesDuringUpdate = 0xBADBEEFE;
|
| 85 | // Random value for component count
|
| 86 | constexpr uint16_t componentCount = 0xAABB;
|
| 87 | // ActiveCompImageSetVerStrLen is not fixed here taking it as 8
|
| 88 | constexpr uint8_t activeCompImageSetVerStrLen = 8;
|
| 89 | // PendingCompImageSetVerStrLen is not fixed here taking it as 8
|
| 90 | constexpr uint8_t pendingCompImageSetVerStrLen = 8;
|
| 91 | constexpr size_t payloadLen =
|
| 92 | sizeof(struct pldm_get_firmware_parameters_resp) +
|
| 93 | activeCompImageSetVerStrLen + pendingCompImageSetVerStrLen;
|
| 94 |
|
| 95 | std::array<uint8_t, hdrSize + payloadLen> response{};
|
| 96 | auto inResp = reinterpret_cast<struct pldm_get_firmware_parameters_resp*>(
|
| 97 | response.data() + hdrSize);
|
| 98 | inResp->completion_code = PLDM_SUCCESS;
|
| 99 | inResp->capabilities_during_update.value =
|
| 100 | htole32(capabilitiesDuringUpdate);
|
| 101 | inResp->comp_count = htole16(componentCount);
|
| 102 | inResp->active_comp_image_set_ver_str_type = 1;
|
| 103 | inResp->active_comp_image_set_ver_str_len = activeCompImageSetVerStrLen;
|
| 104 | inResp->pending_comp_image_set_ver_str_type = 1;
|
| 105 | inResp->pending_comp_image_set_ver_str_len = pendingCompImageSetVerStrLen;
|
| 106 |
|
| 107 | constexpr size_t activeCompImageSetVerStrPos =
|
| 108 | hdrSize + sizeof(struct pldm_get_firmware_parameters_resp);
|
| 109 | // filling default values for ActiveComponentImageSetVersionString
|
| 110 | std::fill_n(response.data() + activeCompImageSetVerStrPos,
|
| 111 | activeCompImageSetVerStrLen, 0xFF);
|
| 112 | constexpr size_t pendingCompImageSetVerStrPos =
|
| 113 | hdrSize + sizeof(struct pldm_get_firmware_parameters_resp) +
|
| 114 | activeCompImageSetVerStrLen;
|
| 115 | // filling default values for ActiveComponentImageSetVersionString
|
| 116 | std::fill_n(response.data() + pendingCompImageSetVerStrPos,
|
| 117 | pendingCompImageSetVerStrLen, 0xFF);
|
| 118 |
|
| 119 | auto responseMsg = reinterpret_cast<pldm_msg*>(response.data());
|
| 120 | struct pldm_get_firmware_parameters_resp outResp;
|
| 121 | struct variable_field outActiveCompImageSetVerStr;
|
| 122 | struct variable_field outPendingCompImageSetVerStr;
|
| 123 |
|
| 124 | auto rc = decode_get_firmware_parameters_resp_comp_set_info(
|
| 125 | responseMsg, payloadLen, &outResp, &outActiveCompImageSetVerStr,
|
| 126 | &outPendingCompImageSetVerStr);
|
| 127 |
|
| 128 | EXPECT_EQ(rc, PLDM_SUCCESS);
|
| 129 | EXPECT_EQ(outResp.completion_code, PLDM_SUCCESS);
|
| 130 | EXPECT_EQ(outResp.capabilities_during_update.value,
|
| 131 | capabilitiesDuringUpdate);
|
| 132 | EXPECT_EQ(outResp.comp_count, componentCount);
|
| 133 | EXPECT_EQ(inResp->active_comp_image_set_ver_str_type,
|
| 134 | outResp.active_comp_image_set_ver_str_type);
|
| 135 | EXPECT_EQ(inResp->active_comp_image_set_ver_str_len,
|
| 136 | outResp.active_comp_image_set_ver_str_len);
|
| 137 | EXPECT_EQ(0, memcmp(outActiveCompImageSetVerStr.ptr,
|
| 138 | response.data() + activeCompImageSetVerStrPos,
|
| 139 | outActiveCompImageSetVerStr.length));
|
| 140 |
|
| 141 | EXPECT_EQ(inResp->pending_comp_image_set_ver_str_type,
|
| 142 | outResp.pending_comp_image_set_ver_str_type);
|
| 143 | EXPECT_EQ(inResp->pending_comp_image_set_ver_str_len,
|
| 144 | outResp.pending_comp_image_set_ver_str_len);
|
| 145 | EXPECT_EQ(0, memcmp(outPendingCompImageSetVerStr.ptr,
|
| 146 | response.data() + pendingCompImageSetVerStrPos,
|
| 147 | outPendingCompImageSetVerStr.length));
|
| 148 | }
|
gokulsanker | e1fb7a8 | 2021-04-05 16:09:29 +0530 | [diff] [blame] | 149 |
|
| 150 | TEST(GetFirmwareParameters, goodPathDecodeComponentParameterEntry)
|
| 151 | {
|
| 152 | // Random value for component classification
|
| 153 | constexpr uint16_t compClassification = 0x0A0B;
|
| 154 | // Random value for component classification
|
| 155 | constexpr uint16_t compIdentifier = 0x0C0D;
|
| 156 | // Random value for component classification
|
| 157 | constexpr uint32_t timestamp = 0X12345678;
|
| 158 | // Random value for component activation methods
|
| 159 | constexpr uint16_t compActivationMethods = 0xBBDD;
|
| 160 | // Random value for capabilities during update
|
| 161 | constexpr uint32_t capabilitiesDuringUpdate = 0xBADBEEFE;
|
| 162 |
|
| 163 | // ActiveCompImageSetVerStrLen is not fixed here taking it as 8
|
| 164 | constexpr uint8_t activeCompVerStrLen = 8;
|
| 165 | // PendingCompImageSetVerStrLen is not fixed here taking it as 8
|
| 166 | constexpr uint8_t pendingCompVerStrLen = 8;
|
| 167 | constexpr size_t entryLength =
|
| 168 | sizeof(struct pldm_component_parameter_entry) + activeCompVerStrLen +
|
| 169 | pendingCompVerStrLen;
|
| 170 | std::array<uint8_t, entryLength> entry{};
|
| 171 |
|
| 172 | auto inEntry =
|
| 173 | reinterpret_cast<struct pldm_component_parameter_entry*>(entry.data());
|
| 174 |
|
| 175 | inEntry->comp_classification = htole16(compClassification);
|
| 176 | inEntry->comp_identifier = htole16(compIdentifier);
|
| 177 | inEntry->comp_classification_index = 0x0F;
|
| 178 | inEntry->active_comp_comparison_stamp = htole32(timestamp);
|
| 179 | inEntry->active_comp_ver_str_type = 1;
|
| 180 | inEntry->active_comp_ver_str_len = activeCompVerStrLen;
|
| 181 | std::fill_n(inEntry->active_comp_release_date,
|
| 182 | sizeof(inEntry->active_comp_release_date), 0xFF);
|
| 183 | inEntry->pending_comp_comparison_stamp = htole32(timestamp);
|
| 184 | inEntry->pending_comp_ver_str_type = 1;
|
| 185 | inEntry->pending_comp_ver_str_len = pendingCompVerStrLen;
|
| 186 | std::fill_n(inEntry->pending_comp_release_date,
|
| 187 | sizeof(inEntry->pending_comp_release_date), 0xFF);
|
| 188 | inEntry->comp_activation_methods.value = htole16(compActivationMethods);
|
| 189 | inEntry->capabilities_during_update.value =
|
| 190 | htole32(capabilitiesDuringUpdate);
|
| 191 | constexpr auto activeCompVerStrPos =
|
| 192 | sizeof(struct pldm_component_parameter_entry);
|
| 193 | std::fill_n(entry.data() + activeCompVerStrPos, activeCompVerStrLen, 0xAA);
|
| 194 | constexpr auto pendingCompVerStrPos =
|
| 195 | activeCompVerStrPos + activeCompVerStrLen;
|
| 196 | std::fill_n(entry.data() + pendingCompVerStrPos, pendingCompVerStrLen,
|
| 197 | 0xBB);
|
| 198 |
|
| 199 | struct pldm_component_parameter_entry outEntry;
|
| 200 | struct variable_field outActiveCompVerStr;
|
| 201 | struct variable_field outPendingCompVerStr;
|
| 202 |
|
| 203 | auto rc = decode_get_firmware_parameters_resp_comp_entry(
|
| 204 | entry.data(), entryLength, &outEntry, &outActiveCompVerStr,
|
| 205 | &outPendingCompVerStr);
|
| 206 |
|
| 207 | EXPECT_EQ(rc, PLDM_SUCCESS);
|
| 208 |
|
| 209 | EXPECT_EQ(outEntry.comp_classification, compClassification);
|
| 210 | EXPECT_EQ(outEntry.comp_identifier, compIdentifier);
|
| 211 | EXPECT_EQ(inEntry->comp_classification_index,
|
| 212 | outEntry.comp_classification_index);
|
| 213 | EXPECT_EQ(outEntry.active_comp_comparison_stamp, timestamp);
|
| 214 | EXPECT_EQ(inEntry->active_comp_ver_str_type,
|
| 215 | outEntry.active_comp_ver_str_type);
|
| 216 | EXPECT_EQ(inEntry->active_comp_ver_str_len,
|
| 217 | outEntry.active_comp_ver_str_len);
|
| 218 | EXPECT_EQ(0, memcmp(inEntry->active_comp_release_date,
|
| 219 | outEntry.active_comp_release_date,
|
| 220 | sizeof(inEntry->active_comp_release_date)));
|
| 221 | EXPECT_EQ(outEntry.pending_comp_comparison_stamp, timestamp);
|
| 222 | EXPECT_EQ(inEntry->pending_comp_ver_str_type,
|
| 223 | outEntry.pending_comp_ver_str_type);
|
| 224 | EXPECT_EQ(inEntry->pending_comp_ver_str_len,
|
| 225 | outEntry.pending_comp_ver_str_len);
|
| 226 | EXPECT_EQ(0, memcmp(inEntry->pending_comp_release_date,
|
| 227 | outEntry.pending_comp_release_date,
|
| 228 | sizeof(inEntry->pending_comp_release_date)));
|
| 229 | EXPECT_EQ(outEntry.comp_activation_methods.value, compActivationMethods);
|
| 230 | EXPECT_EQ(outEntry.capabilities_during_update.value,
|
| 231 | capabilitiesDuringUpdate);
|
| 232 |
|
| 233 | EXPECT_EQ(0, memcmp(outActiveCompVerStr.ptr,
|
| 234 | entry.data() + activeCompVerStrPos,
|
| 235 | outActiveCompVerStr.length));
|
| 236 | EXPECT_EQ(0, memcmp(outPendingCompVerStr.ptr,
|
| 237 | entry.data() + pendingCompVerStrPos,
|
| 238 | outPendingCompVerStr.length));
|
| 239 | }
|