1 /*
2 * Copyright (c) 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 #include <iostream>
18 #include <thread>
19 #include "iservice_registry.h"
20 #include "system_ability_definition.h"
21 #include "system_ability_load_callback_stub.h"
22 #include "hilog_wrapper.h"
23 #define private public
24 #include "driver_pkg_manager.h"
25 #include "ibus_extension.h"
26 #include "usb_device_info.h"
27 #undef private
28
29 namespace OHOS {
30 namespace ExternalDeviceManager {
31 using namespace std;
32 using namespace testing::ext;
33 using namespace OHOS::ExternalDeviceManager;
34
35 class DriverPkgManagerTest : public testing::Test {
36 public:
SetUp()37 void SetUp() override {}
TearDown()38 void TearDown() override {}
39 static void SetUpTestCase();
40 static void TearDownTestCase();
41 private:
42 class LoadCallback : public SystemAbilityLoadCallbackStub {
43 public:
44 void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr<IRemoteObject> &remoteObject) override;
45 void OnLoadSystemAbilityFail(int32_t systemAbilityId) override;
46 };
47 };
48
49 enum class LoadStatus {
50 LOAD_SUCCESS,
51 LOAD_FAILED,
52 ALREADY_EXISTS,
53 };
54
55 static LoadStatus g_loadStatus = LoadStatus::LOAD_FAILED;
56 static sptr<IRemoteObject> g_saObject = nullptr;
57 static constexpr uint64_t START_SA_SERVICE_WAIT_TIME = 3;
58
SetUpTestCase()59 void DriverPkgManagerTest::SetUpTestCase()
60 {
61 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
62 if (samgr == nullptr) {
63 EDM_LOGE(EDM_MODULE_TEST, "%{public}s get samgr failed", __func__);
64 g_loadStatus = LoadStatus::LOAD_FAILED;
65 return;
66 }
67 auto saObj = samgr->CheckSystemAbility(HDF_EXTERNAL_DEVICE_MANAGER_SA_ID);
68 if (saObj != nullptr) {
69 EDM_LOGE(EDM_MODULE_TEST, "%{public}s CheckSystemAbility failed", __func__);
70 g_saObject = saObj;
71 g_loadStatus = LoadStatus::ALREADY_EXISTS;
72 EDM_LOGI(EDM_MODULE_TEST, " hdf_ext_devmgr is exits");
73 return;
74 }
75 sptr<LoadCallback> loadCallback_ = new LoadCallback();
76 int32_t ret = samgr->LoadSystemAbility(HDF_EXTERNAL_DEVICE_MANAGER_SA_ID, loadCallback_);
77 if (ret != UsbErrCode::EDM_OK) {
78 EDM_LOGE(EDM_MODULE_TEST, "%{public}s load hdf_ext_devmgr failed", __func__);
79 g_loadStatus = LoadStatus::LOAD_FAILED;
80 return;
81 }
82 if (g_loadStatus != LoadStatus::ALREADY_EXISTS) {
83 std::this_thread::sleep_for(std::chrono::seconds(START_SA_SERVICE_WAIT_TIME));
84 }
85 }
86
TearDownTestCase()87 void DriverPkgManagerTest::TearDownTestCase()
88 {
89 if (g_loadStatus == LoadStatus::LOAD_FAILED || g_loadStatus == LoadStatus::ALREADY_EXISTS) {
90 EDM_LOGE(EDM_MODULE_TEST, "g_loadStatus = %{public}d ", g_loadStatus);
91 return;
92 }
93
94 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
95 if (samgr == nullptr) {
96 EDM_LOGE(EDM_MODULE_TEST, "%{public}s get samgr failed", __func__);
97 return;
98 }
99
100 int32_t ret = samgr->UnloadSystemAbility(HDF_EXTERNAL_DEVICE_MANAGER_SA_ID);
101 if (ret == UsbErrCode::EDM_OK) {
102 EDM_LOGI(EDM_MODULE_TEST, "UnloadSystemAbility success");
103 } else {
104 EDM_LOGI(EDM_MODULE_TEST, "UnloadSystemAbility failed");
105 }
106 }
107
OnLoadSystemAbilitySuccess(int32_t systemAbilityId,const sptr<IRemoteObject> & remoteObject)108 void DriverPkgManagerTest::LoadCallback::OnLoadSystemAbilitySuccess(
109 int32_t systemAbilityId, const sptr<IRemoteObject> &remoteObject)
110 {
111 EDM_LOGI(EDM_MODULE_TEST, "enter test: OnLoadSystemAbilitySuccess ");
112 std::cout << "load success: systemAbilityId:" << systemAbilityId
113 << " IRemoteObject result:" << ((remoteObject != nullptr) ? "succeed" : "failed") << std::endl;
114 g_loadStatus = LoadStatus::LOAD_SUCCESS;
115 g_saObject = remoteObject;
116 }
117
OnLoadSystemAbilityFail(int32_t systemAbilityId)118 void DriverPkgManagerTest::LoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId)
119 {
120 EDM_LOGI(EDM_MODULE_TEST, "enter test: OnLoadSystemAbilityFail ");
121 std::cout << "load failed: systemAbilityId:" << systemAbilityId << std::endl;
122 g_loadStatus = LoadStatus::LOAD_FAILED;
123 g_saObject = nullptr;
124 }
125
126 HWTEST_F(DriverPkgManagerTest, DrvExt_Init_Test, TestSize.Level1)
127 {
128 DriverPkgManager &drvPkgMgrInstance = DriverPkgManager::GetInstance();
129 bool ret = drvPkgMgrInstance.Init();
130 EXPECT_EQ(0, ret);
131 cout << "DrvExt_Init_New_BundleTest" << endl;
132 }
133
Fun(int a,int b,const string & c,const string & d)134 int32_t Fun(int a, int b, const string & c, const string & d)
135 {
136 cout << a << endl;
137 cout << b << endl;
138 cout << c << endl;
139 cout << d << endl;
140 return EDM_OK;
141 }
142
143 HWTEST_F(DriverPkgManagerTest, DrvExt_RegisterOnBundleUpdate_Init_Test, TestSize.Level1)
144 {
145 DriverPkgManager &drvPkgMgrInstance = DriverPkgManager::GetInstance();
146 int32_t regist = drvPkgMgrInstance.RegisterOnBundleUpdate(Fun);
147 EXPECT_EQ(0, regist);
148 cout << "DrvExt_RegisterOnBundleUpdate_Init_Test" << endl;
149 }
150
151 HWTEST_F(DriverPkgManagerTest, DrvExt_QueryMatch_Illegal_Bus_Test, TestSize.Level1)
152 {
153 DriverPkgManager &drvPkgMgrInstance = DriverPkgManager::GetInstance();
154 std::shared_ptr<DeviceInfo> devInfo = std::make_shared<DeviceInfo>(1);
155 devInfo->devInfo_.devBusInfo.busType = BUS_TYPE_INVALID;
156 std::shared_ptr<BundleInfoNames> bundle = drvPkgMgrInstance.QueryMatchDriver(devInfo);
157 EXPECT_EQ(nullptr, bundle);
158 cout << "DrvExt_QueryMatch_Illegal_Bus_Test" << endl;
159 }
160
161 class DriverPkgManagerPtrTest : public testing::Test {
162 public:
SetUp()163 void SetUp() override {}
TearDown()164 void TearDown() override {}
165 };
166
167 HWTEST_F(DriverPkgManagerPtrTest, DrvExt_QueryMatch_Before_Init_Test, TestSize.Level1)
168 {
169 DriverPkgManager &drvPkgMgrInstance = DriverPkgManager::GetInstance();
170 std::shared_ptr<DeviceInfo> devInfo = std::make_shared<DeviceInfo>(0);
171 std::shared_ptr<BundleInfoNames> bundle = drvPkgMgrInstance.QueryMatchDriver(devInfo);
172 EXPECT_EQ(nullptr, bundle);
173 cout << "DrvExt_QueryMatch_Before_Init_Test" << endl;
174 }
175
176 HWTEST_F(DriverPkgManagerPtrTest, DrvExt_New_Test, TestSize.Level1)
177 {
178 DriverPkgManager &drvPkgMgrInstance = DriverPkgManager::GetInstance();
179 bool ret = drvPkgMgrInstance.Init();
180 if (ret != 0) {
181 EXPECT_EQ(0, ret);
182 return;
183 }
184 EXPECT_NE(nullptr, &drvPkgMgrInstance);
185 cout << "DrvExt_New_Test" << endl;
186 }
187
188 HWTEST_F(DriverPkgManagerPtrTest, DrvExt_Delete_Test, TestSize.Level1)
189 {
190 auto drvPkgMgrInstance = &(DriverPkgManager::GetInstance());
191 if (drvPkgMgrInstance != nullptr) {
192 delete drvPkgMgrInstance;
193 drvPkgMgrInstance = nullptr;
194 EXPECT_EQ(nullptr, drvPkgMgrInstance);
195 }
196 cout << "DrvExt_Delete_Test" << endl;
197 }
198 }
199 }