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