blob: b656c1a6259068f0f07e7d0840fe92dccfaf240e [file] [log] [blame]
Andrew Jeffery2abbce72023-10-18 10:17:35 +10301#include "common/instance_id.hpp"
Tom Joseph74f27c72021-05-16 07:58:53 -07002#include "common/types.hpp"
3#include "common/utils.hpp"
4#include "mock_request.hpp"
Tom Joseph74f27c72021-05-16 07:58:53 -07005#include "requester/handler.hpp"
Andrew Jeffery7c1dc7e2023-04-28 14:52:16 +09306#include "test/test_instance_id.hpp"
Tom Joseph74f27c72021-05-16 07:58:53 -07007
George Liuc453e162022-12-21 17:16:23 +08008#include <libpldm/base.h>
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +10009#include <libpldm/transport.h>
George Liuc453e162022-12-21 17:16:23 +080010
Tom Joseph74f27c72021-05-16 07:58:53 -070011#include <gmock/gmock.h>
12#include <gtest/gtest.h>
13
14using namespace pldm::requester;
15using namespace std::chrono;
16
17using ::testing::AtLeast;
18using ::testing::Between;
19using ::testing::Exactly;
20using ::testing::NiceMock;
21using ::testing::Return;
22
23class HandlerTest : public testing::Test
24{
25 protected:
Andrew Jefferya330b2f2023-05-04 14:55:37 +093026 HandlerTest() : event(sdeventplus::Event::get_default()), instanceIdDb() {}
Tom Joseph74f27c72021-05-16 07:58:53 -070027
28 int fd = 0;
29 mctp_eid_t eid = 0;
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +100030 PldmTransport* pldmTransport = nullptr;
Tom Joseph74f27c72021-05-16 07:58:53 -070031 sdeventplus::Event event;
Andrew Jeffery7c1dc7e2023-04-28 14:52:16 +093032 TestInstanceIdDb instanceIdDb;
Tom Joseph74f27c72021-05-16 07:58:53 -070033
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
74TEST_F(HandlerTest, singleRequestResponseScenario)
75{
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +100076 Handler<NiceMock<MockRequest>> reqHandler(pldmTransport, event,
77 instanceIdDb, false, seconds(1),
78 2, milliseconds(100));
Tom Joseph74f27c72021-05-16 07:58:53 -070079 pldm::Request request{};
Andrew Jefferya330b2f2023-05-04 14:55:37 +093080 auto instanceId = instanceIdDb.next(eid);
Andrew Jeffery7c1dc7e2023-04-28 14:52:16 +093081 EXPECT_EQ(instanceId, 0);
Tom Josepha5ed6582021-06-17 22:08:47 -070082 auto rc = reqHandler.registerRequest(
Tom Joseph74f27c72021-05-16 07:58:53 -070083 eid, instanceId, 0, 0, std::move(request),
84 std::move(std::bind_front(&HandlerTest::pldmResponseCallBack, this)));
Tom Josepha5ed6582021-06-17 22:08:47 -070085 EXPECT_EQ(rc, PLDM_SUCCESS);
Tom Joseph74f27c72021-05-16 07:58:53 -070086
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 Josepha5ed6582021-06-17 22:08:47 -070092 EXPECT_EQ(validResponse, true);
Tom Joseph74f27c72021-05-16 07:58:53 -070093}
94
95TEST_F(HandlerTest, singleRequestInstanceIdTimerExpired)
96{
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +100097 Handler<NiceMock<MockRequest>> reqHandler(pldmTransport, event,
98 instanceIdDb, false, seconds(1),
99 2, milliseconds(100));
Tom Joseph74f27c72021-05-16 07:58:53 -0700100 pldm::Request request{};
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930101 auto instanceId = instanceIdDb.next(eid);
Andrew Jeffery7c1dc7e2023-04-28 14:52:16 +0930102 EXPECT_EQ(instanceId, 0);
Tom Josepha5ed6582021-06-17 22:08:47 -0700103 auto rc = reqHandler.registerRequest(
Tom Joseph74f27c72021-05-16 07:58:53 -0700104 eid, instanceId, 0, 0, std::move(request),
105 std::move(std::bind_front(&HandlerTest::pldmResponseCallBack, this)));
Tom Josepha5ed6582021-06-17 22:08:47 -0700106 EXPECT_EQ(rc, PLDM_SUCCESS);
Tom Joseph74f27c72021-05-16 07:58:53 -0700107
108 // Waiting for 500ms so that the instance ID expiry callback is invoked
109 waitEventExpiry(milliseconds(500));
110
Tom Josepha5ed6582021-06-17 22:08:47 -0700111 EXPECT_EQ(nullResponse, true);
Tom Joseph74f27c72021-05-16 07:58:53 -0700112}
113
114TEST_F(HandlerTest, multipleRequestResponseScenario)
115{
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +1000116 Handler<NiceMock<MockRequest>> reqHandler(pldmTransport, event,
117 instanceIdDb, false, seconds(2),
118 2, milliseconds(100));
Tom Joseph74f27c72021-05-16 07:58:53 -0700119 pldm::Request request{};
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930120 auto instanceId = instanceIdDb.next(eid);
Andrew Jeffery7c1dc7e2023-04-28 14:52:16 +0930121 EXPECT_EQ(instanceId, 0);
Tom Josepha5ed6582021-06-17 22:08:47 -0700122 auto rc = reqHandler.registerRequest(
Tom Joseph74f27c72021-05-16 07:58:53 -0700123 eid, instanceId, 0, 0, std::move(request),
124 std::move(std::bind_front(&HandlerTest::pldmResponseCallBack, this)));
Tom Josepha5ed6582021-06-17 22:08:47 -0700125 EXPECT_EQ(rc, PLDM_SUCCESS);
Tom Joseph74f27c72021-05-16 07:58:53 -0700126
127 pldm::Request requestNxt{};
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930128 auto instanceIdNxt = instanceIdDb.next(eid);
Andrew Jeffery7c1dc7e2023-04-28 14:52:16 +0930129 EXPECT_EQ(instanceIdNxt, 1);
Tom Josepha5ed6582021-06-17 22:08:47 -0700130 rc = reqHandler.registerRequest(
Tom Joseph74f27c72021-05-16 07:58:53 -0700131 eid, instanceIdNxt, 0, 0, std::move(requestNxt),
132 std::move(std::bind_front(&HandlerTest::pldmResponseCallBack, this)));
Tom Josepha5ed6582021-06-17 22:08:47 -0700133 EXPECT_EQ(rc, PLDM_SUCCESS);
Tom Joseph74f27c72021-05-16 07:58:53 -0700134
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 Josepha5ed6582021-06-17 22:08:47 -0700139 EXPECT_EQ(validResponse, true);
140 EXPECT_EQ(callbackCount, 1);
Tom Joseph74f27c72021-05-16 07:58:53 -0700141 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 Josepha5ed6582021-06-17 22:08:47 -0700150 EXPECT_EQ(validResponse, true);
151 EXPECT_EQ(callbackCount, 2);
Manojkiran Eda9fffea22021-10-27 16:03:27 +0530152}