feat 支持容器限额

BREAKING CHANGE:
支持容器限额对外变更:
1.在proc目录下增加sys/user目录,支持max_容器_container 配额文件

Close #I6HDQK

Signed-off-by: zhushengle <zhushengle@huawei.com>
Change-Id: Ieaac046182f679a6f49cbdc74593ab39fcb31f5f
This commit is contained in:
zhushengle
2023-02-23 20:09:42 +08:00
parent 10c5e2e666
commit c141a92544
39 changed files with 1691 additions and 351 deletions

View File

@@ -88,6 +88,63 @@ int WaitChild(pid_t pid, int *status, int errNo1, int errNo2)
return 0;
}
int ReadFile(const char *filepath, char *buf)
{
FILE *fpid = nullptr;
fpid = fopen(filepath, "r");
if (fpid == nullptr) {
return -1;
}
size_t trd = fread(buf, 1, 512, fpid);
(void)fclose(fpid);
return trd;
}
int WriteFile(const char *filepath, const char *buf)
{
int fd = open(filepath, O_WRONLY);
if (fd == -1) {
return -1;
}
size_t twd = write(fd, buf, strlen(buf));
if (twd == -1) {
(void)close(fd);
return -1;
}
(void)close(fd);
return twd;
}
int GetLine(char *buf, int count, int maxLen, char **array)
{
char *head = buf;
char *tail = buf;
char index = 0;
if ((buf == NULL) || (strlen(buf) == 0)) {
return 0;
}
while (*tail != '\0') {
if (*tail != '\n') {
tail++;
continue;
}
if (index >= count) {
return index + 1;
}
array[index] = head;
index++;
*tail = '\0';
if (strlen(head) > maxLen) {
return index + 1;
}
tail++;
head = tail;
tail++;
}
return (index + 1);
}
std::string GenContainerLinkPath(int pid, const std::string& containerType)
{
std::ostringstream buf;
@@ -125,6 +182,79 @@ HWTEST_F(ContainerTest, ItContainer001, TestSize.Level0)
ItContainer001();
}
#if defined(LOSCFG_USER_TEST_USER_CONTAINER)
/**
* @tc.name: Container_UTS_Test_001
* @tc.desc: uts container function test case
* @tc.type: FUNC
* @tc.require: issueI6EC0A
* @tc.author:
*/
HWTEST_F(ContainerTest, ItUserContainer001, TestSize.Level0)
{
ItUserContainer001();
}
/**
* @tc.name: Container_UTS_Test_002
* @tc.desc: uts container function test case
* @tc.type: FUNC
* @tc.require: issueI6EC0A
* @tc.author:
*/
HWTEST_F(ContainerTest, ItUserContainer002, TestSize.Level0)
{
ItUserContainer002();
}
/**
* @tc.name: Container_UTS_Test_003
* @tc.desc: uts container function test case
* @tc.type: FUNC
* @tc.require: issueI6EC0A
* @tc.author:
*/
HWTEST_F(ContainerTest, ItUserContainer003, TestSize.Level0)
{
ItUserContainer003();
}
/**
* @tc.name: Container_UTS_Test_004
* @tc.desc: uts container function test case
* @tc.type: FUNC
* @tc.require: issueI6EC0A
* @tc.author:
*/
HWTEST_F(ContainerTest, ItUserContainer004, TestSize.Level0)
{
ItUserContainer004();
}
/**
* @tc.name: Container_UTS_Test_006
* @tc.desc: uts container function test case
* @tc.type: FUNC
* @tc.require: issueI6HDQK
* @tc.author:
*/
HWTEST_F(ContainerTest, ItUserContainer006, TestSize.Level0)
{
ItUserContainer006();
}
/**
* @tc.name: Container_UTS_Test_007
* @tc.desc: uts container function test case
* @tc.type: FUNC
* @tc.require: issueI6HDQK
* @tc.author:
*/
HWTEST_F(ContainerTest, ItUserContainer007, TestSize.Level0)
{
ItUserContainer007();
}
#endif
#if defined(LOSCFG_USER_TEST_PID_CONTAINER)
/**
* @tc.name: Container_Pid_Test_023
@@ -221,6 +351,30 @@ HWTEST_F(ContainerTest, ItPidContainer031, TestSize.Level0)
{
ItPidContainer031();
}
/**
* @tc.name: Container_Pid_Test_032
* @tc.desc: pid container function test case
* @tc.type: FUNC
* @tc.require: issueI6HDQK
* @tc.author:
*/
HWTEST_F(ContainerTest, ItPidContainer032, TestSize.Level0)
{
ItPidContainer032();
}
/**
* @tc.name: Container_Pid_Test_033
* @tc.desc: pid container function test case
* @tc.type: FUNC
* @tc.require: issueI6HDQK
* @tc.author:
*/
HWTEST_F(ContainerTest, ItPidContainer033, TestSize.Level0)
{
ItPidContainer033();
}
#endif
#if defined(LOSCFG_USER_TEST_UTS_CONTAINER)
/**
@@ -282,6 +436,30 @@ HWTEST_F(ContainerTest, ItUtsContainer006, TestSize.Level0)
{
ItUtsContainer006();
}
/**
* @tc.name: Container_UTS_Test_007
* @tc.desc: uts container function test case
* @tc.type: FUNC
* @tc.require: issueI6HDQK
* @tc.author:
*/
HWTEST_F(ContainerTest, ItUtsContainer007, TestSize.Level0)
{
ItUtsContainer007();
}
/**
* @tc.name: Container_UTS_Test_008
* @tc.desc: uts container function test case
* @tc.type: FUNC
* @tc.require: issueI6HDQK
* @tc.author:
*/
HWTEST_F(ContainerTest, ItUtsContainer008, TestSize.Level0)
{
ItUtsContainer008();
}
#endif
#if defined(LOSCFG_USER_TEST_MNT_CONTAINER)
@@ -381,6 +559,30 @@ HWTEST_F(ContainerTest, ItMntContainer008, TestSize.Level0)
ItMntContainer008();
}
/**
* @tc.name: Container_MNT_Test_009
* @tc.desc: mnt container function test case
* @tc.type: FUNC
* @tc.require: issueI6HDQK
* @tc.author:
*/
HWTEST_F(ContainerTest, ItMntContainer009, TestSize.Level0)
{
ItMntContainer009();
}
/**
* @tc.name: Container_MNT_Test_010
* @tc.desc: mnt container function test case
* @tc.type: FUNC
* @tc.require: issueI6HDQK
* @tc.author:
*/
HWTEST_F(ContainerTest, ItMntContainer010, TestSize.Level0)
{
ItMntContainer010();
}
/**
* @tc.name: chroot_Test_001
* @tc.desc: chroot function test case
@@ -478,6 +680,30 @@ HWTEST_F(ContainerTest, ItIpcContainer006, TestSize.Level0)
{
ItIpcContainer006();
}
/**
* @tc.name: Container_IPC_Test_007
* @tc.desc: ipc container function test case
* @tc.type: FUNC
* @tc.require: issueI6HDQK
* @tc.author:
*/
HWTEST_F(ContainerTest, ItIpcContainer007, TestSize.Level0)
{
ItIpcContainer007();
}
/**
* @tc.name: Container_IPC_Test_008
* @tc.desc: ipc container function test case
* @tc.type: FUNC
* @tc.require: issueI6HDQK
* @tc.author:
*/
HWTEST_F(ContainerTest, ItIpcContainer008, TestSize.Level0)
{
ItIpcContainer008();
}
#endif
#if defined(LOSCFG_USER_TEST_TIME_CONTAINER)
@@ -541,6 +767,18 @@ HWTEST_F(ContainerTest, ItTimeContainer005, TestSize.Level0)
ItTimeContainer005();
}
/**
* @tc.name: Container_TIME_Test_006
* @tc.desc: time container function test case
* @tc.type: FUNC
* @tc.require: issueI6HDQK
* @tc.author:
*/
HWTEST_F(ContainerTest, ItTimeContainer006, TestSize.Level0)
{
ItTimeContainer006();
}
/*
* @tc.name: Container_TIME_Test_007
* @tc.desc: time container function test case
@@ -589,55 +827,6 @@ HWTEST_F(ContainerTest, ItTimeContainer010, TestSize.Level0)
ItTimeContainer010();
}
#endif
#if defined(LOSCFG_USER_TEST_USER_CONTAINER)
/**
* @tc.name: Container_UTS_Test_001
* @tc.desc: uts container function test case
* @tc.type: FUNC
* @tc.require: issueI6EC0A
* @tc.author:
*/
HWTEST_F(ContainerTest, ItUserContainer001, TestSize.Level0)
{
ItUserContainer001();
}
/**
* @tc.name: Container_UTS_Test_002
* @tc.desc: uts container function test case
* @tc.type: FUNC
* @tc.require: issueI6EC0A
* @tc.author:
*/
HWTEST_F(ContainerTest, ItUserContainer002, TestSize.Level0)
{
ItUserContainer002();
}
/**
* @tc.name: Container_UTS_Test_003
* @tc.desc: uts container function test case
* @tc.type: FUNC
* @tc.require: issueI6EC0A
* @tc.author:
*/
HWTEST_F(ContainerTest, ItUserContainer003, TestSize.Level0)
{
ItUserContainer003();
}
/**
* @tc.name: Container_UTS_Test_004
* @tc.desc: uts container function test case
* @tc.type: FUNC
* @tc.require: issueI6EC0A
* @tc.author:
*/
HWTEST_F(ContainerTest, ItUserContainer004, TestSize.Level0)
{
ItUserContainer004();
}
#endif
#endif /* LOSCFG_USER_TEST_SMOKE */
#if defined(LOSCFG_USER_TEST_FULL)

