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 #include <memory>
16 #include <string>
17 #include <vector>
18
19 #include "tracked_command.h"
20 #include "tracked_command_test.h"
21
22 using namespace testing::ext;
23 using namespace std;
24 using namespace OHOS::HiviewDFX;
25 namespace OHOS {
26 namespace Developtools {
27 namespace HiPerf {
28 class TrackedCommandTest : public testing::Test {
29 public:
30 static void SetUpTestCase(void);
31 static void TearDownTestCase(void);
32 void SetUp();
33 void TearDown();
34
35 const vector<string> cmd_ {"ls"};
36 const vector<string> cmdNotExist_ {"not_a_command"};
37 };
38
SetUpTestCase()39 void TrackedCommandTest::SetUpTestCase() {}
40
TearDownTestCase()41 void TrackedCommandTest::TearDownTestCase() {}
42
SetUp()43 void TrackedCommandTest::SetUp() {}
44
TearDown()45 void TrackedCommandTest::TearDown() {}
46
47 HWTEST_F(TrackedCommandTest, CommandExisted, TestSize.Level1)
48 {
49 unique_ptr<TrackedCommand> trackedCMD = TrackedCommand::CreateInstance(cmd_);
50 ASSERT_NE(trackedCMD, nullptr);
51 auto cmdName = trackedCMD->GetCommandName();
52 EXPECT_EQ(cmdName, cmd_[0]);
53 auto state = trackedCMD->GetState();
54 EXPECT_EQ(state, TrackedCommand::State::COMMAND_WAITING);
55 auto pid = trackedCMD->GetChildPid();
56 EXPECT_NE(pid, -1);
57 bool res = trackedCMD->StartCommand();
58 EXPECT_TRUE(res);
59 state = trackedCMD->GetState();
60 EXPECT_EQ(state, TrackedCommand::State::COMMAND_STARTED);
61 int wstatus {0};
62 res = trackedCMD->WaitCommand(wstatus);
63 state = trackedCMD->GetState();
64 if (res) {
65 EXPECT_EQ(state, TrackedCommand::State::COMMAND_STOPPED);
66 } else {
67 EXPECT_NE(state, TrackedCommand::State::COMMAND_STOPPED);
68 }
69 }
70
71 HWTEST_F(TrackedCommandTest, CommandNotExisted, TestSize.Level1)
72 {
73 unique_ptr<TrackedCommand> trackedCMD = TrackedCommand::CreateInstance(cmdNotExist_);
74 ASSERT_NE(trackedCMD, nullptr);
75 auto cmdName = trackedCMD->GetCommandName();
76 EXPECT_EQ(cmdName, cmdNotExist_[0]);
77 auto state = trackedCMD->GetState();
78 EXPECT_EQ(state, TrackedCommand::State::COMMAND_WAITING);
79 auto pid = trackedCMD->GetChildPid();
80 EXPECT_NE(pid, -1);
81 bool res = trackedCMD->StartCommand();
82 EXPECT_FALSE(res);
83 state = trackedCMD->GetState();
84 EXPECT_EQ(state, TrackedCommand::State::COMMAND_FAILURE);
85 int wstatus {0};
86 res = trackedCMD->WaitCommand(wstatus);
87 state = trackedCMD->GetState();
88 if (res) {
89 EXPECT_EQ(state, TrackedCommand::State::COMMAND_STOPPED);
90 } else {
91 EXPECT_NE(state, TrackedCommand::State::COMMAND_STOPPED);
92 }
93 }
94 } // namespace HiPerf
95 } // namespace Developtools
96 } // namespace OHOS
97