blob: b02e8ca1a7e833c4eb710503d6eebf8290fd2edc [file] [log] [blame]
gokulsanker138ceba2021-04-05 13:25:25 +05301#include "libpldm/base.h"
2#include "libpldm/firmware_update.h"
3
4#include <gtest/gtest.h>
5
6constexpr auto hdrSize = sizeof(pldm_msg_hdr);
7
8TEST(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}
gokulsankereca3e192021-04-05 14:57:41 +053023
24TEST(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}
gokulsanker981fbfb2021-04-05 15:17:25 +053065
66TEST(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}
gokulsanker22fbb342021-04-05 15:55:06 +053080
81TEST(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}