IssueNo:#I85LFI

Description:add testcase
Sig:SIG_ApplicaitonFramework
Feature or Bugfix:Feature
Binary Source:No
Signed-off-by: wangtiantian <wangtiantian19@huawei.com>
This commit is contained in:
wangtiantian 2023-10-06 18:24:33 +08:00
parent dbeff9702a
commit c4b1416cc9
4 changed files with 572 additions and 4 deletions

View File

@ -90,6 +90,7 @@ declare_args() {
if (defined(global_parts_info) &&
!defined(global_parts_info.global_resource_management)) {
global_resmgr_enable = false
bundle_framework_bundle_resource = false
}
if (defined(global_parts_info) && !defined(global_parts_info.global_i18n)) {
@ -150,10 +151,6 @@ declare_args() {
!defined(global_parts_info.multimedia_image_framework)) {
bundle_framework_graphics = false
}
if (!global_resmgr_enable || !global_i18n_enable) {
bundle_framework_bundle_resource = false
}
}
print("bundle_framework_power_mgr_enable = " +

View File

@ -70,6 +70,7 @@ group("unittest") {
"unittest/bms_bundle_permission_grant_test:unittest",
"unittest/bms_bundle_quick_fix_mgr_rdb_test:unittest",
"unittest/bms_bundle_resource_manager_test:unittest",
"unittest/bms_bundle_resource_test:unittest",
"unittest/bms_bundle_sandbox_app_test:unittest",
"unittest/bms_bundle_uninstaller_test:unittest",
"unittest/bms_bundle_updater_test:unittest",

View File

@ -0,0 +1,162 @@
# Copyright (c) 2022 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("//build/test.gni")
import("../../../../../appexecfwk.gni")
import("../../../../../services/bundlemgr/appexecfwk_bundlemgr.gni")
module_output_path = "bundle_framework/bundlemgrservice"
ohos_unittest("BmsBundleResourceTest") {
use_exceptions = true
module_out_path = module_output_path
include_dirs = [
"//third_party/jsoncpp/include",
"//third_party/json/include",
]
sources = bundle_mgr_source
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",
]
sources += [
"${services_path}/bundlemgr/test/mock/src/accesstoken_kit.cpp",
"${services_path}/bundlemgr/test/mock/src/installd_permission_mgr.cpp",
"${services_path}/bundlemgr/test/mock/src/installd_service.cpp",
"${services_path}/bundlemgr/test/mock/src/mock_bundle_status.cpp",
"${services_path}/bundlemgr/test/mock/src/mock_clean_cache.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_resource_test.cpp" ]
configs = [ "${services_path}/bundlemgr/test:bundlemgr_test_config" ]
cflags = []
if (target_cpu == "arm") {
cflags += [ "-DBINDER_IPC_32BIT" ]
}
deps = [
"${base_path}:appexecfwk_base",
"${core_path}:appexecfwk_core",
]
deps += bundle_install_deps
external_deps = [
"ability_base:want",
"access_token:libprivacy_sdk",
"access_token:libtokenid_sdk",
"appverify:libhapverify",
"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 = []
if (configpolicy_enable) {
external_deps += [ "config_policy:configpolicy_util" ]
defines += [ "CONFIG_POLOCY_ENABLE" ]
}
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_info_manager:distributed_device_profile_client",
"device_usage_statistics:usagestatsinner",
"display_manager:displaymgr",
"power_manager:powermgr_client",
"syscap_codec:syscap_interface_shared",
]
defines += [ "BUNDLE_FRAMEWORK_FREE_INSTALL" ]
}
if (ecologic_rule_mgr_enabled) {
external_deps += [ "ecological_rule_manager:erms_client" ]
defines += [ "SUPPORT_ERMS" ]
include_dirs += [ "${ecological_rule_mgr_path}" ]
}
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 (storage_service_enable) {
external_deps += [ "storage_service:storage_manager_sa_proxy" ]
defines += [ "STORAGE_SERVICE_ENABLE" ]
}
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 (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 (bundle_framework_bundle_resource) {
defines += [ "BUNDLE_FRAMEWORK_BUNDLE_RESOURCE" ]
external_deps += [ "ability_base:configuration" ]
sources += bundle_resource
}
}
group("unittest") {
testonly = true
if (ability_runtime_enable) {
deps = [ ":BmsBundleResourceTest" ]
}
}

View File

@ -0,0 +1,408 @@
/*
* Copyright (c) 2022-2023 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 <fstream>
#include <gtest/gtest.h>
#include <sstream>
#include <string>
#include "application_info.h"
#include "bundle_info.h"
#include "bundle_installer_host.h"
#include "bundle_mgr_service.h"
#include "bundle_permission_mgr.h"
#ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
#include "bundle_resource_rdb.h"
#include "bundle_system_state.h"
#endif
#include "bundle_verify_mgr.h"
#include "inner_bundle_info.h"
#include "installd/installd_service.h"
#include "installd_client.h"
#include "mock_status_receiver.h"
#include "parameter.h"
#include "permission_define.h"
#include "remote_ability_info.h"
#include "scope_guard.h"
using namespace testing::ext;
using namespace std::chrono_literals;
using namespace OHOS;
using namespace OHOS::AppExecFwk;
using namespace OHOS::Security;
namespace OHOS {
namespace {
const int32_t USERID = 100;
const int32_t WAIT_TIME = 5; // init mocked bms
} // namespace
class BmsBundleResourceTest : public testing::Test {
public:
BmsBundleResourceTest();
~BmsBundleResourceTest();
static void SetUpTestCase();
static void TearDownTestCase();
void SetUp();
void TearDown();
ErrCode InstallBundle(const std::string &bundlePath) const;
ErrCode UpdateBundle(const std::string &bundlePath) const;
ErrCode UnInstallBundle(const std::string &bundleName) const;
const std::shared_ptr<BundleDataMgr> GetBundleDataMgr() const;
void StartInstalldService() const;
void StartBundleService();
private:
static std::shared_ptr<InstalldService> installdService_;
static std::shared_ptr<BundleMgrService> bundleMgrService_;
};
std::shared_ptr<BundleMgrService> BmsBundleResourceTest::bundleMgrService_ =
DelayedSingleton<BundleMgrService>::GetInstance();
std::shared_ptr<InstalldService> BmsBundleResourceTest::installdService_ =
std::make_shared<InstalldService>();
BmsBundleResourceTest::BmsBundleResourceTest()
{}
BmsBundleResourceTest::~BmsBundleResourceTest()
{}
void BmsBundleResourceTest::SetUpTestCase()
{}
void BmsBundleResourceTest::TearDownTestCase()
{
bundleMgrService_->OnStop();
}
void BmsBundleResourceTest::SetUp()
{
StartInstalldService();
StartBundleService();
}
void BmsBundleResourceTest::TearDown()
{}
ErrCode BmsBundleResourceTest::InstallBundle(const std::string &bundlePath) const
{
if (!bundleMgrService_) {
return ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR;
}
auto installer = bundleMgrService_->GetBundleInstaller();
if (!installer) {
EXPECT_FALSE(true) << "the installer is nullptr";
return ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR;
}
sptr<MockStatusReceiver> receiver = new (std::nothrow) MockStatusReceiver();
if (!receiver) {
EXPECT_FALSE(true) << "the receiver is nullptr";
return ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR;
}
InstallParam installParam;
installParam.installFlag = InstallFlag::NORMAL;
installParam.userId = USERID;
bool result = installer->Install(bundlePath, installParam, receiver);
EXPECT_TRUE(result);
return receiver->GetResultCode();
}
ErrCode BmsBundleResourceTest::UpdateBundle(const std::string &bundlePath) const
{
if (!bundleMgrService_) {
return ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR;
}
auto installer = bundleMgrService_->GetBundleInstaller();
if (!installer) {
EXPECT_FALSE(true) << "the installer is nullptr";
return ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR;
}
sptr<MockStatusReceiver> receiver = new (std::nothrow) MockStatusReceiver();
if (!receiver) {
EXPECT_FALSE(true) << "the receiver is nullptr";
return ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR;
}
InstallParam installParam;
installParam.installFlag = InstallFlag::REPLACE_EXISTING;
installParam.userId = USERID;
bool result = installer->Install(bundlePath, installParam, receiver);
EXPECT_TRUE(result);
return receiver->GetResultCode();
}
ErrCode BmsBundleResourceTest::UnInstallBundle(const std::string &bundleName) const
{
if (!bundleMgrService_) {
return ERR_APPEXECFWK_UNINSTALL_BUNDLE_MGR_SERVICE_ERROR;
}
auto installer = bundleMgrService_->GetBundleInstaller();
if (!installer) {
EXPECT_FALSE(true) << "the installer is nullptr";
return ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR;
}
sptr<MockStatusReceiver> receiver = new (std::nothrow) MockStatusReceiver();
if (!receiver) {
EXPECT_FALSE(true) << "the receiver is nullptr";
return ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR;
}
InstallParam installParam;
installParam.installFlag = InstallFlag::NORMAL;
installParam.userId = USERID;
bool result = installer->Uninstall(bundleName, installParam, receiver);
EXPECT_TRUE(result);
return receiver->GetResultCode();
}
void BmsBundleResourceTest::StartInstalldService() const
{
if (!installdService_->IsServiceReady()) {
installdService_->Start();
}
}
void BmsBundleResourceTest::StartBundleService()
{
if (!bundleMgrService_->IsServiceReady()) {
bundleMgrService_->OnStart();
std::this_thread::sleep_for(std::chrono::seconds(WAIT_TIME));
}
}
const std::shared_ptr<BundleDataMgr> BmsBundleResourceTest::GetBundleDataMgr() const
{
return bundleMgrService_->GetDataMgr();
}
#ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
/**
* @tc.number: BmsBundleResourceTest_0001
* Function: GetKey
* @tc.name: test GetKey
* @tc.desc: 1. system running normally
* 2. test ResourceInfo GetKey
*/
HWTEST_F(BmsBundleResourceTest, BmsBundleResourceTest_0001, Function | SmallTest | Level0)
{
ResourceInfo resourceInfo;
resourceInfo.bundleName_ = "bundleName";
std::string key = resourceInfo.GetKey();
EXPECT_EQ(key, "bundleName");
resourceInfo.moduleName_ = "moduleName";
resourceInfo.abilityName_ = "abilityName";
key = resourceInfo.GetKey();
EXPECT_EQ(key, "bundleName/moduleName/abilityName");
}
/**
* @tc.number: BmsBundleResourceTest_0002
* Function: ParseKey
* @tc.name: test ParseKey
* @tc.desc: 1. system running normally
* 2. test ResourceInfo ParseKey
*/
HWTEST_F(BmsBundleResourceTest, BmsBundleResourceTest_0002, Function | SmallTest | Level0)
{
std::string key = "bundleName";
ResourceInfo resourceInfo;
resourceInfo.ParseKey(key);
EXPECT_EQ(resourceInfo.bundleName_, "bundleName");
EXPECT_EQ(resourceInfo.moduleName_, "");
EXPECT_EQ(resourceInfo.abilityName_, "");
key = "bundleName/";
resourceInfo.ParseKey(key);
EXPECT_EQ(resourceInfo.bundleName_, "bundleName");
EXPECT_EQ(resourceInfo.moduleName_, "");
EXPECT_EQ(resourceInfo.abilityName_, "");
key = "bundleName/abilityName";
resourceInfo.ParseKey(key);
EXPECT_EQ(resourceInfo.bundleName_, "bundleName");
EXPECT_EQ(resourceInfo.moduleName_, "");
EXPECT_EQ(resourceInfo.abilityName_, "abilityName");
key = "bundleName/moduleName/abilityName";
resourceInfo.ParseKey(key);
EXPECT_EQ(resourceInfo.bundleName_, "bundleName");
EXPECT_EQ(resourceInfo.moduleName_, "moduleName");
EXPECT_EQ(resourceInfo.abilityName_, "abilityName");
}
/**
* @tc.number: BmsBundleResourceTest_0003
* Function: GetKey ParseKey
* @tc.name: test GetKey and ParseKey
* @tc.desc: 1. system running normally
* 2. test ResourceInfo GetKey and ParseKey
*/
HWTEST_F(BmsBundleResourceTest, BmsBundleResourceTest_0003, Function | SmallTest | Level0)
{
ResourceInfo resourceInfo;
resourceInfo.bundleName_ = "bundleName";
resourceInfo.moduleName_ = "moduleName";
resourceInfo.abilityName_ = "abilityName";
std::string key = resourceInfo.GetKey();
ResourceInfo newResourceInfo;
newResourceInfo.ParseKey(key);
EXPECT_EQ(resourceInfo.bundleName_, newResourceInfo.bundleName_);
EXPECT_EQ(resourceInfo.moduleName_, newResourceInfo.moduleName_);
EXPECT_EQ(resourceInfo.abilityName_, newResourceInfo.abilityName_);
}
/**
* @tc.number: BmsBundleResourceTest_0004
* Function: BundleSystemState
* @tc.name: test BundleSystemState
* @tc.desc: 1. system running normally
* 2. test BundleSystemState
*/
HWTEST_F(BmsBundleResourceTest, BmsBundleResourceTest_0004, Function | SmallTest | Level0)
{
std::string oldColorMode = BundleSystemState::GetInstance().GetSystemColorMode();
std::string oldLanguage = BundleSystemState::GetInstance().GetSystemLanguage();
std::string colorMode = "light";
BundleSystemState::GetInstance().SetSystemColorMode(colorMode);
EXPECT_EQ(colorMode, BundleSystemState::GetInstance().GetSystemColorMode());
BundleSystemState::GetInstance().SetSystemColorMode(oldColorMode);
std::string language = "zh-Hans";
BundleSystemState::GetInstance().SetSystemLanguage(language);
EXPECT_EQ(language, BundleSystemState::GetInstance().GetSystemLanguage());
BundleSystemState::GetInstance().SetSystemLanguage(oldLanguage);
std::string key = BundleSystemState::GetInstance().ToString();
EXPECT_NE(key, "");
bool ans = BundleSystemState::GetInstance().FromString("{");
EXPECT_FALSE(ans);
ans = BundleSystemState::GetInstance().FromString(key);
EXPECT_TRUE(ans);
}
/**
* @tc.number: BmsBundleResourceTest_0005
* Function: BundleResourceRdb
* @tc.name: test BundleResourceRdb
* @tc.desc: 1. system running normally
* 2. test AddResourceInfo
*/
HWTEST_F(BmsBundleResourceTest, BmsBundleResourceTest_0005, Function | SmallTest | Level0)
{
BundleResourceRdb resourceRdb;
ResourceInfo resourceInfo;
bool ans = resourceRdb.AddResourceInfo(resourceInfo);
EXPECT_FALSE(ans);
ans = resourceRdb.DeleteResourceInfo(resourceInfo.GetKey());
EXPECT_FALSE(ans);
resourceInfo.bundleName_ = "bundleName";
ans = resourceRdb.AddResourceInfo(resourceInfo);
EXPECT_TRUE(ans);
ans = resourceRdb.DeleteResourceInfo(resourceInfo.GetKey());
EXPECT_TRUE(ans);
}
/**
* @tc.number: BmsBundleResourceTest_0006
* Function: BundleResourceRdb
* @tc.name: test BundleResourceRdb
* @tc.desc: 1. system running normally
* 2. test AddResourceInfos
*/
HWTEST_F(BmsBundleResourceTest, BmsBundleResourceTest_0006, Function | SmallTest | Level0)
{
BundleResourceRdb resourceRdb;
std::vector<ResourceInfo> resourceInfos;
bool ans = resourceRdb.AddResourceInfos(resourceInfos);
EXPECT_FALSE(ans);
ResourceInfo resourceInfo;
resourceInfo.bundleName_ = "bundleName";
resourceInfos.push_back(resourceInfo);
ans = resourceRdb.AddResourceInfos(resourceInfos);
EXPECT_TRUE(ans);
ans = resourceRdb.DeleteResourceInfo(resourceInfo.GetKey());
EXPECT_TRUE(ans);
resourceInfos.clear();
ResourceInfo resourceInfo_2;
resourceInfos.push_back(resourceInfo_2);
ans = resourceRdb.AddResourceInfos(resourceInfos);
EXPECT_FALSE(ans);
}
/**
* @tc.number: BmsBundleResourceTest_0007
* Function: BundleResourceRdb
* @tc.name: test BundleResourceRdb
* @tc.desc: 1. system running normally
* 2. test GetAllResourceName
*/
HWTEST_F(BmsBundleResourceTest, BmsBundleResourceTest_0007, Function | SmallTest | Level0)
{
BundleResourceRdb resourceRdb;
ResourceInfo resourceInfo;
resourceInfo.bundleName_ = "bundleName";
bool ans = resourceRdb.AddResourceInfo(resourceInfo);
EXPECT_TRUE(ans);
std::vector<std::string> keyNames;
ans = resourceRdb.GetAllResourceName(keyNames);
EXPECT_TRUE(ans);
EXPECT_TRUE(std::find(keyNames.begin(), keyNames.end(), resourceInfo.GetKey()) != keyNames.end());
ans = resourceRdb.DeleteResourceInfo(resourceInfo.GetKey());
EXPECT_TRUE(ans);
}
/**
* @tc.number: BmsBundleResourceTest_0008
* Function: BundleResourceRdb
* @tc.name: test BundleResourceRdb
* @tc.desc: 1. system running normally
* 2. test IsCurrentColorModeExist
*/
HWTEST_F(BmsBundleResourceTest, BmsBundleResourceTest_0008, Function | SmallTest | Level0)
{
BundleResourceRdb resourceRdb;
ResourceInfo resourceInfo;
resourceInfo.bundleName_ = "bundleName";
bool ans = resourceRdb.AddResourceInfo(resourceInfo);
EXPECT_TRUE(ans);
ans = resourceRdb.IsCurrentColorModeExist();
EXPECT_TRUE(ans);
std::string oldColorMode = BundleSystemState::GetInstance().GetSystemColorMode();
BundleSystemState::GetInstance().SetSystemColorMode("aaaaaa");
ans = resourceRdb.IsCurrentColorModeExist();
EXPECT_FALSE(ans);
BundleSystemState::GetInstance().SetSystemColorMode(oldColorMode);
ans = resourceRdb.DeleteResourceInfo(resourceInfo.GetKey());
EXPECT_TRUE(ans);
}
#endif
} // OHOS