1 /**
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
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 <errno.h>
17 #include <sys/resource.h>
18 #include <sys/time.h>
19 #include "functionalext.h"
20 
21 #define TEST_PRIORITY_MAX 20
22 #define TEST_PRIORITY_MIN (-20)
23 #define TEST_WHO_ERROR 1000
24 
25 /**
26  * @tc.name      : getpriority_0100
27  * @tc.desc      : Get the default program scheduling priority
28  * @tc.level     : Level 0
29  */
getpriority_0100(void)30 void getpriority_0100(void)
31 {
32     errno = 0;
33     int ret = getpriority(PRIO_PROCESS, getpid());
34     EXPECT_EQ("getpriority_0100", errno, CMPFLAG);
35     EXPECT_TRUE("getpriority_0100", ret >= TEST_PRIORITY_MIN && ret <= TEST_PRIORITY_MAX);
36 
37     errno = 0;
38     ret = getpriority(PRIO_PGRP, getpgid(getpid()));
39     EXPECT_EQ("getpriority_0100", errno, CMPFLAG);
40     EXPECT_TRUE("getpriority_0100", ret >= TEST_PRIORITY_MIN && ret <= TEST_PRIORITY_MAX);
41 
42     errno = 0;
43     ret = getpriority(PRIO_USER, getuid());
44     EXPECT_EQ("getpriority_0100", errno, CMPFLAG);
45     EXPECT_TRUE("getpriority_0100", ret >= TEST_PRIORITY_MIN && ret <= TEST_PRIORITY_MAX);
46 }
47 
48 /**
49  * @tc.name      : getpriority_0200
50  * @tc.desc      : Set and get program scheduling priority
51  * @tc.level     : Level 0
52  */
getpriority_0200(void)53 void getpriority_0200(void)
54 {
55     int i, ret;
56     for (i = TEST_PRIORITY_MIN; i < TEST_PRIORITY_MAX; i++) {
57         errno = 0;
58         ret = setpriority(PRIO_PROCESS, getpid(), i);
59         EXPECT_EQ("getpriority_0200", ret, CMPFLAG);
60 
61         errno = 0;
62         ret = getpriority(PRIO_PROCESS, getpid());
63         EXPECT_EQ("getpriority_0200", ret, i);
64         EXPECT_EQ("getpriority_0200", errno, CMPFLAG);
65     }
66 
67     for (i = TEST_PRIORITY_MIN; i < TEST_PRIORITY_MAX; i++) {
68         errno = 0;
69         ret = setpriority(PRIO_PGRP, getpgid(getpid()), i);
70         EXPECT_EQ("getpriority_0200", ret, CMPFLAG);
71 
72         errno = 0;
73         ret = getpriority(PRIO_PGRP, getpgid(getpid()));
74         EXPECT_EQ("getpriority_0200", ret, i);
75         EXPECT_EQ("getpriority_0200", errno, CMPFLAG);
76     }
77 
78     for (i = TEST_PRIORITY_MIN; i < TEST_PRIORITY_MAX; i++) {
79         errno = 0;
80         ret = setpriority(PRIO_USER, getuid(), i);
81         EXPECT_EQ("getpriority_0200", ret, CMPFLAG);
82 
83         errno = 0;
84         ret = getpriority(PRIO_USER, getuid());
85         EXPECT_EQ("getpriority_0200", ret, i);
86         EXPECT_EQ("getpriority_0200", errno, CMPFLAG);
87     }
88 }
89 
90 /**
91  * @tc.name      : getpriority_0300
92  * @tc.desc      : Provide exception parameters, get program scheduling priority
93  * @tc.level     : Level 2
94  */
getpriority_0300(void)95 void getpriority_0300(void)
96 {
97     errno = 0;
98     int ret = getpriority(-1, -1);
99     EXPECT_EQ("getpriority_0300", ret, ERREXPECT);
100     EXPECT_EQ("getpriority_0300", errno, EINVAL);
101 
102     errno = 0;
103     ret = getpriority(TEST_WHO_ERROR, getpid());
104     EXPECT_EQ("getpriority_0300", ret, ERREXPECT);
105     EXPECT_EQ("getpriority_0300", errno, EINVAL);
106 }
107 
main(void)108 int main(void)
109 {
110     getpriority_0100();
111     getpriority_0200();
112     getpriority_0300();
113     return t_status;
114 }