• 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 "third_ability.h"
17 #include <iostream>
18 #include <numeric>
19 #include <sstream>
20 #include "app_log_wrapper.h"
21 #include "test_utils.h"
22 
23 namespace OHOS {
24 namespace AppExecFwk {
25 using namespace OHOS::EventFwk;
26 
27 namespace {
28 const std::string bundleName = "com.ohos.amsst.AppKit";
29 const std::string specialProcessName = "//\\@#$%^&*!~;:,.";
30 const std::string normalProcessName = "com.ohos.amsst.AppKit";
31 }  // namespace
32 
SubscribeEvent(const vector_conststr & eventList)33 void ThirdAbility::SubscribeEvent(const vector_conststr &eventList)
34 {
35     MatchingSkills matchingSkills;
36     for (const auto &e : eventList) {
37         matchingSkills.AddEvent(e);
38     }
39     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
40     subscribeInfo.SetPriority(1);
41     subscriber = std::make_shared<KitTestThirdEventSubscriber>(subscribeInfo, this);
42     CommonEventManager::SubscribeCommonEvent(subscriber);
43 }
44 
ProcessInfoStByCode(int apiIndex,int caseIndex,int code)45 void ThirdAbility::ProcessInfoStByCode(int apiIndex, int caseIndex, int code)
46 {
47     APP_LOGI("ThirdAbility::ProcessInfoStByCode");
48     if (mapStKitFunc_.find(apiIndex) != mapStKitFunc_.end() &&
49         static_cast<int>(mapStKitFunc_[apiIndex].size()) > caseIndex) {
50         mapStKitFunc_[apiIndex][caseIndex](code);
51     } else {
52         APP_LOGI("ProcessInfoStByCode error");
53     }
54 }
55 
CompareProcessInfo(const ProcessInfo & processInfo1,const ProcessInfo & processInfo2)56 bool ThirdAbility::CompareProcessInfo(const ProcessInfo &processInfo1, const ProcessInfo &processInfo2)
57 {
58     bool equalProcessName = (processInfo1.GetProcessName() == processInfo2.GetProcessName());
59     bool equalPid = (processInfo1.GetPid() == processInfo2.GetPid());
60     if (equalProcessName && equalPid) {
61         return true;
62     } else {
63         return false;
64     }
65 }
66 
GetParcelByProcessName(ProcessInfo & processInfo,const std::string & expectedString,int code)67 void ThirdAbility::GetParcelByProcessName(ProcessInfo &processInfo, const std::string &expectedString, int code)
68 {
69     Parcel parcel;
70     processInfo.Marshalling(parcel);
71     bool result = (expectedString == Str16ToStr8(parcel.ReadString16()));
72     TestUtils::PublishEvent(g_respPageThirdAbilityST, code, std::to_string(result));
73 }
74 
GetParcelByProcessID(ProcessInfo & processInfo,const int expectedInt,int code)75 void ThirdAbility::GetParcelByProcessID(ProcessInfo &processInfo, const int expectedInt, int code)
76 {
77     Parcel parcel;
78     processInfo.Marshalling(parcel);
79     bool result =
80         (processInfo.GetProcessName() == Str16ToStr8(parcel.ReadString16())) && (expectedInt == parcel.ReadInt32());
81     TestUtils::PublishEvent(g_respPageThirdAbilityST, code, std::to_string(result));
82 }
83 
GetProcessNameByParcel(const std::string & processName,int processID,int code)84 void ThirdAbility::GetProcessNameByParcel(const std::string &processName, int processID, int code)
85 {
86     Parcel parcel;
87     parcel.WriteString16(Str8ToStr16(processName));
88     parcel.WriteInt32(processID);
89     ProcessInfo *processInfo = ProcessInfo::Unmarshalling(parcel);
90     bool result = (processName == processInfo->GetProcessName());
91     TestUtils::PublishEvent(g_respPageThirdAbilityST, code, std::to_string(result));
92     delete processInfo;
93     processInfo = nullptr;
94 }
95 
GetProcessIDByParcel(const std::string & processName,int processID,int code)96 void ThirdAbility::GetProcessIDByParcel(const std::string &processName, int processID, int code)
97 {
98     Parcel parcel;
99     parcel.WriteString16(Str8ToStr16(processName));
100     parcel.WriteInt32(processID);
101     ProcessInfo *processInfo = ProcessInfo::Unmarshalling(parcel);
102     bool result = processInfo != nullptr;
103     if (processInfo) {
104         result = (processID == processInfo->GetPid());
105         delete processInfo;
106         processInfo = nullptr;
107     }
108     TestUtils::PublishEvent(g_respPageThirdAbilityST, code, std::to_string(result));
109 }
110 
ComparePidProcessName(ProcessInfo & processInfo,int expectedPid,const std::string & expectedName,int code)111 void ThirdAbility::ComparePidProcessName(
112     ProcessInfo &processInfo, int expectedPid, const std::string &expectedName, int code)
113 {
114     bool resultPid = (expectedPid == processInfo.GetPid());
115     bool resultName = (expectedName == processInfo.GetProcessName());
116     bool result = (resultPid && resultName);
117     TestUtils::PublishEvent(g_respPageThirdAbilityST, code, std::to_string(result));
118 }
119 
ProcessInfoGetPidCase1(int code)120 void ThirdAbility::ProcessInfoGetPidCase1(int code)
121 {
122     std::shared_ptr<ProcessInfo> processInfo = AbilityContext::GetProcessInfo();
123     bool result = processInfo != nullptr;
124     if (processInfo) {
125         result = (processInfo->GetPid() > 0);
126     }
127     TestUtils::PublishEvent(g_respPageThirdAbilityST, code, std::to_string(result));
128 }
129 
ProcessInfoGetPidCase2(int code)130 void ThirdAbility::ProcessInfoGetPidCase2(int code)
131 {
132     ProcessInfo processInfo;
133     bool result = (processInfo.GetPid() == 0);
134     TestUtils::PublishEvent(g_respPageThirdAbilityST, code, std::to_string(result));
135 }
136 
ProcessInfoGetPidCase3(int code)137 void ThirdAbility::ProcessInfoGetPidCase3(int code)
138 {
139     ProcessInfo processInfo(normalProcessName, std::numeric_limits<int>::min());
140     bool result = (processInfo.GetPid() == std::numeric_limits<int>::min());
141     TestUtils::PublishEvent(g_respPageThirdAbilityST, code, std::to_string(result));
142 }
143 
ProcessInfoGetPidCase4(int code)144 void ThirdAbility::ProcessInfoGetPidCase4(int code)
145 {
146     ProcessInfo processInfo(normalProcessName, std::numeric_limits<int>::max());
147     bool result = (processInfo.GetPid() == std::numeric_limits<int>::max());
148     TestUtils::PublishEvent(g_respPageThirdAbilityST, code, std::to_string(result));
149 }
150 
ProcessInfoGetProcessNameCase1(int code)151 void ThirdAbility::ProcessInfoGetProcessNameCase1(int code)
152 {
153     std::shared_ptr<ProcessInfo> processInfo = AbilityContext::GetProcessInfo();
154     bool result = (bundleName == processInfo->GetProcessName());
155     TestUtils::PublishEvent(g_respPageThirdAbilityST, code, std::to_string(result));
156 }
157 
ProcessInfoGetProcessNameCase2(int code)158 void ThirdAbility::ProcessInfoGetProcessNameCase2(int code)
159 {
160     ProcessInfo processInfo;
161     bool result = (std::string() == processInfo.GetProcessName());
162     TestUtils::PublishEvent(g_respPageThirdAbilityST, code, std::to_string(result));
163 }
164 
ProcessInfoGetProcessNameCase3(int code)165 void ThirdAbility::ProcessInfoGetProcessNameCase3(int code)
166 {
167     ProcessInfo processInfo("", 0);
168     bool result = (processInfo.GetProcessName() == "");
169     TestUtils::PublishEvent(g_respPageThirdAbilityST, code, std::to_string(result));
170 }
171 
ProcessInfoGetProcessNameCase4(int code)172 void ThirdAbility::ProcessInfoGetProcessNameCase4(int code)
173 {
174     ProcessInfo processInfo(specialProcessName, 0);
175     bool result = (specialProcessName == processInfo.GetProcessName());
176     TestUtils::PublishEvent(g_respPageThirdAbilityST, code, std::to_string(result));
177 }
178 
ProcessInfoMarshallingCase1(int code)179 void ThirdAbility::ProcessInfoMarshallingCase1(int code)
180 {
181     std::shared_ptr<ProcessInfo> processInfo = AbilityContext::GetProcessInfo();
182     Parcel parcel;
183     processInfo->Marshalling(parcel);
184     std::string processName = Str16ToStr8(parcel.ReadString16());
185     bool result = (processName == bundleName);
186     TestUtils::PublishEvent(g_respPageThirdAbilityST, code, std::to_string(result));
187 }
188 
ProcessInfoMarshallingCase2(int code)189 void ThirdAbility::ProcessInfoMarshallingCase2(int code)
190 {
191     std::shared_ptr<ProcessInfo> processInfo = AbilityContext::GetProcessInfo();
192     Parcel parcel;
193     processInfo->Marshalling(parcel);
194     bool result = (parcel.ReadInt32() > 0);
195     TestUtils::PublishEvent(g_respPageThirdAbilityST, code, std::to_string(result));
196 }
197 
ProcessInfoMarshallingCase3(int code)198 void ThirdAbility::ProcessInfoMarshallingCase3(int code)
199 {
200     ProcessInfo processInfo(specialProcessName, 0);
201     GetParcelByProcessName(processInfo, specialProcessName, code);
202 }
203 
ProcessInfoMarshallingCase4(int code)204 void ThirdAbility::ProcessInfoMarshallingCase4(int code)
205 {
206     ProcessInfo processInfo(normalProcessName, std::numeric_limits<int>::min());
207     GetParcelByProcessID(processInfo, std::numeric_limits<int>::min(), code);
208 }
209 
ProcessInfoMarshallingCase5(int code)210 void ThirdAbility::ProcessInfoMarshallingCase5(int code)
211 {
212     ProcessInfo processInfo("", 0);
213     GetParcelByProcessName(processInfo, "", code);
214 }
215 
ProcessInfoMarshallingCase6(int code)216 void ThirdAbility::ProcessInfoMarshallingCase6(int code)
217 {
218     ProcessInfo processInfo(normalProcessName, std::numeric_limits<int>::max());
219     GetParcelByProcessID(processInfo, std::numeric_limits<int>::max(), code);
220 }
221 
ProcessInfoUnmarshallingCase1(int code)222 void ThirdAbility::ProcessInfoUnmarshallingCase1(int code)
223 {
224     GetProcessNameByParcel(specialProcessName, 0, code);
225 }
226 
ProcessInfoUnmarshallingCase2(int code)227 void ThirdAbility::ProcessInfoUnmarshallingCase2(int code)
228 {
229     GetProcessNameByParcel(normalProcessName, 0, code);
230 }
231 
ProcessInfoUnmarshallingCase3(int code)232 void ThirdAbility::ProcessInfoUnmarshallingCase3(int code)
233 {
234     GetProcessNameByParcel("", 0, code);
235 }
236 
ProcessInfoUnmarshallingCase4(int code)237 void ThirdAbility::ProcessInfoUnmarshallingCase4(int code)
238 {
239     GetProcessIDByParcel(normalProcessName, 0, code);
240 }
241 
ProcessInfoUnmarshallingCase5(int code)242 void ThirdAbility::ProcessInfoUnmarshallingCase5(int code)
243 {
244     GetProcessIDByParcel(normalProcessName, std::numeric_limits<int>::min(), code);
245 }
246 
ProcessInfoUnmarshallingCase6(int code)247 void ThirdAbility::ProcessInfoUnmarshallingCase6(int code)
248 {
249     GetProcessIDByParcel(normalProcessName, std::numeric_limits<int>::max(), code);
250 }
251 
ProcessInfoUnmarshallingCase7(int code)252 void ThirdAbility::ProcessInfoUnmarshallingCase7(int code)
253 {
254     ProcessInfo processInfoIn(normalProcessName, std::numeric_limits<int>::max());
255     ProcessInfo *processInfoOut = nullptr;
256     Parcel in;
257     processInfoIn.Marshalling(in);
258     processInfoOut = ProcessInfo::Unmarshalling(in);
259     if (processInfoOut == nullptr) {
260         return;
261     }
262     TestUtils::PublishEvent(
263         g_respPageThirdAbilityST, code, std::to_string(CompareProcessInfo(processInfoIn, *processInfoOut)));
264     delete processInfoOut;
265     processInfoOut = nullptr;
266 }
267 
ProcessInfoUnmarshallingCase8(int code)268 void ThirdAbility::ProcessInfoUnmarshallingCase8(int code)
269 {
270     ProcessInfo processInfoIn(specialProcessName, std::numeric_limits<int>::max());
271     ProcessInfo *processInfoOut = nullptr;
272     Parcel in;
273     processInfoIn.Marshalling(in);
274     processInfoOut = ProcessInfo::Unmarshalling(in);
275     if (processInfoOut == nullptr) {
276         return;
277     }
278     TestUtils::PublishEvent(
279         g_respPageThirdAbilityST, code, std::to_string(CompareProcessInfo(processInfoIn, *processInfoOut)));
280     delete processInfoOut;
281     processInfoOut = nullptr;
282 }
283 
ProcessInfoUnmarshallingCase9(int code)284 void ThirdAbility::ProcessInfoUnmarshallingCase9(int code)
285 {
286     ProcessInfo processInfoIn(specialProcessName, std::numeric_limits<int>::min());
287     ProcessInfo *processInfoOut = nullptr;
288     Parcel in;
289     processInfoIn.Marshalling(in);
290     processInfoOut = ProcessInfo::Unmarshalling(in);
291     if (processInfoOut == nullptr) {
292         return;
293     }
294     TestUtils::PublishEvent(
295         g_respPageThirdAbilityST, code, std::to_string(CompareProcessInfo(processInfoIn, *processInfoOut)));
296     delete processInfoOut;
297     processInfoOut = nullptr;
298 }
299 
ProcessInfoProcessInfoCase1(int code)300 void ThirdAbility::ProcessInfoProcessInfoCase1(int code)
301 {
302     ProcessInfo processInfo;
303     bool result = (processInfo.GetPid() == 0);
304     TestUtils::PublishEvent(g_respPageThirdAbilityST, code, std::to_string(result));
305 }
306 
ProcessInfoProcessInfoCase2(int code)307 void ThirdAbility::ProcessInfoProcessInfoCase2(int code)
308 {
309     ProcessInfo processInfo;
310     bool result = (processInfo.GetProcessName().empty());
311     TestUtils::PublishEvent(g_respPageThirdAbilityST, code, std::to_string(result));
312 }
313 
ProcessInfoProcessInfoStringintCase1(int code)314 void ThirdAbility::ProcessInfoProcessInfoStringintCase1(int code)
315 {
316     ProcessInfo processInfo(normalProcessName, 0);
317     ComparePidProcessName(processInfo, 0, normalProcessName, code);
318 }
319 
ProcessInfoProcessInfoStringintCase2(int code)320 void ThirdAbility::ProcessInfoProcessInfoStringintCase2(int code)
321 {
322     ProcessInfo processInfo(normalProcessName, std::numeric_limits<int>::min());
323     ComparePidProcessName(processInfo, std::numeric_limits<int>::min(), normalProcessName, code);
324 }
325 
ProcessInfoProcessInfoStringintCase3(int code)326 void ThirdAbility::ProcessInfoProcessInfoStringintCase3(int code)
327 {
328     ProcessInfo processInfo(normalProcessName, std::numeric_limits<int>::max());
329     ComparePidProcessName(processInfo, std::numeric_limits<int>::max(), normalProcessName, code);
330 }
331 
ProcessInfoProcessInfoStringintCase4(int code)332 void ThirdAbility::ProcessInfoProcessInfoStringintCase4(int code)
333 {
334     ProcessInfo processInfo(specialProcessName, 0);
335     ComparePidProcessName(processInfo, 0, specialProcessName, code);
336 }
337 
ProcessInfoProcessInfoStringintCase5(int code)338 void ThirdAbility::ProcessInfoProcessInfoStringintCase5(int code)
339 {
340     ProcessInfo processInfo(specialProcessName, std::numeric_limits<int>::min());
341     ComparePidProcessName(processInfo, std::numeric_limits<int>::min(), specialProcessName, code);
342 }
343 
ProcessInfoProcessInfoStringintCase6(int code)344 void ThirdAbility::ProcessInfoProcessInfoStringintCase6(int code)
345 {
346     ProcessInfo processInfo(specialProcessName, std::numeric_limits<int>::max());
347     ComparePidProcessName(processInfo, std::numeric_limits<int>::max(), specialProcessName, code);
348 }
349 
OnStart(const Want & want)350 void ThirdAbility::OnStart(const Want &want)
351 {
352     APP_LOGI("ThirdAbility::onStart");
353     GetWantInfo(want);
354     Ability::OnStart(want);
355     SubscribeEvent(g_requPageThirdAbilitySTVector);
356     std::string eventData = GetAbilityName() + g_abilityStateOnStart;
357     TestUtils::PublishEvent(g_respPageThirdAbilityST, 0, eventData);
358 }
359 
OnStop()360 void ThirdAbility::OnStop()
361 {
362     APP_LOGI("ThirdAbility::onStop");
363     Ability::OnStop();
364     CommonEventManager::UnSubscribeCommonEvent(subscriber);
365     std::string eventData = GetAbilityName() + g_abilityStateOnStop;
366     TestUtils::PublishEvent(g_respPageThirdAbilityST, 0, eventData);
367 }
368 
OnActive()369 void ThirdAbility::OnActive()
370 {
371     APP_LOGI("ThirdAbility::OnActive");
372     Ability::OnActive();
373     std::string startBundleName = this->Split(targetBundle_, ",");
374     std::string startAabilityName = this->Split(targetAbility_, ",");
375     if (!startBundleName.empty() && !startAabilityName.empty()) {
376         Want want;
377         want.SetElementName(startBundleName, startAabilityName);
378         want.SetParam("shouldReturn", shouldReturn_);
379         if (!targetBundle_.empty() && !targetAbility_.empty()) {
380             want.SetParam("targetBundle", targetBundle_);
381             want.SetParam("targetAbility", targetAbility_);
382         }
383         StartAbility(want);
384     }
385     if (std::string::npos != shouldReturn_.find(GetAbilityName())) {
386         TerminateAbility();
387     }
388     Clear();
389     std::string eventData = GetAbilityName() + g_abilityStateOnActive;
390     TestUtils::PublishEvent(g_respPageThirdAbilityST, 0, eventData);
391 }
392 
OnInactive()393 void ThirdAbility::OnInactive()
394 {
395     APP_LOGI("ThirdAbility::OnInactive");
396     Ability::OnInactive();
397     std::string eventData = GetAbilityName() + g_abilityStateOnInactive;
398     TestUtils::PublishEvent(g_respPageThirdAbilityST, 0, eventData);
399 }
400 
OnBackground()401 void ThirdAbility::OnBackground()
402 {
403     APP_LOGI("ThirdAbility::OnBackground");
404     Ability::OnBackground();
405     std::string eventData = GetAbilityName() + g_abilityStateOnBackground;
406     TestUtils::PublishEvent(g_respPageThirdAbilityST, 0, eventData);
407 }
408 
OnForeground(const Want & want)409 void ThirdAbility::OnForeground(const Want &want)
410 {
411     APP_LOGI("ThirdAbility::OnForeground");
412     GetWantInfo(want);
413     Ability::OnForeground(want);
414     std::string eventData = GetAbilityName() + g_abilityStateOnForeground;
415     TestUtils::PublishEvent(g_respPageThirdAbilityST, 0, eventData);
416 }
417 
OnNewWant(const Want & want)418 void ThirdAbility::OnNewWant(const Want &want)
419 {
420     APP_LOGI("ThirdAbility::OnNewWant");
421     GetWantInfo(want);
422     Ability::OnNewWant(want);
423     std::string eventData = GetAbilityName() + g_abilityStateOnNewWant;
424     TestUtils::PublishEvent(g_respPageThirdAbilityST, 0, eventData);
425 }
426 
Clear()427 void ThirdAbility::Clear()
428 {
429     shouldReturn_ = "";
430     targetBundle_ = "";
431     targetAbility_ = "";
432 }
433 
GetWantInfo(const Want & want)434 void ThirdAbility::GetWantInfo(const Want &want)
435 {
436     Want mWant(want);
437     shouldReturn_ = mWant.GetStringParam("shouldReturn");
438     targetBundle_ = mWant.GetStringParam("targetBundle");
439     targetAbility_ = mWant.GetStringParam("targetAbility");
440 }
441 
Split(std::string & str,std::string delim)442 std::string ThirdAbility::Split(std::string &str, std::string delim)
443 {
444     std::string result;
445     if (!str.empty()) {
446         size_t index = str.find(delim);
447         if (index != std::string::npos) {
448             result = str.substr(0, index);
449             str = str.substr(index + delim.size());
450         } else {
451             result = str;
452             str = "";
453         }
454     }
455     return result;
456 }
457 
OnReceiveEvent(const CommonEventData & data)458 void KitTestThirdEventSubscriber::OnReceiveEvent(const CommonEventData &data)
459 {
460     APP_LOGI("KitTestEventSubscriber::OnReceiveEvent:event=%{public}s", data.GetWant().GetAction().c_str());
461     APP_LOGI("KitTestEventSubscriber::OnReceiveEvent:data=%{public}s", data.GetData().c_str());
462     APP_LOGI("KitTestEventSubscriber::OnReceiveEvent:code=%{public}d", data.GetCode());
463     auto eventName = data.GetWant().GetAction();
464     if (g_requPageThirdAbilityST == eventName) {
465         auto target = data.GetData();
466         auto handle = 0;
467         auto api = 1;
468         auto code = 2;
469         auto paramMinSize = 3;
470         vector_str splitResult = TestUtils::split(target, "_");
471         auto keyMap = splitResult.at(handle);
472         if (mapTestFunc_.find(keyMap) != mapTestFunc_.end() &&
473             splitResult.size() >= static_cast<unsigned int>(paramMinSize)) {
474             auto apiIndex = atoi(splitResult.at(api).c_str());
475             auto caseIndex = atoi(splitResult.at(code).c_str());
476             mapTestFunc_[keyMap](apiIndex, caseIndex, data.GetCode());
477         } else {
478             if (keyMap == "TerminateAbility") {
479                 KitTerminateAbility();
480             } else {
481                 APP_LOGI("OnReceiveEvent: CommonEventData error(%{public}s)", target.c_str());
482             }
483         }
484     }
485 }
486 
ProcessInfoStByCode(int apiIndex,int caseIndex,int code)487 void KitTestThirdEventSubscriber::ProcessInfoStByCode(int apiIndex, int caseIndex, int code)
488 {
489     if (thirdAbility_ != nullptr) {
490         thirdAbility_->ProcessInfoStByCode(apiIndex, caseIndex, code);
491     }
492 }
493 
KitTerminateAbility()494 void KitTestThirdEventSubscriber::KitTerminateAbility()
495 {
496     if (thirdAbility_ != nullptr) {
497         thirdAbility_->TerminateAbility();
498     }
499 }
500 
501 REGISTER_AA(ThirdAbility)
502 }  // namespace AppExecFwk
503 }  // namespace OHOS