github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/syscalls/linux/sched.cc (about)

     1  // Copyright 2018 The gVisor Authors.
     2  //
     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  #include <errno.h>
    16  #include <sched.h>
    17  
    18  #include "gtest/gtest.h"
    19  #include "test/util/test_util.h"
    20  
    21  namespace gvisor {
    22  namespace testing {
    23  
    24  namespace {
    25  
    26  // In linux, pid is limited to 29 bits because how futex is implemented.
    27  constexpr int kImpossiblePID = (1 << 29) + 1;
    28  
    29  TEST(SchedGetparamTest, ReturnsZero) {
    30    struct sched_param param;
    31    EXPECT_THAT(sched_getparam(getpid(), &param), SyscallSucceeds());
    32    EXPECT_EQ(param.sched_priority, 0);
    33    EXPECT_THAT(sched_getparam(/*pid=*/0, &param), SyscallSucceeds());
    34    EXPECT_EQ(param.sched_priority, 0);
    35  }
    36  
    37  TEST(SchedGetparamTest, InvalidPIDReturnsEINVAL) {
    38    struct sched_param param;
    39    EXPECT_THAT(sched_getparam(/*pid=*/-1, &param),
    40                SyscallFailsWithErrno(EINVAL));
    41  }
    42  
    43  TEST(SchedGetparamTest, ImpossiblePIDReturnsESRCH) {
    44    struct sched_param param;
    45    EXPECT_THAT(sched_getparam(kImpossiblePID, &param),
    46                SyscallFailsWithErrno(ESRCH));
    47  }
    48  
    49  TEST(SchedGetparamTest, NullParamReturnsEINVAL) {
    50    EXPECT_THAT(sched_getparam(0, nullptr), SyscallFailsWithErrno(EINVAL));
    51  }
    52  
    53  TEST(SchedGetschedulerTest, ReturnsSchedOther) {
    54    EXPECT_THAT(sched_getscheduler(getpid()),
    55                SyscallSucceedsWithValue(SCHED_OTHER));
    56    EXPECT_THAT(sched_getscheduler(/*pid=*/0),
    57                SyscallSucceedsWithValue(SCHED_OTHER));
    58  }
    59  
    60  TEST(SchedGetschedulerTest, ReturnsEINVAL) {
    61    EXPECT_THAT(sched_getscheduler(/*pid=*/-1), SyscallFailsWithErrno(EINVAL));
    62  }
    63  
    64  TEST(SchedGetschedulerTest, ReturnsESRCH) {
    65    EXPECT_THAT(sched_getscheduler(kImpossiblePID), SyscallFailsWithErrno(ESRCH));
    66  }
    67  
    68  }  // namespace
    69  
    70  }  // namespace testing
    71  }  // namespace gvisor