1de2a132aSGilbert Chen #include "common/instance_id.hpp"
2de2a132aSGilbert Chen #include "mock_terminus_manager.hpp"
3de2a132aSGilbert Chen #include "platform-mc/platform_manager.hpp"
4de2a132aSGilbert Chen #include "test/test_instance_id.hpp"
5de2a132aSGilbert Chen 
6de2a132aSGilbert Chen #include <sdeventplus/event.hpp>
7de2a132aSGilbert Chen 
8de2a132aSGilbert Chen #include <bitset>
9de2a132aSGilbert Chen 
10de2a132aSGilbert Chen #include <gtest/gtest.h>
11de2a132aSGilbert Chen 
12de2a132aSGilbert Chen class PlatformManagerTest : public testing::Test
13de2a132aSGilbert Chen {
14de2a132aSGilbert Chen   protected:
15de2a132aSGilbert Chen     PlatformManagerTest() :
16de2a132aSGilbert Chen         bus(pldm::utils::DBusHandler::getBus()),
17de2a132aSGilbert Chen         event(sdeventplus::Event::get_default()), instanceIdDb(),
18de2a132aSGilbert Chen         reqHandler(pldmTransport, event, instanceIdDb, false,
19de2a132aSGilbert Chen                    std::chrono::seconds(1), 2, std::chrono::milliseconds(100)),
20de2a132aSGilbert Chen         mockTerminusManager(event, reqHandler, instanceIdDb, termini, nullptr),
21de2a132aSGilbert Chen         platformManager(mockTerminusManager, termini)
22de2a132aSGilbert Chen     {}
23de2a132aSGilbert Chen 
24de2a132aSGilbert Chen     PldmTransport* pldmTransport = nullptr;
25de2a132aSGilbert Chen     sdbusplus::bus_t& bus;
26de2a132aSGilbert Chen     sdeventplus::Event event;
27de2a132aSGilbert Chen     TestInstanceIdDb instanceIdDb;
28de2a132aSGilbert Chen     pldm::requester::Handler<pldm::requester::Request> reqHandler;
29de2a132aSGilbert Chen     pldm::platform_mc::MockTerminusManager mockTerminusManager;
30de2a132aSGilbert Chen     pldm::platform_mc::PlatformManager platformManager;
31de2a132aSGilbert Chen     std::map<pldm_tid_t, std::shared_ptr<pldm::platform_mc::Terminus>> termini;
32de2a132aSGilbert Chen };
33de2a132aSGilbert Chen 
34de2a132aSGilbert Chen TEST_F(PlatformManagerTest, initTerminusTest)
35de2a132aSGilbert Chen {
36de2a132aSGilbert Chen     // Add terminus
37de2a132aSGilbert Chen     auto mappedTid = mockTerminusManager.mapTid(pldm::MctpInfo(10, "", "", 1));
38de2a132aSGilbert Chen     auto tid = mappedTid.value();
39de2a132aSGilbert Chen     termini[tid] = std::make_shared<pldm::platform_mc::Terminus>(
40de2a132aSGilbert Chen         tid, 1 << PLDM_BASE | 1 << PLDM_PLATFORM);
41de2a132aSGilbert Chen     auto terminus = termini[tid];
42de2a132aSGilbert Chen 
43de2a132aSGilbert Chen     /* Set supported command by terminus */
44de2a132aSGilbert Chen     auto size = PLDM_MAX_TYPES * (PLDM_MAX_CMDS_PER_TYPE / 8);
45de2a132aSGilbert Chen     std::vector<uint8_t> pldmCmds(size);
46de2a132aSGilbert Chen     uint8_t type = PLDM_PLATFORM;
47de2a132aSGilbert Chen     uint8_t cmd = PLDM_GET_PDR;
48de2a132aSGilbert Chen     auto idx = type * (PLDM_MAX_CMDS_PER_TYPE / 8) + (cmd / 8);
49de2a132aSGilbert Chen     pldmCmds[idx] = pldmCmds[idx] | (1 << (cmd % 8));
50de2a132aSGilbert Chen     cmd = PLDM_GET_PDR_REPOSITORY_INFO;
51de2a132aSGilbert Chen     idx = type * (PLDM_MAX_CMDS_PER_TYPE / 8) + (cmd / 8);
52de2a132aSGilbert Chen     pldmCmds[idx] = pldmCmds[idx] | (1 << (cmd % 8));
53de2a132aSGilbert Chen     termini[tid]->setSupportedCommands(pldmCmds);
54de2a132aSGilbert Chen 
55de2a132aSGilbert Chen     // queue getPDRRepositoryInfo response
56de2a132aSGilbert Chen     const size_t getPDRRepositoryInfoLen =
57de2a132aSGilbert Chen         PLDM_GET_PDR_REPOSITORY_INFO_RESP_BYTES;
58de2a132aSGilbert Chen     std::array<uint8_t, sizeof(pldm_msg_hdr) + getPDRRepositoryInfoLen>
59de2a132aSGilbert Chen         getPDRRepositoryInfoResp{
60de2a132aSGilbert Chen             0x0, 0x02, 0x50, PLDM_SUCCESS,
61de2a132aSGilbert Chen             0x0,                                     // repositoryState
62de2a132aSGilbert Chen             0x0, 0x0,  0x0,  0x0,          0x0, 0x0, 0x0,
63de2a132aSGilbert Chen             0x0, 0x0,  0x0,  0x0,          0x0, 0x0, // updateTime
64de2a132aSGilbert Chen             0x0, 0x0,  0x0,  0x0,          0x0, 0x0, 0x0,
65de2a132aSGilbert Chen             0x0, 0x0,  0x0,  0x0,          0x0, 0x0, // OEMUpdateTime
66de2a132aSGilbert Chen             1,   0x0,  0x0,  0x0,                    // recordCount
67de2a132aSGilbert Chen             0x0, 0x1,  0x0,  0x0,                    // repositorySize
68de2a132aSGilbert Chen             59,  0x0,  0x0,  0x0,                    // largestRecordSize
69de2a132aSGilbert Chen             0x0 // dataTransferHandleTimeout
70de2a132aSGilbert Chen         };
71de2a132aSGilbert Chen     auto rc = mockTerminusManager.enqueueResponse(
72*98831af9SThu Nguyen         reinterpret_cast<pldm_msg*>(getPDRRepositoryInfoResp.data()),
73de2a132aSGilbert Chen         sizeof(getPDRRepositoryInfoResp));
74de2a132aSGilbert Chen     EXPECT_EQ(rc, PLDM_SUCCESS);
75de2a132aSGilbert Chen 
76de2a132aSGilbert Chen     // queue getPDR responses
77de2a132aSGilbert Chen     const size_t getPdrRespLen = 81;
78de2a132aSGilbert Chen     std::array<uint8_t, sizeof(pldm_msg_hdr) + getPdrRespLen> getPdrResp{
79de2a132aSGilbert Chen         0x0, 0x02, 0x51, PLDM_SUCCESS, 0x0, 0x0, 0x0, 0x0, // nextRecordHandle
80de2a132aSGilbert Chen         0x0, 0x0, 0x0, 0x0, // nextDataTransferHandle
81de2a132aSGilbert Chen         0x5,                // transferFlag
82de2a132aSGilbert Chen         69, 0x0,            // responseCount
83de2a132aSGilbert Chen         // numeric Sensor PDR
84de2a132aSGilbert Chen         0x1, 0x0, 0x0,
85de2a132aSGilbert Chen         0x0,                     // record handle
86de2a132aSGilbert Chen         0x1,                     // PDRHeaderVersion
87de2a132aSGilbert Chen         PLDM_NUMERIC_SENSOR_PDR, // PDRType
88de2a132aSGilbert Chen         0x0,
89de2a132aSGilbert Chen         0x0,                     // recordChangeNumber
90de2a132aSGilbert Chen         PLDM_PDR_NUMERIC_SENSOR_PDR_FIXED_LENGTH +
91de2a132aSGilbert Chen             PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_SENSOR_DATA_SIZE_MIN_LENGTH +
92de2a132aSGilbert Chen             PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_RANGE_FIELD_MIN_LENGTH,
93de2a132aSGilbert Chen         0,                             // dataLength
94de2a132aSGilbert Chen         0,
95de2a132aSGilbert Chen         0,                             // PLDMTerminusHandle
96de2a132aSGilbert Chen         0x1,
97de2a132aSGilbert Chen         0x0,                           // sensorID=1
98de2a132aSGilbert Chen         120,
99de2a132aSGilbert Chen         0,                             // entityType=Power Supply(120)
100de2a132aSGilbert Chen         1,
101de2a132aSGilbert Chen         0,                             // entityInstanceNumber
102de2a132aSGilbert Chen         0x1,
103de2a132aSGilbert Chen         0x0,                           // containerID=1
104de2a132aSGilbert Chen         PLDM_NO_INIT,                  // sensorInit
105de2a132aSGilbert Chen         false,                         // sensorAuxiliaryNamesPDR
106de2a132aSGilbert Chen         PLDM_SENSOR_UNIT_DEGRESS_C,    // baseUint(2)=degrees C
107de2a132aSGilbert Chen         1,                             // unitModifier = 1
108de2a132aSGilbert Chen         0,                             // rateUnit
109de2a132aSGilbert Chen         0,                             // baseOEMUnitHandle
110de2a132aSGilbert Chen         0,                             // auxUnit
111de2a132aSGilbert Chen         0,                             // auxUnitModifier
112de2a132aSGilbert Chen         0,                             // auxRateUnit
113de2a132aSGilbert Chen         0,                             // rel
114de2a132aSGilbert Chen         0,                             // auxOEMUnitHandle
115de2a132aSGilbert Chen         true,                          // isLinear
116de2a132aSGilbert Chen         PLDM_SENSOR_DATA_SIZE_UINT8,   // sensorDataSize
117de2a132aSGilbert Chen         0, 0, 0xc0,
118de2a132aSGilbert Chen         0x3f,                          // resolution=1.5
119de2a132aSGilbert Chen         0, 0, 0x80,
120de2a132aSGilbert Chen         0x3f,                          // offset=1.0
121de2a132aSGilbert Chen         0,
122de2a132aSGilbert Chen         0,                             // accuracy
123de2a132aSGilbert Chen         0,                             // plusTolerance
124de2a132aSGilbert Chen         0,                             // minusTolerance
125de2a132aSGilbert Chen         2,                             // hysteresis
126de2a132aSGilbert Chen         0,                             // supportedThresholds
127de2a132aSGilbert Chen         0,                             // thresholdAndHysteresisVolatility
128de2a132aSGilbert Chen         0, 0, 0x80,
129de2a132aSGilbert Chen         0x3f,                          // stateTransistionInterval=1.0
130de2a132aSGilbert Chen         0, 0, 0x80,
131de2a132aSGilbert Chen         0x3f,                          // updateInverval=1.0
132de2a132aSGilbert Chen         255,                           // maxReadable
133de2a132aSGilbert Chen         0,                             // minReadable
134de2a132aSGilbert Chen         PLDM_RANGE_FIELD_FORMAT_UINT8, // rangeFieldFormat
135de2a132aSGilbert Chen         0,                             // rangeFieldsupport
136de2a132aSGilbert Chen         0,                             // nominalValue
137de2a132aSGilbert Chen         0,                             // normalMax
138de2a132aSGilbert Chen         0,                             // normalMin
139de2a132aSGilbert Chen         0,                             // warningHigh
140de2a132aSGilbert Chen         0,                             // warningLow
141de2a132aSGilbert Chen         0,                             // criticalHigh
142de2a132aSGilbert Chen         0,                             // criticalLow
143de2a132aSGilbert Chen         0,                             // fatalHigh
144de2a132aSGilbert Chen         0                              // fatalLow
145de2a132aSGilbert Chen     };
146*98831af9SThu Nguyen     rc = mockTerminusManager.enqueueResponse(
147*98831af9SThu Nguyen         reinterpret_cast<pldm_msg*>(getPdrResp.data()), sizeof(getPdrResp));
148de2a132aSGilbert Chen     EXPECT_EQ(rc, PLDM_SUCCESS);
149de2a132aSGilbert Chen 
150de2a132aSGilbert Chen     stdexec::sync_wait(platformManager.initTerminus());
151de2a132aSGilbert Chen     EXPECT_EQ(true, terminus->initialized);
152de2a132aSGilbert Chen     EXPECT_EQ(1, terminus->pdrs.size());
153de2a132aSGilbert Chen }
154de2a132aSGilbert Chen 
155b8cf46b8SThu Nguyen TEST_F(PlatformManagerTest, parseTerminusNameTest)
156b8cf46b8SThu Nguyen {
157b8cf46b8SThu Nguyen     // Add terminus
158b8cf46b8SThu Nguyen     auto mappedTid = mockTerminusManager.mapTid(pldm::MctpInfo(10, "", "", 1));
159b8cf46b8SThu Nguyen     auto tid = mappedTid.value();
160b8cf46b8SThu Nguyen     termini[tid] = std::make_shared<pldm::platform_mc::Terminus>(
161b8cf46b8SThu Nguyen         tid, 1 << PLDM_BASE | 1 << PLDM_PLATFORM);
162b8cf46b8SThu Nguyen     auto terminus = termini[tid];
163b8cf46b8SThu Nguyen 
164b8cf46b8SThu Nguyen     /* Set supported command by terminus */
165b8cf46b8SThu Nguyen     auto size = PLDM_MAX_TYPES * (PLDM_MAX_CMDS_PER_TYPE / 8);
166b8cf46b8SThu Nguyen     std::vector<uint8_t> pldmCmds(size);
167b8cf46b8SThu Nguyen     uint8_t type = PLDM_PLATFORM;
168b8cf46b8SThu Nguyen     uint8_t cmd = PLDM_GET_PDR;
169b8cf46b8SThu Nguyen     auto idx = type * (PLDM_MAX_CMDS_PER_TYPE / 8) + (cmd / 8);
170b8cf46b8SThu Nguyen     pldmCmds[idx] = pldmCmds[idx] | (1 << (cmd % 8));
171b8cf46b8SThu Nguyen     cmd = PLDM_GET_PDR_REPOSITORY_INFO;
172b8cf46b8SThu Nguyen     idx = type * (PLDM_MAX_CMDS_PER_TYPE / 8) + (cmd / 8);
173b8cf46b8SThu Nguyen     pldmCmds[idx] = pldmCmds[idx] | (1 << (cmd % 8));
174b8cf46b8SThu Nguyen     termini[tid]->setSupportedCommands(pldmCmds);
175b8cf46b8SThu Nguyen 
176b8cf46b8SThu Nguyen     // queue getPDRRepositoryInfo response
177b8cf46b8SThu Nguyen     const size_t getPDRRepositoryInfoLen =
178b8cf46b8SThu Nguyen         PLDM_GET_PDR_REPOSITORY_INFO_RESP_BYTES;
179b8cf46b8SThu Nguyen     std::array<uint8_t, sizeof(pldm_msg_hdr) + getPDRRepositoryInfoLen>
180b8cf46b8SThu Nguyen         getPDRRepositoryInfoResp{
181b8cf46b8SThu Nguyen             0x0, 0x02, 0x50, PLDM_SUCCESS,
182b8cf46b8SThu Nguyen             0x0,                                     // repositoryState
183b8cf46b8SThu Nguyen             0x0, 0x0,  0x0,  0x0,          0x0, 0x0, 0x0,
184b8cf46b8SThu Nguyen             0x0, 0x0,  0x0,  0x0,          0x0, 0x0, // updateTime
185b8cf46b8SThu Nguyen             0x0, 0x0,  0x0,  0x0,          0x0, 0x0, 0x0,
186b8cf46b8SThu Nguyen             0x0, 0x0,  0x0,  0x0,          0x0, 0x0, // OEMUpdateTime
187b8cf46b8SThu Nguyen             2,   0x0,  0x0,  0x0,                    // recordCount
188b8cf46b8SThu Nguyen             0x0, 0x1,  0x0,  0x0,                    // repositorySize
189b8cf46b8SThu Nguyen             59,  0x0,  0x0,  0x0,                    // largestRecordSize
190b8cf46b8SThu Nguyen             0x0 // dataTransferHandleTimeout
191b8cf46b8SThu Nguyen         };
192b8cf46b8SThu Nguyen     auto rc = mockTerminusManager.enqueueResponse(
193b8cf46b8SThu Nguyen         reinterpret_cast<pldm_msg*>(getPDRRepositoryInfoResp.data()),
194b8cf46b8SThu Nguyen         sizeof(getPDRRepositoryInfoResp));
195b8cf46b8SThu Nguyen     EXPECT_EQ(rc, PLDM_SUCCESS);
196b8cf46b8SThu Nguyen 
197b8cf46b8SThu Nguyen     // queue getPDR responses
198b8cf46b8SThu Nguyen     const size_t getPdrRespLen = 81;
199b8cf46b8SThu Nguyen     std::array<uint8_t, sizeof(pldm_msg_hdr) + getPdrRespLen> getPdrResp{
200b8cf46b8SThu Nguyen         0x0, 0x02, 0x51, PLDM_SUCCESS, 0x1, 0x0, 0x0, 0x0, // nextRecordHandle
201b8cf46b8SThu Nguyen         0x0, 0x0, 0x0, 0x0, // nextDataTransferHandle
202b8cf46b8SThu Nguyen         0x5,                // transferFlag
203b8cf46b8SThu Nguyen         69, 0x0,            // responseCount
204b8cf46b8SThu Nguyen         // numeric Sensor PDR
205b8cf46b8SThu Nguyen         0x0, 0x0, 0x0,
206b8cf46b8SThu Nguyen         0x0,                     // record handle
207b8cf46b8SThu Nguyen         0x1,                     // PDRHeaderVersion
208b8cf46b8SThu Nguyen         PLDM_NUMERIC_SENSOR_PDR, // PDRType
209b8cf46b8SThu Nguyen         0x0,
210b8cf46b8SThu Nguyen         0x0,                     // recordChangeNumber
211b8cf46b8SThu Nguyen         PLDM_PDR_NUMERIC_SENSOR_PDR_FIXED_LENGTH +
212b8cf46b8SThu Nguyen             PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_SENSOR_DATA_SIZE_MIN_LENGTH +
213b8cf46b8SThu Nguyen             PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_RANGE_FIELD_MIN_LENGTH,
214b8cf46b8SThu Nguyen         0,                             // dataLength
215b8cf46b8SThu Nguyen         0,
216b8cf46b8SThu Nguyen         0,                             // PLDMTerminusHandle
217b8cf46b8SThu Nguyen         0x1,
218b8cf46b8SThu Nguyen         0x0,                           // sensorID=1
219b8cf46b8SThu Nguyen         120,
220b8cf46b8SThu Nguyen         0,                             // entityType=Power Supply(120)
221b8cf46b8SThu Nguyen         1,
222b8cf46b8SThu Nguyen         0,                             // entityInstanceNumber
223b8cf46b8SThu Nguyen         0x1,
224b8cf46b8SThu Nguyen         0x0,                           // containerID=1
225b8cf46b8SThu Nguyen         PLDM_NO_INIT,                  // sensorInit
226b8cf46b8SThu Nguyen         false,                         // sensorAuxiliaryNamesPDR
227b8cf46b8SThu Nguyen         PLDM_SENSOR_UNIT_DEGRESS_C,    // baseUint(2)=degrees C
228b8cf46b8SThu Nguyen         1,                             // unitModifier = 1
229b8cf46b8SThu Nguyen         0,                             // rateUnit
230b8cf46b8SThu Nguyen         0,                             // baseOEMUnitHandle
231b8cf46b8SThu Nguyen         0,                             // auxUnit
232b8cf46b8SThu Nguyen         0,                             // auxUnitModifier
233b8cf46b8SThu Nguyen         0,                             // auxRateUnit
234b8cf46b8SThu Nguyen         0,                             // rel
235b8cf46b8SThu Nguyen         0,                             // auxOEMUnitHandle
236b8cf46b8SThu Nguyen         true,                          // isLinear
237b8cf46b8SThu Nguyen         PLDM_SENSOR_DATA_SIZE_UINT8,   // sensorDataSize
238b8cf46b8SThu Nguyen         0, 0, 0xc0,
239b8cf46b8SThu Nguyen         0x3f,                          // resolution=1.5
240b8cf46b8SThu Nguyen         0, 0, 0x80,
241b8cf46b8SThu Nguyen         0x3f,                          // offset=1.0
242b8cf46b8SThu Nguyen         0,
243b8cf46b8SThu Nguyen         0,                             // accuracy
244b8cf46b8SThu Nguyen         0,                             // plusTolerance
245b8cf46b8SThu Nguyen         0,                             // minusTolerance
246b8cf46b8SThu Nguyen         2,                             // hysteresis
247b8cf46b8SThu Nguyen         0,                             // supportedThresholds
248b8cf46b8SThu Nguyen         0,                             // thresholdAndHysteresisVolatility
249b8cf46b8SThu Nguyen         0, 0, 0x80,
250b8cf46b8SThu Nguyen         0x3f,                          // stateTransistionInterval=1.0
251b8cf46b8SThu Nguyen         0, 0, 0x80,
252b8cf46b8SThu Nguyen         0x3f,                          // updateInverval=1.0
253b8cf46b8SThu Nguyen         255,                           // maxReadable
254b8cf46b8SThu Nguyen         0,                             // minReadable
255b8cf46b8SThu Nguyen         PLDM_RANGE_FIELD_FORMAT_UINT8, // rangeFieldFormat
256b8cf46b8SThu Nguyen         0,                             // rangeFieldsupport
257b8cf46b8SThu Nguyen         0,                             // nominalValue
258b8cf46b8SThu Nguyen         0,                             // normalMax
259b8cf46b8SThu Nguyen         0,                             // normalMin
260b8cf46b8SThu Nguyen         0,                             // warningHigh
261b8cf46b8SThu Nguyen         0,                             // warningLow
262b8cf46b8SThu Nguyen         0,                             // criticalHigh
263b8cf46b8SThu Nguyen         0,                             // criticalLow
264b8cf46b8SThu Nguyen         0,                             // fatalHigh
265b8cf46b8SThu Nguyen         0                              // fatalLow
266b8cf46b8SThu Nguyen     };
267b8cf46b8SThu Nguyen     rc = mockTerminusManager.enqueueResponse(
268b8cf46b8SThu Nguyen         reinterpret_cast<pldm_msg*>(getPdrResp.data()), sizeof(getPdrResp));
269b8cf46b8SThu Nguyen     EXPECT_EQ(rc, PLDM_SUCCESS);
270b8cf46b8SThu Nguyen 
271b8cf46b8SThu Nguyen     const size_t getPdrAuxNameRespLen = 39;
272b8cf46b8SThu Nguyen     std::array<uint8_t, sizeof(pldm_msg_hdr) + getPdrAuxNameRespLen>
273b8cf46b8SThu Nguyen         getPdrAuxNameResp{
274b8cf46b8SThu Nguyen             0x0, 0x02, 0x51, PLDM_SUCCESS, 0x0, 0x0, 0x0,
275b8cf46b8SThu Nguyen             0x0,                // nextRecordHandle
276b8cf46b8SThu Nguyen             0x0, 0x0, 0x0, 0x0, // nextDataTransferHandle
277b8cf46b8SThu Nguyen             0x5,                // transferFlag
278b8cf46b8SThu Nguyen             0x1b, 0x0,          // responseCount
279b8cf46b8SThu Nguyen             // Common PDR Header
280b8cf46b8SThu Nguyen             0x1, 0x0, 0x0,
281b8cf46b8SThu Nguyen             0x0,                             // record handle
282b8cf46b8SThu Nguyen             0x1,                             // PDRHeaderVersion
283b8cf46b8SThu Nguyen             PLDM_ENTITY_AUXILIARY_NAMES_PDR, // PDRType
284b8cf46b8SThu Nguyen             0x1,
285b8cf46b8SThu Nguyen             0x0,                             // recordChangeNumber
286b8cf46b8SThu Nguyen             0x11,
287b8cf46b8SThu Nguyen             0,                               // dataLength
288b8cf46b8SThu Nguyen             /* Entity Auxiliary Names PDR Data*/
289b8cf46b8SThu Nguyen             3,
290b8cf46b8SThu Nguyen             0x80, // entityType system software
291b8cf46b8SThu Nguyen             0x1,
292b8cf46b8SThu Nguyen             0x0,  // Entity instance number =1
293b8cf46b8SThu Nguyen             0,
294b8cf46b8SThu Nguyen             0,    // Overal system
295b8cf46b8SThu Nguyen             0,    // shared Name Count one name only
296b8cf46b8SThu Nguyen             01,   // nameStringCount
297b8cf46b8SThu Nguyen             0x65, 0x6e, 0x00,
298b8cf46b8SThu Nguyen             0x00, // Language Tag "en"
299b8cf46b8SThu Nguyen             0x53, 0x00, 0x30, 0x00,
300b8cf46b8SThu Nguyen             0x00  // Entity Name "S0"
301b8cf46b8SThu Nguyen         };
302b8cf46b8SThu Nguyen     rc = mockTerminusManager.enqueueResponse(
303b8cf46b8SThu Nguyen         reinterpret_cast<pldm_msg*>(getPdrAuxNameResp.data()),
304b8cf46b8SThu Nguyen         sizeof(getPdrAuxNameResp));
305b8cf46b8SThu Nguyen     EXPECT_EQ(rc, PLDM_SUCCESS);
306b8cf46b8SThu Nguyen 
307b8cf46b8SThu Nguyen     stdexec::sync_wait(platformManager.initTerminus());
308b8cf46b8SThu Nguyen     EXPECT_EQ(true, terminus->initialized);
309b8cf46b8SThu Nguyen     EXPECT_EQ(2, terminus->pdrs.size());
310b8cf46b8SThu Nguyen     EXPECT_EQ("S0", terminus->getTerminusName());
311b8cf46b8SThu Nguyen }
312b8cf46b8SThu Nguyen 
313de2a132aSGilbert Chen TEST_F(PlatformManagerTest, initTerminusDontSupportGetPDRTest)
314de2a132aSGilbert Chen {
315de2a132aSGilbert Chen     // Add terminus
316de2a132aSGilbert Chen     auto mappedTid = mockTerminusManager.mapTid(pldm::MctpInfo(10, "", "", 1));
317de2a132aSGilbert Chen     auto tid = mappedTid.value();
318de2a132aSGilbert Chen     termini[tid] = std::make_shared<pldm::platform_mc::Terminus>(
319de2a132aSGilbert Chen         tid, 1 << PLDM_BASE | 1 << PLDM_PLATFORM);
320de2a132aSGilbert Chen     auto terminus = termini[tid];
321de2a132aSGilbert Chen 
322de2a132aSGilbert Chen     /* Set supported command by terminus */
323de2a132aSGilbert Chen     auto size = PLDM_MAX_TYPES * (PLDM_MAX_CMDS_PER_TYPE / 8);
324de2a132aSGilbert Chen     std::vector<uint8_t> pldmCmds(size);
325de2a132aSGilbert Chen     uint8_t type = PLDM_PLATFORM;
326de2a132aSGilbert Chen     uint8_t cmd = PLDM_GET_PDR_REPOSITORY_INFO;
327de2a132aSGilbert Chen     auto idx = type * (PLDM_MAX_CMDS_PER_TYPE / 8) + (cmd / 8);
328de2a132aSGilbert Chen     pldmCmds[idx] = pldmCmds[idx] | (1 << (cmd % 8));
329de2a132aSGilbert Chen     termini[tid]->setSupportedCommands(pldmCmds);
330de2a132aSGilbert Chen 
331de2a132aSGilbert Chen     // queue getPDRRepositoryInfo response
332de2a132aSGilbert Chen     const size_t getPDRRepositoryInfoLen =
333de2a132aSGilbert Chen         PLDM_GET_PDR_REPOSITORY_INFO_RESP_BYTES;
334de2a132aSGilbert Chen     std::array<uint8_t, sizeof(pldm_msg_hdr) + getPDRRepositoryInfoLen>
335de2a132aSGilbert Chen         getPDRRepositoryInfoResp{
336de2a132aSGilbert Chen             0x0, 0x02, 0x50, PLDM_SUCCESS,
337de2a132aSGilbert Chen             0x0,                                     // repositoryState
338de2a132aSGilbert Chen             0x0, 0x0,  0x0,  0x0,          0x0, 0x0, 0x0,
339de2a132aSGilbert Chen             0x0, 0x0,  0x0,  0x0,          0x0, 0x0, // updateTime
340de2a132aSGilbert Chen             0x0, 0x0,  0x0,  0x0,          0x0, 0x0, 0x0,
341de2a132aSGilbert Chen             0x0, 0x0,  0x0,  0x0,          0x0, 0x0, // OEMUpdateTime
342de2a132aSGilbert Chen             1,   0x0,  0x0,  0x0,                    // recordCount
343de2a132aSGilbert Chen             0x0, 0x1,  0x0,  0x0,                    // repositorySize
344de2a132aSGilbert Chen             59,  0x0,  0x0,  0x0,                    // largestRecordSize
345de2a132aSGilbert Chen             0x0 // dataTransferHandleTimeout
346de2a132aSGilbert Chen         };
347de2a132aSGilbert Chen     auto rc = mockTerminusManager.enqueueResponse(
348*98831af9SThu Nguyen         reinterpret_cast<pldm_msg*>(getPDRRepositoryInfoResp.data()),
349de2a132aSGilbert Chen         sizeof(getPDRRepositoryInfoResp));
350de2a132aSGilbert Chen     EXPECT_EQ(rc, PLDM_SUCCESS);
351de2a132aSGilbert Chen 
352de2a132aSGilbert Chen     // queue getPDR responses
353de2a132aSGilbert Chen     const size_t getPdrRespLen = 81;
354de2a132aSGilbert Chen     std::array<uint8_t, sizeof(pldm_msg_hdr) + getPdrRespLen> getPdrResp{
355de2a132aSGilbert Chen         0x0, 0x02, 0x51, PLDM_SUCCESS, 0x0, 0x0, 0x0, 0x0, // nextRecordHandle
356de2a132aSGilbert Chen         0x0, 0x0, 0x0, 0x0, // nextDataTransferHandle
357de2a132aSGilbert Chen         0x5,                // transferFlag
358de2a132aSGilbert Chen         69, 0x0,            // responseCount
359de2a132aSGilbert Chen         // numeric Sensor PDR
360de2a132aSGilbert Chen         0x1, 0x0, 0x0,
361de2a132aSGilbert Chen         0x0,                     // record handle
362de2a132aSGilbert Chen         0x1,                     // PDRHeaderVersion
363de2a132aSGilbert Chen         PLDM_NUMERIC_SENSOR_PDR, // PDRType
364de2a132aSGilbert Chen         0x0,
365de2a132aSGilbert Chen         0x0,                     // recordChangeNumber
366de2a132aSGilbert Chen         PLDM_PDR_NUMERIC_SENSOR_PDR_FIXED_LENGTH +
367de2a132aSGilbert Chen             PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_SENSOR_DATA_SIZE_MIN_LENGTH +
368de2a132aSGilbert Chen             PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_RANGE_FIELD_MIN_LENGTH,
369de2a132aSGilbert Chen         0,                             // dataLength
370de2a132aSGilbert Chen         0,
371de2a132aSGilbert Chen         0,                             // PLDMTerminusHandle
372de2a132aSGilbert Chen         0x1,
373de2a132aSGilbert Chen         0x0,                           // sensorID=1
374de2a132aSGilbert Chen         120,
375de2a132aSGilbert Chen         0,                             // entityType=Power Supply(120)
376de2a132aSGilbert Chen         1,
377de2a132aSGilbert Chen         0,                             // entityInstanceNumber
378de2a132aSGilbert Chen         0x1,
379de2a132aSGilbert Chen         0x0,                           // containerID=1
380de2a132aSGilbert Chen         PLDM_NO_INIT,                  // sensorInit
381de2a132aSGilbert Chen         false,                         // sensorAuxiliaryNamesPDR
382de2a132aSGilbert Chen         PLDM_SENSOR_UNIT_DEGRESS_C,    // baseUint(2)=degrees C
383de2a132aSGilbert Chen         1,                             // unitModifier = 1
384de2a132aSGilbert Chen         0,                             // rateUnit
385de2a132aSGilbert Chen         0,                             // baseOEMUnitHandle
386de2a132aSGilbert Chen         0,                             // auxUnit
387de2a132aSGilbert Chen         0,                             // auxUnitModifier
388de2a132aSGilbert Chen         0,                             // auxRateUnit
389de2a132aSGilbert Chen         0,                             // rel
390de2a132aSGilbert Chen         0,                             // auxOEMUnitHandle
391de2a132aSGilbert Chen         true,                          // isLinear
392de2a132aSGilbert Chen         PLDM_SENSOR_DATA_SIZE_UINT8,   // sensorDataSize
393de2a132aSGilbert Chen         0, 0, 0xc0,
394de2a132aSGilbert Chen         0x3f,                          // resolution=1.5
395de2a132aSGilbert Chen         0, 0, 0x80,
396de2a132aSGilbert Chen         0x3f,                          // offset=1.0
397de2a132aSGilbert Chen         0,
398de2a132aSGilbert Chen         0,                             // accuracy
399de2a132aSGilbert Chen         0,                             // plusTolerance
400de2a132aSGilbert Chen         0,                             // minusTolerance
401de2a132aSGilbert Chen         2,                             // hysteresis
402de2a132aSGilbert Chen         0,                             // supportedThresholds
403de2a132aSGilbert Chen         0,                             // thresholdAndHysteresisVolatility
404de2a132aSGilbert Chen         0, 0, 0x80,
405de2a132aSGilbert Chen         0x3f,                          // stateTransistionInterval=1.0
406de2a132aSGilbert Chen         0, 0, 0x80,
407de2a132aSGilbert Chen         0x3f,                          // updateInverval=1.0
408de2a132aSGilbert Chen         255,                           // maxReadable
409de2a132aSGilbert Chen         0,                             // minReadable
410de2a132aSGilbert Chen         PLDM_RANGE_FIELD_FORMAT_UINT8, // rangeFieldFormat
411de2a132aSGilbert Chen         0,                             // rangeFieldsupport
412de2a132aSGilbert Chen         0,                             // nominalValue
413de2a132aSGilbert Chen         0,                             // normalMax
414de2a132aSGilbert Chen         0,                             // normalMin
415de2a132aSGilbert Chen         0,                             // warningHigh
416de2a132aSGilbert Chen         0,                             // warningLow
417de2a132aSGilbert Chen         0,                             // criticalHigh
418de2a132aSGilbert Chen         0,                             // criticalLow
419de2a132aSGilbert Chen         0,                             // fatalHigh
420de2a132aSGilbert Chen         0                              // fatalLow
421de2a132aSGilbert Chen     };
422*98831af9SThu Nguyen     rc = mockTerminusManager.enqueueResponse(
423*98831af9SThu Nguyen         reinterpret_cast<pldm_msg*>(getPdrResp.data()), sizeof(getPdrResp));
424de2a132aSGilbert Chen     EXPECT_EQ(rc, PLDM_SUCCESS);
425de2a132aSGilbert Chen 
426de2a132aSGilbert Chen     stdexec::sync_wait(platformManager.initTerminus());
427de2a132aSGilbert Chen     EXPECT_EQ(true, terminus->initialized);
428de2a132aSGilbert Chen     EXPECT_EQ(0, terminus->pdrs.size());
429de2a132aSGilbert Chen }
430de2a132aSGilbert Chen 
431de2a132aSGilbert Chen TEST_F(PlatformManagerTest, negativeInitTerminusTest1)
432de2a132aSGilbert Chen {
433de2a132aSGilbert Chen     // terminus doesn't Type2 support
434de2a132aSGilbert Chen     auto mappedTid = mockTerminusManager.mapTid(pldm::MctpInfo(10, "", "", 1));
435de2a132aSGilbert Chen     auto tid = mappedTid.value();
436de2a132aSGilbert Chen     termini[tid] =
437de2a132aSGilbert Chen         std::make_shared<pldm::platform_mc::Terminus>(tid, 1 << PLDM_BASE);
438de2a132aSGilbert Chen     auto terminus = termini[tid];
439de2a132aSGilbert Chen 
440de2a132aSGilbert Chen     stdexec::sync_wait(platformManager.initTerminus());
441de2a132aSGilbert Chen     EXPECT_EQ(true, terminus->initialized);
442de2a132aSGilbert Chen     EXPECT_EQ(0, terminus->pdrs.size());
443de2a132aSGilbert Chen }
444de2a132aSGilbert Chen 
445de2a132aSGilbert Chen TEST_F(PlatformManagerTest, negativeInitTerminusTest2)
446de2a132aSGilbert Chen {
447de2a132aSGilbert Chen     // terminus responses error
448de2a132aSGilbert Chen     auto mappedTid = mockTerminusManager.mapTid(pldm::MctpInfo(10, "", "", 1));
449de2a132aSGilbert Chen     auto tid = mappedTid.value();
450de2a132aSGilbert Chen     termini[tid] = std::make_shared<pldm::platform_mc::Terminus>(
451de2a132aSGilbert Chen         tid, 1 << PLDM_BASE | 1 << PLDM_PLATFORM);
452de2a132aSGilbert Chen     auto terminus = termini[tid];
453de2a132aSGilbert Chen 
454de2a132aSGilbert Chen     // queue getPDRRepositoryInfo response cc=PLDM_ERROR
455de2a132aSGilbert Chen     const size_t getPDRRepositoryInfoLen = 1;
456de2a132aSGilbert Chen     std::array<uint8_t, sizeof(pldm_msg_hdr) + getPDRRepositoryInfoLen>
457de2a132aSGilbert Chen         getPDRRepositoryInfoResp{0x0, 0x02, 0x50, PLDM_ERROR};
458de2a132aSGilbert Chen     auto rc = mockTerminusManager.enqueueResponse(
459*98831af9SThu Nguyen         reinterpret_cast<pldm_msg*>(getPDRRepositoryInfoResp.data()),
460de2a132aSGilbert Chen         sizeof(getPDRRepositoryInfoResp));
461de2a132aSGilbert Chen     EXPECT_EQ(rc, PLDM_SUCCESS);
462de2a132aSGilbert Chen 
463de2a132aSGilbert Chen     // queue getPDR responses cc=PLDM_ERROR
464de2a132aSGilbert Chen     const size_t getPdrRespLen = 1;
465de2a132aSGilbert Chen     std::array<uint8_t, sizeof(pldm_msg_hdr) + getPdrRespLen> getPdrResp{
466de2a132aSGilbert Chen         0x0, 0x02, 0x51, PLDM_ERROR};
467*98831af9SThu Nguyen     rc = mockTerminusManager.enqueueResponse(
468*98831af9SThu Nguyen         reinterpret_cast<pldm_msg*>(getPdrResp.data()), sizeof(getPdrResp));
469de2a132aSGilbert Chen     EXPECT_EQ(rc, PLDM_SUCCESS);
470de2a132aSGilbert Chen 
471de2a132aSGilbert Chen     stdexec::sync_wait(platformManager.initTerminus());
472de2a132aSGilbert Chen     EXPECT_EQ(true, terminus->initialized);
473de2a132aSGilbert Chen     EXPECT_EQ(0, terminus->pdrs.size());
474de2a132aSGilbert Chen }
475