xref: /openbmc/phosphor-host-ipmid/test/message/pack.cpp (revision 1318a5ed36cfd41335e687b54db1c17c0dde8f45)
1ebe8e906SVernon Mauery /**
2ebe8e906SVernon Mauery  * Copyright © 2018 Intel Corporation
3ebe8e906SVernon Mauery  *
4ebe8e906SVernon Mauery  * Licensed under the Apache License, Version 2.0 (the "License");
5ebe8e906SVernon Mauery  * you may not use this file except in compliance with the License.
6ebe8e906SVernon Mauery  * You may obtain a copy of the License at
7ebe8e906SVernon Mauery  *
8ebe8e906SVernon Mauery  *     http://www.apache.org/licenses/LICENSE-2.0
9ebe8e906SVernon Mauery  *
10ebe8e906SVernon Mauery  * Unless required by applicable law or agreed to in writing, software
11ebe8e906SVernon Mauery  * distributed under the License is distributed on an "AS IS" BASIS,
12ebe8e906SVernon Mauery  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13ebe8e906SVernon Mauery  * See the License for the specific language governing permissions and
14ebe8e906SVernon Mauery  * limitations under the License.
15ebe8e906SVernon Mauery  */
16ebe8e906SVernon Mauery #include <ipmid/api.hpp>
17ebe8e906SVernon Mauery #include <ipmid/message.hpp>
18ebe8e906SVernon Mauery 
19ebe8e906SVernon Mauery #include <gtest/gtest.h>
20ebe8e906SVernon Mauery 
21ebe8e906SVernon Mauery // TODO: Add testing of Payload response API
22ebe8e906SVernon Mauery 
TEST(PackBasics,Uint8)23ebe8e906SVernon Mauery TEST(PackBasics, Uint8)
24ebe8e906SVernon Mauery {
25ebe8e906SVernon Mauery     ipmi::message::Payload p;
26ebe8e906SVernon Mauery     uint8_t v = 4;
27ebe8e906SVernon Mauery     p.pack(v);
28ebe8e906SVernon Mauery     // check that the number of bytes matches
29ebe8e906SVernon Mauery     ASSERT_EQ(p.size(), sizeof(v));
30ebe8e906SVernon Mauery     // check that the bytes were correctly packed (LSB first)
31997952afSVernon Mauery     ipmi::SecureBuffer k = {0x04};
32ebe8e906SVernon Mauery     ASSERT_EQ(p.raw, k);
33ebe8e906SVernon Mauery }
34ebe8e906SVernon Mauery 
TEST(PackBasics,Uint16)35ebe8e906SVernon Mauery TEST(PackBasics, Uint16)
36ebe8e906SVernon Mauery {
37ebe8e906SVernon Mauery     ipmi::message::Payload p;
38ebe8e906SVernon Mauery     uint16_t v = 0x8604;
39ebe8e906SVernon Mauery     p.pack(v);
40ebe8e906SVernon Mauery     // check that the number of bytes matches
41ebe8e906SVernon Mauery     ASSERT_EQ(p.size(), sizeof(v));
42ebe8e906SVernon Mauery     // check that the bytes were correctly packed (LSB first)
43997952afSVernon Mauery     ipmi::SecureBuffer k = {0x04, 0x86};
44ebe8e906SVernon Mauery     ASSERT_EQ(p.raw, k);
45ebe8e906SVernon Mauery }
46ebe8e906SVernon Mauery 
TEST(PackBasics,Uint32)47ebe8e906SVernon Mauery TEST(PackBasics, Uint32)
48ebe8e906SVernon Mauery {
49ebe8e906SVernon Mauery     ipmi::message::Payload p;
50ebe8e906SVernon Mauery     uint32_t v = 0x02008604;
51ebe8e906SVernon Mauery     p.pack(v);
52ebe8e906SVernon Mauery     // check that the number of bytes matches
53ebe8e906SVernon Mauery     ASSERT_EQ(p.size(), sizeof(v));
54ebe8e906SVernon Mauery     // check that the bytes were correctly packed (LSB first)
55997952afSVernon Mauery     ipmi::SecureBuffer k = {0x04, 0x86, 0x00, 0x02};
56ebe8e906SVernon Mauery     ASSERT_EQ(p.raw, k);
57ebe8e906SVernon Mauery }
58ebe8e906SVernon Mauery 
TEST(PackBasics,Uint64)59ebe8e906SVernon Mauery TEST(PackBasics, Uint64)
60ebe8e906SVernon Mauery {
61ebe8e906SVernon Mauery     ipmi::message::Payload p;
62ebe8e906SVernon Mauery     uint64_t v = 0x1122334402008604ull;
63ebe8e906SVernon Mauery     p.pack(v);
64ebe8e906SVernon Mauery     // check that the number of bytes matches
65ebe8e906SVernon Mauery     ASSERT_EQ(p.size(), sizeof(v));
66ebe8e906SVernon Mauery     // check that the bytes were correctly packed (LSB first)
67997952afSVernon Mauery     ipmi::SecureBuffer k = {0x04, 0x86, 0x00, 0x02, 0x44, 0x33, 0x22, 0x11};
68ebe8e906SVernon Mauery     ASSERT_EQ(p.raw, k);
69ebe8e906SVernon Mauery }
70ebe8e906SVernon Mauery 
TEST(PackBasics,Uint24)71ebe8e906SVernon Mauery TEST(PackBasics, Uint24)
72ebe8e906SVernon Mauery {
73ebe8e906SVernon Mauery     ipmi::message::Payload p;
74ebe8e906SVernon Mauery     uint24_t v = 0x112358;
75ebe8e906SVernon Mauery     p.pack(v);
76ebe8e906SVernon Mauery     // check that the number of bytes matches
77ebe8e906SVernon Mauery     ASSERT_EQ(p.size(), types::nrFixedBits<decltype(v)> / CHAR_BIT);
78ebe8e906SVernon Mauery     // check that the bytes were correctly packed (LSB first)
79997952afSVernon Mauery     ipmi::SecureBuffer k = {0x58, 0x23, 0x11};
80ebe8e906SVernon Mauery     ASSERT_EQ(p.raw, k);
81ebe8e906SVernon Mauery }
82ebe8e906SVernon Mauery 
TEST(PackBasics,Uint3Uint5)83ebe8e906SVernon Mauery TEST(PackBasics, Uint3Uint5)
84ebe8e906SVernon Mauery {
85ebe8e906SVernon Mauery     // individual bytes are packed low-order-bits first
86ebe8e906SVernon Mauery     // v1 will occupy [2:0], v2 will occupy [7:3]
87ebe8e906SVernon Mauery     ipmi::message::Payload p;
88ebe8e906SVernon Mauery     uint3_t v1 = 0x1;
89ebe8e906SVernon Mauery     uint5_t v2 = 0x19;
90ebe8e906SVernon Mauery     p.pack(v1, v2);
91ebe8e906SVernon Mauery     // check that the number of bytes matches
92ebe8e906SVernon Mauery     ASSERT_EQ(p.size(), (types::nrFixedBits<decltype(v1)> +
93ebe8e906SVernon Mauery                          types::nrFixedBits<decltype(v2)>) /
94ebe8e906SVernon Mauery                             CHAR_BIT);
95ebe8e906SVernon Mauery     // check that the bytes were correctly packed (LSB first)
96997952afSVernon Mauery     ipmi::SecureBuffer k = {0xc9};
97ebe8e906SVernon Mauery     ASSERT_EQ(p.raw, k);
98ebe8e906SVernon Mauery }
99ebe8e906SVernon Mauery 
TEST(PackBasics,Boolx8)100ebe8e906SVernon Mauery TEST(PackBasics, Boolx8)
101ebe8e906SVernon Mauery {
102ebe8e906SVernon Mauery     // individual bytes are packed low-order-bits first
103ebe8e906SVernon Mauery     // [v8, v7, v6, v5, v4, v3, v2, v1]
104ebe8e906SVernon Mauery     ipmi::message::Payload p;
105ebe8e906SVernon Mauery     bool v8 = true, v7 = true, v6 = false, v5 = false;
106ebe8e906SVernon Mauery     bool v4 = true, v3 = false, v2 = false, v1 = true;
107ebe8e906SVernon Mauery     p.pack(v1, v2, v3, v4, v5, v6, v7, v8);
108ebe8e906SVernon Mauery     // check that the number of bytes matches
109ebe8e906SVernon Mauery     ASSERT_EQ(p.size(), sizeof(uint8_t));
110ebe8e906SVernon Mauery     // check that the bytes were correctly packed (LSB first)
111997952afSVernon Mauery     ipmi::SecureBuffer k = {0xc9};
112ebe8e906SVernon Mauery     ASSERT_EQ(p.raw, k);
113ebe8e906SVernon Mauery }
114ebe8e906SVernon Mauery 
TEST(PackBasics,Bitset8)115ebe8e906SVernon Mauery TEST(PackBasics, Bitset8)
116ebe8e906SVernon Mauery {
117ebe8e906SVernon Mauery     // individual bytes are packed low-order-bits first
118ebe8e906SVernon Mauery     // a bitset for 8 bits fills the full byte
119ebe8e906SVernon Mauery     ipmi::message::Payload p;
120ebe8e906SVernon Mauery     std::bitset<8> v(0xc9);
121ebe8e906SVernon Mauery     p.pack(v);
122ebe8e906SVernon Mauery     // check that the number of bytes matches
123ebe8e906SVernon Mauery     ASSERT_EQ(p.size(), v.size() / CHAR_BIT);
124ebe8e906SVernon Mauery     // check that the bytes were correctly packed (LSB first)
125997952afSVernon Mauery     ipmi::SecureBuffer k = {0xc9};
126ebe8e906SVernon Mauery     ASSERT_EQ(p.raw, k);
127ebe8e906SVernon Mauery }
128ebe8e906SVernon Mauery 
TEST(PackBasics,Bitset3Bitset5)129ebe8e906SVernon Mauery TEST(PackBasics, Bitset3Bitset5)
130ebe8e906SVernon Mauery {
131ebe8e906SVernon Mauery     // individual bytes are packed low-order-bits first
132ebe8e906SVernon Mauery     // v1 will occupy [2:0], v2 will occupy [7:3]
133ebe8e906SVernon Mauery     ipmi::message::Payload p;
134ebe8e906SVernon Mauery     std::bitset<3> v1(0x1);
135ebe8e906SVernon Mauery     std::bitset<5> v2(0x19);
136ebe8e906SVernon Mauery     p.pack(v1, v2);
137ebe8e906SVernon Mauery     // check that the number of bytes matches
138ebe8e906SVernon Mauery     ASSERT_EQ(p.size(), (v1.size() + v2.size()) / CHAR_BIT);
139ebe8e906SVernon Mauery     // check that the bytes were correctly packed (LSB first)
140997952afSVernon Mauery     ipmi::SecureBuffer k = {0xc9};
141ebe8e906SVernon Mauery     ASSERT_EQ(p.raw, k);
142ebe8e906SVernon Mauery }
143ebe8e906SVernon Mauery 
TEST(PackBasics,Bitset32)144ebe8e906SVernon Mauery TEST(PackBasics, Bitset32)
145ebe8e906SVernon Mauery {
146ebe8e906SVernon Mauery     // individual bytes are packed low-order-bits first
147ebe8e906SVernon Mauery     // v1 will occupy 4 bytes, but in LSByte first order
148ebe8e906SVernon Mauery     // v1[7:0] v1[15:9] v1[23:16] v1[31:24]
149ebe8e906SVernon Mauery     ipmi::message::Payload p;
150ebe8e906SVernon Mauery     std::bitset<32> v(0x02008604);
151ebe8e906SVernon Mauery     p.pack(v);
152ebe8e906SVernon Mauery     // check that the number of bytes matches
153ebe8e906SVernon Mauery     ASSERT_EQ(p.size(), v.size() / CHAR_BIT);
154ebe8e906SVernon Mauery     // check that the bytes were correctly packed (LSB first)
155997952afSVernon Mauery     ipmi::SecureBuffer k = {0x04, 0x86, 0x00, 0x02};
156ebe8e906SVernon Mauery     ASSERT_EQ(p.raw, k);
157ebe8e906SVernon Mauery }
158ebe8e906SVernon Mauery 
TEST(PackBasics,Tuple)159508c4576SVernon Mauery TEST(PackBasics, Tuple)
160508c4576SVernon Mauery {
161508c4576SVernon Mauery     // tuples are the new struct, pack a tuple
162508c4576SVernon Mauery     ipmi::message::Payload p;
163508c4576SVernon Mauery     auto v = std::make_tuple(static_cast<uint16_t>(0x8604), 'A');
164508c4576SVernon Mauery     p.pack(v);
165508c4576SVernon Mauery     // check that the number of bytes matches
166508c4576SVernon Mauery     ASSERT_EQ(p.size(), sizeof(uint16_t) + sizeof(char));
167508c4576SVernon Mauery     // check that the bytes were correctly packed (LSB first)
168997952afSVernon Mauery     ipmi::SecureBuffer k = {0x04, 0x86, 0x41};
169508c4576SVernon Mauery     ASSERT_EQ(p.raw, k);
170508c4576SVernon Mauery }
171508c4576SVernon Mauery 
TEST(PackBasics,Array4xUint8)172ebe8e906SVernon Mauery TEST(PackBasics, Array4xUint8)
173ebe8e906SVernon Mauery {
174ebe8e906SVernon Mauery     // an array of bytes will be output verbatim, low-order element first
175ebe8e906SVernon Mauery     ipmi::message::Payload p;
176ebe8e906SVernon Mauery     std::array<uint8_t, 4> v = {{0x02, 0x00, 0x86, 0x04}};
177ebe8e906SVernon Mauery     p.pack(v);
178ebe8e906SVernon Mauery     // check that the number of bytes matches
179ebe8e906SVernon Mauery     ASSERT_EQ(p.size(), v.size() * sizeof(v[0]));
180ebe8e906SVernon Mauery     // check that the bytes were correctly packed (in byte order)
181997952afSVernon Mauery     ipmi::SecureBuffer k = {0x02, 0x00, 0x86, 0x04};
182ebe8e906SVernon Mauery     ASSERT_EQ(p.raw, k);
183ebe8e906SVernon Mauery }
184ebe8e906SVernon Mauery 
TEST(PackBasics,Array4xUint32)185ebe8e906SVernon Mauery TEST(PackBasics, Array4xUint32)
186ebe8e906SVernon Mauery {
187ebe8e906SVernon Mauery     // an array of multi-byte values will be output in order low-order
188ebe8e906SVernon Mauery     // element first, each multi-byte element in LSByte order
189ebe8e906SVernon Mauery     // v[0][7:0] v[0][15:9] v[0][23:16] v[0][31:24]
190ebe8e906SVernon Mauery     // v[1][7:0] v[1][15:9] v[1][23:16] v[1][31:24]
191ebe8e906SVernon Mauery     // v[2][7:0] v[2][15:9] v[2][23:16] v[2][31:24]
192ebe8e906SVernon Mauery     // v[3][7:0] v[3][15:9] v[3][23:16] v[3][31:24]
193ebe8e906SVernon Mauery     ipmi::message::Payload p;
194ebe8e906SVernon Mauery     std::array<uint32_t, 4> v = {
195ebe8e906SVernon Mauery         {0x11223344, 0x22446688, 0x33557799, 0x12345678}};
196ebe8e906SVernon Mauery     p.pack(v);
197ebe8e906SVernon Mauery     // check that the number of bytes matches
198ebe8e906SVernon Mauery     ASSERT_EQ(p.size(), v.size() * sizeof(v[0]));
199ebe8e906SVernon Mauery     // check that the bytes were correctly packed (in byte order)
200997952afSVernon Mauery     ipmi::SecureBuffer k = {0x44, 0x33, 0x22, 0x11, 0x88, 0x66, 0x44, 0x22,
201ebe8e906SVernon Mauery                             0x99, 0x77, 0x55, 0x33, 0x78, 0x56, 0x34, 0x12};
202ebe8e906SVernon Mauery     ASSERT_EQ(p.raw, k);
203ebe8e906SVernon Mauery }
204ebe8e906SVernon Mauery 
TEST(PackBasics,VectorUint32)205ebe8e906SVernon Mauery TEST(PackBasics, VectorUint32)
206ebe8e906SVernon Mauery {
207ebe8e906SVernon Mauery     // a vector of multi-byte values will be output in order low-order
208ebe8e906SVernon Mauery     // element first, each multi-byte element in LSByte order
209ebe8e906SVernon Mauery     // v[0][7:0] v[0][15:9] v[0][23:16] v[0][31:24]
210ebe8e906SVernon Mauery     // v[1][7:0] v[1][15:9] v[1][23:16] v[1][31:24]
211ebe8e906SVernon Mauery     // v[2][7:0] v[2][15:9] v[2][23:16] v[2][31:24]
212ebe8e906SVernon Mauery     // v[3][7:0] v[3][15:9] v[3][23:16] v[3][31:24]
213ebe8e906SVernon Mauery     ipmi::message::Payload p;
214ebe8e906SVernon Mauery     std::vector<uint32_t> v = {
215ebe8e906SVernon Mauery         {0x11223344, 0x22446688, 0x33557799, 0x12345678}};
216ebe8e906SVernon Mauery     p.pack(v);
217ebe8e906SVernon Mauery     // check that the number of bytes matches
218ebe8e906SVernon Mauery     ASSERT_EQ(p.size(), v.size() * sizeof(v[0]));
219ebe8e906SVernon Mauery     // check that the bytes were correctly packed (in byte order)
220997952afSVernon Mauery     ipmi::SecureBuffer k = {0x44, 0x33, 0x22, 0x11, 0x88, 0x66, 0x44, 0x22,
221ebe8e906SVernon Mauery                             0x99, 0x77, 0x55, 0x33, 0x78, 0x56, 0x34, 0x12};
222ebe8e906SVernon Mauery     ASSERT_EQ(p.raw, k);
223ebe8e906SVernon Mauery }
224ebe8e906SVernon Mauery 
TEST(PackBasics,VectorUint8)225ebe8e906SVernon Mauery TEST(PackBasics, VectorUint8)
226ebe8e906SVernon Mauery {
227ebe8e906SVernon Mauery     // a vector of bytes will be output verbatim, low-order element first
228ebe8e906SVernon Mauery     ipmi::message::Payload p;
229ebe8e906SVernon Mauery     std::vector<uint8_t> v = {0x02, 0x00, 0x86, 0x04};
230ebe8e906SVernon Mauery     p.pack(v);
231ebe8e906SVernon Mauery     // check that the number of bytes matches
232ebe8e906SVernon Mauery     ASSERT_EQ(p.size(), v.size() * sizeof(v[0]));
233ebe8e906SVernon Mauery     // check that the bytes were correctly packed (in byte order)
234997952afSVernon Mauery     ipmi::SecureBuffer k = {0x02, 0x00, 0x86, 0x04};
235ebe8e906SVernon Mauery     ASSERT_EQ(p.raw, k);
236ebe8e906SVernon Mauery }
237ebe8e906SVernon Mauery 
TEST(PackBasics,VectorUnaligned)238906e0f80SWilliam A. Kennington III TEST(PackBasics, VectorUnaligned)
239906e0f80SWilliam A. Kennington III {
240906e0f80SWilliam A. Kennington III     ipmi::message::Payload p;
241906e0f80SWilliam A. Kennington III     EXPECT_EQ(p.pack(true, std::vector<uint8_t>{1}), 1);
242997952afSVernon Mauery     EXPECT_EQ(p.raw, ipmi::SecureBuffer{0b1});
243906e0f80SWilliam A. Kennington III }
244906e0f80SWilliam A. Kennington III 
TEST(PackBasics,StringView)245e2aec26cSWilliam A. Kennington III TEST(PackBasics, StringView)
246e2aec26cSWilliam A. Kennington III {
247e2aec26cSWilliam A. Kennington III     ipmi::message::Payload p;
248e2aec26cSWilliam A. Kennington III     EXPECT_EQ(p.pack(std::string_view{"\x24\x30\x11"}), 0);
249997952afSVernon Mauery     EXPECT_EQ(p.raw, ipmi::SecureBuffer({0x24, 0x30, 0x11}));
250e2aec26cSWilliam A. Kennington III }
251e2aec26cSWilliam A. Kennington III 
TEST(PackBasics,StringViewUnaligned)252e2aec26cSWilliam A. Kennington III TEST(PackBasics, StringViewUnaligned)
253e2aec26cSWilliam A. Kennington III {
254e2aec26cSWilliam A. Kennington III     ipmi::message::Payload p;
255e2aec26cSWilliam A. Kennington III     EXPECT_EQ(p.pack(true, std::string_view{"abc"}), 1);
256997952afSVernon Mauery     EXPECT_EQ(p.raw, ipmi::SecureBuffer({0b1}));
257e2aec26cSWilliam A. Kennington III }
258e2aec26cSWilliam A. Kennington III 
TEST(PackBasics,OptionalEmpty)259bae91350SVernon Mauery TEST(PackBasics, OptionalEmpty)
260bae91350SVernon Mauery {
261bae91350SVernon Mauery     // an optional will only pack if the value is present
262bae91350SVernon Mauery     ipmi::message::Payload p;
263bae91350SVernon Mauery     std::optional<uint32_t> v;
264bae91350SVernon Mauery     p.pack(v);
265bae91350SVernon Mauery     // check that the number of bytes matches
266bae91350SVernon Mauery     ASSERT_EQ(p.size(), 0);
267bae91350SVernon Mauery     // check that the bytes were correctly packed (in byte order)
268997952afSVernon Mauery     ipmi::SecureBuffer k = {};
269bae91350SVernon Mauery     ASSERT_EQ(p.raw, k);
270bae91350SVernon Mauery }
271bae91350SVernon Mauery 
TEST(PackBasics,OptionalContainsValue)272bae91350SVernon Mauery TEST(PackBasics, OptionalContainsValue)
273bae91350SVernon Mauery {
274bae91350SVernon Mauery     // an optional will only pack if the value is present
275bae91350SVernon Mauery     ipmi::message::Payload p;
276bae91350SVernon Mauery     std::optional<uint32_t> v(0x04860002);
277bae91350SVernon Mauery     p.pack(v);
278bae91350SVernon Mauery     // check that the number of bytes matches
279bae91350SVernon Mauery     ASSERT_EQ(p.size(), sizeof(uint32_t));
280bae91350SVernon Mauery     // check that the bytes were correctly packed (in byte order)
281997952afSVernon Mauery     ipmi::SecureBuffer k = {0x02, 0x00, 0x86, 0x04};
282bae91350SVernon Mauery     ASSERT_EQ(p.raw, k);
283bae91350SVernon Mauery }
284bae91350SVernon Mauery 
TEST(PackBasics,Payload)285e15e53ebSWilliam A. Kennington III TEST(PackBasics, Payload)
286e15e53ebSWilliam A. Kennington III {
287e15e53ebSWilliam A. Kennington III     ipmi::message::Payload p;
288e15e53ebSWilliam A. Kennington III     EXPECT_EQ(p.pack(true), 0);
289e15e53ebSWilliam A. Kennington III     EXPECT_EQ(p.pack(ipmi::message::Payload({0x24, 0x30})), 0);
290997952afSVernon Mauery     EXPECT_EQ(p.raw, ipmi::SecureBuffer({0b1, 0x24, 0x30}));
291e15e53ebSWilliam A. Kennington III }
292e15e53ebSWilliam A. Kennington III 
TEST(PackBasics,PayloadUnaligned)293e15e53ebSWilliam A. Kennington III TEST(PackBasics, PayloadUnaligned)
294e15e53ebSWilliam A. Kennington III {
295e15e53ebSWilliam A. Kennington III     ipmi::message::Payload p;
296e15e53ebSWilliam A. Kennington III     EXPECT_EQ(p.pack(true, ipmi::message::Payload({0x24})), 1);
297997952afSVernon Mauery     EXPECT_EQ(p.raw, ipmi::SecureBuffer({0b1}));
298e15e53ebSWilliam A. Kennington III }
299e15e53ebSWilliam A. Kennington III 
TEST(PackBasics,PayloadOtherUnaligned)300e15e53ebSWilliam A. Kennington III TEST(PackBasics, PayloadOtherUnaligned)
301e15e53ebSWilliam A. Kennington III {
302e15e53ebSWilliam A. Kennington III     ipmi::message::Payload p, q;
303e15e53ebSWilliam A. Kennington III     q.appendBits(1, 1);
304e15e53ebSWilliam A. Kennington III     EXPECT_EQ(p.pack(true), 0);
305e15e53ebSWilliam A. Kennington III     EXPECT_EQ(p.pack(q), 1);
306997952afSVernon Mauery     EXPECT_EQ(p.raw, ipmi::SecureBuffer({0b1}));
307e15e53ebSWilliam A. Kennington III }
308e15e53ebSWilliam A. Kennington III 
TEST(PackBasics,PrependPayload)30992476a84SWilliam A. Kennington III TEST(PackBasics, PrependPayload)
31092476a84SWilliam A. Kennington III {
31192476a84SWilliam A. Kennington III     ipmi::message::Payload p;
31292476a84SWilliam A. Kennington III     EXPECT_EQ(p.pack(true), 0);
31392476a84SWilliam A. Kennington III     EXPECT_EQ(p.prepend(ipmi::message::Payload({0x24, 0x30})), 0);
314997952afSVernon Mauery     EXPECT_EQ(p.raw, ipmi::SecureBuffer({0x24, 0x30, 0b1}));
31592476a84SWilliam A. Kennington III }
31692476a84SWilliam A. Kennington III 
TEST(PackBasics,PrependPayloadUnaligned)31792476a84SWilliam A. Kennington III TEST(PackBasics, PrependPayloadUnaligned)
31892476a84SWilliam A. Kennington III {
31992476a84SWilliam A. Kennington III     ipmi::message::Payload p;
32092476a84SWilliam A. Kennington III     p.appendBits(1, 1);
32192476a84SWilliam A. Kennington III     EXPECT_EQ(p.prepend(ipmi::message::Payload({0x24})), 1);
32292476a84SWilliam A. Kennington III     p.drain();
323997952afSVernon Mauery     EXPECT_EQ(p.raw, ipmi::SecureBuffer({0b1}));
32492476a84SWilliam A. Kennington III }
32592476a84SWilliam A. Kennington III 
TEST(PackBasics,PrependPayloadOtherUnaligned)32692476a84SWilliam A. Kennington III TEST(PackBasics, PrependPayloadOtherUnaligned)
32792476a84SWilliam A. Kennington III {
32892476a84SWilliam A. Kennington III     ipmi::message::Payload p, q;
32992476a84SWilliam A. Kennington III     q.appendBits(1, 1);
33092476a84SWilliam A. Kennington III     EXPECT_EQ(p.pack(true), 0);
33192476a84SWilliam A. Kennington III     EXPECT_EQ(p.prepend(q), 1);
332997952afSVernon Mauery     EXPECT_EQ(p.raw, ipmi::SecureBuffer({0b1}));
33392476a84SWilliam A. Kennington III }
33492476a84SWilliam A. Kennington III 
TEST(PackAdvanced,Uints)335ebe8e906SVernon Mauery TEST(PackAdvanced, Uints)
336ebe8e906SVernon Mauery {
337ebe8e906SVernon Mauery     // all elements will be processed in order, with each multi-byte
338ebe8e906SVernon Mauery     // element being processed LSByte first
339ebe8e906SVernon Mauery     // v1[7:0] v2[7:0] v2[15:8] v3[7:0] v3[15:8] v3[23:16] v3[31:24]
340ebe8e906SVernon Mauery     // v4[7:0] v4[15:8] v4[23:16] v4[31:24]
341ebe8e906SVernon Mauery     // v4[39:25] v4[47:40] v4[55:48] v4[63:56]
342ebe8e906SVernon Mauery     ipmi::message::Payload p;
343ebe8e906SVernon Mauery     uint8_t v1 = 0x02;
344ebe8e906SVernon Mauery     uint16_t v2 = 0x0604;
345ebe8e906SVernon Mauery     uint32_t v3 = 0x44332211;
346ebe8e906SVernon Mauery     uint64_t v4 = 0xccbbaa9988776655ull;
347ebe8e906SVernon Mauery     p.pack(v1, v2, v3, v4);
348ebe8e906SVernon Mauery     // check that the number of bytes matches
349ebe8e906SVernon Mauery     ASSERT_EQ(p.size(), sizeof(v1) + sizeof(v2) + sizeof(v3) + sizeof(v4));
350ebe8e906SVernon Mauery     // check that the bytes were correctly packed (LSB first)
351997952afSVernon Mauery     ipmi::SecureBuffer k = {0x02, 0x04, 0x06, 0x11, 0x22, 0x33, 0x44, 0x55,
352ebe8e906SVernon Mauery                             0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc};
353ebe8e906SVernon Mauery     ASSERT_EQ(p.raw, k);
354ebe8e906SVernon Mauery }
355ebe8e906SVernon Mauery 
TEST(PackAdvanced,TupleInts)356ebe8e906SVernon Mauery TEST(PackAdvanced, TupleInts)
357ebe8e906SVernon Mauery {
358ebe8e906SVernon Mauery     // all elements will be processed in order, with each multi-byte
359ebe8e906SVernon Mauery     // element being processed LSByte first
360ebe8e906SVernon Mauery     // v1[7:0] v2[7:0] v2[15:8] v3[7:0] v3[15:8] v3[23:16] v3[31:24]
361ebe8e906SVernon Mauery     // v4[7:0] v4[15:8] v4[23:16] v4[31:24]
362ebe8e906SVernon Mauery     // v4[39:25] v4[47:40] v4[55:48] v4[63:56]
363ebe8e906SVernon Mauery     ipmi::message::Payload p;
364ebe8e906SVernon Mauery     uint8_t v1 = 0x02;
365ebe8e906SVernon Mauery     uint16_t v2 = 0x0604;
366ebe8e906SVernon Mauery     uint32_t v3 = 0x44332211;
367ebe8e906SVernon Mauery     uint64_t v4 = 0xccbbaa9988776655ull;
368ebe8e906SVernon Mauery     auto v = std::make_tuple(v1, v2, v3, v4);
369ebe8e906SVernon Mauery     p.pack(v);
370ebe8e906SVernon Mauery     // check that the number of bytes matches
371ebe8e906SVernon Mauery     ASSERT_EQ(p.size(), sizeof(v1) + sizeof(v2) + sizeof(v3) + sizeof(v4));
372ebe8e906SVernon Mauery     // check that the bytes were correctly packed (LSB first)
373997952afSVernon Mauery     ipmi::SecureBuffer k = {0x02, 0x04, 0x06, 0x11, 0x22, 0x33, 0x44, 0x55,
374ebe8e906SVernon Mauery                             0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc};
375ebe8e906SVernon Mauery     ASSERT_EQ(p.raw, k);
376ebe8e906SVernon Mauery }
377ebe8e906SVernon Mauery 
TEST(PackAdvanced,VariantArray)378f299807fSJames Feist TEST(PackAdvanced, VariantArray)
379f299807fSJames Feist {
380f299807fSJames Feist     ipmi::message::Payload p;
381f299807fSJames Feist     std::variant<std::array<uint8_t, 2>, uint32_t> variant;
382f299807fSJames Feist     auto data = std::array<uint8_t, 2>{2, 4};
383f299807fSJames Feist     variant = data;
384f299807fSJames Feist 
385f299807fSJames Feist     p.pack(variant);
386f299807fSJames Feist     ASSERT_EQ(p.size(), sizeof(data));
387f299807fSJames Feist 
388f299807fSJames Feist     // check that the bytes were correctly packed packed (LSB first)
389997952afSVernon Mauery     ipmi::SecureBuffer k = {2, 4};
390f299807fSJames Feist     ASSERT_EQ(p.raw, k);
391f299807fSJames Feist }
392f299807fSJames Feist 
TEST(PackAdvanced,BoolsnBitfieldsnFixedIntsOhMy)393ebe8e906SVernon Mauery TEST(PackAdvanced, BoolsnBitfieldsnFixedIntsOhMy)
394ebe8e906SVernon Mauery {
395ebe8e906SVernon Mauery     // each element will be added, filling the low-order bits first
396ebe8e906SVernon Mauery     // with multi-byte values getting added LSByte first
397ebe8e906SVernon Mauery     // v1 will occupy k[0][1:0]
398ebe8e906SVernon Mauery     // v2 will occupy k[0][2]
399ebe8e906SVernon Mauery     // v3[4:0] will occupy k[0][7:3], v3[6:5] will occupy k[1][1:0]
400ebe8e906SVernon Mauery     // v4 will occupy k[1][2]
401ebe8e906SVernon Mauery     // v5 will occupy k[1][7:3]
402ebe8e906SVernon Mauery     ipmi::message::Payload p;
403ebe8e906SVernon Mauery     uint2_t v1 = 2;          // binary 0b10
404ebe8e906SVernon Mauery     bool v2 = true;          // binary 0b1
405ebe8e906SVernon Mauery     std::bitset<7> v3(0x73); // binary 0b1110011
406ebe8e906SVernon Mauery     bool v4 = false;         // binary 0b0
407ebe8e906SVernon Mauery     uint5_t v5 = 27;         // binary 0b11011
408ebe8e906SVernon Mauery     // concat binary: 0b1101101110011110 -> 0xdb9e -> 0x9e 0xdb (LSByte first)
409ebe8e906SVernon Mauery     p.pack(v1, v2, v3, v4, v5);
410ebe8e906SVernon Mauery     // check that the number of bytes matches
411ebe8e906SVernon Mauery     ASSERT_EQ(p.size(), sizeof(uint16_t));
412ebe8e906SVernon Mauery     // check that the bytes were correctly packed (LSB first)
413997952afSVernon Mauery     ipmi::SecureBuffer k = {0x9e, 0xdb};
414ebe8e906SVernon Mauery     ASSERT_EQ(p.raw, k);
415ebe8e906SVernon Mauery }
416ebe8e906SVernon Mauery 
TEST(PackAdvanced,UnalignedBitPacking)417ebe8e906SVernon Mauery TEST(PackAdvanced, UnalignedBitPacking)
418ebe8e906SVernon Mauery {
419ebe8e906SVernon Mauery     // unaligned multi-byte values will be packed the same as
420ebe8e906SVernon Mauery     // other bits, effectively building up a large value, low-order
421ebe8e906SVernon Mauery     // bits first, then outputting a stream of LSByte values
422ebe8e906SVernon Mauery     // v1 will occupy k[0][1:0]
423ebe8e906SVernon Mauery     // v2[5:0] will occupy k[0][7:2], v2[7:6] will occupy k[1][1:0]
424ebe8e906SVernon Mauery     // v3 will occupy k[1][2]
425ebe8e906SVernon Mauery     // v4[4:0] will occupy k[1][7:3] v4[12:5] will occupy k[2][7:0]
426ebe8e906SVernon Mauery     // v4[15:13] will occupy k[3][2:0]
427ebe8e906SVernon Mauery     // v5 will occupy k[3][3]
428ebe8e906SVernon Mauery     // v6[3:0] will occupy k[3][7:0] v6[11:4] will occupy k[4][7:0]
429ebe8e906SVernon Mauery     // v6[19:12] will occupy k[5][7:0] v6[27:20] will occupy k[6][7:0]
430ebe8e906SVernon Mauery     // v6[31:28] will occupy k[7][3:0]
431ebe8e906SVernon Mauery     // v7 will occupy k[7][7:4]
432ebe8e906SVernon Mauery     ipmi::message::Payload p;
433ebe8e906SVernon Mauery     uint2_t v1 = 2;           // binary 0b10
434ebe8e906SVernon Mauery     uint8_t v2 = 0xa5;        // binary 0b10100101
435ebe8e906SVernon Mauery     bool v3 = false;          // binary 0b0
436ebe8e906SVernon Mauery     uint16_t v4 = 0xa55a;     // binary 0b1010010101011010
437ebe8e906SVernon Mauery     bool v5 = true;           // binary 0b1
438ebe8e906SVernon Mauery     uint32_t v6 = 0xdbc3bd3c; // binary 0b11011011110000111011110100111100
439ebe8e906SVernon Mauery     uint4_t v7 = 9;           // binary 0b1001
440ebe8e906SVernon Mauery     // concat binary:
441ebe8e906SVernon Mauery     //   0b1001110110111100001110111101001111001101001010101101001010010110
442ebe8e906SVernon Mauery     //   -> 0x9dbc3bd3cd2ad296 -> 0x96 0xd2 0x2a 0xcd 0xd3 0x3b 0xbc 0x9d
443ebe8e906SVernon Mauery     p.pack(v1, v2, v3, v4, v5, v6, v7);
444ebe8e906SVernon Mauery     // check that the number of bytes matches
445ebe8e906SVernon Mauery     ASSERT_EQ(p.size(), sizeof(uint64_t));
446ebe8e906SVernon Mauery     // check that the bytes were correctly packed (LSB first)
447997952afSVernon Mauery     ipmi::SecureBuffer k = {0x96, 0xd2, 0x2a, 0xcd, 0xd3, 0x3b, 0xbc, 0x9d};
448ebe8e906SVernon Mauery     ASSERT_EQ(p.raw, k);
449ebe8e906SVernon Mauery }
450508c4576SVernon Mauery 
TEST(PackAdvanced,ComplexOptionalTuple)451508c4576SVernon Mauery TEST(PackAdvanced, ComplexOptionalTuple)
452508c4576SVernon Mauery {
453508c4576SVernon Mauery     constexpr size_t macSize = 6;
454508c4576SVernon Mauery     // inspired from a real-world case of Get Session Info
455508c4576SVernon Mauery     constexpr uint8_t handle = 0x23;       // handle for active session
456508c4576SVernon Mauery     constexpr uint8_t maxSessions = 15;    // number of possible active sessions
457508c4576SVernon Mauery     constexpr uint8_t currentSessions = 4; // number of current active sessions
458508c4576SVernon Mauery     std::optional<                         // only returned for active session
459508c4576SVernon Mauery         std::tuple<uint8_t,                // user ID
460508c4576SVernon Mauery                    uint8_t,                // privilege
461508c4576SVernon Mauery                    uint4_t,                // channel number
462508c4576SVernon Mauery                    uint4_t                 // protocol (RMCP+)
463508c4576SVernon Mauery                    >>
464508c4576SVernon Mauery         activeSession;
465508c4576SVernon Mauery     std::optional<           // only returned for channel type LAN
466508c4576SVernon Mauery         std::tuple<uint32_t, // IPv4 address
467508c4576SVernon Mauery                    std::array<uint8_t, macSize>, // MAC address
468508c4576SVernon Mauery                    uint16_t                      // port
469508c4576SVernon Mauery                    >>
470508c4576SVernon Mauery         lanSession;
471508c4576SVernon Mauery 
472508c4576SVernon Mauery     constexpr uint8_t userID = 7;
473508c4576SVernon Mauery     constexpr uint8_t priv = 4;
474508c4576SVernon Mauery     constexpr uint4_t channel = 2;
475508c4576SVernon Mauery     constexpr uint4_t protocol = 1;
476508c4576SVernon Mauery     activeSession.emplace(userID, priv, channel, protocol);
477508c4576SVernon Mauery     constexpr std::array<uint8_t, macSize> macAddr{0};
478508c4576SVernon Mauery     lanSession.emplace(0x0a010105, macAddr, 55327);
479508c4576SVernon Mauery 
480508c4576SVernon Mauery     ipmi::message::Payload p;
481508c4576SVernon Mauery     p.pack(handle, maxSessions, currentSessions, activeSession, lanSession);
482*1318a5edSPatrick Williams     ASSERT_EQ(p.size(),
483*1318a5edSPatrick Williams               sizeof(handle) + sizeof(maxSessions) + sizeof(currentSessions) +
484*1318a5edSPatrick Williams                   3 * sizeof(uint8_t) + sizeof(uint32_t) +
485*1318a5edSPatrick Williams                   sizeof(uint8_t) * macSize + sizeof(uint16_t));
486*1318a5edSPatrick Williams     uint8_t protocol_channel =
487*1318a5edSPatrick Williams         (static_cast<uint8_t>(protocol) << 4) | static_cast<uint8_t>(channel);
488*1318a5edSPatrick Williams     ipmi::SecureBuffer k = {
489*1318a5edSPatrick Williams         handle, maxSessions, currentSessions, userID, priv, protocol_channel,
490508c4576SVernon Mauery         // ip addr
491508c4576SVernon Mauery         0x05, 0x01, 0x01, 0x0a,
492508c4576SVernon Mauery         // mac addr
493508c4576SVernon Mauery         0, 0, 0, 0, 0, 0,
494508c4576SVernon Mauery         // port
495508c4576SVernon Mauery         0x1f, 0xd8};
496508c4576SVernon Mauery     ASSERT_EQ(p.raw, k);
497508c4576SVernon Mauery }
498