• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "bundle_manager_internal_test.h"
17 #include "bundle/bundle_manager_internal.h"
18 
19 #define private public
20 #include "bundle/bundle_manager_callback_stub.h"
21 #undef private
22 
23 #include "dtbschedmgr_log.h"
24 #include "test_log.h"
25 
26 using namespace testing;
27 using namespace testing::ext;
28 
29 namespace OHOS {
30 namespace DistributedSchedule {
31 namespace {
32 const string GROUP_ID = "TEST_GROUP_ID";
33 }
34 
SetUpTestCase()35 void BundleManagerInternalTest::SetUpTestCase()
36 {
37     DTEST_LOG << "BundleManagerInternalTest::SetUpTestCase" << std::endl;
38 }
39 
TearDownTestCase()40 void BundleManagerInternalTest::TearDownTestCase()
41 {
42     DTEST_LOG << "BundleManagerInternalTest::TearDownTestCase" << std::endl;
43 }
44 
TearDown()45 void BundleManagerInternalTest::TearDown()
46 {
47     DTEST_LOG << "BundleManagerInternalTest::TearDown" << std::endl;
48 }
49 
SetUp()50 void BundleManagerInternalTest::SetUp()
51 {
52     DTEST_LOG << "BundleManagerInternalTest::SetUp" << std::endl;
53 }
54 
55 /**
56  * @tc.name: BundleManagerInternalTest_001
57  * @tc.desc: input invalid params
58  * @tc.type: FUNC
59  */
60 HWTEST_F(BundleManagerInternalTest, BundleManagerInternalTest_001, TestSize.Level0)
61 {
62     DTEST_LOG << "BundleManagerInternalTest BundleManagerInternalTest_001 begin" << std::endl;
63     AAFwk::Want want;
64     AppExecFwk::ExtensionAbilityInfo extensionInfo;
65 
66     bool ret = BundleManagerInternal::QueryExtensionAbilityInfo(want, extensionInfo);
67     EXPECT_TRUE(!ret);
68     EXPECT_TRUE(extensionInfo.name.empty());
69     DTEST_LOG << "BundleManagerInternalTest BundleManagerInternalTest_001 end ret:" << ret << std::endl;
70 }
71 
72 /**
73  * @tc.name: BundleManagerInternalTest_002
74  * @tc.desc: test ability info convert
75  * @tc.type: FUNC
76  */
77 HWTEST_F(BundleManagerInternalTest, BundleManagerInternalTest_002, TestSize.Level0)
78 {
79     DTEST_LOG << "BundleManagerInternalTest BundleManagerInternalTest_002 begin" << std::endl;
80     AppExecFwk::AbilityInfo abilityInfo;
81     AppExecFwk::ExtensionAbilityInfo extensionInfo;
82     extensionInfo.bundleName.append("bundleName");
83     extensionInfo.name.append("name");
84     vector<string> permissions;
85     extensionInfo.permissions = permissions;
86     extensionInfo.visible = true;
87 
88     BundleManagerInternal::InitAbilityInfoFromExtension(extensionInfo, abilityInfo);
89     EXPECT_TRUE(abilityInfo.bundleName.compare("bundleName") == 0);
90     EXPECT_TRUE(abilityInfo.name.compare("name") == 0);
91     EXPECT_TRUE(abilityInfo.permissions == permissions);
92     EXPECT_TRUE(abilityInfo.visible);
93     DTEST_LOG << "BundleManagerInternalTest BundleManagerInternalTest_002 end "<< std::endl;
94 }
95 
96 /**
97  * @tc.name: BundleManagerInternalTest_003
98  * @tc.desc: test CheckRemoteBundleInfo with empty bundleName
99  * @tc.type: FUNC
100  */
101 HWTEST_F(BundleManagerInternalTest, BundleManagerInternalTest_003, TestSize.Level1)
102 {
103     DTEST_LOG << "BundleManagerInternalTest BundleManagerInternalTest_003 begin" << std::endl;
104     string deviceId = "123456";
105     string bundleName = "";
106     AppExecFwk::DistributedBundleInfo remoteBundleInfo;
107     int ret = BundleManagerInternal::CheckRemoteBundleInfoForContinuation(deviceId, bundleName, remoteBundleInfo);
108     EXPECT_TRUE(INVALID_PARAMETERS_ERR == ret);
109 
110     DTEST_LOG << "BundleManagerInternalTest BundleManagerInternalTest_003 end "<< std::endl;
111 }
112 
113 /**
114  * @tc.name: BundleManagerInternalTest_004
115  * @tc.desc: test CheckRemoteBundleInfo with unexist bundle
116  * @tc.type: FUNC
117  */
118 HWTEST_F(BundleManagerInternalTest, BundleManagerInternalTest_004, TestSize.Level1)
119 {
120     DTEST_LOG << "BundleManagerInternalTest BundleManagerInternalTest_004 begin" << std::endl;
121     string deviceId = "123456";
122     string bundleName = "ohos.samples.testApp";
123     AppExecFwk::DistributedBundleInfo remoteBundleInfo;
124     int ret = BundleManagerInternal::CheckRemoteBundleInfoForContinuation(deviceId, bundleName, remoteBundleInfo);
125     EXPECT_TRUE(INVALID_PARAMETERS_ERR == ret);
126 
127     DTEST_LOG << "BundleManagerInternalTest BundleManagerInternalTest_004 end "<< std::endl;
128 }
129 
130 /**
131  * @tc.name: BundleManagerInternalTest_005
132  * @tc.desc: test CheckRemoteBundleInfo with valid param
133  * @tc.type: FUNC
134  */
135 HWTEST_F(BundleManagerInternalTest, BundleManagerInternalTest_005, TestSize.Level1)
136 {
137     DTEST_LOG << "BundleManagerInternalTest BundleManagerInternalTest_005 begin" << std::endl;
138     string deviceId = "123456";
139     string bundleName = "ohos.samples.distributedcalc";
140     AppExecFwk::DistributedBundleInfo remoteBundleInfo;
141     int ret = BundleManagerInternal::CheckRemoteBundleInfoForContinuation(deviceId, bundleName, remoteBundleInfo);
142     EXPECT_TRUE(CONTINUE_REMOTE_UNINSTALLED_UNSUPPORT_FREEINSTALL == ret);
143 
144     DTEST_LOG << "BundleManagerInternalTest BundleManagerInternalTest_005 end "<< std::endl;
145 }
146 
147 /**
148  * @tc.name: BundleManagerInternalTest_006
149  * @tc.desc: test CheckIfRemoteCanInstall with valid param
150  * @tc.type: FUNC
151  */
152 HWTEST_F(BundleManagerInternalTest, BundleManagerInternalTest_006, TestSize.Level1)
153 {
154     DTEST_LOG << "BundleManagerInternalTest BundleManagerInternalTest_006 begin" << std::endl;
155     string deviceId = "123456";
156     string bundleName = "ohos.samples.distributedcalc";
157     string moduleName = "entry";
158     string abilityName = "MainAbility";
159     AAFwk::Want want;
160     want.SetElementName(deviceId, bundleName, abilityName, moduleName);
161     int32_t missionId = 0;
162     bool ret = BundleManagerInternal::CheckIfRemoteCanInstall(want, missionId);
163     EXPECT_EQ(ret, true);
164     DTEST_LOG << "BundleManagerInternalTest BundleManagerInternalTest_006 end "<< std::endl;
165 }
166 
167 /**
168  * @tc.name: BundleManagerInternalTest_007
169  * @tc.desc: test CheckIfRemoteCanInstall with invalid param
170  * @tc.type: FUNC
171  */
172 HWTEST_F(BundleManagerInternalTest, BundleManagerInternalTest_007, TestSize.Level1)
173 {
174     DTEST_LOG << "BundleManagerInternalTest BundleManagerInternalTest_007 begin" << std::endl;
175     string deviceId = "";
176     string bundleName = "ohos.samples.testApp";
177     string moduleName = "entry";
178     string abilityName = "MainAbility";
179     AAFwk::Want want;
180     want.SetElementName(deviceId, bundleName, abilityName, moduleName);
181     int32_t missionId = 0;
182     bool ret = BundleManagerInternal::CheckIfRemoteCanInstall(want, missionId);
183     EXPECT_EQ(ret, false);
184 
185     deviceId = "123456";
186     bundleName = "";
187     want.SetElementName(deviceId, bundleName, abilityName, moduleName);
188     ret = BundleManagerInternal::CheckIfRemoteCanInstall(want, missionId);
189     EXPECT_EQ(ret, false);
190 
191     bundleName = "ohos.samples.testApp";
192     moduleName = "";
193     want.SetElementName(deviceId, bundleName, abilityName, moduleName);
194     ret = BundleManagerInternal::CheckIfRemoteCanInstall(want, missionId);
195     EXPECT_EQ(ret, false);
196 
197     moduleName = "entry";
198     abilityName = "";
199     want.SetElementName(deviceId, bundleName, abilityName, moduleName);
200     ret = BundleManagerInternal::CheckIfRemoteCanInstall(want, missionId);
201     EXPECT_EQ(ret, false);
202 
203     DTEST_LOG << "BundleManagerInternalTest BundleManagerInternalTest_007 end "<< std::endl;
204 }
205 
206 /**
207  * @tc.name: BundleManagerInternalTest_008
208  * @tc.desc: test CheckIfRemoteCanInstall with unexist bundle
209  * @tc.type: FUNC
210  */
211 HWTEST_F(BundleManagerInternalTest, BundleManagerInternalTest_008, TestSize.Level1)
212 {
213     DTEST_LOG << "BundleManagerInternalTest BundleManagerInternalTest_008 begin" << std::endl;
214     string deviceId = "123456";
215     string bundleName = "ohos.samples.testApp";
216     string moduleName = "entry";
217     string abilityName = "MainAbility";
218     AAFwk::Want want;
219     want.SetElementName(deviceId, bundleName, abilityName, moduleName);
220     int32_t missionId = 0;
221     bool ret = BundleManagerInternal::CheckIfRemoteCanInstall(want, missionId);
222     EXPECT_EQ(ret, false);
223     DTEST_LOG << "BundleManagerInternalTest BundleManagerInternalTest_008 end "<< std::endl;
224 }
225 
226 /**
227  * @tc.name: BundleManagerInternalTest_009
228  * @tc.desc: test get callerappId from bms with invalid param
229  * @tc.type: FUNC
230  */
231 HWTEST_F(BundleManagerInternalTest, BundleManagerInternalTest_009, TestSize.Level4)
232 {
233     DTEST_LOG << "BundleManagerInternalTest BundleManagerInternalTest_009 begin" << std::endl;
234     int32_t callingUid = -1;
235     string appId;
236     bool ret = BundleManagerInternal::GetCallerAppIdFromBms(callingUid, appId);
237     EXPECT_EQ(ret, false);
238     DTEST_LOG << "BundleManagerInternalTest BundleManagerInternalTest_009 end "<< std::endl;
239 }
240 
241 /**
242  * @tc.name: BundleManagerInternalTest_010
243  * @tc.desc: test get callerappId from bms with invalid param
244  * @tc.type: FUNC
245  */
246 HWTEST_F(BundleManagerInternalTest, BundleManagerInternalTest_010, TestSize.Level3)
247 {
248     DTEST_LOG << "BundleManagerInternalTest BundleManagerInternalTest_010 begin" << std::endl;
249     int32_t callingUid = 5522;
250     string appId;
251     bool ret = BundleManagerInternal::GetCallerAppIdFromBms(callingUid, appId);
252     EXPECT_EQ(ret, false);
253     DTEST_LOG << "BundleManagerInternalTest BundleManagerInternalTest_010 end "<< std::endl;
254 }
255 
256 /**
257  * @tc.name: BundleManagerInternalTest_011
258  * @tc.desc: test IsSameAppId with invalid param
259  * @tc.type: FUNC
260  */
261 HWTEST_F(BundleManagerInternalTest, BundleManagerInternalTest_011, TestSize.Level3)
262 {
263     DTEST_LOG << "BundleManagerInternalTest BundleManagerInternalTest_011 begin" << std::endl;
264     string appId;
265     string targetBundleName;
266     bool ret = BundleManagerInternal::IsSameAppId(appId, targetBundleName);
267     EXPECT_EQ(ret, false);
268     DTEST_LOG << "BundleManagerInternalTest BundleManagerInternalTest_011 end "<< std::endl;
269 }
270 
271 /**
272  * @tc.name: BundleManagerInternalTest_012
273  * @tc.desc: test IsSameAppId with invalid param
274  * @tc.type: FUNC
275  */
276 HWTEST_F(BundleManagerInternalTest, BundleManagerInternalTest_012, TestSize.Level3)
277 {
278     DTEST_LOG << "BundleManagerInternalTest BundleManagerInternalTest_012 begin" << std::endl;
279     string appId = "1001";
280     string targetBundleName = "ohos.samples.testApp";
281     bool ret = BundleManagerInternal::IsSameAppId(appId, targetBundleName);
282     EXPECT_EQ(ret, false);
283     DTEST_LOG << "BundleManagerInternalTest BundleManagerInternalTest_012 end "<< std::endl;
284 }
285 
286 /**
287  * @tc.name: BundleManagerInternalTest_013
288  * @tc.desc: test IsSameAppId with invalid param
289  * @tc.type: FUNC
290  */
291 HWTEST_F(BundleManagerInternalTest, BundleManagerInternalTest_013, TestSize.Level3)
292 {
293     DTEST_LOG << "BundleManagerInternalTest BundleManagerInternalTest_013 begin" << std::endl;
294     string bundleName = "ohos.samples.testApp";
295     int32_t uid = BundleManagerInternal::GetUidFromBms(bundleName);
296     EXPECT_EQ(uid, -1);
297     DTEST_LOG << "BundleManagerInternalTest BundleManagerInternalTest_013 end "<< std::endl;
298 }
299 
300 /**
301  * @tc.name: BundleManagerCallBackTest_001
302  * @tc.desc: test OnQueryInstallationFinished with failed result
303  * @tc.type: FUNC
304  */
305 HWTEST_F(BundleManagerInternalTest, BundleManagerCallBackTest_001, TestSize.Level1)
306 {
307     DTEST_LOG << "BundleManagerCallBackTest BundleManagerCallBackTest_001 begin" << std::endl;
308     int32_t resultCode = -1;
309     int32_t missionId = 0;
310     int32_t versionCode = 10000;
311     auto callback = new DmsBundleManagerCallbackStub();
312     int32_t ret = callback->OnQueryInstallationFinished(resultCode, missionId, versionCode);
313     EXPECT_TRUE(CONTINUE_REMOTE_UNINSTALLED_CANNOT_FREEINSTALL == ret);
314     DTEST_LOG << "BundleManagerCallBackTest BundleManagerCallBackTest_001 end "<< std::endl;
315 }
316 
317 /**
318  * @tc.name: BundleManagerCallBackTest_002
319  * @tc.desc: test OnQueryInstallationFinished
320  * @tc.type: FUNC
321  */
322 HWTEST_F(BundleManagerInternalTest, BundleManagerCallBackTest_002, TestSize.Level1)
323 {
324     DTEST_LOG << "BundleManagerCallBackTest BundleManagerCallBackTest_002 begin" << std::endl;
325     int32_t resultCode = 0;
326     int32_t missionId = 0;
327     int32_t versionCode = 10000;
328     auto callback = new DmsBundleManagerCallbackStub();
329     int32_t ret = callback->OnQueryInstallationFinished(resultCode, missionId, versionCode);
330     EXPECT_TRUE(ERR_OK != ret);
331     DTEST_LOG << "BundleManagerCallBackTest BundleManagerCallBackTest_002 end "<< std::endl;
332 }
333 
334 /**
335  * @tc.name: BundleManagerCallBackTest_003
336  * @tc.desc: test OnRemoteRequest
337  * @tc.type: FUNC
338  */
339 HWTEST_F(BundleManagerInternalTest, BundleManagerCallBackTest_003, TestSize.Level1)
340 {
341     DTEST_LOG << "BundleManagerCallBackTest BundleManagerCallBackTest_003 begin" << std::endl;
342     MessageParcel data;
343     MessageParcel reply;
344     MessageOption option;
345     auto callback = new DmsBundleManagerCallbackStub();
346     int32_t ret = callback->OnRemoteRequest(-1, data, reply, option);
347     EXPECT_TRUE(ERR_OK != ret);
348     DTEST_LOG << "BundleManagerCallBackTest BundleManagerCallBackTest_003 end "<< std::endl;
349 }
350 
351 /**
352  * @tc.name: GetBundleNameListFromBms_001
353  * @tc.desc: test GetBundleNameListFromBms
354  * @tc.type: FUNC
355  */
356 HWTEST_F(BundleManagerInternalTest, GetBundleNameListFromBms_001, TestSize.Level3)
357 {
358     DTEST_LOG << "BundleManagerCallBackTest GetBundleNameListFromBms_001 begin" << std::endl;
359     const std::string bundleName = "com.ohos.launcher";
360     int32_t uid = BundleManagerInternal::GetUidFromBms(bundleName);
361     if (uid <= 0) {
362         return;
363     }
364     std::vector<std::u16string> u16BundleNameList;
365     BundleManagerInternal::GetBundleNameListFromBms(uid, u16BundleNameList);
366     EXPECT_TRUE(!u16BundleNameList.empty());
367     u16BundleNameList.clear();
368     BundleManagerInternal::GetBundleNameListFromBms(-1, u16BundleNameList);
369     EXPECT_TRUE(u16BundleNameList.empty());
370     DTEST_LOG << "BundleManagerCallBackTest GetBundleNameListFromBms_001 end "<< std::endl;
371 }
372 
373 /**
374  * @tc.name: GetCallerAppIdFromBms_001
375  * @tc.desc: test get callerappId from bms
376  * @tc.type: FUNC
377  */
378 HWTEST_F(BundleManagerInternalTest, GetCallerAppIdFromBms_001, TestSize.Level3)
379 {
380     DTEST_LOG << "BundleManagerInternalTest GetCallerAppIdFromBms_001 begin" << std::endl;
381     const std::string bundleName = "com.ohos.launcher";
382     int32_t uid = BundleManagerInternal::GetUidFromBms(bundleName);
383     if (uid <= 0) {
384         return;
385     }
386     string appId;
387     bool ret = BundleManagerInternal::GetCallerAppIdFromBms(uid, appId);
388     EXPECT_EQ(ret, true);
389     DTEST_LOG << "BundleManagerInternalTest GetCallerAppIdFromBms_001 end "<< std::endl;
390 }
391 
392 /**
393  * @tc.name: IsSameAppId_001
394  * @tc.desc: test IsSameAppId with invalid param
395  * @tc.type: FUNC
396  */
397 HWTEST_F(BundleManagerInternalTest, IsSameAppId_001, TestSize.Level3)
398 {
399     DTEST_LOG << "BundleManagerInternalTest IsSameAppId_001 begin" << std::endl;
400     string appId = "1001";
401     string targetBundleName = "ohos.samples.dms.testApp";
402     bool ret = BundleManagerInternal::IsSameAppId(appId, targetBundleName);
403     EXPECT_EQ(ret, false);
404     DTEST_LOG << "BundleManagerInternalTest IsSameAppId_001 end "<< std::endl;
405 }
406 }
407 }