diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index c6f52ad36b0c711b864d6a2889d0a5714f770b15..c95800c9ad845719348daaeb930ef02c3c98286d 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -323,6 +323,7 @@ group("unittest") { "data_ability_observer_proxy_test:unittest", "data_ability_observer_stub_test:unittest", "data_ability_record_test:unittest", + "data_ability_record_second_test:unittest", "dataobs_mgr_client_test:unittest", "dataobs_mgr_inner_ext_test:unittest", "dataobs_mgr_inner_pref_test:unittest", @@ -331,6 +332,7 @@ group("unittest") { "dataobs_mgr_service_dump_test:unittest", "dataobs_mgr_service_second_test:unittest", "dataobs_mgr_service_test:unittest", + "dataobs_mgr_service_third_test:unittest", "dataobs_mgr_stub_test:unittest", "deeplink_reserve_config_test:unittest", "dfr_test:unittest", diff --git a/test/unittest/data_ability_record_second_test/BUILD.gn b/test/unittest/data_ability_record_second_test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..6564d2693f268a909a71467f224a6b84d24ef3ec --- /dev/null +++ b/test/unittest/data_ability_record_second_test/BUILD.gn @@ -0,0 +1,83 @@ +# Copyright (c) 2025 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("//foundation/ability/ability_runtime/ability_runtime.gni") + +module_output_path = "ability_runtime/ability_runtime/abilitymgr" + +ohos_unittest("data_ability_record_second_test") { + module_out_path = module_output_path + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + blocklist = "../../cfi_blocklist.txt" + } + branch_protector_ret = "pac_ret" + include_dirs = [ + "${ability_runtime_test_path}/mock/services_abilitymgr_test/libs/system_ability_mock", + "${ability_runtime_test_path}/mock/services_abilitymgr_test/libs/ability_scheduler_mock", + "${ability_runtime_innerkits_path}/ability_manager/include", + "${ability_runtime_path}/interfaces/kits/native/ability/native", + "${ability_runtime_services_path}/common/include", + ] + + sources = [ + "data_ability_record_second_test.cpp", # add mock file + ] + + configs = [ + "${ability_runtime_services_path}/abilitymgr:abilityms_config", + "${ability_runtime_test_path}/mock/services_abilitymgr_test:aafwk_mock_config", + ] + cflags = [] + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } + deps = [ + "${ability_runtime_innerkits_path}/ability_manager:ability_manager", + "${ability_runtime_native_path}/ability/native:abilitykit_native", + "${ability_runtime_services_path}/abilitymgr:abilityms", + "${ability_runtime_services_path}/common:perm_verification", + "${ability_runtime_test_path}/mock/services_abilitymgr_test/libs/aakit:aakit_mock", + "${ability_runtime_test_path}/mock/services_abilitymgr_test/libs/appexecfwk_core:appexecfwk_appmgr_mock", + "${ability_runtime_test_path}/mock/services_abilitymgr_test/libs/appexecfwk_core:appexecfwk_bundlemgr_mock", + ] + + external_deps = [ + "ability_base:session_info", + "ability_base:want", + "ability_base:zuri", + "access_token:libaccesstoken_sdk", + "bundle_framework:appexecfwk_base", + "c_utils:utils", + "common_event_service:cesfwk_innerkits", + "ffrt:libffrt", + "googletest:gmock_main", + "googletest:gtest_main", + "hilog:libhilog", + "ipc:ipc_core", + "napi:ace_napi", + "samgr:samgr_proxy", + ] + + if (background_task_mgr_continuous_task_enable) { + external_deps += [ "background_task_mgr:bgtaskmgr_innerkits" ] + } +} + +group("unittest") { + testonly = true + deps = [ ":data_ability_record_second_test" ] +} diff --git a/test/unittest/data_ability_record_second_test/data_ability_record_second_test.cpp b/test/unittest/data_ability_record_second_test/data_ability_record_second_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bee9c04d164aed4ab1a428de13a6063c38b6a0a4 --- /dev/null +++ b/test/unittest/data_ability_record_second_test/data_ability_record_second_test.cpp @@ -0,0 +1,185 @@ +/* + * Copyright (c) 2025 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 + +#define private public +#define protected public +#include "data_ability_record.h" +#undef private +#undef protected + +#include "ability_scheduler_mock.h" +#include "app_process_data.h" +#include "hilog_tag_wrapper.h" + +using namespace testing::ext; +using namespace testing; +using namespace std::chrono; +using namespace OHOS::AppExecFwk; + +namespace OHOS { +namespace AAFwk { +class DataAbilityRecordTest : public testing::TestWithParam { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + +protected: + sptr abilitySchedulerMock_{ nullptr }; + AbilityRequest abilityRequest_; + std::shared_ptr abilityRecord_{ nullptr }; + OHOS::AAFwk::AbilityState abilityState_; +}; + +void DataAbilityRecordTest::SetUpTestCase(void) +{} +void DataAbilityRecordTest::TearDownTestCase(void) +{} + +void DataAbilityRecordTest::SetUp(void) +{ + if (abilitySchedulerMock_ == nullptr) { + abilitySchedulerMock_ = new AbilitySchedulerMock(); + } + + abilityRequest_.appInfo.bundleName = "com.data_ability.hiworld"; + abilityRequest_.appInfo.name = "com.data_ability.hiworld"; + abilityRequest_.abilityInfo.name = "DataAbilityHiworld"; + abilityRequest_.abilityInfo.type = AbilityType::DATA; + + if (abilityRecord_ == nullptr) { + OHOS::AppExecFwk::AbilityInfo abilityInfo; + OHOS::AppExecFwk::ApplicationInfo applicationInfo; + const Want want; + abilityRecord_ = std::make_shared(want, abilityInfo, applicationInfo); + abilityRecord_->Init(); + } + abilityState_ = INITIAL; +} + +void DataAbilityRecordTest::TearDown(void) +{ + abilitySchedulerMock_.clear(); +} + +/* + * Feature: AbilityManager + * Function: DataAbility + * SubFunction: GetDiedCallerPid + * FunctionPoints: The parameter of function GetDiedCallerPid. + * EnvConditions: Can run ohos test framework + * CaseDescription: Verify function GetDiedCallerPid + */ +HWTEST_F(DataAbilityRecordTest, AaFwk_DataAbilityRecord_GetDiedCallerPid_001, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "AaFwk_DataAbilityRecord_GetDiedCallerPid_001 start."); + + auto dataAbilityRecord = std::make_shared(abilityRequest_); + int32_t result = dataAbilityRecord->GetDiedCallerPid(nullptr); + EXPECT_EQ(result, 0); + + TAG_LOGI(AAFwkTag::TEST, "AaFwk_DataAbilityRecord_GetDiedCallerPid_001 end."); +} + +/* + * Feature: AbilityManager + * Function: DataAbility + * SubFunction: GetDiedCallerPid + * FunctionPoints: The parameter of function GetDiedCallerPid. + * EnvConditions: Can run ohos test framework + * CaseDescription: Verify function GetDiedCallerPid + */ +HWTEST_F(DataAbilityRecordTest, AaFwk_DataAbilityRecord_GetDiedCallerPid_002, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "AaFwk_DataAbilityRecord_GetDiedCallerPid_002 start."); + + auto dataAbilityRecord = std::make_shared(abilityRequest_); + dataAbilityRecord->clients_.clear(); + int32_t result = dataAbilityRecord->GetDiedCallerPid(abilityRecord_->GetToken()); + EXPECT_EQ(result, 0); + + TAG_LOGI(AAFwkTag::TEST, "AaFwk_DataAbilityRecord_GetDiedCallerPid_002 end."); +} + +/* + * Feature: AbilityManager + * Function: DataAbility + * SubFunction: GetDiedCallerPid + * FunctionPoints: The parameter of function GetDiedCallerPid. + * EnvConditions: Can run ohos test framework + * CaseDescription: Verify function GetDiedCallerPid + */ +HWTEST_F(DataAbilityRecordTest, AaFwk_DataAbilityRecord_GetDiedCallerPid_003, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "AaFwk_DataAbilityRecord_GetDiedCallerPid_003 start."); + + auto dataAbilityRecord = std::make_shared(abilityRequest_); + DataAbilityRecord::ClientInfo clientInfo; + dataAbilityRecord->clients_.push_back(clientInfo); + int32_t result = dataAbilityRecord->GetDiedCallerPid(abilityRecord_->GetToken()); + EXPECT_EQ(result, 0); + + TAG_LOGI(AAFwkTag::TEST, "AaFwk_DataAbilityRecord_GetDiedCallerPid_003 end."); +} + +/* + * Feature: AbilityManager + * Function: DataAbility + * SubFunction: GetDiedCallerPid + * FunctionPoints: The parameter of function GetDiedCallerPid. + * EnvConditions: Can run ohos test framework + * CaseDescription: Verify function GetDiedCallerPid + */ +HWTEST_F(DataAbilityRecordTest, AaFwk_DataAbilityRecord_GetDiedCallerPid_004, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "AaFwk_DataAbilityRecord_GetDiedCallerPid_004 start."); + + auto dataAbilityRecord = std::make_shared(abilityRequest_); + DataAbilityRecord::ClientInfo clientInfo; + clientInfo.client = abilityRecord_->GetToken(); + clientInfo.clientPid = 1; + dataAbilityRecord->clients_.push_back(clientInfo); + int32_t result = dataAbilityRecord->GetDiedCallerPid(abilityRecord_->GetToken()); + EXPECT_EQ(result, 1); + + TAG_LOGI(AAFwkTag::TEST, "AaFwk_DataAbilityRecord_GetDiedCallerPid_004 end."); +} + +/* + * Feature: AbilityManager + * Function: DataAbility + * SubFunction: GetDiedCallerPid + * FunctionPoints: The parameter of function GetDiedCallerPid. + * EnvConditions: Can run ohos test framework + * CaseDescription: Verify function GetDiedCallerPid + */ +HWTEST_F(DataAbilityRecordTest, AaFwk_DataAbilityRecord_GetClientCount_001, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "AaFwk_DataAbilityRecord_GetClientCount_001 start."); + + auto dataAbilityRecord = std::make_shared(abilityRequest_); + dataAbilityRecord->ability_ = abilityRecord_; + dataAbilityRecord->scheduler_ = abilitySchedulerMock_; + dataAbilityRecord->ability_->SetAbilityState(ACTIVE); + size_t result = dataAbilityRecord->GetClientCount(nullptr); + EXPECT_EQ(result, 0); + + TAG_LOGI(AAFwkTag::TEST, "AaFwk_DataAbilityRecord_GetClientCount_001 end."); +} +} // namespace AAFwk +} // namespace OHOS diff --git a/test/unittest/dataobs_mgr_service_third_test/BUILD.gn b/test/unittest/dataobs_mgr_service_third_test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..171a8cffaa3e225283a9406d0fc20387f3b582fb --- /dev/null +++ b/test/unittest/dataobs_mgr_service_third_test/BUILD.gn @@ -0,0 +1,72 @@ +# Copyright (c) 2025 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("//foundation/ability/ability_runtime/ability_runtime.gni") + +module_output_path = "ability_runtime/ability_runtime/dataobsmgr" + +ohos_unittest("dataobs_mgr_service_third_test") { + branch_protector_ret = "pac_ret" + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + blocklist = "../../cfi_blocklist.txt" + } + module_out_path = module_output_path + + include_dirs = [ + "mock/include" + ] + + sources = [ + "dataobs_mgr_service_third_test.cpp", + "mock/src/mock_accesstoken_kit.cpp" + ] + + configs = [ "${ability_runtime_services_path}/dataobsmgr:dataobsms_config" ] + cflags = [ + "-Dprivate=public", + "-Dprotected=public", + ] + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } + deps = [ + "${ability_runtime_innerkits_path}/dataobs_manager:dataobs_manager", + "${ability_runtime_services_path}/common:task_handler_wrap", + "${ability_runtime_services_path}/dataobsmgr:dataobsms_static", + ] + + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "access_token:libtokenid_sdk", + "c_utils:utils", + "ffrt:libffrt", + "googletest:gmock_main", + "googletest:gtest_main", + "hilog:libhilog", + "ipc:ipc_core", + "safwk:api_cache_manager", + "safwk:system_ability_fwk", + ] +} + +group("unittest") { + testonly = true + deps = [ ":dataobs_mgr_service_third_test" ] +} diff --git a/test/unittest/dataobs_mgr_service_third_test/dataobs_mgr_service_third_test.cpp b/test/unittest/dataobs_mgr_service_third_test/dataobs_mgr_service_third_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b5b83cecc556f89362067e0c6186eeefe0c89018 --- /dev/null +++ b/test/unittest/dataobs_mgr_service_third_test/dataobs_mgr_service_third_test.cpp @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2025 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 "gtest/gtest.h" +#include "gmock/gmock.h" +#include "hilog_tag_wrapper.h" +#include "mock_accesstoken_kit.h" +#define private public +#include "dataobs_mgr_service.h" +#undef private + +using namespace OHOS::Security::AccessToken; +namespace OHOS { +namespace AAFwk { +using namespace testing::ext; +class DataObsMgrServiceThirdTest : public testing::Test { +public: + DataObsMgrServiceThirdTest() = default; + virtual ~DataObsMgrServiceThirdTest() = default; + + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; +void DataObsMgrServiceThirdTest::SetUpTestCase(void) +{} +void DataObsMgrServiceThirdTest::TearDownTestCase(void) +{} +void DataObsMgrServiceThirdTest::SetUp() +{} +void DataObsMgrServiceThirdTest::TearDown() +{} + +/* + * Feature: DataObsMgrService + * Function: GetCallingUserId + * SubFunction: NA + * FunctionPoints: DataObsMgrService GetCallingUserId + * EnvConditions: NA + */ +HWTEST_F(DataObsMgrServiceThirdTest, DataObsMgrServiceThirdTest_GetCallingUserId_001, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "DataObsMgrServiceThirdTest_GetCallingUserId_001 start"); + AccessTokenKit::tokenTypeFlag_ = Security::AccessToken::TOKEN_NATIVE; + auto dataObsMgrServer = DelayedSingleton::GetInstance(); + int32_t ret = dataObsMgrServer->GetCallingUserId(); + EXPECT_EQ(ret, 0); + TAG_LOGI(AAFwkTag::TEST, "DataObsMgrServiceThirdTest_GetCallingUserId_001 end"); +} + + +/* + * Feature: DataObsMgrService + * Function: GetCallingUserId + * SubFunction: NA + * FunctionPoints: DataObsMgrService GetCallingUserId + * EnvConditions: NA + */ +HWTEST_F(DataObsMgrServiceThirdTest, DataObsMgrServiceThirdTest_GetCallingUserId_002, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "DataObsMgrServiceThirdTest_GetCallingUserId_002 start"); + AccessTokenKit::tokenTypeFlag_ = Security::AccessToken::TOKEN_SHELL; + auto dataObsMgrServer = DelayedSingleton::GetInstance(); + int32_t ret = dataObsMgrServer->GetCallingUserId(); + EXPECT_EQ(ret, 0); + TAG_LOGI(AAFwkTag::TEST, "DataObsMgrServiceThirdTest_GetCallingUserId_002 end"); +} + +/* + * Feature: DataObsMgrService + * Function: GetCallingUserId + * SubFunction: NA + * FunctionPoints: DataObsMgrService GetCallingUserId + * EnvConditions: NA + */ +HWTEST_F(DataObsMgrServiceThirdTest, DataObsMgrServiceThirdTest_GetCallingUserId_003, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "DataObsMgrServiceThirdTest_GetCallingUserId_003 start"); + AccessTokenKit::tokenTypeFlag_ = Security::AccessToken::TOKEN_HAP; + AccessTokenKit::hapTokenInfo_ = -1; + auto dataObsMgrServer = DelayedSingleton::GetInstance(); + int32_t ret = dataObsMgrServer->GetCallingUserId(); + EXPECT_EQ(ret, -1); + TAG_LOGI(AAFwkTag::TEST, "DataObsMgrServiceThirdTest_GetCallingUserId_003 end"); +} + +/* + * Feature: DataObsMgrService + * Function: GetCallingUserId + * SubFunction: NA + * FunctionPoints: DataObsMgrService GetCallingUserId + * EnvConditions: NA + */ +HWTEST_F(DataObsMgrServiceThirdTest, DataObsMgrServiceThirdTest_GetCallingUserId_004, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "DataObsMgrServiceThirdTest_GetCallingUserId_004 start"); + AccessTokenKit::tokenTypeFlag_ = Security::AccessToken::TOKEN_HAP; + AccessTokenKit::hapTokenInfo_ = 0; + AccessTokenKit::hapTokenUserId_ = 2; + auto dataObsMgrServer = DelayedSingleton::GetInstance(); + int32_t ret = dataObsMgrServer->GetCallingUserId(); + EXPECT_EQ(ret, 2); + TAG_LOGI(AAFwkTag::TEST, "DataObsMgrServiceThirdTest_GetCallingUserId_004 end"); +} +} // namespace AAFwk +} // namespace OHOS \ No newline at end of file diff --git a/test/unittest/dataobs_mgr_service_third_test/mock/include/mock_accesstoken_kit.h b/test/unittest/dataobs_mgr_service_third_test/mock/include/mock_accesstoken_kit.h new file mode 100644 index 0000000000000000000000000000000000000000..c1df3ed88308c66d44f88698164b9a4c4a11d6f3 --- /dev/null +++ b/test/unittest/dataobs_mgr_service_third_test/mock/include/mock_accesstoken_kit.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2025 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. + */ + +#ifndef INTERFACES_INNER_KITS_ACCESSTOKEN_KIT_H +#define INTERFACES_INNER_KITS_ACCESSTOKEN_KIT_H + +#include "access_token.h" +#include "native_token_info.h" +#include "hap_token_info.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { + +class AccessTokenKit { +public: + static ATokenTypeEnum GetTokenTypeFlag(AccessTokenID tokenID); + + static int GetHapTokenInfo(AccessTokenID tokenID, HapTokenInfo &hapInfo); + + static ATokenTypeEnum tokenTypeFlag_; + static int hapTokenInfo_; + static int hapTokenUserId_; +}; +} // namespace AccessToken +} // namespace Security +} // namespace OHOS +#endif diff --git a/test/unittest/dataobs_mgr_service_third_test/mock/src/mock_accesstoken_kit.cpp b/test/unittest/dataobs_mgr_service_third_test/mock/src/mock_accesstoken_kit.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7737b4ef6fd55e1a2bca67a08d3b1c10265f1425 --- /dev/null +++ b/test/unittest/dataobs_mgr_service_third_test/mock/src/mock_accesstoken_kit.cpp @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2025 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 "mock_accesstoken_kit.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +ATokenTypeEnum AccessTokenKit::tokenTypeFlag_ = ATokenTypeEnum::TOKEN_INVALID; +int AccessTokenKit::hapTokenInfo_ = 0; +int AccessTokenKit::hapTokenUserId_ = 0; + +ATokenTypeEnum AccessTokenKit::GetTokenTypeFlag(AccessTokenID tokenID) +{ + return tokenTypeFlag_; +} + +int AccessTokenKit::GetHapTokenInfo(AccessTokenID tokenID, HapTokenInfo &hapInfo) +{ + hapInfo.userID = hapTokenUserId_; + return hapTokenInfo_; +} +} // namespace AccessToken +} // namespace Security +} // namespace OHOS \ No newline at end of file diff --git a/test/unittest/quick_fix/BUILD.gn b/test/unittest/quick_fix/BUILD.gn index 7d99c9cccc91210f78e242b93c048ceeb2881b6f..9669a13d2d156458d6d859b7e7355b45f547660e 100644 --- a/test/unittest/quick_fix/BUILD.gn +++ b/test/unittest/quick_fix/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2022 Huawei Device Co., Ltd. +# Copyright (c) 2022-2025 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 @@ -23,5 +23,6 @@ group("unittest") { "quick_fix_manager_client_test:unittest", "quick_fix_manager_service_ability_test:unittest", "quick_fix_manager_service_test:unittest", + "quick_fix_manager_service_second_test:unittest", ] } diff --git a/test/unittest/quick_fix/mock/include/mock_quick_fix_util.h b/test/unittest/quick_fix/mock/include/mock_quick_fix_util.h index 5371c8d0e0c0a2848b1d386d1e354ee97997154b..f9f7434e5f2b7bde26f6a4759c928b3945a16ee5 100644 --- a/test/unittest/quick_fix/mock/include/mock_quick_fix_util.h +++ b/test/unittest/quick_fix/mock/include/mock_quick_fix_util.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 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 @@ -30,6 +30,8 @@ public: static sptr GetBundleQuickFixMgrProxy(); void RegisterSystemAbility(const int32_t systemAbilityId, sptr object); + static bool setAppManagerProxyNull_; + static bool setBundleMgrProxyNull_; private: static std::mutex saMutex_; diff --git a/test/unittest/quick_fix/mock/src/mock_quick_fix_util.cpp b/test/unittest/quick_fix/mock/src/mock_quick_fix_util.cpp index 0a14c48fcc6d2835f917e72bda65109799cc4378..6d6c4e9bc35f00ef0e5afd3f65459458c9344083 100644 --- a/test/unittest/quick_fix/mock/src/mock_quick_fix_util.cpp +++ b/test/unittest/quick_fix/mock/src/mock_quick_fix_util.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 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 @@ -24,6 +24,8 @@ namespace OHOS { namespace AAFwk { std::mutex QuickFixUtil::saMutex_; std::unordered_map> QuickFixUtil::servicesMap_; +bool QuickFixUtil::setAppManagerProxyNull_ = false; +bool QuickFixUtil::setBundleMgrProxyNull_ = false; sptr QuickFixUtil::GetRemoteObjectOfSystemAbility(const int32_t systemAbilityId) { @@ -44,11 +46,17 @@ sptr QuickFixUtil::GetRemoteObjectOfSystemAbility(const int32_t s sptr QuickFixUtil::GetAppManagerProxy() { + if (setAppManagerProxyNull_) { + return nullptr; + } return iface_cast(GetRemoteObjectOfSystemAbility(APP_MGR_SERVICE_ID)); } sptr QuickFixUtil::GetBundleManagerProxy() { + if (setBundleMgrProxyNull_) { + return nullptr; + } return iface_cast(GetRemoteObjectOfSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID)); } diff --git a/test/unittest/quick_fix/quick_fix_manager_service_second_test/BUILD.gn b/test/unittest/quick_fix/quick_fix_manager_service_second_test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..fae57fb64e6ba3112ec24b1c666bf0bec5bd1096 --- /dev/null +++ b/test/unittest/quick_fix/quick_fix_manager_service_second_test/BUILD.gn @@ -0,0 +1,77 @@ +# Copyright (c) 2025 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/ohos.gni") +import("//build/test.gni") +import("//foundation/ability/ability_runtime/ability_runtime.gni") + +ohos_unittest("quick_fix_manager_service_second_test") { + module_out_path = "ability_runtime/ability_runtime/quick_fix" + + include_dirs = [ + "${ability_runtime_path}/interfaces/kits/native/appkit/ability_bundle_manager_helper", + "${ability_runtime_test_path}/unittest/quick_fix/mock/include", + "${ability_runtime_test_path}/mock/services_appmgr_test/include", + "${ability_runtime_test_path}/mock/common/include", + "mock/include" + ] + + sources = [ + "${ability_runtime_test_path}/mock/services_appmgr_test/src/mock_bundle_manager.cpp", + "${ability_runtime_test_path}/mock/services_appmgr_test/src/mock_overlay_manager.cpp", + "${ability_runtime_test_path}/unittest/quick_fix/mock/src/mock_quick_fix_util.cpp", + "mock/src/mock_my_flag.cpp", + "mock/src/mock_permission_verification.cpp", + "quick_fix_manager_service_second_test.cpp", + ] + + configs = [ "${ability_runtime_services_path}/common:common_config" ] + + cflags = [] + + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } + + deps = [ + "${ability_runtime_native_path}/appkit:appkit_manager_helper", + "${ability_runtime_services_path}/quickfixmgr:quickfixms_static", + ] + + external_deps = [ + "ability_base:want", + "ability_runtime:abilitykit_native", + "ability_runtime:app_manager", + "ability_runtime:quickfix_manager", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "common_event_service:cesfwk_innerkits", + "eventhandler:libeventhandler", + "googletest:gmock_main", + "googletest:gtest_main", + "hilog:libhilog", + "hitrace:hitrace_meter", + "ipc:ipc_core", + "napi:ace_napi", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] +} + +group("unittest") { + testonly = true + deps = [ + ":quick_fix_manager_service_second_test", + ] +} diff --git a/test/unittest/quick_fix/quick_fix_manager_service_second_test/mock/include/mock_my_flag.h b/test/unittest/quick_fix/quick_fix_manager_service_second_test/mock/include/mock_my_flag.h new file mode 100644 index 0000000000000000000000000000000000000000..46711df377726ad32baf49a61f66768e0b1a9182 --- /dev/null +++ b/test/unittest/quick_fix/quick_fix_manager_service_second_test/mock/include/mock_my_flag.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2025 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. + */ + +#ifndef MOCK_MY_FLAG_H +#define MOCK_MY_FLAG_H + +namespace OHOS { +namespace AAFwk { +class MyFlag { +public: + static int flag_; + static bool isAllowedToUseSystemAPIFlag_; + static bool verifyCallingPermissionFlag_; + static bool checkSpecificFlag_; + static int usedCheckSpecificTimes_; + static bool isVerifyInstallBundlePermission_; + static bool isVerifyPrivilegedPermission_; +}; +} // namespace AAFwk +} // namespace OHOS +#endif // MOCK_MY_FLAG_H diff --git a/test/unittest/quick_fix/quick_fix_manager_service_second_test/mock/include/mock_permission_verification.h b/test/unittest/quick_fix/quick_fix_manager_service_second_test/mock/include/mock_permission_verification.h new file mode 100644 index 0000000000000000000000000000000000000000..d99fcf560aa7ea7b9eec6b3fdb027c070f311117 --- /dev/null +++ b/test/unittest/quick_fix/quick_fix_manager_service_second_test/mock/include/mock_permission_verification.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2025 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. + */ + +#ifndef OHOS_ABILITY_RUNTIME_PERMISSION_VERIFICATION_H +#define OHOS_ABILITY_RUNTIME_PERMISSION_VERIFICATION_H + +#include + +#include "mock_my_flag.h" +#include "singleton.h" + +namespace OHOS { +namespace AAFwk { + +class PermissionVerification : public DelayedSingleton { +public: + PermissionVerification() = default; + ~PermissionVerification() = default; + static PermissionVerification &GetInstance() + { + static PermissionVerification instance; + return instance; + }; + + bool VerifyCallingPermission(const std::string &permissionName) const; + + bool IsSACall() const; + + bool CheckSpecificSystemAbilityAccessPermission(const std::string &processName) const; + bool JudgeCallerIsAllowedToUseSystemAPI() const; + bool VerifyInstallBundlePermission() const; + bool VerifyGetBundleInfoPrivilegedPermission() const; + +private: + DISALLOW_COPY_AND_MOVE(PermissionVerification); +}; +} // namespace AAFwk +} // namespace OHOS +#endif // OHOS_ABILITY_RUNTIME_PERMISSION_VERIFICATION_H \ No newline at end of file diff --git a/test/unittest/quick_fix/quick_fix_manager_service_second_test/mock/src/mock_my_flag.cpp b/test/unittest/quick_fix/quick_fix_manager_service_second_test/mock/src/mock_my_flag.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7467830fbb668e9313b00d19c39f83d4efbde1cb --- /dev/null +++ b/test/unittest/quick_fix/quick_fix_manager_service_second_test/mock/src/mock_my_flag.cpp @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2025 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 "../../mock/include/mock_my_flag.h" + +namespace OHOS { +namespace AAFwk { +int MyFlag::flag_ = 0; +bool MyFlag::isAllowedToUseSystemAPIFlag_ = false; +bool MyFlag::verifyCallingPermissionFlag_ = false; +bool MyFlag::checkSpecificFlag_ = false; +int MyFlag::usedCheckSpecificTimes_ = 0; +bool MyFlag::isVerifyInstallBundlePermission_ = false; +bool MyFlag::isVerifyPrivilegedPermission_ = false; +} // namespace AAFwk +} // namespace OHOS diff --git a/test/unittest/quick_fix/quick_fix_manager_service_second_test/mock/src/mock_permission_verification.cpp b/test/unittest/quick_fix/quick_fix_manager_service_second_test/mock/src/mock_permission_verification.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b67448815175ad65d71adf728ea1861d12301a50 --- /dev/null +++ b/test/unittest/quick_fix/quick_fix_manager_service_second_test/mock/src/mock_permission_verification.cpp @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 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 "../../mock/include/mock_permission_verification.h" +#include "../../mock/include/mock_my_flag.h" + +namespace OHOS { +namespace AAFwk { +bool PermissionVerification::VerifyCallingPermission(const std::string &permissionName) const +{ + return MyFlag::verifyCallingPermissionFlag_; +} + +bool PermissionVerification::IsSACall() const +{ + return (MyFlag::flag_); +} + +bool PermissionVerification::CheckSpecificSystemAbilityAccessPermission(const std::string &processName) const +{ + MyFlag::usedCheckSpecificTimes_++; + return MyFlag::checkSpecificFlag_; +} + +bool PermissionVerification::JudgeCallerIsAllowedToUseSystemAPI() const +{ + return MyFlag::isAllowedToUseSystemAPIFlag_; +} + +bool PermissionVerification::VerifyInstallBundlePermission() const +{ + return MyFlag::isVerifyInstallBundlePermission_; +} + +bool PermissionVerification::VerifyGetBundleInfoPrivilegedPermission() const +{ + return MyFlag::isVerifyPrivilegedPermission_; +} +} // namespace AAFwk +} // namespace OHOS diff --git a/test/unittest/quick_fix/quick_fix_manager_service_second_test/quick_fix_manager_service_second_test.cpp b/test/unittest/quick_fix/quick_fix_manager_service_second_test/quick_fix_manager_service_second_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6190f0b50aa45d2dd705bfc0406e601d0e83ddd9 --- /dev/null +++ b/test/unittest/quick_fix/quick_fix_manager_service_second_test/quick_fix_manager_service_second_test.cpp @@ -0,0 +1,291 @@ +/* + * Copyright (c) 2025 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 "bundle_mgr_interface.h" +#include "hilog_tag_wrapper.h" +#include "if_system_ability_manager.h" +#include "mock_bundle_installer_service.h" +#include "mock_bundle_manager.h" +#include "mock_bundle_manager_service.h" +#include "mock_quick_fix_util.h" +#include "mock_system_ability_manager.h" +#include "quick_fix_error_utils.h" +#define private public +#include "iservice_registry.h" +#include "quick_fix_manager_service.h" +#undef private +#include "system_ability_definition.h" +#include "mock_my_flag.h" +#include "bundle_mgr_helper.h" +#include "mock_permission_verification.h" + +using namespace testing; +using namespace testing::ext; + +namespace OHOS { +namespace AAFwk { +class QuickFixManagerServiceTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp() override; + void TearDown() override; + + sptr quickFixMs_ = nullptr; +}; + +void QuickFixManagerServiceTest::SetUpTestCase(void) +{} + +void QuickFixManagerServiceTest::TearDownTestCase(void) +{} + +void QuickFixManagerServiceTest::SetUp() +{ + quickFixMs_ = QuickFixManagerService::GetInstance(); + ASSERT_NE(quickFixMs_, nullptr); + + auto ret = quickFixMs_->Init(); + ASSERT_TRUE(ret); + ASSERT_NE(quickFixMs_->eventRunner_, nullptr); + ASSERT_NE(quickFixMs_->eventHandler_, nullptr); +} + +void QuickFixManagerServiceTest::TearDown() +{ +} + +/** + * @tc.name: ApplyQuickFix_0100 + * @tc.desc: ApplyQuickFix + * @tc.type: FUNC + * @tc.require: issueI5OD2E + */ +HWTEST_F(QuickFixManagerServiceTest, ApplyQuickFix_0100, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__); + + MyFlag::isAllowedToUseSystemAPIFlag_ = false; + std::vector quickFixFiles; + int32_t ret = quickFixMs_->ApplyQuickFix(quickFixFiles); + EXPECT_EQ(ret, QUICK_FIX_NOT_SYSTEM_APP); + + TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__); +} + +/** + * @tc.name: ApplyQuickFix_0200 + * @tc.desc: ApplyQuickFix + * @tc.type: FUNC + * @tc.require: issueI5OD2E + */ +HWTEST_F(QuickFixManagerServiceTest, ApplyQuickFix_0200, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__); + + MyFlag::isAllowedToUseSystemAPIFlag_ = true; + MyFlag::isVerifyInstallBundlePermission_= false; + std::vector quickFixFiles; + int32_t ret = quickFixMs_->ApplyQuickFix(quickFixFiles); + EXPECT_EQ(ret, QUICK_FIX_VERIFY_PERMISSION_FAILED); + + TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__); +} + +/** + * @tc.name: ApplyQuickFix_0300 + * @tc.desc: ApplyQuickFix + * @tc.type: FUNC + * @tc.require: issueI5OD2E + */ +HWTEST_F(QuickFixManagerServiceTest, ApplyQuickFix_0300, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__); + + MyFlag::isAllowedToUseSystemAPIFlag_ = true; + MyFlag::isVerifyInstallBundlePermission_= true; + MyFlag::isVerifyPrivilegedPermission_= false; + std::vector quickFixFiles; + int32_t ret = quickFixMs_->ApplyQuickFix(quickFixFiles); + EXPECT_EQ(ret, QUICK_FIX_CONNECT_FAILED); + + TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__); +} + +/** + * @tc.name: ApplyQuickFix_0400 + * @tc.desc: ApplyQuickFix + * @tc.type: FUNC + * @tc.require: issueI5OD2E + */ +HWTEST_F(QuickFixManagerServiceTest, ApplyQuickFix_0400, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__); + + MyFlag::isAllowedToUseSystemAPIFlag_ = true; + MyFlag::isVerifyInstallBundlePermission_= true; + MyFlag::isVerifyPrivilegedPermission_= true; + QuickFixUtil::setBundleMgrProxyNull_ = true; + std::vector quickFixFiles; + int32_t ret = quickFixMs_->ApplyQuickFix(quickFixFiles); + QuickFixUtil::setBundleMgrProxyNull_ = false; + EXPECT_EQ(ret, QUICK_FIX_CONNECT_FAILED); + + TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__); +} + +/** + * @tc.name: ApplyQuickFix_0500 + * @tc.desc: ApplyQuickFix + * @tc.type: FUNC + * @tc.require: issueI5OD2E + */ +HWTEST_F(QuickFixManagerServiceTest, ApplyQuickFix_0500, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__); + + MyFlag::isAllowedToUseSystemAPIFlag_ = true; + MyFlag::isVerifyInstallBundlePermission_= true; + MyFlag::isVerifyPrivilegedPermission_= true; + QuickFixUtil::setAppManagerProxyNull_ = true; + std::vector quickFixFiles; + int32_t ret = quickFixMs_->ApplyQuickFix(quickFixFiles); + QuickFixUtil::setAppManagerProxyNull_ = false; + EXPECT_EQ(ret, QUICK_FIX_CONNECT_FAILED); + + TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__); +} + +/** + * @tc.name: GetApplyedQuickFixInfo_0100 + * @tc.desc: GetApplyedQuickFixInfo + * @tc.type: FUNC + * @tc.require: issueI5OD2E + */ +HWTEST_F(QuickFixManagerServiceTest, GetApplyedQuickFixInfo_0100, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__); + + MyFlag::isAllowedToUseSystemAPIFlag_ = false; + std::string bundleName = "test bundleName"; + ApplicationQuickFixInfo quickFixFileInfo; + int32_t ret = quickFixMs_->GetApplyedQuickFixInfo(bundleName, quickFixFileInfo); + EXPECT_EQ(ret, QUICK_FIX_NOT_SYSTEM_APP); + + TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__); +} + +/** + * @tc.name: GetApplyedQuickFixInfo_0200 + * @tc.desc: GetApplyedQuickFixInfo + * @tc.type: FUNC + * @tc.require: issueI5OD2E + */ +HWTEST_F(QuickFixManagerServiceTest, GetApplyedQuickFixInfo_0200, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__); + + MyFlag::isAllowedToUseSystemAPIFlag_ = true; + MyFlag::isVerifyPrivilegedPermission_ = false; + std::string bundleName = "test bundleName"; + ApplicationQuickFixInfo quickFixFileInfo; + int32_t ret = quickFixMs_->GetApplyedQuickFixInfo(bundleName, quickFixFileInfo); + EXPECT_EQ(ret, QUICK_FIX_VERIFY_PERMISSION_FAILED); + + TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__); +} + +/** + * @tc.name: GetApplyedQuickFixInfo_0300 + * @tc.desc: GetApplyedQuickFixInfo + * @tc.type: FUNC + * @tc.require: issueI5OD2E + */ +HWTEST_F(QuickFixManagerServiceTest, GetApplyedQuickFixInfo_0300, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__); + + MyFlag::isAllowedToUseSystemAPIFlag_ = true; + MyFlag::isVerifyPrivilegedPermission_= true; + QuickFixUtil::setBundleMgrProxyNull_ = true; + std::string bundleName = "test bundleName"; + ApplicationQuickFixInfo quickFixFileInfo; + int32_t ret = quickFixMs_->GetApplyedQuickFixInfo(bundleName, quickFixFileInfo); + QuickFixUtil::setBundleMgrProxyNull_ = false; + EXPECT_EQ(ret, QUICK_FIX_GET_BUNDLE_INFO_FAILED); + + TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__); +} + +/** + * @tc.name: RevokeQuickFix_0100 + * @tc.desc: RevokeQuickFix + * @tc.type: FUNC + * @tc.require: issueI5OD2E + */ +HWTEST_F(QuickFixManagerServiceTest, RevokeQuickFix_0100, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__); + + MyFlag::isAllowedToUseSystemAPIFlag_ = false; + std::string bundleName = "test bundleName"; + int32_t ret = quickFixMs_->RevokeQuickFix(bundleName); + EXPECT_EQ(ret, QUICK_FIX_NOT_SYSTEM_APP); + + TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__); +} + +/** + * @tc.name: RevokeQuickFix_0200 + * @tc.desc: RevokeQuickFix + * @tc.type: FUNC + * @tc.require: issueI5OD2E + */ +HWTEST_F(QuickFixManagerServiceTest, RevokeQuickFix_0200, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__); + + MyFlag::isAllowedToUseSystemAPIFlag_ = true; + MyFlag::isVerifyInstallBundlePermission_= false; + std::string bundleName = "test bundleName"; + int32_t ret = quickFixMs_->RevokeQuickFix(bundleName); + EXPECT_EQ(ret, QUICK_FIX_VERIFY_PERMISSION_FAILED); + + TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__); +} + +/** + * @tc.name: RevokeQuickFix_0300 + * @tc.desc: RevokeQuickFix + * @tc.type: FUNC + * @tc.require: issueI5OD2E + */ +HWTEST_F(QuickFixManagerServiceTest, RevokeQuickFix_0300, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__); + + MyFlag::isAllowedToUseSystemAPIFlag_ = true; + MyFlag::isVerifyInstallBundlePermission_= true; + MyFlag::isVerifyPrivilegedPermission_= false; + std::string bundleName = "test bundleName"; + int32_t ret = quickFixMs_->RevokeQuickFix(bundleName); + EXPECT_EQ(ret, QUICK_FIX_VERIFY_PERMISSION_FAILED); + + TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__); +} +} // namespace AppExecFwk +} // namespace OHOS \ No newline at end of file diff --git a/test/unittest/quick_fix/quick_fix_manager_service_test/quick_fix_manager_apply_task_test.cpp b/test/unittest/quick_fix/quick_fix_manager_service_test/quick_fix_manager_apply_task_test.cpp index 2e13c4df141944b8ea938caf54cbc44f85992bed..9e4c48916bb71521d73a4b4acb4f87aeccf2bcd8 100644 --- a/test/unittest/quick_fix/quick_fix_manager_service_test/quick_fix_manager_apply_task_test.cpp +++ b/test/unittest/quick_fix/quick_fix_manager_service_test/quick_fix_manager_apply_task_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2023 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 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 @@ -28,6 +28,7 @@ #include "quick_fix_result_info.h" #include "system_ability_definition.h" #include "token_setproc.h" +#include "json_utils.h" using namespace testing; using namespace testing::ext; @@ -35,6 +36,11 @@ using namespace testing::ext; namespace OHOS { namespace AAFwk { namespace { +constexpr const char *QUICK_FIX_BUNDLE_NAME = "bundleName"; +constexpr const char *QUICK_FIX_BUNDLE_VERSION_CODE = "bundleVersionCode"; +constexpr const char *QUICK_FIX_PATCH_VERSION_CODE = "patchVersionCode"; +constexpr const char *QUICK_FIX_IS_SO_CONTAINED = "isSoContained"; +constexpr const char *QUICK_FIX_TYPE = "type"; template static void WaitUntilTaskCalled(const F& f, const std::shared_ptr& handler, std::atomic& taskCalled) @@ -994,5 +1000,268 @@ HWTEST_F(QuickFixManagerApplyTaskTest, HandleRevokeQuickFixAppStop_0200, TestSiz EXPECT_EQ(applyTask->quickFixMgrService_.promote(), quickFixMs_); TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__); } + +/** + * @tc.name: ExtractQuickFixDataFromJson_0100 + * @tc.desc: Verify the correctness of the function parsing + * @tc.type: FUNC + * @tc.require: issueI5OD2E + */ +HWTEST_F(QuickFixManagerApplyTaskTest, ExtractQuickFixDataFromJson_0100, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__); + nlohmann::json jsonData = { + {QUICK_FIX_BUNDLE_NAME, "com.example.bundle"}, + {QUICK_FIX_BUNDLE_VERSION_CODE, 123}, + {QUICK_FIX_PATCH_VERSION_CODE, 456}, + {QUICK_FIX_IS_SO_CONTAINED, true}, + {QUICK_FIX_TYPE, 2} + }; + auto applyTask = std::make_shared(nullptr, appMgr_, + quickFixMs_->eventHandler_, quickFixMs_); + bool ret = applyTask->ExtractQuickFixDataFromJson(jsonData); + EXPECT_TRUE(ret); + EXPECT_EQ(applyTask->bundleName_, "com.example.bundle"); + EXPECT_EQ(applyTask->bundleVersionCode_, 123); + EXPECT_EQ(applyTask->patchVersionCode_, 456); + EXPECT_EQ(applyTask->isSoContained_, true); + EXPECT_EQ(applyTask->type_, AppExecFwk::QuickFixType::HOT_RELOAD); + TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__); +} + +/** + * @tc.name: ExtractQuickFixDataFromJson_0200 + * @tc.desc: Verify the parsing fail, when QUICK_FIX_BUNDLE_NAME is missing + * @tc.type: FUNC + * @tc.require: issueI5OD2E + */ +HWTEST_F(QuickFixManagerApplyTaskTest, ExtractQuickFixDataFromJson_0200, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__); + nlohmann::json jsonData = { + {QUICK_FIX_BUNDLE_VERSION_CODE, 123}, + {QUICK_FIX_PATCH_VERSION_CODE, 456}, + {QUICK_FIX_IS_SO_CONTAINED, true}, + {QUICK_FIX_TYPE, 2} + }; + auto applyTask = std::make_shared(nullptr, appMgr_, + quickFixMs_->eventHandler_, quickFixMs_); + bool ret = applyTask->ExtractQuickFixDataFromJson(jsonData); + EXPECT_FALSE(ret); + TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__); +} + +/** + * @tc.name: ExtractQuickFixDataFromJson_0300 + * @tc.desc: Verify the parsing fail, when QUICK_FIX_BUNDLE_VERSION_CODE is missing + * @tc.type: FUNC + * @tc.require: issueI5OD2E + */ +HWTEST_F(QuickFixManagerApplyTaskTest, ExtractQuickFixDataFromJson_0300, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__); + nlohmann::json jsonData = { + {QUICK_FIX_BUNDLE_NAME, "com.example.bundle"}, + {QUICK_FIX_PATCH_VERSION_CODE, 456}, + {QUICK_FIX_IS_SO_CONTAINED, true}, + {QUICK_FIX_TYPE, 2} + }; + auto applyTask = std::make_shared(nullptr, appMgr_, + quickFixMs_->eventHandler_, quickFixMs_); + bool ret = applyTask->ExtractQuickFixDataFromJson(jsonData); + EXPECT_FALSE(ret); + TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__); +} + +/** + * @tc.name: ExtractQuickFixDataFromJson_0400 + * @tc.desc: Verify the parsing fail, when QUICK_FIX_PATCH_VERSION_CODE is missing + * @tc.type: FUNC + * @tc.require: issueI5OD2E + */ +HWTEST_F(QuickFixManagerApplyTaskTest, ExtractQuickFixDataFromJson_0400, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__); + nlohmann::json jsonData = { + {QUICK_FIX_BUNDLE_NAME, "com.example.bundle"}, + {QUICK_FIX_BUNDLE_VERSION_CODE, 123}, + {QUICK_FIX_IS_SO_CONTAINED, true}, + {QUICK_FIX_TYPE, 2} + }; + auto applyTask = std::make_shared(nullptr, appMgr_, + quickFixMs_->eventHandler_, quickFixMs_); + bool ret = applyTask->ExtractQuickFixDataFromJson(jsonData); + EXPECT_FALSE(ret); + TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__); +} + +/** + * @tc.name: ExtractQuickFixDataFromJson_0500 + * @tc.desc: Verify the parsing fail, when QUICK_FIX_IS_SO_CONTAINED is missing + * @tc.type: FUNC + * @tc.require: issueI5OD2E + */ +HWTEST_F(QuickFixManagerApplyTaskTest, ExtractQuickFixDataFromJson_0500, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__); + nlohmann::json jsonData = { + {QUICK_FIX_BUNDLE_NAME, "com.example.bundle"}, + {QUICK_FIX_BUNDLE_VERSION_CODE, 123}, + {QUICK_FIX_PATCH_VERSION_CODE, 456}, + {QUICK_FIX_TYPE, 2} + }; + auto applyTask = std::make_shared(nullptr, appMgr_, + quickFixMs_->eventHandler_, quickFixMs_); + bool ret = applyTask->ExtractQuickFixDataFromJson(jsonData); + EXPECT_FALSE(ret); + TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__); +} + +/** + * @tc.name: ExtractQuickFixDataFromJson_0600 + * @tc.desc: Verify the parsing fail, when QUICK_FIX_TYPE is missing + * @tc.type: FUNC + * @tc.require: issueI5OD2E + */ +HWTEST_F(QuickFixManagerApplyTaskTest, ExtractQuickFixDataFromJson_0600, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__); + nlohmann::json jsonData = { + {QUICK_FIX_BUNDLE_NAME, "com.example.bundle"}, + {QUICK_FIX_BUNDLE_VERSION_CODE, 123}, + {QUICK_FIX_PATCH_VERSION_CODE, 456}, + {QUICK_FIX_IS_SO_CONTAINED, true}, + }; + auto applyTask = std::make_shared(nullptr, appMgr_, + quickFixMs_->eventHandler_, quickFixMs_); + bool ret = applyTask->ExtractQuickFixDataFromJson(jsonData); + EXPECT_FALSE(ret); + TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__); +} + +/** + * @tc.name: ExtractQuickFixDataFromJson_0700 + * @tc.desc: Verify the parsing fail, when QUICK_FIX_BUNDLE_NAME type is wrong + * @tc.type: FUNC + * @tc.require: issueI5OD2E + */ +HWTEST_F(QuickFixManagerApplyTaskTest, ExtractQuickFixDataFromJson_0700, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__); + nlohmann::json jsonData = { + {QUICK_FIX_BUNDLE_NAME, 123}, + {QUICK_FIX_BUNDLE_VERSION_CODE, 123}, + {QUICK_FIX_PATCH_VERSION_CODE, 456}, + {QUICK_FIX_IS_SO_CONTAINED, true}, + {QUICK_FIX_TYPE, 2} + }; + auto applyTask = std::make_shared(nullptr, appMgr_, + quickFixMs_->eventHandler_, quickFixMs_); + bool ret = applyTask->ExtractQuickFixDataFromJson(jsonData); + EXPECT_FALSE(ret); + TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__); +} + +/** + * @tc.name: ExtractQuickFixDataFromJson_0800 + * @tc.desc: Verify the parsing fail, when QUICK_FIX_BUNDLE_VERSION_CODE type is wrong + * @tc.type: FUNC + * @tc.require: issueI5OD2E + */ +HWTEST_F(QuickFixManagerApplyTaskTest, ExtractQuickFixDataFromJson_0800, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__); + nlohmann::json jsonData = { + {QUICK_FIX_BUNDLE_NAME, "com.example.bundle"}, + {QUICK_FIX_BUNDLE_VERSION_CODE, "invalid"}, + {QUICK_FIX_PATCH_VERSION_CODE, 456}, + {QUICK_FIX_IS_SO_CONTAINED, true}, + {QUICK_FIX_TYPE, 2} + }; + auto applyTask = std::make_shared(nullptr, appMgr_, + quickFixMs_->eventHandler_, quickFixMs_); + bool ret = applyTask->ExtractQuickFixDataFromJson(jsonData); + EXPECT_FALSE(ret); + EXPECT_EQ(applyTask->bundleName_, "com.example.bundle"); + TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__); +} + +/** + * @tc.name: ExtractQuickFixDataFromJson_0900 + * @tc.desc: Verify the parsing fail, when QUICK_FIX_PATCH_VERSION_CODE type is wrong + * @tc.type: FUNC + * @tc.require: issueI5OD2E + */ +HWTEST_F(QuickFixManagerApplyTaskTest, ExtractQuickFixDataFromJson_0900, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__); + nlohmann::json jsonData = { + {QUICK_FIX_BUNDLE_NAME, "com.example.bundle"}, + {QUICK_FIX_BUNDLE_VERSION_CODE, 123}, + {QUICK_FIX_PATCH_VERSION_CODE, "invalid"}, + {QUICK_FIX_IS_SO_CONTAINED, true}, + {QUICK_FIX_TYPE, 2} + }; + auto applyTask = std::make_shared(nullptr, appMgr_, + quickFixMs_->eventHandler_, quickFixMs_); + bool ret = applyTask->ExtractQuickFixDataFromJson(jsonData); + EXPECT_FALSE(ret); + EXPECT_EQ(applyTask->bundleName_, "com.example.bundle"); + EXPECT_EQ(applyTask->bundleVersionCode_, 123); + TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__); +} + +/** + * @tc.name: ExtractQuickFixDataFromJson_1000 + * @tc.desc: Verify the parsing fail, when QUICK_FIX_IS_SO_CONTAINED type is wrong + * @tc.type: FUNC + * @tc.require: issueI5OD2E + */ +HWTEST_F(QuickFixManagerApplyTaskTest, ExtractQuickFixDataFromJson_1000, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__); + nlohmann::json jsonData = { + {QUICK_FIX_BUNDLE_NAME, "com.example.bundle"}, + {QUICK_FIX_BUNDLE_VERSION_CODE, 123}, + {QUICK_FIX_PATCH_VERSION_CODE, 456}, + {QUICK_FIX_IS_SO_CONTAINED, "invalid"}, + {QUICK_FIX_TYPE, 2} + }; + auto applyTask = std::make_shared(nullptr, appMgr_, + quickFixMs_->eventHandler_, quickFixMs_); + bool ret = applyTask->ExtractQuickFixDataFromJson(jsonData); + EXPECT_FALSE(ret); + EXPECT_EQ(applyTask->bundleName_, "com.example.bundle"); + EXPECT_EQ(applyTask->bundleVersionCode_, 123); + EXPECT_EQ(applyTask->patchVersionCode_, 456); + TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__); +} + +/** + * @tc.name: ExtractQuickFixDataFromJson_1100 + * @tc.desc: Verify the parsing fail, when QUICK_FIX_TYPE type is wrong + * @tc.type: FUNC + * @tc.require: issueI5OD2E + */ +HWTEST_F(QuickFixManagerApplyTaskTest, ExtractQuickFixDataFromJson_1100, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__); + nlohmann::json jsonData = { + {QUICK_FIX_BUNDLE_NAME, "com.example.bundle"}, + {QUICK_FIX_BUNDLE_VERSION_CODE, 123}, + {QUICK_FIX_PATCH_VERSION_CODE, 456}, + {QUICK_FIX_IS_SO_CONTAINED, false}, + {QUICK_FIX_TYPE, "invalid"} + }; + auto applyTask = std::make_shared(nullptr, appMgr_, + quickFixMs_->eventHandler_, quickFixMs_); + bool ret = applyTask->ExtractQuickFixDataFromJson(jsonData); + EXPECT_FALSE(ret); + EXPECT_EQ(applyTask->bundleName_, "com.example.bundle"); + EXPECT_EQ(applyTask->bundleVersionCode_, 123); + EXPECT_EQ(applyTask->patchVersionCode_, 456); + EXPECT_FALSE(applyTask->isSoContained_); + TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__); +} } // namespace AppExecFwk } // namespace OHOS \ No newline at end of file