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 24 using namespace Updater; 25 using namespace testing::ext; 26 using namespace std; 27 28 namespace UpdaterUt { 29 class UtilsUnitTest : public testing::Test { 30 public: SetUpTestCase(void)31 static void SetUpTestCase(void) {}; TearDownTestCase(void)32 static void TearDownTestCase(void) {}; SetUp()33 void SetUp() {}; TearDown()34 void TearDown() {}; 35 }; 36 37 HWTEST_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 47 HWTEST_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 55 HWTEST_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 63 HWTEST_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 68 HWTEST_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 78 HWTEST_F(UtilsUnitTest, updater_utils_test_006, TestSize.Level0) 79 { 80 std::vector<std::string> files; 81 string path = "/data"; 82 EXPECT_NE(Utils::GetFilesFromDirectory(path, files, true), -1); 83 } 84 85 HWTEST_F(UtilsUnitTest, RemoveDirTest, TestSize.Level0) 86 { 87 string path = ""; 88 EXPECT_EQ(Utils::RemoveDir(path), false); 89 path = TEST_PATH_FROM + "../utils/nonExistDir"; 90 EXPECT_EQ(Utils::RemoveDir(path), false); 91 path = "/data/updater/rmDir"; 92 int ret = mkdir(path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); 93 if (ret == 0) { 94 ofstream tmpFile; 95 string filePath = path + "/tmpFile"; 96 tmpFile.open(filePath.c_str()); 97 if (tmpFile.is_open()) { 98 tmpFile.close(); 99 EXPECT_EQ(Utils::RemoveDir(path), true); 100 } 101 } 102 } 103 104 HWTEST_F(UtilsUnitTest, IsUpdaterMode, TestSize.Level0) 105 { 106 EXPECT_EQ(Utils::IsUpdaterMode(), false); 107 } 108 109 HWTEST_F(UtilsUnitTest, IsFileExist, TestSize.Level0) 110 { 111 EXPECT_EQ(Utils::IsFileExist("/bin/test_updater"), false); 112 EXPECT_EQ(Utils::IsFileExist("/bin/updater_binary"), true); 113 } 114 115 HWTEST_F(UtilsUnitTest, IsDirExist, TestSize.Level0) 116 { 117 EXPECT_EQ(Utils::IsDirExist("/bin/test_updater"), false); 118 EXPECT_EQ(Utils::IsDirExist("/bin"), true); 119 EXPECT_EQ(Utils::IsDirExist("/bin/"), true); 120 } 121 } // updater_ut 122