blob: c945a68a81ce92bfac5d42b8af714d53eb4f3c1a [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,
36 "default_string_length" : 2,
George Liudaa69232020-09-02 17:22:09 +080037 "default_string" : "ef",
George Liu92bb4022020-09-03 14:58:24 +080038 "readOnly" : true,
39 "helpText" : "HelpText",
40 "displayName" : "DisplayName"
John Wang29683b52020-02-27 16:41:44 +080041 })"_json;
42 BIOSStringAttribute stringReadOnly{jsonStringReadOnly, nullptr};
43 EXPECT_EQ(stringReadOnly.name, "str_example3");
44 EXPECT_TRUE(stringReadOnly.readOnly);
45
46 auto& stringInfo = getStringInfo(stringReadOnly);
47 EXPECT_EQ(stringInfo.stringType,
48 static_cast<uint8_t>(BIOSStringAttribute::Encoding::ASCII));
49 EXPECT_EQ(stringInfo.minLength, 1);
50 EXPECT_EQ(stringInfo.maxLength, 100);
51 EXPECT_EQ(stringInfo.defLength, 2);
52 EXPECT_EQ(stringInfo.defString, "ef");
53
54 auto jsonStringReadOnlyError = R"( {
55 "attribute_name" : "str_example3",
56 "string_type" : "ASCII",
57 "minimum_string_length" : 1,
58 "maximum_string_length" : 100,
George Liu92bb4022020-09-03 14:58:24 +080059 "default_string" : "ef",
60 "helpText" : "HelpText",
61 "displayName" : "DisplayName"
John Wang29683b52020-02-27 16:41:44 +080062 })"_json; // missing default_string_length
63
64 EXPECT_THROW((BIOSStringAttribute{jsonStringReadOnlyError, nullptr}),
65 Json::exception);
66
67 auto jsonStringReadWrite = R"({
68 "attribute_name" : "str_example1",
69 "string_type" : "ASCII",
70 "minimum_string_length" : 1,
71 "maximum_string_length" : 100,
72 "default_string_length" : 3,
73 "default_string" : "abc",
George Liudaa69232020-09-02 17:22:09 +080074 "readOnly" : false,
George Liu92bb4022020-09-03 14:58:24 +080075 "helpText" : "HelpText",
76 "displayName" : "DisplayName",
John Wang29683b52020-02-27 16:41:44 +080077 "dbus" : {
78 "object_path" : "/xyz/abc/def",
79 "interface" : "xyz.openbmc_project.str_example1.value",
80 "property_name" : "Str_example1",
81 "property_type" : "string"
82 }
83 })"_json;
84 BIOSStringAttribute stringReadWrite{jsonStringReadWrite, nullptr};
85
86 EXPECT_EQ(stringReadWrite.name, "str_example1");
87 EXPECT_TRUE(!stringReadWrite.readOnly);
88}
89
90TEST_F(TestBIOSStringAttribute, ConstructEntry)
91{
92 MockBIOSStringTable biosStringTable;
93 MockdBusHandler dbusHandler;
94
95 auto jsonStringReadOnly = R"({
96 "attribute_name" : "str_example1",
97 "string_type" : "ASCII",
98 "minimum_string_length" : 1,
99 "maximum_string_length" : 100,
100 "default_string_length" : 3,
George Liudaa69232020-09-02 17:22:09 +0800101 "default_string" : "abc",
George Liu92bb4022020-09-03 14:58:24 +0800102 "readOnly" : true,
103 "helpText" : "HelpText",
104 "displayName" : "DisplayName"
John Wang29683b52020-02-27 16:41:44 +0800105 })"_json;
106
107 std::vector<uint8_t> expectedAttrEntry{
108 0, 0, /* attr handle */
109 0x81, /* attr type string read-only */
110 5, 0, /* attr name handle */
111 1, /* string type */
112 1, 0, /* minimum length of the string in bytes */
113 100, 0, /* maximum length of the string in bytes */
114 3, 0, /* length of default string in length */
115 'a', 'b', 'c' /* default string */
116 };
117
118 std::vector<uint8_t> expectedAttrValueEntry{
119 0, 0, /* attr handle */
120 0x81, /* attr type string read-only */
121 3, 0, /* current string length */
122 'a', 'b', 'c', /* defaut value string handle index */
123 };
124
125 ON_CALL(biosStringTable, findHandle(StrEq("str_example1")))
126 .WillByDefault(Return(5));
127 BIOSStringAttribute stringReadOnly{jsonStringReadOnly, nullptr};
128
129 checkConstructEntry(stringReadOnly, biosStringTable, expectedAttrEntry,
130 expectedAttrValueEntry);
131
132 auto jsonStringReadWrite = R"({
133 "attribute_name" : "str_example1",
134 "string_type" : "ASCII",
135 "minimum_string_length" : 1,
136 "maximum_string_length" : 100,
137 "default_string_length" : 3,
138 "default_string" : "abc",
George Liudaa69232020-09-02 17:22:09 +0800139 "readOnly" : false,
George Liu92bb4022020-09-03 14:58:24 +0800140 "helpText" : "HelpText",
141 "displayName" : "DisplayName",
John Wang29683b52020-02-27 16:41:44 +0800142 "dbus" : {
143 "object_path" : "/xyz/abc/def",
144 "interface" : "xyz.openbmc_project.str_example1.value",
145 "property_name" : "Str_example1",
146 "property_type" : "string"
147 }
148 })"_json;
149 BIOSStringAttribute stringReadWrite{jsonStringReadWrite, &dbusHandler};
150
151 /* Set expected attr type to read-write */
152 expectedAttrEntry[2] = PLDM_BIOS_STRING;
153 expectedAttrValueEntry[2] = PLDM_BIOS_STRING;
154
155 EXPECT_CALL(
156 dbusHandler,
157 getDbusPropertyVariant(StrEq("/xyz/abc/def"), StrEq("Str_example1"),
158 StrEq("xyz.openbmc_project.str_example1.value")))
159 .WillOnce(Throw(std::exception()));
160
161 checkConstructEntry(stringReadWrite, biosStringTable, expectedAttrEntry,
162 expectedAttrValueEntry);
163
164 EXPECT_CALL(
165 dbusHandler,
166 getDbusPropertyVariant(StrEq("/xyz/abc/def"), StrEq("Str_example1"),
167 StrEq("xyz.openbmc_project.str_example1.value")))
168 .WillOnce(Return(PropertyValue(std::string("abcd"))));
169
170 expectedAttrValueEntry = {
171 0, 0, /* attr handle */
172 1, /* attr type string read-write */
173 4, 0, /* current string length */
174 'a', 'b', 'c', 'd', /* defaut value string handle index */
175 };
176
177 checkConstructEntry(stringReadWrite, biosStringTable, expectedAttrEntry,
178 expectedAttrValueEntry);
179}
180
181TEST_F(TestBIOSStringAttribute, setAttrValueOnDbus)
182{
183 auto jsonStringReadWrite = R"({
184 "attribute_name" : "str_example1",
185 "string_type" : "ASCII",
186 "minimum_string_length" : 1,
187 "maximum_string_length" : 100,
188 "default_string_length" : 3,
189 "default_string" : "abc",
George Liudaa69232020-09-02 17:22:09 +0800190 "readOnly" : false,
George Liu92bb4022020-09-03 14:58:24 +0800191 "helpText" : "HelpText",
192 "displayName" : "DisplayName",
John Wang29683b52020-02-27 16:41:44 +0800193 "dbus" : {
194 "object_path" : "/xyz/abc/def",
195 "interface" : "xyz.openbmc_project.str_example1.value",
196 "property_name" : "Str_example1",
197 "property_type" : "string"
198 }
199 })"_json;
200
201 MockdBusHandler dbusHandler;
202 MockBIOSStringTable biosStringTable;
203
204 BIOSStringAttribute stringReadWrite{jsonStringReadWrite, &dbusHandler};
205 DBusMapping dbusMapping{"/xyz/abc/def",
206 "xyz.openbmc_project.str_example1.value",
207 "Str_example1", "string"};
208 std::vector<uint8_t> attrValueEntry{
209 0, 0, /* attr handle */
210 1, /* attr type string read-write */
211 4, 0, /* current string length */
212 'a', 'b', 'c', 'd', /* defaut value string handle index */
213 };
214 auto entry = reinterpret_cast<pldm_bios_attr_val_table_entry*>(
215 attrValueEntry.data());
216 PropertyValue value = std::string("abcd");
217 EXPECT_CALL(dbusHandler, setDbusProperty(dbusMapping, value)).Times(1);
218 stringReadWrite.setAttrValueOnDbus(entry, nullptr, biosStringTable);
219}