• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define protected public
17 #define private public
18 #include "manage_keep_alive_apps_plugin_test.h"
19 #include "disallowed_running_bundles_plugin.h"
20 #undef private
21 #undef protected
22 
23 #include "bundle_info.h"
24 #include "bundle_mgr_interface.h"
25 #include "if_system_ability_manager.h"
26 #include "iremote_stub.h"
27 #include "iservice_registry.h"
28 #include "parameters.h"
29 #include "system_ability_definition.h"
30 #include "array_string_serializer.h"
31 #include "edm_constants.h"
32 #include "edm_ipc_interface_code.h"
33 #include "edm_log.h"
34 #include "edm_sys_manager.h"
35 #include "func_code.h"
36 #include "utils.h"
37 
38 using namespace testing::ext;
39 
40 namespace OHOS {
41 namespace EDM {
42 namespace TEST {
SetUpTestSuite(void)43 void ManageKeepAliveAppsPluginTest::SetUpTestSuite(void)
44 {
45     Utils::SetEdmInitialEnv();
46 }
47 
TearDownTestSuite(void)48 void ManageKeepAliveAppsPluginTest::TearDownTestSuite(void)
49 {
50     Utils::ResetTokenTypeAndUid();
51     ASSERT_TRUE(Utils::IsOriginalUTEnv());
52     std::cout << "now ut process is orignal ut env : " << Utils::IsOriginalUTEnv() << std::endl;
53 }
54 
55 /**
56  * @tc.name: TestOnHandlePolicyAddFailWithNullData
57  * @tc.desc: Test ManageKeepAliveAppsPlugin::OnHandlePolicy add when data is empty.
58  * @tc.type: FUNC
59  */
60 HWTEST_F(ManageKeepAliveAppsPluginTest, TestOnHandlePolicyAddFailWithNullData, TestSize.Level1)
61 {
62     ManageKeepAliveAppsPlugin plugin;
63     MessageParcel data;
64     MessageParcel reply;
65     HandlePolicyData policyData;
66     std::vector<std::string> keepAliveApps;
67     data.WriteStringVector(keepAliveApps);
68 
69     std::uint32_t funcCode =
70         POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::MANAGE_KEEP_ALIVE_APPS);
71     ErrCode ret = plugin.OnHandlePolicy(funcCode, data, reply, policyData, DEFAULT_USER_ID);
72     ASSERT_TRUE(ret == ERR_OK);
73     ASSERT_TRUE(reply.ReadInt32() == ERR_OK);
74 }
75 
76 /**
77  * @tc.name: TestOnHandlePolicyRemoveFailWithNullData
78  * @tc.desc: Test ManageKeepAliveAppsPlugin::OnHandlePolicy remove when data is empty.
79  * @tc.type: FUNC
80  */
81 HWTEST_F(ManageKeepAliveAppsPluginTest, TestOnHandlePolicyRemoveFailWithNullData, TestSize.Level1)
82 {
83     ManageKeepAliveAppsPlugin plugin;
84     MessageParcel data;
85     MessageParcel reply;
86     HandlePolicyData policyData;
87     std::vector<std::string> keepAliveApps;
88     data.WriteStringVector(keepAliveApps);
89 
90     std::uint32_t funcCode =
91         POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::REMOVE, EdmInterfaceCode::MANAGE_KEEP_ALIVE_APPS);
92     ErrCode ret = plugin.OnHandlePolicy(funcCode, data, reply, policyData, DEFAULT_USER_ID);
93     ASSERT_TRUE(ret == ERR_OK);
94     ASSERT_TRUE(reply.ReadInt32() == ERR_OK);
95 }
96 
97 /**
98  * @tc.name: TestOnHandlePolicyFailWithOversizeData
99  * @tc.desc: Test ManageKeepAliveAppsPlugin::OnHandlePolicy when data is oversize.
100  * @tc.type: FUNC
101  */
102 HWTEST_F(ManageKeepAliveAppsPluginTest, TestOnHandlePolicyFailWithOversizeData, TestSize.Level1)
103 {
104     ManageKeepAliveAppsPlugin plugin;
105     MessageParcel data;
106     MessageParcel reply;
107     HandlePolicyData policyData;
108     std::vector<std::string> keepAliveApps =
109         {"com.test1", "com.test2", "com.test3", "com.test4", "com.test5", "com.test6"};
110     data.WriteStringVector(keepAliveApps);
111 
112     std::uint32_t funcCode =
113         POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::MANAGE_KEEP_ALIVE_APPS);
114     ErrCode ret = plugin.OnHandlePolicy(funcCode, data, reply, policyData, DEFAULT_USER_ID);
115     ASSERT_TRUE(ret == EdmReturnErrCode::PARAM_ERROR);
116 }
117 
118 /**
119  * @tc.name: TestOnHandlePolicyFailWithConflictData
120  * @tc.desc: Test ManageKeepAliveAppsPlugin::OnHandlePolicy when data is conflict.
121  * @tc.type: FUNC
122  */
123 HWTEST_F(ManageKeepAliveAppsPluginTest, TestOnHandlePolicyFailWithConflictData, TestSize.Level1)
124 {
125     std::vector<std::string> keepAliveApps = {"com.test"};
126     std::vector<std::string> currentData;
127     std::vector<std::string> mergeData;
128     DisallowedRunningBundlesPlugin disllowedPlugin;
129     disllowedPlugin.maxListSize_ = EdmConstants::APPID_MAX_SIZE;
130     ErrCode ret = disllowedPlugin.OnBasicSetPolicy(keepAliveApps, currentData, mergeData, DEFAULT_USER_ID);
131     ASSERT_TRUE(ret == ERR_OK);
132 
133     ManageKeepAliveAppsPlugin keepAlivePlugin;
134     MessageParcel data;
135     MessageParcel reply;
136     HandlePolicyData policyData;
137     data.WriteStringVector(keepAliveApps);
138 
139     std::uint32_t funcCode =
140         POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::MANAGE_KEEP_ALIVE_APPS);
141     ret = keepAlivePlugin.OnHandlePolicy(funcCode, data, reply, policyData, DEFAULT_USER_ID);
142     ASSERT_TRUE(ret == EdmReturnErrCode::CONFIGURATION_CONFLICT_FAILED);
143     ASSERT_TRUE(reply.ReadInt32() == EdmReturnErrCode::CONFIGURATION_CONFLICT_FAILED);
144     mergeData.clear();
145     ret = disllowedPlugin.OnBasicRemovePolicy(keepAliveApps, currentData, mergeData, DEFAULT_USER_ID);
146     ASSERT_TRUE(ret == ERR_OK);
147 }
148 
149 /**
150  * @tc.name: TestOnHandlePolicyAddFailWithNotExistedData
151  * @tc.desc: Test ManageKeepAliveAppsPlugin::OnHandlePolicy add when app is not existed.
152  * @tc.type: FUNC
153  */
154 HWTEST_F(ManageKeepAliveAppsPluginTest, TestOnHandlePolicyAddFailWithNotExistedData, TestSize.Level1)
155 {
156     std::vector<std::string> keepAliveApps = {"com.not.existed"};
157     ManageKeepAliveAppsPlugin plugin;
158     MessageParcel data;
159     MessageParcel reply;
160     data.WriteStringVector(keepAliveApps);
161     HandlePolicyData policyData;
162 
163     std::uint32_t funcCode =
164         POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::MANAGE_KEEP_ALIVE_APPS);
165     ErrCode ret = plugin.OnHandlePolicy(funcCode, data, reply, policyData, DEFAULT_USER_ID);
166     ErrCode res;
167     reply.ReadInt32(res);
168     ASSERT_TRUE(res == EdmReturnErrCode::ADD_KEEP_ALIVE_APP_FAILED || res == EdmReturnErrCode::INTERFACE_UNSUPPORTED);
169     ASSERT_TRUE(ret == res);
170 }
171 
172 /**
173  * @tc.name: TestOnHandlePolicyRemoveFailWithNotExistedData
174  * @tc.desc: Test ManageKeepAliveAppsPlugin::OnHandlePolicy remove when app is not existed.
175  * @tc.type: FUNC
176  */
177 HWTEST_F(ManageKeepAliveAppsPluginTest, TestOnHandlePolicyRemoveFailWithNotExistedData, TestSize.Level1)
178 {
179     std::vector<std::string> keepAliveApps = {"com.not.existed"};
180     ManageKeepAliveAppsPlugin plugin;
181     MessageParcel data;
182     MessageParcel reply;
183     data.WriteStringVector(keepAliveApps);
184     HandlePolicyData policyData;
185     ArrayStringSerializer::GetInstance()->Serialize(keepAliveApps, policyData.policyData);
186 
187     std::uint32_t funcCode =
188         POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::REMOVE, EdmInterfaceCode::MANAGE_KEEP_ALIVE_APPS);
189     ErrCode ret = plugin.OnHandlePolicy(funcCode, data, reply, policyData, DEFAULT_USER_ID);
190     ErrCode res;
191     reply.ReadInt32(res);
192     ASSERT_TRUE(res == EdmReturnErrCode::ADD_KEEP_ALIVE_APP_FAILED || res == EdmReturnErrCode::INTERFACE_UNSUPPORTED);
193     ASSERT_TRUE(ret == res);
194 }
195 
196 /**
197  * @tc.name: TestOnGetPolicyFail
198  * @tc.desc: Test ManageKeepAliveAppsPlugin::OnGetPolicy.
199  * @tc.type: FUNC
200  */
201 HWTEST_F(ManageKeepAliveAppsPluginTest, TestOnGetPolicyFail, TestSize.Level1)
202 {
203     ManageKeepAliveAppsPlugin plugin;
204     std::string policyData;
205     MessageParcel data;
206     MessageParcel reply;
207     std::vector<std::string> keepAliveApps;
208     data.WriteString("bundleName");
209     ErrCode ret = plugin.OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
210     reply.ReadStringVector(&keepAliveApps);
211     ASSERT_TRUE((ret == EdmReturnErrCode::SYSTEM_ABNORMALLY) || (keepAliveApps.empty()));
212 }
213 
214 /**
215  * @tc.name: TestGetDisallowModifyFail
216  * @tc.desc: Test ManageKeepAliveAppsPlugin::OnGetPolicy.
217  * @tc.type: FUNC
218  */
219 HWTEST_F(ManageKeepAliveAppsPluginTest, TestGetDisallowModifyFail, TestSize.Level1)
220 {
221     ManageKeepAliveAppsPlugin plugin;
222     std::string policyData;
223     MessageParcel data;
224     MessageParcel reply;
225     std::vector<std::string> keepAliveApps;
226     data.WriteString("disallowModity");
227     data.WriteString("com.test1");
228     ErrCode ret = plugin.OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
229     bool disallowModify = reply.ReadBool();
230     ASSERT_TRUE((ret == EdmReturnErrCode::SYSTEM_ABNORMALLY) || (!disallowModify));
231 }
232 
233 /**
234  * @tc.name: TestOnAdminRemoveDoneFail
235  * @tc.desc: Test ManageKeepAliveAppsPlugin::OnAdminRemoveDone.
236  * @tc.type: FUNC
237  */
238 HWTEST_F(ManageKeepAliveAppsPluginTest, TestOnAdminRemoveDoneFail, TestSize.Level1)
239 {
240     ManageKeepAliveAppsPlugin plugin;
241     std::string adminName;
242     std::string currentJsonData;
243     plugin.OnAdminRemoveDone(adminName, currentJsonData, DEFAULT_USER_ID);
244 
245     std::string policyData;
246     MessageParcel data;
247     MessageParcel reply;
248     data.WriteString("bundleName");
249     std::vector<std::string> keepAliveApps;
250     ErrCode ret = plugin.OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
251     reply.ReadStringVector(&keepAliveApps);
252     ASSERT_TRUE((ret == EdmReturnErrCode::SYSTEM_ABNORMALLY) || (keepAliveApps.empty()));
253 }
254 } // namespace TEST
255 } // namespace EDM
256 } // namespace OHOS