blob: f906f776ef7f51972987bb21f55215fa4db7cf96 [file] [log] [blame]
Deepak Kodihalli1b24f972019-02-01 04:09:13 -06001#include <string.h>
2
3#include <array>
4
5#include "libpldm/base.h"
6
7#include <gtest/gtest.h>
8
Tom Joseph41251042019-02-07 16:17:07 +05309TEST(PackPLDMMessage, BadPathTest)
10{
11 struct pldm_header_info hdr;
12 struct pldm_header_info* hdr_ptr = NULL;
13 pldm_msg_hdr msg{};
14
15 // PLDM header information pointer is NULL
16 auto rc = pack_pldm_header(hdr_ptr, &msg);
17 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
18
19 // PLDM message pointer is NULL
20 rc = pack_pldm_header(&hdr, nullptr);
21 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
22
23 // PLDM header information pointer and PLDM message pointer is NULL
24 rc = pack_pldm_header(hdr_ptr, nullptr);
25 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
26
27 // RESERVED message type
28 hdr.msg_type = PLDM_RESERVED;
29 rc = pack_pldm_header(&hdr, &msg);
30 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
31
32 // Instance ID out of range
33 hdr.msg_type = PLDM_REQUEST;
34 hdr.instance = 32;
35 rc = pack_pldm_header(&hdr, &msg);
36 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
37
38 // PLDM type out of range
39 hdr.msg_type = PLDM_REQUEST;
40 hdr.instance = 31;
41 hdr.pldm_type = 64;
42 rc = pack_pldm_header(&hdr, &msg);
43 ASSERT_EQ(rc, PLDM_ERROR_INVALID_PLDM_TYPE);
44}
45
46TEST(PackPLDMMessage, RequestMessageGoodPath)
47{
48 struct pldm_header_info hdr;
49 pldm_msg_hdr msg{};
50
51 // Message type is REQUEST and lower range of the field values
52 hdr.msg_type = PLDM_REQUEST;
53 hdr.instance = 0;
54 hdr.pldm_type = 0;
55 hdr.command = 0;
56
57 auto rc = pack_pldm_header(&hdr, &msg);
58 ASSERT_EQ(rc, PLDM_SUCCESS);
59 ASSERT_EQ(msg.request, 1);
60 ASSERT_EQ(msg.datagram, 0);
61 ASSERT_EQ(msg.instance_id, 0);
62 ASSERT_EQ(msg.type, 0);
63 ASSERT_EQ(msg.command, 0);
64
65 // Message type is REQUEST and upper range of the field values
66 hdr.instance = 31;
67 hdr.pldm_type = 63;
68 hdr.command = 255;
69
70 rc = pack_pldm_header(&hdr, &msg);
71 ASSERT_EQ(rc, PLDM_SUCCESS);
72 ASSERT_EQ(msg.request, 1);
73 ASSERT_EQ(msg.datagram, 0);
74 ASSERT_EQ(msg.instance_id, 31);
75 ASSERT_EQ(msg.type, 63);
76 ASSERT_EQ(msg.command, 255);
77
78 // Message type is PLDM_ASYNC_REQUEST_NOTIFY
79 hdr.msg_type = PLDM_ASYNC_REQUEST_NOTIFY;
80
81 rc = pack_pldm_header(&hdr, &msg);
82 ASSERT_EQ(rc, PLDM_SUCCESS);
83 ASSERT_EQ(msg.request, 1);
84 ASSERT_EQ(msg.datagram, 1);
85 ASSERT_EQ(msg.instance_id, 31);
86 ASSERT_EQ(msg.type, 63);
87 ASSERT_EQ(msg.command, 255);
88}
89
90TEST(PackPLDMMessage, ResponseMessageGoodPath)
91{
92 struct pldm_header_info hdr;
93 pldm_msg_hdr msg{};
94
95 // Message type is PLDM_RESPONSE and lower range of the field values
96 hdr.msg_type = PLDM_RESPONSE;
97 hdr.instance = 0;
98 hdr.pldm_type = 0;
99 hdr.command = 0;
100
101 auto rc = pack_pldm_header(&hdr, &msg);
102 ASSERT_EQ(rc, PLDM_SUCCESS);
103 ASSERT_EQ(msg.request, 0);
104 ASSERT_EQ(msg.datagram, 0);
105 ASSERT_EQ(msg.instance_id, 0);
106 ASSERT_EQ(msg.type, 0);
107 ASSERT_EQ(msg.command, 0);
108
109 // Message type is PLDM_RESPONSE and upper range of the field values
110 hdr.instance = 31;
111 hdr.pldm_type = 63;
112 hdr.command = 255;
113
114 rc = pack_pldm_header(&hdr, &msg);
115 ASSERT_EQ(rc, PLDM_SUCCESS);
116 ASSERT_EQ(msg.request, 0);
117 ASSERT_EQ(msg.datagram, 0);
118 ASSERT_EQ(msg.instance_id, 31);
119 ASSERT_EQ(msg.type, 63);
120 ASSERT_EQ(msg.command, 255);
121}
122
123TEST(UnpackPLDMMessage, BadPathTest)
124{
125 struct pldm_header_info hdr;
126
127 // PLDM message pointer is NULL
128 auto rc = unpack_pldm_header(nullptr, &hdr);
129 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
130}
131
132TEST(UnpackPLDMMessage, RequestMessageGoodPath)
133{
134 struct pldm_header_info hdr;
135 pldm_msg_hdr msg{};
136
137 // Unpack PLDM request message and lower range of field values
138 msg.request = 1;
139 auto rc = unpack_pldm_header(&msg, &hdr);
140 ASSERT_EQ(rc, PLDM_SUCCESS);
141 ASSERT_EQ(hdr.msg_type, PLDM_REQUEST);
142 ASSERT_EQ(hdr.instance, 0);
143 ASSERT_EQ(hdr.pldm_type, 0);
144 ASSERT_EQ(hdr.command, 0);
145
146 // Unpack PLDM async request message and lower range of field values
147 msg.datagram = 1;
148 rc = unpack_pldm_header(&msg, &hdr);
149 ASSERT_EQ(rc, PLDM_SUCCESS);
150 ASSERT_EQ(hdr.msg_type, PLDM_ASYNC_REQUEST_NOTIFY);
151
152 // Unpack PLDM request message and upper range of field values
153 msg.datagram = 0;
154 msg.instance_id = 31;
155 msg.type = 63;
156 msg.command = 255;
157 rc = unpack_pldm_header(&msg, &hdr);
158 ASSERT_EQ(rc, PLDM_SUCCESS);
159 ASSERT_EQ(hdr.msg_type, PLDM_REQUEST);
160 ASSERT_EQ(hdr.instance, 31);
161 ASSERT_EQ(hdr.pldm_type, 63);
162 ASSERT_EQ(hdr.command, 255);
163}
164
165TEST(UnpackPLDMMessage, ResponseMessageGoodPath)
166{
167 struct pldm_header_info hdr;
168 pldm_msg_hdr msg{};
169
170 // Unpack PLDM response message and lower range of field values
171 auto rc = unpack_pldm_header(&msg, &hdr);
172 ASSERT_EQ(rc, PLDM_SUCCESS);
173 ASSERT_EQ(hdr.msg_type, PLDM_RESPONSE);
174 ASSERT_EQ(hdr.instance, 0);
175 ASSERT_EQ(hdr.pldm_type, 0);
176 ASSERT_EQ(hdr.command, 0);
177
178 // Unpack PLDM response message and upper range of field values
179 msg.instance_id = 31;
180 msg.type = 63;
181 msg.command = 255;
182 rc = unpack_pldm_header(&msg, &hdr);
183 ASSERT_EQ(rc, PLDM_SUCCESS);
184 ASSERT_EQ(hdr.msg_type, PLDM_RESPONSE);
185 ASSERT_EQ(hdr.instance, 31);
186 ASSERT_EQ(hdr.pldm_type, 63);
187 ASSERT_EQ(hdr.command, 255);
188}
189
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600190TEST(GetPLDMCommands, testEncodeRequest)
191{
192 uint8_t pldmType = 0x05;
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600193 ver32_t version{0xFF, 0xFF, 0xFF, 0xFF};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600194 std::array<uint8_t, PLDM_GET_COMMANDS_REQ_BYTES> requestMsg{};
195 pldm_msg request{};
196 request.body.payload = requestMsg.data();
197 request.body.payload_length = requestMsg.size();
198
199 auto rc = encode_get_commands_req(0, pldmType, version, &request);
200 ASSERT_EQ(rc, PLDM_SUCCESS);
201 ASSERT_EQ(0, memcmp(request.body.payload, &pldmType, sizeof(pldmType)));
202 ASSERT_EQ(0, memcmp(request.body.payload + sizeof(pldmType), &version,
203 sizeof(version)));
204}
205
206TEST(GetPLDMCommands, testDecodeRequest)
207{
208 uint8_t pldmType = 0x05;
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600209 ver32_t version{0xFF, 0xFF, 0xFF, 0xFF};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600210 uint8_t pldmTypeOut{};
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600211 ver32_t versionOut{0xFF, 0xFF, 0xFF, 0xFF};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600212 std::array<uint8_t, PLDM_GET_COMMANDS_REQ_BYTES> requestMsg{};
213 pldm_msg_payload request{};
214 request.payload = requestMsg.data();
215 request.payload_length = requestMsg.size();
216
217 memcpy(request.payload, &pldmType, sizeof(pldmType));
218 memcpy(request.payload + sizeof(pldmType), &version, sizeof(version));
219 auto rc = decode_get_commands_req(&request, &pldmTypeOut, &versionOut);
220 ASSERT_EQ(rc, PLDM_SUCCESS);
221 ASSERT_EQ(pldmTypeOut, pldmType);
222 ASSERT_EQ(0, memcmp(&versionOut, &version, sizeof(version)));
223}
224
225TEST(GetPLDMCommands, testEncodeResponse)
226{
227 uint8_t completionCode = 0;
228 std::array<uint8_t, PLDM_GET_COMMANDS_RESP_BYTES> responseMsg{};
229 pldm_msg response{};
230 response.body.payload = responseMsg.data();
231 response.body.payload_length = responseMsg.size();
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600232 std::array<bitfield8_t, PLDM_MAX_CMDS_PER_TYPE / 8> commands{};
233 commands[0].byte = 1;
234 commands[1].byte = 2;
235 commands[2].byte = 3;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600236
237 auto rc =
238 encode_get_commands_resp(0, PLDM_SUCCESS, commands.data(), &response);
239 ASSERT_EQ(rc, PLDM_SUCCESS);
240 ASSERT_EQ(completionCode, response.body.payload[0]);
241 ASSERT_EQ(1, response.body.payload[1]);
242 ASSERT_EQ(2, response.body.payload[2]);
243 ASSERT_EQ(3, response.body.payload[3]);
244}
245
246TEST(GetPLDMTypes, testEncodeResponse)
247{
248 uint8_t completionCode = 0;
249 std::array<uint8_t, PLDM_GET_TYPES_RESP_BYTES> responseMsg{};
250 pldm_msg response{};
251 response.body.payload = responseMsg.data();
252 response.body.payload_length = responseMsg.size();
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600253 std::array<bitfield8_t, PLDM_MAX_TYPES / 8> types{};
254 types[0].byte = 1;
255 types[1].byte = 2;
256 types[2].byte = 3;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600257
258 auto rc = encode_get_types_resp(0, PLDM_SUCCESS, types.data(), &response);
259 ASSERT_EQ(rc, PLDM_SUCCESS);
260 ASSERT_EQ(completionCode, response.body.payload[0]);
261 ASSERT_EQ(1, response.body.payload[1]);
262 ASSERT_EQ(2, response.body.payload[2]);
263 ASSERT_EQ(3, response.body.payload[3]);
264}
265
266TEST(GetPLDMTypes, testDecodeResponse)
267{
268 std::array<uint8_t, PLDM_GET_TYPES_RESP_BYTES> responseMsg{};
269 pldm_msg_payload response{};
270 response.payload = responseMsg.data();
271 response.payload_length = responseMsg.size();
272 response.payload[1] = 1;
273 response.payload[2] = 2;
274 response.payload[3] = 3;
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600275 std::array<bitfield8_t, PLDM_MAX_TYPES / 8> outTypes{};
Deepak Kodihalli8c643462019-02-21 10:43:36 -0600276 uint8_t completion_code;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600277
Deepak Kodihalli8c643462019-02-21 10:43:36 -0600278 auto rc =
279 decode_get_types_resp(&response, &completion_code, outTypes.data());
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600280
281 ASSERT_EQ(rc, PLDM_SUCCESS);
Deepak Kodihalli8c643462019-02-21 10:43:36 -0600282 ASSERT_EQ(completion_code, PLDM_SUCCESS);
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600283 ASSERT_EQ(response.payload[1], outTypes[0].byte);
284 ASSERT_EQ(response.payload[2], outTypes[1].byte);
285 ASSERT_EQ(response.payload[3], outTypes[2].byte);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600286}
287
288TEST(GetPLDMCommands, testDecodeResponse)
289{
290 std::array<uint8_t, PLDM_MAX_CMDS_PER_TYPE> responseMsg{};
291 pldm_msg_payload response{};
292 response.payload = responseMsg.data();
293 response.payload_length = responseMsg.size();
294 response.payload[1] = 1;
295 response.payload[2] = 2;
296 response.payload[3] = 3;
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600297 std::array<bitfield8_t, PLDM_MAX_CMDS_PER_TYPE / 8> outTypes{};
Deepak Kodihalli8c643462019-02-21 10:43:36 -0600298 uint8_t completion_code;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600299
Deepak Kodihalli8c643462019-02-21 10:43:36 -0600300 auto rc =
301 decode_get_commands_resp(&response, &completion_code, outTypes.data());
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600302
303 ASSERT_EQ(rc, PLDM_SUCCESS);
Deepak Kodihalli8c643462019-02-21 10:43:36 -0600304 ASSERT_EQ(completion_code, PLDM_SUCCESS);
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600305 ASSERT_EQ(response.payload[1], outTypes[0].byte);
306 ASSERT_EQ(response.payload[2], outTypes[1].byte);
307 ASSERT_EQ(response.payload[3], outTypes[2].byte);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600308}
Sampa Misra432e1872019-02-13 03:49:43 -0600309
Sampa Misra94d704b2019-02-22 03:36:02 -0600310TEST(GetPLDMVersion, testGoodEncodeRequest)
Sampa Misra432e1872019-02-13 03:49:43 -0600311{
312 std::array<uint8_t, PLDM_GET_VERSION_REQ_BYTES> requestMsg{};
313 pldm_msg request{};
314 request.body.payload = requestMsg.data();
315 request.body.payload_length = requestMsg.size();
316 uint8_t pldmType = 0x03;
317 uint32_t transferHandle = 0x0;
318 uint8_t opFlag = 0x01;
319
320 auto rc =
321 encode_get_version_req(0, transferHandle, opFlag, pldmType, &request);
322 ASSERT_EQ(rc, PLDM_SUCCESS);
323 ASSERT_EQ(0, memcmp(request.body.payload, &transferHandle,
324 sizeof(transferHandle)));
325 ASSERT_EQ(0, memcmp(request.body.payload + sizeof(transferHandle), &opFlag,
326 sizeof(opFlag)));
327 ASSERT_EQ(0, memcmp(request.body.payload + sizeof(transferHandle) +
328 sizeof(opFlag),
329 &pldmType, sizeof(pldmType)));
330}
331
Sampa Misra94d704b2019-02-22 03:36:02 -0600332TEST(GetPLDMVersion, testBadEncodeRequest)
333{
334 uint8_t pldmType = 0x03;
335 uint32_t transferHandle = 0x0;
336 uint8_t opFlag = 0x01;
337
338 auto rc =
339 encode_get_version_req(0, transferHandle, opFlag, pldmType, nullptr);
340
341 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
342}
343
Sampa Misra432e1872019-02-13 03:49:43 -0600344TEST(GetPLDMVersion, testEncodeResponse)
345{
346 pldm_msg response{};
347 uint8_t completionCode = 0;
348 uint32_t transferHandle = 0;
349 uint8_t flag = PLDM_START_AND_END;
350 std::array<uint8_t, PLDM_GET_VERSION_RESP_BYTES> responseMsg{};
351 response.body.payload = responseMsg.data();
352 response.body.payload_length = responseMsg.size();
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600353 ver32_t version = {0xFF, 0xFF, 0xFF, 0xFF};
Sampa Misra432e1872019-02-13 03:49:43 -0600354
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600355 auto rc = encode_get_version_resp(0, PLDM_SUCCESS, 0, PLDM_START_AND_END,
356 &version, sizeof(ver32_t), &response);
Sampa Misra432e1872019-02-13 03:49:43 -0600357
358 ASSERT_EQ(rc, PLDM_SUCCESS);
359 ASSERT_EQ(completionCode, response.body.payload[0]);
360 ASSERT_EQ(0,
361 memcmp(response.body.payload + sizeof(response.body.payload[0]),
362 &transferHandle, sizeof(transferHandle)));
363 ASSERT_EQ(0,
364 memcmp(response.body.payload + sizeof(response.body.payload[0]) +
365 sizeof(transferHandle),
366 &flag, sizeof(flag)));
367 ASSERT_EQ(0,
368 memcmp(response.body.payload + sizeof(response.body.payload[0]) +
369 sizeof(transferHandle) + sizeof(flag),
370 &version, sizeof(version)));
371}
372
373TEST(GetPLDMVersion, testDecodeRequest)
374{
375 std::array<uint8_t, PLDM_GET_VERSION_REQ_BYTES> requestMsg{};
376 pldm_msg_payload request{};
377 request.payload = requestMsg.data();
378 request.payload_length = requestMsg.size();
379 uint32_t transferHandle = 0x0;
380 uint32_t retTransferHandle = 0x0;
381 uint8_t flag = PLDM_GET_FIRSTPART;
382 uint8_t retFlag = PLDM_GET_FIRSTPART;
383 uint8_t pldmType = PLDM_BASE;
384 uint8_t retType = PLDM_BASE;
385
386 memcpy(request.payload, &transferHandle, sizeof(transferHandle));
387 memcpy(request.payload + sizeof(transferHandle), &flag, sizeof(flag));
388 memcpy(request.payload + sizeof(transferHandle) + sizeof(flag), &pldmType,
389 sizeof(pldmType));
390
391 auto rc = decode_get_version_req(&request, &retTransferHandle, &retFlag,
392 &retType);
393
394 ASSERT_EQ(rc, PLDM_SUCCESS);
395 ASSERT_EQ(transferHandle, retTransferHandle);
396 ASSERT_EQ(flag, retFlag);
397 ASSERT_EQ(pldmType, retType);
398}
399
400TEST(GetPLDMVersion, testDecodeResponse)
401{
402 std::array<uint8_t, PLDM_GET_VERSION_RESP_BYTES> responseMsg{};
403 pldm_msg_payload response{};
404 response.payload = responseMsg.data();
405 response.payload_length = responseMsg.size();
406 uint32_t transferHandle = 0x0;
407 uint32_t retTransferHandle = 0x0;
408 uint8_t flag = PLDM_START_AND_END;
409 uint8_t retFlag = PLDM_START_AND_END;
410 uint8_t completionCode = 0;
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600411 ver32_t version = {0xFF, 0xFF, 0xFF, 0xFF};
412 ver32_t versionOut;
Deepak Kodihalli8c643462019-02-21 10:43:36 -0600413 uint8_t completion_code;
Sampa Misra432e1872019-02-13 03:49:43 -0600414
415 memcpy(response.payload + sizeof(completionCode), &transferHandle,
416 sizeof(transferHandle));
417 memcpy(response.payload + sizeof(completionCode) + sizeof(transferHandle),
418 &flag, sizeof(flag));
419 memcpy(response.payload + sizeof(completionCode) + sizeof(transferHandle) +
420 sizeof(flag),
421 &version, sizeof(version));
422
Deepak Kodihalli8c643462019-02-21 10:43:36 -0600423 auto rc = decode_get_version_resp(
424 &response, &completion_code, &retTransferHandle, &retFlag, &versionOut);
Sampa Misra432e1872019-02-13 03:49:43 -0600425 ASSERT_EQ(rc, PLDM_SUCCESS);
426 ASSERT_EQ(transferHandle, retTransferHandle);
427 ASSERT_EQ(flag, retFlag);
428
429 ASSERT_EQ(versionOut.major, version.major);
430 ASSERT_EQ(versionOut.minor, version.minor);
431 ASSERT_EQ(versionOut.update, version.update);
432 ASSERT_EQ(versionOut.alpha, version.alpha);
433}