提交 ca556c4d 编写于 作者: C chenxuihui

hiappevent bugfix

Signed-off-by: Nchenxuihui <chenxuhui2@huawei.com>
上级 c3f23eeb
......@@ -20,6 +20,7 @@ config("hilogtest_config") {
visibility = [ ":*" ]
include_dirs = [
"//utils/native/base/include/",
"../../utils/native",
"//base/hiviewdfx/hilog/interfaces/native/innerkits/include",
"//base/hiviewdfx/hiappevent/interfaces/native/kits/include/hiappevent",
"//base/hiviewdfx/hiappevent/frameworks/native/libhiappevent/include",
......@@ -34,6 +35,7 @@ ohos_moduletest_suite("HiAppEventCPPTest") {
"//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog",
"//third_party/googletest:gtest_main",
"//utils/native/base:utils",
"../../utils/native:utilskit",
]
configs = [ ":hilogtest_config" ]
}
......@@ -24,6 +24,7 @@
#include "hiappevent.h"
#include "hiappevent_config.h"
#include "hilog/log.h"
#include "file_utils.h"
using namespace std;
......
# Copyright (C) 2021-2022 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("//build/ohos.gni")
import("//test/xts/tools/build/suite.gni")
###############################################################################
config("utils_config") {
visibility = [ ":*" ]
include_dirs = [
"//third_party/cJSON/",
"//third_party/zlib/",
"//utils/native/base/",
]
}
sources_common = [ "file_utils.cpp" ]
ohos_static_library("utilskit") {
sources = sources_common
configs = [ ":utils_config" ]
#external_deps = [ "hilog:libhilog" ]
deps = [
"//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog",
"//base/hiviewdfx/hiview/base:hiviewbase",
"//utils/native/base:utils",
]
}
/*
* Copyright (C) 2021-2022 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 "file_utils.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <chrono>
#include <dirent.h>
#include <condition_variable>
#include <vector>
#include "include/securec.h"
#include "string_util.h"
using namespace OHOS::HiviewDFX;
int ExecCmdWithRet(std::string cmd, std::vector<std::string> &resvec)
{
if (cmd.size() == 0) {
return 0;
}
std::cout<< "cmd is " + cmd <<std::endl;
if ((cmd.find("hilog") == std::string::npos) && (cmd.find("hidumper") == std::string::npos)
&& (cmd.find("ps") == std::string::npos) && (cmd.find("rm") == std::string::npos) &&
(cmd.find("hiperf") == std::string::npos) && (cmd.find("hisysevent") == std::string::npos) &&
(cmd.find("mkdir") == std::string::npos) && (cmd.find("dd") == std::string::npos)) {
std::cout<<"unsupport cmd!" + cmd <<std::endl;
return 0;
}
resvec.clear();
FILE *pp = popen(cmd.c_str(), "r");
if (pp == nullptr) {
return -1;
}
char tmp[1024];
while (fgets(tmp, sizeof(tmp), pp) != nullptr) {
if (tmp[strlen(tmp) - 1] == '\n') {
tmp[strlen(tmp) - 1] = '\0';
}
resvec.push_back(tmp);
}
pclose(pp);
return resvec.size();
}
void ExeCmd(std::string cmd)
{
std::vector<std::string> cmdret;
ExecCmdWithRet(cmd, cmdret);
}
void CmdRun(std::string cmd, std::string &result)
{
std::vector<std::string> cmdret;
int resultlen;
int i = 0;
std::string rst;
resultlen = ExecCmdWithRet(cmd, cmdret);
while (i < resultlen) {
rst = rst + cmdret[i];
i = i + 1;
}
result = rst;
}
void CleanCmd()
{
std::string cmdResult;
std::string cleanCmd = "hilog -r";
CmdRun(cleanCmd, cmdResult);
std::cout << cmdResult;
}
std::string ExecuteCmd(std::string cmd)
{
std::vector<std::string> cmdret;
int resultlen;
int i = 0;
std::string rst;
resultlen = ExecCmdWithRet(cmd, cmdret);
while (i < resultlen) {
rst = rst + cmdret[i] + "\n";
i = i + 1;
}
return rst;
}
void SaveCmdOutput(std::string cmd, std::string saveFile)
{
std::fstream fstr(saveFile, std::ios::out);
std::string cmdRet = ExecuteCmd(cmd);
fstr << cmdRet;
fstr.close();
}
void RedirecthiLog(std::string &hilogredirect, std::string &timeout)
{
unsigned long i;
std::vector<std::string> cmdret;
unsigned long cmdretlen;
std::string cmd = "rm " + hilogredirect;
cmdretlen = ExecCmdWithRet(cmd, cmdret);
for (i = 0; i < cmdretlen; i++) {
std::cout<<cmdret[i].c_str()<<std::endl;
}
cmd = "timeout " + timeout + " hilog >" + hilogredirect;
std::cout<<cmd<<std::endl;
cmdretlen = ExecCmdWithRet(cmd, cmdret);
for (i = 0; i < cmdretlen; i++) {
std::cout<<cmdret[i].c_str()<<std::endl;
}
}
bool CheckInfo(std::vector<std::string> &para, std::string info)
{
if (info.empty()) {
return false;
}
bool result = false;
unsigned long matchcnt = 0;
for (unsigned long i = 0; i < para.size(); i++) {
if (int(info.find(para[i])) >= 0) {
matchcnt++;
}
}
std::cout<< matchcnt <<std::endl;
if (matchcnt == para.size()) {
result = true;
}
return result;
}
bool CompareString(const std::string& x, const std::string& y)
{
int len = x.length() - 1;
while (x[len] == y[len] && len >= 0) {
len--;
}
if (len >= 0 && x[len] > y[len]) {
return false;
}
return true;
}
int GetTxtLine(std::string filename)
{
FILE *fd = fopen(filename.c_str(), "r");
int count = 0;
if (fd != nullptr) {
while (!feof(fd)) {
if (fgetc(fd) == '\n') {
count++;
}
}
}
std::cout << count << std::endl;
if (fd != nullptr) {
fclose(fd);
}
return count;
}
std::string ReadFile(std::string filename)
{
std::ifstream ifile(filename);
std::ostringstream buf;
char ch;
if (ifile.fail()) {
std::cout<<"open file fail!"<<std::endl;
return "";
}
while (buf && ifile.get(ch)) {
buf.put(ch);
}
ifile.close();
return buf.str();
}
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
/*
* Copyright (C) 2021-2022 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 HIVIEWDFX_UTILS_NATIVE_FILE_UTILS_H_
#define HIVIEWDFX_UTILS_NATIVE_FILE_UTILS_H_
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
#include <chrono>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <vector>
bool CheckInfo(std::vector<std::string> &para, std::string eventinfo);
int ExecCmdWithRet(std::string cmd, std::vector<std::string> &resvec);
std::string ReadFile(std::string filename);
void RedirecthiLog(std::string &hilogredirect, std::string &timeout);
void ExeCmd(std::string cmd);
void CmdRun(std::string cmd, std::string &result);
std::string ExecuteCmd(std::string cmd);
bool CompareString(const std::string& x, const std::string& y);
int GetTxtLine(std::string filename);
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 // HIVIEWDFX_UTILS_NATIVE_FILE_UTILS_H_
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册