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 bool funcResult = false;
94 comm->PublishCommonEvent(event, publishinfo, nullptr, uid, callerToken, userId, funcResult);
95 EXPECT_EQ(false, funcResult);
96 GTEST_LOG_(INFO) << "CommonEventManagerService_0200 end";
97 }
98
99 /**
100 * @tc.name: CommonEventManagerService_0500
101 * @tc.desc: test GetStickyCommonEvent function.
102 * @tc.type: FUNC
103 */
HWTEST_F(CommonEventManagerServiceTest,CommonEventManagerService_0500,Level1)104 HWTEST_F(CommonEventManagerServiceTest, CommonEventManagerService_0500, Level1)
105 {
106 GTEST_LOG_(INFO) << "CommonEventManagerService_0500 start";
107 std::shared_ptr<CommonEventManagerService> comm = std::make_shared<CommonEventManagerService>();
108 ASSERT_NE(nullptr, comm);
109 // set IsReady is true
110 comm->innerCommonEventManager_ = std::make_shared<InnerCommonEventManager>();
111 comm->commonEventSrvQueue_ = std::make_shared<ffrt::queue>("CesSrvMain");
112 // set event is empty
113 std::string event = "";
114 CommonEventData eventData;
115 bool funcResult = false;
116 comm->GetStickyCommonEvent(event, eventData, funcResult);
117 EXPECT_EQ(false, funcResult);
118 GTEST_LOG_(INFO) << "CommonEventManagerService_0500 end";
119 }
120
121 /**
122 * @tc.name: CommonEventManagerService_0600
123 * @tc.desc: test GetStickyCommonEvent function.
124 * @tc.type: FUNC
125 */
HWTEST_F(CommonEventManagerServiceTest,CommonEventManagerService_0600,Level1)126 HWTEST_F(CommonEventManagerServiceTest, CommonEventManagerService_0600, Level1)
127 {
128 GTEST_LOG_(INFO) << "CommonEventManagerService_0600 start";
129 std::shared_ptr<CommonEventManagerService> comm = std::make_shared<CommonEventManagerService>();
130 ASSERT_NE(nullptr, comm);
131 // set IsReady is true
132 comm->innerCommonEventManager_ = std::make_shared<InnerCommonEventManager>();
133 comm->commonEventSrvQueue_ = std::make_shared<ffrt::queue>("CesSrvMain");
134 // set VerifyAccessToken is false
135 MockIsVerfyPermisson(false);
136 // test GetStickyCommonEvent function
137 std::string event = "sticky common event";
138 CommonEventData eventData;
139 bool funcResult = false;
140 comm->GetStickyCommonEvent(event, eventData, funcResult);
141 EXPECT_EQ(false, funcResult);
142 GTEST_LOG_(INFO) << "CommonEventManagerService_0600 end";
143 }
144
145 /**
146 * @tc.name: CommonEventManagerService_0700
147 * @tc.desc: test DumpState function.
148 * @tc.type: FUNC
149 */
HWTEST_F(CommonEventManagerServiceTest,CommonEventManagerService_0700,Level1)150 HWTEST_F(CommonEventManagerServiceTest, CommonEventManagerService_0700, Level1)
151 {
152 GTEST_LOG_(INFO) << "CommonEventManagerService_0700 start";
153 std::shared_ptr<CommonEventManagerService> comm = std::make_shared<CommonEventManagerService>();
154 ASSERT_NE(nullptr, comm);
155 // set VerifyNativeToken is true
156 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE);
157 // set IsReady is false
158 comm->innerCommonEventManager_ = std::make_shared<InnerCommonEventManager>();
159 // test DumpState function
160 uint8_t dumpType = 1;
161 std::string event = "dump state";
162 int32_t userId = 2;
163 std::vector<std::string> state;
164 bool funcResult = false;
165 comm->DumpState(dumpType, event, userId, state, funcResult);
166 EXPECT_EQ(false, funcResult);
167 GTEST_LOG_(INFO) << "CommonEventManagerService_0700 end";
168 }
169
170 /**
171 * @tc.name: CommonEventManagerService_0800
172 * @tc.desc: test DumpState function.
173 * @tc.type: FUNC
174 */
HWTEST_F(CommonEventManagerServiceTest,CommonEventManagerService_0800,Level1)175 HWTEST_F(CommonEventManagerServiceTest, CommonEventManagerService_0800, Level1)
176 {
177 GTEST_LOG_(INFO) << "CommonEventManagerService_0800 start";
178 std::shared_ptr<CommonEventManagerService> comm = std::make_shared<CommonEventManagerService>();
179 ASSERT_NE(nullptr, comm);
180 // set VerifyNativeToken is true
181 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE);
182 // set IsReady is true
183 comm->innerCommonEventManager_ = std::make_shared<InnerCommonEventManager>();
184 comm->commonEventSrvQueue_ = std::make_shared<ffrt::queue>("CesSrvMain");
185 // test DumpState function
186 uint8_t dumpType = 1;
187 std::string event = "dump state";
188 int32_t userId = 2;
189 std::vector<std::string> state;
190 bool funcResult = false;
191 comm->DumpState(dumpType, event, userId, state, funcResult);
192 EXPECT_EQ(true, funcResult);
193 GTEST_LOG_(INFO) << "CommonEventManagerService_0800 end";
194 }
195
196 /**
197 * @tc.name: CommonEventManagerService_0900
198 * @tc.desc: test FinishReceiver function.
199 * @tc.type: FUNC
200 */
HWTEST_F(CommonEventManagerServiceTest,CommonEventManagerService_0900,Level1)201 HWTEST_F(CommonEventManagerServiceTest, CommonEventManagerService_0900, Level1)
202 {
203 GTEST_LOG_(INFO) << "CommonEventManagerService_0900 start";
204 std::shared_ptr<CommonEventManagerService> comm = std::make_shared<CommonEventManagerService>();
205 ASSERT_NE(nullptr, comm);
206 // set IsReady is true
207 comm->innerCommonEventManager_ = std::make_shared<InnerCommonEventManager>();
208 comm->commonEventSrvQueue_ = std::make_shared<ffrt::queue>("CesSrvMain");
209 // test FinishReceiver function
210 int32_t code = 1;
211 std::string receiverData = "finish receiver";
212 bool abortEvent = true;
213 bool funcResult = false;
214 comm->FinishReceiver(nullptr, code, receiverData, abortEvent, funcResult);
215 EXPECT_EQ(true, funcResult);
216 GTEST_LOG_(INFO) << "CommonEventManagerService_0900 end";
217 }
218
219 /**
220 * @tc.name: CommonEventManagerService_1000
221 * @tc.desc: test Freeze function and IsReady is false.
222 * @tc.type: FUNC
223 */
HWTEST_F(CommonEventManagerServiceTest,CommonEventManagerService_1000,Level1)224 HWTEST_F(CommonEventManagerServiceTest, CommonEventManagerService_1000, Level1)
225 {
226 GTEST_LOG_(INFO) << "CommonEventManagerService_1000 start";
227 std::shared_ptr<CommonEventManagerService> comm = std::make_shared<CommonEventManagerService>();
228 ASSERT_NE(nullptr, comm);
229 // set VerifyNativeToken is true
230 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE);
231 uid_t uid = 1;
232 bool funcResult = false;
233 comm->Freeze(uid, funcResult);
234 EXPECT_EQ(false, funcResult);
235 GTEST_LOG_(INFO) << "CommonEventManagerService_1000 end";
236 }
237
238 /**
239 * @tc.name: CommonEventManagerService_1100
240 * @tc.desc: test Freeze function.
241 * @tc.type: FUNC
242 */
HWTEST_F(CommonEventManagerServiceTest,CommonEventManagerService_1100,Level1)243 HWTEST_F(CommonEventManagerServiceTest, CommonEventManagerService_1100, Level1)
244 {
245 GTEST_LOG_(INFO) << "CommonEventManagerService_1100 start";
246 std::shared_ptr<CommonEventManagerService> comm = std::make_shared<CommonEventManagerService>();
247 ASSERT_NE(nullptr, comm);
248 // set VerifyNativeToken is true
249 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE);
250 // set IsReady is true
251 comm->innerCommonEventManager_ = std::make_shared<InnerCommonEventManager>();
252 comm->commonEventSrvQueue_ = std::make_shared<ffrt::queue>("CesSrvMain");
253 // test Freeze function
254 uid_t uid = 1;
255 bool funcResult = false;
256 comm->Freeze(uid, funcResult);
257 EXPECT_EQ(false, funcResult);
258 GTEST_LOG_(INFO) << "CommonEventManagerService_1100 end";
259 }
260
261 /**
262 * @tc.name: CommonEventManagerService_1200
263 * @tc.desc: test Unfreeze function and IsReady is false.
264 * @tc.type: FUNC
265 */
HWTEST_F(CommonEventManagerServiceTest,CommonEventManagerService_1200,Level1)266 HWTEST_F(CommonEventManagerServiceTest, CommonEventManagerService_1200, Level1)
267 {
268 GTEST_LOG_(INFO) << "CommonEventManagerService_1200 start";
269 std::shared_ptr<CommonEventManagerService> comm = std::make_shared<CommonEventManagerService>();
270 ASSERT_NE(nullptr, comm);
271 // set VerifyNativeToken is true
272 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE);
273 uid_t uid = 1;
274 bool funcResult = false;
275 comm->Unfreeze(uid, funcResult);
276 EXPECT_EQ(false, funcResult);
277 GTEST_LOG_(INFO) << "CommonEventManagerService_1200 end";
278 }
279
280 /**
281 * @tc.name: CommonEventManagerService_1300
282 * @tc.desc: test Unfreeze function.
283 * @tc.type: FUNC
284 */
HWTEST_F(CommonEventManagerServiceTest,CommonEventManagerService_1300,Level1)285 HWTEST_F(CommonEventManagerServiceTest, CommonEventManagerService_1300, Level1)
286 {
287 GTEST_LOG_(INFO) << "CommonEventManagerService_1300 start";
288 std::shared_ptr<CommonEventManagerService> comm = std::make_shared<CommonEventManagerService>();
289 ASSERT_NE(nullptr, comm);
290 // set VerifyNativeToken is true
291 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE);
292 // set IsReady is true
293 comm->innerCommonEventManager_ = std::make_shared<InnerCommonEventManager>();
294 comm->commonEventSrvQueue_ = std::make_shared<ffrt::queue>("CesSrvMain");
295 // test Unfreeze function
296 uid_t uid = 1;
297 bool funcResult = false;
298 comm->Unfreeze(uid, funcResult);
299 EXPECT_EQ(false, funcResult);
300 GTEST_LOG_(INFO) << "CommonEventManagerService_1300 end";
301 }
302
303 /**
304 * @tc.name: CommonEventManagerService_1400
305 * @tc.desc: test UnfreezeAll function and IsReady is false.
306 * @tc.type: FUNC
307 */
HWTEST_F(CommonEventManagerServiceTest,CommonEventManagerService_1400,Level1)308 HWTEST_F(CommonEventManagerServiceTest, CommonEventManagerService_1400, Level1)
309 {
310 GTEST_LOG_(INFO) << "CommonEventManagerService_1400 start";
311 std::shared_ptr<CommonEventManagerService> comm = std::make_shared<CommonEventManagerService>();
312 ASSERT_NE(nullptr, comm);
313 // set VerifyNativeToken is true
314 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE);
315 bool funcResult = false;
316 comm->UnfreezeAll(funcResult);
317 EXPECT_EQ(false, funcResult);
318 GTEST_LOG_(INFO) << "CommonEventManagerService_1400 end";
319 }
320
321 /**
322 * @tc.name: CommonEventManagerService_1500
323 * @tc.desc: test UnfreezeAll function.
324 * @tc.type: FUNC
325 */
HWTEST_F(CommonEventManagerServiceTest,CommonEventManagerService_1500,Level1)326 HWTEST_F(CommonEventManagerServiceTest, CommonEventManagerService_1500, Level1)
327 {
328 GTEST_LOG_(INFO) << "CommonEventManagerService_1500 start";
329 std::shared_ptr<CommonEventManagerService> comm = std::make_shared<CommonEventManagerService>();
330 ASSERT_NE(nullptr, comm);
331 // set VerifyNativeToken is true
332 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE);
333 // set IsReady is true
334 comm->innerCommonEventManager_ = std::make_shared<InnerCommonEventManager>();
335 comm->commonEventSrvQueue_ = std::make_shared<ffrt::queue>("CesSrvMain");
336 // test UnfreezeAll function
337 bool funcResult = false;
338 comm->UnfreezeAll(funcResult);
339 EXPECT_EQ(false, funcResult);
340 GTEST_LOG_(INFO) << "CommonEventManagerService_1500 end";
341 }
342
343 /**
344 * @tc.name: CommonEventManagerService_1600
345 * @tc.desc: test Dump function.
346 * @tc.type: FUNC
347 */
HWTEST_F(CommonEventManagerServiceTest,CommonEventManagerService_1600,Level1)348 HWTEST_F(CommonEventManagerServiceTest, CommonEventManagerService_1600, Level1)
349 {
350 GTEST_LOG_(INFO) << "CommonEventManagerService_1600 start";
351 std::shared_ptr<CommonEventManagerService> comm = std::make_shared<CommonEventManagerService>();
352 ASSERT_NE(nullptr, comm);
353 // set VerifyNativeToken is true
354 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE);
355 int fd = 1;
356 std::vector<std::u16string> args;
357 EXPECT_EQ(OHOS::ERR_INVALID_VALUE, comm->Dump(fd, args));
358 GTEST_LOG_(INFO) << "CommonEventManagerService_1600 end";
359 }
360
361 /**
362 * @tc.name: CommonEventManagerService_1700
363 * @tc.desc: test Dump function.
364 * @tc.type: FUNC
365 */
HWTEST_F(CommonEventManagerServiceTest,CommonEventManagerService_1700,Level1)366 HWTEST_F(CommonEventManagerServiceTest, CommonEventManagerService_1700, Level1)
367 {
368 GTEST_LOG_(INFO) << "CommonEventManagerService_1700 start";
369 std::shared_ptr<CommonEventManagerService> comm = std::make_shared<CommonEventManagerService>();
370 ASSERT_NE(nullptr, comm);
371 // set VerifyNativeToken is true
372 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE);
373 // set IsReady is true
374 comm->innerCommonEventManager_ = std::make_shared<InnerCommonEventManager>();
375 comm->commonEventSrvQueue_ = std::make_shared<ffrt::queue>("CesSrvMain");
376 // test Dump function.
377 int fd = 1;
378 std::vector<std::u16string> args;
379 EXPECT_EQ(OHOS::ERR_OK, comm->Dump(fd, args));
380 GTEST_LOG_(INFO) << "CommonEventManagerService_1700 end";
381 }
382
383 /**
384 * @tc.name: CommonEventManagerService_1800
385 * @tc.desc: test SetStaticSubscriberState function.
386 * @tc.type: FUNC
387 */
HWTEST_F(CommonEventManagerServiceTest,CommonEventManagerService_1800,Level1)388 HWTEST_F(CommonEventManagerServiceTest, CommonEventManagerService_1800, Level1)
389 {
390 GTEST_LOG_(INFO) << "CommonEventManagerService_1800 start";
391 std::shared_ptr<CommonEventManagerService> comm = std::make_shared<CommonEventManagerService>();
392 ASSERT_NE(nullptr, comm);
393 // mock ATokenTypeEnum type
394 MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
395 // set IsReady is true
396 comm->innerCommonEventManager_ = std::make_shared<InnerCommonEventManager>();
397 comm->commonEventSrvQueue_ = std::make_shared<ffrt::queue>("CesSrvMain");
398 int32_t funcResult = -1;
399 comm->SetStaticSubscriberState(true, funcResult);
400 EXPECT_EQ(OHOS::ERR_INVALID_OPERATION, funcResult);
401 GTEST_LOG_(INFO) << "CommonEventManagerService_1800 end";
402 }
403 }
404 }
405