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 35extern "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 48typedef struct ParameterNode { 49 const char *paraName; 50 const char *paraContext; 51 int index; 52} ParameterNode; 53 54typedef struct ParamContextsList { 55 struct ParameterNode info; 56 struct ParamContextsList *next; 57} ParamContextsList; 58 59typedef struct SrcInfo { 60 int sockFd; 61 struct ucred uc; 62} SrcInfo; 63#endif 64 65void CreateTestFile(const char *fileName, const char *data); 66void PrepareInitUnitTestEnv(void); 67void TestSetSelinuxOps(void); 68void SetTestPermissionResult(int result); 69void TestSetParamCheckResult(const char *prefix, uint16_t mode, int result); 70int TestCheckParamPermission(const ParamLabelIndex *labelIndex, 71 const ParamSecurityLabel *srcLabel, const char *name, uint32_t mode); 72int TestFreeLocalSecurityLabel(ParamSecurityLabel *srcLabel); 73 74typedef enum { 75 STUB_SPRINTF, 76 STUB_MOUNT, 77 STUB_MKNODE, 78 STUB_EXECV, 79 STUB_UMOUNT, 80 STUB_MAX 81} STUB_TYPE; 82void SetStubResult(STUB_TYPE type, int result); 83void PrepareCmdLineData(); 84ParamLabelIndex *TestGetParamLabelIndex(const char *name); 85 86// for test 87void HandleUevent(const struct Uevent *uevent); 88 89#ifndef OHOS_LITE 90void EngineerOverlay(void); 91void DebugFilesOverlay(const char *source, const char *target); 92void BindMountFile(const char *source, const char *target); 93void MountEngPartitions(void); 94void BuildMountCmd(char *buffer, size_t len, const char *mp, const char *dev, const char *fstype); 95bool IsFileExistWithType(const char *file, FileType type); 96 97int DoRoot_(const char *jobName, int type); 98int DoRebootShutdown(int id, const char *name, int argc, const char **argv); 99int DoRebootFlashed(int id, const char *name, int argc, const char **argv); 100int DoRebootOther(int id, const char *name, int argc, const char **argv); 101 102//remount_overlay 103bool MntNeedRemount(const char *mnt); 104bool IsSkipRemount(const struct mntent mentry); 105int ExecCommand(int argc, char **argv); 106int GetDevSize(const char *fsBlkDev, uint64_t *devSize); 107int FormatExt4(const char *fsBlkDev, const char *fsMntPoint); 108void OverlayRemountPre(const char *mnt); 109void OverlayRemountPost(const char *mnt); 110bool DoRemount(struct mntent *mentry, bool *result); 111bool DirectoryExists(const char *path); 112bool DoSystemRemount(struct mntent *mentry, bool *result); 113 114//remount 115int Modem2Exchange(const char *modemPath, const char *exchangePath); 116int Exchange2Modem(const char *modemPath, const char *exchangePath); 117 118SubInitContext *GetSubInitContext(InitContextType type); 119uint64_t LookupErofsEnd(const char *dev); 120void 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