• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "ondemand_helper.h"
17 
18 #include <iostream>
19 #include <memory>
20 #include <thread>
21 #include <unistd.h>
22 
23 #include "datetime_ex.h"
24 #include "errors.h"
25 #include "if_system_ability_manager.h"
26 #include "ipc_types.h"
27 #include "iremote_object.h"
28 #include "iservice_registry.h"
29 #include "isystem_ability_load_callback.h"
30 #ifdef SUPPORT_ACCESS_TOKEN
31 #include "nativetoken_kit.h"
32 #include "token_setproc.h"
33 #endif
34 #include "sam_mock_permission.h"
35 #ifdef SUPPORT_SOFTBUS
36 #include "softbus_bus_center.h"
37 #endif
38 #include "system_ability_ondemand_reason.h"
39 #include "system_ability_definition.h"
40 #include "parameter.h"
41 #include "parameters.h"
42 
43 using namespace OHOS;
44 using namespace std;
45 
46 namespace OHOS {
47 namespace {
48 constexpr int32_t SLEEP_3_SECONDS = 3 * 1000 * 1000;
49 std::string g_inputTimeStr = "2023-10-9-10:00:00"; // time format
50 constexpr int32_t FIRST_NUM = 1;
51 constexpr int32_t SECOND_NUM = 2;
52 constexpr int32_t THIRD_NUM = 3;
53 constexpr int32_t FOURTH_NUM = 4;
54 constexpr int32_t FIFTH_NUM = 5;
55 constexpr int32_t SIXTH_NUM = 6;
56 constexpr int32_t SEVENTH_NUM = 7;
57 }
58 
TestLoad(OHOS::OnDemandHelper & ondemandHelper)59 void TestLoad(OHOS::OnDemandHelper& ondemandHelper)
60 {
61     cout << "input any word to start load test case 1" << endl;
62     ondemandHelper.LoadOndemandAbilityCase1();
63 
64     cout << "input any word to start load test case 2" << endl;
65     ::system("kill -9 `pidof listen_test`");
66     usleep(SLEEP_3_SECONDS);
67     ondemandHelper.LoadOndemandAbilityCase2();
68 
69     cout << "input any word to start load test case 3" << endl;
70     ::system("kill -9 `pidof listen_test`");
71     usleep(SLEEP_3_SECONDS);
72     ondemandHelper.LoadOndemandAbilityCase3();
73 
74     cout << "input any word to start load test case 4" << endl;
75     ::system("kill -9 `pidof listen_test`");
76     usleep(SLEEP_3_SECONDS);
77     ondemandHelper.LoadOndemandAbilityCase4();
78 
79     cout << "input any word to start load test case 6" << endl;
80     ::system("kill -9 `pidof listen_test`");
81     usleep(SLEEP_3_SECONDS);
82     ondemandHelper.LoadOndemandAbilityCase6();
83 
84     cout << "input any word to start load test case 7" << endl;
85     ::system("kill -9 `pidof listen_test`");
86     usleep(SLEEP_3_SECONDS);
87     ondemandHelper.LoadOndemandAbilityCase7();
88 
89     cout << "input any word to start load test case 8" << endl;
90     ::system("kill -9 `pidof listen_test`");
91     usleep(SLEEP_3_SECONDS);
92     ondemandHelper.LoadOndemandAbilityCase8();
93 
94     cout << "input any word to start load test case 9" << endl;
95     ::system("kill -9 `pidof listen_test`");
96     usleep(SLEEP_3_SECONDS);
97     ondemandHelper.LoadOndemandAbilityCase9();
98 
99     cout << "input any word to start load test case 10" << endl;
100     ::system("kill -9 `pidof listen_test`");
101     usleep(SLEEP_3_SECONDS);
102     ondemandHelper.LoadOndemandAbilityCase10();
103 
104     cout << "input any word to start load test case 11" << endl;
105     ::system("kill -9 `pidof listen_test`");
106     usleep(SLEEP_3_SECONDS);
107     ondemandHelper.LoadOndemandAbilityCase11();
108 }
109 
TestUnload(OHOS::OnDemandHelper & ondemandHelper)110 void TestUnload(OHOS::OnDemandHelper& ondemandHelper)
111 {
112     cout << "input any word to start unload test case 1" << endl;
113     ::system("kill -9 `pidof listen_test`");
114     usleep(SLEEP_3_SECONDS);
115     ondemandHelper.UnloadOndemandAbilityCase1();
116 
117     cout << "input any word to start unload test case 2" << endl;
118     ::system("kill -9 `pidof listen_test`");
119     usleep(SLEEP_3_SECONDS);
120     ondemandHelper.UnloadOndemandAbilityCase2();
121 
122     cout << "input any word to start unload test case 3" << endl;
123     ::system("kill -9 `pidof listen_test`");
124     usleep(SLEEP_3_SECONDS);
125     ondemandHelper.UnloadOndemandAbilityCase3();
126 
127     cout << "input any word to start unload test case 4" << endl;
128     ::system("kill -9 `pidof listen_test`");
129     usleep(SLEEP_3_SECONDS);
130     ondemandHelper.UnloadOndemandAbilityCase4();
131 
132     cout << "input any word to start unload test case 5" << endl;
133     ::system("kill -9 `pidof listen_test`");
134     usleep(SLEEP_3_SECONDS);
135     ondemandHelper.UnloadOndemandAbilityCase5();
136 
137     cout << "input any word to start unload test case 6" << endl;
138     ::system("kill -9 `pidof listen_test`");
139     usleep(SLEEP_3_SECONDS);
140     ondemandHelper.UnloadOndemandAbilityCase6();
141 
142     cout << "input any word to start unload test case 7" << endl;
143     ::system("kill -9 `pidof listen_test`");
144     usleep(SLEEP_3_SECONDS);
145     ondemandHelper.UnloadOndemandAbilityCase7();
146 
147     cout << "input any word to start unload test case 8" << endl;
148     ::system("kill -9 `pidof listen_test`");
149     usleep(SLEEP_3_SECONDS);
150     ondemandHelper.UnloadOndemandAbilityCase8();
151 
152     cout << "input any word to start unload test case 9" << endl;
153     ::system("kill -9 `pidof listen_test`");
154     usleep(SLEEP_3_SECONDS);
155     ondemandHelper.UnloadOndemandAbilityCase9();
156 }
157 
TestGet(OHOS::OnDemandHelper & ondemandHelper)158 void TestGet(OHOS::OnDemandHelper& ondemandHelper)
159 {
160     cout << "input any word to start get test case 1" << endl;
161     ::system("kill -9 `pidof listen_test`");
162     usleep(SLEEP_3_SECONDS);
163     ondemandHelper.GetOndemandAbilityCase1();
164 
165     cout << "input any word to start get test case 2" << endl;
166     ::system("kill -9 `pidof listen_test`");
167     usleep(SLEEP_3_SECONDS);
168     ondemandHelper.GetOndemandAbilityCase2();
169 
170     cout << "input any word to start get test case 3" << endl;
171     ::system("kill -9 `pidof listen_test`");
172     usleep(SLEEP_3_SECONDS);
173     ondemandHelper.GetOndemandAbilityCase3();
174 
175     cout << "input any word to start get test case 4" << endl;
176     ::system("kill -9 `pidof listen_test`");
177     usleep(SLEEP_3_SECONDS);
178     ondemandHelper.GetOndemandAbilityCase4();
179 
180     cout << "input any word to start get test case 5" << endl;
181     ::system("kill -9 `pidof listen_test`");
182     usleep(SLEEP_3_SECONDS);
183     ondemandHelper.GetOndemandAbilityCase5();
184 
185     cout << "input any word to start get test case 6" << endl;
186     ::system("kill -9 `pidof listen_test`");
187     usleep(SLEEP_3_SECONDS);
188     ondemandHelper.GetOndemandAbilityCase6();
189 }
190 
TestScheduler(OHOS::OnDemandHelper & ondemandHelper)191 void TestScheduler(OHOS::OnDemandHelper& ondemandHelper)
192 {
193     SamMockPermission::MockProcess("listen_test");
194     std::string pause;
195 
196     TestLoad(ondemandHelper);
197     TestUnload(ondemandHelper);
198     TestGet(ondemandHelper);
199 
200     cout << "all test case done" << endl;
201     ::system("kill -9 `pidof listen_test`");
202 }
203 }
204 
TestProcess(OHOS::OnDemandHelper & ondemandHelper,char * inputcmd)205 static void TestProcess(OHOS::OnDemandHelper& ondemandHelper, char* inputcmd)
206 {
207     std::string cmd = "";
208     cout << "please input proc test case(1-getp/2-initp)" << endl;
209     if (strcmp(inputcmd, "getp") == 0 || strcmp(inputcmd, "1") == 0) {
210         SamMockPermission::MockProcess("resource_schedule_service");
211         ondemandHelper.GetSystemProcess();
212     } else if (strcmp(inputcmd, "initp") == 0 || strcmp(inputcmd, "2") == 0) {
213         ondemandHelper.InitSystemProcessStatusChange();
214         SamMockPermission::MockProcess("resource_schedule_service");
215         ondemandHelper.SubscribeSystemProcess();
216         ::system("ps -e|grep media_service");
217         ::system("kill -9 $(pidof media_service)");
218         ::system("ps -e|grep media_service");
219 
220         sleep(1);
221         ondemandHelper.UnSubscribeSystemProcess();
222         ::system("ps -e|grep media_service");
223         ::system("kill -9 $(pidof media_service)");
224         ::system("ps -e|grep media_service");
225     } else {
226         cout << "invalid input" << endl;
227     }
228 }
229 
TestSystemAbility(OHOS::OnDemandHelper & ondemandHelper,char * inputcmd,char * inputsaid,char * inputOtherSaid,char * inputOtherDevice)230 static void TestSystemAbility(OHOS::OnDemandHelper& ondemandHelper, char* inputcmd, char* inputsaid,
231     char* inputOtherSaid, char* inputOtherDevice)
232 {
233     cout << "please input sa test case(1-get/2-load/3-unload/4-getinfo/5-syncload)" << endl;
234 #ifdef SUPPORT_SOFTBUS
235     std::string deviceId = ondemandHelper.GetFirstDevice();
236 #endif
237     int32_t systemAbilityId = atoi(inputsaid);
238     if (strcmp(inputcmd, "get") == 0 || strcmp(inputcmd, "1") == 0) {
239         ondemandHelper.GetSystemAbility(systemAbilityId);
240     } else if (strcmp(inputcmd, "load") == 0 || strcmp(inputcmd, "2") == 0) {
241         ondemandHelper.OnDemandAbility(systemAbilityId);
242 #ifdef SUPPORT_SOFTBUS
243     } else if (strcmp(inputcmd, "device") == 0) { // get remote networkid
244         ondemandHelper.GetDeviceList();
245     } else if (strcmp(inputcmd, "loadrmt1") == 0) { // single thread with one device, one system ability, one callback
246         ondemandHelper.LoadRemoteAbility(systemAbilityId, deviceId, nullptr);
247     } else if (strcmp(inputcmd, "loadrmt2") == 0) { // one device, one system ability, one callback, three threads
248         ondemandHelper.LoadRemoteAbilityMuti(systemAbilityId, deviceId);
249     } else if (strcmp(inputcmd, "loadrmt3") == 0) { // one device, one system ability, three callbacks, three threads
250         ondemandHelper.LoadRemoteAbilityMutiCb(systemAbilityId, deviceId);
251     } else if (strcmp(inputcmd, "loadrmt4") == 0) { // one device, three system abilities, one callback, three threads
252         ondemandHelper.LoadRemoteAbilityMutiSA(systemAbilityId, deviceId);
253     } else if (strcmp(inputcmd, "loadrmt5") == 0) {
254         // one device, three system abilities, three callbacks, three threads
255         ondemandHelper.LoadRemoteAbilityMutiSACb(systemAbilityId, deviceId);
256     } else if (strcmp(inputcmd, "loadrmt6") == 0) { // two devices
257         int32_t otherSystemAbilityId = atoi(inputOtherSaid);
258         std::string otherDevice = inputOtherDevice;
259         ondemandHelper.LoadRemoteAbility(systemAbilityId, deviceId, nullptr);
260         ondemandHelper.LoadRemoteAbility(otherSystemAbilityId, otherDevice, nullptr);
261     } else if (strcmp(inputcmd, "loadmuti") == 0) {
262         ondemandHelper.LoadRemoteAbilityPressure(systemAbilityId, deviceId);
263 #endif
264     } else if (strcmp(inputcmd, "unload") == 0 || strcmp(inputcmd, "3") == 0) {
265         ondemandHelper.UnloadSystemAbility(systemAbilityId);
266     } else if (strcmp(inputcmd, "getinfo") == 0 || strcmp(inputcmd, "4") == 0) {
267         ondemandHelper.GetSystemProcessInfo(systemAbilityId);
268     } else if (strcmp(inputcmd, "syncload") == 0 || strcmp(inputcmd, "5") == 0) {
269         ondemandHelper.TestSyncOnDemandAbility(systemAbilityId);
270     } else {
271         cout << "invalid inputcmd" << endl;
272     }
273 }
274 
TestParamPlugin(OHOS::OnDemandHelper & ondemandHelper,char * input)275 static void TestParamPlugin(OHOS::OnDemandHelper& ondemandHelper, char* input)
276 {
277     cout << "please input param's value" << endl;
278     if (strcmp(input, "false") == 0) {
279         int ret = SetParameter("persist.samgr.deviceparam", "false");
280         cout << "ret = " << ret <<endl;
281     } else if (strcmp(input, "true") == 0) {
282         int ret = SetParameter("persist.samgr.deviceparam", "true");
283         cout << "ret = " << ret <<endl;
284     } else {
285         cout << "invalid input" << endl;
286     }
287 }
288 
CreateOnDemandStartPolicy(SystemAbilityOnDemandEvent & event,int eventId)289 static void CreateOnDemandStartPolicy(SystemAbilityOnDemandEvent& event, int eventId)
290 {
291     cout << "please input on demand event id:" << endl;
292     cout << "1:deviceonline on" << endl;
293     cout << "2:wifi_status on" << endl;
294     cout << "3:persist.samgr.deviceparam true" << endl;
295     cout << "4:usual.event.SCREEN_ON" << endl;
296     cout << "5:loopevent 60" << endl;
297     if (eventId == static_cast<int32_t>(OnDemandEventId::DEVICE_ONLINE)) {
298         event.eventId = OnDemandEventId::DEVICE_ONLINE;
299         event.name = "deviceonline";
300         event.value = "on";
301     } else if (eventId == static_cast<int32_t>(OnDemandEventId::SETTING_SWITCH)) {
302         event.eventId = OnDemandEventId::SETTING_SWITCH;
303         event.name = "wifi_status";
304         event.value = "on";
305     } else if (eventId == static_cast<int32_t>(OnDemandEventId::PARAM)) {
306         event.eventId = OnDemandEventId::PARAM;
307         event.name = "persist.samgr.deviceparam";
308         event.value = "true";
309     } else if (eventId == static_cast<int32_t>(OnDemandEventId::COMMON_EVENT)) {
310         event.eventId = OnDemandEventId::COMMON_EVENT;
311         event.name = "usual.event.SCREEN_ON";
312         event.value = "";
313     } else if (eventId == static_cast<int32_t>(OnDemandEventId::TIMED_EVENT)) {
314         event.eventId = OnDemandEventId::TIMED_EVENT;
315         event.name = "loopevent";
316         event.value = "60";
317     }
318 }
319 
CreateOnDemandStopPolicy(SystemAbilityOnDemandEvent & event,int eventId)320 static void CreateOnDemandStopPolicy(SystemAbilityOnDemandEvent& event, int eventId)
321 {
322     cout << "please input on demand event id:" << endl;
323     cout << "1:deviceonline off" << endl;
324     cout << "2:wifi_status off" << endl;
325     cout << "3:persist.samgr.deviceparam false" << endl;
326     cout << "4:usual.event.SCREEN_OFF" << endl;
327     cout << "5:loopevent 70" << endl;
328     if (eventId == static_cast<int32_t>(OnDemandEventId::DEVICE_ONLINE)) {
329         event.eventId = OnDemandEventId::DEVICE_ONLINE;
330         event.name = "deviceonline";
331         event.value = "off";
332     } else if (eventId == static_cast<int32_t>(OnDemandEventId::SETTING_SWITCH)) {
333         event.eventId = OnDemandEventId::SETTING_SWITCH;
334         event.name = "wifi_status";
335         event.value = "off";
336     } else if (eventId == static_cast<int32_t>(OnDemandEventId::PARAM)) {
337         event.eventId = OnDemandEventId::PARAM;
338         event.name = "persist.samgr.deviceparam";
339         event.value = "false";
340     } else if (eventId == static_cast<int32_t>(OnDemandEventId::COMMON_EVENT)) {
341         event.eventId = OnDemandEventId::COMMON_EVENT;
342         event.name = "usual.event.SCREEN_OFF";
343         event.value = "";
344     } else if (eventId == static_cast<int32_t>(OnDemandEventId::TIMED_EVENT)) {
345         event.eventId = OnDemandEventId::TIMED_EVENT;
346         event.name = "loopevent";
347         event.value = "70";
348     }
349 }
350 
TestOnDemandPolicy(OHOS::OnDemandHelper & ondemandHelper,char * argv[])351 static void TestOnDemandPolicy(OHOS::OnDemandHelper& ondemandHelper, char *argv[])
352 {
353     char* inputcmd = argv[SECOND_NUM];
354     char* inputtype = argv[THIRD_NUM];
355     char* inputsaid = argv[FOURTH_NUM];
356     char* inputeventid = argv[FIFTH_NUM];
357     char* anotherinputeventid = argv[SIXTH_NUM];
358     cout << "please input on demand policy test case(get/update)" << endl;
359     cout << "please input on demand type test case(start/stop/start_multi/stop_multi)" << endl;
360     SystemAbilityOnDemandEvent event;
361     SystemAbilityOnDemandEvent event2;
362     std::vector<SystemAbilityOnDemandEvent> abilityOnDemandEvents;
363     int32_t systemAbilityId = atoi(inputsaid);
364     if (strcmp(inputcmd, "get") == 0 && strcmp(inputtype, "start") == 0) {
365         ondemandHelper.GetOnDemandPolicy(systemAbilityId, OnDemandPolicyType::START_POLICY);
366     } else if (strcmp(inputcmd, "get") == 0 && strcmp(inputtype, "stop") == 0) {
367         ondemandHelper.GetOnDemandPolicy(systemAbilityId, OnDemandPolicyType::STOP_POLICY);
368     } else if (strcmp(inputcmd, "update") == 0 && strcmp(inputtype, "start") == 0) {
369         int eventId = atoi(inputeventid);
370         CreateOnDemandStartPolicy(event, eventId);
371         abilityOnDemandEvents.push_back(event);
372         ondemandHelper.UpdateOnDemandPolicy(systemAbilityId, OnDemandPolicyType::START_POLICY, abilityOnDemandEvents);
373     } else if (strcmp(inputcmd, "update") == 0 && strcmp(inputtype, "start_multi") == 0) {
374         int eventId = atoi(inputeventid);
375         int anothereventId = atoi(anotherinputeventid);
376         CreateOnDemandStartPolicy(event, eventId);
377         CreateOnDemandStartPolicy(event2, anothereventId);
378         abilityOnDemandEvents.push_back(event);
379         abilityOnDemandEvents.push_back(event2);
380         ondemandHelper.UpdateOnDemandPolicy(systemAbilityId, OnDemandPolicyType::START_POLICY, abilityOnDemandEvents);
381     } else if (strcmp(inputcmd, "update") == 0 && strcmp(inputtype, "stop") == 0) {
382         int eventId = atoi(inputeventid);
383         CreateOnDemandStopPolicy(event, eventId);
384         abilityOnDemandEvents.push_back(event);
385         ondemandHelper.UpdateOnDemandPolicy(systemAbilityId, OnDemandPolicyType::STOP_POLICY, abilityOnDemandEvents);
386     } else if (strcmp(inputcmd, "update") == 0 && strcmp(inputtype, "stop_multi") == 0) {
387         int eventId = atoi(inputeventid);
388         int anothereventId = atoi(anotherinputeventid);
389         CreateOnDemandStopPolicy(event, eventId);
390         CreateOnDemandStopPolicy(event2, anothereventId);
391         abilityOnDemandEvents.push_back(event);
392         abilityOnDemandEvents.push_back(event2);
393         ondemandHelper.UpdateOnDemandPolicy(systemAbilityId, OnDemandPolicyType::STOP_POLICY, abilityOnDemandEvents);
394     } else {
395         cout << "invalid input" << endl;
396     }
397 }
398 
CreateOnDemandStartPolicy1(SystemAbilityOnDemandEvent & event,int eventId)399 static void CreateOnDemandStartPolicy1(SystemAbilityOnDemandEvent& event, int eventId)
400 {
401     cout << "please input on demand event id:" << endl;
402     cout << "1:deviceonline on" << endl;
403     cout << "2:wifi_status on" << endl;
404     cout << "3:persist.samgr.deviceparam true" << endl;
405     cout << "4:usual.event.SCREEN_ON" << endl;
406     cout << "5:timedevent" << endl;
407     if (eventId == static_cast<int32_t>(OnDemandEventId::DEVICE_ONLINE)) {
408         event.eventId = OnDemandEventId::DEVICE_ONLINE;
409         event.name = "deviceonline";
410         event.value = "on";
411     } else if (eventId == static_cast<int32_t>(OnDemandEventId::SETTING_SWITCH)) {
412         event.eventId = OnDemandEventId::SETTING_SWITCH;
413         event.name = "wifi_status";
414         event.value = "on";
415     } else if (eventId == static_cast<int32_t>(OnDemandEventId::PARAM)) {
416         event.eventId = OnDemandEventId::PARAM;
417         event.name = "persist.samgr.deviceparam";
418         event.value = "true";
419     } else if (eventId == static_cast<int32_t>(OnDemandEventId::COMMON_EVENT)) {
420         event.eventId = OnDemandEventId::COMMON_EVENT;
421         event.name = "usual.event.SCREEN_ON";
422         event.value = "";
423     } else if (eventId == static_cast<int32_t>(OnDemandEventId::TIMED_EVENT)) {
424         event.eventId = OnDemandEventId::TIMED_EVENT;
425         event.name = "timedevent";
426         event.value = g_inputTimeStr;
427         event.persistence = true;
428     }
429 }
430 
CreateOnDemandStopPolicy1(SystemAbilityOnDemandEvent & event,int eventId)431 static void CreateOnDemandStopPolicy1(SystemAbilityOnDemandEvent& event, int eventId)
432 {
433     cout << "please input on demand event id:" << endl;
434     cout << "1:deviceonline off" << endl;
435     cout << "2:wifi_status off" << endl;
436     cout << "3:persist.samgr.deviceparam false" << endl;
437     cout << "4:usual.event.SCREEN_OFF" << endl;
438     cout << "5:timedevent" << endl;
439     if (eventId == static_cast<int32_t>(OnDemandEventId::DEVICE_ONLINE)) {
440         event.eventId = OnDemandEventId::DEVICE_ONLINE;
441         event.name = "deviceonline";
442         event.value = "off";
443     } else if (eventId == static_cast<int32_t>(OnDemandEventId::SETTING_SWITCH)) {
444         event.eventId = OnDemandEventId::SETTING_SWITCH;
445         event.name = "wifi_status";
446         event.value = "off";
447     } else if (eventId == static_cast<int32_t>(OnDemandEventId::PARAM)) {
448         event.eventId = OnDemandEventId::PARAM;
449         event.name = "persist.samgr.deviceparam";
450         event.value = "false";
451     } else if (eventId == static_cast<int32_t>(OnDemandEventId::COMMON_EVENT)) {
452         event.eventId = OnDemandEventId::COMMON_EVENT;
453         event.name = "usual.event.SCREEN_OFF";
454         event.value = "";
455     } else if (eventId == static_cast<int32_t>(OnDemandEventId::TIMED_EVENT)) {
456         event.eventId = OnDemandEventId::TIMED_EVENT;
457         event.name = "timedevent";
458         event.value = g_inputTimeStr;
459         event.persistence = true;
460     }
461 }
462 
TestOnDemandPolicy1(OHOS::OnDemandHelper & ondemandHelper,char * argv[])463 static void TestOnDemandPolicy1(OHOS::OnDemandHelper& ondemandHelper, char *argv[])
464 {
465     g_inputTimeStr = argv[SECOND_NUM];
466     char* inputcmd = argv[THIRD_NUM];
467     char* inputtype = argv[FOURTH_NUM];
468     char* inputsaid = argv[FIFTH_NUM];
469     char* inputeventid = argv[SIXTH_NUM];
470     char* anotherinputeventid = argv[SEVENTH_NUM];
471     cout << "please input on demand policy test case(get/update)" << endl;
472     cout << "please input on demand type test case(start/stop)" << endl;
473     int32_t systemAbilityId = atoi(inputsaid);
474     SystemAbilityOnDemandEvent event;
475     SystemAbilityOnDemandEvent event2;
476     std::vector<SystemAbilityOnDemandEvent> abilityOnDemandEvents;
477     if (strcmp(inputcmd, "get") == 0 && strcmp(inputtype, "start") == 0) {
478         ondemandHelper.GetOnDemandPolicy(systemAbilityId, OnDemandPolicyType::START_POLICY);
479     } else if (strcmp(inputcmd, "get") == 0 && strcmp(inputtype, "stop") == 0) {
480         ondemandHelper.GetOnDemandPolicy(systemAbilityId, OnDemandPolicyType::STOP_POLICY);
481     } else if (strcmp(inputcmd, "update") == 0 && strcmp(inputtype, "start") == 0) {
482         int eventId = atoi(inputeventid);
483         CreateOnDemandStartPolicy1(event, eventId);
484         abilityOnDemandEvents.push_back(event);
485         ondemandHelper.UpdateOnDemandPolicy(systemAbilityId, OnDemandPolicyType::START_POLICY, abilityOnDemandEvents);
486     } else if (strcmp(inputcmd, "update") == 0 && strcmp(inputtype, "start_multi") == 0) {
487         int eventId = atoi(inputeventid);
488         int anothereventId = atoi(anotherinputeventid);
489         CreateOnDemandStartPolicy1(event, eventId);
490         CreateOnDemandStartPolicy1(event2, anothereventId);
491         abilityOnDemandEvents.push_back(event);
492         abilityOnDemandEvents.push_back(event2);
493         ondemandHelper.UpdateOnDemandPolicy(systemAbilityId, OnDemandPolicyType::START_POLICY, abilityOnDemandEvents);
494     } else if (strcmp(inputcmd, "update") == 0 && strcmp(inputtype, "stop") == 0) {
495         int eventId = atoi(inputeventid);
496         CreateOnDemandStopPolicy1(event, eventId);
497         abilityOnDemandEvents.push_back(event);
498         ondemandHelper.UpdateOnDemandPolicy(systemAbilityId, OnDemandPolicyType::STOP_POLICY, abilityOnDemandEvents);
499     } else if (strcmp(inputcmd, "update") == 0 && strcmp(inputtype, "stop_multi") == 0) {
500         int eventId = atoi(inputeventid);
501         int anothereventId = atoi(anotherinputeventid);
502         CreateOnDemandStopPolicy1(event, eventId);
503         CreateOnDemandStopPolicy1(event2, anothereventId);
504         abilityOnDemandEvents.push_back(event);
505         abilityOnDemandEvents.push_back(event2);
506         ondemandHelper.UpdateOnDemandPolicy(systemAbilityId, OnDemandPolicyType::STOP_POLICY, abilityOnDemandEvents);
507     } else {
508         cout << "invalid input" << endl;
509     }
510 }
511 
TestCommonEvent(OHOS::OnDemandHelper & ondemandHelper,char * inputcmd,char * inputsaid,char * inputEvent)512 static void TestCommonEvent(OHOS::OnDemandHelper& ondemandHelper, char* inputcmd, char* inputsaid, char* inputEvent)
513 {
514     cout << "please input common event test case(1 get/2 get_with_event)" << endl;
515     cout << "please input systemAbilityId for 1/2 operation" << endl;
516     int32_t saId = atoi(inputsaid);
517     if (strcmp(inputcmd, "1") == 0) {
518         ondemandHelper.GetCommonEventExtraId(saId, "");
519     } else if (strcmp(inputcmd, "2") == 0) {
520         cout << "please input common event name" << endl;
521         std::string eventName = inputEvent;
522         ondemandHelper.GetCommonEventExtraId(saId, eventName);
523     } else {
524         cout << "invalid input" << endl;
525     }
526 }
527 
TestGetExtension(OHOS::OnDemandHelper & ondemandHelper,char * input)528 static void TestGetExtension(OHOS::OnDemandHelper& ondemandHelper, char* input)
529 {
530     std::string extension = input;
531     std::vector<int32_t> saIds;
532     if (ondemandHelper.GetExtensionSaIds(extension, saIds) != ERR_OK) {
533         cout << "get extension: " << extension << " failed" << endl;
534         return;
535     }
536     std::vector<sptr<IRemoteObject>> saList;
537     if (ondemandHelper.GetExtensionRunningSaList(extension, saList) != ERR_OK) {
538         cout << "get handle extension: " << extension << " failed" << endl;
539         return;
540     }
541     return;
542 }
543 
TestCheckSystemAbility(OHOS::OnDemandHelper & ondemandHelper,char * inputcmd,char * inputsaid)544 static void TestCheckSystemAbility(OHOS::OnDemandHelper& ondemandHelper, char* inputcmd, char* inputsaid)
545 {
546 #ifdef SUPPORT_SOFTBUS
547     std::string cmd = "";
548     cout << "please input check case(local/remote)" << endl;
549 #endif
550     int32_t saId = 0;
551     cout << "please input systemAbilityId" << endl;
552     saId = atoi(inputsaid);
553 #ifdef SUPPORT_SOFTBUS
554     if (strcmp(inputcmd, "local") == 0) {
555         ondemandHelper.CheckSystemAbility(saId);
556     } else if (strcmp(inputcmd, "remote") == 0) {
557         std::string deviceId = ondemandHelper.GetFirstDevice();
558         ondemandHelper.CheckSystemAbility(saId, deviceId);
559     } else {
560         cout << "invalid input" << endl;
561     }
562 #else
563     ondemandHelper.CheckSystemAbility(saId);
564 #endif
565 }
566 
TestMemory(OHOS::OnDemandHelper & ondemandHelper,char * argv[])567 static void TestMemory(OHOS::OnDemandHelper& ondemandHelper, char* argv[])
568 {
569     if (strcmp(argv[SECOND_NUM], "unoadall") == 0 || strcmp(argv[SECOND_NUM], "1") == 0) {
570         ondemandHelper.UnloadAllIdleSystemAbility();
571     } else if (strcmp(argv[SECOND_NUM], "getlru") == 0 || strcmp(argv[SECOND_NUM], "2") == 0) {
572         ondemandHelper.GetLruIdleSystemAbilityProc();
573     } else if (strcmp(argv[SECOND_NUM], "unloadprocess") == 0 || strcmp(argv[SECOND_NUM], "3") == 0) {
574         vector<u16string> processList;
575         for (int i = THIRD_NUM; i < ondemandHelper.argc_; ++i) {
576             processList.push_back(Str8ToStr16(string(argv[i])));
577         }
578         ondemandHelper.UnloadProcess(processList);
579     } else {
580         cout << "invalid input" << endl;
581     }
582 }
583 
TestIntCommand(OHOS::OnDemandHelper & ondemandHelper,char * argv[])584 static void TestIntCommand(OHOS::OnDemandHelper& ondemandHelper, char* argv[])
585 {
586     if (strcmp(argv[FIRST_NUM], "1") == 0) {
587         TestParamPlugin(ondemandHelper, argv[SECOND_NUM]);
588     } else if (strcmp(argv[FIRST_NUM], "2") == 0) {
589         TestSystemAbility(ondemandHelper, argv[SECOND_NUM], argv[THIRD_NUM], argv[FOURTH_NUM], argv[FIFTH_NUM]);
590     } else if (strcmp(argv[FIRST_NUM], "3") == 0) {
591         TestProcess(ondemandHelper, argv[SECOND_NUM]);
592     } else if (strcmp(argv[FIRST_NUM], "4") == 0) {
593         TestOnDemandPolicy(ondemandHelper, argv);
594     } else if (strcmp(argv[FIRST_NUM], "5") == 0) {
595         TestGetExtension(ondemandHelper, argv[SECOND_NUM]);
596     } else if (strcmp(argv[FIRST_NUM], "6") == 0) {
597         TestCommonEvent(ondemandHelper, argv[SECOND_NUM], argv[THIRD_NUM], argv[FOURTH_NUM]);
598     } else if (strcmp(argv[FIRST_NUM], "7") == 0) {
599         TestCheckSystemAbility(ondemandHelper, argv[SECOND_NUM], argv[THIRD_NUM]);
600     } else if (strcmp(argv[FIRST_NUM], "8") == 0) {
601         TestOnDemandPolicy1(ondemandHelper, argv);
602     } else if (strcmp(argv[FIRST_NUM], "9") == 0) {
603         TestScheduler(ondemandHelper);
604     } else if (strcmp(argv[FIRST_NUM], "10") == 0) {
605         TestMemory(ondemandHelper, argv);
606     } else {
607         cout << "invalid input" << endl;
608     }
609 }
610 
TestStringCommand(OHOS::OnDemandHelper & ondemandHelper,char * argv[])611 static void TestStringCommand(OHOS::OnDemandHelper& ondemandHelper, char* argv[])
612 {
613     if (strcmp(argv[FIRST_NUM], "param") == 0) {
614         TestParamPlugin(ondemandHelper, argv[SECOND_NUM]);
615     } else if (strcmp(argv[FIRST_NUM], "sa") == 0) {
616         TestSystemAbility(ondemandHelper, argv[SECOND_NUM], argv[THIRD_NUM], argv[FOURTH_NUM], argv[FIFTH_NUM]);
617     } else if (strcmp(argv[FIRST_NUM], "proc") == 0) {
618         TestProcess(ondemandHelper, argv[SECOND_NUM]);
619     } else if (strcmp(argv[FIRST_NUM], "policy") == 0) {
620         TestOnDemandPolicy(ondemandHelper, argv);
621     } else if (strcmp(argv[FIRST_NUM], "getExtension") == 0) {
622         TestGetExtension(ondemandHelper, argv[SECOND_NUM]);
623     } else if (strcmp(argv[FIRST_NUM], "getEvent") == 0) {
624         TestCommonEvent(ondemandHelper, argv[SECOND_NUM], argv[THIRD_NUM], argv[FOURTH_NUM]);
625     } else if (strcmp(argv[FIRST_NUM], "check") == 0) {
626         TestCheckSystemAbility(ondemandHelper, argv[SECOND_NUM], argv[THIRD_NUM]);
627     } else if (strcmp(argv[FIRST_NUM], "policy_time") == 0) {
628         TestOnDemandPolicy1(ondemandHelper, argv);
629     } else if (strcmp(argv[FIRST_NUM], "test") == 0) {
630         TestScheduler(ondemandHelper);
631     } else if (strcmp(argv[FIRST_NUM], "memory") == 0) {
632         TestMemory(ondemandHelper, argv);
633     } else {
634         cout << "invalid input" << endl;
635     }
636 }
637 
main(int argc,char * argv[])638 int main(int argc, char* argv[])
639 {
640     SamMockPermission::MockPermission();
641     OHOS::OnDemandHelper& ondemandHelper = OnDemandHelper::GetInstance();
642     ondemandHelper.argc_ = argc;
643     cout << "please input operation(1-param/2-sa/3-proc/4-policy/5-getExtension)" << endl;
644     cout << "please input operation(6-getEvent/7-check/8-policy_time/9-test/10-memory)" << endl;
645     int32_t cmd = atoi(argv[FIRST_NUM]);
646     if (cmd == 0) {
647         TestStringCommand(ondemandHelper, argv);
648     } else {
649         TestIntCommand(ondemandHelper, argv);
650     }
651     return 0;
652 }
653