• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "dfx_test_util.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 CommonTest : public testing::Test {
36 public:
SetUpTestCase(void)37     static void SetUpTestCase(void) {}
TearDownTestCase(void)38     static void TearDownTestCase(void) {}
SetUp()39     void SetUp() {}
TearDown()40     void TearDown() {}
41 };
42 
getLibPathsBySystemBits(vector<std::string> & filePaths)43 static void getLibPathsBySystemBits(vector<std::string> &filePaths)
44 {
45     std::vector<std::string> lib64FilePaths = {
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-sdk/libcrash_exception.z.so",
50         "/system/lib64/chipset-sdk-sp/libdfx_signalhandler.z.so",
51         "/system/lib64/chipset-sdk-sp/librustc_demangle.z.so",
52         "/system/lib64/chipset-sdk-sp/libasync_stack.z.so",
53         "/system/lib64/chipset-sdk-sp/libdfx_dumpcatcher.z.so",
54         "/system/lib64/chipset-sdk-sp/libfaultloggerd.z.so",
55         "/system/lib64/chipset-sdk-sp/libbacktrace_local.so",
56         "/system/lib64/chipset-sdk-sp/libunwinder.z.so",
57         "/system/lib64/chipset-sdk-sp/libdfx_procinfo.z.so",
58         "/system/lib64/module/libfaultlogger_napi.z.so"
59     };
60     std::vector<std::string> libFilePaths = {
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-sdk/libcrash_exception.z.so",
65         "/system/lib/chipset-sdk-sp/libdfx_signalhandler.z.so",
66         "/system/lib/chipset-sdk-sp/librustc_demangle.z.so",
67         "/system/lib/chipset-sdk-sp/libasync_stack.z.so",
68         "/system/lib/chipset-sdk-sp/libdfx_dumpcatcher.z.so",
69         "/system/lib/chipset-sdk-sp/libfaultloggerd.z.so",
70         "/system/lib/chipset-sdk-sp/libbacktrace_local.so",
71         "/system/lib/chipset-sdk-sp/libunwinder.z.so",
72         "/system/lib/chipset-sdk-sp/libdfx_procinfo.z.so",
73         "/system/lib/module/libfaultlogger_napi.z.so"
74     };
75 #ifdef __LP64__
76     filePaths.insert(filePaths.end(), lib64FilePaths.begin(), lib64FilePaths.end());
77 #else
78     filePaths.insert(filePaths.end(), libFilePaths.begin(), libFilePaths.end());
79 #endif
80 }
81 
82 namespace {
83 #ifdef LOG_DOMAIN
84 #undef LOG_DOMAIN
85 #define LOG_DOMAIN 0xD002D11
86 #endif
87 
88 #ifdef LOG_TAG
89 #undef LOG_TAG
90 #define LOG_TAG "DfxCommonTest"
91 #endif
92 
93 /**
94  * @tc.name: DfxUtilTest001
95  * @tc.desc: test DfxUtil GetCurrentTimeStr
96  * @tc.type: FUNC
97  */
98 HWTEST_F(CommonTest, DfxUtilTest001, TestSize.Level2)
99 {
100     GTEST_LOG_(INFO) << "DfxUtilTest001: start.";
101     time_t now = time(nullptr);
102     std::string timeStr = GetCurrentTimeStr(static_cast<uint64_t>(now));
103     GTEST_LOG_(INFO) << timeStr;
104     ASSERT_NE(timeStr, "invalid timestamp\n");
105     now += 100000; // 100000 : test time offset
106     timeStr = GetCurrentTimeStr(static_cast<uint64_t>(now));
107     GTEST_LOG_(INFO) << timeStr;
108     ASSERT_NE(timeStr, "invalid timestamp\n");
109     GTEST_LOG_(INFO) << "DfxUtilTest001: end.";
110 }
111 
112 /**
113  * @tc.name: DfxUtilTest002
114  * @tc.desc: test DfxUtil TrimAndDupStr
115  * @tc.type: FUNC
116  */
117 HWTEST_F(CommonTest, DfxUtilTest002, TestSize.Level2)
118 {
119     GTEST_LOG_(INFO) << "DfxUtilTest002: start.";
120     std::string testStr = " abcd  ";
121     std::string resStr;
122     bool ret = TrimAndDupStr(testStr, resStr);
123     ASSERT_EQ(ret, true);
124     ASSERT_EQ(resStr, "abcd");
125     GTEST_LOG_(INFO) << "DfxUtilTest002: end.";
126 }
127 
128 #if is_ohos && !is_mingw
129 /**
130  * @tc.name: DfxUtilTest003
131  * @tc.desc: test DfxUtil ReadProcMemByPid
132  * @tc.type: FUNC
133  */
134 HWTEST_F(CommonTest, DfxUtilTest003, TestSize.Level2)
135 {
136     GTEST_LOG_(INFO) << "DfxUtilTest003: start.";
137     size_t size = 4096 * 2048;
138     char *p = static_cast<char *>(malloc(size));
139     ASSERT_EQ(memset_s(p, size, '0', size), 0);
140     *(p + size - 2) = 'O';
141     *(p + size - 1) = 'K';
142     std::vector<char> data(size);
143     ASSERT_EQ(ReadProcMemByPid(getpid(), reinterpret_cast<uintptr_t>(p), data.data(), size), size);
144     ASSERT_EQ(data[size - 2], 'O') << data[size - 2];
145     ASSERT_EQ(data[size - 1], 'K') << data[size - 1];
146     free(p);
147     GTEST_LOG_(INFO) << "DfxUtilTest003: end.";
148 }
149 #endif
150 
151 /**
152  * @tc.name: DfxUtilTest004
153  * @tc.desc: test DfxUtil CheckAndExit
154  * @tc.type: FUNC
155  */
156 HWTEST_F(CommonTest, DfxUtilTest004, TestSize.Level2)
157 {
158     GTEST_LOG_(INFO) << "DfxUtilTest004: start.";
159     int pid = fork();
160     if (pid == 0) {
161         EXPECT_EQ(pid, 0);
162         CheckAndExit(HasFailure());
163     } else if (pid < 0) {
164         GTEST_LOG_(INFO) << "DfxUtilTest004 fail. ";
165     } else {
166         int status;
167         wait(&status);
168         ASSERT_EQ(status, 0);
169     }
170     GTEST_LOG_(INFO) << "DfxUtilTest004: end.";
171 }
172 
173 /**
174  * @tc.name: DfxUtilTest005
175  * @tc.desc: test DfxUtil CheckAndExit
176  * @tc.type: FUNC
177  */
178 HWTEST_F(CommonTest, DfxUtilTest005, TestSize.Level2)
179 {
180     GTEST_LOG_(INFO) << "DfxUtilTest005: start.";
181     int pid = fork();
182     if (pid == 0) {
183         EXPECT_NE(pid, 0);
184         CheckAndExit(HasFailure());
185     } else if (pid < 0) {
186         GTEST_LOG_(INFO) << "DfxUtilTest005 fail. ";
187     } else {
188         int status;
189         wait(&status);
190         ASSERT_NE(status, 0);
191     }
192     GTEST_LOG_(INFO) << "DfxUtilTest005: end.";
193 }
194 
195 /**
196  * @tc.name: DfxDumpResTest001
197  * @tc.desc: test DfxDumpRes functions
198  * @tc.type: FUNC
199  */
200 HWTEST_F(CommonTest, DfxDumpResTest001, TestSize.Level2)
201 {
202     GTEST_LOG_(INFO) << "DfxDumpResTest001: start.";
203     int32_t res = DUMP_ESUCCESS;
204     std::string result = DfxDumpRes::ToString(res);
205     ASSERT_EQ(result, "0 ( no error )");
206     GTEST_LOG_(INFO) << "DfxDumpResTest001: end.";
207 }
208 
209 /**
210  * @tc.name: StringUtilTest001
211  * @tc.desc: test StartsWith functions
212  * @tc.type: FUNC
213  */
214 HWTEST_F(CommonTest, StringUtilTest001, TestSize.Level2)
215 {
216     GTEST_LOG_(INFO) << "StringUtilTest001: start.";
217     std::string start = "[anon:ArkTS Code2024]";
218     bool ret = StartsWith(start, "[anon:ArkTS Code");
219     EXPECT_TRUE(ret);
220     std::string end = "test.hap";
221     ret = EndsWith(end, ".hap");
222     EXPECT_TRUE(ret);
223     GTEST_LOG_(INFO) << "StringUtilTest001: end.";
224 }
225 
226 /**
227  * @tc.name: ROMBaselineTest001
228  * @tc.desc: test the ROM baseline of the faultloggerd component
229  * @tc.type: FUNC
230  */
231 HWTEST_F(CommonTest, ROMBaselineTest001, TestSize.Level2)
232 {
233     GTEST_LOG_(INFO) << "ROMBaselineTest001: start.";
234     std::vector<std::string> filePaths = {
235         "/system/etc/faultlogger.conf",
236         "/system/etc/param/faultloggerd.para",
237         "/system/etc/param/faultloggerd.para.dac",
238         "/system/etc/init/faultloggerd.cfg",
239         "/system/etc/hiview/extract_rule.json",
240         "/system/etc/hiview/compose_rule.json",
241         "/system/bin/processdump",
242         "/system/bin/faultloggerd",
243         "/system/bin/dumpcatcher"
244     };
245     getLibPathsBySystemBits(filePaths);
246     struct stat fileStat;
247     long long totalSize = 0;
248     constexpr long long sectorSize = 4;
249     for (const auto &filePath : filePaths) {
250         int ret = stat(filePath.c_str(), &fileStat);
251         if (ret != 0) {
252             EXPECT_EQ(ret, 0) << "Failed to get file size of " << filePath;
253         } else {
254             long long size = fileStat.st_size / 1024;
255             size = size + sectorSize - size % sectorSize;
256             GTEST_LOG_(INFO) << "File size of " << filePath << " is " << size << "KB";
257             totalSize += size;
258         }
259     }
260     printf("Total file size is %lldKB\n", totalSize);
261     constexpr long long baseline = 1335;
262     // There is a 5% threshold for the baseline value
263     EXPECT_LT(totalSize, static_cast<long long>(baseline * 1.05));
264     GTEST_LOG_(INFO) << "ROMBaselineTest001: end.";
265 }
266 }
267 } // namespace HiviewDFX
268 } // namespace OHOS
269