update_engine: Move test-only utils to test_utils.{h,cc}.

utils.{h,cc} contains a collections of basic or small functions used
in different parts of the codebase. The test_utils.{h,cc} instead
contains functions only required during testing split out to a
separated file to be reused in different tests.

This CL moves without changes some functions defined in utils.h that
were only used during unittests. Two other basic functions were replaced
by the same function already present in base/ (StringHasSuffix and
StringHasPrefix). The functions in test_utils.h now have their own
namespace chromeos_update_engine::test_utils so is clear they come
from the test_utils file, in the same way the ones from utils are
in their own namespace.

Some othe minor linter fixes included here.

BUG=chromium:351429
TEST=Unittest still pass.

Change-Id: I73ab72a14158cb21c8e1f404cbc728423bc8f34f
Reviewed-on: https://chromium-review.googlesource.com/229021
Reviewed-by: Alex Deymo <deymo@chromium.org>
Commit-Queue: Alex Deymo <deymo@chromium.org>
Tested-by: Alex Deymo <deymo@chromium.org>
diff --git a/utils.cc b/utils.cc
index c6c5226..b84eda0 100644
--- a/utils.cc
+++ b/utils.cc
@@ -6,7 +6,6 @@
 
 #include <stdint.h>
 
-#include <attr/xattr.h>
 #include <dirent.h>
 #include <elf.h>
 #include <errno.h>
@@ -249,10 +248,9 @@
   return Read(fp.get(), size, out_p);
 }
 
-// Invokes a pipe |cmd|, then uses |append_func| to append its stdout to a
-// container |out_p|.
-template <class T>
-static bool ReadPipeAndAppend(const string& cmd, T* out_p) {
+// TODO(deymo): This is only used in unittest, but requires the private
+// Read<string>() defined here. Expose Read<string>() or move to base/ version.
+bool ReadPipe(const string& cmd, string* out_p) {
   FILE* fp = popen(cmd.c_str(), "r");
   if (!fp)
     return false;
@@ -260,7 +258,6 @@
   return (success && pclose(fp) >= 0);
 }
 
-
 bool ReadFile(const string& path, vector<char>* out_p) {
   return ReadFileChunkAndAppend(path, 0, -1, out_p);
 }
@@ -274,14 +271,6 @@
   return ReadFileChunkAndAppend(path, offset, size, out_p);
 }
 
-bool ReadPipe(const string& cmd, vector<char>* out_p) {
-  return ReadPipeAndAppend(cmd, out_p);
-}
-
-bool ReadPipe(const string& cmd, string* out_p) {
-  return ReadPipeAndAppend(cmd, out_p);
-}
-
 off_t BlockDevSize(int fd) {
   uint64_t dev_size;
   int rc = ioctl(fd, BLKGETSIZE64, &dev_size);
@@ -360,63 +349,6 @@
   }
 }
 
