1 /*
2  * Copyright (c) 2022-2023 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 #include "event_report.h"
19 
20 namespace OHOS {
21 namespace EventFwk {
22 using namespace testing::ext;
23 
24 namespace {
25 const std::string TEST_EVENT_NAME = "usual.event.TEST_HISYSEVENT";
26 const std::string TEST_BUNDLE_NAME_OF_SUBSCRIBER = "subscriber";
27 const std::string TEST_BUNDLE_NAME_OF_PUBLISHER = "publisher";
28 constexpr int32_t TEST_USER_ID = 1000;
29 constexpr int32_t TEST_SUBSCRIBER_NUM = 256;
30 } // namespace
31 
32 class CommonEventHisyseventTest : public testing::Test {
33 public:
CommonEventHisyseventTest()34     CommonEventHisyseventTest()
35     {}
~CommonEventHisyseventTest()36     ~CommonEventHisyseventTest()
37     {}
38 
39     static void SetUpTestCase(void);
40     static void TearDownTestCase(void);
41     void SetUp();
42     void TearDown();
43 };
44 
SetUpTestCase(void)45 void CommonEventHisyseventTest::SetUpTestCase(void)
46 {}
47 
TearDownTestCase(void)48 void CommonEventHisyseventTest::TearDownTestCase(void)
49 {}
50 
SetUp(void)51 void CommonEventHisyseventTest::SetUp(void)
52 {}
53 
TearDown(void)54 void CommonEventHisyseventTest::TearDown(void)
55 {}
56 
57 /**
58  * @tc.name: SendOrderedEventProcTimeoutSysEvent_0100
59  * @tc.desc: Send "ORDERED_EVENT_PROC_TIMEOUT" hisysevent.
60  * @tc.type: FUNC
61  * @tc.require: I582Y4
62  */
HWTEST_F(CommonEventHisyseventTest,SendOrderedEventProcTimeoutSysEvent_0100,Level1)63 HWTEST_F(CommonEventHisyseventTest, SendOrderedEventProcTimeoutSysEvent_0100, Level1)
64 {
65     GTEST_LOG_(INFO) << "SendOrderedEventProcTimeoutSysEvent_0100 start";
66 
67     std::shared_ptr<EventReport> eventReport = std::make_shared<EventReport>();
68     ASSERT_NE(nullptr, eventReport);
69     EventInfo eventInfo;
70     eventInfo.userId = TEST_USER_ID;
71     eventInfo.subscriberName = TEST_BUNDLE_NAME_OF_SUBSCRIBER;
72     eventInfo.pid = getpid();
73     eventInfo.uid = getuid();
74     eventInfo.eventName = TEST_EVENT_NAME;
75     eventReport->SendHiSysEvent(ORDERED_EVENT_PROC_TIMEOUT, eventInfo);
76 
77     GTEST_LOG_(INFO) << "SendOrderedEventProcTimeoutSysEvent_0100 end";
78 }
79 
80 /**
81  * @tc.name: SendStaticEventProcErrorSysEvent_0100
82  * @tc.desc: Send "STATIC_EVENT_PROC_ERROR" hisysevent.
83  * @tc.type: FUNC
84  * @tc.require: I582Y4
85  */
HWTEST_F(CommonEventHisyseventTest,SendStaticEventProcErrorSysEvent_0100,Level1)86 HWTEST_F(CommonEventHisyseventTest, SendStaticEventProcErrorSysEvent_0100, Level1)
87 {
88     GTEST_LOG_(INFO) << "SendStaticEventProcErrorSysEvent_0100 start";
89 
90     std::shared_ptr<EventReport> eventReport = std::make_shared<EventReport>();
91     ASSERT_NE(nullptr, eventReport);
92     EventInfo eventInfo;
93     eventInfo.userId = TEST_USER_ID;
94     eventInfo.publisherName = TEST_BUNDLE_NAME_OF_PUBLISHER;
95     eventInfo.subscriberName = TEST_BUNDLE_NAME_OF_SUBSCRIBER;
96     eventInfo.eventName = TEST_EVENT_NAME;
97     eventReport->SendHiSysEvent(STATIC_EVENT_PROC_ERROR, eventInfo);
98 
99     GTEST_LOG_(INFO) << "SendStaticEventProcErrorSysEvent_0100 end";
100 }
101 
102 /**
103  * @tc.name: SendSubscriberExceedMaximumSysEvent_0100
104  * @tc.desc: Send "SUBSCRIBER_EXCEED_MAXIMUM" hisysevent.
105  * @tc.type: FUNC
106  * @tc.require: I582Y4
107  */
HWTEST_F(CommonEventHisyseventTest,SendSubscriberExceedMaximumSysEvent_0100,Level1)108 HWTEST_F(CommonEventHisyseventTest, SendSubscriberExceedMaximumSysEvent_0100, Level1)
109 {
110     GTEST_LOG_(INFO) << "SendSubscriberExceedMaximumSysEvent_0100 start";
111 
112     std::shared_ptr<EventReport> eventReport = std::make_shared<EventReport>();
113     ASSERT_NE(nullptr, eventReport);
114     EventInfo eventInfo;
115     eventInfo.userId = TEST_USER_ID;
116     eventInfo.eventName = TEST_EVENT_NAME;
117     eventInfo.subscriberNum = TEST_SUBSCRIBER_NUM;
118     eventReport->SendHiSysEvent(SUBSCRIBER_EXCEED_MAXIMUM, eventInfo);
119 
120     GTEST_LOG_(INFO) << "SendSubscriberExceedMaximumSysEvent_0100 end";
121 }
122 
123 /**
124  * @tc.name: SendPublishEventErrorSysEvent_0100
125  * @tc.desc: Send "PUBLISH_EVENT_ERROR" hisysevent.
126  * @tc.type: FUNC
127  * @tc.require: I582Y4
128  */
HWTEST_F(CommonEventHisyseventTest,SendPublishEventErrorSysEvent_0100,Level1)129 HWTEST_F(CommonEventHisyseventTest, SendPublishEventErrorSysEvent_0100, Level1)
130 {
131     GTEST_LOG_(INFO) << "SendSubscriberExceedMaximumSysEvent_0100 start";
132 
133     EventInfo eventInfo;
134     eventInfo.userId = TEST_USER_ID;
135     eventInfo.publisherName = TEST_BUNDLE_NAME_OF_SUBSCRIBER;
136     eventInfo.pid = getpid();
137     eventInfo.uid = getuid();
138     eventInfo.eventName = TEST_EVENT_NAME;
139     std::shared_ptr<EventReport> eventReport = std::make_shared<EventReport>();
140     ASSERT_NE(nullptr, eventReport);
141     eventReport->SendHiSysEvent(PUBLISH_ERROR, eventInfo);
142 
143     GTEST_LOG_(INFO) << "SendPublishEventErrorSysEvent_0100 end";
144 }
145 
146 /**
147  * @tc.name: SendSubscribeSysEvent_0100
148  * @tc.desc: Send "SUBSCRIBE_EVENT" hisysevent.
149  * @tc.type: FUNC
150  * @tc.require: I582Y4
151  */
HWTEST_F(CommonEventHisyseventTest,SendSubscribeSysEvent_0100,Level1)152 HWTEST_F(CommonEventHisyseventTest, SendSubscribeSysEvent_0100, Level1)
153 {
154     GTEST_LOG_(INFO) << "SendSubscribeSysEvent_0100 start";
155 
156     std::shared_ptr<EventReport> eventReport = std::make_shared<EventReport>();
157     ASSERT_NE(nullptr, eventReport);
158     EventInfo eventInfo;
159     eventInfo.userId = TEST_USER_ID;
160     eventInfo.subscriberName = TEST_BUNDLE_NAME_OF_SUBSCRIBER;
161     eventInfo.pid = getpid();
162     eventInfo.uid = getuid();
163     std::vector<std::string> events = {"event1", "event2", "event3"};
164     eventInfo.eventName = std::accumulate(events.begin(), events.end(), eventInfo.eventName,
165         [events](std::string eventName, const std::string &str) {
166             return (str == events.front()) ? (eventName + str) : (eventName + "," + str);
167         });
168     eventReport->SendHiSysEvent(SUBSCRIBE, eventInfo);
169 
170     std::vector<std::string> anotherEvent = {"event"};
171     eventInfo.eventName = std::accumulate(anotherEvent.begin(), anotherEvent.end(), std::string(""),
172         [anotherEvent](std::string eventName, const std::string &str) {
173             return (str == anotherEvent.front()) ? (eventName + str) : (eventName + "," + str);
174         });
175     eventReport->SendHiSysEvent(SUBSCRIBE, eventInfo);
176 
177     GTEST_LOG_(INFO) << "SendSubscribeSysEvent_0100 end";
178 }
179 
180 /**
181  * @tc.name: SendUnSubscribeSysEvent_0100
182  * @tc.desc: Send "UNSUBSCRIBE_EVENT" hisysevent.
183  * @tc.type: FUNC
184  * @tc.require: I582Y4
185  */
HWTEST_F(CommonEventHisyseventTest,SendUnSubscribeSysEvent_0100,Level1)186 HWTEST_F(CommonEventHisyseventTest, SendUnSubscribeSysEvent_0100, Level1)
187 {
188     GTEST_LOG_(INFO) << "SendUnSubscribeSysEvent_0100 start";
189 
190     std::shared_ptr<EventReport> eventReport = std::make_shared<EventReport>();
191     ASSERT_NE(nullptr, eventReport);
192     EventInfo eventInfo;
193     eventInfo.userId = TEST_USER_ID;
194     eventInfo.subscriberName = TEST_BUNDLE_NAME_OF_SUBSCRIBER;
195     eventInfo.pid = getpid();
196     eventInfo.uid = getuid();
197     std::vector<std::string> events = {"event1_1", "event1_2", "event1_3"};
198     eventInfo.eventName = std::accumulate(events.begin(), events.end(), eventInfo.eventName,
199         [events](std::string eventName, const std::string &str) {
200             return (str == events.front()) ? (eventName + str) : (eventName + "," + str);
201         });
202     eventReport->SendHiSysEvent(UNSUBSCRIBE, eventInfo);
203 
204     eventInfo.eventName = "";
205     std::vector<std::string> anotherEvent = {"event"};
206     eventInfo.eventName = std::accumulate(anotherEvent.begin(), anotherEvent.end(), eventInfo.eventName,
207         [anotherEvent](std::string eventName, const std::string &str) {
208             return (str == anotherEvent.front()) ? (eventName + str) : (eventName + "," + str);
209         });
210     eventReport->SendHiSysEvent(UNSUBSCRIBE, eventInfo);
211 
212     GTEST_LOG_(INFO) << "SendUnSubscribeSysEvent_0100 end";
213 }
214 
215 /**
216  * @tc.name: SendPublishSysEvent_0100
217  * @tc.desc: Send "PUBLISH_EVENT" hisysevent.
218  * @tc.type: FUNC
219  * @tc.require: I582Y4
220  */
HWTEST_F(CommonEventHisyseventTest,SendPublishSysEvent_0100,Level1)221 HWTEST_F(CommonEventHisyseventTest, SendPublishSysEvent_0100, Level1)
222 {
223     GTEST_LOG_(INFO) << "SendPublishSysEvent_0100 start";
224 
225     std::shared_ptr<EventReport> eventReport = std::make_shared<EventReport>();
226     ASSERT_NE(nullptr, eventReport);
227     EventInfo eventInfo;
228     eventInfo.userId = TEST_USER_ID;
229     eventInfo.publisherName = TEST_BUNDLE_NAME_OF_PUBLISHER;
230     eventInfo.pid = getpid();
231     eventInfo.uid = getuid();
232     eventInfo.eventName = TEST_EVENT_NAME;
233     eventReport->SendHiSysEvent(PUBLISH, eventInfo);
234 
235     GTEST_LOG_(INFO) << "SendPublishSysEvent_0100 end";
236 }
237 }  // namespace EventFwk
238 }  // namespace OHOS