blob: 744e3d7ea224be37220f39506baa4afd3c501b15 [file] [log] [blame]
Deepak Kodihallid130e1a2020-06-17 05:55:32 -05001#include "common/utils.hpp"
Riya Dixit754041d2024-02-20 06:15:49 -06002#include "mocked_utils.hpp"
George Liu6492f522020-06-16 10:34:05 +08003
George Liuc453e162022-12-21 17:16:23 +08004#include <libpldm/platform.h>
Gilbert Chen6c7fed42022-02-22 15:40:17 +00005#include <linux/mctp.h>
George Liuc453e162022-12-21 17:16:23 +08006
George Liu83409572019-12-24 18:42:54 +08007#include <gtest/gtest.h>
8
9using namespace pldm::utils;
10
Manojkiran Eda93ad7952025-05-15 11:36:32 +053011TEST(GetNumPadBytesTest, NoPaddingNeeded)
12{
13 EXPECT_EQ(getNumPadBytes(0), 0);
14 EXPECT_EQ(getNumPadBytes(4), 0);
15 EXPECT_EQ(getNumPadBytes(8), 0);
16 EXPECT_EQ(getNumPadBytes(12), 0);
17}
18
19TEST(GetNumPadBytesTest, OneBytePadding)
20{
21 EXPECT_EQ(getNumPadBytes(3), 1);
22 EXPECT_EQ(getNumPadBytes(7), 1);
23 EXPECT_EQ(getNumPadBytes(11), 1);
24}
25
26TEST(GetNumPadBytesTest, TwoBytesPadding)
27{
28 EXPECT_EQ(getNumPadBytes(2), 2);
29 EXPECT_EQ(getNumPadBytes(6), 2);
30 EXPECT_EQ(getNumPadBytes(10), 2);
31}
32
33TEST(GetNumPadBytesTest, ThreeBytesPadding)
34{
35 EXPECT_EQ(getNumPadBytes(1), 3);
36 EXPECT_EQ(getNumPadBytes(5), 3);
37 EXPECT_EQ(getNumPadBytes(9), 3);
38}
39
40TEST(GetNumPadBytesTest, LargeValues)
41{
42 EXPECT_EQ(getNumPadBytes(1001), 3);
43 EXPECT_EQ(getNumPadBytes(1024), 0);
44 EXPECT_EQ(getNumPadBytes(65535), 1);
45}
46
Riya Dixit754041d2024-02-20 06:15:49 -060047TEST(GetInventoryObjects, testForEmptyObject)
48{
49 ObjectValueTree result =
50 DBusHandler::getInventoryObjects<GetManagedEmptyObject>();
51 EXPECT_TRUE(result.empty());
52}
53
54TEST(GetInventoryObjects, testForObject)
55{
56 std::string path = "/foo/bar";
57 std::string service = "foo.bar";
58 auto result = DBusHandler::getInventoryObjects<GetManagedObject>();
59 EXPECT_EQ(result[path].begin()->first, service);
60 auto function =
61 std::get<bool>(result[path][service][std::string("Functional")]);
62 auto model =
63 std::get<std::string>(result[path][service][std::string("Model")]);
64 EXPECT_FALSE(result.empty());
65 EXPECT_TRUE(function);
66 EXPECT_EQ(model, std::string("1234 - 00Z"));
67}
68
Manojkiran Edacd4cd452024-04-23 08:53:17 +053069TEST(printBuffer, testprintBufferGoodPath)
70{
71 std::vector<uint8_t> buffer = {10, 12, 14, 25, 233};
72 std::ostringstream localString;
73 auto coutBuffer = std::cout.rdbuf();
74 std::cout.rdbuf(localString.rdbuf());
75 printBuffer(false, buffer);
76 std::cout.rdbuf(coutBuffer);
77 EXPECT_EQ(localString.str(), "Rx: 0a 0c 0e 19 e9 \n");
78 localString.str("");
79 localString.clear();
80 std::cerr << localString.str() << std::endl;
81 buffer = {12, 0, 200, 12, 255};
82 std::cout.rdbuf(localString.rdbuf());
83 printBuffer(true, buffer);
84 std::cout.rdbuf(coutBuffer);
85 EXPECT_EQ(localString.str(), "Tx: 0c 00 c8 0c ff \n");
86}
87
88TEST(printBuffer, testprintBufferBadPath)
89{
90 std::vector<uint8_t> buffer = {};
91 std::ostringstream localString;
92 auto coutBuffer = std::cout.rdbuf();
93 std::cout.rdbuf(localString.rdbuf());
94 printBuffer(false, buffer);
95 EXPECT_EQ(localString.str(), "");
96 printBuffer(true, buffer);
97 std::cout.rdbuf(coutBuffer);
98 EXPECT_EQ(localString.str(), "");
99}
100
George Liu83409572019-12-24 18:42:54 +0800101TEST(decodeDate, testGooduintToDate)
102{
103 uint64_t data = 20191212115959;
104 uint16_t year = 2019;
105 uint8_t month = 12;
106 uint8_t day = 12;
107 uint8_t hours = 11;
108 uint8_t minutes = 59;
109 uint8_t seconds = 59;
110
111 uint16_t retyear = 0;
112 uint8_t retmonth = 0;
113 uint8_t retday = 0;
114 uint8_t rethours = 0;
115 uint8_t retminutes = 0;
116 uint8_t retseconds = 0;
117
118 auto ret = uintToDate(data, &retyear, &retmonth, &retday, &rethours,
119 &retminutes, &retseconds);
120
121 EXPECT_EQ(ret, true);
122 EXPECT_EQ(year, retyear);
123 EXPECT_EQ(month, retmonth);
124 EXPECT_EQ(day, retday);
125 EXPECT_EQ(hours, rethours);
126 EXPECT_EQ(minutes, retminutes);
127 EXPECT_EQ(seconds, retseconds);
128}
129
130TEST(decodeDate, testBaduintToDate)
131{
132 uint64_t data = 10191212115959;
133
134 uint16_t retyear = 0;
135 uint8_t retmonth = 0;
136 uint8_t retday = 0;
137 uint8_t rethours = 0;
138 uint8_t retminutes = 0;
139 uint8_t retseconds = 0;
140
141 auto ret = uintToDate(data, &retyear, &retmonth, &retday, &rethours,
142 &retminutes, &retseconds);
143
144 EXPECT_EQ(ret, false);
145}
146
George Liuba4c1fb2020-02-05 14:13:30 +0800147TEST(parseEffecterData, testGoodDecodeEffecterData)
George Liu83409572019-12-24 18:42:54 +0800148{
George Liuba4c1fb2020-02-05 14:13:30 +0800149 std::vector<uint8_t> effecterData = {1, 1, 0, 1};
150 uint8_t effecterCount = 2;
George Liu83409572019-12-24 18:42:54 +0800151 set_effecter_state_field stateField0 = {1, 1};
George Liuba4c1fb2020-02-05 14:13:30 +0800152 set_effecter_state_field stateField1 = {0, 1};
George Liu83409572019-12-24 18:42:54 +0800153
George Liuba4c1fb2020-02-05 14:13:30 +0800154 auto effecterField = parseEffecterData(effecterData, effecterCount);
155 EXPECT_NE(effecterField, std::nullopt);
156 EXPECT_EQ(effecterCount, effecterField->size());
George Liu83409572019-12-24 18:42:54 +0800157
George Liuba4c1fb2020-02-05 14:13:30 +0800158 std::vector<set_effecter_state_field> stateField = effecterField.value();
George Liu83409572019-12-24 18:42:54 +0800159 EXPECT_EQ(stateField[0].set_request, stateField0.set_request);
160 EXPECT_EQ(stateField[0].effecter_state, stateField0.effecter_state);
161 EXPECT_EQ(stateField[1].set_request, stateField1.set_request);
162 EXPECT_EQ(stateField[1].effecter_state, stateField1.effecter_state);
George Liu83409572019-12-24 18:42:54 +0800163}
164
George Liuba4c1fb2020-02-05 14:13:30 +0800165TEST(parseEffecterData, testBadDecodeEffecterData)
George Liu83409572019-12-24 18:42:54 +0800166{
George Liuba4c1fb2020-02-05 14:13:30 +0800167 std::vector<uint8_t> effecterData = {0, 1, 0, 1, 0, 1};
168 uint8_t effecterCount = 2;
George Liu83409572019-12-24 18:42:54 +0800169
George Liuba4c1fb2020-02-05 14:13:30 +0800170 auto effecterField = parseEffecterData(effecterData, effecterCount);
George Liu83409572019-12-24 18:42:54 +0800171
George Liuba4c1fb2020-02-05 14:13:30 +0800172 EXPECT_EQ(effecterField, std::nullopt);
George Liu83409572019-12-24 18:42:54 +0800173}
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500174
175TEST(FindStateEffecterPDR, testOneMatch)
176{
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500177 auto repo = pldm_pdr_init();
178 uint8_t tid = 1;
179 uint16_t entityID = 33;
180 uint16_t stateSetId = 196;
181
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400182 std::vector<uint8_t> pdr(
183 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
184 sizeof(struct state_effecter_possible_states));
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500185
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530186 auto rec = new (pdr.data()) pldm_state_effecter_pdr;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500187
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530188 auto state = new (rec->possible_states) state_effecter_possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500189
190 rec->hdr.type = 11;
191 rec->hdr.record_handle = 1;
192 rec->entity_type = 33;
193 rec->container_id = 0;
194 rec->composite_effecter_count = 1;
195 state->state_set_id = 196;
196 state->possible_states_size = 1;
197
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930198 uint32_t handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000199 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500200
201 auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
202
203 EXPECT_EQ(pdr, record[0]);
204
205 pldm_pdr_destroy(repo);
206}
207
208TEST(FindStateEffecterPDR, testNoMatch)
209{
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500210 auto repo = pldm_pdr_init();
211 uint8_t tid = 1;
212 uint16_t entityID = 44;
213 uint16_t stateSetId = 196;
214
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400215 std::vector<uint8_t> pdr(
216 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
217 sizeof(struct state_effecter_possible_states));
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500218
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530219 auto rec = new (pdr.data()) pldm_state_effecter_pdr;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500220
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530221 auto state = new (rec->possible_states) state_effecter_possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500222
223 rec->hdr.type = 11;
224 rec->hdr.record_handle = 1;
225 rec->entity_type = 33;
226 rec->container_id = 0;
227 rec->composite_effecter_count = 1;
228 state->state_set_id = 196;
229 state->possible_states_size = 1;
230
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930231 uint32_t handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000232 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500233
234 auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
235
236 EXPECT_EQ(record.empty(), true);
237
238 pldm_pdr_destroy(repo);
239}
240
241TEST(FindStateEffecterPDR, testEmptyRepo)
242{
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500243 auto repo = pldm_pdr_init();
244 uint8_t tid = 1;
245 uint16_t entityID = 33;
246 uint16_t stateSetId = 196;
247
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400248 std::vector<uint8_t> pdr(
249 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
250 sizeof(struct state_effecter_possible_states));
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500251
252 auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
253
254 EXPECT_EQ(record.empty(), true);
255
256 pldm_pdr_destroy(repo);
257}
258
259TEST(FindStateEffecterPDR, testMoreMatch)
260{
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500261 auto repo = pldm_pdr_init();
262 uint8_t tid = 1;
263
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400264 std::vector<uint8_t> pdr(
265 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
266 sizeof(struct state_effecter_possible_states));
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500267
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530268 auto rec = new (pdr.data()) pldm_state_effecter_pdr;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500269
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530270 auto state = new (rec->possible_states) state_effecter_possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500271
272 rec->hdr.type = 11;
273 rec->hdr.record_handle = 1;
274 rec->entity_type = 31;
275 rec->container_id = 0;
276 rec->composite_effecter_count = 1;
277 state->state_set_id = 129;
278 state->possible_states_size = 1;
279
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930280 uint32_t handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000281 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500282
283 std::vector<uint8_t> pdr_second(
284 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
285 sizeof(struct state_effecter_possible_states));
286
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530287 auto rec_second = new (pdr_second.data()) pldm_state_effecter_pdr;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500288
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530289 auto state_second = new (rec_second->possible_states)
290 state_effecter_possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500291
292 rec_second->hdr.type = 11;
293 rec_second->hdr.record_handle = 2;
294 rec_second->entity_type = 31;
295 rec_second->container_id = 0;
296 rec_second->composite_effecter_count = 1;
297 state_second->state_set_id = 129;
298 state_second->possible_states_size = 1;
299
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930300 handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000301 ASSERT_EQ(pldm_pdr_add(repo, pdr_second.data(), pdr_second.size(), false, 1,
302 &handle),
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930303 0);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500304
305 uint16_t entityID_ = 31;
306 uint16_t stateSetId_ = 129;
307
308 auto record = findStateEffecterPDR(tid, entityID_, stateSetId_, repo);
309
310 EXPECT_EQ(pdr, record[0]);
311 EXPECT_EQ(pdr_second, record[1]);
312
313 pldm_pdr_destroy(repo);
314}
315
316TEST(FindStateEffecterPDR, testManyNoMatch)
317{
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500318 auto repo = pldm_pdr_init();
319 uint8_t tid = 1;
320 uint16_t entityID = 33;
321 uint16_t stateSetId = 196;
322
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400323 std::vector<uint8_t> pdr(
324 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
325 sizeof(struct state_effecter_possible_states));
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500326
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530327 auto rec = new (pdr.data()) pldm_state_effecter_pdr;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500328
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530329 auto state = new (rec->possible_states) state_effecter_possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500330
331 rec->hdr.type = 11;
332 rec->hdr.record_handle = 1;
333 rec->entity_type = 34;
334 rec->container_id = 0;
335 rec->composite_effecter_count = 1;
336 state->state_set_id = 198;
337 state->possible_states_size = 1;
338
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930339 uint32_t handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000340 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500341
342 std::vector<uint8_t> pdr_second(
343 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
344 sizeof(struct state_effecter_possible_states));
345
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530346 auto rec_second = new (pdr_second.data()) pldm_state_effecter_pdr;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500347
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530348 auto state_second = new (rec_second->possible_states)
349 state_effecter_possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500350
351 rec_second->hdr.type = 11;
352 rec_second->hdr.record_handle = 2;
353 rec_second->entity_type = 39;
354 rec_second->container_id = 0;
355 rec_second->composite_effecter_count = 1;
356 state_second->state_set_id = 169;
357 state_second->possible_states_size = 1;
358
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930359 handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000360 ASSERT_EQ(pldm_pdr_add(repo, pdr_second.data(), pdr_second.size(), false, 1,
361 &handle),
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930362 0);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500363
364 auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
365
366 EXPECT_EQ(record.empty(), true);
367
368 pldm_pdr_destroy(repo);
369}
370
371TEST(FindStateEffecterPDR, testOneMatchOneNoMatch)
372{
373 auto repo = pldm_pdr_init();
374 uint8_t tid = 1;
375 uint16_t entityID = 67;
376 uint16_t stateSetId = 192;
377
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400378 std::vector<uint8_t> pdr(
379 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
380 sizeof(struct state_effecter_possible_states));
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500381
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530382 auto rec = new (pdr.data()) pldm_state_effecter_pdr;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500383
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530384 auto state = new (rec->possible_states) state_effecter_possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500385
386 rec->hdr.type = 11;
387 rec->hdr.record_handle = 1;
388 rec->entity_type = 32;
389 rec->container_id = 0;
390 rec->composite_effecter_count = 1;
391 state->state_set_id = 198;
392 state->possible_states_size = 1;
393
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930394 uint32_t handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000395 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500396
397 std::vector<uint8_t> pdr_second(
398 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
399 sizeof(struct state_effecter_possible_states));
400
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530401 auto rec_second = new (pdr_second.data()) pldm_state_effecter_pdr;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500402
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530403 auto state_second = new (rec_second->possible_states)
404 state_effecter_possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500405
406 rec_second->hdr.type = 11;
407 rec_second->hdr.record_handle = 2;
408 rec_second->entity_type = 67;
409 rec_second->container_id = 0;
410 rec_second->composite_effecter_count = 1;
411 state_second->state_set_id = 192;
412 state_second->possible_states_size = 1;
413
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930414 handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000415 ASSERT_EQ(pldm_pdr_add(repo, pdr_second.data(), pdr_second.size(), false, 1,
416 &handle),
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930417 0);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500418
419 auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
420
421 EXPECT_EQ(pdr_second, record[0]);
422 EXPECT_EQ(record.size(), 1);
423
424 pldm_pdr_destroy(repo);
425}
426
427TEST(FindStateEffecterPDR, testOneMatchManyNoMatch)
428{
429 auto repo = pldm_pdr_init();
430 uint8_t tid = 1;
431 uint16_t entityID = 67;
432 uint16_t stateSetId = 192;
433
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400434 std::vector<uint8_t> pdr(
435 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
436 sizeof(struct state_effecter_possible_states));
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500437
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530438 auto rec = new (pdr.data()) pldm_state_effecter_pdr;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500439
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530440 auto state = new (rec->possible_states) state_effecter_possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500441
442 rec->hdr.type = 11;
443 rec->hdr.record_handle = 1;
444 rec->entity_type = 32;
445 rec->container_id = 0;
446 rec->composite_effecter_count = 1;
447 state->state_set_id = 198;
448 state->possible_states_size = 1;
449
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930450 uint32_t handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000451 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500452
453 std::vector<uint8_t> pdr_second(
454 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
455 sizeof(struct state_effecter_possible_states));
456
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530457 auto rec_second = new (pdr_second.data()) pldm_state_effecter_pdr;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500458
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530459 auto state_second = new (rec_second->possible_states)
460 state_effecter_possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500461
462 rec_second->hdr.type = 11;
463 rec_second->hdr.record_handle = 2;
464 rec_second->entity_type = 67;
465 rec_second->container_id = 0;
466 rec_second->composite_effecter_count = 1;
467 state_second->state_set_id = 192;
468 state_second->possible_states_size = 1;
469
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930470 handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000471 ASSERT_EQ(pldm_pdr_add(repo, pdr_second.data(), pdr_second.size(), false, 1,
472 &handle),
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930473 0);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500474
475 std::vector<uint8_t> pdr_third(
476 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
477 sizeof(struct state_effecter_possible_states));
478
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530479 auto rec_third = new (pdr_third.data()) pldm_state_effecter_pdr;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500480
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530481 auto state_third = new (rec_third->possible_states)
482 state_effecter_possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500483
484 rec_third->hdr.type = 11;
485 rec_third->hdr.record_handle = 3;
486 rec_third->entity_type = 69;
487 rec_third->container_id = 0;
488 rec_third->composite_effecter_count = 1;
489 state_third->state_set_id = 199;
490 state_third->possible_states_size = 1;
491
492 auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
493
494 EXPECT_EQ(pdr_second, record[0]);
495 EXPECT_EQ(record.size(), 1);
496
497 pldm_pdr_destroy(repo);
498}
499
500TEST(FindStateEffecterPDR, testCompositeEffecter)
501{
502 auto repo = pldm_pdr_init();
503 uint8_t tid = 1;
504 uint16_t entityID = 67;
505 uint16_t stateSetId = 192;
506
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400507 std::vector<uint8_t> pdr(
508 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
509 sizeof(struct state_effecter_possible_states) * 3);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500510
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530511 auto rec = new (pdr.data()) pldm_state_effecter_pdr;
Chicago Duana7aacc32020-06-10 18:03:38 +0800512 auto state_start = rec->possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500513
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530514 auto state = new (state_start) state_effecter_possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500515
516 rec->hdr.type = 11;
517 rec->hdr.record_handle = 1;
518 rec->entity_type = 67;
519 rec->container_id = 0;
520 rec->composite_effecter_count = 3;
521 state->state_set_id = 198;
522 state->possible_states_size = 1;
523
Chicago Duana7aacc32020-06-10 18:03:38 +0800524 state_start += state->possible_states_size + sizeof(state->state_set_id) +
525 sizeof(state->possible_states_size);
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530526 state = new (state_start) state_effecter_possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500527 state->state_set_id = 193;
528 state->possible_states_size = 1;
529
Chicago Duana7aacc32020-06-10 18:03:38 +0800530 state_start += state->possible_states_size + sizeof(state->state_set_id) +
531 sizeof(state->possible_states_size);
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530532 state = new (state_start) state_effecter_possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500533 state->state_set_id = 192;
534 state->possible_states_size = 1;
535
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930536 uint32_t handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000537 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500538
539 auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
540
541 EXPECT_EQ(pdr, record[0]);
542
543 pldm_pdr_destroy(repo);
544}
545
546TEST(FindStateEffecterPDR, testNoMatchCompositeEffecter)
547{
548 auto repo = pldm_pdr_init();
549 uint8_t tid = 1;
550 uint16_t entityID = 67;
551 uint16_t stateSetId = 192;
552
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400553 std::vector<uint8_t> pdr(
554 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
555 sizeof(struct state_effecter_possible_states) * 3);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500556
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530557 auto rec = new (pdr.data()) pldm_state_effecter_pdr;
Chicago Duana7aacc32020-06-10 18:03:38 +0800558 auto state_start = rec->possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500559
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530560 auto state = new (state_start) state_effecter_possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500561
562 rec->hdr.type = 11;
563 rec->hdr.record_handle = 1;
564 rec->entity_type = 34;
565 rec->container_id = 0;
566 rec->composite_effecter_count = 3;
567 state->state_set_id = 198;
568 state->possible_states_size = 1;
569
Chicago Duana7aacc32020-06-10 18:03:38 +0800570 state_start += state->possible_states_size + sizeof(state->state_set_id) +
571 sizeof(state->possible_states_size);
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530572 state = new (state_start) state_effecter_possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500573 state->state_set_id = 193;
574 state->possible_states_size = 1;
575
Chicago Duana7aacc32020-06-10 18:03:38 +0800576 state_start += state->possible_states_size + sizeof(state->state_set_id) +
577 sizeof(state->possible_states_size);
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530578 state = new (state_start) state_effecter_possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500579 state->state_set_id = 123;
580 state->possible_states_size = 1;
581
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930582 uint32_t handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000583 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500584
585 auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
586
587 EXPECT_EQ(record.empty(), true);
588
589 pldm_pdr_destroy(repo);
590}
Chicago Duan738e4d82020-05-28 16:39:19 +0800591
592TEST(FindStateSensorPDR, testOneMatch)
593{
Chicago Duan738e4d82020-05-28 16:39:19 +0800594 auto repo = pldm_pdr_init();
595 uint8_t tid = 1;
596 uint16_t entityID = 5;
597 uint16_t stateSetId = 1;
598
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400599 std::vector<uint8_t> pdr(
600 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
601 sizeof(struct state_sensor_possible_states));
Chicago Duan738e4d82020-05-28 16:39:19 +0800602
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530603 auto rec = new (pdr.data()) pldm_state_sensor_pdr;
Chicago Duan738e4d82020-05-28 16:39:19 +0800604
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530605 auto state = new (rec->possible_states) state_sensor_possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800606
607 rec->hdr.type = 4;
608 rec->hdr.record_handle = 1;
609 rec->entity_type = 5;
610 rec->container_id = 0;
611 rec->composite_sensor_count = 1;
612 state->state_set_id = 1;
613 state->possible_states_size = 1;
614
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930615 uint32_t handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000616 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Chicago Duan738e4d82020-05-28 16:39:19 +0800617
618 auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
619
620 EXPECT_EQ(pdr, record[0]);
621
622 pldm_pdr_destroy(repo);
623}
624
625TEST(FindStateSensorPDR, testNoMatch)
626{
Chicago Duan738e4d82020-05-28 16:39:19 +0800627 auto repo = pldm_pdr_init();
628 uint8_t tid = 1;
629 uint16_t entityID = 5;
630 uint16_t stateSetId = 1;
631
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400632 std::vector<uint8_t> pdr(
633 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
634 sizeof(struct state_sensor_possible_states));
Chicago Duan738e4d82020-05-28 16:39:19 +0800635
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530636 auto rec = new (pdr.data()) pldm_state_sensor_pdr;
Chicago Duan738e4d82020-05-28 16:39:19 +0800637
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530638 auto state = new (rec->possible_states) state_sensor_possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800639
640 rec->hdr.type = 4;
641 rec->hdr.record_handle = 1;
642 rec->entity_type = 55;
643 rec->container_id = 0;
644 rec->composite_sensor_count = 1;
645 state->state_set_id = 1;
646 state->possible_states_size = 1;
647
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930648 uint32_t handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000649 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Chicago Duan738e4d82020-05-28 16:39:19 +0800650
651 auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
652
653 EXPECT_EQ(record.empty(), true);
654
655 pldm_pdr_destroy(repo);
656}
657
658TEST(FindStateSensorPDR, testEmptyRepo)
659{
Chicago Duan738e4d82020-05-28 16:39:19 +0800660 auto repo = pldm_pdr_init();
661 uint8_t tid = 1;
662 uint16_t entityID = 5;
663 uint16_t stateSetId = 1;
664
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400665 std::vector<uint8_t> pdr(
666 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
667 sizeof(struct state_sensor_possible_states));
Chicago Duan738e4d82020-05-28 16:39:19 +0800668
669 auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
670
671 EXPECT_EQ(record.empty(), true);
672
673 pldm_pdr_destroy(repo);
674}
675
676TEST(FindStateSensorPDR, testMoreMatch)
677{
Chicago Duan738e4d82020-05-28 16:39:19 +0800678 auto repo = pldm_pdr_init();
679 uint8_t tid = 1;
680
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400681 std::vector<uint8_t> pdr(
682 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
683 sizeof(struct state_sensor_possible_states));
Chicago Duan738e4d82020-05-28 16:39:19 +0800684
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530685 auto rec = new (pdr.data()) pldm_state_sensor_pdr;
Chicago Duan738e4d82020-05-28 16:39:19 +0800686
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530687 auto state = new (rec->possible_states) state_sensor_possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800688
689 rec->hdr.type = 4;
690 rec->hdr.record_handle = 1;
691 rec->entity_type = 5;
692 rec->container_id = 0;
693 rec->composite_sensor_count = 1;
694 state->state_set_id = 1;
695 state->possible_states_size = 1;
696
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930697 uint32_t handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000698 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Chicago Duan738e4d82020-05-28 16:39:19 +0800699
700 std::vector<uint8_t> pdr_second(
701 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
702 sizeof(struct state_sensor_possible_states));
703
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530704 auto rec_second = new (pdr_second.data()) pldm_state_sensor_pdr;
Chicago Duan738e4d82020-05-28 16:39:19 +0800705
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530706 auto state_second = new (rec_second->possible_states)
707 state_sensor_possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800708
709 rec_second->hdr.type = 4;
710 rec_second->hdr.record_handle = 2;
711 rec_second->entity_type = 5;
712 rec_second->container_id = 0;
713 rec_second->composite_sensor_count = 1;
714 state_second->state_set_id = 1;
715 state_second->possible_states_size = 1;
716
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930717 handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000718 ASSERT_EQ(pldm_pdr_add(repo, pdr_second.data(), pdr_second.size(), false, 1,
719 &handle),
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930720 0);
Chicago Duan738e4d82020-05-28 16:39:19 +0800721
722 uint16_t entityID_ = 5;
723 uint16_t stateSetId_ = 1;
724
725 auto record = findStateSensorPDR(tid, entityID_, stateSetId_, repo);
726
727 EXPECT_EQ(pdr, record[0]);
728 EXPECT_EQ(pdr_second, record[1]);
729
730 pldm_pdr_destroy(repo);
731}
732
733TEST(FindStateSensorPDR, testManyNoMatch)
734{
Chicago Duan738e4d82020-05-28 16:39:19 +0800735 auto repo = pldm_pdr_init();
736 uint8_t tid = 1;
737 uint16_t entityID = 5;
738 uint16_t stateSetId = 1;
739
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400740 std::vector<uint8_t> pdr(
741 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
742 sizeof(struct state_sensor_possible_states));
Chicago Duan738e4d82020-05-28 16:39:19 +0800743
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530744 auto rec = new (pdr.data()) pldm_state_sensor_pdr;
Chicago Duan738e4d82020-05-28 16:39:19 +0800745
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530746 auto state = new (rec->possible_states) state_sensor_possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800747
748 rec->hdr.type = 4;
749 rec->hdr.record_handle = 1;
750 rec->entity_type = 56;
751 rec->container_id = 0;
752 rec->composite_sensor_count = 1;
753 state->state_set_id = 2;
754 state->possible_states_size = 1;
755
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930756 uint32_t handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000757 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Chicago Duan738e4d82020-05-28 16:39:19 +0800758
759 std::vector<uint8_t> pdr_second(
760 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
761 sizeof(struct state_sensor_possible_states));
762
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530763 auto rec_second = new (pdr_second.data()) pldm_state_sensor_pdr;
Chicago Duan738e4d82020-05-28 16:39:19 +0800764
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530765 auto state_second = new (rec_second->possible_states)
766 state_sensor_possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800767
768 rec_second->hdr.type = 4;
769 rec_second->hdr.record_handle = 2;
770 rec_second->entity_type = 66;
771 rec_second->container_id = 0;
772 rec_second->composite_sensor_count = 1;
773 state_second->state_set_id = 3;
774 state_second->possible_states_size = 1;
775
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930776 handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000777 ASSERT_EQ(pldm_pdr_add(repo, pdr_second.data(), pdr_second.size(), false, 1,
778 &handle),
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930779 0);
Chicago Duan738e4d82020-05-28 16:39:19 +0800780
781 auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
782
783 EXPECT_EQ(record.empty(), true);
784
785 pldm_pdr_destroy(repo);
786}
787
788TEST(FindStateSensorPDR, testOneMatchOneNoMatch)
789{
790 auto repo = pldm_pdr_init();
791 uint8_t tid = 1;
792 uint16_t entityID = 5;
793 uint16_t stateSetId = 1;
794
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400795 std::vector<uint8_t> pdr(
796 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
797 sizeof(struct state_sensor_possible_states));
Chicago Duan738e4d82020-05-28 16:39:19 +0800798
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530799 auto rec = new (pdr.data()) pldm_state_sensor_pdr;
Chicago Duan738e4d82020-05-28 16:39:19 +0800800
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530801 auto state = new (rec->possible_states) state_sensor_possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800802
803 rec->hdr.type = 4;
804 rec->hdr.record_handle = 1;
805 rec->entity_type = 10;
806 rec->container_id = 0;
807 rec->composite_sensor_count = 1;
808 state->state_set_id = 20;
809 state->possible_states_size = 1;
810
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930811 uint32_t handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000812 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Chicago Duan738e4d82020-05-28 16:39:19 +0800813
814 std::vector<uint8_t> pdr_second(
815 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
816 sizeof(struct state_sensor_possible_states));
817
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530818 auto rec_second = new (pdr_second.data()) pldm_state_sensor_pdr;
Chicago Duan738e4d82020-05-28 16:39:19 +0800819
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530820 auto state_second = new (rec_second->possible_states)
821 state_sensor_possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800822
823 rec_second->hdr.type = 4;
824 rec_second->hdr.record_handle = 2;
825 rec_second->entity_type = 5;
826 rec_second->container_id = 0;
827 rec_second->composite_sensor_count = 1;
828 state_second->state_set_id = 1;
829 state_second->possible_states_size = 1;
830
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930831 handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000832 ASSERT_EQ(pldm_pdr_add(repo, pdr_second.data(), pdr_second.size(), false, 1,
833 &handle),
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930834 0);
Chicago Duan738e4d82020-05-28 16:39:19 +0800835
836 auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
837
838 EXPECT_EQ(pdr_second, record[0]);
839 EXPECT_EQ(record.size(), 1);
840
841 pldm_pdr_destroy(repo);
842}
843
844TEST(FindStateSensorPDR, testOneMatchManyNoMatch)
845{
846 auto repo = pldm_pdr_init();
847 uint8_t tid = 1;
848 uint16_t entityID = 5;
849 uint16_t stateSetId = 1;
850
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400851 std::vector<uint8_t> pdr(
852 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
853 sizeof(struct state_sensor_possible_states));
Chicago Duan738e4d82020-05-28 16:39:19 +0800854
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530855 auto rec = new (pdr.data()) pldm_state_sensor_pdr;
Chicago Duan738e4d82020-05-28 16:39:19 +0800856
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530857 auto state = new (rec->possible_states) state_sensor_possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800858
859 rec->hdr.type = 4;
860 rec->hdr.record_handle = 1;
861 rec->entity_type = 6;
862 rec->container_id = 0;
863 rec->composite_sensor_count = 1;
864 state->state_set_id = 9;
865 state->possible_states_size = 1;
866
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930867 uint32_t handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000868 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Chicago Duan738e4d82020-05-28 16:39:19 +0800869
870 std::vector<uint8_t> pdr_second(
871 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
872 sizeof(struct state_sensor_possible_states));
873
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530874 auto rec_second = new (pdr_second.data()) pldm_state_sensor_pdr;
Chicago Duan738e4d82020-05-28 16:39:19 +0800875
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530876 auto state_second = new (rec_second->possible_states)
877 state_sensor_possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800878
879 rec_second->hdr.type = 4;
880 rec_second->hdr.record_handle = 2;
881 rec_second->entity_type = 5;
882 rec_second->container_id = 0;
883 rec_second->composite_sensor_count = 1;
884 state_second->state_set_id = 1;
885 state_second->possible_states_size = 1;
886
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930887 handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000888 ASSERT_EQ(pldm_pdr_add(repo, pdr_second.data(), pdr_second.size(), false, 1,
889 &handle),
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930890 0);
Chicago Duan738e4d82020-05-28 16:39:19 +0800891
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400892 std::vector<uint8_t> pdr_third(
893 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
894 sizeof(struct state_sensor_possible_states));
Chicago Duan738e4d82020-05-28 16:39:19 +0800895
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530896 auto rec_third = new (pdr_third.data()) pldm_state_sensor_pdr;
Chicago Duan738e4d82020-05-28 16:39:19 +0800897
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530898 auto state_third = new (rec_third->possible_states)
899 state_sensor_possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800900
901 rec_third->hdr.type = 4;
902 rec_third->hdr.record_handle = 3;
903 rec_third->entity_type = 7;
904 rec_third->container_id = 0;
905 rec_third->composite_sensor_count = 1;
906 state_third->state_set_id = 12;
907 state_third->possible_states_size = 1;
908
909 auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
910
911 EXPECT_EQ(pdr_second, record[0]);
912 EXPECT_EQ(record.size(), 1);
913
914 pldm_pdr_destroy(repo);
915}
916
917TEST(FindStateSensorPDR, testCompositeSensor)
918{
919 auto repo = pldm_pdr_init();
920 uint8_t tid = 1;
921 uint16_t entityID = 5;
922 uint16_t stateSetId = 1;
923
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400924 std::vector<uint8_t> pdr(
925 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
926 sizeof(struct state_sensor_possible_states) * 3);
Chicago Duan738e4d82020-05-28 16:39:19 +0800927
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530928 auto rec = new (pdr.data()) pldm_state_sensor_pdr;
Chicago Duana7aacc32020-06-10 18:03:38 +0800929 auto state_start = rec->possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800930
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530931 auto state = new (state_start) state_sensor_possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800932
933 rec->hdr.type = 4;
934 rec->hdr.record_handle = 1;
935 rec->entity_type = 5;
936 rec->container_id = 0;
937 rec->composite_sensor_count = 3;
938 state->state_set_id = 2;
939 state->possible_states_size = 1;
940
Chicago Duana7aacc32020-06-10 18:03:38 +0800941 state_start += state->possible_states_size + sizeof(state->state_set_id) +
942 sizeof(state->possible_states_size);
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530943 state = new (state_start) state_sensor_possible_states;
Chicago Duana7aacc32020-06-10 18:03:38 +0800944
Chicago Duan738e4d82020-05-28 16:39:19 +0800945 state->state_set_id = 7;
946 state->possible_states_size = 1;
947
Chicago Duana7aacc32020-06-10 18:03:38 +0800948 state_start += state->possible_states_size + sizeof(state->state_set_id) +
949 sizeof(state->possible_states_size);
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530950 state = new (state_start) state_sensor_possible_states;
Chicago Duana7aacc32020-06-10 18:03:38 +0800951
Chicago Duan738e4d82020-05-28 16:39:19 +0800952 state->state_set_id = 1;
953 state->possible_states_size = 1;
954
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930955 uint32_t handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000956 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Chicago Duan738e4d82020-05-28 16:39:19 +0800957
958 auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
959
960 EXPECT_EQ(pdr, record[0]);
961
962 pldm_pdr_destroy(repo);
963}
964
965TEST(FindStateSensorPDR, testNoMatchCompositeSensor)
966{
967 auto repo = pldm_pdr_init();
968 uint8_t tid = 1;
969 uint16_t entityID = 5;
970 uint16_t stateSetId = 1;
971
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400972 std::vector<uint8_t> pdr(
973 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
974 sizeof(struct state_sensor_possible_states) * 3);
Chicago Duan738e4d82020-05-28 16:39:19 +0800975
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530976 auto rec = new (pdr.data()) pldm_state_sensor_pdr;
Chicago Duana7aacc32020-06-10 18:03:38 +0800977 auto state_start = rec->possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800978
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530979 auto state = new (state_start) state_sensor_possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800980
981 rec->hdr.type = 4;
982 rec->hdr.record_handle = 1;
983 rec->entity_type = 21;
984 rec->container_id = 0;
985 rec->composite_sensor_count = 3;
986 state->state_set_id = 15;
987 state->possible_states_size = 1;
988
Chicago Duana7aacc32020-06-10 18:03:38 +0800989 state_start += state->possible_states_size + sizeof(state->state_set_id) +
990 sizeof(state->possible_states_size);
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530991 state = new (state_start) state_sensor_possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800992 state->state_set_id = 19;
993 state->possible_states_size = 1;
994
Chicago Duana7aacc32020-06-10 18:03:38 +0800995 state_start += state->possible_states_size + sizeof(state->state_set_id) +
996 sizeof(state->possible_states_size);
Pavithra Barithaya5ea72372025-02-04 18:09:57 +0530997 state = new (state_start) state_sensor_possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800998 state->state_set_id = 39;
999 state->possible_states_size = 1;
1000
Andrew Jeffery64f37fe2023-07-03 15:41:13 +09301001 uint32_t handle = 0;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +00001002 ASSERT_EQ(pldm_pdr_add(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Chicago Duan738e4d82020-05-28 16:39:19 +08001003
1004 auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
1005
1006 EXPECT_EQ(record.empty(), true);
1007
1008 pldm_pdr_destroy(repo);
Chicago Duana7aacc32020-06-10 18:03:38 +08001009}
Tom Joseph54922072021-06-19 02:45:46 -07001010
1011TEST(toString, allTestCases)
1012{
1013 variable_field buffer{};
1014 constexpr std::string_view str1{};
1015 auto returnStr1 = toString(buffer);
1016 EXPECT_EQ(returnStr1, str1);
1017
1018 constexpr std::string_view str2{"0penBmc"};
1019 constexpr std::array<uint8_t, 7> bufferArr1{0x30, 0x70, 0x65, 0x6e,
1020 0x42, 0x6d, 0x63};
1021 buffer.ptr = bufferArr1.data();
1022 buffer.length = bufferArr1.size();
1023 auto returnStr2 = toString(buffer);
1024 EXPECT_EQ(returnStr2, str2);
1025
1026 constexpr std::string_view str3{"0pen mc"};
1027 // \xa0 - the non-breaking space in ISO-8859-1
1028 constexpr std::array<uint8_t, 7> bufferArr2{0x30, 0x70, 0x65, 0x6e,
1029 0xa0, 0x6d, 0x63};
1030 buffer.ptr = bufferArr2.data();
1031 buffer.length = bufferArr2.size();
1032 auto returnStr3 = toString(buffer);
1033 EXPECT_EQ(returnStr3, str3);
George Liu872f0f62021-11-25 16:26:16 +08001034}
1035
1036TEST(Split, allTestCases)
1037{
1038 std::string s1 = "aa,bb,cc,dd";
1039 auto results1 = split(s1, ",");
1040 EXPECT_EQ(results1[0], "aa");
1041 EXPECT_EQ(results1[1], "bb");
1042 EXPECT_EQ(results1[2], "cc");
1043 EXPECT_EQ(results1[3], "dd");
1044
1045 std::string s2 = "aa||bb||cc||dd";
1046 auto results2 = split(s2, "||");
1047 EXPECT_EQ(results2[0], "aa");
1048 EXPECT_EQ(results2[1], "bb");
1049 EXPECT_EQ(results2[2], "cc");
1050 EXPECT_EQ(results2[3], "dd");
1051
1052 std::string s3 = " aa || bb||cc|| dd";
1053 auto results3 = split(s3, "||", " ");
1054 EXPECT_EQ(results3[0], "aa");
1055 EXPECT_EQ(results3[1], "bb");
1056 EXPECT_EQ(results3[2], "cc");
1057 EXPECT_EQ(results3[3], "dd");
1058
1059 std::string s4 = "aa\\\\bb\\cc\\dd";
1060 auto results4 = split(s4, "\\");
1061 EXPECT_EQ(results4[0], "aa");
1062 EXPECT_EQ(results4[1], "bb");
1063 EXPECT_EQ(results4[2], "cc");
1064 EXPECT_EQ(results4[3], "dd");
1065
1066 std::string s5 = "aa\\";
1067 auto results5 = split(s5, "\\");
1068 EXPECT_EQ(results5[0], "aa");
Manojkiran Eda3ca40452021-10-04 22:51:37 +05301069}
Gilbert Chen6c7fed42022-02-22 15:40:17 +00001070
1071TEST(ValidEID, allTestCases)
1072{
1073 auto rc = isValidEID(MCTP_ADDR_NULL);
1074 EXPECT_EQ(rc, false);
1075 rc = isValidEID(MCTP_ADDR_ANY);
1076 EXPECT_EQ(rc, false);
1077 rc = isValidEID(1);
1078 EXPECT_EQ(rc, false);
1079 rc = isValidEID(2);
1080 EXPECT_EQ(rc, false);
1081 rc = isValidEID(3);
1082 EXPECT_EQ(rc, false);
1083 rc = isValidEID(4);
1084 EXPECT_EQ(rc, false);
1085 rc = isValidEID(5);
1086 EXPECT_EQ(rc, false);
1087 rc = isValidEID(6);
1088 EXPECT_EQ(rc, false);
1089 rc = isValidEID(7);
1090 EXPECT_EQ(rc, false);
1091 rc = isValidEID(MCTP_START_VALID_EID);
1092 EXPECT_EQ(rc, true);
1093 rc = isValidEID(254);
1094 EXPECT_EQ(rc, true);
1095}
Thu Nguyenb8cf46b2024-06-15 02:44:35 +00001096
1097TEST(TrimNameForDbus, goodTest)
1098{
1099 std::string name = "Name with space";
1100 std::string_view expectedName = "Name_with__space";
1101 std::string_view result = trimNameForDbus(name);
1102 EXPECT_EQ(expectedName, result);
Pavithra Barithaya5d86cdf2024-08-20 15:08:46 +05301103 name = "Name 1\0"; // NOLINT(bugprone-string-literal-with-embedded-nul)
Thu Nguyenb8cf46b2024-06-15 02:44:35 +00001104 expectedName = "Name_1";
1105 result = trimNameForDbus(name);
1106 EXPECT_EQ(expectedName, result);
1107}
Thu Nguyena34a64b2022-03-31 08:56:39 +07001108
1109TEST(dbusPropValuesToDouble, goodTest)
1110{
1111 double value = 0;
1112 bool ret =
1113 dbusPropValuesToDouble("uint8_t", static_cast<uint8_t>(0x12), &value);
1114 EXPECT_EQ(true, ret);
1115 EXPECT_EQ(0x12, value);
1116 ret =
1117 dbusPropValuesToDouble("int16_t", static_cast<int16_t>(0x1234), &value);
1118 EXPECT_EQ(true, ret);
1119 EXPECT_EQ(0x1234, value);
1120 ret = dbusPropValuesToDouble("uint16_t", static_cast<uint16_t>(0x8234),
1121 &value);
1122 EXPECT_EQ(true, ret);
1123 EXPECT_EQ(0x8234, value);
1124 ret = dbusPropValuesToDouble("int32_t", static_cast<int32_t>(0x12345678),
1125 &value);
1126 EXPECT_EQ(true, ret);
1127 EXPECT_EQ(0x12345678, value);
1128 ret = dbusPropValuesToDouble("uint32_t", static_cast<uint32_t>(0x82345678),
1129 &value);
1130 EXPECT_EQ(true, ret);
1131 EXPECT_EQ(0x82345678, value);
1132 ret = dbusPropValuesToDouble(
1133 "int64_t", static_cast<int64_t>(0x1234567898765432), &value);
1134 EXPECT_EQ(true, ret);
1135 EXPECT_EQ(0x1234567898765432, value);
1136 ret = dbusPropValuesToDouble(
1137 "uint64_t", static_cast<uint64_t>(0x8234567898765432), &value);
1138 EXPECT_EQ(true, ret);
1139 EXPECT_EQ(0x8234567898765432, value);
1140 ret = dbusPropValuesToDouble("double", static_cast<double>(1234.5678),
1141 &value);
1142 EXPECT_EQ(true, ret);
1143 EXPECT_EQ(1234.5678, value);
1144}
1145
1146TEST(dbusPropValuesToDouble, badTest)
1147{
1148 double value = std::numeric_limits<double>::quiet_NaN();
1149 /* Type and Data variant are different */
1150 bool ret =
1151 dbusPropValuesToDouble("uint8_t", static_cast<uint16_t>(0x12), &value);
1152 EXPECT_EQ(false, ret);
1153 /* Unsupported Types */
1154 ret = dbusPropValuesToDouble("string", static_cast<std::string>("hello"),
1155 &value);
1156 EXPECT_EQ(false, ret);
1157 ret = dbusPropValuesToDouble("bool", static_cast<bool>(true), &value);
1158 EXPECT_EQ(false, ret);
1159 ret = dbusPropValuesToDouble("vector<uint8_t>",
1160 static_cast<std::string>("hello"), &value);
1161 EXPECT_EQ(false, ret);
1162 ret = dbusPropValuesToDouble("vector<string>",
1163 static_cast<std::string>("hello"), &value);
1164 EXPECT_EQ(false, ret);
1165 /* Support Type but Data Type is unsupported */
1166 ret = dbusPropValuesToDouble("double", static_cast<std::string>("hello"),
1167 &value);
1168 EXPECT_EQ(false, ret);
1169 /* Null pointer */
1170 ret = dbusPropValuesToDouble("double", static_cast<std::string>("hello"),
1171 nullptr);
1172 EXPECT_EQ(false, ret);
1173}
Dung Caob6d39432024-06-05 03:46:47 +00001174
1175TEST(FruFieldValuestring, goodTest)
1176{
1177 std::vector<uint8_t> data = {0x41, 0x6d, 0x70, 0x65, 0x72, 0x65};
1178 std::string expectedString = "Ampere";
1179 auto result = fruFieldValuestring(data.data(), data.size());
1180 EXPECT_EQ(expectedString, result);
1181}
1182
1183TEST(FruFieldValuestring, BadTest)
1184{
1185 std::vector<uint8_t> data = {0x41, 0x6d, 0x70, 0x65, 0x72, 0x65};
1186 auto result = fruFieldValuestring(data.data(), 0);
1187 EXPECT_EQ(std::nullopt, result);
1188 result = fruFieldValuestring(nullptr, data.size());
1189 EXPECT_EQ(std::nullopt, result);
1190}
1191
1192TEST(fruFieldParserU32, goodTest)
1193{
1194 std::vector<uint8_t> data = {0x10, 0x12, 0x14, 0x25};
1195 uint32_t expectedU32 = 0x25141210;
1196 auto result = fruFieldParserU32(data.data(), data.size());
1197 EXPECT_EQ(expectedU32, result.value());
1198}
1199
1200TEST(fruFieldParserU32, BadTest)
1201{
1202 std::vector<uint8_t> data = {0x10, 0x12, 0x14, 0x25};
1203 auto result = fruFieldParserU32(data.data(), data.size() - 1);
1204 EXPECT_EQ(std::nullopt, result);
1205 result = fruFieldParserU32(nullptr, data.size());
1206 EXPECT_EQ(std::nullopt, result);
1207}