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_manager_errors.h"
24 #include "ui_service_mgr_client_mock.h"
25
26 using namespace testing;
27 using namespace testing::ext;
28 using namespace OHOS::AppExecFwk;
29
30 namespace {
31 const std::string DEVICE_ID = "15010038475446345206a332922cb765";
32 const std::string BUNDLE_NAME = "testBundle";
33 const std::string NAME = ".testMainAbility";
34 const std::string EVENT_WAITING_CODE = "0";
35 const std::string EVENT_CLOSE_CODE = "1";
36 }
37
38 namespace OHOS {
39 namespace AAFwk {
40 class AbilityManagerServiceAnrTest : public testing::Test {
41 public:
42 static void SetUpTestCase();
43 static void TearDownTestCase();
44 void SetUp();
45 void TearDown();
46 static constexpr int TEST_WAIT_TIME = 100000;
47 };
48
SetUpTestCase()49 void AbilityManagerServiceAnrTest::SetUpTestCase() {}
50
TearDownTestCase()51 void AbilityManagerServiceAnrTest::TearDownTestCase() {}
52
SetUp()53 void AbilityManagerServiceAnrTest::SetUp() {}
54
TearDown()55 void AbilityManagerServiceAnrTest::TearDown() {}
56
57 /*
58 * Feature: AbilityManagerService
59 * Function: SendANRProcessID
60 * SubFunction: NA
61 * FunctionPoints: Kill anr process
62 * EnvConditions: NA
63 * CaseDescription: Fork a new process, call SendANRProcessID func in new process id
64 * click close button, kill the new process
65 */
66 HWTEST_F(AbilityManagerServiceAnrTest, SendANRProcessID_001, TestSize.Level1)
67 {
68 auto abilityMs_ = std::make_shared<AbilityManagerService>();
69 abilityMs_->OnStart();
70 pid_t pid;
71 if ((pid = fork()) == 0) {
72 for (;;) {
73 }
74 } else {
75 Ace::UIServiceMgrClient::GetInstance()->SetDialogCheckState(pid, EVENT_CLOSE_CODE);
76 auto result = abilityMs_->SendANRProcessID(pid);
77 sleep(6);
78 if (result == ERR_OK) {
79 EXPECT_FALSE(Ace::UIServiceMgrClient::GetInstance()->GetAppRunningState());
80 }
81 kill(pid, SIGKILL);
82 }
83 }
84
85 /*
86 * Feature: AbilityManagerService
87 * Function: SendANRProcessID
88 * SubFunction: NA
89 * FunctionPoints: Waiting anr process
90 * EnvConditions: NA
91 * CaseDescription: Fork a new process, call SendANRProcessID func in new process id
92 * click waiting button, do not kill the new process
93 */
94 HWTEST_F(AbilityManagerServiceAnrTest, SendANRProcessID_002, TestSize.Level1)
95 {
96 auto abilityMs_ = std::make_shared<AbilityManagerService>();
97 abilityMs_->OnStart();
98 pid_t pid;
99 if ((pid = fork()) == 0) {
100 for (;;) {
101 usleep(500);
102 }
103 } else {
104 Ace::UIServiceMgrClient::GetInstance()->SetDialogCheckState(pid, EVENT_WAITING_CODE);
105 auto result = abilityMs_->SendANRProcessID(pid);
106 sleep(6);
107 if (result == ERR_OK) {
108 EXPECT_TRUE(Ace::UIServiceMgrClient::GetInstance()->GetAppRunningState());
109 }
110 (void)kill(pid, SIGKILL);
111 }
112 }
113
114 /*
115 * Feature: AbilityManagerService
116 * Function: SendANRProcessID
117 * SubFunction: NA
118 * FunctionPoints: Waiting anr process
119 * EnvConditions: NA
120 * CaseDescription: create a new exception process, call SendANRProcessID func
121 * click waiting button, do not kill the new process
122 */
123 HWTEST_F(AbilityManagerServiceAnrTest, SendANRProcessID_003, TestSize.Level1)
124 {
125 auto abilityMs_ = std::make_shared<AbilityManagerService>();
126 abilityMs_->OnStart();
127 pid_t pid = -1;
128 auto result = abilityMs_->SendANRProcessID(pid);
129 sleep(6);
130 EXPECT_TRUE(result == ERR_INVALID_VALUE);
131 }
132 }
133 }
134