• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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: Kill anr process
153  * EnvConditions: NA
154  * CaseDescription: Fork a new process, call SendANRProcessID func in new process id
155  * click close button, 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         }
163     }
164     else {
165         Ace::UIServiceMgrClient::GetInstance()->SetDialogCheckState(pid, EVENT_CLOSE_CODE);
166         auto result = abilityMs_->SendANRProcessID(pid);
167         sleep(6);
168         if (result == ERR_OK) {
169             EXPECT_FALSE(Ace::UIServiceMgrClient::GetInstance()->GetAppRunningState());
170         }
171         kill(pid, SIGKILL);
172     }
173 }
174 
175 /*
176  * Feature: AbilityManagerService
177  * Function: SendANRProcessID
178  * SubFunction: NA
179  * FunctionPoints: Waiting anr process
180  * EnvConditions: NA
181  * CaseDescription: Fork a new process, call SendANRProcessID func in new process id
182  * click waiting button, do not kill the new process
183  */
184 HWTEST_F(AbilityManagerServiceAnrTest, SendANRProcessID_002, TestSize.Level1)
185 {
186     pid_t pid;
187     if ((pid=fork()) == 0) {
188         for (;;) {
189             usleep(500);
190         }
191     }
192     else {
193         Ace::UIServiceMgrClient::GetInstance()->SetDialogCheckState(pid, EVENT_WAITING_CODE);
194         auto result = abilityMs_->SendANRProcessID(pid);
195         sleep(6);
196         if (result == ERR_OK) {
197             EXPECT_TRUE(Ace::UIServiceMgrClient::GetInstance()->GetAppRunningState());
198         }
199         (void)kill(pid, SIGKILL);
200     }
201 }
202 
203 /*
204  * Feature: AbilityManagerService
205  * Function: SendANRProcessID
206  * SubFunction: NA
207  * FunctionPoints: Waiting anr process
208  * EnvConditions: NA
209  * CaseDescription: create a new exception process, call SendANRProcessID func
210  * click waiting button, do not kill the new process
211  */
212 HWTEST_F(AbilityManagerServiceAnrTest, SendANRProcessID_003, TestSize.Level1)
213 {
214     pid_t pid = -1;
215     auto result = abilityMs_->SendANRProcessID(pid);
216     sleep(6);
217     EXPECT_TRUE(result == ERR_INVALID_VALUE);
218 }
219 }
220 }
221