1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. 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 #include <hwext/gtest-ext.h> 16 #include <hwext/gtest-tag.h> 17 #include <string> 18 19 #include "process_utils.h" 20 21 namespace { 22 using testing::ext::TestSize; 23 class ProcessUtilsTest : public ::testing::Test { 24 protected: SetUp()25 void SetUp() override {} 26 TearDown()27 void TearDown() override {} 28 FindCmd(const std::string & cmd)29 std::string FindCmd(const std::string& cmd) 30 { 31 std::vector<std::string> dirs = { 32 "/bin/", 33 "/system/bin/", 34 "/vendor/bin/", 35 }; 36 for (auto& dir : dirs) { 37 std::string path = dir + cmd; 38 if (access(path.c_str(), X_OK) == 0) { 39 return path; 40 } 41 } 42 return ""; 43 } 44 }; 45 46 /* 47 * @tc.name: ExecuteNormal 48 * @tc.desc: test ProcessUtils::Execute with normal case. 49 * @tc.type: FUNC 50 */ 51 HWTEST_F(ProcessUtilsTest, ExecuteNormal, TestSize.Level1) 52 { 53 ExecuteArgs args; 54 args.bin_ = FindCmd("ls"); 55 args.argv_ = {"ls", "--color"}; 56 args.out2pipe_ = true; 57 args.err2pipe_ = true; 58 59 std::string output; 60 EXPECT_EQ(ProcessUtils::Execute(args, output), 0); 61 EXPECT_NE(output, ""); 62 } 63 } // namespace 64