1/*
2 * Copyright (C) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#ifndef TEST_COMMON_H
16#define TEST_COMMON_H
17#include "file_utils.h"
18#include <cstdio>
19#include <cstdlib>
20#include <cstring>
21#include <string>
22#include <fstream>
23#include <sstream>
24#include <iostream>
25#include <unistd.h>
26#include <sys/types.h>
27#include <sys/stat.h>
28#include <chrono>
29#include <dirent.h>
30#include <condition_variable>
31#include <vector>
32#include "include/securec.h"
33#include "string_util.h"
34
35using namespace OHOS::HiviewDFX;
36int ExecCmdWithRet(std::string cmd, std::vector<std::string> &resvec)
37{
38    if (cmd.size() == 0) {
39        return 0;
40    }
41
42    std::cout<< "cmd is " + cmd <<std::endl;
43    if ((cmd.find("hilog") == std::string::npos) && (cmd.find("hidumper") == std::string::npos)
44        && (cmd.find("ps") == std::string::npos) && (cmd.find("rm") == std::string::npos) &&
45        (cmd.find("hiperf") == std::string::npos) && (cmd.find("hisysevent") == std::string::npos) &&
46        (cmd.find("mkdir") == std::string::npos) && (cmd.find("dd") == std::string::npos)) {
47        std::cout<<"unsupport cmd!" + cmd <<std::endl;
48        return 0;
49    }
50    resvec.clear();
51    FILE *pp = popen(cmd.c_str(), "r");
52    if (pp == nullptr) {
53        return -1;
54    }
55    char tmp[1024];
56    while (fgets(tmp, sizeof(tmp), pp) != nullptr) {
57        if (tmp[strlen(tmp) - 1] == '\n') {
58            tmp[strlen(tmp) - 1] = '\0';
59        }
60        resvec.push_back(tmp);
61    }
62    pclose(pp);
63    return resvec.size();
64}
65
66void ExeCmd(std::string cmd)
67{
68    std::vector<std::string> cmdret;
69    ExecCmdWithRet(cmd, cmdret);
70}
71
72void CmdRun(std::string cmd, std::string &result)
73{
74    std::vector<std::string> cmdret;
75    int resultlen;
76    int i = 0;
77    std::string rst;
78    resultlen = ExecCmdWithRet(cmd, cmdret);
79    while (i < resultlen) {
80        rst = rst + cmdret[i];
81        i = i + 1;
82    }
83    result = rst;
84}
85
86void CleanCmd()
87{
88    std::string cmdResult;
89    std::string cleanCmd = "hilog -r";
90    CmdRun(cleanCmd, cmdResult);
91    std::cout << cmdResult;
92}
93
94std::string ExecuteCmd(std::string cmd)
95{
96    std::vector<std::string> cmdret;
97    int resultlen;
98    int i = 0;
99    std::string rst;
100    resultlen = ExecCmdWithRet(cmd, cmdret);
101    while (i < resultlen) {
102        rst = rst + cmdret[i] + "\n";
103        i = i + 1;
104    }
105    return rst;
106}
107void SaveCmdOutput(std::string cmd, std::string saveFile)
108{
109    std::fstream fstr(saveFile, std::ios::out);
110    std::string cmdRet = ExecuteCmd(cmd);
111    fstr << cmdRet;
112    fstr.close();
113}
114void RedirecthiLog(std::string &hilogredirect, std::string &timeout)
115{
116    unsigned long i;
117    std::vector<std::string> cmdret;
118    unsigned long cmdretlen;
119    std::string cmd = "rm " + hilogredirect;
120    cmdretlen = ExecCmdWithRet(cmd, cmdret);
121    for (i = 0; i < cmdretlen; i++) {
122        std::cout<<cmdret[i].c_str()<<std::endl;
123    }
124    cmd = "timeout " + timeout + " hilog >" + hilogredirect;
125    std::cout<<cmd<<std::endl;
126    cmdretlen = ExecCmdWithRet(cmd, cmdret);
127    for (i = 0; i < cmdretlen; i++) {
128        std::cout<<cmdret[i].c_str()<<std::endl;
129    }
130}
131
132bool CheckInfo(std::vector<std::string> &para, std::string info)
133{
134    if (info.empty()) {
135        return false;
136    }
137    bool result = false;
138    unsigned long matchcnt = 0;
139    for (unsigned long i = 0; i < para.size(); i++) {
140        if (int(info.find(para[i])) >= 0) {
141            matchcnt++;
142        }
143    }
144    std::cout<< matchcnt <<std::endl;
145    if (matchcnt == para.size()) {
146        result = true;
147    }
148    return result;
149}
150
151bool CompareString(const std::string& x, const std::string& y)
152{
153    int len = x.length() - 1;
154    while (x[len] == y[len] && len >= 0) {
155        len--;
156    }
157    if (len >= 0 && x[len] > y[len]) {
158        return false;
159    }
160    return true;
161}
162
163int GetTxtLine(std::string filename)
164{
165    FILE *fd = fopen(filename.c_str(), "r");
166    int count = 0;
167    if (fd != nullptr) {
168        while (!feof(fd)) {
169            if (fgetc(fd) == '\n') {
170                count++;
171            }
172        }
173    }
174    std::cout << count << std::endl;
175    if (fd != nullptr) {
176        fclose(fd);
177    }
178    return count;
179}
180
181std::string ReadFile(std::string filename)
182{
183    std::ifstream ifile(filename);
184    std::ostringstream buf;
185    char ch;
186    if (ifile.fail()) {
187        std::cout<<"open file fail!"<<std::endl;
188        return "";
189    }
190    while (buf && ifile.get(ch)) {
191        buf.put(ch);
192    }
193    ifile.close();
194    return buf.str();
195}
196
197std::vector<std::string> getfileinpath(std::string path)
198{
199    std::vector<std::string> filelist;
200    DIR *dir;
201    struct dirent *pdirent;
202    dir = opendir(path.c_str());
203    if (dir != nullptr) {
204        while ((pdirent = readdir(dir)) != nullptr) {
205            if (strncmp(pdirent->d_name, ".", strlen(pdirent->d_name)) == 0 ||
206                strncmp(pdirent->d_name, "..", strlen(pdirent->d_name)) == 0) {
207                continue;
208            } else if (pdirent->d_type == 4) {
209                continue;
210            } else {
211                filelist.push_back(pdirent->d_name);
212            }
213        }
214        closedir(dir);
215    }
216    return filelist;
217}
218#endif
219