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 #include <gtest/gtest.h>
17 #define private public
18 #define protected public
19 #include "ability_manager_service.h"
20 #include "interceptor/disposed_rule_interceptor.h"
21 #undef private
22 #undef protected
23
24 #include "bundlemgr/mock_bundle_manager.h"
25 #include "permission_constants.h"
26 #include "start_ability_utils.h"
27
28 using namespace testing;
29 using namespace testing::ext;
30 using namespace OHOS::AppExecFwk;
31
32 namespace {
33 const std::string BUNDLE_NAME = "testBundle";
34 constexpr const char* TEST_IS_FROM_PARENTCONTROL = "ohos.ability.isFromParentControl";
35 constexpr const char* TEST_INTERCEPT_BUNDLE_NAME = "intercept_bundleName";
36 }
37
38 namespace OHOS {
39 namespace AAFwk {
40 class AbilityInterceptorSecondTest : public testing::Test {
41 public:
42 static void SetUpTestCase();
43 static void TearDownTestCase();
44 void SetUp();
45 void TearDown();
46
47 public:
48 };
49
SetUpTestCase()50 void AbilityInterceptorSecondTest::SetUpTestCase()
51 {
52 GTEST_LOG_(INFO) << "AbilityInterceptorSecondTest SetUpTestCase called";
53 AbilityManagerClient::GetInstance()->CleanAllMissions();
54 OHOS::DelayedSingleton<SaMgrClient>::DestroyInstance();
55
56 OHOS::DelayedSingleton<SaMgrClient>::GetInstance()->RegisterSystemAbility(
57 OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, new BundleMgrService());
58 }
59
TearDownTestCase()60 void AbilityInterceptorSecondTest::TearDownTestCase()
61 {
62 GTEST_LOG_(INFO) << "AbilityInterceptorSecondTest TearDownTestCase called";
63 OHOS::DelayedSingleton<SaMgrClient>::DestroyInstance();
64 }
65
SetUp()66 void AbilityInterceptorSecondTest::SetUp()
67 {}
68
TearDown()69 void AbilityInterceptorSecondTest::TearDown()
70 {}
71
72 HWTEST_F(AbilityInterceptorSecondTest, CreateExecuter_001, TestSize.Level1)
73 {
74 TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__);
75 std::shared_ptr<AbilityInterceptorExecuter> executer = std::make_shared<AbilityInterceptorExecuter>();
76 EXPECT_NE(executer, nullptr);
77 TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__);
78 }
79
80 /**
81 * @tc.name: AbilityInterceptorSecondTest_DisposedRuleInterceptor_001
82 * @tc.desc: DisposedRuleInterceptor
83 * @tc.type: FUNC
84 * @tc.require: No
85 */
86 HWTEST_F(AbilityInterceptorSecondTest, DisposedRuleInterceptor_001, TestSize.Level1)
87 {
88 TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__);
89 std::shared_ptr<DisposedRuleInterceptor> executer = std::make_shared<DisposedRuleInterceptor>();
90 Want want;
91 ElementName element("", "com.test.disposedrule", "MainAbility", "entry");
92 want.SetElement(element);
93 AppExecFwk::DisposedRule disposedRule;
94 disposedRule.disposedType = AppExecFwk::DisposedType::BLOCK_ABILITY;
95 bool result = executer->CheckDisposedRule(want, disposedRule);
96 EXPECT_EQ(result, true);
97 TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__);
98 }
99
100 /**
101 * @tc.name: AbilityInterceptorSecondTest_DisposedRuleInterceptor_002
102 * @tc.desc: DisposedRuleInterceptor
103 * @tc.type: FUNC
104 * @tc.require: No
105 */
106 HWTEST_F(AbilityInterceptorSecondTest, DisposedRuleInterceptor_002, TestSize.Level1)
107 {
108 TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__);
109 std::shared_ptr<DisposedRuleInterceptor> executer = std::make_shared<DisposedRuleInterceptor>();
110 Want want;
111 ElementName element("", "com.test.disposedrule", "MainAbility", "entry");
112 want.SetElement(element);
113 AppExecFwk::DisposedRule disposedRule;
114 disposedRule.disposedType = AppExecFwk::DisposedType::BLOCK_ABILITY;
115 disposedRule.controlType = AppExecFwk::ControlType::DISALLOWED_LIST;
116 bool result = executer->CheckDisposedRule(want, disposedRule);
117 EXPECT_EQ(result, false);
118 TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__);
119 }
120
121 /**
122 * @tc.name: AbilityInterceptorSecondTest_DisposedRuleInterceptor_003
123 * @tc.desc: DisposedRuleInterceptor
124 * @tc.type: FUNC
125 * @tc.require: No
126 */
127 HWTEST_F(AbilityInterceptorSecondTest, DisposedRuleInterceptor_003, TestSize.Level1)
128 {
129 TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__);
130 std::shared_ptr<DisposedRuleInterceptor> executer = std::make_shared<DisposedRuleInterceptor>();
131 std::string bundleName = "com.example.disposedruletest1";
132 Want want;
133 want.SetBundle(bundleName);
134 DisposedRule disposedRule;
135 disposedRule.want = std::make_shared<Want>();
136 disposedRule.want->SetBundle(bundleName);
137 ErrCode result = executer->StartNonBlockRule(want, disposedRule);
138 EXPECT_EQ(result, ERR_OK);
139 TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__);
140 }
141
142 /**
143 * @tc.name: AbilityInterceptorSecondTest_DisposedRuleInterceptor_004
144 * @tc.desc: DisposedRuleInterceptor
145 * @tc.type: FUNC
146 * @tc.require: No
147 */
148 HWTEST_F(AbilityInterceptorSecondTest, DisposedRuleInterceptor_004, TestSize.Level1)
149 {
150 TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__);
151 std::shared_ptr<DisposedRuleInterceptor> executer = std::make_shared<DisposedRuleInterceptor>();
152 std::string bundleName1 = "com.example.disposedruletest1";
153 std::string bundleName2 = "com.example.disposedruletest2";
154 Want want;
155 want.SetBundle(bundleName1);
156 DisposedRule disposedRule;
157 disposedRule.want = std::make_shared<Want>();
158 disposedRule.want->SetBundle(bundleName2);
159 ErrCode result = executer->StartNonBlockRule(want, disposedRule);
160 EXPECT_NE(result, ERR_OK);
161 TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__);
162 }
163
164 /**
165 * @tc.name: AbilityInterceptorSecondTest_DisposedRuleInterceptor_005
166 * @tc.desc: DisposedRuleInterceptor
167 * @tc.type: FUNC
168 * @tc.require: No
169 */
170 HWTEST_F(AbilityInterceptorSecondTest, DisposedRuleInterceptor_005, TestSize.Level1)
171 {
172 TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__);
173 std::shared_ptr<DisposedRuleInterceptor> executer = std::make_shared<DisposedRuleInterceptor>();
174 std::string bundleName = "com.example.disposedruletest";
175 Want want;
176 want.SetBundle(bundleName);
177 AbilityRequest abilityRequest;
178 auto abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
179 sptr<IRemoteObject> callerToken = abilityRecord->GetToken();
180 executer->CreateModalUIExtension(want, callerToken);
181 EXPECT_NE(executer, nullptr);
182 TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__);
183 }
184
185 /**
186 * @tc.name: AbilityInterceptorSecondTest_DisposedRuleInterceptor_006
187 * @tc.desc: DisposedRuleInterceptor
188 * @tc.type: FUNC
189 * @tc.require: No
190 */
191 HWTEST_F(AbilityInterceptorSecondTest, DisposedRuleInterceptor_006, TestSize.Level1)
192 {
193 TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__);
194 std::shared_ptr<DisposedRuleInterceptor> executer = std::make_shared<DisposedRuleInterceptor>();
195 std::string bundleName = "com.test.disposedrule";
196 Want want;
197 ElementName element("", "com.test.disposedrule", "MainAbility", "entry");
198 want.SetElement(element);
199 DisposedRule disposedRule;
200 disposedRule.want = std::make_shared<Want>();
201 disposedRule.want->SetParam(TEST_IS_FROM_PARENTCONTROL, true);
202 executer->SetInterceptInfo(want, disposedRule);
203 EXPECT_STREQ(disposedRule.want->GetStringParam(TEST_INTERCEPT_BUNDLE_NAME).c_str(), bundleName.c_str());
204 TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__);
205 }
206
207 /**
208 * @tc.name: AbilityInterceptorSecondTest_DisposedRuleInterceptor_007
209 * @tc.desc: DisposedRuleInterceptor
210 * @tc.type: FUNC
211 * @tc.require: No
212 */
213 HWTEST_F(AbilityInterceptorSecondTest, DisposedRuleInterceptor_007, TestSize.Level1)
214 {
215 TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__);
216 std::shared_ptr<DisposedRuleInterceptor> executer = std::make_shared<DisposedRuleInterceptor>();
217 std::string bundleName = "interceptor_callerBundleName";
218 Want want;
219 want.SetBundle(bundleName);
220 int32_t userId = 10;
221 DisposedRule disposedRule;
222 int32_t appIndex = 0;
223 bool result = executer->CheckControl(want, userId, disposedRule, appIndex);
224 EXPECT_EQ(result, false);
225 TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__);
226 }
227
228 /**
229 * @tc.name: AbilityInterceptorSecondTest_DisposedRuleInterceptor_008
230 * @tc.desc: DisposedRuleInterceptor
231 * @tc.type: FUNC
232 * @tc.require: No
233 */
234 HWTEST_F(AbilityInterceptorSecondTest, DisposedRuleInterceptor_008, TestSize.Level1)
235 {
236 TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__);
237 std::shared_ptr<DisposedRuleInterceptor> executer = std::make_shared<DisposedRuleInterceptor>();
238 std::string bundleName = "interceptor_callerBundleName";
239 Want want;
240 want.SetBundle(bundleName);
241 int32_t userId = 10;
242 int32_t appIndex = 0;
243 DisposedRule disposedRule;
244 disposedRule.disposedType = AppExecFwk::DisposedType::BLOCK_APPLICATION;
245 disposedRule.controlType == AppExecFwk::ControlType::DISALLOWED_LIST;
246 executer->CheckDisposedRule(want, disposedRule);
247 bool result = executer->CheckControl(want, userId, disposedRule, appIndex);
248 EXPECT_EQ(result, false);
249 TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__);
250 }
251
252 } // namespace AAFwk
253 } // namespace OHOS
254