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 <fcntl.h>
21#include <unistd.h>
22#include <vector>
23#include <arpa/inet.h>
24#include <gtest/gtest.h>
25#include <netinet/in.h>
26#include <sys/stat.h>
27#include <sys/socket.h>
28#include <sys/types.h>
29#include "securec.h"
30
31using namespace testing::ext;
32
33static const int TEST_PRIO = 5;
34
35class HatsPriorityTest : public testing::Test {
36public:
37static void SetUpTestCase();
38static void TearDownTestCase();
39void SetUp();
40void TearDown();
41private:
42};
43void HatsPriorityTest::SetUp()
44{
45}
46void HatsPriorityTest::TearDown()
47{
48}
49void HatsPriorityTest::SetUpTestCase()
50{
51}
52void HatsPriorityTest::TearDownTestCase()
53{
54}
55
56/*
57 * @tc.number : SUB_KERNEL_SYSCALL_PRIORITY_0100
58 * @tc.name   : PrioritySetAndGetTestSuccess_0001
59 * @tc.desc   : priority set and get test success.
60 * @tc.size   : MediumTest
61 * @tc.type   : Function
62 * @tc.level  : Level 1
63 */
64HWTEST_F(HatsPriorityTest, PrioritySetAndGetTestSuccess_0001, Function | MediumTest | Level1)
65{
66    int pid = 0;
67    int ret = setpriority(PRIO_PROCESS, pid, TEST_PRIO);
68    EXPECT_EQ(ret, 0);
69
70    int prio = getpriority(PRIO_PROCESS, pid);
71    EXPECT_EQ(prio, TEST_PRIO);
72
73    ret = setpriority(PRIO_PGRP, pid, TEST_PRIO);
74    EXPECT_EQ(ret, 0);
75
76    prio = getpriority(PRIO_PGRP, pid);
77    EXPECT_EQ(prio, TEST_PRIO);
78
79    ret = setpriority(PRIO_USER, getuid(), TEST_PRIO);
80    EXPECT_EQ(ret, 0);
81
82    prio = getpriority(PRIO_USER, getuid());
83    EXPECT_EQ(prio, TEST_PRIO);
84}
85
86/*
87 * @tc.number : SUB_KERNEL_SYSCALL_PRIORITY_0200
88 * @tc.name   : PrioritySetAndGetUseInvalidWhichFailed_0002
89 * @tc.desc   : setpriority and getpriority use invalid which test failed.
90 * @tc.size   : MediumTest
91 * @tc.type   : Function
92 * @tc.level  : Level 2
93 */
94HWTEST_F(HatsPriorityTest, PrioritySetAndGetUseInvalidWhichFailed_0002, Function | MediumTest | Level2)
95{
96    int pid = getpid();
97    errno = 0;
98    int ret = setpriority(-1, pid, TEST_PRIO);
99    EXPECT_EQ(ret, -1);
100    EXPECT_EQ(errno, EINVAL);
101
102    errno = 0;
103    int prio = getpriority(-1, pid);
104    EXPECT_EQ(prio, -1);
105    EXPECT_EQ(errno, EINVAL);
106}
107
108/*
109 * @tc.number : SUB_KERNEL_SYSCALL_PRIORITY_0300
110 * @tc.name   : PrioritySetAndGetUseInvalidWhoFailed_0003
111 * @tc.desc   : setpriority and getpriority use invalid Who test failed.
112 * @tc.size   : MediumTest
113 * @tc.type   : Function
114 * @tc.level  : Level 2
115 */
116HWTEST_F(HatsPriorityTest, PrioritySetAndGetUseInvalidWhichFailed_0003, Function | MediumTest | Level2)
117{
118    errno = 0;
119    int ret = setpriority(PRIO_PROCESS, -1, TEST_PRIO);
120    EXPECT_EQ(ret, -1);
121    EXPECT_EQ(errno, ESRCH);
122
123    errno = 0;
124    int prio = getpriority(PRIO_PROCESS, -1);
125    EXPECT_EQ(prio, -1);
126    EXPECT_EQ(errno, ESRCH);
127}
128