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