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" 17 #include "test/mocked_i2c_interface.hpp" 18 19 #include <filesystem> 20 21 #include <gtest/gtest.h> 22 23 namespace fs = std::filesystem; 24 25 using ::testing::_; 26 using ::testing::An; 27 28 namespace updater 29 { 30 namespace internal 31 { 32 33 std::string getDeviceName(std::string devPath); 34 std::pair<uint8_t, uint8_t> parseDeviceName(const std::string& devName); 35 36 } // namespace internal 37 } // namespace updater 38 39 using namespace updater; 40 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 EXPECT_CALL(i2c, read(_, An<uint8_t&>())); 100 EXPECT_CALL(i2c, read(_, An<uint16_t&>())); 101 EXPECT_CALL(i2c, read(_, An<uint8_t&>(), _)); 102 updater->doUpdate(); 103 } 104 105 TEST_F(TestUpdater, getDeviceName) 106 { 107 auto ret = internal::getDeviceName(""); 108 EXPECT_TRUE(ret.empty()); 109 110 ret = internal::getDeviceName("/sys/bus/i2c/devices/3-0069"); 111 EXPECT_EQ("3-0069", ret); 112 113 ret = internal::getDeviceName("/sys/bus/i2c/devices/3-0069/"); 114 EXPECT_EQ("3-0069", ret); 115 } 116 117 TEST_F(TestUpdater, parseDeviceName) 118 { 119 auto [id, addr] = internal::parseDeviceName("3-0068"); 120 EXPECT_EQ(3, id); 121 EXPECT_EQ(0x68, addr); 122 123 std::tie(id, addr) = internal::parseDeviceName("11-0069"); 124 EXPECT_EQ(11, id); 125 EXPECT_EQ(0x69, addr); 126 127 EXPECT_THROW(internal::parseDeviceName("no-number"), std::invalid_argument); 128 129 EXPECT_DEATH(internal::parseDeviceName("invalid"), ""); 130 } 131