• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 OpenFilesTest : 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 OpenFilesTest::result = "";
47 
SetUpTestCase(void)48 void OpenFilesTest::SetUpTestCase(void)
49 {
50     result = "";
51 }
52 
SetUp(void)53 void OpenFilesTest::SetUp(void)
54 {
55     DfxBufferWriter::GetInstance().SetWriteFunc(OpenFilesTest::WriteLogFunc);
56 }
57 
WriteLogFunc(int32_t fd,const char * buf,size_t len)58 int OpenFilesTest::WriteLogFunc(int32_t fd, const char *buf, size_t len)
59 {
60     OpenFilesTest::result.append(std::string(buf, len));
61     return 0;
62 }
63 
64 namespace {
65 /**
66  * @tc.name: OpenFilesTest001
67  * @tc.desc: test print open files
68  * @tc.type: FUNC
69  */
70 HWTEST_F(OpenFilesTest, OpenFilesTest001, TestSize.Level2)
71 {
72     GTEST_LOG_(INFO) << "MapsTest001: start.";
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     ProcessDumpRequest request = {
83         .type = ProcessDumpType::DUMP_TYPE_CPP_CRASH,
84         .tid = tid,
85         .pid = pid,
86         .nsPid = pid,
87         .fdTableAddr = (uint64_t)fdsan_get_fd_table(),
88     };
89     DfxProcess process;
90     process.InitProcessInfo(pid, nsPid, getuid(), "");
91     Unwinder unwinder(pid, nsPid, request.type == ProcessDumpType::DUMP_TYPE_CPP_CRASH);
92     OpenFiles openFiles;
93     openFiles.Print(process, request, unwinder);
94     std::vector<std::string> keyWords = {
95         "OpenFiles:",
96         "0->",
97         "1->",
98         "2->",
99     };
100     for (const std::string& keyWord : keyWords) {
101         ASSERT_TRUE(CheckContent(result, keyWord, true));
102     }
103     process.Detach();
104     GTEST_LOG_(INFO) << "OpenFilesTest001: end.";
105 }
106 }
107