From d239f5d79fe3fedc5bcc1393df47935537f4e034 Mon Sep 17 00:00:00 2001 From: libing23 Date: Sat, 26 Aug 2023 17:35:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0first=5Fuse=5Fdialog=20ut=20S?= =?UTF-8?q?igned-off-by:=20libing23=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../include/sec_comp_enhance_adapter.h | 2 +- .../unittest/src/sec_comp_enhance_test.cpp | 2 +- .../sa/sa_main/first_use_dialog.cpp | 18 +- .../sa/sa_main/first_use_dialog.h | 6 +- .../sa/sa_main/sec_comp_perm_manager.cpp | 1 - .../sa/test/BUILD.gn | 3 + .../sa/test/mock/include/accesstoken_kit.h | 3 +- .../sa/test/mock/include/app_mgr_interface.h | 1 - .../sa/test/mock/src/accesstoken_kit.cpp | 24 ++ .../unittest/src/first_use_dialog_test.cpp | 372 ++++++++++++++++++ .../test/unittest/src/first_use_dialog_test.h | 40 ++ .../sa/test/unittest/src/sec_comp_stub_test.h | 1 - ...eafterverifysavepermissionInner_fuzzer.cpp | 2 +- .../registersecuritycomponent_fuzzer.cpp | 2 +- 14 files changed, 456 insertions(+), 21 deletions(-) create mode 100644 services/security_component_service/sa/test/mock/src/accesstoken_kit.cpp create mode 100644 services/security_component_service/sa/test/unittest/src/first_use_dialog_test.cpp create mode 100644 services/security_component_service/sa/test/unittest/src/first_use_dialog_test.h diff --git a/frameworks/enhance_adapter/include/sec_comp_enhance_adapter.h b/frameworks/enhance_adapter/include/sec_comp_enhance_adapter.h index faada7e..5aad9b0 100644 --- a/frameworks/enhance_adapter/include/sec_comp_enhance_adapter.h +++ b/frameworks/enhance_adapter/include/sec_comp_enhance_adapter.h @@ -43,7 +43,7 @@ public: // for security component service to send command to enhance service class SecCompSrvEnhanceInterface { - public: +public: // enable input enhance, then enhance service send config to multimodalinput virtual int32_t EnableInputEnhance() = 0; diff --git a/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.cpp b/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.cpp index 0dad6f0..c36b4f8 100644 --- a/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.cpp +++ b/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.cpp @@ -13,9 +13,9 @@ * limitations under the License. */ #include "sec_comp_enhance_test.h" +#include #include "sec_comp_err.h" #include "sec_comp_log.h" -#include using namespace testing::ext; using namespace OHOS::Security::SecurityComponent; diff --git a/services/security_component_service/sa/sa_main/first_use_dialog.cpp b/services/security_component_service/sa/sa_main/first_use_dialog.cpp index 6439824..6840b51 100644 --- a/services/security_component_service/sa/sa_main/first_use_dialog.cpp +++ b/services/security_component_service/sa/sa_main/first_use_dialog.cpp @@ -43,6 +43,8 @@ const std::string TYPE_KEY = "ohos.user.security.type"; const std::string TOKEN_KEY = "ohos.ability.params.token"; constexpr uint32_t MAX_CFG_FILE_SIZE = 100 * 1024; // 100k +constexpr uint64_t LOCATION_BUTTON_FIRST_USE = 1 << 0; +constexpr uint64_t SAVE_BUTTON_FIRST_USE = 1 << 1; } bool FirstUseDialog::IsCfgDirExist(void) @@ -78,7 +80,7 @@ bool FirstUseDialog::IsCfgFileValid(void) return false; } if (fstat.st_size > MAX_CFG_FILE_SIZE) { - SC_LOG_INFO(LABEL, "path %{public}s errno %{public}d.", FIRST_USE_RECORD_JSON.c_str(), errno); + SC_LOG_INFO(LABEL, "path %{public}s size too large.", FIRST_USE_RECORD_JSON.c_str()); return false; } return true; @@ -243,16 +245,16 @@ void FirstUseDialog::SendSaveEventHandler(void) secHandler_->ProxyPostTask(delayed); } -void FirstUseDialog::NotifyFirstUseDialog(AccessToken::AccessTokenID tokenId, SecCompType type, +bool FirstUseDialog::NotifyFirstUseDialog(AccessToken::AccessTokenID tokenId, SecCompType type, sptr callerToken) { if (secHandler_ == nullptr) { SC_LOG_ERROR(LABEL, "event handler invalid."); - return; + return false; } if (callerToken == nullptr) { SC_LOG_INFO(LABEL, "callerToken is null, no need to notify dialog"); - return; + return false; } uint64_t typeMask; @@ -262,7 +264,7 @@ void FirstUseDialog::NotifyFirstUseDialog(AccessToken::AccessTokenID tokenId, Se typeMask = SAVE_BUTTON_FIRST_USE; } else { SC_LOG_INFO(LABEL, "this type need not notify dialog to user"); - return; + return false; } std::unique_lock lock(useMapMutex_); @@ -272,17 +274,18 @@ void FirstUseDialog::NotifyFirstUseDialog(AccessToken::AccessTokenID tokenId, Se StartDialogAbility(type, callerToken); firstUseMap_[tokenId] = typeMask; SendSaveEventHandler(); - return; + return true; } uint64_t compTypes = firstUseMap_[tokenId]; if ((compTypes & typeMask) == typeMask) { SC_LOG_INFO(LABEL, "no need notify again."); - return; + return true; } StartDialogAbility(type, callerToken); firstUseMap_[tokenId] |= typeMask; SendSaveEventHandler(); + return true; } void FirstUseDialog::Init(std::shared_ptr secHandler) @@ -294,4 +297,3 @@ void FirstUseDialog::Init(std::shared_ptr secHandler) } // namespace SecurityComponent } // namespace Security } // namespace OHOS - diff --git a/services/security_component_service/sa/sa_main/first_use_dialog.h b/services/security_component_service/sa/sa_main/first_use_dialog.h index fd04777..753500d 100644 --- a/services/security_component_service/sa/sa_main/first_use_dialog.h +++ b/services/security_component_service/sa/sa_main/first_use_dialog.h @@ -29,14 +29,11 @@ namespace OHOS { namespace Security { namespace SecurityComponent { -constexpr uint64_t LOCATION_BUTTON_FIRST_USE = 1 << 0; -constexpr uint64_t SAVE_BUTTON_FIRST_USE = 1 << 1; - class FirstUseDialog final { public: FirstUseDialog() = default; ~FirstUseDialog() = default; - void NotifyFirstUseDialog(AccessToken::AccessTokenID tokenId, SecCompType type, sptr callerToken); + bool NotifyFirstUseDialog(AccessToken::AccessTokenID tokenId, SecCompType type, sptr callerToken); void Init(std::shared_ptr secHandler); private: @@ -61,4 +58,3 @@ private: } // namespace Security } // namespace OHOS #endif // FIRST_USE_DIALOG_H - 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 e839a56..eaf7bec 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 @@ -121,7 +121,6 @@ bool SecCompPermManager::InitEventHandler(const std::shared_ptr secHandler_ = secHandler; return true; } - } // namespace SecurityComponent } // namespace Security } // namespace OHOS diff --git a/services/security_component_service/sa/test/BUILD.gn b/services/security_component_service/sa/test/BUILD.gn index 7e2e468..2759299 100644 --- a/services/security_component_service/sa/test/BUILD.gn +++ b/services/security_component_service/sa/test/BUILD.gn @@ -43,6 +43,7 @@ ohos_unittest("sec_comp_service_test") { "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_service.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_stub.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_event_handler.cpp", + "${sec_comp_root_dir}/services/security_component_service/sa/test/mock/src/accesstoken_kit.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/test/mock/src/mock_app_mgr_proxy.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/test/mock/src/mock_iservice_registry.cpp", "unittest/src/app_state_observer_test.cpp", @@ -111,9 +112,11 @@ ohos_unittest("sec_comp_service_mock_test") { "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_service.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_comp_stub.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/sec_event_handler.cpp", + "${sec_comp_root_dir}/services/security_component_service/sa/test/mock/src/accesstoken_kit.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/test/mock/src/mock_app_mgr_proxy.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/test/mock/src/mock_iservice_registry.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/test/mock/src/sec_comp_enhance_adapter.cpp", + "unittest/src/first_use_dialog_test.cpp", "unittest/src/sec_comp_service_mock_test.cpp", "unittest/src/service_test_common.cpp", ] diff --git a/services/security_component_service/sa/test/mock/include/accesstoken_kit.h b/services/security_component_service/sa/test/mock/include/accesstoken_kit.h index 0a746c2..9608e24 100644 --- a/services/security_component_service/sa/test/mock/include/accesstoken_kit.h +++ b/services/security_component_service/sa/test/mock/include/accesstoken_kit.h @@ -38,7 +38,7 @@ public: static int GetHapTokenInfo(AccessTokenID tokenID, HapTokenInfo& hapTokenInfoRes) { - return 0; + return AccessTokenKit::getHapTokenInfoRes; }; static AccessTokenID GetHapTokenID(int32_t userID, const std::string& bundleName, int32_t instIndex) @@ -51,6 +51,7 @@ public: AccessTokenIDInner *idInner = reinterpret_cast(&tokenID); return static_cast(idInner->type); }; + static int getHapTokenInfoRes; }; } // namespace SECURITY_COMPONENT_INTERFACES_INNER_KITS_ACCESSTOKEN_KIT_H } // namespace Security diff --git a/services/security_component_service/sa/test/mock/include/app_mgr_interface.h b/services/security_component_service/sa/test/mock/include/app_mgr_interface.h index b0a36ec..532309c 100644 --- a/services/security_component_service/sa/test/mock/include/app_mgr_interface.h +++ b/services/security_component_service/sa/test/mock/include/app_mgr_interface.h @@ -21,7 +21,6 @@ namespace OHOS { namespace AppExecFwk { - class IAppMgr : public IRemoteBroker { public: DECLARE_INTERFACE_DESCRIPTOR(u"ohos.appexecfwk.mock.AppMgr"); diff --git a/services/security_component_service/sa/test/mock/src/accesstoken_kit.cpp b/services/security_component_service/sa/test/mock/src/accesstoken_kit.cpp new file mode 100644 index 0000000..e88b5a0 --- /dev/null +++ b/services/security_component_service/sa/test/mock/src/accesstoken_kit.cpp @@ -0,0 +1,24 @@ +/* +* 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 "accesstoken_kit.h" + +namespace OHOS { +namespace Security { +namespace AccessToken { +int32_t AccessTokenKit::getHapTokenInfoRes = 0; +} // namespace SECURITY_COMPONENT_INTERFACES_INNER_KITS_ACCESSTOKEN_KIT_H +} // namespace Security +} // namespace OHOS 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 new file mode 100644 index 0000000..143447c --- /dev/null +++ b/services/security_component_service/sa/test/unittest/src/first_use_dialog_test.cpp @@ -0,0 +1,372 @@ +/* + * 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 "first_use_dialog_test.h" + +#include "accesstoken_kit.h" +#include "sec_comp_log.h" +#include "sec_comp_err.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, "FirstUseDialogTest"}; +static const std::string SEC_COMP_SRV_CFG_PATH = "/data/service/el1/public/security_component_service"; +static const std::string SEC_COMP_SRV_CFG_FILE = SEC_COMP_SRV_CFG_PATH + "/" + "first_use_record.json"; +static const std::string SEC_COMP_SRV_CFG_BACK_FILE = SEC_COMP_SRV_CFG_PATH + "/" + "first_use_record.json.bak"; +constexpr uint64_t LOCATION_BUTTON_FIRST_USE = 1 << 0; +constexpr uint64_t SAVE_BUTTON_FIRST_USE = 1 << 1; +} + +void FirstUseDialogTest::SetUpTestCase() +{ +} + +void FirstUseDialogTest::TearDownTestCase() +{ +} + +void FirstUseDialogTest::SetUp() +{ + SC_LOG_INFO(LABEL, "setup"); + struct stat fstat = {}; + if (stat(SEC_COMP_SRV_CFG_FILE.c_str(), &fstat) != 0) { + return; + } + std::string cmdline = "mv " + SEC_COMP_SRV_CFG_FILE + " " + SEC_COMP_SRV_CFG_BACK_FILE; + system(cmdline.c_str()); +} + +void FirstUseDialogTest::TearDown() +{ + struct stat fstat = {}; + if (stat(SEC_COMP_SRV_CFG_BACK_FILE.c_str(), &fstat) != 0) { + return; + } + std::string cmdline = "mv " + SEC_COMP_SRV_CFG_BACK_FILE + " " + SEC_COMP_SRV_CFG_FILE; + system(cmdline.c_str()); +} + +/** + * @tc.name: LoadFirstUseRecord001 + * @tc.desc: Test cfg file not exist + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(FirstUseDialogTest, LoadFirstUseRecord001, TestSize.Level1) +{ + FirstUseDialog diag; + diag.LoadFirstUseRecord(); + ASSERT_EQ(0, static_cast(diag.firstUseMap_.size())); +} + +/** + * @tc.name: LoadFirstUseRecord002 + * @tc.desc: Test cfg file too large + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(FirstUseDialogTest, LoadFirstUseRecord002, TestSize.Level1) +{ + std::string cmdline = "dd if=/dev/random of=" + SEC_COMP_SRV_CFG_FILE + " bs=101k count=1"; + system(cmdline.c_str()); + FirstUseDialog diag; + diag.LoadFirstUseRecord(); + ASSERT_EQ(0, static_cast(diag.firstUseMap_.size())); +} + +/** + * @tc.name: LoadFirstUseRecord003 + * @tc.desc: Test cfg file is not json + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(FirstUseDialogTest, LoadFirstUseRecord003, TestSize.Level1) +{ + std::string cmdline = "echo {x=\\\' > " + SEC_COMP_SRV_CFG_FILE; + system(cmdline.c_str()); + FirstUseDialog diag; + diag.LoadFirstUseRecord(); + ASSERT_EQ(0, static_cast(diag.firstUseMap_.size())); +} + +/** + * @tc.name: LoadFirstUseRecord004 + * @tc.desc: Test cfg file has no tag named FIRST_USE_RECORD_TAG + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(FirstUseDialogTest, LoadFirstUseRecord004, TestSize.Level1) +{ + std::string cmdline = "echo {\\\"x\\\":1} > " + SEC_COMP_SRV_CFG_FILE; + system(cmdline.c_str()); + FirstUseDialog diag; + diag.LoadFirstUseRecord(); + ASSERT_EQ(0, static_cast(diag.firstUseMap_.size())); +} + +/** + * @tc.name: LoadFirstUseRecord005 + * @tc.desc: Test cfg file has no tag named TOKEN_ID_TAG + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(FirstUseDialogTest, LoadFirstUseRecord005, TestSize.Level1) +{ + std::string cmdline = "echo {\\\"FirstUseRecord\\\":[{\\\"CompType\\\":1}]} > " + + SEC_COMP_SRV_CFG_FILE; + system(cmdline.c_str()); + FirstUseDialog diag; + diag.LoadFirstUseRecord(); + ASSERT_EQ(0, static_cast(diag.firstUseMap_.size())); +} + +/** + * @tc.name: LoadFirstUseRecord006 + * @tc.desc: Test cfg file TOKEN_ID_TAG is not number + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(FirstUseDialogTest, LoadFirstUseRecord006, TestSize.Level1) +{ + std::string cmdline = "echo {\\\"FirstUseRecord\\\":[{\\\"CompType\\\":1,\\\"TokenId\\\":\\\"kk\\\"}]} > " + + SEC_COMP_SRV_CFG_FILE; + system(cmdline.c_str()); + FirstUseDialog diag; + diag.LoadFirstUseRecord(); + ASSERT_EQ(0, static_cast(diag.firstUseMap_.size())); +} + +/** + * @tc.name: LoadFirstUseRecord007 + * @tc.desc: Test cfg file TOKEN_ID_TAG value is 0 + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(FirstUseDialogTest, LoadFirstUseRecord007, TestSize.Level1) +{ + std::string cmdline = "echo {\\\"FirstUseRecord\\\":[{\\\"CompType\\\":1,\\\"TokenId\\\":0}]} > " + + SEC_COMP_SRV_CFG_FILE; + system(cmdline.c_str()); + FirstUseDialog diag; + diag.LoadFirstUseRecord(); + ASSERT_EQ(0, static_cast(diag.firstUseMap_.size())); +} + +/** + * @tc.name: LoadFirstUseRecord008 + * @tc.desc: Test cfg file COMP_TYPE_TAG is not exist + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(FirstUseDialogTest, LoadFirstUseRecord008, TestSize.Level1) +{ + std::string cmdline = "echo {\\\"FirstUseRecord\\\":[{\\\"TokenId\\\":1}]} > " + + SEC_COMP_SRV_CFG_FILE; + system(cmdline.c_str()); + FirstUseDialog diag; + diag.LoadFirstUseRecord(); + ASSERT_EQ(0, static_cast(diag.firstUseMap_.size())); +} + +/** + * @tc.name: LoadFirstUseRecord009 + * @tc.desc: Test cfg file COMP_TYPE_TAG is number + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(FirstUseDialogTest, LoadFirstUseRecord009, TestSize.Level1) +{ + std::string cmdline = "echo {\\\"FirstUseRecord\\\":[{\\\"CompType\\\":\\\"k\\\",\\\"TokenId\\\":1}]} > " + + SEC_COMP_SRV_CFG_FILE; + system(cmdline.c_str()); + FirstUseDialog diag; + diag.LoadFirstUseRecord(); + ASSERT_EQ(0, static_cast(diag.firstUseMap_.size())); +} + +/** + * @tc.name: LoadFirstUseRecord010 + * @tc.desc: Test cfg file is success + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(FirstUseDialogTest, LoadFirstUseRecord010, TestSize.Level1) +{ + std::string cmdline = "echo {\\\"FirstUseRecord\\\":[{\\\"CompType\\\":1,\\\"TokenId\\\":1}]} > " + + SEC_COMP_SRV_CFG_FILE; + system(cmdline.c_str()); + FirstUseDialog diag; + diag.LoadFirstUseRecord(); + ASSERT_EQ(1, static_cast(diag.firstUseMap_.size())); + ASSERT_EQ(1, static_cast(diag.firstUseMap_[1])); +} + +/** + * @tc.name: SaveFirstUseRecord001 + * @tc.desc: Test cfg dir is not exist + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(FirstUseDialogTest, SaveFirstUseRecord001, TestSize.Level1) +{ + std::string cmdline = "rm " + SEC_COMP_SRV_CFG_PATH + " -rf"; + system(cmdline.c_str()); + FirstUseDialog diag; + diag.firstUseMap_[1] = 1; + diag.SaveFirstUseRecord(); + diag.firstUseMap_.clear(); + diag.LoadFirstUseRecord(); + EXPECT_EQ(0, static_cast(diag.firstUseMap_.size())); + std::string cmdline1 = "mkdir " + SEC_COMP_SRV_CFG_PATH + " 0750 security_component security_component"; + system(cmdline1.c_str()); +} + +/** + * @tc.name: SaveFirstUseRecord002 + * @tc.desc: Test cfg dir is not dir + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(FirstUseDialogTest, SaveFirstUseRecord002, TestSize.Level1) +{ + std::string cmdline = "rm " + SEC_COMP_SRV_CFG_PATH + " -rf && touch " + SEC_COMP_SRV_CFG_PATH; + system(cmdline.c_str()); + FirstUseDialog diag; + diag.firstUseMap_[1] = 1; + diag.SaveFirstUseRecord(); + diag.firstUseMap_.clear(); + diag.LoadFirstUseRecord(); + EXPECT_EQ(0, static_cast(diag.firstUseMap_.size())); + std::string cmdline1 = "rm " + SEC_COMP_SRV_CFG_PATH + " && mkdir " + + SEC_COMP_SRV_CFG_PATH + " 0750 security_component security_component"; + system(cmdline1.c_str()); +} + +/** + * @tc.name: SaveFirstUseRecord003 + * @tc.desc: Test cfg file is not exist, tokenid is invalid. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(FirstUseDialogTest, SaveFirstUseRecord003, TestSize.Level1) +{ + FirstUseDialog diag; + OHOS::Security::AccessToken::AccessTokenKit::getHapTokenInfoRes = -1; + diag.firstUseMap_[1] = 1; + diag.SaveFirstUseRecord(); + diag.firstUseMap_.clear(); + diag.LoadFirstUseRecord(); + EXPECT_EQ(0, static_cast(diag.firstUseMap_.size())); + OHOS::Security::AccessToken::AccessTokenKit::getHapTokenInfoRes = 0; +} + +/** + * @tc.name: SaveFirstUseRecord004 + * @tc.desc: Test cfg file is exist, save ok + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(FirstUseDialogTest, SaveFirstUseRecord004, TestSize.Level1) +{ + FirstUseDialog diag; + diag.firstUseMap_[1] = 1; + diag.SaveFirstUseRecord(); + diag.firstUseMap_.clear(); + diag.LoadFirstUseRecord(); + EXPECT_EQ(1, static_cast(diag.firstUseMap_.size())); + EXPECT_EQ(1, static_cast(diag.firstUseMap_[1])); +} + +class TestRemoteObject : public IRemoteObject { +public: + explicit TestRemoteObject(std::u16string descriptor) : IRemoteObject(descriptor) + { + }; + + ~TestRemoteObject() = default; + + bool IsProxyObject() const override + { + return false; + }; + + int32_t GetObjectRefCount() override + { + return 0; + }; + + int Dump(int fd, const std::vector& args) override + { + return 0; + }; + + int SendRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) override + { + return -1; + }; + + bool AddDeathRecipient(const sptr& recipient) override + { + return false; + }; + + bool RemoveDeathRecipient(const sptr& recipient) override + { + return false; + }; +}; + +/* + * @tc.name: NotifyFirstUseDialog001 + * @tc.desc: Test NotifyFirstUseDialog + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(FirstUseDialogTest, NotifyFirstUseDialog001, TestSize.Level1) +{ + FirstUseDialog diag; + diag.secHandler_ = nullptr; + // no handler + ASSERT_FALSE(diag.NotifyFirstUseDialog(0, LOCATION_COMPONENT, nullptr)); + + // no calltoken + std::shared_ptr runner = AppExecFwk::EventRunner::Create(true); + ASSERT_NE(nullptr, runner); + std::shared_ptr handler = std::make_shared(runner); + diag.secHandler_ = handler; + ASSERT_FALSE(diag.NotifyFirstUseDialog(0, LOCATION_COMPONENT, nullptr)); + + // type invalid + sptr testRemoteObject = new TestRemoteObject(std::u16string()); + ASSERT_FALSE(diag.NotifyFirstUseDialog(0, PASTE_COMPONENT, testRemoteObject)); + + // first use location button + ASSERT_TRUE(diag.NotifyFirstUseDialog(0, LOCATION_COMPONENT, testRemoteObject)); + ASSERT_EQ(LOCATION_BUTTON_FIRST_USE, static_cast(diag.firstUseMap_[0])); + + // first use save button + ASSERT_TRUE(diag.NotifyFirstUseDialog(0, SAVE_COMPONENT, testRemoteObject)); + ASSERT_EQ(LOCATION_BUTTON_FIRST_USE | SAVE_BUTTON_FIRST_USE, static_cast(diag.firstUseMap_[0])); + + // second use save button + ASSERT_TRUE(diag.NotifyFirstUseDialog(0, SAVE_COMPONENT, testRemoteObject)); + ASSERT_EQ(LOCATION_BUTTON_FIRST_USE | SAVE_BUTTON_FIRST_USE, static_cast(diag.firstUseMap_[0])); + + diag.StartDialogAbility(PASTE_COMPONENT, nullptr); +} diff --git a/services/security_component_service/sa/test/unittest/src/first_use_dialog_test.h b/services/security_component_service/sa/test/unittest/src/first_use_dialog_test.h new file mode 100644 index 0000000..5161aad --- /dev/null +++ b/services/security_component_service/sa/test/unittest/src/first_use_dialog_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_FIRST_USE_DIALOG_TEST_H +#define SEC_COMP_FIRST_USE_DIALOG_TEST_H + +#include +#define private public +#include "first_use_dialog.h" +#undef private + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +class FirstUseDialogTest : public testing::Test { +public: + static void SetUpTestCase(); + + static void TearDownTestCase(); + + void SetUp(); + + void TearDown(); +}; +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif // SEC_COMP_FIRST_USE_DIALOG_TEST_H diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.h b/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.h index e56866c..c7ea755 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.h +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.h @@ -24,7 +24,6 @@ namespace OHOS { namespace Security { namespace SecurityComponent { - // stub is abstract class struct SecCompStubMock : public SecCompStub { public: diff --git a/test/fuzztest/security_component/reduceafterverifysavepermissionInner_fuzzer/reduceafterverifysavepermissionInner_fuzzer.cpp b/test/fuzztest/security_component/reduceafterverifysavepermissionInner_fuzzer/reduceafterverifysavepermissionInner_fuzzer.cpp index 785c42c..4886ddf 100644 --- a/test/fuzztest/security_component/reduceafterverifysavepermissionInner_fuzzer/reduceafterverifysavepermissionInner_fuzzer.cpp +++ b/test/fuzztest/security_component/reduceafterverifysavepermissionInner_fuzzer/reduceafterverifysavepermissionInner_fuzzer.cpp @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "reduceafterverifysavepermissionInner_fuzzer.h" #include #include @@ -20,7 +21,6 @@ #include "accesstoken_kit.h" #include "securec.h" #include "token_setproc.h" -#include "reduceafterverifysavepermissionInner_fuzzer.h" using namespace OHOS::Security::SecurityComponent; using namespace OHOS::Security::AccessToken; diff --git a/test/fuzztest/security_component/registersecuritycomponent_fuzzer/registersecuritycomponent_fuzzer.cpp b/test/fuzztest/security_component/registersecuritycomponent_fuzzer/registersecuritycomponent_fuzzer.cpp index ea9d275..e6b86cf 100644 --- a/test/fuzztest/security_component/registersecuritycomponent_fuzzer/registersecuritycomponent_fuzzer.cpp +++ b/test/fuzztest/security_component/registersecuritycomponent_fuzzer/registersecuritycomponent_fuzzer.cpp @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "registersecuritycomponent_fuzzer.h" #include #include @@ -20,7 +21,6 @@ #include "accesstoken_kit.h" #include "securec.h" #include "token_setproc.h" -#include "registersecuritycomponent_fuzzer.h" using namespace OHOS::Security::SecurityComponent; using namespace OHOS::Security::AccessToken; -- Gitee