bios: Implement BIOSStringAttribute

Implement BIOSStringAttribute, most of the code is copied from
bios/bios_parser.cpp.

Implement SetAttrValueOnDbus and constructEntry for string attribute

Signed-off-by: John Wang <wangzqbj@inspur.com>
Change-Id: Ic7c6b35d32738d698d7649f97cb7843606b8a2ba
diff --git a/test/libpldmresponder_bios_string_attribute_test.cpp b/test/libpldmresponder_bios_string_attribute_test.cpp
new file mode 100644
index 0000000..747e60f
--- /dev/null
+++ b/test/libpldmresponder_bios_string_attribute_test.cpp
@@ -0,0 +1,200 @@
+#include "libpldmresponder/bios_string_attribute.hpp"
+#include "mocked_bios.hpp"
+#include "mocked_utils.hpp"
+
+#include <memory>
+#include <nlohmann/json.hpp>
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+using namespace pldm::responder::bios;
+using ::testing::_;
+using ::testing::ElementsAreArray;
+using ::testing::Return;
+using ::testing::StrEq;
+using ::testing::Throw;
+
+class TestBIOSStringAttribute : public ::testing::Test
+{
+  public:
+    const auto& getStringInfo(const BIOSStringAttribute& biosStringAttribute)
+    {
+        return biosStringAttribute.stringInfo;
+    }
+};
+
+TEST_F(TestBIOSStringAttribute, CtorTest)
+{
+    auto jsonStringReadOnly = R"(  {
+            "attribute_name" : "str_example3",
+            "string_type" : "ASCII",
+            "minimum_string_length" : 1,
+            "maximum_string_length" : 100,
+            "default_string_length" : 2,
+            "default_string" : "ef"
+        })"_json;
+    BIOSStringAttribute stringReadOnly{jsonStringReadOnly, nullptr};
+    EXPECT_EQ(stringReadOnly.name, "str_example3");
+    EXPECT_TRUE(stringReadOnly.readOnly);
+
+    auto& stringInfo = getStringInfo(stringReadOnly);
+    EXPECT_EQ(stringInfo.stringType,
+              static_cast<uint8_t>(BIOSStringAttribute::Encoding::ASCII));
+    EXPECT_EQ(stringInfo.minLength, 1);
+    EXPECT_EQ(stringInfo.maxLength, 100);
+    EXPECT_EQ(stringInfo.defLength, 2);
+    EXPECT_EQ(stringInfo.defString, "ef");
+
+    auto jsonStringReadOnlyError = R"(  {
+            "attribute_name" : "str_example3",
+            "string_type" : "ASCII",
+            "minimum_string_length" : 1,
+            "maximum_string_length" : 100,
+            "default_string" : "ef"
+        })"_json; // missing default_string_length
+
+    EXPECT_THROW((BIOSStringAttribute{jsonStringReadOnlyError, nullptr}),
+                 Json::exception);
+
+    auto jsonStringReadWrite = R"({
+            "attribute_name" : "str_example1",
+            "string_type" : "ASCII",
+            "minimum_string_length" : 1,
+            "maximum_string_length" : 100,
+            "default_string_length" : 3,
+            "default_string" : "abc",
+            "dbus" : {
+                "object_path" : "/xyz/abc/def",
+                "interface" : "xyz.openbmc_project.str_example1.value",
+                "property_name" : "Str_example1",
+                "property_type" : "string"
+            }
+        })"_json;
+    BIOSStringAttribute stringReadWrite{jsonStringReadWrite, nullptr};
+
+    EXPECT_EQ(stringReadWrite.name, "str_example1");
+    EXPECT_TRUE(!stringReadWrite.readOnly);
+}
+
+TEST_F(TestBIOSStringAttribute, ConstructEntry)
+{
+    MockBIOSStringTable biosStringTable;
+    MockdBusHandler dbusHandler;
+
+    auto jsonStringReadOnly = R"({
+            "attribute_name" : "str_example1",
+            "string_type" : "ASCII",
+            "minimum_string_length" : 1,
+            "maximum_string_length" : 100,
+            "default_string_length" : 3,
+            "default_string" : "abc"
+        })"_json;
+
+    std::vector<uint8_t> expectedAttrEntry{
+        0,    0,       /* attr handle */
+        0x81,          /* attr type string read-only */
+        5,    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> expectedAttrValueEntry{
+        0,    0,        /* attr handle */
+        0x81,           /* attr type string read-only */
+        3,    0,        /* current string length */
+        'a',  'b', 'c', /* defaut value string handle index */
+    };
+
+    ON_CALL(biosStringTable, findHandle(StrEq("str_example1")))
+        .WillByDefault(Return(5));
+    BIOSStringAttribute stringReadOnly{jsonStringReadOnly, nullptr};
+
+    checkConstructEntry(stringReadOnly, biosStringTable, expectedAttrEntry,
+                        expectedAttrValueEntry);
+
+    auto jsonStringReadWrite = R"({
+            "attribute_name" : "str_example1",
+            "string_type" : "ASCII",
+            "minimum_string_length" : 1,
+            "maximum_string_length" : 100,
+            "default_string_length" : 3,
+            "default_string" : "abc",
+            "dbus" : {
+                "object_path" : "/xyz/abc/def",
+                "interface" : "xyz.openbmc_project.str_example1.value",
+                "property_name" : "Str_example1",
+                "property_type" : "string"
+            }
+        })"_json;
+    BIOSStringAttribute stringReadWrite{jsonStringReadWrite, &dbusHandler};
+
+    /* Set expected attr type to read-write */
+    expectedAttrEntry[2] = PLDM_BIOS_STRING;
+    expectedAttrValueEntry[2] = PLDM_BIOS_STRING;
+
+    EXPECT_CALL(
+        dbusHandler,
+        getDbusPropertyVariant(StrEq("/xyz/abc/def"), StrEq("Str_example1"),
+                               StrEq("xyz.openbmc_project.str_example1.value")))
+        .WillOnce(Throw(std::exception()));
+
+    checkConstructEntry(stringReadWrite, biosStringTable, expectedAttrEntry,
+                        expectedAttrValueEntry);
+
+    EXPECT_CALL(
+        dbusHandler,
+        getDbusPropertyVariant(StrEq("/xyz/abc/def"), StrEq("Str_example1"),
+                               StrEq("xyz.openbmc_project.str_example1.value")))
+        .WillOnce(Return(PropertyValue(std::string("abcd"))));
+
+    expectedAttrValueEntry = {
+        0,   0,             /* attr handle */
+        1,                  /* attr type string read-write */
+        4,   0,             /* current string length */
+        'a', 'b', 'c', 'd', /* defaut value string handle index */
+    };
+
+    checkConstructEntry(stringReadWrite, biosStringTable, expectedAttrEntry,
+                        expectedAttrValueEntry);
+}
+
+TEST_F(TestBIOSStringAttribute, setAttrValueOnDbus)
+{
+    auto jsonStringReadWrite = R"({
+            "attribute_name" : "str_example1",
+            "string_type" : "ASCII",
+            "minimum_string_length" : 1,
+            "maximum_string_length" : 100,
+            "default_string_length" : 3,
+            "default_string" : "abc",
+            "dbus" : {
+                "object_path" : "/xyz/abc/def",
+                "interface" : "xyz.openbmc_project.str_example1.value",
+                "property_name" : "Str_example1",
+                "property_type" : "string"
+            }
+        })"_json;
+
+    MockdBusHandler dbusHandler;
+    MockBIOSStringTable biosStringTable;
+
+    BIOSStringAttribute stringReadWrite{jsonStringReadWrite, &dbusHandler};
+    DBusMapping dbusMapping{"/xyz/abc/def",
+                            "xyz.openbmc_project.str_example1.value",
+                            "Str_example1", "string"};
+    std::vector<uint8_t> attrValueEntry{
+        0,   0,             /* attr handle */
+        1,                  /* attr type string read-write */
+        4,   0,             /* current string length */
+        'a', 'b', 'c', 'd', /* defaut value string handle index */
+    };
+    auto entry = reinterpret_cast<pldm_bios_attr_val_table_entry*>(
+        attrValueEntry.data());
+    PropertyValue value = std::string("abcd");
+    EXPECT_CALL(dbusHandler, setDbusProperty(dbusMapping, value)).Times(1);
+    stringReadWrite.setAttrValueOnDbus(entry, nullptr, biosStringTable);
+}
diff --git a/test/meson.build b/test/meson.build
index a7bd88f..827f9fc 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -20,6 +20,7 @@
   'libpldmresponder_base_test',
   'libpldmresponder_bios_test',
   'libpldmresponder_bios_attribute_test',
