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
18 #define private public
19 #include "ams_mgr_scheduler.h"
20 #undef private
21 #include "app_mgr_interface.h"
22 #include "hilog_tag_wrapper.h"
23 #include "mock_bundle_manager.h"
24 #include "mock_native_token.h"
25 #include "mock_sa_call.h"
26 #include "system_ability_definition.h"
27 #include "sys_mgr_client.h"
28
29 using namespace testing::ext;
30 using namespace OHOS;
31 using namespace OHOS::AppExecFwk;
32
33 namespace {
34 const std::string STRING_BUNDLE_NAME = "com.example.bundle";
35
36 constexpr int ACCOUNT_ID = 100;
37 } // namespace
38
39 class AmsMgrKillProcessTest : public testing::Test {
40 public:
41 static void SetUpTestCase(void);
42 static void TearDownTestCase(void);
43 void SetUp(void) override;
44 void TearDown(void) override;
45
46 std::shared_ptr<AppMgrServiceInner> GetAppMgrServiceInner(void);
47 sptr<IAppMgr> GetAppMgrProxy(void);
48 };
49
SetUpTestCase(void)50 void AmsMgrKillProcessTest::SetUpTestCase(void)
51 {
52 MockNativeToken::SetNativeToken();
53 }
54
TearDownTestCase(void)55 void AmsMgrKillProcessTest::TearDownTestCase(void)
56 {}
57
SetUp(void)58 void AmsMgrKillProcessTest::SetUp(void)
59 {}
60
TearDown(void)61 void AmsMgrKillProcessTest::TearDown(void)
62 {}
63
GetAppMgrServiceInner(void)64 std::shared_ptr<AppMgrServiceInner> AmsMgrKillProcessTest::GetAppMgrServiceInner(void)
65 {
66 auto appMgrServiceInner = std::make_shared<AppMgrServiceInner>();
67 EXPECT_NE(appMgrServiceInner, nullptr);
68 auto bundleMgr = DelayedSingleton<BundleMgrHelper>::GetInstance();
69 appMgrServiceInner->remoteClientManager_->SetBundleManagerHelper(bundleMgr);
70
71 return appMgrServiceInner;
72 }
73
GetAppMgrProxy(void)74 sptr<IAppMgr> AmsMgrKillProcessTest::GetAppMgrProxy(void)
75 {
76 auto instance = DelayedSingleton<SysMrgClient>::GetInstance();
77 EXPECT_NE(instance, nullptr);
78
79 auto object = instance->GetSystemAbility(APP_MGR_SERVICE_ID);
80 EXPECT_NE(object, nullptr);
81
82 auto proxy = iface_cast<IAppMgr>(object);
83 EXPECT_NE(proxy, nullptr);
84
85 return proxy;
86 }
87
88 /*
89 * Feature: AppMgrServiceInner
90 * Function: KillApplicationByUserId
91 * SubFunction: NA
92 * FunctionPoints:Kill process
93 * EnvConditions: NA
94 * CaseDescription: creat AppMgrServiceInner object, call function.
95 */
96 HWTEST_F(AmsMgrKillProcessTest, KillProcess_0100, TestSize.Level0)
97 {
98 TAG_LOGI(AAFwkTag::TEST, "AmsMgrKillProcessTest_KillProcess_0100");
99
100 AAFwk::IsMockSaCall::IsMockSaCallWithPermission();
101 auto appMgrServiceInner = GetAppMgrServiceInner();
102 EXPECT_NE(appMgrServiceInner, nullptr);
103
104 ErrCode result = appMgrServiceInner->KillApplicationByUserId(STRING_BUNDLE_NAME, 0, ACCOUNT_ID);
105 EXPECT_EQ(result, ERR_OK);
106 }
107
108 /*
109 * Feature: AppMgrProxy
110 * Function: KillApplicationByUserId
111 * SubFunction: NA
112 * FunctionPoints:Kill process
113 * EnvConditions: NA
114 * CaseDescription: creat AppMgrProxy object, call function.
115 */
116 HWTEST_F(AmsMgrKillProcessTest, KillProcess_0200, TestSize.Level0)
117 {
118 TAG_LOGI(AAFwkTag::TEST, "AmsMgrKillProcessTest_KillProcess_0200");
119
120 AAFwk::IsMockSaCall::IsMockSaCallWithPermission();
121 auto proxy = GetAppMgrProxy();
122 EXPECT_NE(proxy, nullptr);
123
124 ErrCode result = proxy->GetAmsMgr()->KillProcessWithAccount(STRING_BUNDLE_NAME, ACCOUNT_ID);
125 EXPECT_EQ(result, ERR_OK);
126 }
127