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 29 namespace OHOS { 30 namespace MMI { 31 namespace { 32 using namespace testing::ext; 33 const std::string PROGRAM_NAME { "uds_session_test" }; 34 constexpr int32_t MODULE_TYPE { 1 }; 35 constexpr int32_t UDS_FD { 1 }; 36 constexpr int32_t UDS_UID { 100 }; 37 constexpr int32_t UDS_PID { 100 }; 38 constexpr int64_t INPUT_UI_TIMEOUT_TIME { 5 * 1000000 }; 39 constexpr int32_t TIME_CONVERT_RATIO { 1000 }; 40 } // namespace 41 42 class AnrManagerTest : public testing::Test { 43 public: SetUpTestCase(void)44 static void SetUpTestCase(void) {} TearDownTestCase(void)45 static void TearDownTestCase(void) {} 46 }; 47 48 /** 49 * @tc.name: AnrManagerTest_MarkProcessed_001 50 * @tc.desc: Features of the mark processed function 51 * @tc.type: FUNC 52 * @tc.require:SR000HQ0RR 53 */ 54 HWTEST_F(AnrManagerTest, AnrManagerTest_MarkProcessed_001, TestSize.Level1) 55 { 56 CALL_TEST_DEBUG; 57 UDSServer udsServer; 58 ANRManager anrMgr; 59 ASSERT_NO_FATAL_FAILURE(ANRMgr->Init(udsServer)); 60 int32_t pid = 123; 61 int32_t eventType = 1; 62 int32_t eventId = 456; 63 int64_t time = 123456789; 64 SessionPtr sess = std::shared_ptr<OHOS::MMI::UDSSession>(); 65 ANRMgr->TriggerANR(eventType, time, sess); 66 67 ASSERT_NO_FATAL_FAILURE(ANRMgr->MarkProcessed(pid, eventType, eventId)); 68 } 69 70 /** 71 * @tc.name: AnrManagerTest_MarkProcessed_003 72 * @tc.desc: Features of the mark processed function 73 * @tc.type: FUNC 74 * @tc.require:SR000HQ0RR 75 */ 76 HWTEST_F(AnrManagerTest, AnrManagerTest_MarkProcessed_003, TestSize.Level1) 77 { 78 CALL_TEST_DEBUG; 79 UDSServer udsServer; 80 ANRManager anrMgr; 81 ASSERT_NO_FATAL_FAILURE(ANRMgr->Init(udsServer)); 82 int32_t pid = 123; 83 int32_t eventType = 1; 84 int32_t eventId = 456; 85 int64_t time = 123456789; 86 SessionPtr sess = std::shared_ptr<OHOS::MMI::UDSSession>(); 87 ANRMgr->TriggerANR(eventType, time, sess); 88 ASSERT_NO_FATAL_FAILURE(ANRMgr->MarkProcessed(pid, eventType, eventId)); 89 90 ASSERT_NO_FATAL_FAILURE(ANRMgr->Init(udsServer)); 91 pid = 124; 92 eventType = 1; 93 eventId = 457; 94 time = 123456789; 95 ANRMgr->TriggerANR(eventType, time, sess); 96 ASSERT_NO_FATAL_FAILURE(ANRMgr->MarkProcessed(pid, eventType, eventId)); 97 98 ASSERT_NO_FATAL_FAILURE(ANRMgr->Init(udsServer)); 99 pid = 124; 100 eventType = 1; 101 eventId = 457; 102 time = 1000; 103 ANRMgr->TriggerANR(eventType, time, sess); 104 ASSERT_NO_FATAL_FAILURE(ANRMgr->MarkProcessed(pid, eventType, eventId)); 105 106 ASSERT_NO_FATAL_FAILURE(ANRMgr->Init(udsServer)); 107 pid = 124; 108 eventType = 1; 109 eventId = 457; 110 time = 1000; 111 ANRMgr->TriggerANR(eventType, time, sess); 112 ASSERT_NO_FATAL_FAILURE(ANRMgr->MarkProcessed(pid, eventType, eventId)); 113 } 114 115 /** 116 * @tc.name: AnrManagerTest_RemoveTimers_001 117 * @tc.desc: Features of the remove timers function 118 * @tc.type: FUNC 119 * @tc.require:SR000HQ0RR 120 */ 121 HWTEST_F(AnrManagerTest, AnrManagerTest_RemoveTimers_001, TestSize.Level1) 122 { 123 CALL_TEST_DEBUG; 124 UDSServer udsServer; 125 ASSERT_NO_FATAL_FAILURE(ANRMgr->Init(udsServer)); 126 SessionPtr sess; 127 ASSERT_NO_FATAL_FAILURE(ANRMgr->RemoveTimers(sess)); 128 } 129 130 /** 131 * @tc.name: AnrManagerTest_RemoveTimersByType_001 132 * @tc.desc: Remove timers by type abnormal 133 * @tc.type: FUNC 134 * @tc.require:SR000HQ0RR 135 */ 136 HWTEST_F(AnrManagerTest, AnrManagerTest_RemoveTimersByType_001, TestSize.Level1) 137 { 138 CALL_TEST_DEBUG; 139 UDSServer udsServer; 140 ASSERT_NO_FATAL_FAILURE(ANRMgr->Init(udsServer)); 141 SessionPtr session; 142 int32_t dispatchType = -1; 143 ASSERT_NO_FATAL_FAILURE(ANRMgr->RemoveTimersByType(session, dispatchType)); 144 } 145 146 /** 147 * @tc.name: AnrManagerTest_RemoveTimersByType_002 148 * @tc.desc: Remove timers by type abnormal 149 * @tc.type: FUNC 150 * @tc.require:SR000HQ0RR 151 */ 152 HWTEST_F(AnrManagerTest, AnrManagerTest_RemoveTimersByType_002, TestSize.Level1) 153 { 154 CALL_TEST_DEBUG; 155 UDSServer udsServer; 156 ASSERT_NO_FATAL_FAILURE(ANRMgr->Init(udsServer)); 157 SessionPtr session; 158 int32_t monitorType = 0; 159 ASSERT_NO_FATAL_FAILURE(ANRMgr->RemoveTimersByType(session, monitorType)); 160 } 161 162 /** 163 * @tc.name: AnrManagerTest_RemoveTimersByType_003 164 * @tc.desc: Remove timers by type normal 165 * @tc.type: FUNC 166 * @tc.require:SR000HQ0RR 167 */ 168 HWTEST_F(AnrManagerTest, AnrManagerTest_RemoveTimersByType_003, TestSize.Level1) 169 { 170 CALL_TEST_DEBUG; 171 UDSServer udsServer; 172 ASSERT_NO_FATAL_FAILURE(ANRMgr->Init(udsServer)); 173 SessionPtr session; 174 int32_t illegalType = 123; 175 ASSERT_NO_FATAL_FAILURE(ANRMgr->RemoveTimersByType(session, illegalType)); 176 } 177 178 /** 179 * @tc.name: AnrManagerTest_SetANRNoticedPid_001 180 * @tc.desc: Set noticed pid normal 181 * @tc.type: FUNC 182 * @tc.require:SR000HQ0RR 183 */ 184 HWTEST_F(AnrManagerTest, AnrManagerTest_SetANRNoticedPid_003, TestSize.Level1) 185 { 186 CALL_TEST_DEBUG; 187 UDSServer udsServer; 188 ASSERT_NO_FATAL_FAILURE(ANRMgr->Init(udsServer)); 189 int32_t pid = 1234; 190 int32_t ret = ANRMgr->SetANRNoticedPid(pid); 191 ASSERT_EQ(ret, RET_OK); 192 } 193 194 /** 195 * @tc.name: AnrManagerTest_SetANRNoticedPid_002 196 * @tc.desc: Set noticed pid abnormal 197 * @tc.type: FUNC 198 * @tc.require:SR000HQ0RR 199 */ 200 HWTEST_F(AnrManagerTest, AnrManagerTest_SetANRNoticedPid_002, TestSize.Level1) 201 { 202 CALL_TEST_DEBUG; 203 UDSServer udsServer; 204 ASSERT_NO_FATAL_FAILURE(ANRMgr->Init(udsServer)); 205 int32_t pid = -1; 206 int32_t ret = ANRMgr->SetANRNoticedPid(pid); 207 ASSERT_EQ(ret, RET_OK); 208 } 209 210 /** 211 * @tc.name: AnrManagerTest_AddTimer_001 212 * @tc.desc: Add timer function normal 213 * @tc.type: FUNC 214 * @tc.require:SR000HQ0RR 215 */ 216 HWTEST_F(AnrManagerTest, AnrManagerTest_AddTimer_001, TestSize.Level1) 217 { 218 CALL_TEST_DEBUG; 219 ANRManager anrMgr; 220 int32_t type = 1; 221 int32_t id = 1001; 222 int64_t currentTime = 123456789; 223 SessionPtr sess = std::shared_ptr<OHOS::MMI::UDSSession>(); 224 ASSERT_NO_FATAL_FAILURE(ANRMgr->AddTimer(type, id, currentTime, sess)); 225 } 226 227 /** 228 * @tc.name: AnrManagerTest_AddTimer_002 229 * @tc.desc: Add timer function abnormal 230 * @tc.type: FUNC 231 * @tc.require:SR000HQ0RR 232 */ 233 HWTEST_F(AnrManagerTest, AnrManagerTest_AddTimer_002, TestSize.Level1) 234 { 235 CALL_TEST_DEBUG; 236 ANRManager anrMgr; 237 int32_t type = -1; 238 int32_t id = -2; 239 int64_t currentTime = 123456789; 240 SessionPtr sess = std::shared_ptr<OHOS::MMI::UDSSession>(); 241 ASSERT_NO_FATAL_FAILURE(ANRMgr->AddTimer(type, id, currentTime, sess)); 242 } 243 244 /** 245 * @tc.name: AnrManagerTest_TriggerANR_001 246 * @tc.desc: Trigger function normal 247 * @tc.type: FUNC 248 * @tc.require:SR000HQ0RR 249 */ 250 HWTEST_F(AnrManagerTest, AnrManagerTest_TriggerANR_001, TestSize.Level1) 251 { 252 CALL_TEST_DEBUG; 253 int32_t type = 1; 254 int64_t time = 123456789; 255 SessionPtr sess = std::shared_ptr<OHOS::MMI::UDSSession>(); 256 bool result = ANRMgr->TriggerANR(type, time, sess); 257 EXPECT_FALSE(result); 258 } 259 260 /** 261 * @tc.name: AnrManagerTest_MarkProcessed_002 262 * @tc.desc: Cover the else branch of the if (pid_ != pid) 263 * @tc.type: FUNC 264 * @tc.require: 265 */ 266 HWTEST_F(AnrManagerTest, AnrManagerTest_MarkProcessed_002, TestSize.Level1) 267 { 268 CALL_TEST_DEBUG; 269 ANRManager anrMgr; 270 UDSServer udsServer; 271 int32_t pid = 10; 272 int32_t eventType = 1; 273 int32_t eventId = 100; 274 udsServer.pid_ = 20; 275 anrMgr.pid_ = 10; 276 anrMgr.udsServer_ = &udsServer; 277 ASSERT_EQ(anrMgr.MarkProcessed(pid, eventType, eventId), RET_ERR); 278 } 279 280 /** 281 * @tc.name: AnrManagerTest_AddTimer_003 282 * @tc.desc: Cover the else branch of the 283 * <br> if (sess->GetTokenType() != TokenType::TOKEN_HAP || sess->GetProgramName() == FOUNDATION) 284 * @tc.type: FUNC 285 * @tc.require: 286 */ 287 HWTEST_F(AnrManagerTest, AnrManagerTest_AddTimer_003, TestSize.Level1) 288 { 289 CALL_TEST_DEBUG; 290 ANRManager anrMgr; 291 int32_t type = ANR_MONITOR; 292 int32_t id = 1; 293 int64_t currentTime = 100; 294 std::string programName = "foundation"; 295 SessionPtr sess = std::make_shared<UDSSession>(programName, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID); 296 sess->SetTokenType(TokenType::TOKEN_NATIVE); 297 ASSERT_NO_FATAL_FAILURE(anrMgr.AddTimer(type, id, currentTime, sess)); 298 299 sess->SetTokenType(TokenType::TOKEN_HAP); 300 ASSERT_NO_FATAL_FAILURE(anrMgr.AddTimer(type, id, currentTime, sess)); 301 } 302 303 /** 304 * @tc.name: AnrManagerTest_AddTimer_004 305 * @tc.desc:Cover the else branch of the if (anrTimerCount_ >= MAX_TIMER_COUNT) 306 * @tc.type: FUNC 307 * @tc.require: 308 */ 309 HWTEST_F(AnrManagerTest, AnrManagerTest_AddTimer_004, TestSize.Level1) 310 { 311 CALL_TEST_DEBUG; 312 ANRManager anrMgr; 313 int32_t type = ANR_MONITOR; 314 int32_t id = 1; 315 int64_t currentTime = 100; 316 SessionPtr sess = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID); 317 sess->SetTokenType(TokenType::TOKEN_HAP); 318 anrMgr.anrTimerCount_ = 51; 319 ASSERT_NO_FATAL_FAILURE(anrMgr.AddTimer(type, id, currentTime, sess)); 320 321 anrMgr.anrTimerCount_ = 49; 322 ASSERT_NO_FATAL_FAILURE(anrMgr.AddTimer(type, id, currentTime, sess)); 323 } 324 325 /** 326 * @tc.name: AnrManagerTest_RemoveTimers_002 327 * @tc.desc: Cover the RemoveTimers function branch 328 * @tc.type: FUNC 329 * @tc.require: 330 */ 331 HWTEST_F(AnrManagerTest, AnrManagerTest_RemoveTimers_002, TestSize.Level1) 332 { 333 CALL_TEST_DEBUG; 334 ANRManager anrMgr; 335 SessionPtr sess = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID); 336 std::vector<UDSSession::EventTime> events { { 0, 0, -1 }, { 1, 1, 10 } }; 337 sess->events_[ANR_DISPATCH] = events; 338 sess->events_[ANR_MONITOR] = events; 339 ASSERT_NO_FATAL_FAILURE(anrMgr.RemoveTimers(sess)); 340 } 341 342 /** 343 * @tc.name: AnrManagerTest_RemoveTimersByType_004 344 * @tc.desc: Cover the RemoveTimersByType function branch 345 * @tc.type: FUNC 346 * @tc.require: 347 */ 348 HWTEST_F(AnrManagerTest, AnrManagerTest_RemoveTimersByType_004, TestSize.Level1) 349 { 350 CALL_TEST_DEBUG; 351 ANRManager anrMgr; 352 SessionPtr sess = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID); 353 int32_t type = 5; 354 ASSERT_NO_FATAL_FAILURE(anrMgr.RemoveTimersByType(sess, type)); 355 356 type = ANR_DISPATCH; 357 std::vector<UDSSession::EventTime> events { { 0, 0, -1 }, { 1, 1, 10 } }; 358 sess->events_[ANR_MONITOR] = events; 359 ASSERT_NO_FATAL_FAILURE(anrMgr.RemoveTimersByType(sess, type)); 360 } 361 362 /** 363 * @tc.name: AnrManagerTest_TriggerANR_002 364 * @tc.desc: Cover the TriggerANR function branch 365 * @tc.type: FUNC 366 * @tc.require: 367 */ 368 HWTEST_F(AnrManagerTest, AnrManagerTest_TriggerANR_002, TestSize.Level1) 369 { 370 CALL_TEST_DEBUG; 371 ANRManager anrMgr; 372 int32_t type = ANR_MONITOR; 373 int64_t time = 1; 374 std::string programName = "foundation"; 375 SessionPtr sess = std::make_shared<UDSSession>(programName, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID); 376 UDSServer udsServer; 377 anrMgr.udsServer_ = &udsServer; 378 sess->SetTokenType(TokenType::TOKEN_NATIVE); 379 EXPECT_FALSE(anrMgr.TriggerANR(type, time, sess)); 380 381 sess->SetTokenType(TokenType::TOKEN_HAP); 382 EXPECT_FALSE(anrMgr.TriggerANR(type, time, sess)); 383 384 bool status = true; 385 SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID); 386 session->SetTokenType(TokenType::TOKEN_HAP); 387 session->SetAnrStatus(type, status); 388 EXPECT_TRUE(anrMgr.TriggerANR(type, time, session)); 389 390 type = ANR_DISPATCH; 391 status = false; 392 EXPECT_FALSE(anrMgr.TriggerANR(type, time, session)); 393 } 394 395 /** 396 * @tc.name: AnrManagerTest_OnSessionLost 397 * @tc.desc: Cover the OnSessionLost function branch 398 * @tc.type: FUNC 399 * @tc.require: 400 */ 401 HWTEST_F(AnrManagerTest, AnrManagerTest_OnSessionLost, TestSize.Level1) 402 { 403 CALL_TEST_DEBUG; 404 ANRManager anrMgr; 405 SessionPtr sess = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID); 406 anrMgr.anrNoticedPid_ = UDS_PID; 407 ASSERT_NO_FATAL_FAILURE(anrMgr.OnSessionLost(sess)); 408 409 anrMgr.anrNoticedPid_ = 200; 410 ASSERT_NO_FATAL_FAILURE(anrMgr.OnSessionLost(sess)); 411 } 412 413 /** 414 * @tc.name : AnrManagerTest_HandleAnrState_001 415 * @tc.desc : Test without timeout event. 416 * @tc.type: FUNC 417 * @tc.require: 418 */ 419 HWTEST_F(AnrManagerTest, AnrManagerTest_HandleAnrState_001, TestSize.Level1) 420 { 421 SessionPtr sess = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID); 422 int32_t type = ANR_DISPATCH; 423 int64_t currentTime = 1000; 424 425 ANRMgr->HandleAnrState(sess, type, currentTime); 426 427 EXPECT_TRUE(sess->GetEventsByType(type).empty()); 428 } 429 430 /** 431 * @tc.name : AnrManagerTest_HandleAnrState_002 432 * @tc.desc : Test when there are multiple timeout event. 433 * @tc.type: FUNC 434 * @tc.require: 435 */ 436 HWTEST_F(AnrManagerTest, AnrManagerTest_HandleAnrState_002, TestSize.Level1) 437 { 438 SessionPtr sess = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID); 439 int32_t type = ANR_DISPATCH; 440 int64_t currentTime = 10000; 441 442 std::vector<UDSSession::EventTime> event { {1, 1000, 1}, {2, 2000, 2}, {3, 3000, 3} }; 443 sess->events_[ANR_DISPATCH] = event; 444 445 ANRMgr->HandleAnrState(sess, type, currentTime); 446 447 auto events = sess->GetEventsByType(type); 448 EXPECT_EQ(events.size(), 1); 449 EXPECT_EQ(events[0].id, 3); 450 } 451 452 /** 453 * @tc.name : AnrManagerTest_HandleAnrState_003 454 * @tc.desc : Test when there is only one timeout event. 455 * @tc.type: FUNC 456 * @tc.require: 457 */ 458 HWTEST_F(AnrManagerTest, AnrManagerTest_HandleAnrState_003, TestSize.Level1) 459 { 460 SessionPtr sess = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID); 461 int32_t type = ANR_DISPATCH; 462 int64_t currentTime = 10000; 463 std::vector<UDSSession::EventTime> event { {1, 1000, 1} }; 464 sess->events_[ANR_DISPATCH] = event; 465 ANRMgr->HandleAnrState(sess, type, currentTime); 466 467 auto events = sess->GetEventsByType(type); 468 EXPECT_EQ(events.size(), 1); 469 EXPECT_EQ(events[0].id, 1); 470 } 471 472 /** 473 * @tc.name : AnrManagerTest_HandleAnrState_004 474 * @tc.desc : Test when the event exists but there are no timeout events. 475 * @tc.type: FUNC 476 * @tc.require: 477 */ 478 HWTEST_F(AnrManagerTest, AnrManagerTest_HandleAnrState_004, TestSize.Level1) 479 { 480 SessionPtr sess = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID); 481 int32_t type = ANR_DISPATCH; 482 int64_t currentTime = 1500; 483 std::vector<UDSSession::EventTime> events { {1, 1000, 1} }; 484 sess->events_[type] = events; 485 ANRMgr->HandleAnrState(sess, type, currentTime); 486 auto resultEvents = sess->GetEventsByType(type); 487 EXPECT_EQ(resultEvents.size(), 1); 488 EXPECT_EQ(resultEvents[0].id, 1); 489 } 490 491 /** 492 * @tc.name : AnrManagerTest_HandleAnrState_005 493 * @tc.desc : Test multiple events with partial timeouts. 494 * @tc.type: FUNC 495 * @tc.require: 496 */ 497 HWTEST_F(AnrManagerTest, AnrManagerTest_HandleAnrState_005, TestSize.Level1) 498 { 499 SessionPtr sess = std::make_shared<UDSSession>(PROGRAM_NAME, MODULE_TYPE, UDS_FD, UDS_UID, UDS_PID); 500 int32_t type = ANR_DISPATCH; 501 int64_t currentTime = 10000; 502 const int64_t timeoutThreshold = INPUT_UI_TIMEOUT_TIME / TIME_CONVERT_RATIO; 503 std::vector<UDSSession::EventTime> events { 504 {1, currentTime - timeoutThreshold - 100, 1}, 505 {2, currentTime - timeoutThreshold + 100, 2}, 506 {3, currentTime - timeoutThreshold - 200, 3} }; 507 sess->events_[type] = events; 508 ANRMgr->HandleAnrState(sess, type, currentTime); 509 auto resultEvents = sess->GetEventsByType(type); 510 ASSERT_EQ(resultEvents.size(), 0); 511 } 512 } // namespace MMI 513 } // namespace OHOS