blob: ffa76de02bccf6d9478eef1d7a99725450f53738 [file] [log] [blame]
Deepak Kodihalli1b24f972019-02-01 04:09:13 -06001#include <string.h>
2
3#include <array>
John Wang7f02d702019-12-03 13:38:14 +08004#include <cstring>
5#include <vector>
Deepak Kodihalli1b24f972019-02-01 04:09:13 -06006
7#include "libpldm/base.h"
8
John Wang7f02d702019-12-03 13:38:14 +08009#include <gmock/gmock.h>
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060010#include <gtest/gtest.h>
11
John Wang7f02d702019-12-03 13:38:14 +080012using testing::ElementsAreArray;
13
Zahed Hossain223a73d2019-07-04 12:46:18 -050014constexpr auto hdrSize = sizeof(pldm_msg_hdr);
15
Tom Joseph41251042019-02-07 16:17:07 +053016TEST(PackPLDMMessage, BadPathTest)
17{
18 struct pldm_header_info hdr;
19 struct pldm_header_info* hdr_ptr = NULL;
20 pldm_msg_hdr msg{};
21
22 // PLDM header information pointer is NULL
23 auto rc = pack_pldm_header(hdr_ptr, &msg);
24 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
25
26 // PLDM message pointer is NULL
27 rc = pack_pldm_header(&hdr, nullptr);
28 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
29
30 // PLDM header information pointer and PLDM message pointer is NULL
31 rc = pack_pldm_header(hdr_ptr, nullptr);
32 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
33
34 // RESERVED message type
35 hdr.msg_type = PLDM_RESERVED;
36 rc = pack_pldm_header(&hdr, &msg);
37 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
38
39 // Instance ID out of range
40 hdr.msg_type = PLDM_REQUEST;
41 hdr.instance = 32;
42 rc = pack_pldm_header(&hdr, &msg);
43 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
44
45 // PLDM type out of range
46 hdr.msg_type = PLDM_REQUEST;
47 hdr.instance = 31;
48 hdr.pldm_type = 64;
49 rc = pack_pldm_header(&hdr, &msg);
50 ASSERT_EQ(rc, PLDM_ERROR_INVALID_PLDM_TYPE);
51}
52
53TEST(PackPLDMMessage, RequestMessageGoodPath)
54{
55 struct pldm_header_info hdr;
56 pldm_msg_hdr msg{};
57
58 // Message type is REQUEST and lower range of the field values
59 hdr.msg_type = PLDM_REQUEST;
60 hdr.instance = 0;
61 hdr.pldm_type = 0;
62 hdr.command = 0;
63
64 auto rc = pack_pldm_header(&hdr, &msg);
65 ASSERT_EQ(rc, PLDM_SUCCESS);
66 ASSERT_EQ(msg.request, 1);
67 ASSERT_EQ(msg.datagram, 0);
68 ASSERT_EQ(msg.instance_id, 0);
69 ASSERT_EQ(msg.type, 0);
70 ASSERT_EQ(msg.command, 0);
71
72 // Message type is REQUEST and upper range of the field values
73 hdr.instance = 31;
74 hdr.pldm_type = 63;
75 hdr.command = 255;
76
77 rc = pack_pldm_header(&hdr, &msg);
78 ASSERT_EQ(rc, PLDM_SUCCESS);
79 ASSERT_EQ(msg.request, 1);
80 ASSERT_EQ(msg.datagram, 0);
81 ASSERT_EQ(msg.instance_id, 31);
82 ASSERT_EQ(msg.type, 63);
83 ASSERT_EQ(msg.command, 255);
84
85 // Message type is PLDM_ASYNC_REQUEST_NOTIFY
86 hdr.msg_type = PLDM_ASYNC_REQUEST_NOTIFY;
87
88 rc = pack_pldm_header(&hdr, &msg);
89 ASSERT_EQ(rc, PLDM_SUCCESS);
90 ASSERT_EQ(msg.request, 1);
91 ASSERT_EQ(msg.datagram, 1);
92 ASSERT_EQ(msg.instance_id, 31);
93 ASSERT_EQ(msg.type, 63);
94 ASSERT_EQ(msg.command, 255);
95}
96
97TEST(PackPLDMMessage, ResponseMessageGoodPath)
98{
99 struct pldm_header_info hdr;
100 pldm_msg_hdr msg{};
101
102 // Message type is PLDM_RESPONSE and lower range of the field values
103 hdr.msg_type = PLDM_RESPONSE;
104 hdr.instance = 0;
105 hdr.pldm_type = 0;
106 hdr.command = 0;
107
108 auto rc = pack_pldm_header(&hdr, &msg);
109 ASSERT_EQ(rc, PLDM_SUCCESS);
110 ASSERT_EQ(msg.request, 0);
111 ASSERT_EQ(msg.datagram, 0);
112 ASSERT_EQ(msg.instance_id, 0);
113 ASSERT_EQ(msg.type, 0);
114 ASSERT_EQ(msg.command, 0);
115
116 // Message type is PLDM_RESPONSE and upper range of the field values
117 hdr.instance = 31;
118 hdr.pldm_type = 63;
119 hdr.command = 255;
120
121 rc = pack_pldm_header(&hdr, &msg);
122 ASSERT_EQ(rc, PLDM_SUCCESS);
123 ASSERT_EQ(msg.request, 0);
124 ASSERT_EQ(msg.datagram, 0);
125 ASSERT_EQ(msg.instance_id, 31);
126 ASSERT_EQ(msg.type, 63);
127 ASSERT_EQ(msg.command, 255);
128}
129
130TEST(UnpackPLDMMessage, BadPathTest)
131{
132 struct pldm_header_info hdr;
133
134 // PLDM message pointer is NULL
135 auto rc = unpack_pldm_header(nullptr, &hdr);
136 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
137}
138
139TEST(UnpackPLDMMessage, RequestMessageGoodPath)
140{
141 struct pldm_header_info hdr;
142 pldm_msg_hdr msg{};
143
144 // Unpack PLDM request message and lower range of field values
145 msg.request = 1;
146 auto rc = unpack_pldm_header(&msg, &hdr);
147 ASSERT_EQ(rc, PLDM_SUCCESS);
148 ASSERT_EQ(hdr.msg_type, PLDM_REQUEST);
149 ASSERT_EQ(hdr.instance, 0);
150 ASSERT_EQ(hdr.pldm_type, 0);
151 ASSERT_EQ(hdr.command, 0);
152
153 // Unpack PLDM async request message and lower range of field values
154 msg.datagram = 1;
155 rc = unpack_pldm_header(&msg, &hdr);
156 ASSERT_EQ(rc, PLDM_SUCCESS);
157 ASSERT_EQ(hdr.msg_type, PLDM_ASYNC_REQUEST_NOTIFY);
158
159 // Unpack PLDM request message and upper range of field values
160 msg.datagram = 0;
161 msg.instance_id = 31;
162 msg.type = 63;
163 msg.command = 255;
164 rc = unpack_pldm_header(&msg, &hdr);
165 ASSERT_EQ(rc, PLDM_SUCCESS);
166 ASSERT_EQ(hdr.msg_type, PLDM_REQUEST);
167 ASSERT_EQ(hdr.instance, 31);
168 ASSERT_EQ(hdr.pldm_type, 63);
169 ASSERT_EQ(hdr.command, 255);
170}
171
172TEST(UnpackPLDMMessage, ResponseMessageGoodPath)
173{
174 struct pldm_header_info hdr;
175 pldm_msg_hdr msg{};
176
177 // Unpack PLDM response message and lower range of field values
178 auto rc = unpack_pldm_header(&msg, &hdr);
179 ASSERT_EQ(rc, PLDM_SUCCESS);
180 ASSERT_EQ(hdr.msg_type, PLDM_RESPONSE);
181 ASSERT_EQ(hdr.instance, 0);
182 ASSERT_EQ(hdr.pldm_type, 0);
183 ASSERT_EQ(hdr.command, 0);
184
185 // Unpack PLDM response message and upper range of field values
186 msg.instance_id = 31;
187 msg.type = 63;
188 msg.command = 255;
189 rc = unpack_pldm_header(&msg, &hdr);
190 ASSERT_EQ(rc, PLDM_SUCCESS);
191 ASSERT_EQ(hdr.msg_type, PLDM_RESPONSE);
192 ASSERT_EQ(hdr.instance, 31);
193 ASSERT_EQ(hdr.pldm_type, 63);
194 ASSERT_EQ(hdr.command, 255);
195}
196
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600197TEST(GetPLDMCommands, testEncodeRequest)
198{
199 uint8_t pldmType = 0x05;
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600200 ver32_t version{0xFF, 0xFF, 0xFF, 0xFF};
vkaverapa6575b82019-04-03 05:33:52 -0500201 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_REQ_BYTES>
202 requestMsg{};
203 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600204
vkaverapa6575b82019-04-03 05:33:52 -0500205 auto rc = encode_get_commands_req(0, pldmType, version, request);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600206 ASSERT_EQ(rc, PLDM_SUCCESS);
vkaverapa6575b82019-04-03 05:33:52 -0500207 ASSERT_EQ(0, memcmp(request->payload, &pldmType, sizeof(pldmType)));
208 ASSERT_EQ(0, memcmp(request->payload + sizeof(pldmType), &version,
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600209 sizeof(version)));
210}
211
212TEST(GetPLDMCommands, testDecodeRequest)
213{
214 uint8_t pldmType = 0x05;
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600215 ver32_t version{0xFF, 0xFF, 0xFF, 0xFF};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600216 uint8_t pldmTypeOut{};
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600217 ver32_t versionOut{0xFF, 0xFF, 0xFF, 0xFF};
Zahed Hossain223a73d2019-07-04 12:46:18 -0500218 std::array<uint8_t, hdrSize + PLDM_GET_COMMANDS_REQ_BYTES> requestMsg{};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600219
Zahed Hossain223a73d2019-07-04 12:46:18 -0500220 memcpy(requestMsg.data() + hdrSize, &pldmType, sizeof(pldmType));
221 memcpy(requestMsg.data() + sizeof(pldmType) + hdrSize, &version,
222 sizeof(version));
223
224 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
225 auto rc = decode_get_commands_req(request, requestMsg.size() - hdrSize,
vkaverapa6575b82019-04-03 05:33:52 -0500226 &pldmTypeOut, &versionOut);
Zahed Hossain223a73d2019-07-04 12:46:18 -0500227
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600228 ASSERT_EQ(rc, PLDM_SUCCESS);
229 ASSERT_EQ(pldmTypeOut, pldmType);
230 ASSERT_EQ(0, memcmp(&versionOut, &version, sizeof(version)));
231}
232
233TEST(GetPLDMCommands, testEncodeResponse)
234{
235 uint8_t completionCode = 0;
vkaverapa6575b82019-04-03 05:33:52 -0500236 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_RESP_BYTES>
237 responseMsg{};
238 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600239 std::array<bitfield8_t, PLDM_MAX_CMDS_PER_TYPE / 8> commands{};
240 commands[0].byte = 1;
241 commands[1].byte = 2;
242 commands[2].byte = 3;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600243
244 auto rc =
vkaverapa6575b82019-04-03 05:33:52 -0500245 encode_get_commands_resp(0, PLDM_SUCCESS, commands.data(), response);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600246 ASSERT_EQ(rc, PLDM_SUCCESS);
vkaverapa6575b82019-04-03 05:33:52 -0500247 uint8_t* payload_ptr = response->payload;
248 ASSERT_EQ(completionCode, payload_ptr[0]);
249 ASSERT_EQ(1, payload_ptr[sizeof(completionCode)]);
250 ASSERT_EQ(2,
251 payload_ptr[sizeof(completionCode) + sizeof(commands[0].byte)]);
252 ASSERT_EQ(3, payload_ptr[sizeof(completionCode) + sizeof(commands[0].byte) +
253 sizeof(commands[1].byte)]);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600254}
255
256TEST(GetPLDMTypes, testEncodeResponse)
257{
258 uint8_t completionCode = 0;
vkaverapa6575b82019-04-03 05:33:52 -0500259 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_TYPES_RESP_BYTES>
260 responseMsg{};
261 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600262 std::array<bitfield8_t, PLDM_MAX_TYPES / 8> types{};
263 types[0].byte = 1;
264 types[1].byte = 2;
265 types[2].byte = 3;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600266
vkaverapa6575b82019-04-03 05:33:52 -0500267 auto rc = encode_get_types_resp(0, PLDM_SUCCESS, types.data(), response);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600268 ASSERT_EQ(rc, PLDM_SUCCESS);
vkaverapa6575b82019-04-03 05:33:52 -0500269 uint8_t* payload_ptr = response->payload;
270 ASSERT_EQ(completionCode, payload_ptr[0]);
271 ASSERT_EQ(1, payload_ptr[sizeof(completionCode)]);
272 ASSERT_EQ(2, payload_ptr[sizeof(completionCode) + sizeof(types[0].byte)]);
273 ASSERT_EQ(3, payload_ptr[sizeof(completionCode) + sizeof(types[0].byte) +
274 sizeof(types[1].byte)]);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600275}
276
277TEST(GetPLDMTypes, testDecodeResponse)
278{
Zahed Hossain223a73d2019-07-04 12:46:18 -0500279 std::array<uint8_t, hdrSize + PLDM_GET_TYPES_RESP_BYTES> responseMsg{};
280 responseMsg[1 + hdrSize] = 1;
281 responseMsg[2 + hdrSize] = 2;
282 responseMsg[3 + hdrSize] = 3;
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600283 std::array<bitfield8_t, PLDM_MAX_TYPES / 8> outTypes{};
Deepak Kodihalli8c643462019-02-21 10:43:36 -0600284 uint8_t completion_code;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600285
Zahed Hossain223a73d2019-07-04 12:46:18 -0500286 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
287
288 auto rc = decode_get_types_resp(response, responseMsg.size() - hdrSize,
vkaverapa6575b82019-04-03 05:33:52 -0500289 &completion_code, outTypes.data());
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600290
291 ASSERT_EQ(rc, PLDM_SUCCESS);
Deepak Kodihalli8c643462019-02-21 10:43:36 -0600292 ASSERT_EQ(completion_code, PLDM_SUCCESS);
Zahed Hossain223a73d2019-07-04 12:46:18 -0500293 ASSERT_EQ(responseMsg[1 + hdrSize], outTypes[0].byte);
294 ASSERT_EQ(responseMsg[2 + hdrSize], outTypes[1].byte);
295 ASSERT_EQ(responseMsg[3 + hdrSize], outTypes[2].byte);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600296}
297
298TEST(GetPLDMCommands, testDecodeResponse)
299{
Zahed Hossain223a73d2019-07-04 12:46:18 -0500300 std::array<uint8_t, hdrSize + PLDM_GET_COMMANDS_RESP_BYTES> responseMsg{};
301 responseMsg[1 + hdrSize] = 1;
302 responseMsg[2 + hdrSize] = 2;
303 responseMsg[3 + hdrSize] = 3;
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600304 std::array<bitfield8_t, PLDM_MAX_CMDS_PER_TYPE / 8> outTypes{};
Deepak Kodihalli8c643462019-02-21 10:43:36 -0600305 uint8_t completion_code;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600306
Zahed Hossain223a73d2019-07-04 12:46:18 -0500307 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
308
309 auto rc = decode_get_commands_resp(response, responseMsg.size() - hdrSize,
vkaverapa6575b82019-04-03 05:33:52 -0500310 &completion_code, outTypes.data());
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600311
312 ASSERT_EQ(rc, PLDM_SUCCESS);
Deepak Kodihalli8c643462019-02-21 10:43:36 -0600313 ASSERT_EQ(completion_code, PLDM_SUCCESS);
Zahed Hossain223a73d2019-07-04 12:46:18 -0500314 ASSERT_EQ(responseMsg[1 + hdrSize], outTypes[0].byte);
315 ASSERT_EQ(responseMsg[2 + hdrSize], outTypes[1].byte);
316 ASSERT_EQ(responseMsg[3 + hdrSize], outTypes[2].byte);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600317}
Sampa Misra432e1872019-02-13 03:49:43 -0600318
Sampa Misra94d704b2019-02-22 03:36:02 -0600319TEST(GetPLDMVersion, testGoodEncodeRequest)
Sampa Misra432e1872019-02-13 03:49:43 -0600320{
vkaverapa6575b82019-04-03 05:33:52 -0500321 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_REQ_BYTES>
322 requestMsg{};
323 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
Sampa Misra432e1872019-02-13 03:49:43 -0600324 uint8_t pldmType = 0x03;
325 uint32_t transferHandle = 0x0;
326 uint8_t opFlag = 0x01;
327
328 auto rc =
vkaverapa6575b82019-04-03 05:33:52 -0500329 encode_get_version_req(0, transferHandle, opFlag, pldmType, request);
Sampa Misra432e1872019-02-13 03:49:43 -0600330 ASSERT_EQ(rc, PLDM_SUCCESS);
vkaverapa6575b82019-04-03 05:33:52 -0500331 ASSERT_EQ(
332 0, memcmp(request->payload, &transferHandle, sizeof(transferHandle)));
333 ASSERT_EQ(0, memcmp(request->payload + sizeof(transferHandle), &opFlag,
Sampa Misra432e1872019-02-13 03:49:43 -0600334 sizeof(opFlag)));
vkaverapa6575b82019-04-03 05:33:52 -0500335 ASSERT_EQ(0,
336 memcmp(request->payload + sizeof(transferHandle) + sizeof(opFlag),
337 &pldmType, sizeof(pldmType)));
Sampa Misra432e1872019-02-13 03:49:43 -0600338}
339
Sampa Misra94d704b2019-02-22 03:36:02 -0600340TEST(GetPLDMVersion, testBadEncodeRequest)
341{
342 uint8_t pldmType = 0x03;
343 uint32_t transferHandle = 0x0;
344 uint8_t opFlag = 0x01;
345
346 auto rc =
347 encode_get_version_req(0, transferHandle, opFlag, pldmType, nullptr);
348
349 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
350}
351
Sampa Misra432e1872019-02-13 03:49:43 -0600352TEST(GetPLDMVersion, testEncodeResponse)
353{
Sampa Misra432e1872019-02-13 03:49:43 -0600354 uint8_t completionCode = 0;
355 uint32_t transferHandle = 0;
356 uint8_t flag = PLDM_START_AND_END;
vkaverapa6575b82019-04-03 05:33:52 -0500357 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_RESP_BYTES>
358 responseMsg{};
359 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600360 ver32_t version = {0xFF, 0xFF, 0xFF, 0xFF};
Sampa Misra432e1872019-02-13 03:49:43 -0600361
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600362 auto rc = encode_get_version_resp(0, PLDM_SUCCESS, 0, PLDM_START_AND_END,
vkaverapa6575b82019-04-03 05:33:52 -0500363 &version, sizeof(ver32_t), response);
Sampa Misra432e1872019-02-13 03:49:43 -0600364
365 ASSERT_EQ(rc, PLDM_SUCCESS);
vkaverapa6575b82019-04-03 05:33:52 -0500366 ASSERT_EQ(completionCode, response->payload[0]);
367 ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]),
368 &transferHandle, sizeof(transferHandle)));
369 ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]) +
370 sizeof(transferHandle),
371 &flag, sizeof(flag)));
372 ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]) +
373 sizeof(transferHandle) + sizeof(flag),
374 &version, sizeof(version)));
Sampa Misra432e1872019-02-13 03:49:43 -0600375}
376
377TEST(GetPLDMVersion, testDecodeRequest)
378{
Zahed Hossain223a73d2019-07-04 12:46:18 -0500379 std::array<uint8_t, hdrSize + PLDM_GET_VERSION_REQ_BYTES> requestMsg{};
Sampa Misra432e1872019-02-13 03:49:43 -0600380 uint32_t transferHandle = 0x0;
381 uint32_t retTransferHandle = 0x0;
382 uint8_t flag = PLDM_GET_FIRSTPART;
383 uint8_t retFlag = PLDM_GET_FIRSTPART;
384 uint8_t pldmType = PLDM_BASE;
385 uint8_t retType = PLDM_BASE;
386
Zahed Hossain223a73d2019-07-04 12:46:18 -0500387 memcpy(requestMsg.data() + hdrSize, &transferHandle,
388 sizeof(transferHandle));
389 memcpy(requestMsg.data() + sizeof(transferHandle) + hdrSize, &flag,
390 sizeof(flag));
391 memcpy(requestMsg.data() + sizeof(transferHandle) + sizeof(flag) + hdrSize,
392 &pldmType, sizeof(pldmType));
Sampa Misra432e1872019-02-13 03:49:43 -0600393
Zahed Hossain223a73d2019-07-04 12:46:18 -0500394 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
395
396 auto rc = decode_get_version_req(request, requestMsg.size() - hdrSize,
vkaverapa6575b82019-04-03 05:33:52 -0500397 &retTransferHandle, &retFlag, &retType);
Sampa Misra432e1872019-02-13 03:49:43 -0600398
399 ASSERT_EQ(rc, PLDM_SUCCESS);
400 ASSERT_EQ(transferHandle, retTransferHandle);
401 ASSERT_EQ(flag, retFlag);
402 ASSERT_EQ(pldmType, retType);
403}
404
405TEST(GetPLDMVersion, testDecodeResponse)
406{
Zahed Hossain223a73d2019-07-04 12:46:18 -0500407 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_RESP_BYTES>
408 responseMsg{};
Sampa Misra432e1872019-02-13 03:49:43 -0600409 uint32_t transferHandle = 0x0;
410 uint32_t retTransferHandle = 0x0;
411 uint8_t flag = PLDM_START_AND_END;
412 uint8_t retFlag = PLDM_START_AND_END;
413 uint8_t completionCode = 0;
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600414 ver32_t version = {0xFF, 0xFF, 0xFF, 0xFF};
415 ver32_t versionOut;
Deepak Kodihalli8c643462019-02-21 10:43:36 -0600416 uint8_t completion_code;
Sampa Misra432e1872019-02-13 03:49:43 -0600417
Zahed Hossain223a73d2019-07-04 12:46:18 -0500418 memcpy(responseMsg.data() + sizeof(completionCode) + hdrSize,
419 &transferHandle, sizeof(transferHandle));
420 memcpy(responseMsg.data() + sizeof(completionCode) +
421 sizeof(transferHandle) + hdrSize,
Sampa Misra432e1872019-02-13 03:49:43 -0600422 &flag, sizeof(flag));
vkaverapa6575b82019-04-03 05:33:52 -0500423 memcpy(responseMsg.data() + sizeof(completionCode) +
Zahed Hossain223a73d2019-07-04 12:46:18 -0500424 sizeof(transferHandle) + sizeof(flag) + hdrSize,
Sampa Misra432e1872019-02-13 03:49:43 -0600425 &version, sizeof(version));
426
Zahed Hossain223a73d2019-07-04 12:46:18 -0500427 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
428
429 auto rc = decode_get_version_resp(response, responseMsg.size() - hdrSize,
vkaverapa6575b82019-04-03 05:33:52 -0500430 &completion_code, &retTransferHandle,
431 &retFlag, &versionOut);
Sampa Misra432e1872019-02-13 03:49:43 -0600432 ASSERT_EQ(rc, PLDM_SUCCESS);
433 ASSERT_EQ(transferHandle, retTransferHandle);
434 ASSERT_EQ(flag, retFlag);
435
436 ASSERT_EQ(versionOut.major, version.major);
437 ASSERT_EQ(versionOut.minor, version.minor);
438 ASSERT_EQ(versionOut.update, version.update);
439 ASSERT_EQ(versionOut.alpha, version.alpha);
440}
John Wang5c4f80d2019-07-29 11:12:18 +0800441
Sridevi Rameshbc6ff262019-12-12 04:58:35 -0600442TEST(GetTID, testEncodeRequest)
443{
444 pldm_msg request{};
445
446 auto rc = encode_get_tid_req(0, &request);
447 ASSERT_EQ(rc, PLDM_SUCCESS);
448}
449
John Wang5c4f80d2019-07-29 11:12:18 +0800450TEST(GetTID, testEncodeResponse)
451{
452 uint8_t completionCode = 0;
453 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_TID_RESP_BYTES>
454 responseMsg{};
455 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
456 uint8_t tid = 1;
457
458 auto rc = encode_get_tid_resp(0, PLDM_SUCCESS, tid, response);
459 ASSERT_EQ(rc, PLDM_SUCCESS);
460 uint8_t* payload = response->payload;
461 ASSERT_EQ(completionCode, payload[0]);
462 ASSERT_EQ(1, payload[sizeof(completionCode)]);
463}
464
465TEST(GetTID, testDecodeResponse)
466{
467 std::array<uint8_t, hdrSize + PLDM_GET_TID_RESP_BYTES> responseMsg{};
468 responseMsg[1 + hdrSize] = 1;
469
470 uint8_t tid;
471 uint8_t completion_code;
472
473 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
474
475 auto rc = decode_get_tid_resp(response, responseMsg.size() - hdrSize,
476 &completion_code, &tid);
477
478 ASSERT_EQ(rc, PLDM_SUCCESS);
479 ASSERT_EQ(completion_code, PLDM_SUCCESS);
480 ASSERT_EQ(tid, 1);
481}
John Wang7f02d702019-12-03 13:38:14 +0800482
483TEST(CcOnlyResponse, testEncode)
484{
485 struct pldm_msg responseMsg;
486
487 auto rc =
488 encode_cc_only_resp(0 /*instance id*/, 1 /*pldm type*/, 2 /*command*/,
489 3 /*complection code*/, &responseMsg);
490 EXPECT_EQ(rc, PLDM_SUCCESS);
491
492 auto p = reinterpret_cast<uint8_t*>(&responseMsg);
493 EXPECT_THAT(std::vector<uint8_t>(p, p + sizeof(responseMsg)),
494 ElementsAreArray({0, 1, 2, 3}));
495
496 rc = encode_cc_only_resp(PLDM_INSTANCE_MAX + 1, 1, 2, 3, &responseMsg);
497 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
498
499 rc = encode_cc_only_resp(0, 1, 2, 3, nullptr);
500 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
501}