Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 1 | #include "invoker.hpp" |
| 2 | |
| 3 | #include <stdexcept> |
| 4 | |
| 5 | #include "libpldm/base.h" |
| 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 | |
| 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 | |
| 31 | TEST(Registration, testSuccess) |
| 32 | { |
| 33 | Invoker invoker{}; |
| 34 | invoker.registerHandler(testType, std::make_unique<TestHandler>()); |
| 35 | auto result = invoker.handle(testType, testCmd, nullptr, 0); |
| 36 | ASSERT_EQ(result[0], 100); |
| 37 | ASSERT_EQ(result[1], 200); |
| 38 | } |
| 39 | |
| 40 | TEST(Registration, testFailure) |
| 41 | { |
| 42 | Invoker invoker{}; |
| 43 | ASSERT_THROW(invoker.handle(testType, testCmd, nullptr, 0), |
| 44 | std::out_of_range); |
| 45 | invoker.registerHandler(testType, std::make_unique<TestHandler>()); |
| 46 | uint8_t badCmd = 0xFE; |
| 47 | ASSERT_THROW(invoker.handle(testType, badCmd, nullptr, 0), |
| 48 | std::out_of_range); |
| 49 | } |