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