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