blob: 01079c3c413d1508159f64146259409efdd681bb [file] [log] [blame]
John Wang29683b52020-02-27 16:41:44 +08001#include "libpldmresponder/bios_string_attribute.hpp"
2#include "mocked_bios.hpp"
3#include "mocked_utils.hpp"
4
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;
13using ::testing::_;
14using ::testing::ElementsAreArray;
15using ::testing::Return;
16using ::testing::StrEq;
17using ::testing::Throw;
18
19class TestBIOSStringAttribute : public ::testing::Test
20{
21 public:
22 const auto& getStringInfo(const BIOSStringAttribute& biosStringAttribute)
23 {
24 return biosStringAttribute.stringInfo;
25 }
26};
27
28TEST_F(TestBIOSStringAttribute, CtorTest)
29{
30 auto jsonStringReadOnly = R"( {
31 "attribute_name" : "str_example3",
32 "string_type" : "ASCII",
33 "minimum_string_length" : 1,
34 "maximum_string_length" : 100,
35 "default_string_length" : 2,
George Liudaa69232020-09-02 17:22:09 +080036 "default_string" : "ef",
37 "readOnly" : true
John Wang29683b52020-02-27 16:41:44 +080038 })"_json;
39 BIOSStringAttribute stringReadOnly{jsonStringReadOnly, nullptr};
40 EXPECT_EQ(stringReadOnly.name, "str_example3");
41 EXPECT_TRUE(stringReadOnly.readOnly);
42
43 auto& stringInfo = getStringInfo(stringReadOnly);
44 EXPECT_EQ(stringInfo.stringType,
45 static_cast<uint8_t>(BIOSStringAttribute::Encoding::ASCII));
46 EXPECT_EQ(stringInfo.minLength, 1);
47 EXPECT_EQ(stringInfo.maxLength, 100);
48 EXPECT_EQ(stringInfo.defLength, 2);
49 EXPECT_EQ(stringInfo.defString, "ef");
50
51 auto jsonStringReadOnlyError = R"( {
52 "attribute_name" : "str_example3",
53 "string_type" : "ASCII",
54 "minimum_string_length" : 1,
55 "maximum_string_length" : 100,
56 "default_string" : "ef"
57 })"_json; // missing default_string_length
58
59 EXPECT_THROW((BIOSStringAttribute{jsonStringReadOnlyError, nullptr}),
60 Json::exception);
61
62 auto jsonStringReadWrite = R"({
63 "attribute_name" : "str_example1",
64 "string_type" : "ASCII",
65 "minimum_string_length" : 1,
66 "maximum_string_length" : 100,
67 "default_string_length" : 3,
68 "default_string" : "abc",
George Liudaa69232020-09-02 17:22:09 +080069 "readOnly" : false,
John Wang29683b52020-02-27 16:41:44 +080070 "dbus" : {
71 "object_path" : "/xyz/abc/def",
72 "interface" : "xyz.openbmc_project.str_example1.value",
73 "property_name" : "Str_example1",
74 "property_type" : "string"
75 }
76 })"_json;
77 BIOSStringAttribute stringReadWrite{jsonStringReadWrite, nullptr};
78
79 EXPECT_EQ(stringReadWrite.name, "str_example1");
80 EXPECT_TRUE(!stringReadWrite.readOnly);
81}
82
83TEST_F(TestBIOSStringAttribute, ConstructEntry)
84{
85 MockBIOSStringTable biosStringTable;
86 MockdBusHandler dbusHandler;
87
88 auto jsonStringReadOnly = R"({
89 "attribute_name" : "str_example1",
90 "string_type" : "ASCII",
91 "minimum_string_length" : 1,
92 "maximum_string_length" : 100,
93 "default_string_length" : 3,
George Liudaa69232020-09-02 17:22:09 +080094 "default_string" : "abc",
95 "readOnly" : true
John Wang29683b52020-02-27 16:41:44 +080096 })"_json;
97
98 std::vector<uint8_t> expectedAttrEntry{
99 0, 0, /* attr handle */
100 0x81, /* attr type string read-only */
101 5, 0, /* attr name handle */
102 1, /* string type */
103 1, 0, /* minimum length of the string in bytes */
104 100, 0, /* maximum length of the string in bytes */
105 3, 0, /* length of default string in length */
106 'a', 'b', 'c' /* default string */
107 };
108
109 std::vector<uint8_t> expectedAttrValueEntry{
110 0, 0, /* attr handle */
111 0x81, /* attr type string read-only */
112 3, 0, /* current string length */
113 'a', 'b', 'c', /* defaut value string handle index */
114 };
115
116 ON_CALL(biosStringTable, findHandle(StrEq("str_example1")))
117 .WillByDefault(Return(5));
118 BIOSStringAttribute stringReadOnly{jsonStringReadOnly, nullptr};
119
120 checkConstructEntry(stringReadOnly, biosStringTable, expectedAttrEntry,
121 expectedAttrValueEntry);
122
123 auto jsonStringReadWrite = R"({
124 "attribute_name" : "str_example1",
125 "string_type" : "ASCII",
126 "minimum_string_length" : 1,
127 "maximum_string_length" : 100,
128 "default_string_length" : 3,
129 "default_string" : "abc",
George Liudaa69232020-09-02 17:22:09 +0800130 "readOnly" : false,
John Wang29683b52020-02-27 16:41:44 +0800131 "dbus" : {
132 "object_path" : "/xyz/abc/def",
133 "interface" : "xyz.openbmc_project.str_example1.value",
134 "property_name" : "Str_example1",
135 "property_type" : "string"
136 }
137 })"_json;
138 BIOSStringAttribute stringReadWrite{jsonStringReadWrite, &dbusHandler};
139
140 /* Set expected attr type to read-write */
141 expectedAttrEntry[2] = PLDM_BIOS_STRING;
142 expectedAttrValueEntry[2] = PLDM_BIOS_STRING;
143
144 EXPECT_CALL(
145 dbusHandler,
146 getDbusPropertyVariant(StrEq("/xyz/abc/def"), StrEq("Str_example1"),
147 StrEq("xyz.openbmc_project.str_example1.value")))
148 .WillOnce(Throw(std::exception()));
149
150 checkConstructEntry(stringReadWrite, biosStringTable, expectedAttrEntry,
151 expectedAttrValueEntry);
152
153 EXPECT_CALL(
154 dbusHandler,
155 getDbusPropertyVariant(StrEq("/xyz/abc/def"), StrEq("Str_example1"),
156 StrEq("xyz.openbmc_project.str_example1.value")))
157 .WillOnce(Return(PropertyValue(std::string("abcd"))));
158
159 expectedAttrValueEntry = {
160 0, 0, /* attr handle */
161 1, /* attr type string read-write */
162 4, 0, /* current string length */
163 'a', 'b', 'c', 'd', /* defaut value string handle index */
164 };
165
166 checkConstructEntry(stringReadWrite, biosStringTable, expectedAttrEntry,
167 expectedAttrValueEntry);
168}
169
170TEST_F(TestBIOSStringAttribute, setAttrValueOnDbus)
171{
172 auto jsonStringReadWrite = R"({
173 "attribute_name" : "str_example1",
174 "string_type" : "ASCII",
175 "minimum_string_length" : 1,
176 "maximum_string_length" : 100,
177 "default_string_length" : 3,
178 "default_string" : "abc",
George Liudaa69232020-09-02 17:22:09 +0800179 "readOnly" : false,
John Wang29683b52020-02-27 16:41:44 +0800180 "dbus" : {
181 "object_path" : "/xyz/abc/def",
182 "interface" : "xyz.openbmc_project.str_example1.value",
183 "property_name" : "Str_example1",
184 "property_type" : "string"
185 }
186 })"_json;
187
188 MockdBusHandler dbusHandler;
189 MockBIOSStringTable biosStringTable;
190
191 BIOSStringAttribute stringReadWrite{jsonStringReadWrite, &dbusHandler};
192 DBusMapping dbusMapping{"/xyz/abc/def",
193 "xyz.openbmc_project.str_example1.value",
194 "Str_example1", "string"};
195 std::vector<uint8_t> attrValueEntry{
196 0, 0, /* attr handle */
197 1, /* attr type string read-write */
198 4, 0, /* current string length */
199 'a', 'b', 'c', 'd', /* defaut value string handle index */
200 };
201 auto entry = reinterpret_cast<pldm_bios_attr_val_table_entry*>(
202 attrValueEntry.data());
203 PropertyValue value = std::string("abcd");
204 EXPECT_CALL(dbusHandler, setDbusProperty(dbusMapping, value)).Times(1);
205 stringReadWrite.setAttrValueOnDbus(entry, nullptr, biosStringTable);
206}