faultloggertest.cpp 4.2 KB
Newer Older
T
tuxingsun 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
/*
 * 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:
T
tuxingsun 已提交
70
            GenFault(faulttype);
T
tuxingsun 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
            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);
92
    sleep(1);
T
tuxingsun 已提交
93 94 95 96
    std::vector<std::string> faultfilelist;
    faultfilelist = getfileinpath("/data/log/faultlog/temp/");
    printf("sizeof faultfilelist is %d\r\n", faultfilelist.size());
    bool result = false;
T
tuxingsun 已提交
97
    for (std::string filename : faultfilelist) {
T
tuxingsun 已提交
98
        printf("file list is %s\r\n", filename.c_str());
99
        if (filename.find("cppcrash-" + to_string(pid)) != string::npos) {
T
tuxingsun 已提交
100
            result = true;
101
            break;
T
tuxingsun 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
        }
    }
    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);
117
    sleep(1);
T
tuxingsun 已提交
118 119 120 121 122
    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 = "";
T
tuxingsun 已提交
123
    for (std::string filename : faultfilelist) {
T
tuxingsun 已提交
124 125 126 127 128 129 130 131 132
        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",
133
                            "Process name:", "/data/local/tmp/faultloggertest",
T
tuxingsun 已提交
134 135 136 137 138 139 140 141 142 143
                            "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;
}