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 <cstdio>
18 #include <cstdlib>
19 #include <string>
20 #include <vector>
21 #include <unistd.h>
22 #include <gtest/gtest.h>
23 #include <sys/resource.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26
27
28 using namespace testing::ext;
29 using namespace std;
30
31 class GetrlimitApiTest : public testing::Test {
32 public:
33 static void SetUpTestCase();
34 static void TearDownTestCase();
35 void SetUp();
36 void TearDown();
37 private:
38 };
SetUp()39 void GetrlimitApiTest::SetUp()
40 {
41 }
TearDown()42 void GetrlimitApiTest::TearDown()
43 {
44 }
SetUpTestCase()45 void GetrlimitApiTest::SetUpTestCase()
46 {
47 }
TearDownTestCase()48 void GetrlimitApiTest::TearDownTestCase()
49 {
50 }
51
52 /*
53 * @tc.number : SUB_KERNEL_SYSCALL_GETRLIMIT_0100
54 * @tc.name : GetrlimitGetResourceLimitsSuccess_0001
55 * @tc.desc : Getrlimit get resource limits success.
56 * @tc.size : MediumTest
57 * @tc.type : Function
58 * @tc.level : Level 1
59 */
HWTEST_F(GetrlimitApiTest, GetrlimitGetResourceLimitsSuccess_0001, Function | MediumTest | Level1)60 HWTEST_F(GetrlimitApiTest, GetrlimitGetResourceLimitsSuccess_0001, Function | MediumTest | Level1)
61 {
62 int ret;
63 struct rlimit limit;
64 ret = getrlimit(RLIMIT_NPROC, &limit);
65 EXPECT_EQ(ret, 0);
66 ret = getrlimit(RLIMIT_AS, &limit);
67 EXPECT_EQ(ret, 0);
68 ret = getrlimit(RLIMIT_CORE, &limit);
69 EXPECT_EQ(ret, 0);
70 ret = getrlimit(RLIMIT_DATA, &limit);
71 EXPECT_EQ(ret, 0);
72 ret = getrlimit(RLIMIT_FSIZE, &limit);
73 EXPECT_EQ(ret, 0);
74 ret = getrlimit(RLIMIT_LOCKS, &limit);
75 EXPECT_EQ(ret, 0);
76 ret = getrlimit(RLIMIT_MEMLOCK, &limit);
77 EXPECT_EQ(ret, 0);
78 ret = getrlimit(RLIMIT_MSGQUEUE, &limit);
79 EXPECT_EQ(ret, 0);
80 ret = getrlimit(RLIMIT_NOFILE, &limit);
81 EXPECT_EQ(ret, 0);
82 ret = getrlimit(RLIMIT_RTPRIO, &limit);
83 EXPECT_EQ(ret, 0);
84 ret = getrlimit(RLIMIT_RTTIME, &limit);
85 EXPECT_EQ(ret, 0);
86 ret = getrlimit(RLIMIT_SIGPENDING, &limit);
87 EXPECT_EQ(ret, 0);
88 ret = getrlimit(RLIMIT_STACK, &limit);
89 EXPECT_EQ(ret, 0);
90 ret = getrlimit(RLIMIT_NICE, &limit);
91 EXPECT_EQ(ret, 0);
92 }
93
94 /*
95 * @tc.number : SUB_KERNEL_SYSCALL_GETRLIMIT_0200
96 * @tc.name : GetrlimitGetResourceLimitsFail_0002
97 * @tc.desc : Getrlimit get resource limits fail.
98 * @tc.size : MediumTest
99 * @tc.type : Function
100 * @tc.level : Level 2
101 */
HWTEST_F(GetrlimitApiTest, GetrlimitGetResourceLimitsFail_0002, Function | MediumTest | Level2)102 HWTEST_F(GetrlimitApiTest, GetrlimitGetResourceLimitsFail_0002, Function | MediumTest | Level2)
103 {
104 struct rlimit limit;
105 int ret = getrlimit(-1, &limit);
106 EXPECT_EQ(ret, -1);
107 EXPECT_EQ(errno, EINVAL);
108 }
109