+  'libpldmresponder_bios_string_attribute_test',
   'libpldmresponder_pdr_state_effecter_test',
   'libpldmresponder_bios_table_test',
   'libpldmresponder_platform_test',
diff --git a/test/mocked_bios.hpp b/test/mocked_bios.hpp
new file mode 100644
index 0000000..eb95c33
--- /dev/null
+++ b/test/mocked_bios.hpp
@@ -0,0 +1,57 @@
+#include "libpldmresponder/bios_table.hpp"
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+using testing::ElementsAreArray;
+using namespace pldm::responder::bios;
+
+class MockBIOSStringTable : public BIOSStringTable
+{
+  public:
+    MockBIOSStringTable() : BIOSStringTable({})
+    {
+    }
+
+    MOCK_METHOD(uint16_t, findHandle, (const std::string&), (const override));
+};
+
+void checkHeader(const Table& attrEntry, const Table& attrValueEntry)
+{
+    auto attrHeader = table::attribute::decodeHeader(
+        reinterpret_cast<const pldm_bios_attr_table_entry*>(attrEntry.data()));
+    auto attrValueHeader = table::attribute_value::decodeHeader(
+        reinterpret_cast<const pldm_bios_attr_val_table_entry*>(
+            attrValueEntry.data()));
+
+    EXPECT_EQ(attrHeader.attrHandle, attrValueHeader.attrHandle);
+}
+
+void checkEntry(Table& entry, Table& expectedEntry)
+{
+    /** backup the attr handle */
+    auto attr0 = entry[0], eAttr0 = expectedEntry[0];
+    auto attr1 = entry[1], eAttr1 = expectedEntry[1];
+
+    /** attr handle is computed by libpldm, set it to 0 to test */
+    entry[0] = 0, expectedEntry[0] = 0;
+    entry[1] = 0, expectedEntry[1] = 0;
+
+    EXPECT_THAT(entry, ElementsAreArray(expectedEntry));
+
+    /** restore the attr handle */
+    entry[0] = attr0, expectedEntry[0] = eAttr0;
+    entry[1] = attr1, expectedEntry[1] = eAttr1;
+}
+
+void checkConstructEntry(BIOSAttribute& attribute, BIOSStringTable& stringTable,
+                         Table& expectedAttrEntry,
+                         Table& expectedAttrValueEntry)
+{
+    Table attrEntry, attrValueEntry;
+    attribute.constructEntry(stringTable, attrEntry, attrValueEntry);
+
+    checkHeader(attrEntry, attrValueEntry);
+    checkEntry(attrEntry, expectedAttrEntry);
+    checkEntry(attrValueEntry, expectedAttrValueEntry);
+}
\ No newline at end of file
diff --git a/test/mocked_utils.hpp b/test/mocked_utils.hpp
index 39ac8bb..0813830 100644
--- a/test/mocked_utils.hpp
+++ b/test/mocked_utils.hpp
@@ -1,5 +1,3 @@
-#pragma once
-
 #include "utils.hpp"
 
 #include <gmock/gmock.h>
@@ -32,4 +30,7 @@
   public:
     MOCK_METHOD(void, setDbusProperty,
                 (const DBusMapping&, const PropertyValue&), (const override));
+
+    MOCK_METHOD(PropertyValue, getDbusPropertyVariant,
+                (const char*, const char*, const char*), (const override));
 };