1 /* 2 * Copyright (c) 2022 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 <fcntl.h> 17 #include <gtest/gtest.h> 18 #include <memory> 19 #include <sys/ioctl.h> 20 #include "log/log.h" 21 #include "securec.h" 22 #include "updater/updater_const.h" 23 #include "updater/updater.h" 24 #include "fs_manager/mount.h" 25 #include "misc_info/misc_info.h" 26 #include "updater_main.h" 27 28 using namespace Updater; 29 using namespace std; 30 using namespace testing::ext; 31 32 namespace { 33 constexpr uint32_t MAX_ARG_SIZE = 24; 34 class UpdaterUtilUnitTest : public testing::Test { 35 public: UpdaterUtilUnitTest()36 UpdaterUtilUnitTest() 37 { 38 std::cout<<"UpdaterUtilUnitTest()"; 39 } ~UpdaterUtilUnitTest()40 ~UpdaterUtilUnitTest() {} 41 SetUpTestCase(void)42 static void SetUpTestCase(void) {} TearDownTestCase(void)43 static void TearDownTestCase(void) {} SetUp()44 void SetUp() {} TearDown()45 void TearDown() {} TestBody()46 void TestBody() {} 47 }; 48 49 HWTEST_F(UpdaterUtilUnitTest, DeleteUpdaterPath, TestSize.Level1) 50 { 51 std::string path = "/data/test/test/test"; 52 bool ret = DeleteUpdaterPath(path); 53 EXPECT_EQ(ret, true); 54 55 path = "/data/test"; 56 ret = DeleteUpdaterPath(path); 57 EXPECT_EQ(ret, true); 58 } 59 60 HWTEST_F(UpdaterUtilUnitTest, ClearMisc, TestSize.Level1) 61 { 62 bool ret = ClearMisc(); 63 EXPECT_EQ(ret, true); 64 } 65 66 HWTEST_F(UpdaterUtilUnitTest, IsSDCardExist, TestSize.Level1) 67 { 68 std::string sdcardStr = ""; 69 bool ret = IsSDCardExist(sdcardStr); 70 EXPECT_EQ(ret, false); 71 } 72 73 HWTEST_F(UpdaterUtilUnitTest, GetBootMode, TestSize.Level1) 74 { 75 int mode = BOOT_UPDATER; 76 int ret = GetBootMode(mode); 77 EXPECT_EQ(ret, 0); 78 } 79 80 HWTEST_F(UpdaterUtilUnitTest, ParseParams, TestSize.Level1) 81 { 82 UpdateMessage boot {}; 83 std::string commandMsg = ""; 84 std::string updateMsg = ""; 85 const std::string commandFile = "/data/updater/command"; 86 auto fp = std::unique_ptr<FILE, decltype(&fclose)>(fopen(commandFile.c_str(), "wb"), fclose); 87 EXPECT_NE(fp, nullptr); 88 EXPECT_EQ(strncpy_s(boot.command, sizeof(boot.command) - 1, commandMsg.c_str(), commandMsg.size()), 0); 89 EXPECT_EQ(strncpy_s(boot.update, sizeof(boot.update) - 1, updateMsg.c_str(), updateMsg.size()), 0); 90 bool bRet = WriteUpdaterMessage(commandFile, boot); 91 EXPECT_EQ(bRet, true); 92 char **argv = new char*[1]; 93 argv[0] = new char[MAX_ARG_SIZE]; 94 std::string str = "./UpdaterMain"; 95 EXPECT_EQ(strncpy_s(argv[0], MAX_ARG_SIZE, str.c_str(), str.size()), 0); 96 int argc = 1; 97 std::vector<std::string> args = ParseParams(argc, argv); 98 std::string res = ""; 99 for (auto s : args) { 100 res += s; 101 } 102 EXPECT_EQ("./UpdaterMain", res); 103 104 commandMsg = "boot_updater"; 105 updateMsg = "--update_package=updater_full.zip"; 106 EXPECT_EQ(strncpy_s(boot.command, sizeof(boot.command) - 1, commandMsg.c_str(), commandMsg.size()), 0); 107 EXPECT_EQ(strncpy_s(boot.update, sizeof(boot.update) - 1, updateMsg.c_str(), updateMsg.size()), 0); 108 bRet = WriteUpdaterMessage(commandFile, boot); 109 EXPECT_EQ(bRet, true); 110 111 args = ParseParams(argc, argv); 112 res = ""; 113 for (auto s : args) { 114 res += s; 115 } 116 EXPECT_EQ("./UpdaterMain--update_package=updater_full.zip", res); 117 } 118 }