1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
#include "fan_pwm.hpp"
#include "hwmonio_mock.hpp"
#include <sdbusplus/test/sdbus_mock.hpp>
#include <string>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
using ::testing::_;
using ::testing::Invoke;
using ::testing::IsNull;
using ::testing::NotNull;
using ::testing::Return;
using ::testing::StrEq;
static auto FanPwmIntf = "xyz.openbmc_project.Control.FanPwm";
static auto FanPwmProp = "Target";
// Handle basic expectations we'll need for all these tests, if it's found that
// this is helpful for more tests, it can be promoted in scope.
void SetupDbusObject(sdbusplus::SdBusMock* sdbus_mock, const std::string& path,
const std::string& intf, const std::string property = "")
{
EXPECT_CALL(*sdbus_mock,
sd_bus_add_object_vtable(IsNull(), NotNull(), StrEq(path),
StrEq(intf), NotNull(), NotNull()))
.WillOnce(Return(0));
if (property.empty())
{
EXPECT_CALL(*sdbus_mock,
sd_bus_emit_properties_changed_strv(IsNull(), StrEq(path),
StrEq(intf), NotNull()))
.WillOnce(Return(0));
}
else
{
EXPECT_CALL(*sdbus_mock,
sd_bus_emit_properties_changed_strv(IsNull(), StrEq(path),
StrEq(intf), NotNull()))
.WillOnce(Invoke([=](sd_bus* bus, const char* path,
const char* interface, char** names) {
EXPECT_STREQ(property.c_str(), names[0]);
return 0;
}));
}
return;
}
TEST(FanPwmTest, BasicConstructorDeferredTest)
{
// Attempt to just instantiate one.
// NOTE: This test's goal is to figure out what's minimally required to
// mock to instantiate this object.
sdbusplus::SdBusMock sdbus_mock;
auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
std::string instancePath = "";
std::string devPath = "";
std::string id = "";
std::string objPath = "asdf";
bool defer = true;
uint64_t target = 0x01;
std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
std::make_unique<hwmonio::HwmonIOMock>();
SetupDbusObject(&sdbus_mock, objPath, FanPwmIntf, FanPwmProp);
hwmon::FanPwm f(std::move(hwmonio_mock), devPath, id, bus_mock,
objPath.c_str(), defer, target);
}
TEST(FanPwmTest, BasicConstructorNotDeferredTest)
{
// Attempt to just instantiate one.
// NOTE: This test's goal is to figure out what's minimally required to
// mock to instantiate this object.
sdbusplus::SdBusMock sdbus_mock;
auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
std::string instancePath = "";
std::string devPath = "";
std::string id = "";
std::string objPath = "asdf";
bool defer = false;
uint64_t target = 0x01;
std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
std::make_unique<hwmonio::HwmonIOMock>();
SetupDbusObject(&sdbus_mock, objPath, FanPwmIntf, FanPwmProp);
EXPECT_CALL(sdbus_mock, sd_bus_emit_object_added(IsNull(), StrEq("asdf")))
.WillOnce(Return(0));
EXPECT_CALL(sdbus_mock, sd_bus_emit_object_removed(IsNull(), StrEq("asdf")))
.WillOnce(Return(0));
hwmon::FanPwm f(std::move(hwmonio_mock), devPath, id, bus_mock,
objPath.c_str(), defer, target);
}
TEST(FanPwmTest, WriteTargetValue)
{
// Create a FanPwm and write a value to the object.
sdbusplus::SdBusMock sdbus_mock;
auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
std::string instancePath = "";
std::string devPath = "devp";
std::string id = "the_id";
std::string objPath = "asdf";
bool defer = true;
uint64_t target = 0x01;
std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
std::make_unique<hwmonio::HwmonIOMock>();
SetupDbusObject(&sdbus_mock, objPath, FanPwmIntf, FanPwmProp);
hwmonio::HwmonIOMock* hwmonio =
reinterpret_cast<hwmonio::HwmonIOMock*>(hwmonio_mock.get());
hwmon::FanPwm f(std::move(hwmonio_mock), devPath, id, bus_mock,
objPath.c_str(), defer, target);
target = 0x64;
EXPECT_CALL(*hwmonio,
write(static_cast<uint32_t>(target), StrEq("pwm"),
StrEq("the_id"), _, hwmonio::retries, hwmonio::delay));
EXPECT_CALL(sdbus_mock,
sd_bus_emit_properties_changed_strv(
IsNull(), StrEq("asdf"), StrEq(FanPwmIntf), NotNull()))
.WillOnce(Invoke([&](sd_bus* bus, const char* path,
const char* interface, char** names) {
EXPECT_EQ(0, strncmp("Target", names[0], 6));
return 0;
}));
EXPECT_EQ(target, f.target(target));
}
TEST(FanPwmTest, WriteTargetValueNoUpdate)
{
// Create a FanPwm and write a value to the object that was the previous
// value.
sdbusplus::SdBusMock sdbus_mock;
auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
std::string instancePath = "";
std::string devPath = "devp";
std::string id = "the_id";
std::string objPath = "asdf";
bool defer = true;
uint64_t target = 0x01;
std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
std::make_unique<hwmonio::HwmonIOMock>();
SetupDbusObject(&sdbus_mock, objPath, FanPwmIntf, FanPwmProp);
hwmon::FanPwm f(std::move(hwmonio_mock), devPath, id, bus_mock,
objPath.c_str(), defer, target);
EXPECT_EQ(target, f.target(target));
}
|