diff --git a/services/ans/test/unittest/BUILD.gn b/services/ans/test/unittest/BUILD.gn index d017bfdbb49fd65e9a74b0418c919d8ff826d795..0646309d23db1e5b3d0607aef668ff3164f55ad3 100644 --- a/services/ans/test/unittest/BUILD.gn +++ b/services/ans/test/unittest/BUILD.gn @@ -50,6 +50,7 @@ ohos_unittest("ans_unit_test") { "mock/blob.cpp", "mock/distributed_kv_data_manager.cpp", "mock/mock_access_token_helper.cpp", + "mock/mock_accesstoken_kit.cpp", "mock/mock_bundle_manager_helper.cpp", "mock/mock_event_handler.cpp", "mock/mock_ipc.cpp", @@ -61,6 +62,7 @@ ohos_unittest("ans_unit_test") { "notification_subscriber_manager_test.cpp", "permission_filter_test.cpp", "reminder_data_manager_test.cpp", + "system_event_observer_test.cpp", ] configs = [ "//commonlibrary/c_utils/base:utils_config" ] @@ -124,4 +126,6 @@ group("unittest") { deps = [] deps += [ ":ans_unit_test" ] + + deps += [ "access_token_helper_test:unittest" ] } diff --git a/services/ans/test/unittest/access_token_helper_test/BUILD.gn b/services/ans/test/unittest/access_token_helper_test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..b4c64162190a1982b05fd993da1995ece789bd03 --- /dev/null +++ b/services/ans/test/unittest/access_token_helper_test/BUILD.gn @@ -0,0 +1,71 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//base/notification/distributed_notification_service/notification.gni") +import("//build/ohos.gni") +import("//build/test.gni") + +module_output_path = "${component_name}/unittest" + +ohos_unittest("access_token_helper_test") { + module_out_path = module_output_path + include_dirs = [ + "${core_path}/include", + "//commonlibrary/c_utils/base/include", + "/${services_path}/ans/include", + ] + + sources = [ + "${services_path}/ans/src/access_token_helper.cpp", + "${services_path}/ans/test/unittest/mock/mock_accesstoken_kit.cpp", + "access_token_helper_test.cpp", + ] + + deps = [ "${core_path}:ans_core" ] + + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "ability_runtime:abilitykit_native", + "ability_runtime:app_manager", + "ability_runtime:wantagent_innerkits", + "access_token:libaccesstoken_sdk", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "common_event_service:cesfwk_innerkits", + "eventhandler:libeventhandler", + "hitrace_native:hitrace_meter", + "hitrace_native:libhitracechain", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "kv_store:distributeddata_inner", + "multimedia_image_framework:image_native", + "multimedia_player_framework:media_client", + "os_account:os_account_innerkits", + "relational_store:native_rdb", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + "time_service:time_client", + ] + + subsystem_name = "${subsystem_name}" + part_name = "${component_name}" +} + +group("unittest") { + testonly = true + deps = [] + + deps += [ ":access_token_helper_test" ] +} diff --git a/services/ans/test/unittest/access_token_helper_test/access_token_helper_test.cpp b/services/ans/test/unittest/access_token_helper_test/access_token_helper_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..69a0fca56ee0fceac1bad0712177aee4ddafb5c5 --- /dev/null +++ b/services/ans/test/unittest/access_token_helper_test/access_token_helper_test.cpp @@ -0,0 +1,147 @@ +/* + * Copyright (c) 2021 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 +#include "accesstoken_kit.h" +#define private public +#define protected public +#include "access_token_helper.h" +#include "ans_log_wrapper.h" +#include "ipc_skeleton.h" +#undef private +#undef protected +using namespace testing::ext; +using namespace OHOS::Security::AccessToken; + +namespace OHOS { +namespace Notification { + +extern void MockGetTokenTypeFlag(ATokenTypeEnum mockRet); +extern void MockDlpType(DlpType mockRet); +extern void MockApl(ATokenAplEnum mockRet); +class AccessTokenHelperTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp() override; + void TearDown() override; + + std::shared_ptr stub_; +}; + +void AccessTokenHelperTest::SetUpTestCase() +{ +} + +void AccessTokenHelperTest::TearDownTestCase() +{ +} + +void AccessTokenHelperTest::SetUp() +{ + stub_ = std::make_shared(); +} + +void AccessTokenHelperTest::TearDown() +{ +} + +/** + * @tc.number : AccessTokenHelperTest + * @tc.name : VerifyCallerPermission_00100 + * @tc.desc : VerifyCallerPermission success + */ +HWTEST_F(AccessTokenHelperTest, VerifyCallerPermission_00100, Function | SmallTest | Level1) +{ + AccessTokenID tokenID = 0; + string permission; + EXPECT_TRUE(stub_->VerifyCallerPermission(tokenID, permission)); +} + +/** + * @tc.number : AccessTokenHelperTest + * @tc.name : VerifyNativeToken_00100 + * @tc.desc : VerifyNativeToken success + */ +HWTEST_F(AccessTokenHelperTest, VerifyNativeToken_00100, Function | SmallTest | Level1) +{ + AccessTokenID tokenID = 0; + MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE); + EXPECT_TRUE(stub_->VerifyNativeToken(tokenID)); +} + +/** + * @tc.number : AccessTokenHelperTest + * @tc.name : IsSystemHap_00100 + * @tc.desc : IsSystemHap Token Type TOKEN_NATIVE + */ +HWTEST_F(AccessTokenHelperTest, IsSystemHap_00100, Function | SmallTest | Level1) +{ + MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE); + EXPECT_TRUE(stub_->IsSystemHap()); +} + +/** + * @tc.number : AccessTokenHelperTest + * @tc.name : IsSystemHap_00200 + * @tc.desc : IsSystemHap Token Type TOKEN_HAP + */ +HWTEST_F(AccessTokenHelperTest, IsSystemHap_00200, Function | SmallTest | Level1) +{ + MockApl(ATokenAplEnum::APL_SYSTEM_CORE); + MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP); + EXPECT_TRUE(stub_->IsSystemHap()); +} + +/** + * @tc.number : AccessTokenHelperTest + * @tc.name : IsSystemHap_00300 + * @tc.desc : IsSystemHap Token Type TOKEN_HAP + */ +HWTEST_F(AccessTokenHelperTest, IsSystemHap_00300, Function | SmallTest | Level1) +{ + MockApl(ATokenAplEnum::APL_NORMAL); + MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP); + EXPECT_FALSE(stub_->IsSystemHap()); +} + +/** + * @tc.number : AccessTokenHelperTest + * @tc.name : IsDlpHap_00100 + * @tc.desc : IsDlpHap Token Type TOKEN_HAP + */ +HWTEST_F(AccessTokenHelperTest, IsDlpHap_00100, Function | SmallTest | Level1) +{ + AccessTokenID tokenID = 0; + MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP); + MockDlpType(DlpType::DLP_READ); + EXPECT_TRUE(stub_->IsDlpHap(tokenID)); +} + +/** + * @tc.number : AccessTokenHelperTest + * @tc.name : IsDlpHap_00200 + * @tc.desc : IsDlpHap Token Type TOKEN_NATIVE + */ +HWTEST_F(AccessTokenHelperTest, IsDlpHap_00200, Function | SmallTest | Level1) +{ + AccessTokenID tokenID = 0; + MockDlpType(DlpType::DLP_COMMON); + MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE); + EXPECT_FALSE(stub_->IsDlpHap(tokenID)); +} +} // namespace Notification +} // namespace OHOS diff --git a/services/ans/test/unittest/bundle_manager_helper_test.cpp b/services/ans/test/unittest/bundle_manager_helper_test.cpp index 0e29e8528450a406075fa76bd90bb56c3d4098af..60bac7438bb4ce0e8e11ed1936b5bc52b61cdd5e 100644 --- a/services/ans/test/unittest/bundle_manager_helper_test.cpp +++ b/services/ans/test/unittest/bundle_manager_helper_test.cpp @@ -109,16 +109,18 @@ HWTEST_F(BundleManagerHelperTest, BundleManagerHelperTest_00500, Level1) } /** - * @tc.number : AccessTokenHelperTest_00100 - * @tc.name : IsSystemHap - * @tc.desc : Test IsSystemHap function when the parameeter are normal + * @tc.number : GetDistributedNotificationEnabled_00100 + * @tc.name : GetDistributedNotificationEnabled + * @tc.desc : Test GetDistributedNotificationEnabled function when the parameeter are normal * @tc.require : issueI5S4VP */ -HWTEST_F(BundleManagerHelperTest, AccessTokenHelperTest_00100, Level1) +HWTEST_F(BundleManagerHelperTest, GetDistributedNotificationEnabled_00100, Level1) { - AccessTokenHelper accessTokenHelper; - bool result = accessTokenHelper.IsSystemHap(); - EXPECT_EQ(result, false); + std::string bundle = "Bundle"; + int32_t userId = 1; + BundleManagerHelper bundleManagerHelper; + bool result = bundleManagerHelper.GetDistributedNotificationEnabled(bundle, userId); + EXPECT_EQ(result, true); } } // namespace Notification } // namespace OHOS diff --git a/services/ans/test/unittest/mock/mock_accesstoken_kit.cpp b/services/ans/test/unittest/mock/mock_accesstoken_kit.cpp new file mode 100644 index 0000000000000000000000000000000000000000..302555481ade7996031cfbc9923a561785a4c14b --- /dev/null +++ b/services/ans/test/unittest/mock/mock_accesstoken_kit.cpp @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "accesstoken_kit.h" +#include "ans_log_wrapper.h" +#include "ipc_skeleton.h" + +using namespace OHOS::Security::AccessToken; +namespace OHOS { +namespace Notification { +namespace { +ATokenTypeEnum g_mockGetTokenTypeFlagRet = ATokenTypeEnum::TOKEN_INVALID; +DlpType g_mockDlpType = DlpType::DLP_COMMON; +ATokenAplEnum g_mockApl = ATokenAplEnum::APL_NORMAL; +} + +void MockGetTokenTypeFlag(ATokenTypeEnum mockRet) +{ + g_mockGetTokenTypeFlagRet = mockRet; +} +void MockDlpType(DlpType mockRet) +{ + g_mockDlpType = mockRet; +} +void MockApl(ATokenAplEnum mockRet) +{ + g_mockApl = mockRet; +} +} +} +namespace OHOS { +namespace Security { +namespace AccessToken { +int AccessTokenKit::VerifyAccessToken(AccessTokenID tokenID, const std::string& permissionName) +{ + return PERMISSION_GRANTED; +} + +ATokenTypeEnum AccessTokenKit::GetTokenTypeFlag(AccessTokenID tokenID) +{ + return Notification::g_mockGetTokenTypeFlagRet; +} + +int AccessTokenKit::GetHapTokenInfo(AccessTokenID tokenID, HapTokenInfo& info) +{ + info.dlpType = Notification::g_mockDlpType; + info.apl = Notification::g_mockApl; + return 0; +} +} // namespace AccessToken +} // namespace Security +} // namespace OHOS diff --git a/services/ans/test/unittest/notification_slot_filter_test.cpp b/services/ans/test/unittest/notification_slot_filter_test.cpp index 90e5730dce3032169e07761c9ab0f090b67776e8..f08f6978a91cf099ce6fcc82d342f3b9f885d76f 100644 --- a/services/ans/test/unittest/notification_slot_filter_test.cpp +++ b/services/ans/test/unittest/notification_slot_filter_test.cpp @@ -21,6 +21,7 @@ #include "notification_slot.h" #undef private #undef protected +#include "ans_inner_errors.h" #include "notification_slot_filter.h" #include "notification_subscribe_info.h" @@ -77,6 +78,20 @@ HWTEST_F(NotificationSlotFilterTest, NotificationSlotFilterTest_00200, Function * @tc.desc : Test OnPublish function */ HWTEST_F(NotificationSlotFilterTest, NotificationSlotFilterTest_00300, Function | SmallTest | Level1) +{ + NotificationSlotFilter notificationSlotFilter; + std::shared_ptr record = std::make_shared(); + ErrCode result = notificationSlotFilter.OnPublish(record); + EXPECT_EQ(result, ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_NOT_EXIST); +} + + +/** + * @tc.number : NotificationSlotFilterTest_00400 + * @tc.name : ANS_OnPublish_0200 + * @tc.desc : Test OnPublish function + */ +HWTEST_F(NotificationSlotFilterTest, NotificationSlotFilterTest_00400, Function | SmallTest | Level1) { NotificationSlotFilter notificationSlotFilter; std::shared_ptr record = std::make_shared(); diff --git a/services/ans/test/unittest/system_event_observer_test.cpp b/services/ans/test/unittest/system_event_observer_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7b0baeda394e4e58de65e9c8ac489a3740051e60 --- /dev/null +++ b/services/ans/test/unittest/system_event_observer_test.cpp @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2021 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 + +#define private public +#define protected public +#include "system_event_observer.h" +#undef private +#undef protected +#include "ans_inner_errors.h" +#include "common_event_support.h" + +using namespace testing::ext; +namespace OHOS { +namespace Notification { +class SystemEventObserverTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp() override; + void TearDown() override; + + std::shared_ptr stub_; +}; + +void SystemEventObserverTest::SetUpTestCase() +{ +} + +void SystemEventObserverTest::TearDownTestCase() +{ +} + +void SystemEventObserverTest::SetUp() +{ + ISystemEvent iSystemEvent; + stub_ = std::make_shared(iSystemEvent); +} + +void SystemEventObserverTest::TearDown() +{ +} + +/** + * @tc.number : OnReceiveEvent_001 + * @tc.name : + * @tc.desc : Test OnReceiveEvent function, return is void. + */ +HWTEST_F(SystemEventObserverTest, OnReceiveEvent_001, Function | SmallTest | Level1) +{ + EventFwk::Want want; + EventFwk::CommonEventData data; + data.SetWant(want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED)); + + stub_->OnReceiveEvent(data); +} + +/** + * @tc.number : OnReceiveEvent_002 + * @tc.name : + * @tc.desc : Test OnReceiveEvent function, return is void. + */ +HWTEST_F(SystemEventObserverTest, OnReceiveEvent_002, Function | SmallTest | Level1) +{ + EventFwk::Want want; + EventFwk::CommonEventData data; + data.SetWant(want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED)); + stub_->OnReceiveEvent(data); +} + +/** + * @tc.number : OnReceiveEvent_003 + * @tc.name : + * @tc.desc : Test OnReceiveEvent function, return is void. + */ +HWTEST_F(SystemEventObserverTest, OnReceiveEvent_003, Function | SmallTest | Level1) +{ + EventFwk::Want want; + EventFwk::CommonEventData data; + data.SetWant(want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED)); + stub_->OnReceiveEvent(data); +} + +/** + * @tc.number : OnReceiveEvent_004 + * @tc.name : + * @tc.desc : Test OnReceiveEvent function, return is void. + */ +HWTEST_F(SystemEventObserverTest, OnReceiveEvent_004, Function | SmallTest | Level1) +{ + EventFwk::Want want; + EventFwk::CommonEventData data; + data.SetWant(want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED)); + stub_->OnReceiveEvent(data); +} + +} // namespace Notification +} // namespace OHOS \ No newline at end of file