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> |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 12 | #include "base/string_util.h" |
Mike Frysinger | 8155d08 | 2012-04-06 15:23:18 -0400 | [diff] [blame] | 13 | #include <base/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_; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 34 | EXPECT_EQ(0, System(StringPrintf("rm -rf %s", TestDir()))); |
| 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 | 3db938e | 2013-07-01 06:45:41 -0700 | [diff] [blame] | 47 | string first_image; |
| 48 | ASSERT_TRUE(utils::MakeTempFile("FilesystemIteratorTest.image1-XXXXXX", |
| 49 | &first_image, NULL)); |
| 50 | string sub_image; |
| 51 | ASSERT_TRUE(utils::MakeTempFile("FilesystemIteratorTest.image2-XXXXXX", |
| 52 | &sub_image, NULL)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 53 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 54 | vector<string> expected_paths_vector; |
| 55 | CreateExtImageAtPath(first_image, &expected_paths_vector); |
| 56 | CreateExtImageAtPath(sub_image, NULL); |
| 57 | ASSERT_EQ(0, System(string("mount -o loop ") + first_image + " " + |
| 58 | kMountPath)); |
| 59 | ASSERT_EQ(0, System(string("mount -o loop ") + sub_image + " " + |
| 60 | kMountPath + "/some_dir/mnt")); |
| 61 | for (vector<string>::iterator it = expected_paths_vector.begin(); |
| 62 | it != expected_paths_vector.end(); ++it) |
| 63 | *it = kMountPath + *it; |
| 64 | set<string> expected_paths(expected_paths_vector.begin(), |
| 65 | expected_paths_vector.end()); |
| 66 | VerifyAllPaths(kMountPath, expected_paths); |
Ben Chan | 77a1eba | 2012-10-07 22:54:55 -0700 | [diff] [blame] | 67 | |
| 68 | EXPECT_TRUE(utils::UnmountFilesystem(kMountPath + string("/some_dir/mnt"))); |
| 69 | EXPECT_TRUE(utils::UnmountFilesystem(kMountPath)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 70 | EXPECT_EQ(0, System(string("rm -f ") + first_image + " " + sub_image)); |
| 71 | } |
| 72 | |
| 73 | TEST_F(FilesystemIteratorTest, NegativeTest) { |
| 74 | { |
| 75 | FilesystemIterator iter("/non/existent/path", set<string>()); |
| 76 | EXPECT_TRUE(iter.IsEnd()); |
| 77 | EXPECT_TRUE(iter.IsErr()); |
| 78 | } |
| 79 | |
| 80 | { |
| 81 | FilesystemIterator iter(TestDir(), set<string>()); |
| 82 | EXPECT_FALSE(iter.IsEnd()); |
| 83 | EXPECT_FALSE(iter.IsErr()); |
| 84 | // Here I'm deleting the exact directory that iterator is point at, |
| 85 | // then incrementing (which normally would descend into that directory). |
| 86 | EXPECT_EQ(0, rmdir(TestDir())); |
| 87 | iter.Increment(); |
| 88 | EXPECT_TRUE(iter.IsEnd()); |
| 89 | EXPECT_FALSE(iter.IsErr()); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | TEST_F(FilesystemIteratorTest, DeleteWhileTraverseTest) { |
Gilad Arnold | 3db938e | 2013-07-01 06:45:41 -0700 | [diff] [blame] | 94 | const string dir_name = TestDir(); |
| 95 | ASSERT_EQ(0, chmod(dir_name.c_str(), 0755)); |
| 96 | const string sub_dir_name(dir_name + "/a"); |
| 97 | ASSERT_EQ(0, mkdir(sub_dir_name.c_str(), 0755)); |
| 98 | const string sub_sub_dir_name(sub_dir_name + "/b"); |
| 99 | ASSERT_EQ(0, mkdir(sub_sub_dir_name.c_str(), 0755)); |
| 100 | ASSERT_EQ(0, mkdir((dir_name + "/b").c_str(), 0755)); |
| 101 | ASSERT_EQ(0, mkdir((dir_name + "/c").c_str(), 0755)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 102 | |
| 103 | string expected_paths_arr[] = { |
| 104 | "", |
| 105 | "/a", |
| 106 | "/b", |
| 107 | "/c" |
| 108 | }; |
| 109 | set<string> expected_paths(expected_paths_arr, |
| 110 | expected_paths_arr + |
| 111 | arraysize(expected_paths_arr)); |
| 112 | |
Gilad Arnold | 3db938e | 2013-07-01 06:45:41 -0700 | [diff] [blame] | 113 | FilesystemIterator iter(dir_name, set<string>()); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 114 | while (!iter.IsEnd()) { |
| 115 | string path = iter.GetPartialPath(); |
| 116 | EXPECT_TRUE(expected_paths.find(path) != expected_paths.end()); |
| 117 | if (expected_paths.find(path) != expected_paths.end()) { |
| 118 | expected_paths.erase(path); |
| 119 | } |
| 120 | if (path == "/a") { |
Gilad Arnold | 3db938e | 2013-07-01 06:45:41 -0700 | [diff] [blame] | 121 | EXPECT_EQ(0, rmdir(sub_sub_dir_name.c_str())); |
| 122 | EXPECT_EQ(0, rmdir(sub_dir_name.c_str())); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 123 | } |
| 124 | iter.Increment(); |
| 125 | } |
| 126 | EXPECT_FALSE(iter.IsErr()); |
| 127 | EXPECT_TRUE(expected_paths.empty()); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | } // namespace chromeos_update_engine |