blob: 6d4026aa71df3f05321d668867c2c77c969f75a1 [file] [log] [blame]
Tom Joseph53279882021-04-28 06:29:13 -07001#include "common/test/mocked_utils.hpp"
John Wang95e6b3c2020-02-13 09:43:24 +08002#include "libpldmresponder/bios_integer_attribute.hpp"
3#include "mocked_bios.hpp"
John Wang95e6b3c2020-02-13 09:43:24 +08004
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
Brad Bishop5079ac42021-08-19 18:35:06 -040012using namespace pldm::utils;
13using namespace pldm::responder::bios;
14
John Wang95e6b3c2020-02-13 09:43:24 +080015using ::testing::_;
16using ::testing::ElementsAreArray;
17using ::testing::Return;
18using ::testing::StrEq;
19using ::testing::Throw;
20
21class TestBIOSIntegerAttribute : public ::testing::Test
22{
23 public:
24 const auto& getIntegerInfo(const BIOSIntegerAttribute& attribute)
25 {
26 return attribute.integerInfo;
27 }
28};
29
30TEST_F(TestBIOSIntegerAttribute, CtorTest)
31{
32 auto jsonIntegerReadOnly = R"({
33 "attribute_name" : "SBE_IMAGE_MINIMUM_VALID_ECS",
34 "lower_bound" : 1,
35 "upper_bound" : 15,
36 "scalar_increment" : 1,
George Liudaa69232020-09-02 17:22:09 +080037 "default_value" : 2,
Archana Kakanid6608092024-05-23 01:14:56 -050038 "read_only" : true,
39 "help_text" : "HelpText",
40 "display_name" : "DisplayName"
John Wang95e6b3c2020-02-13 09:43:24 +080041 })"_json;
42
43 BIOSIntegerAttribute integerReadOnly{jsonIntegerReadOnly, nullptr};
44 EXPECT_EQ(integerReadOnly.name, "SBE_IMAGE_MINIMUM_VALID_ECS");
45 EXPECT_TRUE(integerReadOnly.readOnly);
46 auto& integerInfo = getIntegerInfo(integerReadOnly);
47 EXPECT_EQ(integerInfo.lowerBound, 1);
48 EXPECT_EQ(integerInfo.upperBound, 15);
49 EXPECT_EQ(integerInfo.scalarIncrement, 1);
50 EXPECT_EQ(integerInfo.defaultValue, 2);
51
52 auto jsonIntegerReadOnlyError = R"({
53 "attribute_name" : "SBE_IMAGE_MINIMUM_VALID_ECS",
54 "lower_bound" : 1,
55 "upper_bound" : 15,
56 "scalar_increment" : 1,
George Liudaa69232020-09-02 17:22:09 +080057 "default_valu" : 2,
Archana Kakanid6608092024-05-23 01:14:56 -050058 "read_only" : true,
59 "help_text" : "HelpText",
60 "display_name" : "DisplayName"
John Wang95e6b3c2020-02-13 09:43:24 +080061 })"_json; // default_valu -> default_value
62 EXPECT_THROW((BIOSIntegerAttribute{jsonIntegerReadOnlyError, nullptr}),
63 Json::exception);
64
65 auto jsonIntegerReadWrite = R"({
66 "attribute_name" : "VDD_AVSBUS_RAIL",
67 "lower_bound" : 0,
68 "upper_bound" : 15,
69 "scalar_increment" : 1,
70 "default_value" : 0,
Archana Kakanid6608092024-05-23 01:14:56 -050071 "read_only" : false,
72 "help_text" : "HelpText",
73 "display_name" : "DisplayName",
John Wang95e6b3c2020-02-13 09:43:24 +080074 "dbus":{
75 "object_path" : "/xyz/openbmc_project/avsbus",
76 "interface" : "xyz.openbmc.AvsBus.Manager",
77 "property_type" : "uint8_t",
78 "property_name" : "Rail"
79 }
80 })"_json;
81
82 BIOSIntegerAttribute integerReadWrite{jsonIntegerReadWrite, nullptr};
83 EXPECT_EQ(integerReadWrite.name, "VDD_AVSBUS_RAIL");
84 EXPECT_TRUE(!integerReadWrite.readOnly);
85}
86
87TEST_F(TestBIOSIntegerAttribute, ConstructEntry)
88{
89 MockBIOSStringTable biosStringTable;
90 MockdBusHandler dbusHandler;
91
92 auto jsonIntegerReadOnly = R"({
93 "attribute_name" : "VDD_AVSBUS_RAIL",
94 "lower_bound" : 1,
95 "upper_bound" : 15,
96 "scalar_increment" : 1,
George Liudaa69232020-09-02 17:22:09 +080097 "default_value" : 2,
Archana Kakanid6608092024-05-23 01:14:56 -050098 "read_only" : true,
99 "help_text" : "HelpText",
100 "display_name" : "DisplayName"
John Wang95e6b3c2020-02-13 09:43:24 +0800101 })"_json;
102
103 std::vector<uint8_t> expectedAttrEntry{
104 0, 0, /* attr handle */
105 0x83, /* attr type integer read-only*/
106 5, 0, /* attr name handle */
107 1, 0, 0, 0, 0, 0, 0, 0, /* lower bound */
108 15, 0, 0, 0, 0, 0, 0, 0, /* upper bound */
109 1, 0, 0, 0, /* scalar increment */
Manojkiran Eda2576aec2024-06-17 12:05:17 +0530110 2, 0, 0, 0, 0, 0, 0, 0, /* default value */
John Wang95e6b3c2020-02-13 09:43:24 +0800111 };
112 std::vector<uint8_t> expectedAttrValueEntry{
113 0, 0, /* attr handle */
114 0x83, /* attr type integer read-only*/
115 2, 0, 0, 0, 0, 0, 0, 0, /* current value */
116 };
117
118 BIOSIntegerAttribute integerReadOnly{jsonIntegerReadOnly, nullptr};
119
120 ON_CALL(biosStringTable, findHandle(StrEq("VDD_AVSBUS_RAIL")))
121 .WillByDefault(Return(5));
122
123 checkConstructEntry(integerReadOnly, biosStringTable, expectedAttrEntry,
124 expectedAttrValueEntry);
125
126 auto jsonIntegerReadWrite = R"({
127 "attribute_name" : "VDD_AVSBUS_RAIL",
128 "lower_bound" : 1,
129 "upper_bound" : 15,
130 "scalar_increment" : 1,
131 "default_value" : 2,
Archana Kakanid6608092024-05-23 01:14:56 -0500132 "read_only" : false,
133 "help_text" : "HelpText",
134 "display_name" : "DisplayName",
John Wang95e6b3c2020-02-13 09:43:24 +0800135 "dbus":{
136 "object_path" : "/xyz/openbmc_project/avsbus",
137 "interface" : "xyz.openbmc.AvsBus.Manager",
138 "property_type" : "uint8_t",
139 "property_name" : "Rail"
140 }
141 })"_json;
142 BIOSIntegerAttribute integerReadWrite{jsonIntegerReadWrite, &dbusHandler};
143
144 EXPECT_CALL(dbusHandler,
145 getDbusPropertyVariant(StrEq("/xyz/openbmc_project/avsbus"),
146 StrEq("Rail"),
147 StrEq("xyz.openbmc.AvsBus.Manager")))
148 .WillOnce(Throw(std::exception()));
149
150 /* Set expected attr type to read-write */
151 expectedAttrEntry[2] = PLDM_BIOS_INTEGER;
152 expectedAttrValueEntry[2] = PLDM_BIOS_INTEGER;
153
154 checkConstructEntry(integerReadWrite, biosStringTable, expectedAttrEntry,
155 expectedAttrValueEntry);
156
157 EXPECT_CALL(dbusHandler,
158 getDbusPropertyVariant(StrEq("/xyz/openbmc_project/avsbus"),
159 StrEq("Rail"),
160 StrEq("xyz.openbmc.AvsBus.Manager")))
161 .WillOnce(Return(PropertyValue(uint8_t(7))));
162
163 expectedAttrValueEntry = {
164 0, 0, /* attr handle */
165 3, /* attr type integer read-write*/
166 7, 0, 0, 0, 0, 0, 0, 0, /* current value */
167 };
168
169 checkConstructEntry(integerReadWrite, biosStringTable, expectedAttrEntry,
170 expectedAttrValueEntry);
171}
172
173TEST_F(TestBIOSIntegerAttribute, setAttrValueOnDbus)
174{
175 MockdBusHandler dbusHandler;
176 MockBIOSStringTable biosStringTable;
177
178 auto jsonIntegerReadWrite = R"({
179 "attribute_name" : "VDD_AVSBUS_RAIL",
180 "lower_bound" : 1,
181 "upper_bound" : 15,
182 "scalar_increment" : 1,
183 "default_value" : 2,
Archana Kakanid6608092024-05-23 01:14:56 -0500184 "read_only" : false,
185 "help_text" : "HelpText",
186 "display_name" : "DisplayName",
John Wang95e6b3c2020-02-13 09:43:24 +0800187 "dbus":{
188 "object_path" : "/xyz/openbmc_project/avsbus",
189 "interface" : "xyz.openbmc.AvsBus.Manager",
190 "property_type" : "uint8_t",
191 "property_name" : "Rail"
192 }
193 })"_json;
194 BIOSIntegerAttribute integerReadWrite{jsonIntegerReadWrite, &dbusHandler};
195 DBusMapping dbusMapping{"/xyz/openbmc_project/avsbus",
196 "xyz.openbmc.AvsBus.Manager", "Rail", "uint8_t"};
197 std::vector<uint8_t> attrValueEntry = {
198 0, 0, /* attr handle */
199 3, /* attr type integer read-write*/
200 7, 0, 0, 0, 0, 0, 0, 0, /* current value */
201 };
202
203 auto entry = reinterpret_cast<pldm_bios_attr_val_table_entry*>(
204 attrValueEntry.data());
205 EXPECT_CALL(dbusHandler,
206 setDbusProperty(dbusMapping, PropertyValue{uint8_t(7)}))
207 .Times(1);
208 integerReadWrite.setAttrValueOnDbus(entry, nullptr, biosStringTable);
George Liu92bb4022020-09-03 14:58:24 +0800209}