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 <gtest/gtest.h>
16 #include <unistd.h>
17
18 #include "faultlog_formatter.h"
19 #include "common_utils.h"
20 #include <fcntl.h>
21 using namespace testing::ext;
22 namespace OHOS {
23 namespace HiviewDFX {
24
25 /**
26 * @tc.name: WriteStackTraceFromLogTest001
27 * @tc.desc: Test WriteStackTraceFromLog
28 * @tc.type: FUNC
29 */
30 HWTEST(FaultlogFormatterUnittest, WriteStackTraceFromLogTest001, testing::ext::TestSize.Level1)
31 {
32 std::string pidStr;
33 int32_t fd = -1;
34 std::string path = "/testError";
35 bool ret = FaultLogger::WriteStackTraceFromLog(fd, pidStr, path);
36 ASSERT_FALSE(ret);
37 path = "/data/test/test_faultlogger_data/plugin_config_test";
38 ret = FaultLogger::WriteStackTraceFromLog(fd, pidStr, path);
39 ASSERT_TRUE(ret);
40 }
41
GetPipeData(int pipeRead)42 static std::string GetPipeData(int pipeRead)
43 {
44 constexpr int maxPipeBuffSize = 1024 * 1024;
45 std::vector<uint8_t> buf(maxPipeBuffSize, 0);
46 ssize_t nread = TEMP_FAILURE_RETRY(read(pipeRead, buf.data(), buf.size()));
47 if (nread > 0) {
48 return std::string(buf.begin(), buf.begin() + nread);
49 }
50 return {};
51 }
52
53 /**
54 * @tc.name: WriteFaultLogToFileTest001
55 * @tc.desc: Test WriteFaultLogToFile
56 * @tc.type: FUNC
57 */
58 HWTEST(FaultlogFormatterUnittest, WriteFaultLogToFileTest001, testing::ext::TestSize.Level1)
59 {
60 std::map<std::string, std::string> sections = {
61 {"KEYLOGFILE", "hello"},
62 {"PID", "1234"}
63 };
64 int pipe[2] = {-1, -1};
65 if (pipe2(pipe, O_CLOEXEC | O_NONBLOCK)) {
66 FaultLogger::WriteFaultLogToFile(pipe[1], 0, sections);
67 auto result = GetPipeData(pipe[0]);
68 ASSERT_TRUE(result.find("Additional Logs:") != std::string::npos);
69 close(pipe[0]);
70 close(pipe[1]);
71 }
72 }
73
74 /**
75 * @tc.name: WriteFaultLogToFileTest002
76 * @tc.desc: Test WriteFaultLogToFile
77 * @tc.type: FUNC
78 */
79 HWTEST(FaultlogFormatterUnittest, WriteFaultLogToFileTest002, testing::ext::TestSize.Level1)
80 {
81 std::map<std::string, std::string> sections = {
82 {"KEYLOGFILE", "hello"},
83 };
84 int pipe[2] = {-1, -1};
85 if (pipe2(pipe, O_CLOEXEC | O_NONBLOCK)) {
86 FaultLogger::WriteFaultLogToFile(pipe[1], 0, sections);
87 auto result = GetPipeData(pipe[0]);
88 ASSERT_TRUE(result.find("Additional Logs:") == std::string::npos);
89 close(pipe[0]);
90 close(pipe[1]);
91 }
92 }
93
94 /**
95 * @tc.name: WriteFaultLogToFileTest003
96 * @tc.desc: Test WriteFaultLogToFile
97 * @tc.type: FUNC
98 */
99 HWTEST(FaultlogFormatterUnittest, WriteFaultLogToFileTest003, testing::ext::TestSize.Level1)
100 {
101 std::map<std::string, std::string> sections;
102 int pipe[2] = {-1, -1};
103 if (pipe2(pipe, O_CLOEXEC | O_NONBLOCK)) {
104 FaultLogger::WriteFaultLogToFile(pipe[1], 0, sections);
105 auto result = GetPipeData(pipe[0]);
106 ASSERT_TRUE(result.find("Additional Logs:") == std::string::npos);
107 close(pipe[0]);
108 close(pipe[1]);
109 }
110 }
111 } // namespace HiviewDFX
112 } // namespace OHOS
113