• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 <string>
18 #include <unistd.h>
19 #include <vector>
20 
21 #include "dfx_buffer_writer.h"
22 #include "dfx_cutil.h"
23 #include "dfx_define.h"
24 #include "dfx_test_util.h"
25 #include "dfx_util.h"
26 #include "decorative_dump_info.h"
27 
28 using namespace OHOS::HiviewDFX;
29 using namespace testing::ext;
30 using namespace std;
31 
32 namespace OHOS {
33 namespace HiviewDFX {
34 class DumpInfoHeaderTest : public testing::Test {
35 public:
36     static void SetUpTestCase(void);
TearDownTestCase(void)37     static void TearDownTestCase(void) {}
38     void SetUp();
TearDown()39     void TearDown() {}
40     static int WriteLogFunc(int32_t fd, const char *buf, size_t len);
41     static std::string result;
42 };
43 } // namespace HiviewDFX
44 } // namespace OHOS
45 
46 std::string DumpInfoHeaderTest::result = "";
47 
SetUpTestCase(void)48 void DumpInfoHeaderTest::SetUpTestCase(void)
49 {
50     result = "";
51 }
52 
SetUp(void)53 void DumpInfoHeaderTest::SetUp(void)
54 {
55     DfxBufferWriter::GetInstance().SetWriteFunc(DumpInfoHeaderTest::WriteLogFunc);
56 }
57 
WriteLogFunc(int32_t fd,const char * buf,size_t len)58 int DumpInfoHeaderTest::WriteLogFunc(int32_t fd, const char *buf, size_t len)
59 {
60     DumpInfoHeaderTest::result.append(std::string(buf, len));
61     return 0;
62 }
63 namespace {
64 /**
65  * @tc.name: DumpInfoHeaderTest001
66  * @tc.desc: test print cpp crash dump info header
67  * @tc.type: FUNC
68  */
69 HWTEST_F(DumpInfoHeaderTest, DumpInfoHeaderTest001, TestSize.Level2)
70 {
71     GTEST_LOG_(INFO) << "DumpInfoHeaderTest001: start.";
72     std::string msg = "test string type crashObject.";
73     pid_t pid = fork();
74     if (pid < 0) {
75         GTEST_LOG_(ERROR) << "Failed to fork new test process.";
76     } else if (pid == 0) {
77         sleep(3); // 3 : sleep 3 seconds
78         exit(0);
79     }
80     pid_t tid = pid;
81     pid_t nsPid = pid;
82     uint64_t logFileCutoffSize = 1024;
83     ProcessDumpRequest request = {
84         .type = ProcessDumpType::DUMP_TYPE_CPP_CRASH,
85         .tid = tid, .pid = pid, .nsPid = pid,
86         .timeStamp = GetTimeMilliSeconds(),
87         .crashLogConfig = (logFileCutoffSize << 32) + 0x3, // 32 : high 32 bit save cutoff size
88     };
89 #ifdef __aarch_64__
90     const int moveBit = 56;
91     uint8_t type = 1;
92     request.crashObj = (static_cast<uintptr_t>(type) << moveBit) |
93         (reinterpret_cast<uintptr_t>(msg.c_str()) & 0x00ffffffffffffff);
94 #endif
95     siginfo_t siginfo = {
96         .si_signo = SIGABRT,
97         .si_code = SI_TKILL,
98     };
99     request.siginfo = siginfo;
100     GetThreadNameByTid(request.tid, request.threadName, sizeof(request.threadName));
101     GetProcessName(request.processName, sizeof(request.processName));
102     DfxProcess process;
103     process.InitProcessInfo(pid, nsPid, getuid(), request.processName);
104     Unwinder unwinder(pid, nsPid, request.type == ProcessDumpType::DUMP_TYPE_CPP_CRASH);
105     DumpInfoHeader dumpInfoHeader;
106     dumpInfoHeader.Print(process, request, unwinder);
107     const std::vector<std::string> keyWords = {
108         "Build info:", "Enabled app log configs:", "Extend pc lr printing:true",
109         "Log cut off size:1024B", "Simplify maps printing:true", "Pid:", "Uid:",
110         "Process name", "Timestamp:", "Process life time:", "Process Memory(kB):", "Reason:Signal:SIGABRT(SI_TKILL)",
111 #ifdef __aarch_64__
112         "LastFatalMessage:" + msg,
113 #endif
114     };
115     for (const std::string& keyWord : keyWords) {
116         ASSERT_TRUE(CheckContent(result, keyWord, true));
117     }
118     GTEST_LOG_(INFO) << "DumpInfoHeaderTest001: end.";
119 }
120 
121 /**
122  * @tc.name: DumpInfoHeaderTest002
123  * @tc.desc: test print dump catch dump info header
124  * @tc.type: FUNC
125  */
126 HWTEST_F(DumpInfoHeaderTest, DumpInfoHeaderTest002, TestSize.Level2)
127 {
128     GTEST_LOG_(INFO) << "DumpInfoHeaderTest002: start.";
129     std::string msg = "test string type crashObject.";
130     pid_t pid = fork();
131     if (pid < 0) {
132         GTEST_LOG_(ERROR) << "Failed to fork new test process.";
133     } else if (pid == 0) {
134         sleep(3); // 3 : sleep 3 seconds
135         exit(0);
136     }
137     pid_t tid = pid;
138     pid_t nsPid = pid;
139     ProcessDumpRequest request = {
140         .type = ProcessDumpType::DUMP_TYPE_DUMP_CATCH,
141         .tid = tid,
142         .pid = pid,
143         .nsPid = pid,
144         .timeStamp = GetTimeMilliSeconds(),
145     };
146     GetProcessName(request.processName, sizeof(request.processName));
147     DfxProcess process;
148     process.InitProcessInfo(pid, nsPid, getuid(), request.processName);
149     Unwinder unwinder(pid, nsPid, request.type == ProcessDumpType::DUMP_TYPE_CPP_CRASH);
150     DumpInfoHeader dumpInfoHeader;
151     dumpInfoHeader.Print(process, request, unwinder);
152     const std::vector<std::string> keyWords = {
153         "Timestamp:",
154         "Pid:",
155         "Uid:",
156         "Process name",
157     };
158     for (const std::string& keyWord : keyWords) {
159         ASSERT_TRUE(CheckContent(result, keyWord, true));
160     }
161     GTEST_LOG_(INFO) << "DumpInfoHeaderTest002: end.";
162 }
163 }
164