1 /*
2 * Copyright (c) 2024 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 "mock_bundle_mgr_helper.h"
17
18 #include "hilog_tag_wrapper.h"
19
20 namespace OHOS {
21 namespace AppExecFwk {
22 namespace {
23 const int32_t TOKENID_LENGTH = 4;
IsDigitString(std::string name)24 bool IsDigitString(std::string name)
25 {
26 for (int i = 0; i < name.size(); i++) {
27 if (!isdigit(name[i])) {
28 return false;
29 }
30 }
31 return true;
32 }
33 }
34
BundleMgrHelper()35 BundleMgrHelper::BundleMgrHelper() {}
36
~BundleMgrHelper()37 BundleMgrHelper::~BundleMgrHelper()
38 {}
39
OnDeath()40 void BundleMgrHelper::OnDeath()
41 {}
42
GetSandboxBundleInfo(const std::string & bundleName,int32_t appIndex,int32_t userId,BundleInfo & info)43 ErrCode BundleMgrHelper::GetSandboxBundleInfo(
44 const std::string &bundleName, int32_t appIndex, int32_t userId, BundleInfo &info)
45 {
46 if (bundleName.size() < TOKENID_LENGTH) {
47 return -1;
48 }
49 auto tokenIdStr = bundleName.substr(bundleName.size() - TOKENID_LENGTH, TOKENID_LENGTH);
50 if (!IsDigitString(tokenIdStr)) {
51 return -1;
52 }
53 uint32_t tokenId = std::stoi(tokenIdStr);
54 info.applicationInfo.accessTokenId = tokenId;
55 return ERR_OK;
56 }
57
GetBundleInfo(const std::string & bundleName,const BundleFlag flags,BundleInfo & bundleInfo,int32_t userId)58 bool BundleMgrHelper::GetBundleInfo(const std::string &bundleName, const BundleFlag flags,
59 BundleInfo &bundleInfo, int32_t userId)
60 {
61 if (bundleName.size() < TOKENID_LENGTH) {
62 return false;
63 }
64 auto tokenIdStr = bundleName.substr(bundleName.size() - TOKENID_LENGTH, TOKENID_LENGTH);
65 if (!IsDigitString(tokenIdStr)) {
66 return false;
67 }
68 uint32_t tokenId = std::stoi(tokenIdStr);
69 bundleInfo.applicationInfo.accessTokenId = tokenId;
70 return true;
71 }
72
GetApplicationInfo(const std::string & appName,int32_t flags,int32_t userId,ApplicationInfo & appInfo)73 bool BundleMgrHelper::GetApplicationInfo(const std::string &appName, int32_t flags, int32_t userId,
74 ApplicationInfo &appInfo)
75 {
76 return true;
77 }
78
GetAppIdByBundleName(const std::string & bundleName,const int32_t userId)79 std::string BundleMgrHelper::GetAppIdByBundleName(const std::string &bundleName, const int32_t userId)
80 {
81 auto appId = bundleName + "_appId";
82 return appId;
83 }
84
GetCloneBundleInfo(const std::string & bundleName,int32_t flags,int32_t appCloneIndex,BundleInfo & bundleInfo,int32_t userId)85 ErrCode BundleMgrHelper::GetCloneBundleInfo(const std::string &bundleName, int32_t flags, int32_t appCloneIndex,
86 BundleInfo &bundleInfo, int32_t userId)
87 {
88 if (bundleName.size() < TOKENID_LENGTH) {
89 return -1;
90 }
91 auto tokenIdStr = bundleName.substr(bundleName.size() - TOKENID_LENGTH, TOKENID_LENGTH);
92 if (!IsDigitString(tokenIdStr)) {
93 return -1;
94 }
95 uint32_t tokenId = std::stoi(tokenIdStr);
96 bundleInfo.applicationInfo.accessTokenId = tokenId;
97 return ERR_OK;
98 }
99 } // namespace AppExecFwk
100 } // namespace OHOS