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 
24 #include "common_event_listener.h"
25 #include "common_event_subscriber.h"
26 #include "common_event_support.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 
34 namespace {
35 constexpr uint8_t PID = 0;
36 constexpr uint16_t SYSTEM_APP_UID = 1000;  // system app
37 constexpr uint32_t NON_SYSTEM_APP_UID = 20010099;  // third party app
38 constexpr uint8_t PUBLISH_SLEEP = 1;
39 }  // namespace
40 
41 static OHOS::sptr<OHOS::IRemoteObject> bundleObject = nullptr;
42 
43 class CommonEventPublishSystemEventTest : public testing::Test {
44 public:
CommonEventPublishSystemEventTest()45     CommonEventPublishSystemEventTest()
46     {}
~CommonEventPublishSystemEventTest()47     ~CommonEventPublishSystemEventTest()
48     {}
49     static void SetUpTestCase(void);
50     static void TearDownTestCase(void);
51     void SetUp();
52     void TearDown();
53 
54 public:
55     InnerCommonEventManager innerCommonEventManager;
56 };
57 
SetUpTestCase(void)58 void CommonEventPublishSystemEventTest::SetUpTestCase(void)
59 {
60     bundleObject = new MockBundleMgrService();
61     OHOS::DelayedSingleton<BundleManagerHelper>::GetInstance()->sptrBundleMgr_ =
62         OHOS::iface_cast<OHOS::AppExecFwk::IBundleMgr>(bundleObject);
63 }
64 
TearDownTestCase(void)65 void CommonEventPublishSystemEventTest::TearDownTestCase(void)
66 {}
67 
SetUp(void)68 void CommonEventPublishSystemEventTest::SetUp(void)
69 {}
70 
TearDown(void)71 void CommonEventPublishSystemEventTest::TearDown(void)
72 {}
73 
74 class SubscriberTest : public CommonEventSubscriber {
75 public:
SubscriberTest(const CommonEventSubscribeInfo & sp)76     explicit SubscriberTest(const CommonEventSubscribeInfo &sp) : CommonEventSubscriber(sp)
77     {}
78 
~SubscriberTest()79     ~SubscriberTest()
80     {}
81 
OnReceiveEvent(const CommonEventData & data)82     virtual void OnReceiveEvent(const CommonEventData &data)
83     {
84         GTEST_LOG_(INFO) << "CESPublishOrderedEventSystmTest::Subscriber OnReceiveEvent ";
85         std::string action = data.GetWant().GetAction();
86         std::string event = GetSubscribeInfo().GetMatchingSkills().GetEvent(0);
87         EXPECT_EQ(action, event);
88     }
89 };
90 
91 /*
92  * @tc.number: CommonEventPublishSystemEventTest_0100
93  * @tc.name: test PublishCommonEvent
94  * @tc.desc: Verify Publish System CommonEvent success
95  */
96 HWTEST_F(CommonEventPublishSystemEventTest, CommonEventPublishSystemEventTest_0100, Function | MediumTest | Level1)
97 {
98     /* Publish */
99     OHOS::Security::AccessToken::AccessTokenID tokenID = 0;
100 
101     // make a want
102     Want want;
103     want.SetAction(CommonEventSupport::COMMON_EVENT_LOCKED_BOOT_COMPLETED);
104 
105     // make common event data
106     CommonEventData data;
107     data.SetWant(want);
108 
109     // make publish info
110     CommonEventPublishInfo publishInfo;
111     publishInfo.SetOrdered(false);
112 
113     struct tm curTime {0};
114     // publish system event
115     bool publishResult = innerCommonEventManager.PublishCommonEvent(
116         data, publishInfo, nullptr, curTime, PID, SYSTEM_APP_UID, tokenID, UNDEFINED_USER, "bundlename");
117     sleep(PUBLISH_SLEEP);
118     EXPECT_TRUE(publishResult);
119 }
120 
121 /*
122  * @tc.number: CommonEventPublishSystemEventTest_0200
123  * @tc.name: test PublishCommonEvent
124  * @tc.desc: Verify Publish System CommonEvent fail because is not systemapp
125  */
126 HWTEST_F(CommonEventPublishSystemEventTest, CommonEventPublishSystemEventTest_0200, Function | MediumTest | Level1)
127 {
128     /* Publish */
129     OHOS::Security::AccessToken::AccessTokenID tokenID = 1;
130 
131     // make a want
132     Want want;
133     want.SetAction(CommonEventSupport::COMMON_EVENT_LOCKED_BOOT_COMPLETED);
134 
135     // make common event data
136     CommonEventData data;
137     data.SetWant(want);
138 
139     // make publish info
140     CommonEventPublishInfo publishInfo;
141     publishInfo.SetOrdered(false);
142 
143     struct tm curTime {0};
144     // publish system event
145     bool publishResult = innerCommonEventManager.PublishCommonEvent(
146         data, publishInfo, nullptr, curTime, PID, NON_SYSTEM_APP_UID, tokenID, UNDEFINED_USER, "bundlename");
147     sleep(PUBLISH_SLEEP);
148     EXPECT_FALSE(publishResult);
149 }
150 
151 /*
152  * @tc.number: CommonEventPublishSystemEventTest_0300
153  * @tc.name: test PublishCommonEvent
154  * @tc.desc: Verify Publish mapped System CommonEvent
155  */
156 HWTEST_F(CommonEventPublishSystemEventTest, CommonEventPublishSystemEventTest_0300, Function | MediumTest | Level1)
157 {
158     /* Subscribe */
159     OHOS::Security::AccessToken::AccessTokenID tokenID = 0;
160 
161     // make subscriber info
162     MatchingSkills matchingSkills;
163     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_TEST_ACTION1);
164     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
165 
166     // make subscriber
167     std::shared_ptr<SubscriberTest> subscriber = std::make_shared<SubscriberTest>(subscribeInfo);
168 
169     // make common event listener
170     OHOS::sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriber);
171     OHOS::sptr<OHOS::IRemoteObject> commonEventListenerPtr(commonEventListener);
172 
173     // SubscribeCommonEvent
174     struct tm curTime{0};
175     InnerCommonEventManager innerCommonEventManager;
176     EXPECT_TRUE(innerCommonEventManager.SubscribeCommonEvent(
177         subscribeInfo, commonEventListenerPtr, curTime, PID, SYSTEM_APP_UID, tokenID, "bundlename"));
178 
179     /* Publish */
180 
181     // make a want
182     Want want;
183     want.SetAction(CommonEventSupport::COMMON_EVENT_TEST_ACTION2);
184 
185     // make common event data
186     CommonEventData data;
187     data.SetWant(want);
188 
189     // make publish info
190     CommonEventPublishInfo publishInfo;
191     publishInfo.SetOrdered(false);
192 
193     // publish system event
194     bool publishResult = innerCommonEventManager.PublishCommonEvent(
195         data, publishInfo, nullptr, curTime, PID, SYSTEM_APP_UID, tokenID, UNDEFINED_USER, "bundlename");
196     sleep(PUBLISH_SLEEP);
197     EXPECT_TRUE(publishResult);
198 
199     innerCommonEventManager.UnsubscribeCommonEvent(commonEventListenerPtr);
200 }
201 
202 /*
203  * @tc.number: CommonEventPublishSystemEventTest_0400
204  * @tc.name: test PublishCommonEvent
205  * @tc.desc: Verify Publish mapped System CommonEvent
206  */
207 HWTEST_F(CommonEventPublishSystemEventTest, CommonEventPublishSystemEventTest_0400, Function | MediumTest | Level1)
208 {
209     /* Subscribe */
210     OHOS::Security::AccessToken::AccessTokenID tokenID = 0;
211 
212     // make subscriber info
213     MatchingSkills matchingSkills;
214     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_TEST_ACTION2);
215     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
216 
217     // make subscriber
218     std::shared_ptr<SubscriberTest> subscriber = std::make_shared<SubscriberTest>(subscribeInfo);
219 
220     // make common event listener
221     OHOS::sptr<CommonEventListener> commonEventListener = new CommonEventListener(subscriber);
222     OHOS::sptr<OHOS::IRemoteObject> commonEventListenerPtr(commonEventListener);
223 
224     // SubscribeCommonEvent
225     struct tm curTime{0};
226     InnerCommonEventManager innerCommonEventManager;
227     EXPECT_TRUE(innerCommonEventManager.SubscribeCommonEvent(
228         subscribeInfo, commonEventListenerPtr, curTime, PID, SYSTEM_APP_UID, tokenID, "bundlename"));
229 
230     /* Publish */
231 
232     // make a want
233     Want want;
234     want.SetAction(CommonEventSupport::COMMON_EVENT_TEST_ACTION1);
235 
236     // make common event data
237     CommonEventData data;
238     data.SetWant(want);
239 
240     // make publish info
241     CommonEventPublishInfo publishInfo;
242     publishInfo.SetOrdered(false);
243 
244     // publish system event
245     bool publishResult = innerCommonEventManager.PublishCommonEvent(
246         data, publishInfo, nullptr, curTime, PID, SYSTEM_APP_UID, tokenID, UNDEFINED_USER, "bundlename");
247     sleep(PUBLISH_SLEEP);
248     EXPECT_TRUE(publishResult);
249 
250     innerCommonEventManager.UnsubscribeCommonEvent(commonEventListenerPtr);
251 }
252