1 #include "pldmd/invoker.hpp"
2 
3 #include <libpldm/base.h>
4 
5 #include <stdexcept>
6 
7 #include <gtest/gtest.h>
8 
9 using namespace pldm;
10 using namespace pldm::responder;
11 constexpr Command testCmd = 0xFF;
12 constexpr Type testType = 0xFF;
13 constexpr pldm_tid_t tid = 0;
14 
15 class TestHandler : public CmdHandler
16 {
17   public:
TestHandler()18     TestHandler()
19     {
20         handlers.emplace(testCmd, [this](uint8_t tid, const pldm_msg* request,
21                                          size_t payloadLength) {
22             return this->handle(tid, request, payloadLength);
23         });
24     }
25 
handle(uint8_t,const pldm_msg *,size_t)26     Response handle(uint8_t /*tid*/, const pldm_msg* /*request*/,
27                     size_t /*payloadLength*/)
28     {
29         return {100, 200};
30     }
31 };
32 
TEST(CcOnlyResponse,testEncode)33 TEST(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 
TEST(Registration,testSuccess)44 TEST(Registration, testSuccess)
45 {
46     Invoker invoker{};
47     invoker.registerHandler(testType, std::make_unique<TestHandler>());
48     auto result = invoker.handle(tid, testType, testCmd, nullptr, 0);
49     ASSERT_EQ(result[0], 100);
50     ASSERT_EQ(result[1], 200);
51 }
52 
TEST(Registration,testFailure)53 TEST(Registration, testFailure)
54 {
55     Invoker invoker{};
56     ASSERT_THROW(invoker.handle(tid, testType, testCmd, nullptr, 0),
57                  std::out_of_range);
58     invoker.registerHandler(testType, std::make_unique<TestHandler>());
59     uint8_t badCmd = 0xFE;
60     ASSERT_THROW(invoker.handle(tid, testType, badCmd, nullptr, 0),
61                  std::out_of_range);
62 }
63