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