Lei YU | 9ab6d75 | 2019-10-28 17:03:20 +0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2019 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #include "../updater.hpp" |
Faisal Awada | ec61bbd | 2024-11-04 08:46:20 -0600 | [diff] [blame] | 17 | #include "../version.hpp" |
Shawn McCarney | b1216b9 | 2020-01-21 15:22:32 -0600 | [diff] [blame] | 18 | #include "mocked_i2c_interface.hpp" |
Lei YU | 7c2fbbb | 2019-11-06 14:56:02 +0800 | [diff] [blame] | 19 | |
| 20 | #include <filesystem> |
Lei YU | 9ab6d75 | 2019-10-28 17:03:20 +0800 | [diff] [blame] | 21 | |
| 22 | #include <gtest/gtest.h> |
| 23 | |
Lei YU | 7c2fbbb | 2019-11-06 14:56:02 +0800 | [diff] [blame] | 24 | namespace fs = std::filesystem; |
| 25 | |
| 26 | using ::testing::_; |
| 27 | using ::testing::An; |
Lei YU | 34fb8bd | 2019-11-07 14:24:20 +0800 | [diff] [blame] | 28 | using ::testing::Pointee; |
Lei YU | 7c2fbbb | 2019-11-06 14:56:02 +0800 | [diff] [blame] | 29 | |
Lei YU | 9ab6d75 | 2019-10-28 17:03:20 +0800 | [diff] [blame] | 30 | using namespace updater; |
| 31 | |
Lei YU | 7c2fbbb | 2019-11-06 14:56:02 +0800 | [diff] [blame] | 32 | class TestUpdater : public ::testing::Test |
| 33 | { |
| 34 | public: |
| 35 | using MockedI2CInterface = i2c::MockedI2CInterface; |
| 36 | using I2CInterface = i2c::I2CInterface; |
| 37 | |
| 38 | TestUpdater() |
| 39 | { |
| 40 | setupDeviceSysfs(); |
| 41 | } |
| 42 | ~TestUpdater() |
| 43 | { |
| 44 | fs::remove_all(tmpDir); |
| 45 | } |
| 46 | |
| 47 | void setupDeviceSysfs() |
| 48 | { |
| 49 | auto tmpPath = fs::temp_directory_path(); |
| 50 | tmpDir = (tmpPath / "test_XXXXXX"); |
| 51 | if (!mkdtemp(tmpDir.data())) |
| 52 | { |
| 53 | throw "Failed to create temp dir"; |
| 54 | } |
| 55 | // Create device path with symbol link |
| 56 | realDevicePath = fs::path(tmpDir) / "devices/3-0068"; |
| 57 | devPath = fs::path(tmpDir) / "i2c"; |
| 58 | fs::create_directories(realDevicePath); |
| 59 | fs::create_directories(realDevicePath / "driver"); |
| 60 | fs::create_directories(devPath); |
| 61 | devPath /= "3-0068"; |
| 62 | fs::create_directory_symlink(realDevicePath, devPath); |
| 63 | } |
| 64 | |
| 65 | MockedI2CInterface& getMockedI2c() |
| 66 | { |
| 67 | return *reinterpret_cast<MockedI2CInterface*>(updater->i2c.get()); |
| 68 | } |
| 69 | |
| 70 | std::shared_ptr<I2CInterface> stolenI2C; |
| 71 | std::unique_ptr<Updater> updater; |
| 72 | fs::path realDevicePath; |
| 73 | fs::path devPath; |
| 74 | std::string tmpDir; |
| 75 | std::string psuInventoryPath = "/com/example/psu"; |
| 76 | std::string imageDir = "/tmp/image/xxx"; |
| 77 | }; |
| 78 | |
| 79 | TEST_F(TestUpdater, ctordtor) |
| 80 | { |
| 81 | updater = std::make_unique<Updater>(psuInventoryPath, devPath, imageDir); |
| 82 | } |
| 83 | |
| 84 | TEST_F(TestUpdater, doUpdate) |
| 85 | { |
| 86 | updater = std::make_unique<Updater>(psuInventoryPath, devPath, imageDir); |
| 87 | updater->createI2CDevice(); |
| 88 | auto& i2c = getMockedI2c(); |
Lei YU | 34fb8bd | 2019-11-07 14:24:20 +0800 | [diff] [blame] | 89 | |
Lei YU | 1d10342 | 2019-11-29 14:00:02 +0800 | [diff] [blame] | 90 | EXPECT_CALL(i2c, write(0xf0, 12, _, I2CInterface::Mode::SMBUS)); |
Lei YU | 34fb8bd | 2019-11-07 14:24:20 +0800 | [diff] [blame] | 91 | EXPECT_CALL(i2c, write(0xf1, An<uint8_t>())); |
| 92 | EXPECT_CALL(i2c, read(0xf1, An<uint8_t&>())); |
Lei YU | 7c2fbbb | 2019-11-06 14:56:02 +0800 | [diff] [blame] | 93 | updater->doUpdate(); |
| 94 | } |