blob: 4004ae9e904f5184745e5963764ac8e50b35129b [file] [log] [blame]
John Wang95e6b3c2020-02-13 09:43:24 +08001#include "libpldmresponder/bios_integer_attribute.hpp"
2#include "mocked_bios.hpp"
3#include "mocked_utils.hpp"
4
John Wang95e6b3c2020-02-13 09:43:24 +08005#include <nlohmann/json.hpp>
6
George Liu6492f522020-06-16 10:34:05 +08007#include <memory>
8
John Wang95e6b3c2020-02-13 09:43:24 +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 TestBIOSIntegerAttribute : public ::testing::Test
19{
20 public:
21 const auto& getIntegerInfo(const BIOSIntegerAttribute& attribute)
22 {
23 return attribute.integerInfo;
24 }
25};
26
27TEST_F(TestBIOSIntegerAttribute, CtorTest)
28{
29 auto jsonIntegerReadOnly = R"({
30 "attribute_name" : "SBE_IMAGE_MINIMUM_VALID_ECS",
31 "lower_bound" : 1,
32 "upper_bound" : 15,
33 "scalar_increment" : 1,
George Liudaa69232020-09-02 17:22:09 +080034 "default_value" : 2,
35 "readOnly" : true
John Wang95e6b3c2020-02-13 09:43:24 +080036 })"_json;
37
38 BIOSIntegerAttribute integerReadOnly{jsonIntegerReadOnly, nullptr};
39 EXPECT_EQ(integerReadOnly.name, "SBE_IMAGE_MINIMUM_VALID_ECS");
40 EXPECT_TRUE(integerReadOnly.readOnly);
41 auto& integerInfo = getIntegerInfo(integerReadOnly);
42 EXPECT_EQ(integerInfo.lowerBound, 1);
43 EXPECT_EQ(integerInfo.upperBound, 15);
44 EXPECT_EQ(integerInfo.scalarIncrement, 1);
45 EXPECT_EQ(integerInfo.defaultValue, 2);
46
47 auto jsonIntegerReadOnlyError = R"({
48 "attribute_name" : "SBE_IMAGE_MINIMUM_VALID_ECS",
49 "lower_bound" : 1,
50 "upper_bound" : 15,
51 "scalar_increment" : 1,
George Liudaa69232020-09-02 17:22:09 +080052 "default_valu" : 2,
53 "readOnly" : true
John Wang95e6b3c2020-02-13 09:43:24 +080054 })"_json; // default_valu -> default_value
55 EXPECT_THROW((BIOSIntegerAttribute{jsonIntegerReadOnlyError, nullptr}),
56 Json::exception);
57
58 auto jsonIntegerReadWrite = R"({
59 "attribute_name" : "VDD_AVSBUS_RAIL",
60 "lower_bound" : 0,
61 "upper_bound" : 15,
62 "scalar_increment" : 1,
63 "default_value" : 0,
George Liudaa69232020-09-02 17:22:09 +080064 "readOnly" : false,
John Wang95e6b3c2020-02-13 09:43:24 +080065 "dbus":{
66 "object_path" : "/xyz/openbmc_project/avsbus",
67 "interface" : "xyz.openbmc.AvsBus.Manager",
68 "property_type" : "uint8_t",
69 "property_name" : "Rail"
70 }
71 })"_json;
72
73 BIOSIntegerAttribute integerReadWrite{jsonIntegerReadWrite, nullptr};
74 EXPECT_EQ(integerReadWrite.name, "VDD_AVSBUS_RAIL");
75 EXPECT_TRUE(!integerReadWrite.readOnly);
76}
77
78TEST_F(TestBIOSIntegerAttribute, ConstructEntry)
79{
80 MockBIOSStringTable biosStringTable;
81 MockdBusHandler dbusHandler;
82
83 auto jsonIntegerReadOnly = R"({
84 "attribute_name" : "VDD_AVSBUS_RAIL",
85 "lower_bound" : 1,
86 "upper_bound" : 15,
87 "scalar_increment" : 1,
George Liudaa69232020-09-02 17:22:09 +080088 "default_value" : 2,
89 "readOnly" : true
John Wang95e6b3c2020-02-13 09:43:24 +080090 })"_json;
91
92 std::vector<uint8_t> expectedAttrEntry{
93 0, 0, /* attr handle */
94 0x83, /* attr type integer read-only*/
95 5, 0, /* attr name handle */
96 1, 0, 0, 0, 0, 0, 0, 0, /* lower bound */
97 15, 0, 0, 0, 0, 0, 0, 0, /* upper bound */
98 1, 0, 0, 0, /* scalar increment */
99 2, 0, 0, 0, 0, 0, 0, 0, /* defaut value */
100 };
101 std::vector<uint8_t> expectedAttrValueEntry{
102 0, 0, /* attr handle */
103 0x83, /* attr type integer read-only*/
104 2, 0, 0, 0, 0, 0, 0, 0, /* current value */
105 };
106
107 BIOSIntegerAttribute integerReadOnly{jsonIntegerReadOnly, nullptr};
108
109 ON_CALL(biosStringTable, findHandle(StrEq("VDD_AVSBUS_RAIL")))
110 .WillByDefault(Return(5));
111
112 checkConstructEntry(integerReadOnly, biosStringTable, expectedAttrEntry,
113 expectedAttrValueEntry);
114
115 auto jsonIntegerReadWrite = R"({
116 "attribute_name" : "VDD_AVSBUS_RAIL",
117 "lower_bound" : 1,
118 "upper_bound" : 15,
119 "scalar_increment" : 1,
120 "default_value" : 2,
George Liudaa69232020-09-02 17:22:09 +0800121 "readOnly" : false,
John Wang95e6b3c2020-02-13 09:43:24 +0800122 "dbus":{
123 "object_path" : "/xyz/openbmc_project/avsbus",
124 "interface" : "xyz.openbmc.AvsBus.Manager",
125 "property_type" : "uint8_t",
126 "property_name" : "Rail"
127 }
128 })"_json;
129 BIOSIntegerAttribute integerReadWrite{jsonIntegerReadWrite, &dbusHandler};
130
131 EXPECT_CALL(dbusHandler,
132 getDbusPropertyVariant(StrEq("/xyz/openbmc_project/avsbus"),
133 StrEq("Rail"),
134 StrEq("xyz.openbmc.AvsBus.Manager")))
135 .WillOnce(Throw(std::exception()));
136
137 /* Set expected attr type to read-write */
138 expectedAttrEntry[2] = PLDM_BIOS_INTEGER;
139 expectedAttrValueEntry[2] = PLDM_BIOS_INTEGER;
140
141 checkConstructEntry(integerReadWrite, biosStringTable, expectedAttrEntry,
142 expectedAttrValueEntry);
143
144 EXPECT_CALL(dbusHandler,
145 getDbusPropertyVariant(StrEq("/xyz/openbmc_project/avsbus"),
146 StrEq("Rail"),
147 StrEq("xyz.openbmc.AvsBus.Manager")))
148 .WillOnce(Return(PropertyValue(uint8_t(7))));
149
150 expectedAttrValueEntry = {
151 0, 0, /* attr handle */
152 3, /* attr type integer read-write*/
153 7, 0, 0, 0, 0, 0, 0, 0, /* current value */
154 };
155
156 checkConstructEntry(integerReadWrite, biosStringTable, expectedAttrEntry,
157 expectedAttrValueEntry);
158}
159
160TEST_F(TestBIOSIntegerAttribute, setAttrValueOnDbus)
161{
162 MockdBusHandler dbusHandler;
163 MockBIOSStringTable biosStringTable;
164
165 auto jsonIntegerReadWrite = R"({
166 "attribute_name" : "VDD_AVSBUS_RAIL",
167 "lower_bound" : 1,
168 "upper_bound" : 15,
169 "scalar_increment" : 1,
170 "default_value" : 2,
George Liudaa69232020-09-02 17:22:09 +0800171 "readOnly" : false,
John Wang95e6b3c2020-02-13 09:43:24 +0800172 "dbus":{
173 "object_path" : "/xyz/openbmc_project/avsbus",
174 "interface" : "xyz.openbmc.AvsBus.Manager",
175 "property_type" : "uint8_t",
176 "property_name" : "Rail"
177 }
178 })"_json;
179 BIOSIntegerAttribute integerReadWrite{jsonIntegerReadWrite, &dbusHandler};
180 DBusMapping dbusMapping{"/xyz/openbmc_project/avsbus",
181 "xyz.openbmc.AvsBus.Manager", "Rail", "uint8_t"};
182 std::vector<uint8_t> attrValueEntry = {
183 0, 0, /* attr handle */
184 3, /* attr type integer read-write*/
185 7, 0, 0, 0, 0, 0, 0, 0, /* current value */
186 };
187
188 auto entry = reinterpret_cast<pldm_bios_attr_val_table_entry*>(
189 attrValueEntry.data());
190 EXPECT_CALL(dbusHandler,
191 setDbusProperty(dbusMapping, PropertyValue{uint8_t(7)}))
192 .Times(1);
193 integerReadWrite.setAttrValueOnDbus(entry, nullptr, biosStringTable);
194}