1800b99b8Sopenharmony_ci/* 2800b99b8Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 3800b99b8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4800b99b8Sopenharmony_ci * you may not use this file except in compliance with the License. 5800b99b8Sopenharmony_ci * You may obtain a copy of the License at 6800b99b8Sopenharmony_ci * 7800b99b8Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8800b99b8Sopenharmony_ci * 9800b99b8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10800b99b8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11800b99b8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12800b99b8Sopenharmony_ci * See the License for the specific language governing permissions and 13800b99b8Sopenharmony_ci * limitations under the License. 14800b99b8Sopenharmony_ci */ 15800b99b8Sopenharmony_ci 16800b99b8Sopenharmony_ci#include <gtest/gtest.h> 17800b99b8Sopenharmony_ci#include <sys/stat.h> 18800b99b8Sopenharmony_ci#include <ctime> 19800b99b8Sopenharmony_ci#include <securec.h> 20800b99b8Sopenharmony_ci#include <string> 21800b99b8Sopenharmony_ci#include <vector> 22800b99b8Sopenharmony_ci#include "dfx_define.h" 23800b99b8Sopenharmony_ci#include "dfx_util.h" 24800b99b8Sopenharmony_ci#include "dfx_dump_res.h" 25800b99b8Sopenharmony_ci#include "dfx_log.h" 26800b99b8Sopenharmony_ci#include "string_util.h" 27800b99b8Sopenharmony_ci 28800b99b8Sopenharmony_ciusing namespace OHOS::HiviewDFX; 29800b99b8Sopenharmony_ciusing namespace testing::ext; 30800b99b8Sopenharmony_ciusing namespace std; 31800b99b8Sopenharmony_ci 32800b99b8Sopenharmony_cinamespace OHOS { 33800b99b8Sopenharmony_cinamespace HiviewDFX { 34800b99b8Sopenharmony_ciclass CommonTest : public testing::Test { 35800b99b8Sopenharmony_cipublic: 36800b99b8Sopenharmony_ci static void SetUpTestCase(void) {} 37800b99b8Sopenharmony_ci static void TearDownTestCase(void) {} 38800b99b8Sopenharmony_ci void SetUp() {} 39800b99b8Sopenharmony_ci void TearDown() {} 40800b99b8Sopenharmony_ci}; 41800b99b8Sopenharmony_ci 42800b99b8Sopenharmony_cistatic void getLibPathsBySystemBits(vector<std::string> &filePaths) 43800b99b8Sopenharmony_ci{ 44800b99b8Sopenharmony_ci std::vector<std::string> lib64FilePaths = { 45800b99b8Sopenharmony_ci "/system/lib64/librustc_demangle.z.so", 46800b99b8Sopenharmony_ci "/system/lib64/libstacktrace_rust.dylib.so", 47800b99b8Sopenharmony_ci "/system/lib64/libpanic_handler.dylib.so", 48800b99b8Sopenharmony_ci "/system/lib64/platformsdk/libjson_stack_formatter.z.so", 49800b99b8Sopenharmony_ci "/system/lib64/chipset-pub-sdk/libdfx_dumpcatcher.z.so", 50800b99b8Sopenharmony_ci "/system/lib64/chipset-pub-sdk/libfaultloggerd.z.so", 51800b99b8Sopenharmony_ci "/system/lib64/chipset-pub-sdk/libdfx_signalhandler.z.so", 52800b99b8Sopenharmony_ci "/system/lib64/chipset-pub-sdk/libasync_stack.z.so", 53800b99b8Sopenharmony_ci "/system/lib64/chipset-pub-sdk/libbacktrace_local.so", 54800b99b8Sopenharmony_ci "/system/lib64/chipset-pub-sdk/libunwinder.z.so", 55800b99b8Sopenharmony_ci "/system/lib64/chipset-pub-sdk/libdfx_procinfo.z.so", 56800b99b8Sopenharmony_ci "/system/lib64/chipset-pub-sdk/libcrash_exception.z.so", 57800b99b8Sopenharmony_ci "/system/lib64/module/libfaultlogger_napi.z.so" 58800b99b8Sopenharmony_ci }; 59800b99b8Sopenharmony_ci std::vector<std::string> libFilePaths = { 60800b99b8Sopenharmony_ci "/system/lib/librustc_demangle.z.so", 61800b99b8Sopenharmony_ci "/system/lib/libstacktrace_rust.dylib.so", 62800b99b8Sopenharmony_ci "/system/lib/libpanic_handler.dylib.so", 63800b99b8Sopenharmony_ci "/system/lib/platformsdk/libjson_stack_formatter.z.so", 64800b99b8Sopenharmony_ci "/system/lib/chipset-pub-sdk/libdfx_dumpcatcher.z.so", 65800b99b8Sopenharmony_ci "/system/lib/chipset-pub-sdk/libfaultloggerd.z.so", 66800b99b8Sopenharmony_ci "/system/lib/chipset-pub-sdk/libdfx_signalhandler.z.so", 67800b99b8Sopenharmony_ci "/system/lib/chipset-pub-sdk/libasync_stack.z.so", 68800b99b8Sopenharmony_ci "/system/lib/chipset-pub-sdk/libbacktrace_local.so", 69800b99b8Sopenharmony_ci "/system/lib/chipset-pub-sdk/libunwinder.z.so", 70800b99b8Sopenharmony_ci "/system/lib/chipset-pub-sdk/libdfx_procinfo.z.so", 71800b99b8Sopenharmony_ci "/system/lib/chipset-pub-sdk/libcrash_exception.z.so", 72800b99b8Sopenharmony_ci "/system/lib/module/libfaultlogger_napi.z.so" 73800b99b8Sopenharmony_ci }; 74800b99b8Sopenharmony_ci#ifdef __LP64__ 75800b99b8Sopenharmony_ci filePaths.insert(filePaths.end(), lib64FilePaths.begin(), lib64FilePaths.end()); 76800b99b8Sopenharmony_ci#else 77800b99b8Sopenharmony_ci filePaths.insert(filePaths.end(), libFilePaths.begin(), libFilePaths.end()); 78800b99b8Sopenharmony_ci#endif 79800b99b8Sopenharmony_ci} 80800b99b8Sopenharmony_ci 81800b99b8Sopenharmony_cinamespace { 82800b99b8Sopenharmony_ci#ifdef LOG_DOMAIN 83800b99b8Sopenharmony_ci#undef LOG_DOMAIN 84800b99b8Sopenharmony_ci#define LOG_DOMAIN 0xD002D11 85800b99b8Sopenharmony_ci#endif 86800b99b8Sopenharmony_ci 87800b99b8Sopenharmony_ci#ifdef LOG_TAG 88800b99b8Sopenharmony_ci#undef LOG_TAG 89800b99b8Sopenharmony_ci#define LOG_TAG "DfxCommonTest" 90800b99b8Sopenharmony_ci#endif 91800b99b8Sopenharmony_ci 92800b99b8Sopenharmony_ci/** 93800b99b8Sopenharmony_ci * @tc.name: DfxUtilTest001 94800b99b8Sopenharmony_ci * @tc.desc: test DfxUtil GetCurrentTimeStr 95800b99b8Sopenharmony_ci * @tc.type: FUNC 96800b99b8Sopenharmony_ci */ 97800b99b8Sopenharmony_ciHWTEST_F(CommonTest, DfxUtilTest001, TestSize.Level2) 98800b99b8Sopenharmony_ci{ 99800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DfxUtilTest001: start."; 100800b99b8Sopenharmony_ci time_t now = time(nullptr); 101800b99b8Sopenharmony_ci std::string timeStr = GetCurrentTimeStr(static_cast<uint64_t>(now)); 102800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << timeStr; 103800b99b8Sopenharmony_ci ASSERT_NE(timeStr, "invalid timestamp\n"); 104800b99b8Sopenharmony_ci now += 100000; // 100000 : test time offset 105800b99b8Sopenharmony_ci timeStr = GetCurrentTimeStr(static_cast<uint64_t>(now)); 106800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << timeStr; 107800b99b8Sopenharmony_ci ASSERT_NE(timeStr, "invalid timestamp\n"); 108800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DfxUtilTest001: end."; 109800b99b8Sopenharmony_ci} 110800b99b8Sopenharmony_ci 111800b99b8Sopenharmony_ci/** 112800b99b8Sopenharmony_ci * @tc.name: DfxUtilTest002 113800b99b8Sopenharmony_ci * @tc.desc: test DfxUtil TrimAndDupStr 114800b99b8Sopenharmony_ci * @tc.type: FUNC 115800b99b8Sopenharmony_ci */ 116800b99b8Sopenharmony_ciHWTEST_F(CommonTest, DfxUtilTest002, TestSize.Level2) 117800b99b8Sopenharmony_ci{ 118800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DfxUtilTest002: start."; 119800b99b8Sopenharmony_ci std::string testStr = " abcd "; 120800b99b8Sopenharmony_ci std::string resStr; 121800b99b8Sopenharmony_ci bool ret = TrimAndDupStr(testStr, resStr); 122800b99b8Sopenharmony_ci ASSERT_EQ(ret, true); 123800b99b8Sopenharmony_ci ASSERT_EQ(resStr, "abcd"); 124800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DfxUtilTest002: end."; 125800b99b8Sopenharmony_ci} 126800b99b8Sopenharmony_ci 127800b99b8Sopenharmony_ci/** 128800b99b8Sopenharmony_ci * @tc.name: DfxDumpResTest001 129800b99b8Sopenharmony_ci * @tc.desc: test DfxDumpRes functions 130800b99b8Sopenharmony_ci * @tc.type: FUNC 131800b99b8Sopenharmony_ci */ 132800b99b8Sopenharmony_ciHWTEST_F(CommonTest, DfxDumpResTest001, TestSize.Level2) 133800b99b8Sopenharmony_ci{ 134800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DfxDumpResTest001: start."; 135800b99b8Sopenharmony_ci int32_t res = DUMP_ESUCCESS; 136800b99b8Sopenharmony_ci std::string result = DfxDumpRes::ToString(res); 137800b99b8Sopenharmony_ci ASSERT_EQ(result, "0 ( no error )"); 138800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DfxDumpResTest001: end."; 139800b99b8Sopenharmony_ci} 140800b99b8Sopenharmony_ci 141800b99b8Sopenharmony_ci/** 142800b99b8Sopenharmony_ci * @tc.name: StringUtilTest001 143800b99b8Sopenharmony_ci * @tc.desc: test StartsWith functions 144800b99b8Sopenharmony_ci * @tc.type: FUNC 145800b99b8Sopenharmony_ci */ 146800b99b8Sopenharmony_ciHWTEST_F(CommonTest, StringUtilTest001, TestSize.Level2) 147800b99b8Sopenharmony_ci{ 148800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "StringUtilTest001: start."; 149800b99b8Sopenharmony_ci std::string start = "[anon:ArkTS Code2024]"; 150800b99b8Sopenharmony_ci bool ret = StartsWith(start, "[anon:ArkTS Code"); 151800b99b8Sopenharmony_ci EXPECT_TRUE(ret); 152800b99b8Sopenharmony_ci std::string end = "test.hap"; 153800b99b8Sopenharmony_ci ret = EndsWith(end, ".hap"); 154800b99b8Sopenharmony_ci EXPECT_TRUE(ret); 155800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "StringUtilTest001: end."; 156800b99b8Sopenharmony_ci} 157800b99b8Sopenharmony_ci 158800b99b8Sopenharmony_ci/** 159800b99b8Sopenharmony_ci * @tc.name: ROMBaselineTest001 160800b99b8Sopenharmony_ci * @tc.desc: test the ROM baseline of the faultloggerd component 161800b99b8Sopenharmony_ci * @tc.type: FUNC 162800b99b8Sopenharmony_ci */ 163800b99b8Sopenharmony_ciHWTEST_F(CommonTest, ROMBaselineTest001, TestSize.Level2) 164800b99b8Sopenharmony_ci{ 165800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "ROMBaselineTest001: start."; 166800b99b8Sopenharmony_ci std::vector<std::string> filePaths = { 167800b99b8Sopenharmony_ci "/system/etc/faultlogger.conf", 168800b99b8Sopenharmony_ci "/system/etc/param/faultloggerd.para", 169800b99b8Sopenharmony_ci "/system/etc/param/faultloggerd.para.dac", 170800b99b8Sopenharmony_ci "/system/etc/init/faultloggerd.cfg", 171800b99b8Sopenharmony_ci "/system/etc/hiview/extract_rule.json", 172800b99b8Sopenharmony_ci "/system/etc/hiview/compose_rule.json", 173800b99b8Sopenharmony_ci "/system/bin/processdump", 174800b99b8Sopenharmony_ci "/system/bin/faultloggerd", 175800b99b8Sopenharmony_ci "/system/bin/dumpcatcher", 176800b99b8Sopenharmony_ci "/system/bin/crashvalidator" 177800b99b8Sopenharmony_ci }; 178800b99b8Sopenharmony_ci getLibPathsBySystemBits(filePaths); 179800b99b8Sopenharmony_ci struct stat fileStat; 180800b99b8Sopenharmony_ci long long totalSize = 0; 181800b99b8Sopenharmony_ci constexpr long long sectorSize = 4; 182800b99b8Sopenharmony_ci for (const auto &filePath : filePaths) { 183800b99b8Sopenharmony_ci int ret = stat(filePath.c_str(), &fileStat); 184800b99b8Sopenharmony_ci if (ret != 0) { 185800b99b8Sopenharmony_ci printf("Failed to get file size of %s\n", filePath.c_str()); 186800b99b8Sopenharmony_ci } else { 187800b99b8Sopenharmony_ci long long size = fileStat.st_size / 1024; 188800b99b8Sopenharmony_ci size = size + sectorSize - size % sectorSize; 189800b99b8Sopenharmony_ci printf("File size of %s is %lldKB\n", filePath.c_str(), size); 190800b99b8Sopenharmony_ci totalSize += size; 191800b99b8Sopenharmony_ci } 192800b99b8Sopenharmony_ci } 193800b99b8Sopenharmony_ci printf("Total file size is %lldKB\n", totalSize); 194800b99b8Sopenharmony_ci constexpr long long baseline = 1054; 195800b99b8Sopenharmony_ci EXPECT_LT(totalSize, static_cast<long long>(baseline * 1.05)); 196800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "ROMBaselineTest001: end."; 197800b99b8Sopenharmony_ci} 198800b99b8Sopenharmony_ci} 199800b99b8Sopenharmony_ci} // namespace HiviewDFX 200800b99b8Sopenharmony_ci} // namespace OHOS