blob: 9c349267e8953e8554cfd692d30a7779bffd6825 [file] [log] [blame]
Lei YU9ab6d752019-10-28 17:03:20 +08001/**
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 Awadaec61bbd2024-11-04 08:46:20 -060017#include "../version.hpp"
Shawn McCarneyb1216b92020-01-21 15:22:32 -060018#include "mocked_i2c_interface.hpp"
Lei YU7c2fbbb2019-11-06 14:56:02 +080019
20#include <filesystem>
Lei YU9ab6d752019-10-28 17:03:20 +080021
22#include <gtest/gtest.h>
23
Lei YU7c2fbbb2019-11-06 14:56:02 +080024namespace fs = std::filesystem;
25
26using ::testing::_;
27using ::testing::An;
Lei YU34fb8bd2019-11-07 14:24:20 +080028using ::testing::Pointee;
Lei YU7c2fbbb2019-11-06 14:56:02 +080029
Lei YU9ab6d752019-10-28 17:03:20 +080030namespace updater
31{
32namespace internal
33{
34
35std::string getDeviceName(std::string devPath);
Lei YU7c2fbbb2019-11-06 14:56:02 +080036std::pair<uint8_t, uint8_t> parseDeviceName(const std::string& devName);
Lei YU9ab6d752019-10-28 17:03:20 +080037
38} // namespace internal
39} // namespace updater
40
41using namespace updater;
42
Lei YU7c2fbbb2019-11-06 14:56:02 +080043class TestUpdater : public ::testing::Test
44{
45 public:
46 using MockedI2CInterface = i2c::MockedI2CInterface;
47 using I2CInterface = i2c::I2CInterface;
48
49 TestUpdater()
50 {
51 setupDeviceSysfs();
52 }
53 ~TestUpdater()
54 {
55 fs::remove_all(tmpDir);
56 }
57
58 void setupDeviceSysfs()
59 {
60 auto tmpPath = fs::temp_directory_path();
61 tmpDir = (tmpPath / "test_XXXXXX");
62 if (!mkdtemp(tmpDir.data()))
63 {
64 throw "Failed to create temp dir";
65 }
66 // Create device path with symbol link
67 realDevicePath = fs::path(tmpDir) / "devices/3-0068";
68 devPath = fs::path(tmpDir) / "i2c";
69 fs::create_directories(realDevicePath);
70 fs::create_directories(realDevicePath / "driver");
71 fs::create_directories(devPath);
72 devPath /= "3-0068";
73 fs::create_directory_symlink(realDevicePath, devPath);
74 }
75
76 MockedI2CInterface& getMockedI2c()
77 {
78 return *reinterpret_cast<MockedI2CInterface*>(updater->i2c.get());
79 }
80
81 std::shared_ptr<I2CInterface> stolenI2C;
82 std::unique_ptr<Updater> updater;
83 fs::path realDevicePath;
84 fs::path devPath;
85 std::string tmpDir;
86 std::string psuInventoryPath = "/com/example/psu";
87 std::string imageDir = "/tmp/image/xxx";
88};
89
90TEST_F(TestUpdater, ctordtor)
91{
92 updater = std::make_unique<Updater>(psuInventoryPath, devPath, imageDir);
93}
94
95TEST_F(TestUpdater, doUpdate)
96{
97 updater = std::make_unique<Updater>(psuInventoryPath, devPath, imageDir);
98 updater->createI2CDevice();
99 auto& i2c = getMockedI2c();
Lei YU34fb8bd2019-11-07 14:24:20 +0800100
Lei YU1d103422019-11-29 14:00:02 +0800101 EXPECT_CALL(i2c, write(0xf0, 12, _, I2CInterface::Mode::SMBUS));
Lei YU34fb8bd2019-11-07 14:24:20 +0800102 EXPECT_CALL(i2c, write(0xf1, An<uint8_t>()));
103 EXPECT_CALL(i2c, read(0xf1, An<uint8_t&>()));
Lei YU7c2fbbb2019-11-06 14:56:02 +0800104 updater->doUpdate();
105}
106
107TEST_F(TestUpdater, getDeviceName)
Lei YU9ab6d752019-10-28 17:03:20 +0800108{
109 auto ret = internal::getDeviceName("");
110 EXPECT_TRUE(ret.empty());
111
112 ret = internal::getDeviceName("/sys/bus/i2c/devices/3-0069");
113 EXPECT_EQ("3-0069", ret);
114
115 ret = internal::getDeviceName("/sys/bus/i2c/devices/3-0069/");
116 EXPECT_EQ("3-0069", ret);
117}
Lei YU7c2fbbb2019-11-06 14:56:02 +0800118
119TEST_F(TestUpdater, parseDeviceName)
120{
121 auto [id, addr] = internal::parseDeviceName("3-0068");
122 EXPECT_EQ(3, id);
123 EXPECT_EQ(0x68, addr);
124
125 std::tie(id, addr) = internal::parseDeviceName("11-0069");
126 EXPECT_EQ(11, id);
127 EXPECT_EQ(0x69, addr);
128
129 EXPECT_THROW(internal::parseDeviceName("no-number"), std::invalid_argument);
130
131 EXPECT_DEATH(internal::parseDeviceName("invalid"), "");
132}