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; |
Delphine CC Chiu | d2e4899 | 2023-12-05 16:29:51 +0800 | [diff] [blame] | 13 | constexpr pldm_tid_t tid = 0; |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 14 | |
| 15 | class TestHandler : public CmdHandler |
| 16 | { |
| 17 | public: |
| 18 | TestHandler() |
| 19 | { |
Delphine CC Chiu | d2e4899 | 2023-12-05 16:29:51 +0800 | [diff] [blame] | 20 | handlers.emplace(testCmd, [this](uint8_t tid, const pldm_msg* request, |
| 21 | size_t payloadLength) { |
| 22 | return this->handle(tid, request, payloadLength); |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 23 | }); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 24 | } |
| 25 | |
Delphine CC Chiu | d2e4899 | 2023-12-05 16:29:51 +0800 | [diff] [blame] | 26 | Response handle(uint8_t /*tid*/, const pldm_msg* /*request*/, |
| 27 | size_t /*payloadLength*/) |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 28 | { |
| 29 | return {100, 200}; |
| 30 | } |
| 31 | }; |
| 32 | |
John Wang | 7f02d70 | 2019-12-03 13:38:14 +0800 | [diff] [blame] | 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 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 44 | TEST(Registration, testSuccess) |
| 45 | { |
| 46 | Invoker invoker{}; |
| 47 | invoker.registerHandler(testType, std::make_unique<TestHandler>()); |
Delphine CC Chiu | d2e4899 | 2023-12-05 16:29:51 +0800 | [diff] [blame] | 48 | auto result = invoker.handle(tid, testType, testCmd, nullptr, 0); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 49 | ASSERT_EQ(result[0], 100); |
| 50 | ASSERT_EQ(result[1], 200); |
| 51 | } |
| 52 | |
| 53 | TEST(Registration, testFailure) |
| 54 | { |
| 55 | Invoker invoker{}; |
Delphine CC Chiu | d2e4899 | 2023-12-05 16:29:51 +0800 | [diff] [blame] | 56 | ASSERT_THROW(invoker.handle(tid, testType, testCmd, nullptr, 0), |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 57 | std::out_of_range); |
| 58 | invoker.registerHandler(testType, std::make_unique<TestHandler>()); |
| 59 | uint8_t badCmd = 0xFE; |
Delphine CC Chiu | d2e4899 | 2023-12-05 16:29:51 +0800 | [diff] [blame] | 60 | ASSERT_THROW(invoker.handle(tid, testType, badCmd, nullptr, 0), |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 61 | std::out_of_range); |
| 62 | } |