100600bfbSopenharmony_ci/* 200600bfbSopenharmony_ci * Copyright (C) 2023 Huawei Device Co., Ltd. 300600bfbSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 400600bfbSopenharmony_ci * you may not use this file except in compliance with the License. 500600bfbSopenharmony_ci * You may obtain a copy of the License at 600600bfbSopenharmony_ci * 700600bfbSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 800600bfbSopenharmony_ci * 900600bfbSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1000600bfbSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1100600bfbSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1200600bfbSopenharmony_ci * See the License for the specific language governing permissions and 1300600bfbSopenharmony_ci * limitations under the License. 1400600bfbSopenharmony_ci */ 1500600bfbSopenharmony_ci#include <fstream> 1600600bfbSopenharmony_ci#include <sstream> 1700600bfbSopenharmony_ci#include "hidumper_test_utils.h" 1800600bfbSopenharmony_ci 1900600bfbSopenharmony_ciusing namespace std; 2000600bfbSopenharmony_cinamespace OHOS { 2100600bfbSopenharmony_cinamespace HiviewDFX { 2200600bfbSopenharmony_ciHidumperTestUtils::HidumperTestUtils() 2300600bfbSopenharmony_ci{ 2400600bfbSopenharmony_ci} 2500600bfbSopenharmony_ci 2600600bfbSopenharmony_ciHidumperTestUtils::~HidumperTestUtils() 2700600bfbSopenharmony_ci{ 2800600bfbSopenharmony_ci} 2900600bfbSopenharmony_ci 3000600bfbSopenharmony_cibool HidumperTestUtils::IsExistInCmdResult(const std::string &cmd, const std::string &str) 3100600bfbSopenharmony_ci{ 3200600bfbSopenharmony_ci std::string line = ""; 3300600bfbSopenharmony_ci return GetSpecialLine(cmd, str, line); 3400600bfbSopenharmony_ci} 3500600bfbSopenharmony_ci 3600600bfbSopenharmony_cibool HidumperTestUtils::IsExistStrInFile(const std::string &cmd, const std::string &str, const std::string &filePath) 3700600bfbSopenharmony_ci{ 3800600bfbSopenharmony_ci bool res = false; 3900600bfbSopenharmony_ci FILE *fp = popen(cmd.c_str(), "r"); 4000600bfbSopenharmony_ci pclose(fp); 4100600bfbSopenharmony_ci 4200600bfbSopenharmony_ci std::ifstream file(filePath); 4300600bfbSopenharmony_ci if (!file.is_open()) { 4400600bfbSopenharmony_ci return res; 4500600bfbSopenharmony_ci } 4600600bfbSopenharmony_ci std::string line; 4700600bfbSopenharmony_ci while (std::getline(file, line)) { 4800600bfbSopenharmony_ci if (line.find(str) != string::npos) { 4900600bfbSopenharmony_ci res = true; 5000600bfbSopenharmony_ci break; 5100600bfbSopenharmony_ci } 5200600bfbSopenharmony_ci } 5300600bfbSopenharmony_ci file.close(); 5400600bfbSopenharmony_ci return res; 5500600bfbSopenharmony_ci} 5600600bfbSopenharmony_ci 5700600bfbSopenharmony_cipid_t HidumperTestUtils::GetPidByName(const std::string& processName) 5800600bfbSopenharmony_ci{ 5900600bfbSopenharmony_ci FILE *fp = nullptr; 6000600bfbSopenharmony_ci char buf[100]; // 100: buf size 6100600bfbSopenharmony_ci pid_t pid = -1; 6200600bfbSopenharmony_ci std::string cmd = "pidof " + processName; 6300600bfbSopenharmony_ci if ((fp = popen(cmd.c_str(), "r")) != nullptr) { 6400600bfbSopenharmony_ci if (fgets(buf, sizeof(buf), fp) != nullptr) { 6500600bfbSopenharmony_ci pid = std::atoi(buf); 6600600bfbSopenharmony_ci } 6700600bfbSopenharmony_ci pclose(fp); 6800600bfbSopenharmony_ci } 6900600bfbSopenharmony_ci return pid; 7000600bfbSopenharmony_ci} 7100600bfbSopenharmony_ci 7200600bfbSopenharmony_cibool HidumperTestUtils::GetSpecialLine(const std::string &cmd, const std::string &str, std::string &specialLine) 7300600bfbSopenharmony_ci{ 7400600bfbSopenharmony_ci bool res = false; 7500600bfbSopenharmony_ci size_t len = 0; 7600600bfbSopenharmony_ci FILE *fp = popen(cmd.c_str(), "r"); 7700600bfbSopenharmony_ci char* buffer = nullptr; 7800600bfbSopenharmony_ci while (getline(&buffer, &len, fp) != -1) { 7900600bfbSopenharmony_ci std::string line = buffer; 8000600bfbSopenharmony_ci if (line.find(str) != string::npos) { 8100600bfbSopenharmony_ci res = true; 8200600bfbSopenharmony_ci specialLine = line; 8300600bfbSopenharmony_ci break; 8400600bfbSopenharmony_ci } 8500600bfbSopenharmony_ci } 8600600bfbSopenharmony_ci pclose(fp); 8700600bfbSopenharmony_ci return res; 8800600bfbSopenharmony_ci} 8900600bfbSopenharmony_ci 9000600bfbSopenharmony_cistd::string HidumperTestUtils::GetValueInLine(const std::string &line, int index) 9100600bfbSopenharmony_ci{ 9200600bfbSopenharmony_ci std::istringstream iss(line); 9300600bfbSopenharmony_ci std::vector<std::string> tokens; 9400600bfbSopenharmony_ci std::string token; 9500600bfbSopenharmony_ci while (iss >> token) { 9600600bfbSopenharmony_ci tokens.push_back(token); 9700600bfbSopenharmony_ci } 9800600bfbSopenharmony_ci if (index < tokens.size()) { 9900600bfbSopenharmony_ci return tokens[index]; 10000600bfbSopenharmony_ci } 10100600bfbSopenharmony_ci return ""; 10200600bfbSopenharmony_ci} 10300600bfbSopenharmony_ci} // namespace HiviewDFX 10400600bfbSopenharmony_ci} // namespace OHOS 105