blob: 69facdbe9902805d5c48b755560dcd60ebe3e180 [file] [log] [blame]
Tom Joseph74f27c72021-05-16 07:58:53 -07001#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 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>
9
Tom Joseph74f27c72021-05-16 07:58:53 -070010#include <gmock/gmock.h>
11#include <gtest/gtest.h>
12
13using namespace pldm::requester;
14using namespace std::chrono;
15
16using ::testing::AtLeast;
17using ::testing::Between;
18using ::testing::Exactly;
19using ::testing::NiceMock;
20using ::testing::Return;
21
22class HandlerTest : public testing::Test
23{
24 protected:
25 HandlerTest() :
Andrew Jeffery7c1dc7e2023-04-28 14:52:16 +093026 event(sdeventplus::Event::get_default()), instanceIdDb(),
Tom Joseph74f27c72021-05-16 07:58:53 -070027 dbusImplReq(pldm::utils::DBusHandler::getBus(),
Andrew Jeffery7c1dc7e2023-04-28 14:52:16 +093028 "/xyz/openbmc_project/pldm", instanceIdDb)
Tom Joseph74f27c72021-05-16 07:58:53 -070029 {}
30
31 int fd = 0;
32 mctp_eid_t eid = 0;
33 sdeventplus::Event event;
Andrew Jeffery7c1dc7e2023-04-28 14:52:16 +093034 TestInstanceIdDb instanceIdDb;
Tom Joseph74f27c72021-05-16 07:58:53 -070035 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
77TEST_F(HandlerTest, singleRequestResponseScenario)
78{
Manojkiran Eda9fffea22021-10-27 16:03:27 +053079 Handler<NiceMock<MockRequest>> reqHandler(
80 fd, event, dbusImplReq, false, 90000, seconds(1), 2, milliseconds(100));
Tom Joseph74f27c72021-05-16 07:58:53 -070081 pldm::Request request{};
82 auto instanceId = dbusImplReq.getInstanceId(eid);
Andrew Jeffery7c1dc7e2023-04-28 14:52:16 +093083 EXPECT_EQ(instanceId, 0);
Tom Josepha5ed6582021-06-17 22:08:47 -070084 auto rc = reqHandler.registerRequest(
Tom Joseph74f27c72021-05-16 07:58:53 -070085 eid, instanceId, 0, 0, std::move(request),
86 std::move(std::bind_front(&HandlerTest::pldmResponseCallBack, this)));
Tom Josepha5ed6582021-06-17 22:08:47 -070087 EXPECT_EQ(rc, PLDM_SUCCESS);
Tom Joseph74f27c72021-05-16 07:58:53 -070088
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 Josepha5ed6582021-06-17 22:08:47 -070094 EXPECT_EQ(validResponse, true);
Tom Joseph74f27c72021-05-16 07:58:53 -070095}
96
97TEST_F(HandlerTest, singleRequestInstanceIdTimerExpired)
98{
Manojkiran Eda9fffea22021-10-27 16:03:27 +053099 Handler<NiceMock<MockRequest>> reqHandler(
100 fd, event, dbusImplReq, false, 90000, seconds(1), 2, milliseconds(100));
Tom Joseph74f27c72021-05-16 07:58:53 -0700101 pldm::Request request{};
102 auto instanceId = dbusImplReq.getInstanceId(eid);
Andrew Jeffery7c1dc7e2023-04-28 14:52:16 +0930103 EXPECT_EQ(instanceId, 0);
Tom Josepha5ed6582021-06-17 22:08:47 -0700104 auto rc = reqHandler.registerRequest(
Tom Joseph74f27c72021-05-16 07:58:53 -0700105 eid, instanceId, 0, 0, std::move(request),
106 std::move(std::bind_front(&HandlerTest::pldmResponseCallBack, this)));
Tom Josepha5ed6582021-06-17 22:08:47 -0700107 EXPECT_EQ(rc, PLDM_SUCCESS);
Tom Joseph74f27c72021-05-16 07:58:53 -0700108
109 // Waiting for 500ms so that the instance ID expiry callback is invoked
110 waitEventExpiry(milliseconds(500));
111
Tom Josepha5ed6582021-06-17 22:08:47 -0700112 EXPECT_EQ(nullResponse, true);
Tom Joseph74f27c72021-05-16 07:58:53 -0700113}
114
115TEST_F(HandlerTest, multipleRequestResponseScenario)
116{
Manojkiran Eda9fffea22021-10-27 16:03:27 +0530117 Handler<NiceMock<MockRequest>> reqHandler(
118 fd, event, dbusImplReq, false, 90000, seconds(2), 2, milliseconds(100));
Tom Joseph74f27c72021-05-16 07:58:53 -0700119 pldm::Request request{};
120 auto instanceId = dbusImplReq.getInstanceId(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{};
128 auto instanceIdNxt = dbusImplReq.getInstanceId(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}