blob: 11bf027b4ee6e49ce746f9737669e199e37ff7ec [file] [log] [blame]
Pavithra Barithaya0f74c982020-04-27 02:17:10 -05001#include "libpldm/platform.h"
2
Deepak Kodihallid130e1a2020-06-17 05:55:32 -05003#include "common/utils.hpp"
George Liu6492f522020-06-16 10:34:05 +08004
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
107 pldm_pdr_add(repo, pdr.data(), pdr.size(), 0, false);
108
109 auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
110
111 EXPECT_EQ(pdr, record[0]);
112
113 pldm_pdr_destroy(repo);
114}
115
116TEST(FindStateEffecterPDR, testNoMatch)
117{
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500118 auto repo = pldm_pdr_init();
119 uint8_t tid = 1;
120 uint16_t entityID = 44;
121 uint16_t stateSetId = 196;
122
123 std::vector<uint8_t> pdr(sizeof(struct pldm_state_effecter_pdr) -
124 sizeof(uint8_t) +
125 sizeof(struct state_effecter_possible_states));
126
127 auto rec = reinterpret_cast<pldm_state_effecter_pdr*>(pdr.data());
128
129 auto state =
130 reinterpret_cast<state_effecter_possible_states*>(rec->possible_states);
131
132 rec->hdr.type = 11;
133 rec->hdr.record_handle = 1;
134 rec->entity_type = 33;
135 rec->container_id = 0;
136 rec->composite_effecter_count = 1;
137 state->state_set_id = 196;
138 state->possible_states_size = 1;
139
140 pldm_pdr_add(repo, pdr.data(), pdr.size(), 0, false);
141
142 auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
143
144 EXPECT_EQ(record.empty(), true);
145
146 pldm_pdr_destroy(repo);
147}
148
149TEST(FindStateEffecterPDR, testEmptyRepo)
150{
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500151 auto repo = pldm_pdr_init();
152 uint8_t tid = 1;
153 uint16_t entityID = 33;
154 uint16_t stateSetId = 196;
155
156 std::vector<uint8_t> pdr(sizeof(struct pldm_state_effecter_pdr) -
157 sizeof(uint8_t) +
158 sizeof(struct state_effecter_possible_states));
159
160 auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
161
162 EXPECT_EQ(record.empty(), true);
163
164 pldm_pdr_destroy(repo);
165}
166
167TEST(FindStateEffecterPDR, testMoreMatch)
168{
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500169 auto repo = pldm_pdr_init();
170 uint8_t tid = 1;
171
172 std::vector<uint8_t> pdr(sizeof(struct pldm_state_effecter_pdr) -
173 sizeof(uint8_t) +
174 sizeof(struct state_effecter_possible_states));
175
176 auto rec = reinterpret_cast<pldm_state_effecter_pdr*>(pdr.data());
177
178 auto state =
179 reinterpret_cast<state_effecter_possible_states*>(rec->possible_states);
180
181 rec->hdr.type = 11;
182 rec->hdr.record_handle = 1;
183 rec->entity_type = 31;
184 rec->container_id = 0;
185 rec->composite_effecter_count = 1;
186 state->state_set_id = 129;
187 state->possible_states_size = 1;
188
189 pldm_pdr_add(repo, pdr.data(), pdr.size(), 0, false);
190
191 std::vector<uint8_t> pdr_second(
192 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
193 sizeof(struct state_effecter_possible_states));
194
195 auto rec_second =
196 reinterpret_cast<pldm_state_effecter_pdr*>(pdr_second.data());
197
198 auto state_second = reinterpret_cast<state_effecter_possible_states*>(
199 rec_second->possible_states);
200
201 rec_second->hdr.type = 11;
202 rec_second->hdr.record_handle = 2;
203 rec_second->entity_type = 31;
204 rec_second->container_id = 0;
205 rec_second->composite_effecter_count = 1;
206 state_second->state_set_id = 129;
207 state_second->possible_states_size = 1;
208
209 pldm_pdr_add(repo, pdr_second.data(), pdr_second.size(), 0, false);
210
211 uint16_t entityID_ = 31;
212 uint16_t stateSetId_ = 129;
213
214 auto record = findStateEffecterPDR(tid, entityID_, stateSetId_, repo);
215
216 EXPECT_EQ(pdr, record[0]);
217 EXPECT_EQ(pdr_second, record[1]);
218
219 pldm_pdr_destroy(repo);
220}
221
222TEST(FindStateEffecterPDR, testManyNoMatch)
223{
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500224 auto repo = pldm_pdr_init();
225 uint8_t tid = 1;
226 uint16_t entityID = 33;
227 uint16_t stateSetId = 196;
228
229 std::vector<uint8_t> pdr(sizeof(struct pldm_state_effecter_pdr) -
230 sizeof(uint8_t) +
231 sizeof(struct state_effecter_possible_states));
232
233 auto rec = reinterpret_cast<pldm_state_effecter_pdr*>(pdr.data());
234
235 auto state =
236 reinterpret_cast<state_effecter_possible_states*>(rec->possible_states);
237
238 rec->hdr.type = 11;
239 rec->hdr.record_handle = 1;
240 rec->entity_type = 34;
241 rec->container_id = 0;
242 rec->composite_effecter_count = 1;
243 state->state_set_id = 198;
244 state->possible_states_size = 1;
245
246 pldm_pdr_add(repo, pdr.data(), pdr.size(), 0, false);
247
248 std::vector<uint8_t> pdr_second(
249 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
250 sizeof(struct state_effecter_possible_states));
251
252 auto rec_second =
253 reinterpret_cast<pldm_state_effecter_pdr*>(pdr_second.data());
254
255 auto state_second = reinterpret_cast<state_effecter_possible_states*>(
256 rec_second->possible_states);
257
258 rec_second->hdr.type = 11;
259 rec_second->hdr.record_handle = 2;
260 rec_second->entity_type = 39;
261 rec_second->container_id = 0;
262 rec_second->composite_effecter_count = 1;
263 state_second->state_set_id = 169;
264 state_second->possible_states_size = 1;
265
266 pldm_pdr_add(repo, pdr_second.data(), pdr_second.size(), 0, false);
267
268 auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
269
270 EXPECT_EQ(record.empty(), true);
271
272 pldm_pdr_destroy(repo);
273}
274
275TEST(FindStateEffecterPDR, testOneMatchOneNoMatch)
276{
277 auto repo = pldm_pdr_init();
278 uint8_t tid = 1;
279 uint16_t entityID = 67;
280 uint16_t stateSetId = 192;
281
282 std::vector<uint8_t> pdr(sizeof(struct pldm_state_effecter_pdr) -
283 sizeof(uint8_t) +
284 sizeof(struct state_effecter_possible_states));
285
286 auto rec = reinterpret_cast<pldm_state_effecter_pdr*>(pdr.data());
287
288 auto state =
289 reinterpret_cast<state_effecter_possible_states*>(rec->possible_states);
290
291 rec->hdr.type = 11;
292 rec->hdr.record_handle = 1;
293 rec->entity_type = 32;
294 rec->container_id = 0;
295 rec->composite_effecter_count = 1;
296 state->state_set_id = 198;
297 state->possible_states_size = 1;
298
299 pldm_pdr_add(repo, pdr.data(), pdr.size(), 0, false);
300
301 std::vector<uint8_t> pdr_second(
302 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
303 sizeof(struct state_effecter_possible_states));
304
305 auto rec_second =
306 reinterpret_cast<pldm_state_effecter_pdr*>(pdr_second.data());
307
308 auto state_second = reinterpret_cast<state_effecter_possible_states*>(
309 rec_second->possible_states);
310
311 rec_second->hdr.type = 11;
312 rec_second->hdr.record_handle = 2;
313 rec_second->entity_type = 67;
314 rec_second->container_id = 0;
315 rec_second->composite_effecter_count = 1;
316 state_second->state_set_id = 192;
317 state_second->possible_states_size = 1;
318
319 pldm_pdr_add(repo, pdr_second.data(), pdr_second.size(), 0, false);
320
321 auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
322
323 EXPECT_EQ(pdr_second, record[0]);
324 EXPECT_EQ(record.size(), 1);
325
326 pldm_pdr_destroy(repo);
327}
328
329TEST(FindStateEffecterPDR, testOneMatchManyNoMatch)
330{
331 auto repo = pldm_pdr_init();
332 uint8_t tid = 1;
333 uint16_t entityID = 67;
334 uint16_t stateSetId = 192;
335
336 std::vector<uint8_t> pdr(sizeof(struct pldm_state_effecter_pdr) -
337 sizeof(uint8_t) +
338 sizeof(struct state_effecter_possible_states));
339
340 auto rec = reinterpret_cast<pldm_state_effecter_pdr*>(pdr.data());
341
342 auto state =
343 reinterpret_cast<state_effecter_possible_states*>(rec->possible_states);
344
345 rec->hdr.type = 11;
346 rec->hdr.record_handle = 1;
347 rec->entity_type = 32;
348 rec->container_id = 0;
349 rec->composite_effecter_count = 1;
350 state->state_set_id = 198;
351 state->possible_states_size = 1;
352
353 pldm_pdr_add(repo, pdr.data(), pdr.size(), 0, false);
354
355 std::vector<uint8_t> pdr_second(
356 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
357 sizeof(struct state_effecter_possible_states));
358
359 auto rec_second =
360 reinterpret_cast<pldm_state_effecter_pdr*>(pdr_second.data());
361
362 auto state_second = reinterpret_cast<state_effecter_possible_states*>(
363 rec_second->possible_states);
364
365 rec_second->hdr.type = 11;
366 rec_second->hdr.record_handle = 2;
367 rec_second->entity_type = 67;
368 rec_second->container_id = 0;
369 rec_second->composite_effecter_count = 1;
370 state_second->state_set_id = 192;
371 state_second->possible_states_size = 1;
372
373 pldm_pdr_add(repo, pdr_second.data(), pdr_second.size(), 0, false);
374
375 std::vector<uint8_t> pdr_third(
376 sizeof(struct pldm_state_effecter_pdr) - sizeof(uint8_t) +
377 sizeof(struct state_effecter_possible_states));
378
379 auto rec_third =
380 reinterpret_cast<pldm_state_effecter_pdr*>(pdr_third.data());
381
382 auto state_third = reinterpret_cast<state_effecter_possible_states*>(
383 rec_third->possible_states);
384
385 rec_third->hdr.type = 11;
386 rec_third->hdr.record_handle = 3;
387 rec_third->entity_type = 69;
388 rec_third->container_id = 0;
389 rec_third->composite_effecter_count = 1;
390 state_third->state_set_id = 199;
391 state_third->possible_states_size = 1;
392
393 auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
394
395 EXPECT_EQ(pdr_second, record[0]);
396 EXPECT_EQ(record.size(), 1);
397
398 pldm_pdr_destroy(repo);
399}
400
401TEST(FindStateEffecterPDR, testCompositeEffecter)
402{
403 auto repo = pldm_pdr_init();
404 uint8_t tid = 1;
405 uint16_t entityID = 67;
406 uint16_t stateSetId = 192;
407
408 std::vector<uint8_t> pdr(sizeof(struct pldm_state_effecter_pdr) -
409 sizeof(uint8_t) +
Chicago Duana7aacc32020-06-10 18:03:38 +0800410 sizeof(struct state_effecter_possible_states) * 3);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500411
412 auto rec = reinterpret_cast<pldm_state_effecter_pdr*>(pdr.data());
Chicago Duana7aacc32020-06-10 18:03:38 +0800413 auto state_start = rec->possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500414
Chicago Duana7aacc32020-06-10 18:03:38 +0800415 auto state = reinterpret_cast<state_effecter_possible_states*>(state_start);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500416
417 rec->hdr.type = 11;
418 rec->hdr.record_handle = 1;
419 rec->entity_type = 67;
420 rec->container_id = 0;
421 rec->composite_effecter_count = 3;
422 state->state_set_id = 198;
423 state->possible_states_size = 1;
424
Chicago Duana7aacc32020-06-10 18:03:38 +0800425 state_start += state->possible_states_size + sizeof(state->state_set_id) +
426 sizeof(state->possible_states_size);
427 state = reinterpret_cast<state_effecter_possible_states*>(state_start);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500428 state->state_set_id = 193;
429 state->possible_states_size = 1;
430
Chicago Duana7aacc32020-06-10 18:03:38 +0800431 state_start += state->possible_states_size + sizeof(state->state_set_id) +
432 sizeof(state->possible_states_size);
433 state = reinterpret_cast<state_effecter_possible_states*>(state_start);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500434 state->state_set_id = 192;
435 state->possible_states_size = 1;
436
437 pldm_pdr_add(repo, pdr.data(), pdr.size(), 0, false);
438
439 auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
440
441 EXPECT_EQ(pdr, record[0]);
442
443 pldm_pdr_destroy(repo);
444}
445
446TEST(FindStateEffecterPDR, testNoMatchCompositeEffecter)
447{
448 auto repo = pldm_pdr_init();
449 uint8_t tid = 1;
450 uint16_t entityID = 67;
451 uint16_t stateSetId = 192;
452
453 std::vector<uint8_t> pdr(sizeof(struct pldm_state_effecter_pdr) -
454 sizeof(uint8_t) +
Chicago Duana7aacc32020-06-10 18:03:38 +0800455 sizeof(struct state_effecter_possible_states) * 3);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500456
457 auto rec = reinterpret_cast<pldm_state_effecter_pdr*>(pdr.data());
Chicago Duana7aacc32020-06-10 18:03:38 +0800458 auto state_start = rec->possible_states;
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500459
Chicago Duana7aacc32020-06-10 18:03:38 +0800460 auto state = reinterpret_cast<state_effecter_possible_states*>(state_start);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500461
462 rec->hdr.type = 11;
463 rec->hdr.record_handle = 1;
464 rec->entity_type = 34;
465 rec->container_id = 0;
466 rec->composite_effecter_count = 3;
467 state->state_set_id = 198;
468 state->possible_states_size = 1;
469
Chicago Duana7aacc32020-06-10 18:03:38 +0800470 state_start += state->possible_states_size + sizeof(state->state_set_id) +
471 sizeof(state->possible_states_size);
472 state = reinterpret_cast<state_effecter_possible_states*>(state_start);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500473 state->state_set_id = 193;
474 state->possible_states_size = 1;
475
Chicago Duana7aacc32020-06-10 18:03:38 +0800476 state_start += state->possible_states_size + sizeof(state->state_set_id) +
477 sizeof(state->possible_states_size);
478 state = reinterpret_cast<state_effecter_possible_states*>(state_start);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500479 state->state_set_id = 123;
480 state->possible_states_size = 1;
481
482 pldm_pdr_add(repo, pdr.data(), pdr.size(), 0, false);
483
484 auto record = findStateEffecterPDR(tid, entityID, stateSetId, repo);
485
486 EXPECT_EQ(record.empty(), true);
487
488 pldm_pdr_destroy(repo);
489}
Chicago Duan738e4d82020-05-28 16:39:19 +0800490
491TEST(FindStateSensorPDR, testOneMatch)
492{
Chicago Duan738e4d82020-05-28 16:39:19 +0800493 auto repo = pldm_pdr_init();
494 uint8_t tid = 1;
495 uint16_t entityID = 5;
496 uint16_t stateSetId = 1;
497
498 std::vector<uint8_t> pdr(sizeof(struct pldm_state_sensor_pdr) -
499 sizeof(uint8_t) +
500 sizeof(struct state_sensor_possible_states));
501
502 auto rec = reinterpret_cast<pldm_state_sensor_pdr*>(pdr.data());
503
504 auto state =
505 reinterpret_cast<state_sensor_possible_states*>(rec->possible_states);
506
507 rec->hdr.type = 4;
508 rec->hdr.record_handle = 1;
509 rec->entity_type = 5;
510 rec->container_id = 0;
511 rec->composite_sensor_count = 1;
512 state->state_set_id = 1;
513 state->possible_states_size = 1;
514
515 pldm_pdr_add(repo, pdr.data(), pdr.size(), 0, false);
516
517 auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
518
519 EXPECT_EQ(pdr, record[0]);
520
521 pldm_pdr_destroy(repo);
522}
523
524TEST(FindStateSensorPDR, testNoMatch)
525{
Chicago Duan738e4d82020-05-28 16:39:19 +0800526 auto repo = pldm_pdr_init();
527 uint8_t tid = 1;
528 uint16_t entityID = 5;
529 uint16_t stateSetId = 1;
530
531 std::vector<uint8_t> pdr(sizeof(struct pldm_state_sensor_pdr) -
532 sizeof(uint8_t) +
533 sizeof(struct state_sensor_possible_states));
534
535 auto rec = reinterpret_cast<pldm_state_sensor_pdr*>(pdr.data());
536
537 auto state =
538 reinterpret_cast<state_sensor_possible_states*>(rec->possible_states);
539
540 rec->hdr.type = 4;
541 rec->hdr.record_handle = 1;
542 rec->entity_type = 55;
543 rec->container_id = 0;
544 rec->composite_sensor_count = 1;
545 state->state_set_id = 1;
546 state->possible_states_size = 1;
547
548 pldm_pdr_add(repo, pdr.data(), pdr.size(), 0, false);
549
550 auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
551
552 EXPECT_EQ(record.empty(), true);
553
554 pldm_pdr_destroy(repo);
555}
556
557TEST(FindStateSensorPDR, testEmptyRepo)
558{
Chicago Duan738e4d82020-05-28 16:39:19 +0800559 auto repo = pldm_pdr_init();
560 uint8_t tid = 1;
561 uint16_t entityID = 5;
562 uint16_t stateSetId = 1;
563
564 std::vector<uint8_t> pdr(sizeof(struct pldm_state_sensor_pdr) -
565 sizeof(uint8_t) +
566 sizeof(struct state_sensor_possible_states));
567
568 auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
569
570 EXPECT_EQ(record.empty(), true);
571
572 pldm_pdr_destroy(repo);
573}
574
575TEST(FindStateSensorPDR, testMoreMatch)
576{
Chicago Duan738e4d82020-05-28 16:39:19 +0800577 auto repo = pldm_pdr_init();
578 uint8_t tid = 1;
579
580 std::vector<uint8_t> pdr(sizeof(struct pldm_state_sensor_pdr) -
581 sizeof(uint8_t) +
582 sizeof(struct state_sensor_possible_states));
583
584 auto rec = reinterpret_cast<pldm_state_sensor_pdr*>(pdr.data());
585
586 auto state =
587 reinterpret_cast<state_sensor_possible_states*>(rec->possible_states);
588
589 rec->hdr.type = 4;
590 rec->hdr.record_handle = 1;
591 rec->entity_type = 5;
592 rec->container_id = 0;
593 rec->composite_sensor_count = 1;
594 state->state_set_id = 1;
595 state->possible_states_size = 1;
596
597 pldm_pdr_add(repo, pdr.data(), pdr.size(), 0, false);
598
599 std::vector<uint8_t> pdr_second(
600 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
601 sizeof(struct state_sensor_possible_states));
602
603 auto rec_second =
604 reinterpret_cast<pldm_state_sensor_pdr*>(pdr_second.data());
605
606 auto state_second = reinterpret_cast<state_sensor_possible_states*>(
607 rec_second->possible_states);
608
609 rec_second->hdr.type = 4;
610 rec_second->hdr.record_handle = 2;
611 rec_second->entity_type = 5;
612 rec_second->container_id = 0;
613 rec_second->composite_sensor_count = 1;
614 state_second->state_set_id = 1;
615 state_second->possible_states_size = 1;
616
617 pldm_pdr_add(repo, pdr_second.data(), pdr_second.size(), 0, false);
618
619 uint16_t entityID_ = 5;
620 uint16_t stateSetId_ = 1;
621
622 auto record = findStateSensorPDR(tid, entityID_, stateSetId_, repo);
623
624 EXPECT_EQ(pdr, record[0]);
625 EXPECT_EQ(pdr_second, record[1]);
626
627 pldm_pdr_destroy(repo);
628}
629
630TEST(FindStateSensorPDR, testManyNoMatch)
631{
Chicago Duan738e4d82020-05-28 16:39:19 +0800632 auto repo = pldm_pdr_init();
633 uint8_t tid = 1;
634 uint16_t entityID = 5;
635 uint16_t stateSetId = 1;
636
637 std::vector<uint8_t> pdr(sizeof(struct pldm_state_sensor_pdr) -
638 sizeof(uint8_t) +
639 sizeof(struct state_sensor_possible_states));
640
641 auto rec = reinterpret_cast<pldm_state_sensor_pdr*>(pdr.data());
642
643 auto state =
644 reinterpret_cast<state_sensor_possible_states*>(rec->possible_states);
645
646 rec->hdr.type = 4;
647 rec->hdr.record_handle = 1;
648 rec->entity_type = 56;
649 rec->container_id = 0;
650 rec->composite_sensor_count = 1;
651 state->state_set_id = 2;
652 state->possible_states_size = 1;
653
654 pldm_pdr_add(repo, pdr.data(), pdr.size(), 0, false);
655
656 std::vector<uint8_t> pdr_second(
657 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
658 sizeof(struct state_sensor_possible_states));
659
660 auto rec_second =
661 reinterpret_cast<pldm_state_sensor_pdr*>(pdr_second.data());
662
663 auto state_second = reinterpret_cast<state_sensor_possible_states*>(
664 rec_second->possible_states);
665
666 rec_second->hdr.type = 4;
667 rec_second->hdr.record_handle = 2;
668 rec_second->entity_type = 66;
669 rec_second->container_id = 0;
670 rec_second->composite_sensor_count = 1;
671 state_second->state_set_id = 3;
672 state_second->possible_states_size = 1;
673
674 pldm_pdr_add(repo, pdr_second.data(), pdr_second.size(), 0, false);
675
676 auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
677
678 EXPECT_EQ(record.empty(), true);
679
680 pldm_pdr_destroy(repo);
681}
682
683TEST(FindStateSensorPDR, testOneMatchOneNoMatch)
684{
685 auto repo = pldm_pdr_init();
686 uint8_t tid = 1;
687 uint16_t entityID = 5;
688 uint16_t stateSetId = 1;
689
690 std::vector<uint8_t> pdr(sizeof(struct pldm_state_sensor_pdr) -
691 sizeof(uint8_t) +
692 sizeof(struct state_sensor_possible_states));
693
694 auto rec = reinterpret_cast<pldm_state_sensor_pdr*>(pdr.data());
695
696 auto state =
697 reinterpret_cast<state_sensor_possible_states*>(rec->possible_states);
698
699 rec->hdr.type = 4;
700 rec->hdr.record_handle = 1;
701 rec->entity_type = 10;
702 rec->container_id = 0;
703 rec->composite_sensor_count = 1;
704 state->state_set_id = 20;
705 state->possible_states_size = 1;
706
707 pldm_pdr_add(repo, pdr.data(), pdr.size(), 0, false);
708
709 std::vector<uint8_t> pdr_second(
710 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
711 sizeof(struct state_sensor_possible_states));
712
713 auto rec_second =
714 reinterpret_cast<pldm_state_sensor_pdr*>(pdr_second.data());
715
716 auto state_second = reinterpret_cast<state_sensor_possible_states*>(
717 rec_second->possible_states);
718
719 rec_second->hdr.type = 4;
720 rec_second->hdr.record_handle = 2;
721 rec_second->entity_type = 5;
722 rec_second->container_id = 0;
723 rec_second->composite_sensor_count = 1;
724 state_second->state_set_id = 1;
725 state_second->possible_states_size = 1;
726
727 pldm_pdr_add(repo, pdr_second.data(), pdr_second.size(), 0, false);
728
729 auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
730
731 EXPECT_EQ(pdr_second, record[0]);
732 EXPECT_EQ(record.size(), 1);
733
734 pldm_pdr_destroy(repo);
735}
736
737TEST(FindStateSensorPDR, testOneMatchManyNoMatch)
738{
739 auto repo = pldm_pdr_init();
740 uint8_t tid = 1;
741 uint16_t entityID = 5;
742 uint16_t stateSetId = 1;
743
744 std::vector<uint8_t> pdr(sizeof(struct pldm_state_sensor_pdr) -
745 sizeof(uint8_t) +
746 sizeof(struct state_sensor_possible_states));
747
748 auto rec = reinterpret_cast<pldm_state_sensor_pdr*>(pdr.data());
749
750 auto state =
751 reinterpret_cast<state_sensor_possible_states*>(rec->possible_states);
752
753 rec->hdr.type = 4;
754 rec->hdr.record_handle = 1;
755 rec->entity_type = 6;
756 rec->container_id = 0;
757 rec->composite_sensor_count = 1;
758 state->state_set_id = 9;
759 state->possible_states_size = 1;
760
761 pldm_pdr_add(repo, pdr.data(), pdr.size(), 0, false);
762
763 std::vector<uint8_t> pdr_second(
764 sizeof(struct pldm_state_sensor_pdr) - sizeof(uint8_t) +
765 sizeof(struct state_sensor_possible_states));
766
767 auto rec_second =
768 reinterpret_cast<pldm_state_sensor_pdr*>(pdr_second.data());
769
770 auto state_second = reinterpret_cast<state_sensor_possible_states*>(
771 rec_second->possible_states);
772
773 rec_second->hdr.type = 4;
774 rec_second->hdr.record_handle = 2;
775 rec_second->entity_type = 5;
776 rec_second->container_id = 0;
777 rec_second->composite_sensor_count = 1;
778 state_second->state_set_id = 1;
779 state_second->possible_states_size = 1;
780
781 pldm_pdr_add(repo, pdr_second.data(), pdr_second.size(), 0, false);
782
783 std::vector<uint8_t> pdr_third(sizeof(struct pldm_state_sensor_pdr) -
784 sizeof(uint8_t) +
785 sizeof(struct state_sensor_possible_states));
786
787 auto rec_third = reinterpret_cast<pldm_state_sensor_pdr*>(pdr_third.data());
788
789 auto state_third = reinterpret_cast<state_sensor_possible_states*>(
790 rec_third->possible_states);
791
792 rec_third->hdr.type = 4;
793 rec_third->hdr.record_handle = 3;
794 rec_third->entity_type = 7;
795 rec_third->container_id = 0;
796 rec_third->composite_sensor_count = 1;
797 state_third->state_set_id = 12;
798 state_third->possible_states_size = 1;
799
800 auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
801
802 EXPECT_EQ(pdr_second, record[0]);
803 EXPECT_EQ(record.size(), 1);
804
805 pldm_pdr_destroy(repo);
806}
807
808TEST(FindStateSensorPDR, testCompositeSensor)
809{
810 auto repo = pldm_pdr_init();
811 uint8_t tid = 1;
812 uint16_t entityID = 5;
813 uint16_t stateSetId = 1;
814
815 std::vector<uint8_t> pdr(sizeof(struct pldm_state_sensor_pdr) -
816 sizeof(uint8_t) +
Chicago Duana7aacc32020-06-10 18:03:38 +0800817 sizeof(struct state_sensor_possible_states) * 3);
Chicago Duan738e4d82020-05-28 16:39:19 +0800818
819 auto rec = reinterpret_cast<pldm_state_sensor_pdr*>(pdr.data());
Chicago Duana7aacc32020-06-10 18:03:38 +0800820 auto state_start = rec->possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800821
Chicago Duana7aacc32020-06-10 18:03:38 +0800822 auto state = reinterpret_cast<state_sensor_possible_states*>(state_start);
Chicago Duan738e4d82020-05-28 16:39:19 +0800823
824 rec->hdr.type = 4;
825 rec->hdr.record_handle = 1;
826 rec->entity_type = 5;
827 rec->container_id = 0;
828 rec->composite_sensor_count = 3;
829 state->state_set_id = 2;
830 state->possible_states_size = 1;
831
Chicago Duana7aacc32020-06-10 18:03:38 +0800832 state_start += state->possible_states_size + sizeof(state->state_set_id) +
833 sizeof(state->possible_states_size);
834 state = reinterpret_cast<state_sensor_possible_states*>(state_start);
835
Chicago Duan738e4d82020-05-28 16:39:19 +0800836 state->state_set_id = 7;
837 state->possible_states_size = 1;
838
Chicago Duana7aacc32020-06-10 18:03:38 +0800839 state_start += state->possible_states_size + sizeof(state->state_set_id) +
840 sizeof(state->possible_states_size);
841 state = reinterpret_cast<state_sensor_possible_states*>(state_start);
842
Chicago Duan738e4d82020-05-28 16:39:19 +0800843 state->state_set_id = 1;
844 state->possible_states_size = 1;
845
846 pldm_pdr_add(repo, pdr.data(), pdr.size(), 0, false);
847
848 auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
849
850 EXPECT_EQ(pdr, record[0]);
851
852 pldm_pdr_destroy(repo);
853}
854
855TEST(FindStateSensorPDR, testNoMatchCompositeSensor)
856{
857 auto repo = pldm_pdr_init();
858 uint8_t tid = 1;
859 uint16_t entityID = 5;
860 uint16_t stateSetId = 1;
861
862 std::vector<uint8_t> pdr(sizeof(struct pldm_state_sensor_pdr) -
863 sizeof(uint8_t) +
Chicago Duana7aacc32020-06-10 18:03:38 +0800864 sizeof(struct state_sensor_possible_states) * 3);
Chicago Duan738e4d82020-05-28 16:39:19 +0800865
866 auto rec = reinterpret_cast<pldm_state_sensor_pdr*>(pdr.data());
Chicago Duana7aacc32020-06-10 18:03:38 +0800867 auto state_start = rec->possible_states;
Chicago Duan738e4d82020-05-28 16:39:19 +0800868
Chicago Duana7aacc32020-06-10 18:03:38 +0800869 auto state = reinterpret_cast<state_sensor_possible_states*>(state_start);
Chicago Duan738e4d82020-05-28 16:39:19 +0800870
871 rec->hdr.type = 4;
872 rec->hdr.record_handle = 1;
873 rec->entity_type = 21;
874 rec->container_id = 0;
875 rec->composite_sensor_count = 3;
876 state->state_set_id = 15;
877 state->possible_states_size = 1;
878
Chicago Duana7aacc32020-06-10 18:03:38 +0800879 state_start += state->possible_states_size + sizeof(state->state_set_id) +
880 sizeof(state->possible_states_size);
881 state = reinterpret_cast<state_sensor_possible_states*>(state_start);
Chicago Duan738e4d82020-05-28 16:39:19 +0800882 state->state_set_id = 19;
883 state->possible_states_size = 1;
884
Chicago Duana7aacc32020-06-10 18:03:38 +0800885 state_start += state->possible_states_size + sizeof(state->state_set_id) +
886 sizeof(state->possible_states_size);
887 state = reinterpret_cast<state_sensor_possible_states*>(state_start);
Chicago Duan738e4d82020-05-28 16:39:19 +0800888 state->state_set_id = 39;
889 state->possible_states_size = 1;
890
891 pldm_pdr_add(repo, pdr.data(), pdr.size(), 0, false);
892
893 auto record = findStateSensorPDR(tid, entityID, stateSetId, repo);
894
895 EXPECT_EQ(record.empty(), true);
896
897 pldm_pdr_destroy(repo);
Chicago Duana7aacc32020-06-10 18:03:38 +0800898}
Tom Joseph54922072021-06-19 02:45:46 -0700899
900TEST(toString, allTestCases)
901{
902 variable_field buffer{};
903 constexpr std::string_view str1{};
904 auto returnStr1 = toString(buffer);
905 EXPECT_EQ(returnStr1, str1);
906
907 constexpr std::string_view str2{"0penBmc"};
908 constexpr std::array<uint8_t, 7> bufferArr1{0x30, 0x70, 0x65, 0x6e,
909 0x42, 0x6d, 0x63};
910 buffer.ptr = bufferArr1.data();
911 buffer.length = bufferArr1.size();
912 auto returnStr2 = toString(buffer);
913 EXPECT_EQ(returnStr2, str2);
914
915 constexpr std::string_view str3{"0pen mc"};
916 // \xa0 - the non-breaking space in ISO-8859-1
917 constexpr std::array<uint8_t, 7> bufferArr2{0x30, 0x70, 0x65, 0x6e,
918 0xa0, 0x6d, 0x63};
919 buffer.ptr = bufferArr2.data();
920 buffer.length = bufferArr2.size();
921 auto returnStr3 = toString(buffer);
922 EXPECT_EQ(returnStr3, str3);
George Liu872f0f62021-11-25 16:26:16 +0800923}
924
925TEST(Split, allTestCases)
926{
927 std::string s1 = "aa,bb,cc,dd";
928 auto results1 = split(s1, ",");
929 EXPECT_EQ(results1[0], "aa");
930 EXPECT_EQ(results1[1], "bb");
931 EXPECT_EQ(results1[2], "cc");
932 EXPECT_EQ(results1[3], "dd");
933
934 std::string s2 = "aa||bb||cc||dd";
935 auto results2 = split(s2, "||");
936 EXPECT_EQ(results2[0], "aa");
937 EXPECT_EQ(results2[1], "bb");
938 EXPECT_EQ(results2[2], "cc");
939 EXPECT_EQ(results2[3], "dd");
940
941 std::string s3 = " aa || bb||cc|| dd";
942 auto results3 = split(s3, "||", " ");
943 EXPECT_EQ(results3[0], "aa");
944 EXPECT_EQ(results3[1], "bb");
945 EXPECT_EQ(results3[2], "cc");
946 EXPECT_EQ(results3[3], "dd");
947
948 std::string s4 = "aa\\\\bb\\cc\\dd";
949 auto results4 = split(s4, "\\");
950 EXPECT_EQ(results4[0], "aa");
951 EXPECT_EQ(results4[1], "bb");
952 EXPECT_EQ(results4[2], "cc");
953 EXPECT_EQ(results4[3], "dd");
954
955 std::string s5 = "aa\\";
956 auto results5 = split(s5, "\\");
957 EXPECT_EQ(results5[0], "aa");
Tom Joseph54922072021-06-19 02:45:46 -0700958}