View File

@@ -91,6 +91,10 @@ extern "C" {
#define CLONE_NEWTIME 0x00000080
}
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);
int ChildFunction(void *args);
pid_t CloneWrapper(int (*func)(void *), int flag, void *args);
@@ -142,11 +146,11 @@ void ItUserContainer002(void);
void ItUserContainer003(void);
void ItUserContainer004(void);
void ItUserContainer005(void);
#if defined(LOSCFG_USER_TEST_SMOKE)
void ItUserContainer006(void);
void ItUserContainer007(void);
void ItContainer001(void);
void ItContainerChroot001(void);
void ItContainerChroot002(void);
#if defined(LOSCFG_USER_TEST_PID_CONTAINER)
void ItPidContainer023(void);
void ItPidContainer025(void);
void ItPidContainer026(void);
@@ -155,15 +159,15 @@ void ItPidContainer028(void);
void ItPidContainer029(void);
void ItPidContainer030(void);
void ItPidContainer031(void);
#endif
#if defined(LOSCFG_USER_TEST_UTS_CONTAINER)
void ItPidContainer032(void);
void ItPidContainer033(void);
void ItUtsContainer001(void);
void ItUtsContainer002(void);
void ItUtsContainer004(void);
void ItUtsContainer005(void);
void ItUtsContainer006(void);
#endif
#if defined(LOSCFG_USER_TEST_MNT_CONTAINER)
void ItUtsContainer007(void);
void ItUtsContainer008(void);
void ItMntContainer001(void);
void ItMntContainer002(void);
void ItMntContainer003(void);
@@ -172,16 +176,16 @@ void ItMntContainer005(void);
void ItMntContainer006(void);
void ItMntContainer007(void);
void ItMntContainer008(void);
#endif
#if defined(LOSCFG_USER_TEST_IPC_CONTAINER)
void ItMntContainer009(void);
void ItMntContainer010(void);
void ItIpcContainer001(void);
void ItIpcContainer002(void);
void ItIpcContainer003(void);
void ItIpcContainer004(void);
void ItIpcContainer005(void);
void ItIpcContainer006(void);
#endif
#if defined(LOSCFG_USER_TEST_TIME_CONTAINER)
void ItIpcContainer007(void);
void ItIpcContainer008(void);
void ItTimeContainer001(void);
void ItTimeContainer002(void);
void ItTimeContainer003(void);
@@ -192,11 +196,6 @@ void ItTimeContainer007(void);
void ItTimeContainer008(void);
void ItTimeContainer009(void);
void ItTimeContainer010(void);
#endif
#endif
#if defined(LOSCFG_USER_TEST_FULL)
#if defined(LOSCFG_USER_TEST_PID_CONTAINER)
void ItPidContainer001(void);
void ItPidContainer002(void);
void ItPidContainer003(void);
@@ -220,10 +219,6 @@ void ItPidContainer020(void);
void ItPidContainer021(void);
void ItPidContainer022(void);
void ItPidContainer024(void);
#endif
#if defined(LOSCFG_USER_TEST_UTS_CONTAINER)
void ItUtsContainer003(void);
#endif
#endif
#endif /* _IT_CONTAINER_TEST_H */

View File

@@ -50,6 +50,8 @@ if (defined(LOSCFG_USER_TEST_PID_CONTAINER)) {
"$TEST_UNITTEST_DIR/container/smoke/It_pid_container_029.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_pid_container_030.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_pid_container_031.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_pid_container_032.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_pid_container_033.cpp",
]
sources_full += [
"$TEST_UNITTEST_DIR/container/full/It_pid_container_001.cpp",
@@ -84,6 +86,8 @@ if (defined(LOSCFG_USER_TEST_UTS_CONTAINER)) {
"$TEST_UNITTEST_DIR/container/smoke/It_uts_container_004.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_uts_container_005.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_uts_container_006.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_uts_container_007.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_uts_container_008.cpp",
]
sources_full +=
[ "$TEST_UNITTEST_DIR/container/full/It_uts_container_003.cpp" ]
@@ -100,6 +104,8 @@ if (defined(LOSCFG_USER_TEST_MNT_CONTAINER)) {
"$TEST_UNITTEST_DIR/container/smoke/It_mnt_container_006.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_mnt_container_007.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_mnt_container_008.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_mnt_container_009.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_mnt_container_010.cpp",
]
}
@@ -111,6 +117,8 @@ if (defined(LOSCFG_USER_TEST_IPC_CONTAINER)) {
"$TEST_UNITTEST_DIR/container/smoke/It_ipc_container_004.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_ipc_container_005.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_ipc_container_006.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_ipc_container_007.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_ipc_container_008.cpp",
]
}
@@ -135,6 +143,8 @@ if (defined(LOSCFG_USER_TEST_USER_CONTAINER)) {
"$TEST_UNITTEST_DIR/container/smoke/It_user_container_002.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_user_container_003.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_user_container_004.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_user_container_006.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_user_container_007.cpp",
]
sources_full +=
[ "$TEST_UNITTEST_DIR/container/full/It_user_container_005.cpp" ]

