From b2551690887fc48a2e6dec4e659401dea0de8900 Mon Sep 17 00:00:00 2001 From: baozeyu Date: Fri, 28 Mar 2025 11:25:57 +0800 Subject: [PATCH] publish_process test add Signed-off-by: baozeyu --- services/ans/test/unittest/BUILD.gn | 1 + .../unittest/mock/mock_accesstoken_kit.cpp | 13 +- .../test/unittest/publish_process_test.cpp | 261 ++++++++++++++++++ 3 files changed, 274 insertions(+), 1 deletion(-) create mode 100644 services/ans/test/unittest/publish_process_test.cpp diff --git a/services/ans/test/unittest/BUILD.gn b/services/ans/test/unittest/BUILD.gn index 19aad802a..e50a81c04 100644 --- a/services/ans/test/unittest/BUILD.gn +++ b/services/ans/test/unittest/BUILD.gn @@ -423,6 +423,7 @@ ohos_unittest("notification_publish_service_test") { "mock/mock_push_callback_stub.cpp", "mock/mock_single_kv_store.cpp", "notification_dialog_test/mock_os_account_manager_annex.cpp", + "publish_process_test.cpp", ] deps = [ diff --git a/services/ans/test/unittest/mock/mock_accesstoken_kit.cpp b/services/ans/test/unittest/mock/mock_accesstoken_kit.cpp index de367afe8..f03eb1a94 100644 --- a/services/ans/test/unittest/mock/mock_accesstoken_kit.cpp +++ b/services/ans/test/unittest/mock/mock_accesstoken_kit.cpp @@ -13,7 +13,7 @@ * limitations under the License. */ -#include "mock_accesstoken_kit.h" +#include "accesstoken_kit.h" #include "ans_ut_constant.h" #include "ipc_skeleton.h" @@ -25,6 +25,7 @@ ATokenTypeEnum g_mockGetTokenTypeFlagRet = ATokenTypeEnum::TOKEN_INVALID; DlpType g_mockDlpType = DlpType::DLP_COMMON; ATokenAplEnum g_mockApl = ATokenAplEnum::APL_NORMAL; bool g_mockVerfyPermisson = true; +bool g_isSystemApp = false; } void MockGetTokenTypeFlag(ATokenTypeEnum mockRet) @@ -44,6 +45,11 @@ void MockIsVerfyPermisson(bool isVerify) { g_mockVerfyPermisson = isVerify; } + +void MockIsSystemAppByFullTokenID(bool isSystemApp) +{ + g_isSystemApp = isSystemApp; +} } } namespace OHOS { @@ -71,6 +77,11 @@ int AccessTokenKit::GetHapTokenInfo(AccessTokenID tokenID, HapTokenInfo& info) info.dlpType = Notification::g_mockDlpType; return 0; } + +bool AccessTokenKit::IsSystemAppByFullTokenID(uint64_t tokenId) +{ + return Notification::g_isSystemApp; +} } // namespace AccessToken } // namespace Security } // namespace OHOS diff --git a/services/ans/test/unittest/publish_process_test.cpp b/services/ans/test/unittest/publish_process_test.cpp new file mode 100644 index 000000000..8f1020a4b --- /dev/null +++ b/services/ans/test/unittest/publish_process_test.cpp @@ -0,0 +1,261 @@ +/* + * 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 "gtest/gtest.h" +#define private public +#define protected public +#include "common_notification_publish_process.h" +#include "ans_inner_errors.h" +#include "live_publish_process.h" +#include "accesstoken_kit.h" +#include "notification_content.h" +#include "notification_constant.h" +#undef private +#undef protected + +using namespace testing::ext; +using namespace OHOS::Security::AccessToken; +namespace OHOS { +namespace Notification { + +extern void MockIsVerfyPermisson(bool isVerify); +extern void MockGetTokenTypeFlag(ATokenTypeEnum mockRet); +extern void MockIsSystemAppByFullTokenID(bool isSystemApp); +extern void MockDlpType(DlpType mockRet); +extern void MockIsSystemApp(bool isSystemApp); + +class PublishProcessTest : public testing::Test { +public: + PublishProcessTest() + {} + ~PublishProcessTest() + {} + static void SetUpTestCas(void) {}; + static void TearDownTestCase(void) {}; + void SetUp() {}; + void TearDown() {}; +}; + +/** + * @tc.name: Test CommonPublishCheck + * @tc.desc: Test CommonPublishCheck + * @tc.type: FUNC + */ +HWTEST_F(PublishProcessTest, BaseCommonPublishCheck_00001, Function | SmallTest | Level1) +{ + MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE); + MockIsSystemAppByFullTokenID(false); + sptr request(new NotificationRequest(1)); + request->SetReceiverUserId(100); + + CommonNotificationPublishProcess process; + auto res = process.CommonPublishCheck(request); + ASSERT_EQ(res, ERR_ANS_NON_SYSTEM_APP); +} + +/** + * @tc.name: Test CommonPublishCheck + * @tc.desc: Test CommonPublishCheck + * @tc.type: FUNC + */ +HWTEST_F(PublishProcessTest, BaseCommonPublishCheck_00002, Function | SmallTest | Level1) +{ + MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP); + MockIsSystemAppByFullTokenID(true); + MockIsVerfyPermisson(false); + sptr request(new NotificationRequest(1)); + request->SetReceiverUserId(100); + + CommonNotificationPublishProcess process; + auto res = process.CommonPublishCheck(request); + ASSERT_EQ(res, ERR_ANS_PERMISSION_DENIED); +} + +/** + * @tc.name: Test CommonPublishProcess + * @tc.desc: Test CommonPublishProcess + * @tc.type: FUNC + */ +HWTEST_F(PublishProcessTest, BaseCommonPublishProcess_00001, Function | SmallTest | Level1) +{ + MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP); + MockDlpType(DlpType::DLP_READ); + sptr request(new NotificationRequest(1)); + + CommonNotificationPublishProcess process; + auto res = process.CommonPublishProcess(request); + ASSERT_EQ(res, ERR_ANS_DLP_HAP); +} + +/** + * @tc.name: Test PublishNotificationByApp + * @tc.desc: Test PublishNotificationByApp + * @tc.type: FUNC + */ +HWTEST_F(PublishProcessTest, PublishNotificationByApp_00001, Function | SmallTest | Level1) +{ + MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE); + MockIsSystemAppByFullTokenID(false); + sptr request(new NotificationRequest(1)); + request->SetReceiverUserId(100); + + CommonNotificationPublishProcess progress; + auto res = progress.PublishNotificationByApp(request); + ASSERT_EQ(res, ERR_ANS_NON_SYSTEM_APP); +} + +/** + * @tc.name: Test PublishNotificationByApp + * @tc.desc: Test PublishNotificationByApp + * @tc.type: FUNC + */ +HWTEST_F(PublishProcessTest, PublishNotificationByApp_00002, Function | SmallTest | Level1) +{ + MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE); + sptr request(new NotificationRequest(1)); + request->SetInProgress(true); + + CommonNotificationPublishProcess progress; + auto res = progress.PublishNotificationByApp(request); + ASSERT_EQ(request->IsInProgress(), false); +} + +/** + * @tc.name: Test PublishNotificationByApp + * @tc.desc: Test PublishNotificationByApp + * @tc.type: FUNC + */ +HWTEST_F(PublishProcessTest, PublishNotificationByApp_00003, Function | SmallTest | Level1) +{ + MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP); + MockDlpType(DlpType::DLP_READ); + sptr request(new NotificationRequest(1)); + + CommonNotificationPublishProcess progress; + auto res = progress.PublishNotificationByApp(request); + ASSERT_EQ(res, ERR_ANS_DLP_HAP); +} + +/** + * @tc.name: Test LivePublishPreWork + * @tc.desc: Test LivePublishPreWork + * @tc.type: FUNC + */ +HWTEST_F(PublishProcessTest, LivePublishPreWork_00001, Function | SmallTest | Level1) +{ + sptr request(new NotificationRequest(1)); + request->SetRemoveAllowed(false); + + LivePublishProcess progress; + auto res = progress.PublishPreWork(request, true); + ASSERT_EQ(request->IsRemoveAllowed(), true); + ASSERT_EQ(res, ERR_OK); +} + +/** + * @tc.name: Test LivePublishPreWork + * @tc.desc: Test LivePublishPreWork + * @tc.type: FUNC + */ +HWTEST_F(PublishProcessTest, LivePublishPreWork_00002, Function | SmallTest | Level1) +{ + MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP); + MockIsSystemApp(false); + sptr request(new NotificationRequest(1)); + + LivePublishProcess progress; + auto res = progress.PublishPreWork(request, true); + ASSERT_EQ(res, ERR_ANS_INVALID_PARAM); +} + +/** + * @tc.name: Test LivePublishNotificationByApp_00001 + * @tc.desc: Test LivePublishNotificationByApp_00001 + * @tc.type: FUNC + */ +HWTEST_F(PublishProcessTest, LivePublishNotificationByApp_00001, Function | SmallTest | Level1) +{ + MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP); + MockIsSystemApp(false); + sptr request(new NotificationRequest(1)); + std::shared_ptr liveViewContent = + std::make_shared(); + std::shared_ptr content = + std::make_shared(liveViewContent); + request->SetContent(content); + request->SetReceiverUserId(100); + + LivePublishProcess progress; + auto res = progress.PublishPreWork(request, false); + ASSERT_EQ(res, ERR_ANS_NON_SYSTEM_APP); +} + +/** + * @tc.name: Test LivePublishNotificationByApp_00002 + * @tc.desc: Test LivePublishNotificationByApp_00002 + * @tc.type: FUNC + */ +HWTEST_F(PublishProcessTest, LivePublishNotificationByApp_00002, Function | SmallTest | Level1) +{ + MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE); + sptr request(new NotificationRequest(1)); + request->SetInProgress(true); + + LivePublishProcess progress; + auto res = progress.PublishNotificationByApp(request); + ASSERT_EQ(request->IsInProgress(), false); +} + +/** + * @tc.name: Test LivePublishNotificationByApp_00002 + * @tc.desc: Test LivePublishNotificationByApp_00002 + * @tc.type: FUNC + */ +HWTEST_F(PublishProcessTest, LivePublishNotificationByApp_00003, Function | SmallTest | Level1) +{ + MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP); + MockDlpType(DlpType::DLP_READ); + sptr request(new NotificationRequest(1)); + request->SetInProgress(true); + + LivePublishProcess progress; + auto res = progress.PublishNotificationByApp(request); + ASSERT_EQ(res, ERR_ANS_DLP_HAP); +} + + +/** + * @tc.name: Test LiveCheckLocalLiveViewSubscribed_00001 + * @tc.desc: Test LiveCheckLocalLiveViewSubscribed_00001 + * @tc.type: FUNC + */ +HWTEST_F(PublishProcessTest, LiveCheckLocalLiveViewSubscribed_00001, Function | SmallTest | Level1) +{ + sptr request(new NotificationRequest(1)); + request->SetSlotType(NotificationConstant::SlotType::LIVE_VIEW); + + std::shared_ptr liveViewContent = + std::make_shared(); + liveViewContent->SetIsOnlyLocalUpdate(true); + std::shared_ptr content = + std::make_shared(liveViewContent); + request->SetContent(content); + + LivePublishProcess progress; + auto res = progress.CheckLocalLiveViewSubscribed(request, true, 100); + ASSERT_FALSE(res); +} +} //namespace Notification +} //namespace OHOS \ No newline at end of file -- Gitee