1 /*
2  * Copyright (c) 2023 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 
16 #include <gtest/gtest.h>
17 
18 #define private public
19 #include "shell_command_executor.h"
20 #undef private
21 
22 using namespace testing::ext;
23 using namespace OHOS;
24 using namespace OHOS::AAFwk;
25 
26 namespace {
27 const int64_t TIMEOUT = 1;
28 const std::string CMD = "ls";
29 }  // namespace
30 
31 class ShellCommandExecutorTest : public ::testing::Test {
32 public:
33     static void SetUpTestCase();
34     static void TearDownTestCase();
35     void SetUp() override;
36     void TearDown() override;
37     std::shared_ptr<ShellCommandExecutor> Command_;
38 };
39 
SetUpTestCase()40 void ShellCommandExecutorTest::SetUpTestCase()
41 {}
42 
TearDownTestCase()43 void ShellCommandExecutorTest::TearDownTestCase()
44 {}
45 
SetUp()46 void ShellCommandExecutorTest::SetUp()
47 {
48     Command_ = std::make_shared<ShellCommandExecutor>(CMD, TIMEOUT);
49 }
50 
TearDown()51 void ShellCommandExecutorTest::TearDown()
52 {}
53 
54 /**
55  * @tc.number: ShellCommandExecutor_DoWork_0100
56  * @tc.name: DoWork
57  * @tc.desc: Verify cmd_ When it is null, calling DoWork function returns false.
58  */
59 HWTEST_F(ShellCommandExecutorTest, ShellCommandExecutor_DoWork_0100, TestSize.Level1)
60 {
61     GTEST_LOG_(INFO) << "ShellCommandExecutor_DoWork_0100 start";
62     Command_->cmd_ = "";
63     EXPECT_EQ(Command_->DoWork(), false);
64     GTEST_LOG_(INFO) << "ShellCommandExecutor_DoWork_0100 end";
65 }
66 
67 /**
68  * @tc.number: ShellCommandExecutor_DoWork_0200
69  * @tc.name: DoWork
70  * @tc.desc: Verify the handler_ When it is nullptr, calling DoWork function returns false.
71  */
72 HWTEST_F(ShellCommandExecutorTest, ShellCommandExecutor_DoWork_0200, TestSize.Level1)
73 {
74     GTEST_LOG_(INFO) << "ShellCommandExecutor_DoWork_0200 start";
75     Command_->handler_ = nullptr;
76     EXPECT_EQ(Command_->DoWork(), false);
77     GTEST_LOG_(INFO) << "ShellCommandExecutor_DoWork_0200 end";
78 }
79 
80 /**
81  * @tc.number: ShellCommandExecutor_DoWork_0300
82  * @tc.name: DoWork
83  * @tc.desc: Verify that PostTask conditions are met and cmd_ Not empty, call DoWork function branch to overwrite.
84  */
85 HWTEST_F(ShellCommandExecutorTest, ShellCommandExecutor_DoWork_0300, TestSize.Level1)
86 {
87     GTEST_LOG_(INFO) << "ShellCommandExecutor_DoWork_0300 start";
88     std::shared_ptr<AppExecFwk::EventHandler> handler_ = std::make_shared<AppExecFwk::EventHandler>();
89     ASSERT_NE(handler_, nullptr);
__anonf6e31a290202() 90     auto task = []() { GTEST_LOG_(INFO) << "ShellCommandExecutor_DoWork_0300 task called"; };
91     handler_->PostTask(task, 1000);
92     Command_->cmd_ = CMD;
93     GTEST_LOG_(INFO) << "ShellCommandExecutor_DoWork_0300 end";
94 }
95 
96 /**
97  * @tc.number: ShellCommandExecutor_DoWork_0400
98  * @tc.name: DoWork
99  * @tc.desc: Verify that PostTask conditions are met and cmd_ Exception, calling DoWork function branch
100  *           failed to reach coverage.
101  */
102 HWTEST_F(ShellCommandExecutorTest, ShellCommandExecutor_DoWork_0400, TestSize.Level1)
103 {
104     GTEST_LOG_(INFO) << "ShellCommandExecutor_DoWork_0400 start";
105     std::shared_ptr<AppExecFwk::EventHandler> handler_ = std::make_shared<AppExecFwk::EventHandler>();
106     ASSERT_NE(handler_, nullptr);
__anonf6e31a290302() 107     auto task = []() { GTEST_LOG_(INFO) << "ShellCommandExecutor_DoWork_0400 task called"; };
108     handler_->PostTask(task, 1000);
109     Command_->cmd_ = "CMD12";
110     GTEST_LOG_(INFO) << "ShellCommandExecutor_DoWork_0400 end";
111 }
112 
113 /**
114  * @tc.number: ShellCommandExecutor_WaitWorkDone_0100
115  * @tc.name: WaitWorkDone
116  * @tc.desc: Verify that timeoutSec is met_ Condition, call WaitWorkDone function branch to reach coverage.
117  */
118 HWTEST_F(ShellCommandExecutorTest, ShellCommandExecutor_WaitWorkDone_0100, TestSize.Level1)
119 {
120     GTEST_LOG_(INFO) << "ShellCommandExecutor_WaitWorkDone_0100 start";
121     ASSERT_NE(Command_, nullptr);
122     Command_->timeoutSec_ = -1;
123     Command_->cmdResult_.exitCode = 0;
124     Command_->WaitWorkDone();
125     Command_->DoWork();
126     GTEST_LOG_(INFO) << "ShellCommandExecutor_WaitWorkDone_0100 end";
127 }