1 /*
2  * Copyright (c) 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 
18 #define private public
19 #include "app_mgr_event.h"
20 #undef private
21 
22 using namespace testing;
23 using namespace testing::ext;
24 
25 namespace OHOS {
26 namespace AppExecFwk {
27 
28 class AppMgrEventTest : public testing::Test {
29 public:
30     static void SetUpTestCase();
31     static void TearDownTestCase();
32     void SetUp() override;
33     void TearDown() override;
34 };
35 
SetUpTestCase(void)36 void AppMgrEventTest::SetUpTestCase(void)
37 {}
38 
TearDownTestCase(void)39 void AppMgrEventTest::TearDownTestCase(void)
40 {}
41 
SetUp()42 void AppMgrEventTest::SetUp()
43 {}
44 
TearDown()45 void AppMgrEventTest::TearDown()
46 {}
47 
48 /**
49  * @tc.name: SendCreateAtomicServiceProcessEvent
50  * @tc.desc: Send Create Atomic service process event, appRecord is nullptr
51  * @tc.type: FUNC
52  */
53 HWTEST_F(AppMgrEventTest, SendCreateAtomicServiceProcessEvent_0100, TestSize.Level1)
54 {
55     std::shared_ptr<AppRunningRecord> callerAppRecord = nullptr;
56     std::shared_ptr<AppRunningRecord> appRecord = nullptr;
57     std::string moduleName = "testModuleName";
58     std::string abilityName = "testAbilityName";
59     bool ret = AppMgrEventUtil::SendCreateAtomicServiceProcessEvent(callerAppRecord, appRecord,
60         moduleName, abilityName);
61     EXPECT_EQ(ret, false);
62 }
63 
64 /**
65  * @tc.name: SendCreateAtomicServiceProcessEvent
66  * @tc.desc: Send Create Atomic service process event, callerAppRecord is nullptr
67  * @tc.type: FUNC
68  */
69 HWTEST_F(AppMgrEventTest, SendCreateAtomicServiceProcessEvent_0200, TestSize.Level1)
70 {
71     auto appInfo = std::make_shared<ApplicationInfo>();
72     EXPECT_NE(appInfo, nullptr);
73     appInfo->bundleName = "testBundleName";
74     appInfo->name = "testBundleName";
75 
76     int32_t recordId = 1;
77     std::string processName = "testProcess";
78     auto appRecord = std::make_shared<AppRunningRecord>(appInfo, recordId, processName);
79     appRecord->SetCallerUid(-1);
80     EXPECT_NE(appRecord, nullptr);
81 
82     std::shared_ptr<AppRunningRecord> callerAppRecord = nullptr;
83     std::string moduleName = "testModuleName";
84     std::string abilityName = "testAbilityName";
85     bool ret = AppMgrEventUtil::SendCreateAtomicServiceProcessEvent(callerAppRecord, appRecord,
86         moduleName, abilityName);
87     EXPECT_EQ(ret, true);
88 }
89 
90 /**
91  * @tc.name: SendCreateAtomicServiceProcessEvent
92  * @tc.desc: Send Create Atomic service process event, callerAppRecord is not nullptr
93  * @tc.type: FUNC
94  */
95 HWTEST_F(AppMgrEventTest, SendCreateAtomicServiceProcessEvent_0300, TestSize.Level1)
96 {
97     auto appInfo = std::make_shared<ApplicationInfo>();
98     EXPECT_NE(appInfo, nullptr);
99     appInfo->bundleName = "testBundleName";
100     appInfo->name = "testBundleName";
101 
102     int32_t recordId = 1;
103     std::string processName = "testProcess";
104     auto appRecord = std::make_shared<AppRunningRecord>(appInfo, recordId, processName);
105     EXPECT_NE(appRecord, nullptr);
106     appRecord->SetCallerUid(-1);
107 
108     auto callerAppInfo = std::make_shared<ApplicationInfo>();
109     EXPECT_NE(callerAppInfo, nullptr);
110     callerAppInfo->bundleName = "testCallerBundleName";
111     callerAppInfo->name = "testCallerBundleName";
112     std::string callerProcessName = "testCallerProcess";
113     int32_t callerRecordId = 2;
114     auto callerAppRecord = std::make_shared<AppRunningRecord>(callerAppInfo, callerRecordId, callerProcessName);
115     EXPECT_NE(callerAppRecord, nullptr);
116 
117     std::string moduleName = "testModuleName";
118     std::string abilityName = "testAbilityName";
119     bool ret = AppMgrEventUtil::SendCreateAtomicServiceProcessEvent(callerAppRecord, appRecord,
120         moduleName, abilityName);
121     EXPECT_EQ(ret, true);
122 }
123 
124 /**
125  * @tc.name: SendProcessStartEvent
126  * @tc.desc: Send process start event, appRecord is nullptr
127  * @tc.type: FUNC
128  */
129 HWTEST_F(AppMgrEventTest, SendProcessStartEvent_0100, TestSize.Level1)
130 {
131     std::shared_ptr<AppRunningRecord> callerAppRecord = nullptr;
132     std::shared_ptr<AppRunningRecord> appRecord = nullptr;
133     AAFwk::EventInfo eventInfo;
134     bool ret = AppMgrEventUtil::SendProcessStartEvent(callerAppRecord, appRecord, eventInfo);
135     EXPECT_EQ(ret, false);
136 }
137 
138 /**
139  * @tc.name: SendProcessStartEvent
140  * @tc.desc: Send process start event: callerAppRecord is nullptr
141  * @tc.type: FUNC
142  */
143 HWTEST_F(AppMgrEventTest, SendProcessStartEvent_0200, TestSize.Level1)
144 {
145     auto appInfo = std::make_shared<ApplicationInfo>();
146     EXPECT_NE(appInfo, nullptr);
147 
148     appInfo->bundleName = "testBundleName";
149     appInfo->name = "testBundleName";
150 
151     int32_t recordId = 1;
152     std::string processName = "testProcess";
153     auto appRecord = std::make_shared<AppRunningRecord>(appInfo, recordId, processName);
154     appRecord->SetCallerUid(-1);
155     appRecord->priorityObject_->SetPid(1001);
156     EXPECT_NE(appRecord, nullptr);
157 
158     std::shared_ptr<AppRunningRecord> callerAppRecord = nullptr;
159 
160     AAFwk::EventInfo eventInfo;
161     bool ret = AppMgrEventUtil::SendProcessStartEvent(callerAppRecord, appRecord, eventInfo);
162     EXPECT_EQ(ret, true);
163 }
164 
165 /**
166  * @tc.name: SendProcessStartEvent
167  * @tc.desc: Send process start event, callerAppRecord is not nullptr
168  * @tc.type: FUNC
169  */
170 HWTEST_F(AppMgrEventTest, SendProcessStartEvent_0300, TestSize.Level1)
171 {
172     auto appInfo = std::make_shared<ApplicationInfo>();
173     EXPECT_NE(appInfo, nullptr);
174     appInfo->bundleName = "testBundleName";
175     appInfo->name = "testBundleName";
176 
177     int32_t recordId = 1;
178     std::string processName = "testProcess";
179     auto appRecord = std::make_shared<AppRunningRecord>(appInfo, recordId, processName);
180     EXPECT_NE(appRecord, nullptr);
181     appRecord->SetCallerUid(-1);
182 
183     auto callerAppInfo = std::make_shared<ApplicationInfo>();
184     EXPECT_NE(callerAppInfo, nullptr);
185     callerAppInfo->bundleName = "testCallerBundleName";
186     callerAppInfo->name = "testCallerBundleName";
187     std::string callerProcessName = "testCallerProcess";
188     int32_t callerRecordId = 2;
189     auto callerAppRecord = std::make_shared<AppRunningRecord>(callerAppInfo, callerRecordId, callerProcessName);
190     EXPECT_NE(callerAppRecord, nullptr);
191 
192     AAFwk::EventInfo eventInfo;
193     bool ret = AppMgrEventUtil::SendProcessStartEvent(callerAppRecord, appRecord, eventInfo);
194     EXPECT_EQ(ret, true);
195 }
196 
197 /**
198  * @tc.name: SendProcessStartFailedEvent_0100
199  * @tc.desc: Send process start failed event
200  * @tc.type: FUNC
201  */
202 HWTEST_F(AppMgrEventTest, SendProcessStartFailedEvent_0100, TestSize.Level1)
203 {
204     std::shared_ptr<AppRunningRecord> callerAppRecord = nullptr;
205     std::shared_ptr<AppRunningRecord> appRecord = nullptr;
206     AAFwk::EventInfo eventInfo;
207     bool ret = AppMgrEventUtil::SendProcessStartFailedEvent(callerAppRecord, appRecord, eventInfo);
208     EXPECT_EQ(ret, false);
209 }
210 
211 /**
212  * @tc.name: SendProcessStartFailedEvent_0200
213  * @tc.desc: Send process start failed event
214  * @tc.type: FUNC
215  */
216 HWTEST_F(AppMgrEventTest, SendProcessStartFailedEvent_0200, TestSize.Level1)
217 {
218     auto appInfo = std::make_shared<ApplicationInfo>();
219     EXPECT_NE(appInfo, nullptr);
220 
221     appInfo->bundleName = "testBundleName";
222     appInfo->name = "testBundleName";
223 
224     int32_t recordId = 1;
225     std::string processName = "testProcess";
226     auto appRecord = std::make_shared<AppRunningRecord>(appInfo, recordId, processName);
227     appRecord->SetCallerUid(-1);
228     appRecord->priorityObject_->SetPid(1001);
229     EXPECT_NE(appRecord, nullptr);
230 
231     std::shared_ptr<AppRunningRecord> callerAppRecord = nullptr;
232 
233     AAFwk::EventInfo eventInfo;
234     bool ret = AppMgrEventUtil::SendProcessStartFailedEvent(callerAppRecord, appRecord, eventInfo);
235     EXPECT_EQ(ret, true);
236 }
237 
238 /**
239  * @tc.name: SendProcessStartFailedEvent_0300
240  * @tc.desc: Send process start failed event
241  * @tc.type: FUNC
242  */
243 HWTEST_F(AppMgrEventTest, SendProcessStartFailedEvent_0300, TestSize.Level1)
244 {
245     auto appInfo = std::make_shared<ApplicationInfo>();
246     EXPECT_NE(appInfo, nullptr);
247     appInfo->bundleName = "testBundleName";
248     appInfo->name = "testBundleName";
249 
250     int32_t recordId = 1;
251     std::string processName = "testProcess";
252     auto appRecord = std::make_shared<AppRunningRecord>(appInfo, recordId, processName);
253     EXPECT_NE(appRecord, nullptr);
254     appRecord->SetCallerUid(-1);
255 
256     auto callerAppInfo = std::make_shared<ApplicationInfo>();
257     EXPECT_NE(callerAppInfo, nullptr);
258     callerAppInfo->bundleName = "testCallerBundleName";
259     callerAppInfo->name = "testCallerBundleName";
260     std::string callerProcessName = "testCallerProcess";
261     int32_t callerRecordId = 2;
262     auto callerAppRecord = std::make_shared<AppRunningRecord>(callerAppInfo, callerRecordId, callerProcessName);
263     EXPECT_NE(callerAppRecord, nullptr);
264 
265     AAFwk::EventInfo eventInfo;
266     bool ret = AppMgrEventUtil::SendProcessStartFailedEvent(callerAppRecord, appRecord, eventInfo);
267     EXPECT_EQ(ret, true);
268 }
269 
270 /**
271  * @tc.name: SendRenderProcessStartFailedEvent_0100
272  * @tc.desc: Send render process start failed event
273  * @tc.type: FUNC
274  */
275 HWTEST_F(AppMgrEventTest, SendRenderProcessStartFailedEvent_0100, TestSize.Level1)
276 {
277     auto appInfo = std::make_shared<ApplicationInfo>();
278     EXPECT_NE(appInfo, nullptr);
279     appInfo->bundleName = "testBundleName";
280     appInfo->name = "testBundleName";
281 
282     std::string processName = "testProcess";
283     int32_t recordId = 1;
284     pid_t hostPid = 1001;
285     auto hostRecord = std::make_shared<AppRunningRecord>(appInfo, recordId, processName);
286     hostRecord->priorityObject_->SetPid(hostPid);
287     hostRecord->SetUid(100);
288     EXPECT_NE(hostRecord, nullptr);
289 
290     std::string renderParam = "test_render_param";
291     int32_t ipcFd = 1;
292     int32_t sharedFd = 1;
293     int32_t crashFd = 1;
294     std::shared_ptr<RenderRecord> renderRecord =
295         RenderRecord::CreateRenderRecord(hostPid, renderParam, ipcFd, sharedFd, crashFd, hostRecord);
296 
297     bool ret = AppMgrEventUtil::SendRenderProcessStartFailedEvent(renderRecord,
298         ProcessStartFailedReason::APPSPAWN_FAILED, 123);
299     EXPECT_EQ(ret, true);
300 }
301 } // namespace AppExecFwk
302 } // namespace OHOS
303