• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <string>
16 #include "gtest/gtest.h"
17 #include "CrashHandler.h"
18 
19 namespace {
20     class CrashHandlerTest : public ::testing::Test {
21     protected:
SetUpTestCase()22         static void SetUpTestCase()
23         {
24             // 在测试开始前重置信号处理函数为空
25             if (signal(SIGSEGV, SIG_DFL) == SIG_ERR) {
26                 printf("set signal failed");
27             }
28         }
29 
TearDownTestCase()30         static void TearDownTestCase()
31         {
32             // 在测试结束后重置信号处理函数为空
33             if (signal(SIGSEGV, SIG_DFL) == SIG_ERR) {
34                 printf("set signal failed");
35             }
36         }
37     };
38 
TEST_F(CrashHandlerTest,InitExceptionHandlerTest)39     TEST_F(CrashHandlerTest, InitExceptionHandlerTest)
40     {
41         // 保存原始的信号处理函数
42         void (*originalHandler)(int) = signal(SIGSEGV, SIG_DFL);
43         // init exception handler
44         auto richCrashHandler = std::make_unique<CrashHandler>();
45         richCrashHandler->InitExceptionHandler();
46         // 检查信号处理函数是否被正确注册
47         EXPECT_NE(signal(SIGSEGV, SIG_DFL), originalHandler);
48     }
49 }