xref: /openbmc/pldm/test/pldmd_registration_test.cpp (revision d2e48991c2a6d16c2b31de947bd1c927313046af)
11521f6d1SDeepak Kodihalli #include "pldmd/invoker.hpp"
2bc669f1bSDeepak Kodihalli 
3c453e164SGeorge Liu #include <libpldm/base.h>
4c453e164SGeorge 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;
13*d2e48991SDelphine CC Chiu constexpr pldm_tid_t tid = 0;
14bc669f1bSDeepak Kodihalli 
15bc669f1bSDeepak Kodihalli class TestHandler : public CmdHandler
16bc669f1bSDeepak Kodihalli {
17bc669f1bSDeepak Kodihalli   public:
TestHandler()18bc669f1bSDeepak Kodihalli     TestHandler()
19bc669f1bSDeepak Kodihalli     {
20*d2e48991SDelphine CC Chiu         handlers.emplace(testCmd, [this](uint8_t tid, const pldm_msg* request,
21*d2e48991SDelphine CC Chiu                                          size_t payloadLength) {
22*d2e48991SDelphine CC Chiu             return this->handle(tid, request, payloadLength);
23bc669f1bSDeepak Kodihalli         });
24bc669f1bSDeepak Kodihalli     }
25bc669f1bSDeepak Kodihalli 
handle(uint8_t,const pldm_msg *,size_t)26*d2e48991SDelphine CC Chiu     Response handle(uint8_t /*tid*/, const pldm_msg* /*request*/,
27*d2e48991SDelphine CC Chiu                     size_t /*payloadLength*/)
28bc669f1bSDeepak Kodihalli     {
29bc669f1bSDeepak Kodihalli         return {100, 200};
30bc669f1bSDeepak Kodihalli     }
31bc669f1bSDeepak Kodihalli };
32bc669f1bSDeepak Kodihalli 
TEST(CcOnlyResponse,testEncode)337f02d700SJohn Wang TEST(CcOnlyResponse, testEncode)
347f02d700SJohn Wang {
357f02d700SJohn Wang     std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr));
367f02d700SJohn Wang     auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
377f02d700SJohn Wang     encode_get_types_req(0, request);
387f02d700SJohn Wang 
397f02d700SJohn Wang     auto responseMsg = CmdHandler::ccOnlyResponse(request, PLDM_ERROR);
407f02d700SJohn Wang     std::vector<uint8_t> expectMsg = {0, 0, 4, 1};
417f02d700SJohn Wang     EXPECT_EQ(responseMsg, expectMsg);
427f02d700SJohn Wang }
437f02d700SJohn Wang 
TEST(Registration,testSuccess)44bc669f1bSDeepak Kodihalli TEST(Registration, testSuccess)
45bc669f1bSDeepak Kodihalli {
46bc669f1bSDeepak Kodihalli     Invoker invoker{};
47bc669f1bSDeepak Kodihalli     invoker.registerHandler(testType, std::make_unique<TestHandler>());
48*d2e48991SDelphine CC Chiu     auto result = invoker.handle(tid, testType, testCmd, nullptr, 0);
49bc669f1bSDeepak Kodihalli     ASSERT_EQ(result[0], 100);
50bc669f1bSDeepak Kodihalli     ASSERT_EQ(result[1], 200);
51bc669f1bSDeepak Kodihalli }
52bc669f1bSDeepak Kodihalli 
TEST(Registration,testFailure)53bc669f1bSDeepak Kodihalli TEST(Registration, testFailure)
54bc669f1bSDeepak Kodihalli {
55bc669f1bSDeepak Kodihalli     Invoker invoker{};
56*d2e48991SDelphine CC Chiu     ASSERT_THROW(invoker.handle(tid, testType, testCmd, nullptr, 0),
57bc669f1bSDeepak Kodihalli                  std::out_of_range);
58bc669f1bSDeepak Kodihalli     invoker.registerHandler(testType, std::make_unique<TestHandler>());
59bc669f1bSDeepak Kodihalli     uint8_t badCmd = 0xFE;
60*d2e48991SDelphine CC Chiu     ASSERT_THROW(invoker.handle(tid, testType, badCmd, nullptr, 0),
61bc669f1bSDeepak Kodihalli                  std::out_of_range);
62bc669f1bSDeepak Kodihalli }
63