blob: 76cc9d4948f98cceccb69e0a13674e545d1b8f17 [file] [log] [blame]
#include <endian.h>
#include <algorithm>
#include <cstdint>
#include <cstring>
#include <iterator>
#include <string>
#include <utility>
#include <vector>
#include "libpldm/base.h"
#include "libpldm/bios.h"
#include "libpldm/bios_table.h"
#include "libpldm/utils.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
using testing::ElementsAreArray;
using Table = std::vector<uint8_t>;
void buildTable(Table& table)
{
auto padSize = ((table.size() % 4) ? (4 - table.size() % 4) : 0);
table.insert(table.end(), padSize, 0);
uint32_t checksum = crc32(table.data(), table.size());
checksum = htole32(checksum);
uint8_t a[4];
std::memcpy(a, &checksum, sizeof(checksum));
table.insert(table.end(), std::begin(a), std::end(a));
}
template <typename First, typename... Rest>
void buildTable(Table& table, First& first, Rest&... rest)
{
table.insert(table.end(), first.begin(), first.end());
buildTable(table, rest...);
}
TEST(AttrTable, HeaderDecodeTest)
{
std::vector<uint8_t> enumEntry{
2, 0, /* attr handle */
0, /* attr type */
1, 0, /* attr name handle (string handle) */
2, /* number of possible value */
2, 0, /* possible value handle */
3, 0, /* possible value handle */
1, /* number of default value */
0 /* defaut value string handle index */
};
auto entry =
reinterpret_cast<struct pldm_bios_attr_table_entry*>(enumEntry.data());
auto attrHandle = pldm_bios_table_attr_entry_decode_attribute_handle(entry);
EXPECT_EQ(attrHandle, 2);
auto attrType = pldm_bios_table_attr_entry_decode_attribute_type(entry);
EXPECT_EQ(attrType, 0);
auto stringHandle = pldm_bios_table_attr_entry_decode_string_handle(entry);
EXPECT_EQ(stringHandle, 1);
}
TEST(AttrTable, EnumEntryDecodeTest)
{
std::vector<uint8_t> enumEntry{
0, 0, /* attr handle */
0, /* attr type */
1, 0, /* attr name handle */
2, /* number of possible value */
2, 0, /* possible value handle */
3, 0, /* possible value handle */
1, /* number of default value */
1 /* defaut value string handle index */
};
auto entry =
reinterpret_cast<struct pldm_bios_attr_table_entry*>(enumEntry.data());
uint8_t pvNumber;
ASSERT_EQ(
pldm_bios_table_attr_entry_enum_decode_pv_num_check(entry, &pvNumber),
PLDM_SUCCESS);
EXPECT_EQ(pvNumber, 2);
pvNumber = 0;
auto rc =
pldm_bios_table_attr_entry_enum_decode_pv_num_check(entry, &pvNumber);
EXPECT_EQ(rc, PLDM_SUCCESS);
EXPECT_EQ(pvNumber, 2);
std::vector<uint16_t> pvHandles(pvNumber, 0);
ASSERT_EQ(pldm_bios_table_attr_entry_enum_decode_pv_hdls_check(
entry, pvHandles.data(), pvHandles.size()),
PLDM_SUCCESS);
EXPECT_EQ(pvNumber, 2);
EXPECT_EQ(pvHandles[0], 2);
EXPECT_EQ(pvHandles[1], 3);
pvHandles.resize(1);
ASSERT_EQ(pldm_bios_table_attr_entry_enum_decode_pv_hdls_check(
entry, pvHandles.data(), pvHandles.size()),
PLDM_SUCCESS);
EXPECT_EQ(pvHandles[0], 2);
pvHandles.resize(2);
rc = pldm_bios_table_attr_entry_enum_decode_pv_hdls_check(
entry, pvHandles.data(), pvHandles.size());
EXPECT_EQ(rc, PLDM_SUCCESS);
EXPECT_EQ(pvHandles[0], 2);
EXPECT_EQ(pvHandles[1], 3);
rc = pldm_bios_table_attr_entry_enum_decode_pv_hdls_check(
entry, pvHandles.data(), 1);
EXPECT_EQ(rc, PLDM_SUCCESS);
uint8_t defNumber;
ASSERT_EQ(
pldm_bios_table_attr_entry_enum_decode_def_num_check(entry, &defNumber),
PLDM_SUCCESS);
EXPECT_EQ(defNumber, 1);
std::vector<uint8_t> defIndices(defNumber);
rc = pldm_bios_table_attr_entry_enum_decode_def_indices(
entry, defIndices.data(), defIndices.size());
EXPECT_EQ(rc, defNumber);
EXPECT_THAT(defIndices, ElementsAreArray({1}));
defNumber = 0;
rc =
pldm_bios_table_attr_entry_enum_decode_def_num_check(entry, &defNumber);
EXPECT_EQ(rc, PLDM_SUCCESS);
EXPECT_EQ(defNumber, 1);
rc =
pldm_bios_table_attr_entry_enum_decode_pv_num_check(nullptr, &pvNumber);
EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
rc = pldm_bios_table_attr_entry_enum_decode_def_num_check(entry, nullptr);
EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
entry->attr_type = PLDM_BIOS_STRING;
rc = pldm_bios_table_attr_entry_enum_decode_pv_num_check(entry, &pvNumber);
EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
rc =
pldm_bios_table_attr_entry_enum_decode_def_num_check(entry, &defNumber);
EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
rc =
pldm_bios_table_attr_entry_enum_decode_pv_hdls_check(entry, nullptr, 0);
EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
}
TEST(AttrTable, EnumEntryEncodeTest)
{
std::vector<uint8_t> enumEntry{
0, 0, /* attr handle */
0, /* attr type */
1, 0, /* attr name handle */
2, /* number of possible value */
2, 0, /* possible value handle */
3, 0, /* possible value handle */
1, /* number of default value */
0 /* defaut value string handle index */
};
std::vector<uint16_t> pv_hdls{2, 3};
std::vector<uint8_t> defs{0};
struct pldm_bios_table_attr_entry_enum_info info = {
1, /* name handle */
false, /* read only */
2, /* pv number */
pv_hdls.data(), /* pv handle */
1, /*def number */
defs.data() /*def index*/
};
auto encodeLength = pldm_bios_table_attr_entry_enum_encode_length(2, 1);
EXPECT_EQ(encodeLength, enumEntry.size());
std::vector<uint8_t> encodeEntry(encodeLength, 0);
ASSERT_EQ(pldm_bios_table_attr_entry_enum_encode_check(
encodeEntry.data(), encodeEntry.size(), &info),
PLDM_SUCCESS);
// set attr handle = 0
encodeEntry[0] = 0;
encodeEntry[1] = 0;
EXPECT_EQ(enumEntry, encodeEntry);
EXPECT_NE(pldm_bios_table_attr_entry_enum_encode_check(
encodeEntry.data(), encodeEntry.size() - 1, &info),
PLDM_SUCCESS);
auto rc = pldm_bios_table_attr_entry_enum_encode_check(
encodeEntry.data(), encodeEntry.size(), &info);
EXPECT_EQ(rc, PLDM_SUCCESS);
// set attr handle = 0
encodeEntry[0] = 0;
encodeEntry[1] = 0;
EXPECT_EQ(enumEntry, encodeEntry);
rc = pldm_bios_table_attr_entry_enum_encode_check(
encodeEntry.data(), encodeEntry.size() - 1, &info);
EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
}
TEST(AttrTable, StringEntryDecodeTest)
{
std::vector<uint8_t> stringEntry{
1, 0, /* attr handle */
1, /* attr type */
12, 0, /* attr name handle */
1, /* string type */
1, 0, /* minimum length of the string in bytes */
100, 0, /* maximum length of the string in bytes */
3, 0, /* length of default string in length */
'a', 'b', 'c' /* default string */
};
auto entry = reinterpret_cast<struct pldm_bios_attr_table_entry*>(
stringEntry.data());
auto stringType =
pldm_bios_table_attr_entry_string_decode_string_type(entry);
EXPECT_EQ(stringType, 1);
auto minLength = pldm_bios_table_attr_entry_string_decode_min_length(entry);
EXPECT_EQ(minLength, 1);
auto maxLength = pldm_bios_table_attr_entry_string_decode_max_length(entry);
EXPECT_EQ(maxLength, 100);
uint16_t defStringLength;
ASSERT_EQ(pldm_bios_table_attr_entry_string_decode_def_string_length_check(
entry, &defStringLength),
PLDM_SUCCESS);
EXPECT_EQ(defStringLength, 3);
std::vector<char> defString(defStringLength + 1);
auto rc = pldm_bios_table_attr_entry_string_decode_def_string(
entry, defString.data(), defString.size());
EXPECT_EQ(rc, 3);
EXPECT_STREQ(defString.data(), "abc");
rc = pldm_bios_table_attr_entry_string_decode_def_string(
entry, defString.data(), defString.size() - 1);
EXPECT_EQ(rc, 2);
EXPECT_STREQ(defString.data(), "ab");
defStringLength = 0;
rc = pldm_bios_table_attr_entry_string_decode_def_string_length_check(
entry, &defStringLength);
EXPECT_EQ(rc, PLDM_SUCCESS);
EXPECT_EQ(defStringLength, 3);
rc = pldm_bios_table_attr_entry_string_decode_def_string_length_check(
entry, nullptr);
EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
rc = pldm_bios_table_attr_entry_string_decode_def_string_length_check(
nullptr, &defStringLength);
EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
entry->attr_type = PLDM_BIOS_INTEGER;
rc = pldm_bios_table_attr_entry_string_decode_def_string_length_check(
entry, &defStringLength);
EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
rc = pldm_bios_table_attr_entry_string_decode_def_string_length_check(
nullptr, &defStringLength);
EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
}
TEST(AttrTable, StringEntryEncodeTest)
{
std::vector<uint8_t> stringEntry{
0, 0, /* attr handle */
1, /* attr type */
3, 0, /* attr name handle */
1, /* string type */
1, 0, /* min string length */
100, 0, /* max string length */
3, 0, /* default string length */
'a', 'b', 'c', /* defaul string */
};
struct pldm_bios_table_attr_entry_string_info info = {
3, /* name handle */
false, /* read only */
1, /* string type ascii */
1, /* min length */
100, /* max length */
3, /* def length */
"abc", /* def string */
};
auto encodeLength = pldm_bios_table_attr_entry_string_encode_length(3);
EXPECT_EQ(encodeLength, stringEntry.size());
std::vector<uint8_t> encodeEntry(encodeLength, 0);
ASSERT_EQ(pldm_bios_table_attr_entry_string_encode_check(
encodeEntry.data(), encodeEntry.size(), &info),
PLDM_SUCCESS);
// set attr handle = 0
encodeEntry[0] = 0;
encodeEntry[1] = 0;
EXPECT_EQ(stringEntry, encodeEntry);
EXPECT_NE(pldm_bios_table_attr_entry_string_encode_check(
encodeEntry.data(), encodeEntry.size() - 1, &info),
PLDM_SUCCESS);
auto rc = pldm_bios_table_attr_entry_string_encode_check(
encodeEntry.data(), encodeEntry.size(), &info);
EXPECT_EQ(rc, PLDM_SUCCESS);
// set attr handle = 0
encodeEntry[0] = 0;
encodeEntry[1] = 0;
EXPECT_EQ(stringEntry, encodeEntry);
rc = pldm_bios_table_attr_entry_string_encode_check(
encodeEntry.data(), encodeEntry.size() - 1, &info);
EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
std::swap(info.max_length, info.min_length);
const char* errmsg;
rc = pldm_bios_table_attr_entry_string_info_check(&info, &errmsg);
EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
EXPECT_STREQ(
"MinimumStingLength should not be greater than MaximumStringLength",
errmsg);
rc = pldm_bios_table_attr_entry_string_encode_check(
encodeEntry.data(), encodeEntry.size(), &info);
EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
std::swap(info.max_length, info.min_length);
std::vector<uint8_t> stringEntryLength0{
0, 0, /* attr handle */
1, /* attr type */
3, 0, /* attr name handle */
1, /* string type */
0, 0, /* min string length */
100, 0, /* max string length */
0, 0, /* default string length */
};
info.min_length = 0;
info.def_length = 0;
info.def_string = nullptr;
encodeLength = pldm_bios_table_attr_entry_string_encode_length(0);
EXPECT_EQ(encodeLength, stringEntryLength0.size());
encodeEntry.resize(encodeLength);
ASSERT_EQ(pldm_bios_table_attr_entry_string_encode_check(
encodeEntry.data(), encodeEntry.size(), &info),
PLDM_SUCCESS);
// set attr handle = 0
encodeEntry[0] = 0;
encodeEntry[1] = 0;
EXPECT_EQ(stringEntryLength0, encodeEntry);
}
TEST(AttrTable, integerEntryEncodeTest)
{
std::vector<uint8_t> integerEntry{
0, 0, /* attr handle */
3, /* attr type */
1, 0, /* attr name handle */
1, 0, 0, 0, 0, 0, 0, 0, /* lower bound */
10, 0, 0, 0, 0, 0, 0, 0, /* upper bound */
2, 0, 0, 0, /* scalar increment */
3, 0, 0, 0, 0, 0, 0, 0, /* defaut value */
};
std::vector<uint16_t> pv_hdls{2, 3};
std::vector<uint8_t> defs{0};
struct pldm_bios_table_attr_entry_integer_info info = {
1, /* name handle */
false, /* read only */
1, /* lower bound */
10, /* upper bound */
2, /* sacalar increment */
3 /* default value */
};
auto encodeLength = pldm_bios_table_attr_entry_integer_encode_length();
EXPECT_EQ(encodeLength, integerEntry.size());
std::vector<uint8_t> encodeEntry(encodeLength, 0);
ASSERT_EQ(pldm_bios_table_attr_entry_integer_encode_check(
encodeEntry.data(), encodeEntry.size(), &info),
PLDM_SUCCESS);
// set attr handle = 0
encodeEntry[0] = 0;
encodeEntry[1] = 0;
EXPECT_EQ(integerEntry, encodeEntry);
EXPECT_NE(pldm_bios_table_attr_entry_integer_encode_check(
encodeEntry.data(), encodeEntry.size() - 1, &info),
PLDM_SUCCESS);
auto rc = pldm_bios_table_attr_entry_integer_encode_check(
encodeEntry.data(), encodeEntry.size(), &info);
EXPECT_EQ(rc, PLDM_SUCCESS);
// set attr handle = 0
encodeEntry[0] = 0;
encodeEntry[1] = 0;
EXPECT_EQ(integerEntry, encodeEntry);
rc = pldm_bios_table_attr_entry_integer_encode_check(
encodeEntry.data(), encodeEntry.size() - 1, &info);
EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
info.lower_bound = 100;
info.upper_bound = 50;
const char* errmsg;
rc = pldm_bios_table_attr_entry_integer_info_check(&info, &errmsg);
EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
EXPECT_STREQ("LowerBound should not be greater than UpperBound", errmsg);
rc = pldm_bios_table_attr_entry_integer_encode_check(
encodeEntry.data(), encodeEntry.size(), &info);
EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
}
TEST(AttrTable, integerEntryDecodeTest)
{
std::vector<uint8_t> integerEntry{
0, 0, /* attr handle */
3, /* attr type */
1, 0, /* attr name handle */
1, 0, 0, 0, 0, 0, 0, 0, /* lower bound */
10, 0, 0, 0, 0, 0, 0, 0, /* upper bound */
2, 0, 0, 0, /* scalar increment */
3, 0, 0, 0, 0, 0, 0, 0, /* defaut value */
};
uint64_t lower;
uint64_t upper;
uint64_t def;
uint32_t scalar;
auto entry = reinterpret_cast<struct pldm_bios_attr_table_entry*>(
integerEntry.data());
pldm_bios_table_attr_entry_integer_decode(entry, &lower, &upper, &scalar,
&def);
EXPECT_EQ(lower, 1u);
EXPECT_EQ(upper, 10u);
EXPECT_EQ(scalar, 2u);
EXPECT_EQ(def, 3u);
}
TEST(AttrTable, ItearatorTest)
{
std::vector<uint8_t> enumEntry{
0, 0, /* attr handle */
0, /* attr type */
1, 0, /* attr name handle */
2, /* number of possible value */
2, 0, /* possible value handle */
3, 0, /* possible value handle */
1, /* number of default value */
0 /* defaut value string handle index */
};
std::vector<uint8_t> stringEntry{
1, 0, /* attr handle */
1, /* attr type */
12, 0, /* attr name handle */
1, /* string type */
1, 0, /* minimum length of the string in bytes */
100, 0, /* maximum length of the string in bytes */
3, 0, /* length of default string in length */
'a', 'b', 'c' /* default string */
};
std::vector<uint8_t> integerEntry{
0, 0, /* attr handle */
3, /* attr type */
1, 0, /* attr name handle */
1, 0, 0, 0, 0, 0, 0, 0, /* lower bound */
10, 0, 0, 0, 0, 0, 0, 0, /* upper bound */
2, 0, 0, 0, /* scalar increment */
3, 0, 0, 0, 0, 0, 0, 0, /* defaut value */
};
Table table;
buildTable(table, enumEntry, stringEntry, integerEntry, enumEntry);
auto iter = pldm_bios_table_iter_create(table.data(), table.size(),
PLDM_BIOS_ATTR_TABLE);
auto entry = pldm_bios_table_iter_attr_entry_value(iter);
auto rc = std::memcmp(entry, enumEntry.data(), enumEntry.size());
EXPECT_EQ(rc, 0);
pldm_bios_table_iter_next(iter);
entry = pldm_bios_table_iter_attr_entry_value(iter);
rc = std::memcmp(entry, stringEntry.data(), stringEntry.size());
EXPECT_EQ(rc, 0);
pldm_bios_table_iter_next(iter);
entry = pldm_bios_table_iter_attr_entry_value(iter);
rc = std::memcmp(entry, integerEntry.data(), integerEntry.size());
EXPECT_EQ(rc, 0);
pldm_bios_table_iter_next(iter);
entry = pldm_bios_table_iter_attr_entry_value(iter);
rc = std::memcmp(entry, enumEntry.data(), enumEntry.size());
EXPECT_EQ(rc, 0);
pldm_bios_table_iter_next(iter);
EXPECT_TRUE(pldm_bios_table_iter_is_end(iter));
pldm_bios_table_iter_free(iter);
}
TEST(AttrTable, FindTest)
{
std::vector<uint8_t> enumEntry{
0, 0, /* attr handle */
0, /* attr type */
1, 0, /* attr name handle */
2, /* number of possible value */
2, 0, /* possible value handle */
3, 0, /* possible value handle */
1, /* number of default value */
0 /* defaut value string handle index */
};
std::vector<uint8_t> stringEntry{
1, 0, /* attr handle */
1, /* attr type */
2, 0, /* attr name handle */
1, /* string type */
1, 0, /* minimum length of the string in bytes */
100, 0, /* maximum length of the string in bytes */
3, 0, /* length of default string in length */
'a', 'b', 'c' /* default string */
};
std::vector<uint8_t> integerEntry{
0, 0, /* attr handle */
3, /* attr type */
3, 0, /* attr name handle */
1, 0, 0, 0, 0, 0, 0, 0, /* lower bound */
10, 0, 0, 0, 0, 0, 0, 0, /* upper bound */
2, 0, 0, 0, /* scalar increment */
3, 0, 0, 0, 0, 0, 0, 0, /* defaut value */
};
Table table;
buildTable(table, enumEntry, stringEntry, integerEntry, enumEntry);
auto entry =
pldm_bios_table_attr_find_by_handle(table.data(), table.size(), 1);
EXPECT_NE(entry, nullptr);
auto p = reinterpret_cast<const uint8_t*>(entry);
EXPECT_THAT(std::vector<uint8_t>(p, p + stringEntry.size()),
ElementsAreArray(stringEntry));
entry = pldm_bios_table_attr_find_by_handle(table.data(), table.size(), 3);
EXPECT_EQ(entry, nullptr);
entry = pldm_bios_table_attr_find_by_string_handle(table.data(),
table.size(), 2);
EXPECT_NE(entry, nullptr);
p = reinterpret_cast<const uint8_t*>(entry);
EXPECT_THAT(std::vector<uint8_t>(p, p + stringEntry.size()),
ElementsAreArray(stringEntry));
entry = pldm_bios_table_attr_find_by_string_handle(table.data(),
table.size(), 4);
EXPECT_EQ(entry, nullptr);
}
TEST(AttrValTable, HeaderDecodeTest)
{
std::vector<uint8_t> enumEntry{
1, 0, /* attr handle */
0, /* attr type */
2, /* number of current value */
0, /* current value string handle index */
1, /* current value string handle index */
};
auto entry = reinterpret_cast<struct pldm_bios_attr_val_table_entry*>(
enumEntry.data());
auto attrHandle =
pldm_bios_table_attr_value_entry_decode_attribute_handle(entry);
EXPECT_EQ(attrHandle, 1);
auto attrType =
pldm_bios_table_attr_value_entry_decode_attribute_type(entry);
EXPECT_EQ(attrType, 0);
}
TEST(AttrValTable, EnumEntryEncodeTest)
{
std::vector<uint8_t> enumEntry{
0, 0, /* attr handle */
0, /* attr type */
2, /* number of current value */
0, /* current value string handle index */
1, /* current value string handle index */
};
auto length = pldm_bios_table_attr_value_entry_encode_enum_length(2);
EXPECT_EQ(length, enumEntry.size());
std::vector<uint8_t> encodeEntry(length, 0);
uint8_t handles[] = {0, 1};
pldm_bios_table_attr_value_entry_encode_enum(
encodeEntry.data(), encodeEntry.size(), 0, 0, 2, handles);
EXPECT_EQ(encodeEntry, enumEntry);
EXPECT_DEATH(
pldm_bios_table_attr_value_entry_encode_enum(
encodeEntry.data(), encodeEntry.size() - 1, 0, 0, 2, handles),
"rc == PLDM_SUCCESS");
auto rc = pldm_bios_table_attr_value_entry_encode_enum_check(
encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_ENUMERATION, 2,
handles);
EXPECT_EQ(rc, PLDM_SUCCESS);
EXPECT_EQ(encodeEntry, enumEntry);
auto entry = reinterpret_cast<struct pldm_bios_attr_val_table_entry*>(
enumEntry.data());
entry->attr_type = PLDM_BIOS_ENUMERATION_READ_ONLY;
rc = pldm_bios_table_attr_value_entry_encode_enum_check(
encodeEntry.data(), encodeEntry.size(), 0,
PLDM_BIOS_ENUMERATION_READ_ONLY, 2, handles);
EXPECT_EQ(rc, PLDM_SUCCESS);
EXPECT_EQ(encodeEntry, enumEntry);
rc = pldm_bios_table_attr_value_entry_encode_enum_check(
encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_PASSWORD, 2,
handles);
EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
rc = pldm_bios_table_attr_value_entry_encode_enum_check(
encodeEntry.data(), encodeEntry.size() - 1, 0, PLDM_BIOS_ENUMERATION, 2,
handles);
EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
}
TEST(AttrValTable, EnumEntryDecodeTest)
{
std::vector<uint8_t> enumEntry{
0, 0, /* attr handle */
0, /* attr type */
2, /* number of current value */
0, /* current value string handle index */
1, /* current value string handle index */
};
auto entry = reinterpret_cast<struct pldm_bios_attr_val_table_entry*>(
enumEntry.data());
auto number = pldm_bios_table_attr_value_entry_enum_decode_number(entry);
EXPECT_EQ(2, number);
std::vector<uint8_t> handles(2, 0);
auto rc = pldm_bios_table_attr_value_entry_enum_decode_handles(
entry, handles.data(), handles.size());
EXPECT_EQ(rc, 2);
EXPECT_EQ(handles[0], 0);
EXPECT_EQ(handles[1], 1);
}
TEST(AttrValTable, stringEntryEncodeTest)
{
std::vector<uint8_t> stringEntry{
0, 0, /* attr handle */
1, /* attr type */
3, 0, /* current string length */
'a', 'b', 'c', /* defaut value string handle index */
};
auto length = pldm_bios_table_attr_value_entry_encode_string_length(3);
EXPECT_EQ(length, stringEntry.size());
std::vector<uint8_t> encodeEntry(length, 0);
pldm_bios_table_attr_value_entry_encode_string(
encodeEntry.data(), encodeEntry.size(), 0, 1, 3, "abc");
EXPECT_EQ(encodeEntry, stringEntry);
EXPECT_DEATH(
pldm_bios_table_attr_value_entry_encode_string(
encodeEntry.data(), encodeEntry.size() - 1, 0, 1, 3, "abc"),
"rc == PLDM_SUCCESS");
auto rc = pldm_bios_table_attr_value_entry_encode_string_check(
encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_STRING, 3, "abc");
EXPECT_EQ(rc, PLDM_SUCCESS);
EXPECT_EQ(encodeEntry, stringEntry);
auto entry = reinterpret_cast<struct pldm_bios_attr_val_table_entry*>(
stringEntry.data());
entry->attr_type = PLDM_BIOS_STRING_READ_ONLY;
rc = pldm_bios_table_attr_value_entry_encode_string_check(
encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_STRING_READ_ONLY,
3, "abc");
EXPECT_EQ(rc, PLDM_SUCCESS);
EXPECT_EQ(encodeEntry, stringEntry);
rc = pldm_bios_table_attr_value_entry_encode_string_check(
encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_PASSWORD, 3,
"abc");
EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
rc = pldm_bios_table_attr_value_entry_encode_string_check(
encodeEntry.data(), encodeEntry.size() - 1, 0, PLDM_BIOS_STRING, 3,
"abc");
EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
}
TEST(AttrValTable, StringEntryDecodeTest)
{
std::vector<uint8_t> stringEntry{
0, 0, /* attr handle */
1, /* attr type */
3, 0, /* current string length */
'a', 'b', 'c', /* defaut value string handle index */
};
auto entry = reinterpret_cast<struct pldm_bios_attr_val_table_entry*>(
stringEntry.data());
auto length = pldm_bios_table_attr_value_entry_string_decode_length(entry);
EXPECT_EQ(3, length);
auto handle = pldm_bios_table_attr_value_entry_decode_handle(entry);
EXPECT_EQ(0, handle);
auto entryLength = pldm_bios_table_attr_value_entry_length(entry);
EXPECT_EQ(stringEntry.size(), entryLength);
variable_field currentString{};
pldm_bios_table_attr_value_entry_string_decode_string(entry,
&currentString);
EXPECT_THAT(std::vector<uint8_t>(currentString.ptr,
currentString.ptr + currentString.length),
ElementsAreArray(std::vector<uint8_t>{'a', 'b', 'c'}));
}
TEST(AttrValTable, integerEntryEncodeTest)
{
std::vector<uint8_t> integerEntry{
0, 0, /* attr handle */
3, /* attr type */
10, 0, 0, 0, 0, 0, 0, 0, /* current value */
};
auto length = pldm_bios_table_attr_value_entry_encode_integer_length();
EXPECT_EQ(length, integerEntry.size());
std::vector<uint8_t> encodeEntry(length, 0);
pldm_bios_table_attr_value_entry_encode_integer(
encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_INTEGER, 10);
EXPECT_EQ(encodeEntry, integerEntry);
EXPECT_DEATH(pldm_bios_table_attr_value_entry_encode_integer(
encodeEntry.data(), encodeEntry.size() - 1, 0,
PLDM_BIOS_INTEGER, 10),
"rc == PLDM_SUCCESS");
auto rc = pldm_bios_table_attr_value_entry_encode_integer_check(
encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_INTEGER, 10);
EXPECT_EQ(rc, PLDM_SUCCESS);
EXPECT_EQ(encodeEntry, integerEntry);
auto entry = reinterpret_cast<struct pldm_bios_attr_val_table_entry*>(
integerEntry.data());
entry->attr_type = PLDM_BIOS_INTEGER_READ_ONLY;
rc = pldm_bios_table_attr_value_entry_encode_integer_check(
encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_INTEGER_READ_ONLY,
10);
EXPECT_EQ(rc, PLDM_SUCCESS);
EXPECT_EQ(encodeEntry, integerEntry);
rc = pldm_bios_table_attr_value_entry_encode_integer_check(
encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_PASSWORD, 10);
EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
rc = pldm_bios_table_attr_value_entry_encode_integer_check(
encodeEntry.data(), encodeEntry.size() - 1, 0,
PLDM_BIOS_INTEGER_READ_ONLY, 10);
EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
}
TEST(AttrValTable, integerEntryDecodeTest)
{
std::vector<uint8_t> integerEntry{
0, 0, /* attr handle */
3, /* attr type */
10, 0, 0, 0, 0, 0, 0, 0, /* current value */
};
auto entry = reinterpret_cast<struct pldm_bios_attr_val_table_entry*>(
integerEntry.data());
auto cv = pldm_bios_table_attr_value_entry_integer_decode_cv(entry);
EXPECT_EQ(cv, 10u);
}
TEST(AttrValTable, IteratorTest)
{
std::vector<uint8_t> enumEntry{
0, 0, /* attr handle */
0, /* attr type */
2, /* number of current value */
0, /* current value string handle index */
1, /* current value string handle index */
};
std::vector<uint8_t> stringEntry{
0, 0, /* attr handle */
1, /* attr type */
3, 0, /* current string length */
'a', 'b', 'c', /* defaut value string handle index */
};
std::vector<uint8_t> integerEntry{
0, 0, /* attr handle */
3, /* attr type */
10, 0, 0, 0, 0, 0, 0, 0, /* current value */
};
Table table;
buildTable(table, enumEntry, stringEntry, integerEntry, enumEntry);
auto iter = pldm_bios_table_iter_create(table.data(), table.size(),
PLDM_BIOS_ATTR_VAL_TABLE);
auto entry = pldm_bios_table_iter_attr_value_entry_value(iter);
auto p = reinterpret_cast<const uint8_t*>(entry);
EXPECT_THAT(std::vector<uint8_t>(p, p + enumEntry.size()),
ElementsAreArray(enumEntry));
pldm_bios_table_iter_next(iter);
entry = pldm_bios_table_iter_attr_value_entry_value(iter);
p = reinterpret_cast<const uint8_t*>(entry);
EXPECT_THAT(std::vector<uint8_t>(p, p + stringEntry.size()),
ElementsAreArray(stringEntry));
pldm_bios_table_iter_next(iter);
entry = pldm_bios_table_iter_attr_value_entry_value(iter);
p = reinterpret_cast<const uint8_t*>(entry);
EXPECT_THAT(std::vector<uint8_t>(p, p + integerEntry.size()),
ElementsAreArray(integerEntry));
pldm_bios_table_iter_next(iter);
entry = pldm_bios_table_iter_attr_value_entry_value(iter);
p = reinterpret_cast<const uint8_t*>(entry);
EXPECT_THAT(std::vector<uint8_t>(p, p + enumEntry.size()),
ElementsAreArray(enumEntry));
pldm_bios_table_iter_next(iter);
EXPECT_TRUE(pldm_bios_table_iter_is_end(iter));
pldm_bios_table_iter_free(iter);
}
TEST(AttrValTable, FindTest)
{
std::vector<uint8_t> enumEntry{
0, 0, /* attr handle */
0, /* attr type */
2, /* number of current value */
0, /* current value string handle index */
1, /* current value string handle index */
};
std::vector<uint8_t> stringEntry{
1, 0, /* attr handle */
1, /* attr type */
3, 0, /* current string length */
'a', 'b', 'c', /* defaut value string handle index */
};
std::vector<uint8_t> integerEntry{
2, 0, /* attr handle */
3, /* attr type */
10, 0, 0, 0, 0, 0, 0, 0, /* current value */
};
Table table;
buildTable(table, enumEntry, stringEntry, integerEntry);
auto entry = pldm_bios_table_attr_value_find_by_handle(table.data(),
table.size(), 1);
EXPECT_NE(entry, nullptr);
auto p = reinterpret_cast<const uint8_t*>(entry);
EXPECT_THAT(std::vector<uint8_t>(p, p + stringEntry.size()),
ElementsAreArray(stringEntry));
entry = pldm_bios_table_attr_value_find_by_handle(table.data(),
table.size(), 3);
EXPECT_EQ(entry, nullptr);
auto firstEntry =
reinterpret_cast<struct pldm_bios_attr_val_table_entry*>(table.data());
firstEntry->attr_type = PLDM_BIOS_PASSWORD;
EXPECT_DEATH(pldm_bios_table_attr_value_find_by_handle(table.data(),
table.size(), 1),
"entry_length != NULL");
}
TEST(AttrValTable, CopyAndUpdateTest)
{
std::vector<uint8_t> enumEntry{
0, 0, /* attr handle */
0, /* attr type */
2, /* number of current value */
0, /* current value string handle index */
1, /* current value string handle index */
};
std::vector<uint8_t> stringEntry{
1, 0, /* attr handle */
1, /* attr type */
3, 0, /* current string length */
'a', 'b', 'c', /* defaut value string handle index */
};
std::vector<uint8_t> integerEntry{
2, 0, /* attr handle */
3, /* attr type */
10, 0, 0, 0, 0, 0, 0, 0, /* current value */
};
Table srcTable;
buildTable(srcTable, enumEntry, stringEntry, integerEntry);
std::vector<uint8_t> stringEntry1{
1, 0, /* attr handle */
1, /* attr type */
3, 0, /* current string length */
'd', 'e', 'f', /* defaut value string handle index */
};
Table expectTable;
buildTable(expectTable, enumEntry, stringEntry1, integerEntry);
Table destTable(expectTable.size() + 10);
auto destLength = destTable.size();
auto rc = pldm_bios_table_attr_value_copy_and_update(
srcTable.data(), srcTable.size(), destTable.data(), &destLength,
stringEntry1.data(), stringEntry1.size());
EXPECT_EQ(rc, PLDM_SUCCESS);
EXPECT_EQ(destLength, expectTable.size());
destTable.resize(destLength);
EXPECT_THAT(destTable, ElementsAreArray(expectTable));
std::vector<uint8_t> stringEntry2{
1, 0, /* attr handle */
1, /* attr type */
5, 0, /* current string length */
'd', 'e', 'f', 'a', 'b', /* defaut value string handle index */
};
expectTable.resize(0);
buildTable(expectTable, enumEntry, stringEntry2, integerEntry);
destTable.resize(expectTable.size() + 10);
destLength = destTable.size();
rc = pldm_bios_table_attr_value_copy_and_update(
srcTable.data(), srcTable.size(), destTable.data(), &destLength,
stringEntry2.data(), stringEntry2.size());
EXPECT_EQ(rc, PLDM_SUCCESS);
EXPECT_EQ(destLength, expectTable.size());
destTable.resize(destLength);
EXPECT_THAT(destTable, ElementsAreArray(expectTable));
std::vector<uint8_t> stringEntry3{
1, 0, /* attr handle */
1, /* attr type */
1, 0, /* current string length */
'd', /* defaut value string handle index */
};
expectTable.resize(0);
buildTable(expectTable, enumEntry, stringEntry3, integerEntry);
destTable.resize(expectTable.size() + 10);
destLength = destTable.size();
rc = pldm_bios_table_attr_value_copy_and_update(
srcTable.data(), srcTable.size(), destTable.data(), &destLength,
stringEntry3.data(), stringEntry3.size());
EXPECT_EQ(rc, PLDM_SUCCESS);
EXPECT_EQ(destLength, expectTable.size());
destTable.resize(destLength);
EXPECT_THAT(destTable, ElementsAreArray(expectTable));
stringEntry3[2] = PLDM_BIOS_INTEGER; // set attribute type to integer
rc = pldm_bios_table_attr_value_copy_and_update(
srcTable.data(), srcTable.size(), destTable.data(), &destLength,
stringEntry3.data(), stringEntry3.size());
EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
stringEntry3[2] = PLDM_BIOS_STRING; // set attribute type to string
destTable.resize(expectTable.size() - 1);
destLength = destTable.size();
rc = pldm_bios_table_attr_value_copy_and_update(
srcTable.data(), srcTable.size(), destTable.data(), &destLength,
stringEntry3.data(), stringEntry3.size());
EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
}
TEST(StringTable, EntryEncodeTest)
{
std::vector<uint8_t> stringEntry{
0, 0, /* string handle*/
7, 0, /* string length */
'A', 'l', 'l', 'o', 'w', 'e', 'd', /* string */
};
const char* str = "Allowed";
auto str_length = std::strlen(str);
auto encodeLength = pldm_bios_table_string_entry_encode_length(str_length);
EXPECT_EQ(encodeLength, stringEntry.size());
std::vector<uint8_t> encodeEntry(encodeLength, 0);
ASSERT_EQ(pldm_bios_table_string_entry_encode_check(
encodeEntry.data(), encodeEntry.size(), str, str_length),
PLDM_SUCCESS);
// set string handle = 0
encodeEntry[0] = 0;
encodeEntry[1] = 0;
EXPECT_EQ(stringEntry, encodeEntry);
EXPECT_NE(pldm_bios_table_string_entry_encode_check(
encodeEntry.data(), encodeEntry.size() - 1, str, str_length),
PLDM_SUCCESS);
auto rc = pldm_bios_table_string_entry_encode_check(
encodeEntry.data(), encodeEntry.size() - 1, str, str_length);
EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
}
TEST(StringTable, EntryDecodeTest)
{
std::vector<uint8_t> stringEntry{
4, 0, /* string handle*/
7, 0, /* string length */
'A', 'l', 'l', 'o', 'w', 'e', 'd', /* string */
};
auto entry = reinterpret_cast<struct pldm_bios_string_table_entry*>(
stringEntry.data());
auto handle = pldm_bios_table_string_entry_decode_handle(entry);
EXPECT_EQ(handle, 4);
auto strLength = pldm_bios_table_string_entry_decode_string_length(entry);
EXPECT_EQ(strLength, 7);
std::vector<char> buffer(strLength + 1, 0);
pldm_bios_table_string_entry_decode_string_check(entry, buffer.data(),
buffer.size());
EXPECT_EQ(strlen(buffer.data()), strLength);
EXPECT_EQ(std::strcmp("Allowed", buffer.data()), 0);
EXPECT_EQ(pldm_bios_table_string_entry_decode_string_check(
entry, buffer.data(), 2 + 1 /* sizeof '\0'*/),
PLDM_SUCCESS);
EXPECT_EQ(strlen(buffer.data()), 2);
EXPECT_EQ(std::strcmp("Al", buffer.data()), 0);
auto rc = pldm_bios_table_string_entry_decode_string_check(
entry, buffer.data(), buffer.size());
EXPECT_EQ(rc, PLDM_SUCCESS);
EXPECT_EQ(std::strcmp("Allowed", buffer.data()), 0);
/* Ensure equivalence with the unchecked API */
rc = pldm_bios_table_string_entry_decode_string_check(
entry, buffer.data(), 2 + 1 /* sizeof '\0' */);
EXPECT_EQ(rc, std::strcmp("Al", buffer.data()));
}
TEST(StringTable, IteratorTest)
{
std::vector<uint8_t> stringHello{
0, 0, /* string handle*/
5, 0, /* string length */
'H', 'e', 'l', 'l', 'o', /* string */
};
std::vector<uint8_t> stringWorld{
1, 0, /* string handle*/
6, 0, /* string length */
'W', 'o', 'r', 'l', 'd', '!', /* string */
};
Table table;
buildTable(table, stringHello, stringWorld);
auto iter = pldm_bios_table_iter_create(table.data(), table.size(),
PLDM_BIOS_STRING_TABLE);
auto entry = pldm_bios_table_iter_string_entry_value(iter);
auto rc = std::memcmp(entry, stringHello.data(), stringHello.size());
EXPECT_EQ(rc, 0);
pldm_bios_table_iter_next(iter);
entry = pldm_bios_table_iter_string_entry_value(iter);
rc = std::memcmp(entry, stringWorld.data(), stringWorld.size());
EXPECT_EQ(rc, 0);
pldm_bios_table_iter_next(iter);
EXPECT_TRUE(pldm_bios_table_iter_is_end(iter));
pldm_bios_table_iter_free(iter);
}
TEST(StringTable, FindTest)
{
std::vector<uint8_t> stringHello{
1, 0, /* string handle*/
5, 0, /* string length */
'H', 'e', 'l', 'l', 'o', /* string */
};
std::vector<uint8_t> stringWorld{
2, 0, /* string handle*/
6, 0, /* string length */
'W', 'o', 'r', 'l', 'd', '!', /* string */
};
std::vector<uint8_t> stringHi{
3, 0, /* string handle*/
2, 0, /* string length */
'H', 'i', /* string */
};
Table table;
buildTable(table, stringHello, stringWorld, stringHi);
auto entry = pldm_bios_table_string_find_by_string(table.data(),
table.size(), "World!");
EXPECT_NE(entry, nullptr);
auto handle = pldm_bios_table_string_entry_decode_handle(entry);
EXPECT_EQ(handle, 2);
entry = pldm_bios_table_string_find_by_string(table.data(), table.size(),
"Worl");
EXPECT_EQ(entry, nullptr);
entry =
pldm_bios_table_string_find_by_handle(table.data(), table.size(), 3);
EXPECT_NE(entry, nullptr);
auto str_length = pldm_bios_table_string_entry_decode_string_length(entry);
EXPECT_EQ(str_length, 2);
std::vector<char> strBuf(str_length + 1, 0);
auto rc = pldm_bios_table_string_entry_decode_string_check(
entry, strBuf.data(), strBuf.size());
EXPECT_EQ(rc, PLDM_SUCCESS);
EXPECT_EQ(std::strcmp("Hi", strBuf.data()), 0);
entry =
pldm_bios_table_string_find_by_handle(table.data(), table.size(), 4);
EXPECT_EQ(entry, nullptr);
}
TEST(Iterator, DeathTest)
{
Table table(256, 0);
/* first entry */
auto attr_entry =
reinterpret_cast<struct pldm_bios_attr_table_entry*>(table.data());
auto iter = pldm_bios_table_iter_create(table.data(), table.size(),
PLDM_BIOS_ATTR_TABLE);
attr_entry->attr_type = PLDM_BIOS_PASSWORD;
EXPECT_DEATH(pldm_bios_table_iter_next(iter), "attr_table_entry != NULL");
pldm_bios_table_iter_free(iter);
}
TEST(PadAndChecksum, PadAndChecksum)
{
EXPECT_EQ(4u, pldm_bios_table_pad_checksum_size(0));
EXPECT_EQ(7u, pldm_bios_table_pad_checksum_size(1));
EXPECT_EQ(6u, pldm_bios_table_pad_checksum_size(2));
EXPECT_EQ(5u, pldm_bios_table_pad_checksum_size(3));
EXPECT_EQ(4u, pldm_bios_table_pad_checksum_size(4));
// The table is borrowed from
// https://github.com/openbmc/pldm/commit/69d3e7fb2d9935773f4fbf44326c33f3fc0a3c38
// refer to the commit message
Table attrValTable = {0x09, 0x00, 0x01, 0x02, 0x00, 0x65, 0x66};
auto sizeWithoutPad = attrValTable.size();
attrValTable.resize(sizeWithoutPad +
pldm_bios_table_pad_checksum_size(sizeWithoutPad));
pldm_bios_table_append_pad_checksum(attrValTable.data(),
attrValTable.size(), sizeWithoutPad);
Table expectedTable = {0x09, 0x00, 0x01, 0x02, 0x00, 0x65,
0x66, 0x00, 0x6d, 0x81, 0x4a, 0xb6};
EXPECT_EQ(attrValTable, expectedTable);
}
TEST(BIOSTableChecksum, testBIOSTableChecksum)
{
std::vector<uint8_t> stringTable{
1, 0, /* string handle*/
5, 0, /* string length */
'T', 'a', 'b', 'l', 'e', /* string */
};
buildTable(stringTable);
EXPECT_EQ(true,
pldm_bios_table_checksum(stringTable.data(), stringTable.size()));
}