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 #include "ability_event_handler.h"
21 #undef private
22 #undef protected
23
24 #include "app_process_data.h"
25 #include "system_ability_definition.h"
26 #include "ability_manager_errors.h"
27 #include "ability_scheduler.h"
28 #include "sa_mgr_client.h"
29 #include "mock_ability_connect_callback.h"
30 #include "mock_ability_token.h"
31 #include "mock_bundle_mgr.h"
32 #include "nativetoken_kit.h"
33 #include "token_setproc.h"
34 #include "if_system_ability_manager.h"
35 #include "iservice_registry.h"
36
37 using namespace testing;
38 using namespace testing::ext;
39 using namespace OHOS::AppExecFwk;
40
41 namespace {
42 const std::string DEVICE_ID = "15010038475446345206a332922cb765";
43 const std::string BUNDLE_NAME = "testBundle";
44 const std::string NAME = ".testMainAbility";
45 }
46
47 namespace OHOS {
48 namespace AAFwk {
WaitUntilTaskFinished()49 static void WaitUntilTaskFinished()
50 {
51 const uint32_t maxRetryCount = 1000;
52 const uint32_t sleepTime = 1000;
53 uint32_t count = 0;
54 auto handler = OHOS::DelayedSingleton<AbilityManagerService>::GetInstance()->GetEventHandler();
55 std::atomic<bool> taskCalled(false);
56 auto f = [&taskCalled]() { taskCalled.store(true); };
57 if (handler->PostTask(f)) {
58 while (!taskCalled.load()) {
59 ++count;
60 if (count >= maxRetryCount) {
61 break;
62 }
63 usleep(sleepTime);
64 }
65 }
66 }
67
68 #define SLEEP(milli) std::this_thread::sleep_for(std::chrono::seconds(milli))
69
70 namespace {
71 const std::string WaitUntilTaskFinishedByTimer = "BundleMgrService";
72
SetNativeToken()73 void SetNativeToken()
74 {
75 uint64_t tokenId;
76 const char *perms[] = {
77 "ohos.permission.START_INVISIBLE_ABILITY",
78 "ohos.permission.START_ABILITIES_FROM_BACKGROUND",
79 "ohos.permission.START_ABILIIES_FROM_BACKGROUND",
80 "ohos.permission.ABILITY_BACKGROUND_COMMUNICATION"
81 };
82 NativeTokenInfoParams infoInstance = {
83 .dcapsNum = 0,
84 .permsNum = 4,
85 .aclsNum = 0,
86 .dcaps = nullptr,
87 .perms = perms,
88 .acls = nullptr,
89 .aplStr = "system_core",
90 };
91
92 infoInstance.processName = "SetUpTestCase";
93 tokenId = GetAccessTokenId(&infoInstance);
94 SetSelfTokenID(tokenId);
95 }
96 } // namespace
97
98 class AbilityManagerServiceTest : public testing::Test {
99 public:
100 static void SetUpTestCase();
101 static void TearDownTestCase();
102 void SetUp();
103 void TearDown();
104 void OnStartAms();
105 void OnStopAms();
106 int StartAbility(const Want &want);
107 static constexpr int TEST_WAIT_TIME = 100000;
108
109 public:
110 AbilityRequest abilityRequest_;
111 std::shared_ptr<AbilityRecord> abilityRecord_ {nullptr};
112 std::shared_ptr<AbilityManagerService> abilityMs_ = DelayedSingleton<AbilityManagerService>::GetInstance();
113 };
114
StartAbility(const Want & want)115 int AbilityManagerServiceTest::StartAbility(const Want &want)
116 {
117 int ref = -1;
118 return ref;
119 }
120
OnStartAms()121 void AbilityManagerServiceTest::OnStartAms()
122 {
123 if (abilityMs_) {
124 if (abilityMs_->state_ == ServiceRunningState::STATE_RUNNING) {
125 return;
126 }
127
128 abilityMs_->state_ = ServiceRunningState::STATE_RUNNING;
129
130 abilityMs_->eventLoop_ = AppExecFwk::EventRunner::Create(AbilityConfig::NAME_ABILITY_MGR_SERVICE);
131 EXPECT_TRUE(abilityMs_->eventLoop_);
132
133 abilityMs_->handler_ = std::make_shared<AbilityEventHandler>(abilityMs_->eventLoop_, abilityMs_);
134 abilityMs_->connectManager_ = std::make_shared<AbilityConnectManager>(0);
135 abilityMs_->connectManagers_.emplace(0, abilityMs_->connectManager_);
136 EXPECT_TRUE(abilityMs_->handler_);
137 EXPECT_TRUE(abilityMs_->connectManager_);
138
139 abilityMs_->dataAbilityManager_ = std::make_shared<DataAbilityManager>();
140 abilityMs_->dataAbilityManagers_.emplace(0, abilityMs_->dataAbilityManager_);
141 EXPECT_TRUE(abilityMs_->dataAbilityManager_);
142
143 abilityMs_->amsConfigResolver_ = std::make_shared<AmsConfigurationParameter>();
144 EXPECT_TRUE(abilityMs_->amsConfigResolver_);
145 abilityMs_->amsConfigResolver_->Parse();
146
147 abilityMs_->pendingWantManager_ = std::make_shared<PendingWantManager>();
148 EXPECT_TRUE(abilityMs_->pendingWantManager_);
149
150 abilityMs_->currentMissionListManager_ = std::make_shared<MissionListManager>(0);
151 abilityMs_->currentMissionListManager_->Init();
152 abilityMs_->eventLoop_->Run();
153 return;
154 }
155
156 GTEST_LOG_(INFO) << "OnStart fail";
157 }
158
OnStopAms()159 void AbilityManagerServiceTest::OnStopAms()
160 {
161 abilityMs_->OnStop();
162 }
163
SetUpTestCase()164 void AbilityManagerServiceTest::SetUpTestCase()
165 {
166 OHOS::DelayedSingleton<SaMgrClient>::GetInstance()->RegisterSystemAbility(
167 OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, new BundleMgrService());
168 SetNativeToken();
169 }
170
TearDownTestCase()171 void AbilityManagerServiceTest::TearDownTestCase()
172 {
173 OHOS::DelayedSingleton<SaMgrClient>::DestroyInstance();
174 }
175
SetUp()176 void AbilityManagerServiceTest::SetUp()
177 {
178 OnStartAms();
179 WaitUntilTaskFinished();
180 if (abilityRecord_ == nullptr) {
181 abilityRequest_.appInfo.bundleName = "data.client.bundle";
182 abilityRequest_.abilityInfo.name = "ClientAbility";
183 abilityRequest_.abilityInfo.type = AbilityType::DATA;
184 abilityRecord_ = AbilityRecord::CreateAbilityRecord(abilityRequest_);
185 }
186 }
187
TearDown()188 void AbilityManagerServiceTest::TearDown()
189 {
190 OnStopAms();
191 }
192
193 /**
194 * @tc.name: CheckStartByCallPermission_001
195 * @tc.desc: Verify function CheckStartByCallPermission return RESOLVE_CALL_NO_PERMISSIONS
196 * @tc.type: FUNC
197 * @tc.require:
198 */
199 HWTEST_F(AbilityManagerServiceTest, CheckStartByCallPermission_001, TestSize.Level1)
200 {
201 abilityRequest_.callerUid = 0;
202 EXPECT_EQ(RESOLVE_CALL_ABILITY_TYPE_ERR, abilityMs_->CheckStartByCallPermission(abilityRequest_));
203 }
204
205 /**
206 * @tc.name: CheckStartByCallPermission_002
207 * @tc.desc: Verify function CheckStartByCallPermission return ERR_OK
208 * @tc.type: FUNC
209 * @tc.require:
210 */
211 HWTEST_F(AbilityManagerServiceTest, CheckStartByCallPermission_002, TestSize.Level1)
212 {
213 abilityRequest_.callerUid = 1000;
214 abilityRequest_.abilityInfo.type = AppExecFwk::AbilityType::PAGE;
215 abilityRequest_.abilityInfo.launchMode = AppExecFwk::LaunchMode::SINGLETON;
216 EXPECT_EQ(ERR_OK, abilityMs_->CheckStartByCallPermission(abilityRequest_));
217 }
218 }
219 }
220