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 GetrusageApiTest : 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 GetrusageApiTest::SetUp()
40 {
41 }
TearDown()42 void GetrusageApiTest::TearDown()
43 {
44 }
SetUpTestCase()45 void GetrusageApiTest::SetUpTestCase()
46 {
47 }
TearDownTestCase()48 void GetrusageApiTest::TearDownTestCase()
49 {
50 }
51
52 /*
53 * @tc.number : SUB_KERNEL_SYSCALL_GETRUSAGE_0100
54 * @tc.name : GetrusageSuccess_0001
55 * @tc.desc : Getrusage flags and get usage success.
56 * @tc.size : MediumTest
57 * @tc.type : Function
58 * @tc.level : Level 1
59 */
60 HWTEST_F(GetrusageApiTest, GetrusageSuccess_0001, Function | MediumTest | Level1)
61 {
62 struct rusage usage;
63 int ret = getrusage(RUSAGE_SELF, &usage);
64 EXPECT_EQ(ret, 0);
65 ret = getrusage(RUSAGE_CHILDREN, &usage);
66 EXPECT_EQ(ret, 0);
67 ret = getrusage(RUSAGE_THREAD, &usage);
68 EXPECT_EQ(ret, 0);
69 EXPECT_GE(usage.ru_utime.tv_sec, 0);
70 EXPECT_GE(usage.ru_maxrss, 0);
71 EXPECT_GE(usage.ru_ixrss, 0);
72 EXPECT_GE(usage.ru_minflt, 0);
73 EXPECT_GE(usage.ru_majflt, 0);
74 EXPECT_GE(usage.ru_nvcsw, 0);
75 EXPECT_GE(usage.ru_nivcsw, 0);
76 }
77
78 /*
79 * @tc.number : SUB_KERNEL_SYSCALL_GETRUSAGE_0200
80 * @tc.name : GetrusageNullUsageStructFail_0002
81 * @tc.desc : Getrusage failed and return -1 errno EFAULT.
82 * @tc.size : MediumTest
83 * @tc.type : Function
84 * @tc.level : Level 2
85 */
86 HWTEST_F(GetrusageApiTest, GetrusageNullUsageStructFail_0002, Function | MediumTest | Level2)
87 {
88 int ret = getrusage(RUSAGE_SELF, nullptr);
89 EXPECT_EQ(ret, -1);
90 EXPECT_EQ(errno, EFAULT);
91 }