blob: 625197430c69974b0524e6ad56e3b82a75d15828 [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"
Andrew Jefferya330b2f2023-05-04 14:55:37 +09304#include "pldmd/instance_id.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>
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:
Andrew Jefferya330b2f2023-05-04 14:55:37 +093025 HandlerTest() : event(sdeventplus::Event::get_default()), instanceIdDb() {}
Tom Joseph74f27c72021-05-16 07:58:53 -070026
27 int fd = 0;
28 mctp_eid_t eid = 0;
29 sdeventplus::Event event;
Andrew Jeffery7c1dc7e2023-04-28 14:52:16 +093030 TestInstanceIdDb instanceIdDb;
Tom Joseph74f27c72021-05-16 07:58:53 -070031
32 /** @brief This function runs the sd_event_run in a loop till all the events
33 * in the testcase are dispatched and exits when there are no events
34 * for the timeout time.
35 *
36 * @param[in] timeout - maximum time to wait for an event
37 */
38 void waitEventExpiry(milliseconds timeout)
39 {
40 while (1)
41 {
42 auto sleepTime = duration_cast<microseconds>(timeout);
43 // Returns 0 on timeout
44 if (!sd_event_run(event.get(), sleepTime.count()))
45 {
46 break;
47 }
48 }
49 }
50
51 public:
52 bool nullResponse = false;
53 bool validResponse = false;
54 int callbackCount = 0;
55 bool response2 = false;
56
57 void pldmResponseCallBack(mctp_eid_t /*eid*/, const pldm_msg* response,
58 size_t respMsgLen)
59 {
60 if (response == nullptr && respMsgLen == 0)
61 {
62 nullResponse = true;
63 }
64 else
65 {
66 validResponse = true;
67 }
68 callbackCount++;
69 }
70};
71
72TEST_F(HandlerTest, singleRequestResponseScenario)
73{
Andrew Jefferya330b2f2023-05-04 14:55:37 +093074 Handler<NiceMock<MockRequest>> reqHandler(fd, event, instanceIdDb, false,
75 90000, seconds(1), 2,
76 milliseconds(100));
Tom Joseph74f27c72021-05-16 07:58:53 -070077 pldm::Request request{};
Andrew Jefferya330b2f2023-05-04 14:55:37 +093078 auto instanceId = instanceIdDb.next(eid);
Andrew Jeffery7c1dc7e2023-04-28 14:52:16 +093079 EXPECT_EQ(instanceId, 0);
Tom Josepha5ed6582021-06-17 22:08:47 -070080 auto rc = reqHandler.registerRequest(
Tom Joseph74f27c72021-05-16 07:58:53 -070081 eid, instanceId, 0, 0, std::move(request),
82 std::move(std::bind_front(&HandlerTest::pldmResponseCallBack, this)));
Tom Josepha5ed6582021-06-17 22:08:47 -070083 EXPECT_EQ(rc, PLDM_SUCCESS);
Tom Joseph74f27c72021-05-16 07:58:53 -070084
85 pldm::Response response(sizeof(pldm_msg_hdr) + sizeof(uint8_t));
86 auto responsePtr = reinterpret_cast<const pldm_msg*>(response.data());
87 reqHandler.handleResponse(eid, instanceId, 0, 0, responsePtr,
88 sizeof(response));
89
Tom Josepha5ed6582021-06-17 22:08:47 -070090 EXPECT_EQ(validResponse, true);
Tom Joseph74f27c72021-05-16 07:58:53 -070091}
92
93TEST_F(HandlerTest, singleRequestInstanceIdTimerExpired)
94{
Andrew Jefferya330b2f2023-05-04 14:55:37 +093095 Handler<NiceMock<MockRequest>> reqHandler(fd, event, instanceIdDb, false,
96 90000, seconds(1), 2,
97 milliseconds(100));
Tom Joseph74f27c72021-05-16 07:58:53 -070098 pldm::Request request{};
Andrew Jefferya330b2f2023-05-04 14:55:37 +093099 auto instanceId = instanceIdDb.next(eid);
Andrew Jeffery7c1dc7e2023-04-28 14:52:16 +0930100 EXPECT_EQ(instanceId, 0);
Tom Josepha5ed6582021-06-17 22:08:47 -0700101 auto rc = reqHandler.registerRequest(
Tom Joseph74f27c72021-05-16 07:58:53 -0700102 eid, instanceId, 0, 0, std::move(request),
103 std::move(std::bind_front(&HandlerTest::pldmResponseCallBack, this)));
Tom Josepha5ed6582021-06-17 22:08:47 -0700104 EXPECT_EQ(rc, PLDM_SUCCESS);
Tom Joseph74f27c72021-05-16 07:58:53 -0700105
106 // Waiting for 500ms so that the instance ID expiry callback is invoked
107 waitEventExpiry(milliseconds(500));
108
Tom Josepha5ed6582021-06-17 22:08:47 -0700109 EXPECT_EQ(nullResponse, true);
Tom Joseph74f27c72021-05-16 07:58:53 -0700110}
111
112TEST_F(HandlerTest, multipleRequestResponseScenario)
113{
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930114 Handler<NiceMock<MockRequest>> reqHandler(fd, event, instanceIdDb, false,
115 90000, seconds(2), 2,
116 milliseconds(100));
Tom Joseph74f27c72021-05-16 07:58:53 -0700117 pldm::Request request{};
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930118 auto instanceId = instanceIdDb.next(eid);
Andrew Jeffery7c1dc7e2023-04-28 14:52:16 +0930119 EXPECT_EQ(instanceId, 0);
Tom Josepha5ed6582021-06-17 22:08:47 -0700120 auto rc = reqHandler.registerRequest(
Tom Joseph74f27c72021-05-16 07:58:53 -0700121 eid, instanceId, 0, 0, std::move(request),
122 std::move(std::bind_front(&HandlerTest::pldmResponseCallBack, this)));
Tom Josepha5ed6582021-06-17 22:08:47 -0700123 EXPECT_EQ(rc, PLDM_SUCCESS);
Tom Joseph74f27c72021-05-16 07:58:53 -0700124
125 pldm::Request requestNxt{};
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930126 auto instanceIdNxt = instanceIdDb.next(eid);
Andrew Jeffery7c1dc7e2023-04-28 14:52:16 +0930127 EXPECT_EQ(instanceIdNxt, 1);
Tom Josepha5ed6582021-06-17 22:08:47 -0700128 rc = reqHandler.registerRequest(
Tom Joseph74f27c72021-05-16 07:58:53 -0700129 eid, instanceIdNxt, 0, 0, std::move(requestNxt),
130 std::move(std::bind_front(&HandlerTest::pldmResponseCallBack, this)));
Tom Josepha5ed6582021-06-17 22:08:47 -0700131 EXPECT_EQ(rc, PLDM_SUCCESS);
Tom Joseph74f27c72021-05-16 07:58:53 -0700132
133 pldm::Response response(sizeof(pldm_msg_hdr) + sizeof(uint8_t));
134 auto responsePtr = reinterpret_cast<const pldm_msg*>(response.data());
135 reqHandler.handleResponse(eid, instanceIdNxt, 0, 0, responsePtr,
136 sizeof(response));
Tom Josepha5ed6582021-06-17 22:08:47 -0700137 EXPECT_EQ(validResponse, true);
138 EXPECT_EQ(callbackCount, 1);
Tom Joseph74f27c72021-05-16 07:58:53 -0700139 validResponse = false;
140
141 // Waiting for 500ms and handle the response for the first request, to
142 // simulate a delayed response for the first request
143 waitEventExpiry(milliseconds(500));
144
145 reqHandler.handleResponse(eid, instanceId, 0, 0, responsePtr,
146 sizeof(response));
147
Tom Josepha5ed6582021-06-17 22:08:47 -0700148 EXPECT_EQ(validResponse, true);
149 EXPECT_EQ(callbackCount, 2);
Manojkiran Eda9fffea22021-10-27 16:03:27 +0530150}