Use pre-generated ext image instead of create it on the fly.

mkfs.ext3 is not available in target device, so we don't use it.
Also removed all usage of dd.
This fixes a UtilsTest and the first step of integration test, the rest is
still failing because bsdiff is missing.

Test: ./update_engine_unittests
Test: cros_workon_make update_engine --test
Bug: 26955860

Change-Id: Ic600bd6fab1d6839f38d5ef767fbffc5a35571a4
diff --git a/common/test_utils.cc b/common/test_utils.cc
index 4ab1663..2de8ced 100644
--- a/common/test_utils.cc
+++ b/common/test_utils.cc
@@ -60,8 +60,6 @@
 
 namespace test_utils {
 
-const char* const kMountPathTemplate = "UpdateEngineTests_mnt-XXXXXX";
-
 const uint8_t kRandomString[] = {
   0xf2, 0xb7, 0x55, 0x92, 0xea, 0xa6, 0xc9, 0x57,
   0xe0, 0xf8, 0xeb, 0x34, 0x93, 0xd9, 0xc4, 0x8f,
@@ -245,74 +243,6 @@
   }
 }
 
-void CreateEmptyExtImageAtPath(const string& path,
-                               size_t size,
-                               int block_size) {
-  EXPECT_EQ(0, System(StringPrintf("dd if=/dev/zero of=%s"
-                                   " seek=%" PRIuS " bs=1 count=1 status=none",
-                                   path.c_str(), size)));
-  EXPECT_EQ(0, System(StringPrintf("mkfs.ext3 -q -b %d -F %s",
-                                   block_size, path.c_str())));
-}
-
-void CreateExtImageAtPath(const string& path, vector<string>* out_paths) {
-  // create 10MiB sparse file, mounted at a unique location.
-  string mount_path;
-  CHECK(utils::MakeTempDirectory(kMountPathTemplate, &mount_path));
-  ScopedDirRemover mount_path_unlinker(mount_path);
-
-  EXPECT_EQ(0, System(StringPrintf("dd if=/dev/zero of=%s"
-                                   " seek=10485759 bs=1 count=1 status=none",
-                                   path.c_str())));
-  EXPECT_EQ(0, System(StringPrintf("mkfs.ext3 -q -b 4096 -F %s",
-                                   path.c_str())));
-  EXPECT_EQ(0, System(StringPrintf("mount -o loop %s %s", path.c_str(),
-                                   mount_path.c_str())));
-  EXPECT_EQ(0, System(StringPrintf("echo hi > %s/hi", mount_path.c_str())));
-  EXPECT_EQ(0, System(StringPrintf("echo hello > %s/hello",
-                                   mount_path.c_str())));
-  EXPECT_EQ(0, System(StringPrintf("mkdir %s/some_dir", mount_path.c_str())));
-  EXPECT_EQ(0, System(StringPrintf("mkdir %s/some_dir/empty_dir",
-                                   mount_path.c_str())));
-  EXPECT_EQ(0, System(StringPrintf("mkdir %s/some_dir/mnt",
-                                   mount_path.c_str())));
-  EXPECT_EQ(0, System(StringPrintf("echo T > %s/some_dir/test",
-                                   mount_path.c_str())));
-  EXPECT_EQ(0, System(StringPrintf("mkfifo %s/some_dir/fifo",
-                                   mount_path.c_str())));
-  EXPECT_EQ(0, System(StringPrintf("mknod %s/cdev c 2 3", mount_path.c_str())));
-  EXPECT_EQ(0, System(StringPrintf("ln -s /some/target %s/sym",
-                                   mount_path.c_str())));
-  EXPECT_EQ(0, System(StringPrintf("ln %s/some_dir/test %s/testlink",
-                                   mount_path.c_str(), mount_path.c_str())));
-  EXPECT_EQ(0, System(StringPrintf("echo T > %s/srchardlink0",
-                                   mount_path.c_str())));
-  EXPECT_EQ(0, System(StringPrintf("ln %s/srchardlink0 %s/srchardlink1",
-                                   mount_path.c_str(), mount_path.c_str())));
-  EXPECT_EQ(0, System(StringPrintf("ln -s bogus %s/boguslink",
-                                   mount_path.c_str())));
-  EXPECT_TRUE(utils::UnmountFilesystem(mount_path.c_str()));
-
-  if (out_paths) {
-    out_paths->clear();
-    out_paths->push_back("");
-    out_paths->push_back("/hi");
-    out_paths->push_back("/boguslink");
-    out_paths->push_back("/hello");
-    out_paths->push_back("/some_dir");
-    out_paths->push_back("/some_dir/empty_dir");
-    out_paths->push_back("/some_dir/mnt");
-    out_paths->push_back("/some_dir/test");
-    out_paths->push_back("/some_dir/fifo");
-    out_paths->push_back("/cdev");
-    out_paths->push_back("/testlink");
-    out_paths->push_back("/sym");
-    out_paths->push_back("/srchardlink0");
-    out_paths->push_back("/srchardlink1");
-    out_paths->push_back("/lost+found");
-  }
-}
-
 ScopedLoopMounter::ScopedLoopMounter(const string& file_path,
                                      string* mnt_path,
                                      unsigned long flags) {  // NOLINT - long
diff --git a/common/test_utils.h b/common/test_utils.h
index 60ec90e..e4919d8 100644
--- a/common/test_utils.h
+++ b/common/test_utils.h
@@ -100,16 +100,6 @@
 
 void FillWithData(brillo::Blob* buffer);
 
-// Creates an empty ext image.
-void CreateEmptyExtImageAtPath(const std::string& path,
-                               size_t size,
-                               int block_size);
-
-// Creates an ext image with some files in it. The paths creates are
-// returned in out_paths.
-void CreateExtImageAtPath(const std::string& path,
-                          std::vector<std::string>* out_paths);
-
 // Class to unmount FS when object goes out of scope
 class ScopedFilesystemUnmounter {
  public:
diff --git a/common/utils_unittest.cc b/common/utils_unittest.cc
index f840a75..5bc2428 100644
--- a/common/utils_unittest.cc
+++ b/common/utils_unittest.cc
@@ -17,6 +17,7 @@
 #include "update_engine/common/utils.h"
 
 #include <errno.h>
+#include <fcntl.h>
 #include <stdint.h>
 #include <sys/stat.h>
 #include <sys/types.h>
@@ -192,21 +193,26 @@
   }
 }
 
