Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 1 | #include "common/types.hpp" |
| 2 | #include "common/utils.hpp" |
| 3 | #include "mock_request.hpp" |
| 4 | #include "pldmd/dbus_impl_requester.hpp" |
| 5 | #include "requester/handler.hpp" |
Andrew Jeffery | 7c1dc7e | 2023-04-28 14:52:16 +0930 | [diff] [blame^] | 6 | #include "test/test_instance_id.hpp" |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 7 | |
George Liu | c453e16 | 2022-12-21 17:16:23 +0800 | [diff] [blame] | 8 | #include <libpldm/base.h> |
| 9 | |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 10 | #include <gmock/gmock.h> |
| 11 | #include <gtest/gtest.h> |
| 12 | |
| 13 | using namespace pldm::requester; |
| 14 | using namespace std::chrono; |
| 15 | |
| 16 | using ::testing::AtLeast; |
| 17 | using ::testing::Between; |
| 18 | using ::testing::Exactly; |
| 19 | using ::testing::NiceMock; |
| 20 | using ::testing::Return; |
| 21 | |
| 22 | class HandlerTest : public testing::Test |
| 23 | { |
| 24 | protected: |
| 25 | HandlerTest() : |
Andrew Jeffery | 7c1dc7e | 2023-04-28 14:52:16 +0930 | [diff] [blame^] | 26 | event(sdeventplus::Event::get_default()), instanceIdDb(), |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 27 | dbusImplReq(pldm::utils::DBusHandler::getBus(), |
Andrew Jeffery | 7c1dc7e | 2023-04-28 14:52:16 +0930 | [diff] [blame^] | 28 | "/xyz/openbmc_project/pldm", instanceIdDb) |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 29 | {} |
| 30 | |
| 31 | int fd = 0; |
| 32 | mctp_eid_t eid = 0; |
| 33 | sdeventplus::Event event; |
Andrew Jeffery | 7c1dc7e | 2023-04-28 14:52:16 +0930 | [diff] [blame^] | 34 | TestInstanceIdDb instanceIdDb; |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 35 | pldm::dbus_api::Requester dbusImplReq; |
| 36 | |
| 37 | /** @brief This function runs the sd_event_run in a loop till all the events |
| 38 | * in the testcase are dispatched and exits when there are no events |
| 39 | * for the timeout time. |
| 40 | * |
| 41 | * @param[in] timeout - maximum time to wait for an event |
| 42 | */ |
| 43 | void waitEventExpiry(milliseconds timeout) |
| 44 | { |
| 45 | while (1) |
| 46 | { |
| 47 | auto sleepTime = duration_cast<microseconds>(timeout); |
| 48 | // Returns 0 on timeout |
| 49 | if (!sd_event_run(event.get(), sleepTime.count())) |
| 50 | { |
| 51 | break; |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | public: |
| 57 | bool nullResponse = false; |
| 58 | bool validResponse = false; |
| 59 | int callbackCount = 0; |
| 60 | bool response2 = false; |
| 61 | |
| 62 | void pldmResponseCallBack(mctp_eid_t /*eid*/, const pldm_msg* response, |
| 63 | size_t respMsgLen) |
| 64 | { |
| 65 | if (response == nullptr && respMsgLen == 0) |
| 66 | { |
| 67 | nullResponse = true; |
| 68 | } |
| 69 | else |
| 70 | { |
| 71 | validResponse = true; |
| 72 | } |
| 73 | callbackCount++; |
| 74 | } |
| 75 | }; |
| 76 | |
| 77 | TEST_F(HandlerTest, singleRequestResponseScenario) |
| 78 | { |
Manojkiran Eda | 9fffea2 | 2021-10-27 16:03:27 +0530 | [diff] [blame] | 79 | Handler<NiceMock<MockRequest>> reqHandler( |
| 80 | fd, event, dbusImplReq, false, 90000, seconds(1), 2, milliseconds(100)); |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 81 | pldm::Request request{}; |
| 82 | auto instanceId = dbusImplReq.getInstanceId(eid); |
Andrew Jeffery | 7c1dc7e | 2023-04-28 14:52:16 +0930 | [diff] [blame^] | 83 | EXPECT_EQ(instanceId, 0); |
Tom Joseph | a5ed658 | 2021-06-17 22:08:47 -0700 | [diff] [blame] | 84 | auto rc = reqHandler.registerRequest( |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 85 | eid, instanceId, 0, 0, std::move(request), |
| 86 | std::move(std::bind_front(&HandlerTest::pldmResponseCallBack, this))); |
Tom Joseph | a5ed658 | 2021-06-17 22:08:47 -0700 | [diff] [blame] | 87 | EXPECT_EQ(rc, PLDM_SUCCESS); |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 88 | |
| 89 | pldm::Response response(sizeof(pldm_msg_hdr) + sizeof(uint8_t)); |
| 90 | auto responsePtr = reinterpret_cast<const pldm_msg*>(response.data()); |
| 91 | reqHandler.handleResponse(eid, instanceId, 0, 0, responsePtr, |
| 92 | sizeof(response)); |
| 93 | |
Tom Joseph | a5ed658 | 2021-06-17 22:08:47 -0700 | [diff] [blame] | 94 | EXPECT_EQ(validResponse, true); |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | TEST_F(HandlerTest, singleRequestInstanceIdTimerExpired) |
| 98 | { |
Manojkiran Eda | 9fffea2 | 2021-10-27 16:03:27 +0530 | [diff] [blame] | 99 | Handler<NiceMock<MockRequest>> reqHandler( |
| 100 | fd, event, dbusImplReq, false, 90000, seconds(1), 2, milliseconds(100)); |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 101 | pldm::Request request{}; |
| 102 | auto instanceId = dbusImplReq.getInstanceId(eid); |
Andrew Jeffery | 7c1dc7e | 2023-04-28 14:52:16 +0930 | [diff] [blame^] | 103 | EXPECT_EQ(instanceId, 0); |
Tom Joseph | a5ed658 | 2021-06-17 22:08:47 -0700 | [diff] [blame] | 104 | auto rc = reqHandler.registerRequest( |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 105 | eid, instanceId, 0, 0, std::move(request), |
| 106 | std::move(std::bind_front(&HandlerTest::pldmResponseCallBack, this))); |
Tom Joseph | a5ed658 | 2021-06-17 22:08:47 -0700 | [diff] [blame] | 107 | EXPECT_EQ(rc, PLDM_SUCCESS); |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 108 | |
| 109 | // Waiting for 500ms so that the instance ID expiry callback is invoked |
| 110 | waitEventExpiry(milliseconds(500)); |
| 111 | |
Tom Joseph | a5ed658 | 2021-06-17 22:08:47 -0700 | [diff] [blame] | 112 | EXPECT_EQ(nullResponse, true); |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | TEST_F(HandlerTest, multipleRequestResponseScenario) |
| 116 | { |
Manojkiran Eda | 9fffea2 | 2021-10-27 16:03:27 +0530 | [diff] [blame] | 117 | Handler<NiceMock<MockRequest>> reqHandler( |
| 118 | fd, event, dbusImplReq, false, 90000, seconds(2), 2, milliseconds(100)); |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 119 | pldm::Request request{}; |
| 120 | auto instanceId = dbusImplReq.getInstanceId(eid); |
Andrew Jeffery | 7c1dc7e | 2023-04-28 14:52:16 +0930 | [diff] [blame^] | 121 | EXPECT_EQ(instanceId, 0); |
Tom Joseph | a5ed658 | 2021-06-17 22:08:47 -0700 | [diff] [blame] | 122 | auto rc = reqHandler.registerRequest( |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 123 | eid, instanceId, 0, 0, std::move(request), |
| 124 | std::move(std::bind_front(&HandlerTest::pldmResponseCallBack, this))); |
Tom Joseph | a5ed658 | 2021-06-17 22:08:47 -0700 | [diff] [blame] | 125 | EXPECT_EQ(rc, PLDM_SUCCESS); |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 126 | |
| 127 | pldm::Request requestNxt{}; |
| 128 | auto instanceIdNxt = dbusImplReq.getInstanceId(eid); |
Andrew Jeffery | 7c1dc7e | 2023-04-28 14:52:16 +0930 | [diff] [blame^] | 129 | EXPECT_EQ(instanceIdNxt, 1); |
Tom Joseph | a5ed658 | 2021-06-17 22:08:47 -0700 | [diff] [blame] | 130 | rc = reqHandler.registerRequest( |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 131 | eid, instanceIdNxt, 0, 0, std::move(requestNxt), |
| 132 | std::move(std::bind_front(&HandlerTest::pldmResponseCallBack, this))); |
Tom Joseph | a5ed658 | 2021-06-17 22:08:47 -0700 | [diff] [blame] | 133 | EXPECT_EQ(rc, PLDM_SUCCESS); |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 134 | |
| 135 | pldm::Response response(sizeof(pldm_msg_hdr) + sizeof(uint8_t)); |
| 136 | auto responsePtr = reinterpret_cast<const pldm_msg*>(response.data()); |
| 137 | reqHandler.handleResponse(eid, instanceIdNxt, 0, 0, responsePtr, |
| 138 | sizeof(response)); |
Tom Joseph | a5ed658 | 2021-06-17 22:08:47 -0700 | [diff] [blame] | 139 | EXPECT_EQ(validResponse, true); |
| 140 | EXPECT_EQ(callbackCount, 1); |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 141 | validResponse = false; |
| 142 | |
| 143 | // Waiting for 500ms and handle the response for the first request, to |
| 144 | // simulate a delayed response for the first request |
| 145 | waitEventExpiry(milliseconds(500)); |
| 146 | |
| 147 | reqHandler.handleResponse(eid, instanceId, 0, 0, responsePtr, |
| 148 | sizeof(response)); |
| 149 | |
Tom Joseph | a5ed658 | 2021-06-17 22:08:47 -0700 | [diff] [blame] | 150 | EXPECT_EQ(validResponse, true); |
| 151 | EXPECT_EQ(callbackCount, 2); |
Manojkiran Eda | 9fffea2 | 2021-10-27 16:03:27 +0530 | [diff] [blame] | 152 | } |