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 #ifndef PARAM_TEST_STUB
16 #define PARAM_TEST_STUB
17 #include <cstdio>
18 #include <cstdlib>
19 #include <cstring>
20 #include <gtest/gtest.h>
21 #include <sys/socket.h>
22 #include <sys/types.h>
23 
24 #ifndef OHOS_LITE
25 #include "init_eng.h"
26 #include "init_context.h"
27 #endif
28 #include "param_manager.h"
29 #include "param_security.h"
30 #include "param_utils.h"
31 #include "securec.h"
32 
33 #ifdef __cplusplus
34 #if __cplusplus
35 extern "C" {
36 #endif
37 #endif
38 
39 #ifdef SUPPORT_64BIT
40 #define MODULE_LIB_NAME "/system/lib64/init"
41 #else
42 #define MODULE_LIB_NAME "/system/lib/init"
43 #endif
44 
45 #define DEFAULT_ERROR (-65535)
46 
47 #ifndef PARAM_SUPPORT_SELINUX
48 typedef struct ParameterNode {
49     const char *paraName;
50     const char *paraContext;
51     int index;
52 } ParameterNode;
53 
54 typedef struct ParamContextsList {
55     struct ParameterNode info;
56     struct ParamContextsList *next;
57 } ParamContextsList;
58 
59 typedef struct SrcInfo {
60     int sockFd;
61     struct ucred uc;
62 } SrcInfo;
63 #endif
64 
65 void CreateTestFile(const char *fileName, const char *data);
66 void PrepareInitUnitTestEnv(void);
67 void TestSetSelinuxOps(void);
68 void SetTestPermissionResult(int result);
69 void TestSetParamCheckResult(const char *prefix, uint16_t mode, int result);
70 int TestCheckParamPermission(const ParamLabelIndex *labelIndex,
71     const ParamSecurityLabel *srcLabel, const char *name, uint32_t mode);
72 int TestFreeLocalSecurityLabel(ParamSecurityLabel *srcLabel);
73 
74 typedef enum {
75     STUB_SPRINTF,
76     STUB_MOUNT,
77     STUB_MKNODE,
78     STUB_EXECV,
79     STUB_UMOUNT,
80     STUB_MAX
81 } STUB_TYPE;
82 void SetStubResult(STUB_TYPE type, int result);
83 void PrepareCmdLineData();
84 ParamLabelIndex *TestGetParamLabelIndex(const char *name);
85 
86 // for test
87 void HandleUevent(const struct Uevent *uevent);
88 
89 #ifndef OHOS_LITE
90 void EngineerOverlay(void);
91 void DebugFilesOverlay(const char *source, const char *target);
92 void BindMountFile(const char *source, const char *target);
93 void MountEngPartitions(void);
94 void BuildMountCmd(char *buffer, size_t len, const char *mp, const char *dev, const char *fstype);
95 bool IsFileExistWithType(const char *file, FileType type);
96 
97 int DoRoot_(const char *jobName, int type);
98 int DoRebootShutdown(int id, const char *name, int argc, const char **argv);
99 int DoRebootFlashed(int id, const char *name, int argc, const char **argv);
100 int DoRebootOther(int id, const char *name, int argc, const char **argv);
101 
102 //remount_overlay
103 bool MntNeedRemount(const char *mnt);
104 bool IsSkipRemount(const struct mntent mentry);
105 int ExecCommand(int argc, char **argv);
106 int GetDevSize(const char *fsBlkDev, uint64_t *devSize);
107 int FormatExt4(const char *fsBlkDev, const char *fsMntPoint);
108 void OverlayRemountPre(const char *mnt);
109 void OverlayRemountPost(const char *mnt);
110 bool DoRemount(struct mntent *mentry, bool *result);
111 bool DirectoryExists(const char *path);
112 bool DoSystemRemount(struct mntent *mentry, bool *result);
113 
114 //remount
115 int Modem2Exchange(const char *modemPath, const char *exchangePath);
116 int Exchange2Modem(const char *modemPath, const char *exchangePath);
117 
118 SubInitContext *GetSubInitContext(InitContextType type);
119 uint64_t LookupErofsEnd(const char *dev);
120 void AllocDmName(const char *name, char *nameRofs, const uint64_t nameRofsLen,
121     char *nameExt4, const uint64_t nameExt4Len);
122 #endif
123 #ifdef __cplusplus
124 #if __cplusplus
125 }
126 #endif
127 #endif
128 #endif  // PARAM_TEST_STUB_
129