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: