blob: 8990d22f54906b0114f1549181ad0b0776e32ad2 [file] [log] [blame]
Deepak Kodihalli1b24f972019-02-01 04:09:13 -06001#include "libpldmresponder/base.hpp"
2
3#include <string.h>
4
5#include <array>
6
7#include "libpldm/base.h"
8
9#include <gtest/gtest.h>
10
11using namespace pldm::responder;
12
13TEST(GetPLDMTypes, testGoodRequest)
14{
vkaverapa6575b82019-04-03 05:33:52 -050015 std::array<uint8_t, sizeof(pldm_msg_hdr)> requestPayload{};
16 auto request = reinterpret_cast<pldm_msg*>(requestPayload.data());
17 // payload length will be 0 in this case
18 size_t requestPayloadLength = 0;
19 auto response = getPLDMTypes(request, requestPayloadLength);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060020 // Only base type supported at the moment
vkaverapa6575b82019-04-03 05:33:52 -050021 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
22 uint8_t* payload_ptr = responsePtr->payload;
23 ASSERT_EQ(payload_ptr[0], 0);
24 ASSERT_EQ(payload_ptr[1], 1);
25 ASSERT_EQ(payload_ptr[2], 0);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060026}
27
28TEST(GetPLDMCommands, testGoodRequest)
29{
30 // Only base type supported at the moment, and commands -
31 // GetPLDMTypes, GetPLDMCommands
vkaverapa6575b82019-04-03 05:33:52 -050032 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_REQ_BYTES>
33 requestPayload{};
34 auto request = reinterpret_cast<pldm_msg*>(requestPayload.data());
35 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
36 auto response = getPLDMCommands(request, requestPayloadLength);
37 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
38 uint8_t* payload_ptr = responsePtr->payload;
39 ASSERT_EQ(payload_ptr[0], 0);
40 ASSERT_EQ(payload_ptr[1], 48); // 48 = 0b110000
41 ASSERT_EQ(payload_ptr[2], 0);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060042}
43
44TEST(GetPLDMCommands, testBadRequest)
45{
vkaverapa6575b82019-04-03 05:33:52 -050046 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_REQ_BYTES>
47 requestPayload{};
48 auto request = reinterpret_cast<pldm_msg*>(requestPayload.data());
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060049
vkaverapa6575b82019-04-03 05:33:52 -050050 request->payload[0] = 0xFF;
51 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
52 auto response = getPLDMCommands(request, requestPayloadLength);
53 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
54 uint8_t* payload_ptr = responsePtr->payload;
55 ASSERT_EQ(payload_ptr[0], PLDM_ERROR_INVALID_PLDM_TYPE);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060056}
Sampa Misra432e1872019-02-13 03:49:43 -060057TEST(GetPLDMVersion, testGoodRequest)
58{
vkaverapa6575b82019-04-03 05:33:52 -050059 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_REQ_BYTES>
60 requestPayload{};
61 auto request = reinterpret_cast<pldm_msg*>(requestPayload.data());
62 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
Sampa Misra432e1872019-02-13 03:49:43 -060063
64 uint8_t pldmType = PLDM_BASE;
65 uint32_t transferHandle = 0x0;
66 uint8_t flag = PLDM_GET_FIRSTPART;
67 uint8_t retFlag = PLDM_START_AND_END;
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060068 ver32_t version = {0xF1, 0xF0, 0xF0, 0x00};
Sampa Misra432e1872019-02-13 03:49:43 -060069
70 auto rc =
vkaverapa6575b82019-04-03 05:33:52 -050071 encode_get_version_req(0, transferHandle, flag, pldmType, request);
Sampa Misra432e1872019-02-13 03:49:43 -060072
73 ASSERT_EQ(0, rc);
74
vkaverapa6575b82019-04-03 05:33:52 -050075 auto response = getPLDMVersion(request, requestPayloadLength);
76 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Sampa Misra432e1872019-02-13 03:49:43 -060077
vkaverapa6575b82019-04-03 05:33:52 -050078 ASSERT_EQ(responsePtr->payload[0], 0);
79 ASSERT_EQ(0, memcmp(responsePtr->payload + sizeof(responsePtr->payload[0]),
80 &transferHandle, sizeof(transferHandle)));
81 ASSERT_EQ(0, memcmp(responsePtr->payload + sizeof(responsePtr->payload[0]) +
82 sizeof(transferHandle),
83 &retFlag, sizeof(flag)));
84 ASSERT_EQ(0, memcmp(responsePtr->payload + sizeof(responsePtr->payload[0]) +
85 sizeof(transferHandle) + sizeof(flag),
86 &version, sizeof(version)));
Sampa Misra432e1872019-02-13 03:49:43 -060087}
Sampa Misra94d704b2019-02-22 03:36:02 -060088TEST(GetPLDMVersion, testBadRequest)
89{
vkaverapa6575b82019-04-03 05:33:52 -050090 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_REQ_BYTES>
91 requestPayload{};
92 auto request = reinterpret_cast<pldm_msg*>(requestPayload.data());
93 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
Sampa Misra94d704b2019-02-22 03:36:02 -060094
95 uint8_t pldmType = 7;
96 uint32_t transferHandle = 0x0;
97 uint8_t flag = PLDM_GET_FIRSTPART;
98
99 auto rc =
vkaverapa6575b82019-04-03 05:33:52 -0500100 encode_get_version_req(0, transferHandle, flag, pldmType, request);
Sampa Misra94d704b2019-02-22 03:36:02 -0600101
102 ASSERT_EQ(0, rc);
103
vkaverapa6575b82019-04-03 05:33:52 -0500104 auto response = getPLDMVersion(request, requestPayloadLength - 1);
105 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Sampa Misra94d704b2019-02-22 03:36:02 -0600106
vkaverapa6575b82019-04-03 05:33:52 -0500107 ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR_INVALID_LENGTH);
Sampa Misra94d704b2019-02-22 03:36:02 -0600108
vkaverapa6575b82019-04-03 05:33:52 -0500109 request = reinterpret_cast<pldm_msg*>(requestPayload.data());
110 requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
Sampa Misra94d704b2019-02-22 03:36:02 -0600111
vkaverapa6575b82019-04-03 05:33:52 -0500112 rc = encode_get_version_req(0, transferHandle, flag, pldmType, request);
Sampa Misra94d704b2019-02-22 03:36:02 -0600113
114 ASSERT_EQ(0, rc);
115
vkaverapa6575b82019-04-03 05:33:52 -0500116 response = getPLDMVersion(request, requestPayloadLength);
117 responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Sampa Misra94d704b2019-02-22 03:36:02 -0600118
vkaverapa6575b82019-04-03 05:33:52 -0500119 ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR_INVALID_PLDM_TYPE);
Sampa Misra94d704b2019-02-22 03:36:02 -0600120}