-TEST(UtilsTest, RunAsRootGetFilesystemSizeTest) {
+TEST(UtilsTest, GetFilesystemSizeTest) {
   string img;
   EXPECT_TRUE(utils::MakeTempFile("img.XXXXXX", &img, nullptr));
   ScopedPathUnlinker img_unlinker(img);
-  test_utils::CreateExtImageAtPath(img, nullptr);
-  // Extend the "partition" holding the file system from 10MiB to 20MiB.
-  EXPECT_EQ(0, test_utils::System(base::StringPrintf(
-      "dd if=/dev/zero of=%s seek=20971519 bs=1 count=1 status=none",
-      img.c_str())));
-  EXPECT_EQ(20 * 1024 * 1024, utils::FileSize(img));
+  EXPECT_TRUE(base::CopyFile(
+      test_utils::GetBuildArtifactsPath().Append("gen/disk_ext2_4k.img"),
+      base::FilePath(img)));
+  {
+    // Extend the "partition" holding the file system from 4MiB to 10MiB.
+    int fd = HANDLE_EINTR(open(img.c_str(), O_WRONLY));
+    ASSERT_GT(fd, 0);
+    ScopedFdCloser fd_closer(&fd);
+    EXPECT_TRUE(utils::PWriteAll(fd, "\0", 1, 10 * 1024 * 1024 - 1));
+  }
+  EXPECT_EQ(10 * 1024 * 1024, utils::FileSize(img));
   int block_count = 0;
   int block_size = 0;
   EXPECT_TRUE(utils::GetFilesystemSize(img, &block_count, &block_size));
   EXPECT_EQ(4096, block_size);
-  EXPECT_EQ(10 * 1024 * 1024 / 4096, block_count);
+  EXPECT_EQ(4 * 1024 * 1024 / 4096, block_count);
 }
 
 // Squashfs example filesystem, generated with:
diff --git a/payload_consumer/delta_performer_integration_test.cc b/payload_consumer/delta_performer_integration_test.cc
index 3d7f8fa..5f0dc89 100644
--- a/payload_consumer/delta_performer_integration_test.cc
+++ b/payload_consumer/delta_performer_integration_test.cc
@@ -156,6 +156,14 @@
   return true;
 }
 
