1 /*
2  * Copyright (c) 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 
16 #include <gtest/gtest.h>
17 #include <gmock/gmock.h>
18 
19 #define protected public
20 #include "ability_command.h"
21 #undef protected
22 #include "mock_ability_manager_stub.h"
23 #define private public
24 #include "ability_manager_client.h"
25 #undef private
26 #include "ability_manager_interface.h"
27 
28 using namespace testing::ext;
29 using namespace OHOS;
30 using namespace OHOS::AAFwk;
31 using testing::_;
32 using testing::Invoke;
33 using testing::Return;
34 
35 namespace {
36 const std::string STRING_BUNDLE_NAME = "bundle";
37 } // namespace
38 
39 class AaCommandForceStopTest : public ::testing::Test {
40 public:
41     static void SetUpTestCase();
42     static void TearDownTestCase();
43     void SetUp() override;
44     void TearDown() override;
45 
46     void MakeMockObjects() const;
47 
48     std::string cmd_ = "force-stop";
49 };
50 
SetUpTestCase()51 void AaCommandForceStopTest::SetUpTestCase()
52 {}
53 
TearDownTestCase()54 void AaCommandForceStopTest::TearDownTestCase()
55 {}
56 
SetUp()57 void AaCommandForceStopTest::SetUp()
58 {
59     // reset optind to 0
60     optind = 0;
61 
62     // make mock objects
63     MakeMockObjects();
64 }
65 
TearDown()66 void AaCommandForceStopTest::TearDown()
67 {}
68 
MakeMockObjects() const69 void AaCommandForceStopTest::MakeMockObjects() const
70 {
71     // mock a stub
72     auto managerStubPtr = sptr<IAbilityManager>(new MockAbilityManagerStub());
73 
74     // set the mock stub
75     auto managerClientPtr = AbilityManagerClient::GetInstance();
76     managerClientPtr->proxy_ = managerStubPtr;
77 }
78 
79 /**
80  * @tc.number: Aa_Command_Force_Stop_0100
81  * @tc.name: ExecCommand
82  * @tc.desc: Verify the "aa force-stop" command.
83  */
84 HWTEST_F(AaCommandForceStopTest, Aa_Command_Force_Stop_0100, Function | MediumTest | Level1)
85 {
86     char* argv[] = {
87         (char*)TOOL_NAME.c_str(),
88         (char*)cmd_.c_str(),
89         (char*)"",
90     };
91     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
92 
93     AbilityManagerShellCommand cmd(argc, argv);
94     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_FORCE_STOP);
95 }
96 
97 /**
98  * @tc.number: Aa_Command_Force_Stop_0200
99  * @tc.name: ExecCommand
100  * @tc.desc: Verify the "aa force-stop xxx" command.
101  */
102 HWTEST_F(AaCommandForceStopTest, Aa_Command_Force_Stop_0200, Function | MediumTest | Level1)
103 {
104     char* argv[] = {
105         (char*)TOOL_NAME.c_str(),
106         (char*)cmd_.c_str(),
107         (char*)"xxx",
108         (char*)"",
109     };
110     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
111     AbilityManagerShellCommand cmd(argc, argv);
112 
113     auto managerClientPtr = AbilityManagerClient::GetInstance();
114     auto mockAbilityManagerStub = sptr<MockAbilityManagerStub>(new MockAbilityManagerStub());
115     ASSERT_NE(mockAbilityManagerStub, nullptr);
116     EXPECT_CALL(*mockAbilityManagerStub, KillProcess(_, _))
117         .Times(1)
118         .WillOnce(Return(-1));
119     managerClientPtr->proxy_ = static_cast<IAbilityManager*>(mockAbilityManagerStub);
120 
121     EXPECT_EQ(cmd.ExecCommand(), STRING_FORCE_STOP_NG + "\n");
122     testing::Mock::AllowLeak(mockAbilityManagerStub);
123 }
124 
125 /**
126  * @tc.number: Aa_Command_Force_Stop_0300
127  * @tc.name: ExecCommand
128  * @tc.desc: Verify the "aa force-stop bundle" command.
129  */
130 HWTEST_F(AaCommandForceStopTest, Aa_Command_Force_Stop_0300, Function | MediumTest | Level1)
131 {
132     char* argv[] = {
133         (char*)TOOL_NAME.c_str(),
134         (char*)cmd_.c_str(),
135         (char*)"STRING_BUNDLE_NAME",
136         (char*)"",
137     };
138     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
139     AbilityManagerShellCommand cmd(argc, argv);
140 
141     auto managerClientPtr = AbilityManagerClient::GetInstance();
142     auto mockAbilityManagerStub = sptr<MockAbilityManagerStub>(new MockAbilityManagerStub());
143     ASSERT_NE(mockAbilityManagerStub, nullptr);
144     EXPECT_CALL(*mockAbilityManagerStub, KillProcess(_, _))
145         .Times(1)
146         .WillOnce(Return(0));
147     managerClientPtr->proxy_ = static_cast<IAbilityManager*>(mockAbilityManagerStub);
148 
149     EXPECT_EQ(cmd.ExecCommand(), STRING_FORCE_STOP_OK + "\n");
150     testing::Mock::AllowLeak(mockAbilityManagerStub);
151 }