• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 #include <iostream>
16 #include <unistd.h>
17 
18 #include <gtest/gtest.h>
19 
20 #include "app_event_processor_mgr.h"
21 #include "app_event_observer_mgr.h"
22 #include "application_context.h"
23 #include "hiappevent_base.h"
24 #include "hiappevent_config.h"
25 #include "hiappevent_test_common.h"
26 
27 using namespace testing::ext;
28 using namespace OHOS::HiviewDFX;
29 using namespace OHOS::HiviewDFX::ErrorCode;
30 using namespace OHOS::HiviewDFX::HiAppEvent;
31 namespace {
32 const std::string TEST_PROCESSOR_NAME = "test_processor";
33 const std::string TEST_EVENT_DOMAIN = "test_domain";
34 const std::string TEST_EVENT_NAME = "test_name";
35 constexpr int TEST_EVENT_TYPE = 1;
36 constexpr int32_t TEST_UID = 200000 * 100;
37 
WriteEventOnce()38 void WriteEventOnce()
39 {
40     auto event = std::make_shared<AppEventPack>(TEST_EVENT_DOMAIN, TEST_EVENT_NAME, TEST_EVENT_TYPE);
41     constexpr int testInt = 1;
42     event->AddParam("int_key", testInt);
43     constexpr double testDou = 1.2;
44     event->AddParam("double_key", testDou);
45     constexpr bool testBool = false;
46     event->AddParam("bool_key", testBool);
47     const std::string testStr = "str";
48     event->AddParam("str_key", testStr);
49     std::vector<std::shared_ptr<AppEventPack>> events;
50     events.emplace_back(event);
51     AppEventObserverMgr::GetInstance().HandleEvents(events);
52     sleep(1); // 1s
53 }
54 
CheckRegisterObserver(const std::string & observer,std::shared_ptr<AppEventProcessor> processor,int64_t & processorSeq)55 void CheckRegisterObserver(const std::string& observer,
56     std::shared_ptr<AppEventProcessor> processor, int64_t& processorSeq)
57 {
58     ASSERT_EQ(AppEventProcessorMgr::RegisterProcessor(observer, processor), 0);
59     processorSeq = AppEventObserverMgr::GetInstance().RegisterObserver(observer);
60     ASSERT_GT(processorSeq, 0);
61 }
62 
CheckRegisterObserverWithConfig(const std::string & observer,std::shared_ptr<AppEventProcessor> processor,const ReportConfig & config,int64_t & processorSeq)63 void CheckRegisterObserverWithConfig(
64     const std::string& observer,
65     std::shared_ptr<AppEventProcessor> processor,
66     const ReportConfig& config,
67     int64_t& processorSeq)
68 {
69     ASSERT_EQ(AppEventProcessorMgr::RegisterProcessor(observer, processor), 0);
70     processorSeq = AppEventObserverMgr::GetInstance().RegisterObserver(observer, config);
71     ASSERT_GT(processorSeq, 0);
72 }
73 
CheckUnregisterObserver(const std::string & observer)74 void CheckUnregisterObserver(const std::string& observer)
75 {
76     ASSERT_EQ(AppEventProcessorMgr::UnregisterProcessor(observer), 0);
77 }
78 
CheckGetEmptyConfig(int64_t processorSeq)79 void CheckGetEmptyConfig(int64_t processorSeq)
80 {
81     ReportConfig config;
82     ASSERT_EQ(AppEventProcessorMgr::GetProcessorConfig(processorSeq, config), 0);
83     ASSERT_TRUE(config.name.empty());
84     ASSERT_FALSE(config.debugMode);
85     ASSERT_TRUE(config.routeInfo.empty());
86     ASSERT_TRUE(config.appId.empty());
87     ASSERT_EQ(config.triggerCond.row, 0);
88     ASSERT_EQ(config.triggerCond.size, 0);
89     ASSERT_EQ(config.triggerCond.timeout, 0);
90     ASSERT_FALSE(config.triggerCond.onStartup);
91     ASSERT_FALSE(config.triggerCond.onBackground);
92     ASSERT_TRUE(config.userIdNames.empty());
93     ASSERT_TRUE(config.userPropertyNames.empty());
94     ASSERT_TRUE(config.eventConfigs.empty());
95     ASSERT_EQ(config.configId, 0);
96     ASSERT_TRUE(config.customConfigs.empty());
97 }
98 
CheckGetSeqs(const std::string & observer,const std::vector<int64_t> & expectSeqs)99 void CheckGetSeqs(const std::string& observer, const std::vector<int64_t>& expectSeqs)
100 {
101     std::vector<int64_t> processorSeqs;
102     ASSERT_EQ(AppEventProcessorMgr::GetProcessorSeqs(observer, processorSeqs), 0);
103     ASSERT_EQ(processorSeqs.size(), expectSeqs.size());
104 
105     // test repeated getting seqs
106     ASSERT_EQ(AppEventProcessorMgr::GetProcessorSeqs(observer, processorSeqs), 0);
107     ASSERT_EQ(processorSeqs.size(), expectSeqs.size());
108 }
109 
CheckSameConfig(const ReportConfig & configA,const ReportConfig & configB)110 void CheckSameConfig(const ReportConfig& configA, const ReportConfig& configB)
111 {
112     ASSERT_EQ(configA.name, configB.name);
113     ASSERT_EQ(configA.debugMode, configB.debugMode);
114     ASSERT_EQ(configA.routeInfo, configB.routeInfo);
115     ASSERT_EQ(configA.appId, configB.appId);
116     ASSERT_EQ(configA.triggerCond.row, configB.triggerCond.row);
117     ASSERT_EQ(configA.triggerCond.size, configB.triggerCond.size);
118     ASSERT_EQ(configA.triggerCond.timeout, configB.triggerCond.timeout);
119     ASSERT_EQ(configA.triggerCond.onStartup, configB.triggerCond.onStartup);
120     ASSERT_EQ(configA.triggerCond.onBackground, configB.triggerCond.onBackground);
121     ASSERT_EQ(configA.userIdNames.size(), configB.userIdNames.size());
122     ASSERT_EQ(configA.userPropertyNames.size(), configB.userPropertyNames.size());
123     ASSERT_EQ(configA.eventConfigs.size(), configB.eventConfigs.size());
124     ASSERT_EQ(configA.configId, configB.configId);
125     ASSERT_EQ(configA.customConfigs.size(), configB.customConfigs.size());
126 }
127 
CheckSameConfig(int64_t processorSeq,const ReportConfig & testConfig)128 void CheckSameConfig(int64_t processorSeq, const ReportConfig& testConfig)
129 {
130     ReportConfig getConfig;
131     ASSERT_EQ(AppEventProcessorMgr::GetProcessorConfig(processorSeq, getConfig), 0);
132     CheckSameConfig(testConfig, getConfig);
133 }
134 
CheckSetConfig(int64_t processorSeq)135 void CheckSetConfig(int64_t processorSeq)
136 {
137     ReportConfig testConfig = {
138         .name = "test_name",
139         .debugMode = true,
140         .routeInfo = "test_routeInfo",
141         .appId = "test_appid",
142         .triggerCond = {1, 1, 1, true, true},
143         .userIdNames = {"test_id"},
144         .userPropertyNames = {"test_property"},
145         .eventConfigs = {{"test_domain", "test_name", true}},
146         .configId = 1,
147         .customConfigs = {{"str_key", "str_value"}},
148     };
149     ASSERT_EQ(AppEventProcessorMgr::SetProcessorConfig(processorSeq, testConfig), 0);
150     ReportConfig getConfig;
151     ASSERT_EQ(AppEventProcessorMgr::GetProcessorConfig(processorSeq, getConfig), 0);
152     CheckSameConfig(testConfig, getConfig);
153 }
154 
CheckSetRealTimeConfig(int64_t processorSeq)155 void CheckSetRealTimeConfig(int64_t processorSeq)
156 {
157     ReportConfig testConfig = {
158         .eventConfigs = {{TEST_EVENT_DOMAIN, TEST_EVENT_NAME, true}},
159     };
160     ASSERT_EQ(AppEventProcessorMgr::SetProcessorConfig(processorSeq, testConfig), 0);
161 }
162 
CheckSetRowConfig(int64_t processorSeq)163 void CheckSetRowConfig(int64_t processorSeq)
164 {
165     ReportConfig testConfig = {
166         .triggerCond = {
167             .row = 2, // 2 events
168         },
169         .eventConfigs = {{TEST_EVENT_DOMAIN, TEST_EVENT_NAME}},
170     };
171     ASSERT_EQ(AppEventProcessorMgr::SetProcessorConfig(processorSeq, testConfig), 0);
172 }
173 
CheckSetSizeConfig(int64_t processorSeq)174 void CheckSetSizeConfig(int64_t processorSeq)
175 {
176     ReportConfig testConfig = {
177         .triggerCond = {
178             .size = 300, // 300 byte, 2 events
179         },
180         .eventConfigs = {{TEST_EVENT_DOMAIN, TEST_EVENT_NAME}},
181     };
182     ASSERT_EQ(AppEventProcessorMgr::SetProcessorConfig(processorSeq, testConfig), 0);
183 }
184 
CheckSetTimeoutConfig(int64_t processorSeq)185 void CheckSetTimeoutConfig(int64_t processorSeq)
186 {
187     ReportConfig testConfig = {
188         .triggerCond = {
189             .timeout = 2, // 2s
190         },
191         .eventConfigs = {{TEST_EVENT_DOMAIN, TEST_EVENT_NAME}},
192     };
193     ASSERT_EQ(AppEventProcessorMgr::SetProcessorConfig(processorSeq, testConfig), 0);
194 }
195 
CheckSetOnBackgroundConfig(int64_t processorSeq)196 void CheckSetOnBackgroundConfig(int64_t processorSeq)
197 {
198     ReportConfig testConfig = {
199         .triggerCond = {
200             .onBackground = true,
201         },
202         .eventConfigs = {{TEST_EVENT_DOMAIN, TEST_EVENT_NAME}},
203     };
204     ASSERT_EQ(AppEventProcessorMgr::SetProcessorConfig(processorSeq, testConfig), 0);
205 }
206 
CheckOnReport(const std::vector<UserId> & userIds,const std::vector<UserProperty> & userProperties,const std::vector<AppEventInfo> & events)207 void CheckOnReport(
208     const std::vector<UserId>& userIds,
209     const std::vector<UserProperty>& userProperties,
210     const std::vector<AppEventInfo>& events)
211 {
212     ASSERT_EQ(userIds.size(), 0);
213     ASSERT_EQ(userProperties.size(), 0);
214     ASSERT_GT(events.size(), 0);
215     for (const auto& event : events) {
216         ASSERT_EQ(event.domain, TEST_EVENT_DOMAIN);
217         ASSERT_EQ(event.name, TEST_EVENT_NAME);
218         ASSERT_EQ(event.eventType, TEST_EVENT_TYPE);
219         ASSERT_GT(event.timestamp, 0);
220         ASSERT_EQ(event.params, "{\"int_key\":1,\"double_key\":1.2,\"bool_key\":false,\"str_key\":\"str\"}\n");
221     }
222 }
223 }
224 
225 class HiAppEventInnerApiTest : public testing::Test {
226 public:
SetUpTestCase()227     static void SetUpTestCase()
228     {
229         // set app uid
230         setuid(TEST_UID);
231         HiAppEventConfig::GetInstance().SetStorageDir("/data/test/hiappevent/");
232         // set context bundle name
233         auto context = OHOS::AbilityRuntime::ApplicationContext::GetInstance();
234         if (context != nullptr) {
235             auto contextImpl = std::make_shared<TestContextImpl>("ohos.hiappevent.innerapi.test");
236             context->AttachContextImpl(contextImpl);
237             std::cout << "set bundle name." << std::endl;
238             return;
239         }
240         std::cout << "context is null." << std::endl;
241     }
242 
TearDownTestCase()243     static void TearDownTestCase() {}
SetUp()244     void SetUp() {}
TearDown()245     void TearDown() {}
246 };
247 
248 class AppEventProcessorTest : public AppEventProcessor {
249 public:
250     int OnReport(
251         int64_t processorSeq,
252         const std::vector<UserId>& userIds,
253         const std::vector<UserProperty>& userProperties,
254         const std::vector<AppEventInfo>& events) override;
255     int ValidateUserId(const UserId& userId) override;
256     int ValidateUserProperty(const UserProperty& userProperty) override;
257     int ValidateEvent(const AppEventInfo& event) override;
GetReportTimes()258     int GetReportTimes() { return reportTimes_; }
259 
260 private:
261     int reportTimes_ = 0;
262 };
263 
OnReport(int64_t processorSeq,const std::vector<UserId> & userIds,const std::vector<UserProperty> & userProperties,const std::vector<AppEventInfo> & events)264 int AppEventProcessorTest::OnReport(
265     int64_t processorSeq,
266     const std::vector<UserId>& userIds,
267     const std::vector<UserProperty>& userProperties,
268     const std::vector<AppEventInfo>& events)
269 {
270     reportTimes_++;
271 
272     std::cout << "UserId size=" << userIds.size() << std::endl;
273     std::cout << "UserProperty size=" << userProperties.size() << std::endl;
274     std::cout << "AppEventInfo size=" << events.size() << std::endl;
275     for (const auto& event : events) {
276         std::cout << "AppEventInfo.domain=" << event.domain << std::endl;
277         std::cout << "AppEventInfo.name=" << event.name << std::endl;
278         std::cout << "AppEventInfo.eventType=" << event.eventType << std::endl;
279         std::cout << "AppEventInfo.timestamp=" << event.timestamp << std::endl;
280         std::cout << "AppEventInfo.params=" << event.params << std::endl;
281     }
282     CheckOnReport(userIds, userProperties, events);
283     return 0;
284 }
285 
ValidateUserId(const UserId & userId)286 int AppEventProcessorTest::ValidateUserId(const UserId& userId)
287 {
288     return (userId.name.find("test") == std::string::npos) ? -1 : 0;
289 }
290 
ValidateUserProperty(const UserProperty & userProperty)291 int AppEventProcessorTest::ValidateUserProperty(const UserProperty& userProperty)
292 {
293     return (userProperty.name.find("test") == std::string::npos) ? -1 : 0;
294 }
295 
ValidateEvent(const AppEventInfo & event)296 int AppEventProcessorTest::ValidateEvent(const AppEventInfo& event)
297 {
298     return (event.domain.find("test") == std::string::npos) ? -1 : 0;
299 }
300 
301 /**
302  * @tc.name: HiAppEventInnerApiTest001
303  * @tc.desc: check the api AppEventProcessorMgr.
304  * @tc.type: FUNC
305  */
306 HWTEST_F(HiAppEventInnerApiTest, HiAppEventInnerApiTest001, TestSize.Level0)
307 {
308     /**
309      * @tc.steps: step1. Create an AppEventProcessor object.
310      * @tc.steps: step2. Register the AppEventProcessor object.
311      * @tc.steps: step3. Get processor sequence by name.
312      * @tc.steps: step4. Get processor config by sequence.
313      * @tc.steps: step5. Set processor config by sequence.
314      * @tc.steps: step6. Unregister the AppEventProcessor object.
315      */
316     auto processor = std::make_shared<AppEventProcessorTest>();
317     int64_t processorSeq = 0;
318     CheckRegisterObserver(TEST_PROCESSOR_NAME, processor, processorSeq);
319     CheckGetSeqs(TEST_PROCESSOR_NAME, {processorSeq});
320     CheckGetEmptyConfig(processorSeq);
321     CheckSetConfig(processorSeq);
322     WriteEventOnce();
323     CheckUnregisterObserver(TEST_PROCESSOR_NAME);
324 }
325 
326 /**
327  * @tc.name: HiAppEventInnerApiTest002
328  * @tc.desc: check the api AppEventProcessorMgr.
329  * @tc.type: FUNC
330  */
331 HWTEST_F(HiAppEventInnerApiTest, HiAppEventInnerApiTest002, TestSize.Level0)
332 {
333     /**
334      * @tc.steps: step1. Create an AppEventProcessor object.
335      * @tc.steps: step2. Register the AppEventProcessor object.
336      * @tc.steps: step3. Register the same AppEventProcessor object.
337      * @tc.steps: step4. Unregister the AppEventProcessor object.
338      * @tc.steps: step5. Unregister the same AppEventProcessor object.
339      */
340     auto processor = std::make_shared<AppEventProcessorTest>();
341     auto ret = AppEventProcessorMgr::RegisterProcessor(TEST_PROCESSOR_NAME, processor);
342     ASSERT_EQ(ret, 0);
343     ret = AppEventProcessorMgr::RegisterProcessor(TEST_PROCESSOR_NAME, processor);
344     ASSERT_EQ(ret, -1);
345     ret = AppEventProcessorMgr::UnregisterProcessor(TEST_PROCESSOR_NAME);
346     ASSERT_EQ(ret, 0);
347     ret = AppEventProcessorMgr::UnregisterProcessor(TEST_PROCESSOR_NAME);
348     ASSERT_EQ(ret, -1);
349 }
350 
351 /**
352  * @tc.name: HiAppEventInnerApiTest003
353  * @tc.desc: check the real-time event callback AppEventProcessor.
354  * @tc.type: FUNC
355  */
356 HWTEST_F(HiAppEventInnerApiTest, HiAppEventInnerApiTest003, TestSize.Level0)
357 {
358     /**
359      * @tc.steps: step1. Register an AppEventProcessor object.
360      * @tc.steps: step2. Set config to the AppEventProcessor object.
361      * @tc.steps: step3. Write an test event.
362      * @tc.steps: step4. Unregister the AppEventProcessor object.
363      */
364     auto processor = std::make_shared<AppEventProcessorTest>();
365     int64_t processorSeq = 0;
366     CheckRegisterObserver(TEST_PROCESSOR_NAME, processor, processorSeq);
367     CheckSetRealTimeConfig(processorSeq);
368     WriteEventOnce();
369     ASSERT_EQ(processor->GetReportTimes(), 1);
370     CheckUnregisterObserver(TEST_PROCESSOR_NAME);
371 }
372 
373 /**
374  * @tc.name: HiAppEventInnerApiTest004
375  * @tc.desc: check the row callback AppEventProcessor.
376  * @tc.type: FUNC
377  */
378 HWTEST_F(HiAppEventInnerApiTest, HiAppEventInnerApiTest004, TestSize.Level0)
379 {
380     /**
381      * @tc.steps: step1. Register an AppEventProcessor object.
382      * @tc.steps: step2. Set config to the AppEventProcessor object.
383      * @tc.steps: step3. Write an test event.
384      * @tc.steps: step4. Unregister the AppEventProcessor object.
385      */
386     auto processor = std::make_shared<AppEventProcessorTest>();
387     int64_t processorSeq = 0;
388     CheckRegisterObserver(TEST_PROCESSOR_NAME, processor, processorSeq);
389     CheckSetRowConfig(processorSeq);
390 
391     ASSERT_EQ(processor->GetReportTimes(), 0);
392     WriteEventOnce();
393     ASSERT_EQ(processor->GetReportTimes(), 0);
394     WriteEventOnce();
395     ASSERT_EQ(processor->GetReportTimes(), 1);
396 
397     CheckUnregisterObserver(TEST_PROCESSOR_NAME);
398 }
399 
400 /**
401  * @tc.name: HiAppEventInnerApiTest005
402  * @tc.desc: check the size callback AppEventProcessor.
403  * @tc.type: FUNC
404  */
405 HWTEST_F(HiAppEventInnerApiTest, HiAppEventInnerApiTest005, TestSize.Level0)
406 {
407     /**
408      * @tc.steps: step1. Register an AppEventProcessor object.
409      * @tc.steps: step2. Set config to the AppEventProcessor object.
410      * @tc.steps: step3. Write an test event.
411      * @tc.steps: step4. Unregister the AppEventProcessor object.
412      */
413     auto processor = std::make_shared<AppEventProcessorTest>();
414     int64_t processorSeq = 0;
415     CheckRegisterObserver(TEST_PROCESSOR_NAME, processor, processorSeq);
416     CheckSetSizeConfig(processorSeq);
417 
418     ASSERT_EQ(processor->GetReportTimes(), 0);
419     WriteEventOnce();
420     ASSERT_EQ(processor->GetReportTimes(), 0);
421     WriteEventOnce();
422     ASSERT_EQ(processor->GetReportTimes(), 1);
423 
424     CheckUnregisterObserver(TEST_PROCESSOR_NAME);
425 }
426 
427 /**
428  * @tc.name: HiAppEventInnerApiTest006
429  * @tc.desc: check the timeout callback AppEventProcessor.
430  * @tc.type: FUNC
431  */
432 HWTEST_F(HiAppEventInnerApiTest, HiAppEventInnerApiTest006, TestSize.Level0)
433 {
434     /**
435      * @tc.steps: step1. Register an AppEventProcessor object.
436      * @tc.steps: step2. Set config to the AppEventProcessor object.
437      * @tc.steps: step3. Write an test event.
438      * @tc.steps: step4. Unregister the AppEventProcessor object.
439      */
440     auto processor = std::make_shared<AppEventProcessorTest>();
441     int64_t processorSeq = 0;
442     CheckRegisterObserver(TEST_PROCESSOR_NAME, processor, processorSeq);
443     CheckSetTimeoutConfig(processorSeq);
444 
445     ASSERT_EQ(processor->GetReportTimes(), 0);
446     WriteEventOnce();
447     ASSERT_EQ(processor->GetReportTimes(), 0);
448     sleep(HiAppEvent::TIMEOUT_STEP + 1); // 31s
449     ASSERT_EQ(processor->GetReportTimes(), 1);
450 
451     CheckUnregisterObserver(TEST_PROCESSOR_NAME);
452 }
453 
454 /**
455  * @tc.name: HiAppEventInnerApiTest007
456  * @tc.desc: check the background callback AppEventProcessor.
457  * @tc.type: FUNC
458  */
459 HWTEST_F(HiAppEventInnerApiTest, HiAppEventInnerApiTest007, TestSize.Level0)
460 {
461     /**
462      * @tc.steps: step1. Register an AppEventProcessor object.
463      * @tc.steps: step2. Set config to the AppEventProcessor object.
464      * @tc.steps: step3. Write an test event.
465      * @tc.steps: step4. Unregister the AppEventProcessor object.
466      */
467     auto processor = std::make_shared<AppEventProcessorTest>();
468     int64_t processorSeq = 0;
469     CheckRegisterObserver(TEST_PROCESSOR_NAME, processor, processorSeq);
470     CheckSetOnBackgroundConfig(processorSeq);
471 
472     ASSERT_EQ(processor->GetReportTimes(), 0);
473     WriteEventOnce();
474     ASSERT_EQ(processor->GetReportTimes(), 0);
475     AppEventObserverMgr::GetInstance().HandleBackground();
476     sleep(1); // 1s
477     ASSERT_EQ(processor->GetReportTimes(), 1);
478 
479     CheckUnregisterObserver(TEST_PROCESSOR_NAME);
480 }
481 
482 /**
483  * @tc.name: HiAppEventInnerApiTest008
484  * @tc.desc: check the startup callback AppEventProcessor.
485  * @tc.type: FUNC
486  */
487 HWTEST_F(HiAppEventInnerApiTest, HiAppEventInnerApiTest008, TestSize.Level0)
488 {
489     /**
490      * @tc.steps: step1. Register an AppEventProcessor object.
491      * @tc.steps: step2. Write an test event.
492      * @tc.steps: step3. Register an AppEventProcessor object with same configuration.
493      * @tc.steps: step4. Unregister the AppEventProcessor object.
494      */
495     auto processor = std::make_shared<AppEventProcessorTest>();
496     int64_t processorSeq1 = 0;
497     ReportConfig config = {
498         .name = TEST_PROCESSOR_NAME,
499         .triggerCond = {
500             .onStartup = true,
501         },
502         .eventConfigs = {{TEST_EVENT_DOMAIN, TEST_EVENT_NAME}},
503     };
504     CheckRegisterObserverWithConfig(TEST_PROCESSOR_NAME, processor, config, processorSeq1);
505 
506     ASSERT_EQ(processor->GetReportTimes(), 0);
507     WriteEventOnce();
508     ASSERT_EQ(processor->GetReportTimes(), 0);
509 
510     int64_t processorSeq2 = AppEventObserverMgr::GetInstance().RegisterObserver(TEST_PROCESSOR_NAME, config);
511     ASSERT_EQ(processorSeq1, processorSeq2);
512     sleep(1); // 1s
513     ASSERT_EQ(processor->GetReportTimes(), 1);
514 
515     CheckUnregisterObserver(TEST_PROCESSOR_NAME);
516 }
517 
518 /**
519  * @tc.name: HiAppEventInnerApiTest009
520  * @tc.desc: Adding a processor that does not exist.
521  * @tc.type: FUNC
522  */
523 HWTEST_F(HiAppEventInnerApiTest, HiAppEventInnerApiTest009, TestSize.Level1)
524 {
525     ReportConfig config = {
526         .name = "invalid_processor",
527     };
528     int64_t processorId = AppEventProcessorMgr::AddProcessor(config);
529     ASSERT_EQ(processorId, -1);
530     ASSERT_EQ(AppEventProcessorMgr::RemoveProcessor(processorId), 0);
531 }
532 
533 /**
534  * @tc.name: HiAppEventInnerApiTest010
535  * @tc.desc: Adding an existing processor.
536  * @tc.type: FUNC
537  */
538 HWTEST_F(HiAppEventInnerApiTest, HiAppEventInnerApiTest010, TestSize.Level1)
539 {
540     ReportConfig config = {
541         .name = "test_processor",
542         .debugMode = true,
543         .routeInfo = "test_routeInfo",
544         .appId = "test_appid",
545         .triggerCond = {
546             .row = 1,
547             .timeout = 0,
548             .onStartup = false,
549             .onBackground = false,
550         },
551         .userIdNames = {"test_id"},
552         .userPropertyNames = {"test_property"},
553         .eventConfigs = {{"test_domain", "test_name", true}, {"test_domain", "test_realtime"}},
554         .configId = 1,
555         .customConfigs = {{"str_key", "str_value"}, {"str_key1", "str_value1"}},
556     };
557     int64_t processorId = AppEventProcessorMgr::AddProcessor(config);
558     ASSERT_GT(processorId, 0);
559     CheckSameConfig(processorId, config);
560     WriteEventOnce();
561     ASSERT_EQ(AppEventProcessorMgr::RemoveProcessor(processorId), 0);
562 }
563 
564 /**
565  * @tc.name: HiAppEventInnerApiTest011
566  * @tc.desc: Adding an invalid processor with invalid name.
567  * @tc.type: FUNC
568  */
569 HWTEST_F(HiAppEventInnerApiTest, HiAppEventInnerApiTest011, TestSize.Level1)
570 {
571     ReportConfig config = {
572         .name = "",
573     };
574     ASSERT_EQ(AppEventProcessorMgr::AddProcessor(config), -1);
575 
576     constexpr size_t limitLen = 256;
577     config.name = std::string(limitLen, 'a');
578     ASSERT_EQ(AppEventProcessorMgr::AddProcessor(config), -1);
579 
580     config.name = "***dddd";
581     ASSERT_EQ(AppEventProcessorMgr::AddProcessor(config), -1);
582 
583     config.name = "999aaaa";
584     ASSERT_EQ(AppEventProcessorMgr::AddProcessor(config), -1);
585 }
586 
587 /**
588  * @tc.name: HiAppEventInnerApiTest012
589  * @tc.desc: Adding an processor with invalid routeInfo.
590  * @tc.type: FUNC
591  */
592 HWTEST_F(HiAppEventInnerApiTest, HiAppEventInnerApiTest012, TestSize.Level1)
593 {
594     constexpr size_t limitLen = 8 * 1024;
595     ReportConfig config = {
596         .name = "test_processor",
597         .routeInfo = std::string(limitLen, 'a'),
598     };
599     int64_t processorId1 = AppEventProcessorMgr::AddProcessor(config);
600     ASSERT_GT(processorId1, 0);
601     CheckSameConfig(processorId1, config);
602     ASSERT_EQ(AppEventProcessorMgr::RemoveProcessor(processorId1), 0);
603 
604     config.routeInfo = std::string(limitLen + 1, 'a');
605     int64_t processorId2 = AppEventProcessorMgr::AddProcessor(config);
606     ASSERT_GT(processorId2, 0);
607 
608     ReportConfig expectConfig = {
609         .name = "test_processor",
610         .routeInfo = "", // default routeInfo value
611     };
612     CheckSameConfig(processorId2, expectConfig);
613 
614     ASSERT_EQ(AppEventProcessorMgr::RemoveProcessor(processorId2), 0);
615 }
616 
617 /**
618  * @tc.name: HiAppEventInnerApiTest013
619  * @tc.desc: Adding an processor with invalid appId.
620  * @tc.type: FUNC
621  */
622 HWTEST_F(HiAppEventInnerApiTest, HiAppEventInnerApiTest013, TestSize.Level1)
623 {
624     constexpr size_t limitLen = 8 * 1024;
625     ReportConfig config = {
626         .name = "test_processor",
627         .appId = std::string(limitLen, 'a'),
628     };
629     int64_t processorId1 = AppEventProcessorMgr::AddProcessor(config);
630     ASSERT_GT(processorId1, 0);
631     CheckSameConfig(processorId1, config);
632     ASSERT_EQ(AppEventProcessorMgr::RemoveProcessor(processorId1), 0);
633 
634     config.appId = std::string(limitLen + 1, 'a');
635     int64_t processorId2 = AppEventProcessorMgr::AddProcessor(config);
636     ASSERT_GT(processorId2, 0);
637 
638     ReportConfig expectConfig = {
639         .name = "test_processor",
640         .appId = "", // default appId value
641     };
642     CheckSameConfig(processorId2, expectConfig);
643 
644     ASSERT_EQ(AppEventProcessorMgr::RemoveProcessor(processorId2), 0);
645 }
646 
647 /**
648  * @tc.name: HiAppEventInnerApiTest014
649  * @tc.desc: Adding an processor with invalid triggerCond.row.
650  * @tc.type: FUNC
651  */
652 HWTEST_F(HiAppEventInnerApiTest, HiAppEventInnerApiTest014, TestSize.Level1)
653 {
654     constexpr int limitRow = 1000;
655     ReportConfig config = {
656         .name = "test_processor",
657         .triggerCond = {
658             .row = limitRow,
659         },
660     };
661     int64_t processorId1 = AppEventProcessorMgr::AddProcessor(config);
662     ASSERT_GT(processorId1, 0);
663     CheckSameConfig(processorId1, config);
664     ASSERT_EQ(AppEventProcessorMgr::RemoveProcessor(processorId1), 0);
665 
666     config.triggerCond.row = -1;
667     int64_t processorId2 = AppEventProcessorMgr::AddProcessor(config);
668     ASSERT_GT(processorId2, 0);
669     ReportConfig expectConfig = {
670         .name = "test_processor",
671         .triggerCond.row = 0, // default row value
672     };
673     CheckSameConfig(processorId2, expectConfig);
674     ASSERT_EQ(AppEventProcessorMgr::RemoveProcessor(processorId2), 0);
675 
676     config.triggerCond.row = limitRow + 1;
677     int64_t processorId3 = AppEventProcessorMgr::AddProcessor(config);
678     ASSERT_GT(processorId3, 0);
679     CheckSameConfig(processorId3, expectConfig);
680     ASSERT_EQ(AppEventProcessorMgr::RemoveProcessor(processorId3), 0);
681 }
682 
683 /**
684  * @tc.name: HiAppEventInnerApiTest015
685  * @tc.desc: Adding an processor with invalid triggerCond.timeout.
686  * @tc.type: FUNC
687  */
688 HWTEST_F(HiAppEventInnerApiTest, HiAppEventInnerApiTest015, TestSize.Level1)
689 {
690     ReportConfig config = {
691         .name = "test_processor",
692         .triggerCond = {
693             .timeout = -1,
694         },
695     };
696     int64_t processorId1 = AppEventProcessorMgr::AddProcessor(config);
697     ASSERT_GT(processorId1, 0);
698     ReportConfig expectConfig = {
699         .name = "test_processor",
700         .triggerCond.timeout = 0, // default timeout value
701     };
702     CheckSameConfig(processorId1, expectConfig);
703     ASSERT_EQ(AppEventProcessorMgr::RemoveProcessor(processorId1), 0);
704 }
705 
706 /**
707  * @tc.name: HiAppEventInnerApiTest016
708  * @tc.desc: Adding an processor with invalid userIdNames.
709  * @tc.type: FUNC
710  */
711 HWTEST_F(HiAppEventInnerApiTest, HiAppEventInnerApiTest016, TestSize.Level1)
712 {
713     constexpr size_t limitLen = 256;
714     ReportConfig config = {
715         .name = "test_processor",
716         .userIdNames = {
717             std::string(limitLen, 'a'), "id1"
718         },
719     };
720     int64_t processorId = AppEventProcessorMgr::AddProcessor(config);
721     ASSERT_GT(processorId, 0);
722     CheckSameConfig(processorId, config);
723     ASSERT_EQ(AppEventProcessorMgr::RemoveProcessor(processorId), 0);
724 
725     config.userIdNames = {"***xxxx", "id1"};
726     processorId = AppEventProcessorMgr::AddProcessor(config);
727     ASSERT_GT(processorId, 0);
728     ReportConfig expectConfig = {
729         .name = "test_processor",
730         .userIdNames = {}, // default userIdNames value
731     };
732     CheckSameConfig(processorId, expectConfig);
733     ASSERT_EQ(AppEventProcessorMgr::RemoveProcessor(processorId), 0);
734 
735     config.userIdNames = {"", "id1"};
736     processorId = AppEventProcessorMgr::AddProcessor(config);
737     ASSERT_GT(processorId, 0);
738     CheckSameConfig(processorId, expectConfig);
739     ASSERT_EQ(AppEventProcessorMgr::RemoveProcessor(processorId), 0);
740 
741     config.userIdNames = {std::string(limitLen + 1, 'a'), "id1"};
742     processorId = AppEventProcessorMgr::AddProcessor(config);
743     ASSERT_GT(processorId, 0);
744     CheckSameConfig(processorId, expectConfig);
745     ASSERT_EQ(AppEventProcessorMgr::RemoveProcessor(processorId), 0);
746 }
747 
748 /**
749  * @tc.name: HiAppEventInnerApiTest017
750  * @tc.desc: Adding an processor with invalid userIdNames.
751  * @tc.type: FUNC
752  */
753 HWTEST_F(HiAppEventInnerApiTest, HiAppEventInnerApiTest017, TestSize.Level1)
754 {
755     constexpr size_t limitLen = 256;
756     ReportConfig config = {
757         .name = "test_processor",
758         .userPropertyNames = {
759             std::string(limitLen, 'a'), "id1"
760         },
761     };
762     int64_t processorId = AppEventProcessorMgr::AddProcessor(config);
763     ASSERT_GT(processorId, 0);
764     CheckSameConfig(processorId, config);
765     ASSERT_EQ(AppEventProcessorMgr::RemoveProcessor(processorId), 0);
766 
767     config.userPropertyNames = {"***xxxx", "id1"};
768     processorId = AppEventProcessorMgr::AddProcessor(config);
769     ASSERT_GT(processorId, 0);
770     ReportConfig expectConfig = {
771         .name = "test_processor",
772         .userPropertyNames = {}, // default userPropertyNames value
773     };
774     CheckSameConfig(processorId, expectConfig);
775     ASSERT_EQ(AppEventProcessorMgr::RemoveProcessor(processorId), 0);
776 
777     config.userPropertyNames = {"", "id1"};
778     processorId = AppEventProcessorMgr::AddProcessor(config);
779     ASSERT_GT(processorId, 0);
780     CheckSameConfig(processorId, expectConfig);
781     ASSERT_EQ(AppEventProcessorMgr::RemoveProcessor(processorId), 0);
782 
783     config.userPropertyNames = {std::string(limitLen + 1, 'a'), "id1"};
784     processorId = AppEventProcessorMgr::AddProcessor(config);
785     ASSERT_GT(processorId, 0);
786     CheckSameConfig(processorId, expectConfig);
787     ASSERT_EQ(AppEventProcessorMgr::RemoveProcessor(processorId), 0);
788 }
789 
790 /**
791  * @tc.name: HiAppEventInnerApiTest018
792  * @tc.desc: Adding an processor with invalid eventConfigs.
793  * @tc.type: FUNC
794  */
795 HWTEST_F(HiAppEventInnerApiTest, HiAppEventInnerApiTest018, TestSize.Level1)
796 {
797     ReportConfig config = {
798         .name = "test_processor",
799         .eventConfigs = {
800             {"***domain", "name"}
801         },
802     };
803     int64_t processorId = AppEventProcessorMgr::AddProcessor(config);
804     ASSERT_GT(processorId, 0);
805     ReportConfig expectConfig = {
806         .name = "test_processor",
807         .eventConfigs = {}, // default eventConfigs value
808     };
809     CheckSameConfig(processorId, expectConfig);
810     ASSERT_EQ(AppEventProcessorMgr::RemoveProcessor(processorId), 0);
811 
812     config.eventConfigs = {{"domain", "***name"}};
813     processorId = AppEventProcessorMgr::AddProcessor(config);
814     ASSERT_GT(processorId, 0);
815     CheckSameConfig(processorId, expectConfig);
816     ASSERT_EQ(AppEventProcessorMgr::RemoveProcessor(processorId), 0);
817 
818     config.eventConfigs = {{"", "", true}, {"test_domain", "test_name"}};
819     processorId = AppEventProcessorMgr::AddProcessor(config);
820     ASSERT_GT(processorId, 0);
821     CheckSameConfig(processorId, expectConfig);
822     ASSERT_EQ(AppEventProcessorMgr::RemoveProcessor(processorId), 0);
823 }
824 
825 /**
826  * @tc.name: HiAppEventInnerApiTest019
827  * @tc.desc: Adding an existing processor with empty domain.
828  * @tc.type: FUNC
829  */
830 HWTEST_F(HiAppEventInnerApiTest, HiAppEventInnerApiTest019, TestSize.Level1)
831 {
832     ReportConfig config = {
833         .name = "test_processor",
834         .eventConfigs = {
835             {"", TEST_EVENT_NAME, true}
836         },
837     };
838     int64_t processorId = AppEventProcessorMgr::AddProcessor(config);
839     ASSERT_GT(processorId, 0);
840     CheckSameConfig(processorId, config);
841     WriteEventOnce();
842     ASSERT_EQ(AppEventProcessorMgr::RemoveProcessor(processorId), 0);
843 }
844 
845 /**
846  * @tc.name: HiAppEventInnerApiTest020
847  * @tc.desc: Adding an existing processor with empty name.
848  * @tc.type: FUNC
849  */
850 HWTEST_F(HiAppEventInnerApiTest, HiAppEventInnerApiTest020, TestSize.Level1)
851 {
852     ReportConfig config = {
853         .name = "test_processor",
854         .eventConfigs = {
855             {TEST_EVENT_DOMAIN, "", true}
856         },
857     };
858     int64_t processorId = AppEventProcessorMgr::AddProcessor(config);
859     ASSERT_GT(processorId, 0);
860     CheckSameConfig(processorId, config);
861     WriteEventOnce();
862     ASSERT_EQ(AppEventProcessorMgr::RemoveProcessor(processorId), 0);
863 }
864 
865 /**
866  * @tc.name: HiAppEventInnerApiTest021
867  * @tc.desc: Adding an processor with same processorId.
868  * @tc.type: FUNC
869  */
870 HWTEST_F(HiAppEventInnerApiTest, HiAppEventInnerApiTest021, TestSize.Level1)
871 {
872     ReportConfig config = {
873         .name = "test_processor",
874         .configId = 1,
875     };
876     int64_t processorId1 = AppEventProcessorMgr::AddProcessor(config);
877     ASSERT_GT(processorId1, 0);
878 
879     config.eventConfigs = {{"test_domain", "test_name", true}, {"test_domain", "test_realtime"}};
880     int64_t processorId2 = AppEventProcessorMgr::AddProcessor(config);
881     ASSERT_GT(processorId2, 0);
882     ASSERT_EQ(processorId1, processorId2);
883 
884     config.configId = 0;
885     int64_t processorId3 = AppEventProcessorMgr::AddProcessor(config);
886     ASSERT_GT(processorId3, 0);
887     ASSERT_NE(processorId2, processorId3);
888 
889     ASSERT_EQ(AppEventProcessorMgr::RemoveProcessor(processorId1), 0);
890     ASSERT_EQ(AppEventProcessorMgr::RemoveProcessor(processorId2), 0);
891     ASSERT_EQ(AppEventProcessorMgr::RemoveProcessor(processorId3), 0);
892 }
893 
894 /**
895  * @tc.name: HiAppEventInnerApiTest022
896  * @tc.desc: Adding an processor with invalid configId.
897  * @tc.type: FUNC
898  */
899 HWTEST_F(HiAppEventInnerApiTest, HiAppEventInnerApiTest022, TestSize.Level1)
900 {
901     ReportConfig config = {
902         .name = "test_processor",
903         .configId = -1,
904     };
905     int64_t processorId = AppEventProcessorMgr::AddProcessor(config);
906     ASSERT_GT(processorId, 0);
907     ReportConfig expectConfig = {
908         .name = "test_processor",
909         .configId = 0, // default configId
910     };
911     CheckSameConfig(processorId, expectConfig);
912     ASSERT_EQ(AppEventProcessorMgr::RemoveProcessor(processorId), 0);
913 }
914 
915 /**
916  * @tc.name: HiAppEventInnerApiTest023
917  * @tc.desc: Adding an processor with invalid customConfigs.
918  * @tc.type: FUNC
919  */
920 HWTEST_F(HiAppEventInnerApiTest, HiAppEventInnerApiTest023, TestSize.Level1)
921 {
922     ReportConfig config = {
923         .name = "test_processor",
924         .customConfigs = {{"", "test_str"}},
925     };
926     int64_t processorId = AppEventProcessorMgr::AddProcessor(config);
927     ASSERT_GT(processorId, 0);
928     ReportConfig expectConfig = {
929         .name = "test_processor",
930         .customConfigs = {}, // default customConfigs value
931     };
932     CheckSameConfig(processorId, expectConfig);
933     ASSERT_EQ(AppEventProcessorMgr::RemoveProcessor(processorId), 0);
934 
935     config.customConfigs = {{"**name", "value"}};
936     processorId = AppEventProcessorMgr::AddProcessor(config);
937     ASSERT_GT(processorId, 0);
938     CheckSameConfig(processorId, expectConfig);
939     ASSERT_EQ(AppEventProcessorMgr::RemoveProcessor(processorId), 0);
940 
941     constexpr size_t limitLen = 1024;
942     config.customConfigs = {{"test_name", std::string(limitLen + 1, 'a')}};
943     processorId = AppEventProcessorMgr::AddProcessor(config);
944     ASSERT_GT(processorId, 0);
945     CheckSameConfig(processorId, expectConfig);
946     ASSERT_EQ(AppEventProcessorMgr::RemoveProcessor(processorId), 0);
947 }
948 
949 /**
950  * @tc.name: HiAppEventInnerApiTest024
951  * @tc.desc: check the api AppEventProcessorMgr not app.
952  * @tc.type: FUNC
953  */
954 HWTEST_F(HiAppEventInnerApiTest, HiAppEventInnerApiTest024, TestSize.Level0)
955 {
956     setuid(0); // 0 means root uid
957 
958     auto processor = std::make_shared<AppEventProcessorTest>();
959     ASSERT_EQ(AppEventProcessorMgr::RegisterProcessor(TEST_PROCESSOR_NAME, processor), ERROR_NOT_APP);
960     ASSERT_EQ(AppEventProcessorMgr::UnregisterProcessor(TEST_PROCESSOR_NAME), ERROR_NOT_APP);
961 
962     std::vector<int64_t> processorSeqs;
963     ASSERT_EQ(AppEventProcessorMgr::GetProcessorSeqs(TEST_PROCESSOR_NAME, processorSeqs), ERROR_NOT_APP);
964 
965     int64_t processorSeq = 0;
966     ReportConfig config = {
967         .name = TEST_PROCESSOR_NAME,
968     };
969     ASSERT_EQ(AppEventProcessorMgr::SetProcessorConfig(processorSeq, config), ERROR_NOT_APP);
970     ASSERT_EQ(AppEventProcessorMgr::GetProcessorConfig(processorSeq, config), ERROR_NOT_APP);
971 
972     ASSERT_EQ(AppEventProcessorMgr::AddProcessor(config), ERROR_NOT_APP);
973     ASSERT_EQ(AppEventProcessorMgr::RemoveProcessor(processorSeq), ERROR_NOT_APP);
974 
975     // set app uid
976     setuid(TEST_UID);
977 }
978