• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 <unistd.h>
17 #include <gtest/gtest.h>
18 
19 #include "app_mgr_proxy.h"
20 #include "app_record_id.h"
21 #include "app_scheduler_proxy.h"
22 #include "application_state_observer_stub.h"
23 #include "errors.h"
24 #include "hilog_tag_wrapper.h"
25 #include "ipc_types.h"
26 #include "mock_application.h"
27 #include "mock_app_mgr_service.h"
28 #include "mock_kia_interceptor.h"
29 #include "native_child_notify_stub.h"
30 
31 using namespace testing::ext;
32 
33 using OHOS::iface_cast;
34 using OHOS::sptr;
35 using testing::_;
36 using testing::Invoke;
37 using testing::InvokeWithoutArgs;
38 using testing::Return;
39 
40 namespace OHOS {
41 namespace AppExecFwk {
42 
43 class NativeChildCallbackMock : public NativeChildNotifyStub {
44 public:
45     NativeChildCallbackMock() = default;
46     virtual ~NativeChildCallbackMock() = default;
47 
OnNativeChildStarted(const sptr<IRemoteObject> & nativeChild)48     void OnNativeChildStarted(const sptr<IRemoteObject> &nativeChild) {}
OnError(int32_t errCode)49     void OnError(int32_t errCode) {}
OnNativeChildExit(int32_t pid,int32_t signal)50     int32_t OnNativeChildExit(int32_t pid, int32_t signal) { return 0; }
51 };
52 
53 class AmsIpcAppMgrInterfaceTest : public testing::Test {
54 public:
55     static void SetUpTestCase();
56     static void TearDownTestCase();
57     void SetUp();
58     void TearDown();
59 };
60 
SetUpTestCase()61 void AmsIpcAppMgrInterfaceTest::SetUpTestCase()
62 {}
63 
TearDownTestCase()64 void AmsIpcAppMgrInterfaceTest::TearDownTestCase()
65 {}
66 
SetUp()67 void AmsIpcAppMgrInterfaceTest::SetUp()
68 {}
69 
TearDown()70 void AmsIpcAppMgrInterfaceTest::TearDown()
71 {}
72 
73 /*
74  * Feature: AMS
75  * Function: IPC
76  * SubFunction: appmgr interface
77  * FunctionPoints: interface
78  * CaseDescription: test interface of AttachApplication
79  */
80 HWTEST_F(AmsIpcAppMgrInterfaceTest, Interface_001, TestSize.Level1)
81 {
82     TAG_LOGD(AAFwkTag::TEST, "AppMgrIpcInterfaceTest_AppMgr_001 start");
83     sptr<MockAppMgrService> mockAppMgr(new MockAppMgrService());
84     sptr<IAppMgr> appMgrClient = iface_cast<IAppMgr>(mockAppMgr);
85     sptr<MockApplication> app(new MockApplication());
86 
87     EXPECT_CALL(*mockAppMgr, AttachApplication(_))
88         .WillOnce(InvokeWithoutArgs(mockAppMgr.GetRefPtr(), &MockAppMgrService::Post));
89     appMgrClient->AttachApplication(app);
90     mockAppMgr->Wait();
91     TAG_LOGD(AAFwkTag::TEST, "AppMgrIpcInterfaceTest_AppMgr_001 end");
92 }
93 
94 /*
95  * Feature: AMS
96  * Function: IPC
97  * SubFunction: appmgr interface
98  * FunctionPoints: interface
99  * CaseDescription: test interface of ApplicationForegrounded
100  */
101 HWTEST_F(AmsIpcAppMgrInterfaceTest, Interface_002, TestSize.Level1)
102 {
103     TAG_LOGD(AAFwkTag::TEST, "AppMgrIpcInterfaceTest_AppMgr_002 start");
104     sptr<MockAppMgrService> mockAppMgr(new MockAppMgrService());
105     sptr<IAppMgr> appMgrClient = iface_cast<IAppMgr>(mockAppMgr);
106 
107     EXPECT_CALL(*mockAppMgr, ApplicationForegrounded(_))
108         .WillOnce(InvokeWithoutArgs(mockAppMgr.GetRefPtr(), &MockAppMgrService::Post));
109     auto recordId = AppRecordId::Create();
110     appMgrClient->ApplicationForegrounded(recordId);
111     mockAppMgr->Wait();
112     TAG_LOGD(AAFwkTag::TEST, "AppMgrIpcInterfaceTest_AppMgr_002 end");
113 }
114 
115 /*
116  * Feature: AMS
117  * Function: IPC
118  * SubFunction: appmgr interface
119  * FunctionPoints: interface
120  * CaseDescription: test interface of ApplicationBackgrounded
121  */
122 HWTEST_F(AmsIpcAppMgrInterfaceTest, Interface_003, TestSize.Level1)
123 {
124     TAG_LOGD(AAFwkTag::TEST, "AppMgrIpcInterfaceTest_AppMgr_003 start");
125     sptr<MockAppMgrService> mockAppMgr(new MockAppMgrService());
126     sptr<IAppMgr> appMgrClient = iface_cast<IAppMgr>(mockAppMgr);
127 
128     EXPECT_CALL(*mockAppMgr, ApplicationBackgrounded(_))
129         .WillOnce(InvokeWithoutArgs(mockAppMgr.GetRefPtr(), &MockAppMgrService::Post));
130     auto recordId = AppRecordId::Create();
131     appMgrClient->ApplicationBackgrounded(recordId);
132     mockAppMgr->Wait();
133     TAG_LOGD(AAFwkTag::TEST, "AppMgrIpcInterfaceTest_AppMgr_003 end");
134 }
135 
136 /*
137  * Feature: AMS
138  * Function: IPC
139  * SubFunction: appmgr interface
140  * FunctionPoints: interface
141  * CaseDescription: test interface of ApplicationTerminated
142  */
143 HWTEST_F(AmsIpcAppMgrInterfaceTest, Interface_004, TestSize.Level1)
144 {
145     TAG_LOGD(AAFwkTag::TEST, "AppMgrIpcInterfaceTest_AppMgr_004 start");
146     sptr<MockAppMgrService> mockAppMgr(new MockAppMgrService());
147     sptr<IAppMgr> appMgrClient = iface_cast<IAppMgr>(mockAppMgr);
148 
149     EXPECT_CALL(*mockAppMgr, ApplicationTerminated(_))
150         .WillOnce(InvokeWithoutArgs(mockAppMgr.GetRefPtr(), &MockAppMgrService::Post));
151     auto recordId = AppRecordId::Create();
152     appMgrClient->ApplicationTerminated(recordId);
153     mockAppMgr->Wait();
154     TAG_LOGD(AAFwkTag::TEST, "AppMgrIpcInterfaceTest_AppMgr_004 end");
155 }
156 
157 /*
158  * Feature: AMS
159  * Function: IPC
160  * SubFunction: appmgr interface
161  * FunctionPoints: KillApplication interface
162  * CaseDescription: test IPC can transact data
163  */
164 HWTEST_F(AmsIpcAppMgrInterfaceTest, ClearUpApplicationData_008, TestSize.Level1)
165 {
166     TAG_LOGD(AAFwkTag::TEST, "ClearUpApplicationData_008 start");
167 
168     sptr<MockAppMgrService> mockAppMgr(new MockAppMgrService());
169     sptr<IAppMgr> appMgrClient = iface_cast<IAppMgr>(mockAppMgr);
170 
171     EXPECT_CALL(*mockAppMgr, ClearUpApplicationData(_, _, _)).Times(1);
172 
173     appMgrClient->ClearUpApplicationData("PROCESS", 0);
174 
175     TAG_LOGD(AAFwkTag::TEST, "ClearUpApplicationData_008 end");
176 }
177 
178 /*
179  * Feature: AMS
180  * Function: IPC
181  * SubFunction: appmgr interface
182  * FunctionPoints: KillApplication interface
183  * CaseDescription: test IPC can transact data
184  */
185 HWTEST_F(AmsIpcAppMgrInterfaceTest, GetAllRunningProcesses_010, TestSize.Level1)
186 {
187     TAG_LOGD(AAFwkTag::TEST, "GetAllRunningProcesses_009 start");
188 
189     sptr<MockAppMgrService> mockAppMgr(new MockAppMgrService());
190     sptr<IAppMgr> appMgrClient = iface_cast<IAppMgr>(mockAppMgr);
191 
192     EXPECT_CALL(*mockAppMgr, GetAllRunningProcesses(_)).Times(1).WillOnce(Return(OHOS::ERR_NULL_OBJECT));
193 
194     std::vector<RunningProcessInfo> runningProcessInfo;
195     int32_t ret = appMgrClient->GetAllRunningProcesses(runningProcessInfo);
196     EXPECT_EQ(ret, OHOS::ERR_NULL_OBJECT);
197 
198     EXPECT_CALL(*mockAppMgr, GetAllRunningProcesses(_)).Times(1).WillOnce(Return(OHOS::ERR_NONE));
199     ret = appMgrClient->GetAllRunningProcesses(runningProcessInfo);
200     EXPECT_EQ(ret, OHOS::ERR_NONE);
201 
202     TAG_LOGD(AAFwkTag::TEST, "GetAllRunningProcesses_009 end");
203 }
204 
205 /*
206  * @tc.name: RegisterApplicationStateObserver_001
207  * @tc.desc: Register application state observer test.
208  * @tc.type: FUNC
209  * @tc.require: issueI5822Q
210  */
211 HWTEST_F(AmsIpcAppMgrInterfaceTest, RegisterApplicationStateObserver_001, TestSize.Level0)
212 {
213     TAG_LOGD(AAFwkTag::TEST, "RegisterApplicationStateObserver_001 start");
214 
215     sptr<IApplicationStateObserver> observer = new ApplicationStateObserverStub();
216     std::vector<std::string> bundleNameList;
217     sptr<MockAppMgrService> mockAppMgr(new MockAppMgrService());
218     sptr<IAppMgr> appMgrClient = iface_cast<IAppMgr>(mockAppMgr);
219 
220     EXPECT_CALL(*mockAppMgr, RegisterApplicationStateObserver(_, _)).Times(1).WillOnce(Return(OHOS::NO_ERROR));
221 
222     int32_t err = appMgrClient->RegisterApplicationStateObserver(observer, bundleNameList);
223 
224     EXPECT_EQ(OHOS::NO_ERROR, err);
225 
226     TAG_LOGD(AAFwkTag::TEST, "RegisterApplicationStateObserver_001 end");
227 }
228 
229 /*
230  * Feature: AMS
231  * Function: IPC
232  * SubFunction: appmgr interface
233  * FunctionPoints: KillApplication interface
234  * CaseDescription: test IPC can transact data
235  */
236 HWTEST_F(AmsIpcAppMgrInterfaceTest, UnregisterApplicationStateObserver_001, TestSize.Level0)
237 {
238     TAG_LOGD(AAFwkTag::TEST, "UnregisterApplicationStateObserver_001 start");
239 
240     sptr<IApplicationStateObserver> observer = new ApplicationStateObserverStub();
241     sptr<MockAppMgrService> mockAppMgr(new MockAppMgrService());
242     sptr<IAppMgr> appMgrClient = iface_cast<IAppMgr>(mockAppMgr);
243 
244     EXPECT_CALL(*mockAppMgr, UnregisterApplicationStateObserver(_)).Times(1).WillOnce(Return(OHOS::NO_ERROR));
245 
246     int32_t err = appMgrClient->UnregisterApplicationStateObserver(observer);
247 
248     EXPECT_EQ(OHOS::NO_ERROR, err);
249 
250     TAG_LOGD(AAFwkTag::TEST, "UnregisterApplicationStateObserver_001 end");
251 }
252 
253 /*
254  * @tc.name: RegisterNativeChildExitNotify_001
255  * @tc.desc: Register native child exit notify test.
256  * @tc.type: FUNC
257  * @tc.require: issueI5822Q
258  */
259 HWTEST_F(AmsIpcAppMgrInterfaceTest, RegisterNativeChildExitNotify_001, TestSize.Level0)
260 {
261     TAG_LOGD(AAFwkTag::TEST, "RegisterNativeChildExitNotify_001 start");
262 
263     sptr<INativeChildNotify> notify = new NativeChildCallbackMock();
264     sptr<MockAppMgrService> mockAppMgr(new MockAppMgrService());
265     sptr<IAppMgr> appMgrClient = iface_cast<IAppMgr>(mockAppMgr);
266 
267     EXPECT_CALL(*mockAppMgr, RegisterNativeChildExitNotify(_)).Times(1).WillOnce(Return(OHOS::NO_ERROR));
268 
269     int32_t err = appMgrClient->RegisterNativeChildExitNotify(notify);
270 
271     EXPECT_EQ(OHOS::NO_ERROR, err);
272 
273     TAG_LOGD(AAFwkTag::TEST, "RegisterNativeChildExitNotify_001 end");
274 }
275 
276 /*
277  * @tc.name: UnregisterNativeChildExitNotify_001
278  * @tc.desc: Unregister native child exit notify test.
279  * @tc.type: FUNC
280  * @tc.require: issueI5822Q
281  */
282 HWTEST_F(AmsIpcAppMgrInterfaceTest, UnregisterNativeChildExitNotify_001, TestSize.Level0)
283 {
284     TAG_LOGD(AAFwkTag::TEST, "UnregisterNativeChildExitNotify_001 start");
285 
286     sptr<INativeChildNotify> notify = new NativeChildCallbackMock();
287     sptr<MockAppMgrService> mockAppMgr(new MockAppMgrService());
288     sptr<IAppMgr> appMgrClient = iface_cast<IAppMgr>(mockAppMgr);
289 
290     EXPECT_CALL(*mockAppMgr, UnregisterNativeChildExitNotify(_)).Times(1).WillOnce(Return(OHOS::NO_ERROR));
291 
292     int32_t err = appMgrClient->UnregisterNativeChildExitNotify(notify);
293 
294     EXPECT_EQ(OHOS::NO_ERROR, err);
295 
296     TAG_LOGD(AAFwkTag::TEST, "UnregisterNativeChildExitNotify_001 end");
297 }
298 
299 /*
300  * @tc.name: RegisterKiaInterceptor_001
301  * @tc.desc: Register kia interceptor test.
302  * @tc.type: FUNC
303  */
304 HWTEST_F(AmsIpcAppMgrInterfaceTest, RegisterKiaInterceptor_001, TestSize.Level2)
305 {
306     TAG_LOGD(AAFwkTag::TEST, "RegisterKiaInterceptor_001 start");
307 
308     sptr<MockAppMgrService> mockAppMgr(new MockAppMgrService());
309     sptr<IAppMgr> appMgrClient = iface_cast<IAppMgr>(mockAppMgr);
310 
311     EXPECT_CALL(*mockAppMgr, RegisterKiaInterceptor(_)).Times(1).WillOnce(Return(ERR_OK));
312 
313     sptr<IKiaInterceptor> interceptor = new MockKiaInterceptor();
314     int32_t err = appMgrClient->RegisterKiaInterceptor(interceptor);
315     EXPECT_EQ(ERR_OK, err);
316 
317     TAG_LOGD(AAFwkTag::TEST, "RegisterKiaInterceptor_001 end");
318 }
319 
320 /*
321  * @tc.name: RegisterKiaInterceptor_002
322  * @tc.desc: Register kia interceptor test.
323  * @tc.type: FUNC
324  */
325 HWTEST_F(AmsIpcAppMgrInterfaceTest, RegisterKiaInterceptor_002, TestSize.Level2)
326 {
327     TAG_LOGD(AAFwkTag::TEST, "RegisterKiaInterceptor_002 start");
328 
329     sptr<MockAppMgrService> mockAppMgr(new MockAppMgrService());
330     sptr<IAppMgr> appMgrClient = iface_cast<IAppMgr>(mockAppMgr);
331 
332     EXPECT_CALL(*mockAppMgr, RegisterKiaInterceptor(_)).Times(1).WillOnce(Return(ERR_INVALID_VALUE));
333 
334     sptr<IKiaInterceptor> interceptor = new MockKiaInterceptor();
335     int32_t err = appMgrClient->RegisterKiaInterceptor(interceptor);
336     EXPECT_EQ(ERR_INVALID_VALUE, err);
337 
338     TAG_LOGD(AAFwkTag::TEST, "RegisterKiaInterceptor_002 end");
339 }
340 
341 /*
342  * @tc.name: CheckIsKiaProcess_001
343  * @tc.desc: Check if a process is kia protected.
344  * @tc.type: FUNC
345  */
346 HWTEST_F(AmsIpcAppMgrInterfaceTest, CheckIsKiaProcess_001, TestSize.Level2)
347 {
348     TAG_LOGD(AAFwkTag::TEST, "CheckIsKiaProcess_001 start");
349 
350     sptr<MockAppMgrService> mockAppMgr(new MockAppMgrService());
351     sptr<IAppMgr> appMgrClient = iface_cast<IAppMgr>(mockAppMgr);
352 
353     EXPECT_CALL(*mockAppMgr, CheckIsKiaProcess(_, _)).Times(1).WillOnce(Return(ERR_OK));
354 
355     pid_t pid = 1234;
356     bool isKia = false;
357     int32_t err = appMgrClient->CheckIsKiaProcess(pid, isKia);
358     EXPECT_EQ(ERR_OK, err);
359 
360     TAG_LOGD(AAFwkTag::TEST, "CheckIsKiaProcess_001 end");
361 }
362 
363 /*
364  * @tc.name: CheckIsKiaProcess_002
365  * @tc.desc: Check if a process is kia protected.
366  * @tc.type: FUNC
367  */
368 HWTEST_F(AmsIpcAppMgrInterfaceTest, CheckIsKiaProcess_002, TestSize.Level2)
369 {
370     TAG_LOGD(AAFwkTag::TEST, "CheckIsKiaProcess_002 start");
371 
372     sptr<MockAppMgrService> mockAppMgr(new MockAppMgrService());
373     sptr<IAppMgr> appMgrClient = iface_cast<IAppMgr>(mockAppMgr);
374 
375     EXPECT_CALL(*mockAppMgr, CheckIsKiaProcess(_, _)).Times(1).WillOnce(Return(ERR_INVALID_VALUE));
376 
377     pid_t pid = 1234;
378     bool isKia = false;
379     int32_t err = appMgrClient->CheckIsKiaProcess(pid, isKia);
380     EXPECT_EQ(ERR_INVALID_VALUE, err);
381 
382     TAG_LOGD(AAFwkTag::TEST, "CheckIsKiaProcess_002 end");
383 }
384 }  // namespace AppExecFwk
385 }  // namespace OHOS
386