• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #define protected public
17 #include "bundle_install_plugin_test.h"
18 #undef protected
19 
20 #include "allowed_install_bundles_plugin.h"
21 #include "bundle_mgr_proxy.h"
22 #include "disallowed_install_bundles_plugin.h"
23 #include "disallowed_uninstall_bundles_plugin.h"
24 #include "edm_constants.h"
25 #include "edm_sys_manager.h"
26 #include "if_system_ability_manager.h"
27 #include "iremote_stub.h"
28 #include "iservice_registry.h"
29 #include "system_ability_definition.h"
30 #include "utils.h"
31 
32 using namespace testing::ext;
33 
34 namespace OHOS {
35 namespace EDM {
36 namespace TEST {
37 const std::string TEST_BUNDLE = "testBundle";
38 
SetUpTestSuite(void)39 void BundleInstallPluginTest::SetUpTestSuite(void)
40 {
41     Utils::SetEdmInitialEnv();
42 }
43 
TearDownTestSuite(void)44 void BundleInstallPluginTest::TearDownTestSuite(void)
45 {
46     Utils::ResetTokenTypeAndUid();
47     ASSERT_TRUE(Utils::IsOriginalUTEnv());
48     std::cout << "now ut process is orignal ut env : " << Utils::IsOriginalUTEnv() << std::endl;
49 }
50 
51 /**
52  * @tc.name: TestOnSetPolicySuc
53  * @tc.desc: Test BundleInstallPlugin::OnSetPolicy when data is empty.
54  * @tc.type: FUNC
55  */
56 HWTEST_F(BundleInstallPluginTest, TestOnSetPolicySuc, TestSize.Level1)
57 {
58     BundleInstallPlugin plugin;
59     plugin.maxListSize_ = EdmConstants::APPID_MAX_SIZE;
60     for (int32_t policyType = static_cast<int32_t>(AppExecFwk::AppInstallControlRuleType::DISALLOWED_UNINSTALL);
61         policyType <= static_cast<int32_t>(AppExecFwk::AppInstallControlRuleType::DISALLOWED_INSTALL); policyType++) {
62         plugin.SetAppInstallControlRuleType(static_cast<AppExecFwk::AppInstallControlRuleType>(policyType));
63         std::vector<std::string> data;
64         std::vector<std::string> currentData;
65         std::vector<std::string> mergeData;
66         ErrCode ret = plugin.OnBasicSetPolicy(data, currentData, mergeData, DEFAULT_USER_ID);
67         ASSERT_TRUE(ret == ERR_OK);
68     }
69 }
70 
71 /**
72  * @tc.name: TestOnSetPolicyFail
73  * @tc.desc: Test BundleInstallPlugin::OnSetPolicy when data.size() > EdmConstants::APPID_MAX_SIZE.
74  * @tc.type: FUNC
75  */
76 HWTEST_F(BundleInstallPluginTest, TestOnSetPolicyFail, TestSize.Level1)
77 {
78     BundleInstallPlugin plugin;
79     plugin.maxListSize_ = EdmConstants::APPID_MAX_SIZE;
80     for (int32_t policyType = static_cast<int32_t>(AppExecFwk::AppInstallControlRuleType::DISALLOWED_UNINSTALL);
81         policyType <= static_cast<int32_t>(AppExecFwk::AppInstallControlRuleType::DISALLOWED_INSTALL); policyType++) {
82         plugin.SetAppInstallControlRuleType(static_cast<AppExecFwk::AppInstallControlRuleType>(policyType));
83         std::vector<std::string> data(EdmConstants::APPID_MAX_SIZE + 1, TEST_BUNDLE);
84         std::vector<std::string> currentData;
85         std::vector<std::string> mergeData;
86         ErrCode ret = plugin.OnBasicSetPolicy(data, currentData, mergeData, DEFAULT_USER_ID);
87         ASSERT_TRUE(ret == EdmReturnErrCode::PARAM_ERROR);
88     }
89 }
90 
91 /**
92  * @tc.name: TestOnSetPolicySysAbnormally
93  * @tc.desc: Test BundleInstallPluginTest::OnSetPolicy func is SYSTEM_ABNORMALLY.
94  * @tc.type: FUNC
95  */
96 HWTEST_F(BundleInstallPluginTest, TestOnSetPolicySysAbnormally, TestSize.Level1)
97 {
98     Utils::ResetTokenTypeAndUid();
99     BundleInstallPlugin plugin;
100     plugin.maxListSize_ = EdmConstants::APPID_MAX_SIZE;
101     for (int32_t policyType = static_cast<int32_t>(AppExecFwk::AppInstallControlRuleType::DISALLOWED_UNINSTALL);
102         policyType <= static_cast<int32_t>(AppExecFwk::AppInstallControlRuleType::DISALLOWED_INSTALL); policyType++) {
103         plugin.SetAppInstallControlRuleType(static_cast<AppExecFwk::AppInstallControlRuleType>(policyType));
104         std::vector<std::string> data = { TEST_BUNDLE };
105         std::vector<std::string> currentData;
106         std::vector<std::string> mergeData;
107         ErrCode ret = plugin.OnBasicSetPolicy(data, currentData, mergeData, DEFAULT_USER_ID);
108         ASSERT_TRUE(ret == EdmReturnErrCode::SYSTEM_ABNORMALLY);
109     }
110     Utils::SetEdmInitialEnv();
111 }
112 
113 /**
114  * @tc.name: TestOnGetPolicy
115  * @tc.desc: Test AllowedInstallBundlesPlugin::OnGetPolicy function.
116  * @tc.type: FUNC
117  */
118 HWTEST_F(BundleInstallPluginTest, TestOnGetPolicy, TestSize.Level1)
119 {
120     std::string policyData;
121     MessageParcel data;
122     MessageParcel reply;
123 
124     AllowedInstallBundlesPlugin allowedInsatllPlugin;
125     ErrCode ret = allowedInsatllPlugin.OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
126     ASSERT_TRUE(ret == ERR_OK);
127 
128     DisallowedInstallBundlesPlugin disallowedInsatllPlugin;
129     ret = disallowedInsatllPlugin.OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
130     ASSERT_TRUE(ret == ERR_OK);
131 
132     DisallowedUninstallBundlesPlugin disallowedUninsatllPlugin;
133     ret = disallowedUninsatllPlugin.OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
134     ASSERT_TRUE(ret == ERR_OK);
135 }
136 
137 /**
138  * @tc.name: TestAllowedInstallBundlesPlugin
139  * @tc.desc: Test AllowedInstallBundlesPlugin::OnRemovePolicy when data is empty.
140  * @tc.type: FUNC
141  */
142 HWTEST_F(BundleInstallPluginTest, TestAllowedInstallBundlesPlugin005, TestSize.Level1)
143 {
144     BundleInstallPlugin plugin;
145     plugin.maxListSize_ = EdmConstants::APPID_MAX_SIZE;
146     for (int32_t policyType = static_cast<int32_t>(AppExecFwk::AppInstallControlRuleType::DISALLOWED_UNINSTALL);
147         policyType <= static_cast<int32_t>(AppExecFwk::AppInstallControlRuleType::DISALLOWED_INSTALL); policyType++) {
148         plugin.SetAppInstallControlRuleType(static_cast<AppExecFwk::AppInstallControlRuleType>(policyType));
149         std::vector<std::string> data;
150         std::vector<std::string> currentData;
151         std::vector<std::string> mergeData;
152         ErrCode ret = plugin.OnBasicRemovePolicy(data, currentData, mergeData, DEFAULT_USER_ID);
153         ASSERT_TRUE(ret == ERR_OK);
154     }
155 }
156 
157 /**
158  * @tc.name: TestAllowedInstallBundlesPlugin
159  * @tc.desc: Test AllowedInstallBundlesPlugin::OnRemovePolicy func when it is SYSTEM_ABNORMALLY.
160  * @tc.type: FUNC
161  */
162 HWTEST_F(BundleInstallPluginTest, TestAllowedInstallBundlesPlugin006, TestSize.Level1)
163 {
164     Utils::ResetTokenTypeAndUid();
165     BundleInstallPlugin plugin;
166     plugin.maxListSize_ = EdmConstants::APPID_MAX_SIZE;
167     for (int32_t policyType = static_cast<int32_t>(AppExecFwk::AppInstallControlRuleType::DISALLOWED_UNINSTALL);
168         policyType <= static_cast<int32_t>(AppExecFwk::AppInstallControlRuleType::DISALLOWED_INSTALL); policyType++) {
169         plugin.SetAppInstallControlRuleType(static_cast<AppExecFwk::AppInstallControlRuleType>(policyType));
170         std::vector<std::string> data = { TEST_BUNDLE };
171         std::vector<std::string> currentData = { TEST_BUNDLE };
172         std::vector<std::string> mergeData;
173         ErrCode ret = plugin.OnBasicRemovePolicy(data, currentData, mergeData, DEFAULT_USER_ID);
174         ASSERT_TRUE(ret == EdmReturnErrCode::SYSTEM_ABNORMALLY);
175     }
176     Utils::SetEdmInitialEnv();
177 }
178 
179 /**
180  * @tc.name: TestAllowedInstallBundlesPlugin
181  * @tc.desc: Test AllowedInstallBundlesPlugin::OnRemovePolicy function success.
182  * @tc.type: FUNC
183  */
184 HWTEST_F(BundleInstallPluginTest, TestAllowedInstallBundlesPluginSuc, TestSize.Level1)
185 {
186     BundleInstallPlugin plugin;
187     plugin.maxListSize_ = EdmConstants::APPID_MAX_SIZE;
188     for (int32_t policyType = static_cast<int32_t>(AppExecFwk::AppInstallControlRuleType::DISALLOWED_UNINSTALL);
189         policyType <= static_cast<int32_t>(AppExecFwk::AppInstallControlRuleType::DISALLOWED_INSTALL); policyType++) {
190         plugin.SetAppInstallControlRuleType(static_cast<AppExecFwk::AppInstallControlRuleType>(policyType));
191         std::vector<std::string> data = { TEST_BUNDLE };
192         std::vector<std::string> currentData;
193         std::vector<std::string> mergeData;
194         ErrCode ret = plugin.OnBasicRemovePolicy(data, currentData, mergeData, DEFAULT_USER_ID);
195         ASSERT_TRUE(ret == ERR_OK);
196     }
197 }
198 
199 /**
200  * @tc.name: TestAllowedInstallBundlesPlugin
201  * @tc.desc: Test AllowedInstallBundlesPlugin::OnAdminRemoveDone func.
202  * @tc.type: FUNC
203  */
204 HWTEST_F(BundleInstallPluginTest, TestAllowedInstallBundlesPlugin007, TestSize.Level1)
205 {
206     sptr<AppExecFwk::BundleMgrProxy> bundleMgrProxy = iface_cast<AppExecFwk::BundleMgrProxy>(
207         EdmSysManager::GetRemoteObjectOfSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID));
208     sptr<AppExecFwk::IAppControlMgr> appControlProxy = bundleMgrProxy->GetAppControlProxy();
209     BundleInstallPlugin plugin;
210     plugin.maxListSize_ = EdmConstants::APPID_MAX_SIZE;
211     for (int32_t policyType = static_cast<int32_t>(AppExecFwk::AppInstallControlRuleType::DISALLOWED_UNINSTALL);
212         policyType <= static_cast<int32_t>(AppExecFwk::AppInstallControlRuleType::DISALLOWED_INSTALL); policyType++) {
213         plugin.SetAppInstallControlRuleType(static_cast<AppExecFwk::AppInstallControlRuleType>(policyType));
214         // set policy that "testBundle" is allowed to install.
215         std::vector<std::string> data = { TEST_BUNDLE };
216         std::vector<std::string> currentData;
217         std::vector<std::string> mergeData;
218         ErrCode res = plugin.OnBasicSetPolicy(data, currentData, mergeData, DEFAULT_USER_ID);
219         ASSERT_TRUE(res == ERR_OK);
220         ASSERT_TRUE(currentData.size() == 1);
221 
222         // get current policy.
223         std::vector<std::string> result;
224         res = appControlProxy->GetAppInstallControlRule(static_cast<AppExecFwk::AppInstallControlRuleType>(policyType),
225              DEFAULT_USER_ID, result);
226         ASSERT_TRUE(res == ERR_OK);
227         ASSERT_TRUE(result.size() == 1);
228         ASSERT_TRUE(result[0] == TEST_BUNDLE);
229 
230         // remove policy.
231         std::string adminName = TEST_BUNDLE;
232         mergeData.clear();
233         res = plugin.OnBasicAdminRemove(adminName, currentData, mergeData, DEFAULT_USER_ID);
234 
235         // get current policy.
236         result.clear();
237         res = appControlProxy->GetAppInstallControlRule(static_cast<AppExecFwk::AppInstallControlRuleType>(policyType),
238             DEFAULT_USER_ID, result);
239         ASSERT_TRUE(res == ERR_OK);
240         ASSERT_TRUE(result.size() == 0);
241     }
242 }
243 } // namespace TEST
244 } // namespace EDM
245 } // namespace OHOS