blob: d2dfd11f76b4a9db61d966415649c21eae816678 [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;
27
Lei YU9ab6d752019-10-28 17:03:20 +080028namespace updater
29{
30namespace internal
31{
32
33std::string getDeviceName(std::string devPath);
Lei YU7c2fbbb2019-11-06 14:56:02 +080034std::pair<uint8_t, uint8_t> parseDeviceName(const std::string& devName);
Lei YU9ab6d752019-10-28 17:03:20 +080035
36} // namespace internal
37} // namespace updater
38
39using namespace updater;
40
Lei YU7c2fbbb2019-11-06 14:56:02 +080041class 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
88TEST_F(TestUpdater, ctordtor)
89{
90 updater = std::make_unique<Updater>(psuInventoryPath, devPath, imageDir);
91}
92
93TEST_F(TestUpdater, doUpdate)
94{
95 updater = std::make_unique<Updater>(psuInventoryPath, devPath, imageDir);
96 updater->createI2CDevice();
97 auto& i2c = getMockedI2c();
Lei YU92e89eb2019-11-06 18:08:25 +080098 EXPECT_CALL(i2c, read(An<uint8_t&>()));
Lei YU7c2fbbb2019-11-06 14:56:02 +080099 EXPECT_CALL(i2c, read(_, An<uint8_t&>()));
Lei YU92e89eb2019-11-06 18:08:25 +0800100 EXPECT_CALL(i2c, read(_, An<uint16_t&>()));
101 EXPECT_CALL(i2c, read(_, An<uint8_t&>(), _));
Lei YU7c2fbbb2019-11-06 14:56:02 +0800102 updater->doUpdate();
103}
104
105TEST_F(TestUpdater, getDeviceName)
Lei YU9ab6d752019-10-28 17:03:20 +0800106{
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}
Lei YU7c2fbbb2019-11-06 14:56:02 +0800116
117TEST_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}