gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/syscalls/linux/sysinfo.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  // This is a very simple sanity test to validate that the sysinfo syscall is
    16  // supported by gvisor and returns sane values.
    17  #include <sys/syscall.h>
    18  #include <sys/sysinfo.h>
    19  #include <sys/types.h>
    20  #include <unistd.h>
    21  
    22  #include "gtest/gtest.h"
    23  #include "absl/time/clock.h"
    24  #include "absl/time/time.h"
    25  #include "test/util/test_util.h"
    26  
    27  namespace gvisor {
    28  namespace testing {
    29  
    30  namespace {
    31  
    32  TEST(SysinfoTest, SysinfoIsCallable) {
    33    struct sysinfo ignored = {};
    34    EXPECT_THAT(syscall(SYS_sysinfo, &ignored), SyscallSucceedsWithValue(0));
    35  }
    36  
    37  TEST(SysinfoTest, EfaultProducedOnBadAddress) {
    38    // Validate that we return EFAULT when a bad address is provided.
    39    // specified by man 2 sysinfo
    40    EXPECT_THAT(syscall(SYS_sysinfo, nullptr), SyscallFailsWithErrno(EFAULT));
    41  }
    42  
    43  TEST(SysinfoTest, TotalRamSaneValue) {
    44    struct sysinfo s = {};
    45    EXPECT_THAT(sysinfo(&s), SyscallSucceedsWithValue(0));
    46    EXPECT_GT(s.totalram, 0);
    47  }
    48  
    49  TEST(SysinfoTest, MemunitSet) {
    50    struct sysinfo s = {};
    51    EXPECT_THAT(sysinfo(&s), SyscallSucceedsWithValue(0));
    52    EXPECT_GE(s.mem_unit, 1);
    53  }
    54  
    55  TEST(SysinfoTest, UptimeSaneValue) {
    56    struct sysinfo s = {};
    57    EXPECT_THAT(sysinfo(&s), SyscallSucceedsWithValue(0));
    58    EXPECT_GE(s.uptime, 0);
    59  }
    60  
    61  TEST(SysinfoTest, UptimeIncreasingValue) {
    62    struct sysinfo s = {};
    63    EXPECT_THAT(sysinfo(&s), SyscallSucceedsWithValue(0));
    64    absl::SleepFor(absl::Seconds(2));
    65    struct sysinfo s2 = {};
    66    EXPECT_THAT(sysinfo(&s2), SyscallSucceedsWithValue(0));
    67    EXPECT_LT(s.uptime, s2.uptime);
    68  }
    69  
    70  TEST(SysinfoTest, FreeRamSaneValue) {
    71    struct sysinfo s = {};
    72    EXPECT_THAT(sysinfo(&s), SyscallSucceedsWithValue(0));
    73    EXPECT_GT(s.freeram, 0);
    74    EXPECT_LT(s.freeram, s.totalram);
    75  }
    76  
    77  TEST(SysinfoTest, NumProcsSaneValue) {
    78    struct sysinfo s = {};
    79    EXPECT_THAT(sysinfo(&s), SyscallSucceedsWithValue(0));
    80    EXPECT_GT(s.procs, 0);
    81  }
    82  
    83  }  // namespace
    84  
    85  }  // namespace testing
    86  }  // namespace gvisor