blob: 83d699a6ae537b80beae2125a1d0fce74fffd988 [file] [log] [blame]
Mike Frysinger8155d082012-04-06 15:23:18 -04001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
adlr@google.com3defe6a2009-12-04 20:57:17 +00002// 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.com3defe6a2009-12-04 20:57:17 +000011#include <gtest/gtest.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070012#include <base/strings/string_util.h>
13#include <base/strings/stringprintf.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +000014#include "update_engine/filesystem_iterator.h"
15#include "update_engine/test_utils.h"
16#include "update_engine/utils.h"
17
18using std::set;
19using std::string;
20using std::vector;
21
22namespace chromeos_update_engine {
23
adlr@google.com3defe6a2009-12-04 20:57:17 +000024class FilesystemIteratorTest : public ::testing::Test {
25 protected:
26 virtual void SetUp() {
Gilad Arnold3db938e2013-07-01 06:45:41 -070027 ASSERT_TRUE(utils::MakeTempDirectory("FilesystemIteratorTest-XXXXXX",
28 &test_dir_));
29 LOG(INFO) << "SetUp() mkdir " << test_dir_;
adlr@google.com3defe6a2009-12-04 20:57:17 +000030 }
Gilad Arnold3db938e2013-07-01 06:45:41 -070031
adlr@google.com3defe6a2009-12-04 20:57:17 +000032 virtual void TearDown() {
Gilad Arnold3db938e2013-07-01 06:45:41 -070033 LOG(INFO) << "TearDown() rmdir " << test_dir_;
Alex Vakulenko75039d72014-03-25 12:36:28 -070034 EXPECT_EQ(0, System(base::StringPrintf("rm -rf %s", TestDir())));
adlr@google.com3defe6a2009-12-04 20:57:17 +000035 }
Gilad Arnold3db938e2013-07-01 06:45:41 -070036
37 const char* TestDir() {
38 return test_dir_.c_str();
39 }
40
41 private:
42 string test_dir_;
adlr@google.com3defe6a2009-12-04 20:57:17 +000043};
44
45TEST_F(FilesystemIteratorTest, RunAsRootSuccessTest) {
46 ASSERT_EQ(0, getuid());
Gilad Arnold61d9d2c2013-07-22 17:54:52 -070047
48 // Create uniquely named main/sub images.
49 string main_image;
Gilad Arnold3db938e2013-07-01 06:45:41 -070050 ASSERT_TRUE(utils::MakeTempFile("FilesystemIteratorTest.image1-XXXXXX",
Gilad Arnold61d9d2c2013-07-22 17:54:52 -070051 &main_image, NULL));
52 ScopedPathUnlinker main_image_unlinker(main_image);
53
Gilad Arnold3db938e2013-07-01 06:45:41 -070054 string sub_image;
55 ASSERT_TRUE(utils::MakeTempFile("FilesystemIteratorTest.image2-XXXXXX",
56 &sub_image, NULL));
Gilad Arnold61d9d2c2013-07-22 17:54:52 -070057 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 Arnolda6742b32014-01-11 00:18:34 -080062 "FilesystemIteratorTest.mount-XXXXXX",
Gilad Arnold61d9d2c2013-07-22 17:54:52 -070063 &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.com3defe6a2009-12-04 20:57:17 +000066
adlr@google.com3defe6a2009-12-04 20:57:17 +000067 vector<string> expected_paths_vector;
Gilad Arnold61d9d2c2013-07-22 17:54:52 -070068 CreateExtImageAtPath(main_image, &expected_paths_vector);
adlr@google.com3defe6a2009-12-04 20:57:17 +000069 CreateExtImageAtPath(sub_image, NULL);
Gilad Arnold61d9d2c2013-07-22 17:54:52 -070070 ASSERT_EQ(0, System(string("mount -o loop ") + main_image + " " +
71 main_image_mount_point));
adlr@google.com3defe6a2009-12-04 20:57:17 +000072 ASSERT_EQ(0, System(string("mount -o loop ") + sub_image + " " +
Gilad Arnold61d9d2c2013-07-22 17:54:52 -070073 sub_image_mount_point));
adlr@google.com3defe6a2009-12-04 20:57:17 +000074 for (vector<string>::iterator it = expected_paths_vector.begin();
75 it != expected_paths_vector.end(); ++it)
Gilad Arnold61d9d2c2013-07-22 17:54:52 -070076 *it = main_image_mount_point + *it;
adlr@google.com3defe6a2009-12-04 20:57:17 +000077 set<string> expected_paths(expected_paths_vector.begin(),
78 expected_paths_vector.end());
Gilad Arnold61d9d2c2013-07-22 17:54:52 -070079 VerifyAllPaths(main_image_mount_point, expected_paths);
Ben Chan77a1eba2012-10-07 22:54:55 -070080
Gilad Arnold61d9d2c2013-07-22 17:54:52 -070081 EXPECT_TRUE(utils::UnmountFilesystem(sub_image_mount_point));
82 EXPECT_TRUE(utils::UnmountFilesystem(main_image_mount_point));
adlr@google.com3defe6a2009-12-04 20:57:17 +000083}
84
85TEST_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
105TEST_F(FilesystemIteratorTest, DeleteWhileTraverseTest) {
Gilad Arnold3db938e2013-07-01 06:45:41 -0700106 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.com3defe6a2009-12-04 20:57:17 +0000114
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 Arnold3db938e2013-07-01 06:45:41 -0700125 FilesystemIterator iter(dir_name, set<string>());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000126 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 Arnold3db938e2013-07-01 06:45:41 -0700133 EXPECT_EQ(0, rmdir(sub_sub_dir_name.c_str()));
134 EXPECT_EQ(0, rmdir(sub_dir_name.c_str()));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000135 }
136 iter.Increment();
137 }
138 EXPECT_FALSE(iter.IsErr());
139 EXPECT_TRUE(expected_paths.empty());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000140}
141
142} // namespace chromeos_update_engine