From dfa18550d432bfc84668e4e14526d77310ec37ae Mon Sep 17 00:00:00 2001 From: sunjiakun Date: Fri, 11 Oct 2024 09:50:05 +0800 Subject: [PATCH 1/2] cherry pick tdd case Signed-off-by: sunjiakun --- .../bms_bundle_installer_test/BUILD.gn | 201 +++++++++ .../bms_bundle_installer_permission_test.cpp | 400 ++++++++++++++++++ .../bms_bundle_installer_test.cpp | 93 +++- .../bms_bundle_kit_service_base_test.cpp | 20 +- 4 files changed, 712 insertions(+), 2 deletions(-) create mode 100644 services/bundlemgr/test/unittest/bms_bundle_installer_test/bms_bundle_installer_permission_test.cpp diff --git a/services/bundlemgr/test/unittest/bms_bundle_installer_test/BUILD.gn b/services/bundlemgr/test/unittest/bms_bundle_installer_test/BUILD.gn index 904912e66f..16c0fd9bf8 100755 --- a/services/bundlemgr/test/unittest/bms_bundle_installer_test/BUILD.gn +++ b/services/bundlemgr/test/unittest/bms_bundle_installer_test/BUILD.gn @@ -1583,6 +1583,206 @@ ohos_unittest("BmsBundleAppServiceFwkInstallerTest") { } } +ohos_unittest("BmsBundleInstallerPermissionTest") { + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + } + use_exceptions = true + module_out_path = module_output_path + include_dirs = [ "//third_party/jsoncpp/include" ] + sources = bundle_mgr_source + sources -= [ "${services_path}/bundlemgr/src/bms_param.cpp" ] + sources += [ "${services_path}/bundlemgr/test/mock/src/bms_param.cpp" ] + sources -= [ "${services_path}/bundlemgr/src/system_ability_helper.cpp" ] + sources += [ + "${services_path}/bundlemgr/src/aot/aot_executor.cpp", + "${services_path}/bundlemgr/src/installd/installd_host_impl.cpp", + "${services_path}/bundlemgr/src/installd/installd_operator.cpp", + "${services_path}/bundlemgr/test/mock/src/installd_service.cpp", + ] + sources -= + [ "${services_path}/bundlemgr/src/bundle_mgr_service_event_handler.cpp" ] + + sources += [ + "${services_path}/bundlemgr/test/mock/src/accesstoken_kit.cpp", + "${services_path}/bundlemgr/test/mock/src/bundle_mgr_service_event_handler.cpp", + "${services_path}/bundlemgr/test/mock/src/installd_permission_mgr.cpp", + "${services_path}/bundlemgr/test/mock/src/mock_status_receiver.cpp", + "${services_path}/bundlemgr/test/mock/src/system_ability_helper.cpp", + ] + + sources += bundle_install_sources + sources -= [ "${services_path}/bundlemgr/src/installd_client.cpp" ] + sources += [ "${services_path}/bundlemgr/test/mock/src/installd_client.cpp" ] + + sources += [ "bms_bundle_installer_permission_test.cpp" ] + + configs = [ "${services_path}/bundlemgr/test:bundlemgr_test_config" ] + cflags = [] + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } + deps = [ + "${bundle_framework_path}/test/sceneProject/unittest/ohos_test:copy_ohos_test", + "${bundle_framework_path}/test/sceneProject/unittest/test_bundle/backupTest:backupTest", + "${bundle_framework_path}/test/sceneProject/unittest/test_bundle/devicetype_error:devicetype_error", + "${bundle_framework_path}/test/sceneProject/unittest/test_bundle/fifth_right:fifth_right", + "${bundle_framework_path}/test/sceneProject/unittest/test_bundle/first_right:first_right", + "${bundle_framework_path}/test/sceneProject/unittest/test_bundle/fourth_right:fourth_right", + "${bundle_framework_path}/test/sceneProject/unittest/test_bundle/moduleJsonTest:moduleJsonTest", + "${bundle_framework_path}/test/sceneProject/unittest/test_bundle/previewTest:previewTest", + "${bundle_framework_path}/test/sceneProject/unittest/test_bundle/rightTest:rightTest", + "${bundle_framework_path}/test/sceneProject/unittest/test_bundle/rightTest1:rightTest1", + "${bundle_framework_path}/test/sceneProject/unittest/test_bundle/second_right:second_right", + "${bundle_framework_path}/test/sceneProject/unittest/test_bundle/seventh_right:seventh_right", + "${bundle_framework_path}/test/sceneProject/unittest/test_bundle/signatureTest:signatureTest", + "${bundle_framework_path}/test/sceneProject/unittest/test_bundle/system_module:system_module", + "${bundle_framework_path}/test/sceneProject/unittest/test_bundle/third_right:third_right", + "${bundle_framework_path}/test/sceneProject/unittest/test_bundle/thumbnailTest:thumbnailTest", + "${bundle_framework_path}/test/sceneProject/unittest/test_bundle/versionTest1:versionTest1", + "${bundle_framework_path}/test/sceneProject/unittest/test_bundle/versionTest2:versionTest2", + "${bundle_framework_path}/test/sceneProject/unittest/test_bundle/versionTest3:versionTest3", + "${bundle_framework_path}/test/sceneProject/unittest/test_bundle/wrong_bundle_name:wrong_bundle_name", + "${core_path}:appexecfwk_core", + ] + deps += bundle_install_deps + + external_deps = [ + "ability_base:want", + "ability_runtime:app_manager", + "access_token:el5_filekey_manager_sdk", + "access_token:libprivacy_sdk", + "access_token:libtokenid_sdk", + "appspawn:hnpapi", + "appverify:libhapverify", + "bounds_checking_function:libsec_shared", + "common_event_service:cesfwk_innerkits", + "eventhandler:libeventhandler", + "hilog:libhilog", + "hitrace:hitrace_meter", + "init:libbegetutil", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] + external_deps += bundle_install_external_deps + defines = [ "BUNDLE_FRAMEWORK_RETURN_FALSE" ] + if (code_signature_enable) { + sources += [ + "${services_path}/bundlemgr/src/aot/aot_sign_data_cache_mgr.cpp", + "${services_path}/bundlemgr/src/code_sign_helper.cpp", + ] + include_dirs += [ "${services_path}/bundlemgr/include" ] + external_deps += [ + "bounds_checking_function:libsec_shared", + "code_signature:libcode_sign_utils", + "ets_runtime:libcompiler_service", + ] + defines += [ "CODE_SIGNATURE_ENABLE" ] + } + + if (configpolicy_enable) { + external_deps += [ "config_policy:configpolicy_util" ] + defines += [ "CONFIG_POLOCY_ENABLE" ] + } + + if (bundle_framework_app_control) { + defines += [ "BUNDLE_FRAMEWORK_APP_CONTROL" ] + sources += [ + "${services_path}/bundlemgr/src/app_control/app_control_manager.cpp", + "${services_path}/bundlemgr/src/app_control/app_control_manager_host_impl.cpp", + "${services_path}/bundlemgr/src/app_control/app_control_manager_rdb.cpp", + "${services_path}/bundlemgr/src/app_control/app_jump_interceptor_event_subscriber.cpp", + "${services_path}/bundlemgr/src/app_control/app_jump_interceptor_manager_rdb.cpp", + ] + include_dirs += [ "${services_path}/bundlemgr/include/app_control" ] + external_deps += [ "c_utils:utils" ] + } + if (current_cpu == "arm64") { + defines += [ "USE_BUNDLE_EXTENSION" ] + } + + if (build_selinux) { + external_deps += [ "selinux_adapter:libhap_restorecon" ] + cflags += [ "-DWITH_SELINUX" ] + } + if (account_enable) { + external_deps += [ "os_account:os_account_innerkits" ] + defines += [ "ACCOUNT_ENABLE" ] + } + if (bundle_framework_free_install) { + sources += aging + sources += free_install + sources += distributed_manager + external_deps += [ + "ability_runtime:ability_manager", + "ability_runtime:app_manager", + "battery_manager:batterysrv_client", + "device_usage_statistics:usagestatsinner", + "display_manager:displaymgr", + "power_manager:powermgr_client", + "syscap_codec:syscap_interface_shared", + ] + defines += [ "BUNDLE_FRAMEWORK_FREE_INSTALL" ] + } + if (global_resmgr_enable) { + defines += [ "GLOBAL_RESMGR_ENABLE" ] + external_deps += [ "resource_management:global_resmgr" ] + } + if (hicollie_enable) { + external_deps += [ "hicollie:libhicollie" ] + defines += [ "HICOLLIE_ENABLE" ] + } + + if (hisysevent_enable) { + sources += [ "${services_path}/bundlemgr/src/inner_event_report.cpp" ] + external_deps += [ "hisysevent:libhisysevent" ] + defines += [ "HISYSEVENT_ENABLE" ] + } + + if (bundle_framework_quick_fix) { + include_dirs += [ "${services_path}/bundlemgr/src/include/quick_fix" ] + sources += quick_fix + defines += [ "BUNDLE_FRAMEWORK_QUICK_FIX" ] + } + + if (storage_service_enable) { + external_deps += [ "storage_service:storage_manager_sa_proxy" ] + defines += [ "STORAGE_SERVICE_ENABLE" ] + } + + external_deps += [ "kv_store:distributeddata_inner" ] + configs += [ "../../../../../services/bundlemgr:rdb_config" ] + external_deps += [ "relational_store:native_rdb" ] + sources += [ + "${services_path}/bundlemgr/src/bundle_data_storage_rdb.cpp", + "${services_path}/bundlemgr/src/preinstall_data_storage_rdb.cpp", + "${services_path}/bundlemgr/src/rdb/bms_rdb_open_callback.cpp", + "${services_path}/bundlemgr/src/rdb/rdb_data_manager.cpp", + ] + if (udmf_enabled) { + defines += [ "BUNDLE_FRAMEWORK_UDMF_ENABLED" ] + external_deps += [ "udmf:utd_client" ] + } + + if (bms_device_info_manager_part_enabled) { + external_deps += [ + "device_info_manager:distributed_device_profile_common", + "device_info_manager:distributed_device_profile_sdk", + ] + defines += [ "BMS_DEVICE_INFO_MANAGER_ENABLE" ] + } + + if (app_domain_verify_enabled) { + external_deps += [ + "app_domain_verify:app_domain_verify_common", + "app_domain_verify:app_domain_verify_mgr_client", + ] + defines += [ "APP_DOMAIN_VERIFY_ENABLED" ] + } +} + group("unittest") { testonly = true @@ -1593,6 +1793,7 @@ group("unittest") { ":BmsBundleInstallDeviceTypeTest", ":BmsBundleInstallDriverTest", ":BmsBundleInstallIpcTest", + ":BmsBundleInstallerPermissionTest", ":BmsBundleInstallerTest", ":BmsBundleOtaUpdateTest", ":BmsMultipleBundleInstallerTest", diff --git a/services/bundlemgr/test/unittest/bms_bundle_installer_test/bms_bundle_installer_permission_test.cpp b/services/bundlemgr/test/unittest/bms_bundle_installer_test/bms_bundle_installer_permission_test.cpp new file mode 100644 index 0000000000..c6e99f6b9a --- /dev/null +++ b/services/bundlemgr/test/unittest/bms_bundle_installer_test/bms_bundle_installer_permission_test.cpp @@ -0,0 +1,400 @@ +/* + * 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. + */ + +#define private public +#define protected public +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef BUNDLE_FRAMEWORK_APP_CONTROL +#include "app_control_manager_host_impl.h" +#include "app_control_constants.h" +#endif +#ifdef APP_DOMAIN_VERIFY_ENABLED +#include "app_domain_verify_mgr_client.h" +#endif +#include "app_service_fwk/app_service_fwk_installer.h" +#include "bundle_info.h" +#include "bundle_installer_host.h" +#include "bundle_mgr_service.h" +#include "directory_ex.h" +#include "install_param.h" +#include "installd/installd_service.h" +#include "installd_client.h" +#include "mock_status_receiver.h" +#include "scope_guard.h" +#include "shared/shared_bundle_installer.h" +#include "system_bundle_installer.h" +#include "want.h" +#include "file_ex.h" + +using namespace testing::ext; +using namespace std::chrono_literals; +using namespace OHOS::AppExecFwk; +using OHOS::DelayedSingleton; + +namespace OHOS { +namespace { +const std::string BUNDLE_NAME = "com.example.l3jsdemo"; +const std::string BUNDLE_DATA_DIR = "/data/app/el2/100/base/com.example.l3jsdemo"; +const std::string BUNDLE_CODE_DIR = "/data/app/el1/bundle/public/com.example.l3jsdemo"; +const int32_t USERID = 100; +const int32_t WAIT_TIME = 5; // init mocked bms +const std::string BUNDLE_LIBRARY_PATH_DIR = "/data/app/el1/bundle/public/com.example.l3jsdemo/libs/arm"; +} // namespace + +class BmsBundleInstallerPermissionTest : public testing::Test { +public: + BmsBundleInstallerPermissionTest(); + ~BmsBundleInstallerPermissionTest(); + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); + bool InstallSystemBundle(const std::string &filePath) const; + bool OTAInstallSystemBundle(const std::string &filePath) const; + void CheckFileExist() const; + void CheckFileNonExist() const; + const std::shared_ptr GetBundleDataMgr() const; + const std::shared_ptr GetBundleInstallerManager() const; + void StopInstalldService() const; + void StopBundleService(); + ErrCode InstallThirdPartyBundle(const std::string &filePath) const; + ErrCode UpdateThirdPartyBundle(const std::string &filePath) const; + ErrCode UnInstallBundle(const std::string &bundleName) const; + void CreateInstallerManager(); + void ClearBundleInfo(); + void ClearDataMgr(); + void ResetDataMgr(); + +private: + std::shared_ptr manager_ = nullptr; + static std::shared_ptr installdService_; + static std::shared_ptr bundleMgrService_; +}; + +std::shared_ptr BmsBundleInstallerPermissionTest::bundleMgrService_ = + DelayedSingleton::GetInstance(); + +std::shared_ptr BmsBundleInstallerPermissionTest::installdService_ = + std::make_shared(); + +BmsBundleInstallerPermissionTest::BmsBundleInstallerPermissionTest() +{} + +BmsBundleInstallerPermissionTest::~BmsBundleInstallerPermissionTest() +{} + +bool BmsBundleInstallerPermissionTest::InstallSystemBundle(const std::string &filePath) const +{ + bundleMgrService_->GetDataMgr()->AddUserId(USERID); + auto installer = std::make_unique(); + InstallParam installParam; + installParam.userId = USERID; + installParam.needSendEvent = false; + installParam.needSavePreInstallInfo = true; + installParam.isPreInstallApp = true; + installParam.noSkipsKill = false; + installParam.copyHapToInstallPath = false; + return installer->InstallSystemBundle( + filePath, installParam, Constants::AppType::SYSTEM_APP) == ERR_OK; +} + +bool BmsBundleInstallerPermissionTest::OTAInstallSystemBundle(const std::string &filePath) const +{ + bundleMgrService_->GetDataMgr()->AddUserId(USERID); + auto installer = std::make_unique(); + std::vector filePaths; + filePaths.push_back(filePath); + InstallParam installParam; + installParam.userId = USERID; + installParam.needSendEvent = false; + installParam.needSavePreInstallInfo = true; + installParam.isPreInstallApp = true; + installParam.noSkipsKill = false; + installParam.copyHapToInstallPath = false; + return installer->OTAInstallSystemBundle( + filePaths, installParam, Constants::AppType::SYSTEM_APP) == ERR_OK; +} + +ErrCode BmsBundleInstallerPermissionTest::InstallThirdPartyBundle(const std::string &filePath) const +{ + bundleMgrService_->GetDataMgr()->AddUserId(USERID); + auto installer = bundleMgrService_->GetBundleInstaller(); + if (!installer) { + EXPECT_FALSE(true) << "the installer is nullptr"; + return ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR; + } + sptr receiver = new (std::nothrow) MockStatusReceiver(); + if (!receiver) { + EXPECT_FALSE(true) << "the receiver is nullptr"; + return ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR; + } + InstallParam installParam; + installParam.userId = USERID; + installParam.installFlag = InstallFlag::NORMAL; + bool result = installer->Install(filePath, installParam, receiver); + EXPECT_TRUE(result); + return receiver->GetResultCode(); +} + +ErrCode BmsBundleInstallerPermissionTest::UpdateThirdPartyBundle(const std::string &filePath) const +{ + bundleMgrService_->GetDataMgr()->AddUserId(USERID); + auto installer = bundleMgrService_->GetBundleInstaller(); + if (!installer) { + EXPECT_FALSE(true) << "the installer is nullptr"; + return ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR; + } + sptr receiver = new (std::nothrow) MockStatusReceiver(); + if (!receiver) { + EXPECT_FALSE(true) << "the receiver is nullptr"; + return ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR; + } + InstallParam installParam; + installParam.userId = USERID; + installParam.installFlag = InstallFlag::REPLACE_EXISTING; + bool result = installer->Install(filePath, installParam, receiver); + EXPECT_TRUE(result); + return receiver->GetResultCode(); +} + +ErrCode BmsBundleInstallerPermissionTest::UnInstallBundle(const std::string &bundleName) const +{ + bundleMgrService_->GetDataMgr()->AddUserId(USERID); + auto installer = bundleMgrService_->GetBundleInstaller(); + if (!installer) { + EXPECT_FALSE(true) << "the installer is nullptr"; + return ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR; + } + sptr receiver = new (std::nothrow) MockStatusReceiver(); + if (!receiver) { + EXPECT_FALSE(true) << "the receiver is nullptr"; + return ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR; + } + InstallParam installParam; + installParam.userId = USERID; + installParam.installFlag = InstallFlag::NORMAL; + bool result = installer->Uninstall(bundleName, installParam, receiver); + EXPECT_TRUE(result); + return receiver->GetResultCode(); +} + +void BmsBundleInstallerPermissionTest::SetUpTestCase() +{ +} + +void BmsBundleInstallerPermissionTest::TearDownTestCase() +{ + bundleMgrService_->OnStop(); +} + +void BmsBundleInstallerPermissionTest::SetUp() +{ + if (!installdService_->IsServiceReady()) { + installdService_->Start(); + } + if (!bundleMgrService_->IsServiceReady()) { + bundleMgrService_->OnStart(); + bundleMgrService_->GetDataMgr()->AddUserId(USERID); + std::this_thread::sleep_for(std::chrono::seconds(WAIT_TIME)); + } +} + +void BmsBundleInstallerPermissionTest::TearDown() +{ + OHOS::ForceRemoveDirectory(BUNDLE_DATA_DIR); + OHOS::ForceRemoveDirectory(BUNDLE_CODE_DIR); + OHOS::ForceRemoveDirectory(BUNDLE_LIBRARY_PATH_DIR); +} + +void BmsBundleInstallerPermissionTest::CheckFileExist() const +{ + int bundleCodeExist = access(BUNDLE_CODE_DIR.c_str(), F_OK); + EXPECT_EQ(bundleCodeExist, 0) << "the bundle code dir does not exists: " << BUNDLE_CODE_DIR; + + int bundleDataExist = access(BUNDLE_DATA_DIR.c_str(), F_OK); + EXPECT_EQ(bundleDataExist, 0) << "the bundle data dir does not exists: " << BUNDLE_DATA_DIR; +} + +void BmsBundleInstallerPermissionTest::CheckFileNonExist() const +{ + int bundleCodeExist = access(BUNDLE_CODE_DIR.c_str(), F_OK); + EXPECT_NE(bundleCodeExist, 0) << "the bundle code dir exists: " << BUNDLE_CODE_DIR; + + int bundleDataExist = access(BUNDLE_DATA_DIR.c_str(), F_OK); + EXPECT_NE(bundleDataExist, 0) << "the bundle data dir exists: " << BUNDLE_DATA_DIR; +} + +const std::shared_ptr BmsBundleInstallerPermissionTest::GetBundleDataMgr() const +{ + return bundleMgrService_->GetDataMgr(); +} + +const std::shared_ptr BmsBundleInstallerPermissionTest::GetBundleInstallerManager() const +{ + return manager_; +} + +void BmsBundleInstallerPermissionTest::ClearDataMgr() +{ + bundleMgrService_->dataMgr_ = nullptr; +} + +void BmsBundleInstallerPermissionTest::ResetDataMgr() +{ + bundleMgrService_->dataMgr_ = std::make_shared(); + EXPECT_NE(bundleMgrService_->dataMgr_, nullptr); +} + +void BmsBundleInstallerPermissionTest::StopInstalldService() const +{ + if (installdService_->IsServiceReady()) { + installdService_->Stop(); + InstalldClient::GetInstance()->ResetInstalldProxy(); + } +} + +void BmsBundleInstallerPermissionTest::StopBundleService() +{ + if (bundleMgrService_->IsServiceReady()) { + bundleMgrService_->OnStop(); + bundleMgrService_.reset(); + } +} + +void BmsBundleInstallerPermissionTest::CreateInstallerManager() +{ + if (manager_ != nullptr) { + return; + } + manager_ = std::make_shared(); + EXPECT_NE(nullptr, manager_); +} + +void BmsBundleInstallerPermissionTest::ClearBundleInfo() +{ + if (bundleMgrService_ == nullptr) { + return; + } + auto dataMgt = bundleMgrService_->GetDataMgr(); + if (dataMgt == nullptr) { + return; + } + auto dataStorage = dataMgt->GetDataStorage(); + if (dataStorage == nullptr) { + return; + } + + // clear innerBundleInfo from data manager + dataMgt->UpdateBundleInstallState(BUNDLE_NAME, InstallState::UNINSTALL_START); + dataMgt->UpdateBundleInstallState(BUNDLE_NAME, InstallState::UNINSTALL_SUCCESS); + + InnerBundleInfo innerBundleInfo; + ApplicationInfo applicationInfo; + applicationInfo.bundleName = BUNDLE_NAME; + innerBundleInfo.SetBaseApplicationInfo(applicationInfo); + // clear innerBundleInfo from data storage + bool result = dataStorage->DeleteStorageBundleInfo(innerBundleInfo); + EXPECT_TRUE(result) << "the bundle info in db clear fail: " << BUNDLE_NAME; +} + +/** + * @tc.number: ExtractHnpFiles_0100 + * @tc.name: test ExtractHnpFiles + * @tc.desc: 1.Test the ExtractHnpFiles of InstalldHostImpl without permission +*/ +HWTEST_F(BmsBundleInstallerPermissionTest, ExtractHnpFiles_0100, Function | SmallTest | Level1) +{ + InstalldHostImpl installdHostImpl; + std::string hnpPackageInfo; + ExtractParam extractParam; + auto ret = installdHostImpl.ExtractHnpFiles(hnpPackageInfo, extractParam); + EXPECT_EQ(ret, ERR_APPEXECFWK_INSTALLD_PERMISSION_DENIED); +} + +/** + * @tc.number: ProcessBundleInstallNative_0100 + * @tc.name: test ProcessBundleInstallNative_0100 + * @tc.desc: 1.Test the ProcessBundleInstallNative of InstalldHostImpl without permission +*/ +HWTEST_F(BmsBundleInstallerPermissionTest, ProcessBundleInstallNative_0100, Function | SmallTest | Level1) +{ + InstalldHostImpl installdHostImpl; + std::string userId = std::to_string(USERID); + std::string cpuAbi = "arm64"; + std::string packageName = "com.example.test"; + std::string hnpRootPath = "/data/app/el1/bundle/public/com.example.test/entry_tmp/hnp_tmp_extract_dir/"; + std::string hapPath = "/system/app/module01/module01.hap"; + auto ret = installdHostImpl.ProcessBundleInstallNative(userId, hnpRootPath, hapPath, cpuAbi, packageName); + EXPECT_EQ(ret, ERR_APPEXECFWK_INSTALLD_PERMISSION_DENIED); +} + +/** + * @tc.number: ProcessBundleUnInstallNative_0100 + * @tc.name: test ProcessBundleUnInstallNative + * @tc.desc: 1.Test the ProcessBundleUnInstallNative of InstalldHostImpl without permission +*/ +HWTEST_F(BmsBundleInstallerPermissionTest, ProcessBundleUnInstallNative_0100, Function | SmallTest | Level1) +{ + InstalldHostImpl installdHostImpl; + std::string userId = std::to_string(USERID); + std::string packageName = "com.example.test"; + auto ret = installdHostImpl.ProcessBundleUnInstallNative(userId, packageName); + EXPECT_EQ(ret, ERR_APPEXECFWK_INSTALLD_PERMISSION_DENIED); +} + +/** + * @tc.number: PendSignAOT_0100 + * @tc.name: test PendSignAOT + * @tc.desc: 1.Test the PendSignAOT of InstalldHostImpl without permission +*/ +HWTEST_F(BmsBundleInstallerPermissionTest, PendSignAOT_0100, Function | SmallTest | Level1) +{ + InstalldHostImpl installdHostImpl; + std::string anFileName; + std::vector signData; + auto ret = installdHostImpl.PendSignAOT(anFileName, signData); + EXPECT_EQ(ret, ERR_APPEXECFWK_INSTALLD_PERMISSION_DENIED); + ret = installdHostImpl.StopAOT(); + EXPECT_EQ(ret, ERR_APPEXECFWK_INSTALLD_PERMISSION_DENIED); +} + +/** + * @tc.number: CreateBundleDataDirWithVector_0100 + * @tc.name: test CreateBundleDataDirWithVector + * @tc.desc: test CreateBundleDataDirWithVector of InstalldHostImpl without permission +*/ +HWTEST_F(BmsBundleInstallerPermissionTest, CreateBundleDataDirWithVector_0100, Function | SmallTest | Level1) +{ + InstalldHostImpl installdHostImpl; + std::vector createDirParams; + auto ret = installdHostImpl.CreateBundleDataDirWithVector(createDirParams); + EXPECT_EQ(ret, ERR_APPEXECFWK_INSTALLD_PERMISSION_DENIED); + + CreateDirParam createDirParam; + createDirParams.push_back(createDirParam); + ret = installdHostImpl.CreateBundleDataDirWithVector(createDirParams); + EXPECT_EQ(ret, ERR_APPEXECFWK_INSTALLD_PERMISSION_DENIED); +} +} // OHOS diff --git a/services/bundlemgr/test/unittest/bms_bundle_installer_test/bms_bundle_installer_test.cpp b/services/bundlemgr/test/unittest/bms_bundle_installer_test/bms_bundle_installer_test.cpp index 145d598179..6e4d5dcd25 100644 --- a/services/bundlemgr/test/unittest/bms_bundle_installer_test/bms_bundle_installer_test.cpp +++ b/services/bundlemgr/test/unittest/bms_bundle_installer_test/bms_bundle_installer_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2023 Huawei Device Co., Ltd. + * Copyright (c) 2021-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 @@ -5354,6 +5354,22 @@ HWTEST_F(BmsBundleInstallerTest, BeforeInstall_0100, Function | SmallTest | Leve ResetDataMgr(); } +/** + * @tc.number: BeforeInstall_0200 + * @tc.name: test BeforeInstall + * @tc.desc: 1.Test the BeforeInstall +*/ +HWTEST_F(BmsBundleInstallerTest, BeforeInstall_0200, Function | SmallTest | Level0) +{ + AppServiceFwkInstaller appServiceFwkInstaller; + std::vector hspPaths; + InstallParam installParam; + installParam.isPreInstallApp = false; + + ErrCode res = appServiceFwkInstaller.BeforeInstall(hspPaths, installParam); + EXPECT_EQ(res, ERR_APPEXECFWK_INSTALL_PARAM_ERROR); +} + /** * @tc.number: CheckFileType_0100 * @tc.name: test CheckFileType @@ -6135,4 +6151,79 @@ HWTEST_F(BmsBundleInstallerTest, CreateSharedBundleTempDir_0100, Function | Smal auto ret = bundleUtil.CreateSharedBundleTempDir(INSTALLER_ID, INDEX); EXPECT_FALSE(ret.empty()); } + +/** + * @tc.number: LoadExtensionNeedCreateSandbox_0100 + * @tc.name: test LoadExtensionNeedCreateSandbox + * @tc.desc: test LoadExtensionNeedCreateSandbox of InstalldHostImpl +*/ +HWTEST_F(BmsBundleInstallerTest, LoadExtensionNeedCreateSandbox_0100, Function | SmallTest | Level1) +{ + InstalldHostImpl hostImpl; + nlohmann::json object; + std::vector typeList; + hostImpl.LoadNeedCreateSandbox(object, typeList); + std::string extensionTypeName; + bool ret = hostImpl.LoadExtensionNeedCreateSandbox(object, extensionTypeName); + EXPECT_FALSE(ret); +} + +/** + * @tc.number: InnerRemoveAtomicServiceBundleDataDir_0100 + * @tc.name: test InnerRemoveAtomicServiceBundleDataDir + * @tc.desc: test InnerRemoveAtomicServiceBundleDataDir of InstalldHostImpl +*/ +HWTEST_F(BmsBundleInstallerTest, InnerRemoveAtomicServiceBundleDataDir_0100, Function | SmallTest | Level1) +{ + InstalldHostImpl hostImpl; + std::string bundleName = BUNDLE_NAME; + int32_t userId = USERID; + ErrCode ret = hostImpl.InnerRemoveAtomicServiceBundleDataDir(bundleName, userId); + EXPECT_EQ(ret, ERR_OK); +} + +/** + * @tc.number: ReadFileIntoJson_0100 + * @tc.name: test ReadFileIntoJson + * @tc.desc: test ReadFileIntoJson of InstalldHostImpl +*/ +HWTEST_F(BmsBundleInstallerTest, ReadFileIntoJson_0100, Function | SmallTest | Level1) +{ + InstalldHostImpl hostImpl; + std::string filePath; + filePath.resize(PATH_MAX + 1); + nlohmann::json jsonBuf; + bool ret = hostImpl.ReadFileIntoJson(filePath, jsonBuf); + EXPECT_FALSE(ret); +} + +/** + * @tc.number: ExtractModule_0100 + * @tc.name: test ExtractModule + * @tc.desc: 1.Test the ExtractModule +*/ +HWTEST_F(BmsBundleInstallerTest, ExtractModule_0100, Function | SmallTest | Level0) +{ + AppServiceFwkInstaller appServiceFwkInstaller; + InnerBundleInfo newInfo; + std::string bundlePath; + + appServiceFwkInstaller.newInnerBundleInfo_.baseApplicationInfo_->bundleName = "com.acts.example"; + appServiceFwkInstaller.MergeBundleInfos(newInfo); + + std::unordered_map infos; + std::vector hapVerifyRes; + + appServiceFwkInstaller.GenerateOdid(infos, hapVerifyRes); + + Security::Verify::HapVerifyResult hapVerifyResult; + Security::Verify::ProvisionInfo provisionInfo; + hapVerifyResult.SetProvisionInfo(provisionInfo); + hapVerifyRes.push_back(hapVerifyResult); + + appServiceFwkInstaller.GenerateOdid(infos, hapVerifyRes); + + ErrCode ret = appServiceFwkInstaller.ExtractModule(newInfo, bundlePath); + EXPECT_EQ(ret, ERR_OK); +} } // OHOS diff --git a/services/bundlemgr/test/unittest/bms_bundle_kit_service_base_test/bms_bundle_kit_service_base_test.cpp b/services/bundlemgr/test/unittest/bms_bundle_kit_service_base_test/bms_bundle_kit_service_base_test.cpp index e69aedcb00..a98bf0c90d 100644 --- a/services/bundlemgr/test/unittest/bms_bundle_kit_service_base_test/bms_bundle_kit_service_base_test.cpp +++ b/services/bundlemgr/test/unittest/bms_bundle_kit_service_base_test/bms_bundle_kit_service_base_test.cpp @@ -997,4 +997,22 @@ HWTEST_F(BmsBundleKitServiceBaseTest, BundleDistributedManager_3000, Function | EXPECT_TRUE(param.callback == nullptr); sleep(1); } -} \ No newline at end of file + +/** + * @tc.number: BundleManagerCallbackStub_0500 + * @tc.name: Test OnRemoteRequest + * @tc.desc: Verify the OnRemoteRequest return 0. + */ +HWTEST_F(BmsBundleKitServiceBaseTest, BundleManagerCallbackStub_0500, Function | MediumTest | Level1) +{ + MockBundleManagerCallbackStub stub; + uint32_t code = 1; + MessageParcel data; + data.WriteInterfaceToken(BundleManagerCallbackStub::GetDescriptor()); + MessageParcel reply; + MessageOption option; + + int32_t ret = stub.OnRemoteRequest(code, data, reply, option); + EXPECT_NE(ret, 0); +} +} -- Gitee From 9ed10b08744aefa45620249babced35196d3ee2a Mon Sep 17 00:00:00 2001 From: sunjiakun Date: Mon, 14 Oct 2024 09:33:58 +0800 Subject: [PATCH 2/2] add code Signed-off-by: sunjiakun --- .../bms_bundle_installer_permission_test.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/services/bundlemgr/test/unittest/bms_bundle_installer_test/bms_bundle_installer_permission_test.cpp b/services/bundlemgr/test/unittest/bms_bundle_installer_test/bms_bundle_installer_permission_test.cpp index c6e99f6b9a..c00a38e434 100644 --- a/services/bundlemgr/test/unittest/bms_bundle_installer_test/bms_bundle_installer_permission_test.cpp +++ b/services/bundlemgr/test/unittest/bms_bundle_installer_test/bms_bundle_installer_permission_test.cpp @@ -114,7 +114,9 @@ bool BmsBundleInstallerPermissionTest::InstallSystemBundle(const std::string &fi installParam.needSendEvent = false; installParam.needSavePreInstallInfo = true; installParam.isPreInstallApp = true; - installParam.noSkipsKill = false; + setuid(Constants::FOUNDATION_UID); + installParam.SetKillProcess(false); + setuid(Constants::ROOT_UID); installParam.copyHapToInstallPath = false; return installer->InstallSystemBundle( filePath, installParam, Constants::AppType::SYSTEM_APP) == ERR_OK; @@ -131,7 +133,9 @@ bool BmsBundleInstallerPermissionTest::OTAInstallSystemBundle(const std::string installParam.needSendEvent = false; installParam.needSavePreInstallInfo = true; installParam.isPreInstallApp = true; - installParam.noSkipsKill = false; + setuid(Constants::FOUNDATION_UID); + installParam.SetKillProcess(false); + setuid(Constants::ROOT_UID); installParam.copyHapToInstallPath = false; return installer->OTAInstallSystemBundle( filePaths, installParam, Constants::AppType::SYSTEM_APP) == ERR_OK; -- Gitee