blob: 327817c7bc49eb5f9a985501a70cdcfc101a3280 [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
Gilbert Chena85c69d2024-01-09 21:25:26 +070011#include <sdbusplus/async.hpp>
12
Tom Joseph74f27c72021-05-16 07:58:53 -070013#include <gmock/gmock.h>
14#include <gtest/gtest.h>
15
16using namespace pldm::requester;
17using namespace std::chrono;
18
19using ::testing::AtLeast;
20using ::testing::Between;
21using ::testing::Exactly;
22using ::testing::NiceMock;
23using ::testing::Return;
24
25class HandlerTest : public testing::Test
26{
27 protected:
Andrew Jefferya330b2f2023-05-04 14:55:37 +093028 HandlerTest() : event(sdeventplus::Event::get_default()), instanceIdDb() {}
Tom Joseph74f27c72021-05-16 07:58:53 -070029
30 int fd = 0;
31 mctp_eid_t eid = 0;
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +100032 PldmTransport* pldmTransport = nullptr;
Tom Joseph74f27c72021-05-16 07:58:53 -070033 sdeventplus::Event event;
Andrew Jeffery7c1dc7e2023-04-28 14:52:16 +093034 TestInstanceIdDb instanceIdDb;
Tom Joseph74f27c72021-05-16 07:58:53 -070035
36 /** @brief This function runs the sd_event_run in a loop till all the events
37 * in the testcase are dispatched and exits when there are no events
38 * for the timeout time.
39 *
40 * @param[in] timeout - maximum time to wait for an event
41 */
42 void waitEventExpiry(milliseconds timeout)
43 {
44 while (1)
45 {
46 auto sleepTime = duration_cast<microseconds>(timeout);
47 // Returns 0 on timeout
48 if (!sd_event_run(event.get(), sleepTime.count()))
49 {
50 break;
51 }
52 }
53 }
54
55 public:
56 bool nullResponse = false;
57 bool validResponse = false;
58 int callbackCount = 0;
59 bool response2 = false;
60
61 void pldmResponseCallBack(mctp_eid_t /*eid*/, const pldm_msg* response,
62 size_t respMsgLen)
63 {
64 if (response == nullptr && respMsgLen == 0)
65 {
66 nullResponse = true;
67 }
68 else
69 {
70 validResponse = true;
71 }
72 callbackCount++;
73 }
74};
75
76TEST_F(HandlerTest, singleRequestResponseScenario)
77{
Patrick Williams16c2a0a2024-08-16 15:20:59 -040078 Handler<NiceMock<MockRequest>> reqHandler(
79 pldmTransport, event, instanceIdDb, false, seconds(1), 2,
80 milliseconds(100));
Tom Joseph74f27c72021-05-16 07:58:53 -070081 pldm::Request request{};
Andrew Jefferya330b2f2023-05-04 14:55:37 +093082 auto instanceId = instanceIdDb.next(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),
Andrew Jefferyc14fb4b2024-07-25 22:13:09 +093086 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,
Pavithra Barithaya93f1e002023-07-11 10:14:13 -050092 response.size());
Tom Joseph74f27c72021-05-16 07:58:53 -070093
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{
Patrick Williams16c2a0a2024-08-16 15:20:59 -040099 Handler<NiceMock<MockRequest>> reqHandler(
100 pldmTransport, event, instanceIdDb, false, seconds(1), 2,
101 milliseconds(100));
Tom Joseph74f27c72021-05-16 07:58:53 -0700102 pldm::Request request{};
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930103 auto instanceId = instanceIdDb.next(eid);
Andrew Jeffery7c1dc7e2023-04-28 14:52:16 +0930104 EXPECT_EQ(instanceId, 0);
Tom Josepha5ed6582021-06-17 22:08:47 -0700105 auto rc = reqHandler.registerRequest(
Tom Joseph74f27c72021-05-16 07:58:53 -0700106 eid, instanceId, 0, 0, std::move(request),
Andrew Jefferyc14fb4b2024-07-25 22:13:09 +0930107 std::bind_front(&HandlerTest::pldmResponseCallBack, this));
Tom Josepha5ed6582021-06-17 22:08:47 -0700108 EXPECT_EQ(rc, PLDM_SUCCESS);
Tom Joseph74f27c72021-05-16 07:58:53 -0700109
110 // Waiting for 500ms so that the instance ID expiry callback is invoked
111 waitEventExpiry(milliseconds(500));
112
Tom Josepha5ed6582021-06-17 22:08:47 -0700113 EXPECT_EQ(nullResponse, true);
Tom Joseph74f27c72021-05-16 07:58:53 -0700114}
115
116TEST_F(HandlerTest, multipleRequestResponseScenario)
117{
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400118 Handler<NiceMock<MockRequest>> reqHandler(
119 pldmTransport, event, instanceIdDb, false, seconds(2), 2,
120 milliseconds(100));
Tom Joseph74f27c72021-05-16 07:58:53 -0700121 pldm::Request request{};
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930122 auto instanceId = instanceIdDb.next(eid);
Andrew Jeffery7c1dc7e2023-04-28 14:52:16 +0930123 EXPECT_EQ(instanceId, 0);
Tom Josepha5ed6582021-06-17 22:08:47 -0700124 auto rc = reqHandler.registerRequest(
Tom Joseph74f27c72021-05-16 07:58:53 -0700125 eid, instanceId, 0, 0, std::move(request),
Andrew Jefferyc14fb4b2024-07-25 22:13:09 +0930126 std::bind_front(&HandlerTest::pldmResponseCallBack, this));
Tom Josepha5ed6582021-06-17 22:08:47 -0700127 EXPECT_EQ(rc, PLDM_SUCCESS);
Tom Joseph74f27c72021-05-16 07:58:53 -0700128
129 pldm::Request requestNxt{};
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930130 auto instanceIdNxt = instanceIdDb.next(eid);
Andrew Jeffery7c1dc7e2023-04-28 14:52:16 +0930131 EXPECT_EQ(instanceIdNxt, 1);
Tom Josepha5ed6582021-06-17 22:08:47 -0700132 rc = reqHandler.registerRequest(
Tom Joseph74f27c72021-05-16 07:58:53 -0700133 eid, instanceIdNxt, 0, 0, std::move(requestNxt),
Andrew Jefferyc14fb4b2024-07-25 22:13:09 +0930134 std::bind_front(&HandlerTest::pldmResponseCallBack, this));
Tom Josepha5ed6582021-06-17 22:08:47 -0700135 EXPECT_EQ(rc, PLDM_SUCCESS);
Tom Joseph74f27c72021-05-16 07:58:53 -0700136
137 pldm::Response response(sizeof(pldm_msg_hdr) + sizeof(uint8_t));
138 auto responsePtr = reinterpret_cast<const pldm_msg*>(response.data());
Thu Nguyen4ddee3a2023-08-03 08:20:03 +0700139 reqHandler.handleResponse(eid, instanceId, 0, 0, responsePtr,
Pavithra Barithaya93f1e002023-07-11 10:14:13 -0500140 response.size());
Tom Josepha5ed6582021-06-17 22:08:47 -0700141 EXPECT_EQ(validResponse, true);
142 EXPECT_EQ(callbackCount, 1);
Tom Joseph74f27c72021-05-16 07:58:53 -0700143 validResponse = false;
144
145 // Waiting for 500ms and handle the response for the first request, to
146 // simulate a delayed response for the first request
147 waitEventExpiry(milliseconds(500));
148
Thu Nguyen4ddee3a2023-08-03 08:20:03 +0700149 reqHandler.handleResponse(eid, instanceIdNxt, 0, 0, responsePtr,
Pavithra Barithaya93f1e002023-07-11 10:14:13 -0500150 response.size());
Tom Joseph74f27c72021-05-16 07:58:53 -0700151
Tom Josepha5ed6582021-06-17 22:08:47 -0700152 EXPECT_EQ(validResponse, true);
153 EXPECT_EQ(callbackCount, 2);
Manojkiran Eda9fffea22021-10-27 16:03:27 +0530154}
Gilbert Chena85c69d2024-01-09 21:25:26 +0700155
156TEST_F(HandlerTest, singleRequestResponseScenarioUsingCoroutine)
157{
158 exec::async_scope scope;
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400159 Handler<NiceMock<MockRequest>> reqHandler(
160 pldmTransport, event, instanceIdDb, false, seconds(1), 2,
161 milliseconds(100));
Gilbert Chena85c69d2024-01-09 21:25:26 +0700162
163 auto instanceId = instanceIdDb.next(eid);
164 EXPECT_EQ(instanceId, 0);
165
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400166 scope.spawn(
167 stdexec::just() | stdexec::let_value([&] -> exec::task<void> {
168 pldm::Request request(sizeof(pldm_msg_hdr) + sizeof(uint8_t), 0);
169 const pldm_msg* responseMsg;
170 size_t responseLen;
171 int rc = PLDM_SUCCESS;
Gilbert Chena85c69d2024-01-09 21:25:26 +0700172
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400173 auto requestPtr = reinterpret_cast<pldm_msg*>(request.data());
174 requestPtr->hdr.instance_id = instanceId;
Gilbert Chena85c69d2024-01-09 21:25:26 +0700175
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400176 try
177 {
178 std::tie(rc, responseMsg, responseLen) =
179 co_await reqHandler.sendRecvMsg(eid, std::move(request));
180 }
181 catch (...)
182 {
183 std::rethrow_exception(std::current_exception());
184 }
Gilbert Chena85c69d2024-01-09 21:25:26 +0700185
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400186 EXPECT_NE(responseLen, 0);
Gilbert Chena85c69d2024-01-09 21:25:26 +0700187
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400188 this->pldmResponseCallBack(eid, responseMsg, responseLen);
Gilbert Chena85c69d2024-01-09 21:25:26 +0700189
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400190 EXPECT_EQ(validResponse, true);
191 }),
192 exec::default_task_context<void>(exec::inline_scheduler{}));
Gilbert Chena85c69d2024-01-09 21:25:26 +0700193
194 pldm::Response mockResponse(sizeof(pldm_msg_hdr) + sizeof(uint8_t), 0);
195 auto mockResponsePtr =
196 reinterpret_cast<const pldm_msg*>(mockResponse.data());
197 reqHandler.handleResponse(eid, instanceId, 0, 0, mockResponsePtr,
198 mockResponse.size() - sizeof(pldm_msg_hdr));
199
200 stdexec::sync_wait(scope.on_empty());
201}
202
203TEST_F(HandlerTest, singleRequestCancellationScenarioUsingCoroutine)
204{
205 exec::async_scope scope;
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400206 Handler<NiceMock<MockRequest>> reqHandler(
207 pldmTransport, event, instanceIdDb, false, seconds(1), 2,
208 milliseconds(100));
Gilbert Chena85c69d2024-01-09 21:25:26 +0700209 auto instanceId = instanceIdDb.next(eid);
210 EXPECT_EQ(instanceId, 0);
211
212 bool stopped = false;
213
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400214 scope.spawn(
215 stdexec::just() | stdexec::let_value([&] -> exec::task<void> {
216 pldm::Request request(sizeof(pldm_msg_hdr) + sizeof(uint8_t), 0);
217 pldm::Response response;
Gilbert Chena85c69d2024-01-09 21:25:26 +0700218
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400219 auto requestPtr = reinterpret_cast<pldm_msg*>(request.data());
220 requestPtr->hdr.instance_id = instanceId;
Gilbert Chena85c69d2024-01-09 21:25:26 +0700221
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400222 co_await reqHandler.sendRecvMsg(eid, std::move(request));
Gilbert Chena85c69d2024-01-09 21:25:26 +0700223
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400224 EXPECT_TRUE(false); // unreachable
225 }) | stdexec::upon_stopped([&] { stopped = true; }),
226 exec::default_task_context<void>(exec::inline_scheduler{}));
Gilbert Chena85c69d2024-01-09 21:25:26 +0700227
228 scope.request_stop();
229
230 EXPECT_TRUE(stopped);
231
232 stdexec::sync_wait(scope.on_empty());
233}
234
235TEST_F(HandlerTest, asyncRequestResponseByCoroutine)
236{
237 struct _
238 {
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400239 static exec::task<uint8_t>
240 getTIDTask(Handler<MockRequest>& handler, mctp_eid_t eid,
241 uint8_t instanceId, uint8_t& tid)
Gilbert Chena85c69d2024-01-09 21:25:26 +0700242 {
243 pldm::Request request(sizeof(pldm_msg_hdr), 0);
244 auto requestMsg = reinterpret_cast<pldm_msg*>(request.data());
245 const pldm_msg* responseMsg;
246 size_t responseLen;
247
248 auto rc = encode_get_tid_req(instanceId, requestMsg);
249 EXPECT_EQ(rc, PLDM_SUCCESS);
250
Thu Nguyen6b901e42024-07-10 15:21:10 +0700251 std::tie(rc, responseMsg, responseLen) =
Gilbert Chena85c69d2024-01-09 21:25:26 +0700252 co_await handler.sendRecvMsg(eid, std::move(request));
253 EXPECT_NE(responseLen, 0);
254
255 uint8_t cc = 0;
256 rc = decode_get_tid_resp(responseMsg, responseLen, &cc, &tid);
257 EXPECT_EQ(rc, PLDM_SUCCESS);
258
259 co_return cc;
260 }
261 };
262
263 exec::async_scope scope;
264 Handler<MockRequest> reqHandler(pldmTransport, event, instanceIdDb, false,
265 seconds(1), 2, milliseconds(100));
266 auto instanceId = instanceIdDb.next(eid);
267
268 uint8_t expectedTid = 1;
269
270 // Execute a coroutine to send getTID command. The coroutine is suspended
271 // until reqHandler.handleResponse() is received.
272 scope.spawn(stdexec::just() | stdexec::let_value([&] -> exec::task<void> {
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400273 uint8_t respTid = 0;
Gilbert Chena85c69d2024-01-09 21:25:26 +0700274
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400275 co_await _::getTIDTask(reqHandler, eid, instanceId,
276 respTid);
Gilbert Chena85c69d2024-01-09 21:25:26 +0700277
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400278 EXPECT_EQ(expectedTid, respTid);
279 }),
Patrick Williams61be7342024-07-30 16:55:33 -0500280 exec::default_task_context<void>(exec::inline_scheduler{}));
Gilbert Chena85c69d2024-01-09 21:25:26 +0700281
282 pldm::Response mockResponse(sizeof(pldm_msg_hdr) + PLDM_GET_TID_RESP_BYTES,
283 0);
284 auto mockResponseMsg = reinterpret_cast<pldm_msg*>(mockResponse.data());
285
286 // Compose response message of getTID command
287 encode_get_tid_resp(instanceId, PLDM_SUCCESS, expectedTid, mockResponseMsg);
288
289 // Send response back to resume getTID coroutine to update respTid by
290 // calling reqHandler.handleResponse() manually
291 reqHandler.handleResponse(eid, instanceId, PLDM_BASE, PLDM_GET_TID,
292 mockResponseMsg,
293 mockResponse.size() - sizeof(pldm_msg_hdr));
294
295 stdexec::sync_wait(scope.on_empty());
296}