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 "driver_ext_mgr_callback_stub.h"
19 #include "driver_ext_mgr_client.h"
20 #include "iservice_registry.h"
21 #include "system_ability_definition.h"
22 #include "system_ability_load_callback_stub.h"
23 #include "ext_object.h"
24 #define private public
25 #include "edm_errors.h"
26 #include "hilog_wrapper.h"
27 #include "pkg_db_helper.h"
28 #include "ibus_extension.h"
29 #include "usb_device_info.h"
30 #undef private
31
32 namespace OHOS {
33 namespace ExternalDeviceManager {
34 using namespace std;
35 using namespace testing::ext;
36 using namespace OHOS::ExternalDeviceManager;
37
38 class PkgDbHelperTest : public testing::Test {
39 public:
40 static void SetUpTestCase();
41 static void TearDownTestCase();
42 static bool LoadSaService();
43 static bool UnLoadSaService();
44
SetUp()45 void SetUp() override {}
TearDown()46 void TearDown() override {}
47 private:
48 class LoadCallback : public SystemAbilityLoadCallbackStub {
49 public:
50 void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr<IRemoteObject> &remoteObject) override;
51 void OnLoadSystemAbilityFail(int32_t systemAbilityId) override;
52 };
53 };
54
55 enum class LoadStatus {
56 LOAD_SUCCESS,
57 LOAD_FAILED,
58 ALREADY_EXISTS,
59 };
60
61 static std::string g_bundleName = "testBundleName";
62 static std::string g_Ability = "testAbility";
63 static size_t g_expect_size = 1;
64 static LoadStatus g_loadStatus = LoadStatus::LOAD_FAILED;
65 static sptr<IRemoteObject> g_saObject = nullptr;
66 static constexpr uint64_t START_SA_SERVICE_WAIT_TIME = 3;
67 bool g_loadSaServiceSucc = false;
68 bool g_unLoadSaServiceSucc = false;
69
SetUpTestCase()70 void PkgDbHelperTest::SetUpTestCase()
71 {
72 int32_t ret = 0;
73 g_loadSaServiceSucc = LoadSaService();
74 if (g_loadSaServiceSucc) {
75 EDM_LOGE(EDM_MODULE_TEST, "%{public}s load hdf_ext_devmgr successlfully", __func__);
76 } else {
77 EDM_LOGE(EDM_MODULE_TEST, "%{public}s load hdf_ext_devmgr failed", __func__);
78 }
79
80 std::shared_ptr<PkgDbHelper> helper = PkgDbHelper::GetInstance();
81 bool isUpdate = false;
82 ret = helper->CheckIfNeedUpdateEx(isUpdate, g_Ability);
83 if (!isUpdate) {
84 return;
85 }
86 ret = helper->DeleteRightRecord(g_bundleName);
87 if (ret != PKG_OK) {
88 EDM_LOGE(EDM_MODULE_TEST, "%{public}s delete pkg from db fail", __func__);
89 }
90 }
91
TearDownTestCase()92 void PkgDbHelperTest::TearDownTestCase()
93 {
94 std::shared_ptr<PkgDbHelper> helper= PkgDbHelper::GetInstance();
95 bool isUpdate = false;
96 int32_t ret = helper->CheckIfNeedUpdateEx(isUpdate, g_Ability);
97 if (!isUpdate) {
98 return;
99 }
100 ret = helper->DeleteRightRecord(g_bundleName);
101 if (ret != PKG_OK) {
102 EDM_LOGE(EDM_MODULE_TEST, "%{public}s delete pkg from db fail", __func__);
103 }
104
105 g_unLoadSaServiceSucc = UnLoadSaService();
106 if (g_unLoadSaServiceSucc) {
107 EDM_LOGE(EDM_MODULE_TEST, "%{public}s unload hdf_ext_devmgr successlfully", __func__);
108 } else {
109 EDM_LOGE(EDM_MODULE_TEST, "%{public}s unload hdf_ext_devmgr failed", __func__);
110 }
111 }
112
LoadSaService()113 bool PkgDbHelperTest::LoadSaService()
114 {
115 int32_t ret = 0;
116 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
117 if (samgr == nullptr) {
118 EDM_LOGE(EDM_MODULE_TEST, "%{public}s get samgr failed", __func__);
119 g_loadStatus = LoadStatus::LOAD_FAILED;
120 return false;
121 }
122 auto saObj = samgr->CheckSystemAbility(HDF_EXTERNAL_DEVICE_MANAGER_SA_ID);
123 if (saObj != nullptr) {
124 EDM_LOGE(EDM_MODULE_TEST, "%{public}s CheckSystemAbility failed", __func__);
125 g_saObject = saObj;
126 g_loadStatus = LoadStatus::ALREADY_EXISTS;
127 EDM_LOGI(EDM_MODULE_TEST, " hdf_ext_devmgr is exits");
128 return true;
129 }
130 sptr<LoadCallback> loadCallback_ = new LoadCallback();
131 ret = samgr->LoadSystemAbility(HDF_EXTERNAL_DEVICE_MANAGER_SA_ID, loadCallback_);
132 if (ret != UsbErrCode::EDM_OK) {
133 EDM_LOGE(EDM_MODULE_TEST, "%{public}s load hdf_ext_devmgr failed", __func__);
134 g_loadStatus = LoadStatus::LOAD_FAILED;
135 return false;
136 }
137 if (g_loadStatus != LoadStatus::ALREADY_EXISTS) {
138 std::this_thread::sleep_for(std::chrono::seconds(START_SA_SERVICE_WAIT_TIME));
139 }
140 if (g_saObject != nullptr) {
141 return true;
142 } else {
143 return false;
144 }
145 }
146
UnLoadSaService()147 bool PkgDbHelperTest::UnLoadSaService()
148 {
149 if (g_loadStatus == LoadStatus::LOAD_FAILED || g_loadStatus == LoadStatus::ALREADY_EXISTS) {
150 EDM_LOGE(EDM_MODULE_TEST, "g_loadStatus = %{public}d ", g_loadStatus);
151 return false;
152 }
153
154 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
155 if (samgr == nullptr) {
156 EDM_LOGE(EDM_MODULE_TEST, "%{public}s get samgr failed", __func__);
157 return false;
158 }
159
160 int32_t ret = samgr->UnloadSystemAbility(HDF_EXTERNAL_DEVICE_MANAGER_SA_ID);
161 if (ret != UsbErrCode::EDM_OK) {
162 return false;
163 } else {
164 return true;
165 }
166 }
167
OnLoadSystemAbilitySuccess(int32_t systemAbilityId,const sptr<IRemoteObject> & remoteObject)168 void PkgDbHelperTest::LoadCallback::OnLoadSystemAbilitySuccess(
169 int32_t systemAbilityId, const sptr<IRemoteObject> &remoteObject)
170 {
171 EDM_LOGI(EDM_MODULE_TEST, "enter test: OnLoadSystemAbilitySuccess ");
172 std::cout << "load success: systemAbilityId:" << systemAbilityId
173 << " IRemoteObject result:" << ((remoteObject != nullptr) ? "succeed" : "failed") << std::endl;
174 g_loadStatus = LoadStatus::LOAD_SUCCESS;
175 g_saObject = remoteObject;
176 }
177
OnLoadSystemAbilityFail(int32_t systemAbilityId)178 void PkgDbHelperTest::LoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId)
179 {
180 EDM_LOGI(EDM_MODULE_TEST, "enter test: OnLoadSystemAbilityFail ");
181 std::cout << "load failed: systemAbilityId:" << systemAbilityId << std::endl;
182 g_loadStatus = LoadStatus::LOAD_FAILED;
183 g_saObject = nullptr;
184 }
185
186 HWTEST_F(PkgDbHelperTest, PkgDb_CheckIfNeedUpdateEx_Test, TestSize.Level1)
187 {
188 std::shared_ptr<PkgDbHelper> helper= PkgDbHelper::GetInstance();
189 bool isUpdate = false;
190 int32_t ret = helper->CheckIfNeedUpdateEx(isUpdate, g_Ability);
191 EXPECT_EQ(false, isUpdate);
192 EXPECT_EQ(0, ret);
193 cout << "PkgDb_CheckIfNeedUpdateEx_Test" << endl;
194 }
195
196 HWTEST_F(PkgDbHelperTest, PkgDb_AddOrUpdateRightRecord_Test, TestSize.Level1)
197 {
198 std::shared_ptr<PkgDbHelper> helper= PkgDbHelper::GetInstance();
199 string driverInfo = "{}";
200 int32_t ret = helper->AddOrUpdateRightRecord(g_bundleName, g_Ability, driverInfo);
201 EXPECT_EQ(0, ret);
202 cout << "PkgDb_AddOrUpdateRightRecord_Test" << endl;
203 }
204
205 HWTEST_F(PkgDbHelperTest, PkgDb_QueryAllSize_Test, TestSize.Level1)
206 {
207 std::shared_ptr<PkgDbHelper> helper= PkgDbHelper::GetInstance();
208 std::vector<std::string> allBundleAbilityNames;
209 int32_t ret = helper->QueryAllSize(allBundleAbilityNames);
210 EXPECT_LE(1, ret);
211 EXPECT_LE(g_expect_size, allBundleAbilityNames.size());
212 cout << "PkgDb_QueryAllSize_Test" << endl;
213 }
214
215 HWTEST_F(PkgDbHelperTest, PkgDb_QueryAllBundleAbilityNames_Test, TestSize.Level1)
216 {
217 std::shared_ptr<PkgDbHelper> helper= PkgDbHelper::GetInstance();
218 std::vector<std::string> bundleAbilityNames;
219 int32_t ret = helper->QueryAllBundleAbilityNames(g_bundleName, bundleAbilityNames);
220 EXPECT_EQ(1, ret);
221 EXPECT_EQ(g_expect_size, bundleAbilityNames.size());
222 cout << "PkgDb_QueryAllBundleAbilityNames_Test" << endl;
223 }
224
225 HWTEST_F(PkgDbHelperTest, PkgDb_QueryAllDriverInfos_Test, TestSize.Level1)
226 {
227 std::shared_ptr<PkgDbHelper> helper= PkgDbHelper::GetInstance();
228 std::vector<std::string> driverInfos;
229 int32_t ret = helper->QueryAllDriverInfos(driverInfos);
230 EXPECT_LE(1, ret);
231 EXPECT_LE(g_expect_size, driverInfos.size());
232 cout << "PkgDb_QueryAllDriverInfos_Test" << endl;
233 }
234
235 HWTEST_F(PkgDbHelperTest, PkgDb_QueryBundleInfoNames_Test, TestSize.Level1)
236 {
237 std::shared_ptr<PkgDbHelper> helper= PkgDbHelper::GetInstance();
238 string driverInfo = "{}";
239 string bundleName = helper->QueryBundleInfoNames(driverInfo);
240 EXPECT_EQ("testAbility", bundleName);
241 cout << "PkgDb_QueryBundleInfoNames_Test" << endl;
242 }
243
244 HWTEST_F(PkgDbHelperTest, PkgDb_QueryBundleInfoNames1_Test, TestSize.Level1)
245 {
246 std::shared_ptr<PkgDbHelper> helper= PkgDbHelper::GetInstance();
247 string driverInfo = "{test}";
248 string bundleName = helper->QueryBundleInfoNames(driverInfo);
249 EXPECT_EQ("", bundleName);
250 cout << "PkgDb_QueryBundleInfoNames1_Test" << endl;
251 }
252
253 HWTEST_F(PkgDbHelperTest, PkgDb_DeleteRightRecord_Test, TestSize.Level1)
254 {
255 std::shared_ptr<PkgDbHelper> helper= PkgDbHelper::GetInstance();
256 int32_t ret = helper->DeleteRightRecord(g_bundleName);
257 EXPECT_EQ(0, ret);
258 cout << "PkgDb_DeleteRightRecord_Test" << endl;
259 }
260 }
261 }