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_NEWTIME);
45 if (ret != 0) {
46 return EXIT_CODE_ERRNO_1;
47 }
48
49 ret = unshare(CLONE_NEWTIME);
50 if (ret != 0) {
51 return EXIT_CODE_ERRNO_2;
52 }
53
54 return 0;
55 }
56
ItTimeContainer006(void)57 void ItTimeContainer006(void)
58 {
59 std::string path = "/proc/sys/user/max_time_container";
60 char *array[g_arryLen] = { nullptr };
61 char buf[g_buffSize] = { 0 };
62 int status = 0;
63
64 int ret = ReadFile(path.c_str(), buf);
65 ASSERT_NE(ret, -1);
66
67 GetLine(buf, g_arryLen, g_readLen, array);
68
69 int value = atoi(array[1] + strlen("limit: "));
70 ASSERT_EQ(value, MAX_CONTAINER);
71
72 int usedCount = atoi(array[2] + strlen("count: "));
73
74 (void)memset_s(buf, configLen, 0, configLen);
75 ret = sprintf_s(buf, configLen, "%d", usedCount + 1);
76 ASSERT_GT(ret, 0);
77
78 ret = WriteFile(path.c_str(), buf);
79 ASSERT_NE(ret, -1);
80
81 char *stack = (char *)mmap(nullptr, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
82 -1, 0);
83 ASSERT_NE(stack, nullptr);
84 char *stackTop = stack + STACK_SIZE;
85
86 auto pid1 = clone(childFunc, stackTop, CLONE_NEWTIME, NULL);
87 ASSERT_NE(pid1, -1);
88
89 ret = waitpid(pid1, &status, 0);
90 ASSERT_EQ(ret, pid1);
91 ret = WIFEXITED(status);
92 ASSERT_NE(ret, 0);
93 ret = WEXITSTATUS(status);
94 ASSERT_EQ(ret, EXIT_CODE_ERRNO_2);
95
96 (void)memset_s(buf, configLen, 0, configLen);
97 ret = sprintf_s(buf, configLen, "%d", value);
98 ASSERT_GT(ret, 0);
99
100 ret = WriteFile(path.c_str(), buf);
101 ASSERT_NE(ret, -1);
102
103 (void)memset_s(buf, configLen, 0, configLen);
104 ret = ReadFile(path.c_str(), buf);
105 ASSERT_NE(ret, -1);
106
107 GetLine(buf, g_arryLen, g_readLen, array);
108
109 value = atoi(array[1] + strlen("limit: "));
110 ASSERT_EQ(value, MAX_CONTAINER);
111 }
112