1f6603c60Sopenharmony_ci/* 2f6603c60Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 3f6603c60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4f6603c60Sopenharmony_ci * you may not use this file except in compliance with the License. 5f6603c60Sopenharmony_ci * You may obtain a copy of the License at 6f6603c60Sopenharmony_ci * 7f6603c60Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8f6603c60Sopenharmony_ci * 9f6603c60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10f6603c60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11f6603c60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12f6603c60Sopenharmony_ci * See the License for the specific language governing permissions and 13f6603c60Sopenharmony_ci * limitations under the License. 14f6603c60Sopenharmony_ci */ 15f6603c60Sopenharmony_ci 16f6603c60Sopenharmony_ci 17f6603c60Sopenharmony_ci#include <unistd.h> 18f6603c60Sopenharmony_ci#include <sys/resource.h> 19f6603c60Sopenharmony_ci#include <sys/shm.h> 20f6603c60Sopenharmony_ci#include <gtest/gtest.h> 21f6603c60Sopenharmony_ci#include "log.h" 22f6603c60Sopenharmony_ci#include "utils.h" 23f6603c60Sopenharmony_ci#include "KernelConstants.h" 24f6603c60Sopenharmony_ci#include "mt_utils.h" 25f6603c60Sopenharmony_ci 26f6603c60Sopenharmony_ciusing namespace testing::ext; 27f6603c60Sopenharmony_ci 28f6603c60Sopenharmony_ciclass ProcessSchedApiTest : public testing::Test { 29f6603c60Sopenharmony_ci}; 30f6603c60Sopenharmony_ci 31f6603c60Sopenharmony_ci/** 32f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_SCHED_API_GETPRIORITY_0100 33f6603c60Sopenharmony_ci * @tc.name getpriority api test. 34f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 35f6603c60Sopenharmony_ci */ 36f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testGetPriority, Function | MediumTest | Level1) 37f6603c60Sopenharmony_ci{ 38f6603c60Sopenharmony_ci int priority = getpriority(PRIO_PROCESS, INIT_PROCESS_ID); 39f6603c60Sopenharmony_ci EXPECT_EQ(priority, DEFAULT_INIT_PROCESS_PRIORITY) << "check 'init' priority failed."; 40f6603c60Sopenharmony_ci priority = getpriority(PRIO_PROCESS, KERNEL_PROCESS_ID); 41f6603c60Sopenharmony_ci EXPECT_EQ(priority, DEFAULT_KERNEL_PROCESS_PRIORITY) << "check 'KProcess' priority failed."; 42f6603c60Sopenharmony_ci} 43f6603c60Sopenharmony_ci 44f6603c60Sopenharmony_ci/** 45f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_SCHED_API_GETPRIORITY_0200 46f6603c60Sopenharmony_ci * @tc.name getpriority error test with unsupport parameter 1. 47f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 48f6603c60Sopenharmony_ci */ 49f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testGetPriorityError0200, Function | MediumTest | Level3) 50f6603c60Sopenharmony_ci{ 51f6603c60Sopenharmony_ci LOG("invalid 'which' test:"); 52f6603c60Sopenharmony_ci errno = 0; 53f6603c60Sopenharmony_ci int priority = getpriority(PRIO_PGRP, 0); 54f6603c60Sopenharmony_ci EXPECT_EQ(priority, -1); 55f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL); 56f6603c60Sopenharmony_ci errno = 0; 57f6603c60Sopenharmony_ci priority = getpriority(PRIO_USER, 0); 58f6603c60Sopenharmony_ci EXPECT_EQ(priority, -1); 59f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL); 60f6603c60Sopenharmony_ci} 61f6603c60Sopenharmony_ci 62f6603c60Sopenharmony_ci/** 63f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_SCHED_API_GETPRIORITY_0300 64f6603c60Sopenharmony_ci * @tc.name getpriority error test with invalid parameter 1. 65f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 66f6603c60Sopenharmony_ci */ 67f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testGetPriorityError0300, Function | MediumTest | Level3) 68f6603c60Sopenharmony_ci{ 69f6603c60Sopenharmony_ci int priority; 70f6603c60Sopenharmony_ci 71f6603c60Sopenharmony_ci LOG("invalid 'which' test:"); 72f6603c60Sopenharmony_ci errno = 0; 73f6603c60Sopenharmony_ci priority = getpriority(PRIO_USER + GetRandom(1000), 0); 74f6603c60Sopenharmony_ci EXPECT_EQ(priority, -1); 75f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL); 76f6603c60Sopenharmony_ci errno = 0; 77f6603c60Sopenharmony_ci priority = getpriority(-GetRandom(1000), 0); 78f6603c60Sopenharmony_ci EXPECT_EQ(priority, -1); 79f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL); 80f6603c60Sopenharmony_ci} 81f6603c60Sopenharmony_ci 82f6603c60Sopenharmony_ci/** 83f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_SCHED_API_GETPRIORITY_0400 84f6603c60Sopenharmony_ci * @tc.name getpriority error test with invalid parameter 2. 85f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 86f6603c60Sopenharmony_ci */ 87f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testGetPriorityError0400, Function | MediumTest | Level3) 88f6603c60Sopenharmony_ci{ 89f6603c60Sopenharmony_ci int priority; 90f6603c60Sopenharmony_ci LOG("invalid 'who' test:"); 91f6603c60Sopenharmony_ci errno = 0; 92f6603c60Sopenharmony_ci priority = getpriority(PRIO_PROCESS, -1); 93f6603c60Sopenharmony_ci EXPECT_EQ(priority, -1); 94f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL); 95f6603c60Sopenharmony_ci errno = 0; 96f6603c60Sopenharmony_ci priority = getpriority(PRIO_PROCESS, MAX_PROCESS_NUMBER + 1); 97f6603c60Sopenharmony_ci EXPECT_EQ(priority, -1); 98f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL); 99f6603c60Sopenharmony_ci errno = 0; 100f6603c60Sopenharmony_ci priority = getpriority(PRIO_PROCESS, MAX_PROCESS_NUMBER + GetRandom(100000)); 101f6603c60Sopenharmony_ci EXPECT_EQ(priority, -1); 102f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL); 103f6603c60Sopenharmony_ci} 104f6603c60Sopenharmony_ci 105f6603c60Sopenharmony_ci/** 106f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_SCHED_API_GETPRIORITY_0500 107f6603c60Sopenharmony_ci * @tc.name getpriority error test with not exist parameter 2. 108f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 109f6603c60Sopenharmony_ci */ 110f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testGetPriorityError0500, Function | MediumTest | Level3) 111f6603c60Sopenharmony_ci{ 112f6603c60Sopenharmony_ci int priority; 113f6603c60Sopenharmony_ci LOG("invalid 'who' test:"); 114f6603c60Sopenharmony_ci LOG("not exist pid test:"); 115f6603c60Sopenharmony_ci pid_t nonExitPid = GetNonExistPid(); // valid but not exist pid 116f6603c60Sopenharmony_ci if (nonExitPid != -1) { 117f6603c60Sopenharmony_ci priority = getpriority(PRIO_PROCESS, nonExitPid); 118f6603c60Sopenharmony_ci EXPECT_EQ(priority, -1); 119f6603c60Sopenharmony_ci EXPECT_EQ(errno, ESRCH); 120f6603c60Sopenharmony_ci } 121f6603c60Sopenharmony_ci} 122f6603c60Sopenharmony_ci 123f6603c60Sopenharmony_civoid SetPriorityAllTest() 124f6603c60Sopenharmony_ci{ 125f6603c60Sopenharmony_ci int rt, newPrio; 126f6603c60Sopenharmony_ci struct sched_param param; 127f6603c60Sopenharmony_ci LOG("test pid '0' and cover all supported priority:"); 128f6603c60Sopenharmony_ci for (int prio = HIGHEST_USER_PROCESS_PRIORITY; prio <= LOWEST_USER_PROCESS_PRIORITY; prio++) { 129f6603c60Sopenharmony_ci rt = setpriority(PRIO_PROCESS, 0, prio); 130f6603c60Sopenharmony_ci EXPECT_EQ(rt, 0) << "setpriority failed for prio=" << prio << ", errno=" << errno; 131f6603c60Sopenharmony_ci newPrio = getpriority(PRIO_PROCESS, 0); 132f6603c60Sopenharmony_ci EXPECT_EQ(newPrio, prio); 133f6603c60Sopenharmony_ci rt = sched_getparam(getpid(), ¶m); 134f6603c60Sopenharmony_ci EXPECT_EQ(rt, 0); 135f6603c60Sopenharmony_ci newPrio = param.sched_priority; 136f6603c60Sopenharmony_ci EXPECT_EQ(newPrio, prio); 137f6603c60Sopenharmony_ci } 138f6603c60Sopenharmony_ci LOG("set back to default value:"); 139f6603c60Sopenharmony_ci rt = setpriority(PRIO_PROCESS, getpid(), DEFAULT_SHELL_PROCESS_PRIORITY); 140f6603c60Sopenharmony_ci EXPECT_EQ(rt, 0); 141f6603c60Sopenharmony_ci newPrio = getpriority(PRIO_PROCESS, 0); 142f6603c60Sopenharmony_ci EXPECT_EQ(newPrio, DEFAULT_SHELL_PROCESS_PRIORITY); 143f6603c60Sopenharmony_ci} 144f6603c60Sopenharmony_ci/** 145f6603c60Sopenharmony_ci* @tc.number SUB_KERNEL_SCHED_API_SETPRIORITY_0100 146f6603c60Sopenharmony_ci* @tc.name test setpriority for all supported priority. 147f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 148f6603c60Sopenharmony_ci */ 149f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testSetPriority, Function | MediumTest | Level1) 150f6603c60Sopenharmony_ci{ 151f6603c60Sopenharmony_ci SetPriorityAllTest(); 152f6603c60Sopenharmony_ci} 153f6603c60Sopenharmony_ci/** 154f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_SCHED_API_SETPRIORITY_0200 155f6603c60Sopenharmony_ci * @tc.name test setpriority for all supported priority, in RR mode 156f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 157f6603c60Sopenharmony_ci */ 158f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testSetPriorityFiFo, Function | MediumTest | Level3) 159f6603c60Sopenharmony_ci{ 160f6603c60Sopenharmony_ci LOG("change sched policy"); 161f6603c60Sopenharmony_ci struct sched_param param; 162f6603c60Sopenharmony_ci int rt = sched_getparam(0, ¶m); 163f6603c60Sopenharmony_ci ASSERT_EQ(rt, 0) << "get sched param failed, errno=" << errno; 164f6603c60Sopenharmony_ci 165f6603c60Sopenharmony_ci SetPriorityAllTest(); 166f6603c60Sopenharmony_ci 167f6603c60Sopenharmony_ci LOG("set back to RR"); 168f6603c60Sopenharmony_ci rt = sched_setscheduler(0, SCHED_RR, ¶m); 169f6603c60Sopenharmony_ci ASSERT_EQ(rt, 0) << "set SCHED_RR failed, errno=" << errno; 170f6603c60Sopenharmony_ci} 171f6603c60Sopenharmony_ci 172f6603c60Sopenharmony_ci/** 173f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_SCHED_API_SETPRIORITY_0300 174f6603c60Sopenharmony_ci * @tc.name setpriority error test with unsupport parameter 1. 175f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 176f6603c60Sopenharmony_ci */ 177f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testSetPriorityError0300, Function | MediumTest | Level3) 178f6603c60Sopenharmony_ci{ 179f6603c60Sopenharmony_ci const int testPrio = 18; 180f6603c60Sopenharmony_ci LOG("invalid 'which' test:"); 181f6603c60Sopenharmony_ci errno = 0; 182f6603c60Sopenharmony_ci int rt = setpriority(PRIO_PGRP, 0, testPrio); 183f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 184f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL); 185f6603c60Sopenharmony_ci errno = 0; 186f6603c60Sopenharmony_ci rt = setpriority(PRIO_USER, 0, testPrio); 187f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 188f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL); 189f6603c60Sopenharmony_ci} 190f6603c60Sopenharmony_ci 191f6603c60Sopenharmony_ci/** 192f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_SCHED_API_SETPRIORITY_0400 193f6603c60Sopenharmony_ci * @tc.name setpriority error test with invlid parameter 1. 194f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 195f6603c60Sopenharmony_ci */ 196f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testSetPriorityError0400, Function | MediumTest | Level3) 197f6603c60Sopenharmony_ci{ 198f6603c60Sopenharmony_ci const int testPrio = 18; 199f6603c60Sopenharmony_ci int rt; 200f6603c60Sopenharmony_ci LOG("invalid 'which' test:"); 201f6603c60Sopenharmony_ci errno = 0; 202f6603c60Sopenharmony_ci rt = setpriority(PRIO_USER + GetRandom(1000), 0, testPrio); 203f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 204f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL); 205f6603c60Sopenharmony_ci errno = 0; 206f6603c60Sopenharmony_ci rt = setpriority(-GetRandom(1000), 0, testPrio); 207f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 208f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL); 209f6603c60Sopenharmony_ci} 210f6603c60Sopenharmony_ci 211f6603c60Sopenharmony_ci/** 212f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_SCHED_API_SETPRIORITY_0500 213f6603c60Sopenharmony_ci * @tc.name setpriority error test with invalid parameter 2. 214f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 215f6603c60Sopenharmony_ci */ 216f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testSetPriorityError0500, Function | MediumTest | Level3) 217f6603c60Sopenharmony_ci{ 218f6603c60Sopenharmony_ci int rt; 219f6603c60Sopenharmony_ci const int testPrio = 18; 220f6603c60Sopenharmony_ci LOG("invalid 'pid' test:"); 221f6603c60Sopenharmony_ci errno = 0; 222f6603c60Sopenharmony_ci rt = setpriority(PRIO_PROCESS, -1, testPrio); 223f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 224f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL); 225f6603c60Sopenharmony_ci errno = 0; 226f6603c60Sopenharmony_ci rt = setpriority(PRIO_PROCESS, MAX_PROCESS_NUMBER + 1, testPrio); 227f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 228f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL); 229f6603c60Sopenharmony_ci errno = 0; 230f6603c60Sopenharmony_ci rt = setpriority(PRIO_PROCESS, MAX_PROCESS_NUMBER + GetRandom(1000), testPrio); 231f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 232f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL); 233f6603c60Sopenharmony_ci} 234f6603c60Sopenharmony_ci 235f6603c60Sopenharmony_ci/** 236f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_SCHED_API_SETPRIORITY_0600 237f6603c60Sopenharmony_ci * @tc.name setpriority error test with not exist parameter 2. 238f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 239f6603c60Sopenharmony_ci */ 240f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testSetPriorityError0600, Function | MediumTest | Level3) 241f6603c60Sopenharmony_ci{ 242f6603c60Sopenharmony_ci const int testPrio = 18; 243f6603c60Sopenharmony_ci int rt; 244f6603c60Sopenharmony_ci LOG("not exist pid test:"); 245f6603c60Sopenharmony_ci pid_t nonExitPid = GetNonExistPid(); // valid but not exist pid 246f6603c60Sopenharmony_ci if (nonExitPid != -1) { 247f6603c60Sopenharmony_ci rt = setpriority(PRIO_PROCESS, nonExitPid, testPrio); 248f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 249f6603c60Sopenharmony_ci EXPECT_EQ(errno, ESRCH); 250f6603c60Sopenharmony_ci } 251f6603c60Sopenharmony_ci} 252f6603c60Sopenharmony_ci 253f6603c60Sopenharmony_ci/** 254f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_SCHED_API_SETPRIORITY_0700 255f6603c60Sopenharmony_ci * @tc.name setpriority error test with invalid parameter 3. 256f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 257f6603c60Sopenharmony_ci */ 258f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testSetPriorityError0700, Function | MediumTest | Level3) 259f6603c60Sopenharmony_ci{ 260f6603c60Sopenharmony_ci int rt; 261f6603c60Sopenharmony_ci LOG("invalid 'priority' test:"); 262f6603c60Sopenharmony_ci errno = 0; 263f6603c60Sopenharmony_ci rt = setpriority(PRIO_PROCESS, 0, -1); 264f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 265f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL); 266f6603c60Sopenharmony_ci errno = 0; 267f6603c60Sopenharmony_ci rt = setpriority(PRIO_PROCESS, 0, -GetRandom(1000)); 268f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 269f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL); 270f6603c60Sopenharmony_ci errno = 0; 271f6603c60Sopenharmony_ci rt = setpriority(PRIO_PROCESS, 0, 0); 272f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 273f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL); 274f6603c60Sopenharmony_ci errno = 0; 275f6603c60Sopenharmony_ci rt = setpriority(PRIO_PROCESS, 0, HIGHEST_USER_PROCESS_PRIORITY - 1); 276f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 277f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL); 278f6603c60Sopenharmony_ci LOG("check the default priority not changed:"); 279f6603c60Sopenharmony_ci int newPrio = getpriority(PRIO_PROCESS, 0); 280f6603c60Sopenharmony_ci if (newPrio != DEFAULT_SHELL_PROCESS_PRIORITY) { 281f6603c60Sopenharmony_ci rt = setpriority(PRIO_PROCESS, 0, DEFAULT_SHELL_PROCESS_PRIORITY); 282f6603c60Sopenharmony_ci EXPECT_EQ(rt, 0); 283f6603c60Sopenharmony_ci FAIL() << "setpriority failed but priority has changed"; 284f6603c60Sopenharmony_ci } 285f6603c60Sopenharmony_ci} 286f6603c60Sopenharmony_ci 287f6603c60Sopenharmony_ci/** 288f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_SCHED_API_SETPRIORITY_0800 289f6603c60Sopenharmony_ci * @tc.name setpriority error test with no permission. 290f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 291f6603c60Sopenharmony_ci */ 292f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testSetPriorityError0800, Function | MediumTest | Level3) 293f6603c60Sopenharmony_ci{ 294f6603c60Sopenharmony_ci const int testPrio = 18; 295f6603c60Sopenharmony_ci int rt; 296f6603c60Sopenharmony_ci LOG("no permission test:"); 297f6603c60Sopenharmony_ci errno = 0; 298f6603c60Sopenharmony_ci rt = setpriority(PRIO_PROCESS, INIT_PROCESS_ID, testPrio); 299f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 300f6603c60Sopenharmony_ci EXPECT_EQ(errno, EPERM); 301f6603c60Sopenharmony_ci errno = 0; 302f6603c60Sopenharmony_ci rt = setpriority(PRIO_PROCESS, KERNEL_PROCESS_ID, testPrio); 303f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 304f6603c60Sopenharmony_ci EXPECT_EQ(errno, EPERM); 305f6603c60Sopenharmony_ci} 306f6603c60Sopenharmony_ci 307f6603c60Sopenharmony_ci/** 308f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_SCHED_API_SCHED_GETPARAM_0100 309f6603c60Sopenharmony_ci * @tc.name sched_getparam api test. 310f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 311f6603c60Sopenharmony_ci */ 312f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testSchedGetParam, Function | MediumTest | Level1) 313f6603c60Sopenharmony_ci{ 314f6603c60Sopenharmony_ci struct sched_param param; 315f6603c60Sopenharmony_ci int rt = sched_getparam(0, ¶m); 316f6603c60Sopenharmony_ci EXPECT_EQ(rt, 0) << "sched_getparam failed, errno=" << errno; 317f6603c60Sopenharmony_ci EXPECT_EQ(param.sched_priority, DEFAULT_SHELL_PROCESS_PRIORITY); 318f6603c60Sopenharmony_ci 319f6603c60Sopenharmony_ci rt = sched_getparam(getpid(), ¶m); 320f6603c60Sopenharmony_ci EXPECT_EQ(rt, 0) << "sched_getparam failed, errno=" << errno; 321f6603c60Sopenharmony_ci EXPECT_EQ(param.sched_priority, DEFAULT_SHELL_PROCESS_PRIORITY); 322f6603c60Sopenharmony_ci 323f6603c60Sopenharmony_ci rt = sched_getparam(INIT_PROCESS_ID, ¶m); 324f6603c60Sopenharmony_ci EXPECT_EQ(rt, 0) << "sched_getparam failed, errno=" << errno; 325f6603c60Sopenharmony_ci EXPECT_EQ(param.sched_priority, DEFAULT_INIT_PROCESS_PRIORITY); 326f6603c60Sopenharmony_ci 327f6603c60Sopenharmony_ci rt = sched_getparam(KERNEL_PROCESS_ID, ¶m); 328f6603c60Sopenharmony_ci EXPECT_EQ(rt, 0) << "sched_getparam failed, errno=" << errno; 329f6603c60Sopenharmony_ci EXPECT_EQ(param.sched_priority, DEFAULT_KERNEL_PROCESS_PRIORITY); 330f6603c60Sopenharmony_ci} 331f6603c60Sopenharmony_ci 332f6603c60Sopenharmony_ci/** 333f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_SCHED_API_SCHED_GETPARAM_0200 334f6603c60Sopenharmony_ci * @tc.name sched_getparam error test with invalid parameter 1. 335f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 336f6603c60Sopenharmony_ci */ 337f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testSchedGetParamError0200, Function | MediumTest | Level3) 338f6603c60Sopenharmony_ci{ 339f6603c60Sopenharmony_ci struct sched_param param; 340f6603c60Sopenharmony_ci LOG("invalid 'pid' test:"); 341f6603c60Sopenharmony_ci errno = 0; 342f6603c60Sopenharmony_ci int rt = sched_getparam(-1, ¶m); 343f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 344f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL); 345f6603c60Sopenharmony_ci errno = 0; 346f6603c60Sopenharmony_ci rt = sched_getparam(MAX_PROCESS_NUMBER + 1, ¶m); 347f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 348f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL); 349f6603c60Sopenharmony_ci errno = 0; 350f6603c60Sopenharmony_ci rt = sched_getparam(MAX_PROCESS_NUMBER + GetRandom(100000), ¶m); 351f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 352f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL); 353f6603c60Sopenharmony_ci} 354f6603c60Sopenharmony_ci 355f6603c60Sopenharmony_ci/** 356f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_SCHED_API_SCHED_GETPARAM_0300 357f6603c60Sopenharmony_ci * @tc.name sched_getparam error test with not exist pid. 358f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 359f6603c60Sopenharmony_ci */ 360f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testSchedGetParamError0300, Function | MediumTest | Level3) 361f6603c60Sopenharmony_ci{ 362f6603c60Sopenharmony_ci struct sched_param param; 363f6603c60Sopenharmony_ci int rt; 364f6603c60Sopenharmony_ci LOG("not exist pid test:"); 365f6603c60Sopenharmony_ci pid_t nonExitPid = GetNonExistPid(); // valid but not exist pid 366f6603c60Sopenharmony_ci if (nonExitPid != -1) { 367f6603c60Sopenharmony_ci rt = sched_getparam(nonExitPid, ¶m); 368f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 369f6603c60Sopenharmony_ci EXPECT_EQ(errno, ESRCH); 370f6603c60Sopenharmony_ci } 371f6603c60Sopenharmony_ci} 372f6603c60Sopenharmony_ci 373f6603c60Sopenharmony_ci/** 374f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_SCHED_API_SCHED_GETPARAM_0400 375f6603c60Sopenharmony_ci * @tc.name sched_getparam error test with invalid parameter 2. 376f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 377f6603c60Sopenharmony_ci */ 378f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testSchedGetParamError0400, Function | MediumTest | Level3) 379f6603c60Sopenharmony_ci{ 380f6603c60Sopenharmony_ci int rt; 381f6603c60Sopenharmony_ci LOG("invalid 'param' test:"); 382f6603c60Sopenharmony_ci rt = sched_getparam(0, NULL); 383f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 384f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL); 385f6603c60Sopenharmony_ci} 386f6603c60Sopenharmony_ci 387f6603c60Sopenharmony_civoid SchedSetParamAllTest() 388f6603c60Sopenharmony_ci{ 389f6603c60Sopenharmony_ci int rt, newPrio; 390f6603c60Sopenharmony_ci struct sched_param param; 391f6603c60Sopenharmony_ci LOG("test all supported priority:"); 392f6603c60Sopenharmony_ci for (int prio = HIGHEST_USER_PROCESS_PRIORITY; prio <= LOWEST_USER_PROCESS_PRIORITY; prio++) { 393f6603c60Sopenharmony_ci param.sched_priority = prio; 394f6603c60Sopenharmony_ci rt = sched_setparam(0, ¶m); 395f6603c60Sopenharmony_ci EXPECT_EQ(rt, 0) << "sched_setparam failed for prio=" << prio << ", errno=" << errno; 396f6603c60Sopenharmony_ci newPrio = getpriority(PRIO_PROCESS, getpid()); 397f6603c60Sopenharmony_ci EXPECT_EQ(prio, newPrio); 398f6603c60Sopenharmony_ci rt = sched_getparam(0, ¶m); 399f6603c60Sopenharmony_ci EXPECT_EQ(rt, 0); 400f6603c60Sopenharmony_ci EXPECT_EQ(prio, param.sched_priority); 401f6603c60Sopenharmony_ci } 402f6603c60Sopenharmony_ci LOG("set back to default value:"); 403f6603c60Sopenharmony_ci param.sched_priority = DEFAULT_SHELL_PROCESS_PRIORITY; 404f6603c60Sopenharmony_ci rt = sched_setparam(getpid(), ¶m); 405f6603c60Sopenharmony_ci EXPECT_EQ(rt, 0); 406f6603c60Sopenharmony_ci newPrio = getpriority(PRIO_PROCESS, 0); 407f6603c60Sopenharmony_ci EXPECT_EQ(newPrio, DEFAULT_SHELL_PROCESS_PRIORITY); 408f6603c60Sopenharmony_ci} 409f6603c60Sopenharmony_ci/** 410f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_SCHED_API_SCHED_SETPARAM_0100 411f6603c60Sopenharmony_ci * @tc.name test sched_setparam for all supported priority. 412f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 413f6603c60Sopenharmony_ci */ 414f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testSchedSetParam, Function | MediumTest | Level1) 415f6603c60Sopenharmony_ci{ 416f6603c60Sopenharmony_ci SchedSetParamAllTest(); 417f6603c60Sopenharmony_ci} 418f6603c60Sopenharmony_ci/** 419f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_SCHED_API_SCHED_SETPARAM_0200 420f6603c60Sopenharmony_ci * @tc.name test sched_setparam for all supported priority, in FIFO mode 421f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 422f6603c60Sopenharmony_ci */ 423f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testSchedSetParamFiFo, Function | MediumTest | Level3) 424f6603c60Sopenharmony_ci{ 425f6603c60Sopenharmony_ci struct sched_param param; 426f6603c60Sopenharmony_ci int rt = sched_getparam(0, ¶m); 427f6603c60Sopenharmony_ci ASSERT_EQ(rt, 0) << "get sched param failed, errno=" << errno; 428f6603c60Sopenharmony_ci 429f6603c60Sopenharmony_ci SchedSetParamAllTest(); 430f6603c60Sopenharmony_ci 431f6603c60Sopenharmony_ci LOG("set to RR"); 432f6603c60Sopenharmony_ci rt = sched_setscheduler(0, SCHED_RR, ¶m); 433f6603c60Sopenharmony_ci ASSERT_EQ(rt, 0) << "set SCHED_RR failed, errno=" << errno; 434f6603c60Sopenharmony_ci} 435f6603c60Sopenharmony_ci 436f6603c60Sopenharmony_ci/** 437f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_SCHED_API_SCHED_SETPARAM_0300 438f6603c60Sopenharmony_ci * @tc.name sched_setparam error test with invalid pid. 439f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 440f6603c60Sopenharmony_ci */ 441f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testSchedSetParamError0300, Function | MediumTest | Level3) 442f6603c60Sopenharmony_ci{ 443f6603c60Sopenharmony_ci int rt; 444f6603c60Sopenharmony_ci struct sched_param param; 445f6603c60Sopenharmony_ci param.sched_priority = 18; 446f6603c60Sopenharmony_ci 447f6603c60Sopenharmony_ci LOG("invalid 'pid' test:"); 448f6603c60Sopenharmony_ci errno = 0; 449f6603c60Sopenharmony_ci rt = sched_setparam(-1, ¶m); 450f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 451f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL); 452f6603c60Sopenharmony_ci errno = 0; 453f6603c60Sopenharmony_ci rt = sched_setparam(MAX_PROCESS_NUMBER + 1, ¶m); 454f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 455f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL); 456f6603c60Sopenharmony_ci errno = 0; 457f6603c60Sopenharmony_ci rt = sched_setparam(MAX_PROCESS_NUMBER + GetRandom(100000), ¶m); 458f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 459f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL); 460f6603c60Sopenharmony_ci} 461f6603c60Sopenharmony_ci 462f6603c60Sopenharmony_ci/** 463f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_SCHED_API_SCHED_SETPARAM_0400 464f6603c60Sopenharmony_ci * @tc.name sched_setparam error test with not exist pid. 465f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 466f6603c60Sopenharmony_ci */ 467f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testSchedSetParamError0400, Function | MediumTest | Level3) 468f6603c60Sopenharmony_ci{ 469f6603c60Sopenharmony_ci int rt; 470f6603c60Sopenharmony_ci struct sched_param param; 471f6603c60Sopenharmony_ci param.sched_priority = 18; 472f6603c60Sopenharmony_ci 473f6603c60Sopenharmony_ci LOG("not exist pid test:"); 474f6603c60Sopenharmony_ci pid_t nonExitPid = GetNonExistPid(); // valid but not exist pid 475f6603c60Sopenharmony_ci if (nonExitPid != -1) { 476f6603c60Sopenharmony_ci rt = sched_setparam(nonExitPid, ¶m); 477f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 478f6603c60Sopenharmony_ci EXPECT_EQ(errno, ESRCH); 479f6603c60Sopenharmony_ci } 480f6603c60Sopenharmony_ci} 481f6603c60Sopenharmony_ci 482f6603c60Sopenharmony_ci/** 483f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_SCHED_API_SCHED_SETPARAM_0500 484f6603c60Sopenharmony_ci * @tc.name sched_setparam error test with invalid parameter 2. 485f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 486f6603c60Sopenharmony_ci */ 487f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testSchedSetParamError0500, Function | MediumTest | Level3) 488f6603c60Sopenharmony_ci{ 489f6603c60Sopenharmony_ci int rt; 490f6603c60Sopenharmony_ci LOG("invalid 'param' test:"); 491f6603c60Sopenharmony_ci rt = sched_setparam(0, NULL); 492f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 493f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL); 494f6603c60Sopenharmony_ci} 495f6603c60Sopenharmony_ci 496f6603c60Sopenharmony_ci/** 497f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_SCHED_API_SCHED_SETPARAM_0600 498f6603c60Sopenharmony_ci * @tc.name sched_setparam error test with invalid priority. 499f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 500f6603c60Sopenharmony_ci */ 501f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testSchedSetParamError0600, Function | MediumTest | Level3) 502f6603c60Sopenharmony_ci{ 503f6603c60Sopenharmony_ci int rt; 504f6603c60Sopenharmony_ci struct sched_param param; 505f6603c60Sopenharmony_ci param.sched_priority = 18; 506f6603c60Sopenharmony_ci 507f6603c60Sopenharmony_ci LOG("invalid 'priority' test:"); 508f6603c60Sopenharmony_ci errno = 0; 509f6603c60Sopenharmony_ci param.sched_priority = -1; 510f6603c60Sopenharmony_ci rt = sched_setparam(0, ¶m); 511f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 512f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL); 513f6603c60Sopenharmony_ci errno = 0; 514f6603c60Sopenharmony_ci param.sched_priority = -GetRandom(1000); 515f6603c60Sopenharmony_ci rt = sched_setparam(0, ¶m); 516f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 517f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL); 518f6603c60Sopenharmony_ci errno = 0; 519f6603c60Sopenharmony_ci param.sched_priority = 0; 520f6603c60Sopenharmony_ci rt = sched_setparam(0, ¶m); 521f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 522f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL); 523f6603c60Sopenharmony_ci errno = 0; 524f6603c60Sopenharmony_ci param.sched_priority = HIGHEST_USER_PROCESS_PRIORITY - 1; 525f6603c60Sopenharmony_ci rt = sched_setparam(0, ¶m); 526f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 527f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL); 528f6603c60Sopenharmony_ci 529f6603c60Sopenharmony_ci LOG("check the default priority not changed:"); 530f6603c60Sopenharmony_ci int newPrio = getpriority(PRIO_PROCESS, 0); 531f6603c60Sopenharmony_ci if (newPrio != DEFAULT_SHELL_PROCESS_PRIORITY) { 532f6603c60Sopenharmony_ci rt = setpriority(PRIO_PROCESS, 0, DEFAULT_SHELL_PROCESS_PRIORITY); 533f6603c60Sopenharmony_ci EXPECT_EQ(rt, 0); 534f6603c60Sopenharmony_ci FAIL() << "sched_setparam failed but priority has changed"; 535f6603c60Sopenharmony_ci } 536f6603c60Sopenharmony_ci} 537f6603c60Sopenharmony_ci 538f6603c60Sopenharmony_ci/** 539f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_SCHED_API_SCHED_SETPARAM_0700 540f6603c60Sopenharmony_ci * @tc.name sched_setparam error test with no permission. 541f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 542f6603c60Sopenharmony_ci */ 543f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testSchedSetParamError0700, Function | MediumTest | Level3) 544f6603c60Sopenharmony_ci{ 545f6603c60Sopenharmony_ci int rt; 546f6603c60Sopenharmony_ci struct sched_param param; 547f6603c60Sopenharmony_ci param.sched_priority = 18; 548f6603c60Sopenharmony_ci 549f6603c60Sopenharmony_ci LOG("no permission test:"); 550f6603c60Sopenharmony_ci errno = 0; 551f6603c60Sopenharmony_ci rt = sched_setparam(INIT_PROCESS_ID, ¶m); 552f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 553f6603c60Sopenharmony_ci EXPECT_EQ(errno, EPERM); 554f6603c60Sopenharmony_ci errno = 0; 555f6603c60Sopenharmony_ci rt = sched_setparam(KERNEL_PROCESS_ID, ¶m); 556f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 557f6603c60Sopenharmony_ci EXPECT_EQ(errno, EPERM); 558f6603c60Sopenharmony_ci 559f6603c60Sopenharmony_ci LOG("check the default priority not changed:"); 560f6603c60Sopenharmony_ci int newPrio = getpriority(PRIO_PROCESS, 0); 561f6603c60Sopenharmony_ci if (newPrio != DEFAULT_SHELL_PROCESS_PRIORITY) { 562f6603c60Sopenharmony_ci rt = setpriority(PRIO_PROCESS, 0, DEFAULT_SHELL_PROCESS_PRIORITY); 563f6603c60Sopenharmony_ci EXPECT_EQ(rt, 0); 564f6603c60Sopenharmony_ci FAIL() << "sched_setparam failed but priority has changed"; 565f6603c60Sopenharmony_ci } 566f6603c60Sopenharmony_ci} 567f6603c60Sopenharmony_ci 568f6603c60Sopenharmony_ci/** 569f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_SCHED_API_GET_PRIORITY_MAX_0100 570f6603c60Sopenharmony_ci * @tc.name sched_get_priority_max api test. 571f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 572f6603c60Sopenharmony_ci */ 573f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testSchedGetPriorityMax, Function | MediumTest | Level3) 574f6603c60Sopenharmony_ci{ 575f6603c60Sopenharmony_ci int prio = sched_get_priority_max(SCHED_RR); 576f6603c60Sopenharmony_ci EXPECT_EQ(prio, LOWEST_USER_PROCESS_PRIORITY); 577f6603c60Sopenharmony_ci} 578f6603c60Sopenharmony_ci 579f6603c60Sopenharmony_ci/** 580f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_SCHED_API_GET_PRIORITY_MAX_0200 581f6603c60Sopenharmony_ci * @tc.name sched_get_priority_max api error test with unsupport policy. 582f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 583f6603c60Sopenharmony_ci */ 584f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testSchedGetPriorityMaxError0200, Function | MediumTest | Level3) 585f6603c60Sopenharmony_ci{ 586f6603c60Sopenharmony_ci // SCHED_RR is 2 587f6603c60Sopenharmony_ci int invalidPolicy[] = {SCHED_FIFO, SCHED_OTHER, SCHED_BATCH, SCHED_IDLE, SCHED_DEADLINE, SCHED_RESET_ON_FORK}; 588f6603c60Sopenharmony_ci int testLoop = sizeof(invalidPolicy)/sizeof(int); 589f6603c60Sopenharmony_ci for (int i = 0; i < testLoop; i++) { 590f6603c60Sopenharmony_ci errno = 0; 591f6603c60Sopenharmony_ci int prio = sched_get_priority_max(invalidPolicy[i]); 592f6603c60Sopenharmony_ci EXPECT_EQ(prio, -1) << "sched_get_priority_max ok for policy=" << invalidPolicy[i]; 593f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL) << "errno check fail for policy=" << invalidPolicy[i]; 594f6603c60Sopenharmony_ci } 595f6603c60Sopenharmony_ci} 596f6603c60Sopenharmony_ci 597f6603c60Sopenharmony_ci/** 598f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_SCHED_API_GET_PRIORITY_MAX_0300 599f6603c60Sopenharmony_ci * @tc.name sched_get_priority_max api error test with invalid policy. 600f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 601f6603c60Sopenharmony_ci */ 602f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testSchedGetPriorityMaxError0300, Function | MediumTest | Level3) 603f6603c60Sopenharmony_ci{ 604f6603c60Sopenharmony_ci // SCHED_RR is 2 605f6603c60Sopenharmony_ci int invalidPolicyVal; 606f6603c60Sopenharmony_ci int prio; 607f6603c60Sopenharmony_ci 608f6603c60Sopenharmony_ci invalidPolicyVal = -GetRandom(10000); 609f6603c60Sopenharmony_ci errno = 0; 610f6603c60Sopenharmony_ci prio = sched_get_priority_max(invalidPolicyVal); 611f6603c60Sopenharmony_ci EXPECT_EQ(prio, -1) << "sched_get_priority_max ok for policy=" << invalidPolicyVal; 612f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL) << "errno check fail for policy=" << invalidPolicyVal; 613f6603c60Sopenharmony_ci 614f6603c60Sopenharmony_ci invalidPolicyVal = GetRandom(10000) + SCHED_DEADLINE; 615f6603c60Sopenharmony_ci errno = 0; 616f6603c60Sopenharmony_ci prio = sched_get_priority_max(invalidPolicyVal); 617f6603c60Sopenharmony_ci EXPECT_EQ(prio, -1) << "sched_get_priority_max ok for policy=" << invalidPolicyVal; 618f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL) << "errno check fail for policy=" << invalidPolicyVal; 619f6603c60Sopenharmony_ci} 620f6603c60Sopenharmony_ci 621f6603c60Sopenharmony_ci/** 622f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_SCHED_API_GET_PRIORITY_MIN_0100 623f6603c60Sopenharmony_ci * @tc.name sched_get_priority_min api test. 624f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 625f6603c60Sopenharmony_ci */ 626f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testSchedGetPriorityMin, Function | MediumTest | Level3) 627f6603c60Sopenharmony_ci{ 628f6603c60Sopenharmony_ci int prio = sched_get_priority_min(SCHED_RR); 629f6603c60Sopenharmony_ci EXPECT_EQ(prio, HIGHEST_USER_PROCESS_PRIORITY); 630f6603c60Sopenharmony_ci} 631f6603c60Sopenharmony_ci 632f6603c60Sopenharmony_ci/** 633f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_SCHED_API_GET_PRIORITY_MIN_0200 634f6603c60Sopenharmony_ci * @tc.name sched_get_priority_min api error test with unsupport policy. 635f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 636f6603c60Sopenharmony_ci */ 637f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testSchedGetPriorityMinError0200, Function | MediumTest | Level3) 638f6603c60Sopenharmony_ci{ 639f6603c60Sopenharmony_ci // SCHED_RR is 2 640f6603c60Sopenharmony_ci int invalidPolicy[] = {SCHED_FIFO, SCHED_OTHER, SCHED_BATCH, SCHED_IDLE, SCHED_DEADLINE, SCHED_RESET_ON_FORK}; 641f6603c60Sopenharmony_ci int testLoop = sizeof(invalidPolicy)/sizeof(int); 642f6603c60Sopenharmony_ci for (int i = 0; i < testLoop; i++) { 643f6603c60Sopenharmony_ci errno = 0; 644f6603c60Sopenharmony_ci int prio = sched_get_priority_min(invalidPolicy[i]); 645f6603c60Sopenharmony_ci EXPECT_EQ(prio, -1) << "sched_get_priority_min ok for policy=" << invalidPolicy[i]; 646f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL) << "errno check fail for policy=" << invalidPolicy[i]; 647f6603c60Sopenharmony_ci } 648f6603c60Sopenharmony_ci} 649f6603c60Sopenharmony_ci 650f6603c60Sopenharmony_ci/** 651f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_SCHED_API_GET_PRIORITY_MIN_0300 652f6603c60Sopenharmony_ci * @tc.name sched_get_priority_min api error test with invalid policy. 653f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 654f6603c60Sopenharmony_ci */ 655f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testSchedGetPriorityMinError0300, Function | MediumTest | Level3) 656f6603c60Sopenharmony_ci{ 657f6603c60Sopenharmony_ci int invalidPolicyVal; 658f6603c60Sopenharmony_ci int prio; 659f6603c60Sopenharmony_ci invalidPolicyVal = -GetRandom(10000); 660f6603c60Sopenharmony_ci errno = 0; 661f6603c60Sopenharmony_ci prio = sched_get_priority_min(invalidPolicyVal); 662f6603c60Sopenharmony_ci EXPECT_EQ(prio, -1) << "sched_get_priority_min ok for policy=" << invalidPolicyVal; 663f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL) << "errno check fail for policy=" << invalidPolicyVal; 664f6603c60Sopenharmony_ci 665f6603c60Sopenharmony_ci invalidPolicyVal = GetRandom(10000) + SCHED_DEADLINE; 666f6603c60Sopenharmony_ci errno = 0; 667f6603c60Sopenharmony_ci prio = sched_get_priority_min(invalidPolicyVal); 668f6603c60Sopenharmony_ci EXPECT_EQ(prio, -1) << "sched_get_priority_min ok for policy=" << invalidPolicyVal; 669f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL) << "errno check fail for policy=" << invalidPolicyVal; 670f6603c60Sopenharmony_ci} 671f6603c60Sopenharmony_ci 672f6603c60Sopenharmony_ci/** 673f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_SCHED_API_GETSCHEDULER_0100 674f6603c60Sopenharmony_ci * @tc.name sched_getscheduler api test. 675f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 676f6603c60Sopenharmony_ci */ 677f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testSchedGetScheduler, Function | MediumTest | Level1) 678f6603c60Sopenharmony_ci{ 679f6603c60Sopenharmony_ci int policy = sched_getscheduler(0); 680f6603c60Sopenharmony_ci EXPECT_EQ(policy, SCHED_RR); 681f6603c60Sopenharmony_ci policy = sched_getscheduler(getpid()); 682f6603c60Sopenharmony_ci EXPECT_EQ(policy, SCHED_RR) << "check 'shell' policy failed."; 683f6603c60Sopenharmony_ci policy = sched_getscheduler(INIT_PROCESS_ID); 684f6603c60Sopenharmony_ci EXPECT_EQ(policy, SCHED_RR) << "check 'init' policy failed."; 685f6603c60Sopenharmony_ci policy = sched_getscheduler(KERNEL_PROCESS_ID); 686f6603c60Sopenharmony_ci EXPECT_EQ(policy, SCHED_RR) << "check 'KProcess' policy failed."; 687f6603c60Sopenharmony_ci} 688f6603c60Sopenharmony_ci 689f6603c60Sopenharmony_ci/** 690f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_SCHED_API_GETSCHEDULER_0200 691f6603c60Sopenharmony_ci * @tc.name sched_getscheduler api error test with invalid pid. 692f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 693f6603c60Sopenharmony_ci */ 694f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testSchedGetSchedulerError0200, Function | MediumTest | Level1) 695f6603c60Sopenharmony_ci{ 696f6603c60Sopenharmony_ci LOG("invalid pid test:"); 697f6603c60Sopenharmony_ci errno = 0; 698f6603c60Sopenharmony_ci int rt = sched_getscheduler(-1); 699f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 700f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL); 701f6603c60Sopenharmony_ci errno = 0; 702f6603c60Sopenharmony_ci rt = sched_getscheduler(MAX_PROCESS_NUMBER + 1); 703f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 704f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL); 705f6603c60Sopenharmony_ci errno = 0; 706f6603c60Sopenharmony_ci rt = sched_getscheduler(MAX_PROCESS_NUMBER + GetRandom(1000)); 707f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 708f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL); 709f6603c60Sopenharmony_ci} 710f6603c60Sopenharmony_ci 711f6603c60Sopenharmony_ci/** 712f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_SCHED_API_GETSCHEDULER_0300 713f6603c60Sopenharmony_ci * @tc.name sched_getscheduler api error test with not exist pid. 714f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 715f6603c60Sopenharmony_ci */ 716f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testSchedGetSchedulerError0300, Function | MediumTest | Level1) 717f6603c60Sopenharmony_ci{ 718f6603c60Sopenharmony_ci int rt; 719f6603c60Sopenharmony_ci errno = 0; 720f6603c60Sopenharmony_ci LOG("not exist pid test:"); 721f6603c60Sopenharmony_ci pid_t nonExitPid = GetNonExistPid(); // valid but not exist pid 722f6603c60Sopenharmony_ci if (nonExitPid != -1) { 723f6603c60Sopenharmony_ci rt = sched_getscheduler(nonExitPid); 724f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 725f6603c60Sopenharmony_ci EXPECT_EQ(errno, ESRCH); 726f6603c60Sopenharmony_ci } 727f6603c60Sopenharmony_ci} 728f6603c60Sopenharmony_ci 729f6603c60Sopenharmony_ci/** 730f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_SCHED_API_SETSCHEDULER_0100 731f6603c60Sopenharmony_ci * @tc.name sched_setscheduler api test. 732f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 733f6603c60Sopenharmony_ci */ 734f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testSchedSetScheduler, Function | MediumTest | Level1) 735f6603c60Sopenharmony_ci{ 736f6603c60Sopenharmony_ci struct sched_param param; 737f6603c60Sopenharmony_ci int rt = sched_getparam(0, ¶m); 738f6603c60Sopenharmony_ci ASSERT_EQ(rt, 0) << "get sched param failed, errno=" << errno; 739f6603c60Sopenharmony_ci 740f6603c60Sopenharmony_ci LOG("change policy and priority:"); 741f6603c60Sopenharmony_ci rt = sched_setscheduler(0, SCHED_RR, ¶m); 742f6603c60Sopenharmony_ci ASSERT_EQ(rt, 0) << "set RR failed, errno=" << errno; 743f6603c60Sopenharmony_ci int policy = sched_getscheduler(0); 744f6603c60Sopenharmony_ci EXPECT_EQ(policy, SCHED_RR); 745f6603c60Sopenharmony_ci rt = sched_setscheduler(getpid(), SCHED_RR, ¶m); 746f6603c60Sopenharmony_ci EXPECT_EQ(rt, 0) << "set RR again failed, errno=" << errno; 747f6603c60Sopenharmony_ci policy = sched_getscheduler(0); 748f6603c60Sopenharmony_ci EXPECT_EQ(policy, SCHED_RR); 749f6603c60Sopenharmony_ci 750f6603c60Sopenharmony_ci param.sched_priority = LOWEST_USER_PROCESS_PRIORITY; 751f6603c60Sopenharmony_ci rt = sched_setscheduler(getpid(), SCHED_RR, ¶m); 752f6603c60Sopenharmony_ci EXPECT_EQ(rt, 0) << "set RR again failed, errno=" << errno; 753f6603c60Sopenharmony_ci policy = sched_getscheduler(0); 754f6603c60Sopenharmony_ci EXPECT_EQ(policy, SCHED_RR); 755f6603c60Sopenharmony_ci int newPrio = getpriority(PRIO_PROCESS, 0); 756f6603c60Sopenharmony_ci EXPECT_EQ(newPrio, LOWEST_USER_PROCESS_PRIORITY); 757f6603c60Sopenharmony_ci 758f6603c60Sopenharmony_ci LOG("set back to SCHED_RR and default priority:"); 759f6603c60Sopenharmony_ci param.sched_priority = DEFAULT_SHELL_PROCESS_PRIORITY; 760f6603c60Sopenharmony_ci rt = sched_setscheduler(getpid(), SCHED_RR, ¶m); 761f6603c60Sopenharmony_ci EXPECT_EQ(rt, 0) << "set back to SCHED_RR failed, errno=" << errno; 762f6603c60Sopenharmony_ci policy = sched_getscheduler(0); 763f6603c60Sopenharmony_ci EXPECT_EQ(policy, SCHED_RR); 764f6603c60Sopenharmony_ci} 765f6603c60Sopenharmony_ci 766f6603c60Sopenharmony_ci/** 767f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_SCHED_API_SETSCHEDULER_0200 768f6603c60Sopenharmony_ci * @tc.name sched_setscheduler api error test with invalid pid. 769f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 770f6603c60Sopenharmony_ci */ 771f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testSchedSetSchedulerError0200, Function | MediumTest | Level1) 772f6603c60Sopenharmony_ci{ 773f6603c60Sopenharmony_ci struct sched_param param; 774f6603c60Sopenharmony_ci int rt = sched_getparam(0, ¶m); 775f6603c60Sopenharmony_ci ASSERT_EQ(rt, 0) << "get sched param failed, errno=" << errno; 776f6603c60Sopenharmony_ci 777f6603c60Sopenharmony_ci LOG("invalid pid test:"); 778f6603c60Sopenharmony_ci errno = 0; 779f6603c60Sopenharmony_ci rt = sched_setscheduler(-1, SCHED_RR, ¶m); 780f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 781f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL); 782f6603c60Sopenharmony_ci errno = 0; 783f6603c60Sopenharmony_ci rt = sched_setscheduler(MAX_PROCESS_NUMBER + 1, SCHED_RR, ¶m); 784f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 785f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL); 786f6603c60Sopenharmony_ci errno = 0; 787f6603c60Sopenharmony_ci rt = sched_setscheduler(MAX_PROCESS_NUMBER + GetRandom(1000), SCHED_RR, ¶m); 788f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 789f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL); 790f6603c60Sopenharmony_ci} 791f6603c60Sopenharmony_ci 792f6603c60Sopenharmony_ci/** 793f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_SCHED_API_SETSCHEDULER_0300 794f6603c60Sopenharmony_ci * @tc.name sched_setscheduler api error test with not exist pid. 795f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 796f6603c60Sopenharmony_ci */ 797f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testSchedSetSchedulerError0300, Function | MediumTest | Level1) 798f6603c60Sopenharmony_ci{ 799f6603c60Sopenharmony_ci struct sched_param param; 800f6603c60Sopenharmony_ci int rt = sched_getparam(0, ¶m); 801f6603c60Sopenharmony_ci ASSERT_EQ(rt, 0) << "get sched param failed, errno=" << errno; 802f6603c60Sopenharmony_ci 803f6603c60Sopenharmony_ci LOG("not exist pid test:"); 804f6603c60Sopenharmony_ci pid_t nonExitPid = GetNonExistPid(); // valid but not exist pid 805f6603c60Sopenharmony_ci if (nonExitPid != -1) { 806f6603c60Sopenharmony_ci rt = sched_setscheduler(nonExitPid, SCHED_RR, ¶m); 807f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 808f6603c60Sopenharmony_ci EXPECT_EQ(errno, ESRCH); 809f6603c60Sopenharmony_ci } 810f6603c60Sopenharmony_ci} 811f6603c60Sopenharmony_ci 812f6603c60Sopenharmony_ci/** 813f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_SCHED_API_SETSCHEDULER_0400 814f6603c60Sopenharmony_ci * @tc.name sched_setscheduler api error test with unsupport policy. 815f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 816f6603c60Sopenharmony_ci */ 817f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testSchedSetSchedulerError0400, Function | MediumTest | Level1) 818f6603c60Sopenharmony_ci{ 819f6603c60Sopenharmony_ci struct sched_param param; 820f6603c60Sopenharmony_ci int rt = sched_getparam(0, ¶m); 821f6603c60Sopenharmony_ci ASSERT_EQ(rt, 0) << "get sched param failed, errno=" << errno; 822f6603c60Sopenharmony_ci 823f6603c60Sopenharmony_ci LOG("invalid policy test:"); 824f6603c60Sopenharmony_ci //and SCHED_RR is 2 825f6603c60Sopenharmony_ci int invalidPolicy[] = {SCHED_FIFO, SCHED_OTHER, SCHED_BATCH, SCHED_IDLE, SCHED_DEADLINE, SCHED_RESET_ON_FORK}; 826f6603c60Sopenharmony_ci int testLoop = sizeof(invalidPolicy)/sizeof(int); 827f6603c60Sopenharmony_ci for (int i = 0; i < testLoop; i++) { 828f6603c60Sopenharmony_ci errno = 0; 829f6603c60Sopenharmony_ci rt = sched_setscheduler(0, invalidPolicy[i], ¶m); 830f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1) << "sched_get_priority_min ok for policy=" << invalidPolicy[i]; 831f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL) << "errno check fail for policy=" << invalidPolicy[i]; 832f6603c60Sopenharmony_ci } 833f6603c60Sopenharmony_ci 834f6603c60Sopenharmony_ci LOG("check the default priority not changed:"); 835f6603c60Sopenharmony_ci int policy = sched_getscheduler(0); 836f6603c60Sopenharmony_ci if (policy != SCHED_RR) { 837f6603c60Sopenharmony_ci rt = sched_setscheduler(0, SCHED_RR, ¶m); 838f6603c60Sopenharmony_ci EXPECT_EQ(rt, 0); 839f6603c60Sopenharmony_ci FAIL() << "sched_setscheduler failed but policy has changed"; 840f6603c60Sopenharmony_ci } 841f6603c60Sopenharmony_ci} 842f6603c60Sopenharmony_ci 843f6603c60Sopenharmony_ci/** 844f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_SCHED_API_SETSCHEDULER_0500 845f6603c60Sopenharmony_ci * @tc.name sched_setscheduler api error test with invalid policy. 846f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 847f6603c60Sopenharmony_ci */ 848f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testSchedSetSchedulerError0500, Function | MediumTest | Level1) 849f6603c60Sopenharmony_ci{ 850f6603c60Sopenharmony_ci struct sched_param param; 851f6603c60Sopenharmony_ci int rt = sched_getparam(0, ¶m); 852f6603c60Sopenharmony_ci ASSERT_EQ(rt, 0) << "get sched param failed, errno=" << errno; 853f6603c60Sopenharmony_ci 854f6603c60Sopenharmony_ci LOG("invalid policy test:"); 855f6603c60Sopenharmony_ci int invalidPolicy = -GetRandom(10000); 856f6603c60Sopenharmony_ci errno = 0; 857f6603c60Sopenharmony_ci rt = sched_setscheduler(0, invalidPolicy, ¶m); 858f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1) << "sched_get_priority_min ok for policy=" << invalidPolicy; 859f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL) << "errno check fail for policy=" << invalidPolicy; 860f6603c60Sopenharmony_ci invalidPolicy = GetRandom(10000) + SCHED_DEADLINE; 861f6603c60Sopenharmony_ci errno = 0; 862f6603c60Sopenharmony_ci rt = sched_setscheduler(0, invalidPolicy, ¶m); 863f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1) << "sched_get_priority_min ok for policy=" << invalidPolicy; 864f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL) << "errno check fail for policy=" << invalidPolicy; 865f6603c60Sopenharmony_ci 866f6603c60Sopenharmony_ci LOG("check the default priority not changed:"); 867f6603c60Sopenharmony_ci int policy = sched_getscheduler(0); 868f6603c60Sopenharmony_ci if (policy != SCHED_RR) { 869f6603c60Sopenharmony_ci rt = sched_setscheduler(0, SCHED_RR, ¶m); 870f6603c60Sopenharmony_ci EXPECT_EQ(rt, 0); 871f6603c60Sopenharmony_ci FAIL() << "sched_setscheduler failed but policy has changed"; 872f6603c60Sopenharmony_ci } 873f6603c60Sopenharmony_ci} 874f6603c60Sopenharmony_ci 875f6603c60Sopenharmony_ci/** 876f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_SCHED_API_SETSCHEDULER_0600 877f6603c60Sopenharmony_ci * @tc.name sched_setscheduler api error test with invalid parameter. 878f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 879f6603c60Sopenharmony_ci */ 880f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testSchedSetSchedulerError0600, Function | MediumTest | Level1) 881f6603c60Sopenharmony_ci{ 882f6603c60Sopenharmony_ci struct sched_param param; 883f6603c60Sopenharmony_ci int rt = sched_getparam(0, ¶m); 884f6603c60Sopenharmony_ci ASSERT_EQ(rt, 0) << "get sched param failed, errno=" << errno; 885f6603c60Sopenharmony_ci 886f6603c60Sopenharmony_ci LOG("invalid param test:"); 887f6603c60Sopenharmony_ci rt = sched_setscheduler(0, SCHED_RR, NULL); 888f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 889f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL); 890f6603c60Sopenharmony_ci} 891f6603c60Sopenharmony_ci 892f6603c60Sopenharmony_ci/** 893f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_SCHED_API_SETSCHEDULER_0700 894f6603c60Sopenharmony_ci * @tc.name sched_setscheduler api error test with no permission. 895f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 896f6603c60Sopenharmony_ci */ 897f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testSchedSetSchedulerError0700, Function | MediumTest | Level1) 898f6603c60Sopenharmony_ci{ 899f6603c60Sopenharmony_ci struct sched_param param; 900f6603c60Sopenharmony_ci int rt = sched_getparam(0, ¶m); 901f6603c60Sopenharmony_ci ASSERT_EQ(rt, 0) << "get sched param failed, errno=" << errno; 902f6603c60Sopenharmony_ci 903f6603c60Sopenharmony_ci LOG("no permission test:"); 904f6603c60Sopenharmony_ci errno = 0; 905f6603c60Sopenharmony_ci rt = sched_setscheduler(INIT_PROCESS_ID, SCHED_RR, ¶m); 906f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 907f6603c60Sopenharmony_ci EXPECT_EQ(errno, EPERM); 908f6603c60Sopenharmony_ci errno = 0; 909f6603c60Sopenharmony_ci rt = sched_setscheduler(KERNEL_PROCESS_ID, SCHED_RR, ¶m); 910f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 911f6603c60Sopenharmony_ci EXPECT_EQ(errno, EPERM); 912f6603c60Sopenharmony_ci} 913f6603c60Sopenharmony_ci 914f6603c60Sopenharmony_ci/** 915f6603c60Sopenharmony_ci* @tc.number SUB_KERNEL_SCHED_API_GETRRINTERVAL_0100 916f6603c60Sopenharmony_ci* @tc.name sched_rr_get_interval api test 917f6603c60Sopenharmony_ci* @tc.desc [C- SOFTWARE -0200] 918f6603c60Sopenharmony_ci*/ 919f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testSchedRRGetInterval, Function | MediumTest | Level1) 920f6603c60Sopenharmony_ci{ 921f6603c60Sopenharmony_ci struct timespec time1 = {1, 0}; 922f6603c60Sopenharmony_ci int rt = sched_rr_get_interval(0, &time1); 923f6603c60Sopenharmony_ci printf("%llu, %u\n", time1.tv_sec, time1.tv_nsec); 924f6603c60Sopenharmony_ci EXPECT_EQ(rt, 0); 925f6603c60Sopenharmony_ci EXPECT_EQ(time1.tv_sec, 0); 926f6603c60Sopenharmony_ci EXPECT_GE (time1.tv_nsec, DEFAULT_RR_SCHED_INTERVAL); 927f6603c60Sopenharmony_ci 928f6603c60Sopenharmony_ci time1.tv_sec = 1; 929f6603c60Sopenharmony_ci time1.tv_nsec = 0; 930f6603c60Sopenharmony_ci rt = sched_rr_get_interval(getpid(), &time1); 931f6603c60Sopenharmony_ci printf("%llu, %u\n", time1.tv_sec, time1.tv_nsec); 932f6603c60Sopenharmony_ci EXPECT_EQ(rt, 0); 933f6603c60Sopenharmony_ci EXPECT_EQ(time1.tv_sec, 0); 934f6603c60Sopenharmony_ci EXPECT_GE(time1.tv_nsec, DEFAULT_RR_SCHED_INTERVAL); 935f6603c60Sopenharmony_ci} 936f6603c60Sopenharmony_ci 937f6603c60Sopenharmony_ci/** 938f6603c60Sopenharmony_ci* @tc.number SUB_KERNEL_SCHED_API_GETRRINTERVAL_0200 939f6603c60Sopenharmony_ci* @tc.name sched_rr_get_interval error test with invalid pid 940f6603c60Sopenharmony_ci* @tc.desc [C- SOFTWARE -0200] 941f6603c60Sopenharmony_ci*/ 942f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testSchedRRGetIntervalError0200, Function | MediumTest | Level1) 943f6603c60Sopenharmony_ci{ 944f6603c60Sopenharmony_ci struct timespec time1 = {0, 0}; 945f6603c60Sopenharmony_ci LOG("invalid 'pid' test:"); 946f6603c60Sopenharmony_ci errno = 0; 947f6603c60Sopenharmony_ci int rt = sched_rr_get_interval(-1, &time1); 948f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 949f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL); 950f6603c60Sopenharmony_ci errno = 0; 951f6603c60Sopenharmony_ci rt = sched_rr_get_interval(MAX_PROCESS_NUMBER + 1, &time1); 952f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 953f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL); 954f6603c60Sopenharmony_ci errno = 0; 955f6603c60Sopenharmony_ci rt = sched_rr_get_interval(MAX_PROCESS_NUMBER + GetRandom(1000), &time1); 956f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 957f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL); 958f6603c60Sopenharmony_ci} 959f6603c60Sopenharmony_ci 960f6603c60Sopenharmony_ci/** 961f6603c60Sopenharmony_ci* @tc.number SUB_KERNEL_SCHED_API_GETRRINTERVAL_0300 962f6603c60Sopenharmony_ci* @tc.name sched_rr_get_interval error test with not exist pid 963f6603c60Sopenharmony_ci* @tc.desc [C- SOFTWARE -0200] 964f6603c60Sopenharmony_ci*/ 965f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testSchedRRGetIntervalError0300, Function | MediumTest | Level1) 966f6603c60Sopenharmony_ci{ 967f6603c60Sopenharmony_ci struct timespec time1 = {0, 0}; 968f6603c60Sopenharmony_ci int rt; 969f6603c60Sopenharmony_ci 970f6603c60Sopenharmony_ci LOG("not exist pid test:"); 971f6603c60Sopenharmony_ci pid_t nonExitPid = GetNonExistPid(); // valid but not exist pid 972f6603c60Sopenharmony_ci if (nonExitPid != -1) { 973f6603c60Sopenharmony_ci rt = sched_rr_get_interval(nonExitPid, &time1); 974f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 975f6603c60Sopenharmony_ci EXPECT_EQ(errno, ESRCH); 976f6603c60Sopenharmony_ci } 977f6603c60Sopenharmony_ci 978f6603c60Sopenharmony_ci LOG("invalid 'timespec' test:"); 979f6603c60Sopenharmony_ci rt = sched_rr_get_interval(0, NULL); 980f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 981f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL); 982f6603c60Sopenharmony_ci} 983f6603c60Sopenharmony_ci 984f6603c60Sopenharmony_ci/** 985f6603c60Sopenharmony_ci* @tc.number SUB_KERNEL_SCHED_API_GETRRINTERVAL_0400 986f6603c60Sopenharmony_ci* @tc.name sched_rr_get_interval error test with invalid timespec 987f6603c60Sopenharmony_ci* @tc.desc [C- SOFTWARE -0200] 988f6603c60Sopenharmony_ci*/ 989f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testSchedRRGetIntervalError0400, Function | MediumTest | Level1) 990f6603c60Sopenharmony_ci{ 991f6603c60Sopenharmony_ci int rt; 992f6603c60Sopenharmony_ci 993f6603c60Sopenharmony_ci LOG("invalid 'timespec' test:"); 994f6603c60Sopenharmony_ci rt = sched_rr_get_interval(0, NULL); 995f6603c60Sopenharmony_ci EXPECT_EQ(rt, -1); 996f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL); 997f6603c60Sopenharmony_ci} 998f6603c60Sopenharmony_ci 999f6603c60Sopenharmony_ci/** 1000f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_SCHED_API_YIELD_0100 1001f6603c60Sopenharmony_ci * @tc.name test sched_yield with two process 1002f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 1003f6603c60Sopenharmony_ci */ 1004f6603c60Sopenharmony_ciHWTEST_F(ProcessSchedApiTest, testSchedYieldBasic, Function | MediumTest | Level1) 1005f6603c60Sopenharmony_ci{ 1006f6603c60Sopenharmony_ci int ret; 1007f6603c60Sopenharmony_ci int cpuCnt = 0; 1008f6603c60Sopenharmony_ci cpu_set_t oldMask; 1009f6603c60Sopenharmony_ci CPU_ZERO(&oldMask); 1010f6603c60Sopenharmony_ci cpuCnt = GetCpuCount(); 1011f6603c60Sopenharmony_ci LOG("GetCpuCount=%d", cpuCnt); 1012f6603c60Sopenharmony_ci if (cpuCnt > 1) { 1013f6603c60Sopenharmony_ci ret = FixCurProcessToOneCpu(0, &oldMask); 1014f6603c60Sopenharmony_ci ASSERT_EQ(ret, 0); 1015f6603c60Sopenharmony_ci } 1016f6603c60Sopenharmony_ci 1017f6603c60Sopenharmony_ci ASSERT_EQ(InitPipe(), 0) << "> parent: pipe errno = " << errno; 1018f6603c60Sopenharmony_ci EXPECT_NE(InitGlobalVariable(), -1); 1019f6603c60Sopenharmony_ci pid_t pid = fork(); 1020f6603c60Sopenharmony_ci ASSERT_TRUE(pid >= 0) << "> parent: fork errno = " << errno; 1021f6603c60Sopenharmony_ci if (pid == 0) { 1022f6603c60Sopenharmony_ci if (cpuCnt > 1) { 1023f6603c60Sopenharmony_ci ret = FixCurProcessToOneCpu(0, &oldMask); 1024f6603c60Sopenharmony_ci EXPECT_EQ(ret, 0); 1025f6603c60Sopenharmony_ci } 1026f6603c60Sopenharmony_ci // control step 2 1027f6603c60Sopenharmony_ci BlockOnPipe(); 1028f6603c60Sopenharmony_ci SetGlobalVariable(1); 1029f6603c60Sopenharmony_ci LOG("222"); 1030f6603c60Sopenharmony_ci exit(0); 1031f6603c60Sopenharmony_ci } 1032f6603c60Sopenharmony_ci Msleep(50); 1033f6603c60Sopenharmony_ci // control step 1 1034f6603c60Sopenharmony_ci UnBlockPipe(); 1035f6603c60Sopenharmony_ci LOG("111"); 1036f6603c60Sopenharmony_ci EXPECT_EQ(sched_yield(), 0) << "> sched_yield error"; 1037f6603c60Sopenharmony_ci // control step 3 1038f6603c60Sopenharmony_ci EXPECT_NE(SetGlobalVariable(2), -1); 1039f6603c60Sopenharmony_ci LOG("333"); 1040f6603c60Sopenharmony_ci // control step 4 1041f6603c60Sopenharmony_ci WaitProcExitedOK(pid); 1042f6603c60Sopenharmony_ci EXPECT_EQ(GetGlobalVariable(), 2); 1043f6603c60Sopenharmony_ci EXPECT_NE(DeleteGlobalVariable(), -1); 1044f6603c60Sopenharmony_ci if (cpuCnt > 1) { 1045f6603c60Sopenharmony_ci ret = sched_setaffinity(0, sizeof(oldMask), &oldMask); 1046f6603c60Sopenharmony_ci ASSERT_EQ(ret, 0); 1047f6603c60Sopenharmony_ci } 1048f6603c60Sopenharmony_ci} 1049