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