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