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