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 class HatsSchedaffinityTest : public testing::Test {
34 public:
35 static void SetUpTestCase();
36 static void TearDownTestCase();
37 void SetUp();
38 void TearDown();
39 private:
40 };
SetUp()41 void HatsSchedaffinityTest::SetUp()
42 {
43 }
TearDown()44 void HatsSchedaffinityTest::TearDown()
45 {
46 }
SetUpTestCase()47 void HatsSchedaffinityTest::SetUpTestCase()
48 {
49 }
TearDownTestCase()50 void HatsSchedaffinityTest::TearDownTestCase()
51 {
52 }
53
54 /*
55 * @tc.number : SUB_KERNEL_SYSCALL_SCHEDAFFINITY_0100
56 * @tc.name : SchedaffinitySetAndGetCurrentThreadSuccess_0001
57 * @tc.desc : sched_setaffinity CPU 0 to current thread and sched_getaffinity success.
58 * @tc.size : MediumTest
59 * @tc.type : Function
60 * @tc.level : Level 1
61 */
62 HWTEST_F(HatsSchedaffinityTest, SchedsetaffinityOfCurrentThreadSuccess_0001, Function | MediumTest | Level1)
63 {
64 int ret;
65 pid_t pid = 0;
66 cpu_set_t set;
67 CPU_ZERO(&set);
68 CPU_SET(0, &set);
69 ret = sched_setaffinity(pid, sizeof(cpu_set_t), &set);
70 EXPECT_EQ(ret, 0);
71
72 ret = sched_getaffinity(pid, sizeof(cpu_set_t), &set);
73 EXPECT_EQ(ret, 0);
74 }
75
76 /*
77 * @tc.number : SUB_KERNEL_SYSCALL_SCHEDAFFINITY_0200
78 * @tc.name : SchedsetaffinityInvalidCPUFail_0002
79 * @tc.desc : sched_setaffinity use invalid CPU to current thread fail.
80 * @tc.size : MediumTest
81 * @tc.type : Function
82 * @tc.level : Level 2
83 */
84 HWTEST_F(HatsSchedaffinityTest, SchedsetaffinityInvalidCPUFail_0002, Function | MediumTest | Level2)
85 {
86 int ret;
87 pid_t pid = 0;
88 cpu_set_t set;
89 CPU_ZERO(&set);
90 CPU_SET(-1, &set);
91 errno = 0;
92 ret = sched_setaffinity(pid, sizeof(cpu_set_t), &set);
93 EXPECT_EQ(ret, -1);
94 EXPECT_EQ(errno, EINVAL);
95 }
96
97 /*
98 * @tc.number : SUB_KERNEL_SYSCALL_SCHEDAFFINITY_0300
99 * @tc.name : SchedaffinitySetAndGetUseInvalidPIDFail_0003
100 * @tc.desc : sched_setaffinity and sched_getaffinity use invalid PID fail.
101 * @tc.size : MediumTest
102 * @tc.type : Function
103 * @tc.level : Level 2
104 */
105 HWTEST_F(HatsSchedaffinityTest, SchedaffinitySetAndGetUseInvalidPIDFail_0003, Function | MediumTest | Level2)
106 {
107 int ret;
108 cpu_set_t set;
109 CPU_ZERO(&set);
110 CPU_SET(0, &set);
111
112 // pid = -1 test
113 pid_t pid = -1;
114 errno = 0;
115 ret = sched_setaffinity(pid, sizeof(cpu_set_t), &set);
116 EXPECT_EQ(ret, -1);
117 EXPECT_EQ(errno, ESRCH);
118
119 errno = 0;
120 ret = sched_getaffinity(pid, sizeof(cpu_set_t), &set);
121 EXPECT_EQ(ret, -1);
122 EXPECT_EQ(errno, ESRCH);
123 }
124
125 /*
126 * @tc.number : SUB_KERNEL_SYSCALL_SCHEDAFFINITY_0400
127 * @tc.name : SchedaffinitySetAndGetUseInvalidCPUSetFail_0004
128 * @tc.desc : sched_setaffinity and sched_getaffinity use invalid cpu_set fail.
129 * @tc.size : MediumTest
130 * @tc.type : Function
131 * @tc.level : Level 2
132 */
133 HWTEST_F(HatsSchedaffinityTest, SchedaffinitySetAndGetUseInvalidCPUSetFail_0004, Function | MediumTest | Level2)
134 {
135 int ret;
136 cpu_set_t set;
137 CPU_ZERO(&set);
138 CPU_SET(0, &set);
139
140 // invalid size test
141 errno = 0;
142 ret = sched_setaffinity(0, 0, &set);
143 EXPECT_EQ(ret, -1);
144 EXPECT_EQ(errno, EINVAL);
145
146 errno = 0;
147 ret = sched_getaffinity(0, 0, &set);
148 EXPECT_EQ(ret, -1);
149 EXPECT_EQ(errno, EINVAL);
150
151 // invalid cpu_set_t test
152 errno = 0;
153 ret = sched_setaffinity(0, sizeof(cpu_set_t), nullptr);
154 EXPECT_EQ(ret, -1);
155 EXPECT_EQ(errno, EFAULT);
156
157 errno = 0;
158 ret = sched_getaffinity(0, sizeof(cpu_set_t), nullptr);
159 EXPECT_EQ(ret, -1);
160 EXPECT_EQ(errno, EFAULT);
161 }