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 private public
19 #include "bundle_command.h"
20 #undef private
21 #include "bundle_constants.h"
22 #include "bundle_installer_interface.h"
23 #include "iremote_broker.h"
24 #include "iremote_object.h"
25 #include "mock_bundle_installer_host.h"
26 #include "mock_bundle_mgr_host.h"
27 
28 using namespace testing::ext;
29 using namespace OHOS;
30 using namespace OHOS::AAFwk;
31 using namespace OHOS::AppExecFwk;
32 
33 namespace OHOS {
34 class BmCommandDumpModuleTest : public ::testing::Test {
35 public:
36     static void SetUpTestCase();
37     static void TearDownTestCase();
38     void SetUp() override;
39     void TearDown() override;
40 
41     void MakeMockObjects();
42     void SetMockObjects(BundleManagerShellCommand &cmd) const;
43 
44     std::string cmd_ = "dump";
45     sptr<IBundleMgr> mgrProxyPtr_;
46     sptr<IBundleInstaller> installerProxyPtr_;
47 };
48 
SetUpTestCase()49 void BmCommandDumpModuleTest::SetUpTestCase()
50 {}
51 
TearDownTestCase()52 void BmCommandDumpModuleTest::TearDownTestCase()
53 {}
54 
SetUp()55 void BmCommandDumpModuleTest::SetUp()
56 {
57     // reset optind to 0
58     optind = 0;
59 
60     // make mock objects
61     MakeMockObjects();
62 }
63 
TearDown()64 void BmCommandDumpModuleTest::TearDown()
65 {}
66 
MakeMockObjects()67 void BmCommandDumpModuleTest::MakeMockObjects()
68 {
69     // mock a mgr host
70     auto mgrHostPtr = sptr<IRemoteObject>(new (std::nothrow) MockBundleMgrHost());
71     // mock a mgr proxy
72     mgrProxyPtr_ = iface_cast<IBundleMgr>(mgrHostPtr);
73 
74     // mock a installer host
75     auto installerHostPtr = sptr<IRemoteObject>(new (std::nothrow) MockBundleInstallerHost());
76     // mock a installer proxy
77     installerProxyPtr_ = iface_cast<IBundleInstaller>(installerHostPtr);
78 }
79 
SetMockObjects(BundleManagerShellCommand & cmd) const80 void BmCommandDumpModuleTest::SetMockObjects(BundleManagerShellCommand &cmd) const
81 {
82     // set the mock mgr proxy
83     cmd.bundleMgrProxy_ = mgrProxyPtr_;
84 
85     // set the mock installer proxy
86     cmd.bundleInstallerProxy_ = installerProxyPtr_;
87 }
88 
89 /**
90  * @tc.number: Bm_Command_Dump_ModuleTest_0100
91  * @tc.name: ExecCommand
92  * @tc.desc: Verify the "bm dump -a" command.
93  */
94 HWTEST_F(BmCommandDumpModuleTest, Bm_Command_Dump_ModuleTest_0100, Function | MediumTest | Level1)
95 {
96     // install a bundle
97     char *argv[] = {
98         const_cast<char *>(TOOL_NAME.c_str()),
99         const_cast<char *>(cmd_.c_str()),
100         const_cast<char *>("-a"),
101         const_cast<char *>(""),
102     };
103     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
104 
105     BundleManagerShellCommand cmd(argc, argv);
106 
107     // set the mock objects
108     SetMockObjects(cmd);
109 
110     EXPECT_EQ(cmd.ExecCommand(), "OK");
111 }
112 
113 /**
114  * @tc.number: Bm_Command_Dump_ModuleTest_0200
115  * @tc.name: ExecCommand
116  * @tc.desc: Verify the "bm dump --all" command.
117  */
118 HWTEST_F(BmCommandDumpModuleTest, Bm_Command_Dump_ModuleTest_0200, Function | MediumTest | Level1)
119 {
120     // install a bundle
121     char *argv[] = {
122         const_cast<char *>(TOOL_NAME.c_str()),
123         const_cast<char *>(cmd_.c_str()),
124         const_cast<char *>("--all"),
125         const_cast<char *>(""),
126     };
127     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
128 
129     BundleManagerShellCommand cmd(argc, argv);
130 
131     // set the mock objects
132     SetMockObjects(cmd);
133 
134     EXPECT_EQ(cmd.ExecCommand(), "OK");
135 }
136 } // OHOS
137