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 int GetStubResult(STUB_TYPE type); 84 void PrepareCmdLineData(); 85 ParamLabelIndex *TestGetParamLabelIndex(const char *name); 86 87 // for test 88 void HandleUevent(const struct Uevent *uevent); 89 90 #ifndef OHOS_LITE 91 void EngineerOverlay(void); 92 void DebugFilesOverlay(const char *source, const char *target); 93 void BindMountFile(const char *source, const char *target); 94 void MountEngPartitions(void); 95 void BuildMountCmd(char *buffer, size_t len, const char *mp, const char *dev, const char *fstype); 96 bool IsFileExistWithType(const char *file, FileType type); 97 98 int DoRoot_(const char *jobName, int type); 99 int DoRebootShutdown(int id, const char *name, int argc, const char **argv); 100 int DoRebootFlashed(int id, const char *name, int argc, const char **argv); 101 int DoRebootOther(int id, const char *name, int argc, const char **argv); 102 103 //remount_overlay 104 bool MntNeedRemount(const char *mnt); 105 bool IsSkipRemount(const struct mntent mentry); 106 int ExecCommand(int argc, char **argv); 107 int GetDevSize(const char *fsBlkDev, uint64_t *devSize); 108 int FormatExt4(const char *fsBlkDev, const char *fsMntPoint); 109 void OverlayRemountPre(const char *mnt); 110 void OverlayRemountPost(const char *mnt); 111 bool DoRemount(struct mntent *mentry); 112 bool DirectoryExists(const char *path); 113 bool DoSystemRemount(struct mntent *mentry); 114 115 //remount 116 int Modem2Exchange(const char *modemPath, const char *exchangePath); 117 int Exchange2Modem(const char *modemPath, const char *exchangePath); 118 119 SubInitContext *GetSubInitContext(InitContextType type); 120 uint64_t LookupErofsEnd(const char *dev); 121 void AllocDmName(const char *name, char *nameRofs, const uint64_t nameRofsLen, 122 char *nameExt4, const uint64_t nameExt4Len); 123 #endif 124 #ifdef __cplusplus 125 #if __cplusplus 126 } 127 #endif 128 #endif 129 #endif // PARAM_TEST_STUB_ 130