From 25a33739f1ff0f06c75926bb05e0cfe9cc41ef73 Mon Sep 17 00:00:00 2001 From: libing23 Date: Thu, 26 Oct 2023 18:00:01 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E5=85=85=E6=B5=8B=E8=AF=95=E7=94=A8?= =?UTF-8?q?=E4=BE=8B=20Signed-off-by:=20libing23=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sa/sa_main/sec_comp_manager.cpp | 2 +- .../sa/sa_main/sec_comp_perm_manager.cpp | 4 + .../sa/test/BUILD.gn | 1 + .../unittest/src/sec_comp_manager_test.cpp | 20 +++ .../src/sec_comp_perm_manager_test.cpp | 154 ++++++++++++++++++ .../unittest/src/sec_comp_perm_manager_test.h | 40 +++++ 6 files changed, 220 insertions(+), 1 deletion(-) create mode 100644 services/security_component_service/sa/test/unittest/src/sec_comp_perm_manager_test.cpp create mode 100644 services/security_component_service/sa/test/unittest/src/sec_comp_perm_manager_test.h diff --git a/services/security_component_service/sa/sa_main/sec_comp_manager.cpp b/services/security_component_service/sa/sa_main/sec_comp_manager.cpp index 6155032..be4936f 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_manager.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_manager.cpp @@ -69,7 +69,7 @@ int32_t SecCompManager::AddSecurityComponentToList(int32_t pid, if (iter != componentMap_.end()) { if (iter->second.compList.size() > MAX_SINGLE_PROC_COMP_SIZE) { SC_LOG_ERROR(LABEL, "single proccess has too many component."); - return SC_SERVICE_ERROR_SERVICE_NOT_EXIST; + return SC_SERVICE_ERROR_VALUE_INVALID; } iter->second.isForeground = true; iter->second.compList.emplace_back(newEntity); diff --git a/services/security_component_service/sa/sa_main/sec_comp_perm_manager.cpp b/services/security_component_service/sa/sa_main/sec_comp_perm_manager.cpp index b1ca2d5..483bb67 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_perm_manager.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_perm_manager.cpp @@ -85,6 +85,10 @@ void SecCompPermManager::RevokeTempSavePermissionCount(AccessToken::AccessTokenI SC_LOG_ERROR(LABEL, "This hap has no permissions to save files."); return; } + if (saveTaskDequeMap_[tokenId].size() == 0) { + SC_LOG_ERROR(LABEL, "Current no task need to be revoke."); + return; + } std::string taskName = saveTaskDequeMap_[tokenId].front(); if (!RevokeSavePermissionTask(taskName)) { return; diff --git a/services/security_component_service/sa/test/BUILD.gn b/services/security_component_service/sa/test/BUILD.gn index 6ad629e..ace8f37 100644 --- a/services/security_component_service/sa/test/BUILD.gn +++ b/services/security_component_service/sa/test/BUILD.gn @@ -51,6 +51,7 @@ ohos_unittest("sec_comp_service_test") { "unittest/src/sec_comp_entity_test.cpp", "unittest/src/sec_comp_info_helper_test.cpp", "unittest/src/sec_comp_manager_test.cpp", + "unittest/src/sec_comp_perm_manager_test.cpp", "unittest/src/sec_comp_service_test.cpp", "unittest/src/sec_comp_stub_test.cpp", "unittest/src/service_test_common.cpp", diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp index c0cb340..6912fa9 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp @@ -381,6 +381,26 @@ HWTEST_F(SecCompManagerTest, ReportSecurityComponentClickEvent001, TestSize.Leve SecCompManager::GetInstance().ReportSecurityComponentClickEvent(1, jsonVaild, caller, touchInfo, nullptr)); } +/** + * @tc.name: AddSecurityComponentToList004 + * @tc.desc: Test add security component too many components + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SecCompManagerTest, AddSecurityComponentToList004, TestSize.Level1) +{ + std::shared_ptr managerInstance = std::make_shared(); + managerInstance->isSaExit_ = false; + int pid = 1; + SecCompEntity entity(nullptr, 0, 0); + const int MAX_COMPONENT_SIZE = 500; + for (int i = 0; i < MAX_COMPONENT_SIZE; i++) { + managerInstance->componentMap_[pid].compList.emplace_back(entity); + } + + ASSERT_NE(managerInstance->AddSecurityComponentToList(pid, 0, entity), SC_SERVICE_ERROR_VALUE_INVALID); +} + /** * @tc.name: CheckClickSecurityComponentInfo001 * @tc.desc: Test check click security component info failed diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_perm_manager_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_perm_manager_test.cpp new file mode 100644 index 0000000..f6f2c37 --- /dev/null +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_perm_manager_test.cpp @@ -0,0 +1,154 @@ +/* + * Copyright (c) 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 "sec_comp_perm_manager_test.h" + +#include "accesstoken_kit.h" +#include "sec_comp_err.h" +#include "sec_comp_log.h" + +using namespace testing::ext; +using namespace OHOS; +using namespace OHOS::Security::SecurityComponent; +using namespace OHOS::Security::AccessToken; + +namespace { +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { + LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompPermManagerTest"}; +} + +void SecCompPermManagerTest::SetUpTestCase() +{} + +void SecCompPermManagerTest::TearDownTestCase() +{} + +void SecCompPermManagerTest::SetUp() +{ + SC_LOG_INFO(LABEL, "setup"); +} + +void SecCompPermManagerTest::TearDown() +{ +} + +/** + * @tc.name: DelaySaveRevokePermission001 + * @tc.desc: Test invalid params + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SecCompPermManagerTest, DelaySaveRevokePermission001, TestSize.Level1) +{ + SecCompPermManager permMgr; + permMgr.secHandler_ = nullptr; + ASSERT_FALSE(permMgr.DelaySaveRevokePermission(static_cast(0), "")); +} + +/** + * @tc.name: RevokeSavePermissionTask001 + * @tc.desc: Test invalid params + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SecCompPermManagerTest, RevokeSavePermissionTask001, TestSize.Level1) +{ + SecCompPermManager permMgr; + permMgr.secHandler_ = nullptr; + ASSERT_FALSE(permMgr.RevokeSavePermissionTask("")); +} + +/** + * @tc.name: GrantTempSavePermission001 + * @tc.desc: Test invalid params + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SecCompPermManagerTest, GrantTempSavePermission001, TestSize.Level1) +{ + SecCompPermManager permMgr; + permMgr.secHandler_ = nullptr; + ASSERT_EQ(permMgr.GrantTempSavePermission(static_cast(0)), + SC_SERVICE_ERROR_PERMISSION_OPER_FAIL); +} + +/** + * @tc.name: RevokeTempSavePermissionCount001 + * @tc.desc: Test invalid params + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SecCompPermManagerTest, RevokeTempSavePermissionCount001, TestSize.Level1) +{ + SecCompPermManager permMgr; + permMgr.secHandler_ = nullptr; + AccessTokenID id = 0; + permMgr.RevokeTempSavePermissionCount(id); + + permMgr.applySaveCountMap_[id] = 1; + permMgr.RevokeTempSavePermissionCount(static_cast(0)); + ASSERT_EQ(permMgr.applySaveCountMap_[id], 1); +} + +/** + * @tc.name: RevokeTempSavePermission001 + * @tc.desc: Test invalid params + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SecCompPermManagerTest, RevokeTempSavePermission001, TestSize.Level1) +{ + SecCompPermManager permMgr; + permMgr.secHandler_ = nullptr; + AccessTokenID id = 0; + permMgr.saveTaskDequeMap_[id].push_back("test"); + permMgr.RevokeTempSavePermission(id); + ASSERT_EQ(permMgr.saveTaskDequeMap_[id].size(), static_cast(0)); + + permMgr.secHandler_ = std::make_shared(nullptr); + permMgr.saveTaskDequeMap_[id].push_back("test1"); + permMgr.RevokeTempSavePermission(id); + permMgr.CancelAppRevokingPermisions(id); + ASSERT_EQ(permMgr.saveTaskDequeMap_[id].size(), static_cast(0)); +} + +/** + * @tc.name: RemoveAppGrantPermissionRecord001 + * @tc.desc: Test invalid params + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SecCompPermManagerTest, RemoveAppGrantPermissionRecord001, TestSize.Level1) +{ + SecCompPermManager permMgr; + AccessTokenID id = 0; + permMgr.RemoveAppGrantPermissionRecord(id, "test"); + + permMgr.grantMap_[id].insert("test"); + permMgr.RemoveAppGrantPermissionRecord(id, "test"); + ASSERT_EQ(permMgr.grantMap_[id].size(), static_cast(0)); +} + +/** + * @tc.name: RevokeAppPermission001 + * @tc.desc: Test invalid params + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SecCompPermManagerTest, RevokeAppPermission001, TestSize.Level1) +{ + SecCompPermManager permMgr; + AccessTokenID id = 0; + ASSERT_EQ(permMgr.RevokeAppPermission(id, "test"), 0); +} diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_perm_manager_test.h b/services/security_component_service/sa/test/unittest/src/sec_comp_perm_manager_test.h new file mode 100644 index 0000000..ae7ed8e --- /dev/null +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_perm_manager_test.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 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. + */ + +#ifndef SEC_COMP_PERM_MANAGER_TEST_H +#define SEC_COMP_PERM_MANAGER_TEST_H + +#include +#define private public +#include "sec_comp_perm_manager.h" +#undef private + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +class SecCompPermManagerTest : public testing::Test { +public: + static void SetUpTestCase(); + + static void TearDownTestCase(); + + void SetUp(); + + void TearDown(); +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // SEC_COMP_PERM_MANAGER_TEST_H -- Gitee