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 "async_stack.h"
22 #include "dfx_buffer_writer.h"
23 #include "dfx_cutil.h"
24 #include "dfx_define.h"
25 #include "dfx_test_util.h"
26 #include "dfx_util.h"
27 #include "decorative_dump_info.h"
28
29 using namespace OHOS::HiviewDFX;
30 using namespace testing::ext;
31 using namespace std;
32
33 namespace OHOS {
34 namespace HiviewDFX {
35 class SubmitterStackTest : public testing::Test {
36 public:
37 static void SetUpTestCase(void);
TearDownTestCase(void)38 static void TearDownTestCase(void) {}
39 void SetUp();
TearDown()40 void TearDown() {}
41 static int WriteLogFunc(int32_t fd, const char *buf, size_t len);
42 static std::string result;
43 };
44 } // namespace HiviewDFX
45 } // namespace OHOS
46
47 std::string SubmitterStackTest::result = "";
48
SetUpTestCase(void)49 void SubmitterStackTest::SetUpTestCase(void)
50 {
51 result = "";
52 }
53
SetUp(void)54 void SubmitterStackTest::SetUp(void)
55 {
56 DfxBufferWriter::GetInstance().SetWriteFunc(SubmitterStackTest::WriteLogFunc);
57 }
58
WriteLogFunc(int32_t fd,const char * buf,size_t len)59 int SubmitterStackTest::WriteLogFunc(int32_t fd, const char *buf, size_t len)
60 {
61 SubmitterStackTest::result.append(std::string(buf, len));
62 return 0;
63 }
64
65 namespace {
66 /**
67 * @tc.name: SubmitterStackTest001
68 * @tc.desc: test print submitter stack
69 * @tc.type: FUNC
70 */
71 HWTEST_F(SubmitterStackTest, SubmitterStackTest001, TestSize.Level2)
72 {
73 GTEST_LOG_(INFO) << "SubmitterStackTest001: start.";
74 uint64_t stackId = CollectAsyncStack();
75 SetStackId(stackId);
76 pid_t pid = fork();
77 if (pid < 0) {
78 GTEST_LOG_(ERROR) << "Failed to fork new test process.";
79 } else if (pid == 0) {
80 sleep(3); // 3 : sleep 3 seconds
81 exit(0);
82 }
83 pid_t tid = pid;
84 pid_t nsPid = pid;
85 ProcessDumpRequest request = {
86 .type = ProcessDumpType::DUMP_TYPE_CPP_CRASH,
87 .tid = tid,
88 .pid = pid,
89 .nsPid = pid,
90 .stackId = GetStackId(),
91 };
92 DfxProcess process;
93 process.InitProcessInfo(pid, nsPid, getuid(), "");
94 process.SetVmPid(pid);
95 process.InitKeyThread(request);
96 Unwinder unwinder(pid, nsPid, request.type == ProcessDumpType::DUMP_TYPE_CPP_CRASH);
97 SubmitterStack submitterStack;
98 submitterStack.Print(process, request, unwinder);
99 std::vector<std::string> keyWords = {
100 "SubmitterStacktrace",
101 "#00",
102 "#01",
103 "#02",
104 };
105 for (const std::string& keyWord : keyWords) {
106 ASSERT_TRUE(CheckContent(result, keyWord, true));
107 }
108 process.Detach();
109 GTEST_LOG_(INFO) << "SubmitterStackTest001: end.";
110 }
111 }
112