1 /*
2  * Copyright (c) 2021-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 #include <thread>
19 #include "bundle_command.h"
20 #include "tool_system_test.h"
21 
22 using namespace testing::ext;
23 using namespace OHOS;
24 using namespace OHOS::AppExecFwk;
25 
26 namespace OHOS {
27 namespace {
28 const std::string STRING_BUNDLE_PATH = "/data/test/resource/bm/pageAbilityBundleForUninstall.hap";
29 const std::string STRING_BUNDLE_NAME = "com.ohos.tools.pageAbilityBundleForUninstall";
30 const std::string STRING_BUNDLE_NAME_INVALID = STRING_BUNDLE_NAME + ".invalid";
31 const std::string UNINSTALL_FALSE = "error: uninstall missing installed bundle.";
32 const std::string ERR_CODE = "code:9568386\n";
33 }  // namespace
34 
35 class BmCommandUninstallSystemTest : public ::testing::Test {
36 public:
37     static void SetUpTestCase();
38     static void TearDownTestCase();
39     void SetUp() override;
40     void TearDown() override;
41 };
42 
SetUpTestCase()43 void BmCommandUninstallSystemTest::SetUpTestCase()
44 {}
45 
TearDownTestCase()46 void BmCommandUninstallSystemTest::TearDownTestCase()
47 {}
48 
SetUp()49 void BmCommandUninstallSystemTest::SetUp()
50 {
51     // reset optind to 0
52     optind = 0;
53 }
54 
TearDown()55 void BmCommandUninstallSystemTest::TearDown()
56 {}
57 
58 /**
59  * @tc.number: Bm_Command_Uninstall_SystemTest_0100
60  * @tc.name: ExecCommand
61  * @tc.desc: Verify the "bm uninstall -n <bundle-name>" command.
62  */
63 HWTEST_F(BmCommandUninstallSystemTest, Bm_Command_Uninstall_SystemTest_0100, Function | MediumTest | Level1)
64 {
65     // uninstall the bundle
66     ToolSystemTest::UninstallBundle(STRING_BUNDLE_NAME);
67 
68     // install the bundle
69     ToolSystemTest::InstallBundle(STRING_BUNDLE_PATH, true);
70 
71     // uninstall the bundle
72     ToolSystemTest::UninstallBundle(STRING_BUNDLE_NAME);
73 }
74 
75 /**
76  * @tc.number: Bm_Command_Uninstall_SystemTest_0200
77  * @tc.name: ExecCommand
78  * @tc.desc: Verify the "bm uninstall -n <bundle-name>" command.
79  */
80 HWTEST_F(BmCommandUninstallSystemTest, Bm_Command_Uninstall_SystemTest_0200, Function | MediumTest | Level1)
81 {
82     // uninstall an invalid bundle
83     std::string command = "bm uninstall -n " + STRING_BUNDLE_NAME_INVALID;
84     std::string commandResult = ToolSystemTest::ExecuteCommand(command);
85 
86     EXPECT_EQ(commandResult, STRING_UNINSTALL_BUNDLE_NG + "\n" + ERR_CODE + UNINSTALL_FALSE + "\n");
87 }
88 } // OHOS
89