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" |
Lei YU | 7c2fbbb | 2019-11-06 14:56:02 +0800 | [diff] [blame^] | 17 | #include "test/mocked_i2c_interface.hpp" |
| 18 | |
| 19 | #include <filesystem> |
Lei YU | 9ab6d75 | 2019-10-28 17:03:20 +0800 | [diff] [blame] | 20 | |
| 21 | #include <gtest/gtest.h> |
| 22 | |
Lei YU | 7c2fbbb | 2019-11-06 14:56:02 +0800 | [diff] [blame^] | 23 | namespace fs = std::filesystem; |
| 24 | |
| 25 | using ::testing::_; |
| 26 | using ::testing::An; |
| 27 | |
Lei YU | 9ab6d75 | 2019-10-28 17:03:20 +0800 | [diff] [blame] | 28 | namespace updater |
| 29 | { |
| 30 | namespace internal |
| 31 | { |
| 32 | |
| 33 | std::string getDeviceName(std::string devPath); |
Lei YU | 7c2fbbb | 2019-11-06 14:56:02 +0800 | [diff] [blame^] | 34 | std::pair<uint8_t, uint8_t> parseDeviceName(const std::string& devName); |
Lei YU | 9ab6d75 | 2019-10-28 17:03:20 +0800 | [diff] [blame] | 35 | |
| 36 | } // namespace internal |
| 37 | } // namespace updater |
| 38 | |
| 39 | using namespace updater; |
| 40 | |
Lei YU | 7c2fbbb | 2019-11-06 14:56:02 +0800 | [diff] [blame^] | 41 | class TestUpdater : public ::testing::Test |
| 42 | { |
| 43 | public: |
| 44 | using MockedI2CInterface = i2c::MockedI2CInterface; |
| 45 | using I2CInterface = i2c::I2CInterface; |
| 46 | |
| 47 | TestUpdater() |
| 48 | { |
| 49 | setupDeviceSysfs(); |
| 50 | } |
| 51 | ~TestUpdater() |
| 52 | { |
| 53 | fs::remove_all(tmpDir); |
| 54 | } |
| 55 | |
| 56 | void setupDeviceSysfs() |
| 57 | { |
| 58 | auto tmpPath = fs::temp_directory_path(); |
| 59 | tmpDir = (tmpPath / "test_XXXXXX"); |
| 60 | if (!mkdtemp(tmpDir.data())) |
| 61 | { |
| 62 | throw "Failed to create temp dir"; |
| 63 | } |
| 64 | // Create device path with symbol link |
| 65 | realDevicePath = fs::path(tmpDir) / "devices/3-0068"; |
| 66 | devPath = fs::path(tmpDir) / "i2c"; |
| 67 | fs::create_directories(realDevicePath); |
| 68 | fs::create_directories(realDevicePath / "driver"); |
| 69 | fs::create_directories(devPath); |
| 70 | devPath /= "3-0068"; |
| 71 | fs::create_directory_symlink(realDevicePath, devPath); |
| 72 | } |
| 73 | |
| 74 | MockedI2CInterface& getMockedI2c() |
| 75 | { |
| 76 | return *reinterpret_cast<MockedI2CInterface*>(updater->i2c.get()); |
| 77 | } |
| 78 | |
| 79 | std::shared_ptr<I2CInterface> stolenI2C; |
| 80 | std::unique_ptr<Updater> updater; |
| 81 | fs::path realDevicePath; |
| 82 | fs::path devPath; |
| 83 | std::string tmpDir; |
| 84 | std::string psuInventoryPath = "/com/example/psu"; |
| 85 | std::string imageDir = "/tmp/image/xxx"; |
| 86 | }; |
| 87 | |
| 88 | TEST_F(TestUpdater, ctordtor) |
| 89 | { |
| 90 | updater = std::make_unique<Updater>(psuInventoryPath, devPath, imageDir); |
| 91 | } |
| 92 | |
| 93 | TEST_F(TestUpdater, doUpdate) |
| 94 | { |
| 95 | updater = std::make_unique<Updater>(psuInventoryPath, devPath, imageDir); |
| 96 | updater->createI2CDevice(); |
| 97 | auto& i2c = getMockedI2c(); |
| 98 | EXPECT_CALL(i2c, read(_, An<uint8_t&>())); |
| 99 | updater->doUpdate(); |
| 100 | } |
| 101 | |
| 102 | TEST_F(TestUpdater, getDeviceName) |
Lei YU | 9ab6d75 | 2019-10-28 17:03:20 +0800 | [diff] [blame] | 103 | { |
| 104 | auto ret = internal::getDeviceName(""); |
| 105 | EXPECT_TRUE(ret.empty()); |
| 106 | |
| 107 | ret = internal::getDeviceName("/sys/bus/i2c/devices/3-0069"); |
| 108 | EXPECT_EQ("3-0069", ret); |
| 109 | |
| 110 | ret = internal::getDeviceName("/sys/bus/i2c/devices/3-0069/"); |
| 111 | EXPECT_EQ("3-0069", ret); |
| 112 | } |
Lei YU | 7c2fbbb | 2019-11-06 14:56:02 +0800 | [diff] [blame^] | 113 | |
| 114 | TEST_F(TestUpdater, parseDeviceName) |
| 115 | { |
| 116 | auto [id, addr] = internal::parseDeviceName("3-0068"); |
| 117 | EXPECT_EQ(3, id); |
| 118 | EXPECT_EQ(0x68, addr); |
| 119 | |
| 120 | std::tie(id, addr) = internal::parseDeviceName("11-0069"); |
| 121 | EXPECT_EQ(11, id); |
| 122 | EXPECT_EQ(0x69, addr); |
| 123 | |
| 124 | EXPECT_THROW(internal::parseDeviceName("no-number"), std::invalid_argument); |
| 125 | |
| 126 | EXPECT_DEATH(internal::parseDeviceName("invalid"), ""); |
| 127 | } |