libpldm: bios_table: Update attr value table
SetBIOSAttributeCurrentValue will update an attribute
in attribute value table. And in our implementation,
all attributes are stored continuously in the table.
It may be more appropriate to build a new table with the
original table and the attributes to be updated, since
the length of each attribute could be different.
Signed-off-by: John Wang <wangzqbj@inspur.com>
Change-Id: I0053cb81cc8f279c1d0e3a4505220a0e1e57ffe7
diff --git a/test/libpldm_bios_table_test.cpp b/test/libpldm_bios_table_test.cpp
index f1e0e56..5ab1dfe 100644
--- a/test/libpldm_bios_table_test.cpp
+++ b/test/libpldm_bios_table_test.cpp
@@ -1,3 +1,4 @@
+#include <endian.h>
#include <string.h>
#include <cstring>
@@ -7,6 +8,7 @@
#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>
@@ -18,7 +20,11 @@
{
auto padSize = ((table.size() % 4) ? (4 - table.size() % 4) : 0);
table.insert(table.end(), padSize, 0);
- table.insert(table.end(), sizeof(uint32_t) /*checksum*/, 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>
@@ -667,6 +673,94 @@
"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));
+
+ 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{