github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/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::IsEmpty;
    22  
    23  namespace gvisor {
    24  namespace testing {
    25  
    26  namespace {
    27  
    28  TEST(ParseProcMapsLineTest, WithoutFilename) {
    29    auto entry = ASSERT_NO_ERRNO_AND_VALUE(
    30        ParseProcMapsLine("2ab4f00b7000-2ab4f00b9000 r-xp 00000000 00:00 0 "));
    31    EXPECT_EQ(entry.start, 0x2ab4f00b7000);
    32    EXPECT_EQ(entry.end, 0x2ab4f00b9000);
    33    EXPECT_TRUE(entry.readable);
    34    EXPECT_FALSE(entry.writable);
    35    EXPECT_TRUE(entry.executable);
    36    EXPECT_TRUE(entry.priv);
    37    EXPECT_EQ(entry.offset, 0);
    38    EXPECT_EQ(entry.major, 0);
    39    EXPECT_EQ(entry.minor, 0);
    40    EXPECT_EQ(entry.inode, 0);
    41    EXPECT_THAT(entry.filename, IsEmpty());
    42  }
    43  
    44  TEST(ParseProcMapsLineTest, WithFilename) {
    45    auto entry = ASSERT_NO_ERRNO_AND_VALUE(
    46        ParseProcMapsLine("00407000-00408000 rw-p 00006000 00:0e 10              "
    47                          "                   /bin/cat"));
    48    EXPECT_EQ(entry.start, 0x407000);
    49    EXPECT_EQ(entry.end, 0x408000);
    50    EXPECT_TRUE(entry.readable);
    51    EXPECT_TRUE(entry.writable);
    52    EXPECT_FALSE(entry.executable);
    53    EXPECT_TRUE(entry.priv);
    54    EXPECT_EQ(entry.offset, 0x6000);
    55    EXPECT_EQ(entry.major, 0);
    56    EXPECT_EQ(entry.minor, 0x0e);
    57    EXPECT_EQ(entry.inode, 10);
    58    EXPECT_EQ(entry.filename, "/bin/cat");
    59  }
    60  
    61  TEST(ParseProcMapsLineTest, WithFilenameContainingSpaces) {
    62    auto entry = ASSERT_NO_ERRNO_AND_VALUE(
    63        ParseProcMapsLine("7f26b3b12000-7f26b3b13000 rw-s 00000000 00:05 1432484 "
    64                          "                   /dev/zero (deleted)"));
    65    EXPECT_EQ(entry.start, 0x7f26b3b12000);
    66    EXPECT_EQ(entry.end, 0x7f26b3b13000);
    67    EXPECT_TRUE(entry.readable);
    68    EXPECT_TRUE(entry.writable);
    69    EXPECT_FALSE(entry.executable);
    70    EXPECT_FALSE(entry.priv);
    71    EXPECT_EQ(entry.offset, 0);
    72    EXPECT_EQ(entry.major, 0);
    73    EXPECT_EQ(entry.minor, 0x05);
    74    EXPECT_EQ(entry.inode, 1432484);
    75    EXPECT_EQ(entry.filename, "/dev/zero (deleted)");
    76  }
    77  
    78  }  // namespace
    79  
    80  }  // namespace testing
    81  }  // namespace gvisor