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 
31 using namespace testing::ext;
32 
33 static const int TEST_PRIORITY = 1;
34 
35 class HatsSchedparamTest : public testing::Test {
36 public:
37 static void SetUpTestCase();
38 static void TearDownTestCase();
39 void SetUp();
40 void TearDown();
41 private:
42 };
SetUp()43 void HatsSchedparamTest::SetUp()
44 {
45 }
TearDown()46 void HatsSchedparamTest::TearDown()
47 {
48 }
SetUpTestCase()49 void HatsSchedparamTest::SetUpTestCase()
50 {
51 }
TearDownTestCase()52 void HatsSchedparamTest::TearDownTestCase()
53 {
54 }
55 
56 /*
57  * @tc.number : SUB_KERNEL_SYSCALL_SCHEDPARAM_0100
58  * @tc.name   : SchedparamSetAndGetParametersSuccess_0001
59  * @tc.desc   : sched_setparam set param and sched_setparam get priority param success.
60  * @tc.size   : MediumTest
61  * @tc.type   : Function
62  * @tc.level  : Level 1
63  */
HWTEST_F(HatsSchedparamTest, SchedparamSetAndGetParametersSuccess_0001, Function | MediumTest | Level1)64 HWTEST_F(HatsSchedparamTest, SchedparamSetAndGetParametersSuccess_0001, Function | MediumTest | Level1)
65 {
66     struct sched_param param = {
67         .sched_priority = TEST_PRIORITY,
68     };
69 
70     int ret = sched_setscheduler(0, SCHED_FIFO, &param);
71     EXPECT_EQ(ret, 0);
72 
73     // test sched_setparam set priority success
74     param.sched_priority = SCHED_FIFO;
75     ret = sched_setparam(0, &param);
76     EXPECT_EQ(ret, 0);
77 
78     memset_s(&param, sizeof(struct sched_param), 0, sizeof(struct sched_param));
79 
80     // test sched_getparam get priority success
81     ret = sched_getparam(0, &param);
82     EXPECT_EQ(ret, 0);
83     EXPECT_EQ(param.sched_priority, SCHED_FIFO);
84 }
85 
86 /*
87  * @tc.number : SUB_KERNEL_SYSCALL_SCHEDPARAM_0200
88  * @tc.name   : SchedparamUseInvalidParmFailed_00002
89  * @tc.desc   : sched_setparam and sched_getparam use invalid param failed.
90  * @tc.size   : MediumTest
91  * @tc.type   : Function
92  * @tc.level  : Level 2
93  */
HWTEST_F(HatsSchedparamTest, SchedparamUseInvalidParmFailed_00002, Function | MediumTest | Level2)94 HWTEST_F(HatsSchedparamTest, SchedparamUseInvalidParmFailed_00002, Function | MediumTest | Level2)
95 {
96     struct sched_param param = {
97         .sched_priority = TEST_PRIORITY,
98     };
99 
100     // test sched_setparam use pid = -1 failed, errno EINVAL
101     errno = 0;
102     int ret = sched_getparam(-1, &param);
103     EXPECT_EQ(ret, -1);
104     EXPECT_EQ(errno, EINVAL);
105 
106     // test sched_getparam use pid = -1 pid failed, errno EINVAL
107     errno = 0;
108     ret = sched_getparam(-1, &param);
109     EXPECT_EQ(ret, -1);
110     EXPECT_EQ(errno, EINVAL);
111 
112     // test sched_setparam use param nullptr failed, errno EINVAL
113     errno = 0;
114     ret = sched_setparam(0, nullptr);
115     EXPECT_EQ(ret, -1);
116     EXPECT_EQ(errno, EINVAL);
117 
118     // test sched_getparam use param nullptr failed, errno EINVAL
119     errno = 0;
120     ret = sched_getparam(0, nullptr);
121     EXPECT_EQ(ret, -1);
122     EXPECT_EQ(errno, EINVAL);
123 }
124