View File

@@ -33,13 +33,11 @@
#include "pthread.h"
#include "sched.h"
const int SLEEP_TIME_US = 1000;
const int LOOP_NUM = 1000;
const int LOOP_NUM = 100;
static int ChildFunc(void *arg)
{
(void)arg;
usleep(SLEEP_TIME_US);
exit(EXIT_CODE_ERRNO_5);
}
@@ -61,6 +59,7 @@ static int GroupProcess(void *arg)
if (status != EXIT_CODE_ERRNO_5) {
return EXIT_CODE_ERRNO_2;
}
usleep(10000);
}
exit(EXIT_CODE_ERRNO_5);

View File

@@ -29,12 +29,13 @@
*/
#include <cstdio>
#include "It_process_fs_test.h"
#include "It_container_test.h"
static int const configLen = 16;
static int const invalidNum = 2;
static const int CHILD_FUNC_ARG = 0x2088;
const int STACK_SIZE = (1024 * 1024);
static const int MAX_CONTAINER = 10;
static const int g_buffSize = 512;
static const int g_arryLen = 4;
static const int g_readLen = 254;
static int childFunc(void *arg)
{
@@ -44,32 +45,56 @@ static int childFunc(void *arg)
return 0;
}
void ItProcessFs020(void)
void ItIpcContainer007(void)
{
std::string path = "/proc/sys/user/max_net_container";
int fd = open(path.c_str(), O_WRONLY);
ASSERT_NE(fd, -1);
std::string path = "/proc/sys/user/max_ipc_container";
char *array[g_arryLen] = { nullptr };
char buf[g_buffSize] = { 0 };
char buf[configLen];
size_t ret = sprintf_s(buf, configLen, "%d", invalidNum);
ASSERT_GT(ret, 0);
ret = write(fd, buf, (strlen(buf) + 1));
int ret = ReadFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
int arg = CHILD_FUNC_ARG;
GetLine(buf, g_arryLen, g_readLen, array);
int value = atoi(array[1] + strlen("limit: "));
ASSERT_EQ(value, MAX_CONTAINER);
int usedCount = atoi(array[2] + strlen("count: "));
(void)memset_s(buf, configLen, 0, configLen);
ret = sprintf_s(buf, configLen, "%d", usedCount + 1);
ASSERT_GT(ret, 0);
ret = WriteFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
char *stack = (char *)mmap(nullptr, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
-1, 0);
ASSERT_NE(stack, nullptr);
char *stackTop = stack + STACK_SIZE;
auto pid = clone(childFunc, stackTop, CLONE_NEWNET, &arg);
ASSERT_NE(pid, -1);
pid = clone(childFunc, stackTop, CLONE_NEWNET, &arg);
ASSERT_NE(pid, -1);
auto pid1 = clone(childFunc, stackTop, CLONE_NEWIPC, NULL);
ASSERT_NE(pid1, -1);
pid = clone(childFunc, stackTop, CLONE_NEWNET, &arg);
ASSERT_EQ(pid, -1);
auto pid2 = clone(childFunc, stackTop, CLONE_NEWIPC, NULL);
ASSERT_EQ(pid2, -1);
(void)close(fd);
ret = waitpid(pid1, NULL, 0);
ASSERT_EQ(ret, pid1);
(void)memset_s(buf, configLen, 0, configLen);
ret = sprintf_s(buf, configLen, "%d", value);
ASSERT_GT(ret, 0);
ret = WriteFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
(void)memset_s(buf, configLen, 0, configLen);
ret = ReadFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
GetLine(buf, g_arryLen, g_readLen, array);
value = atoi(array[1] + strlen("limit: "));
ASSERT_EQ(value, MAX_CONTAINER);
}

View File

@@ -0,0 +1,105 @@
/*
* 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.
*/
#include <cstdio>
#include "It_container_test.h"
static int const configLen = 16;
static const int MAX_CONTAINER = 10;
static const int g_buffSize = 512;
static const int g_arryLen = 4;
static const int g_readLen = 254;
static int childFunc(void *arg)
{
(void)arg;
int ret = unshare(CLONE_NEWIPC);
if (ret != 0) {
return EXIT_CODE_ERRNO_1;
}
return 0;
}
void ItIpcContainer008(void)
{
std::string path = "/proc/sys/user/max_ipc_container";
char *array[g_arryLen] = { nullptr };
char buf[g_buffSize] = { 0 };
int status = 0;
int ret = ReadFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
GetLine(buf, g_arryLen, g_readLen, array);
int value = atoi(array[1] + strlen("limit: "));
ASSERT_EQ(value, MAX_CONTAINER);
int usedCount = atoi(array[2] + strlen("count: "));
(void)memset_s(buf, configLen, 0, configLen);
ret = sprintf_s(buf, configLen, "%d", usedCount + 1);
ASSERT_GT(ret, 0);
ret = WriteFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
char *stack = (char *)mmap(nullptr, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
-1, 0);
ASSERT_NE(stack, nullptr);
char *stackTop = stack + STACK_SIZE;
auto pid1 = clone(childFunc, stackTop, CLONE_NEWIPC, NULL);
ASSERT_NE(pid1, -1);
ret = waitpid(pid1, &status, 0);
ASSERT_EQ(ret, pid1);
ret = WIFEXITED(status);
ASSERT_NE(ret, 0);
ret = WEXITSTATUS(status);
ASSERT_EQ(ret, EXIT_CODE_ERRNO_1);
(void)memset_s(buf, configLen, 0, configLen);
ret = sprintf_s(buf, configLen, "%d", value);
ASSERT_GT(ret, 0);
ret = WriteFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
(void)memset_s(buf, configLen, 0, configLen);
ret = ReadFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
GetLine(buf, g_arryLen, g_readLen, array);
value = atoi(array[1] + strlen("limit: "));
ASSERT_EQ(value, MAX_CONTAINER);
}

View File

