blob: 03846c5328af9d7c00209b78a14a094001088f3d [file] [log] [blame]
Vernon Maueryebe8e902018-12-12 09:39:22 -08001/**
2 * Copyright © 2018 Intel Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#include <ipmid/api.hpp>
17#include <ipmid/message.hpp>
18
19#include <gtest/gtest.h>
20
21// TODO: Add testing of Payload response API
22
23TEST(PackBasics, Uint8)
24{
25 ipmi::message::Payload p;
26 uint8_t v = 4;
27 p.pack(v);
28 // check that the number of bytes matches
29 ASSERT_EQ(p.size(), sizeof(v));
30 // check that the bytes were correctly packed (LSB first)
31 std::vector<uint8_t> k = {0x04};
32 ASSERT_EQ(p.raw, k);
33}
34
35TEST(PackBasics, Uint16)
36{
37 ipmi::message::Payload p;
38 uint16_t v = 0x8604;
39 p.pack(v);
40 // check that the number of bytes matches
41 ASSERT_EQ(p.size(), sizeof(v));
42 // check that the bytes were correctly packed (LSB first)
43 std::vector<uint8_t> k = {0x04, 0x86};
44 ASSERT_EQ(p.raw, k);
45}
46
47TEST(PackBasics, Uint32)
48{
49 ipmi::message::Payload p;
50 uint32_t v = 0x02008604;
51 p.pack(v);
52 // check that the number of bytes matches
53 ASSERT_EQ(p.size(), sizeof(v));
54 // check that the bytes were correctly packed (LSB first)
55 std::vector<uint8_t> k = {0x04, 0x86, 0x00, 0x02};
56 ASSERT_EQ(p.raw, k);
57}
58
59TEST(PackBasics, Uint64)
60{
61 ipmi::message::Payload p;
62 uint64_t v = 0x1122334402008604ull;
63 p.pack(v);
64 // check that the number of bytes matches
65 ASSERT_EQ(p.size(), sizeof(v));
66 // check that the bytes were correctly packed (LSB first)
67 std::vector<uint8_t> k = {0x04, 0x86, 0x00, 0x02, 0x44, 0x33, 0x22, 0x11};
68 ASSERT_EQ(p.raw, k);
69}
70
71TEST(PackBasics, Uint24)
72{
73 ipmi::message::Payload p;
74 uint24_t v = 0x112358;
75 p.pack(v);
76 // check that the number of bytes matches
77 ASSERT_EQ(p.size(), types::nrFixedBits<decltype(v)> / CHAR_BIT);
78 // check that the bytes were correctly packed (LSB first)
79 std::vector<uint8_t> k = {0x58, 0x23, 0x11};
80 ASSERT_EQ(p.raw, k);
81}
82
83TEST(PackBasics, Uint3Uint5)
84{
85 // individual bytes are packed low-order-bits first
86 // v1 will occupy [2:0], v2 will occupy [7:3]
87 ipmi::message::Payload p;
88 uint3_t v1 = 0x1;
89 uint5_t v2 = 0x19;
90 p.pack(v1, v2);
91 // check that the number of bytes matches
92 ASSERT_EQ(p.size(), (types::nrFixedBits<decltype(v1)> +
93 types::nrFixedBits<decltype(v2)>) /
94 CHAR_BIT);
95 // check that the bytes were correctly packed (LSB first)
96 std::vector<uint8_t> k = {0xc9};
97 ASSERT_EQ(p.raw, k);
98}
99
100TEST(PackBasics, Boolx8)
101{
102 // individual bytes are packed low-order-bits first
103 // [v8, v7, v6, v5, v4, v3, v2, v1]
104 ipmi::message::Payload p;
105 bool v8 = true, v7 = true, v6 = false, v5 = false;
106 bool v4 = true, v3 = false, v2 = false, v1 = true;
107 p.pack(v1, v2, v3, v4, v5, v6, v7, v8);
108 // check that the number of bytes matches
109 ASSERT_EQ(p.size(), sizeof(uint8_t));
110 // check that the bytes were correctly packed (LSB first)
111 std::vector<uint8_t> k = {0xc9};
112 ASSERT_EQ(p.raw, k);
113}
114
115TEST(PackBasics, Bitset8)
116{
117 // individual bytes are packed low-order-bits first
118 // a bitset for 8 bits fills the full byte
119 ipmi::message::Payload p;
120 std::bitset<8> v(0xc9);
121 p.pack(v);
122 // check that the number of bytes matches
123 ASSERT_EQ(p.size(), v.size() / CHAR_BIT);
124 // check that the bytes were correctly packed (LSB first)
125 std::vector<uint8_t> k = {0xc9};
126 ASSERT_EQ(p.raw, k);
127}
128
129TEST(PackBasics, Bitset3Bitset5)
130{
131 // individual bytes are packed low-order-bits first
132 // v1 will occupy [2:0], v2 will occupy [7:3]
133 ipmi::message::Payload p;
134 std::bitset<3> v1(0x1);
135 std::bitset<5> v2(0x19);
136 p.pack(v1, v2);
137 // check that the number of bytes matches
138 ASSERT_EQ(p.size(), (v1.size() + v2.size()) / CHAR_BIT);
139 // check that the bytes were correctly packed (LSB first)
140 std::vector<uint8_t> k = {0xc9};
141 ASSERT_EQ(p.raw, k);
142}
143
144TEST(PackBasics, Bitset32)
145{
146 // individual bytes are packed low-order-bits first
147 // v1 will occupy 4 bytes, but in LSByte first order
148 // v1[7:0] v1[15:9] v1[23:16] v1[31:24]
149 ipmi::message::Payload p;
150 std::bitset<32> v(0x02008604);
151 p.pack(v);
152 // check that the number of bytes matches
153 ASSERT_EQ(p.size(), v.size() / CHAR_BIT);
154 // check that the bytes were correctly packed (LSB first)
155 std::vector<uint8_t> k = {0x04, 0x86, 0x00, 0x02};
156 ASSERT_EQ(p.raw, k);
157}
158
159TEST(PackBasics, Array4xUint8)
160{
161 // an array of bytes will be output verbatim, low-order element first
162 ipmi::message::Payload p;
163 std::array<uint8_t, 4> v = {{0x02, 0x00, 0x86, 0x04}};
164 p.pack(v);
165 // check that the number of bytes matches
166 ASSERT_EQ(p.size(), v.size() * sizeof(v[0]));
167 // check that the bytes were correctly packed (in byte order)
168 std::vector<uint8_t> k = {0x02, 0x00, 0x86, 0x04};
169 ASSERT_EQ(p.raw, k);
170}
171
172TEST(PackBasics, Array4xUint32)
173{
174 // an array of multi-byte values will be output in order low-order
175 // element first, each multi-byte element in LSByte order
176 // v[0][7:0] v[0][15:9] v[0][23:16] v[0][31:24]
177 // v[1][7:0] v[1][15:9] v[1][23:16] v[1][31:24]
178 // v[2][7:0] v[2][15:9] v[2][23:16] v[2][31:24]
179 // v[3][7:0] v[3][15:9] v[3][23:16] v[3][31:24]
180 ipmi::message::Payload p;
181 std::array<uint32_t, 4> v = {
182 {0x11223344, 0x22446688, 0x33557799, 0x12345678}};
183 p.pack(v);
184 // check that the number of bytes matches
185 ASSERT_EQ(p.size(), v.size() * sizeof(v[0]));
186 // check that the bytes were correctly packed (in byte order)
187 std::vector<uint8_t> k = {0x44, 0x33, 0x22, 0x11, 0x88, 0x66, 0x44, 0x22,
188 0x99, 0x77, 0x55, 0x33, 0x78, 0x56, 0x34, 0x12};
189 ASSERT_EQ(p.raw, k);
190}
191
192TEST(PackBasics, VectorUint32)
193{
194 // a vector of multi-byte values will be output in order low-order
195 // element first, each multi-byte element in LSByte order
196 // v[0][7:0] v[0][15:9] v[0][23:16] v[0][31:24]
197 // v[1][7:0] v[1][15:9] v[1][23:16] v[1][31:24]
198 // v[2][7:0] v[2][15:9] v[2][23:16] v[2][31:24]
199 // v[3][7:0] v[3][15:9] v[3][23:16] v[3][31:24]
200 ipmi::message::Payload p;
201 std::vector<uint32_t> v = {
202 {0x11223344, 0x22446688, 0x33557799, 0x12345678}};
203 p.pack(v);
204 // check that the number of bytes matches
205 ASSERT_EQ(p.size(), v.size() * sizeof(v[0]));
206 // check that the bytes were correctly packed (in byte order)
207 std::vector<uint8_t> k = {0x44, 0x33, 0x22, 0x11, 0x88, 0x66, 0x44, 0x22,
208 0x99, 0x77, 0x55, 0x33, 0x78, 0x56, 0x34, 0x12};
209 ASSERT_EQ(p.raw, k);
210}
211
212TEST(PackBasics, VectorUint8)
213{
214 // a vector of bytes will be output verbatim, low-order element first
215 ipmi::message::Payload p;
216 std::vector<uint8_t> v = {0x02, 0x00, 0x86, 0x04};
217 p.pack(v);
218 // check that the number of bytes matches
219 ASSERT_EQ(p.size(), v.size() * sizeof(v[0]));
220 // check that the bytes were correctly packed (in byte order)
221 std::vector<uint8_t> k = {0x02, 0x00, 0x86, 0x04};
222 ASSERT_EQ(p.raw, k);
223}
224
Vernon Mauerybae91352019-04-03 12:11:08 -0700225TEST(PackBasics, OptionalEmpty)
226{
227 // an optional will only pack if the value is present
228 ipmi::message::Payload p;
229 std::optional<uint32_t> v;
230 p.pack(v);
231 // check that the number of bytes matches
232 ASSERT_EQ(p.size(), 0);
233 // check that the bytes were correctly packed (in byte order)
234 std::vector<uint8_t> k = {};
235 ASSERT_EQ(p.raw, k);
236}
237
238TEST(PackBasics, OptionalContainsValue)
239{
240 // an optional will only pack if the value is present
241 ipmi::message::Payload p;
242 std::optional<uint32_t> v(0x04860002);
243 p.pack(v);
244 // check that the number of bytes matches
245 ASSERT_EQ(p.size(), sizeof(uint32_t));
246 // check that the bytes were correctly packed (in byte order)
247 std::vector<uint8_t> k = {0x02, 0x00, 0x86, 0x04};
248 ASSERT_EQ(p.raw, k);
249}
250
Vernon Maueryebe8e902018-12-12 09:39:22 -0800251TEST(PackAdvanced, Uints)
252{
253 // all elements will be processed in order, with each multi-byte
254 // element being processed LSByte first
255 // v1[7:0] v2[7:0] v2[15:8] v3[7:0] v3[15:8] v3[23:16] v3[31:24]
256 // v4[7:0] v4[15:8] v4[23:16] v4[31:24]
257 // v4[39:25] v4[47:40] v4[55:48] v4[63:56]
258 ipmi::message::Payload p;
259 uint8_t v1 = 0x02;
260 uint16_t v2 = 0x0604;
261 uint32_t v3 = 0x44332211;
262 uint64_t v4 = 0xccbbaa9988776655ull;
263 p.pack(v1, v2, v3, v4);
264 // check that the number of bytes matches
265 ASSERT_EQ(p.size(), sizeof(v1) + sizeof(v2) + sizeof(v3) + sizeof(v4));
266 // check that the bytes were correctly packed (LSB first)
267 std::vector<uint8_t> k = {0x02, 0x04, 0x06, 0x11, 0x22, 0x33, 0x44, 0x55,
268 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc};
269 ASSERT_EQ(p.raw, k);
270}
271
272TEST(PackAdvanced, TupleInts)
273{
274 // all elements will be processed in order, with each multi-byte
275 // element being processed LSByte first
276 // v1[7:0] v2[7:0] v2[15:8] v3[7:0] v3[15:8] v3[23:16] v3[31:24]
277 // v4[7:0] v4[15:8] v4[23:16] v4[31:24]
278 // v4[39:25] v4[47:40] v4[55:48] v4[63:56]
279 ipmi::message::Payload p;
280 uint8_t v1 = 0x02;
281 uint16_t v2 = 0x0604;
282 uint32_t v3 = 0x44332211;
283 uint64_t v4 = 0xccbbaa9988776655ull;
284 auto v = std::make_tuple(v1, v2, v3, v4);
285 p.pack(v);
286 // check that the number of bytes matches
287 ASSERT_EQ(p.size(), sizeof(v1) + sizeof(v2) + sizeof(v3) + sizeof(v4));
288 // check that the bytes were correctly packed (LSB first)
289 std::vector<uint8_t> k = {0x02, 0x04, 0x06, 0x11, 0x22, 0x33, 0x44, 0x55,
290 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc};
291 ASSERT_EQ(p.raw, k);
292}
293
James Feistf2998072019-04-03 11:54:52 -0700294TEST(PackAdvanced, VariantArray)
295{
296 ipmi::message::Payload p;
297 std::variant<std::array<uint8_t, 2>, uint32_t> variant;
298 auto data = std::array<uint8_t, 2>{2, 4};
299 variant = data;
300
301 p.pack(variant);
302 ASSERT_EQ(p.size(), sizeof(data));
303
304 // check that the bytes were correctly packed packed (LSB first)
305 std::vector<uint8_t> k = {2, 4};
306 ASSERT_EQ(p.raw, k);
307}
308
Vernon Maueryebe8e902018-12-12 09:39:22 -0800309TEST(PackAdvanced, BoolsnBitfieldsnFixedIntsOhMy)
310{
311 // each element will be added, filling the low-order bits first
312 // with multi-byte values getting added LSByte first
313 // v1 will occupy k[0][1:0]
314 // v2 will occupy k[0][2]
315 // v3[4:0] will occupy k[0][7:3], v3[6:5] will occupy k[1][1:0]
316 // v4 will occupy k[1][2]
317 // v5 will occupy k[1][7:3]
318 ipmi::message::Payload p;
319 uint2_t v1 = 2; // binary 0b10
320 bool v2 = true; // binary 0b1
321 std::bitset<7> v3(0x73); // binary 0b1110011
322 bool v4 = false; // binary 0b0
323 uint5_t v5 = 27; // binary 0b11011
324 // concat binary: 0b1101101110011110 -> 0xdb9e -> 0x9e 0xdb (LSByte first)
325 p.pack(v1, v2, v3, v4, v5);
326 // check that the number of bytes matches
327 ASSERT_EQ(p.size(), sizeof(uint16_t));
328 // check that the bytes were correctly packed (LSB first)
329 std::vector<uint8_t> k = {0x9e, 0xdb};
330 ASSERT_EQ(p.raw, k);
331}
332
333TEST(PackAdvanced, UnalignedBitPacking)
334{
335 // unaligned multi-byte values will be packed the same as
336 // other bits, effectively building up a large value, low-order
337 // bits first, then outputting a stream of LSByte values
338 // v1 will occupy k[0][1:0]
339 // v2[5:0] will occupy k[0][7:2], v2[7:6] will occupy k[1][1:0]
340 // v3 will occupy k[1][2]
341 // v4[4:0] will occupy k[1][7:3] v4[12:5] will occupy k[2][7:0]
342 // v4[15:13] will occupy k[3][2:0]
343 // v5 will occupy k[3][3]
344 // v6[3:0] will occupy k[3][7:0] v6[11:4] will occupy k[4][7:0]
345 // v6[19:12] will occupy k[5][7:0] v6[27:20] will occupy k[6][7:0]
346 // v6[31:28] will occupy k[7][3:0]
347 // v7 will occupy k[7][7:4]
348 ipmi::message::Payload p;
349 uint2_t v1 = 2; // binary 0b10
350 uint8_t v2 = 0xa5; // binary 0b10100101
351 bool v3 = false; // binary 0b0
352 uint16_t v4 = 0xa55a; // binary 0b1010010101011010
353 bool v5 = true; // binary 0b1
354 uint32_t v6 = 0xdbc3bd3c; // binary 0b11011011110000111011110100111100
355 uint4_t v7 = 9; // binary 0b1001
356 // concat binary:
357 // 0b1001110110111100001110111101001111001101001010101101001010010110
358 // -> 0x9dbc3bd3cd2ad296 -> 0x96 0xd2 0x2a 0xcd 0xd3 0x3b 0xbc 0x9d
359 p.pack(v1, v2, v3, v4, v5, v6, v7);
360 // check that the number of bytes matches
361 ASSERT_EQ(p.size(), sizeof(uint64_t));
362 // check that the bytes were correctly packed (LSB first)
363 std::vector<uint8_t> k = {0x96, 0xd2, 0x2a, 0xcd, 0xd3, 0x3b, 0xbc, 0x9d};
364 ASSERT_EQ(p.raw, k);
365}