diff --git a/hiviewdfx/BUILD.gn b/hiviewdfx/BUILD.gn index 88d467f2b2da48acb0f8a0cf3d04378599f7aca7..d73f7978f5caf26fe386508a92c945c4612ede6f 100755 --- a/hiviewdfx/BUILD.gn +++ b/hiviewdfx/BUILD.gn @@ -15,6 +15,7 @@ group("hiviewdfxtestacts") { testonly = true if (is_standard_system) { deps = [ + "faultloggertest/faultloggercpptest:faultloggertest", "hiappeventtest/hiappeventjstest:hiappeventjstest", "hilogtest/libhilogtest:libhilogtestacts", "hisyseventtest/hisyseventcpptest:HiSysEventCPPTest", diff --git a/hiviewdfx/faultloggertest/faultloggercpptest/BUILD.gn b/hiviewdfx/faultloggertest/faultloggercpptest/BUILD.gn new file mode 100755 index 0000000000000000000000000000000000000000..4276b9c15e70e5febfb48515546536c25bb665be --- /dev/null +++ b/hiviewdfx/faultloggertest/faultloggercpptest/BUILD.gn @@ -0,0 +1,41 @@ +# Copyright (C) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//test/xts/tools/build/suite.gni") +module_output_path = "hits/faultloggertest" + +############################################################################### +config("faultloggertest_config") { + visibility = [ ":*" ] + include_dirs = [ + "../../utils/native", + "//utils/native/base/include/", + "//base/hiviewdfx/hilog/interfaces/native/innerkits/include", + "genfault.h", + ] +} +ohos_moduletest_suite("faultloggertest") { + module_out_path = module_output_path + sources = [ + "faultloggertest.cpp", + "genfault.cpp", + ] + deps = [ + "../../utils/native:utilskit", + "//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog", + "//third_party/googletest:gtest_main", + "//utils/native/base:utils", + ] + + configs = [ ":faultloggertest_config" ] +} diff --git a/hiviewdfx/faultloggertest/faultloggercpptest/Test.json b/hiviewdfx/faultloggertest/faultloggercpptest/Test.json new file mode 100755 index 0000000000000000000000000000000000000000..cf29d5e01dec06d090420d7232e040ac2901ec6e --- /dev/null +++ b/hiviewdfx/faultloggertest/faultloggercpptest/Test.json @@ -0,0 +1,21 @@ +{ + "kits": [ + { + "push": [ + "faultloggertest->/data/local/tmp/faultloggertest" + ], + "type": "PushKit", + "post-push": [ + "chmod -R 777 /data/local/tmp/*" + ] + } + ], + "driver": { + "native-test-timeout": "120000", + "type": "CppTest", + "module-name": "faultloggertest", + "runtime-hint": "1s", + "native-test-device-path": "/data/local/tmp" + }, + "description": "Configuration for faultloggertest Tests" +} \ No newline at end of file diff --git a/hiviewdfx/faultloggertest/faultloggercpptest/faultloggertest.cpp b/hiviewdfx/faultloggertest/faultloggercpptest/faultloggertest.cpp new file mode 100755 index 0000000000000000000000000000000000000000..13467c3e0630817e7a28dfef71ba3cb63536ded4 --- /dev/null +++ b/hiviewdfx/faultloggertest/faultloggercpptest/faultloggertest.cpp @@ -0,0 +1,140 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include + +#include "genfault.h" +#include "file_utils.h" +#include "hilog/log_c.h" +#include "hilog/log_cpp.h" + +#undef LOG_DOMAIN +#undef LOG_TAG +#define LOG_DOMAIN 0xD003200 +#define LOG_TAG "FAULTLOGGERTEST" +using namespace OHOS; +using namespace HiviewDFX; +using namespace testing::ext; +using namespace std; + +class faultloggertest : public testing::Test { +public: + + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); + pid_t DoTestProcess(int faulttype); + int status; + +private: +}; +void faultloggertest::SetUp() +{ +} +void faultloggertest::TearDown() +{ +} +void faultloggertest::SetUpTestCase() +{ +} +void faultloggertest::TearDownTestCase() +{ +} + +pid_t faultloggertest::DoTestProcess(int faulttype) +{ + printf("DoTestProcess, param is %d\r\n", faulttype); + pid_t pid; + pid = fork(); + printf("pid is %d\r\n", pid); + switch (pid) { + case -1: + std::cout<<"for pid failed"< faultfilelist; + faultfilelist = getfileinpath("/data/log/faultlog/temp/"); + printf("sizeof faultfilelist is %d\r\n", faultfilelist.size()); + bool result = false; + for (std::string filename : faultfilelist) { + printf("file list is %s\r\n", filename.c_str()); + if (filename.find("cppcrash-" + to_string(pid))) { + result = true; + } + } + ASSERT_TRUE(true == result); +} + +/* + * @tc.name faultlogger log file check + * @tc.number DFX_DFR_FaultLogger_0100 + * @tc.desc inject a cppcrash fault and check faultlogger file +*/ +HWTEST_F(faultloggertest, Faultlogger_Faultdetect1, Function|MediumTest|Level1) +{ + pid_t pid = DoTestProcess(3); + printf("pid is %d\r\n", pid); + printf("sub process end with status %d\r\n", faultloggertest::status); + std::vector faultfilelist; + faultfilelist = getfileinpath("/data/log/faultlog/temp/"); + printf("sizeof faultfilelist is %d\r\n", faultfilelist.size()); + bool result = false; + std::string faultloggerfile = ""; + for (std::string filename : faultfilelist) { + if (filename.find("cppcrash-" + to_string(pid)) != std::string::npos) { + printf("file list is %s\r\n", filename.c_str()); + faultloggerfile = filename; + break; + } + } + string fileinfo; + fileinfo = ReadFile("/data/log/faultlog/temp/" + faultloggerfile); + std::vector para = {"Pid:" + to_string(pid), "Uid:0", + "Process name:./faultloggertest", + "Reason:Signal:SIGILL", "Fault thread Info:", + "Tid:" + to_string(pid), "Name:faultloggertest"}; + if (!fileinfo.empty()) { + result = CheckInfo(para, fileinfo); + } else { + std::cout << "Faultlogger_Faultdetect1 file error" << std::endl; + } + ASSERT_TRUE(result); + GTEST_LOG_(INFO) << "Faultlogger_Faultdetect1 end" << endl; +} \ No newline at end of file diff --git a/hiviewdfx/faultloggertest/faultloggercpptest/genfault.cpp b/hiviewdfx/faultloggertest/faultloggercpptest/genfault.cpp new file mode 100755 index 0000000000000000000000000000000000000000..5f48c54278e51f633124927d6a83e22e5434635d --- /dev/null +++ b/hiviewdfx/faultloggertest/faultloggercpptest/genfault.cpp @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef TEST_COMMON_H +#define TEST_COMMON_H +#include +#include +#include +#include + +typedef enum { + FAULT_HEAP = 1, + FAULT_STACK = 2, + FAULT_NULLPTR = 3, +}FaultType; + +void FaultHeap(int value) +{ + + int* arraytest = new int[123]; + delete []arraytest; + printf("%d", arraytest[value]); +} + +void FaultStack(int value) +{ + int arraytest[123]; + printf("value of i is %d", arraytest[value]); +} + +void FaultNullPointer() +{ + const int num = 80; + int *i = nullptr; + *i = num; +} + +void GenFault(int faulttype) +{ + printf("genFault, param is %d\r\n", faulttype); + switch (faulttype) { + case FAULT_HEAP: + FaultHeap(125); + case FAULT_STACK: + FaultStack(125); + case FAULT_NULLPTR: + FaultNullPointer(); + break; + } +} +#endif \ No newline at end of file diff --git a/hiviewdfx/faultloggertest/faultloggercpptest/genfault.h b/hiviewdfx/faultloggertest/faultloggercpptest/genfault.h new file mode 100755 index 0000000000000000000000000000000000000000..7dc209d33484b0c211c6b84ff3b3a0ad0e9b7e40 --- /dev/null +++ b/hiviewdfx/faultloggertest/faultloggercpptest/genfault.h @@ -0,0 +1,16 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "file_utils.h" +void GenFault(int faulttype); \ No newline at end of file diff --git a/hiviewdfx/utils/native/file_utils.cpp b/hiviewdfx/utils/native/file_utils.cpp index 4c7e10671b33192d5793dae14592e696f170f7de..05b8feba5a7895f182c5db02d021f1c1cf9676a7 100755 --- a/hiviewdfx/utils/native/file_utils.cpp +++ b/hiviewdfx/utils/native/file_utils.cpp @@ -202,4 +202,26 @@ std::string ReadFile(std::string filename) ifile.close(); return buf.str(); } -#endif \ No newline at end of file + +std::vector getfileinpath(std::string path) +{ + std::vector filelist; + DIR *dir; + struct dirent *pdirent; + dir = opendir(path.c_str()); + if (dir != nullptr) { + while ((pdirent = readdir(dir)) != nullptr) { + if (strncmp(pdirent->d_name, ".", strlen(pdirent->d_name)) == 0 || + strncmp(pdirent->d_name, "..", strlen(pdirent->d_name)) == 0) { + continue; + } else if (pdirent->d_type == 4) { + continue; + } else { + filelist.push_back(pdirent->d_name); + } + } + closedir(dir); + } + return filelist; +} +#endif diff --git a/hiviewdfx/utils/native/file_utils.h b/hiviewdfx/utils/native/file_utils.h index b93e40c05a21aec5df8c17b98971f5156a5eb297..0e2dd581f1212f243ff9eaba522c6f0d10650b94 100755 --- a/hiviewdfx/utils/native/file_utils.h +++ b/hiviewdfx/utils/native/file_utils.h @@ -41,4 +41,5 @@ std::string ReadFile(std::string filename); void CleanCmd(); std::string ReadOneLine(std::string m_path, char* rBuf, int n); void SaveCmdOutput(std::string cmd, std::string saveFile); +std::vector getfileinpath(std::string path); #endif