/**
 * 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)
    ipmi::SecureBuffer 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)
    ipmi::SecureBuffer 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)
    ipmi::SecureBuffer 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)
    ipmi::SecureBuffer 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)
    ipmi::SecureBuffer 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)
    ipmi::SecureBuffer 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)
    ipmi::SecureBuffer 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)
    ipmi::SecureBuffer 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)
    ipmi::SecureBuffer 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)
    ipmi::SecureBuffer k = {0x04, 0x86, 0x00, 0x02};
    ASSERT_EQ(p.raw, k);
}

TEST(PackBasics, Tuple)
{
    // tuples are the new struct, pack a tuple
    ipmi::message::Payload p;
    auto v = std::make_tuple(static_cast<uint16_t>(0x8604), 'A');
    p.pack(v);
    // check that the number of bytes matches
    ASSERT_EQ(p.size(), sizeof(uint16_t) + sizeof(char));
    // check that the bytes were correctly packed (LSB first)
    ipmi::SecureBuffer k = {0x04, 0x86, 0x41};
    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)
    ipmi::SecureBuffer 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)
    ipmi::SecureBuffer 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)
    ipmi::SecureBuffer 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)
    ipmi::SecureBuffer k = {0x02, 0x00, 0x86, 0x04};
    ASSERT_EQ(p.raw, k);
}

TEST(PackBasics, VectorUnaligned)
{
    ipmi::message::Payload p;
    EXPECT_EQ(p.pack(true, std::vector<uint8_t>{1}), 1);
    EXPECT_EQ(p.raw, ipmi::SecureBuffer{0b1});
}

TEST(PackBasics, StringView)
{
    ipmi::message::Payload p;
    EXPECT_EQ(p.pack(std::string_view{"\x24\x30\x11"}), 0);
    EXPECT_EQ(p.raw, ipmi::SecureBuffer({0x24, 0x30, 0x11}));
}

TEST(PackBasics, StringViewUnaligned)
{
    ipmi::message::Payload p;
    EXPECT_EQ(p.pack(true, std::string_view{"abc"}), 1);
    EXPECT_EQ(p.raw, ipmi::SecureBuffer({0b1}));
}

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)
    ipmi::SecureBuffer 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)
    ipmi::SecureBuffer k = {0x02, 0x00, 0x86, 0x04};
    ASSERT_EQ(p.raw, k);
}

TEST(PackBasics, Payload)
{
    ipmi::message::Payload p;
    EXPECT_EQ(p.pack(true), 0);
    EXPECT_EQ(p.pack(ipmi::message::Payload({0x24, 0x30})), 0);
    EXPECT_EQ(p.raw, ipmi::SecureBuffer({0b1, 0x24, 0x30}));
}

TEST(PackBasics, PayloadUnaligned)
{
    ipmi::message::Payload p;
    EXPECT_EQ(p.pack(true, ipmi::message::Payload({0x24})), 1);
    EXPECT_EQ(p.raw, ipmi::SecureBuffer({0b1}));
}

TEST(PackBasics, PayloadOtherUnaligned)
{
    ipmi::message::Payload p, q;
    q.appendBits(1, 1);
    EXPECT_EQ(p.pack(true), 0);
    EXPECT_EQ(p.pack(q), 1);
    EXPECT_EQ(p.raw, ipmi::SecureBuffer({0b1}));
}

TEST(PackBasics, PrependPayload)
{
    ipmi::message::Payload p;
    EXPECT_EQ(p.pack(true), 0);
    EXPECT_EQ(p.prepend(ipmi::message::Payload({0x24, 0x30})), 0);
    EXPECT_EQ(p.raw, ipmi::SecureBuffer({0x24, 0x30, 0b1}));
}

TEST(PackBasics, PrependPayloadUnaligned)
{
    ipmi::message::Payload p;
    p.appendBits(1, 1);
    EXPECT_EQ(p.prepend(ipmi::message::Payload({0x24})), 1);
    p.drain();
    EXPECT_EQ(p.raw, ipmi::SecureBuffer({0b1}));
}

TEST(PackBasics, PrependPayloadOtherUnaligned)
{
    ipmi::message::Payload p, q;
    q.appendBits(1, 1);
    EXPECT_EQ(p.pack(true), 0);
    EXPECT_EQ(p.prepend(q), 1);
    EXPECT_EQ(p.raw, ipmi::SecureBuffer({0b1}));
}

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)
    ipmi::SecureBuffer 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)
    ipmi::SecureBuffer 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)
    ipmi::SecureBuffer 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)
    ipmi::SecureBuffer 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)
    ipmi::SecureBuffer k = {0x96, 0xd2, 0x2a, 0xcd, 0xd3, 0x3b, 0xbc, 0x9d};
    ASSERT_EQ(p.raw, k);
}

TEST(PackAdvanced, ComplexOptionalTuple)
{
    constexpr size_t macSize = 6;
    // inspired from a real-world case of Get Session Info
    constexpr uint8_t handle = 0x23;       // handle for active session
    constexpr uint8_t maxSessions = 15;    // number of possible active sessions
    constexpr uint8_t currentSessions = 4; // number of current active sessions
    std::optional<                         // only returned for active session
        std::tuple<uint8_t,                // user ID
                   uint8_t,                // privilege
                   uint4_t,                // channel number
                   uint4_t                 // protocol (RMCP+)
                   >>
        activeSession;
    std::optional<           // only returned for channel type LAN
        std::tuple<uint32_t, // IPv4 address
                   std::array<uint8_t, macSize>, // MAC address
                   uint16_t                      // port
                   >>
        lanSession;

    constexpr uint8_t userID = 7;
    constexpr uint8_t priv = 4;
    constexpr uint4_t channel = 2;
    constexpr uint4_t protocol = 1;
    activeSession.emplace(userID, priv, channel, protocol);
    constexpr std::array<uint8_t, macSize> macAddr{0};
    lanSession.emplace(0x0a010105, macAddr, 55327);

    ipmi::message::Payload p;
    p.pack(handle, maxSessions, currentSessions, activeSession, lanSession);
    ASSERT_EQ(p.size(),
              sizeof(handle) + sizeof(maxSessions) + sizeof(currentSessions) +
                  3 * sizeof(uint8_t) + sizeof(uint32_t) +
                  sizeof(uint8_t) * macSize + sizeof(uint16_t));
    uint8_t protocol_channel =
        (static_cast<uint8_t>(protocol) << 4) | static_cast<uint8_t>(channel);
    ipmi::SecureBuffer k = {
        handle, maxSessions, currentSessions, userID, priv, protocol_channel,
        // ip addr
        0x05, 0x01, 0x01, 0x0a,
        // mac addr
        0, 0, 0, 0, 0, 0,
        // port
        0x1f, 0xd8};
    ASSERT_EQ(p.raw, k);
}