summaryrefslogtreecommitdiff
path: root/filesystem_iterator_unittest.cc
blob: 82b8d3f4fe4b561bad8f663772d4aab624cda01b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <set>
#include <string>
#include <vector>
#include "base/string_util.h"
#include <gtest/gtest.h>
#include "update_engine/filesystem_iterator.h"
#include "update_engine/test_utils.h"
#include "update_engine/utils.h"

using std::set;
using std::string;
using std::vector;

namespace chromeos_update_engine {

namespace {
const char* TestDir() { return "./FilesystemIteratorTest-dir"; }
}  // namespace {}

class FilesystemIteratorTest : public ::testing::Test {
 protected:
  virtual void SetUp() {
    LOG(INFO) << "SetUp() mkdir";
    EXPECT_EQ(0, System(StringPrintf("rm -rf %s", TestDir())));
    EXPECT_EQ(0, System(StringPrintf("mkdir -p %s", TestDir())));
  }
  virtual void TearDown() {
    LOG(INFO) << "TearDown() rmdir";
    EXPECT_EQ(0, System(StringPrintf("rm -rf %s", TestDir())));
  }
};

TEST_F(FilesystemIteratorTest, RunAsRootSuccessTest) {
  ASSERT_EQ(0, getuid());
  string first_image("FilesystemIteratorTest.image1");
  string sub_image("FilesystemIteratorTest.image2");

  ASSERT_EQ(0, System(string("rm -f ") + first_image + " " + sub_image));
  vector<string> expected_paths_vector;
  CreateExtImageAtPath(first_image, &expected_paths_vector);
  CreateExtImageAtPath(sub_image, NULL);
  ASSERT_EQ(0, System(string("mount -o loop ") + first_image + " " +
                      kMountPath));
  ASSERT_EQ(0, System(string("mount -o loop ") + sub_image + " " +
                      kMountPath + "/some_dir/mnt"));
  for (vector<string>::iterator it = expected_paths_vector.begin();
       it != expected_paths_vector.end(); ++it)
    *it = kMountPath + *it;
  set<string> expected_paths(expected_paths_vector.begin(),
                             expected_paths_vector.end());
  VerifyAllPaths(kMountPath, expected_paths);
  
  EXPECT_EQ(0, System(string("umount ") + kMountPath + "/some_dir/mnt"));
  EXPECT_EQ(0, System(string("umount ") + kMountPath));
  EXPECT_EQ(0, System(string("rm -f ") + first_image + " " + sub_image));
}

TEST_F(FilesystemIteratorTest, NegativeTest) {
  {
    FilesystemIterator iter("/non/existent/path", set<string>());
    EXPECT_TRUE(iter.IsEnd());
    EXPECT_TRUE(iter.IsErr());
  }

  {
    FilesystemIterator iter(TestDir(), set<string>());
    EXPECT_FALSE(iter.IsEnd());
    EXPECT_FALSE(iter.IsErr());
    // Here I'm deleting the exact directory that iterator is point at,
    // then incrementing (which normally would descend into that directory).
    EXPECT_EQ(0, rmdir(TestDir()));
    iter.Increment();
    EXPECT_TRUE(iter.IsEnd());
    EXPECT_FALSE(iter.IsErr());
  }
}

TEST_F(FilesystemIteratorTest, DeleteWhileTraverseTest) {
  ASSERT_EQ(0, mkdir("DeleteWhileTraverseTest", 0755));
  ASSERT_EQ(0, mkdir("DeleteWhileTraverseTest/a", 0755));
  ASSERT_EQ(0, mkdir("DeleteWhileTraverseTest/a/b", 0755));
  ASSERT_EQ(0, mkdir("DeleteWhileTraverseTest/b", 0755));
  ASSERT_EQ(0, mkdir("DeleteWhileTraverseTest/c", 0755));

  string expected_paths_arr[] = {
    "",
    "/a",
    "/b",
    "/c"
  };
  set<string> expected_paths(expected_paths_arr,
                             expected_paths_arr +
                             arraysize(expected_paths_arr));

  FilesystemIterator iter("DeleteWhileTraverseTest", set<string>());
  while (!iter.IsEnd()) {
    string path = iter.GetPartialPath();
    EXPECT_TRUE(expected_paths.find(path) != expected_paths.end());
    if (expected_paths.find(path) != expected_paths.end()) {
      expected_paths.erase(path);
    }
    if (path == "/a") {
      EXPECT_EQ(0, rmdir("DeleteWhileTraverseTest/a/b"));
      EXPECT_EQ(0, rmdir("DeleteWhileTraverseTest/a"));
    }
    iter.Increment();
  }
  EXPECT_FALSE(iter.IsErr());
  EXPECT_TRUE(expected_paths.empty());
  EXPECT_EQ(0, system("rm -rf DeleteWhileTraverseTest"));
}

}  // namespace chromeos_update_engine