diff --git a/BUILD.gn b/BUILD.gn index ac948c97d5f4803f8fc62fe764ced458deba90d8..4c82fe12f6cd6f6dbd2ebb29dfc9c73f0d6e1e38 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -43,6 +43,7 @@ group("unittest") { "test/unittest:updater_unittest", "test/unittest/applypatch_test:applypatch_unittest", "test/unittest/common/ring_buffer:ring_buffer_test", + "test/unittest/factory_reset_test:factory_reset_unittest", "test/unittest/flashd_test:flashd_unittest", "test/unittest/flashd_test:flashd_utils_unittest", "test/unittest/flow_update/update_bin:bin_flow_update_test", diff --git a/services/factory_reset/factory_reset.cpp b/services/factory_reset/factory_reset.cpp index 570690c9d1aa88f8f9dd50dac44bac0ce1485433..88c6ebc7932a632973a595cc3c9ad020e42b9915 100644 --- a/services/factory_reset/factory_reset.cpp +++ b/services/factory_reset/factory_reset.cpp @@ -17,6 +17,7 @@ #include "log/dump.h" #include "log/log.h" #include "fs_manager/mount.h" +#include "scope_guard.h" namespace Updater { FactoryResetProcess &FactoryResetProcess::GetInstance() @@ -46,27 +47,35 @@ int FactoryResetProcess::FactoryResetFunc(FactoryResetMode mode, const std::stri LOG(ERROR) << "Invalid factory reset tag: " << mode; return 1; } - if (CommonResetPreFunc_ == nullptr || - CommonResetPreFunc_(mode == MENU_WIPE_DATA || mode == FACTORY_WIPE_DATA) != 0) { - LOG(ERROR) << "Failed to erase the security status"; - return -1; - } - if (iter->second(path) != 0) { + int resetStatus = iter->second(path); + ON_SCOPE_EXIT(factoryResetPost) { + if (mode == FACTORY_WIPE_DATA && + (FactoryResetPostFunc_ == nullptr || FactoryResetPostFunc_(resetStatus) != 0)) { + LOG(ERROR) << "FactoryResetPostFunc_ fail"; + } + }; + if (resetStatus != 0) { LOG(ERROR) << "Do factory reset failed! tag: " << mode; return 1; } + if (CommonResetPostFunc_ == nullptr || + CommonResetPostFunc_(mode == MENU_WIPE_DATA || mode == FACTORY_WIPE_DATA) != 0) { + resetStatus = 1; + LOG(ERROR) << "CommonResetPostFunc_ fail"; + return -1; + } return 0; } -static int CommonResetPre(bool flag) +static int CommonResetPost(bool flag) { - LOG(INFO) << "CommonResetPre"; + LOG(INFO) << "CommonResetPost"; return 0; } -void FactoryResetProcess::RegisterCommonResetPreFunc(CommonResetPreFunc ptr) +void FactoryResetProcess::RegisterCommonResetPostFunc(CommonResetPostFunc ptr) { - CommonResetPreFunc_ = std::move(ptr); + CommonResetPostFunc_ = std::move(ptr); } static int FactoryResetPre() @@ -122,15 +131,12 @@ int FactoryResetProcess::DoFactoryReset(const std::string &path) } LOG(INFO) << "Factory level FactoryReset status:" << resetStatus; - if (FactoryResetPostFunc_ == nullptr || FactoryResetPostFunc_(resetStatus) != 0) { - LOG(ERROR) << "FactoryResetPostFunc_ fail"; - } return resetStatus; } -extern "C" __attribute__((constructor)) void RegisterCommonResetPreFunc(void) +extern "C" __attribute__((constructor)) void RegisterCommonResetPostFunc(void) { - FactoryResetProcess::GetInstance().RegisterCommonResetPreFunc(CommonResetPre); + FactoryResetProcess::GetInstance().RegisterCommonResetPostFunc(CommonResetPost); } extern "C" __attribute__((constructor)) void RegisterFactoryResetPreFunc(void) diff --git a/services/factory_reset/factory_reset.h b/services/factory_reset/factory_reset.h index 192df9cc06fb933ecd1103f282abeb60ac733858..9892d0ac62e1837fb181864388a2aa8b52031971 100644 --- a/services/factory_reset/factory_reset.h +++ b/services/factory_reset/factory_reset.h @@ -22,7 +22,7 @@ #include "updater_main.h" namespace Updater { -using CommonResetPreFunc = std::function; +using CommonResetPostFunc = std::function; using FactoryResetPreFunc = std::function; using FactoryResetPostFunc = std::function; class FactoryResetProcess { @@ -33,13 +33,15 @@ public: static FactoryResetProcess &GetInstance(); using ResetFunc = std::function; - void RegisterCommonResetPreFunc(CommonResetPreFunc ptr); + void RegisterCommonResetPostFunc(CommonResetPostFunc ptr); void RegisterFactoryResetPreFunc(FactoryResetPreFunc ptr); void RegisterFactoryResetPostFunc(FactoryResetPostFunc ptr); int FactoryResetFunc(FactoryResetMode mode, const std::string &path); +#ifndef UPDATER_UT private: - CommonResetPreFunc CommonResetPreFunc_ = nullptr; +#endif + CommonResetPostFunc CommonResetPostFunc_ = nullptr; FactoryResetPreFunc FactoryResetPreFunc_ = nullptr; FactoryResetPostFunc FactoryResetPostFunc_ = nullptr; std::unordered_map resetTab_; diff --git a/services/include/updater/updater_const.h b/services/include/updater/updater_const.h index 9466c8bd030a7c5d1e4e7df692e23c30109bd7e6..672f638388dc91b2074ff44b267b6e4de4c86afe 100644 --- a/services/include/updater/updater_const.h +++ b/services/include/updater/updater_const.h @@ -51,11 +51,13 @@ constexpr const char *OTA_MODE = "update_package"; constexpr const char *USB_MODE = "usb_update"; constexpr const char *SDCARD_INTRAL_MODE = "sdcard_intral_update"; constexpr const char *UPDATRE_SCRIPT_ZIP = "/etc/updater_script.zip"; +constexpr const char *FACTORY_INTERNAL_MODE = "factory_internal_update"; // sd update ext mode constexpr const char *SDCARD_NORMAL_UPDATE = "sdUpdate"; constexpr const char *SDCARD_UPDATE_FROM_DEV = "sdUpdateFromDev"; constexpr const char *SDCARD_MAINIMG = "mainUpdate"; +constexpr const char *SDCARD_UPDATE_FROM_DATA = "sdUpdateFromData"; #ifndef UPDATER_UT constexpr const char *SDCARD_CARD_PATH = "/sdcard/updater"; diff --git a/services/ptable_parse/emmc_ptable.cpp b/services/ptable_parse/emmc_ptable.cpp index 8bce4a2a05592d29f370903c42fe68ea15b0c32d..4c398ee62927476f4557eb0cef805d0672542c47 100644 --- a/services/ptable_parse/emmc_ptable.cpp +++ b/services/ptable_parse/emmc_ptable.cpp @@ -123,7 +123,7 @@ bool EmmcPtable::ParsePartitionFromBuffer(uint8_t *ptbImgBuffer, const uint32_t bool EmmcPtable::ParseGptHeaderByEmmc(uint8_t *gptImage, const uint32_t len) { GPTHeaderInfo gptHeaderInfo; - uint32_t blockSize = ptableData_.blockSize; // 4096 + uint32_t blockSize = EMMC_BLOCK_SIZE; (void)memset_s(&gptHeaderInfo, sizeof(GPTHeaderInfo), 0, sizeof(GPTHeaderInfo)); if (!GetPartitionGptHeaderInfo(gptImage + blockSize, blockSize, gptHeaderInfo)) { LOG(ERROR) << "GetPartitionGptHeaderInfo fail"; @@ -138,9 +138,15 @@ bool EmmcPtable::ParseGptHeaderByEmmc(uint8_t *gptImage, const uint32_t len) return PartitionCheckGptHeader(gptImage, len, lbaNum, blockSize, gptHeaderInfo); } -bool EmmcPtable::EmmcReadGpt(uint8_t *ptableData) +bool EmmcPtable::EmmcReadGpt(uint8_t *ptableData, uint32_t len) { uint32_t number = 0; + + partitionInfo_.clear(); + if (!ParseGptHeaderByEmmc(emmcPtnDataInfo_.data, len)) { + LOG(ERROR) << "Primary signature invalid"; + return false; + } for (uint32_t i = 0; i < MAX_PARTITION_NUM; i++) { uint8_t *startLbaOffset = ptableData + GPT_PARTITION_START_LBA_OFFSET; uint8_t *endLbaOffset = ptableData + GPT_PARTITION_START_LBA_OFFSET + GPT_PARTITION_END_LBA_OFFSET; @@ -159,6 +165,8 @@ bool EmmcPtable::EmmcReadGpt(uint8_t *ptableData) LOG(ERROR) << "memcpy guid fail"; } newPtnInfo.startAddr = startLba * LBA_LENGTH; + newPtnInfo.writePath = MMC_BLOCK_DEV_NAME; + newPtnInfo.writeMode = "WRITE_RAW"; /* General algorithm : calculate partition size by lba */ newPtnInfo.partitionSize = (endLba - startLba + 1) * LBA_LENGTH; ptableData += GPT_PARTITION_INFO_LENGTH; @@ -176,7 +184,7 @@ bool EmmcPtable::UpdateCommInitializeGptPartition(uint8_t *gptImage, const uint3 return false; } uint32_t imgBlockSize = ptableData_.lbaLen; // 512 - uint32_t deviceBlockSize = ptableData_.blockSize; // 4096 + uint32_t deviceBlockSize = EMMC_BLOCK_SIZE; uint8_t *gptHeaderStart = gptImage + imgBlockSize; uint8_t *ptableData = gptImage + PROTECTIVE_MBR_SIZE + LBA_LENGTH; /* skip MBR and gpt header */ @@ -204,14 +212,10 @@ bool EmmcPtable::UpdateCommInitializeGptPartition(uint8_t *gptImage, const uint3 return false; } emmcPtnDataInfo_.writeDataLen = len; - if (!ParseGptHeaderByEmmc(emmcPtnDataInfo_.data, len)) { - LOG(ERROR) << "Primary signature invalid"; - return false; - } EmmcPatchGptHeader(emmcPtnDataInfo_, deviceBlockSize); emmcPtnDataInfo_.isGptVaild = true; - return EmmcReadGpt(ptableData); + return EmmcReadGpt(emmcPtnDataInfo_.data + 2 * deviceBlockSize, len); // 2: skip 2 lba length to set gpt entry } bool EmmcPtable::ReadEmmcGptImageToRam() @@ -224,7 +228,7 @@ bool EmmcPtable::ReadEmmcGptImageToRam() int32_t imgLen = GPT_PARTITION_SIZE; auto buf = std::make_unique(imgLen); uint8_t *buffer = buf.get(); - uint32_t deviceBlockSize = ptableData_.blockSize; // 4096 + uint32_t deviceBlockSize = EMMC_BLOCK_SIZE; if (buffer == nullptr) { LOG(ERROR) << "new buffer failed!"; return false; @@ -251,7 +255,7 @@ bool EmmcPtable::ReadEmmcGptImageToRam() emmcPtnDataInfo_.isGptVaild = true; uint8_t *ptableData = buffer + 2 * deviceBlockSize; /* skip MBR and gpt header */ - return EmmcReadGpt(ptableData); + return EmmcReadGpt(ptableData, imgLen); } bool EmmcPtable::LoadPtableFromDevice() @@ -282,7 +286,7 @@ bool EmmcPtable::GetPtableImageBuffer(uint8_t *imageBuf, const uint32_t imgBufSi bool EmmcPtable::EditPartitionBuf(uint8_t *imageBuf, uint64_t imgBufSize, std::vector &modifyList) { - if (imageBuf == nullptr || imgBufSize == 0 || modifyList.empty() == 0) { + if (imageBuf == nullptr || imgBufSize == 0 || modifyList.empty()) { LOG(ERROR) << "input invalid"; return false; } @@ -291,9 +295,9 @@ bool EmmcPtable::EditPartitionBuf(uint8_t *imageBuf, uint64_t imgBufSize, std::v uint8_t *gptHeader = gptImage + imgBlockSize; uint32_t maxPtnCnt = GET_LWORD_FROM_BYTE(&gptHeader[PARTITION_COUNT_OFFSET]); uint32_t ptnEntrySize = GET_LWORD_FROM_BYTE(&gptHeader[PENTRY_SIZE_OFFSET]); - uint32_t gptHeaderLen = EMMC_BLOCK_SIZE; - uint32_t gptSize = static_cast(maxPtnCnt) * ptnEntrySize + imgBlockSize + gptHeaderLen; - uint32_t devDensity = GetDeviceCapacity(); + uint64_t gptHeaderLen = EMMC_BLOCK_SIZE; + uint64_t gptSize = static_cast(maxPtnCnt) * ptnEntrySize + imgBlockSize + gptHeaderLen; + uint64_t devDensity = GetDeviceCapacity(); if (devDensity == 0) { LOG(ERROR) << "get emmc capacity fail"; return false; diff --git a/services/ptable_parse/emmc_ptable.h b/services/ptable_parse/emmc_ptable.h index fcec50f73188981cc2de4d0f004e10c7d5c98865..60963db15c05ab57b6a7aa45dc184fa9781c0aed 100644 --- a/services/ptable_parse/emmc_ptable.h +++ b/services/ptable_parse/emmc_ptable.h @@ -42,7 +42,7 @@ private: static constexpr uint32_t LBA_LENGTH = 512; static constexpr uint32_t GPT_PARTITION_INFO_LENGTH = 128; static constexpr uint32_t PROTECTIVE_MBR_SIZE = 512; - static constexpr uint32_t MIN_EMMC_WRITE_SIZE = 4096; + static constexpr uint32_t MIN_EMMC_WRITE_SIZE = 512; static constexpr uint32_t EMMC_BLOCK_SIZE = 512; struct EmmcPartitionDataInfo { @@ -53,7 +53,7 @@ private: struct EmmcPartitionDataInfo emmcPtnDataInfo_; - bool EmmcReadGpt(uint8_t *ptableData); + bool EmmcReadGpt(uint8_t *ptableData, uint32_t len); bool UpdateCommInitializeGptPartition(uint8_t *gptImage, const uint32_t len); bool ReadEmmcGptImageToRam(); uint64_t GetDeviceCapacity(); diff --git a/services/sdcard_update/sdcard_update.cpp b/services/sdcard_update/sdcard_update.cpp index ef0d4766d73d4642ce729a187c47f3141f9b4efd..f13bf526e67039730474d2b04670c7bcc933db58 100644 --- a/services/sdcard_update/sdcard_update.cpp +++ b/services/sdcard_update/sdcard_update.cpp @@ -98,6 +98,12 @@ UpdaterStatus CheckSdcardPkgs(UpdaterParams &upParams) LOG(INFO) << "get sd card from dev succeed, skip get package from sd card"; return UPDATE_SUCCESS; } + + if (GetSdcardInternalPkgs(upParams) == UPDATE_SUCCESS) { + LOG(INFO) << "get sdcard internal pkgs succeed"; + return UPDATE_SUCCESS; + } + std::string mountPoint = std::string(SDCARD_PATH); std::vector sdcardStr = GetBlockDevicesByMountPoint(mountPoint); if (sdcardStr.empty()) { @@ -125,4 +131,10 @@ UpdaterStatus CheckSdcardPkgs(UpdaterParams &upParams) } return UPDATE_SUCCESS; } + +__attribute__((weak)) UpdaterStatus GetSdcardInternalPkgs(UpdaterParams &upParams) +{ + LOG(INFO) << "not implemented get normal update sdcard pkgs"; + return UPDATE_ERROR; +} } // Updater diff --git a/services/sdcard_update/sdcard_update.h b/services/sdcard_update/sdcard_update.h index 5e607594216f6640d668bcc38cead7fa69a3fdc5..9f238b0375ad9bb6e5bd4dbf4546b4256634d464 100644 --- a/services/sdcard_update/sdcard_update.h +++ b/services/sdcard_update/sdcard_update.h @@ -29,6 +29,7 @@ extern "C" { #endif UpdaterStatus GetSdcardPkgsPath(UpdaterParams &upParams); UpdaterStatus GetSdcardPkgsFromDev(UpdaterParams &upParams); +UpdaterStatus GetSdcardInternalPkgs(UpdaterParams &upParams); #ifdef __cplusplus #if __cplusplus } diff --git a/services/updater.cpp b/services/updater.cpp index 36f90f3fbadff6212942b83c52f46e72164280d7..2bf70ddd83f08363be8f134fb84b801b785a06b4 100644 --- a/services/updater.cpp +++ b/services/updater.cpp @@ -258,7 +258,8 @@ UpdaterStatus DoInstallUpdaterPackage(PkgManager::PkgManagerPtr pkgManager, Upda } if (SetupPartitions(updateMode != SDCARD_UPDATE || upParams.sdExtMode == SDCARD_UPDATE_FROM_DEV || - Utils::CheckUpdateMode(Updater::SDCARD_INTRAL_MODE)) != 0) { + upParams.sdExtMode == SDCARD_UPDATE_FROM_DATA || Utils::CheckUpdateMode(Updater::SDCARD_INTRAL_MODE) || + Utils::CheckUpdateMode(Updater::FACTORY_INTERNAL_MODE)) != 0) { UPDATER_UI_INSTANCE.ShowUpdInfo(TR(UPD_SETPART_FAIL), true); UPDATER_LAST_WORD(UPDATE_ERROR); return UPDATE_ERROR; diff --git a/services/updater_binary/update_image_block.cpp b/services/updater_binary/update_image_block.cpp index 21926f8e4e6f67fd7b1d30646528408936a60af0..811229d50ebad0c086e132f42dcce2e5f41ce08c 100644 --- a/services/updater_binary/update_image_block.cpp +++ b/services/updater_binary/update_image_block.cpp @@ -471,21 +471,26 @@ int32_t UScriptInstructionBlockCheck::Execute(Uscript::UScriptEnv &env, Uscript: return USCRIPT_SUCCESS; } +bool UScriptInstructionShaCheck::IsTargetShaDiff(const std::string &devPath, const ShaInfo &shaInfo) +{ + std::string tgtResultSha = CalculateBlockSha(devPath, shaInfo.targetPairs); + if (tgtResultSha.empty()) { + LOG(WARNING) << "target sha is empty"; + return true; + } + LOG(INFO) << "tgtResultSha: " << tgtResultSha << ", shaInfo.targetSha: " << shaInfo.targetSha; + return (tgtResultSha != shaInfo.targetSha); +} + int UScriptInstructionShaCheck::ExecReadShaInfo(Uscript::UScriptEnv &env, const std::string &devPath, const ShaInfo &shaInfo) { UPDATER_INIT_RECORD; std::string resultSha = CalculateBlockSha(devPath, shaInfo.blockPairs); - std::string tgtResultSha = CalculateBlockSha(devPath, shaInfo.targetPairs); - if (resultSha.empty() && tgtResultSha.empty()) { - LOG(ERROR) << "All sha is empty"; - return USCRIPT_ERROR_EXECUTE; - } - - bool isTargetDiff = tgtResultSha.empty() ? true : (tgtResultSha != shaInfo.targetSha); - if (resultSha != shaInfo.contrastSha && isTargetDiff) { + if (resultSha != shaInfo.contrastSha && IsTargetShaDiff(devPath, shaInfo)) { LOG(ERROR) << "Different sha256, cannot continue"; LOG(ERROR) << "blockPairs:" << shaInfo.blockPairs; + LOG(ERROR) << "resultSha: " << resultSha << ", shaInfo.contrastSha: " << shaInfo.contrastSha; PrintAbnormalBlockHash(devPath, shaInfo.blockPairs); UPDATER_LAST_WORD(devPath.substr(devPath.find_last_of("/") + 1), USCRIPT_ERROR_EXECUTE); env.PostMessage(UPDATER_RETRY_TAG, VERIFY_FAILED_REBOOT); diff --git a/services/updater_binary/update_image_block.h b/services/updater_binary/update_image_block.h index 2006b5ae328e5c19ded8c7809312687d83ec5fc4..fe2e39bd5950742640efa54da2a3fd6c62ead45f 100644 --- a/services/updater_binary/update_image_block.h +++ b/services/updater_binary/update_image_block.h @@ -53,6 +53,7 @@ private: void PrintAbnormalBlockHash(const std::string &devPath, const std::string &blockPairs); std::string CalculateBlockSha(const std::string &devPath, const std::string &blockPairs); int32_t SetShaInfo(Uscript::UScriptContext &context, ShaInfo &shaInfo); + bool IsTargetShaDiff(const std::string &devPath, const ShaInfo &shaInfo); }; } diff --git a/services/updater_main.cpp b/services/updater_main.cpp index b0d12d7da6668b69012280e030260c24c97d1dd2..a3e8526739557e5b3e7aae251da77cc109ea293f 100644 --- a/services/updater_main.cpp +++ b/services/updater_main.cpp @@ -793,7 +793,9 @@ __attribute__((weak)) bool IsNeedWipe() void RebootAfterUpdateSuccess(const UpdaterParams &upParams) { - if (IsNeedWipe() || upParams.sdExtMode == SDCARD_UPDATE_FROM_DEV) { + if (IsNeedWipe() || + upParams.sdExtMode == SDCARD_UPDATE_FROM_DEV || + upParams.sdExtMode == SDCARD_UPDATE_FROM_DATA) { Utils::UpdaterDoReboot("updater", "--user_wipe_data"); return; } diff --git a/services/updater_main.h b/services/updater_main.h index f0019166fe3a2c8e61904e321d5532a3bbcc637d..29e0e41e6bc6644d1af4adc2428f75d5b8f84481 100644 --- a/services/updater_main.h +++ b/services/updater_main.h @@ -26,6 +26,7 @@ enum FactoryResetMode { USER_WIPE_DATA = 0, FACTORY_WIPE_DATA, MENU_WIPE_DATA, + INVALID_MODE, }; int UpdaterMain(int argc, char **argv); diff --git a/services/updater_ui.cpp b/services/updater_ui.cpp index bc0819327a708a9eb35d661f21f1d477d9aa1fc8..c1fa1cbc0b4d39644fc0c0fbdaa367d83ad06f09 100644 --- a/services/updater_ui.cpp +++ b/services/updater_ui.cpp @@ -108,6 +108,7 @@ DEFINE_ASYN_CALLBACK(OnLabelSDCardNoDelayEvt) UpdaterParams upParams; upParams.updateMode = SDCARD_UPDATE; if (auto res = UpdaterFromSdcard(upParams); res != UPDATE_SUCCESS) { + Utils::RemoveUpdateInfoFromMisc("sdcard_update"); GetFacade().ShowLogRes(res == UPDATE_CORRUPT ? TR(LOGRES_VERIFY_FAILED) : TR(LOGRES_UPDATE_FAILED)); GetFacade().ShowFailedPage(); Utils::UsSleep(FAIL_DELAY); diff --git a/test/unittest/factory_reset_test/BUILD.gn b/test/unittest/factory_reset_test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..ea3120e981c2604fceb7067ce4119f0808e39963 --- /dev/null +++ b/test/unittest/factory_reset_test/BUILD.gn @@ -0,0 +1,50 @@ +# Copyright (c) 2024 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//base/update/updater/updater_default_cfg.gni") +import("//build/test.gni") + +updater_path = rebase_path("${updater_absolutely_path}", ".") + +MODULE_OUTPUT_PATH = "updater/updater_test" + +ohos_unittest("factory_reset_unittest") { + defines = [ "UPDATER_UT" ] + testonly = true + module_out_path = MODULE_OUTPUT_PATH + sources = [ + "${updater_path}/services/factory_reset/factory_reset.cpp", + "factory_reset_unittest.cpp", + ] + + include_dirs = [ + "${updater_path}/utils/include", + "${updater_path}/services", + "${updater_path}/services/include", + "${updater_path}/services/include/package", + "${updater_path}/services/factory_reset/", + "${updater_path}/interfaces/kits/include/", + "${updater_path}/services/common", + ] + + deps = [ + "${updater_path}/services/fs_manager:libfsmanager", + "${updater_path}/services/log:libupdaterlog", + "${updater_path}/utils:libutils", + ] + + external_deps = [ "init:libbegetutil_static" ] + configs = [ "${updater_path}/test/unittest:utest_config" ] + install_enable = true + part_name = "updater" +} diff --git a/test/unittest/factory_reset_test/factory_reset_unittest.cpp b/test/unittest/factory_reset_test/factory_reset_unittest.cpp new file mode 100644 index 0000000000000000000000000000000000000000..25bb4e3d191b1046cc2b2e28793134c6eb1c3166 --- /dev/null +++ b/test/unittest/factory_reset_test/factory_reset_unittest.cpp @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2024-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "factory_reset.h" + +using namespace Updater; +using namespace testing::ext; + +namespace { +class FactoryResetUnitTest : public testing::Test { +public: + FactoryResetUnitTest() + { + std::cout<<"FactoryResetUnitTest()"; + } + ~FactoryResetUnitTest() {} + FactoryResetProcess* factoryResetProcess; + std::unordered_map resetTab_; + std::function CommonResetPostFunc_; + + static void SetUpTestCase(void) {} + static void TearDownTestCase(void) {} + void SetUp() + { + factoryResetProcess = new FactoryResetProcess(); + factoryResetProcess->resetTab_ = resetTab_; + factoryResetProcess->CommonResetPostFunc_ = CommonResetPostFunc_; + } + void TearDown() + { + delete factoryResetProcess; + factoryResetProcess = nullptr; + } + void TestBody() {} +}; + +HWTEST_F(FactoryResetUnitTest, FactoryResetFunc01, TestSize.Level0) +{ + int ret = factoryResetProcess->GetInstance().FactoryResetFunc(FactoryResetMode::INVALID_MODE, "/data"); + EXPECT_EQ(ret, 1); +} + +HWTEST_F(FactoryResetUnitTest, FactoryResetFunc02, TestSize.Level0) +{ + resetTab_[FactoryResetMode::USER_WIPE_DATA] = [](const std::string &) { return 0; }; + int ret = factoryResetProcess->GetInstance().FactoryResetFunc(FactoryResetMode::USER_WIPE_DATA, "/data"); + EXPECT_EQ(ret, 1); +} + +HWTEST_F(FactoryResetUnitTest, FactoryResetFunc03, TestSize.Level0) +{ + CommonResetPostFunc_ = [](bool) { return 0; }; + resetTab_[FactoryResetMode::USER_WIPE_DATA] = [](const std::string &) { return 0; }; + int ret = factoryResetProcess->GetInstance().FactoryResetFunc(FactoryResetMode::USER_WIPE_DATA, "/data"); + EXPECT_EQ(ret, 1); +} + +HWTEST_F(FactoryResetUnitTest, FactoryResetFunc04, TestSize.Level0) +{ + resetTab_[FactoryResetMode::FACTORY_WIPE_DATA] = [](const std::string &) { return 0; }; + int ret = factoryResetProcess->GetInstance().FactoryResetFunc(FactoryResetMode::FACTORY_WIPE_DATA, "/data"); + EXPECT_EQ(ret, 1); +} + +HWTEST_F(FactoryResetUnitTest, FactoryResetFunc05, TestSize.Level0) +{ + CommonResetPostFunc_ = [](bool) { return 0; }; + resetTab_[FactoryResetMode::FACTORY_WIPE_DATA] = [](const std::string &) { return 0; }; + int ret = factoryResetProcess->GetInstance().FactoryResetFunc(FactoryResetMode::FACTORY_WIPE_DATA, "/data"); + EXPECT_EQ(ret, 1); +} + +HWTEST_F(FactoryResetUnitTest, FactoryResetFunc06, TestSize.Level0) +{ + resetTab_[FactoryResetMode::MENU_WIPE_DATA] = [](const std::string &) { return 0; }; + int ret = factoryResetProcess->GetInstance().FactoryResetFunc(FactoryResetMode::MENU_WIPE_DATA, "/data"); + EXPECT_EQ(ret, 1); +} + +HWTEST_F(FactoryResetUnitTest, FactoryResetFunc07, TestSize.Level0) +{ + CommonResetPostFunc_ = [](bool) { return 0; }; + resetTab_[FactoryResetMode::MENU_WIPE_DATA] = [](const std::string &) { return 0; }; + int ret = factoryResetProcess->GetInstance().FactoryResetFunc(FactoryResetMode::MENU_WIPE_DATA, "/data"); + EXPECT_EQ(ret, 1); +} +} \ No newline at end of file diff --git a/test/unittest/test_data/ohos_test.xml b/test/unittest/test_data/ohos_test.xml index 9389c6f674117588ed65a57310907d9d999d956a..65bfbb0db66fb398b44c0f795a21ced6cc28d2b0 100644 --- a/test/unittest/test_data/ohos_test.xml +++ b/test/unittest/test_data/ohos_test.xml @@ -203,6 +203,8 @@