blob: 08c8ea71b954f674704a2eebde002e5b14e498cf [file] [log] [blame]
Tom Joseph53279882021-04-28 06:29:13 -07001#include "common/test/mocked_utils.hpp"
John Wang29683b52020-02-27 16:41:44 +08002#include "libpldmresponder/bios_string_attribute.hpp"
3#include "mocked_bios.hpp"
John Wang29683b52020-02-27 16:41:44 +08004
John Wang29683b52020-02-27 16:41:44 +08005#include <nlohmann/json.hpp>
6
George Liu6492f522020-06-16 10:34:05 +08007#include <memory>
8
John Wang29683b52020-02-27 16:41:44 +08009#include <gmock/gmock.h>
10#include <gtest/gtest.h>
11
12using namespace pldm::responder::bios;
Brad Bishop5079ac42021-08-19 18:35:06 -040013using namespace pldm::utils;
John Wang29683b52020-02-27 16:41:44 +080014using ::testing::_;
15using ::testing::ElementsAreArray;
16using ::testing::Return;
17using ::testing::StrEq;
18using ::testing::Throw;
19
20class TestBIOSStringAttribute : public ::testing::Test
21{
22 public:
23 const auto& getStringInfo(const BIOSStringAttribute& biosStringAttribute)
24 {
25 return biosStringAttribute.stringInfo;
26 }
27};
28
29TEST_F(TestBIOSStringAttribute, CtorTest)
30{
31 auto jsonStringReadOnly = R"( {
32 "attribute_name" : "str_example3",
33 "string_type" : "ASCII",
34 "minimum_string_length" : 1,
35 "maximum_string_length" : 100,
George Liudaa69232020-09-02 17:22:09 +080036 "default_string" : "ef",
Archana Kakanid6608092024-05-23 01:14:56 -050037 "read_only" : true,
38 "help_text" : "HelpText",
39 "display_name" : "DisplayName"
John Wang29683b52020-02-27 16:41:44 +080040 })"_json;
41 BIOSStringAttribute stringReadOnly{jsonStringReadOnly, nullptr};
42 EXPECT_EQ(stringReadOnly.name, "str_example3");
43 EXPECT_TRUE(stringReadOnly.readOnly);
44
45 auto& stringInfo = getStringInfo(stringReadOnly);
46 EXPECT_EQ(stringInfo.stringType,
47 static_cast<uint8_t>(BIOSStringAttribute::Encoding::ASCII));
48 EXPECT_EQ(stringInfo.minLength, 1);
49 EXPECT_EQ(stringInfo.maxLength, 100);
50 EXPECT_EQ(stringInfo.defLength, 2);
51 EXPECT_EQ(stringInfo.defString, "ef");
52
John Wang29683b52020-02-27 16:41:44 +080053 auto jsonStringReadWrite = R"({
54 "attribute_name" : "str_example1",
55 "string_type" : "ASCII",
56 "minimum_string_length" : 1,
57 "maximum_string_length" : 100,
John Wang29683b52020-02-27 16:41:44 +080058 "default_string" : "abc",
Archana Kakanid6608092024-05-23 01:14:56 -050059 "read_only" : false,
60 "help_text" : "HelpText",
61 "display_name" : "DisplayName",
John Wang29683b52020-02-27 16:41:44 +080062 "dbus" : {
63 "object_path" : "/xyz/abc/def",
64 "interface" : "xyz.openbmc_project.str_example1.value",
65 "property_name" : "Str_example1",
66 "property_type" : "string"
67 }
68 })"_json;
69 BIOSStringAttribute stringReadWrite{jsonStringReadWrite, nullptr};
70
71 EXPECT_EQ(stringReadWrite.name, "str_example1");
72 EXPECT_TRUE(!stringReadWrite.readOnly);
73}
74
75TEST_F(TestBIOSStringAttribute, ConstructEntry)
76{
77 MockBIOSStringTable biosStringTable;
78 MockdBusHandler dbusHandler;
79
80 auto jsonStringReadOnly = R"({
81 "attribute_name" : "str_example1",
82 "string_type" : "ASCII",
83 "minimum_string_length" : 1,
84 "maximum_string_length" : 100,
George Liudaa69232020-09-02 17:22:09 +080085 "default_string" : "abc",
Archana Kakanid6608092024-05-23 01:14:56 -050086 "read_only" : true,
87 "help_text" : "HelpText",
88 "display_name" : "DisplayName"
John Wang29683b52020-02-27 16:41:44 +080089 })"_json;
90
91 std::vector<uint8_t> expectedAttrEntry{
92 0, 0, /* attr handle */
93 0x81, /* attr type string read-only */
94 5, 0, /* attr name handle */
95 1, /* string type */
96 1, 0, /* minimum length of the string in bytes */
97 100, 0, /* maximum length of the string in bytes */
98 3, 0, /* length of default string in length */
99 'a', 'b', 'c' /* default string */
100 };
101
102 std::vector<uint8_t> expectedAttrValueEntry{
103 0, 0, /* attr handle */
104 0x81, /* attr type string read-only */
105 3, 0, /* current string length */
Manojkiran Eda2576aec2024-06-17 12:05:17 +0530106 'a', 'b', 'c', /* default value string handle index */
John Wang29683b52020-02-27 16:41:44 +0800107 };
108
109 ON_CALL(biosStringTable, findHandle(StrEq("str_example1")))
110 .WillByDefault(Return(5));
111 BIOSStringAttribute stringReadOnly{jsonStringReadOnly, nullptr};
112
113 checkConstructEntry(stringReadOnly, biosStringTable, expectedAttrEntry,
114 expectedAttrValueEntry);
115
116 auto jsonStringReadWrite = R"({
117 "attribute_name" : "str_example1",
118 "string_type" : "ASCII",
119 "minimum_string_length" : 1,
120 "maximum_string_length" : 100,
John Wang29683b52020-02-27 16:41:44 +0800121 "default_string" : "abc",
Archana Kakanid6608092024-05-23 01:14:56 -0500122 "read_only" : false,
123 "help_text" : "HelpText",
124 "display_name" : "DisplayName",
John Wang29683b52020-02-27 16:41:44 +0800125 "dbus" : {
126 "object_path" : "/xyz/abc/def",
127 "interface" : "xyz.openbmc_project.str_example1.value",
128 "property_name" : "Str_example1",
129 "property_type" : "string"
130 }
131 })"_json;
132 BIOSStringAttribute stringReadWrite{jsonStringReadWrite, &dbusHandler};
133
134 /* Set expected attr type to read-write */
135 expectedAttrEntry[2] = PLDM_BIOS_STRING;
136 expectedAttrValueEntry[2] = PLDM_BIOS_STRING;
137
138 EXPECT_CALL(
139 dbusHandler,
140 getDbusPropertyVariant(StrEq("/xyz/abc/def"), StrEq("Str_example1"),
141 StrEq("xyz.openbmc_project.str_example1.value")))
142 .WillOnce(Throw(std::exception()));
143
144 checkConstructEntry(stringReadWrite, biosStringTable, expectedAttrEntry,
145 expectedAttrValueEntry);
146
147 EXPECT_CALL(
148 dbusHandler,
149 getDbusPropertyVariant(StrEq("/xyz/abc/def"), StrEq("Str_example1"),
150 StrEq("xyz.openbmc_project.str_example1.value")))
151 .WillOnce(Return(PropertyValue(std::string("abcd"))));
152
153 expectedAttrValueEntry = {
154 0, 0, /* attr handle */
155 1, /* attr type string read-write */
156 4, 0, /* current string length */
Manojkiran Eda2576aec2024-06-17 12:05:17 +0530157 'a', 'b', 'c', 'd', /* default value string handle index */
John Wang29683b52020-02-27 16:41:44 +0800158 };
159
160 checkConstructEntry(stringReadWrite, biosStringTable, expectedAttrEntry,
161 expectedAttrValueEntry);
162}
163
164TEST_F(TestBIOSStringAttribute, setAttrValueOnDbus)
165{
166 auto jsonStringReadWrite = R"({
167 "attribute_name" : "str_example1",
168 "string_type" : "ASCII",
169 "minimum_string_length" : 1,
170 "maximum_string_length" : 100,
John Wang29683b52020-02-27 16:41:44 +0800171 "default_string" : "abc",
Archana Kakanid6608092024-05-23 01:14:56 -0500172 "read_only" : false,
173 "help_text" : "HelpText",
174 "display_name" : "DisplayName",
John Wang29683b52020-02-27 16:41:44 +0800175 "dbus" : {
176 "object_path" : "/xyz/abc/def",
177 "interface" : "xyz.openbmc_project.str_example1.value",
178 "property_name" : "Str_example1",
179 "property_type" : "string"
180 }
181 })"_json;
182
183 MockdBusHandler dbusHandler;
184 MockBIOSStringTable biosStringTable;
185
186 BIOSStringAttribute stringReadWrite{jsonStringReadWrite, &dbusHandler};
187 DBusMapping dbusMapping{"/xyz/abc/def",
188 "xyz.openbmc_project.str_example1.value",
189 "Str_example1", "string"};
190 std::vector<uint8_t> attrValueEntry{
191 0, 0, /* attr handle */
192 1, /* attr type string read-write */
193 4, 0, /* current string length */
Manojkiran Eda2576aec2024-06-17 12:05:17 +0530194 'a', 'b', 'c', 'd', /* default value string handle index */
John Wang29683b52020-02-27 16:41:44 +0800195 };
196 auto entry = reinterpret_cast<pldm_bios_attr_val_table_entry*>(
197 attrValueEntry.data());
198 PropertyValue value = std::string("abcd");
199 EXPECT_CALL(dbusHandler, setDbusProperty(dbusMapping, value)).Times(1);
200 stringReadWrite.setAttrValueOnDbus(entry, nullptr, biosStringTable);
201}