1 /*
2  * Copyright (c) 2021 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 protected public
19 #include "ability_command.h"
20 #undef protected
21 #include "mock_ability_manager_stub.h"
22 #define private public
23 #include "ability_manager_client.h"
24 #undef private
25 #include "ability_manager_interface.h"
26 
27 using namespace testing::ext;
28 using namespace OHOS;
29 using namespace OHOS::AAFwk;
30 
31 namespace {
32 const std::string STRING_DEVICE = "device";
33 const std::string STRING_ABILITY_NAME = "ability";
34 const std::string STRING_ABILITY_NAME_INVALID = "invalid_ability";
35 const std::string STRING_BUNDLE_NAME = "bundle";
36 const std::string STRING_BUNDLE_NAME_INVALID = "invalid_bundle";
37 const std::string STRING_RECORD_ID = "1024";
38 const std::string STRING_RECORD_ID_INVALID = "2048";
39 const std::string STRING_STATE_ON = "on";
40 const std::string STRING_STATE_ON_INVALID = "invalid_on";
41 const std::string STRING_STATE_OFF = "off";
42 const std::string STRING_STATE_OFF_INVALID = "invalid_off";
43 }  // namespace
44 
45 class AaCommandStopServiceModuleTest : public ::testing::Test {
46 public:
47     static void SetUpTestCase();
48     static void TearDownTestCase();
49     void SetUp() override;
50     void TearDown() override;
51 
52     void MakeMockObjects() const;
53 
54     std::string cmd_ = "stop-service";
55 };
56 
SetUpTestCase()57 void AaCommandStopServiceModuleTest::SetUpTestCase()
58 {}
59 
TearDownTestCase()60 void AaCommandStopServiceModuleTest::TearDownTestCase()
61 {}
62 
SetUp()63 void AaCommandStopServiceModuleTest::SetUp()
64 {
65     // reset optind to 0
66     optind = 0;
67 
68     // make mock objects
69     MakeMockObjects();
70 }
71 
TearDown()72 void AaCommandStopServiceModuleTest::TearDown()
73 {}
74 
MakeMockObjects() const75 void AaCommandStopServiceModuleTest::MakeMockObjects() const
76 {
77     // mock a stub
78     auto managerStubPtr = sptr<IAbilityManager>(new MockAbilityManagerStub());
79 
80     // set the mock stub
81     auto managerClientPtr = AbilityManagerClient::GetInstance();
82     managerClientPtr->proxy_ = managerStubPtr;
83 }
84 
85 /**
86  * @tc.number: Aa_Command_StopService_ModuleTest_0100
87  * @tc.name: ExecCommand
88  * @tc.desc: Verify the "aa stop-service -d <device-id> -a <ability-name> -b <bundle-name>" command.
89  */
90 HWTEST_F(AaCommandStopServiceModuleTest, Aa_Command_StopService_ModuleTest_0100, Function | MediumTest | Level1)
91 {
92     char* argv[] = {
93         (char*)TOOL_NAME.c_str(),
94         (char*)cmd_.c_str(),
95         (char*)"-d",
96         (char*)STRING_DEVICE.c_str(),
97         (char*)"-a",
98         (char*)STRING_ABILITY_NAME.c_str(),
99         (char*)"-b",
100         (char*)STRING_BUNDLE_NAME.c_str(),
101         (char*)"",
102     };
103     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
104 
105     AbilityManagerShellCommand cmd(argc, argv);
106     EXPECT_EQ(cmd.ExecCommand(), STRING_STOP_SERVICE_ABILITY_OK + "\n");
107 }
108 
109 /**
110  * @tc.number: Aa_Command_StopService_ModuleTest_0200
111  * @tc.name: ExecCommand
112  * @tc.desc: Verify the "aa stop-service -d <device-id> -a <ability-name> -b <bundle-name>" command.
113  */
114 HWTEST_F(AaCommandStopServiceModuleTest, Aa_Command_StopService_ModuleTest_0200, Function | MediumTest | Level1)
115 {
116     char* argv[] = {
117         (char*)TOOL_NAME.c_str(),
118         (char*)cmd_.c_str(),
119         (char*)"-d",
120         (char*)STRING_DEVICE.c_str(),
121         (char*)"-a",
122         (char*)STRING_ABILITY_NAME_INVALID.c_str(),
123         (char*)"-b",
124         (char*)STRING_BUNDLE_NAME.c_str(),
125         (char*)"",
126     };
127     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
128 
129     AbilityManagerShellCommand cmd(argc, argv);
130     EXPECT_EQ(
131         cmd.ExecCommand(), STRING_STOP_SERVICE_ABILITY_NG + "\n" + cmd.messageMap_.at(RESOLVE_ABILITY_ERR) + "\n");
132 }
133 
134 /**
135  * @tc.number: Aa_Command_StopService_ModuleTest_0300
136  * @tc.name: ExecCommand
137  * @tc.desc: Verify the "aa stop-service -d <device-id> -a <ability-name> -b <bundle-name>" command.
138  */
139 HWTEST_F(AaCommandStopServiceModuleTest, Aa_Command_StopService_ModuleTest_0300, Function | MediumTest | Level1)
140 {
141     char* argv[] = {
142         (char*)TOOL_NAME.c_str(),
143         (char*)cmd_.c_str(),
144         (char*)"-d",
145         (char*)STRING_DEVICE.c_str(),
146         (char*)"-a",
147         (char*)STRING_ABILITY_NAME.c_str(),
148         (char*)"-b",
149         (char*)STRING_BUNDLE_NAME_INVALID.c_str(),
150         (char*)"",
151     };
152     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
153 
154     AbilityManagerShellCommand cmd(argc, argv);
155     EXPECT_EQ(cmd.ExecCommand(), STRING_STOP_SERVICE_ABILITY_NG + "\n" + cmd.messageMap_.at(RESOLVE_APP_ERR) + "\n");
156 }
157