| #include "include/common_utility.hpp" |
| #include "mock_dbus_utility.hpp" |
| |
| #include <gmock/gmock.h> |
| #include <gtest/gtest.h> |
| |
| class CommonUtilityTest : public ::testing::Test |
| { |
| protected: |
| fs::path tempDir; |
| |
| void SetUp() override |
| { |
| tempDir = fs::temp_directory_path() / "common_utility_test"; |
| fs::create_directory(tempDir); |
| } |
| |
| void TearDown() override |
| { |
| fs::remove_all(tempDir); |
| } |
| |
| static bool isStringInFile(const fs::path& filePath, |
| const std::string& searchString) |
| { |
| std::ifstream file(filePath); |
| std::string line; |
| while (std::getline(file, line)) |
| { |
| if (line.find(searchString) != std::string::npos) |
| { |
| return true; |
| } |
| } |
| return false; |
| } |
| }; |
| |
| // Tests for findFiles |
| TEST_F(CommonUtilityTest, FindFiles_MatchesFiles) |
| { |
| fs::path file1 = tempDir / "testFile1.txt"; |
| fs::path file2 = tempDir / "testFile2.txt"; |
| fs::path file3 = tempDir / "otherFile.log"; |
| std::ofstream(file1.string()).put('a'); |
| std::ofstream(file2.string()).put('b'); |
| std::ofstream(file3.string()).put('c'); |
| |
| auto foundFiles = util::findFiles(tempDir, "testFile"); |
| |
| ASSERT_EQ(foundFiles.size(), 2); |
| EXPECT_THAT(foundFiles, ::testing::UnorderedElementsAre(file1, file2)); |
| } |
| |
| TEST_F(CommonUtilityTest, FindFiles_NoMatch) |
| { |
| fs::path file1 = tempDir / "testFile1.txt"; |
| fs::path file2 = tempDir / "testFile2.txt"; |
| std::ofstream(file1.string()).put('a'); |
| std::ofstream(file2.string()).put('b'); |
| |
| auto foundFiles = util::findFiles(tempDir, "nonexistentFile"); |
| |
| EXPECT_TRUE(foundFiles.empty()); |
| } |
| |
| // Tests for summarizeLog |
| TEST_F(CommonUtilityTest, SummarizeLog_FileWriting) |
| { |
| EXPECT_CALL(*getDbusUtil(), dbusAddStoredLog(::testing::_)).Times(1); |
| |
| fs::path logFileDir = tempDir / "currentBoot"; |
| const std::string logFilePrefix = "testLog.log"; |
| size_t fileCount = 0; |
| |
| if (fs::exists(logFileDir)) |
| { |
| for (const auto& entry : fs::directory_iterator(logFileDir)) |
| { |
| if (entry.is_regular_file() && |
| entry.path().filename().string().starts_with(logFilePrefix)) |
| { |
| fileCount++; |
| } |
| } |
| } |
| |
| const std::string logFilename = logFilePrefix + "_" + |
| std::to_string(fileCount); |
| std::string logMessage = "Test log message"; |
| |
| util::summarizeLog(getDbusUtil(), logMessage, tempDir, logFilePrefix); |
| std::cerr << logFileDir / logFilename << "<<LogFile\n"; |
| |
| ASSERT_TRUE(isStringInFile(logFileDir / logFilename, "Test log message")); |
| } |
| |
| TEST_F(CommonUtilityTest, GetProperty_Success) |
| { |
| auto [success, value] = util::getProperty( |
| "some.service", "/some/object", "some.interface", "some.property"); |
| |
| EXPECT_FALSE(success); |
| } |
| |
| TEST_F(CommonUtilityTest, I2cOpenDevice_Failure) |
| { |
| int fd = util::i2cOpenDevice(1, 0x50); |
| EXPECT_EQ(fd, -1); |
| } |
| |
| TEST_F(CommonUtilityTest, I2cWriteRead_Failure) |
| { |
| std::vector<uint8_t> writeBuf = {0x00, 0x01}; |
| auto result = util::i2cWriteRead(1, 0x50, writeBuf, writeBuf.size(), 2); |
| EXPECT_TRUE(result.empty()); |
| } |
| |
| TEST_F(CommonUtilityTest, I2cReadByte_Failure) |
| { |
| uint8_t result = util::i2cReadByte(1, 0x50, 0x00); |
| EXPECT_EQ(result, 0); |
| } |
| |
| TEST_F(CommonUtilityTest, I2cReadBytes_Failure) |
| { |
| auto result = util::i2cReadBytes(1, 0x50, 0x00, 2); |
| EXPECT_TRUE(result.empty()); |
| } |
| |
| TEST_F(CommonUtilityTest, ConvertFromVoutLinear) |
| { |
| uint16_t value = 0x0200; |
| int8_t exponent = 6; |
| double result = util::convertFromVoutLinear(value, exponent); |
| EXPECT_NEAR(result, 32768.0, 1e-5); |
| } |
| |
| TEST_F(CommonUtilityTest, ConvertFromLinear) |
| { |
| uint16_t value = 0x0400; |
| double result = util::convertFromLinear(value); |
| EXPECT_NEAR(result, 1024.0, 1e-5); |
| } |
| |
| TEST_F(CommonUtilityTest, ConvertFromDirect) |
| { |
| uint16_t value = 0x0800; |
| int16_t m = 1; |
| int16_t b = 0; |
| int8_t r = 0; |
| double result = util::convertFromDirect(value, m, b, r); |
| EXPECT_NEAR(result, 2048.0, 1e-5); |
| } |
| |
| TEST_F(CommonUtilityTest, ParseDec_Valid) |
| { |
| json element = "1234"; |
| uint16_t result = util::parseDec(element); |
| EXPECT_EQ(result, 1234); |
| } |
| |
| TEST_F(CommonUtilityTest, ParseDec_Invalid) |
| { |
| json element = "invalid"; |
| uint16_t result = util::parseDec(element); |
| EXPECT_EQ(result, 0); |
| } |
| |
| TEST_F(CommonUtilityTest, ParseHexByteArray_Valid) |
| { |
| json element = {"0x01", "0x02", "0x03"}; |
| auto result = util::parseHexByteArray(element); |
| EXPECT_EQ(result.size(), 3); |
| EXPECT_EQ(result[0], 0x01); |
| EXPECT_EQ(result[1], 0x02); |
| EXPECT_EQ(result[2], 0x03); |
| } |
| |
| TEST_F(CommonUtilityTest, ParseHexByte_Valid) |
| { |
| json element = "0x1A"; |
| uint8_t result = util::parseHexByte(element); |
| EXPECT_EQ(result, 0x1A); |
| } |
| |
| TEST_F(CommonUtilityTest, ParseHexByte_Invalid) |
| { |
| json element = "invalid"; |
| uint8_t result = util::parseHexByte(element); |
| EXPECT_EQ(result, 0); |
| } |
| |
| TEST_F(CommonUtilityTest, BytesIntoWord) |
| { |
| std::string_view upperByte = "0x12"; |
| std::string_view lowerByte = "0x34"; |
| uint16_t result = util::bytesIntoWord(upperByte, lowerByte); |
| EXPECT_EQ(result, 0x1234); |
| } |
| |
| TEST_F(CommonUtilityTest, ByteArrayToHexString) |
| { |
| // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays) |
| uint8_t buf[] = {0x12, 0x34, 0x56}; |
| std::string result = util::byteArrayToHexString(static_cast<uint8_t*>(buf), |
| 3); |
| EXPECT_EQ(result, "0x12 0x34 0x56 "); |
| } |
| |
| TEST_F(CommonUtilityTest, GetCurrentTimestamp) |
| { |
| std::string result = util::getCurrentTimestamp(); |
| EXPECT_FALSE(result.empty()); |
| EXPECT_EQ(result.size(), |
| 19); // Expected timestamp format "YYYY-MM-DD HH:MM:SS" |
| } |