diff --git a/services/native/driver_extension_manager/include/driver_controller/driver_extension_controller.h b/services/native/driver_extension_manager/include/driver_controller/driver_extension_controller.h new file mode 100644 index 0000000000000000000000000000000000000000..8862658f547fd575f807d34a9ccf5dfbd47d56aa --- /dev/null +++ b/services/native/driver_extension_manager/include/driver_controller/driver_extension_controller.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 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. + */ + +#ifndef DRIVER_EXTENSION_CONTROLLER_H +#define DRIVER_EXTENSION_CONTROLLER_H +#include +namespace OHOS { +namespace ExternalDeviceManager { +class DriverExtensionController { +public: + static uint32_t StartDriverExtension( + std::string baudleName, + std::string abilityName); + static uint32_t StopDriverExtension( + std::string bundleName, + std::string abilityName); +}; +} +} +#endif \ No newline at end of file diff --git a/services/native/driver_extension_manager/src/driver_controller/driver_extension_controller.cpp b/services/native/driver_extension_manager/src/driver_controller/driver_extension_controller.cpp new file mode 100644 index 0000000000000000000000000000000000000000..63bad620392fa7f9f02f33a7a718d5bf671afe26 --- /dev/null +++ b/services/native/driver_extension_manager/src/driver_controller/driver_extension_controller.cpp @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2023 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 "ability_manager_client.h" +#include "hilog_wrapper.h" +#include "driver_extension_controller.h" +using namespace OHOS; +using namespace OHOS::ExternalDeviceManager; +static const uint32_t UEC_OK = 0 + +int DriverExtensionController::StartDriverExtension( + std::string bundleName, + std::string abilityName) +{ + EDM_LOGI("Begin to start DriverExtension, bundle:%{public}s, ability:%{public}s", \ + bundleName.c_str(), abilityName.c_str()); + auto abmc = AAFwk::AbilityManagerClient::GetInstance(); + if (abmc == nullptr) { + EDM_LOGE("Get AMC Instance failed"); + return -1; + } + AAFwk::Want want; + want.SetElementName(bundleName, abilityName); + + auto ret = abmc->StartExtensionAbility(want, nullptr); + if (ret != UEC_OK) { + EDM_LOGE("StartExtensionAbility failed %{public}d", ret); + return ret; + } + EDM_LOGI("StartExtensionAbility success"); + return 0; +} + + +int DriverExtensionController::StopDriverExtension( + std::string bundleName, + std::string abilityName) +{ + EDM_LOGI("Begin to stop DriverExtension, bundle:%{public}s, ability:%{public}s", \ + bundleName.c_str(), abilityName.c_str()); + auto abmc = AAFwk::AbilityManagerClient::GetInstance(); + if (abmc == nullptr) { + EDM_LOGE("Get AMC Instance failed"); + return -1; + } + AAFwk::Want want; + want.SetElementName(bundleName, abilityName); + auto ret = abmc->StopExtensionAbility(want, nullptr); + if (ret != UEC_OK) { + EDM_LOGE("StopExtensionAbility failed %{public}d", ret); + return ret; + } + EDM_LOGI("StopExtensionAbility success"); + return 0; +} \ No newline at end of file diff --git a/test/unittest/driver_extension_controller_test/src/driver_extension_controller_test.cpp b/test/unittest/driver_extension_controller_test/src/driver_extension_controller_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..750d4fa301ff71203a234232f2b1bba1f5ef25bc --- /dev/null +++ b/test/unittest/driver_extension_controller_test/src/driver_extension_controller_test.cpp @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2023 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 "gtest/gtest.h" +#include "iostream" +#include "string_ex.h" +#include "iservice_registry.h" +#include "accesstoken_kit.h" +#include "token_setproc.h" +#include "nativetoken_kit.h" +#include "hilog_wrapper.h" +#include "driver_extension_controller.h" + +namespace OHOS { +namespace ExternalDeviceManager { +using namespace OHOS; +using namespace std; +using namespace testing::ext; +class DrvExtCtrlTest : public testing::Test { +public: + void SetUp() override + { + cout << "DrvExtCtrlTest SetUp" << endl; + } + void TearDown() override + { + cout << "DrvExtCtrlTest TearDown" << endl; + } +}; + +const char *TEST_BUNDLE_NAME "com.usb.right.MainAbility" +const char *TEST_ABILITY_NAME "UsbServiceExtAbility" +const char *TEST_ABILITY_NAME_ERR "XXX" + +using namespace OHOS::Security::AccessToken; + +TokenInfoParams g_normalInfoInstance = { + .dcapsNum = 0, + .permsNum = 0, + .aclsNum = 0, + .dcaps = nullptr, + .perms = nullptr, + .acls = nullptr, + .processName = "usb_manager", + .aplStr = "normal", +}; + +TokenInfoParams g_sysInfoInstance = { + .dcapsNum = 0, + .permsNum = 0, + .aclsNum = 0, + .dcaps = nullptr, + .perms = nullptr, + .acls = nullptr, + .processName = "usb_manager", + .aplStr = "system_basic", +}; + +class AccessTokenTest { +public: + static void SetTestCaseNative (TokenInfoParams *infoInstance) + { + uint64_t tokenId = GetAccessTokenId(infoInstance); + int ret = SetSelfTokenID(tokenId); + if (ret == 0) { + EDM_LOGI("SetSelfTokenID success"); + } else { + EDM_LOGE("SetSelfTokenID fail"); + } + AccessTokenKit::ReloadNativeTokenInfo(); + } +}; + +HWTEST_F(DrvExtCtrlTest, DrvExtCtrlWithSATest, TestSize.Level1) +{ + AccessTokenTest::SetTestCaseNative(&g_sysInfoInstance); + int ret = 0; + ret = DriverExtensionController::StartDriverExtension(TEST_BUNDLE_NAME, TEST_ABILITY_NAME); + ASSERT_EQ(ret, 0); + ret = DriverExtensionController::StopDriverExtension(TEST_BUNDLE_NAME, TEST_ABILITY_NAME); + ASSERT_EQ(ret, 0); +} + +HWTEST_F(DrvExtCtrlTest, DrvExtCtrlWithoutSATest, TestSize.Level1) +{ + AccessTokenTest::SetTestCaseNative(&g_normalInfoInstance); + int ret = 0; + ret = DriverExtensionController::StartDriverExtension(TEST_BUNDLE_NAME, TEST_ABILITY_NAME_ERR); + ASSERT_NE(ret, 0); + ret = DriverExtensionController::StopDriverExtension(TEST_BUNDLE_NAME, TEST_ABILITY_NAME_ERR); + ASSERT_NE(ret, 0); +} +} +} \ No newline at end of file