xref: /openbmc/pldm/test/pldmd_registration_test.cpp (revision c453e1641fc46a5a2e6c082b5a561410ecdaf779)
11521f6d1SDeepak Kodihalli #include "pldmd/invoker.hpp"
2bc669f1bSDeepak Kodihalli 
3*c453e164SGeorge Liu #include <libpldm/base.h>
4*c453e164SGeorge Liu 
5bc669f1bSDeepak Kodihalli #include <stdexcept>
6bc669f1bSDeepak Kodihalli 
7bc669f1bSDeepak Kodihalli #include <gtest/gtest.h>
8bc669f1bSDeepak Kodihalli 
9bc669f1bSDeepak Kodihalli using namespace pldm;
10bc669f1bSDeepak Kodihalli using namespace pldm::responder;
11bc669f1bSDeepak Kodihalli constexpr Command testCmd = 0xFF;
12bc669f1bSDeepak Kodihalli constexpr Type testType = 0xFF;
13bc669f1bSDeepak Kodihalli 
14bc669f1bSDeepak Kodihalli class TestHandler : public CmdHandler
15bc669f1bSDeepak Kodihalli {
16bc669f1bSDeepak Kodihalli   public:
17bc669f1bSDeepak Kodihalli     TestHandler()
18bc669f1bSDeepak Kodihalli     {
19bc669f1bSDeepak Kodihalli         handlers.emplace(testCmd,
20bc669f1bSDeepak Kodihalli                          [this](const pldm_msg* request, size_t payloadLength) {
21bc669f1bSDeepak Kodihalli                              return this->handle(request, payloadLength);
22bc669f1bSDeepak Kodihalli                          });
23bc669f1bSDeepak Kodihalli     }
24bc669f1bSDeepak Kodihalli 
25bc669f1bSDeepak Kodihalli     Response handle(const pldm_msg* /*request*/, size_t /*payloadLength*/)
26bc669f1bSDeepak Kodihalli     {
27bc669f1bSDeepak Kodihalli         return {100, 200};
28bc669f1bSDeepak Kodihalli     }
29bc669f1bSDeepak Kodihalli };
30bc669f1bSDeepak Kodihalli 
317f02d700SJohn Wang TEST(CcOnlyResponse, testEncode)
327f02d700SJohn Wang {
337f02d700SJohn Wang     std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr));
347f02d700SJohn Wang     auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
357f02d700SJohn Wang     encode_get_types_req(0, request);
367f02d700SJohn Wang 
377f02d700SJohn Wang     auto responseMsg = CmdHandler::ccOnlyResponse(request, PLDM_ERROR);
387f02d700SJohn Wang     std::vector<uint8_t> expectMsg = {0, 0, 4, 1};
397f02d700SJohn Wang     EXPECT_EQ(responseMsg, expectMsg);
407f02d700SJohn Wang }
417f02d700SJohn Wang 
42bc669f1bSDeepak Kodihalli TEST(Registration, testSuccess)
43bc669f1bSDeepak Kodihalli {
44bc669f1bSDeepak Kodihalli     Invoker invoker{};
45bc669f1bSDeepak Kodihalli     invoker.registerHandler(testType, std::make_unique<TestHandler>());
46bc669f1bSDeepak Kodihalli     auto result = invoker.handle(testType, testCmd, nullptr, 0);
47bc669f1bSDeepak Kodihalli     ASSERT_EQ(result[0], 100);
48bc669f1bSDeepak Kodihalli     ASSERT_EQ(result[1], 200);
49bc669f1bSDeepak Kodihalli }
50bc669f1bSDeepak Kodihalli 
51bc669f1bSDeepak Kodihalli TEST(Registration, testFailure)
52bc669f1bSDeepak Kodihalli {
53bc669f1bSDeepak Kodihalli     Invoker invoker{};
54bc669f1bSDeepak Kodihalli     ASSERT_THROW(invoker.handle(testType, testCmd, nullptr, 0),
55bc669f1bSDeepak Kodihalli                  std::out_of_range);
56bc669f1bSDeepak Kodihalli     invoker.registerHandler(testType, std::make_unique<TestHandler>());
57bc669f1bSDeepak Kodihalli     uint8_t badCmd = 0xFE;
58bc669f1bSDeepak Kodihalli     ASSERT_THROW(invoker.handle(testType, badCmd, nullptr, 0),
59bc669f1bSDeepak Kodihalli                  std::out_of_range);
60bc669f1bSDeepak Kodihalli }
61