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 "It_process_plimits.h"
32
33 static char *testStr1 = nullptr;
34
limitWrite(const char * filepath,const char * buf)35 static int limitWrite(const char *filepath, const char *buf)
36 {
37 int fd = open(filepath, O_WRONLY);
38 size_t ret = write(fd, buf, strlen(buf));
39 close(fd);
40 return ret;
41 }
42
CloneOne(void * arg)43 static int CloneOne(void *arg)
44 {
45 const unsigned int testSize = 11534336;
46
47 testStr1 = (char *)malloc(testSize);
48 EXPECT_STRNE(testStr1, NULL);
49
50 (void)memset_s(testStr1, testSize, 1, testSize);
51 return 0;
52 }
53
ItProcessPlimitsMemory001(void)54 void ItProcessPlimitsMemory001(void)
55 {
56 mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
57 int ret, pid, status;
58 std::string path = "/proc/plimits/test";
59 std::string limitTestPath = "/proc/plimits/test/memory.limit";
60 std::string usageTestPath = "/proc/plimits/test/memory.stat";
61 const char *buffer = "10485760";
62 char buf[512];
63 int twoM = 2 * 1024 * 1024;
64
65 char *stack = (char *)mmap(NULL, twoM, PROT_READ | PROT_WRITE,
66 MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
67 EXPECT_STRNE(stack, NULL);
68 ret = access(path.c_str(), 0);
69 EXPECT_EQ(ret, -1);
70 ret = mkdir(path.c_str(), S_IFDIR | mode);
71 EXPECT_EQ(ret, 0);
72 ret = limitWrite(limitTestPath.c_str(), buffer);
73 EXPECT_NE(ret, -1);
74 pid = clone(CloneOne, stack, 0, NULL);
75 ret = waitpid(pid, &status, 0);
76 ASSERT_EQ(ret, pid);
77 ret = WIFEXITED(status);
78 ASSERT_EQ(ret, 0);
79
80 (void)memset_s(buf, sizeof(buf), 0, sizeof(buf));
81 ret = ReadFile(usageTestPath.c_str(), buf);
82 EXPECT_NE(ret, -1);
83 printf("%s: %s\n", usageTestPath.c_str(), buf);
84
85 ret = rmdir(path.c_str());
86 EXPECT_EQ(ret, 0);
87 }
88