gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/util/fs_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/fs_util.h"
    16  
    17  #include <errno.h>
    18  
    19  #include <vector>
    20  
    21  #include "gmock/gmock.h"
    22  #include "gtest/gtest.h"
    23  #include "test/util/posix_error.h"
    24  #include "test/util/temp_path.h"
    25  #include "test/util/test_util.h"
    26  
    27  namespace gvisor {
    28  namespace testing {
    29  
    30  namespace {
    31  
    32  TEST(FsUtilTest, RecursivelyCreateDirManualDelete) {
    33    const TempPath root = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
    34    const std::string base_path =
    35        JoinPath(root.path(), "/a/b/c/d/e/f/g/h/i/j/k/l/m");
    36  
    37    ASSERT_THAT(Exists(base_path), IsPosixErrorOkAndHolds(false));
    38    ASSERT_NO_ERRNO(RecursivelyCreateDir(base_path));
    39  
    40    // Delete everything until we hit root and then stop, we want to try this
    41    // without using RecursivelyDelete.
    42    std::string cur_path = base_path;
    43    while (cur_path != root.path()) {
    44      ASSERT_THAT(Exists(cur_path), IsPosixErrorOkAndHolds(true));
    45      ASSERT_NO_ERRNO(Rmdir(cur_path));
    46      ASSERT_THAT(Exists(cur_path), IsPosixErrorOkAndHolds(false));
    47      auto dir = Dirname(cur_path);
    48      cur_path = std::string(dir);
    49    }
    50  }
    51  
    52  TEST(FsUtilTest, RecursivelyCreateAndDeleteDir) {
    53    const TempPath root = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
    54    const std::string base_path =
    55        JoinPath(root.path(), "/a/b/c/d/e/f/g/h/i/j/k/l/m");
    56  
    57    ASSERT_THAT(Exists(base_path), IsPosixErrorOkAndHolds(false));
    58    ASSERT_NO_ERRNO(RecursivelyCreateDir(base_path));
    59  
    60    const std::string sub_path = JoinPath(root.path(), "a");
    61    ASSERT_NO_ERRNO(RecursivelyDelete(sub_path, nullptr, nullptr));
    62    ASSERT_THAT(Exists(sub_path), IsPosixErrorOkAndHolds(false));
    63  }
    64  
    65  TEST(FsUtilTest, RecursivelyCreateAndDeletePartial) {
    66    const TempPath root = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
    67    const std::string base_path =
    68        JoinPath(root.path(), "/a/b/c/d/e/f/g/h/i/j/k/l/m");
    69  
    70    ASSERT_THAT(Exists(base_path), IsPosixErrorOkAndHolds(false));
    71    ASSERT_NO_ERRNO(RecursivelyCreateDir(base_path));
    72  
    73    const std::string a = JoinPath(root.path(), "a");
    74    auto listing = ASSERT_NO_ERRNO_AND_VALUE(ListDir(a, true));
    75    ASSERT_THAT(listing, ::testing::Contains("b"));
    76    ASSERT_EQ(listing.size(), 1);
    77  
    78    listing = ASSERT_NO_ERRNO_AND_VALUE(ListDir(a, false));
    79    ASSERT_THAT(listing, ::testing::Contains("."));
    80    ASSERT_THAT(listing, ::testing::Contains(".."));
    81    ASSERT_THAT(listing, ::testing::Contains("b"));
    82    ASSERT_EQ(listing.size(), 3);
    83  
    84    const std::string sub_path = JoinPath(root.path(), "/a/b/c/d/e/f");
    85  
    86    ASSERT_NO_ERRNO(
    87        CreateWithContents(JoinPath(Dirname(sub_path), "file"), "Hello World"));
    88    std::string contents = "";
    89    ASSERT_NO_ERRNO(GetContents(JoinPath(Dirname(sub_path), "file"), &contents));
    90    ASSERT_EQ(contents, "Hello World");
    91  
    92    ASSERT_NO_ERRNO(RecursivelyDelete(sub_path, nullptr, nullptr));
    93    ASSERT_THAT(Exists(sub_path), IsPosixErrorOkAndHolds(false));
    94  
    95    // The parent of the subpath (directory e) should still exist.
    96    ASSERT_THAT(Exists(Dirname(sub_path)), IsPosixErrorOkAndHolds(true));
    97  
    98    // The file we created along side f should also still exist.
    99    ASSERT_THAT(Exists(JoinPath(Dirname(sub_path), "file")),
   100                IsPosixErrorOkAndHolds(true));
   101  }
   102  }  // namespace
   103  
   104  }  // namespace testing
   105  }  // namespace gvisor