summaryrefslogtreecommitdiffstats
path: root/test/message/unpack.cpp
blob: 7d69218bbe31405c21b4ee28679990f64c08c68b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
/**
 * Copyright © 2018 Intel Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <ipmid/api.hpp>
#include <ipmid/message.hpp>

#include <gtest/gtest.h>

TEST(Uints, Uint8)
{
    std::vector<uint8_t> i = {0x04};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    uint8_t v;
    // check that the number of bytes matches
    ASSERT_EQ(p.unpack(v), 0);
    // check that the payload was fully unpacked
    ASSERT_TRUE(p.fullyUnpacked());
    uint8_t k = 0x04;
    // check that the bytes were correctly unpacked (LSB first)
    ASSERT_EQ(v, k);
}

TEST(Uints, Uint8TooManyBytes)
{
    std::vector<uint8_t> i = {0x04, 0x86};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    uint8_t v;
    // check that the number of bytes matches
    ASSERT_EQ(p.unpack(v), 0);
    // check that the payload was not fully unpacked
    ASSERT_FALSE(p.fullyUnpacked());
    uint8_t k = 0x04;
    // check that the bytes were correctly unpacked (LSB first)
    ASSERT_EQ(v, k);
}

TEST(Uints, Uint8InsufficientBytes)
{
    std::vector<uint8_t> i = {};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    uint8_t v = 0;
    // check that the number of bytes matches
    ASSERT_NE(p.unpack(v), 0);
    // check that the payload was not fully unpacked (comprehends unpack errors)
    ASSERT_FALSE(p.fullyUnpacked());
    // check that v is zero
    ASSERT_EQ(v, 0);
}

TEST(Uints, Uint16)
{
    std::vector<uint8_t> i = {0x04, 0x86};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    uint16_t v;
    // check that the number of bytes matches
    ASSERT_EQ(p.unpack(v), 0);
    // check that the payload was fully unpacked
    ASSERT_TRUE(p.fullyUnpacked());
    uint16_t k = 0x8604;
    // check that the bytes were correctly unpacked (LSB first)
    ASSERT_EQ(v, k);
}

TEST(Uints, Uint16TooManyBytes)
{
    std::vector<uint8_t> i = {0x04, 0x86, 0x00};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    uint16_t v;
    // check that the number of bytes matches
    ASSERT_EQ(p.unpack(v), 0);
    // check that the payload was not fully unpacked
    ASSERT_FALSE(p.fullyUnpacked());
    uint16_t k = 0x8604;
    // check that the bytes were correctly unpacked (LSB first)
    ASSERT_EQ(v, k);
}

TEST(Uints, Uint16InsufficientBytes)
{
    std::vector<uint8_t> i = {0x04};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    uint16_t v = 0;
    // check that the number of bytes matches
    ASSERT_NE(p.unpack(v), 0);
    // check that the payload was not fully unpacked (comprehends unpack errors)
    ASSERT_FALSE(p.fullyUnpacked());
    // check that v is zero
    ASSERT_EQ(v, 0);
}

TEST(Uints, Uint32)
{
    std::vector<uint8_t> i = {0x04, 0x86, 0x00, 0x02};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    uint32_t v;
    // check that the number of bytes matches
    ASSERT_EQ(p.unpack(v), 0);
    // check that the payload was fully unpacked
    ASSERT_TRUE(p.fullyUnpacked());
    uint32_t k = 0x02008604;
    // check that the bytes were correctly unpacked (LSB first)
    ASSERT_EQ(v, k);
}

TEST(Uints, Uint32TooManyBytes)
{
    std::vector<uint8_t> i = {0x04, 0x86, 0x00, 0x02, 0x44};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    uint32_t v;
    // check that the number of bytes matches
    ASSERT_EQ(p.unpack(v), 0);
    // check that the payload was not fully unpacked
    ASSERT_FALSE(p.fullyUnpacked());
    uint32_t k = 0x02008604;
    // check that the bytes were correctly unpacked (LSB first)
    ASSERT_EQ(v, k);
}

TEST(Uints, Uint32InsufficientBytes)
{
    std::vector<uint8_t> i = {0x04, 0x86, 0x00};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    uint32_t v = 0;
    // check that the number of bytes matches
    ASSERT_NE(p.unpack(v), 0);
    // check that the payload was not fully unpacked (comprehends unpack errors)
    ASSERT_FALSE(p.fullyUnpacked());
    // check that v is zero
    ASSERT_EQ(v, 0);
}

TEST(Uints, Uint64)
{
    std::vector<uint8_t> i = {0x04, 0x86, 0x00, 0x02, 0x44, 0x33, 0x22, 0x11};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    uint64_t v;
    // check that the number of bytes matches
    ASSERT_EQ(p.unpack(v), 0);
    // check that the payload was fully unpacked
    ASSERT_TRUE(p.fullyUnpacked());
    uint64_t k = 0x1122334402008604ull;
    // check that the bytes were correctly unpacked (LSB first)
    ASSERT_EQ(v, k);
}

TEST(Uints, Uint64TooManyBytes)
{
    std::vector<uint8_t> i = {0x04, 0x86, 0x00, 0x02, 0x44,
                              0x33, 0x22, 0x11, 0x55};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    uint64_t v;
    // check that the number of bytes matches
    ASSERT_EQ(p.unpack(v), 0);
    // check that the payload was not fully unpacked
    ASSERT_FALSE(p.fullyUnpacked());
    uint64_t k = 0x1122334402008604ull;
    // check that the bytes were correctly unpacked (LSB first)
    ASSERT_EQ(v, k);
}

TEST(Uints, Uint64InsufficientBytes)
{
    std::vector<uint8_t> i = {0x04, 0x86, 0x00, 0x02, 0x44, 0x33, 0x22};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    uint64_t v = 0;
    // check that the number of bytes matches
    ASSERT_NE(p.unpack(v), 0);
    // check that the payload was not fully unpacked (comprehends unpack errors)
    ASSERT_FALSE(p.fullyUnpacked());
    // check that v is zero
    ASSERT_EQ(v, 0);
}

TEST(Uints, Uint24)
{
    std::vector<uint8_t> i = {0x58, 0x23, 0x11};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    uint24_t v;
    // check that the number of bytes matches
    ASSERT_EQ(p.unpack(v), 0);
    // check that the payload was fully unpacked
    ASSERT_TRUE(p.fullyUnpacked());
    uint24_t k = 0x112358;
    // check that the bytes were correctly unpacked (LSB first)
    ASSERT_EQ(v, k);
}

TEST(FixedInts, Uint24TooManyBytes)
{
    std::vector<uint8_t> i = {0x58, 0x23, 0x11, 0x00};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    uint24_t v;
    // check that the number of bytes matches
    ASSERT_EQ(p.unpack(v), 0);
    // check that the payload was not fully unpacked
    ASSERT_FALSE(p.fullyUnpacked());
    uint24_t k = 0x112358;
    // check that the bytes were correctly unpacked (LSB first)
    ASSERT_EQ(v, k);
}

TEST(FixedInts, Uint24InsufficientBytes)
{
    std::vector<uint8_t> i = {0x58, 0x23};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    uint24_t v = 0;
    // check that the number of bytes matches
    ASSERT_NE(p.unpack(v), 0);
    // check that the payload was not fully unpacked (comprehends unpack errors)
    ASSERT_FALSE(p.fullyUnpacked());
    // check that v is zero
    ASSERT_EQ(v, 0);
}

TEST(FixedInts, Uint3Uint5)
{
    // individual bytes are unpacked low-order-bits first
    // v1 will use [2:0], v2 will use [7:3]
    std::vector<uint8_t> i = {0xc9};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    uint3_t v1;
    uint5_t v2;
    // check that the number of bytes matches
    ASSERT_EQ(p.unpack(v1, v2), 0);
    // check that the payload was fully unpacked
    ASSERT_TRUE(p.fullyUnpacked());
    uint3_t k1 = 0x1;
    uint5_t k2 = 0x19;
    // check that the bytes were correctly unpacked (LSB first)
    ASSERT_EQ(v1, k1);
    ASSERT_EQ(v2, k2);
}

TEST(FixedInts, Uint3Uint4TooManyBits)
{
    // high order bit should not get unpacked
    std::vector<uint8_t> i = {0xc9};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    uint3_t v1;
    uint4_t v2;
    // check that the number of bytes matches
    ASSERT_EQ(p.unpack(v1, v2), 0);
    // check that the payload was not fully unpacked
    ASSERT_FALSE(p.fullyUnpacked());
    uint3_t k1 = 0x1;
    uint4_t k2 = 0x9;
    // check that the bytes were correctly unpacked (LSB first)
    ASSERT_EQ(v1, k1);
    ASSERT_EQ(v2, k2);
}

TEST(FixedInts, Uint3Uint6InsufficientBits)
{
    // insufficient bits to unpack v2
    std::vector<uint8_t> i = {0xc9};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    uint3_t v1;
    uint6_t v2;
    // check that the number of bytes matches
    ASSERT_NE(p.unpack(v1, v2), 0);
    // check that the payload was not fully unpacked (comprehends unpack errors)
    ASSERT_FALSE(p.fullyUnpacked());
    uint3_t k1 = 0x1;
    // check that the bytes were correctly unpacked (LSB first)
    ASSERT_EQ(v1, k1);
    // check that v2 is zero
    ASSERT_EQ(v2, 0);
}

TEST(Bools, Boolx8)
{
    // individual bytes are unpacked low-order-bits first
    // [v8, v7, v6, v5, v4, v3, v2, v1]
    std::vector<uint8_t> i = {0xc9};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    bool v8, v7, v6, v5;
    bool v4, v3, v2, v1;
    // check that the number of bytes matches
    ASSERT_EQ(p.unpack(v1, v2, v3, v4, v5, v6, v7, v8), 0);
    // check that the payload was fully unpacked
    ASSERT_TRUE(p.fullyUnpacked());
    // check that the bytes were correctly unpacked (LSB first)
    bool k8 = true, k7 = true, k6 = false, k5 = false;
    bool k4 = true, k3 = false, k2 = false, k1 = true;
    ASSERT_EQ(v1, k1);
    ASSERT_EQ(v2, k2);
    ASSERT_EQ(v3, k3);
    ASSERT_EQ(v4, k4);
    ASSERT_EQ(v5, k5);
    ASSERT_EQ(v6, k6);
    ASSERT_EQ(v7, k7);
    ASSERT_EQ(v8, k8);
}

TEST(Bools, Boolx8TooManyBits)
{
    // high order bit should not get unpacked
    // individual bytes are unpacked low-order-bits first
    // [v7, v6, v5, v4, v3, v2, v1]
    std::vector<uint8_t> i = {0xc9};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    bool v7, v6, v5;
    bool v4, v3, v2, v1;
    // check that the number of bytes matches
    ASSERT_EQ(p.unpack(v1, v2, v3, v4, v5, v6, v7), 0);
    // check that the payload was not fully unpacked
    ASSERT_FALSE(p.fullyUnpacked());
    // check that the bytes were correctly unpacked (LSB first)
    bool k7 = true, k6 = false, k5 = false;
    bool k4 = true, k3 = false, k2 = false, k1 = true;
    ASSERT_EQ(v1, k1);
    ASSERT_EQ(v2, k2);
    ASSERT_EQ(v3, k3);
    ASSERT_EQ(v4, k4);
    ASSERT_EQ(v5, k5);
    ASSERT_EQ(v6, k6);
    ASSERT_EQ(v7, k7);
}

TEST(Bools, Boolx8InsufficientBits)
{
    // individual bytes are unpacked low-order-bits first
    // [v8, v7, v6, v5, v4, v3, v2, v1]
    std::vector<uint8_t> i = {0xc9};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    bool v9;
    bool v8, v7, v6, v5;
    bool v4, v3, v2, v1;
    // check that the number of bytes matches
    ASSERT_NE(p.unpack(v1, v2, v3, v4, v5, v6, v7, v8, v9), 0);
    // check that the payload was not fully unpacked (comprehends unpack errors)
    ASSERT_FALSE(p.fullyUnpacked());
    // check that the bytes were correctly unpacked (LSB first)
    bool k8 = true, k7 = true, k6 = false, k5 = false;
    bool k4 = true, k3 = false, k2 = false, k1 = true;
    ASSERT_EQ(v1, k1);
    ASSERT_EQ(v2, k2);
    ASSERT_EQ(v3, k3);
    ASSERT_EQ(v4, k4);
    ASSERT_EQ(v5, k5);
    ASSERT_EQ(v6, k6);
    ASSERT_EQ(v7, k7);
    ASSERT_EQ(v8, k8);
}

TEST(Bitsets, Bitset8)
{
    // individual bytes are unpacked low-order-bits first
    // a bitset for 8 bits fills the full byte
    std::vector<uint8_t> i = {0xc9};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    std::bitset<8> v;
    // check that the number of bytes matches
    ASSERT_EQ(p.unpack(v), 0);
    // check that the payload was fully unpacked
    ASSERT_TRUE(p.fullyUnpacked());
    std::bitset<8> k(0xc9);
    // check that the bytes were correctly unpacked (LSB first)
    ASSERT_EQ(v, k);
}

TEST(Bitsets, Bitset7TooManyBits)
{
    // individual bytes are unpacked low-order-bits first
    // a bitset for 8 bits fills the full byte
    std::vector<uint8_t> i = {0xc9};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    std::bitset<7> v;
    // check that the number of bytes matches
    ASSERT_EQ(p.unpack(v), 0);
    // check that the payload was not fully unpacked
    ASSERT_FALSE(p.fullyUnpacked());
    std::bitset<7> k(0x49);
    // check that the bytes were correctly unpacked (LSB first)
    ASSERT_EQ(v, k);
}

TEST(Bitsets, Bitset9InsufficientBits)
{
    // individual bytes are unpacked low-order-bits first
    // a bitset for 8 bits fills the full byte
    std::vector<uint8_t> i = {0xc9};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    std::bitset<9> v;
    // check that the number of bytes matches
    ASSERT_NE(p.unpack(v), 0);
    // check that the payload was not fully unpacked (comprehends unpack errors)
    ASSERT_FALSE(p.fullyUnpacked());
    std::bitset<9> k(0);
    // check that the bytes were correctly unpacked (LSB first)
    ASSERT_EQ(v, k);
}

TEST(Bitsets, Bitset3Bitset5)
{
    // individual bytes are unpacked low-order-bits first
    // v1 will use [2:0], v2 will use [7:3]
    std::vector<uint8_t> i = {0xc9};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    std::bitset<3> v1;
    std::bitset<5> v2;
    // check that the number of bytes matches
    ASSERT_EQ(p.unpack(v1, v2), 0);
    // check that the payload was fully unpacked
    ASSERT_TRUE(p.fullyUnpacked());
    std::bitset<3> k1(0x1);
    std::bitset<5> k2(0x19);
    // check that the bytes were correctly unpacked (LSB first)
    ASSERT_EQ(v1, k1);
    ASSERT_EQ(v2, k2);
}

TEST(Bitsets, Bitset3Bitset4TooManyBits)
{
    // high order bit should not get unpacked
    std::vector<uint8_t> i = {0xc9};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    std::bitset<3> v1;
    std::bitset<4> v2;
    // check that the number of bytes matches
    ASSERT_EQ(p.unpack(v1, v2), 0);
    // check that the payload was not fully unpacked
    ASSERT_FALSE(p.fullyUnpacked());
    std::bitset<3> k1 = 0x1;
    std::bitset<4> k2 = 0x9;
    // check that the bytes were correctly unpacked (LSB first)
    ASSERT_EQ(v1, k1);
    ASSERT_EQ(v2, k2);
}

TEST(Bitsets, Bitset3Bitset6InsufficientBits)
{
    // insufficient bits to unpack v2
    std::vector<uint8_t> i = {0xc9};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    std::bitset<3> v1;
    std::bitset<6> v2;
    // check that the number of bytes matches
    ASSERT_NE(p.unpack(v1, v2), 0);
    // check that the payload was not fully unpacked (comprehends unpack errors)
    ASSERT_FALSE(p.fullyUnpacked());
    std::bitset<3> k1 = 0x1;
    // check that the bytes were correctly unpacked (LSB first)
    ASSERT_EQ(v1, k1);
    // check that v2 is zero
    ASSERT_EQ(v2, 0);
}

TEST(Bitsets, Bitset32)
{
    // individual bytes are unpacked low-order-bits first
    // v1 will use 4 bytes, but in LSByte first order
    // v1[7:0] v1[15:9] v1[23:16] v1[31:24]
    std::vector<uint8_t> i = {0xb4, 0x86, 0x91, 0xc2};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    std::bitset<32> v;
    // check that the number of bytes matches
    ASSERT_EQ(p.unpack(v), 0);
    // check that the payload was fully unpacked
    ASSERT_TRUE(p.fullyUnpacked());
    std::bitset<32> k(0xc29186b4);
    // check that the bytes were correctly unpacked (LSB first)
    ASSERT_EQ(v, k);
}

TEST(Bitsets, Bitset31TooManyBits)
{
    // high order bit should not get unpacked
    std::vector<uint8_t> i = {0xb4, 0x86, 0x91, 0xc2};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    std::bitset<31> v;
    // check that the number of bytes matches
    ASSERT_EQ(p.unpack(v), 0);
    // check that the payload was not fully unpacked
    ASSERT_FALSE(p.fullyUnpacked());
    std::bitset<31> k(0x429186b4);
    // check that the bytes were correctly unpacked (LSB first)
    ASSERT_EQ(v, k);
}

TEST(Bitsets, Bitset33InsufficientBits)
{
    // insufficient bits to unpack v2
    std::vector<uint8_t> i = {0xb4, 0x86, 0x91, 0xc2};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    std::bitset<33> v;
    // check that the number of bytes matches
    ASSERT_NE(p.unpack(v), 0);
    // check that the payload was not fully unpacked (comprehends unpack errors)
    ASSERT_FALSE(p.fullyUnpacked());
    std::bitset<33> k(0);
    // check that v is zero
    ASSERT_EQ(v, 0);
}

TEST(Arrays, Array4xUint8)
{
    // an array of bytes will be read verbatim, low-order element first
    std::vector<uint8_t> i = {0x02, 0x00, 0x86, 0x04};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    std::array<uint8_t, 4> v;
    // check that the number of bytes matches
    ASSERT_EQ(p.unpack(v), 0);
    // check that the payload was fully unpacked
    ASSERT_TRUE(p.fullyUnpacked());
    std::array<uint8_t, 4> k = {{0x02, 0x00, 0x86, 0x04}};
    // check that the bytes were correctly unpacked (in byte order)
    ASSERT_EQ(v, k);
}

TEST(Arrays, Array4xUint8TooManyBytes)
{
    // last byte should not get unpacked
    // an array of bytes will be read verbatim, low-order element first
    std::vector<uint8_t> i = {0x02, 0x00, 0x86, 0x04, 0x22};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    std::array<uint8_t, 4> v;
    // check that the number of bytes matches
    ASSERT_EQ(p.unpack(v), 0);
    // check that the payload was not fully unpacked
    ASSERT_FALSE(p.fullyUnpacked());
    std::array<uint8_t, 4> k = {{0x02, 0x00, 0x86, 0x04}};
    // check that the bytes were correctly unpacked (in byte order)
    ASSERT_EQ(v, k);
}

TEST(Arrays, Array4xUint8InsufficientBytes)
{
    // last byte should not get unpacked
    // an array of bytes will be read verbatim, low-order element first
    std::vector<uint8_t> i = {0x02, 0x00, 0x86};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    std::array<uint8_t, 4> v;
    // check that the number of bytes matches
    ASSERT_NE(p.unpack(v), 0);
    // check that the payload was not fully unpacked
    ASSERT_FALSE(p.fullyUnpacked());
    // arrays of uint8_t will be unpacked all at once
    // so nothing will get unpacked
    std::array<uint8_t, 4> k = {{0, 0, 0, 0}};
    // check that the bytes were correctly unpacked (in byte order)
    ASSERT_EQ(v, k);
}

TEST(Arrays, Array4xUint32)
{
    // an array of multi-byte values will be unpacked in order low-order
    // element first, each multi-byte element in LSByte order
    // v[0][7:0] v[0][15:9] v[0][23:16] v[0][31:24]
    // v[1][7:0] v[1][15:9] v[1][23:16] v[1][31:24]
    // v[2][7:0] v[2][15:9] v[2][23:16] v[2][31:24]
    // v[3][7:0] v[3][15:9] v[3][23:16] v[3][31:24]
    std::vector<uint8_t> i = {0x44, 0x33, 0x22, 0x11, 0x88, 0x66, 0x44, 0x22,
                              0x99, 0x77, 0x55, 0x33, 0x78, 0x56, 0x34, 0x12};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    std::array<uint32_t, 4> v;
    // check that the number of bytes matches
    ASSERT_EQ(p.unpack(v), 0);
    // check that the payload was fully unpacked
    ASSERT_TRUE(p.fullyUnpacked());
    std::array<uint32_t, 4> k = {
        {0x11223344, 0x22446688, 0x33557799, 0x12345678}};
    // check that the bytes were correctly unpacked (in byte order)
    ASSERT_EQ(v, k);
}

TEST(Arrays, Array4xUint32TooManyBytes)
{
    // last byte should not get unpacked
    // an array of multi-byte values will be unpacked in order low-order
    // element first, each multi-byte element in LSByte order
    // v[0][7:0] v[0][15:9] v[0][23:16] v[0][31:24]
    // v[1][7:0] v[1][15:9] v[1][23:16] v[1][31:24]
    // v[2][7:0] v[2][15:9] v[2][23:16] v[2][31:24]
    // v[3][7:0] v[3][15:9] v[3][23:16] v[3][31:24]
    std::vector<uint8_t> i = {0x44, 0x33, 0x22, 0x11, 0x88, 0x66,
                              0x44, 0x22, 0x99, 0x77, 0x55, 0x33,
                              0x78, 0x56, 0x34, 0x12, 0xaa};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    std::array<uint32_t, 4> v;
    // check that the number of bytes matches
    ASSERT_EQ(p.unpack(v), 0);
    // check that the payload was not fully unpacked
    ASSERT_FALSE(p.fullyUnpacked());
    std::array<uint32_t, 4> k = {
        {0x11223344, 0x22446688, 0x33557799, 0x12345678}};
    // check that the bytes were correctly unpacked (in byte order)
    ASSERT_EQ(v, k);
}

TEST(Arrays, Array4xUint32InsufficientBytes)
{
    // last value should not get unpacked
    // an array of multi-byte values will be unpacked in order low-order
    // element first, each multi-byte element in LSByte order
    // v[0][7:0] v[0][15:9] v[0][23:16] v[0][31:24]
    // v[1][7:0] v[1][15:9] v[1][23:16] v[1][31:24]
    // v[2][7:0] v[2][15:9] v[2][23:16] v[2][31:24]
    // v[3][7:0] v[3][15:9] v[3][23:16] v[3][31:24]
    std::vector<uint8_t> i = {0x44, 0x33, 0x22, 0x11, 0x88, 0x66, 0x44, 0x22,
                              0x99, 0x77, 0x55, 0x33, 0x78, 0x56, 0x34};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    std::array<uint32_t, 4> v;
    // check that the number of bytes matches
    ASSERT_NE(p.unpack(v), 0);
    // check that the payload was not fully unpacked
    ASSERT_FALSE(p.fullyUnpacked());
    // arrays of uint32_t will be unpacked in a way that looks atomic
    std::array<uint32_t, 4> k = {{0, 0, 0, 0}};
    // check that the bytes were correctly unpacked (in byte order)
    ASSERT_EQ(v, k);
}

TEST(Vectors, VectorUint32)
{
    // a vector of multi-byte values will be unpacked in order low-order
    // element first, each multi-byte element in LSByte order
    // v[0][7:0] v[0][15:9] v[0][23:16] v[0][31:24]
    // v[1][7:0] v[1][15:9] v[1][23:16] v[1][31:24]
    // v[2][7:0] v[2][15:9] v[2][23:16] v[2][31:24]
    // v[3][7:0] v[3][15:9] v[3][23:16] v[3][31:24]
    std::vector<uint8_t> i = {0x44, 0x33, 0x22, 0x11, 0x88, 0x66, 0x44, 0x22,
                              0x99, 0x77, 0x55, 0x33, 0x78, 0x56, 0x34, 0x12};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    std::vector<uint32_t> v;
    // check that the number of bytes matches
    ASSERT_EQ(p.unpack(v), 0);
    // check that the payload was fully unpacked
    ASSERT_TRUE(p.fullyUnpacked());
    std::vector<uint32_t> k = {0x11223344, 0x22446688, 0x33557799, 0x12345678};
    // check that the bytes were correctly unpacked (in byte order)
    ASSERT_EQ(v, k);
}

// combination of TooManyBytes and InsufficientBytes because
// vectors will attempt to unpack full <T>s until the end of the input
TEST(Vectors, VectorUint32NonIntegralBytes)
{
    // last value should not get unpacked
    // a vector of multi-byte values will be unpacked in order low-order
    // element first, each multi-byte element in LSByte order,
    // and will attempt to consume all bytes remaining
    // v[0][7:0] v[0][15:9] v[0][23:16] v[0][31:24]
    // v[1][7:0] v[1][15:9] v[1][23:16] v[1][31:24]
    // v[2][7:0] v[2][15:9] v[2][23:16] v[2][31:24]
    // v[3][7:0] v[3][15:9] v[3][23:16] v[3][31:24]
    std::vector<uint8_t> i = {0x44, 0x33, 0x22, 0x11, 0x88, 0x66, 0x44, 0x22,
                              0x99, 0x77, 0x55, 0x33, 0x78, 0x56, 0x34};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    std::vector<uint32_t> v;
    // check that the vector unpacks successfully
    ASSERT_EQ(p.unpack(v), 0);
    // check that the payload was not fully unpacked
    ASSERT_FALSE(p.fullyUnpacked());
    // arrays of uint32_t will be unpacked one at a time, so the
    // last entry should not get unpacked properly
    std::vector<uint32_t> k = {0x11223344, 0x22446688, 0x33557799};
    // check that the bytes were correctly unpacked (in byte order)
    ASSERT_EQ(v, k);
}

TEST(Vectors, VectorUint8)
{
    // a vector of bytes will be unpacked verbatim, low-order element first
    std::vector<uint8_t> i = {0x02, 0x00, 0x86, 0x04};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    std::vector<uint8_t> v;
    // check that the number of bytes matches
    ASSERT_EQ(p.unpack(v), 0);
    // check that the payload was fully unpacked
    ASSERT_TRUE(p.fullyUnpacked());
    std::vector<uint8_t> k = {0x02, 0x00, 0x86, 0x04};
    // check that the bytes were correctly unpacked (in byte order)
    ASSERT_EQ(v, k);
}

TEST(Vectors, VectorEmptyOk)
{
    // an empty input vector to show that unpacking elements is okay
    std::vector<uint8_t> i{};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    std::vector<uint32_t> v;
    // check that the number of bytes matches
    ASSERT_EQ(p.unpack(v), 0);
    // check that the payload was fully unpacked
    ASSERT_TRUE(p.fullyUnpacked());
    std::vector<uint32_t> k{};
    // check that the unpacked vector is empty as expected
    ASSERT_EQ(v, k);
}

TEST(Vectors, VectorOfTuplesOk)
{
    // a vector of bytes will be unpacked verbatim, low-order element first
    std::vector<uint8_t> i = {0x02, 0x00, 0x86, 0x04};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    std::vector<std::tuple<uint8_t, uint8_t>> v;
    // check that the number of bytes matches
    ASSERT_EQ(p.unpack(v), 0);
    // check that the payload was fully unpacked
    ASSERT_TRUE(p.fullyUnpacked());
    std::vector<std::tuple<uint8_t, uint8_t>> k = {{0x02, 0x00}, {0x86, 0x04}};
    // check that the bytes were correctly unpacked (in byte order)
    ASSERT_EQ(v, k);
}

TEST(Vectors, VectorOfTuplesInsufficientBytes)
{
    // a vector of bytes will be unpacked verbatim, low-order element first
    std::vector<uint8_t> i = {0x02, 0x00, 0x86, 0x04, 0xb4};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    std::vector<std::tuple<uint8_t, uint8_t>> v;
    // check that the number of bytes matches
    ASSERT_EQ(p.unpack(v), 0);
    // check that the payload was not fully unpacked
    ASSERT_FALSE(p.fullyUnpacked());
    std::vector<std::tuple<uint8_t, uint8_t>> k = {{0x02, 0x00}, {0x86, 0x04}};
    // check that the bytes were correctly unpacked (in byte order)
    ASSERT_EQ(v, k);
}

// Cannot test TooManyBytes or InsufficientBytes for vector<uint8_t>
// because it will always unpack whatever bytes are remaining
// TEST(Vectors, VectorUint8TooManyBytes) {}
// TEST(Vectors, VectorUint8InsufficientBytes) {}

TEST(UnpackAdvanced, OptionalOk)
{
    // a vector of bytes will be unpacked verbatim, low-order element first
    std::vector<uint8_t> i = {0xbe, 0x02, 0x00, 0x86, 0x04};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    std::optional<std::tuple<uint8_t, uint32_t>> v;
    // check that the number of bytes matches
    ASSERT_EQ(p.unpack(v), 0);
    // check that the payload was fully unpacked
    ASSERT_TRUE(p.fullyUnpacked());
    std::optional<std::tuple<uint8_t, uint32_t>> k{{0xbe, 0x04860002}};
    // check that the bytes were correctly unpacked (in byte order)
    ASSERT_EQ(v, k);
}

TEST(UnpackAdvanced, OptionalInsufficientBytes)
{
    // a vector of bytes will be unpacked verbatim, low-order element first
    std::vector<uint8_t> i = {0x02, 0x00, 0x86, 0x04};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    std::optional<std::tuple<uint8_t, uint32_t>> v;
    // check that the number of bytes matches
    ASSERT_EQ(p.unpack(v), 0);
    // check that the payload was fully unpacked
    ASSERT_FALSE(p.fullyUnpacked());
    std::optional<std::tuple<uint8_t, uint32_t>> k;
    // check that the bytes were correctly unpacked (in byte order)
    ASSERT_EQ(v, k);
}

TEST(UnpackAdvanced, Uints)
{
    // all elements will be unpacked in order, with each multi-byte
    // element being processed LSByte first
    // v1[7:0] v2[7:0] v2[15:8] v3[7:0] v3[15:8] v3[23:16] v3[31:24]
    // v4[7:0] v4[15:8] v4[23:16] v4[31:24]
    // v4[39:25] v4[47:40] v4[55:48] v4[63:56]
    std::vector<uint8_t> i = {0x02, 0x04, 0x06, 0x11, 0x22, 0x33, 0x44, 0x55,
                              0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    uint8_t v1;
    uint16_t v2;
    uint32_t v3;
    uint64_t v4;
    // check that the number of bytes matches
    ASSERT_EQ(p.unpack(v1, v2, v3, v4), 0);
    // check that the payload was fully unpacked
    ASSERT_TRUE(p.fullyUnpacked());
    uint8_t k1 = 0x02;
    uint16_t k2 = 0x0604;
    uint32_t k3 = 0x44332211;
    uint64_t k4 = 0xccbbaa9988776655ull;
    // check that the bytes were correctly unpacked (LSB first)
    ASSERT_EQ(v1, k1);
    ASSERT_EQ(v2, k2);
    ASSERT_EQ(v3, k3);
    ASSERT_EQ(v4, k4);
}

TEST(UnpackAdvanced, TupleInts)
{
    // all elements will be unpacked in order, with each multi-byte
    // element being processed LSByte first
    // v1[7:0] v2[7:0] v2[15:8] v3[7:0] v3[15:8] v3[23:16] v3[31:24]
    // v4[7:0] v4[15:8] v4[23:16] v4[31:24]
    // v4[39:25] v4[47:40] v4[55:48] v4[63:56]
    std::vector<uint8_t> i = {0x02, 0x04, 0x06, 0x11, 0x22, 0x33, 0x44, 0x55,
                              0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    uint8_t v1;
    uint16_t v2;
    uint32_t v3;
    uint64_t v4;
    auto v = std::make_tuple(v1, v2, v3, v4);
    // check that the number of bytes matches
    ASSERT_EQ(p.unpack(v), 0);
    // check that the payload was fully unpacked
    ASSERT_TRUE(p.fullyUnpacked());
    uint8_t k1 = 0x02;
    uint16_t k2 = 0x0604;
    uint32_t k3 = 0x44332211;
    uint64_t k4 = 0xccbbaa9988776655ull;
    auto k = std::make_tuple(k1, k2, k3, k4);
    // check that the bytes were correctly unpacked (LSB first)
    ASSERT_EQ(v, k);
}

TEST(UnpackAdvanced, BoolsnBitfieldsnFixedIntsOhMy)
{
    // each element will be unpacked, filling the low-order bits first
    // with multi-byte values getting unpacked LSByte first
    // v1 will use k[0][1:0]
    // v2 will use k[0][2]
    // v3[4:0] will use k[0][7:3], v3[6:5] will use k[1][1:0]
    // v4 will use k[1][2]
    // v5 will use k[1][7:3]
    std::vector<uint8_t> i = {0x9e, 0xdb};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    uint2_t v1;
    bool v2;
    std::bitset<7> v3;
    bool v4;
    uint5_t v5;
    // check that the number of bytes matches
    ASSERT_EQ(p.unpack(v1, v2, v3, v4, v5), 0);
    // check that the payload was fully unpacked
    ASSERT_TRUE(p.fullyUnpacked());
    uint2_t k1 = 2;          // binary 0b10
    bool k2 = true;          // binary 0b1
    std::bitset<7> k3(0x73); // binary 0b1110011
    bool k4 = false;         // binary 0b0
    uint5_t k5 = 27;         // binary 0b11011
    // check that the bytes were correctly unpacked (LSB first)
    ASSERT_EQ(v1, k1);
    ASSERT_EQ(v2, k2);
    ASSERT_EQ(v3, k3);
    ASSERT_EQ(v4, k4);
    ASSERT_EQ(v5, k5);
}

TEST(UnpackAdvanced, UnalignedBitUnpacking)
{
    // unaligned multi-byte values will be unpacked the same as
    // other bits, effectively reading from a large value, low-order
    // bits first, then consuming the stream LSByte first
    // v1 will use k[0][1:0]
    // v2[5:0] will use k[0][7:2], v2[7:6] will use k[1][1:0]
    // v3 will use k[1][2]
    // v4[4:0] will use k[1][7:3] v4[12:5] will use k[2][7:0]
    // v4[15:13] will use k[3][2:0]
    // v5 will use k[3][3]
    // v6[3:0] will use k[3][7:0] v6[11:4] will use k[4][7:0]
    // v6[19:12] will use k[5][7:0] v6[27:20] will use k[6][7:0]
    // v6[31:28] will use k[7][3:0]
    // v7 will use k[7][7:4]
    std::vector<uint8_t> i = {0x96, 0xd2, 0x2a, 0xcd, 0xd3, 0x3b, 0xbc, 0x9d};
    ipmi::message::Payload p(std::forward<std::vector<uint8_t>>(i));
    uint2_t v1;
    uint8_t v2;
    bool v3;
    uint16_t v4;
    bool v5;
    uint32_t v6;
    uint4_t v7;
    // check that the number of bytes matches
    ASSERT_EQ(p.unpack(v1, v2, v3, v4, v5, v6, v7), 0);
    // check that the payload was fully unpacked
    ASSERT_TRUE(p.fullyUnpacked());
    uint2_t k1 = 2;           // binary 0b10
    uint8_t k2 = 0xa5;        // binary 0b10100101
    bool k3 = false;          // binary 0b0
    uint16_t k4 = 0xa55a;     // binary 0b1010010101011010
    bool k5 = true;           // binary 0b1
    uint32_t k6 = 0xdbc3bd3c; // binary 0b11011011110000111011110100111100
    uint4_t k7 = 9;           // binary 0b1001
    // check that the bytes were correctly unpacked (LSB first)
    ASSERT_EQ(v1, k1);
    ASSERT_EQ(v2, k2);
    ASSERT_EQ(v3, k3);
    ASSERT_EQ(v4, k4);
    ASSERT_EQ(v5, k5);
    ASSERT_EQ(v6, k6);
    ASSERT_EQ(v7, k7);
}
OpenPOWER on IntegriCloud