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