1 /*
2  * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "errors.h"
16 #define private public
17 #define protected public
18 #include "common_event_manager_service.h"
19 
20 #include "ces_inner_error_code.h"
21 #include "common_event.h"
22 #include "common_event_death_recipient.h"
23 #include "common_event_stub.h"
24 #include "common_event_proxy.h"
25 #include "common_event_data.h"
26 #include "common_event_subscriber.h"
27 #include "event_receive_proxy.h"
28 #include "common_event_publish_info.h"
29 #include "matching_skills.h"
30 
31 #include "event_receive_stub.h"
32 #include "event_log_wrapper.h"
33 #include "datetime_ex.h"
34 
35 #undef private
36 #undef protected
37 
38 #include <gtest/gtest.h>
39 
40 using namespace testing::ext;
41 using namespace OHOS::EventFwk;
42 using namespace OHOS;
43 using namespace OHOS::Notification;
44 
45 class CommonEventManagerServiceTest : public testing::Test {
46 public:
47     static void SetUpTestCase();
48     static void TearDownTestCase();
49     void SetUp();
50     void TearDown();
51 
OnReceiveEvent(const CommonEventData & data)52     virtual void OnReceiveEvent(const CommonEventData &data)
53     {}
54 };
55 
SetUpTestCase()56 void CommonEventManagerServiceTest::SetUpTestCase()
57 {}
58 
TearDownTestCase()59 void CommonEventManagerServiceTest::TearDownTestCase()
60 {}
61 
SetUp()62 void CommonEventManagerServiceTest::SetUp()
63 {}
64 
TearDown()65 void CommonEventManagerServiceTest::TearDown()
66 {}
67 
68 /**
69  * @tc.name: Init_001
70  * @tc.desc: Test CommonEventManagerService_
71  * @tc.type: FUNC
72  * @tc.require: I582Y4
73  */
HWTEST_F(CommonEventManagerServiceTest,Init_001,Level1)74 HWTEST_F(CommonEventManagerServiceTest, Init_001, Level1)
75 {
76     CommonEventManagerService commonEventManagerService;
77     int result = commonEventManagerService.Init();
78     EXPECT_EQ(ERR_OK, result);
79 }
80 
81 /**
82  * @tc.name: Init_002
83  * @tc.desc: Test CommonEventManagerService_
84  * @tc.type: FUNC
85  * @tc.require: I582Y4
86  */
HWTEST_F(CommonEventManagerServiceTest,Init_002,Level1)87 HWTEST_F(CommonEventManagerServiceTest, Init_002, Level1)
88 {
89     CommonEventManagerService commonEventManagerService;
90     int result = commonEventManagerService.Init();
91     EXPECT_NE(ERR_INVALID_OPERATION, result);
92 }
93 
94 /**
95  * @tc.name: PublishCommonEvent_001
96  * @tc.desc: Test CommonEventManagerService_
97  * @tc.type: FUNC
98  * @tc.require: I582Y4
99  */
HWTEST_F(CommonEventManagerServiceTest,PublishCommonEvent_001,Level1)100 HWTEST_F(CommonEventManagerServiceTest, PublishCommonEvent_001, Level1)
101 {
102     CommonEventManagerService commonEventManagerService;
103     CommonEventData event;
104     CommonEventPublishInfo publishInfo;
105 
106     int32_t userId = 1;
107     int32_t result = commonEventManagerService.PublishCommonEvent(event, publishInfo, nullptr, userId);
108     EXPECT_EQ(ERR_NOTIFICATION_CESM_ERROR, result);
109 }
110 
111 /**
112  * @tc.name: PublishCommonEvent_002
113  * @tc.desc: Test CommonEventManagerService_
114  * @tc.type: FUNC
115  * @tc.require: I582Y4
116  */
HWTEST_F(CommonEventManagerServiceTest,PublishCommonEvent_002,Level1)117 HWTEST_F(CommonEventManagerServiceTest, PublishCommonEvent_002, Level1)
118 {
119     CommonEventManagerService commonEventManagerService;
120     CommonEventData event;
121     CommonEventPublishInfo publishInfo;
122 
123     int32_t userId = 1;
124     uid_t uid = 1;
125     int32_t callerToken = 2;
126     bool result = commonEventManagerService.PublishCommonEvent(event, publishInfo, nullptr, uid, callerToken, userId);
127     EXPECT_EQ(false, result);
128 }
129 
130 /**
131  * @tc.name: PublishCommonEvent_003
132  * @tc.desc: Test CommonEventManagerService_
133  * @tc.type: FUNC
134  * @tc.require: I582Y4
135  */
HWTEST_F(CommonEventManagerServiceTest,PublishCommonEvent_003,Level1)136 HWTEST_F(CommonEventManagerServiceTest, PublishCommonEvent_003, Level1)
137 {
138     CommonEventManagerService commonEventManagerService;
139     CommonEventData event;
140     CommonEventPublishInfo publishInfo;
141 
142     int32_t userId = 1098;
143     int32_t result = commonEventManagerService.PublishCommonEvent(event, publishInfo, nullptr, userId);
144     EXPECT_EQ(ERR_NOTIFICATION_CESM_ERROR, result);
145 }
146 
147 /**
148  * @tc.name: SubscribeCommonEvent_001
149  * @tc.desc: Test CommonEventManagerService_
150  * @tc.type: FUNC
151  * @tc.require: I582Y4
152  */
HWTEST_F(CommonEventManagerServiceTest,SubscribeCommonEvent_001,Level1)153 HWTEST_F(CommonEventManagerServiceTest, SubscribeCommonEvent_001, Level1)
154 {
155     CommonEventManagerService commonEventManagerService;
156     CommonEventData event;
157     CommonEventSubscribeInfo subscribeInfo;
158 
159     int32_t result = commonEventManagerService.SubscribeCommonEvent(subscribeInfo, nullptr);
160     EXPECT_EQ(ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, result);
161 }
162 
163 /**
164  * @tc.name: SubscribeCommonEvent_002
165  * @tc.desc: Test CommonEventManagerService_
166  * @tc.type: FUNC
167  * @tc.require: I582Y4
168  */
HWTEST_F(CommonEventManagerServiceTest,SubscribeCommonEvent_002,Level1)169 HWTEST_F(CommonEventManagerServiceTest, SubscribeCommonEvent_002, Level1)
170 {
171     CommonEventManagerService commonEventManagerService;
172     CommonEventData event;
173     CommonEventSubscribeInfo subscribeInfo;
174 
175     struct tm recordTime = {0};
176     GetSystemCurrentTime(&recordTime);
177 
178     int32_t result = commonEventManagerService.SubscribeCommonEvent(subscribeInfo, nullptr);
179     EXPECT_EQ(ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID, result);
180 }
181 
182 /**
183  * @tc.name: SubscribeCommonEvent_003
184  * @tc.desc: Test CommonEventManagerService_
185  * @tc.type: FUNC
186  * @tc.require: I582Y4
187  */
HWTEST_F(CommonEventManagerServiceTest,SubscribeCommonEvent_003,Level1)188 HWTEST_F(CommonEventManagerServiceTest, SubscribeCommonEvent_003, Level1)
189 {
190     CommonEventManagerService commonEventManagerService;
191     commonEventManagerService.Init();
192     CommonEventData event;
193     CommonEventSubscribeInfo subscribeInfo;
194     subscribeInfo.SetUserId(1098);
195 
196     int32_t result = commonEventManagerService.SubscribeCommonEvent(subscribeInfo, nullptr);
197     EXPECT_EQ(ERR_NOTIFICATION_CES_USERID_INVALID, result);
198 }
199 
200 /**
201  * @tc.name: SubscribeCommonEvent_004
202  * @tc.desc: Test CommonEventManagerService_
203  * @tc.type: FUNC
204  * @tc.require: I582Y4
205  */
HWTEST_F(CommonEventManagerServiceTest,SubscribeCommonEvent_004,Level1)206 HWTEST_F(CommonEventManagerServiceTest, SubscribeCommonEvent_004, Level1)
207 {
208     CommonEventManagerService commonEventManagerService;
209     commonEventManagerService.Init();
210     CommonEventData event;
211     CommonEventSubscribeInfo subscribeInfo;
212 
213     struct tm recordTime = {0};
214 
215     auto callingUid = 0;
216     auto callingPid = 0;
217     auto callerToken = 0;
218     std::string bundleName = "bundleName";
219     std::shared_ptr<InnerCommonEventManager> innerCommonEventManager = std::make_shared<InnerCommonEventManager>();
220     bool ret = innerCommonEventManager->SubscribeCommonEvent(subscribeInfo,
221             nullptr,
222             recordTime,
223             callingPid,
224             callingUid,
225             callerToken,
226             bundleName);
227 
228     int32_t result = commonEventManagerService.SubscribeCommonEvent(subscribeInfo, nullptr);
229     EXPECT_EQ(false, ret);
230     EXPECT_EQ(ERR_OK, result);
231 }
232 
233 /**
234  * @tc.name: GetStickyCommonEvent_001
235  * @tc.desc: Test CommonEventManagerService_
236  * @tc.type: FUNC
237  * @tc.require: I582Y4
238  */
HWTEST_F(CommonEventManagerServiceTest,GetStickyCommonEvent_001,Level1)239 HWTEST_F(CommonEventManagerServiceTest, GetStickyCommonEvent_001, Level1)
240 {
241     const std::string event = "this is an event";
242     std::shared_ptr<InnerCommonEventManager> innerCommonEventManager_ = std::make_shared<InnerCommonEventManager>();
243 
244     CommonEventData eventData;
245     bool ret = innerCommonEventManager_->GetStickyCommonEvent(event, eventData);
246     EXPECT_EQ(false, ret);
247 }
248 
249 /**
250  * @tc.name: FinishReceiver_001
251  * @tc.desc: Test CommonEventManagerService_
252  * @tc.type: FUNC
253  * @tc.require: I582Y4
254  */
HWTEST_F(CommonEventManagerServiceTest,FinishReceiver_001,Level1)255 HWTEST_F(CommonEventManagerServiceTest, FinishReceiver_001, Level1)
256 {
257     GTEST_LOG_(INFO) << "FinishReceiver_001 start";
258 
259     const std::string event = "this is an event";
260     std::shared_ptr<InnerCommonEventManager> innerCommonEventManager_ = std::make_shared<InnerCommonEventManager>();
261     ASSERT_NE(nullptr, innerCommonEventManager_);
262     const sptr<IRemoteObject> proxy = nullptr;
263     const int32_t code = 0;
264     const std::string receiverData = "this is an receiverData";
265     const bool abortEvent = true;
266 
267     innerCommonEventManager_->FinishReceiver(proxy, code, receiverData, abortEvent);
268     GTEST_LOG_(INFO) << "FinishReceiver_001 end";
269 }
270 
271 /**
272  * @tc.name: Unfreeze_001
273  * @tc.desc: Test CommonEventManagerService_
274  * @tc.type: FUNC
275  * @tc.require: I582Y4
276  */
HWTEST_F(CommonEventManagerServiceTest,Unfreeze_001,Level1)277 HWTEST_F(CommonEventManagerServiceTest, Unfreeze_001, Level1)
278 {
279     GTEST_LOG_(INFO) << "Unfreeze_001 start";
280 
281     std::shared_ptr<InnerCommonEventManager> innerCommonEventManager = std::make_shared<InnerCommonEventManager>();
282     ASSERT_NE(nullptr, innerCommonEventManager);
283     const uid_t uid = 0;
284 
285     innerCommonEventManager->Unfreeze(uid);
286     GTEST_LOG_(INFO) << "Unfreeze_001 end";
287 }
288 
289 /**
290  * @tc.name: UnfreezeAll_001
291  * @tc.desc: Test CommonEventManagerService_
292  * @tc.type: FUNC
293  * @tc.require: I582Y4
294  */
HWTEST_F(CommonEventManagerServiceTest,UnfreezeAll_001,Level1)295 HWTEST_F(CommonEventManagerServiceTest, UnfreezeAll_001, Level1)
296 {
297     GTEST_LOG_(INFO) << "UnfreezeAll_001 start";
298 
299     std::shared_ptr<InnerCommonEventManager> innerCommonEventManager = std::make_shared<InnerCommonEventManager>();
300     ASSERT_NE(nullptr, innerCommonEventManager);
301 
302     innerCommonEventManager->UnfreezeAll();
303     GTEST_LOG_(INFO) << "UnfreezeAll_001 end";
304 }
305 
306 /**
307  * @tc.name: SetStaticSubscriberStateWithTwoParameters_0100
308  * @tc.desc: Test the SetStaticSubscriberState function, test whether it is a system application.
309  * @tc.type: FUNC
310  */
HWTEST_F(CommonEventManagerServiceTest,SetStaticSubscriberStateWithTwoParameters_0100,Level1)311 HWTEST_F(CommonEventManagerServiceTest, SetStaticSubscriberStateWithTwoParameters_0100, Level1)
312 {
313     CommonEventManagerService commonEventManagerService;
314     std::vector<std::string> events;
315     events.push_back("StaticCommonEventA");
316     int32_t result = commonEventManagerService.SetStaticSubscriberState(events, true);
317     EXPECT_EQ(result, OHOS::Notification::ERR_NOTIFICATION_CES_COMMON_NOT_SYSTEM_APP);
318 }
319