1 /*
2 * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific prior written
16 * permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include <cstdio>
32 #include "It_container_test.h"
33
34 static int const configLen = 16;
35 static const int MAX_CONTAINER = 10;
36 static const int g_buffSize = 512;
37 static const int g_arryLen = 4;
38 static const int g_readLen = 254;
39
childFunc(void * arg)40 static int childFunc(void *arg)
41 {
42 (void)arg;
43
44 int ret = unshare(CLONE_NEWNS);
45 if (ret != 0) {
46 return EXIT_CODE_ERRNO_1;
47 }
48 return 0;
49 }
50
ItMntContainer010(void)51 void ItMntContainer010(void)
52 {
53 std::string path = "/proc/sys/user/max_mnt_container";
54 char *array[g_arryLen] = { nullptr };
55 char buf[g_buffSize] = { 0 };
56 int status = 0;
57
58 int ret = ReadFile(path.c_str(), buf);
59 ASSERT_NE(ret, -1);
60
61 GetLine(buf, g_arryLen, g_readLen, array);
62
63 int value = atoi(array[1] + strlen("limit: "));
64 ASSERT_EQ(value, MAX_CONTAINER);
65
66 int usedCount = atoi(array[2] + strlen("count: "));
67
68 (void)memset_s(buf, configLen, 0, configLen);
69 ret = sprintf_s(buf, configLen, "%d", usedCount + 1);
70 ASSERT_GT(ret, 0);
71
72 ret = WriteFile(path.c_str(), buf);
73 ASSERT_NE(ret, -1);
74
75 char *stack = (char *)mmap(nullptr, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
76 -1, 0);
77 ASSERT_NE(stack, nullptr);
78 char *stackTop = stack + STACK_SIZE;
79
80 auto pid1 = clone(childFunc, stackTop, CLONE_NEWNS, NULL);
81 ASSERT_NE(pid1, -1);
82
83 ret = waitpid(pid1, &status, 0);
84 ASSERT_EQ(ret, pid1);
85 ret = WIFEXITED(status);
86 ASSERT_NE(ret, 0);
87 ret = WEXITSTATUS(status);
88 ASSERT_EQ(ret, EXIT_CODE_ERRNO_1);
89
90 (void)memset_s(buf, configLen, 0, configLen);
91 ret = sprintf_s(buf, configLen, "%d", value);
92 ASSERT_GT(ret, 0);
93
94 ret = WriteFile(path.c_str(), buf);
95 ASSERT_NE(ret, -1);
96
97 (void)memset_s(buf, configLen, 0, configLen);
98 ret = ReadFile(path.c_str(), buf);
99 ASSERT_NE(ret, -1);
100
101 GetLine(buf, g_arryLen, g_readLen, array);
102
103 value = atoi(array[1] + strlen("limit: "));
104 ASSERT_EQ(value, MAX_CONTAINER);
105 }
106