@@ -29,12 +29,13 @@
*/
#include <cstdio>
#include "It_process_fs_test.h"
#include "It_container_test.h"
static int const configLen = 16;
static int const invalidNum = 2;
static const int CHILD_FUNC_ARG = 0x2088;
const int STACK_SIZE = (1024 * 1024);
static const int MAX_CONTAINER = 10;
static const int g_buffSize = 512;
static const int g_arryLen = 4;
static const int g_readLen = 254;
static int childFunc(void *arg)
{
@@ -44,32 +45,56 @@ static int childFunc(void *arg)
return 0;
}
void ItProcessFs018(void)
void ItMntContainer009(void)
{
std::string path = "/proc/sys/user/max_mnt_container";
int fd = open(path.c_str(), O_WRONLY);
ASSERT_NE(fd, -1);
char *array[g_arryLen] = { nullptr };
char buf[g_buffSize] = { 0 };
char buf[configLen];
size_t ret = sprintf_s(buf, configLen, "%d", invalidNum);
ASSERT_GT(ret, 0);
ret = write(fd, buf, (strlen(buf) + 1));
int ret = ReadFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
int arg = CHILD_FUNC_ARG;
GetLine(buf, g_arryLen, g_readLen, array);
int value = atoi(array[1] + strlen("limit: "));
ASSERT_EQ(value, MAX_CONTAINER);
int usedCount = atoi(array[2] + strlen("count: "));
(void)memset_s(buf, configLen, 0, configLen);
ret = sprintf_s(buf, configLen, "%d", usedCount + 1);
ASSERT_GT(ret, 0);
ret = WriteFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
char *stack = (char *)mmap(nullptr, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
-1, 0);
ASSERT_NE(stack, nullptr);
char *stackTop = stack + STACK_SIZE;
auto pid = clone(childFunc, stackTop, CLONE_NEWNS, &arg);
ASSERT_NE(pid, -1);
pid = clone(childFunc, stackTop, CLONE_NEWNS, &arg);
ASSERT_NE(pid, -1);
auto pid1 = clone(childFunc, stackTop, CLONE_NEWNS, NULL);
ASSERT_NE(pid1, -1);
pid = clone(childFunc, stackTop, CLONE_NEWNS, &arg);
ASSERT_EQ(pid, -1);
auto pid2 = clone(childFunc, stackTop, CLONE_NEWNS, NULL);
ASSERT_EQ(pid2, -1);
(void)close(fd);
ret = waitpid(pid1, NULL, 0);
ASSERT_EQ(ret, pid1);
(void)memset_s(buf, configLen, 0, configLen);
ret = sprintf_s(buf, configLen, "%d", value);
ASSERT_GT(ret, 0);
ret = WriteFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
(void)memset_s(buf, configLen, 0, configLen);
ret = ReadFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
GetLine(buf, g_arryLen, g_readLen, array);
value = atoi(array[1] + strlen("limit: "));
ASSERT_EQ(value, MAX_CONTAINER);
}

View File

@@ -0,0 +1,105 @@
/*
* 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.
*/
#include <cstdio>
#include "It_container_test.h"
static int const configLen = 16;
static const int MAX_CONTAINER = 10;
static const int g_buffSize = 512;
static const int g_arryLen = 4;
static const int g_readLen = 254;
static int childFunc(void *arg)
{
(void)arg;
int ret = unshare(CLONE_NEWNS);
if (ret != 0) {
return EXIT_CODE_ERRNO_1;
}
return 0;
}
void ItMntContainer010(void)
{
std::string path = "/proc/sys/user/max_mnt_container";
char *array[g_arryLen] = { nullptr };
char buf[g_buffSize] = { 0 };
int status = 0;
int ret = ReadFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
GetLine(buf, g_arryLen, g_readLen, array);
int value = atoi(array[1] + strlen("limit: "));
ASSERT_EQ(value, MAX_CONTAINER);
int usedCount = atoi(array[2] + strlen("count: "));
(void)memset_s(buf, configLen, 0, configLen);
ret = sprintf_s(buf, configLen, "%d", usedCount + 1);
ASSERT_GT(ret, 0);
ret = WriteFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
char *stack = (char *)mmap(nullptr, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
-1, 0);
ASSERT_NE(stack, nullptr);
char *stackTop = stack + STACK_SIZE;
auto pid1 = clone(childFunc, stackTop, CLONE_NEWNS, NULL);
ASSERT_NE(pid1, -1);
ret = waitpid(pid1, &status, 0);
ASSERT_EQ(ret, pid1);
ret = WIFEXITED(status);
ASSERT_NE(ret, 0);
ret = WEXITSTATUS(status);
ASSERT_EQ(ret, EXIT_CODE_ERRNO_1);
(void)memset_s(buf, configLen, 0, configLen);
ret = sprintf_s(buf, configLen, "%d", value);
ASSERT_GT(ret, 0);
ret = WriteFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
(void)memset_s(buf, configLen, 0, configLen);
ret = ReadFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
GetLine(buf, g_arryLen, g_readLen, array);
value = atoi(array[1] + strlen("limit: "));
ASSERT_EQ(value, MAX_CONTAINER);
}

View File

@@ -29,12 +29,13 @@
*/
#include <cstdio>
#include "It_process_fs_test.h"
#include "It_container_test.h"
static int const configLen = 16;
static int const invalidNum = 2;
static const int CHILD_FUNC_ARG = 0x2088;
const int STACK_SIZE = (1024 * 1024);
static const int MAX_CONTAINER = 10;
static const int g_buffSize = 512;
static const int g_arryLen = 4;
static const int g_readLen = 254;
static int childFunc(void *arg)
{
@@ -44,32 +45,56 @@ static int childFunc(void *arg)
return 0;
}
void ItProcessFs019(void)
void ItPidContainer032(void)
{
std::string path = "/proc/sys/user/max_pid_container";
int fd = open(path.c_str(), O_WRONLY);
ASSERT_NE(fd, -1);
char *array[g_arryLen] = { nullptr };
char buf[g_buffSize] = { 0 };
char buf[configLen];
size_t ret = sprintf_s(buf, configLen, "%d", invalidNum);
ASSERT_GT(ret, 0);
ret = write(fd, buf, (strlen(buf) + 1));
int ret = ReadFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
int arg = CHILD_FUNC_ARG;
GetLine(buf, g_arryLen, g_readLen, array);
int value = atoi(array[1] + strlen("limit: "));
ASSERT_EQ(value, MAX_CONTAINER);
int usedCount = atoi(array[2] + strlen("count: "));
(void)memset_s(buf, configLen, 0, configLen);
ret = sprintf_s(buf, configLen, "%d", usedCount + 1);
ASSERT_GT(ret, 0);
ret = WriteFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
char *stack = (char *)mmap(nullptr, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
-1, 0);
ASSERT_NE(stack, nullptr);
char *stackTop = stack + STACK_SIZE;
auto pid = clone(childFunc, stackTop, CLONE_NEWPID, &arg);
ASSERT_NE(pid, -1);
pid = clone(childFunc, stackTop, CLONE_NEWPID, &arg);
ASSERT_NE(pid, -1);
auto pid1 = clone(childFunc, stackTop, CLONE_NEWPID, NULL);
ASSERT_NE(pid1, -1);
pid = clone(childFunc, stackTop, CLONE_NEWPID, &arg);
ASSERT_EQ(pid, -1);
auto pid2 = clone(childFunc, stackTop, CLONE_NEWPID, NULL);
ASSERT_EQ(pid2, -1);
(void)close(fd);
ret = waitpid(pid1, NULL, 0);
ASSERT_EQ(ret, pid1);
(void)memset_s(buf, configLen, 0, configLen);
ret = sprintf_s(buf, configLen, "%d", value);
ASSERT_GT(ret, 0);
ret = WriteFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
(void)memset_s(buf, configLen, 0, configLen);
ret = ReadFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
GetLine(buf, g_arryLen, g_readLen, array);
value = atoi(array[1] + strlen("limit: "));
ASSERT_EQ(value, MAX_CONTAINER);
}

