blob: 4cd5809f68049478d82b8fb480a77f4dc397cd36 [file] [log] [blame]
John Wang3be70852020-02-13 15:59:04 +08001#include "libpldmresponder/bios_enum_attribute.hpp"
2#include "mocked_bios.hpp"
3#include "mocked_utils.hpp"
4
5#include <memory>
6#include <nlohmann/json.hpp>
7
8#include <gmock/gmock.h>
9#include <gtest/gtest.h>
10
11using ::testing::_;
12using ::testing::ElementsAreArray;
13using ::testing::Return;
14using ::testing::StrEq;
15using ::testing::Throw;
16
17class TestBIOSEnumAttribute : public ::testing::Test
18{
19 public:
20 const auto& getPossibleValues(const BIOSEnumAttribute& attribute)
21 {
22 return attribute.possibleValues;
23 }
24
25 const auto& getDefaultValue(const BIOSEnumAttribute& attribute)
26 {
27 return attribute.defaultValue;
28 }
29};
30
31TEST_F(TestBIOSEnumAttribute, CtorTest)
32{
33 auto jsonEnumReadOnly = R"({
34 "attribute_name" : "CodeUpdatePolicy",
35 "possible_values" : [ "Concurrent", "Disruptive" ],
36 "default_values" : [ "Concurrent" ]
37 })"_json;
38
39 BIOSEnumAttribute enumReadOnly{jsonEnumReadOnly, nullptr};
40 EXPECT_EQ(enumReadOnly.name, "CodeUpdatePolicy");
41 EXPECT_TRUE(enumReadOnly.readOnly);
42 EXPECT_THAT(getPossibleValues(enumReadOnly),
43 ElementsAreArray({"Concurrent", "Disruptive"}));
44 EXPECT_EQ(getDefaultValue(enumReadOnly), "Concurrent");
45
46 auto jsonEnumReadOnlyError = R"({
47 "attribute_name" : "CodeUpdatePolicy",
48 "possible_value" : [ "Concurrent", "Disruptive" ],
49 "default_values" : [ "Concurrent" ]
50 })"_json; // possible_value -> possible_values
51 EXPECT_THROW((BIOSEnumAttribute{jsonEnumReadOnlyError, nullptr}),
52 Json::exception);
53
54 auto jsonEnumReadWrite = R"({
55 "attribute_name" : "FWBootSide",
56 "possible_values" : [ "Perm", "Temp" ],
57 "default_values" : [ "Perm" ],
58 "dbus":
59 {
60 "object_path" : "/xyz/abc/def",
61 "interface" : "xyz.openbmc.FWBoot.Side",
62 "property_name" : "Side",
63 "property_type" : "bool",
64 "property_values" : [true, false]
65 }
66 })"_json;
67
68 BIOSEnumAttribute enumReadWrite{jsonEnumReadWrite, nullptr};
69 EXPECT_EQ(enumReadWrite.name, "FWBootSide");
70 EXPECT_TRUE(!enumReadWrite.readOnly);
71}
72
73TEST_F(TestBIOSEnumAttribute, ConstructEntry)
74{
75 MockBIOSStringTable biosStringTable;
76 MockdBusHandler dbusHandler;
77
78 auto jsonEnumReadOnly = R"({
79 "attribute_name" : "CodeUpdatePolicy",
80 "possible_values" : [ "Concurrent", "Disruptive" ],
81 "default_values" : [ "Disruptive" ]
82 })"_json;
83
84 std::vector<uint8_t> expectedAttrEntry{
85 0, 0, /* attr handle */
86 0x80, /* attr type enum read-only*/
87 4, 0, /* attr name handle */
88 2, /* number of possible value */
89 2, 0, /* possible value handle */
90 3, 0, /* possible value handle */
91 1, /* number of default value */
92 1 /* defaut value string handle index */
93 };
94
95 std::vector<uint8_t> expectedAttrValueEntry{
96 0, 0, /* attr handle */
97 0x80, /* attr type enum read-only*/
98 1, /* number of current value */
99 1 /* current value string handle index */
100 };
101
102 BIOSEnumAttribute enumReadOnly{jsonEnumReadOnly, nullptr};
103
104 ON_CALL(biosStringTable, findHandle(StrEq("Concurrent")))
105 .WillByDefault(Return(2));
106 ON_CALL(biosStringTable, findHandle(StrEq("Disruptive")))
107 .WillByDefault(Return(3));
108 ON_CALL(biosStringTable, findHandle(StrEq("CodeUpdatePolicy")))
109 .WillByDefault(Return(4));
110
111 checkConstructEntry(enumReadOnly, biosStringTable, expectedAttrEntry,
112 expectedAttrValueEntry);
113
114 auto jsonEnumReadWrite = R"({
115 "attribute_name" : "CodeUpdatePolicy",
116 "possible_values" : [ "Concurrent", "Disruptive" ],
117 "default_values" : [ "Disruptive" ],
118 "dbus":
119 {
120 "object_path" : "/xyz/abc/def",
121 "interface" : "xyz.openbmc.abc.def",
122 "property_name" : "Policy",
123 "property_type" : "bool",
124 "property_values" : [true, false]
125 }
126 })"_json;
127
128 BIOSEnumAttribute enumReadWrite{jsonEnumReadWrite, &dbusHandler};
129
130 EXPECT_CALL(dbusHandler,
131 getDbusPropertyVariant(StrEq("/xyz/abc/def"), StrEq("Policy"),
132 StrEq("xyz.openbmc.abc.def")))
133 .WillOnce(Throw(std::exception()));
134
135 /* Set expected attr type to read-write */
136 expectedAttrEntry[2] = PLDM_BIOS_ENUMERATION;
137 expectedAttrValueEntry[2] = PLDM_BIOS_ENUMERATION;
138
139 checkConstructEntry(enumReadWrite, biosStringTable, expectedAttrEntry,
140 expectedAttrValueEntry);
141
142 EXPECT_CALL(dbusHandler,
143 getDbusPropertyVariant(StrEq("/xyz/abc/def"), StrEq("Policy"),
144 StrEq("xyz.openbmc.abc.def")))
145 .WillOnce(Return(PropertyValue(true)));
146
147 expectedAttrValueEntry = {
148 0, 0, /* attr handle */
149 0, /* attr type enum read-write*/
150 1, /* number of current value */
151 0 /* current value string handle index */
152 };
153
154 checkConstructEntry(enumReadWrite, biosStringTable, expectedAttrEntry,
155 expectedAttrValueEntry);
156}
157
158TEST_F(TestBIOSEnumAttribute, setAttrValueOnDbus)
159{
160 MockBIOSStringTable biosStringTable;
161 MockdBusHandler dbusHandler;
162
163 auto jsonEnumReadWrite = R"({
164 "attribute_name" : "CodeUpdatePolicy",
165 "possible_values" : [ "Concurrent", "Disruptive" ],
166 "default_values" : [ "Disruptive" ],
167 "dbus":
168 {
169 "object_path" : "/xyz/abc/def",
170 "interface" : "xyz.openbmc.abc.def",
171 "property_name" : "Policy",
172 "property_type" : "bool",
173 "property_values" : [true, false]
174 }
175 })"_json;
176 DBusMapping dbusMapping{"/xyz/abc/def", "xyz.openbmc.abc.def", "Policy",
177 "bool"};
178
179 BIOSEnumAttribute enumReadWrite{jsonEnumReadWrite, &dbusHandler};
180
181 std::vector<uint8_t> attrEntry{
182 0, 0, /* attr handle */
183 0, /* attr type enum read-only*/
184 4, 0, /* attr name handle */
185 2, /* number of possible value */
186 2, 0, /* possible value handle */
187 3, 0, /* possible value handle */
188 1, /* number of default value */
189 1 /* defaut value string handle index */
190 };
191
192 ON_CALL(biosStringTable, findString(2))
193 .WillByDefault(Return(std::string("Concurrent")));
194 ON_CALL(biosStringTable, findString(3))
195 .WillByDefault(Return(std::string("Disruptive")));
196
197 std::vector<uint8_t> attrValueEntry{
198 0, 0, /* attr handle */
199 0, /* attr type enum read-only*/
200 1, /* number of current value */
201 0 /* current value string handle index */
202 };
203
204 EXPECT_CALL(dbusHandler,
205 setDbusProperty(dbusMapping, PropertyValue{bool(true)}))
206 .Times(1);
207 enumReadWrite.setAttrValueOnDbus(
208 reinterpret_cast<pldm_bios_attr_val_table_entry*>(
209 attrValueEntry.data()),
210 reinterpret_cast<pldm_bios_attr_table_entry*>(attrEntry.data()),
211 biosStringTable);
212}