1 /*
2  * Copyright (C) 2022 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_manager.h"
17 #include <fstream>
18 #include <sstream>
19 #include <string>
20 #include "ability_config.h"
21 #include "ability_info.h"
22 #include "application_info.h"
23 #include "bundle_mgr_client.h"
24 #include "hilog_wrapper.h"
25 
26 using namespace OHOS::AAFwk;
27 namespace OHOS {
28 namespace {
29     const std::string ACCESSIBLE_ABILITY_JSON_FILE_PAT = "/system/app/dummy_accessibility_ability_config.json";
30 } // namespace
31 
32 namespace AppExecFwk {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)33 int BundleMgrStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
34 {
35     return 0;
36 }
37 
__anone94a0cbf0202(std::string bundleName, AbilityInfo& abilityInfo, ElementName& elementTemp) 38 auto HiWordInfo = [](std::string bundleName, AbilityInfo& abilityInfo, ElementName& elementTemp) {
39     abilityInfo.name = elementTemp.GetAbilityName();
40     abilityInfo.bundleName = elementTemp.GetBundleName();
41     abilityInfo.applicationInfo.bundleName = elementTemp.GetBundleName();
42     abilityInfo.applicationName = "Helloworld";
43     abilityInfo.applicationInfo.name = "Helloworld";
44     abilityInfo.type = AbilityType::PAGE;
45     abilityInfo.applicationInfo.isLauncherApp = true;
46     return true;
47 };
48 
__anone94a0cbf0302(std::string bundleName, AbilityInfo& abilityInfo, ElementName& elementTemp) 49 auto HiMusicInfo = [](std::string bundleName, AbilityInfo& abilityInfo, ElementName& elementTemp) {
50     abilityInfo.name = elementTemp.GetAbilityName();
51     abilityInfo.bundleName = elementTemp.GetBundleName();
52     abilityInfo.applicationInfo.bundleName = elementTemp.GetBundleName();
53     abilityInfo.applicationName = "hiMusic";
54     abilityInfo.applicationInfo.name = "hiMusic";
55     abilityInfo.type = AbilityType::PAGE;
56     abilityInfo.applicationInfo.isLauncherApp = false;
57 
58     if (elementTemp.GetAbilityName() == "MusicAbility") {
59         abilityInfo.process = "p1";
60         abilityInfo.launchMode = LaunchMode::STANDARD;
61     }
62     if (elementTemp.GetAbilityName() == "MusicTopAbility") {
63         abilityInfo.process = "p1";
64         abilityInfo.launchMode = LaunchMode::STANDARD;
65     }
66     if (elementTemp.GetAbilityName() == "MusicSAbility") {
67         abilityInfo.process = "p2";
68         abilityInfo.launchMode = LaunchMode::SINGLETON;
69     }
70     return true;
71 };
72 
__anone94a0cbf0402(std::string bundleName, AbilityInfo& abilityInfo, ElementName& elementTemp) 73 auto HiRadioInfo = [](std::string bundleName, AbilityInfo& abilityInfo, ElementName& elementTemp) {
74     abilityInfo.name = elementTemp.GetAbilityName();
75     abilityInfo.bundleName = elementTemp.GetBundleName();
76     abilityInfo.applicationInfo.bundleName = elementTemp.GetBundleName();
77     abilityInfo.applicationName = "hiRadio";
78     abilityInfo.applicationInfo.name = "hiRadio";
79     abilityInfo.type = AbilityType::PAGE;
80     abilityInfo.process = "p3";
81     if (elementTemp.GetAbilityName() == "RadioAbility") {
82         abilityInfo.launchMode = LaunchMode::STANDARD;
83     }
84     if (elementTemp.GetAbilityName() == "RadioTopAbility") {
85         abilityInfo.launchMode = LaunchMode::SINGLETON;
86     }
87     return true;
88 };
89 
__anone94a0cbf0502(std::string bundleName, AbilityInfo& abilityInfo, ElementName& elementTemp) 90 auto HiServiceInfo = [](std::string bundleName, AbilityInfo& abilityInfo, ElementName& elementTemp) {
91     abilityInfo.name = elementTemp.GetAbilityName();
92     abilityInfo.bundleName = elementTemp.GetBundleName();
93     abilityInfo.applicationInfo.bundleName = elementTemp.GetBundleName();
94     abilityInfo.applicationName = "hiService";
95     abilityInfo.applicationInfo.name = "hiService";
96     abilityInfo.type = AbilityType::SERVICE;
97     abilityInfo.process = "p4";
98     return true;
99 };
100 
__anone94a0cbf0602(std::string bundleName, AbilityInfo& abilityInfo, ElementName& elementTemp) 101 auto MusicServiceInfo = [](std::string bundleName, AbilityInfo& abilityInfo, ElementName& elementTemp) {
102     abilityInfo.name = elementTemp.GetAbilityName();
103     abilityInfo.bundleName = elementTemp.GetBundleName();
104     abilityInfo.applicationInfo.bundleName = elementTemp.GetBundleName();
105     abilityInfo.applicationName = "musicService";
106     abilityInfo.applicationInfo.name = "musicService";
107     abilityInfo.type = AbilityType::SERVICE;
108     abilityInfo.process = "p5";
109     return true;
110 };
111 
__anone94a0cbf0702(std::string bundleName, AbilityInfo& abilityInfo, ElementName& elementTemp) 112 auto HiDataInfo = [](std::string bundleName, AbilityInfo& abilityInfo, ElementName& elementTemp) {
113     abilityInfo.name = elementTemp.GetAbilityName();
114     abilityInfo.bundleName = elementTemp.GetBundleName();
115     abilityInfo.applicationInfo.bundleName = elementTemp.GetBundleName();
116     abilityInfo.applicationName = "hiData";
117     abilityInfo.applicationInfo.name = "hiData";
118     abilityInfo.type = AbilityType::DATA;
119     abilityInfo.process = "p6";
120     return true;
121 };
122 
BundleMgrService()123 BundleMgrService::BundleMgrService()
124 {
125     abilityInfoMap_.emplace(COM_IX_HIWORLD, HiWordInfo);
126     abilityInfoMap_.emplace(COM_IX_HIMUSIC, HiMusicInfo);
127     abilityInfoMap_.emplace(COM_IX_HIRADIO, HiRadioInfo);
128     abilityInfoMap_.emplace(COM_IX_HISERVICE, HiServiceInfo);
129     abilityInfoMap_.emplace(COM_IX_MUSICSERVICE, MusicServiceInfo);
130     abilityInfoMap_.emplace(COM_IX_HIDATA, HiDataInfo);
131 }
132 
GetBundleInfo(const std::string & bundleName,const BundleFlag flag,BundleInfo & bundleInfo,int32_t userId)133 bool BundleMgrService::GetBundleInfo(
134     const std::string& bundleName, const BundleFlag flag, BundleInfo& bundleInfo, int32_t userId)
135 {
136     return true;
137 }
138 
QueryAbilityInfo(const AAFwk::Want & want,AbilityInfo & abilityInfo)139 bool BundleMgrService::QueryAbilityInfo(const AAFwk::Want& want, AbilityInfo& abilityInfo)
140 {
141     HILOG_DEBUG(" mock BundleMgrService QueryAbilityInfo ------------ start");
142     if (CheckWantEntity(want, abilityInfo)) {
143         HILOG_DEBUG(" mock BundleMgrService CheckWantEntity true ------------ start");
144         return true;
145     }
146 
147     ElementName elementTemp = want.GetElement();
148     std::string abilityNameTemp = elementTemp.GetAbilityName();
149     std::string bundleNameTemp = elementTemp.GetBundleName();
150     abilityInfo.deviceId = elementTemp.GetDeviceID();
151 
152     if (bundleNameTemp.empty() || abilityNameTemp.empty()) {
153         return false;
154     }
155 
156     auto fun = abilityInfoMap_.find(bundleNameTemp);
157     if (fun != abilityInfoMap_.end()) {
158         auto call = fun->second;
159         if (call) {
160             call(bundleNameTemp, abilityInfo, elementTemp);
161             return true;
162         }
163     }
164     if (std::string::npos != elementTemp.GetBundleName().find("service")) {
165         abilityInfo.type = AppExecFwk::AbilityType::SERVICE;
166     }
167     abilityInfo.name = elementTemp.GetAbilityName();
168     abilityInfo.bundleName = elementTemp.GetBundleName();
169     abilityInfo.applicationName = elementTemp.GetBundleName();
170     abilityInfo.deviceId = elementTemp.GetDeviceID();
171     abilityInfo.applicationInfo.bundleName = elementTemp.GetBundleName();
172     abilityInfo.applicationInfo.name = "hello";
173     if (want.HasEntity(Want::ENTITY_HOME) && want.GetAction() == Want::ACTION_HOME) {
174         abilityInfo.applicationInfo.isLauncherApp = true;
175     } else {
176         abilityInfo.applicationInfo.isLauncherApp = false;
177         abilityInfo.applicationInfo.iconPath = "icon path";
178         abilityInfo.applicationInfo.label = "app label";
179     }
180     HILOG_DEBUG(" mock BundleMgrService QueryAbilityInfo ------------ end");
181     return true;
182 }
183 
GetApplicationInfo(const std::string & appName,const ApplicationFlag flag,const int userId,ApplicationInfo & appInfo)184 bool BundleMgrService::GetApplicationInfo(
185     const std::string& appName, const ApplicationFlag flag, const int userId, ApplicationInfo& appInfo)
186 {
187     if (appName.empty()) {
188         return false;
189     }
190     return true;
191 }
192 
CheckWantEntity(const AAFwk::Want & want,AbilityInfo & abilityInfo)193 bool BundleMgrService::CheckWantEntity(const AAFwk::Want& want, AbilityInfo& abilityInfo)
194 {
195     HILOG_DEBUG(" mock BundleMgrService QueryAbilityInfo CheckWantEntity ------------ start---------1");
196     auto entityVector = want.GetEntities();
197     ElementName element = want.GetElement();
198     if (entityVector.empty()) {
199         return false;
200     }
201 
202     auto find = false;
203     HILOG_DEBUG(" mock BundleMgrService QueryAbilityInfo CheckWantEntity ------------ start---------2");
204     if (std::any_of(entityVector.begin(), entityVector.end(),
205         [element, find](const std::string &entity) {
206             return entity == Want::FLAG_HOME_INTENT_FROM_SYSTEM && element.GetAbilityName().empty() &&
207             element.GetBundleName().empty();
208         })) {
209         find = true;
210     }
211 
212     HILOG_DEBUG(" mock BundleMgrService QueryAbilityInfo CheckWantEntity ------------ start---------3");
213     auto bundleName = element.GetBundleName();
214     auto abilityName = element.GetAbilityName();
215     if (find || (bundleName == AbilityConfig::SYSTEM_UI_BUNDLE_NAME &&
216                     (abilityName == AbilityConfig::SYSTEM_UI_STATUS_BAR ||
217                         abilityName == AbilityConfig::SYSTEM_UI_NAVIGATION_BAR))) {
218         GTEST_LOG_(INFO) << "QueryAbilityInfo ++> system luncher, find :" << find;
219         HILOG_DEBUG(" mock BundleMgrService QueryAbilityInfo CheckWantEntity ------------ start---------4");
220         return true;
221     }
222     HILOG_DEBUG(" mock BundleMgrService QueryAbilityInfo CheckWantEntity ------------ start---------5");
223     return false;
224 }
225 
BundleMgrClient()226 BundleMgrClient::BundleMgrClient()
227 {}
228 
~BundleMgrClient()229 BundleMgrClient::~BundleMgrClient()
230 {}
231 
GetResConfigFile(const ExtensionAbilityInfo & extensionInfo,const std::string & metadataName,std::vector<std::string> & profileInfos) const232 bool BundleMgrClient::GetResConfigFile(const ExtensionAbilityInfo& extensionInfo, const std::string& metadataName,
233     std::vector<std::string>& profileInfos) const
234 {
235     std::ifstream jsonFileStream;
236     jsonFileStream.open(ACCESSIBLE_ABILITY_JSON_FILE_PAT, std::ios::in);
237     if (!jsonFileStream.is_open()) {
238         HILOG_ERROR("Open json file failed.");
239         return false;
240     }
241 
242     std::ostringstream buf;
243     char ch;
244     while (buf && jsonFileStream.get(ch)) {
245         buf.put(ch);
246     }
247     jsonFileStream.close();
248 
249     profileInfos.push_back(buf.str());
250     return true;
251 }
252 } // namespace AppExecFwk
253 } // namespace OHOS