summaryrefslogtreecommitdiffstats
path: root/test/message/pack.cpp
blob: 03846c5328af9d7c00209b78a14a094001088f3d (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
/**
 * 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>

// TODO: Add testing of Payload response API

TEST(PackBasics, Uint8)
{
    ipmi::message::Payload p;
    uint8_t v = 4;
    p.pack(v);
    // check that the number of bytes matches
    ASSERT_EQ(p.size(), sizeof(v));
    // check that the bytes were correctly packed (LSB first)
    std::vector<uint8_t> k = {0x04};
    ASSERT_EQ(p.raw, k);
}

TEST(PackBasics, Uint16)
{
    ipmi::message::Payload p;
    uint16_t v = 0x8604;
    p.pack(v);
    // check that the number of bytes matches
    ASSERT_EQ(p.size(), sizeof(v));
    // check that the bytes were correctly packed (LSB first)
    std::vector<uint8_t> k = {0x04, 0x86};
    ASSERT_EQ(p.raw, k);
}

TEST(PackBasics, Uint32)
{
    ipmi::message::Payload p;
    uint32_t v = 0x02008604;
    p.pack(v);
    // check that the number of bytes matches
    ASSERT_EQ(p.size(), sizeof(v));
    // check that the bytes were correctly packed (LSB first)
    std::vector<uint8_t> k = {0x04, 0x86, 0x00, 0x02};
    ASSERT_EQ(p.raw, k);
}

TEST(PackBasics, Uint64)
{
    ipmi::message::Payload p;
    uint64_t v = 0x1122334402008604ull;
    p.pack(v);
    // check that the number of bytes matches
    ASSERT_EQ(p.size(), sizeof(v));
    // check that the bytes were correctly packed (LSB first)
    std::vector<uint8_t> k = {0x04, 0x86, 0x00, 0x02, 0x44, 0x33, 0x22, 0x11};
    ASSERT_EQ(p.raw, k);
}

TEST(PackBasics, Uint24)
{
    ipmi::message::Payload p;
    uint24_t v = 0x112358;
    p.pack(v);
    // check that the number of bytes matches
    ASSERT_EQ(p.size(), types::nrFixedBits<decltype(v)> / CHAR_BIT);
    // check that the bytes were correctly packed (LSB first)
    std::vector<uint8_t> k = {0x58, 0x23, 0x11};
    ASSERT_EQ(p.raw, k);
}

TEST(PackBasics, Uint3Uint5)
{
    // individual bytes are packed low-order-bits first
    // v1 will occupy [2:0], v2 will occupy [7:3]
    ipmi::message::Payload p;
    uint3_t v1 = 0x1;
    uint5_t v2 = 0x19;
    p.pack(v1, v2);
    // check that the number of bytes matches
    ASSERT_EQ(p.size(), (types::nrFixedBits<decltype(v1)> +
                         types::nrFixedBits<decltype(v2)>) /
                            CHAR_BIT);
    // check that the bytes were correctly packed (LSB first)
    std::vector<uint8_t> k = {0xc9};
    ASSERT_EQ(p.raw, k);
}

TEST(PackBasics, Boolx8)
{
    // individual bytes are packed low-order-bits first
    // [v8, v7, v6, v5, v4, v3, v2, v1]
    ipmi::message::Payload p;
    bool v8 = true, v7 = true, v6 = false, v5 = false;
    bool v4 = true, v3 = false, v2 = false, v1 = true;
    p.pack(v1, v2, v3, v4, v5, v6, v7, v8);
    // check that the number of bytes matches
    ASSERT_EQ(p.size(), sizeof(uint8_t));
    // check that the bytes were correctly packed (LSB first)
    std::vector<uint8_t> k = {0xc9};
    ASSERT_EQ(p.raw, k);
}

TEST(PackBasics, Bitset8)
{
    // individual bytes are packed low-order-bits first
    // a bitset for 8 bits fills the full byte
    ipmi::message::Payload p;
    std::bitset<8> v(0xc9);
    p.pack(v);
    // check that the number of bytes matches
    ASSERT_EQ(p.size(), v.size() / CHAR_BIT);
    // check that the bytes were correctly packed (LSB first)
    std::vector<uint8_t> k = {0xc9};
    ASSERT_EQ(p.raw, k);
}

TEST(PackBasics, Bitset3Bitset5)
{
    // individual bytes are packed low-order-bits first
    // v1 will occupy [2:0], v2 will occupy [7:3]
    ipmi::message::Payload p;
    std::bitset<3> v1(0x1);
    std::bitset<5> v2(0x19);
    p.pack(v1, v2);
    // check that the number of bytes matches
    ASSERT_EQ(p.size(), (v1.size() + v2.size()) / CHAR_BIT);
    // check that the bytes were correctly packed (LSB first)
    std::vector<uint8_t> k = {0xc9};
    ASSERT_EQ(p.raw, k);
}

TEST(PackBasics, Bitset32)
{
    // individual bytes are packed low-order-bits first
    // v1 will occupy 4 bytes, but in LSByte first order
    // v1[7:0] v1[15:9] v1[23:16] v1[31:24]
    ipmi::message::Payload p;
    std::bitset<32> v(0x02008604);
    p.pack(v);
    // check that the number of bytes matches
    ASSERT_EQ(p.size(), v.size() / CHAR_BIT);
    // check that the bytes were correctly packed (LSB first)
    std::vector<uint8_t> k = {0x04, 0x86, 0x00, 0x02};
    ASSERT_EQ(p.raw, k);
}

TEST(PackBasics, Array4xUint8)
{
    // an array of bytes will be output verbatim, low-order element first
    ipmi::message::Payload p;
    std::array<uint8_t, 4> v = {{0x02, 0x00, 0x86, 0x04}};
    p.pack(v);
    // check that the number of bytes matches
    ASSERT_EQ(p.size(), v.size() * sizeof(v[0]));
    // check that the bytes were correctly packed (in byte order)
    std::vector<uint8_t> k = {0x02, 0x00, 0x86, 0x04};
    ASSERT_EQ(p.raw, k);
}

TEST(PackBasics, Array4xUint32)
{
    // an array of multi-byte values will be output 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]
    ipmi::message::Payload p;
    std::array<uint32_t, 4> v = {
        {0x11223344, 0x22446688, 0x33557799, 0x12345678}};
    p.pack(v);
    // check that the number of bytes matches
    ASSERT_EQ(p.size(), v.size() * sizeof(v[0]));
    // check that the bytes were correctly packed (in byte order)
    std::vector<uint8_t> k = {0x44, 0x33, 0x22, 0x11, 0x88, 0x66, 0x44, 0x22,
                              0x99, 0x77, 0x55, 0x33, 0x78, 0x56, 0x34, 0x12};
    ASSERT_EQ(p.raw, k);
}

TEST(PackBasics, VectorUint32)
{
    // a vector of multi-byte values will be output 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]
    ipmi::message::Payload p;
    std::vector<uint32_t> v = {
        {0x11223344, 0x22446688, 0x33557799, 0x12345678}};
    p.pack(v);
    // check that the number of bytes matches
    ASSERT_EQ(p.size(), v.size() * sizeof(v[0]));
    // check that the bytes were correctly packed (in byte order)
    std::vector<uint8_t> k = {0x44, 0x33, 0x22, 0x11, 0x88, 0x66, 0x44, 0x22,
                              0x99, 0x77, 0x55, 0x33, 0x78, 0x56, 0x34, 0x12};
    ASSERT_EQ(p.raw, k);
}

TEST(PackBasics, VectorUint8)
{
    // a vector of bytes will be output verbatim, low-order element first
    ipmi::message::Payload p;
    std::vector<uint8_t> v = {0x02, 0x00, 0x86, 0x04};
    p.pack(v);
    // check that the number of bytes matches
    ASSERT_EQ(p.size(), v.size() * sizeof(v[0]));
    // check that the bytes were correctly packed (in byte order)
    std::vector<uint8_t> k = {0x02, 0x00, 0x86, 0x04};
    ASSERT_EQ(p.raw, k);
}

TEST(PackBasics, OptionalEmpty)
{
    // an optional will only pack if the value is present
    ipmi::message::Payload p;
    std::optional<uint32_t> v;
    p.pack(v);
    // check that the number of bytes matches
    ASSERT_EQ(p.size(), 0);
    // check that the bytes were correctly packed (in byte order)
    std::vector<uint8_t> k = {};
    ASSERT_EQ(p.raw, k);
}

TEST(PackBasics, OptionalContainsValue)
{
    // an optional will only pack if the value is present
    ipmi::message::Payload p;
    std::optional<uint32_t> v(0x04860002);
    p.pack(v);
    // check that the number of bytes matches
    ASSERT_EQ(p.size(), sizeof(uint32_t));
    // check that the bytes were correctly packed (in byte order)
    std::vector<uint8_t> k = {0x02, 0x00, 0x86, 0x04};
    ASSERT_EQ(p.raw, k);
}

TEST(PackAdvanced, Uints)
{
    // all elements will be processed 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]
    ipmi::message::Payload p;
    uint8_t v1 = 0x02;
    uint16_t v2 = 0x0604;
    uint32_t v3 = 0x44332211;
    uint64_t v4 = 0xccbbaa9988776655ull;
    p.pack(v1, v2, v3, v4);
    // check that the number of bytes matches
    ASSERT_EQ(p.size(), sizeof(v1) + sizeof(v2) + sizeof(v3) + sizeof(v4));
    // check that the bytes were correctly packed (LSB first)
    std::vector<uint8_t> k = {0x02, 0x04, 0x06, 0x11, 0x22, 0x33, 0x44, 0x55,
                              0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc};
    ASSERT_EQ(p.raw, k);
}

TEST(PackAdvanced, TupleInts)
{
    // all elements will be processed 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]
    ipmi::message::Payload p;
    uint8_t v1 = 0x02;
    uint16_t v2 = 0x0604;
    uint32_t v3 = 0x44332211;
    uint64_t v4 = 0xccbbaa9988776655ull;
    auto v = std::make_tuple(v1, v2, v3, v4);
    p.pack(v);
    // check that the number of bytes matches
    ASSERT_EQ(p.size(), sizeof(v1) + sizeof(v2) + sizeof(v3) + sizeof(v4));
    // check that the bytes were correctly packed (LSB first)
    std::vector<uint8_t> k = {0x02, 0x04, 0x06, 0x11, 0x22, 0x33, 0x44, 0x55,
                              0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc};
    ASSERT_EQ(p.raw, k);
}

TEST(PackAdvanced, VariantArray)
{
    ipmi::message::Payload p;
    std::variant<std::array<uint8_t, 2>, uint32_t> variant;
    auto data = std::array<uint8_t, 2>{2, 4};
    variant = data;

    p.pack(variant);
    ASSERT_EQ(p.size(), sizeof(data));

    // check that the bytes were correctly packed packed (LSB first)
    std::vector<uint8_t> k = {2, 4};
    ASSERT_EQ(p.raw, k);
}

TEST(PackAdvanced, BoolsnBitfieldsnFixedIntsOhMy)
{
    // each element will be added, filling the low-order bits first
    // with multi-byte values getting added LSByte first
    // v1 will occupy k[0][1:0]
    // v2 will occupy k[0][2]
    // v3[4:0] will occupy k[0][7:3], v3[6:5] will occupy k[1][1:0]
    // v4 will occupy k[1][2]
    // v5 will occupy k[1][7:3]
    ipmi::message::Payload p;
    uint2_t v1 = 2;          // binary 0b10
    bool v2 = true;          // binary 0b1
    std::bitset<7> v3(0x73); // binary 0b1110011
    bool v4 = false;         // binary 0b0
    uint5_t v5 = 27;         // binary 0b11011
    // concat binary: 0b1101101110011110 -> 0xdb9e -> 0x9e 0xdb (LSByte first)
    p.pack(v1, v2, v3, v4, v5);
    // check that the number of bytes matches
    ASSERT_EQ(p.size(), sizeof(uint16_t));
    // check that the bytes were correctly packed (LSB first)
    std::vector<uint8_t> k = {0x9e, 0xdb};
    ASSERT_EQ(p.raw, k);
}

TEST(PackAdvanced, UnalignedBitPacking)
{
    // unaligned multi-byte values will be packed the same as
    // other bits, effectively building up a large value, low-order
    // bits first, then outputting a stream of LSByte values
    // v1 will occupy k[0][1:0]
    // v2[5:0] will occupy k[0][7:2], v2[7:6] will occupy k[1][1:0]
    // v3 will occupy k[1][2]
    // v4[4:0] will occupy k[1][7:3] v4[12:5] will occupy k[2][7:0]
    // v4[15:13] will occupy k[3][2:0]
    // v5 will occupy k[3][3]
    // v6[3:0] will occupy k[3][7:0] v6[11:4] will occupy k[4][7:0]
    // v6[19:12] will occupy k[5][7:0] v6[27:20] will occupy k[6][7:0]
    // v6[31:28] will occupy k[7][3:0]
    // v7 will occupy k[7][7:4]
    ipmi::message::Payload p;
    uint2_t v1 = 2;           // binary 0b10
    uint8_t v2 = 0xa5;        // binary 0b10100101
    bool v3 = false;          // binary 0b0
    uint16_t v4 = 0xa55a;     // binary 0b1010010101011010
    bool v5 = true;           // binary 0b1
    uint32_t v6 = 0xdbc3bd3c; // binary 0b11011011110000111011110100111100
    uint4_t v7 = 9;           // binary 0b1001
    // concat binary:
    //   0b1001110110111100001110111101001111001101001010101101001010010110
    //   -> 0x9dbc3bd3cd2ad296 -> 0x96 0xd2 0x2a 0xcd 0xd3 0x3b 0xbc 0x9d
    p.pack(v1, v2, v3, v4, v5, v6, v7);
    // check that the number of bytes matches
    ASSERT_EQ(p.size(), sizeof(uint64_t));
    // check that the bytes were correctly packed (LSB first)
    std::vector<uint8_t> k = {0x96, 0xd2, 0x2a, 0xcd, 0xd3, 0x3b, 0xbc, 0x9d};
    ASSERT_EQ(p.raw, k);
}
OpenPOWER on IntegriCloud