1 /*
2 * Copyright (C) 2024 HiHope Open Source Organization.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <cerrno>
17 #include <csignal>
18 #include <cstdio>
19 #include <cstdlib>
20 #include <string>
21 #include <vector>
22 #include <unistd.h>
23 #include <gtest/gtest.h>
24 #include <sys/resource.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27
28
29 using namespace testing::ext;
30 using namespace std;
31
32 class SetrlimitApiTest : public testing::Test {
33 public:
34 static void SetUpTestCase();
35 static void TearDownTestCase();
36 void SetUp();
37 void TearDown();
38 private:
39 };
SetUp()40 void SetrlimitApiTest::SetUp()
41 {
42 }
TearDown()43 void SetrlimitApiTest::TearDown()
44 {
45 }
SetUpTestCase()46 void SetrlimitApiTest::SetUpTestCase()
47 {
48 }
TearDownTestCase()49 void SetrlimitApiTest::TearDownTestCase()
50 {
51 }
52
53 /*
54 * @tc.number : SUB_KERNEL_SYSCALL_SETRLIMIT_0100
55 * @tc.name : SetrlimitGetResourceLimitsSuccess_0001
56 * @tc.desc : Setrlimit set resource limits success.
57 * @tc.size : MediumTest
58 * @tc.type : Function
59 * @tc.level : Level 1
60 */
61 HWTEST_F(SetrlimitApiTest, SetrlimitGetResourceLimitsSuccess_0001, Function | MediumTest | Level1)
62 {
63 int ret;
64 struct rlimit limit;
65 ret = setrlimit(RLIMIT_CORE, &limit);
66 EXPECT_EQ(ret, 0);
67 ret = setrlimit(RLIMIT_LOCKS, &limit);
68 EXPECT_EQ(ret, 0);
69 ret = setrlimit(RLIMIT_MEMLOCK, &limit);
70 EXPECT_EQ(ret, 0);
71 ret = setrlimit(RLIMIT_MSGQUEUE, &limit);
72 EXPECT_EQ(ret, 0);
73 ret = setrlimit(RLIMIT_NPROC, &limit);
74 EXPECT_EQ(ret, 0);
75 ret = setrlimit(RLIMIT_RTPRIO, &limit);
76 EXPECT_EQ(ret, 0);
77 ret = setrlimit(RLIMIT_RTTIME, &limit);
78 EXPECT_EQ(ret, 0);
79 ret = setrlimit(RLIMIT_SIGPENDING, &limit);
80 EXPECT_EQ(ret, 0);
81 ret = setrlimit(RLIMIT_STACK, &limit);
82 EXPECT_EQ(ret, 0);
83 ret = setrlimit(RLIMIT_NICE, &limit);
84 EXPECT_EQ(ret, 0);
85 }
86
87 /*
88 * @tc.number : SUB_KERNEL_SYSCALL_SETRLIMIT_0200
89 * @tc.name : SetrlimitGetResourceLimitsFail_0002
90 * @tc.desc : Setrlimit get resource limits fail.
91 * @tc.size : MediumTest
92 * @tc.type : Function
93 * @tc.level : Level 2
94 */
95 HWTEST_F(SetrlimitApiTest, SetrlimitGetResourceLimitsFail_0002, Function | MediumTest | Level2)
96 {
97 struct rlimit limit;
98 int ret = setrlimit(-1, &limit);
99 EXPECT_EQ(ret, -1);
100 EXPECT_EQ(errno, EINVAL);
101 }
102