+static bool WriteByteAtOffset(const string& path, off_t offset) {
+  int fd = open(path.c_str(), O_CREAT | O_WRONLY, 0644);
+  TEST_AND_RETURN_FALSE_ERRNO(fd >= 0);
+  ScopedFdCloser fd_closer(&fd);
+  EXPECT_TRUE(utils::PWriteAll(fd, "\0", 1, offset));
+  return true;
+}
+
 static size_t GetSignatureSize(const string& private_key_path) {
   const brillo::Blob data(1, 'x');
   brillo::Blob hash;
@@ -310,7 +318,10 @@
   // in-place on A, we apply it to a new image, result_img.
   EXPECT_TRUE(
       utils::MakeTempFile("result_img.XXXXXX", &state->result_img, nullptr));
-  test_utils::CreateExtImageAtPath(state->a_img, nullptr);
+
+  EXPECT_TRUE(base::CopyFile(
+      test_utils::GetBuildArtifactsPath().Append("gen/disk_ext2_4k.img"),
+      base::FilePath(state->a_img)));
 
   state->image_size = utils::FileSize(state->a_img);
 
@@ -360,10 +371,8 @@
         WriteSparseFile(base::StringPrintf("%s/move-from-sparse",
                                            a_mnt.c_str()), 16 * 1024));
 
-    EXPECT_EQ(0,
-              System(base::StringPrintf("dd if=/dev/zero of=%s/move-semi-sparse"
-                                        " bs=1 seek=4096 count=1 status=none",
-                                        a_mnt.c_str()).c_str()));
+    EXPECT_TRUE(WriteByteAtOffset(
+        base::StringPrintf("%s/move-semi-sparse", a_mnt.c_str()), 4096));
 
     // Write 1 MiB of 0xff to try to catch the case where writing a bsdiff
     // patch fails to zero out the final block.
@@ -389,57 +398,47 @@
                 utils::FileSize(state->result_img));
     }
 
-    test_utils::CreateExtImageAtPath(state->b_img, nullptr);
+    EXPECT_TRUE(base::CopyFile(
+        test_utils::GetBuildArtifactsPath().Append("gen/disk_ext2_4k.img"),
+        base::FilePath(state->b_img)));
 
     // Make some changes to the B image.
     string b_mnt;
     ScopedLoopMounter b_mounter(state->b_img, &b_mnt, 0);
+    base::FilePath mnt_path(b_mnt);
 
