1 /*
2 * Copyright (c) 2022-2023 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
16 #include <gtest/gtest.h>
17 #include <numeric>
18 #define private public
19 #include "common_event.h"
20 #include "common_event_manager.h"
21 #include "common_event_sticky_manager.h"
22 #include "common_event_stub.h"
23 #include "common_event_subscriber_manager.h"
24 #include "inner_common_event_manager.h"
25 #include "common_event_permission_manager.h"
26 #undef private
27
28 using namespace testing::ext;
29 using namespace OHOS;
30 using namespace OHOS::EventFwk;
31 using namespace OHOS::AppExecFwk;
32 namespace OHOS {
33 namespace EventFwk {
34
35 extern void MockGetEventPermission(bool mockRet, PermissionState mockState = PermissionState::AND,
36 int32_t permissionSize = 1);
37 extern void MockIsVerfyPermisson(bool isVerify);
38 extern void MockIsSystemApp(bool mockRet);
39
40 class CommonEventSubscriberManagerTest : public testing::Test {
41 public:
CommonEventSubscriberManagerTest()42 CommonEventSubscriberManagerTest()
43 {}
~CommonEventSubscriberManagerTest()44 ~CommonEventSubscriberManagerTest()
45 {}
46
47 static void SetUpTestCase(void);
48 static void TearDownTestCase(void);
49 void SetUp();
50 void TearDown();
51 };
52
53 class DreivedSubscriber : public CommonEventSubscriber {
54 public:
DreivedSubscriber(const CommonEventSubscribeInfo & sp)55 explicit DreivedSubscriber(const CommonEventSubscribeInfo &sp) : CommonEventSubscriber(sp)
56 {}
57
~DreivedSubscriber()58 ~DreivedSubscriber()
59 {}
60
OnReceiveEvent(const CommonEventData & data)61 virtual void OnReceiveEvent(const CommonEventData &data)
62 {}
63 };
64
SetUpTestCase(void)65 void CommonEventSubscriberManagerTest::SetUpTestCase(void)
66 {}
67
TearDownTestCase(void)68 void CommonEventSubscriberManagerTest::TearDownTestCase(void)
69 {}
70
SetUp(void)71 void CommonEventSubscriberManagerTest::SetUp(void)
72 {}
73
TearDown(void)74 void CommonEventSubscriberManagerTest::TearDown(void)
75 {}
76
77 /**
78 * @tc.name: CommonEventSubscriberManager_0100
79 * @tc.desc: test RemoveSubscriber function and commonEventListener is nullptr.
80 * @tc.type: FUNC
81 */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_0100,Level1)82 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_0100, Level1)
83 {
84 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_0100 start";
85 CommonEventSubscriberManager commonEventSubscriberManager;
86 sptr<IRemoteObject> commonEventListener = nullptr;
87 EXPECT_EQ(ERR_INVALID_VALUE, commonEventSubscriberManager.RemoveSubscriber(commonEventListener));
88 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_0100 end";
89 }
90
91 /**
92 * @tc.name: CommonEventSubscriberManager_0200
93 * @tc.desc: test RemoveSubscriber function and death_ is nullptr.
94 * @tc.type: FUNC
95 */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_0200,Level1)96 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_0200, Level1)
97 {
98 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_0200 start";
99 CommonEventSubscriberManager commonEventSubscriberManager;
100 // set commonEventListener
101 MatchingSkills matchingSkills_;
102 CommonEventSubscribeInfo subscribeInfo(matchingSkills_);
103 std::shared_ptr<DreivedSubscriber> subscriber = std::make_shared<DreivedSubscriber>(subscribeInfo);
104 sptr<IRemoteObject> commonEventListener = new CommonEventListener(subscriber);
105 commonEventSubscriberManager.death_ = nullptr;
106 EXPECT_EQ(ERR_OK, commonEventSubscriberManager.RemoveSubscriber(commonEventListener));
107 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_0200 end";
108 }
109
110 /**
111 * @tc.name: CommonEventSubscriberManager_0300
112 * @tc.desc: test DumpDetailed function and record is nullptr.
113 * @tc.type: FUNC
114 */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_0300,Level1)115 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_0300, Level1)
116 {
117 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_0300 start";
118 std::shared_ptr<CommonEventSubscriberManager> commonEventSubscriberManager =
119 std::make_shared<CommonEventSubscriberManager>();
120 ASSERT_NE(nullptr, commonEventSubscriberManager);
121 std::string title = "aa";
122 SubscriberRecordPtr record = nullptr;
123 std::string format = "aa";
124 std::string dumpInfo = "aa";
125 commonEventSubscriberManager->DumpDetailed(title, record, format, dumpInfo);
126 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_0300 end";
127 }
128
129 /**
130 * @tc.name: CommonEventSubscriberManager_0400
131 * @tc.desc: test DumpDetailed function and record->eventSubscribeInfo is nullptr.
132 * @tc.type: FUNC
133 */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_0400,Level1)134 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_0400, Level1)
135 {
136 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_0400 start";
137 std::shared_ptr<CommonEventSubscriberManager> commonEventSubscriberManager =
138 std::make_shared<CommonEventSubscriberManager>();
139 ASSERT_NE(nullptr, commonEventSubscriberManager);
140 std::string title = "aa";
141 SubscriberRecordPtr record = std::make_shared<EventSubscriberRecord>();
142 record->eventSubscribeInfo = nullptr;
143 std::string format = "aa";
144 std::string dumpInfo = "aa";
145 commonEventSubscriberManager->DumpDetailed(title, record, format, dumpInfo);
146 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_0400 end";
147 }
148
149 /**
150 * @tc.name: CommonEventSubscriberManager_0500
151 * @tc.desc: test DumpDetailed function and userId is UNDEFINED_USER.
152 * @tc.type: FUNC
153 */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_0500,Level1)154 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_0500, Level1)
155 {
156 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_0500 start";
157 std::shared_ptr<CommonEventSubscriberManager> commonEventSubscriberManager =
158 std::make_shared<CommonEventSubscriberManager>();
159 ASSERT_NE(nullptr, commonEventSubscriberManager);
160 std::string title = "aa";
161 SubscriberRecordPtr record = std::make_shared<EventSubscriberRecord>();
162 MatchingSkills matchingSkills_;
163 record->eventSubscribeInfo = std::make_shared<CommonEventSubscribeInfo>(matchingSkills_);
164 int32_t userId = UNDEFINED_USER;
165 record->eventSubscribeInfo->SetUserId(userId);
166 std::string format = "aa";
167 std::string dumpInfo = "aa";
168 commonEventSubscriberManager->DumpDetailed(title, record, format, dumpInfo);
169 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_0500 end";
170 }
171
172 /**
173 * @tc.name: CommonEventSubscriberManager_0501
174 * @tc.desc: test DumpDetailed function and userId is ALL_USER.
175 * @tc.type: FUNC
176 */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_0501,Level1)177 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_0501, Level1)
178 {
179 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_0501 start";
180 std::shared_ptr<CommonEventSubscriberManager> commonEventSubscriberManager =
181 std::make_shared<CommonEventSubscriberManager>();
182 ASSERT_NE(nullptr, commonEventSubscriberManager);
183 std::string title = "aa";
184 SubscriberRecordPtr record = std::make_shared<EventSubscriberRecord>();
185 MatchingSkills matchingSkills_;
186 record->eventSubscribeInfo = std::make_shared<CommonEventSubscribeInfo>(matchingSkills_);
187 int32_t userId = ALL_USER;
188 record->eventSubscribeInfo->SetUserId(userId);
189
190 MatchingSkills matchSkills;
191 std::string event = "event.unit.test";
192 matchSkills.AddEvent(event);
193 EXPECT_EQ(1, matchSkills.CountEvent());
194 std::string entity = "event.unit.test";
195 matchSkills.AddEntity(entity);
196 EXPECT_EQ(1, matchSkills.CountEntities());
197 std::string shceme = "event.unit.test";
198 matchSkills.AddScheme(shceme);
199 EXPECT_EQ(1, matchSkills.CountSchemes());
200 std::string format = "aa";
201 std::string dumpInfo = "aa";
202 commonEventSubscriberManager->DumpDetailed(title, record, format, dumpInfo);
203 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_0501 end";
204 }
205
206 /**
207 * @tc.name: CommonEventSubscriberManager_0600
208 * @tc.desc: test DumpDetailed function and userId is 100.
209 * @tc.type: FUNC
210 */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_0600,Level1)211 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_0600, Level1)
212 {
213 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_0600 start";
214 std::shared_ptr<CommonEventSubscriberManager> commonEventSubscriberManager =
215 std::make_shared<CommonEventSubscriberManager>();
216 ASSERT_NE(nullptr, commonEventSubscriberManager);
217 std::string title = "aa";
218 SubscriberRecordPtr record = std::make_shared<EventSubscriberRecord>();
219 MatchingSkills matchingSkills_;
220 record->eventSubscribeInfo = std::make_shared<CommonEventSubscribeInfo>(matchingSkills_);
221 int32_t userId = 100;
222 record->eventSubscribeInfo->SetUserId(userId);
223 std::string format = "aa";
224 std::string dumpInfo = "aa";
225 commonEventSubscriberManager->DumpDetailed(title, record, format, dumpInfo);
226 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_0600 end";
227 }
228
229 /**
230 * @tc.name: CommonEventSubscriberManager_0700
231 * @tc.desc: test InsertSubscriberRecordLocked function and record is nullptr.
232 * @tc.type: FUNC
233 */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_0700,Level1)234 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_0700, Level1)
235 {
236 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_0700 start";
237 CommonEventSubscriberManager commonEventSubscriberManager;
238 std::string event = "aa";
239 std::vector<std::string> events;
240 events.emplace_back(event);
241 SubscriberRecordPtr record = nullptr;
242 EXPECT_EQ(false, commonEventSubscriberManager.InsertSubscriberRecordLocked(events, record));
243 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_0700 end";
244 }
245
246 /**
247 * @tc.name: CommonEventSubscriberManager_0800
248 * @tc.desc: test InsertSubscriberRecordLocked function.
249 * @tc.type: FUNC
250 */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_0800,Level1)251 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_0800, Level1)
252 {
253 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_0800 start";
254 CommonEventSubscriberManager commonEventSubscriberManager;
255 std::string event = "aa";
256 std::vector<std::string> events;
257 events.emplace_back(event);
258 SubscriberRecordPtr record = std::make_shared<EventSubscriberRecord>();
259 MatchingSkills matchingSkills_;
260 record->eventSubscribeInfo = std::make_shared<CommonEventSubscribeInfo>(matchingSkills_);
261 std::set<SubscriberRecordPtr> mults;
262 mults.insert(record);
263 commonEventSubscriberManager.eventSubscribers_.emplace(event, mults);
264 EXPECT_EQ(true, commonEventSubscriberManager.InsertSubscriberRecordLocked(events, record));
265 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_0800 end";
266 }
267
268 /**
269 * @tc.name: CommonEventSubscriberManager_0900
270 * @tc.desc: test RemoveSubscriberRecordLocked function and commonEventListener is nullptr.
271 * @tc.type: FUNC
272 */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_0900,Level1)273 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_0900, Level1)
274 {
275 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_0900 start";
276 CommonEventSubscriberManager commonEventSubscriberManager;
277 sptr<IRemoteObject> commonEventListener = nullptr;
278 EXPECT_EQ(ERR_INVALID_VALUE, commonEventSubscriberManager.RemoveSubscriberRecordLocked(commonEventListener));
279 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_0900 end";
280 }
281
282 /**
283 * @tc.name: CommonEventSubscriberManager_1000
284 * @tc.desc: test CheckSubscriberByUserId function.
285 * @tc.type: FUNC
286 */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_1000,Level1)287 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_1000, Level1)
288 {
289 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_1000 start";
290 CommonEventSubscriberManager commonEventSubscriberManager;
291 int32_t subscriberUserId = UNDEFINED_USER;
292 bool isSystemApp = true;
293 int32_t userId = UNDEFINED_USER;
294 EXPECT_EQ(true, commonEventSubscriberManager.CheckSubscriberByUserId(subscriberUserId, isSystemApp, userId));
295 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_1000 end";
296 }
297
298 /**
299 * @tc.name: CommonEventSubscriberManager_1100
300 * @tc.desc: test CheckSubscriberByUserId function.
301 * @tc.type: FUNC
302 */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_1100,Level1)303 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_1100, Level1)
304 {
305 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_1100 start";
306 CommonEventSubscriberManager commonEventSubscriberManager;
307 int32_t subscriberUserId = UNDEFINED_USER;
308 bool isSystemApp = true;
309 int32_t userId = ALL_USER;
310 EXPECT_EQ(true, commonEventSubscriberManager.CheckSubscriberByUserId(subscriberUserId, isSystemApp, userId));
311 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_1100 end";
312 }
313
314 /**
315 * @tc.name: CommonEventSubscriberManager_1200
316 * @tc.desc: test CheckSubscriberByUserId function.
317 * @tc.type: FUNC
318 */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_1200,Level1)319 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_1200, Level1)
320 {
321 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_1200 start";
322 CommonEventSubscriberManager commonEventSubscriberManager;
323 int32_t subscriberUserId = 100;
324 bool isSystemApp = false;
325 int32_t userId = 100;
326 EXPECT_EQ(true, commonEventSubscriberManager.CheckSubscriberByUserId(subscriberUserId, isSystemApp, userId));
327 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_1200 end";
328 }
329
330 /**
331 * @tc.name: CommonEventSubscriberManager_1300
332 * @tc.desc: test CheckSubscriberByUserId function.
333 * @tc.type: FUNC
334 */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_1300,Level1)335 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_1300, Level1)
336 {
337 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_1300 start";
338 CommonEventSubscriberManager commonEventSubscriberManager;
339 int32_t subscriberUserId = 100;
340 bool isSystemApp = true;
341 int32_t userId = 99;
342 EXPECT_EQ(false, commonEventSubscriberManager.CheckSubscriberByUserId(subscriberUserId, isSystemApp, userId));
343 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_1300 end";
344 }
345
346 /**
347 * @tc.name: CommonEventSubscriberManager_1400
348 * @tc.desc: test CheckSubscriberByUserId function.
349 * @tc.type: FUNC
350 */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_1400,Level1)351 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_1400, Level1)
352 {
353 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_1400 start";
354 CommonEventSubscriberManager commonEventSubscriberManager;
355 int32_t subscriberUserId = 98;
356 bool isSystemApp = true;
357 int32_t userId = 98;
358 EXPECT_EQ(true, commonEventSubscriberManager.CheckSubscriberByUserId(subscriberUserId, isSystemApp, userId));
359 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_1400 end";
360 }
361
362 /**
363 * @tc.name: CommonEventSubscriberManager_1500
364 * @tc.desc: test CheckSubscriberByUserId function.
365 * @tc.type: FUNC
366 */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_1500,Level1)367 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_1500, Level1)
368 {
369 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_1500 start";
370 CommonEventSubscriberManager commonEventSubscriberManager;
371 int32_t subscriberUserId = 101;
372 bool isSystemApp = false;
373 int32_t userId = 99;
374 EXPECT_EQ(false, commonEventSubscriberManager.CheckSubscriberByUserId(subscriberUserId, isSystemApp, userId));
375 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_1500 end";
376 }
377
378 /**
379 * @tc.name: CommonEventSubscriberManager_1600
380 * @tc.desc: test GetSubscriberRecordsByEvent function.
381 * @tc.type: FUNC
382 */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_1600,Level1)383 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_1600, Level1)
384 {
385 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_1600 start";
386 std::shared_ptr<CommonEventSubscriberManager> commonEventSubscriberManager =
387 std::make_shared<CommonEventSubscriberManager>();
388 ASSERT_NE(nullptr, commonEventSubscriberManager);
389 std::string event = "";
390 int32_t userId = ALL_USER;
391 std::vector<SubscriberRecordPtr> records;
392 commonEventSubscriberManager->GetSubscriberRecordsByEvent(event, userId, records);
393 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_1600 end";
394 }
395
396 /**
397 * @tc.name: CommonEventSubscriberManager_1700
398 * @tc.desc: test GetSubscriberRecordsByEvent function.
399 * @tc.type: FUNC
400 */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_1700,Level1)401 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_1700, Level1)
402 {
403 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_1700 start";
404 std::shared_ptr<CommonEventSubscriberManager> commonEventSubscriberManager =
405 std::make_shared<CommonEventSubscriberManager>();
406 ASSERT_NE(nullptr, commonEventSubscriberManager);
407 std::string event = "";
408 int32_t userId = 100;
409 std::vector<SubscriberRecordPtr> records;
410 SubscriberRecordPtr record = std::make_shared<EventSubscriberRecord>();
411 MatchingSkills matchingSkills_;
412 record->eventSubscribeInfo = std::make_shared<CommonEventSubscribeInfo>(matchingSkills_);
413 record->eventSubscribeInfo->SetUserId(userId);
414 commonEventSubscriberManager->subscribers_.emplace_back(record);
415 commonEventSubscriberManager->GetSubscriberRecordsByEvent(event, userId, records);
416 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_1700 end";
417 }
418
419 /**
420 * @tc.name: CommonEventSubscriberManager_1800
421 * @tc.desc: test GetSubscriberRecordsByEvent function.
422 * @tc.type: FUNC
423 */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_1800,Level1)424 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_1800, Level1)
425 {
426 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_1800 start";
427 std::shared_ptr<CommonEventSubscriberManager> commonEventSubscriberManager =
428 std::make_shared<CommonEventSubscriberManager>();
429 ASSERT_NE(nullptr, commonEventSubscriberManager);
430 std::string event = "";
431 int32_t userId = 100;
432 std::vector<SubscriberRecordPtr> records;
433 SubscriberRecordPtr record = std::make_shared<EventSubscriberRecord>();
434 MatchingSkills matchingSkills_;
435 record->eventSubscribeInfo = std::make_shared<CommonEventSubscribeInfo>(matchingSkills_);
436 int32_t userIds = 90;
437 record->eventSubscribeInfo->SetUserId(userIds);
438 commonEventSubscriberManager->subscribers_.emplace_back(record);
439 commonEventSubscriberManager->GetSubscriberRecordsByEvent(event, userId, records);
440 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_1800 end";
441 }
442
443 /**
444 * @tc.name: CommonEventSubscriberManager_1900
445 * @tc.desc: test GetSubscriberRecordsByEvent function.
446 * @tc.type: FUNC
447 */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_1900,Level1)448 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_1900, Level1)
449 {
450 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_1900 start";
451 std::shared_ptr<CommonEventSubscriberManager> commonEventSubscriberManager =
452 std::make_shared<CommonEventSubscriberManager>();
453 ASSERT_NE(nullptr, commonEventSubscriberManager);
454 std::string event = "aa";
455 int32_t userId = 99;
456 std::vector<SubscriberRecordPtr> records;
457 commonEventSubscriberManager->GetSubscriberRecordsByEvent(event, userId, records);
458 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_1900 end";
459 }
460
461 /**
462 * @tc.name: CommonEventSubscriberManager_2000
463 * @tc.desc: test GetSubscriberRecordsByEvent function.
464 * @tc.type: FUNC
465 */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_2000,Level1)466 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_2000, Level1)
467 {
468 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_2000 start";
469 std::shared_ptr<CommonEventSubscriberManager> commonEventSubscriberManager =
470 std::make_shared<CommonEventSubscriberManager>();
471 ASSERT_NE(nullptr, commonEventSubscriberManager);
472 std::string event = "aa";
473 int32_t userId = 99;
474 std::vector<SubscriberRecordPtr> records;
475 std::set<SubscriberRecordPtr> sub;
476 SubscriberRecordPtr record = std::make_shared<EventSubscriberRecord>();
477 sub.insert(record);
478 MatchingSkills matchingSkills_;
479 record->eventSubscribeInfo = std::make_shared<CommonEventSubscribeInfo>(matchingSkills_);
480 int32_t userIds = -1;
481 record->eventSubscribeInfo->SetUserId(userIds);
482 commonEventSubscriberManager->eventSubscribers_.emplace(event, sub);
483 commonEventSubscriberManager->GetSubscriberRecordsByEvent(event, userId, records);
484 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_2000 end";
485 }
486
487 /**
488 * @tc.name: CommonEventSubscriberManager_2100
489 * @tc.desc: test GetSubscriberRecordsByEvent function.
490 * @tc.type: FUNC
491 */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_2100,Level1)492 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_2100, Level1)
493 {
494 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_2100 start";
495 std::shared_ptr<CommonEventSubscriberManager> commonEventSubscriberManager =
496 std::make_shared<CommonEventSubscriberManager>();
497 ASSERT_NE(nullptr, commonEventSubscriberManager);
498 std::string event = "aa";
499 int32_t userId = 99;
500 std::vector<SubscriberRecordPtr> records;
501 std::set<SubscriberRecordPtr> sub;
502 SubscriberRecordPtr record = std::make_shared<EventSubscriberRecord>();
503 sub.insert(record);
504 MatchingSkills matchingSkills_;
505 record->eventSubscribeInfo = std::make_shared<CommonEventSubscribeInfo>(matchingSkills_);
506 int32_t userIds = 101;
507 record->eventSubscribeInfo->SetUserId(userIds);
508 commonEventSubscriberManager->eventSubscribers_.emplace(event, sub);
509 commonEventSubscriberManager->GetSubscriberRecordsByEvent(event, userId, records);
510 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_2100 end";
511 }
512
513 /**
514 * @tc.name: CommonEventSubscriberManager_2200
515 * @tc.desc: test RemoveFrozenEventsBySubscriber function.
516 * @tc.type: FUNC
517 */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_2200,Level1)518 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_2200, Level1)
519 {
520 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_2200 start";
521 std::shared_ptr<CommonEventSubscriberManager> commonEventSubscriberManager =
522 std::make_shared<CommonEventSubscriberManager>();
523 ASSERT_NE(nullptr, commonEventSubscriberManager);
524 SubscriberRecordPtr subscriberRecord = std::make_shared<EventSubscriberRecord>();
525 uid_t uids = 1;
526 subscriberRecord->eventRecordInfo.uid = uids;
527 // set frozenEvents_
528 FrozenRecords frozenRecord;
529 commonEventSubscriberManager->frozenEvents_.emplace(uids, frozenRecord);
530 commonEventSubscriberManager->RemoveFrozenEventsBySubscriber(subscriberRecord);
531 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_2200 end";
532 }
533
534 /**
535 * @tc.name: CommonEventSubscriberManager_2300
536 * @tc.desc: test DumpState function when records.size()>0.
537 * @tc.type: FUNC
538 */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_2300,Level1)539 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_2300, Level1)
540 {
541 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_2300 start";
542 std::shared_ptr<CommonEventSubscriberManager> commonEventSubscriberManager =
543 std::make_shared<CommonEventSubscriberManager>();
544 ASSERT_NE(nullptr, commonEventSubscriberManager);
545 std::string event = "";
546 int32_t userId = 100;
547 std::vector<SubscriberRecordPtr> records;
548 SubscriberRecordPtr record = std::make_shared<EventSubscriberRecord>();
549 MatchingSkills matchingSkills_;
550 record->eventSubscribeInfo = std::make_shared<CommonEventSubscribeInfo>(matchingSkills_);
551 record->eventSubscribeInfo->SetUserId(userId);
552 commonEventSubscriberManager->subscribers_.emplace_back(record);
553 commonEventSubscriberManager->GetSubscriberRecordsByEvent(event, userId, records);
554 EXPECT_EQ(1, records.size());
555 std::vector<std::string> state;
556 commonEventSubscriberManager->DumpState(event, userId, state);
557 commonEventSubscriberManager->UpdateAllFreezeInfos(true, 1);
558 commonEventSubscriberManager->UpdateAllFreezeInfos(false, 1);
559 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_2300 end";
560 }
561
562 /**
563 * @tc.name: CommonEventSubscriberManager_2400
564 * @tc.desc: test DumpState function when record is nullptr..
565 * @tc.type: FUNC
566 */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_2400,Level1)567 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_2400, Level1)
568 {
569 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_2400 start";
570 std::shared_ptr<CommonEventSubscriberManager> commonEventSubscriberManager =
571 std::make_shared<CommonEventSubscriberManager>();
572 ASSERT_NE(nullptr, commonEventSubscriberManager);
573 SubscriberRecordPtr subscriberRecord = std::make_shared<EventSubscriberRecord>();
574 CommonEventRecord eventRecord;
575 commonEventSubscriberManager->InsertFrozenEvents(subscriberRecord, eventRecord);
576 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_2400 end";
577 }
578
579 /**
580 * @tc.name: CommonEventSubscriberManager_2500
581 * @tc.desc: test DumpState function when record not nullptr..
582 * @tc.type: FUNC
583 */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventSubscriberManager_2500,Level1)584 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventSubscriberManager_2500, Level1)
585 {
586 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_2500 start";
587 std::shared_ptr<CommonEventSubscriberManager> commonEventSubscriberManager =
588 std::make_shared<CommonEventSubscriberManager>();
589 ASSERT_NE(nullptr, commonEventSubscriberManager);
590 SubscriberRecordPtr subscriberRecord = nullptr;
591 CommonEventRecord eventRecord;
592 commonEventSubscriberManager->InsertFrozenEvents(subscriberRecord, eventRecord);
593 GTEST_LOG_(INFO) << "CommonEventSubscriberManager_2500 end";
594 }
595
596 /**
597 * @tc.name: CommonEventStickyManager_0100
598 * @tc.desc: test UpdateStickyEventLocked function.
599 * @tc.type: FUNC
600 */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventStickyManager_0100,Level1)601 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventStickyManager_0100, Level1)
602 {
603 GTEST_LOG_(INFO) << "CommonEventStickyManager_0100 start";
604 CommonEventStickyManager commonEventStickyManager;
605 std::string event = "";
606 std::shared_ptr<CommonEventRecord> record = nullptr;
607 EXPECT_EQ(ERR_INVALID_VALUE, commonEventStickyManager.UpdateStickyEventLocked(event, record));
608 GTEST_LOG_(INFO) << "CommonEventStickyManager_0100 end";
609 }
610
611 /**
612 * @tc.name: CommonEventStickyManager_0200
613 * @tc.desc: test UpdateStickyEventLocked function.
614 * @tc.type: FUNC
615 */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventStickyManager_0200,Level1)616 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventStickyManager_0200, Level1)
617 {
618 GTEST_LOG_(INFO) << "CommonEventStickyManager_0200 start";
619 CommonEventStickyManager commonEventStickyManager;
620 std::string event = "aa";
621 std::shared_ptr<CommonEventRecord> record = nullptr;
622 EXPECT_EQ(ERR_INVALID_VALUE, commonEventStickyManager.UpdateStickyEventLocked(event, record));
623 GTEST_LOG_(INFO) << "CommonEventStickyManager_0200 end";
624 }
625
626 /**
627 * @tc.name: CommonEventStickyManager_0300
628 * @tc.desc: test GetStickyCommonEventRecords function.
629 * @tc.type: FUNC
630 */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventStickyManager_0300,Level1)631 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventStickyManager_0300, Level1)
632 {
633 GTEST_LOG_(INFO) << "CommonEventStickyManager_0300 start";
634 std::shared_ptr<CommonEventStickyManager> commonEventStickyManager =
635 std::make_shared<CommonEventStickyManager>();
636 ASSERT_NE(nullptr, commonEventStickyManager);
637 std::string event = "";
638 int32_t userId = ALL_USER;
639 std::vector<std::shared_ptr<CommonEventRecord>> records;
640 // set commonEventRecords_
641 std::shared_ptr<CommonEventRecord> comm = std::make_shared<CommonEventRecord>();
642 comm->userId = ALL_USER;
643 commonEventStickyManager->commonEventRecords_.emplace(event, comm);
644 commonEventStickyManager->GetStickyCommonEventRecords(event, userId, records);
645 GTEST_LOG_(INFO) << "CommonEventStickyManager_0300 end";
646 }
647
648 /**
649 * @tc.name: CommonEventStickyManager_0400
650 * @tc.desc: test GetStickyCommonEventRecords function.
651 * @tc.type: FUNC
652 */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventStickyManager_0400,Level1)653 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventStickyManager_0400, Level1)
654 {
655 GTEST_LOG_(INFO) << "CommonEventStickyManager_0400 start";
656 std::shared_ptr<CommonEventStickyManager> commonEventStickyManager =
657 std::make_shared<CommonEventStickyManager>();
658 ASSERT_NE(nullptr, commonEventStickyManager);
659 std::string event = "";
660 int32_t userId = 100;
661 std::vector<std::shared_ptr<CommonEventRecord>> records;
662 // set commonEventRecords_
663 std::shared_ptr<CommonEventRecord> comm = std::make_shared<CommonEventRecord>();
664 comm->userId = 101;
665 commonEventStickyManager->commonEventRecords_.emplace(event, comm);
666 commonEventStickyManager->GetStickyCommonEventRecords(event, userId, records);
667 GTEST_LOG_(INFO) << "CommonEventStickyManager_0400 end";
668 }
669
670 /**
671 * @tc.name: CommonEventStickyManager_0500
672 * @tc.desc: test GetStickyCommonEventRecords function.
673 * @tc.type: FUNC
674 */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventStickyManager_0500,Level1)675 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventStickyManager_0500, Level1)
676 {
677 GTEST_LOG_(INFO) << "CommonEventStickyManager_0500 start";
678 std::shared_ptr<CommonEventStickyManager> commonEventStickyManager =
679 std::make_shared<CommonEventStickyManager>();
680 ASSERT_NE(nullptr, commonEventStickyManager);
681 std::string event = "aa";
682 int32_t userId = 100;
683 std::vector<std::shared_ptr<CommonEventRecord>> records;
684 // set commonEventRecords_
685 std::shared_ptr<CommonEventRecord> comm = std::make_shared<CommonEventRecord>();
686 comm->userId = 101;
687 commonEventStickyManager->commonEventRecords_.emplace(event, comm);
688 commonEventStickyManager->GetStickyCommonEventRecords(event, userId, records);
689 GTEST_LOG_(INFO) << "CommonEventStickyManager_0500 end";
690 }
691
692 /**
693 * @tc.name: CommonEventStickyManager_0600
694 * @tc.desc: test GetStickyCommonEventRecords function.
695 * @tc.type: FUNC
696 */
HWTEST_F(CommonEventSubscriberManagerTest,CommonEventStickyManager_0600,Level1)697 HWTEST_F(CommonEventSubscriberManagerTest, CommonEventStickyManager_0600, Level1)
698 {
699 GTEST_LOG_(INFO) << "CommonEventStickyManager_0600 start";
700 std::shared_ptr<CommonEventStickyManager> commonEventStickyManager =
701 std::make_shared<CommonEventStickyManager>();
702 ASSERT_NE(nullptr, commonEventStickyManager);
703 std::string event = "aa";
704 int32_t userId = ALL_USER;
705 std::vector<std::shared_ptr<CommonEventRecord>> records;
706 // set commonEventRecords_
707 std::shared_ptr<CommonEventRecord> comm = std::make_shared<CommonEventRecord>();
708 comm->userId = ALL_USER;
709 commonEventStickyManager->commonEventRecords_.emplace(event, comm);
710 commonEventStickyManager->GetStickyCommonEventRecords(event, userId, records);
711 GTEST_LOG_(INFO) << "CommonEventStickyManager_0600 end";
712 }
713
714 /**
715 * @tc.name: CheckSubscriberPermission_1000
716 * @tc.desc: test CheckSubscriberPermission function.
717 * @tc.type: FUNC
718 */
HWTEST_F(CommonEventSubscriberManagerTest,CheckSubscriberPermission_1000,Level0)719 HWTEST_F(CommonEventSubscriberManagerTest, CheckSubscriberPermission_1000, Level0)
720 {
721 GTEST_LOG_(INFO) << "CheckSubscriberPermission_1000 start";
722 std::shared_ptr<CommonEventSubscriberManager> commonEventSubscriberManager =
723 std::make_shared<CommonEventSubscriberManager>();
724 auto subscriberRecord = std::make_shared<EventSubscriberRecord>();
725 subscriberRecord->eventRecordInfo.isProxy = false;
726 subscriberRecord->eventRecordInfo.isSubsystem = true;
727 subscriberRecord->eventRecordInfo.isSystemApp = true;
728 CommonEventRecord eventRecord;
729 std::shared_ptr<CommonEventData> commonEventData = std::make_shared<CommonEventData>();
730 eventRecord.commonEventData = commonEventData;
731 MockGetEventPermission(true);
732 EXPECT_EQ(true, commonEventSubscriberManager->CheckSubscriberPermission(subscriberRecord, eventRecord));
733 GTEST_LOG_(INFO) << "CheckSubscriberPermission_1000 end";
734 }
735
736 /**
737 * @tc.name: CheckSubscriberPermission_1100
738 * @tc.desc: test CheckSubscriberPermission function.
739 * @tc.type: FUNC
740 */
HWTEST_F(CommonEventSubscriberManagerTest,CheckSubscriberPermission_1100,Level0)741 HWTEST_F(CommonEventSubscriberManagerTest, CheckSubscriberPermission_1100, Level0)
742 {
743 GTEST_LOG_(INFO) << "CheckSubscriberPermission_1100 start";
744 std::shared_ptr<CommonEventSubscriberManager> commonEventSubscriberManager =
745 std::make_shared<CommonEventSubscriberManager>();
746 auto subscriberRecord = std::make_shared<EventSubscriberRecord>();
747 subscriberRecord->eventRecordInfo.isProxy = true;
748 subscriberRecord->eventRecordInfo.isSubsystem = false;
749 subscriberRecord->eventRecordInfo.isSystemApp = false;
750 CommonEventRecord eventRecord;
751 std::shared_ptr<CommonEventData> commonEventData = std::make_shared<CommonEventData>();
752 eventRecord.commonEventData = commonEventData;
753 MockGetEventPermission(true);
754 MockIsVerfyPermisson(true);
755 EXPECT_EQ(true, commonEventSubscriberManager->CheckSubscriberPermission(subscriberRecord, eventRecord));
756 GTEST_LOG_(INFO) << "CheckSubscriberPermission_1100 end";
757 }
758
759 /**
760 * @tc.name: CheckSubscriberPermission_1200
761 * @tc.desc: test CheckSubscriberPermission function.
762 * @tc.type: FUNC
763 */
HWTEST_F(CommonEventSubscriberManagerTest,CheckSubscriberPermission_1200,Level0)764 HWTEST_F(CommonEventSubscriberManagerTest, CheckSubscriberPermission_1200, Level0)
765 {
766 GTEST_LOG_(INFO) << "CheckSubscriberPermission_1200 start";
767 std::shared_ptr<CommonEventSubscriberManager> commonEventSubscriberManager =
768 std::make_shared<CommonEventSubscriberManager>();
769 auto subscriberRecord = std::make_shared<EventSubscriberRecord>();
770 subscriberRecord->eventRecordInfo.isProxy = true;
771 subscriberRecord->eventRecordInfo.isSubsystem = false;
772 subscriberRecord->eventRecordInfo.isSystemApp = false;
773 subscriberRecord->eventRecordInfo.subId = "xxxx";
774 CommonEventRecord eventRecord;
775 std::shared_ptr<CommonEventData> commonEventData = std::make_shared<CommonEventData>();
776 OHOS::AAFwk::Want want;
777 want.SetAction("usual.event.USER_LOCKED");
778 commonEventData->SetWant(want);
779 eventRecord.commonEventData = commonEventData;
780 MockGetEventPermission(true);
781 MockIsVerfyPermisson(false);
782 EXPECT_EQ(false, commonEventSubscriberManager->CheckSubscriberPermission(subscriberRecord, eventRecord));
783 GTEST_LOG_(INFO) << "CheckSubscriberPermission_1200 end";
784 }
785
786 /**
787 * @tc.name: CheckSubscriberPermission_0200
788 * @tc.desc: test CheckSubscriberPermission permission.names.size is 2 and permission.state is PermissionState::AND.
789 * @tc.type: FUNC
790 */
HWTEST_F(CommonEventSubscriberManagerTest,CheckSubscriberPermission_1300,Level0)791 HWTEST_F(CommonEventSubscriberManagerTest, CheckSubscriberPermission_1300, Level0)
792 {
793 GTEST_LOG_(INFO) << "CheckSubscriberPermission_0200 start";
794 std::shared_ptr<CommonEventSubscriberManager> commonEventSubscriberManager =
795 std::make_shared<CommonEventSubscriberManager>();
796 auto subscriberRecord = std::make_shared<EventSubscriberRecord>();
797 std::shared_ptr<CommonEventSubscribeInfo> eventSubscribeInfo = std::make_shared<CommonEventSubscribeInfo>();
798 subscriberRecord->eventSubscribeInfo = eventSubscribeInfo;
799 subscriberRecord->eventRecordInfo.isProxy = true;
800 subscriberRecord->eventRecordInfo.isSubsystem = true;
801 subscriberRecord->eventRecordInfo.isSystemApp = true;
802 subscriberRecord->eventRecordInfo.subId = "xxxx";
803 subscriberRecord->eventRecordInfo.uid = 1001;
804 CommonEventRecord eventRecord;
805 std::shared_ptr<CommonEventData> commonEventData = std::make_shared<CommonEventData>();
806 std::shared_ptr<CommonEventPublishInfo> publishInfo = std::make_shared<CommonEventPublishInfo>();
807 eventRecord.publishInfo = publishInfo;
808
809 OHOS::AAFwk::Want want;
810 want.SetAction("usual.event.USER_LOCKED");
811 commonEventData->SetWant(want);
812 eventRecord.commonEventData = commonEventData;
813
814 // set VerifyAccessToken is false
815 MockIsVerfyPermisson(false);
816 MockIsSystemApp(false);
817 EXPECT_EQ(false, commonEventSubscriberManager->CheckSubscriberPermission(subscriberRecord, eventRecord));
818 GTEST_LOG_(INFO) << "CheckSubscriberPermission_0200 end";
819 }
820
821 /**
822 * @tc.name: CheckSubscriberPermission_0300
823 * @tc.desc: test CheckSubscriberPermission permission.names.size is 2 and permission.state is PermissionState::AND.
824 * @tc.type: FUNC
825 */
HWTEST_F(CommonEventSubscriberManagerTest,CheckSubscriberPermission_1400,Level0)826 HWTEST_F(CommonEventSubscriberManagerTest, CheckSubscriberPermission_1400, Level0)
827 {
828 GTEST_LOG_(INFO) << "CheckSubscriberPermission_0300 start";
829 std::shared_ptr<CommonEventSubscriberManager> commonEventSubscriberManager =
830 std::make_shared<CommonEventSubscriberManager>();
831 auto subscriberRecord = std::make_shared<EventSubscriberRecord>();
832 std::shared_ptr<CommonEventSubscribeInfo> eventSubscribeInfo = std::make_shared<CommonEventSubscribeInfo>();
833 subscriberRecord->eventSubscribeInfo = eventSubscribeInfo;
834 subscriberRecord->eventRecordInfo.isProxy = true;
835 subscriberRecord->eventRecordInfo.isSubsystem = true;
836 subscriberRecord->eventRecordInfo.isSystemApp = true;
837 CommonEventRecord eventRecord;
838 std::shared_ptr<CommonEventData> commonEventData = std::make_shared<CommonEventData>();
839 eventRecord.commonEventData = commonEventData;
840 std::shared_ptr<CommonEventPublishInfo> publishInfo = std::make_shared<CommonEventPublishInfo>();
841 eventRecord.publishInfo = publishInfo;
842 // set VerifyAccessToken is true
843 MockIsVerfyPermisson(true);
844 EXPECT_EQ(true, commonEventSubscriberManager->CheckSubscriberPermission(subscriberRecord, eventRecord));
845 GTEST_LOG_(INFO) << "CheckSubscriberPermission_0300 end";
846 }
847
848 /**
849 * @tc.name: CheckSubscriberPermission_0300
850 * @tc.desc: test CheckSubscriberPermission permission.names.size is 2 and permission.state is PermissionState::AND.
851 * @tc.type: FUNC
852 */
HWTEST_F(CommonEventSubscriberManagerTest,CheckSubscriberPermission_1500,Level0)853 HWTEST_F(CommonEventSubscriberManagerTest, CheckSubscriberPermission_1500, Level0)
854 {
855 GTEST_LOG_(INFO) << "CheckSubscriberPermission_0300 start";
856 std::shared_ptr<CommonEventSubscriberManager> commonEventSubscriberManager =
857 std::make_shared<CommonEventSubscriberManager>();
858 auto subscriberRecord = std::make_shared<EventSubscriberRecord>();
859 std::shared_ptr<CommonEventSubscribeInfo> eventSubscribeInfo = std::make_shared<CommonEventSubscribeInfo>();
860 subscriberRecord->eventSubscribeInfo = eventSubscribeInfo;
861 subscriberRecord->eventRecordInfo.isProxy = true;
862 subscriberRecord->eventRecordInfo.isSubsystem = false;
863 subscriberRecord->eventRecordInfo.isSystemApp = true;
864 CommonEventRecord eventRecord;
865 std::shared_ptr<CommonEventData> commonEventData = std::make_shared<CommonEventData>();
866 eventRecord.commonEventData = commonEventData;
867 std::shared_ptr<CommonEventPublishInfo> publishInfo = std::make_shared<CommonEventPublishInfo>();
868 eventRecord.publishInfo = publishInfo;
869 OHOS::AAFwk::Want want;
870 want.SetAction("usual.event.USER_LOCKED");
871 commonEventData->SetWant(want);
872 eventRecord.commonEventData = commonEventData;
873 // set VerifyAccessToken is true
874 MockGetEventPermission(true, PermissionState::AND, 2);
875 MockIsVerfyPermisson(true);
876
877 EXPECT_EQ(true, commonEventSubscriberManager->CheckSubscriberPermission(subscriberRecord, eventRecord));
878 GTEST_LOG_(INFO) << "CheckSubscriberPermission_0300 end";
879 }
880
881 /**
882 * @tc.name: CheckSubscriberPermission_0300
883 * @tc.desc: test CheckSubscriberPermission permission.names.size is 2 and permission.state is PermissionState::AND.
884 * @tc.type: FUNC
885 */
HWTEST_F(CommonEventSubscriberManagerTest,CheckSubscriberPermission_1600,Level0)886 HWTEST_F(CommonEventSubscriberManagerTest, CheckSubscriberPermission_1600, Level0)
887 {
888 GTEST_LOG_(INFO) << "CheckSubscriberPermission_0300 start";
889 std::shared_ptr<CommonEventSubscriberManager> commonEventSubscriberManager =
890 std::make_shared<CommonEventSubscriberManager>();
891 auto subscriberRecord = std::make_shared<EventSubscriberRecord>();
892 std::shared_ptr<CommonEventSubscribeInfo> eventSubscribeInfo = std::make_shared<CommonEventSubscribeInfo>();
893 subscriberRecord->eventSubscribeInfo = eventSubscribeInfo;
894 subscriberRecord->eventRecordInfo.isProxy = true;
895 subscriberRecord->eventRecordInfo.isSubsystem = false;
896 subscriberRecord->eventRecordInfo.isSystemApp = false;
897 subscriberRecord->eventRecordInfo.subId = "xxxx";
898 CommonEventRecord eventRecord;
899 std::shared_ptr<CommonEventData> commonEventData = std::make_shared<CommonEventData>();
900 eventRecord.commonEventData = commonEventData;
901 std::shared_ptr<CommonEventPublishInfo> publishInfo = std::make_shared<CommonEventPublishInfo>();
902 eventRecord.publishInfo = publishInfo;
903 OHOS::AAFwk::Want want;
904 want.SetAction("usual.event.USER_LOCKED");
905 commonEventData->SetWant(want);
906 eventRecord.commonEventData = commonEventData;
907
908 EXPECT_EQ(false, commonEventSubscriberManager->CheckSubscriberPermission(subscriberRecord, eventRecord));
909 GTEST_LOG_(INFO) << "CheckSubscriberPermission_0300 end";
910 }
911
912 /**
913 * @tc.name: CheckSubscriberPermission_0300
914 * @tc.desc: test CheckSubscriberPermission permission.names.size is 2 and permission.state is PermissionState::AND.
915 * @tc.type: FUNC
916 */
HWTEST_F(CommonEventSubscriberManagerTest,CheckSubscriberPermission_1700,Level0)917 HWTEST_F(CommonEventSubscriberManagerTest, CheckSubscriberPermission_1700, Level0)
918 {
919 GTEST_LOG_(INFO) << "CheckSubscriberPermission_0300 start";
920 std::shared_ptr<CommonEventSubscriberManager> commonEventSubscriberManager =
921 std::make_shared<CommonEventSubscriberManager>();
922 auto subscriberRecord = std::make_shared<EventSubscriberRecord>();
923 std::shared_ptr<CommonEventSubscribeInfo> eventSubscribeInfo = std::make_shared<CommonEventSubscribeInfo>();
924 subscriberRecord->eventSubscribeInfo = eventSubscribeInfo;
925 subscriberRecord->eventRecordInfo.isProxy = true;
926 subscriberRecord->eventRecordInfo.isSubsystem = false;
927 subscriberRecord->eventRecordInfo.isSystemApp = true;
928 subscriberRecord->eventRecordInfo.subId = "xxxx";
929 subscriberRecord->eventRecordInfo.uid = 1001;
930 CommonEventRecord eventRecord;
931 std::shared_ptr<CommonEventData> commonEventData = std::make_shared<CommonEventData>();
932 eventRecord.commonEventData = commonEventData;
933 std::shared_ptr<CommonEventPublishInfo> publishInfo = std::make_shared<CommonEventPublishInfo>();
934 eventRecord.publishInfo = publishInfo;
935 OHOS::AAFwk::Want want;
936 want.SetAction("usual.event.BOOT_COMPLETED");
937 commonEventData->SetWant(want);
938 eventRecord.commonEventData = commonEventData;
939 // set VerifyAccessToken is true
940 MockGetEventPermission(true, PermissionState::AND, 2);
941 MockIsVerfyPermisson(false);
942
943 EXPECT_EQ(false, commonEventSubscriberManager->CheckSubscriberPermission(subscriberRecord, eventRecord));
944 GTEST_LOG_(INFO) << "CheckSubscriberPermission_0300 end";
945 }
946
947 /**
948 * @tc.name: CheckSubscriberPermission_0300
949 * @tc.desc: test CheckSubscriberPermission permission.names.size is 2 and permission.state is PermissionState::AND.
950 * @tc.type: FUNC
951 */
HWTEST_F(CommonEventSubscriberManagerTest,CheckSubscriberPermission_1800,Level0)952 HWTEST_F(CommonEventSubscriberManagerTest, CheckSubscriberPermission_1800, Level0)
953 {
954 GTEST_LOG_(INFO) << "CheckSubscriberPermission_0300 start";
955 std::shared_ptr<CommonEventSubscriberManager> commonEventSubscriberManager =
956 std::make_shared<CommonEventSubscriberManager>();
957 auto subscriberRecord = std::make_shared<EventSubscriberRecord>();
958 std::shared_ptr<CommonEventSubscribeInfo> eventSubscribeInfo = std::make_shared<CommonEventSubscribeInfo>();
959 subscriberRecord->eventSubscribeInfo = eventSubscribeInfo;
960 subscriberRecord->eventRecordInfo.isProxy = true;
961 subscriberRecord->eventRecordInfo.isSubsystem = false;
962 subscriberRecord->eventRecordInfo.isSystemApp = true;
963 subscriberRecord->eventRecordInfo.subId = "xxxx";
964 subscriberRecord->eventRecordInfo.uid = 1001;
965 CommonEventRecord eventRecord;
966 std::shared_ptr<CommonEventData> commonEventData = std::make_shared<CommonEventData>();
967 eventRecord.commonEventData = commonEventData;
968 std::shared_ptr<CommonEventPublishInfo> publishInfo = std::make_shared<CommonEventPublishInfo>();
969 eventRecord.publishInfo = publishInfo;
970 OHOS::AAFwk::Want want;
971 want.SetAction("usual.event.BOOT_COMPLETED");
972 commonEventData->SetWant(want);
973 eventRecord.commonEventData = commonEventData;
974 // set VerifyAccessToken is true
975 MockGetEventPermission(true, PermissionState::OR, 2);
976 MockIsVerfyPermisson(false);
977
978 EXPECT_EQ(false, commonEventSubscriberManager->CheckSubscriberPermission(subscriberRecord, eventRecord));
979 GTEST_LOG_(INFO) << "CheckSubscriberPermission_0300 end";
980 }
981
982 /**
983 * @tc.name: CheckSubscriberPermission_0300
984 * @tc.desc: test CheckSubscriberPermission permission.names.size is 2 and permission.state is PermissionState::AND.
985 * @tc.type: FUNC
986 */
HWTEST_F(CommonEventSubscriberManagerTest,CheckSubscriberPermission_1900,Level0)987 HWTEST_F(CommonEventSubscriberManagerTest, CheckSubscriberPermission_1900, Level0)
988 {
989 GTEST_LOG_(INFO) << "CheckSubscriberPermission_0300 start";
990 std::shared_ptr<CommonEventSubscriberManager> commonEventSubscriberManager =
991 std::make_shared<CommonEventSubscriberManager>();
992 auto subscriberRecord = std::make_shared<EventSubscriberRecord>();
993 std::shared_ptr<CommonEventSubscribeInfo> eventSubscribeInfo = std::make_shared<CommonEventSubscribeInfo>();
994 subscriberRecord->eventSubscribeInfo = eventSubscribeInfo;
995 subscriberRecord->eventRecordInfo.isProxy = true;
996 subscriberRecord->eventRecordInfo.isSubsystem = false;
997 subscriberRecord->eventRecordInfo.isSystemApp = true;
998 CommonEventRecord eventRecord;
999 std::shared_ptr<CommonEventData> commonEventData = std::make_shared<CommonEventData>();
1000 eventRecord.commonEventData = commonEventData;
1001 std::shared_ptr<CommonEventPublishInfo> publishInfo = std::make_shared<CommonEventPublishInfo>();
1002 eventRecord.publishInfo = publishInfo;
1003 OHOS::AAFwk::Want want;
1004 want.SetAction("usual.event.USER_LOCKED");
1005 commonEventData->SetWant(want);
1006 eventRecord.commonEventData = commonEventData;
1007 // set VerifyAccessToken is true
1008 MockGetEventPermission(true, PermissionState::OR, 2);
1009 MockIsVerfyPermisson(true);
1010
1011 EXPECT_EQ(true, commonEventSubscriberManager->CheckSubscriberPermission(subscriberRecord, eventRecord));
1012 GTEST_LOG_(INFO) << "CheckSubscriberPermission_0300 end";
1013 }
1014
1015 /**
1016 * @tc.name: CommonEventControlManager_0800
1017 * @tc.desc: test CheckPublisherRequiredPermissions and VerifyAccessToken is false.
1018 * @tc.type: FUNC
1019 */
HWTEST_F(CommonEventSubscriberManagerTest,CheckPublisherRequiredPermissions_0800,Level0)1020 HWTEST_F(CommonEventSubscriberManagerTest, CheckPublisherRequiredPermissions_0800, Level0)
1021 {
1022 GTEST_LOG_(INFO) << "CommonEventControlManager_0800 start";
1023 std::shared_ptr<CommonEventSubscriberManager> commonEventSubscriberManager =
1024 std::make_shared<CommonEventSubscriberManager>();
1025 std::string publisherRequiredPermission = "aa";
1026 std::vector<std::string> publisherRequiredPermissions;
1027 publisherRequiredPermissions.emplace_back(publisherRequiredPermission);
1028 auto subscriberRecord = std::make_shared<EventSubscriberRecord>();
1029 std::shared_ptr<CommonEventSubscribeInfo> eventSubscribeInfo = std::make_shared<CommonEventSubscribeInfo>();
1030 subscriberRecord->eventSubscribeInfo = eventSubscribeInfo;
1031 subscriberRecord->eventRecordInfo.subId = "xxx";
1032 subscriberRecord->eventRecordInfo.uid = 100;
1033 CommonEventRecord eventRecord;
1034 eventRecord.eventRecordInfo.uid = 1001;
1035 std::shared_ptr<CommonEventData> commonEventData = std::make_shared<CommonEventData>();
1036 eventRecord.commonEventData = commonEventData;
1037 std::shared_ptr<CommonEventPublishInfo> publishInfo = std::make_shared<CommonEventPublishInfo>();
1038 publishInfo->SetSubscriberPermissions(publisherRequiredPermissions);
1039 eventRecord.publishInfo = publishInfo;
1040 // set VerifyAccessToken is false
1041 MockIsVerfyPermisson(false);
1042 EXPECT_EQ(false, commonEventSubscriberManager->CheckPublisherRequiredPermissions(subscriberRecord, eventRecord));
1043 GTEST_LOG_(INFO) << "CommonEventControlManager_0800 end";
1044 }
1045
1046 /**
1047 * @tc.name: CommonEventControlManager_0900
1048 * @tc.desc: test CheckPublisherRequiredPermissions and VerifyAccessToken is true.
1049 * @tc.type: FUNC
1050 */
HWTEST_F(CommonEventSubscriberManagerTest,CheckPublisherRequiredPermissions_0900,Level0)1051 HWTEST_F(CommonEventSubscriberManagerTest, CheckPublisherRequiredPermissions_0900, Level0)
1052 {
1053 GTEST_LOG_(INFO) << "CommonEventControlManager_0900 start";
1054 std::shared_ptr<CommonEventSubscriberManager> commonEventSubscriberManager =
1055 std::make_shared<CommonEventSubscriberManager>();
1056 std::string publisherRequiredPermission = "aa";
1057 std::vector<std::string> publisherRequiredPermissions;
1058 publisherRequiredPermissions.emplace_back(publisherRequiredPermission);
1059 auto subscriberRecord = std::make_shared<EventSubscriberRecord>();
1060 std::shared_ptr<CommonEventSubscribeInfo> eventSubscribeInfo = std::make_shared<CommonEventSubscribeInfo>();
1061 subscriberRecord->eventSubscribeInfo = eventSubscribeInfo;
1062 subscriberRecord->eventRecordInfo.uid = 100;
1063 CommonEventRecord eventRecord;
1064 eventRecord.eventRecordInfo.uid = 1001;
1065 std::shared_ptr<CommonEventData> commonEventData = std::make_shared<CommonEventData>();
1066 eventRecord.commonEventData = commonEventData;
1067 std::shared_ptr<CommonEventPublishInfo> publishInfo = std::make_shared<CommonEventPublishInfo>();
1068 publishInfo->SetSubscriberPermissions(publisherRequiredPermissions);
1069 eventRecord.publishInfo = publishInfo;
1070 // set VerifyAccessToken is true
1071 MockIsVerfyPermisson(true);
1072 EXPECT_EQ(true, commonEventSubscriberManager->CheckPublisherRequiredPermissions(subscriberRecord, eventRecord));
1073 GTEST_LOG_(INFO) << "CommonEventControlManager_0900 end";
1074 }
1075
1076 /**
1077 * @tc.name: CommonEventControlManager_0900
1078 * @tc.desc: test CheckPublisherRequiredPermissions and VerifyAccessToken is true.
1079 * @tc.type: FUNC
1080 */
HWTEST_F(CommonEventSubscriberManagerTest,CheckPublisherRequiredPermissions_1000,Level0)1081 HWTEST_F(CommonEventSubscriberManagerTest, CheckPublisherRequiredPermissions_1000, Level0)
1082 {
1083 GTEST_LOG_(INFO) << "CommonEventControlManager_0900 start";
1084 std::shared_ptr<CommonEventSubscriberManager> commonEventSubscriberManager =
1085 std::make_shared<CommonEventSubscriberManager>();
1086 std::string publisherRequiredPermission = "aa";
1087 std::vector<std::string> publisherRequiredPermissions;
1088 publisherRequiredPermissions.emplace_back(publisherRequiredPermission);
1089 auto subscriberRecord = std::make_shared<EventSubscriberRecord>();
1090 std::shared_ptr<CommonEventSubscribeInfo> eventSubscribeInfo = std::make_shared<CommonEventSubscribeInfo>();
1091 subscriberRecord->eventSubscribeInfo = eventSubscribeInfo;
1092 subscriberRecord->eventRecordInfo.uid = 100;
1093 CommonEventRecord eventRecord;
1094 eventRecord.eventRecordInfo.uid = 100;
1095 std::shared_ptr<CommonEventData> commonEventData = std::make_shared<CommonEventData>();
1096 eventRecord.commonEventData = commonEventData;
1097 std::shared_ptr<CommonEventPublishInfo> publishInfo = std::make_shared<CommonEventPublishInfo>();
1098 publishInfo->SetSubscriberPermissions(publisherRequiredPermissions);
1099 eventRecord.publishInfo = publishInfo;
1100 // set VerifyAccessToken is true
1101 MockIsVerfyPermisson(false);
1102
1103 EXPECT_EQ(true, commonEventSubscriberManager->CheckPublisherRequiredPermissions(subscriberRecord, eventRecord));
1104 GTEST_LOG_(INFO) << "CommonEventControlManager_0900 end";
1105 }
1106
HWTEST_F(CommonEventSubscriberManagerTest,CheckPublisherWhetherMatched_BundleNameNotMatched_ReturnFalse,Level0)1107 HWTEST_F(CommonEventSubscriberManagerTest, CheckPublisherWhetherMatched_BundleNameNotMatched_ReturnFalse, Level0)
1108 {
1109 // Arrange
1110 std::shared_ptr<CommonEventSubscriberManager> manager = std::make_shared<CommonEventSubscriberManager>();
1111 std::shared_ptr<EventSubscriberRecord> subscriberRecord = std::make_shared<EventSubscriberRecord>();
1112 std::shared_ptr<CommonEventSubscribeInfo> eventSubscribeInfo = std::make_shared<CommonEventSubscribeInfo>();
1113 eventSubscribeInfo->SetPublisherBundleName("Bundle2");
1114 subscriberRecord->eventSubscribeInfo = eventSubscribeInfo;
1115
1116 CommonEventRecord eventRecord;
1117 EventRecordInfo eventRecordInfo;
1118 eventRecordInfo.bundleName = "Bundle1";
1119 eventRecord.eventRecordInfo = eventRecordInfo;
1120
1121 // Act
1122 bool result = manager->CheckPublisherWhetherMatched(subscriberRecord, eventRecord);
1123
1124 // Assert
1125 EXPECT_FALSE(result);
1126 }
1127
HWTEST_F(CommonEventSubscriberManagerTest,CheckPublisherWhetherMatched_UidNotMatched_ReturnFalse,Level0)1128 HWTEST_F(CommonEventSubscriberManagerTest, CheckPublisherWhetherMatched_UidNotMatched_ReturnFalse, Level0)
1129 {
1130 // Arrange
1131 std::shared_ptr<CommonEventSubscriberManager> manager = std::make_shared<CommonEventSubscriberManager>();
1132 std::shared_ptr<EventSubscriberRecord> subscriberRecord = std::make_shared<EventSubscriberRecord>();
1133 std::shared_ptr<CommonEventSubscribeInfo> eventSubscribeInfo = std::make_shared<CommonEventSubscribeInfo>();
1134 eventSubscribeInfo->SetPublisherUid(1002);
1135 subscriberRecord->eventSubscribeInfo = eventSubscribeInfo;
1136
1137 CommonEventRecord eventRecord;
1138 EventRecordInfo eventRecordInfo;
1139 eventRecordInfo.uid = 1001;
1140 eventRecord.eventRecordInfo = eventRecordInfo;
1141
1142 // Act
1143 bool result = manager->CheckPublisherWhetherMatched(subscriberRecord, eventRecord);
1144
1145 // Assert
1146 EXPECT_FALSE(result);
1147 }
1148
HWTEST_F(CommonEventSubscriberManagerTest,CheckPublisherWhetherMatched_PermissionNotMatched_ReturnFalse,Level0)1149 HWTEST_F(CommonEventSubscriberManagerTest, CheckPublisherWhetherMatched_PermissionNotMatched_ReturnFalse, Level0)
1150 {
1151 // Arrange
1152 std::shared_ptr<CommonEventSubscriberManager> manager = std::make_shared<CommonEventSubscriberManager>();
1153 std::shared_ptr<EventSubscriberRecord> subscriberRecord = std::make_shared<EventSubscriberRecord>();
1154 std::shared_ptr<CommonEventSubscribeInfo> eventSubscribeInfo = std::make_shared<CommonEventSubscribeInfo>();
1155 eventSubscribeInfo->SetPublisherBundleName("Bundle1");
1156 eventSubscribeInfo->SetPublisherUid(1001);
1157 eventSubscribeInfo->SetPermission("xxxxx");
1158 subscriberRecord->eventSubscribeInfo = eventSubscribeInfo;
1159
1160 CommonEventRecord eventRecord;
1161 EventRecordInfo eventRecordInfo;
1162 eventRecordInfo.uid = 1001;
1163 eventRecordInfo.bundleName = "Bundle1";
1164 eventRecordInfo.subId = "xxxx";
1165 eventRecord.eventRecordInfo = eventRecordInfo;
1166 std::shared_ptr<CommonEventData> commonEventData = std::make_shared<CommonEventData>();
1167 OHOS::AAFwk::Want want;
1168 want.SetAction("usual.event.BOOT_COMPLETED");
1169 commonEventData->SetWant(want);
1170 eventRecord.commonEventData = commonEventData;
1171 MockIsVerfyPermisson(false);
1172
1173 // Act
1174 bool result = manager->CheckPublisherWhetherMatched(subscriberRecord, eventRecord);
1175 // Assert
1176 EXPECT_FALSE(result);
1177 }
1178
HWTEST_F(CommonEventSubscriberManagerTest,CheckPublisherWhetherMatched_AllMatched_ReturnTrue,Level0)1179 HWTEST_F(CommonEventSubscriberManagerTest, CheckPublisherWhetherMatched_AllMatched_ReturnTrue, Level0)
1180 {
1181 // Arrange
1182 std::shared_ptr<CommonEventSubscriberManager> manager = std::make_shared<CommonEventSubscriberManager>();
1183 std::shared_ptr<EventSubscriberRecord> subscriberRecord = std::make_shared<EventSubscriberRecord>();
1184 std::shared_ptr<CommonEventSubscribeInfo> eventSubscribeInfo = std::make_shared<CommonEventSubscribeInfo>();
1185 eventSubscribeInfo->SetPublisherBundleName("Bundle1");
1186 eventSubscribeInfo->SetPublisherUid(1001);
1187 eventSubscribeInfo->SetPermission("xxxxx");
1188 subscriberRecord->eventSubscribeInfo = eventSubscribeInfo;
1189 subscriberRecord->eventRecordInfo.uid = 1001;
1190
1191 CommonEventRecord eventRecord;
1192 EventRecordInfo eventRecordInfo;
1193 eventRecordInfo.bundleName = "Bundle1";
1194 eventRecordInfo.uid = 1001;
1195 eventRecord.eventRecordInfo = eventRecordInfo;
1196 MockIsVerfyPermisson(true);
1197
1198 // Act
1199 bool result = manager->CheckPublisherWhetherMatched(subscriberRecord, eventRecord);
1200 // Assert
1201 EXPECT_TRUE(result);
1202 }
1203
HWTEST_F(CommonEventSubscriberManagerTest,CheckSubscriberWhetherMatched_NoFilter,Level0)1204 HWTEST_F(CommonEventSubscriberManagerTest, CheckSubscriberWhetherMatched_NoFilter, Level0)
1205 {
1206 // Arrange
1207 std::shared_ptr<CommonEventSubscriberManager> manager = std::make_shared<CommonEventSubscriberManager>();
1208 CommonEventRecord eventRecord;
1209 std::shared_ptr<CommonEventPublishInfo> publishInfo = std::make_shared<CommonEventPublishInfo>();
1210 eventRecord.publishInfo = publishInfo;
1211
1212 SubscriberRecordPtr subscriberRecord = std::make_shared<EventSubscriberRecord>();
1213 EventRecordInfo eventRecordInfo;
1214 eventRecordInfo.bundleName = "bundleName2";
1215 subscriberRecord->eventRecordInfo = eventRecordInfo;
1216
1217 // Act
1218 bool result = manager->CheckSubscriberWhetherMatched(subscriberRecord, eventRecord);
1219
1220 // Assert
1221 EXPECT_TRUE(result);
1222 }
1223
HWTEST_F(CommonEventSubscriberManagerTest,CheckSubscriberWhetherMatched_BundleNameNotMatched,Level0)1224 HWTEST_F(CommonEventSubscriberManagerTest, CheckSubscriberWhetherMatched_BundleNameNotMatched, Level0)
1225 {
1226 // Arrange
1227 std::shared_ptr<CommonEventSubscriberManager> manager = std::make_shared<CommonEventSubscriberManager>();
1228 CommonEventRecord eventRecord;
1229 std::shared_ptr<CommonEventPublishInfo> publishInfo = std::make_shared<CommonEventPublishInfo>();
1230 publishInfo->SetBundleName("bundleName1");
1231 eventRecord.publishInfo = publishInfo;
1232
1233 SubscriberRecordPtr subscriberRecord = std::make_shared<EventSubscriberRecord>();
1234 EventRecordInfo eventRecordInfo;
1235 eventRecordInfo.bundleName = "bundleName2";
1236 subscriberRecord->eventRecordInfo = eventRecordInfo;
1237
1238 // Act
1239 bool result = manager->CheckSubscriberWhetherMatched(subscriberRecord, eventRecord);
1240
1241 // Assert
1242 EXPECT_FALSE(result);
1243 }
1244
HWTEST_F(CommonEventSubscriberManagerTest,CheckSubscriberWhetherMatched_SubscriberTypeNotMatched,Level0)1245 HWTEST_F(CommonEventSubscriberManagerTest, CheckSubscriberWhetherMatched_SubscriberTypeNotMatched, Level0)
1246 {
1247 // Arrange
1248 std::shared_ptr<CommonEventSubscriberManager> manager = std::make_shared<CommonEventSubscriberManager>();
1249 CommonEventRecord eventRecord;
1250 std::shared_ptr<CommonEventPublishInfo> publishInfo = std::make_shared<CommonEventPublishInfo>();
1251 publishInfo->SetSubscriberType(SubscriberType::SYSTEM_SUBSCRIBER_TYPE);
1252 eventRecord.publishInfo = publishInfo;
1253
1254 SubscriberRecordPtr subscriberRecord = std::make_shared<EventSubscriberRecord>();
1255 EventRecordInfo eventRecordInfo;
1256 eventRecordInfo.isSystemApp = false;
1257 subscriberRecord->eventRecordInfo = eventRecordInfo;
1258
1259 // Act
1260 bool result = manager->CheckSubscriberWhetherMatched(subscriberRecord, eventRecord);
1261
1262 // Assert
1263 EXPECT_FALSE(result);
1264 }
1265
HWTEST_F(CommonEventSubscriberManagerTest,CheckSubscriberWhetherMatched_SubscriberUidNotMatched,Level0)1266 HWTEST_F(CommonEventSubscriberManagerTest, CheckSubscriberWhetherMatched_SubscriberUidNotMatched, Level0)
1267 {
1268 // Arrange
1269 std::shared_ptr<CommonEventSubscriberManager> manager = std::make_shared<CommonEventSubscriberManager>();
1270 CommonEventRecord eventRecord;
1271 std::shared_ptr<CommonEventPublishInfo> publishInfo = std::make_shared<CommonEventPublishInfo>();
1272 publishInfo->SetSubscriberUid({1, 2, 3});
1273 eventRecord.publishInfo = publishInfo;
1274
1275 SubscriberRecordPtr subscriberRecord = std::make_shared<EventSubscriberRecord>();
1276 EventRecordInfo eventRecordInfo;
1277 eventRecordInfo.uid = 4;
1278 subscriberRecord->eventRecordInfo = eventRecordInfo;
1279
1280 // Act
1281 bool result = manager->CheckSubscriberWhetherMatched(subscriberRecord, eventRecord);
1282
1283 // Assert
1284 EXPECT_FALSE(result);
1285 }
1286
HWTEST_F(CommonEventSubscriberManagerTest,CheckSubscriberWhetherMatched_PublisherRequiredPermissionsNotMatched,Level0)1287 HWTEST_F(CommonEventSubscriberManagerTest, CheckSubscriberWhetherMatched_PublisherRequiredPermissionsNotMatched, Level0)
1288 {
1289 // Arrange
1290 std::shared_ptr<CommonEventSubscriberManager> manager = std::make_shared<CommonEventSubscriberManager>();
1291 SubscriberRecordPtr subscriberRecord = std::make_shared<EventSubscriberRecord>();
1292 EventRecordInfo eventRecordInfo;
1293 eventRecordInfo.uid = 4;
1294 eventRecordInfo.bundleName = "xxx";
1295 eventRecordInfo.subId = "xxxx";
1296 subscriberRecord->eventRecordInfo = eventRecordInfo;
1297 auto eventSubscribeInfo = std::make_shared<CommonEventSubscribeInfo>();
1298 eventSubscribeInfo->SetUserId(100);
1299 subscriberRecord->eventSubscribeInfo = eventSubscribeInfo;
1300 subscriberRecord->eventRecordInfo.uid = 1001;
1301
1302 CommonEventRecord eventRecord;
1303 std::shared_ptr<CommonEventPublishInfo> publishInfo = std::make_shared<CommonEventPublishInfo>();
1304 publishInfo->SetValidationRule(ValidationRule::AND);
1305 std::vector<std::string> publisherRequiredPermissions;
1306 publisherRequiredPermissions.emplace_back("publisherRequiredPermission");
1307 publishInfo->SetSubscriberPermissions(publisherRequiredPermissions);
1308 eventRecord.publishInfo = publishInfo;
1309 eventRecord.eventRecordInfo.uid = 1002;
1310 std::shared_ptr<CommonEventData> commonEventData = std::make_shared<CommonEventData>();
1311 OHOS::AAFwk::Want want;
1312 want.SetAction("usual.event.BOOT_COMPLETED");
1313 commonEventData->SetWant(want);
1314 eventRecord.commonEventData = commonEventData;
1315 MockIsVerfyPermisson(false);
1316
1317 // Act
1318 bool result = manager->CheckSubscriberWhetherMatched(subscriberRecord, eventRecord);
1319
1320 // Assert
1321 EXPECT_FALSE(result);
1322 }
1323
HWTEST_F(CommonEventSubscriberManagerTest,CheckSubscriberWhetherMatched_AllConditionsMatched,Level0)1324 HWTEST_F(CommonEventSubscriberManagerTest, CheckSubscriberWhetherMatched_AllConditionsMatched, Level0)
1325 {
1326 // Arrange
1327 std::shared_ptr<CommonEventSubscriberManager> manager = std::make_shared<CommonEventSubscriberManager>();
1328 std::shared_ptr<CommonEventPublishInfo> publishInfo = std::make_shared<CommonEventPublishInfo>();
1329 CommonEventRecord eventRecord;
1330 publishInfo->SetBundleName("bundleName1");
1331 publishInfo->SetSubscriberType(SubscriberType::SYSTEM_SUBSCRIBER_TYPE);
1332 publishInfo->SetSubscriberUid({1});
1333 publishInfo->SetValidationRule(ValidationRule::AND);
1334 eventRecord.publishInfo = publishInfo;
1335 eventRecord.eventRecordInfo.uid = 1;
1336
1337 SubscriberRecordPtr subscriberRecord = std::make_shared<EventSubscriberRecord>();
1338 EventRecordInfo eventRecordInfo;
1339 eventRecordInfo.bundleName = "bundleName1";
1340 eventRecordInfo.isSystemApp = true;
1341 eventRecordInfo.uid = 1;
1342 subscriberRecord->eventRecordInfo = eventRecordInfo;
1343 std::shared_ptr<CommonEventData> commonEventData = std::make_shared<CommonEventData>();
1344 OHOS::AAFwk::Want want;
1345 want.SetAction("usual.event.BOOT_COMPLETED");
1346 commonEventData->SetWant(want);
1347 eventRecord.commonEventData = commonEventData;
1348 MockIsVerfyPermisson(true);
1349
1350 // Act
1351 bool result = manager->CheckSubscriberWhetherMatched(subscriberRecord, eventRecord);
1352
1353 // Assert
1354 EXPECT_TRUE(result);
1355 }
1356
HWTEST_F(CommonEventSubscriberManagerTest,CheckSubscriberWhetherMatched_UidNotMatchedAndRuleIsAnd,Level0)1357 HWTEST_F(CommonEventSubscriberManagerTest, CheckSubscriberWhetherMatched_UidNotMatchedAndRuleIsAnd, Level0)
1358 {
1359 // Arrange
1360 std::shared_ptr<CommonEventSubscriberManager> manager = std::make_shared<CommonEventSubscriberManager>();
1361 std::shared_ptr<CommonEventPublishInfo> publishInfo = std::make_shared<CommonEventPublishInfo>();
1362 CommonEventRecord eventRecord;
1363 publishInfo->SetBundleName("bundleName1");
1364 publishInfo->SetSubscriberType(SubscriberType::SYSTEM_SUBSCRIBER_TYPE);
1365 publishInfo->SetSubscriberUid({2});
1366 publishInfo->SetValidationRule(ValidationRule::AND);
1367 eventRecord.publishInfo = publishInfo;
1368
1369 SubscriberRecordPtr subscriberRecord = std::make_shared<EventSubscriberRecord>();
1370 EventRecordInfo eventRecordInfo;
1371 eventRecordInfo.bundleName = "bundleName1";
1372 eventRecordInfo.isSystemApp = true;
1373 eventRecordInfo.uid = 1;
1374 subscriberRecord->eventRecordInfo = eventRecordInfo;
1375
1376 // Act
1377 bool result = manager->CheckSubscriberWhetherMatched(subscriberRecord, eventRecord);
1378
1379 // Assert
1380 EXPECT_FALSE(result);
1381 }
1382
HWTEST_F(CommonEventSubscriberManagerTest,CheckSubscriberWhetherMatched_PermissionNotMatchedAndRuleIsAnd,Level0)1383 HWTEST_F(CommonEventSubscriberManagerTest, CheckSubscriberWhetherMatched_PermissionNotMatchedAndRuleIsAnd, Level0)
1384 {
1385 // Arrange
1386 std::shared_ptr<CommonEventSubscriberManager> manager = std::make_shared<CommonEventSubscriberManager>();
1387 std::shared_ptr<CommonEventPublishInfo> publishInfo = std::make_shared<CommonEventPublishInfo>();
1388 CommonEventRecord eventRecord;
1389 publishInfo->SetBundleName("bundleName1");
1390 publishInfo->SetSubscriberType(SubscriberType::SYSTEM_SUBSCRIBER_TYPE);
1391 publishInfo->SetSubscriberUid({1});
1392 publishInfo->SetValidationRule(ValidationRule::AND);
1393 eventRecord.publishInfo = publishInfo;
1394 std::shared_ptr<CommonEventData> commonEventData = std::make_shared<CommonEventData>();
1395 OHOS::AAFwk::Want want;
1396 want.SetAction("usual.event.BOOT_COMPLETED");
1397 commonEventData->SetWant(want);
1398 eventRecord.commonEventData = commonEventData;
1399
1400 SubscriberRecordPtr subscriberRecord = std::make_shared<EventSubscriberRecord>();
1401 EventRecordInfo eventRecordInfo;
1402 eventRecordInfo.bundleName = "bundleName1";
1403 eventRecordInfo.isSystemApp = true;
1404 eventRecordInfo.uid = 1;
1405 eventRecordInfo.subId = "xxxx";
1406 subscriberRecord->eventRecordInfo = eventRecordInfo;
1407 auto eventSubscribeInfo = std::make_shared<CommonEventSubscribeInfo>();
1408 eventSubscribeInfo->SetUserId(100);
1409 subscriberRecord->eventSubscribeInfo = eventSubscribeInfo;
1410 MockIsVerfyPermisson(false);
1411
1412 // Act
1413 bool result = manager->CheckSubscriberWhetherMatched(subscriberRecord, eventRecord);
1414
1415 // Assert
1416 EXPECT_TRUE(result);
1417 }
1418
HWTEST_F(CommonEventSubscriberManagerTest,CheckSubscriberWhetherMatched_OneOfConditionsNotMatchedAndRuleIsOr,Level0)1419 HWTEST_F(CommonEventSubscriberManagerTest, CheckSubscriberWhetherMatched_OneOfConditionsNotMatchedAndRuleIsOr, Level0)
1420 {
1421 // Arrange
1422 std::shared_ptr<CommonEventSubscriberManager> manager = std::make_shared<CommonEventSubscriberManager>();
1423 std::shared_ptr<CommonEventPublishInfo> publishInfo = std::make_shared<CommonEventPublishInfo>();
1424 CommonEventRecord eventRecord;
1425 publishInfo->SetBundleName("bundleName1");
1426 publishInfo->SetSubscriberType(SubscriberType::SYSTEM_SUBSCRIBER_TYPE);
1427 publishInfo->SetSubscriberUid({2});
1428 publishInfo->SetValidationRule(ValidationRule::OR);
1429 std::vector<std::string> publisherRequiredPermissions;
1430 publisherRequiredPermissions.emplace_back("publisherRequiredPermission");
1431 publishInfo->SetSubscriberPermissions(publisherRequiredPermissions);
1432 eventRecord.publishInfo = publishInfo;
1433
1434 SubscriberRecordPtr subscriberRecord = std::make_shared<EventSubscriberRecord>();
1435 EventRecordInfo eventRecordInfo;
1436 eventRecordInfo.bundleName = "bundleName1";
1437 eventRecordInfo.isSystemApp = true;
1438 eventRecordInfo.uid = 1;
1439 subscriberRecord->eventRecordInfo = eventRecordInfo;
1440 std::shared_ptr<CommonEventData> commonEventData = std::make_shared<CommonEventData>();
1441 OHOS::AAFwk::Want want;
1442 want.SetAction("usual.event.BOOT_COMPLETED");
1443 commonEventData->SetWant(want);
1444 eventRecord.commonEventData = commonEventData;
1445 MockIsVerfyPermisson(true);
1446
1447 // Act
1448 bool result = manager->CheckSubscriberWhetherMatched(subscriberRecord, eventRecord);
1449
1450 // Assert
1451 EXPECT_TRUE(result);
1452 }
1453
HWTEST_F(CommonEventSubscriberManagerTest,CheckSubscriberWhetherMatched_AllConditionsNotMatchedAndRuleIsOr,Level0)1454 HWTEST_F(CommonEventSubscriberManagerTest, CheckSubscriberWhetherMatched_AllConditionsNotMatchedAndRuleIsOr, Level0)
1455 {
1456 // Arrange
1457 std::shared_ptr<CommonEventSubscriberManager> manager = std::make_shared<CommonEventSubscriberManager>();
1458 std::shared_ptr<CommonEventPublishInfo> publishInfo = std::make_shared<CommonEventPublishInfo>();
1459 CommonEventRecord eventRecord;
1460 publishInfo->SetBundleName("bundleName1");
1461 publishInfo->SetSubscriberType(SubscriberType::SYSTEM_SUBSCRIBER_TYPE);
1462 publishInfo->SetSubscriberUid({2});
1463 publishInfo->SetValidationRule(ValidationRule::OR);
1464 eventRecord.publishInfo = publishInfo;
1465
1466 SubscriberRecordPtr subscriberRecord = std::make_shared<EventSubscriberRecord>();
1467 EventRecordInfo eventRecordInfo;
1468 eventRecordInfo.bundleName = "bundleName12";
1469 eventRecordInfo.isSystemApp = false;
1470 eventRecordInfo.uid = 1;
1471 subscriberRecord->eventRecordInfo = eventRecordInfo;
1472
1473 // Act
1474 bool result = manager->CheckSubscriberWhetherMatched(subscriberRecord, eventRecord);
1475
1476 // Assert
1477 EXPECT_FALSE(result);
1478 }
1479 }
1480 }