• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 <map>
18 #include <memory>
19 #include <string>
20 
21 #include "dfx_signal.h"
22 
23 using namespace OHOS::HiviewDFX;
24 using namespace testing::ext;
25 using namespace std;
26 
27 namespace OHOS {
28 namespace HiviewDFX {
29 class DfxSignalTest : public testing::Test {
30 public:
SetUpTestCase(void)31     static void SetUpTestCase(void) {}
TearDownTestCase(void)32     static void TearDownTestCase(void) {}
SetUp()33     void SetUp() {}
TearDown()34     void TearDown() {}
35 };
36 } // namespace HiviewDFX
37 } // namespace OHOS
38 
39 namespace {
40 /**
41  * @tc.name: DfxSignalTest001
42  * @tc.desc: test DfxSignal functions
43  * @tc.type: FUNC
44  */
45 HWTEST_F(DfxSignalTest, DfxSignalTest001, TestSize.Level2)
46 {
47     GTEST_LOG_(INFO) << "DfxSignalTest001: start.";
48     siginfo_t si = {
49         .si_signo = SIGSEGV,
50         .si_errno = 0,
51         .si_code = -1,
52         .si_value.sival_int = static_cast<int>(gettid())
53     };
54     std::map<int, std::string> sigKey = {
55         { SIGILL, string("SIGILL") },
56         { SIGTRAP, string("SIGTRAP") },
57         { SIGABRT, string("SIGABRT") },
58         { SIGBUS, string("SIGBUS") },
59         { SIGFPE, string("SIGFPE") },
60         { SIGSEGV, string("SIGSEGV") },
61         { SIGSTKFLT, string("SIGSTKFLT") },
62         { SIGSYS, string("SIGSYS") },
63     };
64     for (auto sigKey : sigKey) {
65         std::string sigKeyword = "Signal:";
66         si.si_signo = sigKey.first;
67         sigKeyword += sigKey.second;
68         std::string sigStr = DfxSignal::PrintSignal(si);
69         GTEST_LOG_(INFO) << sigStr;
70         ASSERT_TRUE(sigStr.find(sigKeyword) != std::string::npos);
71     }
72     GTEST_LOG_(INFO) << "DfxSignalTest001: end.";
73 }
74 
75 /**
76  * @tc.name: DfxSignalTest002
77  * @tc.desc: test if signal info is available
78  * @tc.type: FUNC
79  */
80 HWTEST_F(DfxSignalTest, DfxSignalTest002, TestSize.Level2)
81 {
82     GTEST_LOG_(INFO) << "DfxSignalTest002: start.";
83     int32_t input = 1;
84     std::shared_ptr<DfxSignal> signal = std::make_shared<DfxSignal>(input);
85     bool ret = signal->IsAvailable();
86     EXPECT_EQ(true, ret != true) << "DfxSignalTest002 Failed";
87     GTEST_LOG_(INFO) << "DfxSignalTest002: end.";
88 }
89 
90 /**
91  * @tc.name: DfxSignalTest003
92  * @tc.desc: test if addr is available
93  * @tc.type: FUNC
94  */
95 HWTEST_F(DfxSignalTest, DfxSignalTest003, TestSize.Level2)
96 {
97     GTEST_LOG_(INFO) << "DfxSignalTest003: start.";
98     int32_t input = -100;
99     std::shared_ptr<DfxSignal> signal = std::make_shared<DfxSignal>(input);
100     bool ret = signal->IsAddrAvailable();
101     EXPECT_EQ(true, ret != true) << "DfxSignalTest003 Failed";
102     GTEST_LOG_(INFO) << "DfxSignalTest003: end.";
103 }
104 
105 /**
106  * @tc.name: DfxSignalTest004
107  * @tc.desc: test if pid is available
108  * @tc.type: FUNC
109  */
110 HWTEST_F(DfxSignalTest, DfxSignalTest004, TestSize.Level2)
111 {
112     int32_t input = 100;
113     GTEST_LOG_(INFO) << "DfxSignalTest004: start.";
114     std::shared_ptr<DfxSignal> signal = std::make_shared<DfxSignal>(input);
115     bool ret = signal->IsPidAvailable();
116     EXPECT_EQ(true, ret != true) << "DfxSignalTest004 Failed";
117     GTEST_LOG_(INFO) << "DfxSignalTest004: end.";
118 }
119 
120 /**
121  * @tc.name: DfxSignalTest005
122  * @tc.desc: test if GetSignal
123  * @tc.type: FUNC
124  */
125 HWTEST_F(DfxSignalTest, DfxSignalTest005, TestSize.Level2)
126 {
127     GTEST_LOG_(INFO) << "DfxSignalTest005: start.";
128     int32_t input = 1;
129     std::shared_ptr<DfxSignal> signal = std::make_shared<DfxSignal>(input);
130     int32_t output = signal->GetSignal();
131     EXPECT_EQ(true, output == input) << "DfxSignalTest005 Failed";
132     GTEST_LOG_(INFO) << "DfxSignalTest005: end.";
133 }
134 
135 std::map<int32_t, std::string> sigKeys = {
136     { SIGILL, std::string("SIGILL") },
137     { SIGTRAP, std::string("SIGTRAP") },
138     { SIGBUS, std::string("SIGBUS") },
139     { SIGFPE, std::string("SIGFPE") },
140     { SIGSEGV, std::string("SIGSEGV") },
141 };
142 std::map<int, std::string> segvCode = {
143     { SEGV_MAPERR, string("SEGV_MAPERR") },
144     { SEGV_ACCERR, string("SEGV_ACCERR") },
145     { SI_USER, string("SI_USER") },
146 };
147 std::map<int, std::string> trapCode = {
148     { TRAP_BRKPT, string("TRAP_BRKPT") },
149     { TRAP_TRACE, string("TRAP_TRACE") },
150     { TRAP_BRANCH, string("TRAP_BRANCH") },
151     { TRAP_HWBKPT, string("TRAP_HWBKPT") },
152     { SI_USER, string("SI_USER") },
153 };
154 std::map<int, std::string> illCode = {
155     { ILL_ILLOPC, string("ILL_ILLOPC") },
156     { ILL_ILLOPN, string("ILL_ILLOPN") },
157     { ILL_ILLADR, string("ILL_ILLADR") },
158     { ILL_ILLTRP, string("ILL_ILLTRP") },
159     { ILL_PRVOPC, string("ILL_PRVOPC") },
160     { ILL_PRVREG, string("ILL_PRVREG") },
161     { ILL_COPROC, string("ILL_COPROC") },
162     { ILL_BADSTK, string("ILL_BADSTK") },
163     { SI_USER, string("SI_USER") },
164 };
165 std::map<int, std::string> fpeCode = {
166     { FPE_INTDIV, string("FPE_INTDIV") },
167     { FPE_INTOVF, string("FPE_INTOVF") },
168     { FPE_FLTDIV, string("FPE_FLTDIV") },
169     { FPE_FLTOVF, string("FPE_FLTOVF") },
170     { FPE_FLTUND, string("FPE_FLTUND") },
171     { FPE_FLTRES, string("FPE_FLTRES") },
172     { FPE_FLTINV, string("FPE_FLTINV") },
173     { FPE_FLTSUB, string("FPE_FLTSUB") },
174     { SI_USER, string("SI_USER") },
175 };
176 std::map<int, std::string> busCode = {
177     { BUS_ADRALN, string("BUS_ADRALN") },
178     { BUS_ADRERR, string("BUS_ADRERR") },
179     { BUS_OBJERR, string("BUS_OBJERR") },
180     { BUS_MCEERR_AR, string("BUS_MCEERR_AR") },
181     { BUS_MCEERR_AO, string("BUS_MCEERR_AO") },
182     { SI_USER, string("SI_USER") },
183 };
184 /**
185  * @tc.name: DfxSignalTest006
186  * @tc.desc: test DfxSignal functions
187  * @tc.type: FUNC
188  */
189 HWTEST_F(DfxSignalTest, DfxSignalTest006, TestSize.Level2)
190 {
191     GTEST_LOG_(INFO) << "DfxSignalTest006: start.";
192     siginfo_t si = {
193         .si_signo = SIGSEGV,
194         .si_errno = 0,
195         .si_code = -1,
196         .si_value.sival_int = static_cast<int>(gettid())
197     };
198     for (auto sigKey : sigKeys) {
199         si.si_signo = sigKey.first;
200         std::map<int, std::string> codeMap;
201         switch (si.si_signo) {
202             case SIGILL:
203                 codeMap = illCode;
204                 break;
205             case SIGTRAP:
206                 codeMap = trapCode;
207                 break;
208             case SIGBUS:
209                 codeMap = busCode;
210                 break;
211             case SIGFPE:
212                 codeMap = fpeCode;
213                 break;
214             case SIGSEGV:
215                 codeMap = segvCode;
216                 break;
217             default:
218                 break;
219         }
220         for (auto& code : codeMap) {
221             std::string sigKeyword = "Signal:";
222             sigKeyword += sigKey.second;
223             si.si_code = code.first;
224             sigKeyword = sigKeyword + "(" + code.second + ")";
225             std::string sigStr = DfxSignal::PrintSignal(si);
226             GTEST_LOG_(INFO) << sigStr;
227             ASSERT_TRUE(sigStr.find(sigKeyword) != std::string::npos);
228         }
229     }
230     GTEST_LOG_(INFO) << "DfxSignalTest006: end.";
231 }
232 }
233