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 "../version.hpp" 18 #include "mocked_i2c_interface.hpp" 19 20 #include <filesystem> 21 22 #include <gtest/gtest.h> 23 24 namespace fs = std::filesystem; 25 26 using ::testing::_; 27 using ::testing::An; 28 using ::testing::Pointee; 29 30 using namespace updater; 31 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(); 89 90 EXPECT_CALL(i2c, write(0xf0, 12, _, I2CInterface::Mode::SMBUS)); 91 EXPECT_CALL(i2c, write(0xf1, An<uint8_t>())); 92 EXPECT_CALL(i2c, read(0xf1, An<uint8_t&>())); 93 updater->doUpdate(); 94 } 95