/* * Copyright (c) 2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #define private public #include "bundle_command.h" #undef private #include "bundle_constants.h" #include "bundle_installer_interface.h" #include "iremote_broker.h" #include "iremote_object.h" #include "mock_bundle_installer_host.h" #include "mock_bundle_mgr_host.h" using namespace testing::ext; using namespace OHOS; using namespace OHOS::AAFwk; using namespace OHOS::AppExecFwk; namespace OHOS { class BmCommandInstallModuleTest : public ::testing::Test { public: static void SetUpTestCase(); static void TearDownTestCase(); void SetUp() override; void TearDown() override; void MakeMockObjects(); void SetMockObjects(BundleManagerShellCommand &cmd) const; std::string cmd_ = "install"; sptr mgrProxyPtr_; sptr installerProxyPtr_; }; void BmCommandInstallModuleTest::SetUpTestCase() {} void BmCommandInstallModuleTest::TearDownTestCase() {} void BmCommandInstallModuleTest::SetUp() { // reset optind to 0 optind = 0; // make mock objects MakeMockObjects(); } void BmCommandInstallModuleTest::TearDown() {} void BmCommandInstallModuleTest::MakeMockObjects() { // mock a mgr host auto mgrHostPtr = sptr(new (std::nothrow) MockBundleMgrHost()); // mock a mgr proxy mgrProxyPtr_ = iface_cast(mgrHostPtr); // mock a installer host auto installerHostPtr = sptr(new (std::nothrow) MockBundleInstallerHost()); // mock a installer proxy installerProxyPtr_ = iface_cast(installerHostPtr); } void BmCommandInstallModuleTest::SetMockObjects(BundleManagerShellCommand &cmd) const { // set the mock mgr proxy cmd.bundleMgrProxy_ = mgrProxyPtr_; // set the mock installer proxy cmd.bundleInstallerProxy_ = installerProxyPtr_; } /** * @tc.number: Bm_Command_Install_ModuleTest_0100 * @tc.name: ExecCommand * @tc.desc: Verify the "bm install -p " command. */ HWTEST_F(BmCommandInstallModuleTest, Bm_Command_Install_ModuleTest_0100, Function | MediumTest | Level1) { // install a bundle char *argv[] = { const_cast(TOOL_NAME.c_str()), const_cast(cmd_.c_str()), const_cast("-p"), const_cast(STRING_BUNDLE_PATH.c_str()), const_cast(""), }; int argc = sizeof(argv) / sizeof(argv[0]) - 1; BundleManagerShellCommand cmd(argc, argv); // set the mock objects SetMockObjects(cmd); EXPECT_EQ(cmd.ExecCommand(), STRING_INSTALL_BUNDLE_OK + "\n"); } /** * @tc.number: Bm_Command_Install_ModuleTest_0200 * @tc.name: ExecCommand * @tc.desc: Verify the "bm install -p -r" command. */ HWTEST_F(BmCommandInstallModuleTest, Bm_Command_Install_ModuleTest_0200, Function | MediumTest | Level1) { // install a bundle char *argv[] = { const_cast(TOOL_NAME.c_str()), const_cast(cmd_.c_str()), const_cast("-p"), const_cast(STRING_BUNDLE_PATH.c_str()), const_cast("-r"), const_cast(""), }; int argc = sizeof(argv) / sizeof(argv[0]) - 1; BundleManagerShellCommand cmd(argc, argv); // set the mock objects SetMockObjects(cmd); EXPECT_EQ(cmd.ExecCommand(), STRING_INSTALL_BUNDLE_OK + "\n"); } } // OHOS