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_MAX 79 } STUB_TYPE; 80 void SetStubResult(STUB_TYPE type, int result); 81 void PrepareCmdLineData(); 82 ParamLabelIndex *TestGetParamLabelIndex(const char *name); 83 84 // for test 85 void HandleUevent(const struct Uevent *uevent); 86 87 #ifndef OHOS_LITE 88 void EngineerOverlay(void); 89 void DebugFilesOverlay(const char *source, const char *target); 90 void BindMountFile(const char *source, const char *target); 91 void MountEngPartitions(void); 92 void BuildMountCmd(char *buffer, size_t len, const char *mp, const char *dev, const char *fstype); 93 bool IsFileExistWithType(const char *file, FileType type); 94 95 int DoRoot_(const char *jobName, int type); 96 int DoRebootShutdown(int id, const char *name, int argc, const char **argv); 97 int DoRebootFlashed(int id, const char *name, int argc, const char **argv); 98 int DoRebootOther(int id, const char *name, int argc, const char **argv); 99 100 SubInitContext *GetSubInitContext(InitContextType type); 101 #endif 102 #ifdef __cplusplus 103 #if __cplusplus 104 } 105 #endif 106 #endif 107 #endif // PARAM_TEST_STUB_ 108