1 /*
2  * Copyright (c) 2023 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 <sys/stat.h>
18 #include <ctime>
19 #include <securec.h>
20 #include <string>
21 #include <vector>
22 #include "dfx_define.h"
23 #include "dfx_util.h"
24 #include "dfx_dump_res.h"
25 #include "dfx_log.h"
26 #include "string_util.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 CommonTest : public testing::Test {
35 public:
SetUpTestCase(void)36     static void SetUpTestCase(void) {}
TearDownTestCase(void)37     static void TearDownTestCase(void) {}
SetUp()38     void SetUp() {}
TearDown()39     void TearDown() {}
40 };
41 
getLibPathsBySystemBits(vector<std::string> &filePaths)42 static void getLibPathsBySystemBits(vector<std::string> &filePaths)
43 {
44     std::vector<std::string> lib64FilePaths = {
45         "/system/lib64/librustc_demangle.z.so",
46         "/system/lib64/libstacktrace_rust.dylib.so",
47         "/system/lib64/libpanic_handler.dylib.so",
48         "/system/lib64/platformsdk/libjson_stack_formatter.z.so",
49         "/system/lib64/chipset-pub-sdk/libdfx_dumpcatcher.z.so",
50         "/system/lib64/chipset-pub-sdk/libfaultloggerd.z.so",
51         "/system/lib64/chipset-pub-sdk/libdfx_signalhandler.z.so",
52         "/system/lib64/chipset-pub-sdk/libasync_stack.z.so",
53         "/system/lib64/chipset-pub-sdk/libbacktrace_local.so",
54         "/system/lib64/chipset-pub-sdk/libunwinder.z.so",
55         "/system/lib64/chipset-pub-sdk/libdfx_procinfo.z.so",
56         "/system/lib64/chipset-pub-sdk/libcrash_exception.z.so",
57         "/system/lib64/module/libfaultlogger_napi.z.so"
58     };
59     std::vector<std::string> libFilePaths = {
60         "/system/lib/librustc_demangle.z.so",
61         "/system/lib/libstacktrace_rust.dylib.so",
62         "/system/lib/libpanic_handler.dylib.so",
63         "/system/lib/platformsdk/libjson_stack_formatter.z.so",
64         "/system/lib/chipset-pub-sdk/libdfx_dumpcatcher.z.so",
65         "/system/lib/chipset-pub-sdk/libfaultloggerd.z.so",
66         "/system/lib/chipset-pub-sdk/libdfx_signalhandler.z.so",
67         "/system/lib/chipset-pub-sdk/libasync_stack.z.so",
68         "/system/lib/chipset-pub-sdk/libbacktrace_local.so",
69         "/system/lib/chipset-pub-sdk/libunwinder.z.so",
70         "/system/lib/chipset-pub-sdk/libdfx_procinfo.z.so",
71         "/system/lib/chipset-pub-sdk/libcrash_exception.z.so",
72         "/system/lib/module/libfaultlogger_napi.z.so"
73     };
74 #ifdef __LP64__
75     filePaths.insert(filePaths.end(), lib64FilePaths.begin(), lib64FilePaths.end());
76 #else
77     filePaths.insert(filePaths.end(), libFilePaths.begin(), libFilePaths.end());
78 #endif
79 }
80 
81 namespace {
82 #ifdef LOG_DOMAIN
83 #undef LOG_DOMAIN
84 #define LOG_DOMAIN 0xD002D11
85 #endif
86 
87 #ifdef LOG_TAG
88 #undef LOG_TAG
89 #define LOG_TAG "DfxCommonTest"
90 #endif
91 
92 /**
93  * @tc.name: DfxUtilTest001
94  * @tc.desc: test DfxUtil GetCurrentTimeStr
95  * @tc.type: FUNC
96  */
HWTEST_F(CommonTest, DfxUtilTest001, TestSize.Level2)97 HWTEST_F(CommonTest, DfxUtilTest001, TestSize.Level2)
98 {
99     GTEST_LOG_(INFO) << "DfxUtilTest001: start.";
100     time_t now = time(nullptr);
101     std::string timeStr = GetCurrentTimeStr(static_cast<uint64_t>(now));
102     GTEST_LOG_(INFO) << timeStr;
103     ASSERT_NE(timeStr, "invalid timestamp\n");
104     now += 100000; // 100000 : test time offset
105     timeStr = GetCurrentTimeStr(static_cast<uint64_t>(now));
106     GTEST_LOG_(INFO) << timeStr;
107     ASSERT_NE(timeStr, "invalid timestamp\n");
108     GTEST_LOG_(INFO) << "DfxUtilTest001: end.";
109 }
110 
111 /**
112  * @tc.name: DfxUtilTest002
113  * @tc.desc: test DfxUtil TrimAndDupStr
114  * @tc.type: FUNC
115  */
HWTEST_F(CommonTest, DfxUtilTest002, TestSize.Level2)116 HWTEST_F(CommonTest, DfxUtilTest002, TestSize.Level2)
117 {
118     GTEST_LOG_(INFO) << "DfxUtilTest002: start.";
119     std::string testStr = " abcd  ";
120     std::string resStr;
121     bool ret = TrimAndDupStr(testStr, resStr);
122     ASSERT_EQ(ret, true);
123     ASSERT_EQ(resStr, "abcd");
124     GTEST_LOG_(INFO) << "DfxUtilTest002: end.";
125 }
126 
127 /**
128  * @tc.name: DfxDumpResTest001
129  * @tc.desc: test DfxDumpRes functions
130  * @tc.type: FUNC
131  */
HWTEST_F(CommonTest, DfxDumpResTest001, TestSize.Level2)132 HWTEST_F(CommonTest, DfxDumpResTest001, TestSize.Level2)
133 {
134     GTEST_LOG_(INFO) << "DfxDumpResTest001: start.";
135     int32_t res = DUMP_ESUCCESS;
136     std::string result = DfxDumpRes::ToString(res);
137     ASSERT_EQ(result, "0 ( no error )");
138     GTEST_LOG_(INFO) << "DfxDumpResTest001: end.";
139 }
140 
141 /**
142  * @tc.name: StringUtilTest001
143  * @tc.desc: test StartsWith functions
144  * @tc.type: FUNC
145  */
HWTEST_F(CommonTest, StringUtilTest001, TestSize.Level2)146 HWTEST_F(CommonTest, StringUtilTest001, TestSize.Level2)
147 {
148     GTEST_LOG_(INFO) << "StringUtilTest001: start.";
149     std::string start = "[anon:ArkTS Code2024]";
150     bool ret = StartsWith(start, "[anon:ArkTS Code");
151     EXPECT_TRUE(ret);
152     std::string end = "test.hap";
153     ret = EndsWith(end, ".hap");
154     EXPECT_TRUE(ret);
155     GTEST_LOG_(INFO) << "StringUtilTest001: end.";
156 }
157 
158 /**
159  * @tc.name: ROMBaselineTest001
160  * @tc.desc: test the ROM baseline of the faultloggerd component
161  * @tc.type: FUNC
162  */
HWTEST_F(CommonTest, ROMBaselineTest001, TestSize.Level2)163 HWTEST_F(CommonTest, ROMBaselineTest001, TestSize.Level2)
164 {
165     GTEST_LOG_(INFO) << "ROMBaselineTest001: start.";
166     std::vector<std::string> filePaths = {
167         "/system/etc/faultlogger.conf",
168         "/system/etc/param/faultloggerd.para",
169         "/system/etc/param/faultloggerd.para.dac",
170         "/system/etc/init/faultloggerd.cfg",
171         "/system/etc/hiview/extract_rule.json",
172         "/system/etc/hiview/compose_rule.json",
173         "/system/bin/processdump",
174         "/system/bin/faultloggerd",
175         "/system/bin/dumpcatcher",
176         "/system/bin/crashvalidator"
177     };
178     getLibPathsBySystemBits(filePaths);
179     struct stat fileStat;
180     long long totalSize = 0;
181     constexpr long long sectorSize = 4;
182     for (const auto &filePath : filePaths) {
183         int ret = stat(filePath.c_str(), &fileStat);
184         if (ret != 0) {
185             printf("Failed to get file size of %s\n", filePath.c_str());
186         } else {
187             long long size = fileStat.st_size / 1024;
188             size = size + sectorSize - size % sectorSize;
189             printf("File size of %s is %lldKB\n", filePath.c_str(), size);
190             totalSize += size;
191         }
192     }
193     printf("Total file size is %lldKB\n", totalSize);
194     constexpr long long baseline = 1054;
195     EXPECT_LT(totalSize, static_cast<long long>(baseline * 1.05));
196     GTEST_LOG_(INFO) << "ROMBaselineTest001: end.";
197 }
198 }
199 } // namespace HiviewDFX
200 } // namespace OHOS