blob: 549e28984a8b7669fe40727a7180eba605c8786b [file] [log] [blame]
Deepak Kodihallid130e1a2020-06-17 05:55:32 -05001#include "common/utils.hpp"
George Liu6492f522020-06-16 10:34:05 +08002
George Liuc453e162022-12-21 17:16:23 +08003#include <libpldm/platform.h>
4
George Liu83409572019-12-24 18:42:54 +08005#include <gtest/gtest.h>
6
7using namespace pldm::utils;
8
9TEST(decodeDate, testGooduintToDate)
10{
11 uint64_t data = 20191212115959;
12 uint16_t year = 2019;
13 uint8_t month = 12;
14 uint8_t day = 12;
15 uint8_t hours = 11;
16 uint8_t minutes = 59;
17 uint8_t seconds = 59;
18
19 uint16_t retyear = 0;
20 uint8_t retmonth = 0;
21 uint8_t retday = 0;
22 uint8_t rethours = 0;
23 uint8_t retminutes = 0;
24 uint8_t retseconds = 0;
25
26 auto ret = uintToDate(data, &retyear, &retmonth, &retday, &rethours,
27 &retminutes, &retseconds);
28
29 EXPECT_EQ(ret, true);
30 EXPECT_EQ(year, retyear);
31 EXPECT_EQ(month, retmonth);
32 EXPECT_EQ(day, retday);
33 EXPECT_EQ(hours, rethours);
34 EXPECT_EQ(minutes, retminutes);
35 EXPECT_EQ(seconds, retseconds);
36}
37
38TEST(decodeDate, testBaduintToDate)
39{
40 uint64_t data = 10191212115959;
41
42 uint16_t retyear = 0;
43 uint8_t retmonth = 0;
44 uint8_t retday = 0;
45 uint8_t rethours = 0;
46 uint8_t retminutes = 0;
47 uint8_t retseconds = 0;
48
49 auto ret = uintToDate(data, &retyear, &retmonth, &retday, &rethours,
50 &retminutes, &retseconds);
51
52 EXPECT_EQ(ret, false);
53}
54
George Liuba4c1fb2020-02-05 14:13:30 +080055TEST(parseEffecterData, testGoodDecodeEffecterData)
George Liu83409572019-12-24 18:42:54 +080056{
George Liuba4c1fb2020-02-05 14:13:30 +080057 std::vector<uint8_t> effecterData = {1, 1, 0, 1};
58 uint8_t effecterCount = 2;
George Liu83409572019-12-24 18:42:54 +080059 set_effecter_state_field stateField0 = {1, 1};
George Liuba4c1fb2020-02-05 14:13:30 +080060 set_effecter_state_field stateField1 = {0, 1};
George Liu83409572019-12-24 18:42:54 +080061
George Liuba4c1fb2020-02-05 14:13:30 +080062 auto effecterField = parseEffecterData(effecterData, effecterCount);
63 EXPECT_NE(effecterField, std::nullopt);
64 EXPECT_EQ(effecterCount, effecterField->size());
George Liu83409572019-12-24 18:42:54 +080065
George Liuba4c1fb2020-02-05 14:13:30 +080066 std::vector<set_effecter_state_field> stateField = effecterField.value();
George Liu83409572019-12-24 18:42:54 +080067 EXPECT_EQ(stateField[0].set_request, stateField0.set_request);
68 EXPECT_EQ(stateField[0].effecter_state, stateField0.effecter_state);
69 EXPECT_EQ(stateField[1].set_request, stateField1.set_request);
70 EXPECT_EQ(stateField[1].effecter_state, stateField1.effecter_state);
George Liu83409572019-12-24 18:42:54 +080071}
72
George Liuba4c1fb2020-02-05 14:13:30 +080073TEST(parseEffecterData, testBadDecodeEffecterData)
George Liu83409572019-12-24 18:42:54 +080074{
George Liuba4c1fb2020-02-05 14:13:30 +080075 std::vector<uint8_t> effecterData = {0, 1, 0, 1, 0, 1};
76 uint8_t effecterCount = 2;
George Liu83409572019-12-24 18:42:54 +080077
George Liuba4c1fb2020-02-05 14:13:30 +080078 auto effecterField = parseEffecterData(effecterData, effecterCount);
George Liu83409572019-12-24 18:42:54 +080079
George Liuba4c1fb2020-02-05 14:13:30 +080080 EXPECT_EQ(effecterField, std::nullopt);
George Liu83409572019-12-24 18:42:54 +080081}
Pavithra Barithaya0f74c982020-04-27 02:17:10 -050082
83TEST(FindStateEffecterPDR, testOneMatch)
84{
Pavithra Barithaya0f74c982020-04-27 02:17:10 -050085 auto repo = pldm_pdr_init();
86 uint8_t tid = 1;
87 uint16_t entityID = 33;
88 uint16_t stateSetId = 196;
89
90 std::vector<uint8_t> pdr(sizeof(struct pldm_state_effecter_pdr) -
91 sizeof(uint8_t) +
92 sizeof(struct state_effecter_possible_states));
93
94 auto rec = reinterpret_cast<pldm_state_effecter_pdr*>(pdr.data());
95
96 auto state =
97 reinterpret_cast<state_effecter_possible_states*>(rec->possible_states);
98
99 rec->hdr.type = 11;
100 rec->hdr.record_handle = 1;
101 rec->entity_type = 33;
102 rec->container_id = 0;
103 rec->composite_effecter_count = 1;
104 state->state_set_id = 196;
105 state->possible_states_size = 1;
106
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930107 uint32_t handle = 0;
108 ASSERT_EQ(
109 pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500110
111 auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
112
113 EXPECT_EQ(pdr, record[0]);
114
115 pldm_pdr_destroy(repo);
116}
117
118TEST(FindStateEffecterPDR, testNoMatch)
119{
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500120 auto repo = pldm_pdr_init();
121 uint8_t tid = 1;
122 uint16_t entityID = 44;
123 uint16_t stateSetId = 196;
124
125 std::vector<uint8_t> pdr(sizeof(struct pldm_state_effecter_pdr) -
126 sizeof(uint8_t) +
127 sizeof(struct state_effecter_possible_states));
128
129 auto rec = reinterpret_cast<pldm_state_effecter_pdr*>(pdr.data());
130
131 auto state =
132 reinterpret_cast<state_effecter_possible_states*>(rec->possible_states);
133
134 rec->hdr.type = 11;
135 rec->hdr.record_handle = 1;
136 rec->entity_type = 33;
137 rec->container_id = 0;
138 rec->composite_effecter_count = 1;
139 state->state_set_id = 196;
140 state->possible_states_size = 1;
141
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930142 uint32_t handle = 0;
143 ASSERT_EQ(
144 pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500145
146 auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
147
148 EXPECT_EQ(record.empty(), true);
149
150 pldm_pdr_destroy(repo);
151}
152
153TEST(FindStateEffecterPDR, testEmptyRepo)
154{
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500155 auto repo = pldm_pdr_init();
156 uint8_t tid = 1;
157 uint16_t entityID = 33;
158 uint16_t stateSetId = 196;
159
160 std::vector<uint8_t> pdr(sizeof(struct pldm_state_effecter_pdr) -
161 sizeof(uint8_t) +
162 sizeof(struct state_effecter_possible_states));
163
164 auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
165
166 EXPECT_EQ(record.empty(), true);
167
168 pldm_pdr_destroy(repo);
169}
170
171TEST(FindStateEffecterPDR, testMoreMatch)
172{
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500173 auto repo = pldm_pdr_init();
174 uint8_t tid = 1;
175
176 std::vector<uint8_t> pdr(sizeof(struct pldm_state_effecter_pdr) -
177 sizeof(uint8_t) +
178 sizeof(struct state_effecter_possible_states));
179
180 auto rec = reinterpret_cast<pldm_state_effecter_pdr*>(pdr.data());
181
182 auto state =
183 reinterpret_cast<state_effecter_possible_states*>(rec->possible_states);
184
185 rec->hdr.type = 11;
186 rec->hdr.record_handle = 1;
187 rec->entity_type = 31;
188 rec->container_id = 0;
189 rec->composite_effecter_count = 1;
190 state->state_set_id = 129;
191 state->possible_states_size = 1;
192
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930193 uint32_t handle = 0;
194 ASSERT_EQ(
195 pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500196
197 std::vector<uint8_t> pdr_second(
198 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
199 sizeof(struct state_effecter_possible_states));
200
201 auto rec_second =
202 reinterpret_cast<pldm_state_effecter_pdr*>(pdr_second.data());
203
204 auto state_second = reinterpret_cast<state_effecter_possible_states*>(
205 rec_second->possible_states);
206
207 rec_second->hdr.type = 11;
208 rec_second->hdr.record_handle = 2;
209 rec_second->entity_type = 31;
210 rec_second->container_id = 0;
211 rec_second->composite_effecter_count = 1;
212 state_second->state_set_id = 129;
213 state_second->possible_states_size = 1;
214
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930215 handle = 0;
216 ASSERT_EQ(pldm_pdr_add_check(repo, pdr_second.data(), pdr_second.size(),
217 false, 1, &handle),
218 0);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500219
220 uint16_t entityID_ = 31;
221 uint16_t stateSetId_ = 129;
222
223 auto record = findStateEffecterPDR(tid, entityID_, stateSetId_, repo);
224
225 EXPECT_EQ(pdr, record[0]);
226 EXPECT_EQ(pdr_second, record[1]);
227
228 pldm_pdr_destroy(repo);
229}
230
231TEST(FindStateEffecterPDR, testManyNoMatch)
232{
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500233 auto repo = pldm_pdr_init();
234 uint8_t tid = 1;
235 uint16_t entityID = 33;
236 uint16_t stateSetId = 196;
237
238 std::vector<uint8_t> pdr(sizeof(struct pldm_state_effecter_pdr) -
239 sizeof(uint8_t) +
240 sizeof(struct state_effecter_possible_states));
241
242 auto rec = reinterpret_cast<pldm_state_effecter_pdr*>(pdr.data());
243
244 auto state =
245 reinterpret_cast<state_effecter_possible_states*>(rec->possible_states);
246
247 rec->hdr.type = 11;
248 rec->hdr.record_handle = 1;
249 rec->entity_type = 34;
250 rec->container_id = 0;
251 rec->composite_effecter_count = 1;
252 state->state_set_id = 198;
253 state->possible_states_size = 1;
254
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930255 uint32_t handle = 0;
256 ASSERT_EQ(
257 pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500258
259 std::vector<uint8_t> pdr_second(
260 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
261 sizeof(struct state_effecter_possible_states));
262
263 auto rec_second =
264 reinterpret_cast<pldm_state_effecter_pdr*>(pdr_second.data());
265
266 auto state_second = reinterpret_cast<state_effecter_possible_states*>(
267 rec_second->possible_states);
268
269 rec_second->hdr.type = 11;
270 rec_second->hdr.record_handle = 2;
271 rec_second->entity_type = 39;
272 rec_second->container_id = 0;
273 rec_second->composite_effecter_count = 1;
274 state_second->state_set_id = 169;
275 state_second->possible_states_size = 1;
276
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930277 handle = 0;
278 ASSERT_EQ(pldm_pdr_add_check(repo, pdr_second.data(), pdr_second.size(),
279 false, 1, &handle),
280 0);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500281
282 auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
283
284 EXPECT_EQ(record.empty(), true);
285
286 pldm_pdr_destroy(repo);
287}
288
289TEST(FindStateEffecterPDR, testOneMatchOneNoMatch)
290{
291 auto repo = pldm_pdr_init();
292 uint8_t tid = 1;
293 uint16_t entityID = 67;
294 uint16_t stateSetId = 192;
295
296 std::vector<uint8_t> pdr(sizeof(struct pldm_state_effecter_pdr) -
297 sizeof(uint8_t) +
298 sizeof(struct state_effecter_possible_states));
299
300 auto rec = reinterpret_cast<pldm_state_effecter_pdr*>(pdr.data());
301
302 auto state =
303 reinterpret_cast<state_effecter_possible_states*>(rec->possible_states);
304
305 rec->hdr.type = 11;
306 rec->hdr.record_handle = 1;
307 rec->entity_type = 32;
308 rec->container_id = 0;
309 rec->composite_effecter_count = 1;
310 state->state_set_id = 198;
311 state->possible_states_size = 1;
312
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930313 uint32_t handle = 0;
314 ASSERT_EQ(
315 pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500316
317 std::vector<uint8_t> pdr_second(
318 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
319 sizeof(struct state_effecter_possible_states));
320
321 auto rec_second =
322 reinterpret_cast<pldm_state_effecter_pdr*>(pdr_second.data());
323
324 auto state_second = reinterpret_cast<state_effecter_possible_states*>(
325 rec_second->possible_states);
326
327 rec_second->hdr.type = 11;
328 rec_second->hdr.record_handle = 2;
329 rec_second->entity_type = 67;
330 rec_second->container_id = 0;
331 rec_second->composite_effecter_count = 1;
332 state_second->state_set_id = 192;
333 state_second->possible_states_size = 1;
334
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930335 handle = 0;
336 ASSERT_EQ(pldm_pdr_add_check(repo, pdr_second.data(), pdr_second.size(),
337 false, 1, &handle),
338 0);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500339
340 auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
341
342 EXPECT_EQ(pdr_second, record[0]);
343 EXPECT_EQ(record.size(), 1);
344
345 pldm_pdr_destroy(repo);
346}
347
348TEST(FindStateEffecterPDR, testOneMatchManyNoMatch)
349{
350 auto repo = pldm_pdr_init();
351 uint8_t tid = 1;
352 uint16_t entityID = 67;
353 uint16_t stateSetId = 192;
354
355 std::vector<uint8_t> pdr(sizeof(struct pldm_state_effecter_pdr) -
356 sizeof(uint8_t) +
357 sizeof(struct state_effecter_possible_states));
358
359 auto rec = reinterpret_cast<pldm_state_effecter_pdr*>(pdr.data());
360
361 auto state =
362 reinterpret_cast<state_effecter_possible_states*>(rec->possible_states);
363
364 rec->hdr.type = 11;
365 rec->hdr.record_handle = 1;
366 rec->entity_type = 32;
367 rec->container_id = 0;
368 rec->composite_effecter_count = 1;
369 state->state_set_id = 198;
370 state->possible_states_size = 1;
371
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930372 uint32_t handle = 0;
373 ASSERT_EQ(
374 pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500375
376 std::vector<uint8_t> pdr_second(
377 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
378 sizeof(struct state_effecter_possible_states));
379
380 auto rec_second =
381 reinterpret_cast<pldm_state_effecter_pdr*>(pdr_second.data());
382
383 auto state_second = reinterpret_cast<state_effecter_possible_states*>(
384 rec_second->possible_states);
385
386 rec_second->hdr.type = 11;
387 rec_second->hdr.record_handle = 2;
388 rec_second->entity_type = 67;
389 rec_second->container_id = 0;
390 rec_second->composite_effecter_count = 1;
391 state_second->state_set_id = 192;
392 state_second->possible_states_size = 1;
393
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930394 handle = 0;
395 ASSERT_EQ(pldm_pdr_add_check(repo, pdr_second.data(), pdr_second.size(),
396 false, 1, &handle),
397 0);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500398
399 std::vector<uint8_t> pdr_third(
400 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
401 sizeof(struct state_effecter_possible_states));
402
403 auto rec_third =
404 reinterpret_cast<pldm_state_effecter_pdr*>(pdr_third.data());
405
406 auto state_third = reinterpret_cast<state_effecter_possible_states*>(
407 rec_third->possible_states);
408
409 rec_third->hdr.type = 11;
410 rec_third->hdr.record_handle = 3;
411 rec_third->entity_type = 69;
412 rec_third->container_id = 0;
413 rec_third->composite_effecter_count = 1;
414 state_third->state_set_id = 199;
415 state_third->possible_states_size = 1;
416
417 auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
418
419 EXPECT_EQ(pdr_second, record[0]);
420 EXPECT_EQ(record.size(), 1);
421
422 pldm_pdr_destroy(repo);
423}
424
425TEST(FindStateEffecterPDR, testCompositeEffecter)
426{
427 auto repo = pldm_pdr_init();
428 uint8_t tid = 1;
429 uint16_t entityID = 67;
430 uint16_t stateSetId = 192;
431
432 std::vector<uint8_t> pdr(sizeof(struct pldm_state_effecter_pdr) -
433 sizeof(uint8_t) +
Chicago Duana7aacc32020-06-10 18:03:38 +0800434 sizeof(struct state_effecter_possible_states) * 3);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500435
436 auto rec = reinterpret_cast<pldm_state_effecter_pdr*>(pdr.data());
Chicago Duana7aacc32020-06-10 18:03:38 +0800437 auto state_start = rec->possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500438
Chicago Duana7aacc32020-06-10 18:03:38 +0800439 auto state = reinterpret_cast<state_effecter_possible_states*>(state_start);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500440
441 rec->hdr.type = 11;
442 rec->hdr.record_handle = 1;
443 rec->entity_type = 67;
444 rec->container_id = 0;
445 rec->composite_effecter_count = 3;
446 state->state_set_id = 198;
447 state->possible_states_size = 1;
448
Chicago Duana7aacc32020-06-10 18:03:38 +0800449 state_start += state->possible_states_size + sizeof(state->state_set_id) +
450 sizeof(state->possible_states_size);
451 state = reinterpret_cast<state_effecter_possible_states*>(state_start);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500452 state->state_set_id = 193;
453 state->possible_states_size = 1;
454
Chicago Duana7aacc32020-06-10 18:03:38 +0800455 state_start += state->possible_states_size + sizeof(state->state_set_id) +
456 sizeof(state->possible_states_size);
457 state = reinterpret_cast<state_effecter_possible_states*>(state_start);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500458 state->state_set_id = 192;
459 state->possible_states_size = 1;
460
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930461 uint32_t handle = 0;
462 ASSERT_EQ(
463 pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500464
465 auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
466
467 EXPECT_EQ(pdr, record[0]);
468
469 pldm_pdr_destroy(repo);
470}
471
472TEST(FindStateEffecterPDR, testNoMatchCompositeEffecter)
473{
474 auto repo = pldm_pdr_init();
475 uint8_t tid = 1;
476 uint16_t entityID = 67;
477 uint16_t stateSetId = 192;
478
479 std::vector<uint8_t> pdr(sizeof(struct pldm_state_effecter_pdr) -
480 sizeof(uint8_t) +
Chicago Duana7aacc32020-06-10 18:03:38 +0800481 sizeof(struct state_effecter_possible_states) * 3);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500482
483 auto rec = reinterpret_cast<pldm_state_effecter_pdr*>(pdr.data());
Chicago Duana7aacc32020-06-10 18:03:38 +0800484 auto state_start = rec->possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500485
Chicago Duana7aacc32020-06-10 18:03:38 +0800486 auto state = reinterpret_cast<state_effecter_possible_states*>(state_start);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500487
488 rec->hdr.type = 11;
489 rec->hdr.record_handle = 1;
490 rec->entity_type = 34;
491 rec->container_id = 0;
492 rec->composite_effecter_count = 3;
493 state->state_set_id = 198;
494 state->possible_states_size = 1;
495
Chicago Duana7aacc32020-06-10 18:03:38 +0800496 state_start += state->possible_states_size + sizeof(state->state_set_id) +
497 sizeof(state->possible_states_size);
498 state = reinterpret_cast<state_effecter_possible_states*>(state_start);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500499 state->state_set_id = 193;
500 state->possible_states_size = 1;
501
Chicago Duana7aacc32020-06-10 18:03:38 +0800502 state_start += state->possible_states_size + sizeof(state->state_set_id) +
503 sizeof(state->possible_states_size);
504 state = reinterpret_cast<state_effecter_possible_states*>(state_start);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500505 state->state_set_id = 123;
506 state->possible_states_size = 1;
507
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930508 uint32_t handle = 0;
509 ASSERT_EQ(
510 pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500511
512 auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
513
514 EXPECT_EQ(record.empty(), true);
515
516 pldm_pdr_destroy(repo);
517}
Chicago Duan738e4d82020-05-28 16:39:19 +0800518
519TEST(FindStateSensorPDR, testOneMatch)
520{
Chicago Duan738e4d82020-05-28 16:39:19 +0800521 auto repo = pldm_pdr_init();
522 uint8_t tid = 1;
523 uint16_t entityID = 5;
524 uint16_t stateSetId = 1;
525
526 std::vector<uint8_t> pdr(sizeof(struct pldm_state_sensor_pdr) -
527 sizeof(uint8_t) +
528 sizeof(struct state_sensor_possible_states));
529
530 auto rec = reinterpret_cast<pldm_state_sensor_pdr*>(pdr.data());
531
532 auto state =
533 reinterpret_cast<state_sensor_possible_states*>(rec->possible_states);
534
535 rec->hdr.type = 4;
536 rec->hdr.record_handle = 1;
537 rec->entity_type = 5;
538 rec->container_id = 0;
539 rec->composite_sensor_count = 1;
540 state->state_set_id = 1;
541 state->possible_states_size = 1;
542
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930543 uint32_t handle = 0;
544 ASSERT_EQ(
545 pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Chicago Duan738e4d82020-05-28 16:39:19 +0800546
547 auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
548
549 EXPECT_EQ(pdr, record[0]);
550
551 pldm_pdr_destroy(repo);
552}
553
554TEST(FindStateSensorPDR, testNoMatch)
555{
Chicago Duan738e4d82020-05-28 16:39:19 +0800556 auto repo = pldm_pdr_init();
557 uint8_t tid = 1;
558 uint16_t entityID = 5;
559 uint16_t stateSetId = 1;
560
561 std::vector<uint8_t> pdr(sizeof(struct pldm_state_sensor_pdr) -
562 sizeof(uint8_t) +
563 sizeof(struct state_sensor_possible_states));
564
565 auto rec = reinterpret_cast<pldm_state_sensor_pdr*>(pdr.data());
566
567 auto state =
568 reinterpret_cast<state_sensor_possible_states*>(rec->possible_states);
569
570 rec->hdr.type = 4;
571 rec->hdr.record_handle = 1;
572 rec->entity_type = 55;
573 rec->container_id = 0;
574 rec->composite_sensor_count = 1;
575 state->state_set_id = 1;
576 state->possible_states_size = 1;
577
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930578 uint32_t handle = 0;
579 ASSERT_EQ(
580 pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Chicago Duan738e4d82020-05-28 16:39:19 +0800581
582 auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
583
584 EXPECT_EQ(record.empty(), true);
585
586 pldm_pdr_destroy(repo);
587}
588
589TEST(FindStateSensorPDR, testEmptyRepo)
590{
Chicago Duan738e4d82020-05-28 16:39:19 +0800591 auto repo = pldm_pdr_init();
592 uint8_t tid = 1;
593 uint16_t entityID = 5;
594 uint16_t stateSetId = 1;
595
596 std::vector<uint8_t> pdr(sizeof(struct pldm_state_sensor_pdr) -
597 sizeof(uint8_t) +
598 sizeof(struct state_sensor_possible_states));
599
600 auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
601
602 EXPECT_EQ(record.empty(), true);
603
604 pldm_pdr_destroy(repo);
605}
606
607TEST(FindStateSensorPDR, testMoreMatch)
608{
Chicago Duan738e4d82020-05-28 16:39:19 +0800609 auto repo = pldm_pdr_init();
610 uint8_t tid = 1;
611
612 std::vector<uint8_t> pdr(sizeof(struct pldm_state_sensor_pdr) -
613 sizeof(uint8_t) +
614 sizeof(struct state_sensor_possible_states));
615
616 auto rec = reinterpret_cast<pldm_state_sensor_pdr*>(pdr.data());
617
618 auto state =
619 reinterpret_cast<state_sensor_possible_states*>(rec->possible_states);
620
621 rec->hdr.type = 4;
622 rec->hdr.record_handle = 1;
623 rec->entity_type = 5;
624 rec->container_id = 0;
625 rec->composite_sensor_count = 1;
626 state->state_set_id = 1;
627 state->possible_states_size = 1;
628
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930629 uint32_t handle = 0;
630 ASSERT_EQ(
631 pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Chicago Duan738e4d82020-05-28 16:39:19 +0800632
633 std::vector<uint8_t> pdr_second(
634 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
635 sizeof(struct state_sensor_possible_states));
636
637 auto rec_second =
638 reinterpret_cast<pldm_state_sensor_pdr*>(pdr_second.data());
639
640 auto state_second = reinterpret_cast<state_sensor_possible_states*>(
641 rec_second->possible_states);
642
643 rec_second->hdr.type = 4;
644 rec_second->hdr.record_handle = 2;
645 rec_second->entity_type = 5;
646 rec_second->container_id = 0;
647 rec_second->composite_sensor_count = 1;
648 state_second->state_set_id = 1;
649 state_second->possible_states_size = 1;
650
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930651 handle = 0;
652 ASSERT_EQ(pldm_pdr_add_check(repo, pdr_second.data(), pdr_second.size(),
653 false, 1, &handle),
654 0);
Chicago Duan738e4d82020-05-28 16:39:19 +0800655
656 uint16_t entityID_ = 5;
657 uint16_t stateSetId_ = 1;
658
659 auto record = findStateSensorPDR(tid, entityID_, stateSetId_, repo);
660
661 EXPECT_EQ(pdr, record[0]);
662 EXPECT_EQ(pdr_second, record[1]);
663
664 pldm_pdr_destroy(repo);
665}
666
667TEST(FindStateSensorPDR, testManyNoMatch)
668{
Chicago Duan738e4d82020-05-28 16:39:19 +0800669 auto repo = pldm_pdr_init();
670 uint8_t tid = 1;
671 uint16_t entityID = 5;
672 uint16_t stateSetId = 1;
673
674 std::vector<uint8_t> pdr(sizeof(struct pldm_state_sensor_pdr) -
675 sizeof(uint8_t) +
676 sizeof(struct state_sensor_possible_states));
677
678 auto rec = reinterpret_cast<pldm_state_sensor_pdr*>(pdr.data());
679
680 auto state =
681 reinterpret_cast<state_sensor_possible_states*>(rec->possible_states);
682
683 rec->hdr.type = 4;
684 rec->hdr.record_handle = 1;
685 rec->entity_type = 56;
686 rec->container_id = 0;
687 rec->composite_sensor_count = 1;
688 state->state_set_id = 2;
689 state->possible_states_size = 1;
690
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930691 uint32_t handle = 0;
692 ASSERT_EQ(
693 pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Chicago Duan738e4d82020-05-28 16:39:19 +0800694
695 std::vector<uint8_t> pdr_second(
696 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
697 sizeof(struct state_sensor_possible_states));
698
699 auto rec_second =
700 reinterpret_cast<pldm_state_sensor_pdr*>(pdr_second.data());
701
702 auto state_second = reinterpret_cast<state_sensor_possible_states*>(
703 rec_second->possible_states);
704
705 rec_second->hdr.type = 4;
706 rec_second->hdr.record_handle = 2;
707 rec_second->entity_type = 66;
708 rec_second->container_id = 0;
709 rec_second->composite_sensor_count = 1;
710 state_second->state_set_id = 3;
711 state_second->possible_states_size = 1;
712
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930713 handle = 0;
714 ASSERT_EQ(pldm_pdr_add_check(repo, pdr_second.data(), pdr_second.size(),
715 false, 1, &handle),
716 0);
Chicago Duan738e4d82020-05-28 16:39:19 +0800717
718 auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
719
720 EXPECT_EQ(record.empty(), true);
721
722 pldm_pdr_destroy(repo);
723}
724
725TEST(FindStateSensorPDR, testOneMatchOneNoMatch)
726{
727 auto repo = pldm_pdr_init();
728 uint8_t tid = 1;
729 uint16_t entityID = 5;
730 uint16_t stateSetId = 1;
731
732 std::vector<uint8_t> pdr(sizeof(struct pldm_state_sensor_pdr) -
733 sizeof(uint8_t) +
734 sizeof(struct state_sensor_possible_states));
735
736 auto rec = reinterpret_cast<pldm_state_sensor_pdr*>(pdr.data());
737
738 auto state =
739 reinterpret_cast<state_sensor_possible_states*>(rec->possible_states);
740
741 rec->hdr.type = 4;
742 rec->hdr.record_handle = 1;
743 rec->entity_type = 10;
744 rec->container_id = 0;
745 rec->composite_sensor_count = 1;
746 state->state_set_id = 20;
747 state->possible_states_size = 1;
748
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930749 uint32_t handle = 0;
750 ASSERT_EQ(
751 pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Chicago Duan738e4d82020-05-28 16:39:19 +0800752
753 std::vector<uint8_t> pdr_second(
754 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
755 sizeof(struct state_sensor_possible_states));
756
757 auto rec_second =
758 reinterpret_cast<pldm_state_sensor_pdr*>(pdr_second.data());
759
760 auto state_second = reinterpret_cast<state_sensor_possible_states*>(
761 rec_second->possible_states);
762
763 rec_second->hdr.type = 4;
764 rec_second->hdr.record_handle = 2;
765 rec_second->entity_type = 5;
766 rec_second->container_id = 0;
767 rec_second->composite_sensor_count = 1;
768 state_second->state_set_id = 1;
769 state_second->possible_states_size = 1;
770
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930771 handle = 0;
772 ASSERT_EQ(pldm_pdr_add_check(repo, pdr_second.data(), pdr_second.size(),
773 false, 1, &handle),
774 0);
Chicago Duan738e4d82020-05-28 16:39:19 +0800775
776 auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
777
778 EXPECT_EQ(pdr_second, record[0]);
779 EXPECT_EQ(record.size(), 1);
780
781 pldm_pdr_destroy(repo);
782}
783
784TEST(FindStateSensorPDR, testOneMatchManyNoMatch)
785{
786 auto repo = pldm_pdr_init();
787 uint8_t tid = 1;
788 uint16_t entityID = 5;
789 uint16_t stateSetId = 1;
790
791 std::vector<uint8_t> pdr(sizeof(struct pldm_state_sensor_pdr) -
792 sizeof(uint8_t) +
793 sizeof(struct state_sensor_possible_states));
794
795 auto rec = reinterpret_cast<pldm_state_sensor_pdr*>(pdr.data());
796
797 auto state =
798 reinterpret_cast<state_sensor_possible_states*>(rec->possible_states);
799
800 rec->hdr.type = 4;
801 rec->hdr.record_handle = 1;
802 rec->entity_type = 6;
803 rec->container_id = 0;
804 rec->composite_sensor_count = 1;
805 state->state_set_id = 9;
806 state->possible_states_size = 1;
807
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930808 uint32_t handle = 0;
809 ASSERT_EQ(
810 pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Chicago Duan738e4d82020-05-28 16:39:19 +0800811
812 std::vector<uint8_t> pdr_second(
813 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
814 sizeof(struct state_sensor_possible_states));
815
816 auto rec_second =
817 reinterpret_cast<pldm_state_sensor_pdr*>(pdr_second.data());
818
819 auto state_second = reinterpret_cast<state_sensor_possible_states*>(
820 rec_second->possible_states);
821
822 rec_second->hdr.type = 4;
823 rec_second->hdr.record_handle = 2;
824 rec_second->entity_type = 5;
825 rec_second->container_id = 0;
826 rec_second->composite_sensor_count = 1;
827 state_second->state_set_id = 1;
828 state_second->possible_states_size = 1;
829
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930830 handle = 0;
831 ASSERT_EQ(pldm_pdr_add_check(repo, pdr_second.data(), pdr_second.size(),
832 false, 1, &handle),
833 0);
Chicago Duan738e4d82020-05-28 16:39:19 +0800834
835 std::vector<uint8_t> pdr_third(sizeof(struct pldm_state_sensor_pdr) -
836 sizeof(uint8_t) +
837 sizeof(struct state_sensor_possible_states));
838
839 auto rec_third = reinterpret_cast<pldm_state_sensor_pdr*>(pdr_third.data());
840
841 auto state_third = reinterpret_cast<state_sensor_possible_states*>(
842 rec_third->possible_states);
843
844 rec_third->hdr.type = 4;
845 rec_third->hdr.record_handle = 3;
846 rec_third->entity_type = 7;
847 rec_third->container_id = 0;
848 rec_third->composite_sensor_count = 1;
849 state_third->state_set_id = 12;
850 state_third->possible_states_size = 1;
851
852 auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
853
854 EXPECT_EQ(pdr_second, record[0]);
855 EXPECT_EQ(record.size(), 1);
856
857 pldm_pdr_destroy(repo);
858}
859
860TEST(FindStateSensorPDR, testCompositeSensor)
861{
862 auto repo = pldm_pdr_init();
863 uint8_t tid = 1;
864 uint16_t entityID = 5;
865 uint16_t stateSetId = 1;
866
867 std::vector<uint8_t> pdr(sizeof(struct pldm_state_sensor_pdr) -
868 sizeof(uint8_t) +
Chicago Duana7aacc32020-06-10 18:03:38 +0800869 sizeof(struct state_sensor_possible_states) * 3);
Chicago Duan738e4d82020-05-28 16:39:19 +0800870
871 auto rec = reinterpret_cast<pldm_state_sensor_pdr*>(pdr.data());
Chicago Duana7aacc32020-06-10 18:03:38 +0800872 auto state_start = rec->possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800873
Chicago Duana7aacc32020-06-10 18:03:38 +0800874 auto state = reinterpret_cast<state_sensor_possible_states*>(state_start);
Chicago Duan738e4d82020-05-28 16:39:19 +0800875
876 rec->hdr.type = 4;
877 rec->hdr.record_handle = 1;
878 rec->entity_type = 5;
879 rec->container_id = 0;
880 rec->composite_sensor_count = 3;
881 state->state_set_id = 2;
882 state->possible_states_size = 1;
883
Chicago Duana7aacc32020-06-10 18:03:38 +0800884 state_start += state->possible_states_size + sizeof(state->state_set_id) +
885 sizeof(state->possible_states_size);
886 state = reinterpret_cast<state_sensor_possible_states*>(state_start);
887
Chicago Duan738e4d82020-05-28 16:39:19 +0800888 state->state_set_id = 7;
889 state->possible_states_size = 1;
890
Chicago Duana7aacc32020-06-10 18:03:38 +0800891 state_start += state->possible_states_size + sizeof(state->state_set_id) +
892 sizeof(state->possible_states_size);
893 state = reinterpret_cast<state_sensor_possible_states*>(state_start);
894
Chicago Duan738e4d82020-05-28 16:39:19 +0800895 state->state_set_id = 1;
896 state->possible_states_size = 1;
897
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930898 uint32_t handle = 0;
899 ASSERT_EQ(
900 pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Chicago Duan738e4d82020-05-28 16:39:19 +0800901
902 auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
903
904 EXPECT_EQ(pdr, record[0]);
905
906 pldm_pdr_destroy(repo);
907}
908
909TEST(FindStateSensorPDR, testNoMatchCompositeSensor)
910{
911 auto repo = pldm_pdr_init();
912 uint8_t tid = 1;
913 uint16_t entityID = 5;
914 uint16_t stateSetId = 1;
915
916 std::vector<uint8_t> pdr(sizeof(struct pldm_state_sensor_pdr) -
917 sizeof(uint8_t) +
Chicago Duana7aacc32020-06-10 18:03:38 +0800918 sizeof(struct state_sensor_possible_states) * 3);
Chicago Duan738e4d82020-05-28 16:39:19 +0800919
920 auto rec = reinterpret_cast<pldm_state_sensor_pdr*>(pdr.data());
Chicago Duana7aacc32020-06-10 18:03:38 +0800921 auto state_start = rec->possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800922
Chicago Duana7aacc32020-06-10 18:03:38 +0800923 auto state = reinterpret_cast<state_sensor_possible_states*>(state_start);
Chicago Duan738e4d82020-05-28 16:39:19 +0800924
925 rec->hdr.type = 4;
926 rec->hdr.record_handle = 1;
927 rec->entity_type = 21;
928 rec->container_id = 0;
929 rec->composite_sensor_count = 3;
930 state->state_set_id = 15;
931 state->possible_states_size = 1;
932
Chicago Duana7aacc32020-06-10 18:03:38 +0800933 state_start += state->possible_states_size + sizeof(state->state_set_id) +
934 sizeof(state->possible_states_size);
935 state = reinterpret_cast<state_sensor_possible_states*>(state_start);
Chicago Duan738e4d82020-05-28 16:39:19 +0800936 state->state_set_id = 19;
937 state->possible_states_size = 1;
938
Chicago Duana7aacc32020-06-10 18:03:38 +0800939 state_start += state->possible_states_size + sizeof(state->state_set_id) +
940 sizeof(state->possible_states_size);
941 state = reinterpret_cast<state_sensor_possible_states*>(state_start);
Chicago Duan738e4d82020-05-28 16:39:19 +0800942 state->state_set_id = 39;
943 state->possible_states_size = 1;
944
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930945 uint32_t handle = 0;
946 ASSERT_EQ(
947 pldm_pdr_add_check(repo, pdr.data(), pdr.size(), false, 1, &handle), 0);
Chicago Duan738e4d82020-05-28 16:39:19 +0800948
949 auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
950
951 EXPECT_EQ(record.empty(), true);
952
953 pldm_pdr_destroy(repo);
Chicago Duana7aacc32020-06-10 18:03:38 +0800954}
Tom Joseph54922072021-06-19 02:45:46 -0700955
956TEST(toString, allTestCases)
957{
958 variable_field buffer{};
959 constexpr std::string_view str1{};
960 auto returnStr1 = toString(buffer);
961 EXPECT_EQ(returnStr1, str1);
962
963 constexpr std::string_view str2{"0penBmc"};
964 constexpr std::array<uint8_t, 7> bufferArr1{0x30, 0x70, 0x65, 0x6e,
965 0x42, 0x6d, 0x63};
966 buffer.ptr = bufferArr1.data();
967 buffer.length = bufferArr1.size();
968 auto returnStr2 = toString(buffer);
969 EXPECT_EQ(returnStr2, str2);
970
971 constexpr std::string_view str3{"0pen mc"};
972 // \xa0 - the non-breaking space in ISO-8859-1
973 constexpr std::array<uint8_t, 7> bufferArr2{0x30, 0x70, 0x65, 0x6e,
974 0xa0, 0x6d, 0x63};
975 buffer.ptr = bufferArr2.data();
976 buffer.length = bufferArr2.size();
977 auto returnStr3 = toString(buffer);
978 EXPECT_EQ(returnStr3, str3);
George Liu872f0f62021-11-25 16:26:16 +0800979}
980
981TEST(Split, allTestCases)
982{
983 std::string s1 = "aa,bb,cc,dd";
984 auto results1 = split(s1, ",");
985 EXPECT_EQ(results1[0], "aa");
986 EXPECT_EQ(results1[1], "bb");
987 EXPECT_EQ(results1[2], "cc");
988 EXPECT_EQ(results1[3], "dd");
989
990 std::string s2 = "aa||bb||cc||dd";
991 auto results2 = split(s2, "||");
992 EXPECT_EQ(results2[0], "aa");
993 EXPECT_EQ(results2[1], "bb");
994 EXPECT_EQ(results2[2], "cc");
995 EXPECT_EQ(results2[3], "dd");
996
997 std::string s3 = " aa || bb||cc|| dd";
998 auto results3 = split(s3, "||", " ");
999 EXPECT_EQ(results3[0], "aa");
1000 EXPECT_EQ(results3[1], "bb");
1001 EXPECT_EQ(results3[2], "cc");
1002 EXPECT_EQ(results3[3], "dd");
1003
1004 std::string s4 = "aa\\\\bb\\cc\\dd";
1005 auto results4 = split(s4, "\\");
1006 EXPECT_EQ(results4[0], "aa");
1007 EXPECT_EQ(results4[1], "bb");
1008 EXPECT_EQ(results4[2], "cc");
1009 EXPECT_EQ(results4[3], "dd");
1010
1011 std::string s5 = "aa\\";
1012 auto results5 = split(s5, "\\");
1013 EXPECT_EQ(results5[0], "aa");
Manojkiran Eda3ca40452021-10-04 22:51:37 +05301014}