-namespace {
-class ScopedDirCloser {
- public:
-  explicit ScopedDirCloser(DIR** dir) : dir_(dir) {}
-  ~ScopedDirCloser() {
-    if (dir_ && *dir_) {
-      int r = closedir(*dir_);
-      TEST_AND_RETURN_ERRNO(r == 0);
-      *dir_ = nullptr;
-      dir_ = nullptr;
-    }
-  }
- private:
-  DIR** dir_;
-};
-}  // namespace
-
-bool RecursiveUnlinkDir(const string& path) {
-  struct stat stbuf;
-  int r = lstat(path.c_str(), &stbuf);
-  TEST_AND_RETURN_FALSE_ERRNO((r == 0) || (errno == ENOENT));
-  if ((r < 0) && (errno == ENOENT))
-    // path request is missing. that's fine.
-    return true;
-  if (!S_ISDIR(stbuf.st_mode)) {
-    TEST_AND_RETURN_FALSE_ERRNO((unlink(path.c_str()) == 0) ||
-                                (errno == ENOENT));
-    // success or path disappeared before we could unlink.
-    return true;
-  }
-  {
-    // We have a dir, unlink all children, then delete dir
-    DIR *dir = opendir(path.c_str());
-    TEST_AND_RETURN_FALSE_ERRNO(dir);
-    ScopedDirCloser dir_closer(&dir);
-    struct dirent dir_entry;
-    struct dirent *dir_entry_p;
-    int err = 0;
-    while ((err = readdir_r(dir, &dir_entry, &dir_entry_p)) == 0) {
-      if (dir_entry_p == nullptr) {
-        // end of stream reached
-        break;
-      }
-      // Skip . and ..
-      if (!strcmp(dir_entry_p->d_name, ".") ||
-          !strcmp(dir_entry_p->d_name, ".."))
-        continue;
-      TEST_AND_RETURN_FALSE(RecursiveUnlinkDir(path + "/" +
-                                               dir_entry_p->d_name));
-    }
-    TEST_AND_RETURN_FALSE(err == 0);
-  }
-  // unlink dir
-  TEST_AND_RETURN_FALSE_ERRNO((rmdir(path.c_str()) == 0) || (errno == ENOENT));
-  return true;
-}
-
 string GetDiskName(const string& partition_name) {
   string disk_name;
   return SplitPartitionName(partition_name, &disk_name, nullptr) ?
@@ -432,7 +364,7 @@
 bool SplitPartitionName(const string& partition_name,
                         string* out_disk_name,
                         int* out_partition_num) {
-  if (!StringHasPrefix(partition_name, "/dev/")) {
+  if (!StartsWithASCII(partition_name, "/dev/", true)) {
     LOG(ERROR) << "Invalid partition device name: " << partition_name;
     return false;
   }
@@ -481,7 +413,7 @@
 }
 
 string MakePartitionName(const string& disk_name, int partition_num) {
-  if (!StringHasPrefix(disk_name, "/dev/")) {
+  if (!StartsWithASCII(disk_name, "/dev/", true)) {
     LOG(ERROR) << "Invalid disk name: " << disk_name;
     return string();
   }
@@ -501,7 +433,7 @@
 
   partition_name += std::to_string(partition_num);
 
-  if (StringHasPrefix(partition_name, "/dev/ubiblock")) {
+  if (StartsWithASCII(partition_name, "/dev/ubiblock", true)) {
     // Special case for UBI block devieces that have "_0" suffix.
     partition_name += "_0";
   }
@@ -611,18 +543,6 @@
   return true;
 }
 
-bool StringHasSuffix(const string& str, const string& suffix) {
-  if (suffix.size() > str.size())
-    return false;
-  return 0 == str.compare(str.size() - suffix.size(), suffix.size(), suffix);
-}
-
-bool StringHasPrefix(const string& str, const string& prefix) {
-  if (prefix.size() > str.size())
-    return false;
-  return 0 == str.compare(0, prefix.size(), prefix);
-}
-
 bool MountFilesystem(const string& device,
                      const string& mountpoint,
                      unsigned long mountflags) {  // NOLINT(runtime/int)
@@ -908,11 +828,6 @@
   }
 }
 
-int CompareCpuShares(CpuShares shares_lhs,
-                     CpuShares shares_rhs) {
-  return static_cast<int>(shares_lhs) - static_cast<int>(shares_rhs);
-}
-
 int FuzzInt(int value, unsigned int range) {
   int min = value - range / 2;
   int max = value + range - range / 2;
@@ -1520,36 +1435,6 @@
                       encoded_hash.c_str());
 }
 
-bool IsXAttrSupported(const base::FilePath& dir_path) {
-  char *path = strdup(dir_path.Append("xattr_test_XXXXXX").value().c_str());
-
-  int fd = mkstemp(path);
-  if (fd == -1) {
-    PLOG(ERROR) << "Error creating temporary file in " << dir_path.value();
-    free(path);
-    return false;
-  }
-
-  if (unlink(path) != 0) {
-    PLOG(ERROR) << "Error unlinking temporary file " << path;
-    close(fd);
-    free(path);
-    return false;
-  }
-
-  int xattr_res = fsetxattr(fd, "user.xattr-test", "value", strlen("value"), 0);
-  if (xattr_res != 0) {
-    if (errno == ENOTSUP) {
-      // Leave it to call-sites to warn about non-support.
-    } else {
-      PLOG(ERROR) << "Error setting xattr on " << path;
-    }
-  }
-  close(fd);
-  free(path);
-  return xattr_res == 0;
-}
-
 bool DecodeAndStoreBase64String(const string& base64_encoded,
                                 base::FilePath *out_path) {
   vector<char> contents;