adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <sys/stat.h> |
| 6 | #include <sys/types.h> |
| 7 | #include <unistd.h> |
| 8 | #include <set> |
| 9 | #include <string> |
| 10 | #include <vector> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 11 | #include <gtest/gtest.h> |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 12 | #include "base/string_util.h" |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 13 | #include "update_engine/filesystem_iterator.h" |
| 14 | #include "update_engine/test_utils.h" |
| 15 | #include "update_engine/utils.h" |
| 16 | |
| 17 | using std::set; |
| 18 | using std::string; |
| 19 | using std::vector; |
| 20 | |
| 21 | namespace chromeos_update_engine { |
| 22 | |
| 23 | namespace { |
| 24 | const char* TestDir() { return "./FilesystemIteratorTest-dir"; } |
| 25 | } // namespace {} |
| 26 | |
| 27 | class FilesystemIteratorTest : public ::testing::Test { |
| 28 | protected: |
| 29 | virtual void SetUp() { |
| 30 | LOG(INFO) << "SetUp() mkdir"; |
| 31 | EXPECT_EQ(0, System(StringPrintf("rm -rf %s", TestDir()))); |
| 32 | EXPECT_EQ(0, System(StringPrintf("mkdir -p %s", TestDir()))); |
| 33 | } |
| 34 | virtual void TearDown() { |
| 35 | LOG(INFO) << "TearDown() rmdir"; |
| 36 | EXPECT_EQ(0, System(StringPrintf("rm -rf %s", TestDir()))); |
| 37 | } |
| 38 | }; |
| 39 | |
| 40 | TEST_F(FilesystemIteratorTest, RunAsRootSuccessTest) { |
| 41 | ASSERT_EQ(0, getuid()); |
| 42 | string first_image("FilesystemIteratorTest.image1"); |
| 43 | string sub_image("FilesystemIteratorTest.image2"); |
| 44 | |
| 45 | ASSERT_EQ(0, System(string("rm -f ") + first_image + " " + sub_image)); |
| 46 | vector<string> expected_paths_vector; |
| 47 | CreateExtImageAtPath(first_image, &expected_paths_vector); |
| 48 | CreateExtImageAtPath(sub_image, NULL); |
| 49 | ASSERT_EQ(0, System(string("mount -o loop ") + first_image + " " + |
| 50 | kMountPath)); |
| 51 | ASSERT_EQ(0, System(string("mount -o loop ") + sub_image + " " + |
| 52 | kMountPath + "/some_dir/mnt")); |
| 53 | for (vector<string>::iterator it = expected_paths_vector.begin(); |
| 54 | it != expected_paths_vector.end(); ++it) |
| 55 | *it = kMountPath + *it; |
| 56 | set<string> expected_paths(expected_paths_vector.begin(), |
| 57 | expected_paths_vector.end()); |
| 58 | VerifyAllPaths(kMountPath, expected_paths); |
| 59 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 60 | EXPECT_EQ(0, System(string("umount -d ") + kMountPath + "/some_dir/mnt")); |
| 61 | EXPECT_EQ(0, System(string("umount -d ") + kMountPath)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 62 | EXPECT_EQ(0, System(string("rm -f ") + first_image + " " + sub_image)); |
| 63 | } |
| 64 | |
| 65 | TEST_F(FilesystemIteratorTest, NegativeTest) { |
| 66 | { |
| 67 | FilesystemIterator iter("/non/existent/path", set<string>()); |
| 68 | EXPECT_TRUE(iter.IsEnd()); |
| 69 | EXPECT_TRUE(iter.IsErr()); |
| 70 | } |
| 71 | |
| 72 | { |
| 73 | FilesystemIterator iter(TestDir(), set<string>()); |
| 74 | EXPECT_FALSE(iter.IsEnd()); |
| 75 | EXPECT_FALSE(iter.IsErr()); |
| 76 | // Here I'm deleting the exact directory that iterator is point at, |
| 77 | // then incrementing (which normally would descend into that directory). |
| 78 | EXPECT_EQ(0, rmdir(TestDir())); |
| 79 | iter.Increment(); |
| 80 | EXPECT_TRUE(iter.IsEnd()); |
| 81 | EXPECT_FALSE(iter.IsErr()); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | TEST_F(FilesystemIteratorTest, DeleteWhileTraverseTest) { |
| 86 | ASSERT_EQ(0, mkdir("DeleteWhileTraverseTest", 0755)); |
| 87 | ASSERT_EQ(0, mkdir("DeleteWhileTraverseTest/a", 0755)); |
| 88 | ASSERT_EQ(0, mkdir("DeleteWhileTraverseTest/a/b", 0755)); |
| 89 | ASSERT_EQ(0, mkdir("DeleteWhileTraverseTest/b", 0755)); |
| 90 | ASSERT_EQ(0, mkdir("DeleteWhileTraverseTest/c", 0755)); |
| 91 | |
| 92 | string expected_paths_arr[] = { |
| 93 | "", |
| 94 | "/a", |
| 95 | "/b", |
| 96 | "/c" |
| 97 | }; |
| 98 | set<string> expected_paths(expected_paths_arr, |
| 99 | expected_paths_arr + |
| 100 | arraysize(expected_paths_arr)); |
| 101 | |
| 102 | FilesystemIterator iter("DeleteWhileTraverseTest", set<string>()); |
| 103 | while (!iter.IsEnd()) { |
| 104 | string path = iter.GetPartialPath(); |
| 105 | EXPECT_TRUE(expected_paths.find(path) != expected_paths.end()); |
| 106 | if (expected_paths.find(path) != expected_paths.end()) { |
| 107 | expected_paths.erase(path); |
| 108 | } |
| 109 | if (path == "/a") { |
| 110 | EXPECT_EQ(0, rmdir("DeleteWhileTraverseTest/a/b")); |
| 111 | EXPECT_EQ(0, rmdir("DeleteWhileTraverseTest/a")); |
| 112 | } |
| 113 | iter.Increment(); |
| 114 | } |
| 115 | EXPECT_FALSE(iter.IsErr()); |
| 116 | EXPECT_TRUE(expected_paths.empty()); |
| 117 | EXPECT_EQ(0, system("rm -rf DeleteWhileTraverseTest")); |
| 118 | } |
| 119 | |
| 120 | } // namespace chromeos_update_engine |