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 class FaultlogFormatterUnittest : public testing::Test {};
25
26 /**
27 * @tc.name: GetSummaryByTypeTest001
28 * @tc.desc: Test GetSummaryByType
29 * @tc.type: FUNC
30 */
HWTEST_F(FaultlogFormatterUnittest, GetSummaryByTypeTest001, testing::ext::TestSize.Level1)31 HWTEST_F(FaultlogFormatterUnittest, GetSummaryByTypeTest001, testing::ext::TestSize.Level1)
32 {
33 std::map<std::string, std::string> sections = {
34 {"TRUSTSTACK", "Selected stacktrace:"},
35 {"KEY_THREAD_INFO", "Fault thread info:"}
36 };
37 int32_t logType = FaultLogType::APP_FREEZE;
38 auto summary = FaultLogger::GetSummaryByType(logType, sections);
39 ASSERT_EQ(summary, "Selected stacktrace:");
40 logType = FaultLogType::CPP_CRASH;
41 summary = FaultLogger::GetSummaryByType(logType, sections);
42 ASSERT_EQ(summary, "Fault thread info:");
43 logType = FaultLogType::ADDR_SANITIZER;
44 summary = FaultLogger::GetSummaryByType(logType, sections);
45 ASSERT_EQ(summary, "Could not figure out summary for this fault.");
46 }
47
48 /**
49 * @tc.name: WriteStackTraceFromLogTest001
50 * @tc.desc: Test WriteStackTraceFromLog
51 * @tc.type: FUNC
52 */
HWTEST_F(FaultlogFormatterUnittest, WriteStackTraceFromLogTest001, testing::ext::TestSize.Level1)53 HWTEST_F(FaultlogFormatterUnittest, WriteStackTraceFromLogTest001, testing::ext::TestSize.Level1)
54 {
55 std::string pidStr;
56 int32_t fd = -1;
57 std::string path = "/testError";
58 FaultLogger::WriteStackTraceFromLog(fd, pidStr, path);
59 ASSERT_EQ(fd, -1);
60 path = "/data/test/test_faultlogger_data/plugin_config_test";
61 FaultLogger::WriteStackTraceFromLog(fd, pidStr, path);
62 ASSERT_EQ(fd, -1);
63 }
64 } // namespace HiviewDFX
65 } // namespace OHOS
66