• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <errno.h>
2 #include <gtest/gtest.h>
3 #include <sys/resource.h>
4 
5 using namespace testing::ext;
6 
7 class MiscGetpriorityTest : public testing::Test {
8     int priorityNum = -1, currentErrno = 0;
SetUp()9     void SetUp() override
10     {
11         priorityNum = getpriority(PRIO_USER, getuid());
12         if (priorityNum == -1) {
13             currentErrno = errno;
14         }
15         errno = 0;
16     }
TearDown()17     void TearDown() override
18     {
19         if (currentErrno == 0) {
20             setpriority(PRIO_USER, getuid(), priorityNum);
21         }
22     }
23 };
24 
25 constexpr int PRIORITY = 10;
26 
27 /**
28  * @tc.name: getpriority_001
29  * @tc.desc: The testing viewpoint of this test case is to verify that the getpriority function can successfully obtain
30  *           the priority PRIO of the current PRIO_PROCESS and ensure that the return value matches the expected
31  *           priority.
32  * @tc.type: FUNC
33  */
34 HWTEST_F(MiscGetpriorityTest, getpriority_001, TestSize.Level1)
35 {
36     int result1 = setpriority(PRIO_PROCESS, getpid(), PRIORITY);
37     EXPECT_EQ(result1, 0);
38     int result2 = getpriority(PRIO_PROCESS, getpid());
39     EXPECT_EQ(result2, PRIORITY);
40 }
41 
42 /**
43  * @tc.name: getpriority_002
44  * @tc.desc: The testing viewpoint of this test case is to verify that the getpriority function can successfully obtain
45  *           the priority PRIO of the current PRIO_PGRP and ensure that the return value matches the expected
46  *           priority.
47  * @tc.type: FUNC
48  */
49 HWTEST_F(MiscGetpriorityTest, getpriority_002, TestSize.Level1)
50 {
51     int result1 = setpriority(PRIO_PGRP, getpgid(getpid()), PRIORITY);
52     EXPECT_EQ(result1, 0);
53     int result2 = getpriority(PRIO_PGRP, getpgid(getpid()));
54     EXPECT_EQ(result2, PRIORITY);
55 }
56 
57 /**
58  * @tc.name: getpriority_003
59  * @tc.desc: The testing viewpoint of this test case is to verify that the getpriority function can successfully obtain
60  *           the priority PRIO of the current PRIO_USER and ensure that the return value matches the expected
61  *           priority.
62  * @tc.type: FUNC
63  */
64 HWTEST_F(MiscGetpriorityTest, getpriority_003, TestSize.Level1)
65 {
66     int result1 = setpriority(PRIO_USER, getuid(), PRIORITY);
67     EXPECT_EQ(result1, 0);
68     int result2 = getpriority(PRIO_USER, getuid());
69     EXPECT_EQ(result2, PRIORITY);
70 }