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 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) 31ebe8e906SVernon Mauery std::vector<uint8_t> k = {0x04}; 32ebe8e906SVernon Mauery ASSERT_EQ(p.raw, k); 33ebe8e906SVernon Mauery } 34ebe8e906SVernon Mauery 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) 43ebe8e906SVernon Mauery std::vector<uint8_t> k = {0x04, 0x86}; 44ebe8e906SVernon Mauery ASSERT_EQ(p.raw, k); 45ebe8e906SVernon Mauery } 46ebe8e906SVernon Mauery 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) 55ebe8e906SVernon Mauery std::vector<uint8_t> k = {0x04, 0x86, 0x00, 0x02}; 56ebe8e906SVernon Mauery ASSERT_EQ(p.raw, k); 57ebe8e906SVernon Mauery } 58ebe8e906SVernon Mauery 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) 67ebe8e906SVernon Mauery std::vector<uint8_t> k = {0x04, 0x86, 0x00, 0x02, 0x44, 0x33, 0x22, 0x11}; 68ebe8e906SVernon Mauery ASSERT_EQ(p.raw, k); 69ebe8e906SVernon Mauery } 70ebe8e906SVernon Mauery 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) 79ebe8e906SVernon Mauery std::vector<uint8_t> k = {0x58, 0x23, 0x11}; 80ebe8e906SVernon Mauery ASSERT_EQ(p.raw, k); 81ebe8e906SVernon Mauery } 82ebe8e906SVernon Mauery 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) 96ebe8e906SVernon Mauery std::vector<uint8_t> k = {0xc9}; 97ebe8e906SVernon Mauery ASSERT_EQ(p.raw, k); 98ebe8e906SVernon Mauery } 99ebe8e906SVernon Mauery 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) 111ebe8e906SVernon Mauery std::vector<uint8_t> k = {0xc9}; 112ebe8e906SVernon Mauery ASSERT_EQ(p.raw, k); 113ebe8e906SVernon Mauery } 114ebe8e906SVernon Mauery 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) 125ebe8e906SVernon Mauery std::vector<uint8_t> k = {0xc9}; 126ebe8e906SVernon Mauery ASSERT_EQ(p.raw, k); 127ebe8e906SVernon Mauery } 128ebe8e906SVernon Mauery 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) 140ebe8e906SVernon Mauery std::vector<uint8_t> k = {0xc9}; 141ebe8e906SVernon Mauery ASSERT_EQ(p.raw, k); 142ebe8e906SVernon Mauery } 143ebe8e906SVernon Mauery 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) 155ebe8e906SVernon Mauery std::vector<uint8_t> k = {0x04, 0x86, 0x00, 0x02}; 156ebe8e906SVernon Mauery ASSERT_EQ(p.raw, k); 157ebe8e906SVernon Mauery } 158ebe8e906SVernon Mauery 159*508c4576SVernon Mauery TEST(PackBasics, Tuple) 160*508c4576SVernon Mauery { 161*508c4576SVernon Mauery // tuples are the new struct, pack a tuple 162*508c4576SVernon Mauery ipmi::message::Payload p; 163*508c4576SVernon Mauery auto v = std::make_tuple(static_cast<uint16_t>(0x8604), 'A'); 164*508c4576SVernon Mauery p.pack(v); 165*508c4576SVernon Mauery // check that the number of bytes matches 166*508c4576SVernon Mauery ASSERT_EQ(p.size(), sizeof(uint16_t) + sizeof(char)); 167*508c4576SVernon Mauery // check that the bytes were correctly packed (LSB first) 168*508c4576SVernon Mauery std::vector<uint8_t> k = {0x04, 0x86, 0x41}; 169*508c4576SVernon Mauery ASSERT_EQ(p.raw, k); 170*508c4576SVernon Mauery } 171*508c4576SVernon Mauery 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) 181ebe8e906SVernon Mauery std::vector<uint8_t> k = {0x02, 0x00, 0x86, 0x04}; 182ebe8e906SVernon Mauery ASSERT_EQ(p.raw, k); 183ebe8e906SVernon Mauery } 184ebe8e906SVernon Mauery 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) 200ebe8e906SVernon Mauery std::vector<uint8_t> 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 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) 220ebe8e906SVernon Mauery std::vector<uint8_t> 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 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) 234ebe8e906SVernon Mauery std::vector<uint8_t> k = {0x02, 0x00, 0x86, 0x04}; 235ebe8e906SVernon Mauery ASSERT_EQ(p.raw, k); 236ebe8e906SVernon Mauery } 237ebe8e906SVernon Mauery 238bae91350SVernon Mauery TEST(PackBasics, OptionalEmpty) 239bae91350SVernon Mauery { 240bae91350SVernon Mauery // an optional will only pack if the value is present 241bae91350SVernon Mauery ipmi::message::Payload p; 242bae91350SVernon Mauery std::optional<uint32_t> v; 243bae91350SVernon Mauery p.pack(v); 244bae91350SVernon Mauery // check that the number of bytes matches 245bae91350SVernon Mauery ASSERT_EQ(p.size(), 0); 246bae91350SVernon Mauery // check that the bytes were correctly packed (in byte order) 247bae91350SVernon Mauery std::vector<uint8_t> k = {}; 248bae91350SVernon Mauery ASSERT_EQ(p.raw, k); 249bae91350SVernon Mauery } 250bae91350SVernon Mauery 251bae91350SVernon Mauery TEST(PackBasics, OptionalContainsValue) 252bae91350SVernon Mauery { 253bae91350SVernon Mauery // an optional will only pack if the value is present 254bae91350SVernon Mauery ipmi::message::Payload p; 255bae91350SVernon Mauery std::optional<uint32_t> v(0x04860002); 256bae91350SVernon Mauery p.pack(v); 257bae91350SVernon Mauery // check that the number of bytes matches 258bae91350SVernon Mauery ASSERT_EQ(p.size(), sizeof(uint32_t)); 259bae91350SVernon Mauery // check that the bytes were correctly packed (in byte order) 260bae91350SVernon Mauery std::vector<uint8_t> k = {0x02, 0x00, 0x86, 0x04}; 261bae91350SVernon Mauery ASSERT_EQ(p.raw, k); 262bae91350SVernon Mauery } 263bae91350SVernon Mauery 264ebe8e906SVernon Mauery TEST(PackAdvanced, Uints) 265ebe8e906SVernon Mauery { 266ebe8e906SVernon Mauery // all elements will be processed in order, with each multi-byte 267ebe8e906SVernon Mauery // element being processed LSByte first 268ebe8e906SVernon Mauery // v1[7:0] v2[7:0] v2[15:8] v3[7:0] v3[15:8] v3[23:16] v3[31:24] 269ebe8e906SVernon Mauery // v4[7:0] v4[15:8] v4[23:16] v4[31:24] 270ebe8e906SVernon Mauery // v4[39:25] v4[47:40] v4[55:48] v4[63:56] 271ebe8e906SVernon Mauery ipmi::message::Payload p; 272ebe8e906SVernon Mauery uint8_t v1 = 0x02; 273ebe8e906SVernon Mauery uint16_t v2 = 0x0604; 274ebe8e906SVernon Mauery uint32_t v3 = 0x44332211; 275ebe8e906SVernon Mauery uint64_t v4 = 0xccbbaa9988776655ull; 276ebe8e906SVernon Mauery p.pack(v1, v2, v3, v4); 277ebe8e906SVernon Mauery // check that the number of bytes matches 278ebe8e906SVernon Mauery ASSERT_EQ(p.size(), sizeof(v1) + sizeof(v2) + sizeof(v3) + sizeof(v4)); 279ebe8e906SVernon Mauery // check that the bytes were correctly packed (LSB first) 280ebe8e906SVernon Mauery std::vector<uint8_t> k = {0x02, 0x04, 0x06, 0x11, 0x22, 0x33, 0x44, 0x55, 281ebe8e906SVernon Mauery 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc}; 282ebe8e906SVernon Mauery ASSERT_EQ(p.raw, k); 283ebe8e906SVernon Mauery } 284ebe8e906SVernon Mauery 285ebe8e906SVernon Mauery TEST(PackAdvanced, TupleInts) 286ebe8e906SVernon Mauery { 287ebe8e906SVernon Mauery // all elements will be processed in order, with each multi-byte 288ebe8e906SVernon Mauery // element being processed LSByte first 289ebe8e906SVernon Mauery // v1[7:0] v2[7:0] v2[15:8] v3[7:0] v3[15:8] v3[23:16] v3[31:24] 290ebe8e906SVernon Mauery // v4[7:0] v4[15:8] v4[23:16] v4[31:24] 291ebe8e906SVernon Mauery // v4[39:25] v4[47:40] v4[55:48] v4[63:56] 292ebe8e906SVernon Mauery ipmi::message::Payload p; 293ebe8e906SVernon Mauery uint8_t v1 = 0x02; 294ebe8e906SVernon Mauery uint16_t v2 = 0x0604; 295ebe8e906SVernon Mauery uint32_t v3 = 0x44332211; 296ebe8e906SVernon Mauery uint64_t v4 = 0xccbbaa9988776655ull; 297ebe8e906SVernon Mauery auto v = std::make_tuple(v1, v2, v3, v4); 298ebe8e906SVernon Mauery p.pack(v); 299ebe8e906SVernon Mauery // check that the number of bytes matches 300ebe8e906SVernon Mauery ASSERT_EQ(p.size(), sizeof(v1) + sizeof(v2) + sizeof(v3) + sizeof(v4)); 301ebe8e906SVernon Mauery // check that the bytes were correctly packed (LSB first) 302ebe8e906SVernon Mauery std::vector<uint8_t> k = {0x02, 0x04, 0x06, 0x11, 0x22, 0x33, 0x44, 0x55, 303ebe8e906SVernon Mauery 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc}; 304ebe8e906SVernon Mauery ASSERT_EQ(p.raw, k); 305ebe8e906SVernon Mauery } 306ebe8e906SVernon Mauery 307f299807fSJames Feist TEST(PackAdvanced, VariantArray) 308f299807fSJames Feist { 309f299807fSJames Feist ipmi::message::Payload p; 310f299807fSJames Feist std::variant<std::array<uint8_t, 2>, uint32_t> variant; 311f299807fSJames Feist auto data = std::array<uint8_t, 2>{2, 4}; 312f299807fSJames Feist variant = data; 313f299807fSJames Feist 314f299807fSJames Feist p.pack(variant); 315f299807fSJames Feist ASSERT_EQ(p.size(), sizeof(data)); 316f299807fSJames Feist 317f299807fSJames Feist // check that the bytes were correctly packed packed (LSB first) 318f299807fSJames Feist std::vector<uint8_t> k = {2, 4}; 319f299807fSJames Feist ASSERT_EQ(p.raw, k); 320f299807fSJames Feist } 321f299807fSJames Feist 322ebe8e906SVernon Mauery TEST(PackAdvanced, BoolsnBitfieldsnFixedIntsOhMy) 323ebe8e906SVernon Mauery { 324ebe8e906SVernon Mauery // each element will be added, filling the low-order bits first 325ebe8e906SVernon Mauery // with multi-byte values getting added LSByte first 326ebe8e906SVernon Mauery // v1 will occupy k[0][1:0] 327ebe8e906SVernon Mauery // v2 will occupy k[0][2] 328ebe8e906SVernon Mauery // v3[4:0] will occupy k[0][7:3], v3[6:5] will occupy k[1][1:0] 329ebe8e906SVernon Mauery // v4 will occupy k[1][2] 330ebe8e906SVernon Mauery // v5 will occupy k[1][7:3] 331ebe8e906SVernon Mauery ipmi::message::Payload p; 332ebe8e906SVernon Mauery uint2_t v1 = 2; // binary 0b10 333ebe8e906SVernon Mauery bool v2 = true; // binary 0b1 334ebe8e906SVernon Mauery std::bitset<7> v3(0x73); // binary 0b1110011 335ebe8e906SVernon Mauery bool v4 = false; // binary 0b0 336ebe8e906SVernon Mauery uint5_t v5 = 27; // binary 0b11011 337ebe8e906SVernon Mauery // concat binary: 0b1101101110011110 -> 0xdb9e -> 0x9e 0xdb (LSByte first) 338ebe8e906SVernon Mauery p.pack(v1, v2, v3, v4, v5); 339ebe8e906SVernon Mauery // check that the number of bytes matches 340ebe8e906SVernon Mauery ASSERT_EQ(p.size(), sizeof(uint16_t)); 341ebe8e906SVernon Mauery // check that the bytes were correctly packed (LSB first) 342ebe8e906SVernon Mauery std::vector<uint8_t> k = {0x9e, 0xdb}; 343ebe8e906SVernon Mauery ASSERT_EQ(p.raw, k); 344ebe8e906SVernon Mauery } 345ebe8e906SVernon Mauery 346ebe8e906SVernon Mauery TEST(PackAdvanced, UnalignedBitPacking) 347ebe8e906SVernon Mauery { 348ebe8e906SVernon Mauery // unaligned multi-byte values will be packed the same as 349ebe8e906SVernon Mauery // other bits, effectively building up a large value, low-order 350ebe8e906SVernon Mauery // bits first, then outputting a stream of LSByte values 351ebe8e906SVernon Mauery // v1 will occupy k[0][1:0] 352ebe8e906SVernon Mauery // v2[5:0] will occupy k[0][7:2], v2[7:6] will occupy k[1][1:0] 353ebe8e906SVernon Mauery // v3 will occupy k[1][2] 354ebe8e906SVernon Mauery // v4[4:0] will occupy k[1][7:3] v4[12:5] will occupy k[2][7:0] 355ebe8e906SVernon Mauery // v4[15:13] will occupy k[3][2:0] 356ebe8e906SVernon Mauery // v5 will occupy k[3][3] 357ebe8e906SVernon Mauery // v6[3:0] will occupy k[3][7:0] v6[11:4] will occupy k[4][7:0] 358ebe8e906SVernon Mauery // v6[19:12] will occupy k[5][7:0] v6[27:20] will occupy k[6][7:0] 359ebe8e906SVernon Mauery // v6[31:28] will occupy k[7][3:0] 360ebe8e906SVernon Mauery // v7 will occupy k[7][7:4] 361ebe8e906SVernon Mauery ipmi::message::Payload p; 362ebe8e906SVernon Mauery uint2_t v1 = 2; // binary 0b10 363ebe8e906SVernon Mauery uint8_t v2 = 0xa5; // binary 0b10100101 364ebe8e906SVernon Mauery bool v3 = false; // binary 0b0 365ebe8e906SVernon Mauery uint16_t v4 = 0xa55a; // binary 0b1010010101011010 366ebe8e906SVernon Mauery bool v5 = true; // binary 0b1 367ebe8e906SVernon Mauery uint32_t v6 = 0xdbc3bd3c; // binary 0b11011011110000111011110100111100 368ebe8e906SVernon Mauery uint4_t v7 = 9; // binary 0b1001 369ebe8e906SVernon Mauery // concat binary: 370ebe8e906SVernon Mauery // 0b1001110110111100001110111101001111001101001010101101001010010110 371ebe8e906SVernon Mauery // -> 0x9dbc3bd3cd2ad296 -> 0x96 0xd2 0x2a 0xcd 0xd3 0x3b 0xbc 0x9d 372ebe8e906SVernon Mauery p.pack(v1, v2, v3, v4, v5, v6, v7); 373ebe8e906SVernon Mauery // check that the number of bytes matches 374ebe8e906SVernon Mauery ASSERT_EQ(p.size(), sizeof(uint64_t)); 375ebe8e906SVernon Mauery // check that the bytes were correctly packed (LSB first) 376ebe8e906SVernon Mauery std::vector<uint8_t> k = {0x96, 0xd2, 0x2a, 0xcd, 0xd3, 0x3b, 0xbc, 0x9d}; 377ebe8e906SVernon Mauery ASSERT_EQ(p.raw, k); 378ebe8e906SVernon Mauery } 379*508c4576SVernon Mauery 380*508c4576SVernon Mauery TEST(PackAdvanced, ComplexOptionalTuple) 381*508c4576SVernon Mauery { 382*508c4576SVernon Mauery constexpr size_t macSize = 6; 383*508c4576SVernon Mauery // inspired from a real-world case of Get Session Info 384*508c4576SVernon Mauery constexpr uint8_t handle = 0x23; // handle for active session 385*508c4576SVernon Mauery constexpr uint8_t maxSessions = 15; // number of possible active sessions 386*508c4576SVernon Mauery constexpr uint8_t currentSessions = 4; // number of current active sessions 387*508c4576SVernon Mauery std::optional< // only returned for active session 388*508c4576SVernon Mauery std::tuple<uint8_t, // user ID 389*508c4576SVernon Mauery uint8_t, // privilege 390*508c4576SVernon Mauery uint4_t, // channel number 391*508c4576SVernon Mauery uint4_t // protocol (RMCP+) 392*508c4576SVernon Mauery >> 393*508c4576SVernon Mauery activeSession; 394*508c4576SVernon Mauery std::optional< // only returned for channel type LAN 395*508c4576SVernon Mauery std::tuple<uint32_t, // IPv4 address 396*508c4576SVernon Mauery std::array<uint8_t, macSize>, // MAC address 397*508c4576SVernon Mauery uint16_t // port 398*508c4576SVernon Mauery >> 399*508c4576SVernon Mauery lanSession; 400*508c4576SVernon Mauery 401*508c4576SVernon Mauery constexpr uint8_t userID = 7; 402*508c4576SVernon Mauery constexpr uint8_t priv = 4; 403*508c4576SVernon Mauery constexpr uint4_t channel = 2; 404*508c4576SVernon Mauery constexpr uint4_t protocol = 1; 405*508c4576SVernon Mauery activeSession.emplace(userID, priv, channel, protocol); 406*508c4576SVernon Mauery constexpr std::array<uint8_t, macSize> macAddr{0}; 407*508c4576SVernon Mauery lanSession.emplace(0x0a010105, macAddr, 55327); 408*508c4576SVernon Mauery 409*508c4576SVernon Mauery ipmi::message::Payload p; 410*508c4576SVernon Mauery p.pack(handle, maxSessions, currentSessions, activeSession, lanSession); 411*508c4576SVernon Mauery ASSERT_EQ(p.size(), sizeof(handle) + sizeof(maxSessions) + 412*508c4576SVernon Mauery sizeof(currentSessions) + 3 * sizeof(uint8_t) + 413*508c4576SVernon Mauery sizeof(uint32_t) + sizeof(uint8_t) * macSize + 414*508c4576SVernon Mauery sizeof(uint16_t)); 415*508c4576SVernon Mauery uint8_t protocol_channel = 416*508c4576SVernon Mauery (static_cast<uint8_t>(protocol) << 4) | static_cast<uint8_t>(channel); 417*508c4576SVernon Mauery std::vector<uint8_t> k = {handle, maxSessions, currentSessions, userID, 418*508c4576SVernon Mauery priv, protocol_channel, 419*508c4576SVernon Mauery // ip addr 420*508c4576SVernon Mauery 0x05, 0x01, 0x01, 0x0a, 421*508c4576SVernon Mauery // mac addr 422*508c4576SVernon Mauery 0, 0, 0, 0, 0, 0, 423*508c4576SVernon Mauery // port 424*508c4576SVernon Mauery 0x1f, 0xd8}; 425*508c4576SVernon Mauery ASSERT_EQ(p.raw, k); 426*508c4576SVernon Mauery } 427