gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/util/proc_util_test.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 "test/util/proc_util.h"
    16  
    17  #include "gmock/gmock.h"
    18  #include "gtest/gtest.h"
    19  #include "test/util/test_util.h"
    20  
    21  using ::testing::ElementsAreArray;
    22  using ::testing::IsEmpty;
    23  using ::testing::Optional;
    24  
    25  namespace gvisor {
    26  namespace testing {
    27  
    28  namespace {
    29  
    30  TEST(ParseProcMapsLineTest, WithoutFilename) {
    31    auto entry = ASSERT_NO_ERRNO_AND_VALUE(
    32        ParseProcMapsLine("2ab4f00b7000-2ab4f00b9000 r-xp 00000000 00:00 0 "));
    33    EXPECT_EQ(entry.start, 0x2ab4f00b7000);
    34    EXPECT_EQ(entry.end, 0x2ab4f00b9000);
    35    EXPECT_TRUE(entry.readable);
    36    EXPECT_FALSE(entry.writable);
    37    EXPECT_TRUE(entry.executable);
    38    EXPECT_TRUE(entry.priv);
    39    EXPECT_EQ(entry.offset, 0);
    40    EXPECT_EQ(entry.major, 0);
    41    EXPECT_EQ(entry.minor, 0);
    42    EXPECT_EQ(entry.inode, 0);
    43    EXPECT_THAT(entry.filename, IsEmpty());
    44  }
    45  
    46  TEST(ParseProcMapsLineTest, WithFilename) {
    47    auto entry = ASSERT_NO_ERRNO_AND_VALUE(
    48        ParseProcMapsLine("00407000-00408000 rw-p 00006000 00:0e 10              "
    49                          "                   /bin/cat"));
    50    EXPECT_EQ(entry.start, 0x407000);
    51    EXPECT_EQ(entry.end, 0x408000);
    52    EXPECT_TRUE(entry.readable);
    53    EXPECT_TRUE(entry.writable);
    54    EXPECT_FALSE(entry.executable);
    55    EXPECT_TRUE(entry.priv);
    56    EXPECT_EQ(entry.offset, 0x6000);
    57    EXPECT_EQ(entry.major, 0);
    58    EXPECT_EQ(entry.minor, 0x0e);
    59    EXPECT_EQ(entry.inode, 10);
    60    EXPECT_EQ(entry.filename, "/bin/cat");
    61  }
    62  
    63  TEST(ParseProcSmapsTest, Correctness) {
    64    auto entries = ASSERT_NO_ERRNO_AND_VALUE(
    65        ParseProcSmaps("0-10000 rw-s 00000000 00:00 0 "
    66                       "                   /dev/zero (deleted)\n"
    67                       "Size:                  0 kB\n"
    68                       "Rss:                   1 kB\n"
    69                       "Pss:                   2 kB\n"
    70                       "Shared_Clean:          3 kB\n"
    71                       "Shared_Dirty:          4 kB\n"
    72                       "Private_Clean:         5 kB\n"
    73                       "Private_Dirty:         6 kB\n"
    74                       "Referenced:            7 kB\n"
    75                       "Anonymous:             8 kB\n"
    76                       "AnonHugePages:         9 kB\n"
    77                       "Shared_Hugetlb:       10 kB\n"
    78                       "Private_Hugetlb:      11 kB\n"
    79                       "Swap:                 12 kB\n"
    80                       "SwapPss:              13 kB\n"
    81                       "KernelPageSize:       14 kB\n"
    82                       "MMUPageSize:          15 kB\n"
    83                       "Locked:               16 kB\n"
    84                       "FutureUnknownKey:     17 kB\n"
    85                       "VmFlags: rd wr sh mr mw me ms lo ?? sd \n"));
    86    ASSERT_EQ(entries.size(), 1);
    87    auto& entry = entries[0];
    88    EXPECT_EQ(entry.maps_entry.filename, "/dev/zero (deleted)");
    89    EXPECT_EQ(entry.size_kb, 0);
    90    EXPECT_EQ(entry.rss_kb, 1);
    91    EXPECT_THAT(entry.pss_kb, Optional(2));
    92    EXPECT_EQ(entry.shared_clean_kb, 3);
    93    EXPECT_EQ(entry.shared_dirty_kb, 4);
    94    EXPECT_EQ(entry.private_clean_kb, 5);
    95    EXPECT_EQ(entry.private_dirty_kb, 6);
    96    EXPECT_THAT(entry.referenced_kb, Optional(7));
    97    EXPECT_THAT(entry.anonymous_kb, Optional(8));
    98    EXPECT_THAT(entry.anon_huge_pages_kb, Optional(9));
    99    EXPECT_THAT(entry.shared_hugetlb_kb, Optional(10));
   100    EXPECT_THAT(entry.private_hugetlb_kb, Optional(11));
   101    EXPECT_THAT(entry.swap_kb, Optional(12));
   102    EXPECT_THAT(entry.swap_pss_kb, Optional(13));
   103    EXPECT_THAT(entry.kernel_page_size_kb, Optional(14));
   104    EXPECT_THAT(entry.mmu_page_size_kb, Optional(15));
   105    EXPECT_THAT(entry.locked_kb, Optional(16));
   106    EXPECT_THAT(entry.vm_flags,
   107                Optional(ElementsAreArray({"rd", "wr", "sh", "mr", "mw", "me",
   108                                           "ms", "lo", "??", "sd"})));
   109  }
   110  
   111  TEST(ParseProcMapsLineTest, WithFilenameContainingSpaces) {
   112    auto entry = ASSERT_NO_ERRNO_AND_VALUE(
   113        ParseProcMapsLine("7f26b3b12000-7f26b3b13000 rw-s 00000000 00:05 1432484 "
   114                          "                   /dev/zero (deleted)"));
   115    EXPECT_EQ(entry.start, 0x7f26b3b12000);
   116    EXPECT_EQ(entry.end, 0x7f26b3b13000);
   117    EXPECT_TRUE(entry.readable);
   118    EXPECT_TRUE(entry.writable);
   119    EXPECT_FALSE(entry.executable);
   120    EXPECT_FALSE(entry.priv);
   121    EXPECT_EQ(entry.offset, 0);
   122    EXPECT_EQ(entry.major, 0);
   123    EXPECT_EQ(entry.minor, 0x05);
   124    EXPECT_EQ(entry.inode, 1432484);
   125    EXPECT_EQ(entry.filename, "/dev/zero (deleted)");
   126  }
   127  
   128  }  // namespace
   129  
   130  }  // namespace testing
   131  }  // namespace gvisor