Mike Frysinger | 8155d08 | 2012-04-06 15:23:18 -0400 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 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> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 12 | #include <base/strings/string_util.h> |
| 13 | #include <base/strings/stringprintf.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 14 | #include "update_engine/filesystem_iterator.h" |
| 15 | #include "update_engine/test_utils.h" |
| 16 | #include "update_engine/utils.h" |
| 17 | |
| 18 | using std::set; |
| 19 | using std::string; |
| 20 | using std::vector; |
| 21 | |
| 22 | namespace chromeos_update_engine { |
| 23 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 24 | class FilesystemIteratorTest : public ::testing::Test { |
| 25 | protected: |
| 26 | virtual void SetUp() { |
Gilad Arnold | 3db938e | 2013-07-01 06:45:41 -0700 | [diff] [blame] | 27 | ASSERT_TRUE(utils::MakeTempDirectory("FilesystemIteratorTest-XXXXXX", |
| 28 | &test_dir_)); |
| 29 | LOG(INFO) << "SetUp() mkdir " << test_dir_; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 30 | } |
Gilad Arnold | 3db938e | 2013-07-01 06:45:41 -0700 | [diff] [blame] | 31 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 32 | virtual void TearDown() { |
Gilad Arnold | 3db938e | 2013-07-01 06:45:41 -0700 | [diff] [blame] | 33 | LOG(INFO) << "TearDown() rmdir " << test_dir_; |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 34 | EXPECT_EQ(0, System(base::StringPrintf("rm -rf %s", TestDir()))); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 35 | } |
Gilad Arnold | 3db938e | 2013-07-01 06:45:41 -0700 | [diff] [blame] | 36 | |
| 37 | const char* TestDir() { |
| 38 | return test_dir_.c_str(); |
| 39 | } |
| 40 | |
| 41 | private: |
| 42 | string test_dir_; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | TEST_F(FilesystemIteratorTest, RunAsRootSuccessTest) { |
| 46 | ASSERT_EQ(0, getuid()); |
Gilad Arnold | 61d9d2c | 2013-07-22 17:54:52 -0700 | [diff] [blame] | 47 | |
| 48 | // Create uniquely named main/sub images. |
| 49 | string main_image; |
Gilad Arnold | 3db938e | 2013-07-01 06:45:41 -0700 | [diff] [blame] | 50 | ASSERT_TRUE(utils::MakeTempFile("FilesystemIteratorTest.image1-XXXXXX", |
Gilad Arnold | 61d9d2c | 2013-07-22 17:54:52 -0700 | [diff] [blame] | 51 | &main_image, NULL)); |
| 52 | ScopedPathUnlinker main_image_unlinker(main_image); |
| 53 | |
Gilad Arnold | 3db938e | 2013-07-01 06:45:41 -0700 | [diff] [blame] | 54 | string sub_image; |
| 55 | ASSERT_TRUE(utils::MakeTempFile("FilesystemIteratorTest.image2-XXXXXX", |
| 56 | &sub_image, NULL)); |
Gilad Arnold | 61d9d2c | 2013-07-22 17:54:52 -0700 | [diff] [blame] | 57 | ScopedPathUnlinker sub_image_unlinker(sub_image); |
| 58 | |
| 59 | // Create uniqely named main/sub mount points. |
| 60 | string main_image_mount_point; |
| 61 | ASSERT_TRUE(utils::MakeTempDirectory( |
Gilad Arnold | a6742b3 | 2014-01-11 00:18:34 -0800 | [diff] [blame] | 62 | "FilesystemIteratorTest.mount-XXXXXX", |
Gilad Arnold | 61d9d2c | 2013-07-22 17:54:52 -0700 | [diff] [blame] | 63 | &main_image_mount_point)); |
| 64 | ScopedPathUnlinker main_image_mount_point_unlinker(main_image_mount_point); |
| 65 | const string sub_image_mount_point = main_image_mount_point + "/some_dir/mnt"; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 66 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 67 | vector<string> expected_paths_vector; |
Gilad Arnold | 61d9d2c | 2013-07-22 17:54:52 -0700 | [diff] [blame] | 68 | CreateExtImageAtPath(main_image, &expected_paths_vector); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 69 | CreateExtImageAtPath(sub_image, NULL); |
Gilad Arnold | 61d9d2c | 2013-07-22 17:54:52 -0700 | [diff] [blame] | 70 | ASSERT_EQ(0, System(string("mount -o loop ") + main_image + " " + |
| 71 | main_image_mount_point)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 72 | ASSERT_EQ(0, System(string("mount -o loop ") + sub_image + " " + |
Gilad Arnold | 61d9d2c | 2013-07-22 17:54:52 -0700 | [diff] [blame] | 73 | sub_image_mount_point)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 74 | for (vector<string>::iterator it = expected_paths_vector.begin(); |
| 75 | it != expected_paths_vector.end(); ++it) |
Gilad Arnold | 61d9d2c | 2013-07-22 17:54:52 -0700 | [diff] [blame] | 76 | *it = main_image_mount_point + *it; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 77 | set<string> expected_paths(expected_paths_vector.begin(), |
| 78 | expected_paths_vector.end()); |
Gilad Arnold | 61d9d2c | 2013-07-22 17:54:52 -0700 | [diff] [blame] | 79 | VerifyAllPaths(main_image_mount_point, expected_paths); |
Ben Chan | 77a1eba | 2012-10-07 22:54:55 -0700 | [diff] [blame] | 80 | |
Gilad Arnold | 61d9d2c | 2013-07-22 17:54:52 -0700 | [diff] [blame] | 81 | EXPECT_TRUE(utils::UnmountFilesystem(sub_image_mount_point)); |
| 82 | EXPECT_TRUE(utils::UnmountFilesystem(main_image_mount_point)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | TEST_F(FilesystemIteratorTest, NegativeTest) { |
| 86 | { |
| 87 | FilesystemIterator iter("/non/existent/path", set<string>()); |
| 88 | EXPECT_TRUE(iter.IsEnd()); |
| 89 | EXPECT_TRUE(iter.IsErr()); |
| 90 | } |
| 91 | |
| 92 | { |
| 93 | FilesystemIterator iter(TestDir(), set<string>()); |
| 94 | EXPECT_FALSE(iter.IsEnd()); |
| 95 | EXPECT_FALSE(iter.IsErr()); |
| 96 | // Here I'm deleting the exact directory that iterator is point at, |
| 97 | // then incrementing (which normally would descend into that directory). |
| 98 | EXPECT_EQ(0, rmdir(TestDir())); |
| 99 | iter.Increment(); |
| 100 | EXPECT_TRUE(iter.IsEnd()); |
| 101 | EXPECT_FALSE(iter.IsErr()); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | TEST_F(FilesystemIteratorTest, DeleteWhileTraverseTest) { |
Gilad Arnold | 3db938e | 2013-07-01 06:45:41 -0700 | [diff] [blame] | 106 | const string dir_name = TestDir(); |
| 107 | ASSERT_EQ(0, chmod(dir_name.c_str(), 0755)); |
| 108 | const string sub_dir_name(dir_name + "/a"); |
| 109 | ASSERT_EQ(0, mkdir(sub_dir_name.c_str(), 0755)); |
| 110 | const string sub_sub_dir_name(sub_dir_name + "/b"); |
| 111 | ASSERT_EQ(0, mkdir(sub_sub_dir_name.c_str(), 0755)); |
| 112 | ASSERT_EQ(0, mkdir((dir_name + "/b").c_str(), 0755)); |
| 113 | ASSERT_EQ(0, mkdir((dir_name + "/c").c_str(), 0755)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 114 | |
| 115 | string expected_paths_arr[] = { |
| 116 | "", |
| 117 | "/a", |
| 118 | "/b", |
| 119 | "/c" |
| 120 | }; |
| 121 | set<string> expected_paths(expected_paths_arr, |
| 122 | expected_paths_arr + |
| 123 | arraysize(expected_paths_arr)); |
| 124 | |
Gilad Arnold | 3db938e | 2013-07-01 06:45:41 -0700 | [diff] [blame] | 125 | FilesystemIterator iter(dir_name, set<string>()); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 126 | while (!iter.IsEnd()) { |
| 127 | string path = iter.GetPartialPath(); |
| 128 | EXPECT_TRUE(expected_paths.find(path) != expected_paths.end()); |
| 129 | if (expected_paths.find(path) != expected_paths.end()) { |
| 130 | expected_paths.erase(path); |
| 131 | } |
| 132 | if (path == "/a") { |
Gilad Arnold | 3db938e | 2013-07-01 06:45:41 -0700 | [diff] [blame] | 133 | EXPECT_EQ(0, rmdir(sub_sub_dir_name.c_str())); |
| 134 | EXPECT_EQ(0, rmdir(sub_dir_name.c_str())); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 135 | } |
| 136 | iter.Increment(); |
| 137 | } |
| 138 | EXPECT_FALSE(iter.IsErr()); |
| 139 | EXPECT_TRUE(expected_paths.empty()); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | } // namespace chromeos_update_engine |