It_container_test.h 7.2 KB
Newer Older
Z
zhushengle 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/*
 * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 * conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 * of conditions and the following disclaimer in the documentation and/or other materials
 * provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 * to endorse or promote products derived from this software without specific prior written
 * permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#ifndef _IT_CONTAINER_TEST_H
#define _IT_CONTAINER_TEST_H

#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <regex>
Z
zhushengle 已提交
38
#include <csignal>
Z
zhushengle 已提交
39 40
#include <sys/syscall.h>
#include <sys/capability.h>
Z
zhushengle 已提交
41
#include <cstring>
Z
zhushengle 已提交
42
#include "osTest.h"
Z
zhushengle 已提交
43 44 45 46
#include "mqueue.h"
#include "sys/time.h"
#include "sys/shm.h"
#include "sys/types.h"
Z
zhushengle 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

const int EXIT_CODE_ERRNO_1 = 1;
const int EXIT_CODE_ERRNO_2 = 2;
const int EXIT_CODE_ERRNO_3 = 3;
const int EXIT_CODE_ERRNO_4 = 4;
const int EXIT_CODE_ERRNO_5 = 5;
const int EXIT_CODE_ERRNO_6 = 6;
const int EXIT_CODE_ERRNO_7 = 7;
const int EXIT_CODE_ERRNO_8 = 8;
const int EXIT_CODE_ERRNO_9 = 9;
const int EXIT_CODE_ERRNO_10 = 10;
const int EXIT_CODE_ERRNO_11 = 11;
const int EXIT_CODE_ERRNO_12 = 12;
const int EXIT_CODE_ERRNO_13 = 13;
const int EXIT_CODE_ERRNO_14 = 14;
const int EXIT_CODE_ERRNO_15 = 15;
const int EXIT_CODE_ERRNO_16 = 16;
Z
zhushengle 已提交
64
const int EXIT_CODE_ERRNO_17 = 17;
Z
zhushengle 已提交
65 66 67 68 69
const int EXIT_CODE_ERRNO_255 = 255;
const int CONTAINER_FIRST_PID = 1;
const int CONTAINER_SECOND_PID = 2;
const int CONTAINER_THIRD_PID = 3;

Z
zhushengle 已提交
70 71 72 73 74 75 76
const int MQUEUE_TEST_SIZE = 50;
const int MQUEUE_TEST_MAX_MSG = 255;

const int SHM_TEST_DATA_SIZE = 1024;
const int SHM_TEST_KEY1 = 1234;
const int SHM_TEST_OPEN_PERM = 0666;
const int CLONE_STACK_MMAP_FLAG = MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK;
Z
zhushengle 已提交
77 78 79

extern const char *USERDATA_DIR_NAME;
extern const char *ACCESS_FILE_NAME;
80
extern const char *MNT_ACCESS_FILE_NAME;
Z
zhushengle 已提交
81 82 83 84 85 86 87
extern const char *USERDATA_DEV_NAME;
extern const char *FS_TYPE;

extern const int BIT_ON_RETURN_VALUE;
extern const int STACK_SIZE;
extern const int CHILD_FUNC_ARG;

Z
zhushengle 已提交
88 89
const int MQUEUE_STANDARD_NAME_LENGTH  = 255;

Z
zhushengle 已提交
90 91 92 93
extern "C" {
#define CLONE_NEWTIME   0x00000080
}

Z
zhushengle 已提交
94 95 96 97
int WriteFile(const char *filepath, const char *buf);
int ReadFile(const char *filepath, char *buf);
int GetLine(char *buf, int count, int maxLen, char **array);

Z
zhushengle 已提交
98 99 100 101
int ChildFunction(void *args);

pid_t CloneWrapper(int (*func)(void *), int flag, void *args);

Z
zhushengle 已提交
102 103
int WaitChild(pid_t pid, int *status, int errNo1, int errNo2);

104 105 106 107
int NetContainerResetNetAddr(const char *ifname, const char *ip, const char *netmask, const char *gw);

int NetContainerGetLocalIP(const char *ifname, char *ip, int ipLen);

Z
zhushengle 已提交
108 109 110 111
std::string GenContainerLinkPath(int pid, const std::string& containerType);

extern std::string ReadlinkContainer(int pid, const std::string& containerType);

Z
zhushengle 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
class MQueueFinalizer {
public:
    explicit MQueueFinalizer(mqd_t mqueueParent, const std::string& mqname)
    {
        m_mqueueParent = mqueueParent;
        m_mqname = mqname;
    }
    ~MQueueFinalizer()
    {
        if (m_mqueueParent >= 0) {
            mq_close(m_mqueueParent);
            mq_unlink(m_mqname.c_str());
        }
    }
private:
    mqd_t m_mqueueParent;
    std::string m_mqname;
};

class ShmFinalizer {
public:
    explicit ShmFinalizer(void* shm, int shmid)
    {
        m_shm = shm;
        m_shmid = shmid;
    }
    ~ShmFinalizer()
    {
        shmdt(m_shm);
        shmctl(m_shmid, IPC_RMID, nullptr);
    }
private:
    void* m_shm;
    int m_shmid;
};

Z
zhushengle 已提交
148 149 150 151 152
void ItUserContainer001(void);
void ItUserContainer002(void);
void ItUserContainer003(void);
void ItUserContainer004(void);
void ItUserContainer005(void);
Z
zhushengle 已提交
153 154
void ItUserContainer006(void);
void ItUserContainer007(void);
Z
zhushengle 已提交
155
void ItContainer001(void);
156
void ItContainerChroot001(void);
Z
zhushengle 已提交
157
void ItContainerChroot002(void);
Z
zhushengle 已提交
158
void ItPidContainer023(void);
Z
zhushengle 已提交
159 160
void ItPidContainer025(void);
void ItPidContainer026(void);
Z
zhushengle 已提交
161 162
void ItPidContainer027(void);
void ItPidContainer028(void);
Z
zhushengle 已提交
163 164
void ItPidContainer029(void);
void ItPidContainer030(void);
Z
zhushengle 已提交
165
void ItPidContainer031(void);
Z
zhushengle 已提交
166 167
void ItPidContainer032(void);
void ItPidContainer033(void);
Z
zhushengle 已提交
168 169
void ItUtsContainer001(void);
void ItUtsContainer002(void);
Z
zhushengle 已提交
170
void ItUtsContainer004(void);
Z
zhushengle 已提交
171 172
void ItUtsContainer005(void);
void ItUtsContainer006(void);
Z
zhushengle 已提交
173 174
void ItUtsContainer007(void);
void ItUtsContainer008(void);
175 176 177 178 179 180 181 182
void ItMntContainer001(void);
void ItMntContainer002(void);
void ItMntContainer003(void);
void ItMntContainer004(void);
void ItMntContainer005(void);
void ItMntContainer006(void);
void ItMntContainer007(void);
void ItMntContainer008(void);
Z
zhushengle 已提交
183 184
void ItMntContainer009(void);
void ItMntContainer010(void);
Z
zhushengle 已提交
185 186 187 188 189 190
void ItIpcContainer001(void);
void ItIpcContainer002(void);
void ItIpcContainer003(void);
void ItIpcContainer004(void);
void ItIpcContainer005(void);
void ItIpcContainer006(void);
Z
zhushengle 已提交
191 192
void ItIpcContainer007(void);
void ItIpcContainer008(void);
Z
zhushengle 已提交
193 194 195 196 197 198 199 200 201 202
void ItTimeContainer001(void);
void ItTimeContainer002(void);
void ItTimeContainer003(void);
void ItTimeContainer004(void);
void ItTimeContainer005(void);
void ItTimeContainer006(void);
void ItTimeContainer007(void);
void ItTimeContainer008(void);
void ItTimeContainer009(void);
void ItTimeContainer010(void);
Z
zhushengle 已提交
203 204 205 206
void ItPidContainer001(void);
void ItPidContainer002(void);
void ItPidContainer003(void);
void ItPidContainer004(void);
Z
zhushengle 已提交
207
void ItPidContainer005(void);
Z
zhushengle 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
void ItPidContainer006(void);
void ItPidContainer007(void);
void ItPidContainer008(void);
void ItPidContainer009(void);
void ItPidContainer010(void);
void ItPidContainer011(void);
void ItPidContainer012(void);
void ItPidContainer013(void);
void ItPidContainer014(void);
void ItPidContainer015(void);
void ItPidContainer016(void);
void ItPidContainer017(void);
void ItPidContainer018(void);
void ItPidContainer019(void);
void ItPidContainer020(void);
void ItPidContainer021(void);
void ItPidContainer022(void);
void ItPidContainer024(void);
Z
zhushengle 已提交
226
void ItUtsContainer003(void);
Z
zhushengle 已提交
227 228 229 230 231 232 233 234 235 236 237 238
void ItNetContainer001(void);
void ItNetContainer002(void);
void ItNetContainer003(void);
void ItNetContainer004(void);
void ItNetContainer005(void);
void ItNetContainer006(void);
void ItNetContainer007(void);
void ItNetContainer008(void);
void ItNetContainer009(void);
void ItNetContainer010(void);
void ItNetContainer011(void);
void ItNetContainer012(void);
Z
zhushengle 已提交
239
#endif /* _IT_CONTAINER_TEST_H */