1 /*
2  * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <gtest/gtest.h>
17 #include <numeric>
18 #define private public
19 #include "common_event_manager_service.h"
20 #undef privat
21 #include "ffrt.h"
22 
23 using namespace testing::ext;
24 using namespace OHOS;
25 using namespace OHOS::AppExecFwk;
26 using namespace OHOS::Security::AccessToken;
27 namespace OHOS {
28 namespace EventFwk {
29 extern void MockIsVerfyPermisson(bool isVerify);
30 extern void MockGetTokenTypeFlag(ATokenTypeEnum mockRet);
31 
32 class CommonEventManagerServiceTest : public testing::Test {
33 public:
CommonEventManagerServiceTest()34     CommonEventManagerServiceTest()
35     {}
~CommonEventManagerServiceTest()36     ~CommonEventManagerServiceTest()
37     {}
38 
39     static void SetUpTestCase(void);
40     static void TearDownTestCase(void);
41     void SetUp();
42     void TearDown();
43 };
44 
SetUpTestCase(void)45 void CommonEventManagerServiceTest::SetUpTestCase(void)
46 {}
47 
TearDownTestCase(void)48 void CommonEventManagerServiceTest::TearDownTestCase(void)
49 {}
50 
SetUp(void)51 void CommonEventManagerServiceTest::SetUp(void)
52 {}
53 
TearDown(void)54 void CommonEventManagerServiceTest::TearDown(void)
55 {}
56 
57 /**
58  * @tc.name: CommonEventManagerService_0100
59  * @tc.desc: test IsReady function and handler_ is nullptr.
60  * @tc.type: FUNC
61  */
HWTEST_F(CommonEventManagerServiceTest,CommonEventManagerService_0100,Level1)62 HWTEST_F(CommonEventManagerServiceTest, CommonEventManagerService_0100, Level1)
63 {
64     GTEST_LOG_(INFO) << "CommonEventManagerService_0100 start";
65     std::shared_ptr<CommonEventManagerService> comm = std::make_shared<CommonEventManagerService>();
66     ASSERT_NE(nullptr, comm);
67     comm->innerCommonEventManager_ = std::make_shared<InnerCommonEventManager>();
68     EXPECT_EQ(false, comm->IsReady());
69     GTEST_LOG_(INFO) << "CommonEventManagerService_0100 end";
70 }
71 
72 /**
73  * @tc.name: CommonEventManagerService_0200
74  * @tc.desc: test IsReady function.
75  * @tc.type: FUNC
76  */
HWTEST_F(CommonEventManagerServiceTest,CommonEventManagerService_0200,Level1)77 HWTEST_F(CommonEventManagerServiceTest, CommonEventManagerService_0200, Level1)
78 {
79     GTEST_LOG_(INFO) << "CommonEventManagerService_0200 start";
80     sptr<CommonEventManagerService> comm = new (std::nothrow) CommonEventManagerService();
81     ASSERT_NE(nullptr, comm);
82     // set IsReady is true
83     comm->innerCommonEventManager_ = std::make_shared<InnerCommonEventManager>();
84     comm->commonEventSrvQueue_ = std::make_shared<ffrt::queue>("CesSrvMain");
85     // set VerifyNativeToken is true
86     MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE);
87     // test PublishCommonEvent
88     CommonEventData event;
89     CommonEventPublishInfo publishinfo;
90     uid_t uid = 1;
91     int32_t userId = 100;
92     int32_t callerToken = 0;
93     EXPECT_EQ(false, comm->PublishCommonEvent(event, publishinfo, nullptr, uid, callerToken, userId));
94     GTEST_LOG_(INFO) << "CommonEventManagerService_0200 end";
95 }
96 
97 /**
98  * @tc.name: CommonEventManagerService_0500
99  * @tc.desc: test GetStickyCommonEvent function.
100  * @tc.type: FUNC
101  */
HWTEST_F(CommonEventManagerServiceTest,CommonEventManagerService_0500,Level1)102 HWTEST_F(CommonEventManagerServiceTest, CommonEventManagerService_0500, Level1)
103 {
104     GTEST_LOG_(INFO) << "CommonEventManagerService_0500 start";
105     std::shared_ptr<CommonEventManagerService> comm = std::make_shared<CommonEventManagerService>();
106     ASSERT_NE(nullptr, comm);
107     // set IsReady is true
108     comm->innerCommonEventManager_ = std::make_shared<InnerCommonEventManager>();
109     comm->commonEventSrvQueue_ = std::make_shared<ffrt::queue>("CesSrvMain");
110     // set event is empty
111     std::string event = "";
112     CommonEventData eventData;
113     EXPECT_EQ(false, comm->GetStickyCommonEvent(event, eventData));
114     GTEST_LOG_(INFO) << "CommonEventManagerService_0500 end";
115 }
116 
117 /**
118  * @tc.name: CommonEventManagerService_0600
119  * @tc.desc: test GetStickyCommonEvent function.
120  * @tc.type: FUNC
121  */
HWTEST_F(CommonEventManagerServiceTest,CommonEventManagerService_0600,Level1)122 HWTEST_F(CommonEventManagerServiceTest, CommonEventManagerService_0600, Level1)
123 {
124     GTEST_LOG_(INFO) << "CommonEventManagerService_0600 start";
125     std::shared_ptr<CommonEventManagerService> comm = std::make_shared<CommonEventManagerService>();
126     ASSERT_NE(nullptr, comm);
127     // set IsReady is true
128     comm->innerCommonEventManager_ = std::make_shared<InnerCommonEventManager>();
129     comm->commonEventSrvQueue_ = std::make_shared<ffrt::queue>("CesSrvMain");
130     // set VerifyAccessToken is false
131     MockIsVerfyPermisson(false);
132     // test GetStickyCommonEvent function
133     std::string event = "sticky common event";
134     CommonEventData eventData;
135     EXPECT_EQ(false, comm->GetStickyCommonEvent(event, eventData));
136     GTEST_LOG_(INFO) << "CommonEventManagerService_0600 end";
137 }
138 
139 /**
140  * @tc.name: CommonEventManagerService_0700
141  * @tc.desc: test DumpState function.
142  * @tc.type: FUNC
143  */
HWTEST_F(CommonEventManagerServiceTest,CommonEventManagerService_0700,Level1)144 HWTEST_F(CommonEventManagerServiceTest, CommonEventManagerService_0700, Level1)
145 {
146     GTEST_LOG_(INFO) << "CommonEventManagerService_0700 start";
147     std::shared_ptr<CommonEventManagerService> comm = std::make_shared<CommonEventManagerService>();
148     ASSERT_NE(nullptr, comm);
149     // set VerifyNativeToken is true
150     MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE);
151     // set IsReady is false
152     comm->innerCommonEventManager_ = std::make_shared<InnerCommonEventManager>();
153     // test DumpState function
154     uint8_t dumpType = 1;
155     std::string event = "dump state";
156     int32_t userId = 2;
157     std::vector<std::string> state;
158     EXPECT_EQ(false, comm->DumpState(dumpType, event, userId, state));
159     GTEST_LOG_(INFO) << "CommonEventManagerService_0700 end";
160 }
161 
162 /**
163  * @tc.name: CommonEventManagerService_0800
164  * @tc.desc: test DumpState function.
165  * @tc.type: FUNC
166  */
HWTEST_F(CommonEventManagerServiceTest,CommonEventManagerService_0800,Level1)167 HWTEST_F(CommonEventManagerServiceTest, CommonEventManagerService_0800, Level1)
168 {
169     GTEST_LOG_(INFO) << "CommonEventManagerService_0800 start";
170     std::shared_ptr<CommonEventManagerService> comm = std::make_shared<CommonEventManagerService>();
171     ASSERT_NE(nullptr, comm);
172     // set VerifyNativeToken is true
173     MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE);
174     // set IsReady is true
175     comm->innerCommonEventManager_ = std::make_shared<InnerCommonEventManager>();
176     comm->commonEventSrvQueue_ = std::make_shared<ffrt::queue>("CesSrvMain");
177     // test DumpState function
178     uint8_t dumpType = 1;
179     std::string event = "dump state";
180     int32_t userId = 2;
181     std::vector<std::string> state;
182     EXPECT_EQ(true, comm->DumpState(dumpType, event, userId, state));
183     GTEST_LOG_(INFO) << "CommonEventManagerService_0800 end";
184 }
185 
186 /**
187  * @tc.name: CommonEventManagerService_0900
188  * @tc.desc: test FinishReceiver function.
189  * @tc.type: FUNC
190  */
HWTEST_F(CommonEventManagerServiceTest,CommonEventManagerService_0900,Level1)191 HWTEST_F(CommonEventManagerServiceTest, CommonEventManagerService_0900, Level1)
192 {
193     GTEST_LOG_(INFO) << "CommonEventManagerService_0900 start";
194     std::shared_ptr<CommonEventManagerService> comm = std::make_shared<CommonEventManagerService>();
195     ASSERT_NE(nullptr, comm);
196     // set IsReady is true
197     comm->innerCommonEventManager_ = std::make_shared<InnerCommonEventManager>();
198     comm->commonEventSrvQueue_ = std::make_shared<ffrt::queue>("CesSrvMain");
199     // test FinishReceiver function
200     int32_t code = 1;
201     std::string receiverData = "finish receiver";
202     bool abortEvent = true;
203     EXPECT_EQ(true, comm->FinishReceiver(nullptr, code, receiverData, abortEvent));
204     GTEST_LOG_(INFO) << "CommonEventManagerService_0900 end";
205 }
206 
207 /**
208  * @tc.name: CommonEventManagerService_1000
209  * @tc.desc: test Freeze function and IsReady is false.
210  * @tc.type: FUNC
211  */
HWTEST_F(CommonEventManagerServiceTest,CommonEventManagerService_1000,Level1)212 HWTEST_F(CommonEventManagerServiceTest, CommonEventManagerService_1000, Level1)
213 {
214     GTEST_LOG_(INFO) << "CommonEventManagerService_1000 start";
215     std::shared_ptr<CommonEventManagerService> comm = std::make_shared<CommonEventManagerService>();
216     ASSERT_NE(nullptr, comm);
217     // set VerifyNativeToken is true
218     MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE);
219     uid_t uid = 1;
220     EXPECT_EQ(false, comm->Freeze(uid));
221     GTEST_LOG_(INFO) << "CommonEventManagerService_1000 end";
222 }
223 
224 /**
225  * @tc.name: CommonEventManagerService_1100
226  * @tc.desc: test Freeze function.
227  * @tc.type: FUNC
228  */
HWTEST_F(CommonEventManagerServiceTest,CommonEventManagerService_1100,Level1)229 HWTEST_F(CommonEventManagerServiceTest, CommonEventManagerService_1100, Level1)
230 {
231     GTEST_LOG_(INFO) << "CommonEventManagerService_1100 start";
232     std::shared_ptr<CommonEventManagerService> comm = std::make_shared<CommonEventManagerService>();
233     ASSERT_NE(nullptr, comm);
234     // set VerifyNativeToken is true
235     MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE);
236     // set IsReady is true
237     comm->innerCommonEventManager_ = std::make_shared<InnerCommonEventManager>();
238     comm->commonEventSrvQueue_ = std::make_shared<ffrt::queue>("CesSrvMain");
239     // test Freeze function
240     uid_t uid = 1;
241     EXPECT_EQ(false, comm->Freeze(uid));
242     GTEST_LOG_(INFO) << "CommonEventManagerService_1100 end";
243 }
244 
245 /**
246  * @tc.name: CommonEventManagerService_1200
247  * @tc.desc: test Unfreeze function and IsReady is false.
248  * @tc.type: FUNC
249  */
HWTEST_F(CommonEventManagerServiceTest,CommonEventManagerService_1200,Level1)250 HWTEST_F(CommonEventManagerServiceTest, CommonEventManagerService_1200, Level1)
251 {
252     GTEST_LOG_(INFO) << "CommonEventManagerService_1200 start";
253     std::shared_ptr<CommonEventManagerService> comm = std::make_shared<CommonEventManagerService>();
254     ASSERT_NE(nullptr, comm);
255     // set VerifyNativeToken is true
256     MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE);
257     uid_t uid = 1;
258     EXPECT_EQ(false, comm->Unfreeze(uid));
259     GTEST_LOG_(INFO) << "CommonEventManagerService_1200 end";
260 }
261 
262 /**
263  * @tc.name: CommonEventManagerService_1300
264  * @tc.desc: test Unfreeze function.
265  * @tc.type: FUNC
266  */
HWTEST_F(CommonEventManagerServiceTest,CommonEventManagerService_1300,Level1)267 HWTEST_F(CommonEventManagerServiceTest, CommonEventManagerService_1300, Level1)
268 {
269     GTEST_LOG_(INFO) << "CommonEventManagerService_1300 start";
270     std::shared_ptr<CommonEventManagerService> comm = std::make_shared<CommonEventManagerService>();
271     ASSERT_NE(nullptr, comm);
272     // set VerifyNativeToken is true
273     MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE);
274     // set IsReady is true
275     comm->innerCommonEventManager_ = std::make_shared<InnerCommonEventManager>();
276     comm->commonEventSrvQueue_ = std::make_shared<ffrt::queue>("CesSrvMain");
277     // test Unfreeze function
278     uid_t uid = 1;
279     EXPECT_EQ(false, comm->Unfreeze(uid));
280     GTEST_LOG_(INFO) << "CommonEventManagerService_1300 end";
281 }
282 
283 /**
284  * @tc.name: CommonEventManagerService_1400
285  * @tc.desc: test UnfreezeAll function and IsReady is false.
286  * @tc.type: FUNC
287  */
HWTEST_F(CommonEventManagerServiceTest,CommonEventManagerService_1400,Level1)288 HWTEST_F(CommonEventManagerServiceTest, CommonEventManagerService_1400, Level1)
289 {
290     GTEST_LOG_(INFO) << "CommonEventManagerService_1400 start";
291     std::shared_ptr<CommonEventManagerService> comm = std::make_shared<CommonEventManagerService>();
292     ASSERT_NE(nullptr, comm);
293     // set VerifyNativeToken is true
294     MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE);
295     EXPECT_EQ(false, comm->UnfreezeAll());
296     GTEST_LOG_(INFO) << "CommonEventManagerService_1400 end";
297 }
298 
299 /**
300  * @tc.name: CommonEventManagerService_1500
301  * @tc.desc: test UnfreezeAll function.
302  * @tc.type: FUNC
303  */
HWTEST_F(CommonEventManagerServiceTest,CommonEventManagerService_1500,Level1)304 HWTEST_F(CommonEventManagerServiceTest, CommonEventManagerService_1500, Level1)
305 {
306     GTEST_LOG_(INFO) << "CommonEventManagerService_1500 start";
307     std::shared_ptr<CommonEventManagerService> comm = std::make_shared<CommonEventManagerService>();
308     ASSERT_NE(nullptr, comm);
309     // set VerifyNativeToken is true
310     MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE);
311     // set IsReady is true
312     comm->innerCommonEventManager_ = std::make_shared<InnerCommonEventManager>();
313     comm->commonEventSrvQueue_ = std::make_shared<ffrt::queue>("CesSrvMain");
314     // test UnfreezeAll function
315     EXPECT_EQ(false, comm->UnfreezeAll());
316     GTEST_LOG_(INFO) << "CommonEventManagerService_1500 end";
317 }
318 
319 /**
320  * @tc.name: CommonEventManagerService_1600
321  * @tc.desc: test Dump function.
322  * @tc.type: FUNC
323  */
HWTEST_F(CommonEventManagerServiceTest,CommonEventManagerService_1600,Level1)324 HWTEST_F(CommonEventManagerServiceTest, CommonEventManagerService_1600, Level1)
325 {
326     GTEST_LOG_(INFO) << "CommonEventManagerService_1600 start";
327     std::shared_ptr<CommonEventManagerService> comm = std::make_shared<CommonEventManagerService>();
328     ASSERT_NE(nullptr, comm);
329     // set VerifyNativeToken is true
330     MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE);
331     int fd = 1;
332     std::vector<std::u16string> args;
333     EXPECT_EQ(OHOS::ERR_INVALID_VALUE, comm->Dump(fd, args));
334     GTEST_LOG_(INFO) << "CommonEventManagerService_1600 end";
335 }
336 
337 /**
338  * @tc.name: CommonEventManagerService_1700
339  * @tc.desc: test Dump function.
340  * @tc.type: FUNC
341  */
HWTEST_F(CommonEventManagerServiceTest,CommonEventManagerService_1700,Level1)342 HWTEST_F(CommonEventManagerServiceTest, CommonEventManagerService_1700, Level1)
343 {
344     GTEST_LOG_(INFO) << "CommonEventManagerService_1700 start";
345     std::shared_ptr<CommonEventManagerService> comm = std::make_shared<CommonEventManagerService>();
346     ASSERT_NE(nullptr, comm);
347     // set VerifyNativeToken is true
348     MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE);
349     // set IsReady is true
350     comm->innerCommonEventManager_ = std::make_shared<InnerCommonEventManager>();
351     comm->commonEventSrvQueue_ = std::make_shared<ffrt::queue>("CesSrvMain");
352     // test Dump function.
353     int fd = 1;
354     std::vector<std::u16string> args;
355     EXPECT_EQ(OHOS::ERR_OK, comm->Dump(fd, args));
356     GTEST_LOG_(INFO) << "CommonEventManagerService_1700 end";
357 }
358 
359 /**
360  * @tc.name: CommonEventManagerService_1800
361  * @tc.desc: test SetStaticSubscriberState function.
362  * @tc.type: FUNC
363  */
HWTEST_F(CommonEventManagerServiceTest,CommonEventManagerService_1800,Level1)364 HWTEST_F(CommonEventManagerServiceTest, CommonEventManagerService_1800, Level1)
365 {
366     GTEST_LOG_(INFO) << "CommonEventManagerService_1800 start";
367     std::shared_ptr<CommonEventManagerService> comm = std::make_shared<CommonEventManagerService>();
368     ASSERT_NE(nullptr, comm);
369     // mock ATokenTypeEnum type
370     MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
371     // set IsReady is true
372     comm->innerCommonEventManager_ = std::make_shared<InnerCommonEventManager>();
373     comm->commonEventSrvQueue_ = std::make_shared<ffrt::queue>("CesSrvMain");
374     EXPECT_EQ(OHOS::ERR_INVALID_OPERATION, comm->SetStaticSubscriberState(true));
375     GTEST_LOG_(INFO) << "CommonEventManagerService_1800 end";
376 }
377 }
378 }
379