1 | //===-- Unittests for getrlimit and setrlimit -----------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "src/__support/CPP/string_view.h" |
10 | #include "src/fcntl/open.h" |
11 | #include "src/sys/resource/getrlimit.h" |
12 | #include "src/sys/resource/setrlimit.h" |
13 | #include "src/unistd/close.h" |
14 | #include "src/unistd/unlink.h" |
15 | #include "test/UnitTest/ErrnoCheckingTest.h" |
16 | #include "test/UnitTest/ErrnoSetterMatcher.h" |
17 | #include "test/UnitTest/Test.h" |
18 | |
19 | #include <sys/resource.h> |
20 | #include <sys/stat.h> |
21 | |
22 | using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher; |
23 | using LlvmLibcResourceLimitsTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest; |
24 | |
25 | TEST_F(LlvmLibcResourceLimitsTest, SetNoFileLimit) { |
26 | // The test strategy is to first create initialize two file descriptors |
27 | // successfully. Next, close the files and set the file descriptor limit |
28 | // to 4. This will allow us to open one of those file but not the other. |
29 | |
30 | constexpr const char *TEST_FILE1 = "testdata/resource_limits1.test" ; |
31 | constexpr const char *TEST_FILE2 = "testdata/resource_limits2.test" ; |
32 | |
33 | int fd1 = LIBC_NAMESPACE::open(TEST_FILE1, O_CREAT | O_WRONLY, S_IRWXU); |
34 | ASSERT_GT(fd1, 0); |
35 | ASSERT_ERRNO_SUCCESS(); |
36 | int fd2 = LIBC_NAMESPACE::open(TEST_FILE2, O_CREAT | O_WRONLY, S_IRWXU); |
37 | ASSERT_GT(fd2, 0); |
38 | ASSERT_ERRNO_SUCCESS(); |
39 | |
40 | ASSERT_THAT(LIBC_NAMESPACE::close(fd1), Succeeds(0)); |
41 | ASSERT_THAT(LIBC_NAMESPACE::close(fd2), Succeeds(0)); |
42 | |
43 | struct rlimit limits { |
44 | .rlim_cur: 4, .rlim_max: 4 |
45 | }; |
46 | ASSERT_THAT(LIBC_NAMESPACE::setrlimit(RLIMIT_NOFILE, &limits), Succeeds(0)); |
47 | |
48 | // One can now only open one of the files successfully. |
49 | fd1 = LIBC_NAMESPACE::open(TEST_FILE1, O_RDONLY); |
50 | ASSERT_GT(fd1, 0); |
51 | ASSERT_ERRNO_SUCCESS(); |
52 | fd2 = LIBC_NAMESPACE::open(TEST_FILE2, O_RDONLY); |
53 | ASSERT_LT(fd2, 0); |
54 | ASSERT_ERRNO_FAILURE(); |
55 | |
56 | ASSERT_THAT(LIBC_NAMESPACE::close(fd1), Succeeds(0)); |
57 | |
58 | fd2 = LIBC_NAMESPACE::open(TEST_FILE2, O_RDONLY); |
59 | ASSERT_GT(fd2, 0); |
60 | ASSERT_ERRNO_SUCCESS(); |
61 | fd1 = LIBC_NAMESPACE::open(TEST_FILE1, O_RDONLY); |
62 | ASSERT_LT(fd1, 0); |
63 | ASSERT_ERRNO_FAILURE(); |
64 | |
65 | ASSERT_THAT(LIBC_NAMESPACE::close(fd2), Succeeds(0)); |
66 | |
67 | ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE1), Succeeds(0)); |
68 | ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE2), Succeeds(0)); |
69 | |
70 | struct rlimit current_limits; |
71 | ASSERT_THAT(LIBC_NAMESPACE::getrlimit(RLIMIT_NOFILE, ¤t_limits), |
72 | Succeeds(0)); |
73 | ASSERT_EQ(current_limits.rlim_cur, rlim_t(4)); |
74 | ASSERT_EQ(current_limits.rlim_max, rlim_t(4)); |
75 | } |
76 | |