• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_INVALID_POLICY = 8888;
34 
35 class HatsSchedschedulerTest : 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 HatsSchedschedulerTest::SetUp()
44 {
45 }
TearDown()46 void HatsSchedschedulerTest::TearDown()
47 {
48 }
SetUpTestCase()49 void HatsSchedschedulerTest::SetUpTestCase()
50 {
51 }
TearDownTestCase()52 void HatsSchedschedulerTest::TearDownTestCase()
53 {
54 }
55 
56 /*
57  * @tc.number : SUB_KERNEL_SYSCALL_SCHEDGSCHEDULER_0100
58  * @tc.name   : SchedschedulerSetAndGetSchedulingPolicySuccess_0001
59  * @tc.desc   : sched_setscheduler and sched_getscheduler scheduling policy of current thread success.
60  * @tc.size   : MediumTest
61  * @tc.type   : Function
62  * @tc.level  : Level 1
63  */
64 HWTEST_F(HatsSchedschedulerTest, SchedschedulerSetAndGetSchedulingPolicySuccess_0001, Function | MediumTest | Level1)
65 {
66     struct sched_param param = { 0 };
67 
68     // scheduler SCHED_FIFO set and get test, success
69     param.sched_priority = sched_get_priority_max(SCHED_FIFO);
70     int ret = sched_setscheduler(0, SCHED_FIFO, &param);
71     EXPECT_EQ(ret, 0);
72 
73     int policy = sched_getscheduler(0);
74     EXPECT_EQ(policy, SCHED_FIFO);
75 
76     // scheduler SCHED_RR set and get test, success
77     param.sched_priority = sched_get_priority_max(SCHED_RR);
78     ret = sched_setscheduler(0, SCHED_RR, &param);
79     EXPECT_EQ(ret, 0);
80 
81     policy = sched_getscheduler(0);
82     EXPECT_EQ(policy, SCHED_RR);
83 
84     // scheduler SCHED_OTHER set and get test, success
85     param.sched_priority = sched_get_priority_max(SCHED_OTHER);
86     ret = sched_setscheduler(0, SCHED_OTHER, &param);
87     EXPECT_EQ(ret, 0);
88 
89     policy = sched_getscheduler(0);
90     EXPECT_EQ(policy, SCHED_OTHER);
91 }
92 
93 /*
94  * @tc.number : SUB_KERNEL_SYSCALL_SCHEDGSCHEDULER_0200
95  * @tc.name   : SchedschedulerUseInvalidPidFailed_0002
96  * @tc.desc   : sched_getscheduler and sched_setscheduler use invalid pid failed.
97  * @tc.size   : MediumTest
98  * @tc.type   : Function
99  * @tc.level  : Level 2
100  */
101 HWTEST_F(HatsSchedschedulerTest, SchedschedulerUseInvalidParamFailed_0002, Function | MediumTest | Level2)
102 {
103     struct sched_param param = { 0 };
104 
105     param.sched_priority = sched_get_priority_max(SCHED_FIFO);
106 
107     // negative pid test, errno EINVAL
108     errno = 0;
109     int ret = sched_setscheduler(-1, SCHED_FIFO, &param);
110     EXPECT_EQ(ret, -1);
111     EXPECT_EQ(errno, EINVAL);
112 
113     errno = 0;
114     int policy = sched_getscheduler(-1);
115     EXPECT_EQ(policy, -1);
116     EXPECT_EQ(errno, EINVAL);
117 }
118 
119 /*
120  * @tc.number : SUB_KERNEL_SYSCALL_SCHEDGSCHEDULER_0300
121  * @tc.name   : SchedschedulerUseInvalidPolicyFailed_0003
122  * @tc.desc   : sched_setscheduler use invalid Policy failed, errno EINVAL.
123  * @tc.size   : MediumTest
124  * @tc.type   : Function
125  * @tc.level  : Level 2
126  */
127 HWTEST_F(HatsSchedschedulerTest, SchedschedulerUseInvalidPolicyFailed_0003, Function | MediumTest | Level2)
128 {
129     struct sched_param param = { 0 };
130     param.sched_priority = sched_get_priority_max(SCHED_FIFO);
131 
132     errno = 0;
133     int ret = sched_setscheduler(0, TEST_INVALID_POLICY, &param);
134     EXPECT_EQ(ret, -1);
135     EXPECT_EQ(errno, EINVAL);
136 }
137 
138 /*
139  * @tc.number : SUB_KERNEL_SYSCALL_SCHEDGSCHEDULER_0400
140  * @tc.name   : SchedschedulerUseInvalidParamFailed_0004
141  * @tc.desc   : sched_setscheduler use nullptr param failed, errno EINVAL.
142  * @tc.size   : MediumTest
143  * @tc.type   : Function
144  * @tc.level  : Level 2
145  */
146 HWTEST_F(HatsSchedschedulerTest, SchedschedulerUseInvalidParamFailed_0004, Function | MediumTest | Level2)
147 {
148     errno = 0;
149     int ret = sched_setscheduler(0, SCHED_FIFO, nullptr);
150     EXPECT_EQ(ret, -1);
151     EXPECT_EQ(errno, EINVAL);
152 }