• 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 "param_stub.h"
17 #include <dirent.h>
18 #include <sys/prctl.h>
19 #include <unistd.h>
20 
21 #include "begetctl.h"
22 #include "bootstage.h"
23 #include "init.h"
24 #include "init_log.h"
25 #include "init_param.h"
26 #ifndef OHOS_LITE
27 #include "init_mount.h"
28 #endif
29 #include "hookmgr.h"
30 #include "parameter.h"
31 #include "param_manager.h"
32 #include "param_security.h"
33 #include "param_utils.h"
34 #include "init_group_manager.h"
35 #include "init_module_engine.h"
36 #ifdef PARAM_LOAD_CFG_FROM_CODE
37 #include "param_cfg.h"
38 #endif
39 #include "ueventd.h"
40 
41 #ifdef __cplusplus
42 #if __cplusplus
43 extern "C" {
44 #endif
45 #endif
46 
47 static int g_stubResult[STUB_MAX] = { 0 };
48 static int g_testRandom = 2; // 2 is test random
49 
50 static int g_testPermissionResult = DAC_RESULT_PERMISSION;
SetTestPermissionResult(int result)51 void SetTestPermissionResult(int result)
52 {
53     g_testPermissionResult = result;
54 }
55 
56 static const char *selinuxLabels[][2] = {
57     {"test.permission.read", "u:object_r:test_read:s0"},
58     {"test.permission.write", "u:object_r:test_write:s0"},
59     {"test.permission.watch", "u:object_r:test_watch:s0"},
60 };
61 
TestGenHashCode(const char * buff)62 static int TestGenHashCode(const char *buff)
63 {
64     int code = 0;
65     size_t buffLen = strlen(buff);
66     for (size_t i = 0; i < buffLen; i++) {
67         code += buff[i] - 'A';
68     }
69     return code;
70 }
71 
TestSetSelinuxLogCallback(void)72 static void TestSetSelinuxLogCallback(void) {}
73 
TestSetParamCheck(const char * paraName,const char * context,const SrcInfo * info)74 static int TestSetParamCheck(const char *paraName, const char *context, const SrcInfo *info)
75 {
76     BEGET_LOGI("TestSetParamCheck %s result %d", paraName, g_testPermissionResult);
77     return g_testPermissionResult;
78 }
79 
TestGetParamLabel(const char * paraName)80 static const char *TestGetParamLabel(const char *paraName)
81 {
82     BEGET_LOGI("TestGetParamLabel %s", paraName);
83     if (paraName == nullptr) {
84         return nullptr;
85     }
86     for (size_t i = 0; i < ARRAY_LENGTH(selinuxLabels); i++) {
87         if (strncmp(selinuxLabels[i][0], paraName, strlen(selinuxLabels[i][0])) == 0) {
88             return selinuxLabels[i][1];
89         }
90     }
91     int code = TestGenHashCode(paraName);
92     code = code % (ARRAY_LENGTH(selinuxLabels));
93     return selinuxLabels[code][1];
94 }
95 
TestGetSelinuxLabelIndex(const char * paraName)96 static int32_t TestGetSelinuxLabelIndex(const char *paraName)
97 {
98     for (size_t i = 0; i < ARRAY_LENGTH(selinuxLabels); i++) {
99         if (strncmp(selinuxLabels[i][0], paraName, strlen(selinuxLabels[i][0])) == 0) {
100             return i;
101         }
102     }
103     int code = TestGenHashCode(paraName);
104     code = code % (ARRAY_LENGTH(selinuxLabels));
105     return code;
106 }
107 
108 static const char *g_forbidReadParamName[] = {
109     "ohos.servicectrl.",
110     // "test.permission.write",
111 };
TestReadParamCheck(const char * paraName)112 static int TestReadParamCheck(const char *paraName)
113 {
114     // forbid to read ohos.servicectrl.
115     for (size_t i = 0; i < ARRAY_LENGTH(g_forbidReadParamName); i++) {
116         if (strncmp(paraName, g_forbidReadParamName[i], strlen(g_forbidReadParamName[i])) == 0) {
117             return 1;
118         }
119     }
120     return g_testPermissionResult;
121 }
TestDestroyParamList(ParamContextsList ** list)122 static void TestDestroyParamList(ParamContextsList **list)
123 {
124 #ifdef PARAM_SUPPORT_SELINUX
125     ParamContextsList *head = *list;
126     while (head != nullptr) {
127         ParamContextsList *next = head->next;
128         free((void *)head->info.paraName);
129         free((void *)head->info.paraContext);
130         free(head);
131         head = next;
132     }
133 #endif
134 }
TestGetParamList(void)135 static ParamContextsList *TestGetParamList(void)
136 {
137 #ifdef PARAM_SUPPORT_SELINUX
138     ParamContextsList *head = (ParamContextsList *)malloc(sizeof(ParamContextsList));
139     BEGET_ERROR_CHECK(head != nullptr, return nullptr, "Failed to alloc ParamContextsList");
140     head->info.paraName = strdup(selinuxLabels[0][0]);
141     head->info.paraContext = strdup(selinuxLabels[0][1]);
142     head->info.index = 0;
143     head->next = nullptr;
144     for (size_t i = 1; i < ARRAY_LENGTH(selinuxLabels); i++) {
145         ParamContextsList *node = (ParamContextsList *)malloc(sizeof(ParamContextsList));
146         BEGET_ERROR_CHECK(node != nullptr, TestDestroyParamList(&head);
147             return nullptr, "Failed to alloc ParamContextsList");
148         node->info.paraName = strdup(selinuxLabels[i][0]);
149         node->info.paraContext = strdup(selinuxLabels[i][1]);
150         node->info.index = i;
151         node->next = head->next;
152         head->next = node;
153     }
154     // test error, no node paraName
155     ParamContextsList *node = (ParamContextsList *)malloc(sizeof(ParamContextsList));
156     BEGET_ERROR_CHECK(node != nullptr, TestDestroyParamList(&head);
157         return nullptr, "Failed to alloc ParamContextsList");
158     node->info.paraName = nullptr;
159     node->info.paraContext = strdup(selinuxLabels[0][1]);
160     node->next = head->next;
161     head->next = node;
162 
163     // test error, no node paraContext
164     node = (ParamContextsList *)malloc(sizeof(ParamContextsList));
165     BEGET_ERROR_CHECK(node != nullptr, TestDestroyParamList(&head);
166         return nullptr, "Failed to alloc ParamContextsList");
167     node->info.paraName = strdup(selinuxLabels[0][0]);
168     node->info.paraContext = nullptr;
169     node->next = head->next;
170     head->next = node;
171 
172     // test error, repeat
173     node = (ParamContextsList *)malloc(sizeof(ParamContextsList));
174     BEGET_ERROR_CHECK(node != nullptr, TestDestroyParamList(&head);
175         return nullptr, "Failed to alloc ParamContextsList");
176     node->info.paraName = strdup(selinuxLabels[0][0]);
177     node->info.paraContext = strdup(selinuxLabels[0][1]);
178     node->info.index = 0;
179     node->next = head->next;
180     head->next = node;
181     return head;
182 #else
183     return nullptr;
184 #endif
185 }
186 
TestSetSelinuxOps(void)187 void TestSetSelinuxOps(void)
188 {
189 #ifdef PARAM_SUPPORT_SELINUX
190     SelinuxSpace *selinuxSpace = &GetParamWorkSpace()->selinuxSpace;
191     selinuxSpace->setSelinuxLogCallback = TestSetSelinuxLogCallback;
192     selinuxSpace->setParamCheck = TestSetParamCheck;
193     selinuxSpace->getParamLabel = TestGetParamLabel;
194     selinuxSpace->readParamCheck = TestReadParamCheck;
195     selinuxSpace->getParamList = TestGetParamList;
196     selinuxSpace->destroyParamList = TestDestroyParamList;
197     selinuxSpace->getParamLabelIndex = TestGetSelinuxLabelIndex;
198 #endif
199 }
200 
TestSetParamCheckResult(const char * prefix,uint16_t mode,int result)201 void TestSetParamCheckResult(const char *prefix, uint16_t mode, int result)
202 {
203     ParamAuditData auditData = {};
204     auditData.name = prefix;
205     auditData.dacData.gid = 202;  // 202 test dac gid
206     auditData.dacData.uid = 202;  // 202 test dac uid
207     auditData.dacData.mode = mode;
208     AddSecurityLabel(&auditData);
209     SetTestPermissionResult(result);
210 }
211 
CreateTestFile(const char * fileName,const char * data)212 void CreateTestFile(const char *fileName, const char *data)
213 {
214     CheckAndCreateDir(fileName);
215     PARAM_LOGV("PrepareParamTestData for %s", fileName);
216     FILE *tmpFile = fopen(fileName, "wr");
217     if (tmpFile != nullptr) {
218         fprintf(tmpFile, "%s", data);
219         (void)fflush(tmpFile);
220         fclose(tmpFile);
221     }
222 }
PrepareUeventdcfg(void)223 static void PrepareUeventdcfg(void)
224 {
225     const char *ueventdcfg = "[device]\n"
226         "/dev/test 0666 1000 1000\n"
227         "[device]\n"
228         "/dev/test1 0666 1000\n"
229         "[device]\n"
230         "/dev/test2 0666 1000 1000 1000 1000\n"
231         "[sysfs]\n"
232         "/dir/to/nothing attr_nowhere 0666 1000 1000\n"
233         "[sysfs]\n"
234         "  #/dir/to/nothing attr_nowhere 0666\n"
235         "[sysfs\n"
236         "/dir/to/nothing attr_nowhere 0666\n"
237         "[firmware]\n"
238         "/etc\n"
239         "[device]\n"
240         "/dev/testbinder 0666 1000 1000 const.dev.binder\n"
241         "[device]\n"
242         "/dev/testbinder1 0666 1000 1000 const.dev.binder\n"
243         "[device]\n"
244         "/dev/testbinder2 0666 1000 1000 const.dev.binder\n"
245         "[device]\n"
246         "/dev/testbinder3 0666 1000 1000 const.dev.binder\n";
247     mkdir("/data/ueventd_ut", S_IRWXU | S_IRWXG | S_IRWXO);
248     CreateTestFile(STARTUP_INIT_UT_PATH"/ueventd_ut/valid.config", ueventdcfg);
249 }
PrepareModCfg(void)250 static void PrepareModCfg(void)
251 {
252     const char *modCfg = "testinsmod";
253     CreateTestFile(STARTUP_INIT_UT_PATH"/test_insmod", modCfg);
254 }
PrepareInnerKitsCfg()255 static void PrepareInnerKitsCfg()
256 {
257     const char *innerKitsCfg = "/dev/block/platform/soc/10100000.himci.eMMC/by-name/system /system "
258         "ext4 ro,barrier=1 wait\n"
259         "/dev/block/platform/soc/10100000.himci.eMMC/by-name/vendor /vendor "
260         "ext4 ro,barrier=1 wait\n"
261         "/dev/block/platform/soc/10100000.himci.eMMC/by-name/hos "
262         "/hos ntfs nosuid,nodev,noatime,barrier=1,data=ordered wait\n"
263         "/dev/block/platform/soc/10100000.himci.eMMC/by-name/userdata /data ext4 "
264         "nosuid,nodev,noatime,barrier=1,data=ordered,noauto_da_alloc "
265         "wait,reservedsize=104857600\n"
266         "  aaaa\n"
267         "aa aa\n"
268         "aa aa aa\n"
269         "aa aa aa aa\n";
270     const char *fstabRequired = "# fstab file.\n"
271         "#<src> <mnt_point> <type> <mnt_flags and options> <fs_mgr_flags>\n"
272         "/dev/block/platform/fe310000.sdhci/by-name/testsystem /usr ext4 ro,barrier=1 wait,required,nofail\n"
273         "/dev/block/platform/fe310000.sdhci/by-name/testvendor /vendor ext4 ro,barrier=1 wait,required\n"
274         "/dev/block/platform/fe310000.sdhci/by-name/testuserdata1 /data f2fs noatime,nosuid,nodev wait,check,quota\n"
275         "/dev/block/platform/fe310000.sdhci/by-name/testuserdata2 /data ext4 noatime,fscrypt=xxx wait,check,quota\n"
276         "/dev/block/platform/fe310000.sdhci/by-name/testmisc /misc none none wait,required";
277     mkdir("/data/init_ut/mount_unitest/", S_IRWXU | S_IRWXG | S_IRWXO);
278     CreateTestFile(STARTUP_INIT_UT_PATH"/mount_unitest/ReadFstabFromFile1.fstable", innerKitsCfg);
279     CreateTestFile(STARTUP_INIT_UT_PATH"/etc/fstab.required", fstabRequired);
280     CreateTestFile(STARTUP_INIT_UT_PATH"/system/etc/fstab.required", fstabRequired);
281 }
PrepareGroupTestCfg()282 static void PrepareGroupTestCfg()
283 {
284     const char *data = "{"
285 	    "\"jobs\": [\"param:job1\", \"param:job2\", \"param:job4\"],"
286 	    "\"services\": [\"service:service1\", \"service:service3\", \"service:service2\"],"
287 	    "\"groups\": [\"subsystem.xxx1.group\", \"subsystem.xxx2.group\", \"subsystem.xxx4.group\"]"
288     "}";
289     const char *xxx1 = "{"
290 	    "\"groups\": [\"subsystem.xxx11.group\""
291     "}";
292     const char *xxx11 = "{"
293 	    "\"groups\": [\"subsystem.xxx12.group\""
294     "}";
295     const char *xxx12 = "{"
296 	    "\"groups\": [\"subsystem.xxx13.group\""
297     "}";
298     const char *xxx13 = "{"
299 	    "\"groups\": [\"subsystem.xxx14.group\""
300     "}";
301     const char *xxx14 = "{"
302 	    "\"groups\": [\"subsystem.xxx11.group\""
303     "}";
304     CreateTestFile(GROUP_DEFAULT_PATH "/device.boot.group.cfg", data);
305     CreateTestFile(GROUP_DEFAULT_PATH "/subsystem.xxx1.group.cfg", xxx1);
306     CreateTestFile(GROUP_DEFAULT_PATH "/subsystem.xxx11.group.cfg", xxx11);
307     CreateTestFile(GROUP_DEFAULT_PATH "/subsystem.xxx12.group.cfg", xxx12);
308     CreateTestFile(GROUP_DEFAULT_PATH "/subsystem.xxx13.group.cfg", xxx13);
309     CreateTestFile(GROUP_DEFAULT_PATH "/subsystem.xxx14.group.cfg", xxx14);
310 }
IsDir(const std::string & path)311 static bool IsDir(const std::string &path)
312 {
313     struct stat st {};
314     if (stat(path.c_str(), &st) < 0) {
315         return false;
316     }
317     return S_ISDIR(st.st_mode);
318 }
DeleteDir(const std::string & path)319 static bool DeleteDir(const std::string &path)
320 {
321     auto pDir = std::unique_ptr<DIR, decltype(&closedir)>(opendir(path.c_str()), closedir);
322     if (pDir == nullptr) {
323         return false;
324     }
325 
326     struct dirent *dp = nullptr;
327     while ((dp = readdir(pDir.get())) != nullptr) {
328         std::string currentName(dp->d_name);
329         if (currentName[0] != '.') {
330             std::string tmpName(path);
331             tmpName.append("/" + currentName);
332             if (IsDir(tmpName)) {
333                 DeleteDir(tmpName);
334             }
335             remove(tmpName.c_str());
336         }
337     }
338     if (remove(path.c_str()) != 0) {
339         return false;
340     }
341     return true;
342 }
LoadParamFromCfg(void)343 static void LoadParamFromCfg(void)
344 {
345 #ifdef PARAM_LOAD_CFG_FROM_CODE
346     for (size_t i = 0; i < ARRAY_LENGTH(g_paramDefCfgNodes); i++) {
347         PARAM_LOGI("InitParamClient name %s = %s", g_paramDefCfgNodes[i].name, g_paramDefCfgNodes[i].value);
348         uint32_t dataIndex = 0;
349         int ret = WriteParam(g_paramDefCfgNodes[i].name, g_paramDefCfgNodes[i].value, &dataIndex, 0);
350         PARAM_CHECK(ret == 0, continue, "Failed to set param %d name %s %s",
351             ret, g_paramDefCfgNodes[i].name, g_paramDefCfgNodes[i].value);
352     }
353 #endif
354 }
355 
356 static const char *g_triggerData = "{"
357         "\"jobs\" : [{"
358         "        \"name\" : \"early-init\","
359         "        \"cmds\" : ["
360         "            \"    write        '/proc/sys/kernel/sysrq 0'\","
361         "            \"    load_persist_params \","
362         "            \"    load_persist_params        \","
363         "            \" #   load_persist_params \","
364         "            \"   restorecon /postinstall\","
365         "            \"mkdir /acct/uid\","
366         "            \"chown root system /dev/memcg/memory.pressure_level\","
367         "            \"chmod 0040 /dev/memcg/memory.pressure_level\","
368         "            \"mkdir /dev/memcg/apps/ 0755 system system\","
369         "           \"mkdir /dev/memcg/system 0550 system system\","
370         "            \"start ueventd\","
371         "            \"exec_start apexd-bootstrap\","
372         "            \"setparam sys.usb.config ${persist.sys.usb.config}\""
373         "        ]"
374         "    },"
375         "    {"
376         "        \"name\" : \"param:trigger_test_1\","
377         "        \"condition\" : \"test.sys.boot_from_charger_mode=5\","
378         "        \"cmds\" : ["
379         "            \"class_stop charger\","
380         "            \"trigger late-init\""
381         "        ]"
382         "    },"
383         "    {"
384         "        \"name\" : \"param:trigger_test_2\","
385         "        \"condition\" : \"test.sys.boot_from_charger_mode=1  "
386         " || test.sys.boot_from_charger_mode=2   ||  test.sys.boot_from_charger_mode=3\","
387         "        \"cmds\" : ["
388         "            \"class_stop charger\","
389         "            \"trigger late-init\""
390         "        ]"
391         "    },"
392         "    {"
393         "        \"name\" : \"load_persist_params_action\","
394         "        \"cmds\" : ["
395         "           \"load_persist_params\","
396         "            \"start logd\","
397         "            \"start logd-reinit\""
398         "        ]"
399         "    },"
400         "    {"
401         "        \"name\" : \"firmware_mounts_complete\","
402         "        \"cmds\" : ["
403         "            \"rm /dev/.booting\""
404         "        ]"
405         "    }"
406         "]"
407     "}";
408 
PrepareCmdLineData()409 void PrepareCmdLineData()
410 {
411     const char *cmdLine = "bootgroup=device.boot.group earlycon=uart8250,mmio32,0xfe660000 "
412         "root=PARTUUID=614e0000-0000 rw rootwait rootfstype=ext4 console=ttyFIQ0 hardware=rk3568 "
413         "BOOT_IMAGE=/kernel init=/init default_boot_device=fe310000.sdhci bootslots=2 currentslot=1 initloglevel=2 "
414         "ohos.required_mount.system="
415         "/dev/block/platform/fe310000.sdhci/by-name/system@/usr@ext4@ro,barrier=1@wait,required "
416         "ohos.required_mount.vendor="
417         "/dev/block/platform/fe310000.sdhci/by-name/vendor@/vendor@ext4@ro,barrier=1@wait,required "
418         "ohos.required_mount.misc="
419         "/dev/block/platform/fe310000.sdhci/by-name/misc@none@none@none@wait,required ohos.boot.eng_mode=on ";
420     CreateTestFile(BOOT_CMD_LINE, cmdLine);
421 }
422 
PrepareAreaSizeFile(void)423 static void PrepareAreaSizeFile(void)
424 {
425     const char *ohosParamSize = "default_param=1024\n"
426             "hilog_param=2048\n"
427             "const_product_param=2048\n"
428             "startup_param=20480\n"
429             "persist_param=2048\n"
430             "const_param=20480\n"
431             "test_watch=81920\n"
432             "test_read=81920\n"
433             "const_param***=20480\n"
434             "persist_sys_param=2048\n";
435     CreateTestFile(PARAM_AREA_SIZE_CFG, ohosParamSize);
436 }
437 
PrepareTestGroupFile(void)438 static void PrepareTestGroupFile(void)
439 {
440     std::string groupData = "root:x:0:\n"
441         "bin:x:2:\n"
442         "system:x:1000:\n"
443         "log:x:1007:\n"
444         "deviceinfo:x:1102:\n"
445         "samgr:x:5555:\n"
446         "hdf_devmgr:x:3044:\n\n"
447         "power_host:x:3025:\n"
448         "servicectrl:x:1050:root,  shell,system,   samgr,   hdf_devmgr      \n"
449         "powerctrl:x:1051:root, shell,system,  update,power_host\r\n"
450         "bootctrl:x:1052:root,shell,system\n"
451         "deviceprivate:x:1053:root,shell,system,samgr,hdf_devmgr,deviceinfo,"
452         "dsoftbus,dms,account,useriam,access_token,device_manager,foundation,dbms,deviceauth,huks_server\n"
453         "hiview:x:1201:\n"
454         "hidumper_service:x:1212:\n"
455         "shell:x:2000:\n"
456         "cache:x:2001:\n"
457         "net_bw_stats:x:3006:\n";
458 
459     CreateTestFile(STARTUP_INIT_UT_PATH "/etc/group", groupData.c_str());
460 }
461 
PrepareDacData()462 static void PrepareDacData()
463 {
464     // for dac
465     std::string dacData = "ohos.servicectrl.   = system:servicectrl:0775 \n";
466     dacData += "startup.service.ctl.        = system:servicectrl:0775:int\n";
467     dacData += "test.permission.       = root:root:0770\n";
468     dacData += "test.permission.read. =  root:root:0774\n";
469     dacData += "test.permission.write.=  root:root:0772\n";
470     dacData += "test.permission.watcher. = root:root:0771\n";
471     dacData += "test.test1. = system:test1:0771\n";
472     dacData += "test.test2.watcher. = test2:root:0771\n";
473     dacData += "test.type.int. = root:root:0777:int\n";
474     dacData += "test.type.bool. = root:root:0777:bool\n";
475     dacData += "test.type.string. = root:root:0777\n";
476     dacData += "test.invalid.int. = root:root:\n";
477     dacData += "test.invalid.int. = root::\n";
478     dacData += "test.invalid.int. = ::\n";
479     dacData += "test.invalid.int. = \n";
480     dacData += "test.invalid.int. \n";
481     CreateTestFile(STARTUP_INIT_UT_PATH "/system/etc/param/ohos.para.dac", dacData.c_str());
482     CreateTestFile(STARTUP_INIT_UT_PATH "/system/etc/param/ohos.para.dac_1", dacData.c_str());
483 }
484 
TestHook(const HOOK_INFO * hookInfo,void * cookie)485 static int TestHook(const HOOK_INFO *hookInfo, void *cookie)
486 {
487     return 0;
488 }
489 
PrepareInitUnitTestEnv(void)490 void PrepareInitUnitTestEnv(void)
491 {
492     static int evnOk = 0;
493     if (evnOk) {
494         return;
495     }
496     printf("PrepareInitUnitTestEnv \n");
497     SignalInit();
498 #ifdef PARAM_SUPPORT_SELINUX
499     RegisterSecuritySelinuxOps(nullptr, 0);
500 #endif
501 
502 #ifndef OHOS_LITE
503     InitAddGlobalInitHook(0, TestHook);
504     InitAddPreParamServiceHook(0, TestHook);
505     InitAddPreParamLoadHook(0, TestHook);
506     InitAddPreCfgLoadHook(0, TestHook);
507     InitAddPostCfgLoadHook(0, TestHook);
508     InitAddPostPersistParamLoadHook(0, TestHook);
509     LoadSpecialParam();
510 #endif
511     // read default parameter from system
512     LoadDefaultParams("/system/etc/param/ohos_const", LOAD_PARAM_NORMAL);
513     LoadDefaultParams("/vendor/etc/param", LOAD_PARAM_NORMAL);
514     LoadDefaultParams("/system/etc/param", LOAD_PARAM_ONLY_ADD);
515 
516     // read ut parameters
517     LoadDefaultParams(STARTUP_INIT_UT_PATH "/system/etc/param/ohos_const", LOAD_PARAM_NORMAL);
518     LoadDefaultParams(STARTUP_INIT_UT_PATH "/vendor/etc/param", LOAD_PARAM_NORMAL);
519     LoadDefaultParams(STARTUP_INIT_UT_PATH "/system/etc/param", LOAD_PARAM_ONLY_ADD);
520     LoadParamsFile(STARTUP_INIT_UT_PATH "/system/etc/param", LOAD_PARAM_ONLY_ADD);
521     LoadParamFromCfg();
522 
523     int32_t loglevel = GetIntParameter("persist.init.debug.loglevel", INIT_ERROR);
524     SetInitLogLevel((InitLogLevel)loglevel);
525 
526     // for test int get
527     SystemWriteParam("test.int.get", "-101");
528     SystemWriteParam("test.uint.get", "101");
529     SystemWriteParam("test.string.get", "101");
530     SystemWriteParam("test.bool.get.true", "true");
531     SystemWriteParam("test.bool.get.false", "false");
532 
533     evnOk = 1;
534 }
535 
TestCheckParamPermission(const ParamLabelIndex * labelIndex,const ParamSecurityLabel * srcLabel,const char * name,uint32_t mode)536 int TestCheckParamPermission(const ParamLabelIndex *labelIndex,
537     const ParamSecurityLabel *srcLabel, const char *name, uint32_t mode)
538 {
539     // DAC_RESULT_FORBIDED
540     return g_testPermissionResult;
541 }
542 
TestFreeLocalSecurityLabel(ParamSecurityLabel * srcLabel)543 int TestFreeLocalSecurityLabel(ParamSecurityLabel *srcLabel)
544 {
545     return 0;
546 }
547 
SetStubResult(STUB_TYPE type,int result)548 void SetStubResult(STUB_TYPE type, int result)
549 {
550     g_stubResult[type] = result;
551 }
552 
553 #ifndef OHOS_LITE
TestBeforeInit(void)554 static void TestBeforeInit(void)
555 {
556     ParamWorkSpace *paramSpace = GetParamWorkSpace();
557     EXPECT_NE(paramSpace, nullptr);
558     InitParamService();
559     CloseParamWorkSpace();
560     paramSpace = GetParamWorkSpace();
561     EXPECT_NE(paramSpace, nullptr);
562 
563     // test read cmdline
564     Fstab *stab = LoadRequiredFstab();
565     ReleaseFstab(stab);
566 }
567 #endif
568 
569 static pid_t g_currPid = 0;
ParamTestStubInit(void)570 static __attribute__((constructor(101))) void ParamTestStubInit(void)
571 {
572     g_currPid = getpid();
573     printf("Init unit test start %u \n", g_currPid);
574     EnableInitLog(INIT_ERROR);
575 
576     // prepare data
577     mkdir(STARTUP_INIT_UT_PATH, S_IRWXU | S_IRWXG | S_IRWXO);
578     CheckAndCreateDir(STARTUP_INIT_UT_PATH MODULE_LIB_NAME "/autorun/");
579     int cmdIndex = 0;
580     (void)GetMatchCmd("copy ", &cmdIndex);
581     DoCmdByIndex(cmdIndex, MODULE_LIB_NAME"/libbootchart.z.so "
582         STARTUP_INIT_UT_PATH MODULE_LIB_NAME "/libbootchart.z.so", nullptr);
583     DoCmdByIndex(cmdIndex, MODULE_LIB_NAME"/libbootchart.z.so "
584         STARTUP_INIT_UT_PATH MODULE_LIB_NAME "/autorun/libbootchart.z.so", nullptr);
585     PrepareUeventdcfg();
586     PrepareInnerKitsCfg();
587     PrepareModCfg();
588     PrepareGroupTestCfg();
589     PrepareDacData();
590     CreateTestFile(STARTUP_INIT_UT_PATH"/trigger_test.cfg", g_triggerData);
591     PrepareAreaSizeFile();
592     PrepareTestGroupFile();
593     PrepareCmdLineData();
594 #ifndef OHOS_LITE
595     TestBeforeInit();
596     SystemPrepare();
597     SystemInit();
598     SystemConfig();
599 #endif
600     // init service open
601     InitServiceSpace();
602     // param service open
603     InitParamService();
604     TestSetSelinuxOps();
605 }
606 
ParamTestStubExit(void)607 __attribute__((destructor)) static void ParamTestStubExit(void)
608 {
609     PARAM_LOGI("ParamTestStubExit %u %u \n", g_currPid, getpid());
610     if (g_currPid != getpid()) {
611         return;
612     }
613 #ifndef OHOS_LITE
614     StopParamService();
615 
616     HookMgrExecute(GetBootStageHookMgr(), INIT_BOOT_COMPLETE, nullptr, nullptr);
617     CloseUeventConfig();
618     const char *clearBootEventArgv[] = {"bootevent"};
619     PluginExecCmd("clear", ARRAY_LENGTH(clearBootEventArgv), clearBootEventArgv);
620     CloseServiceSpace();
621     demoExit();
622     LE_CloseLoop(LE_GetDefaultLoop());
623     HookMgrDestroy(GetBootStageHookMgr());
624 #endif
625 }
626 
627 #ifdef OHOS_LITE
LE_DoAsyncEvent(const LoopHandle loopHandle,const TaskHandle taskHandle)628 void __attribute__((weak))LE_DoAsyncEvent(const LoopHandle loopHandle, const TaskHandle taskHandle)
629 {
630 }
631 
HalGetSerial(void)632 const char* HalGetSerial(void)
633 {
634     static const char *serial = "1234567890";
635     return serial;
636 }
637 #endif
638 
SprintfStub(char * buffer,size_t size,const char * fmt,...)639 int __attribute__((weak))SprintfStub(char *buffer, size_t size, const char *fmt, ...)
640 {
641     int len = -1;
642     va_list vargs;
643     va_start(vargs, fmt);
644     len = vsnprintf_s(buffer, size, size - 1, fmt, vargs);
645     va_end(vargs);
646     return len;
647 }
648 
MountStub(const char * source,const char * target,const char * filesystemtype,unsigned long mountflags,const void * data)649 int __attribute__((weak))MountStub(const char* source, const char* target,
650     const char* filesystemtype, unsigned long mountflags, const void * data)
651 {
652     return g_stubResult[STUB_MOUNT];
653 }
654 
UmountStub(const char * target)655 int __attribute__((weak))UmountStub(const char *target)
656 {
657     return 0;
658 }
659 
Umount2Stub(const char * target,int flags)660 int __attribute__((weak))Umount2Stub(const char *target, int flags)
661 {
662     return 0;
663 }
664 
SymlinkStub(const char * oldpath,const char * newpath)665 int __attribute__((weak))SymlinkStub(const char * oldpath, const char * newpath)
666 {
667     return 0;
668 }
669 
PrctlStub(int option,...)670 int PrctlStub(int option, ...)
671 {
672     if (option == PR_SET_SECUREBITS) {
673         static int count = 0;
674         count++;
675         return (count % g_testRandom == 1) ? 0 : -1;
676     }
677     if (option == PR_CAP_AMBIENT) {
678         static int count1 = 0;
679         count1++;
680         return (count1 % g_testRandom == 1) ? 0 : -1;
681     }
682     return 0;
683 }
684 
ExecvStub(const char * pathname,char * const argv[])685 int ExecvStub(const char *pathname, char *const argv[])
686 {
687     printf("ExecvStub %s \n", pathname);
688     return 0;
689 }
690 
LchownStub(const char * pathname,uid_t owner,gid_t group)691 int LchownStub(const char *pathname, uid_t owner, gid_t group)
692 {
693     return 0;
694 }
695 
KillStub(pid_t pid,int signal)696 int KillStub(pid_t pid, int signal)
697 {
698     return 0;
699 }
700 
ExecveStub(const char * pathname,char * const argv[],char * const envp[])701 int ExecveStub(const char *pathname, char *const argv[], char *const envp[])
702 {
703     printf("ExecveStub %s \n", pathname);
704     return 0;
705 }
706 
LoadPolicy()707 int LoadPolicy()
708 {
709     return 0;
710 }
711 
712 static int g_selinuxOptResult = 0;
setcon(const char * name)713 int setcon(const char *name)
714 {
715     g_selinuxOptResult++;
716     return g_selinuxOptResult % g_testRandom;
717 }
718 
RestoreconRecurse(const char * name)719 int RestoreconRecurse(const char *name)
720 {
721     g_selinuxOptResult++;
722     return g_selinuxOptResult % g_testRandom;
723 }
724 
setexeccon(const char * name)725 int setexeccon(const char *name)
726 {
727     g_selinuxOptResult++;
728     return g_selinuxOptResult % g_testRandom;
729 }
730 
setsockcreatecon(const char * name)731 int setsockcreatecon(const char *name)
732 {
733     g_selinuxOptResult++;
734     return g_selinuxOptResult % g_testRandom;
735 }
736 
setfilecon(const char * name,const char * content)737 int setfilecon(const char *name, const char *content)
738 {
739     g_selinuxOptResult++;
740     return g_selinuxOptResult % g_testRandom;
741 }
742 
TestGetParamLabelIndex(const char * name)743 ParamLabelIndex *TestGetParamLabelIndex(const char *name)
744 {
745     static ParamLabelIndex labelIndex = {0};
746     uint32_t index = 0;
747     ParamWorkSpace *paramWorkspace = GetParamWorkSpace();
748     if (paramWorkspace == nullptr) {
749         return &labelIndex;
750     }
751 #ifdef PARAM_SUPPORT_SELINUX
752     if (paramWorkspace->selinuxSpace.getParamLabelIndex == nullptr) {
753         return &labelIndex;
754     }
755     index = (uint32_t)paramWorkspace->selinuxSpace.getParamLabelIndex(name) + WORKSPACE_INDEX_BASE;
756     if (index >= paramWorkspace->maxLabelIndex) {
757         return &labelIndex;
758     }
759 #endif
760     labelIndex.workspace = paramWorkspace->workSpace[index];
761     PARAM_CHECK(labelIndex.workspace != nullptr, return nullptr, "Invalid workSpace");
762     labelIndex.selinuxLabelIndex = labelIndex.workspace->spaceIndex;
763     (void)FindTrieNode(paramWorkspace->workSpace[0], name, strlen(name), &labelIndex.dacLabelIndex);
764     return &labelIndex;
765 }
766 #ifdef __cplusplus
767 #if __cplusplus
768 }
769 #endif
770 #endif
771