update_engine: Cleanup of HardwareInterface - const/override

Made most of the members of HardwareInterface 'const' and
used C++11's override on implementations to guard against method
signature changes.

BUG=None
TEST=Ran all unit tests

Change-Id: I33eecb0e6a0fc662da75fbf8d9953f0bb09cb08d
Reviewed-on: https://chromium-review.googlesource.com/187483
Tested-by: Alex Vakulenko <avakulenko@chromium.org>
Reviewed-by: Alex Vakulenko <avakulenko@chromium.org>
Commit-Queue: Alex Vakulenko <avakulenko@chromium.org>
diff --git a/fake_hardware.h b/fake_hardware.h
index 6f4be77..ff572f9 100644
--- a/fake_hardware.h
+++ b/fake_hardware.h
@@ -17,7 +17,7 @@
   FakeHardware()
     : kernel_device_("/dev/sdz4"),
       boot_device_("/dev/sdz5"),
-      bootable_devices_{"/dev/sdz4", "/dev/sdz5"},
+      bootable_devices_({"/dev/sdz4", "/dev/sdz5"}),
       is_official_build_(true),
       is_normal_boot_mode_(true),
       hardware_class_("Fake HWID BLAH-1234"),
@@ -25,26 +25,43 @@
       ec_version_("Fake EC v1.0a") {}
 
   // HardwareInterface methods.
-  virtual const std::string BootKernelDevice() { return kernel_device_; }
-  virtual const std::string BootDevice() { return boot_device_; }
-  virtual std::vector<std::string> GetKernelDevices() override
-      { return bootable_devices_; }
+  virtual std::string BootKernelDevice() const override {
+    return kernel_device_;
+  }
+
+  virtual std::string BootDevice() const override { return boot_device_; }
+
+  virtual std::vector<std::string> GetKernelDevices() const override {
+    return bootable_devices_;
+  }
 
   virtual bool IsKernelBootable(const std::string& kernel_device,
-                                bool* bootable)
-      { std::map<std::string, bool>::const_iterator i =
-            is_bootable_.find(kernel_device);
-        *bootable = (i != is_bootable_.end()) ? i->second : true;
-        return true; }
+                                bool* bootable) const override {
+    auto i = is_bootable_.find(kernel_device);
+    *bootable = (i != is_bootable_.end()) ? i->second : true;
+    return true;
+  }
 
-  virtual bool MarkKernelUnbootable(const std::string& kernel_device)
-      { is_bootable_[kernel_device] = false; return true;}
+  virtual bool MarkKernelUnbootable(const std::string& kernel_device) override {
+    is_bootable_[kernel_device] = false;
+    return true;
+  }
 