View File

@@ -0,0 +1,105 @@
/*
* 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.
*/
#include <cstdio>
#include "It_container_test.h"
static int const configLen = 16;
static const int MAX_CONTAINER = 10;
static const int g_buffSize = 512;
static const int g_arryLen = 4;
static const int g_readLen = 254;
static int childFunc(void *arg)
{
(void)arg;
int ret = unshare(CLONE_NEWPID);
if (ret != 0) {
return EXIT_CODE_ERRNO_1;
}
return 0;
}
void ItPidContainer033(void)
{
std::string path = "/proc/sys/user/max_pid_container";
char *array[g_arryLen] = { nullptr };
char buf[g_buffSize] = { 0 };
int status = 0;
int ret = ReadFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
GetLine(buf, g_arryLen, g_readLen, array);
int value = atoi(array[1] + strlen("limit: "));
ASSERT_EQ(value, MAX_CONTAINER);
int usedCount = atoi(array[2] + strlen("count: "));
(void)memset_s(buf, configLen, 0, configLen);
ret = sprintf_s(buf, configLen, "%d", usedCount + 1);
ASSERT_GT(ret, 0);
ret = WriteFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
char *stack = (char *)mmap(nullptr, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
-1, 0);
ASSERT_NE(stack, nullptr);
char *stackTop = stack + STACK_SIZE;
auto pid1 = clone(childFunc, stackTop, CLONE_NEWPID, NULL);
ASSERT_NE(pid1, -1);
ret = waitpid(pid1, &status, 0);
ASSERT_EQ(ret, pid1);
ret = WIFEXITED(status);
ASSERT_NE(ret, 0);
ret = WEXITSTATUS(status);
ASSERT_EQ(ret, EXIT_CODE_ERRNO_1);
(void)memset_s(buf, configLen, 0, configLen);
ret = sprintf_s(buf, configLen, "%d", value);
ASSERT_GT(ret, 0);
ret = WriteFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
(void)memset_s(buf, configLen, 0, configLen);
ret = ReadFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
GetLine(buf, g_arryLen, g_readLen, array);
value = atoi(array[1] + strlen("limit: "));
ASSERT_EQ(value, MAX_CONTAINER);
}

View File

@@ -27,26 +27,85 @@
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <cstdio>
#include "It_container_test.h"
const int MAX_TIME_CONTAINER = 64;
const int STR_LEN = 100;
static int const configLen = 16;
static const int MAX_CONTAINER = 10;
static const int g_buffSize = 512;
static const int g_arryLen = 4;
static const int g_readLen = 254;
static int childFunc(void *arg)
{
(void)arg;
int ret = unshare(CLONE_NEWTIME);
if (ret != 0) {
return EXIT_CODE_ERRNO_1;
}
ret = unshare(CLONE_NEWTIME);
if (ret != 0) {
return EXIT_CODE_ERRNO_2;
}
return 0;
}
void ItTimeContainer006(void)
{
int ret;
char *fileName = "/proc/sys/user/max_time_container";
FILE *fp = nullptr;
char strBuf[STR_LEN] = {0};
std::string path = "/proc/sys/user/max_time_container";
char *array[g_arryLen] = { nullptr };
char buf[g_buffSize] = { 0 };
int status = 0;
fp = fopen(fileName, "rb");
ASSERT_TRUE(fp != 0);
int ret = ReadFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
ret = fread(strBuf, 1, STR_LEN, fp);
ASSERT_TRUE(ret != -1);
GetLine(buf, g_arryLen, g_readLen, array);
ret = atoi(strBuf);
ASSERT_EQ(ret, MAX_TIME_CONTAINER);
int value = atoi(array[1] + strlen("limit: "));
ASSERT_EQ(value, MAX_CONTAINER);
(void)fclose(fp);
int usedCount = atoi(array[2] + strlen("count: "));
(void)memset_s(buf, configLen, 0, configLen);
ret = sprintf_s(buf, configLen, "%d", usedCount + 1);
ASSERT_GT(ret, 0);
ret = WriteFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
char *stack = (char *)mmap(nullptr, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
-1, 0);
ASSERT_NE(stack, nullptr);
char *stackTop = stack + STACK_SIZE;
auto pid1 = clone(childFunc, stackTop, CLONE_NEWTIME, NULL);
ASSERT_NE(pid1, -1);
ret = waitpid(pid1, &status, 0);
ASSERT_EQ(ret, pid1);
ret = WIFEXITED(status);
ASSERT_NE(ret, 0);
ret = WEXITSTATUS(status);
ASSERT_EQ(ret, EXIT_CODE_ERRNO_2);
(void)memset_s(buf, configLen, 0, configLen);
ret = sprintf_s(buf, configLen, "%d", value);
ASSERT_GT(ret, 0);
ret = WriteFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
(void)memset_s(buf, configLen, 0, configLen);
ret = ReadFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
GetLine(buf, g_arryLen, g_readLen, array);
value = atoi(array[1] + strlen("limit: "));
ASSERT_EQ(value, MAX_CONTAINER);
}

View File

