• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 
18 #define private public
19 #define protected public
20 #include "bundle_manager_helper.h"
21 #undef private
22 #undef protected
23 #define UNIT_TEST
24 #include "common_event_constant.h"
25 #include "common_event_listener.h"
26 #include "datetime_ex.h"
27 #include "inner_common_event_manager.h"
28 #include "mock_bundle_manager.h"
29 
30 using namespace testing::ext;
31 using namespace OHOS::EventFwk;
32 using namespace OHOS::AppExecFwk;
33 namespace {
34 const std::string EVENTCASE1 = "com.ces.test.event.case1";
35 const std::string EVENTCASE2 = "com.ces.test.event.case2";
36 const std::string EVENTCASE3 = "com.ces.test.event.case3";
37 const std::string EVENTCASE4 = "com.ces.test.event.case4";
38 const std::string EVENTCASE5 = "com.ces.test.event.case5";
39 const std::string EVENTCASE6 = "com.ces.test.event.case6";
40 const std::string EVENTCASE7 = "com.ces.test.event.case7";
41 const std::string INNITDATA = "com.ces.test.initdata";
42 const std::string CHANGEDATA = "com.ces.test.changedata";
43 const std::string CHANGEDATA2 = "com.ces.test.changedata2";
44 constexpr uint8_t INNITCODE = 0;
45 constexpr uint8_t CHANGECODE = 1;
46 constexpr uint8_t CHANGECODE2 = 2;
47 constexpr uint8_t PID = 0;
48 constexpr uint16_t SYSTEM_UID = 1000;
49 constexpr uid_t UID = 1;
50 constexpr uid_t UID2 = 2;
51 constexpr uint8_t FREEZE_SLEEP = 1;
52 constexpr uint8_t FREEZE_SLEEP2 = 12;
53 bool isFreeze_uid = false;
54 bool isFreeze_uid2 = false;
55 std::mutex mtx;
56 
57 static OHOS::sptr<OHOS::IRemoteObject> bundleObject = nullptr;
58 OHOS::sptr<OHOS::IRemoteObject> commonEventListener;
59 OHOS::sptr<OHOS::IRemoteObject> commonEventListener2;
60 OHOS::sptr<OHOS::IRemoteObject> commonEventListener3;
61 class CommonEventFreezeTest : public testing::Test {
62 public:
CommonEventFreezeTest()63     CommonEventFreezeTest()
64     {}
~CommonEventFreezeTest()65     ~CommonEventFreezeTest()
66     {}
67     static void SetUpTestCase(void);
68     static void TearDownTestCase(void);
69     void SetUp();
70     void TearDown();
71     bool SubscribeCommonEvent(const std::shared_ptr<CommonEventSubscriber> &subscriber, uid_t callingUid,
72         OHOS::sptr<OHOS::IRemoteObject> &commonEventListener);
73     bool PublishCommonEvent(const CommonEventData &data, const CommonEventPublishInfo &publishInfo,
74         const std::shared_ptr<CommonEventSubscriber> &subscriber, OHOS::sptr<OHOS::IRemoteObject> &commonEventListener);
75     static bool FinishReceiver(
76         const OHOS::sptr<OHOS::IRemoteObject> &proxy, const int &code, const std::string &data, const bool &abortEvent);
77     bool Freeze(const uid_t &uid);
78     bool Unfreeze(const uid_t &uid);
79     void AsyncProcess();
80 
81 private:
82     std::shared_ptr<EventRunner> runner_;
83     static std::shared_ptr<EventHandler> handler_;
84     static std::shared_ptr<InnerCommonEventManager> innerCommonEventManager_;
85 };
86 
87 class SubscriberTest : public CommonEventSubscriber {
88 public:
SubscriberTest(const CommonEventSubscribeInfo & sp)89     explicit SubscriberTest(const CommonEventSubscribeInfo &sp) : CommonEventSubscriber(sp)
90     {
91         handler_ = std::make_shared<EventHandler>(EventRunner::Create());
92     }
93 
~SubscriberTest()94     ~SubscriberTest()
95     {}
96 
OnReceiveEvent(const CommonEventData & data)97     virtual void OnReceiveEvent(const CommonEventData &data)
98     {
99         std::string action = data.GetWant().GetAction();
100         if (action == EVENTCASE1) {
101             ProcessSubscriberTestCase1(data);
102         } else if (action == EVENTCASE2) {
103             ProcessSubscriberTestCase2(data);
104         } else {
105         }
106     }
107 
108 private:
ProcessSubscriberTestCase1(CommonEventData data)109     void ProcessSubscriberTestCase1(CommonEventData data)
110     {
111         GTEST_LOG_(INFO) << "Subscriber1: Type:  " << data.GetWant().GetType();
112         if (!IsOrderedCommonEvent()) {
113             return;
114         }
115         EXPECT_EQ(INNITCODE, data.GetCode());
116         EXPECT_EQ(INNITDATA, data.GetData());
117         std::shared_ptr<AsyncCommonEventResult> result = GoAsyncCommonEvent();
118         std::function<void()> asyncProcessFunc = std::bind(&SubscriberTest::AsyncProcess, this, commonEventListener);
119         handler_->PostTask(asyncProcessFunc);
120     }
ProcessSubscriberTestCase2(CommonEventData data)121     void ProcessSubscriberTestCase2(CommonEventData data)
122     {
123         GTEST_LOG_(INFO) << "Subscriber1: Type:  " << data.GetWant().GetType();
124         EXPECT_EQ(INNITCODE, data.GetCode());
125         EXPECT_EQ(INNITDATA, data.GetData());
126     }
127 
AsyncProcess(OHOS::sptr<OHOS::IRemoteObject> commonEventListener)128     void AsyncProcess(OHOS::sptr<OHOS::IRemoteObject> commonEventListener)
129     {
130         EXPECT_TRUE(CommonEventFreezeTest::FinishReceiver(commonEventListener, CHANGECODE, CHANGEDATA, false));
131     }
132 
133 private:
134     std::shared_ptr<EventHandler> handler_;
135 };
136 
137 class SubscriberTest2 : public CommonEventSubscriber {
138 public:
SubscriberTest2(const CommonEventSubscribeInfo & sp)139     explicit SubscriberTest2(const CommonEventSubscribeInfo &sp) : CommonEventSubscriber(sp)
140     {
141         handler_ = std::make_shared<EventHandler>(EventRunner::Create());
142     }
143 
~SubscriberTest2()144     ~SubscriberTest2()
145     {}
146 
OnReceiveEvent(const CommonEventData & data)147     virtual void OnReceiveEvent(const CommonEventData &data)
148     {
149         std::string action = data.GetWant().GetAction();
150         if (action == EVENTCASE1) {
151             ProcessSubscriberTest2Case1(data);
152         } else if (action == EVENTCASE2) {
153             ProcessSubscriberTest2Case2(data);
154         } else {
155         }
156     }
157 
158 private:
ProcessSubscriberTest2Case1(CommonEventData data)159     void ProcessSubscriberTest2Case1(CommonEventData data)
160     {
161         GTEST_LOG_(INFO) << "Subscriber2: Type:  " << data.GetWant().GetType();
162         if (!IsOrderedCommonEvent()) {
163             return;
164         }
165         if (!isFreeze_uid) {
166             EXPECT_EQ(CHANGECODE, data.GetCode());
167             EXPECT_EQ(CHANGEDATA, data.GetData());
168         } else {
169             EXPECT_EQ(INNITCODE, data.GetCode());
170             EXPECT_EQ(INNITDATA, data.GetData());
171         }
172         std::shared_ptr<AsyncCommonEventResult> result = GoAsyncCommonEvent();
173         std::function<void()> asyncProcessFunc = std::bind(&SubscriberTest2::AsyncProcess, this, commonEventListener2);
174         handler_->PostTask(asyncProcessFunc);
175     }
ProcessSubscriberTest2Case2(CommonEventData data)176     void ProcessSubscriberTest2Case2(CommonEventData data)
177     {
178         GTEST_LOG_(INFO) << "Subscriber2: Type:  " << data.GetWant().GetType();
179         EXPECT_EQ(INNITCODE, data.GetCode());
180         EXPECT_EQ(INNITDATA, data.GetData());
181     }
182 
AsyncProcess(OHOS::sptr<OHOS::IRemoteObject> commonEventListener)183     void AsyncProcess(OHOS::sptr<OHOS::IRemoteObject> commonEventListener)
184     {
185         EXPECT_TRUE(CommonEventFreezeTest::FinishReceiver(commonEventListener, CHANGECODE2, CHANGEDATA2, false));
186     }
187 
188 private:
189     std::shared_ptr<EventHandler> handler_;
190 };
191 
192 class SubscriberTestLast : public CommonEventSubscriber {
193 public:
SubscriberTestLast()194     SubscriberTestLast() : CommonEventSubscriber()
195     {
196         handler_ = std::make_shared<EventHandler>(EventRunner::Create());
197     }
198 
SubscriberTestLast(const CommonEventSubscribeInfo & sp)199     explicit SubscriberTestLast(const CommonEventSubscribeInfo &sp) : CommonEventSubscriber(sp)
200     {
201         handler_ = std::make_shared<EventHandler>(EventRunner::Create());
202     }
203 
~SubscriberTestLast()204     ~SubscriberTestLast()
205     {}
206 
OnReceiveEvent(const CommonEventData & data)207     virtual void OnReceiveEvent(const CommonEventData &data)
208     {
209         std::string action = data.GetWant().GetAction();
210         if (action == EVENTCASE1) {
211             ProcessSubscriberTestLastCase1(data);
212         } else if (action == EVENTCASE2) {
213             ProcessSubscriberTestLastCase2(data);
214         } else {
215         }
216     }
217 
218 private:
ProcessSubscriberTestLastCase1(CommonEventData data)219     void ProcessSubscriberTestLastCase1(CommonEventData data)
220     {
221         GTEST_LOG_(INFO) << "SubscriberLast: Type:  " << data.GetWant().GetType();
222         if (!isFreeze_uid2) {
223             EXPECT_EQ(CHANGECODE2, data.GetCode());
224             EXPECT_EQ(CHANGEDATA2, data.GetData());
225         } else {
226             if (!isFreeze_uid) {
227                 EXPECT_EQ(CHANGECODE, data.GetCode());
228                 EXPECT_EQ(CHANGEDATA, data.GetData());
229             } else {
230                 EXPECT_EQ(INNITCODE, data.GetCode());
231                 EXPECT_EQ(INNITDATA, data.GetData());
232             }
233         }
234         std::shared_ptr<AsyncCommonEventResult> result = GoAsyncCommonEvent();
235         std::function<void()> asyncProcessFunc =
236             std::bind(&SubscriberTestLast::AsyncProcess, this, commonEventListener3);
237         handler_->PostTask(asyncProcessFunc);
238     }
ProcessSubscriberTestLastCase2(CommonEventData data)239     void ProcessSubscriberTestLastCase2(CommonEventData data)
240     {}
241 
AsyncProcess(OHOS::sptr<OHOS::IRemoteObject> commonEventListener)242     void AsyncProcess(OHOS::sptr<OHOS::IRemoteObject> commonEventListener)
243     {
244         EXPECT_TRUE(CommonEventFreezeTest::FinishReceiver(commonEventListener, CHANGECODE2, CHANGEDATA2, false));
245     }
246 
247 private:
248     std::shared_ptr<EventHandler> handler_;
249 };
250 
251 std::shared_ptr<EventHandler> CommonEventFreezeTest::handler_ = nullptr;
252 std::shared_ptr<InnerCommonEventManager> CommonEventFreezeTest::innerCommonEventManager_ = nullptr;
253 
SetUpTestCase(void)254 void CommonEventFreezeTest::SetUpTestCase(void)
255 {
256     bundleObject = new MockBundleMgrService();
257     OHOS::DelayedSingleton<BundleManagerHelper>::GetInstance()->sptrBundleMgr_ =
258         OHOS::iface_cast<OHOS::AppExecFwk::IBundleMgr>(bundleObject);
259 }
260 
TearDownTestCase(void)261 void CommonEventFreezeTest::TearDownTestCase(void)
262 {}
263 
SetUp(void)264 void CommonEventFreezeTest::SetUp(void)
265 {
266     runner_ = EventRunner::Create(true);
267     if (!runner_) {
268         return;
269     }
270     handler_ = std::make_shared<EventHandler>(runner_);
271     innerCommonEventManager_ = std::make_shared<InnerCommonEventManager>();
272 }
273 
TearDown(void)274 void CommonEventFreezeTest::TearDown(void)
275 {}
276 
SubscribeCommonEvent(const std::shared_ptr<CommonEventSubscriber> & subscriber,uid_t callingUid,OHOS::sptr<OHOS::IRemoteObject> & commonEventListener)277 bool CommonEventFreezeTest::SubscribeCommonEvent(const std::shared_ptr<CommonEventSubscriber> &subscriber,
278     uid_t callingUid, OHOS::sptr<OHOS::IRemoteObject> &commonEventListener)
279 {
280     OHOS::sptr<IEventReceive> listener = new CommonEventListener(subscriber);
281     if (!listener) {
282         return false;
283     }
284     commonEventListener = listener->AsObject();
285     struct tm recordTime = {0};
286     if (!OHOS::GetSystemCurrentTime(&recordTime)) {
287         return false;
288     }
289     pid_t callingPid = 0;
290     OHOS::Security::AccessToken::AccessTokenID tokenID = 0;
291 
292     std::string bundleName = "";
293 
294     std::function<void()> SubscribeCommonEventFunc = std::bind(&InnerCommonEventManager::SubscribeCommonEvent,
295         innerCommonEventManager_,
296         subscriber->GetSubscribeInfo(),
297         commonEventListener,
298         recordTime,
299         callingPid,
300         callingUid,
301         tokenID,
302         bundleName);
303     return handler_->PostTask(SubscribeCommonEventFunc);
304 }
305 
PublishCommonEvent(const CommonEventData & data,const CommonEventPublishInfo & publishInfo,const std::shared_ptr<CommonEventSubscriber> & subscriber,OHOS::sptr<OHOS::IRemoteObject> & commonEventListener)306 bool CommonEventFreezeTest::PublishCommonEvent(const CommonEventData &data, const CommonEventPublishInfo &publishInfo,
307     const std::shared_ptr<CommonEventSubscriber> &subscriber, OHOS::sptr<OHOS::IRemoteObject> &commonEventListener)
308 {
309     if (commonEventListener == nullptr && publishInfo.IsOrdered()) {
310         OHOS::sptr<IEventReceive> listener = new CommonEventListener(subscriber);
311         if (!listener) {
312             return false;
313         }
314         commonEventListener = listener->AsObject();
315     } else if (!publishInfo.IsOrdered()) {
316         commonEventListener = nullptr;
317     }
318 
319     struct tm recordTime = {0};
320     if (!OHOS::GetSystemCurrentTime(&recordTime)) {
321         return false;
322     }
323     OHOS::Security::AccessToken::AccessTokenID tokenID = 0;
324     int32_t userId = UNDEFINED_USER;
325     std::string bundleName = "";
326 
327     std::function<void()> PublishCommonEventFunc = std::bind(&InnerCommonEventManager::PublishCommonEvent,
328         innerCommonEventManager_,
329         data,
330         publishInfo,
331         commonEventListener,
332         recordTime,
333         PID,
334         SYSTEM_UID,
335         tokenID,
336         userId,
337         bundleName,
338         nullptr);
339     return handler_->PostTask(PublishCommonEventFunc);
340 }
341 
FinishReceiver(const OHOS::sptr<OHOS::IRemoteObject> & proxy,const int & code,const std::string & data,const bool & abortEvent)342 bool CommonEventFreezeTest::FinishReceiver(
343     const OHOS::sptr<OHOS::IRemoteObject> &proxy, const int &code, const std::string &data, const bool &abortEvent)
344 {
345     std::function<void()> FinishReceiverFunc =
346         std::bind(&InnerCommonEventManager::FinishReceiver, innerCommonEventManager_, proxy, code, data, abortEvent);
347     return handler_->PostTask(FinishReceiverFunc);
348 }
349 
Freeze(const uid_t & uid)350 bool CommonEventFreezeTest::Freeze(const uid_t &uid)
351 {
352     std::function<void()> FreezeFunc = std::bind(&InnerCommonEventManager::Freeze, innerCommonEventManager_, uid);
353     return handler_->PostImmediateTask(FreezeFunc);
354 }
355 
Unfreeze(const uid_t & uid)356 bool CommonEventFreezeTest::Unfreeze(const uid_t &uid)
357 {
358     std::function<void()> UnfreezeFunc = std::bind(&InnerCommonEventManager::Unfreeze, innerCommonEventManager_, uid);
359     return handler_->PostImmediateTask(UnfreezeFunc);
360 }
361 
AsyncProcess()362 void CommonEventFreezeTest::AsyncProcess()
363 {
364     isFreeze_uid = false;
365     isFreeze_uid2 = false;
366     sleep(FREEZE_SLEEP);
367     GTEST_LOG_(INFO) << "Subscriber1 Freeze";
368     Freeze(UID);
369     GTEST_LOG_(INFO) << "Subscriber2 Freeze";
370     Freeze(UID2);
371     isFreeze_uid = true;
372     isFreeze_uid2 = true;
373     sleep(FREEZE_SLEEP);
374     GTEST_LOG_(INFO) << "Subscriber1 Unfreeze";
375     Unfreeze(UID);
376     isFreeze_uid = false;
377     sleep(FREEZE_SLEEP2);
378     GTEST_LOG_(INFO) << "Subscriber2 Unfreeze";
379     Unfreeze(UID2);
380     isFreeze_uid2 = false;
381     sleep(FREEZE_SLEEP);
382     mtx.unlock();
383 }
384 
385 HWTEST_F(CommonEventFreezeTest, CommonEventFreezeTest_001, TestSize.Level1)
386 {
387     /* Subscribe */
388     // make matching skills
389     MatchingSkills matchingSkills;
390     matchingSkills.AddEvent(EVENTCASE1);
391 
392     // make subscriber info
393     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
394 
395     // make a subscriber object
396     std::shared_ptr<SubscriberTest> subscriberTest = std::make_shared<SubscriberTest>(subscribeInfo);
397 
398     // subscribe a common event
399     EXPECT_TRUE(SubscribeCommonEvent(subscriberTest, UID, commonEventListener));
400 
401     // make another matching skills
402     MatchingSkills matchingSkillsAnother;
403     matchingSkillsAnother.AddEvent(EVENTCASE1);
404 
405     // make another subscriber info
406     CommonEventSubscribeInfo subscribeInfo2(matchingSkillsAnother);
407 
408     // make another subscriber object
409     std::shared_ptr<SubscriberTest2> subscriberTest2 = std::make_shared<SubscriberTest2>(subscribeInfo2);
410 
411     // subscribe another event
412     EXPECT_TRUE(SubscribeCommonEvent(subscriberTest2, UID2, commonEventListener2));
413 
414     mtx.lock();
415     auto handler = std::make_shared<EventHandler>(EventRunner::Create());
416     std::function<void()> asyncProcessFunc = std::bind(&CommonEventFreezeTest::AsyncProcess, this);
417     handler->PostTask(asyncProcessFunc);
418 
419     std::shared_ptr<SubscriberTestLast> subscriber = std::make_shared<SubscriberTestLast>();
420     /* Publish */
421     int i = 0;
422     while (!mtx.try_lock()) {
423         // make a want
424         Want want;
425         want.SetAction(EVENTCASE1);
426         i++;
427         want.SetType(std::to_string(i));
428         GTEST_LOG_(INFO) << "PublishCommonEvent: Type:  " << std::to_string(i);
429         // make common event data
430         CommonEventData data;
431         data.SetWant(want);
432         data.SetData(INNITDATA);
433         data.SetCode(INNITCODE);
434         CommonEventPublishInfo publishInfo;
435         publishInfo.SetOrdered(true);
436 
437         usleep(100000);
438 
439         // publish order event
440         EXPECT_TRUE(PublishCommonEvent(data, publishInfo, subscriber, commonEventListener3));
441     }
442 
443     usleep(100000);
444     mtx.unlock();
445 }
446 
447 HWTEST_F(CommonEventFreezeTest, CommonEventFreezeTest_002, TestSize.Level1)
448 {
449     /* Subscribe */
450     // make matching skills
451     MatchingSkills matchingSkills;
452     matchingSkills.AddEvent(EVENTCASE2);
453 
454     // make subscriber info
455     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
456 
457     // make a subscriber object
458     std::shared_ptr<SubscriberTest> subscriberTest = std::make_shared<SubscriberTest>(subscribeInfo);
459 
460     // subscribe a common event
461     EXPECT_TRUE(SubscribeCommonEvent(subscriberTest, UID, commonEventListener));
462 
463     // make another matching skills
464     MatchingSkills matchingSkillsAnother;
465     matchingSkillsAnother.AddEvent(EVENTCASE2);
466 
467     // make another subscriber info
468     CommonEventSubscribeInfo subscribeInfo2(matchingSkillsAnother);
469 
470     // make another subscriber object
471     std::shared_ptr<SubscriberTest2> subscriberTest2 = std::make_shared<SubscriberTest2>(subscribeInfo2);
472 
473     // subscribe another event
474     EXPECT_TRUE(SubscribeCommonEvent(subscriberTest2, UID2, commonEventListener2));
475 
476     mtx.lock();
477     auto handler = std::make_shared<EventHandler>(EventRunner::Create());
478     std::function<void()> asyncProcessFunc = std::bind(&CommonEventFreezeTest::AsyncProcess, this);
479     handler->PostTask(asyncProcessFunc);
480 
481     std::shared_ptr<SubscriberTestLast> subscriber = nullptr;
482     /* Publish */
483     int i = 0;
484     while (!mtx.try_lock()) {
485         // make a want
486         Want want;
487         want.SetAction(EVENTCASE2);
488         i++;
489         want.SetType(std::to_string(i));
490         GTEST_LOG_(INFO) << "PublishCommonEvent: Type:  " << std::to_string(i);
491         // make common event data
492         CommonEventData data;
493         data.SetWant(want);
494         data.SetData(INNITDATA);
495         data.SetCode(INNITCODE);
496         CommonEventPublishInfo publishInfo;
497         usleep(100000);
498 
499         // publish order event
500         EXPECT_TRUE(PublishCommonEvent(data, publishInfo, subscriber, commonEventListener3));
501     }
502 
503     usleep(100000);
504     mtx.unlock();
505 }
506 }  // namespace
507