-    EXPECT_EQ(0, System(base::StringPrintf("cp %s/hello %s/hello2",
-                                           b_mnt.c_str(),
-                                           b_mnt.c_str()).c_str()));
-    EXPECT_EQ(0, System(base::StringPrintf("rm %s/hello",
-                                           b_mnt.c_str()).c_str()));
-    EXPECT_EQ(0, System(base::StringPrintf("mv %s/hello2 %s/hello",
-                                           b_mnt.c_str(),
-                                           b_mnt.c_str()).c_str()));
-    EXPECT_EQ(0, System(base::StringPrintf("echo foo > %s/foo",
-                                           b_mnt.c_str()).c_str()));
-    EXPECT_EQ(0, System(base::StringPrintf("touch %s/emptyfile",
-                                           b_mnt.c_str()).c_str()));
-    EXPECT_TRUE(WriteSparseFile(base::StringPrintf("%s/fullsparse",
-                                                   b_mnt.c_str()),
-                                                   1024 * 1024));
+    EXPECT_TRUE(base::CopyFile(mnt_path.Append("regular-small"),
+                               mnt_path.Append("regular-small2")));
+    EXPECT_TRUE(base::DeleteFile(mnt_path.Append("regular-small"), false));
+    EXPECT_TRUE(base::Move(mnt_path.Append("regular-small2"),
+                           mnt_path.Append("regular-small")));
+    EXPECT_TRUE(
+        test_utils::WriteFileString(mnt_path.Append("foo").value(), "foo"));
+    EXPECT_EQ(0, base::WriteFile(mnt_path.Append("emptyfile"), "", 0));
 
     EXPECT_TRUE(
-        WriteSparseFile(base::StringPrintf("%s/move-to-sparse", b_mnt.c_str()),
-                        16 * 1024));
+        WriteSparseFile(mnt_path.Append("fullsparse").value(), 1024 * 1024));
+    EXPECT_TRUE(
+        WriteSparseFile(mnt_path.Append("move-to-sparse").value(), 16 * 1024));
 
     brillo::Blob zeros(16 * 1024, 0);
     EXPECT_EQ(static_cast<int>(zeros.size()),
-              base::WriteFile(base::FilePath(base::StringPrintf(
-                                  "%s/move-from-sparse", b_mnt.c_str())),
+              base::WriteFile(mnt_path.Append("move-from-sparse"),
                               reinterpret_cast<const char*>(zeros.data()),
                               zeros.size()));
 
-    EXPECT_EQ(0, System(base::StringPrintf("dd if=/dev/zero "
-                                           "of=%s/move-semi-sparse "
-                                           "bs=1 seek=4096 count=1 status=none",
-                                           b_mnt.c_str()).c_str()));
+    EXPECT_TRUE(
+        WriteByteAtOffset(mnt_path.Append("move-semi-sparse").value(), 4096));
+    EXPECT_TRUE(WriteByteAtOffset(mnt_path.Append("partsparse").value(), 4096));
 
-    EXPECT_EQ(0, System(base::StringPrintf("dd if=/dev/zero "
-                                           "of=%s/partsparse bs=1 "
-                                           "seek=4096 count=1 status=none",
-                                           b_mnt.c_str()).c_str()));
-    EXPECT_EQ(0, System(base::StringPrintf("cp %s/srchardlink0 %s/tmp && "
-                                           "mv %s/tmp %s/srchardlink1",
-                                           b_mnt.c_str(),
-                                           b_mnt.c_str(),
-                                           b_mnt.c_str(),
-                                           b_mnt.c_str()).c_str()));
-    EXPECT_EQ(0, System(
-        base::StringPrintf("rm %s/boguslink && echo foobar > %s/boguslink",
-                           b_mnt.c_str(), b_mnt.c_str()).c_str()));
+    EXPECT_TRUE(
+        base::CopyFile(mnt_path.Append("regular-16k"), mnt_path.Append("tmp")));
+    EXPECT_TRUE(base::Move(mnt_path.Append("tmp"),
+                           mnt_path.Append("link-hard-regular-16k")));
+
+    EXPECT_TRUE(base::DeleteFile(mnt_path.Append("link-short_symlink"), false));
+    EXPECT_TRUE(test_utils::WriteFileString(
+        mnt_path.Append("link-short_symlink").value(), "foobar"));
 
     brillo::Blob hardtocompress;
     while (hardtocompress.size() < 3 * kBlockSize) {
diff --git a/payload_generator/ext2_filesystem_unittest.cc b/payload_generator/ext2_filesystem_unittest.cc
index 17c72d6..3824dc2 100644
--- a/payload_generator/ext2_filesystem_unittest.cc
+++ b/payload_generator/ext2_filesystem_unittest.cc
@@ -126,12 +126,14 @@
     // be included in the list.
     set<string> kExpectedFiles = {
         "/",
+        "/cdev",
         "/dir1",
         "/dir1/file",
         "/dir1/dir2",
         "/dir1/dir2/file",
         "/dir1/dir2/dir1",
         "/empty-file",
+        "/fifo",
         "/link-hard-regular-16k",
         "/link-long_symlink",
         "/link-short_symlink",
diff --git a/sample_images/generate_images.sh b/sample_images/generate_images.sh
index b461175..ed5e00d 100755
--- a/sample_images/generate_images.sh
+++ b/sample_images/generate_images.sh
@@ -87,6 +87,12 @@
   echo "foo" | sudo tee "${mntdir}"/dir1/dir2/file >/dev/null
   echo "bar" | sudo tee "${mntdir}"/dir1/file >/dev/null
 
+  # FIFO
+  sudo mkfifo "${mntdir}"/fifo
+
+  # character special file
+  sudo mknod "${mntdir}"/cdev c 2 3
+
   # removed: removed files that should not be listed.
   echo "We will remove this file so it's contents will be somewhere in the " \
     "empty space data but it won't be all zeros." |
@@ -236,8 +242,8 @@
 
 main() {
   # Add more sample images here.
-  generate_image disk_ext2_1k default 16777216 1024
-  generate_image disk_ext2_4k default 16777216 4096
+  generate_image disk_ext2_1k default $((1024 * 1024)) 1024
+  generate_image disk_ext2_4k default $((1024 * 4096)) 4096
   generate_image disk_ext2_4k_empty empty $((1024 * 4096)) 4096
   generate_image disk_ext2_unittest unittest $((1024 * 4096)) 4096
 
diff --git a/sample_images/sample_images.tar.bz2 b/sample_images/sample_images.tar.bz2
index 20a698b..e614dd7 100644
--- a/sample_images/sample_images.tar.bz2
+++ b/sample_images/sample_images.tar.bz2
Binary files differ