Index: src/platform/update_engine/delta_performer_unittest.cc |
diff --git a/src/platform/update_engine/delta_performer_unittest.cc b/src/platform/update_engine/delta_performer_unittest.cc |
new file mode 100755 |
index 0000000000000000000000000000000000000000..36411253d5d2e3a9b231682ed4f69777199300a9 |
--- /dev/null |
+++ b/src/platform/update_engine/delta_performer_unittest.cc |
@@ -0,0 +1,194 @@ |
+// Copyright (c) 2009 The Chromium OS Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <sys/mount.h> |
+#include <inttypes.h> |
+ |
+#include <algorithm> |
+#include <string> |
+#include <vector> |
+ |
+#include "base/scoped_ptr.h" |
+#include "base/string_util.h" |
+#include <google/protobuf/repeated_field.h> |
+#include <gtest/gtest.h> |
+ |
+#include "update_engine/delta_diff_generator.h" |
+#include "update_engine/delta_performer.h" |
+#include "update_engine/graph_types.h" |
+#include "update_engine/test_utils.h" |
+#include "update_engine/update_metadata.pb.h" |
+#include "update_engine/utils.h" |
+ |
+namespace chromeos_update_engine { |
+ |
+using std::min; |
+using std::string; |
+using std::vector; |
+ |
+class DeltaPerformerTest : public ::testing::Test { }; |
+ |
+TEST(DeltaPerformerTest, ExtentsToByteStringTest) { |
+ uint64_t test[] = {1, 1, 4, 2, kSparseHole, 1, 0, 1}; |
+ COMPILE_ASSERT(arraysize(test) % 2 == 0, array_size_uneven); |
+ const uint64_t block_size = 4096; |
+ const uint64_t file_length = 5 * block_size - 13; |
+ |
+ google::protobuf::RepeatedPtrField<Extent> extents; |
+ for (size_t i = 0; i < arraysize(test); i += 2) { |
+ Extent* extent = extents.Add(); |
+ extent->set_start_block(test[i]); |
+ extent->set_num_blocks(test[i + 1]); |
+ } |
+ |
+ string expected_output = "4096:4096,16384:8192,-1:4096,0:4096"; |
+ string actual_output; |
+ EXPECT_TRUE(DeltaPerformer::ExtentsToBsdiffPositionsString(extents, |
+ block_size, |
+ file_length, |
+ &actual_output)); |
+ EXPECT_EQ(expected_output, actual_output); |
+} |
+ |
+class ScopedLoopMounter { |
+ public: |
+ explicit ScopedLoopMounter(const string& file_path, string* mnt_path, |
+ unsigned long flags) { |
+ EXPECT_TRUE(utils::MakeTempDirectory("/tmp/mnt.XXXXXX", mnt_path)); |
+ dir_remover_.reset(new ScopedDirRemover(*mnt_path)); |
+ |
+ string loop_dev = GetUnusedLoopDevice(); |
+ EXPECT_EQ(0, system(StringPrintf("losetup %s %s", loop_dev.c_str(), |
+ file_path.c_str()).c_str())); |
+ loop_releaser_.reset(new ScopedLoopbackDeviceReleaser(loop_dev)); |
+ |
+ EXPECT_TRUE(utils::MountFilesystem(loop_dev, *mnt_path, flags)); |
+ unmounter_.reset(new ScopedFilesystemUnmounter(*mnt_path)); |
+ } |
+ private: |
+ scoped_ptr<ScopedDirRemover> dir_remover_; |
+ scoped_ptr<ScopedLoopbackDeviceReleaser> loop_releaser_; |
+ scoped_ptr<ScopedFilesystemUnmounter> unmounter_; |
+}; |
+ |
+void CompareFilesByBlock(const string& a_file, const string& b_file) { |
+ vector<char> a_data, b_data; |
+ EXPECT_TRUE(utils::ReadFile(a_file, &a_data)); |
+ EXPECT_TRUE(utils::ReadFile(b_file, &b_data)); |
+ |
+ EXPECT_EQ(a_data.size(), b_data.size()); |
+ size_t kBlockSize = 4096; |
+ EXPECT_EQ(0, a_data.size() % kBlockSize); |
+ for (size_t i = 0; i < a_data.size(); i += kBlockSize) { |
+ EXPECT_EQ(0, i % kBlockSize); |
+ vector<char> a_sub(&a_data[i], &a_data[i + kBlockSize]); |
+ vector<char> b_sub(&b_data[i], &b_data[i + kBlockSize]); |
+ EXPECT_TRUE(a_sub == b_sub) << "Block " << (i/kBlockSize) << " differs"; |
+ } |
+} |
+ |
+namespace { |
+bool WriteSparseFile(const string& path, off_t size) { |
+ int fd = open(path.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644); |
+ TEST_AND_RETURN_FALSE_ERRNO(fd >= 0); |
+ ScopedFdCloser fd_closer(&fd); |
+ off_t rc = lseek(fd, size + 1, SEEK_SET); |
+ TEST_AND_RETURN_FALSE_ERRNO(rc != static_cast<off_t>(-1)); |
+ int return_code = ftruncate(fd, size); |
+ TEST_AND_RETURN_FALSE_ERRNO(return_code == 0); |
+ return true; |
+} |
+} |
+ |
+TEST(DeltaPerformerTest, RunAsRootSmallImageTest) { |
+ string a_img, b_img; |
+ EXPECT_TRUE(utils::MakeTempFile("/tmp/a_img.XXXXXX", &a_img, NULL)); |
+ ScopedPathUnlinker a_img_unlinker(a_img); |
+ EXPECT_TRUE(utils::MakeTempFile("/tmp/b_img.XXXXXX", &b_img, NULL)); |
+ ScopedPathUnlinker b_img_unlinker(b_img); |
+ |
+ CreateExtImageAtPath(a_img, NULL); |
+ CreateExtImageAtPath(b_img, NULL); |
+ |
+ // Make some changes to the A image. |
+ { |
+ string a_mnt; |
+ ScopedLoopMounter b_mounter(a_img, &a_mnt, 0); |
+ |
+ EXPECT_TRUE(utils::WriteFile(StringPrintf("%s/hardtocompress", |
+ a_mnt.c_str()).c_str(), |
+ reinterpret_cast<const char*>(kRandomString), |
+ sizeof(kRandomString) - 1)); |
+ // Write 1 MiB of 0xff to try to catch the case where writing a bsdiff |
+ // patch fails to zero out the final block. |
+ vector<char> ones(1024 * 1024, 0xff); |
+ EXPECT_TRUE(utils::WriteFile(StringPrintf("%s/ones", |
+ a_mnt.c_str()).c_str(), |
+ &ones[0], |
+ ones.size())); |
+ } |
+ |
+ // Make some changes to the B image. |
+ { |
+ string b_mnt; |
+ ScopedLoopMounter b_mounter(b_img, &b_mnt, 0); |
+ |
+ EXPECT_EQ(0, system(StringPrintf("cp %s/hello %s/hello2", b_mnt.c_str(), |
+ b_mnt.c_str()).c_str())); |
+ EXPECT_EQ(0, system(StringPrintf("rm %s/hello", b_mnt.c_str()).c_str())); |
+ EXPECT_EQ(0, system(StringPrintf("mv %s/hello2 %s/hello", b_mnt.c_str(), |
+ b_mnt.c_str()).c_str())); |
+ EXPECT_EQ(0, system(StringPrintf("echo foo > %s/foo", |
+ b_mnt.c_str()).c_str())); |
+ EXPECT_EQ(0, system(StringPrintf("touch %s/emptyfile", |
+ b_mnt.c_str()).c_str())); |
+ EXPECT_TRUE(WriteSparseFile(StringPrintf("%s/fullsparse", b_mnt.c_str()), |
+ 1024 * 1024)); |
+ EXPECT_EQ(0, system(StringPrintf("dd if=/dev/zero of=%s/partsparese bs=1 " |
+ "seek=4096 count=1", |
+ b_mnt.c_str()).c_str())); |
+ EXPECT_TRUE(utils::WriteFile(StringPrintf("%s/hardtocompress", |
+ b_mnt.c_str()).c_str(), |
+ reinterpret_cast<const char*>(kRandomString), |
+ sizeof(kRandomString))); |
+ } |
+ |
+ string delta_path; |
+ EXPECT_TRUE(utils::MakeTempFile("/tmp/delta.XXXXXX", &delta_path, NULL)); |
+ ScopedPathUnlinker delta_path_unlinker(delta_path); |
+ { |
+ string a_mnt, b_mnt; |
+ ScopedLoopMounter a_mounter(a_img, &a_mnt, MS_RDONLY); |
+ ScopedLoopMounter b_mounter(b_img, &b_mnt, MS_RDONLY); |
+ |
+ EXPECT_TRUE(DeltaDiffGenerator::GenerateDeltaUpdateFile(a_mnt, |
+ a_img, |
+ b_mnt, |
+ b_img, |
+ delta_path)); |
+ } |
+ |
+ // Read delta into memory. |
+ vector<char> delta; |
+ EXPECT_TRUE(utils::ReadFile(delta_path, &delta)); |
+ |
+ // Update the A image in place. |
+ DeltaPerformer performer; |
+ |
+ EXPECT_EQ(0, performer.Open(a_img.c_str(), 0, 0)); |
+ |
+ // Write at some number of bytes per operation. Arbitrarily chose 5. |
+ const size_t kBytesPerWrite = 5; |
+ for (size_t i = 0; i < delta.size(); i += kBytesPerWrite) { |
+ size_t count = min(delta.size() - i, kBytesPerWrite); |
+ EXPECT_EQ(count, performer.Write(&delta[i], count)); |
+ } |
+ |
+ // Wrapper around close. Returns 0 on success or -errno on error. |
+ EXPECT_EQ(0, performer.Close()); |
+ |
+ CompareFilesByBlock("/tmp/a_ref", "/tmp/b_ref"); |
+} |
+ |
+} // namespace chromeos_update_engine |