@@ -29,12 +29,13 @@
*/
#include <cstdio>
#include "It_process_fs_test.h"
#include "It_container_test.h"
static int const configLen = 16;
static int const invalidNum = 2;
static const int CHILD_FUNC_ARG = 0x2088;
const int STACK_SIZE = (1024 * 1024);
static const int MAX_CONTAINER = 10;
static const int g_buffSize = 512;
static const int g_arryLen = 4;
static const int g_readLen = 254;
static int childFunc(void *arg)
{
@@ -44,32 +45,56 @@ static int childFunc(void *arg)
return 0;
}
void ItProcessFs017(void)
void ItUserContainer006(void)
{
std::string path = "/proc/sys/user/max_user_container";
int fd = open(path.c_str(), O_WRONLY);
ASSERT_NE(fd, -1);
char *array[g_arryLen] = { nullptr };
char buf[g_buffSize] = { 0 };
char buf[configLen];
size_t ret = sprintf_s(buf, configLen, "%d", invalidNum);
ASSERT_GT(ret, 0);
ret = write(fd, buf, (strlen(buf) + 1));
int ret = ReadFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
int arg = CHILD_FUNC_ARG;
GetLine(buf, g_arryLen, g_readLen, array);
int value = atoi(array[1] + strlen("limit: "));
ASSERT_EQ(value, MAX_CONTAINER);
int usedCount = atoi(array[2] + strlen("count: "));
(void)memset_s(buf, configLen, 0, configLen);
ret = sprintf_s(buf, configLen, "%d", usedCount + 1);
ASSERT_GT(ret, 0);
ret = WriteFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
char *stack = (char *)mmap(nullptr, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
-1, 0);
ASSERT_NE(stack, nullptr);
char *stackTop = stack + STACK_SIZE;
auto pid = clone(childFunc, stackTop, CLONE_NEWUSER, &arg);
ASSERT_NE(pid, -1);
pid = clone(childFunc, stackTop, CLONE_NEWUSER, &arg);
ASSERT_NE(pid, -1);
auto pid1 = clone(childFunc, stackTop, CLONE_NEWUSER, NULL);
ASSERT_NE(pid1, -1);
pid = clone(childFunc, stackTop, CLONE_NEWUSER, &arg);
ASSERT_EQ(pid, -1);
auto pid2 = clone(childFunc, stackTop, CLONE_NEWUSER, NULL);
ASSERT_EQ(pid2, -1);
(void)close(fd);
ret = waitpid(pid1, NULL, 0);
ASSERT_EQ(ret, pid1);
(void)memset_s(buf, configLen, 0, configLen);
ret = sprintf_s(buf, configLen, "%d", value);
ASSERT_GT(ret, 0);
ret = WriteFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
(void)memset_s(buf, configLen, 0, configLen);
ret = ReadFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
GetLine(buf, g_arryLen, g_readLen, array);
value = atoi(array[1] + strlen("limit: "));
ASSERT_EQ(value, MAX_CONTAINER);
}

View File

@@ -0,0 +1,105 @@
/*
* 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.
*/
#include <cstdio>
#include "It_container_test.h"
static int const configLen = 16;
static const int MAX_CONTAINER = 10;
static const int g_buffSize = 512;
static const int g_arryLen = 4;
static const int g_readLen = 254;
static int childFunc(void *arg)
{
(void)arg;
int ret = unshare(CLONE_NEWUSER);
if (ret != 0) {
return EXIT_CODE_ERRNO_1;
}
return 0;
}
void ItUserContainer007(void)
{
std::string path = "/proc/sys/user/max_user_container";
char *array[g_arryLen] = { nullptr };
char buf[g_buffSize] = { 0 };
int status = 0;
int ret = ReadFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
GetLine(buf, g_arryLen, g_readLen, array);
int value = atoi(array[1] + strlen("limit: "));
ASSERT_EQ(value, MAX_CONTAINER);
int usedCount = atoi(array[2] + strlen("count: "));
(void)memset_s(buf, configLen, 0, configLen);
ret = sprintf_s(buf, configLen, "%d", usedCount + 1);
ASSERT_GT(ret, 0);
ret = WriteFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
char *stack = (char *)mmap(nullptr, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
-1, 0);
ASSERT_NE(stack, nullptr);
char *stackTop = stack + STACK_SIZE;
auto pid1 = clone(childFunc, stackTop, CLONE_NEWUSER, NULL);
ASSERT_NE(pid1, -1);
ret = waitpid(pid1, &status, 0);
ASSERT_EQ(ret, pid1);
ret = WIFEXITED(status);
ASSERT_NE(ret, 0);
ret = WEXITSTATUS(status);
ASSERT_EQ(ret, EXIT_CODE_ERRNO_1);
(void)memset_s(buf, configLen, 0, configLen);
ret = sprintf_s(buf, configLen, "%d", value);
ASSERT_GT(ret, 0);
ret = WriteFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
(void)memset_s(buf, configLen, 0, configLen);
ret = ReadFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
GetLine(buf, g_arryLen, g_readLen, array);
value = atoi(array[1] + strlen("limit: "));
ASSERT_EQ(value, MAX_CONTAINER);
}

View File

@@ -38,54 +38,83 @@ static int ChildFun(void *p)
return EXIT_CODE_ERRNO_3;
}
void ItUtsContainer005(void)
static int UtsContainerTest(void *arg)
{
(void)arg;
pid_t callerPid;
int childPid;
int fd = -1;
int ret;
int status;
int setFlag;
int ret, status, setFlag;
char targetpath[100];
char old_uts_link[100];
char new_uts_link[100];
const char *containerType = "uts";
callerPid = getpid();
childPid = clone(ChildFun, NULL, CLONE_NEWUTS | SIGCHLD, NULL);
ASSERT_NE(childPid, -1);
if (childPid == -1) {
return EXIT_CODE_ERRNO_1;
}
auto linkBuffer = ReadlinkContainer(callerPid, containerType);
ASSERT_TRUE(linkBuffer.c_str() != NULL);
ret = sprintf_s(old_uts_link, sizeof(old_uts_link), "%s", linkBuffer.c_str());
ASSERT_NE(ret, -1);
auto linkBuffer1 = ReadlinkContainer(callerPid, containerType);
if (linkBuffer1.c_str() == NULL) {
return EXIT_CODE_ERRNO_2;
}
ret = sprintf_s(targetpath, sizeof(targetpath), "/proc/%d/container/uts", childPid);
ASSERT_NE(ret, -1);
if (ret == -1) {
return EXIT_CODE_ERRNO_4;
}
fd = open(targetpath, O_RDONLY | O_CLOEXEC);
ASSERT_NE(fd, -1);
if (fd == -1) {
return EXIT_CODE_ERRNO_5;
}
setFlag = CLONE_NEWUTS;
ret = setns(fd, setFlag);
ASSERT_NE(ret, -1);
(void)close(fd);
if (ret == -1) {
return EXIT_CODE_ERRNO_6;
}
/* NOTE: close fd, otherwise test fail */
ret = close(fd);
fd = -1;
ASSERT_NE(ret, -1);
auto linkBuffer2 = ReadlinkContainer(callerPid, containerType);
linkBuffer = ReadlinkContainer(callerPid, containerType);
ret = sprintf_s(new_uts_link, sizeof(new_uts_link), "%s", linkBuffer.c_str());
ASSERT_NE(ret, -1);
ASSERT_STRNE(old_uts_link, new_uts_link);
ret = linkBuffer2.compare(linkBuffer1);
if (ret == 0) {
return EXIT_CODE_ERRNO_7;
}
ret = waitpid(childPid, &status, 0);
ASSERT_EQ(ret, childPid);
if (ret != childPid) {
return EXIT_CODE_ERRNO_8;
}
int exitCode = WEXITSTATUS(status);
ASSERT_EQ(exitCode, EXIT_CODE_ERRNO_3);
if (exitCode != EXIT_CODE_ERRNO_3) {
return EXIT_CODE_ERRNO_9;
}
ret = setns(fd, setFlag);
ASSERT_EQ(ret, -1);
if (ret != -1) {
return EXIT_CODE_ERRNO_10;
}
return 0;
}
void ItUtsContainer005(void)
{
int ret;
int arg = CHILD_FUNC_ARG;
auto pid = CloneWrapper(UtsContainerTest, CLONE_NEWUTS, &arg);
ASSERT_NE(pid, -1);
int status;
ret = waitpid(pid, &status, 0);
ASSERT_EQ(ret, pid);
ret = WIFEXITED(status);
ASSERT_NE(ret, 0);
int exitCode = WEXITSTATUS(status);
ASSERT_EQ(exitCode, 0);
}

