OLD | NEW |
(Empty) | |
| 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 <errno.h> |
| 8 #include <set> |
| 9 #include <string> |
| 10 #include <vector> |
| 11 #include "base/string_util.h" |
| 12 #include <gtest/gtest.h> |
| 13 #include "update_engine/delta_diff_generator.h" |
| 14 #include "update_engine/filesystem_iterator.h" |
| 15 #include "update_engine/install_action.h" |
| 16 #include "update_engine/test_utils.h" |
| 17 #include "update_engine/utils.h" |
| 18 |
| 19 using chromeos_update_engine::System; |
| 20 using std::set; |
| 21 using std::string; |
| 22 using std::vector; |
| 23 |
| 24 namespace { |
| 25 void GenerateFilesAtPath(const string& base) { |
| 26 EXPECT_EQ(0, System(StringPrintf("echo hi > %s/hi", base.c_str()))); |
| 27 EXPECT_EQ(0, System(StringPrintf("mkdir -p %s/dir", base.c_str()))); |
| 28 EXPECT_EQ(0, System(StringPrintf("echo hello > %s/dir/hello", base.c_str()))); |
| 29 EXPECT_EQ(0, System(StringPrintf("echo -n > %s/dir/newempty", base.c_str()))); |
| 30 EXPECT_EQ(0, System(StringPrintf("rm -f %s/dir/bdev", base.c_str()))); |
| 31 EXPECT_EQ(0, System(StringPrintf("mknod %s/dir/bdev b 3 1", base.c_str()))); |
| 32 EXPECT_EQ(0, System(StringPrintf("rm -f %s/cdev", base.c_str()))); |
| 33 EXPECT_EQ(0, System(StringPrintf("mknod %s/cdev c 2 1", base.c_str()))); |
| 34 EXPECT_EQ(0, System(StringPrintf("mkdir -p %s/dir/subdir", base.c_str()))); |
| 35 EXPECT_EQ(0, System(StringPrintf("mkdir -p %s/dir/emptydir", base.c_str()))); |
| 36 EXPECT_EQ(0, System(StringPrintf("echo -n foo > %s/dir/bigfile", |
| 37 base.c_str()))); |
| 38 EXPECT_EQ(0, System(StringPrintf("chown 501:503 %s/dir/emptydir", |
| 39 base.c_str()))); |
| 40 EXPECT_EQ(0, System(StringPrintf("rm -f %s/dir/subdir/fifo", base.c_str()))); |
| 41 EXPECT_EQ(0, System(StringPrintf("mkfifo %s/dir/subdir/fifo", base.c_str()))); |
| 42 EXPECT_EQ(0, System(StringPrintf("ln -f -s /target %s/dir/subdir/link", |
| 43 base.c_str()))); |
| 44 } |
| 45 |
| 46 // Returns true if files at paths a, b are equal and there are no errors. |
| 47 bool FilesEqual(const string& a, const string& b) { |
| 48 struct stat a_stbuf; |
| 49 struct stat b_stbuf; |
| 50 |
| 51 int r = lstat(a.c_str(), &a_stbuf); |
| 52 TEST_AND_RETURN_FALSE_ERRNO(r == 0); |
| 53 r = lstat(b.c_str(), &b_stbuf); |
| 54 TEST_AND_RETURN_FALSE_ERRNO(r == 0); |
| 55 |
| 56 TEST_AND_RETURN_FALSE(a_stbuf.st_mode == b_stbuf.st_mode); |
| 57 if (S_ISBLK(a_stbuf.st_mode) || S_ISCHR(a_stbuf.st_mode)) |
| 58 TEST_AND_RETURN_FALSE(a_stbuf.st_rdev == b_stbuf.st_rdev); |
| 59 if (!S_ISREG(a_stbuf.st_mode)) { |
| 60 return true; |
| 61 } |
| 62 // Compare files |
| 63 TEST_AND_RETURN_FALSE(a_stbuf.st_size == b_stbuf.st_size); |
| 64 vector<char> a_data; |
| 65 TEST_AND_RETURN_FALSE(chromeos_update_engine::utils::ReadFile(a, &a_data)); |
| 66 vector<char> b_data; |
| 67 TEST_AND_RETURN_FALSE(chromeos_update_engine::utils::ReadFile(b, &b_data)); |
| 68 TEST_AND_RETURN_FALSE(a_data == b_data); |
| 69 return true; |
| 70 } |
| 71 |
| 72 class ScopedLoopDevUnmapper { |
| 73 public: |
| 74 explicit ScopedLoopDevUnmapper(const string& dev) : dev_(dev) {} |
| 75 ~ScopedLoopDevUnmapper() { |
| 76 EXPECT_EQ(0, System(string("losetup -d ") + dev_)); |
| 77 } |
| 78 private: |
| 79 string dev_; |
| 80 }; |
| 81 |
| 82 } |
| 83 |
| 84 namespace chromeos_update_engine { |
| 85 |
| 86 class InstallActionTest : public ::testing::Test { }; |
| 87 |
| 88 TEST(InstallActionTest, RunAsRootDiffTest) { |
| 89 ASSERT_EQ(0, getuid()); |
| 90 string loop_dev = GetUnusedLoopDevice(); |
| 91 ScopedLoopDevUnmapper loop_dev_unmapper(loop_dev); |
| 92 LOG(INFO) << "Using loop device: " << loop_dev; |
| 93 const string original_image("orig.image"); |
| 94 const string original_dir("orig"); |
| 95 const string new_dir("new"); |
| 96 |
| 97 ASSERT_EQ(0, System(string("dd if=/dev/zero of=") + original_image + |
| 98 " bs=5M count=1")); |
| 99 ASSERT_EQ(0, System(string("mkfs.ext3 -F ") + original_image)); |
| 100 ASSERT_EQ(0, System(string("losetup ") + loop_dev + " " + original_image)); |
| 101 ASSERT_EQ(0, System(string("mkdir ") + original_dir)); |
| 102 ASSERT_EQ(0, System(string("mount ") + loop_dev + " " + original_dir)); |
| 103 ASSERT_EQ(0, System(string("mkdir ") + new_dir)); |
| 104 |
| 105 GenerateFilesAtPath(original_dir); |
| 106 GenerateFilesAtPath(new_dir); |
| 107 |
| 108 { |
| 109 // Fill bigfile w/ some data in the new folder |
| 110 vector<char> buf(100 * 1024); |
| 111 for (unsigned int i = 0; i < buf.size(); i++) { |
| 112 buf[i] = static_cast<char>(i); |
| 113 } |
| 114 EXPECT_TRUE(WriteFileVector(new_dir + "/dir/bigfile", buf)); |
| 115 } |
| 116 // Make a diff |
| 117 DeltaArchiveManifest* delta = |
| 118 DeltaDiffGenerator::EncodeMetadataToProtoBuffer(new_dir.c_str()); |
| 119 EXPECT_TRUE(NULL != delta); |
| 120 EXPECT_TRUE(DeltaDiffGenerator::EncodeDataToDeltaFile(delta, |
| 121 original_dir, |
| 122 new_dir, |
| 123 "delta")); |
| 124 |
| 125 ASSERT_EQ(0, System(string("umount ") + original_dir)); |
| 126 |
| 127 ObjectFeederAction<InstallPlan> feeder_action; |
| 128 InstallAction install_action; |
| 129 ObjectCollectorAction<string> collector_action; |
| 130 |
| 131 BondActions(&feeder_action, &install_action); |
| 132 BondActions(&install_action, &collector_action); |
| 133 |
| 134 ActionProcessor processor; |
| 135 processor.EnqueueAction(&feeder_action); |
| 136 processor.EnqueueAction(&install_action); |
| 137 processor.EnqueueAction(&collector_action); |
| 138 |
| 139 InstallPlan install_plan(false, "", "", "delta", loop_dev); |
| 140 feeder_action.set_obj(install_plan); |
| 141 |
| 142 processor.StartProcessing(); |
| 143 EXPECT_FALSE(processor.IsRunning()) << "Update to handle async actions"; |
| 144 |
| 145 EXPECT_EQ(loop_dev, collector_action.object()); |
| 146 |
| 147 ASSERT_EQ(0, System(string("mount ") + loop_dev + " " + original_dir)); |
| 148 |
| 149 // Check that original_dir and new_dir are equal |
| 150 int original_count = 0; |
| 151 LOG(INFO) << "checking old"; |
| 152 { |
| 153 FilesystemIterator iter(original_dir, |
| 154 utils::SetWithValue<string>("/lost+found")); |
| 155 for (; !iter.IsEnd(); iter.Increment()) { |
| 156 original_count++; |
| 157 LOG(INFO) << "checking path: " << iter.GetPartialPath(); |
| 158 EXPECT_TRUE(FilesEqual(original_dir + iter.GetPartialPath(), |
| 159 new_dir + iter.GetPartialPath())); |
| 160 } |
| 161 EXPECT_FALSE(iter.IsErr()); |
| 162 } |
| 163 LOG(INFO) << "checking new"; |
| 164 int new_count = 0; |
| 165 { |
| 166 FilesystemIterator iter(new_dir, set<string>()); |
| 167 for (; !iter.IsEnd(); iter.Increment()) { |
| 168 new_count++; |
| 169 LOG(INFO) << "checking path: " << iter.GetPartialPath(); |
| 170 EXPECT_TRUE(FilesEqual(original_dir + iter.GetPartialPath(), |
| 171 new_dir + iter.GetPartialPath())); |
| 172 } |
| 173 EXPECT_FALSE(iter.IsErr()); |
| 174 } |
| 175 LOG(INFO) << "new_count = " << new_count; |
| 176 EXPECT_EQ(new_count, original_count); |
| 177 EXPECT_EQ(12, original_count); // 12 files in each dir |
| 178 |
| 179 ASSERT_EQ(0, System(string("umount ") + original_dir)); |
| 180 |
| 181 // Cleanup generated files |
| 182 ASSERT_EQ(0, System(string("rm -rf ") + original_dir)); |
| 183 ASSERT_EQ(0, System(string("rm -rf ") + new_dir)); |
| 184 EXPECT_EQ(0, System(string("rm -f ") + original_image)); |
| 185 ASSERT_EQ(0, system("rm -f delta")); |
| 186 } |
| 187 |
| 188 } // namespace chromeos_update_engine |
OLD | NEW |