blob: 48c228bca07f9f14817518ff880aa75c9ebd3a9b [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,
34 "default_value" : 2
35 })"_json;
36
37 BIOSIntegerAttribute integerReadOnly{jsonIntegerReadOnly, nullptr};
38 EXPECT_EQ(integerReadOnly.name, "SBE_IMAGE_MINIMUM_VALID_ECS");
39 EXPECT_TRUE(integerReadOnly.readOnly);
40 auto& integerInfo = getIntegerInfo(integerReadOnly);
41 EXPECT_EQ(integerInfo.lowerBound, 1);
42 EXPECT_EQ(integerInfo.upperBound, 15);
43 EXPECT_EQ(integerInfo.scalarIncrement, 1);
44 EXPECT_EQ(integerInfo.defaultValue, 2);
45
46 auto jsonIntegerReadOnlyError = R"({
47 "attribute_name" : "SBE_IMAGE_MINIMUM_VALID_ECS",
48 "lower_bound" : 1,
49 "upper_bound" : 15,
50 "scalar_increment" : 1,
51 "default_valu" : 2
52 })"_json; // default_valu -> default_value
53 EXPECT_THROW((BIOSIntegerAttribute{jsonIntegerReadOnlyError, nullptr}),
54 Json::exception);
55
56 auto jsonIntegerReadWrite = R"({
57 "attribute_name" : "VDD_AVSBUS_RAIL",
58 "lower_bound" : 0,
59 "upper_bound" : 15,
60 "scalar_increment" : 1,
61 "default_value" : 0,
62 "dbus":{
63 "object_path" : "/xyz/openbmc_project/avsbus",
64 "interface" : "xyz.openbmc.AvsBus.Manager",
65 "property_type" : "uint8_t",
66 "property_name" : "Rail"
67 }
68 })"_json;
69
70 BIOSIntegerAttribute integerReadWrite{jsonIntegerReadWrite, nullptr};
71 EXPECT_EQ(integerReadWrite.name, "VDD_AVSBUS_RAIL");
72 EXPECT_TRUE(!integerReadWrite.readOnly);
73}
74
75TEST_F(TestBIOSIntegerAttribute, ConstructEntry)
76{
77 MockBIOSStringTable biosStringTable;
78 MockdBusHandler dbusHandler;
79
80 auto jsonIntegerReadOnly = R"({
81 "attribute_name" : "VDD_AVSBUS_RAIL",
82 "lower_bound" : 1,
83 "upper_bound" : 15,
84 "scalar_increment" : 1,
85 "default_value" : 2
86 })"_json;
87
88 std::vector<uint8_t> expectedAttrEntry{
89 0, 0, /* attr handle */
90 0x83, /* attr type integer read-only*/
91 5, 0, /* attr name handle */
92 1, 0, 0, 0, 0, 0, 0, 0, /* lower bound */
93 15, 0, 0, 0, 0, 0, 0, 0, /* upper bound */
94 1, 0, 0, 0, /* scalar increment */
95 2, 0, 0, 0, 0, 0, 0, 0, /* defaut value */
96 };
97 std::vector<uint8_t> expectedAttrValueEntry{
98 0, 0, /* attr handle */
99 0x83, /* attr type integer read-only*/
100 2, 0, 0, 0, 0, 0, 0, 0, /* current value */
101 };
102
103 BIOSIntegerAttribute integerReadOnly{jsonIntegerReadOnly, nullptr};
104
105 ON_CALL(biosStringTable, findHandle(StrEq("VDD_AVSBUS_RAIL")))
106 .WillByDefault(Return(5));
107
108 checkConstructEntry(integerReadOnly, biosStringTable, expectedAttrEntry,
109 expectedAttrValueEntry);
110
111 auto jsonIntegerReadWrite = R"({
112 "attribute_name" : "VDD_AVSBUS_RAIL",
113 "lower_bound" : 1,
114 "upper_bound" : 15,
115 "scalar_increment" : 1,
116 "default_value" : 2,
117 "dbus":{
118 "object_path" : "/xyz/openbmc_project/avsbus",
119 "interface" : "xyz.openbmc.AvsBus.Manager",
120 "property_type" : "uint8_t",
121 "property_name" : "Rail"
122 }
123 })"_json;
124 BIOSIntegerAttribute integerReadWrite{jsonIntegerReadWrite, &dbusHandler};
125
126 EXPECT_CALL(dbusHandler,
127 getDbusPropertyVariant(StrEq("/xyz/openbmc_project/avsbus"),
128 StrEq("Rail"),
129 StrEq("xyz.openbmc.AvsBus.Manager")))
130 .WillOnce(Throw(std::exception()));
131
132 /* Set expected attr type to read-write */
133 expectedAttrEntry[2] = PLDM_BIOS_INTEGER;
134 expectedAttrValueEntry[2] = PLDM_BIOS_INTEGER;
135
136 checkConstructEntry(integerReadWrite, biosStringTable, expectedAttrEntry,
137 expectedAttrValueEntry);
138
139 EXPECT_CALL(dbusHandler,
140 getDbusPropertyVariant(StrEq("/xyz/openbmc_project/avsbus"),
141 StrEq("Rail"),
142 StrEq("xyz.openbmc.AvsBus.Manager")))
143 .WillOnce(Return(PropertyValue(uint8_t(7))));
144
145 expectedAttrValueEntry = {
146 0, 0, /* attr handle */
147 3, /* attr type integer read-write*/
148 7, 0, 0, 0, 0, 0, 0, 0, /* current value */
149 };
150
151 checkConstructEntry(integerReadWrite, biosStringTable, expectedAttrEntry,
152 expectedAttrValueEntry);
153}
154
155TEST_F(TestBIOSIntegerAttribute, setAttrValueOnDbus)
156{
157 MockdBusHandler dbusHandler;
158 MockBIOSStringTable biosStringTable;
159
160 auto jsonIntegerReadWrite = R"({
161 "attribute_name" : "VDD_AVSBUS_RAIL",
162 "lower_bound" : 1,
163 "upper_bound" : 15,
164 "scalar_increment" : 1,
165 "default_value" : 2,
166 "dbus":{
167 "object_path" : "/xyz/openbmc_project/avsbus",
168 "interface" : "xyz.openbmc.AvsBus.Manager",
169 "property_type" : "uint8_t",
170 "property_name" : "Rail"
171 }
172 })"_json;
173 BIOSIntegerAttribute integerReadWrite{jsonIntegerReadWrite, &dbusHandler};
174 DBusMapping dbusMapping{"/xyz/openbmc_project/avsbus",
175 "xyz.openbmc.AvsBus.Manager", "Rail", "uint8_t"};
176 std::vector<uint8_t> attrValueEntry = {
177 0, 0, /* attr handle */
178 3, /* attr type integer read-write*/
179 7, 0, 0, 0, 0, 0, 0, 0, /* current value */
180 };
181
182 auto entry = reinterpret_cast<pldm_bios_attr_val_table_entry*>(
183 attrValueEntry.data());
184 EXPECT_CALL(dbusHandler,
185 setDbusProperty(dbusMapping, PropertyValue{uint8_t(7)}))
186 .Times(1);
187 integerReadWrite.setAttrValueOnDbus(entry, nullptr, biosStringTable);
188}