View File

@@ -29,35 +29,70 @@
*/
#include "It_container_test.h"
void ItUtsContainer006(void)
static int UtsContainerTest(void *arg)
{
(void)arg;
std::string containerType = "uts";
int parentPid = getpid();
auto parentlink = ReadlinkContainer(parentPid, containerType);
int childsPid = CloneWrapper(ChildFunction, CLONE_NEWUTS, NULL);
ASSERT_NE(childsPid, -1);
if (childsPid == -1) {
return EXIT_CODE_ERRNO_1;
}
auto childlink = ReadlinkContainer(childsPid, containerType);
std::string filePath = GenContainerLinkPath(childsPid, containerType);
int fd = open(filePath.c_str(), O_RDONLY);
ASSERT_NE(fd, -1);
if (fd == -1) {
return EXIT_CODE_ERRNO_2;
}
int ret = setns(fd, CLONE_NEWUTS);
ASSERT_NE(ret, -1);
(void)close(fd);
if (ret == -1) {
return EXIT_CODE_ERRNO_3;
}
auto parentlink1 = ReadlinkContainer(parentPid, containerType);
ret = parentlink.compare(parentlink1);
ASSERT_NE(ret, 0);
if (ret == 0) {
return EXIT_CODE_ERRNO_4;
}
ret = parentlink1.compare(childlink);
ASSERT_EQ(ret, 0);
if (ret != 0) {
return EXIT_CODE_ERRNO_5;
}
int status;
ret = waitpid(childsPid, &status, 0);
ASSERT_EQ(ret, childsPid);
if (ret != childsPid) {
return EXIT_CODE_ERRNO_6;
}
int exitCode = WEXITSTATUS(status);
if (exitCode != 0) {
return EXIT_CODE_ERRNO_7;
}
return 0;
}
void ItUtsContainer006(void)
{
int ret;
int arg = CHILD_FUNC_ARG;
auto pid = CloneWrapper(UtsContainerTest, CLONE_NEWUTS, &arg);
ASSERT_NE(pid, -1);
int status;
ret = waitpid(pid, &status, 0);
ASSERT_EQ(ret, pid);
ret = WIFEXITED(status);
ASSERT_NE(ret, 0);
int exitCode = WEXITSTATUS(status);
ASSERT_EQ(exitCode, 0);

View File

@@ -29,12 +29,13 @@
*/
#include <cstdio>
#include "It_process_fs_test.h"
#include "It_container_test.h"
static int const configLen = 16;
static int const invalidNum = 2;
static const int CHILD_FUNC_ARG = 0x2088;
const int STACK_SIZE = (1024 * 1024);
static const int MAX_CONTAINER = 10;
static const int g_buffSize = 512;
static const int g_arryLen = 4;
static const int g_readLen = 254;
static int childFunc(void *arg)
{
@@ -44,31 +45,56 @@ static int childFunc(void *arg)
return 0;
}
void ItProcessFs016(void)
void ItUtsContainer007(void)
{
std::string path = "/proc/sys/user/max_uts_container";
int fd = open(path.c_str(), O_WRONLY);
ASSERT_NE(fd, -1);
char *array[g_arryLen] = { nullptr };
char buf[g_buffSize] = { 0 };
char buf[configLen];
(void)sprintf(buf, "%d", invalidNum);
size_t ret = write(fd, buf, (strlen(buf) + 1));
int ret = ReadFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
int arg = CHILD_FUNC_ARG;
GetLine(buf, g_arryLen, g_readLen, array);
int value = atoi(array[1] + strlen("limit: "));
ASSERT_EQ(value, MAX_CONTAINER);
int usedCount = atoi(array[2] + strlen("count: "));
(void)memset_s(buf, configLen, 0, configLen);
ret = sprintf_s(buf, configLen, "%d", usedCount + 1);
ASSERT_GT(ret, 0);
ret = WriteFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
char *stack = (char *)mmap(nullptr, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
-1, 0);
ASSERT_NE(stack, nullptr);
char *stackTop = stack + STACK_SIZE;
auto pid = clone(childFunc, stackTop, CLONE_NEWUTS, &arg);
ASSERT_NE(pid, -1);
pid = clone(childFunc, stackTop, CLONE_NEWUTS, &arg);
ASSERT_NE(pid, -1);
auto pid1 = clone(childFunc, stackTop, CLONE_NEWUTS, NULL);
ASSERT_NE(pid1, -1);
pid = clone(childFunc, stackTop, CLONE_NEWUTS, &arg);
ASSERT_EQ(pid, -1);
auto pid2 = clone(childFunc, stackTop, CLONE_NEWUTS, NULL);
ASSERT_EQ(pid2, -1);
(void)close(fd);
ret = waitpid(pid1, NULL, 0);
ASSERT_EQ(ret, pid1);
(void)memset_s(buf, configLen, 0, configLen);
ret = sprintf_s(buf, configLen, "%d", value);
ASSERT_GT(ret, 0);
ret = WriteFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
(void)memset_s(buf, configLen, 0, configLen);
ret = ReadFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
GetLine(buf, g_arryLen, g_readLen, array);
value = atoi(array[1] + strlen("limit: "));
ASSERT_EQ(value, MAX_CONTAINER);
}

View File

@@ -0,0 +1,105 @@
/*
* 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.
*/
#include <cstdio>
#include "It_container_test.h"
static int const configLen = 16;
static const int MAX_CONTAINER = 10;
static const int g_buffSize = 512;
static const int g_arryLen = 4;
static const int g_readLen = 254;
static int childFunc(void *arg)
{
(void)arg;
int ret = unshare(CLONE_NEWUTS);
if (ret != 0) {
return EXIT_CODE_ERRNO_1;
}
return 0;
}
void ItUtsContainer008(void)
{
std::string path = "/proc/sys/user/max_uts_container";
char *array[g_arryLen] = { nullptr };
char buf[g_buffSize] = { 0 };
int status = 0;
int ret = ReadFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
GetLine(buf, g_arryLen, g_readLen, array);
int value = atoi(array[1] + strlen("limit: "));
ASSERT_EQ(value, MAX_CONTAINER);
int usedCount = atoi(array[2] + strlen("count: "));
(void)memset_s(buf, configLen, 0, configLen);
ret = sprintf_s(buf, configLen, "%d", usedCount + 1);
ASSERT_GT(ret, 0);
ret = WriteFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
char *stack = (char *)mmap(nullptr, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
-1, 0);
ASSERT_NE(stack, nullptr);
char *stackTop = stack + STACK_SIZE;
auto pid1 = clone(childFunc, stackTop, CLONE_NEWUTS, NULL);
ASSERT_NE(pid1, -1);
ret = waitpid(pid1, &status, 0);
ASSERT_EQ(ret, pid1);
ret = WIFEXITED(status);
ASSERT_NE(ret, 0);
ret = WEXITSTATUS(status);
ASSERT_EQ(ret, EXIT_CODE_ERRNO_1);
(void)memset_s(buf, configLen, 0, configLen);
ret = sprintf_s(buf, configLen, "%d", value);
ASSERT_GT(ret, 0);
ret = WriteFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
(void)memset_s(buf, configLen, 0, configLen);
ret = ReadFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
GetLine(buf, g_arryLen, g_readLen, array);
value = atoi(array[1] + strlen("limit: "));
ASSERT_EQ(value, MAX_CONTAINER);
}

View File

@@ -51,7 +51,6 @@ process_fs_sources_smoke = [
"$TEST_UNITTEST_DIR/process/fs/smoke/It_process_fs_003.cpp",
"$TEST_UNITTEST_DIR/process/fs/smoke/It_process_fs_004.cpp",
"$TEST_UNITTEST_DIR/process/fs/smoke/It_process_fs_005.cpp",
"$TEST_UNITTEST_DIR/process/fs/smoke/It_process_fs_006.cpp",
"$TEST_UNITTEST_DIR/process/fs/smoke/It_process_fs_007.cpp",
"$TEST_UNITTEST_DIR/process/fs/smoke/It_process_fs_008.cpp",
"$TEST_UNITTEST_DIR/process/fs/smoke/It_process_fs_009.cpp",
@@ -61,11 +60,6 @@ process_fs_sources_smoke = [
"$TEST_UNITTEST_DIR/process/fs/smoke/It_process_fs_013.cpp",
"$TEST_UNITTEST_DIR/process/fs/smoke/It_process_fs_014.cpp",
"$TEST_UNITTEST_DIR/process/fs/smoke/It_process_fs_015.cpp",
"$TEST_UNITTEST_DIR/process/fs/smoke/It_process_fs_016.cpp",
"$TEST_UNITTEST_DIR/process/fs/smoke/It_process_fs_017.cpp",
"$TEST_UNITTEST_DIR/process/fs/smoke/It_process_fs_018.cpp",
"$TEST_UNITTEST_DIR/process/fs/smoke/It_process_fs_019.cpp",
"$TEST_UNITTEST_DIR/process/fs/smoke/It_process_fs_020.cpp",
"$TEST_UNITTEST_DIR/process/fs/smoke/It_process_fs_021.cpp",
"$TEST_UNITTEST_DIR/process/fs/smoke/It_process_fs_022.cpp",
]

View File

@@ -41,12 +41,9 @@ void ItProcessFs002(void)
ASSERT_NE(fp, nullptr);
int ret = fread(szStatBuf, 1, LEN_BUFF, fp);
PrintTest("cat /proc/meminfo\n");
PrintTest("%s\n", szStatBuf);
ASSERT_EQ(ret, strlen(szStatBuf));
(void)fclose(fp);
ASSERT_NE(ret, -1);
char *res = strstr(szStatBuf, "UsedSize");
ASSERT_NE(res, nullptr);
(void)fclose(fp);
}

View File

@@ -1,124 +0,0 @@
/*
* 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.
*/
#include <cstdio>
#include "It_process_fs_test.h"
static int const maxContainerNum = 5;
static int const configLen = 16;
static int nInitArry[maxContainerNum] = {61, 54, 49, 44, 41};
static std::string arryEntries[maxContainerNum] = {
"max_mnt_container",
"max_pid_container",
"max_user_container",
"max_net_container",
"max_uts_container"
};
static void WriteContainer(const char *filepath, int value)
{
PrintTest("writeproc %d >> %s\n", value, filepath);
int fd = open(filepath, O_WRONLY);
ASSERT_NE(fd, -1);
char buf[configLen];
size_t twd = sprintf_s(buf, configLen, "%d", value);
ASSERT_GT(twd, 0);
twd = write(fd, buf, (strlen(buf)+1));
ASSERT_EQ(twd, -1);
(void)close(fd);
}
static void ReadContainer(std::string strFile, int value)
{
char szStatBuf[configLen];
FILE *fp = fopen(strFile.c_str(), "rb");
ASSERT_NE(fp, nullptr);
int ret;
(void)memset_s(szStatBuf, configLen, 0, configLen);
ret = fread(szStatBuf, 1, configLen, fp);
ASSERT_NE(ret, 0);
PrintTest("cat %s\n", strFile.c_str());
PrintTest("%s\n", szStatBuf);
ret = atoi(szStatBuf);
ASSERT_EQ(ret, value);
(void)fclose(fp);
}
static void ErrWriteContainer0(const char *filepath)
{
int fd = open(filepath, O_WRONLY);
ASSERT_NE(fd, -1);
char buf[configLen];
int invalidNum = 0;
size_t twd1 = sprintf_s(buf, configLen, "%d", invalidNum);
ASSERT_GT(twd1, 0);
PrintTest("writeproc %d >> %s\n", invalidNum, filepath);
twd1 = write(fd, buf, (strlen(buf)+1));
(void)close(fd);
ASSERT_EQ(twd1, -1);
}
static void ErrWriteContainer65(const char *filepath)
{
int fd = open(filepath, O_WRONLY);
ASSERT_NE(fd, -1);
char buf[configLen];
int invalidNum = 65;
size_t twd2 = sprintf_s(buf, configLen, "%d", invalidNum);
ASSERT_GT(twd2, 0);
PrintTest("writeproc %d >> %s\n", invalidNum, filepath);
twd2 = write(fd, buf, (strlen(buf)+1));
(void)close(fd);
ASSERT_EQ(twd2, -1);
}
void ItProcessFs006(void)
{
const int CONFIG_FILE_LEN = 1024;
char szFile[CONFIG_FILE_LEN] = {0};
for (int i = 0; i < maxContainerNum; i++) {
size_t count = sprintf_s(szFile, CONFIG_FILE_LEN, "/proc/sys/user/%s", arryEntries[i].c_str());
ASSERT_GT(count, 0);
WriteContainer(szFile, nInitArry[i]);
ReadContainer(szFile, nInitArry[i]);
}
for (int i = 0; i < maxContainerNum; i++) {
size_t count = sprintf_s(szFile, CONFIG_FILE_LEN, "/proc/sys/user/%s", arryEntries[i].c_str());
ASSERT_GT(count, 0);
ErrWriteContainer0(szFile);
ErrWriteContainer65(szFile);
}
}