1 /*
2 * Copyright (c) 2025 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 "UTTest_app_manager.h"
17 #include "bundle_mgr_mock.h"
18 #include "dm_constants.h"
19 #include "dm_system_ability_manager_mock.h"
20
21 using namespace OHOS::AppExecFwk;
22 using namespace testing;
23
24 namespace OHOS {
25 namespace DistributedHardware {
26 namespace {
27 constexpr size_t ARG_THIRD = 2;
28 constexpr size_t INVOKE_COUNT = 1;
29 constexpr size_t ERR_FAILED_VALUE = 11600101;
30 constexpr uint32_t VALUABLE_TOKEN_ID = 153;
31 constexpr uint32_t UNVALUABLE_TOKEN_ID = 0;
32 } // namespace
SetUp()33 void AppManagerTest::SetUp()
34 {
35 auto skeleton = IPCSkeletonInterface::GetOrCreateIPCSkeleton();
36 skeleton_ = std::static_pointer_cast<IPCSkeletonMock>(skeleton);
37 auto token = AccessTokenKitInterface::GetOrCreateAccessTokenKit();
38 token_ = std::static_pointer_cast<AccessTokenKitMock>(token);
39 auto client = ISystemAbilityManagerClient::GetOrCreateSAMgrClient();
40 client_ = std::static_pointer_cast<SystemAbilityManagerClientMock>(client);
41 auto accountManager = IOsAccountManager::GetOrCreateOsAccountManager();
42 accountManager_ = std::static_pointer_cast<OsAccountManagerMock>(accountManager);
43 }
44
TearDown()45 void AppManagerTest::TearDown()
46 {
47 IPCSkeletonInterface::ReleaseIPCSkeleton();
48 AccessTokenKitInterface::ReleaseAccessTokenKit();
49 ISystemAbilityManagerClient::ReleaseSAMgrClient();
50 IOsAccountManager::ReleaseAccountManager();
51 skeleton_ = nullptr;
52 token_ = nullptr;
53 client_ = nullptr;
54 accountManager_ = nullptr;
55 }
56
SetUpTestCase()57 void AppManagerTest::SetUpTestCase()
58 {}
59
TearDownTestCase()60 void AppManagerTest::TearDownTestCase()
61 {}
62
63 HWTEST_F(AppManagerTest, RegisterCallerAppId_success_001, testing::ext::TestSize.Level1)
64 {
65 ASSERT_TRUE(skeleton_ != nullptr);
66 ASSERT_TRUE(token_ != nullptr);
67 ASSERT_TRUE(client_ != nullptr);
68 ASSERT_TRUE(accountManager_ != nullptr);
69
70 EXPECT_CALL(*skeleton_, GetCallingTokenID())
71 .Times(INVOKE_COUNT)
72 .WillOnce(Return(VALUABLE_TOKEN_ID));
73 EXPECT_CALL(*skeleton_, GetCallingUid())
74 .Times(AtLeast(INVOKE_COUNT));
75 EXPECT_CALL(*token_, GetTokenTypeFlag(_))
76 .Times(INVOKE_COUNT)
77 .WillOnce(Return(ATokenTypeEnum::TOKEN_HAP));
78 auto bundleMgr = sptr<BundleMgrMock>(new (std::nothrow) BundleMgrMock());
79 auto systemAbilityManager = sptr<SystemAbilityManagerMock>(new (std::nothrow) SystemAbilityManagerMock());
80 EXPECT_CALL(*systemAbilityManager, GetSystemAbility(_))
81 .Times(INVOKE_COUNT)
82 .WillOnce(Return(bundleMgr));
83 EXPECT_CALL(*client_, GetSystemAbilityManager())
84 .Times(INVOKE_COUNT)
85 .WillOnce(Return(systemAbilityManager));
86 EXPECT_CALL(*accountManager_, GetForegroundOsAccountLocalId(_))
87 .Times(INVOKE_COUNT)
88 .WillOnce(Return(ERR_OK));
89 EXPECT_CALL(*bundleMgr, GetNameForUid(_, _))
90 .Times(INVOKE_COUNT)
91 .WillOnce(Return(ERR_OK));
92 BundleInfo bundleInfo;
93 bundleInfo.appId = "mock_appId";
94 EXPECT_CALL(*bundleMgr, GetBundleInfoV9(_, _, _, _))
95 .Times(INVOKE_COUNT)
96 .WillOnce(DoAll(SetArgReferee<ARG_THIRD>(bundleInfo), Return(ERR_OK)));
97 std::string packageName = "packageName";
98 AppManager::GetInstance().RegisterCallerAppId(packageName);
99
100 std::string appId;
101 auto result = AppManager::GetInstance().GetAppIdByPkgName(packageName, appId);
102 EXPECT_EQ(result, DM_OK);
103 EXPECT_STREQ(bundleInfo.appId.c_str(), appId.c_str());
104 AppManager::GetInstance().UnRegisterCallerAppId(packageName);
105 }
106
107 HWTEST_F(AppManagerTest, RegisterCallerAppId_failed_001, testing::ext::TestSize.Level2)
108 {
109 ASSERT_TRUE(skeleton_ != nullptr);
110 ASSERT_TRUE(token_ != nullptr);
111
112 std::string emptyPackageName;
113 std::string appId;
114 AppManager::GetInstance().RegisterCallerAppId(emptyPackageName);
115 AppManager::GetInstance().GetAppIdByPkgName(emptyPackageName, appId);
116 AppManager::GetInstance().UnRegisterCallerAppId(emptyPackageName);
117
118 EXPECT_CALL(*skeleton_, GetCallingTokenID())
119 .Times(AtLeast(INVOKE_COUNT));
120 EXPECT_CALL(*token_, GetTokenTypeFlag(_))
121 .Times(INVOKE_COUNT)
122 .WillOnce(Return(ATokenTypeEnum::TOKEN_NATIVE));
123 std::string packageName = "packageName";
124 AppManager::GetInstance().RegisterCallerAppId(packageName);
125 auto result = AppManager::GetInstance().GetAppIdByPkgName(packageName, appId);
126 EXPECT_EQ(result, ERR_DM_FAILED);
127 AppManager::GetInstance().UnRegisterCallerAppId(packageName);
128 }
129
130 HWTEST_F(AppManagerTest, GetAppId_001, testing::ext::TestSize.Level2)
131 {
132 ASSERT_TRUE(skeleton_ != nullptr);
133 ASSERT_TRUE(token_ != nullptr);
134 ASSERT_TRUE(client_ != nullptr);
135 ASSERT_TRUE(accountManager_ != nullptr);
136
137 EXPECT_CALL(*skeleton_, GetCallingTokenID()).Times(AtLeast(INVOKE_COUNT));
138 EXPECT_CALL(*skeleton_, GetCallingUid()).Times(AtLeast(INVOKE_COUNT));
139 EXPECT_CALL(*token_, GetTokenTypeFlag(_)).WillRepeatedly(Return(ATokenTypeEnum::TOKEN_HAP));
140
141 auto bundleMgr = sptr<BundleMgrMock>(new (std::nothrow) BundleMgrMock());
142 auto systemAbilityManager = sptr<SystemAbilityManagerMock>(new (std::nothrow) SystemAbilityManagerMock());
143 size_t getSAMgrcallCount = 0;
144 size_t getSACallCount = 0;
145 size_t getForegroundOsAccountLocalIdCallCount = 0;
146 size_t getBInfoCallCount = 0;
147 ON_CALL(*client_, GetSystemAbilityManager())
__anon4c12afdc0202() 148 .WillByDefault(Invoke([&getSAMgrcallCount, systemAbilityManager]() -> sptr<ISystemAbilityManager> {
149 return (getSAMgrcallCount++ == 0) ? nullptr : systemAbilityManager;
150 }));
151 ON_CALL(*systemAbilityManager, GetSystemAbility(_))
__anon4c12afdc0302(int32_t) 152 .WillByDefault(Invoke([&getSACallCount, bundleMgr](int32_t) -> sptr<IRemoteObject> {
153 return (getSACallCount++ == 0) ? nullptr : bundleMgr;
154 }));
155 ON_CALL(*accountManager_, GetForegroundOsAccountLocalId(_))
__anon4c12afdc0402(int32_t &) 156 .WillByDefault(Invoke([&getForegroundOsAccountLocalIdCallCount](int32_t &) -> ErrCode {
157 return (getForegroundOsAccountLocalIdCallCount++ == 0) ? ERR_FAILED_VALUE : ERR_OK;
158 }));
159 ON_CALL(*bundleMgr, GetBundleInfoV9(_, _, _, _))
__anon4c12afdc0502(const std::string &, int32_t, BundleInfo &, int32_t) 160 .WillByDefault(Invoke([&getBInfoCallCount](const std::string &, int32_t, BundleInfo &, int32_t) -> ErrCode {
161 return (getBInfoCallCount++ == 0) ? ERR_FAILED_VALUE : ERR_OK;
162 }));
163 EXPECT_CALL(*client_, GetSystemAbilityManager()).Times(AtLeast(INVOKE_COUNT));
164 EXPECT_CALL(*systemAbilityManager, GetSystemAbility(_)).Times(AtLeast(INVOKE_COUNT));
165 EXPECT_CALL(*accountManager_, GetForegroundOsAccountLocalId(_)).Times(AtLeast(INVOKE_COUNT));
166 EXPECT_CALL(*bundleMgr, GetNameForUid(_, _)).Times(AtLeast(INVOKE_COUNT));
167 EXPECT_CALL(*bundleMgr, GetBundleInfoV9(_, _, _, _)).Times(AtLeast(INVOKE_COUNT));
168 for (size_t i = 0; i < 5; ++i) {
169 auto appId = AppManager::GetInstance().GetAppId();
170 EXPECT_TRUE(appId.empty());
171 }
172 }
173
174 HWTEST_F(AppManagerTest, IsSystemSA_001, testing::ext::TestSize.Level1)
175 {
176 ASSERT_TRUE(skeleton_ != nullptr);
177 ASSERT_TRUE(token_ != nullptr);
178 size_t getCallingTokenIDCallCount = 0;
179 size_t getTokenTypeFlagCallCount = 0;
180
181 ON_CALL(*skeleton_, GetCallingTokenID())
__anon4c12afdc0602() 182 .WillByDefault(Invoke([&getCallingTokenIDCallCount]() {
183 return (getCallingTokenIDCallCount++ == 0) ? UNVALUABLE_TOKEN_ID : VALUABLE_TOKEN_ID;
184 }));
185 ON_CALL(*token_, GetTokenTypeFlag(_))
__anon4c12afdc0702(AccessTokenID) 186 .WillByDefault(Invoke([&getTokenTypeFlagCallCount](AccessTokenID) -> ATokenTypeEnum {
187 return (getTokenTypeFlagCallCount++ == 0) ? ATokenTypeEnum::TOKEN_HAP : ATokenTypeEnum::TOKEN_NATIVE;
188 }));
189 EXPECT_CALL(*skeleton_, GetCallingTokenID()).Times(AtLeast(INVOKE_COUNT));
190 EXPECT_CALL(*token_, GetTokenTypeFlag(_)).Times(AtLeast(INVOKE_COUNT));
191 for (size_t i = 0; i < 3; ++i) {
192 auto ret = AppManager::GetInstance().IsSystemSA();
193 if (getCallingTokenIDCallCount > 1 && getTokenTypeFlagCallCount > 1) {
194 EXPECT_TRUE(ret);
195 } else {
196 EXPECT_FALSE(ret);
197 }
198 }
199 }
200
201 HWTEST_F(AppManagerTest, GetCallerName_001, testing::ext::TestSize.Level1)
202 {
203 ASSERT_TRUE(skeleton_ != nullptr);
204 ASSERT_TRUE(token_ != nullptr);
205 size_t getCallingTokenIDCallCount = 0;
206 size_t getHapTokenInfoCallCount = 0;
207
208 ON_CALL(*skeleton_, GetCallingTokenID())
__anon4c12afdc0802() 209 .WillByDefault(Invoke([&getCallingTokenIDCallCount]() {
210 return (getCallingTokenIDCallCount++ == 0) ? UNVALUABLE_TOKEN_ID : VALUABLE_TOKEN_ID;
211 }));
212 ON_CALL(*token_, GetHapTokenInfo(_, _))
__anon4c12afdc0902(AccessTokenID, HapTokenInfo &) 213 .WillByDefault(Invoke([&getHapTokenInfoCallCount](AccessTokenID, HapTokenInfo &) {
214 return (getHapTokenInfoCallCount++ == 0) ? ERR_FAILED_VALUE : ERR_OK;
215 }));
216 EXPECT_CALL(*skeleton_, GetCallingTokenID()).Times(AtLeast(INVOKE_COUNT));
217 EXPECT_CALL(*token_, GetTokenTypeFlag(_)).WillRepeatedly(Return(ATokenTypeEnum::TOKEN_HAP));
218 EXPECT_CALL(*token_, GetHapTokenInfo(_, _)).Times(AtLeast(INVOKE_COUNT));
219 for (size_t i = 0; i < 3; ++i) {
220 bool isSystemSA = false;
221 std::string output;
222 auto ret = AppManager::GetInstance().GetCallerName(isSystemSA, output);
223 if (getCallingTokenIDCallCount > 1 && getHapTokenInfoCallCount > 1) {
224 EXPECT_EQ(ret, DM_OK);
225 } else {
226 EXPECT_EQ(ret, ERR_DM_FAILED);
227 }
228 }
229 }
230
231 HWTEST_F(AppManagerTest, GetCallerName_002, testing::ext::TestSize.Level2)
232 {
233 ASSERT_TRUE(skeleton_ != nullptr);
234 ASSERT_TRUE(token_ != nullptr);
235
236 EXPECT_CALL(*skeleton_, GetCallingTokenID()).WillRepeatedly(Return(VALUABLE_TOKEN_ID));
237 EXPECT_CALL(*token_, GetTokenTypeFlag(_)).WillOnce(Return(ATokenTypeEnum::TOKEN_NATIVE));
238 EXPECT_CALL(*token_, GetNativeTokenInfo(_, _)).WillOnce(Return(ERR_FAILED_VALUE));
239 bool isSystemSA = true;
240 std::string output;
241 auto ret = AppManager::GetInstance().GetCallerName(isSystemSA, output);
242 EXPECT_EQ(ret, ERR_DM_FAILED);
243
244 EXPECT_CALL(*token_, GetTokenTypeFlag(_)).WillOnce(Return(ATokenTypeEnum::TOKEN_INVALID));
245 ret = AppManager::GetInstance().GetCallerName(isSystemSA, output);
246 EXPECT_EQ(ret, ERR_DM_FAILED);
247 }
248
249 HWTEST_F(AppManagerTest, GetNativeTokenIdByName_001, testing::ext::TestSize.Level1)
250 {
251 ASSERT_TRUE(token_ != nullptr);
252
253 EXPECT_CALL(*token_, GetNativeTokenId(_)).WillOnce(Return(UNVALUABLE_TOKEN_ID));
254 std::string processName;
255 int64_t tokenId = 0;
256 auto ret = AppManager::GetInstance().GetNativeTokenIdByName(processName, tokenId);
257 EXPECT_EQ(ret, ERR_DM_FAILED);
258
259 EXPECT_CALL(*token_, GetNativeTokenId(_)).WillOnce(Return(VALUABLE_TOKEN_ID));
260 ret = AppManager::GetInstance().GetNativeTokenIdByName(processName, tokenId);
261 EXPECT_EQ(ret, DM_OK);
262 EXPECT_EQ(tokenId, VALUABLE_TOKEN_ID);
263 }
264
265 HWTEST_F(AppManagerTest, GetHapTokenIdByName_001, testing::ext::TestSize.Level1)
266 {
267 ASSERT_TRUE(token_ != nullptr);
268 EXPECT_CALL(*token_, GetHapTokenID(_, _, _)).WillOnce(Return(UNVALUABLE_TOKEN_ID));
269 int32_t userId = 0;
270 std::string bundleName;
271 int32_t instIndex = 0;
272 int64_t tokenId = 0;
273 auto ret = AppManager::GetInstance().GetHapTokenIdByName(userId, bundleName, instIndex, tokenId);
274 EXPECT_EQ(ret, ERR_DM_FAILED);
275
276 EXPECT_CALL(*token_, GetHapTokenID(_, _, _)).WillOnce(Return(VALUABLE_TOKEN_ID));
277 ret = AppManager::GetInstance().GetHapTokenIdByName(userId, bundleName, instIndex, tokenId);
278 EXPECT_EQ(ret, DM_OK);
279 EXPECT_EQ(tokenId, VALUABLE_TOKEN_ID);
280 }
281
282 HWTEST_F(AppManagerTest, GetCallerProcessName_001, testing::ext::TestSize.Level1)
283 {
284 ASSERT_TRUE(skeleton_ != nullptr);
285 ASSERT_TRUE(token_ != nullptr);
286 size_t getCallingTokenIDCallCount = 0;
287 size_t getNativeTokenInfoCallCount = 0;
288
289 ON_CALL(*skeleton_, GetCallingTokenID())
__anon4c12afdc0a02() 290 .WillByDefault(Invoke([&getCallingTokenIDCallCount]() {
291 return (getCallingTokenIDCallCount++ == 0) ? UNVALUABLE_TOKEN_ID : VALUABLE_TOKEN_ID;
292 }));
293 ON_CALL(*token_, GetNativeTokenInfo(_, _))
__anon4c12afdc0b02(AccessTokenID, NativeTokenInfo &) 294 .WillByDefault(Invoke([&getNativeTokenInfoCallCount](AccessTokenID, NativeTokenInfo &) {
295 return (getNativeTokenInfoCallCount++ == 0) ? ERR_FAILED_VALUE : ERR_OK;
296 }));
297 EXPECT_CALL(*skeleton_, GetCallingTokenID()).Times(AtLeast(INVOKE_COUNT));
298 EXPECT_CALL(*token_, GetTokenTypeFlag(_)).WillRepeatedly(Return(ATokenTypeEnum::TOKEN_NATIVE));
299 EXPECT_CALL(*token_, GetNativeTokenInfo(_, _)).Times(AtLeast(INVOKE_COUNT));
300 for (size_t i = 0; i < 3; ++i) {
301 std::string output;
302 auto ret = AppManager::GetInstance().GetCallerProcessName(output);
303 if (getCallingTokenIDCallCount > 1 && getNativeTokenInfoCallCount > 1) {
304 EXPECT_EQ(ret, DM_OK);
305 } else {
306 EXPECT_EQ(ret, ERR_DM_FAILED);
307 }
308 }
309 }
310
311 HWTEST_F(AppManagerTest, GetCallerProcessName_002, testing::ext::TestSize.Level2)
312 {
313 ASSERT_TRUE(skeleton_ != nullptr);
314 ASSERT_TRUE(token_ != nullptr);
315 size_t GetHapTokenInfoCallCount = 0;
316
317 ON_CALL(*token_, GetHapTokenInfo(_, _))
__anon4c12afdc0c02(AccessTokenID, HapTokenInfo &) 318 .WillByDefault(Invoke([&GetHapTokenInfoCallCount](AccessTokenID, HapTokenInfo &) {
319 return (GetHapTokenInfoCallCount++ == 0) ? ERR_FAILED_VALUE : ERR_OK;
320 }));
321
322 EXPECT_CALL(*skeleton_, GetCallingTokenID()).WillRepeatedly(Return(VALUABLE_TOKEN_ID));
323 EXPECT_CALL(*skeleton_, GetCallingFullTokenID()).Times(AtLeast(INVOKE_COUNT));
324 EXPECT_CALL(*token_, GetTokenTypeFlag(_)).WillRepeatedly(Return(ATokenTypeEnum::TOKEN_HAP));
325 EXPECT_CALL(*token_, GetHapTokenInfo(_, _)).Times(AtLeast(INVOKE_COUNT));
326 std::string output;
327 for (size_t i = 0; i < 2; ++i) {
328 auto ret = AppManager::GetInstance().GetCallerProcessName(output);
329 EXPECT_EQ(ret, ERR_DM_FAILED);
330 }
331
332 EXPECT_CALL(*token_, GetTokenTypeFlag(_)).WillOnce(Return(ATokenTypeEnum::TOKEN_INVALID));
333 auto ret = AppManager::GetInstance().GetCallerProcessName(output);
334 EXPECT_EQ(ret, ERR_DM_FAILED);
335 }
336
337 HWTEST_F(AppManagerTest, GetBundleNameForSelf_001, testing::ext::TestSize.Level2)
338 {
339 ASSERT_TRUE(client_ != nullptr);
340 auto bundleMgr = sptr<BundleMgrMock>(new (std::nothrow) BundleMgrMock());
341 auto systemAbilityManager = sptr<SystemAbilityManagerMock>(new (std::nothrow) SystemAbilityManagerMock());
342 EXPECT_CALL(*systemAbilityManager, GetSystemAbility(_))
343 .Times(INVOKE_COUNT)
344 .WillOnce(Return(bundleMgr));
345 EXPECT_CALL(*client_, GetSystemAbilityManager())
346 .Times(INVOKE_COUNT)
347 .WillOnce(Return(systemAbilityManager));
348 EXPECT_CALL(*bundleMgr, GetBundleInfoForSelf(_, _))
349 .Times(INVOKE_COUNT)
350 .WillOnce(Return(ERR_OK));
351
352 std::string bundleName = "";
353 auto result = AppManager::GetInstance().GetBundleNameForSelf(bundleName);
354 EXPECT_EQ(result, DM_OK);
355 EXPECT_EQ(bundleName, "");
356 }
357 } // namespace DistributedHardware
358 } // namespace OHOS
359