-  virtual bool IsOfficialBuild() { return is_official_build_; }
-  virtual bool IsNormalBootMode() { return is_normal_boot_mode_; }
-  virtual std::string GetHardwareClass() { return hardware_class_; }
-  virtual std::string GetFirmwareVersion() { return firmware_version_; }
-  virtual std::string GetECVersion() { return ec_version_; }
+  virtual bool IsOfficialBuild() const override { return is_official_build_; }
+
+  virtual bool IsNormalBootMode() const override {
+    return is_normal_boot_mode_;
+  }
+
+  virtual std::string GetHardwareClass() const override {
+    return hardware_class_;
+  }
+
+  virtual std::string GetFirmwareVersion() const override {
+    return firmware_version_;
+  }
+
+  virtual std::string GetECVersion() const override { return ec_version_; }
 
   // Setters
   void SetBootDevice(const std::string boot_device) {
diff --git a/hardware.cc b/hardware.cc
index d0e0fc0..8c676e4 100644
--- a/hardware.cc
+++ b/hardware.cc
@@ -33,11 +33,11 @@
 
 Hardware::~Hardware() {}
 
-const string Hardware::BootKernelDevice() {
+string Hardware::BootKernelDevice() const {
   return utils::KernelDeviceOfBootDevice(Hardware::BootDevice());
 }
 
-const string Hardware::BootDevice() {
+string Hardware::BootDevice() const {
   char boot_path[PATH_MAX];
   // Resolve the boot device path fully, including dereferencing
   // through dm-verity.
@@ -55,7 +55,7 @@
 }
 
 bool Hardware::IsKernelBootable(const std::string& kernel_device,
-                                bool* bootable) {
+                                bool* bootable) const {
   CgptAddParams params;
   memset(&params, '\0', sizeof(params));
 
@@ -76,7 +76,7 @@
   return true;
 }
 
-std::vector<std::string> Hardware::GetKernelDevices() {
+std::vector<std::string> Hardware::GetKernelDevices() const {
   LOG(INFO) << "GetAllKernelDevices";
 
   std::string disk_name = utils::GetDiskName(Hardware::BootKernelDevice());
@@ -138,11 +138,11 @@
   return true;
 }
 
-bool Hardware::IsOfficialBuild() {
+bool Hardware::IsOfficialBuild() const {
   return VbGetSystemPropertyInt("debug_build") == 0;
 }
 
-bool Hardware::IsNormalBootMode() {
+bool Hardware::IsNormalBootMode() const {
   bool dev_mode = VbGetSystemPropertyInt("devsw_boot") != 0;
   LOG_IF(INFO, dev_mode) << "Booted in dev mode.";
   return !dev_mode;
@@ -163,24 +163,21 @@
   return "";
 }
 
-string Hardware::GetHardwareClass() {
+string Hardware::GetHardwareClass() const {
   if (USE_HWID_OVERRIDE) {
     return HwidOverride::Read(base::FilePath("/"));
   }
   return ReadValueFromCrosSystem("hwid");
 }
 
-string Hardware::GetFirmwareVersion() {
+string Hardware::GetFirmwareVersion() const {
   return ReadValueFromCrosSystem("fwid");
 }
 
-string Hardware::GetECVersion() {
+string Hardware::GetECVersion() const {
   string input_line;
   int exit_code = 0;
-  vector<string> cmd(1, "/usr/sbin/mosys");
-  cmd.push_back("-k");
-  cmd.push_back("ec");
-  cmd.push_back("info");
+  vector<string> cmd = {"/usr/sbin/mosys", "-k", "ec", "info"};
 
   bool success = Subprocess::SynchronousExec(cmd, &exit_code, &input_line);
   if (!success || exit_code) {
diff --git a/hardware.h b/hardware.h
index cd7c8d5..02ed1ea 100644
--- a/hardware.h
+++ b/hardware.h
@@ -15,20 +15,20 @@
 class Hardware : public HardwareInterface {
  public:
   Hardware();
-  virtual ~Hardware();
+  virtual ~Hardware() override;
 
   // HardwareInterface methods.
-  virtual const std::string BootKernelDevice();
-  virtual const std::string BootDevice();
-  virtual std::vector<std::string> GetKernelDevices() override;
+  virtual std::string BootKernelDevice() const override;
+  virtual std::string BootDevice() const override;
+  virtual std::vector<std::string> GetKernelDevices() const override;
   virtual bool IsKernelBootable(const std::string& kernel_device,
-                                bool* bootable);
-  virtual bool MarkKernelUnbootable(const std::string& kernel_device);
-  virtual bool IsOfficialBuild();
-  virtual bool IsNormalBootMode();
-  virtual std::string GetHardwareClass();
-  virtual std::string GetFirmwareVersion();
-  virtual std::string GetECVersion();
+                                bool* bootable) const override;
+  virtual bool MarkKernelUnbootable(const std::string& kernel_device) override;
+  virtual bool IsOfficialBuild() const override;
+  virtual bool IsNormalBootMode() const override;
+  virtual std::string GetHardwareClass() const override;
+  virtual std::string GetFirmwareVersion() const override;
+  virtual std::string GetECVersion() const override;
 
  private:
   DISALLOW_COPY_AND_ASSIGN(Hardware);
diff --git a/hardware_interface.h b/hardware_interface.h
index 58157d0..f55d700 100644
--- a/hardware_interface.h
+++ b/hardware_interface.h
@@ -19,18 +19,18 @@
 class HardwareInterface {
  public:
   // Returns the currently booted kernel partition. "/dev/sda2", for example.
-  virtual const std::string BootKernelDevice() = 0;
+  virtual std::string BootKernelDevice() const = 0;
 
   // Returns the currently booted rootfs partition. "/dev/sda3", for example.
-  virtual const std::string BootDevice() = 0;
+  virtual std::string BootDevice() const = 0;
 
   // Returns a list of all kernel partitions available (whether bootable or not)
-  virtual std::vector<std::string> GetKernelDevices() = 0;
+  virtual std::vector<std::string> GetKernelDevices() const = 0;
 
   // Is the specified kernel partition currently bootable, based on GPT flags?
   // Returns success.
   virtual bool IsKernelBootable(const std::string& kernel_device,
-                                bool* bootable) = 0;
+                                bool* bootable) const = 0;
 
   // Mark the specified kernel partition unbootable in GPT flags. We mark
   // the other kernel as bootable inside postinst, not inside the UE.
@@ -38,23 +38,23 @@
   virtual bool MarkKernelUnbootable(const std::string& kernel_device) = 0;
 
   // Returns true if this is an official Chrome OS build, false otherwise.
-  virtual bool IsOfficialBuild() = 0;
+  virtual bool IsOfficialBuild() const = 0;
 
   // Returns true if the boot mode is normal or if it's unable to
   // determine the boot mode. Returns false if the boot mode is
   // developer.
-  virtual bool IsNormalBootMode() = 0;
+  virtual bool IsNormalBootMode() const = 0;
 
   // Returns the HWID or an empty string on error.
-  virtual std::string GetHardwareClass() = 0;
+  virtual std::string GetHardwareClass() const = 0;
 
   // Returns the firmware version or an empty string if the system is
   // not running chrome os firmware.
-  virtual std::string GetFirmwareVersion() = 0;
+  virtual std::string GetFirmwareVersion() const = 0;
 
   // Returns the ec version or an empty string if the system is not
   // running a custom chrome os ec.
-  virtual std::string GetECVersion() = 0;
+  virtual std::string GetECVersion() const = 0;
 
   virtual ~HardwareInterface() {}
 };
diff --git a/mock_hardware.h b/mock_hardware.h
index 8257b91..f24a4bf 100644
--- a/mock_hardware.h
+++ b/mock_hardware.h
@@ -51,18 +51,18 @@
   virtual ~MockHardware() {}
 
   // Hardware overrides.
-  MOCK_METHOD0(BootKernelDevice, const std::string());
-  MOCK_METHOD0(BootDevice, const std::string());
-  MOCK_METHOD0(GetKernelDevices, std::vector<std::string>());
-  MOCK_METHOD2(IsKernelBootable,
+  MOCK_CONST_METHOD0(BootKernelDevice, std::string());
+  MOCK_CONST_METHOD0(BootDevice, std::string());
+  MOCK_CONST_METHOD0(GetKernelDevices, std::vector<std::string>());
+  MOCK_CONST_METHOD2(IsKernelBootable,
                bool(const std::string& kernel_device, bool* bootable));
   MOCK_METHOD1(MarkKernelUnbootable,
                bool(const std::string& kernel_device));
-  MOCK_METHOD0(IsOfficialBuild, bool());
-  MOCK_METHOD0(IsNormalBootMode, bool());
-  MOCK_METHOD0(GetHardwareClass, std::string());
-  MOCK_METHOD0(GetFirmwareVersion, std::string());
-  MOCK_METHOD0(GetECVersion, std::string());
+  MOCK_CONST_METHOD0(IsOfficialBuild, bool());
+  MOCK_CONST_METHOD0(IsNormalBootMode, bool());
+  MOCK_CONST_METHOD0(GetHardwareClass, std::string());
+  MOCK_CONST_METHOD0(GetFirmwareVersion, std::string());
+  MOCK_CONST_METHOD0(GetECVersion, std::string());
 
   // Returns a reference to the underlying FakeHardware.
   FakeHardware& fake() {