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 "ability_manager_errors.h"
26 #include "ability_scheduler.h"
27 #include "bundlemgr/mock_bundle_manager.h"
28 #include "if_system_ability_manager.h"
29 #include "iservice_registry.h"
30 #include "mock_ability_connect_callback.h"
31 #include "mock_ability_token.h"
32 #include "sa_mgr_client.h"
33 #include "system_ability_definition.h"
34 #include "ui_service_mgr_client_mock.h"
35
36 using namespace testing;
37 using namespace testing::ext;
38 using namespace OHOS::AppExecFwk;
39
40 namespace {
41 const std::string DEVICE_ID = "15010038475446345206a332922cb765";
42 const std::string BUNDLE_NAME = "testBundle";
43 const std::string NAME = ".testMainAbility";
44 const std::string EVENT_WAITING_CODE = "0";
45 const std::string EVENT_CLOSE_CODE = "1";
46 }
47
48 namespace OHOS {
49 namespace AAFwk {
WaitUntilTaskFinished()50 static void WaitUntilTaskFinished()
51 {
52 const uint32_t maxRetryCount = 1000;
53 const uint32_t sleepTime = 1000;
54 uint32_t count = 0;
55 auto handler = OHOS::DelayedSingleton<AbilityManagerService>::GetInstance()->GetEventHandler();
56 std::atomic<bool> taskCalled(false);
57 auto f = [&taskCalled]() { taskCalled.store(true); };
58 if (handler->PostTask(f)) {
59 while (!taskCalled.load()) {
60 ++count;
61 if (count >= maxRetryCount) {
62 break;
63 }
64 usleep(sleepTime);
65 }
66 }
67 }
68
69 class AbilityManagerServiceAnrTest : public testing::Test {
70 public:
71 static void SetUpTestCase();
72 static void TearDownTestCase();
73 void SetUp();
74 void TearDown();
75 void OnStartAms();
76 void OnStopAms();
77 static constexpr int TEST_WAIT_TIME = 100000;
78
79 public:
80 std::shared_ptr<AbilityManagerService> abilityMs_ = DelayedSingleton<AbilityManagerService>::GetInstance();
81 };
82
OnStartAms()83 void AbilityManagerServiceAnrTest::OnStartAms()
84 {
85 if (abilityMs_) {
86 if (abilityMs_->state_ == ServiceRunningState::STATE_RUNNING) {
87 return;
88 }
89
90 abilityMs_->state_ = ServiceRunningState::STATE_RUNNING;
91
92 abilityMs_->eventLoop_ = AppExecFwk::EventRunner::Create(AbilityConfig::NAME_ABILITY_MGR_SERVICE);
93 EXPECT_TRUE(abilityMs_->eventLoop_);
94
95 abilityMs_->handler_ = std::make_shared<AbilityEventHandler>(abilityMs_->eventLoop_, abilityMs_);
96
97 abilityMs_->dataAbilityManager_ = std::make_shared<DataAbilityManager>();
98 abilityMs_->dataAbilityManagers_.emplace(0, abilityMs_->dataAbilityManager_);
99 EXPECT_TRUE(abilityMs_->dataAbilityManager_);
100
101 abilityMs_->amsConfigResolver_ = std::make_shared<AmsConfigurationParameter>();
102 EXPECT_TRUE(abilityMs_->amsConfigResolver_);
103 abilityMs_->amsConfigResolver_->Parse();
104
105 abilityMs_->pendingWantManager_ = std::make_shared<PendingWantManager>();
106 EXPECT_TRUE(abilityMs_->pendingWantManager_);
107
108 #ifdef SUPPORT_GRAPHICS
109 auto deviceType = abilityMs_->amsConfigResolver_->GetDeviceType();
110 DelayedSingleton<SystemDialogScheduler>::GetInstance()->SetDeviceType(deviceType);
111 #endif
112
113 abilityMs_->eventLoop_->Run();
114 return;
115 }
116
117 GTEST_LOG_(INFO) << "OnStart fail";
118 }
119
OnStopAms()120 void AbilityManagerServiceAnrTest::OnStopAms()
121 {
122 abilityMs_->OnStop();
123 }
124
SetUpTestCase()125 void AbilityManagerServiceAnrTest::SetUpTestCase()
126 {
127 OHOS::DelayedSingleton<SaMgrClient>::GetInstance()->RegisterSystemAbility(
128 OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, new BundleMgrService());
129 }
130
TearDownTestCase()131 void AbilityManagerServiceAnrTest::TearDownTestCase()
132 {
133 OHOS::DelayedSingleton<SaMgrClient>::DestroyInstance();
134 OHOS::DelayedSingleton<AbilityManagerService>::DestroyInstance();
135 }
136
SetUp()137 void AbilityManagerServiceAnrTest::SetUp()
138 {
139 OnStartAms();
140 WaitUntilTaskFinished();
141 }
142
TearDown()143 void AbilityManagerServiceAnrTest::TearDown()
144 {
145 OnStopAms();
146 }
147
148 /*
149 * Feature: AbilityManagerService
150 * Function: SendANRProcessID
151 * SubFunction: NA
152 * FunctionPoints: Waiting anr process
153 * EnvConditions: NA
154 * CaseDescription: Fork a new process, call SendANRProcessID func in new process id
155 * click waiting button, do not kill the new process
156 */
157 HWTEST_F(AbilityManagerServiceAnrTest, SendANRProcessID_001, TestSize.Level1)
158 {
159 pid_t pid;
160 if ((pid=fork()) == 0) {
161 for (;;) {
162 usleep(500);
163 }
164 }
165 else {
166 Ace::UIServiceMgrClient::GetInstance()->SetDialogCheckState(pid, EVENT_WAITING_CODE);
167 auto result = abilityMs_->SendANRProcessID(pid);
168 sleep(6);
169 if (result == ERR_OK) {
170 EXPECT_TRUE(Ace::UIServiceMgrClient::GetInstance()->GetAppRunningState());
171 }
172 (void)kill(pid, SIGKILL);
173 }
174 }
175
176 /*
177 * Feature: AbilityManagerService
178 * Function: SendANRProcessID
179 * SubFunction: NA
180 * FunctionPoints: Waiting anr process
181 * EnvConditions: NA
182 * CaseDescription: create a new exception process, call SendANRProcessID func
183 * click waiting button, do not kill the new process
184 */
185 HWTEST_F(AbilityManagerServiceAnrTest, SendANRProcessID_002, TestSize.Level1)
186 {
187 pid_t pid = -1;
188 auto result = abilityMs_->SendANRProcessID(pid);
189 sleep(6);
190 EXPECT_TRUE(result == ERR_OK);
191 }
192 }
193 }
194