diff --git a/services/security_component_service/sa/test/BUILD.gn b/services/security_component_service/sa/test/BUILD.gn index 303038300435a771a231675adbeda79b7a6e6ef6..528b6fdfd1dda0a9e43ac765069859ae42c74fd4 100644 --- a/services/security_component_service/sa/test/BUILD.gn +++ b/services/security_component_service/sa/test/BUILD.gn @@ -57,6 +57,7 @@ ohos_unittest("sec_comp_service_test") { "unittest/src/sec_comp_stub_test.cpp", "unittest/src/service_test_common.cpp", "unittest/src/window_info_helper_test.cpp", + "unittest/src/zzenv_exit_operate.cpp", ] configs = [ "${sec_comp_root_dir}/config:coverage_flags" ] diff --git a/services/security_component_service/sa/test/unittest/src/first_use_dialog_test.cpp b/services/security_component_service/sa/test/unittest/src/first_use_dialog_test.cpp index bc89beb3836015b9a2c4d6c1f5f144f9ac559f24..fc4d4a08e16eaa825de11b6f6dece6b6a88447f2 100644 --- a/services/security_component_service/sa/test/unittest/src/first_use_dialog_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/first_use_dialog_test.cpp @@ -14,6 +14,7 @@ */ #include "first_use_dialog_test.h" +#include #include "accesstoken_kit.h" #include "location_button.h" #include "save_button.h" @@ -42,6 +43,23 @@ void FirstUseDialogTest::SetUpTestCase() void FirstUseDialogTest::TearDownTestCase() { + struct stat fstatJson = {}; + if (stat(SEC_COMP_SRV_CFG_FILE.c_str(), &fstatJson) != 0) { + return; + } + // if json file is created by root, delete it + if (fstatJson.st_uid == 0) { + std::string cmdline = "rm -f " + SEC_COMP_SRV_CFG_FILE; + system(cmdline.c_str()); + } + struct stat fstatDir = {}; + if (stat(SEC_COMP_SRV_CFG_PATH.c_str(), &fstatDir) != 0) { + return; + } + if (fstatDir.st_uid == 0) { + std::string cmdline = "chown security_component:security_component " + SEC_COMP_SRV_CFG_PATH; + system(cmdline.c_str()); + } } void FirstUseDialogTest::SetUp() diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp index 8504ace88a518cafe05f119945a4b78c145ccace..37aa15007ac22d5f0c73840d9b1a0cb124eef925 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp @@ -24,6 +24,7 @@ #include "save_button.h" #include "sec_comp_log.h" #include "sec_comp_err.h" +#include "sec_comp_tool.h" #include "service_test_common.h" #include "window_manager.h" @@ -620,3 +621,31 @@ HWTEST_F(SecCompInfoHelperTest, GrantTempPermission001, TestSize.Level1) EXPECT_EQ(SC_SERVICE_ERROR_PERMISSION_OPER_FAIL, SecCompInfoHelper::GrantTempPermission(*tokenid, shared_comp)); } + +/** + * @tc.name: IsColorSimilar001 + * @tc.desc: Test IsColorSimilar + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SecCompInfoHelperTest, IsColorSimilar001, TestSize.Level1) +{ + SecCompColor color1 = { + .argb = { + .red = 0xFF, + .green = 0xFF, + .blue = 0xFF, + .alpha = 0xFF, + } + }; + + SecCompColor color2 = { + .argb = { + .red = 0xFF, + .green = 0xFF, + .blue = 0xFF, + .alpha = 0xF0, // different alpha + } + }; + EXPECT_TRUE(IsColorSimilar(color1, color2)); +} 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 e659faf11eb080c648b04bfe2589dabbc1b4d738..8845ecb7931ed14d75dd4e0328fc2ec09a072cd0 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 @@ -703,3 +703,28 @@ HWTEST_F(SecCompManagerTest, TransformCallBackResult001, TestSize.Level1) res = SC_SERVICE_ERROR_VALUE_INVALID; SecCompManager::GetInstance().SendCheckInfoEnhanceSysEvent(scId, LOCATION_COMPONENT, scene, res); } + +/** + * @tc.name: AddSecurityComponentProcess001 + * @tc.desc: Test AddSecurityComponentProcess + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SecCompManagerTest, AddSecurityComponentProcess001, TestSize.Level1) +{ + bool isSaExit = SecCompManager::GetInstance().isSaExit_; + SecCompManager::GetInstance().isSaExit_ = true; + SecCompCallerInfo info = { + .pid = 0, + .tokenId = 0, + .uid = 0, + }; + EXPECT_EQ(SC_SERVICE_ERROR_SERVICE_NOT_EXIST, + SecCompManager::GetInstance().AddSecurityComponentProcess(info)); + auto oldmap = SecCompManager::GetInstance().componentMap_; + SecCompManager::GetInstance().componentMap_.clear(); + SecCompManager::GetInstance().isSaExit_ = false; + EXPECT_EQ(SC_OK, SecCompManager::GetInstance().AddSecurityComponentProcess(info)); + SecCompManager::GetInstance().componentMap_ = oldmap; + SecCompManager::GetInstance().isSaExit_ = isSaExit; +} \ No newline at end of file diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_stub_mock_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_stub_mock_test.cpp index 6491b6856d0f75d520a7ac424e70946d12fb4a20..87d02efdd387ebecb6d3c398c20b4fe243b32a1d 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_stub_mock_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_stub_mock_test.cpp @@ -15,6 +15,7 @@ #include "sec_comp_stub_mock_test.h" +#include "sec_comp_dialog_callback.h" #include "sec_comp_log.h" #include "sec_comp_err.h" #include "sec_comp_click_event_parcel.h" @@ -249,3 +250,19 @@ HWTEST_F(SecCompStubMockTest, UnmarshallingMock001, TestSize.Level1) in.WriteBuffer(data, 32); EXPECT_NE(nullptr, clickParcel->Unmarshalling(in)); } + +/** + * @tc.name: PreRegisterSecCompProcessMock001 + * @tc.desc: Test PreRegisterSecCompProcessInner + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SecCompStubMockTest, PreRegisterSecCompProcessMock001, TestSize.Level1) +{ + MessageParcel data; + MessageParcel reply; + data.FlushBuffer(); + reply.FlushBuffer(); + data.WriteInt32(1); + ASSERT_EQ(SC_OK, stub_->PreRegisterSecCompProcessInner(data, reply)); +} \ No newline at end of file diff --git a/services/security_component_service/sa/test/unittest/src/zzenv_exit_operate.cpp b/services/security_component_service/sa/test/unittest/src/zzenv_exit_operate.cpp new file mode 100644 index 0000000000000000000000000000000000000000..55fc51ebdcf9fa8bcfad5ff6bb4eb764c2d32b8e --- /dev/null +++ b/services/security_component_service/sa/test/unittest/src/zzenv_exit_operate.cpp @@ -0,0 +1,71 @@ +/* + * 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. + */ + +#include +#define private public +#include "event_handler.h" +#include "event_runner.h" +#include "sec_comp_manager.h" +#undef private + +using namespace testing::ext; + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +/** + * @brief This test class is used to release resources before process end to avoid crash + */ +class ZZEnvExitOperate : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void ZZEnvExitOperate::SetUpTestCase() +{} + +void ZZEnvExitOperate::TearDownTestCase() +{} + +void ZZEnvExitOperate::SetUp() +{} + +void ZZEnvExitOperate::TearDown() +{} + +/** + * @tc.name: ZExitOperateTest001 + * @tc.desc: remove ffrt handler to avoid uaf crash + * @tc.type: FUNC + * @tc.require: None + */ +HWTEST_F(ZZEnvExitOperate, ZExitOperateTest001, TestSize.Level1) +{ + if (SecCompManager::GetInstance().secRunner_ != nullptr) { + SecCompManager::GetInstance().secRunner_->queue_.reset(); + SecCompManager::GetInstance().secRunner_ = nullptr; + } + if (SecCompManager::GetInstance().secHandler_ != nullptr) { + SecCompManager::GetInstance().secHandler_->eventRunner_.reset(); + SecCompManager::GetInstance().secHandler_ = nullptr; + } + sleep(5); +} +} +} +} \ No newline at end of file