Deepak Kodihalli | 1521f6d | 2020-06-16 08:51:02 -0500 | [diff] [blame] | 1 | #include "pldmd/invoker.hpp" |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 2 | |
George Liu | c453e16 | 2022-12-21 17:16:23 +0800 | [diff] [blame^] | 3 | #include <libpldm/base.h> |
| 4 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 5 | #include <stdexcept> |
| 6 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 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 | |
| 14 | class TestHandler : public CmdHandler |
| 15 | { |
| 16 | public: |
| 17 | TestHandler() |
| 18 | { |
| 19 | handlers.emplace(testCmd, |
| 20 | [this](const pldm_msg* request, size_t payloadLength) { |
| 21 | return this->handle(request, payloadLength); |
| 22 | }); |
| 23 | } |
| 24 | |
| 25 | Response handle(const pldm_msg* /*request*/, size_t /*payloadLength*/) |
| 26 | { |
| 27 | return {100, 200}; |
| 28 | } |
| 29 | }; |
| 30 | |
John Wang | 7f02d70 | 2019-12-03 13:38:14 +0800 | [diff] [blame] | 31 | TEST(CcOnlyResponse, testEncode) |
| 32 | { |
| 33 | std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr)); |
| 34 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 35 | encode_get_types_req(0, request); |
| 36 | |
| 37 | auto responseMsg = CmdHandler::ccOnlyResponse(request, PLDM_ERROR); |
| 38 | std::vector<uint8_t> expectMsg = {0, 0, 4, 1}; |
| 39 | EXPECT_EQ(responseMsg, expectMsg); |
| 40 | } |
| 41 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 42 | TEST(Registration, testSuccess) |
| 43 | { |
| 44 | Invoker invoker{}; |
| 45 | invoker.registerHandler(testType, std::make_unique<TestHandler>()); |
| 46 | auto result = invoker.handle(testType, testCmd, nullptr, 0); |
| 47 | ASSERT_EQ(result[0], 100); |
| 48 | ASSERT_EQ(result[1], 200); |
| 49 | } |
| 50 | |
| 51 | TEST(Registration, testFailure) |
| 52 | { |
| 53 | Invoker invoker{}; |
| 54 | ASSERT_THROW(invoker.handle(testType, testCmd, nullptr, 0), |
| 55 | std::out_of_range); |
| 56 | invoker.registerHandler(testType, std::make_unique<TestHandler>()); |
| 57 | uint8_t badCmd = 0xFE; |
| 58 | ASSERT_THROW(invoker.handle(testType, badCmd, nullptr, 0), |
| 59 | std::out_of_range); |
| 60 | } |