1 /*
2 * Copyright (c) 2022 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 #define private public
18 #define protected public
19 #include "ability_manager_service.h"
20 #undef private
21 #undef protected
22
23 #include "ability_interceptor.h"
24 #include "ability_interceptor_executer.h"
25 #include "bundlemgr/mock_bundle_manager.h"
26 #include "mock_ecological_rule_manager.h"
27
28 using namespace testing;
29 using namespace testing::ext;
30 using namespace OHOS::AppExecFwk;
31 #ifndef SUPPORT_ERMS
32 using ExperienceRule = OHOS::AppExecFwk::ErmsParams::ExperienceRule;
33 #endif
34
35 namespace {
36 const std::string BUNDLE_NAME = "testBundle";
37 const int32_t ECOLOGICAL_RULE_SA_ID = 9999;
38 const std::string ATOMIC_SERVICE_BUNDLE_NAME = "com.test.atomicservice";
39 const std::string PASS_ABILITY_NAME = "com.test.pass";
40 const std::string DENY_ABILITY_NAME = "com.test.deny";
41 const std::string JUMP_ABILITY_NAME = "com.test.jump";
42 }
43
44 namespace OHOS {
45 namespace AAFwk {
46 class AbilityInterceptorTest : public testing::Test {
47 public:
48 static void SetUpTestCase();
49 static void TearDownTestCase();
50 void SetUp();
51 void TearDown();
52
53 public:
54 };
55
SetUpTestCase()56 void AbilityInterceptorTest::SetUpTestCase()
57 {
58 GTEST_LOG_(INFO) << "AbilityInterceptorTest SetUpTestCase called";
59 AbilityManagerClient::GetInstance()->CleanAllMissions();
60 OHOS::DelayedSingleton<SaMgrClient>::DestroyInstance();
61
62 OHOS::DelayedSingleton<SaMgrClient>::GetInstance()->RegisterSystemAbility(
63 OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, new BundleMgrService());
64 OHOS::DelayedSingleton<SaMgrClient>::GetInstance()->RegisterSystemAbility(
65 ECOLOGICAL_RULE_SA_ID, new MockEcologicalRuleMgrService());
66 }
67
TearDownTestCase()68 void AbilityInterceptorTest::TearDownTestCase()
69 {
70 GTEST_LOG_(INFO) << "AbilityInterceptorTest TearDownTestCase called";
71 OHOS::DelayedSingleton<SaMgrClient>::DestroyInstance();
72 }
73
SetUp()74 void AbilityInterceptorTest::SetUp()
75 {}
76
TearDown()77 void AbilityInterceptorTest::TearDown()
78 {}
79
80 HWTEST_F(AbilityInterceptorTest, CreateExecuter_001, TestSize.Level1)
81 {
82 std::shared_ptr<AbilityInterceptorExecuter> executer = std::make_shared<AbilityInterceptorExecuter>();
83 EXPECT_NE(executer, nullptr);
84 }
85
86 /**
87 * @tc.name: AbilityInterceptorTest_CrowdTestInterceptor_001
88 * @tc.desc: CrowdTestInterceptor
89 * @tc.type: FUNC
90 * @tc.require: issueI5I0DY
91 */
92 HWTEST_F(AbilityInterceptorTest, CrowdTestInterceptor_001, TestSize.Level1)
93 {
94 std::shared_ptr<AbilityInterceptorExecuter> executer = std::make_shared<AbilityInterceptorExecuter>();
95 Want want;
96 ElementName element("", "com.test.crowdtest", "CrowdtestExpired");
97 want.SetElement(element);
98 int requestCode = 0;
99 int userId = 100;
100 executer->AddInterceptor(std::make_shared<CrowdTestInterceptor>());
101 int result = executer->DoProcess(want, requestCode, userId, true);
102 EXPECT_NE(result, ERR_OK);
103 }
104
105 /**
106 * @tc.name: AbilityInterceptorTest_CrowdTestInterceptor_002
107 * @tc.desc: CrowdTestInterceptor
108 * @tc.type: FUNC
109 * @tc.require: issueI5I0DY
110 */
111 HWTEST_F(AbilityInterceptorTest, CrowdTestInterceptor_002, TestSize.Level1)
112 {
113 std::shared_ptr<AbilityInterceptorExecuter> executer = std::make_shared<AbilityInterceptorExecuter>();
114 Want want;
115 ElementName element("", "com.test.crowdtest", "CrowdtestExpired");
116 want.SetElement(element);
117 int userId = 100;
118 executer->AddInterceptor(std::make_shared<CrowdTestInterceptor>());
119 int result = executer->DoProcess(want, 0, userId, false);
120 EXPECT_NE(result, ERR_OK);
121 }
122
123 /**
124 * @tc.name: AbilityInterceptorTest_ControlInterceptor_001
125 * @tc.desc: ControlInterceptor
126 * @tc.type: FUNC
127 * @tc.require: issueI5QT7P
128 */
129 HWTEST_F(AbilityInterceptorTest, ControlInterceptor_001, TestSize.Level1)
130 {
131 std::shared_ptr<AbilityInterceptorExecuter> executer = std::make_shared<AbilityInterceptorExecuter>();
132 Want want;
133 ElementName element("", "com.test.control", "MainAbility");
134 want.SetElement(element);
135 int requestCode = 0;
136 int userId = 100;
137 executer->AddInterceptor(std::make_shared<ControlInterceptor>());
138 int result = executer->DoProcess(want, requestCode, userId, true);
139 EXPECT_EQ(result, ERR_OK);
140 }
141
142 /**
143 * @tc.name: AbilityInterceptorTest_ControlInterceptor_002
144 * @tc.desc: ControlInterceptor
145 * @tc.type: FUNC
146 * @tc.require: issueI5QT7P
147 */
148 HWTEST_F(AbilityInterceptorTest, ControlInterceptor_002, TestSize.Level1)
149 {
150 std::shared_ptr<AbilityInterceptorExecuter> executer = std::make_shared<AbilityInterceptorExecuter>();
151 Want want;
152 ElementName element("", "com.test.control", "MainAbility");
153 want.SetElement(element);
154 int userId = 100;
155 executer->AddInterceptor(std::make_shared<ControlInterceptor>());
156 int result = executer->DoProcess(want, 0, userId, false);
157 EXPECT_EQ(result, ERR_OK);
158 }
159
160 /**
161 * @tc.name: AbilityInterceptorTest_ControlInterceptor_003
162 * @tc.desc: ControlInterceptor
163 * @tc.type: FUNC
164 * @tc.require: issueI5QT7P
165 */
166 HWTEST_F(AbilityInterceptorTest, ControlInterceptor_003, TestSize.Level1)
167 {
168 std::shared_ptr<AbilityInterceptorExecuter> executer = std::make_shared<AbilityInterceptorExecuter>();
169 Want want;
170 ElementName element("", "com.test.control2", "MainAbility");
171 want.SetElement(element);
172 int userId = 100;
173 executer->AddInterceptor(std::make_shared<ControlInterceptor>());
174 int result = executer->DoProcess(want, 0, userId, false);
175 EXPECT_EQ(result, ERR_OK);
176 }
177
178 /**
179 * @tc.name: AbilityInterceptorTest_ControlInterceptor_004
180 * @tc.desc: ControlInterceptor
181 * @tc.type: FUNC
182 * @tc.require: issueI5QT7P
183 */
184 HWTEST_F(AbilityInterceptorTest, ControlInterceptor_004, TestSize.Level1)
185 {
186 std::shared_ptr<AbilityInterceptorExecuter> executer = std::make_shared<AbilityInterceptorExecuter>();
187 Want want;
188 ElementName element("", "com.test.control3", "MainAbility");
189 want.SetElement(element);
190 int userId = 100;
191 executer->AddInterceptor(std::make_shared<ControlInterceptor>());
192 int result = executer->DoProcess(want, 0, userId, false);
193 EXPECT_EQ(result, ERR_OK);
194 }
195
196 /**
197 * @tc.name: AbilityInterceptorTest_ControlInterceptor_005
198 * @tc.desc: ControlInterceptor
199 * @tc.type: FUNC
200 * @tc.require: issueI5QT7P
201 */
202 HWTEST_F(AbilityInterceptorTest, ControlInterceptor_005, TestSize.Level1)
203 {
204 std::shared_ptr<AbilityInterceptorExecuter> executer = std::make_shared<AbilityInterceptorExecuter>();
205 Want want;
206 ElementName element("", "com.test.control", "MainAbility");
207 want.SetElement(element);
208 int userId = 100;
209 auto bms = AbilityUtil::GetBundleManager();
210 // make appControlRule become nullptr by crowdtest interceptor
211 executer->AddInterceptor(std::make_shared<CrowdTestInterceptor>());
212 executer->AddInterceptor(std::make_shared<ControlInterceptor>());
213 int result = executer->DoProcess(want, 0, userId, false);
214 EXPECT_EQ(result, ERR_OK);
215 }
216
217 #ifndef SUPPORT_ERMS
218 /**
219 * @tc.name: AbilityInterceptorTest_EcologicalRuleInterceptor_001
220 * @tc.desc: EcologicalRuleInterceptor DoProcess ERR_OK when erms invalid
221 * @tc.type: FUNC
222 * @tc.require: issueI6HT6C
223 */
224 HWTEST_F(AbilityInterceptorTest, EcologicalRuleInterceptor_001, TestSize.Level1)
225 {
226 std::shared_ptr<AbilityInterceptorExecuter> executer = std::make_shared<AbilityInterceptorExecuter>();
227 Want want;
228 int requestCode = 0;
229 int userId = 100;
230 OHOS::DelayedSingleton<SaMgrClient>::GetInstance()->RegisterSystemAbility(
231 ECOLOGICAL_RULE_SA_ID, nullptr);
232 executer->AddInterceptor(std::make_shared<EcologicalRuleInterceptor>());
233 int result = executer->DoProcess(want, requestCode, userId, true);
234 OHOS::DelayedSingleton<SaMgrClient>::GetInstance()->RegisterSystemAbility(
235 ECOLOGICAL_RULE_SA_ID, new MockEcologicalRuleMgrService());
236 EXPECT_EQ(result, ERR_OK);
237 }
238
239 /**
240 * @tc.name: AbilityInterceptorTest_EcologicalRuleInterceptor_002
241 * @tc.desc: EcologicalRuleInterceptor DoProcess ERR_OK when erms return error
242 * @tc.type: FUNC
243 * @tc.require: issueI6HT6C
244 */
245 HWTEST_F(AbilityInterceptorTest, EcologicalRuleInterceptor_002, TestSize.Level1)
246 {
247 std::shared_ptr<AbilityInterceptorExecuter> executer = std::make_shared<AbilityInterceptorExecuter>();
248 Want want;
249 ElementName elementName;
250 elementName.SetBundleName(ATOMIC_SERVICE_BUNDLE_NAME);
251 want.SetElement(elementName);
252 int requestCode = 0;
253 int userId = 100;
254
255 executer->AddInterceptor(std::make_shared<EcologicalRuleInterceptor>());
256 int result = executer->DoProcess(want, requestCode, userId, true);
257 EXPECT_EQ(result, ERR_OK);
258 }
259
260 /**
261 * @tc.name: AbilityInterceptorTest_EcologicalRuleInterceptor_003
262 * @tc.desc: EcologicalRuleInterceptor DoProcess ERR_OK when erms return pass
263 * @tc.type: FUNC
264 * @tc.require: issueI6HT6C
265 */
266 HWTEST_F(AbilityInterceptorTest, EcologicalRuleInterceptor_003, TestSize.Level1)
267 {
268 std::shared_ptr<AbilityInterceptorExecuter> executer = std::make_shared<AbilityInterceptorExecuter>();
269 Want want;
270 ElementName elementName;
271 elementName.SetBundleName(ATOMIC_SERVICE_BUNDLE_NAME);
272 elementName.SetAbilityName(PASS_ABILITY_NAME);
273 want.SetElement(elementName);
274 int requestCode = 0;
275 int userId = 100;
276
277 executer->AddInterceptor(std::make_shared<EcologicalRuleInterceptor>());
278 int result = executer->DoProcess(want, requestCode, userId, true);
279 EXPECT_EQ(result, ERR_OK);
280 }
281
282 /**
283 * @tc.name: AbilityInterceptorTest_EcologicalRuleInterceptor_004
284 * @tc.desc: EcologicalRuleInterceptor DoProcess ERR_ECOLOGICAL_CONTROL_STATUS when erms return DENY
285 * @tc.type: FUNC
286 * @tc.require: issueI6HT6C
287 */
288 HWTEST_F(AbilityInterceptorTest, EcologicalRuleInterceptor_004, TestSize.Level1)
289 {
290 std::shared_ptr<AbilityInterceptorExecuter> executer = std::make_shared<AbilityInterceptorExecuter>();
291 Want want;
292 ElementName elementName;
293 elementName.SetBundleName(ATOMIC_SERVICE_BUNDLE_NAME);
294 elementName.SetAbilityName(DENY_ABILITY_NAME);
295 want.SetElement(elementName);
296 int requestCode = 0;
297 int userId = 100;
298
299 executer->AddInterceptor(std::make_shared<EcologicalRuleInterceptor>());
300 int result = executer->DoProcess(want, requestCode, userId, true);
301 EXPECT_EQ(result, ERR_OK);
302 }
303
304 /**
305 * @tc.name: AbilityInterceptorTest_EcologicalRuleInterceptor_005
306 * @tc.desc: EcologicalRuleInterceptor DoProcess ERR_ECOLOGICAL_CONTROL_STATUS when erms return JUMP
307 * @tc.type: FUNC
308 * @tc.require: issueI6HT6C
309 */
310 HWTEST_F(AbilityInterceptorTest, EcologicalRuleInterceptor_005, TestSize.Level1)
311 {
312 std::shared_ptr<AbilityInterceptorExecuter> executer = std::make_shared<AbilityInterceptorExecuter>();
313 Want want;
314 ElementName elementName;
315 elementName.SetBundleName(ATOMIC_SERVICE_BUNDLE_NAME);
316 elementName.SetAbilityName(JUMP_ABILITY_NAME);
317 want.SetElement(elementName);
318 int requestCode = 0;
319 int userId = 100;
320
321 executer->AddInterceptor(std::make_shared<EcologicalRuleInterceptor>());
322 int result = executer->DoProcess(want, requestCode, userId, false);
323 EXPECT_EQ(result, ERR_OK);
324 }
325
326 /**
327 * @tc.name: AbilityInterceptorTest_EcologicalRuleInterceptor_006
328 * @tc.desc: EcologicalRuleInterceptor DoProcess ERR_OK when not ATOMIC_SERVICE
329 * @tc.type: FUNC
330 * @tc.require: issueI6HT6C
331 */
332 HWTEST_F(AbilityInterceptorTest, EcologicalRuleInterceptor_006, TestSize.Level1)
333 {
334 std::shared_ptr<AbilityInterceptorExecuter> executer = std::make_shared<AbilityInterceptorExecuter>();
335 Want want;
336 int requestCode = 0;
337 int userId = 100;
338 executer->AddInterceptor(std::make_shared<EcologicalRuleInterceptor>());
339 int result = executer->DoProcess(want, requestCode, userId, true);
340 EXPECT_EQ(result, ERR_OK);
341 }
342 #endif
343 } // namespace AAFwk
344 } // namespace OHOS
345