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