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
27 using namespace testing;
28 using namespace testing::ext;
29 using namespace OHOS::AppExecFwk;
30
31 namespace {
32 const std::string BUNDLE_NAME = "testBundle";
33 }
34
35 namespace OHOS {
36 namespace AAFwk {
37 class AbilityInterceptorTest : public testing::Test {
38 public:
39 static void SetUpTestCase();
40 static void TearDownTestCase();
41 void SetUp();
42 void TearDown();
43
44 public:
45 };
46
SetUpTestCase()47 void AbilityInterceptorTest::SetUpTestCase()
48 {
49 GTEST_LOG_(INFO) << "AbilityInterceptorTest SetUpTestCase called";
50 OHOS::DelayedSingleton<SaMgrClient>::GetInstance()->RegisterSystemAbility(
51 OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, new BundleMgrService());
52 }
53
TearDownTestCase()54 void AbilityInterceptorTest::TearDownTestCase()
55 {
56 GTEST_LOG_(INFO) << "AbilityInterceptorTest TearDownTestCase called";
57 OHOS::DelayedSingleton<SaMgrClient>::DestroyInstance();
58 }
59
SetUp()60 void AbilityInterceptorTest::SetUp()
61 {}
62
TearDown()63 void AbilityInterceptorTest::TearDown()
64 {}
65
66 HWTEST_F(AbilityInterceptorTest, CreateExecuter_001, TestSize.Level1)
67 {
68 std::shared_ptr<AbilityInterceptorExecuter> executer = std::make_shared<AbilityInterceptorExecuter>();
69 EXPECT_NE(executer, nullptr);
70 }
71
72 /**
73 * @tc.name: AbilityInterceptorTest_CrowdTestInterceptor_001
74 * @tc.desc: CrowdTestInterceptor
75 * @tc.type: FUNC
76 * @tc.require: issueI5I0DY
77 */
78 HWTEST_F(AbilityInterceptorTest, CrowdTestInterceptor_001, TestSize.Level1)
79 {
80 std::shared_ptr<AbilityInterceptorExecuter> executer = std::make_shared<AbilityInterceptorExecuter>();
81 Want want;
82 ElementName element("", "com.test.crowdtest", "CrowdtestExpired");
83 want.SetElement(element);
84 int requestCode = 0;
85 int userId = 100;
86 executer->AddInterceptor(std::make_shared<CrowdTestInterceptor>());
87 int result = executer->DoProcess(want, requestCode, userId, true);
88 EXPECT_NE(result, ERR_OK);
89 }
90
91 /**
92 * @tc.name: AbilityInterceptorTest_CrowdTestInterceptor_002
93 * @tc.desc: CrowdTestInterceptor
94 * @tc.type: FUNC
95 * @tc.require: issueI5I0DY
96 */
97 HWTEST_F(AbilityInterceptorTest, CrowdTestInterceptor_002, TestSize.Level1)
98 {
99 std::shared_ptr<AbilityInterceptorExecuter> executer = std::make_shared<AbilityInterceptorExecuter>();
100 Want want;
101 ElementName element("", "com.test.crowdtest", "CrowdtestExpired");
102 want.SetElement(element);
103 int userId = 100;
104 executer->AddInterceptor(std::make_shared<CrowdTestInterceptor>());
105 int result = executer->DoProcess(want, 0, userId, false);
106 EXPECT_NE(result, ERR_OK);
107 }
108
109 /**
110 * @tc.name: AbilityInterceptorTest_ControlInterceptor_001
111 * @tc.desc: ControlInterceptor
112 * @tc.type: FUNC
113 * @tc.require: issueI5QT7P
114 */
115 HWTEST_F(AbilityInterceptorTest, ControlInterceptor_001, TestSize.Level1)
116 {
117 std::shared_ptr<AbilityInterceptorExecuter> executer = std::make_shared<AbilityInterceptorExecuter>();
118 Want want;
119 ElementName element("", "com.test.control", "MainAbility");
120 want.SetElement(element);
121 int requestCode = 0;
122 int userId = 100;
123 executer->AddInterceptor(std::make_shared<ControlInterceptor>());
124 int result = executer->DoProcess(want, requestCode, userId, true);
125 EXPECT_EQ(result, ERR_OK);
126 }
127
128 /**
129 * @tc.name: AbilityInterceptorTest_ControlInterceptor_002
130 * @tc.desc: ControlInterceptor
131 * @tc.type: FUNC
132 * @tc.require: issueI5QT7P
133 */
134 HWTEST_F(AbilityInterceptorTest, ControlInterceptor_002, TestSize.Level1)
135 {
136 std::shared_ptr<AbilityInterceptorExecuter> executer = std::make_shared<AbilityInterceptorExecuter>();
137 Want want;
138 ElementName element("", "com.test.control", "MainAbility");
139 want.SetElement(element);
140 int userId = 100;
141 executer->AddInterceptor(std::make_shared<ControlInterceptor>());
142 int result = executer->DoProcess(want, 0, userId, false);
143 EXPECT_EQ(result, ERR_OK);
144 }
145
146 /**
147 * @tc.name: AbilityInterceptorTest_ControlInterceptor_003
148 * @tc.desc: ControlInterceptor
149 * @tc.type: FUNC
150 * @tc.require: issueI5QT7P
151 */
152 HWTEST_F(AbilityInterceptorTest, ControlInterceptor_003, TestSize.Level1)
153 {
154 std::shared_ptr<AbilityInterceptorExecuter> executer = std::make_shared<AbilityInterceptorExecuter>();
155 Want want;
156 ElementName element("", "com.test.control2", "MainAbility");
157 want.SetElement(element);
158 int userId = 100;
159 executer->AddInterceptor(std::make_shared<ControlInterceptor>());
160 int result = executer->DoProcess(want, 0, userId, false);
161 EXPECT_EQ(result, ERR_OK);
162 }
163
164 /**
165 * @tc.name: AbilityInterceptorTest_ControlInterceptor_004
166 * @tc.desc: ControlInterceptor
167 * @tc.type: FUNC
168 * @tc.require: issueI5QT7P
169 */
170 HWTEST_F(AbilityInterceptorTest, ControlInterceptor_004, TestSize.Level1)
171 {
172 std::shared_ptr<AbilityInterceptorExecuter> executer = std::make_shared<AbilityInterceptorExecuter>();
173 Want want;
174 ElementName element("", "com.test.control3", "MainAbility");
175 want.SetElement(element);
176 int userId = 100;
177 executer->AddInterceptor(std::make_shared<ControlInterceptor>());
178 int result = executer->DoProcess(want, 0, userId, false);
179 EXPECT_EQ(result, ERR_OK);
180 }
181
182 /**
183 * @tc.name: AbilityInterceptorTest_ControlInterceptor_005
184 * @tc.desc: ControlInterceptor
185 * @tc.type: FUNC
186 * @tc.require: issueI5QT7P
187 */
188 HWTEST_F(AbilityInterceptorTest, ControlInterceptor_005, TestSize.Level1)
189 {
190 std::shared_ptr<AbilityInterceptorExecuter> executer = std::make_shared<AbilityInterceptorExecuter>();
191 Want want;
192 ElementName element("", "com.test.control", "MainAbility");
193 want.SetElement(element);
194 int userId = 100;
195 auto bms = AbilityUtil::GetBundleManager();
196 // make appControlRule become nullptr by crowdtest interceptor
197 executer->AddInterceptor(std::make_shared<CrowdTestInterceptor>());
198 executer->AddInterceptor(std::make_shared<ControlInterceptor>());
199 int result = executer->DoProcess(want, 0, userId, false);
200 EXPECT_EQ(result, ERR_OK);
201 }
202 } // namespace AAFwk
203 } // namespace OHOS
204