• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 = 1153;
46 
47     testStr1 = (char *)malloc(testSize);
48     EXPECT_STRNE(testStr1, NULL);
49 
50     (void)memset_s(testStr1, testSize, 1, testSize);
51     free(testStr1);
52     testStr1 = nullptr;
53     return 0;
54 }
ItProcessPlimitsMemory002(void)55 void ItProcessPlimitsMemory002(void)
56 {
57     mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
58     int ret, pid, status;
59     std::string path = "/proc/plimits/test";
60     std::string limitTestPath = "/proc/plimits/test/memory.limit";
61     std::string usageTestPath = "/proc/plimits/test/memory.stat";
62     char buf[512];
63     const char *buffer = "10485760";
64     int twoM = 2 * 1024 * 1024;
65 
66     char *stack = (char *)mmap(NULL, twoM, PROT_READ | PROT_WRITE,
67                                MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
68     EXPECT_STRNE(stack, NULL);
69     ret = access(path.c_str(), 0);
70     EXPECT_EQ(ret, -1);
71     ret = mkdir(path.c_str(), S_IFDIR | mode);
72     EXPECT_EQ(ret, 0);
73     ret = limitWrite(limitTestPath.c_str(), buffer);
74     EXPECT_NE(ret, -1);
75     pid = clone(CloneOne, stack, 0, NULL);
76     ret = waitpid(pid, &status, 0);
77     ASSERT_EQ(ret, pid);
78     ret = WIFEXITED(status);
79     ASSERT_NE(ret, 0);
80     int exitCode = WEXITSTATUS(status);
81     ASSERT_EQ(exitCode, 0);
82 
83     (void)memset_s(buf, sizeof(buf), 0, sizeof(buf));
84     ret = ReadFile(usageTestPath.c_str(), buf);
85     EXPECT_NE(ret, -1);
86     printf("%s: %s\n", usageTestPath.c_str(), buf);
87 
88     ret = rmdir(path.c_str());
89     EXPECT_EQ(ret, 0);
90 }
91