提交 836929f1 编写于 作者: O openharmony_ci 提交者: Gitee

!402 增加faultlogger测试用例

Merge pull request !402 from 土行孙/master
......@@ -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",
......
# 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" ]
}
{
"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
/*
* 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 <cstdarg>
#include <ctime>
#include <gtest/gtest.h>
#include <pthread.h>
#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"<<std::endl;
break;
case 0:
GenFault(faulttype);
break;
default:
break;
}
wait(&status);
printf("sub process end with status %d\r\n", status);
return pid;
}
/*
* @tc.name faultlogger Detect a cpp crash happen
* @tc.number DFX_DFR_FaultLogger_0100
* @tc.desc inject a cppcrash fault and check faultlogger can detect the fault
*/
HWTEST_F(faultloggertest, Faultlogger_Faultdetect, 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);
ASSERT_FALSE(status == 0);
std::vector<std::string> 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<std::string> 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<std::string> 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
/*
* 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 <stdlib.h>
#include <string.h>
#include <thread>
#include <unistd.h>
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
/*
* 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
......@@ -202,4 +202,26 @@ std::string ReadFile(std::string filename)
ifile.close();
return buf.str();
}
#endif
\ No newline at end of file
std::vector<std::string> getfileinpath(std::string path)
{
std::vector<std::string> 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
......@@ -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<std::string> getfileinpath(std::string path);
#endif
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册