blob: 885ef247086c6aea9469c33c3411baff98a31aef [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;
Deepak Kodihallibc669f12019-11-28 08:52:07 -060019 base::Handler handler;
20 auto response = handler.getPLDMTypes(request, requestPayloadLength);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060021 // Only base type supported at the moment
vkaverapa6575b82019-04-03 05:33:52 -050022 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
23 uint8_t* payload_ptr = responsePtr->payload;
24 ASSERT_EQ(payload_ptr[0], 0);
Zahed Hossain5e4e3832019-07-05 03:43:18 -050025 ASSERT_EQ(payload_ptr[1], 13);
vkaverapa6575b82019-04-03 05:33:52 -050026 ASSERT_EQ(payload_ptr[2], 0);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060027}
28
29TEST(GetPLDMCommands, testGoodRequest)
30{
31 // Only base type supported at the moment, and commands -
32 // GetPLDMTypes, GetPLDMCommands
vkaverapa6575b82019-04-03 05:33:52 -050033 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_REQ_BYTES>
34 requestPayload{};
35 auto request = reinterpret_cast<pldm_msg*>(requestPayload.data());
36 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
Deepak Kodihallibc669f12019-11-28 08:52:07 -060037 base::Handler handler;
38 auto response = handler.getPLDMCommands(request, requestPayloadLength);
vkaverapa6575b82019-04-03 05:33:52 -050039 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
40 uint8_t* payload_ptr = responsePtr->payload;
41 ASSERT_EQ(payload_ptr[0], 0);
John Wang5c4f80d2019-07-29 11:12:18 +080042 ASSERT_EQ(payload_ptr[1], 60); // 60 = 0b111100
vkaverapa6575b82019-04-03 05:33:52 -050043 ASSERT_EQ(payload_ptr[2], 0);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060044}
45
46TEST(GetPLDMCommands, testBadRequest)
47{
vkaverapa6575b82019-04-03 05:33:52 -050048 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_REQ_BYTES>
49 requestPayload{};
50 auto request = reinterpret_cast<pldm_msg*>(requestPayload.data());
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060051
vkaverapa6575b82019-04-03 05:33:52 -050052 request->payload[0] = 0xFF;
53 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
Deepak Kodihallibc669f12019-11-28 08:52:07 -060054 base::Handler handler;
55 auto response = handler.getPLDMCommands(request, requestPayloadLength);
vkaverapa6575b82019-04-03 05:33:52 -050056 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
57 uint8_t* payload_ptr = responsePtr->payload;
58 ASSERT_EQ(payload_ptr[0], PLDM_ERROR_INVALID_PLDM_TYPE);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060059}
Sampa Misra432e1872019-02-13 03:49:43 -060060TEST(GetPLDMVersion, testGoodRequest)
61{
vkaverapa6575b82019-04-03 05:33:52 -050062 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_REQ_BYTES>
63 requestPayload{};
64 auto request = reinterpret_cast<pldm_msg*>(requestPayload.data());
65 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
Sampa Misra432e1872019-02-13 03:49:43 -060066
67 uint8_t pldmType = PLDM_BASE;
68 uint32_t transferHandle = 0x0;
69 uint8_t flag = PLDM_GET_FIRSTPART;
70 uint8_t retFlag = PLDM_START_AND_END;
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060071 ver32_t version = {0xF1, 0xF0, 0xF0, 0x00};
Sampa Misra432e1872019-02-13 03:49:43 -060072
73 auto rc =
vkaverapa6575b82019-04-03 05:33:52 -050074 encode_get_version_req(0, transferHandle, flag, pldmType, request);
Sampa Misra432e1872019-02-13 03:49:43 -060075
76 ASSERT_EQ(0, rc);
77
Deepak Kodihallibc669f12019-11-28 08:52:07 -060078 base::Handler handler;
79 auto response = handler.getPLDMVersion(request, requestPayloadLength);
vkaverapa6575b82019-04-03 05:33:52 -050080 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Sampa Misra432e1872019-02-13 03:49:43 -060081
vkaverapa6575b82019-04-03 05:33:52 -050082 ASSERT_EQ(responsePtr->payload[0], 0);
83 ASSERT_EQ(0, memcmp(responsePtr->payload + sizeof(responsePtr->payload[0]),
84 &transferHandle, sizeof(transferHandle)));
85 ASSERT_EQ(0, memcmp(responsePtr->payload + sizeof(responsePtr->payload[0]) +
86 sizeof(transferHandle),
87 &retFlag, sizeof(flag)));
88 ASSERT_EQ(0, memcmp(responsePtr->payload + sizeof(responsePtr->payload[0]) +
89 sizeof(transferHandle) + sizeof(flag),
90 &version, sizeof(version)));
Sampa Misra432e1872019-02-13 03:49:43 -060091}
Sampa Misra94d704b2019-02-22 03:36:02 -060092TEST(GetPLDMVersion, testBadRequest)
93{
vkaverapa6575b82019-04-03 05:33:52 -050094 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_REQ_BYTES>
95 requestPayload{};
96 auto request = reinterpret_cast<pldm_msg*>(requestPayload.data());
97 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
Sampa Misra94d704b2019-02-22 03:36:02 -060098
99 uint8_t pldmType = 7;
100 uint32_t transferHandle = 0x0;
101 uint8_t flag = PLDM_GET_FIRSTPART;
102
103 auto rc =
vkaverapa6575b82019-04-03 05:33:52 -0500104 encode_get_version_req(0, transferHandle, flag, pldmType, request);
Sampa Misra94d704b2019-02-22 03:36:02 -0600105
106 ASSERT_EQ(0, rc);
107
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600108 base::Handler handler;
109 auto response = handler.getPLDMVersion(request, requestPayloadLength - 1);
vkaverapa6575b82019-04-03 05:33:52 -0500110 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Sampa Misra94d704b2019-02-22 03:36:02 -0600111
vkaverapa6575b82019-04-03 05:33:52 -0500112 ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR_INVALID_LENGTH);
Sampa Misra94d704b2019-02-22 03:36:02 -0600113
vkaverapa6575b82019-04-03 05:33:52 -0500114 request = reinterpret_cast<pldm_msg*>(requestPayload.data());
115 requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
Sampa Misra94d704b2019-02-22 03:36:02 -0600116
vkaverapa6575b82019-04-03 05:33:52 -0500117 rc = encode_get_version_req(0, transferHandle, flag, pldmType, request);
Sampa Misra94d704b2019-02-22 03:36:02 -0600118
119 ASSERT_EQ(0, rc);
120
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600121 response = handler.getPLDMVersion(request, requestPayloadLength);
vkaverapa6575b82019-04-03 05:33:52 -0500122 responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Sampa Misra94d704b2019-02-22 03:36:02 -0600123
vkaverapa6575b82019-04-03 05:33:52 -0500124 ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR_INVALID_PLDM_TYPE);
Sampa Misra94d704b2019-02-22 03:36:02 -0600125}
John Wang5c4f80d2019-07-29 11:12:18 +0800126
127TEST(GetTID, testGoodRequest)
128{
129 std::array<uint8_t, sizeof(pldm_msg_hdr)> requestPayload{};
130 auto request = reinterpret_cast<pldm_msg*>(requestPayload.data());
131 size_t requestPayloadLength = 0;
132
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600133 base::Handler handler;
134 auto response = handler.getTID(request, requestPayloadLength);
John Wang5c4f80d2019-07-29 11:12:18 +0800135
136 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
137 uint8_t* payload = responsePtr->payload;
138
139 ASSERT_EQ(payload[0], 0);
140 ASSERT_EQ(payload[1], 1);
141}