add drivers_pkg_manager_test/pkg_db_helper_test

Signed-off-by: leitianyu <leitianyu1@huawei.com>
This commit is contained in:
leitianyu 2024-02-06 10:13:11 +08:00
parent e8558717e8
commit e8b1e40355
6 changed files with 550 additions and 2 deletions

View File

@ -1,4 +1,4 @@
# Copyright (c) 2021-2022 Huawei Device Co., Ltd.
# Copyright (c) Huawei Technologies Co., Ltd. 2023-2024. aLL right reserved.
# 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
@ -22,6 +22,7 @@ group("HatsHdfExternalDeviceTest") {
} else {
deps = [
"bus_extension_usb:HatsHdfExternalBusUsbTest",
"ddk_permission_test:HatsHdfExternalDdkPermissionTest",
"device_manager_test:HatsHdfExternalDeviceManagerTest",
"driver_extension_context:HatsHdfExtensionContextTest",
"driver_extension_controller:HatsHdfExternalControllerTest",

View File

@ -0,0 +1,51 @@
# Copyright (c) Huawei Technologies Co., Ltd. 2023-2024. aLL right reserved.
# 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.
import("//build/ohos_var.gni")
import("//drivers/external_device_manager/extdevmgr.gni")
import("//test/xts/tools/build/suite.gni")
module_output_path = "hats/ddk_permission_test"
ohos_moduletest_suite("HatsHdfExternalDdkPermissionTest") {
module_out_path = module_output_path
sources = [
"${ext_mgr_path}/frameworks/ddk/usb/usb_config_desc_parser.cpp",
"${ext_mgr_path}/frameworks/ddk/usb/usb_ddk_api.cpp",
"ddk_permission_test.cpp",
]
include_dirs = [
"${ext_mgr_path}/frameworks/ddk/usb",
"${ext_mgr_path}/interfaces/ddk/usb",
"${ext_mgr_path}/interfaces/innerkits/",
"${ext_mgr_path}/services/zidl/include/",
"${utils_path}/include/",
]
deps = [
"${ext_mgr_path}/services/native/driver_extension_manager/src/driver_permission:driver_permission_manager",
"//third_party/googletest:gtest_main",
]
external_deps = [
"bundle_framework:appexecfwk_base",
"bundle_framework:appexecfwk_core",
"c_utils:utils",
"drivers_interface_usb:libusb_ddk_proxy_1.0",
"hilog:libhilog",
"ipc:ipc_core",
]
subsystem_name = "xts"
part_name = "hats"
}

View File

@ -0,0 +1,18 @@
{
"kits": [
{
"push": [
"HatsHdfExternalDdkPermissionTest->/data/local/tmp/HatsHdfExternalDdkPermissionTest"
],
"type": "PushKit"
}
],
"driver": {
"native-test-timeout": "120000",
"type": "CppTest",
"module-name": "HatsHdfExternalDdkPermissionTest",
"runtime-hint": "1s",
"native-test-device-path": "/data/local/tmp"
},
"description": "Configuration for HatsHdfExternalDdkPermissionTest Tests"
}

View File

@ -0,0 +1,208 @@
/*
* Copyright (c) Huawei Technologies Co., Ltd. 2023-2024. aLL right reserved.
* 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 "usb_config_desc_parser.h"
#include "usb_ddk_api.h"
#include "usb_ddk_types.h"
using namespace testing::ext;
namespace {
bool g_state = false;
void GrantPermission(void)
{
g_state = true;
}
void RevokePermission(void)
{
g_state = false;
}
class DdkPermissionTest : public testing::Test {
public:
void SetUp() override;
void TearDown() override;
};
} // namespace
namespace OHOS {
namespace ExternalDeviceManager {
class ExtPermissionManager {
static ExtPermissionManager& GetInstance();
bool HasPermission(std::string permissionName);
};
ExtPermissionManager& ExtPermissionManager::GetInstance()
{
static auto instance = new ExtPermissionManager();
return *instance;
}
// required permissions are not yet implemented, mock this method so that the tests may pass.
bool ExtPermissionManager::HasPermission(std::string permissionName)
{
return g_state;
}
} // namespace Security
} // namespace OHOS
void DdkPermissionTest::SetUp()
{
RevokePermission();
int32_t ret = OH_Usb_Init();
EXPECT_NE(ret, 0);
GrantPermission();
ret = OH_Usb_Init();
EXPECT_EQ(ret, 0);
}
void DdkPermissionTest::TearDown()
{
RevokePermission();
OH_Usb_Release();
GrantPermission();
int32_t ret = OH_Usb_GetDeviceDescriptor(0, nullptr);
EXPECT_EQ(ret, USB_DDK_INVALID_PARAMETER);
OH_Usb_Release();
ret = OH_Usb_GetDeviceDescriptor(0, nullptr);
EXPECT_EQ(ret, USB_DDK_INVALID_OPERATION);
}
namespace {
HWTEST_F(DdkPermissionTest, OH_Usb_GetDeviceDescriptor_001, TestSize.Level1)
{
int32_t ret = OH_Usb_GetDeviceDescriptor(0, nullptr);
EXPECT_EQ(ret, USB_DDK_INVALID_PARAMETER);
}
HWTEST_F(DdkPermissionTest, OH_Usb_GetDeviceDescriptor_002, TestSize.Level1)
{
RevokePermission();
int32_t ret = OH_Usb_GetDeviceDescriptor(0, nullptr);
EXPECT_EQ(ret, USB_DDK_FAILED);
}
HWTEST_F(DdkPermissionTest, OH_Usb_GetConfigDescriptor_001, TestSize.Level1)
{
int32_t ret = OH_Usb_GetConfigDescriptor(0, 1, nullptr);
EXPECT_EQ(ret, USB_DDK_INVALID_PARAMETER);
}
HWTEST_F(DdkPermissionTest, OH_Usb_GetConfigDescriptor_002, TestSize.Level1)
{
RevokePermission();
int32_t ret = OH_Usb_GetConfigDescriptor(0, 1, nullptr);
EXPECT_EQ(ret, USB_DDK_FAILED);
}
HWTEST_F(DdkPermissionTest, OH_Usb_ClaimInterface_001, TestSize.Level1)
{
int32_t ret = OH_Usb_ClaimInterface(0, 0, nullptr);
EXPECT_EQ(ret, USB_DDK_INVALID_PARAMETER);
}
HWTEST_F(DdkPermissionTest, OH_Usb_ClaimInterface_002, TestSize.Level1)
{
RevokePermission();
int32_t ret = OH_Usb_ClaimInterface(0, 0, nullptr);
EXPECT_EQ(ret, USB_DDK_FAILED);
}
HWTEST_F(DdkPermissionTest, OH_Usb_ReleaseInterface_001, TestSize.Level1)
{
RevokePermission();
int32_t ret = OH_Usb_ReleaseInterface(1);
EXPECT_EQ(ret, USB_DDK_FAILED);
}
HWTEST_F(DdkPermissionTest, OH_Usb_SelectInterfaceSetting_001, TestSize.Level1)
{
OH_Usb_Release();
int32_t ret = OH_Usb_SelectInterfaceSetting(0, 0);
EXPECT_EQ(ret, USB_DDK_INVALID_OPERATION);
OH_Usb_Init();
}
HWTEST_F(DdkPermissionTest, OH_Usb_SelectInterfaceSetting_002, TestSize.Level1)
{
RevokePermission();
int32_t ret = OH_Usb_SelectInterfaceSetting(0, 0);
EXPECT_EQ(ret, USB_DDK_FAILED);
}
HWTEST_F(DdkPermissionTest, OH_Usb_GetCurrentInterfaceSetting_001, TestSize.Level1)
{
int32_t ret = OH_Usb_GetCurrentInterfaceSetting(0, nullptr);
EXPECT_EQ(ret, USB_DDK_INVALID_PARAMETER);
}
HWTEST_F(DdkPermissionTest, OH_Usb_GetCurrentInterfaceSetting_002, TestSize.Level1)
{
RevokePermission();
int32_t ret = OH_Usb_GetCurrentInterfaceSetting(0, nullptr);
EXPECT_EQ(ret, USB_DDK_FAILED);
}
HWTEST_F(DdkPermissionTest, OH_Usb_SendControlReadRequest_001, TestSize.Level1)
{
int32_t ret = OH_Usb_SendControlReadRequest(0, nullptr, 0, nullptr, nullptr);
EXPECT_EQ(ret, USB_DDK_INVALID_PARAMETER);
}
HWTEST_F(DdkPermissionTest, OH_Usb_SendControlReadRequest_002, TestSize.Level1)
{
RevokePermission();
int32_t ret = OH_Usb_SendControlReadRequest(0, nullptr, 0, nullptr, nullptr);
EXPECT_EQ(ret, USB_DDK_FAILED);
}
HWTEST_F(DdkPermissionTest, OH_Usb_SendControlWriteRequest_001, TestSize.Level1)
{
int32_t ret = OH_Usb_SendControlWriteRequest(0, nullptr, 0, nullptr, 0);
EXPECT_EQ(ret, USB_DDK_INVALID_PARAMETER);
}
HWTEST_F(DdkPermissionTest, OH_Usb_SendControlWriteRequest_002, TestSize.Level1)
{
RevokePermission();
int32_t ret = OH_Usb_SendControlWriteRequest(0, nullptr, 0, nullptr, 0);
EXPECT_EQ(ret, USB_DDK_FAILED);
}
HWTEST_F(DdkPermissionTest, OH_Usb_SendPipeRequest_001, TestSize.Level1)
{
int32_t ret = OH_Usb_SendPipeRequest(nullptr, nullptr);
EXPECT_EQ(ret, USB_DDK_INVALID_PARAMETER);
}
HWTEST_F(DdkPermissionTest, OH_Usb_SendPipeRequest_002, TestSize.Level1)
{
RevokePermission();
int32_t ret = OH_Usb_SendPipeRequest(nullptr, nullptr);
EXPECT_EQ(ret, USB_DDK_FAILED);
}
HWTEST_F(DdkPermissionTest, OH_Usb_CreateDeviceMemMap_001, TestSize.Level1)
{
int32_t ret = OH_Usb_CreateDeviceMemMap(0, 100, nullptr);
EXPECT_EQ(ret, USB_DDK_INVALID_PARAMETER);
}
HWTEST_F(DdkPermissionTest, OH_Usb_CreateDeviceMemMap_002, TestSize.Level1)
{
RevokePermission();
int32_t ret = OH_Usb_CreateDeviceMemMap(0, 100, nullptr);
EXPECT_EQ(ret, USB_DDK_FAILED);
}
} // namespace

View File

@ -1,4 +1,4 @@
# Copyright (c) 2023 Huawei Device Co., Ltd.
# Copyright (c) Huawei Technologies Co., Ltd. 2023-2024. aLL right reserved.
# 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
@ -22,12 +22,16 @@ ohos_moduletest_suite("HatsHdfExternalPkgManagerTest") {
sources = [
"driver_pkg_manager_test.cpp",
"drv_bundle_callback_test.cpp",
"pkg_db_helper_test.cpp",
]
include_dirs = [
"${ext_mgr_path}/services/native/driver_extension_manager/include/drivers_pkg_manager",
"${usb_bus_extension_include_path}",
"${ext_mgr_path}/interfaces/ddk/hid",
"${ext_mgr_path}/services/zidl/include",
]
deps = [
"${ext_mgr_path}/interfaces/innerkits:driver_ext_mgr_client",
"${ext_mgr_path}/services/native/driver_extension_manager/src/drivers_pkg_manager:drivers_pkg_manager",
"//third_party/googletest:gmock_main",
"//third_party/googletest:gtest_main",
@ -41,8 +45,13 @@ ohos_moduletest_suite("HatsHdfExternalPkgManagerTest") {
"common_event_service:cesfwk_innerkits",
"drivers_interface_usb:libusb_proxy_1.0",
"hilog:libhilog",
"init:libbegetutil",
"ipc:ipc_core",
"ipc:ipc_single",
"os_account:os_account_innerkits",
"relational_store:native_appdatafwk",
"relational_store:native_dataability",
"relational_store:native_rdb",
"samgr:samgr_proxy",
]
configs = [ "${utils_path}:utils_config" ]

View File

@ -0,0 +1,261 @@
/*
* Copyright (c) Huawei Technologies Co., Ltd. 2023-2024. aLL right reserved.
* 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 "driver_ext_mgr_callback_stub.h"
#include "driver_ext_mgr_client.h"
#include "iservice_registry.h"
#include "system_ability_definition.h"
#include "system_ability_load_callback_stub.h"
#include "ext_object.h"
#define private public
#include "edm_errors.h"
#include "hilog_wrapper.h"
#include "pkg_db_helper.h"
#include "ibus_extension.h"
#include "usb_device_info.h"
#undef private
namespace OHOS {
namespace ExternalDeviceManager {
using namespace std;
using namespace testing::ext;
using namespace OHOS::ExternalDeviceManager;
class PkgDbHelperTest : public testing::Test {
public:
static void SetUpTestCase();
static void TearDownTestCase();
static bool LoadSaService();
static bool UnLoadSaService();
void SetUp() override {}
void TearDown() override {}
private:
class LoadCallback : public SystemAbilityLoadCallbackStub {
public:
void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr<IRemoteObject> &remoteObject) override;
void OnLoadSystemAbilityFail(int32_t systemAbilityId) override;
};
};
enum class LoadStatus {
LOAD_SUCCESS,
LOAD_FAILED,
ALREADY_EXISTS,
};
static std::string g_bundleName = "testBundleName";
static std::string g_Ability = "testAbility";
static size_t g_expect_size = 1;
static LoadStatus g_loadStatus = LoadStatus::LOAD_FAILED;
static sptr<IRemoteObject> g_saObject = nullptr;
static constexpr uint64_t START_SA_SERVICE_WAIT_TIME = 3;
bool g_loadSaServiceSucc = false;
bool g_unLoadSaServiceSucc = false;
void PkgDbHelperTest::SetUpTestCase()
{
int32_t ret = 0;
g_loadSaServiceSucc = LoadSaService();
if (g_loadSaServiceSucc) {
EDM_LOGE(EDM_MODULE_TEST, "%{public}s load hdf_ext_devmgr successlfully", __func__);
} else {
EDM_LOGE(EDM_MODULE_TEST, "%{public}s load hdf_ext_devmgr failed", __func__);
}
std::shared_ptr<PkgDbHelper> helper = PkgDbHelper::GetInstance();
bool isUpdate = false;
ret = helper->CheckIfNeedUpdateEx(isUpdate, g_Ability);
if (!isUpdate) {
return;
}
ret = helper->DeleteRightRecord(g_bundleName);
if (ret != PKG_OK) {
EDM_LOGE(EDM_MODULE_TEST, "%{public}s delete pkg from db fail", __func__);
}
}
void PkgDbHelperTest::TearDownTestCase()
{
std::shared_ptr<PkgDbHelper> helper= PkgDbHelper::GetInstance();
bool isUpdate = false;
int32_t ret = helper->CheckIfNeedUpdateEx(isUpdate, g_Ability);
if (!isUpdate) {
return;
}
ret = helper->DeleteRightRecord(g_bundleName);
if (ret != PKG_OK) {
EDM_LOGE(EDM_MODULE_TEST, "%{public}s delete pkg from db fail", __func__);
}
g_unLoadSaServiceSucc = UnLoadSaService();
if (g_unLoadSaServiceSucc) {
EDM_LOGE(EDM_MODULE_TEST, "%{public}s unload hdf_ext_devmgr successlfully", __func__);
} else {
EDM_LOGE(EDM_MODULE_TEST, "%{public}s unload hdf_ext_devmgr failed", __func__);
}
}
bool PkgDbHelperTest::LoadSaService()
{
int32_t ret = 0;
sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
if (samgr == nullptr) {
EDM_LOGE(EDM_MODULE_TEST, "%{public}s get samgr failed", __func__);
g_loadStatus = LoadStatus::LOAD_FAILED;
return false;
}
auto saObj = samgr->CheckSystemAbility(HDF_EXTERNAL_DEVICE_MANAGER_SA_ID);
if (saObj != nullptr) {
EDM_LOGE(EDM_MODULE_TEST, "%{public}s CheckSystemAbility failed", __func__);
g_saObject = saObj;
g_loadStatus = LoadStatus::ALREADY_EXISTS;
EDM_LOGI(EDM_MODULE_TEST, " hdf_ext_devmgr is exits");
return true;
}
sptr<LoadCallback> loadCallback_ = new LoadCallback();
ret = samgr->LoadSystemAbility(HDF_EXTERNAL_DEVICE_MANAGER_SA_ID, loadCallback_);
if (ret != UsbErrCode::EDM_OK) {
EDM_LOGE(EDM_MODULE_TEST, "%{public}s load hdf_ext_devmgr failed", __func__);
g_loadStatus = LoadStatus::LOAD_FAILED;
return false;
}
if (g_loadStatus != LoadStatus::ALREADY_EXISTS) {
std::this_thread::sleep_for(std::chrono::seconds(START_SA_SERVICE_WAIT_TIME));
}
if (g_saObject != nullptr) {
return true;
} else {
return false;
}
}
bool PkgDbHelperTest::UnLoadSaService()
{
if (g_loadStatus == LoadStatus::LOAD_FAILED || g_loadStatus == LoadStatus::ALREADY_EXISTS) {
EDM_LOGE(EDM_MODULE_TEST, "g_loadStatus = %{public}d ", g_loadStatus);
return false;
}
sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
if (samgr == nullptr) {
EDM_LOGE(EDM_MODULE_TEST, "%{public}s get samgr failed", __func__);
return false;
}
int32_t ret = samgr->UnloadSystemAbility(HDF_EXTERNAL_DEVICE_MANAGER_SA_ID);
if (ret != UsbErrCode::EDM_OK) {
return false;
} else {
return true;
}
}
void PkgDbHelperTest::LoadCallback::OnLoadSystemAbilitySuccess(
int32_t systemAbilityId, const sptr<IRemoteObject> &remoteObject)
{
EDM_LOGI(EDM_MODULE_TEST, "enter test: OnLoadSystemAbilitySuccess ");
std::cout << "load success: systemAbilityId:" << systemAbilityId
<< " IRemoteObject result:" << ((remoteObject != nullptr) ? "succeed" : "failed") << std::endl;
g_loadStatus = LoadStatus::LOAD_SUCCESS;
g_saObject = remoteObject;
}
void PkgDbHelperTest::LoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId)
{
EDM_LOGI(EDM_MODULE_TEST, "enter test: OnLoadSystemAbilityFail ");
std::cout << "load failed: systemAbilityId:" << systemAbilityId << std::endl;
g_loadStatus = LoadStatus::LOAD_FAILED;
g_saObject = nullptr;
}
HWTEST_F(PkgDbHelperTest, PkgDb_CheckIfNeedUpdateEx_Test, TestSize.Level1)
{
std::shared_ptr<PkgDbHelper> helper= PkgDbHelper::GetInstance();
bool isUpdate = false;
int32_t ret = helper->CheckIfNeedUpdateEx(isUpdate, g_Ability);
EXPECT_EQ(false, isUpdate);
EXPECT_EQ(0, ret);
cout << "PkgDb_CheckIfNeedUpdateEx_Test" << endl;
}
HWTEST_F(PkgDbHelperTest, PkgDb_AddOrUpdateRightRecord_Test, TestSize.Level1)
{
std::shared_ptr<PkgDbHelper> helper= PkgDbHelper::GetInstance();
string driverInfo = "{}";
int32_t ret = helper->AddOrUpdateRightRecord(g_bundleName, g_Ability, driverInfo);
EXPECT_EQ(0, ret);
cout << "PkgDb_AddOrUpdateRightRecord_Test" << endl;
}
HWTEST_F(PkgDbHelperTest, PkgDb_QueryAllSize_Test, TestSize.Level1)
{
std::shared_ptr<PkgDbHelper> helper= PkgDbHelper::GetInstance();
std::vector<std::string> allBundleAbilityNames;
int32_t ret = helper->QueryAllSize(allBundleAbilityNames);
EXPECT_LE(1, ret);
EXPECT_LE(g_expect_size, allBundleAbilityNames.size());
cout << "PkgDb_QueryAllSize_Test" << endl;
}
HWTEST_F(PkgDbHelperTest, PkgDb_QueryAllBundleAbilityNames_Test, TestSize.Level1)
{
std::shared_ptr<PkgDbHelper> helper= PkgDbHelper::GetInstance();
std::vector<std::string> bundleAbilityNames;
int32_t ret = helper->QueryAllBundleAbilityNames(g_bundleName, bundleAbilityNames);
EXPECT_EQ(1, ret);
EXPECT_EQ(g_expect_size, bundleAbilityNames.size());
cout << "PkgDb_QueryAllBundleAbilityNames_Test" << endl;
}
HWTEST_F(PkgDbHelperTest, PkgDb_QueryAllDriverInfos_Test, TestSize.Level1)
{
std::shared_ptr<PkgDbHelper> helper= PkgDbHelper::GetInstance();
std::vector<std::string> driverInfos;
int32_t ret = helper->QueryAllDriverInfos(driverInfos);
EXPECT_LE(1, ret);
EXPECT_LE(g_expect_size, driverInfos.size());
cout << "PkgDb_QueryAllDriverInfos_Test" << endl;
}
HWTEST_F(PkgDbHelperTest, PkgDb_QueryBundleInfoNames_Test, TestSize.Level1)
{
std::shared_ptr<PkgDbHelper> helper= PkgDbHelper::GetInstance();
string driverInfo = "{}";
string bundleName = helper->QueryBundleInfoNames(driverInfo);
EXPECT_EQ("testAbility", bundleName);
cout << "PkgDb_QueryBundleInfoNames_Test" << endl;
}
HWTEST_F(PkgDbHelperTest, PkgDb_QueryBundleInfoNames1_Test, TestSize.Level1)
{
std::shared_ptr<PkgDbHelper> helper= PkgDbHelper::GetInstance();
string driverInfo = "{test}";
string bundleName = helper->QueryBundleInfoNames(driverInfo);
EXPECT_EQ("", bundleName);
cout << "PkgDb_QueryBundleInfoNames1_Test" << endl;
}
HWTEST_F(PkgDbHelperTest, PkgDb_DeleteRightRecord_Test, TestSize.Level1)
{
std::shared_ptr<PkgDbHelper> helper= PkgDbHelper::GetInstance();
int32_t ret = helper->DeleteRightRecord(g_bundleName);
EXPECT_EQ(0, ret);
cout << "PkgDb_DeleteRightRecord_Test" << endl;
}
}
}