bios: Add en/decodes for integer attribute

Add encode/decode functions for integer attribute to
construct attribute table and attribute value table

Signed-off-by: John Wang <wangzqbj@inspur.com>
Change-Id: I1b61e33c169ea9f78864f9919af0f09fb5ffe5e0
diff --git a/test/libpldm_bios_table_test.cpp b/test/libpldm_bios_table_test.cpp
index a07036a..d22f61c 100644
--- a/test/libpldm_bios_table_test.cpp
+++ b/test/libpldm_bios_table_test.cpp
@@ -269,6 +269,65 @@
     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);
+    pldm_bios_table_attr_entry_integer_encode(encodeEntry.data(),
+                                              encodeEntry.size(), &info);
+    // set attr handle = 0
+    encodeEntry[0] = 0;
+    encodeEntry[1] = 0;
+
+    EXPECT_EQ(integerEntry, encodeEntry);
+
+    EXPECT_DEATH(pldm_bios_table_attr_entry_integer_encode(
+                     encodeEntry.data(), encodeEntry.size() - 1, &info),
+                 "length <= entry_length");
+
+    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;
+    rc = pldm_bios_table_attr_entry_integer_encode_check(
+        encodeEntry.data(), encodeEntry.size(), &info);
+    EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+}
+
 TEST(AttrTable, ItearatorTest)
 {
     std::vector<uint8_t> enumEntry{
@@ -291,9 +350,18 @@
         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, enumEntry);
+    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);
@@ -307,6 +375,11 @@
 
     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);
 
@@ -404,6 +477,48 @@
     EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
 }
 
+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),
+                 "length <= entry_length");
+
+    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(StringTable, EntryEncodeTest)
 {
     std::vector<uint8_t> stringEntry{
@@ -558,7 +673,5 @@
                                             PLDM_BIOS_ATTR_TABLE);
     attr_entry->attr_type = PLDM_BIOS_PASSWORD;
     EXPECT_DEATH(pldm_bios_table_iter_next(iter), "attr_table_entry != NULL");
-    attr_entry->attr_type = PLDM_BIOS_INTEGER;
-    EXPECT_DEATH(pldm_bios_table_iter_next(iter), "attr_table_entry != NULL");
     pldm_bios_table_iter_free(iter);
 }