blob: 429335794f8d4c4d72f831aa92242acbee240065 [file] [log] [blame]
Deepak Kodihalli1521f6d2020-06-16 08:51:02 -05001#include "pldmd/invoker.hpp"
Deepak Kodihallibc669f12019-11-28 08:52:07 -06002
George Liuc453e162022-12-21 17:16:23 +08003#include <libpldm/base.h>
4
Deepak Kodihallibc669f12019-11-28 08:52:07 -06005#include <stdexcept>
6
Deepak Kodihallibc669f12019-11-28 08:52:07 -06007#include <gtest/gtest.h>
8
9using namespace pldm;
10using namespace pldm::responder;
11constexpr Command testCmd = 0xFF;
12constexpr Type testType = 0xFF;
Delphine CC Chiud2e48992023-12-05 16:29:51 +080013constexpr pldm_tid_t tid = 0;
Deepak Kodihallibc669f12019-11-28 08:52:07 -060014
15class TestHandler : public CmdHandler
16{
17 public:
18 TestHandler()
19 {
Delphine CC Chiud2e48992023-12-05 16:29:51 +080020 handlers.emplace(testCmd, [this](uint8_t tid, const pldm_msg* request,
21 size_t payloadLength) {
22 return this->handle(tid, request, payloadLength);
Patrick Williams6da4f912023-05-10 07:50:53 -050023 });
Deepak Kodihallibc669f12019-11-28 08:52:07 -060024 }
25
Delphine CC Chiud2e48992023-12-05 16:29:51 +080026 Response handle(uint8_t /*tid*/, const pldm_msg* /*request*/,
27 size_t /*payloadLength*/)
Deepak Kodihallibc669f12019-11-28 08:52:07 -060028 {
29 return {100, 200};
30 }
31};
32
John Wang7f02d702019-12-03 13:38:14 +080033TEST(CcOnlyResponse, testEncode)
34{
35 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr));
36 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
37 encode_get_types_req(0, request);
38
39 auto responseMsg = CmdHandler::ccOnlyResponse(request, PLDM_ERROR);
40 std::vector<uint8_t> expectMsg = {0, 0, 4, 1};
41 EXPECT_EQ(responseMsg, expectMsg);
42}
43
Deepak Kodihallibc669f12019-11-28 08:52:07 -060044TEST(Registration, testSuccess)
45{
46 Invoker invoker{};
47 invoker.registerHandler(testType, std::make_unique<TestHandler>());
Delphine CC Chiud2e48992023-12-05 16:29:51 +080048 auto result = invoker.handle(tid, testType, testCmd, nullptr, 0);
Deepak Kodihallibc669f12019-11-28 08:52:07 -060049 ASSERT_EQ(result[0], 100);
50 ASSERT_EQ(result[1], 200);
51}
52
53TEST(Registration, testFailure)
54{
55 Invoker invoker{};
Delphine CC Chiud2e48992023-12-05 16:29:51 +080056 ASSERT_THROW(invoker.handle(tid, testType, testCmd, nullptr, 0),
Deepak Kodihallibc669f12019-11-28 08:52:07 -060057 std::out_of_range);
58 invoker.registerHandler(testType, std::make_unique<TestHandler>());
59 uint8_t badCmd = 0xFE;
Delphine CC Chiud2e48992023-12-05 16:29:51 +080060 ASSERT_THROW(invoker.handle(tid, testType, badCmd, nullptr, 0),
Deepak Kodihallibc669f12019-11-28 08:52:07 -060061 std::out_of_range);
62}