1/*
2 * Copyright (c) 2024 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#include <libinput.h>
18
19#include "anr_manager.h"
20#include "dfx_hisysevent.h"
21#include "input_event_handler.h"
22#include "i_input_windows_manager.h"
23#include "mmi_log.h"
24#include "napi_constants.h"
25#include "proto.h"
26#include "timer_manager.h"
27#include "window_manager.h"
28
29namespace OHOS {
30namespace MMI {
31namespace {
32using namespace testing::ext;
33const std::string PROGRAM_NAME = "uds_session_test";
34constexpr int32_t MODULE_TYPE = 1;
35constexpr int32_t UDS_FD = 1;
36constexpr int32_t UDS_UID = 100;
37constexpr int32_t UDS_PID = 100;
38} // namespace
39
40class AnrManagerTest : public testing::Test {
41public:
42    static void SetUpTestCase(void) {}
43    static void TearDownTestCase(void) {}
44};
45
46/**
47 * @tc.name: AnrManagerTest_MarkProcessed_001
48 * @tc.desc: Features of the mark processed function
49 * @tc.type: FUNC
50 * @tc.require:SR000HQ0RR
51 */
52HWTEST_F(AnrManagerTest, AnrManagerTest_MarkProcessed_001, TestSize.Level1)
53{
54    CALL_TEST_DEBUG;
55    UDSServer udsServer;
56    ASSERT_NO_FATAL_FAILURE(ANRMgr->Init(udsServer));
57    int32_t pid = 123;
58    int32_t eventType = 1;
59    int32_t eventId = 456;
60    ASSERT_NO_FATAL_FAILURE(ANRMgr->MarkProcessed(pid, eventType, eventId));
61}
62
63/**
64 * @tc.name: AnrManagerTest_RemoveTimers_001
65 * @tc.desc: Features of the remove timers function
66 * @tc.type: FUNC
67 * @tc.require:SR000HQ0RR
68 */
69HWTEST_F(AnrManagerTest, AnrManagerTest_RemoveTimers_001, TestSize.Level1)
70{
71    CALL_TEST_DEBUG;
72    UDSServer udsServer;
73    ASSERT_NO_FATAL_FAILURE(ANRMgr->Init(udsServer));
74    SessionPtr sess;
75    ASSERT_NO_FATAL_FAILURE(ANRMgr->RemoveTimers(sess));
76}
77
78/**
79 * @tc.name: AnrManagerTest_RemoveTimersByType_001
80 * @tc.desc: Remove timers by type abnormal
81 * @tc.type: FUNC
82 * @tc.require:SR000HQ0RR
83 */
84HWTEST_F(AnrManagerTest, AnrManagerTest_RemoveTimersByType_001, TestSize.Level1)
85{
86    CALL_TEST_DEBUG;
87    UDSServer udsServer;
88    ASSERT_NO_FATAL_FAILURE(ANRMgr->Init(udsServer));
89    SessionPtr session;
90    int32_t dispatchType = -1;
91    ASSERT_NO_FATAL_FAILURE(ANRMgr->RemoveTimersByType(session, dispatchType));
92}
93
94/**
95 * @tc.name: AnrManagerTest_RemoveTimersByType_002
96 * @tc.desc: Remove timers by type abnormal
97 * @tc.type: FUNC
98 * @tc.require:SR000HQ0RR
99 */
100HWTEST_F(AnrManagerTest, AnrManagerTest_RemoveTimersByType_002, TestSize.Level1)
101{
102    CALL_TEST_DEBUG;
103    UDSServer udsServer;
104    ASSERT_NO_FATAL_FAILURE(ANRMgr->Init(udsServer));
105    SessionPtr session;
106    int32_t monitorType = 0;
107    ASSERT_NO_FATAL_FAILURE(ANRMgr->RemoveTimersByType(session, monitorType));
108}
109
110/**
111 * @tc.name: AnrManagerTest_RemoveTimersByType_003
112 * @tc.desc: Remove timers by type normal
113 * @tc.type: FUNC
114 * @tc.require:SR000HQ0RR
115 */
116HWTEST_F(AnrManagerTest, AnrManagerTest_RemoveTimersByType_003, TestSize.Level1)
117{
118    CALL_TEST_DEBUG;
119    UDSServer udsServer;
120    ASSERT_NO_FATAL_FAILURE(ANRMgr->Init(udsServer));
121    SessionPtr session;
122    int32_t illegalType = 123;
123    ASSERT_NO_FATAL_FAILURE(ANRMgr->RemoveTimersByType(session, illegalType));
124}
125
126/**
127 * @tc.name: AnrManagerTest_SetANRNoticedPid_001
128 * @tc.desc: Set noticed pid normal
129 * @tc.type: FUNC
130 * @tc.require:SR000HQ0RR
131 */
132HWTEST_F(AnrManagerTest, AnrManagerTest_SetANRNoticedPid_003, TestSize.Level1)
133{
134    CALL_TEST_DEBUG;
135    UDSServer udsServer;
136    ASSERT_NO_FATAL_FAILURE(ANRMgr->Init(udsServer));
137    int32_t pid = 1234;
138    int32_t ret = ANRMgr->SetANRNoticedPid(pid);
139    ASSERT_EQ(ret, RET_OK);
140}
141
142/**
143 * @tc.name: AnrManagerTest_SetANRNoticedPid_002
144 * @tc.desc: Set noticed pid abnormal
145 * @tc.type: FUNC
146 * @tc.require:SR000HQ0RR
147 */
148HWTEST_F(AnrManagerTest, AnrManagerTest_SetANRNoticedPid_002, TestSize.Level1)
149{
150    CALL_TEST_DEBUG;
151    UDSServer udsServer;
152    ASSERT_NO_FATAL_FAILURE(ANRMgr->Init(udsServer));
153    int32_t pid = -1;
154    int32_t ret = ANRMgr->SetANRNoticedPid(pid);
155    ASSERT_EQ(ret, RET_OK);
156}
157
158/**
159 * @tc.name: AnrManagerTest_AddTimer_001
160 * @tc.desc: Add timer function normal
161 * @tc.type: FUNC
162 * @tc.require:SR000HQ0RR
163 */
164HWTEST_F(AnrManagerTest, AnrManagerTest_AddTimer_001, TestSize.Level1)
165{
166    CALL_TEST_DEBUG;
167    int32_t type = 1;
168    int32_t id = 1001;
169    int64_t currentTime = 123456789;
170    SessionPtr sess = std::shared_ptr<OHOS::MMI::UDSSession>();
171    ASSERT_NO_FATAL_FAILURE(ANRMgr->AddTimer(type, id, currentTime, sess));
172}
173
174/**
175 * @tc.name: AnrManagerTest_AddTimer_002
176 * @tc.desc: Add timer function abnormal
177 * @tc.type: FUNC
178 * @tc.require:SR000HQ0RR
179 */
180HWTEST_F(AnrManagerTest, AnrManagerTest_AddTimer_002, TestSize.Level1)
181{
182    CALL_TEST_DEBUG;
183    int32_t type = -1;
184    int32_t id = -2;
185    int64_t currentTime = 123456789;
186    SessionPtr sess = std::shared_ptr<OHOS::MMI::UDSSession>();
187    ASSERT_NO_FATAL_FAILURE(ANRMgr->AddTimer(type, id, currentTime, sess));
188}
189
190/**
191 * @tc.name: AnrManagerTest_TriggerANR_001
192 * @tc.desc: Trigger function normal
193 * @tc.type: FUNC
194 * @tc.require:SR000HQ0RR
195 */
196HWTEST_F(AnrManagerTest, AnrManagerTest_TriggerANR_001, TestSize.Level1)
197{
198    CALL_TEST_DEBUG;
199    int32_t type = 1;
200    int64_t time = 123456789;
201    SessionPtr sess = std::shared_ptr<OHOS::MMI::UDSSession>();
202    bool result = ANRMgr->TriggerANR(type, time, sess);
203    EXPECT_FALSE(result);
204}
205
206/**
207 * @tc.name: AnrManagerTest_MarkProcessed_002
208 * @tc.desc: Cover the else branch of the if (pid_ != pid)
209 * @tc.type: FUNC
210 * @tc.require:
211 */
212HWTEST_F(AnrManagerTest, AnrManagerTest_MarkProcessed_002, TestSize.Level1)
213{
214    CALL_TEST_DEBUG;
215    ANRManager anrMgr;
216    UDSServer udsServer;
217    int32_t pid = 10;
218    int32_t eventType = 1;
219    int32_t eventId = 100;
220    udsServer.pid_ = 20;
221    anrMgr.pid_ = 10;
222    anrMgr.udsServer_ = &udsServer;
223    ASSERT_EQ(anrMgr.MarkProcessed(pid, eventType, eventId), RET_ERR);
224}
225
226/**
227 * @tc.name: AnrManagerTest_AddTimer_003
228 * @tc.desc: Cover the else branch of the
229 * <br> if (sess->GetTokenType() != TokenType::TOKEN_HAP || sess->GetProgramName() == FOUNDATION)
230 * @tc.type: FUNC
231 * @tc.require:
232 */
233HWTEST_F(AnrManagerTest, AnrManagerTest_AddTimer_003, TestSize.Level1)
234{
235    CALL_TEST_DEBUG;
236    ANRManager anrMgr;
237    int32_t type = ANR_MONITOR;
238    int32_t id = 1;
239    int64_t currentTime = 100;
240    std::string programName = "foundation";
241    SessionPtr sess = std::make_shared<UDSSession>(programName, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID);
242    sess->SetTokenType(TokenType::TOKEN_NATIVE);
243    ASSERT_NO_FATAL_FAILURE(anrMgr.AddTimer(type, id, currentTime, sess));
244
245    sess->SetTokenType(TokenType::TOKEN_HAP);
246    ASSERT_NO_FATAL_FAILURE(anrMgr.AddTimer(type, id, currentTime, sess));
247}
248
249/**
250 * @tc.name: AnrManagerTest_AddTimer_004
251 * @tc.desc:Cover the else branch of the if (anrTimerCount_ >= MAX_TIMER_COUNT)
252 * @tc.type: FUNC
253 * @tc.require:
254 */
255HWTEST_F(AnrManagerTest, AnrManagerTest_AddTimer_004, TestSize.Level1)
256{
257    CALL_TEST_DEBUG;
258    ANRManager anrMgr;
259    int32_t type = ANR_MONITOR;
260    int32_t id = 1;
261    int64_t currentTime = 100;
262    SessionPtr sess = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID);
263    sess->SetTokenType(TokenType::TOKEN_HAP);
264    anrMgr.anrTimerCount_ = 51;
265    ASSERT_NO_FATAL_FAILURE(anrMgr.AddTimer(type, id, currentTime, sess));
266
267    anrMgr.anrTimerCount_ = 49;
268    ASSERT_NO_FATAL_FAILURE(anrMgr.AddTimer(type, id, currentTime, sess));
269}
270
271/**
272 * @tc.name: AnrManagerTest_RemoveTimers_002
273 * @tc.desc: Cover the RemoveTimers function branch
274 * @tc.type: FUNC
275 * @tc.require:
276 */
277HWTEST_F(AnrManagerTest, AnrManagerTest_RemoveTimers_002, TestSize.Level1)
278{
279    CALL_TEST_DEBUG;
280    ANRManager anrMgr;
281    SessionPtr sess = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID);
282    std::vector<UDSSession::EventTime> events { { 0, 0, -1 }, { 1, 1, 10 } };
283    sess->events_[ANR_DISPATCH] = events;
284    sess->events_[ANR_MONITOR] = events;
285    ASSERT_NO_FATAL_FAILURE(anrMgr.RemoveTimers(sess));
286}
287
288/**
289 * @tc.name: AnrManagerTest_RemoveTimersByType_004
290 * @tc.desc: Cover the RemoveTimersByType function branch
291 * @tc.type: FUNC
292 * @tc.require:
293 */
294HWTEST_F(AnrManagerTest, AnrManagerTest_RemoveTimersByType_004, TestSize.Level1)
295{
296    CALL_TEST_DEBUG;
297    ANRManager anrMgr;
298    SessionPtr sess = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID);
299    int32_t type = 5;
300    ASSERT_NO_FATAL_FAILURE(anrMgr.RemoveTimersByType(sess, type));
301
302    type = ANR_DISPATCH;
303    std::vector<UDSSession::EventTime> events { { 0, 0, -1 }, { 1, 1, 10 } };
304    sess->events_[ANR_MONITOR] = events;
305    ASSERT_NO_FATAL_FAILURE(anrMgr.RemoveTimersByType(sess, type));
306}
307
308/**
309 * @tc.name: AnrManagerTest_TriggerANR_002
310 * @tc.desc: Cover the TriggerANR function branch
311 * @tc.type: FUNC
312 * @tc.require:
313 */
314HWTEST_F(AnrManagerTest, AnrManagerTest_TriggerANR_002, TestSize.Level1)
315{
316    CALL_TEST_DEBUG;
317    ANRManager anrMgr;
318    int32_t type = ANR_MONITOR;
319    int64_t time = 1;
320    std::string programName = "foundation";
321    SessionPtr sess = std::make_shared<UDSSession>(programName, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID);
322    UDSServer udsServer;
323    anrMgr.udsServer_ = &udsServer;
324    sess->SetTokenType(TokenType::TOKEN_NATIVE);
325    EXPECT_FALSE(anrMgr.TriggerANR(type, time, sess));
326
327    sess->SetTokenType(TokenType::TOKEN_HAP);
328    EXPECT_FALSE(anrMgr.TriggerANR(type, time, sess));
329
330    bool status = true;
331    SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID);
332    session->SetTokenType(TokenType::TOKEN_HAP);
333    session->SetAnrStatus(type, status);
334    EXPECT_TRUE(anrMgr.TriggerANR(type, time, session));
335
336    type = ANR_DISPATCH;
337    status = false;
338    EXPECT_FALSE(anrMgr.TriggerANR(type, time, session));
339}
340
341/**
342 * @tc.name: AnrManagerTest_OnSessionLost
343 * @tc.desc: Cover the OnSessionLost function branch
344 * @tc.type: FUNC
345 * @tc.require:
346 */
347HWTEST_F(AnrManagerTest, AnrManagerTest_OnSessionLost, TestSize.Level1)
348{
349    CALL_TEST_DEBUG;
350    ANRManager anrMgr;
351    SessionPtr sess = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID);
352    anrMgr.anrNoticedPid_ = UDS_PID;
353    ASSERT_NO_FATAL_FAILURE(anrMgr.OnSessionLost(sess));
354
355    anrMgr.anrNoticedPid_ = 200;
356    ASSERT_NO_FATAL_FAILURE(anrMgr.OnSessionLost(sess));
357}
358} // namespace MMI
359} // namespace OHOS