1/* 2* Copyright (c) 2021 Huawei Device Co., Ltd. 3* Licensed under the Apache License, Version 2.0 (the "License"); 4* you may not use this file except in compliance with the License. 5* You may obtain a copy of the License at 6* 7* http://www.apache.org/licenses/LICENSE-2.0 8* 9* Unless required by applicable law or agreed to in writing, software 10* distributed under the License is distributed on an "AS IS" BASIS, 11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12* See the License for the specific language governing permissions and 13* limitations under the License. 14*/ 15 16#include <gmock/gmock.h> 17#include <gtest/gtest.h> 18#include <iostream> 19#include <fstream> 20#include <vector> 21#include <unittest_comm.h> 22#include "utils.h" 23 24using namespace Updater; 25using namespace testing::ext; 26using namespace std; 27 28namespace UpdaterUt { 29class UtilsUnitTest : public testing::Test { 30public: 31 static void SetUpTestCase(void) {}; 32 static void TearDownTestCase(void) {}; 33 void SetUp() {}; 34 void TearDown() {}; 35}; 36 37HWTEST_F(UtilsUnitTest, updater_utils_test_001, TestSize.Level0) 38{ 39 string emptyStr = Utils::Trim(""); 40 EXPECT_STREQ(emptyStr.c_str(), ""); 41 emptyStr = Utils::Trim(" "); 42 EXPECT_STREQ(emptyStr.c_str(), ""); 43 emptyStr = Utils::Trim("aa "); 44 EXPECT_STREQ(emptyStr.c_str(), "aa"); 45} 46 47HWTEST_F(UtilsUnitTest, updater_utils_test_002, TestSize.Level0) 48{ 49 uint8_t a[1] = {0}; 50 a[0] = 1; 51 string newStr = Utils::ConvertSha256Hex(a, 1); 52 EXPECT_STREQ(newStr.c_str(), "01"); 53} 54 55HWTEST_F(UtilsUnitTest, updater_utils_test_003, TestSize.Level0) 56{ 57 string str = "aaa\nbbb"; 58 vector<string> newStr = Utils::SplitString(str, "\n"); 59 EXPECT_EQ(newStr[0], "aaa"); 60 EXPECT_EQ(newStr[1], "bbb"); 61} 62 63HWTEST_F(UtilsUnitTest, updater_utils_test_004, TestSize.Level0) 64{ 65 EXPECT_EQ(Utils::MkdirRecursive("/data/xx?xx", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH), 0); 66} 67 68HWTEST_F(UtilsUnitTest, updater_utils_test_005, TestSize.Level0) 69{ 70 string input = ""; 71 int output = Utils::String2Int<int>(input, 10); 72 EXPECT_EQ(output, 0); 73 input = "0x01"; 74 output = Utils::String2Int<int>(input, 10); 75 EXPECT_EQ(output, 1); 76} 77 78HWTEST_F(UtilsUnitTest, updater_utils_test_006, TestSize.Level0) 79{ 80 std::vector<std::string> files; 81 string path = "/data/updater/log"; 82 Utils::SaveLogs(); 83 Utils::CompressLogs("/data/updater/log/updater_log_test"); 84 EXPECT_NE(Utils::GetFilesFromDirectory(path, files, true), -1); 85} 86 87HWTEST_F(UtilsUnitTest, RemoveDirTest, TestSize.Level0) 88{ 89 string path = ""; 90 EXPECT_EQ(Utils::RemoveDir(path), false); 91 path = TEST_PATH_FROM + "../utils/nonExistDir"; 92 EXPECT_EQ(Utils::RemoveDir(path), false); 93 path = "/data/updater/rmDir"; 94 int ret = mkdir(path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); 95 if (ret == 0) { 96 ofstream tmpFile; 97 string filePath = path + "/tmpFile"; 98 tmpFile.open(filePath.c_str()); 99 if (tmpFile.is_open()) { 100 tmpFile.close(); 101 EXPECT_EQ(Utils::RemoveDir(path), true); 102 } 103 } 104} 105 106HWTEST_F(UtilsUnitTest, IsUpdaterMode, TestSize.Level0) 107{ 108 EXPECT_EQ(Utils::IsUpdaterMode(), false); 109} 110 111HWTEST_F(UtilsUnitTest, IsFileExist, TestSize.Level0) 112{ 113 EXPECT_EQ(Utils::IsFileExist("/bin/test_updater"), false); 114 EXPECT_EQ(Utils::IsFileExist("/data/updater/updater/etc/fstab.ut.updater"), true); 115} 116 117HWTEST_F(UtilsUnitTest, IsDirExist, TestSize.Level0) 118{ 119 EXPECT_EQ(Utils::IsDirExist("/bin/test_updater"), false); 120 EXPECT_EQ(Utils::IsDirExist("/bin"), true); 121 EXPECT_EQ(Utils::IsDirExist("/bin/"), true); 122} 123 124HWTEST_F(UtilsUnitTest, CopyUpdaterLogsTest, TestSize.Level0) 125{ 126 const std::string sLog = "/data/updater/main_data/updater.tab"; 127 const std::string dLog = "/data/updater/main_data/ut_dLog.txt"; 128 bool ret = Utils::CopyUpdaterLogs(sLog, dLog); 129 EXPECT_EQ(ret, true); 130 unlink(dLog.c_str()); 131} 132 133HWTEST_F(UtilsUnitTest, GetDirSizeForFileTest, TestSize.Level0) 134{ 135 const std::string testNoPath = "xxx"; 136 long long int ret = Utils::GetDirSizeForFile(testNoPath); 137 EXPECT_EQ(ret, -1); 138 const std::string testVaildPath = "xxx/xxx"; 139 ret = Utils::GetDirSizeForFile(testVaildPath); 140 EXPECT_EQ(ret, 0); 141 const std::string testPath = "/data/updater/updater/etc/fstab.ut.updater"; 142 ret = Utils::GetDirSizeForFile(testPath); 143 EXPECT_EQ(ret, 827); // 827 : file size 144} 145 146HWTEST_F(UtilsUnitTest, GetTagValInStrTest, TestSize.Level0) 147{ 148 const std::string tag = "abc"; 149 std::string ret = ""; 150 Utils::GetTagValInStr("", tag, ret); 151 EXPECT_EQ(ret, ""); 152 Utils::GetTagValInStr("abcde=12", tag, ret); 153 EXPECT_EQ(ret, ""); 154 Utils::GetTagValInStr("abc=12", tag, ret); 155 EXPECT_EQ(ret, "12"); 156} 157 158HWTEST_F(UtilsUnitTest, IsValidHexTest, TestSize.Level0) 159{ 160 bool ret = Utils::IsValidHexStr("a1"); 161 EXPECT_EQ(ret, true); 162 ret = Utils::IsValidHexStr("1*"); 163 EXPECT_EQ(ret, false); 164 ret = Utils::IsValidHexStr("ABCDEF"); 165 EXPECT_EQ(ret, true); 166} 167 168HWTEST_F(UtilsUnitTest, TrimStringTest, TestSize.Level0) 169{ 170 std::string ret = "abc"; 171 Utils::TrimString(ret); 172 EXPECT_EQ(ret, "abc"); 173 ret = "abc\r\n"; 174 Utils::TrimString(ret); 175 EXPECT_EQ(ret, "abc"); 176} 177 178HWTEST_F(UtilsUnitTest, TrimUpdateMode, TestSize.Level0) 179{ 180 const std::string updateMode = "--update_package="; 181 EXPECT_EQ(Utils::TrimUpdateMode(updateMode), "update_package"); 182 const std::string sdcardMode = "--sdcard_update"; 183 EXPECT_EQ(Utils::TrimUpdateMode(sdcardMode), "sdcard_update"); 184 const std::string intralMode = "--sdcard_intral_update"; 185 EXPECT_EQ(Utils::TrimUpdateMode(intralMode), "sdcard_intral_update"); 186 const std::string emptyStr = ""; 187 EXPECT_EQ(Utils::TrimUpdateMode(emptyStr), ""); 188} 189} // updater_ut 190