PM: Add BoxedValue::ToString() method.

This will be used to dump variables in the EvaluationContext.

BUG=chromium:355724
TEST=New unit test + Unit tests pass.

Change-Id: I82a3fb5d2636fd124dfbf63bf130fcd3c5726bcd
Reviewed-on: https://chromium-review.googlesource.com/197524
Reviewed-by: Alex Deymo <deymo@chromium.org>
Commit-Queue: David Zeuthen <zeuthen@chromium.org>
Tested-by: David Zeuthen <zeuthen@chromium.org>
diff --git a/policy_manager/boxed_value.cc b/policy_manager/boxed_value.cc
new file mode 100644
index 0000000..0cf7ed2
--- /dev/null
+++ b/policy_manager/boxed_value.cc
@@ -0,0 +1,163 @@
+// Copyright (c) 2014 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 <set>
+#include <string>
+
+#include <base/strings/string_number_conversions.h>
+#include <base/time/time.h>
+
+#include "update_engine/policy_manager/boxed_value.h"
+#include "update_engine/policy_manager/shill_provider.h"
+#include "update_engine/policy_manager/updater_provider.h"
+#include "update_engine/utils.h"
+
+using std::set;
+using std::string;
+
+namespace chromeos_policy_manager {
+
+// Template instantiation for common types; used in BoxedValue::ToString().
+// Keep in sync with boxed_value_unitttest.cc.
+
+template<>
+string BoxedValue::ValuePrinter<string>(const void *value) {
+  const string* val = reinterpret_cast<const string*>(value);
+  return *val;
+}
+
+template<>
+string BoxedValue::ValuePrinter<unsigned int>(const void *value) {
+  const unsigned int* val = reinterpret_cast<const unsigned int*>(value);
+  return base::UintToString(*val);
+}
+
+template<>
+string BoxedValue::ValuePrinter<unsigned long>(const void *value) {
+  const unsigned long* val = reinterpret_cast<const unsigned long*>(value);
+  return base::Uint64ToString(static_cast<uint64>(*val));
+}
+
+template<>
+string BoxedValue::ValuePrinter<unsigned long long>(const void *value) {
+  const unsigned long long* val =
+      reinterpret_cast<const unsigned long long*>(value);
+  return base::Uint64ToString(static_cast<uint64>(*val));
+}
+
+template<>
+string BoxedValue::ValuePrinter<int>(const void *value) {
+  const int* val = reinterpret_cast<const int*>(value);
+  return base::IntToString(*val);
+}
+
+template<>
+string BoxedValue::ValuePrinter<bool>(const void *value) {
+  const bool* val = reinterpret_cast<const bool*>(value);
+  return *val ? "true" : "false";
+}
+
+template<>
+string BoxedValue::ValuePrinter<double>(const void *value) {
+  const double* val = reinterpret_cast<const double*>(value);
+  return base::DoubleToString(*val);
+}
+
+template<>
+string BoxedValue::ValuePrinter<base::Time>(const void *value) {
+  const base::Time* val = reinterpret_cast<const base::Time*>(value);
+  return chromeos_update_engine::utils::ToString(*val);
+}
+
+template<>
+string BoxedValue::ValuePrinter<base::TimeDelta>(const void *value) {
+  const base::TimeDelta* val = reinterpret_cast<const base::TimeDelta*>(value);
+  return chromeos_update_engine::utils::FormatTimeDelta(*val);
+}
+
+static std::string ConnectionTypeToString(ConnectionType type) {
+  switch (type) {
+    case ConnectionType::kEthernet:
+      return "Ethernet";
+    case ConnectionType::kWifi:
+      return "Wifi";
+    case ConnectionType::kWimax:
+      return "Wimax";
+    case ConnectionType::kBluetooth:
+      return "Bluetooth";
+    case ConnectionType::kCellular:
+      return "Cellular";
+    case ConnectionType::kUnknown:
+      return "Unknown";
+  }
+  NOTREACHED();
+  return "Unknown";
+}
+
+template<>
+string BoxedValue::ValuePrinter<ConnectionType>(const void *value) {
+  const ConnectionType* val = reinterpret_cast<const ConnectionType*>(value);
+  return ConnectionTypeToString(*val);
+}
+
+template<>
+string BoxedValue::ValuePrinter<set<ConnectionType>>(const void *value) {
+  string ret = "";
+  const set<ConnectionType>* val =
+      reinterpret_cast<const set<ConnectionType>*>(value);
+  for (auto& it : *val) {
+    ConnectionType type = it;
+    if (ret.size() > 0)
+      ret += ",";
+    ret += ConnectionTypeToString(type);
+  }
+  return ret;
+}
+
+template<>
+string BoxedValue::ValuePrinter<ConnectionTethering>(const void *value) {
+  const ConnectionTethering* val =
+      reinterpret_cast<const ConnectionTethering*>(value);
+  switch (*val) {
+    case ConnectionTethering::kNotDetected:
+      return "Not Detected";
+    case ConnectionTethering::kSuspected:
+      return "Suspected";
+    case ConnectionTethering::kConfirmed:
+      return "Confirmed";
+    case ConnectionTethering::kUnknown:
+      return "Unknown";
+  }
+  NOTREACHED();
+  return "Unknown";
+}
+
+template<>
+string BoxedValue::ValuePrinter<Stage>(const void *value) {
+  const Stage* val = reinterpret_cast<const Stage*>(value);
+  switch (*val) {
+   case Stage::kIdle:
+      return "Idle";
+    case Stage::kCheckingForUpdate:
+      return "Checking For Update";
+    case Stage::kUpdateAvailable:
+      return "Update Available";
+    case Stage::kDownloading:
+      return "Downloading";
+    case Stage::kVerifying:
+      return "Verifying";
+    case Stage::kFinalizing:
+      return "Finalizing";
+    case Stage::kUpdatedNeedReboot:
+      return "Updated, Need Reboot";
+    case Stage::kReportingErrorEvent:
+      return "Reporting Error Event";
+    case Stage::kAttemptingRollback:
+      return "Attempting Rollback";
+  }
+  NOTREACHED();
+  return "Unknown";
+}
+
+}  // namespace chromeos_policy_manager
diff --git a/policy_manager/boxed_value.h b/policy_manager/boxed_value.h
index 0d44c3c..4e26292 100644
--- a/policy_manager/boxed_value.h
+++ b/policy_manager/boxed_value.h
@@ -5,6 +5,8 @@
 #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_BOXED_VALUE_H_
 #define CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_BOXED_VALUE_H_
 
+#include <string>
+
 #include <base/basictypes.h>
 
 namespace chromeos_policy_manager {
@@ -41,13 +43,14 @@
   // Creates an empty BoxedValue. Since the pointer can't be assigned from other
   // BoxedValues or pointers, this is only useful in places where a default
   // constructor is required, such as std::map::operator[].
-  BoxedValue() : value_(NULL), deleter_(NULL) {}
+  BoxedValue() : value_(NULL), deleter_(NULL), printer_(NULL) {}
 
   // Creates a BoxedValue for the passed pointer |value|. The BoxedValue keeps
   // the ownership of this pointer and can't be released.
   template<typename T>
   explicit BoxedValue(const T* value)
-      : value_(static_cast<const void*>(value)), deleter_(ValueDeleter<T>) {}
+    : value_(static_cast<const void*>(value)), deleter_(ValueDeleter<T>),
+      printer_(ValuePrinter<T>) {}
 
   // The move constructor takes ownership of the pointer since the semantics of
   // it allows to render the passed BoxedValue undefined. You need to use the
@@ -55,9 +58,11 @@
   // like in:
   //   BoxedValue new_box(std::move(other_box));
   BoxedValue(BoxedValue&& other)
-      : value_(other.value_), deleter_(other.deleter_) {
+      : value_(other.value_), deleter_(other.deleter_),
+        printer_(other.printer_) {
     other.value_ = NULL;
     other.deleter_ = NULL;
+    other.printer_ = NULL;
   }
 
   // Deletes the |value| passed on construction using the delete for the passed
@@ -69,12 +74,25 @@
 
   const void* value() const { return value_; }
 
+  std::string ToString() const {
+    if (!printer_)
+      return "(no printer)";
+    if (!value_)
+      return "(no value)";
+    return printer_(value_);
+  }
+
   // Static method to call the destructor of the right type.
   template<typename T>
   static void ValueDeleter(const void* value) {
     delete reinterpret_cast<const T*>(value);
   }
 
+  // Static method to print a type. See boxed_value.cc for common
+  // instantiations.
+  template<typename T>
+  static std::string ValuePrinter(const void* value);
+
  private:
   // A pointer to the cached value.
   const void* value_;
@@ -82,6 +100,9 @@
   // A function that calls delete for the right type of value_.
   void (*deleter_)(const void*);
 
+  // A function that converts value_ to a string.
+  std::string (*printer_)(const void*);
+
   DISALLOW_COPY_AND_ASSIGN(BoxedValue);
 };
 
diff --git a/policy_manager/boxed_value_unittest.cc b/policy_manager/boxed_value_unittest.cc
index 3d56257..6319ba7 100644
--- a/policy_manager/boxed_value_unittest.cc
+++ b/policy_manager/boxed_value_unittest.cc
@@ -5,11 +5,20 @@
 #include <gtest/gtest.h>
 #include <list>
 #include <map>
+#include <set>
 #include <string>
 
+#include <base/strings/stringprintf.h>
+#include <base/time/time.h>
+
 #include "update_engine/policy_manager/boxed_value.h"
 #include "update_engine/policy_manager/pmtest_utils.h"
+#include "update_engine/policy_manager/shill_provider.h"
+#include "update_engine/policy_manager/updater_provider.h"
 
+using base::Time;
+using base::TimeDelta;
+using std::set;
 using std::list;
 using std::map;
 using std::string;
@@ -24,10 +33,19 @@
   ~DeleterMarker() { *marker_ = true; }
 
  private:
+  friend string BoxedValue::ValuePrinter<DeleterMarker>(const void *);
+
   // Pointer to the bool marker.
   bool* marker_;
 };
 
+template<>
+string BoxedValue::ValuePrinter<DeleterMarker>(const void *value) {
+  const DeleterMarker* val = reinterpret_cast<const DeleterMarker*>(value);
+  return base::StringPrintf("DeleterMarker:%s",
+                            *val->marker_ ? "true" : "false");
+}
+
 TEST(PmBoxedValueTest, Deleted) {
   bool marker = true;
   const DeleterMarker* deleter_marker = new DeleterMarker(&marker);
@@ -75,4 +93,124 @@
   PMTEST_EXPECT_NULL(m[33].value());
 }
 
+TEST(PmBoxedValueTest, StringToString) {
+  EXPECT_EQ("Hej Verden!",
+            BoxedValue(new string("Hej Verden!")).ToString());
+}
+
+TEST(PmBoxedValueTest, IntToString) {
+  EXPECT_EQ("42", BoxedValue(new int(42)).ToString());
+}
+
+TEST(PmBoxedValueTest, UnsignedIntToString) {
+  // 4294967295 is the biggest possible 32-bit unsigned integer.
+  EXPECT_EQ("4294967295", BoxedValue(new unsigned int(4294967295)).ToString());
+}
+
+TEST(PmBoxedValueTest, UnsignedLongToString) {
+  EXPECT_EQ("4294967295", BoxedValue(new unsigned long(4294967295)).ToString());
+}
+
+TEST(PmBoxedValueTest, UnsignedLongLongToString) {
+  // 18446744073709551615 is the biggest possible 64-bit unsigned integer.
+  EXPECT_EQ("18446744073709551615", BoxedValue(
+      new unsigned long long(18446744073709551615ULL)).ToString());
+}
+
+TEST(PmBoxedValueTest, BoolToString) {
+  EXPECT_EQ("false", BoxedValue(new bool(false)).ToString());
+  EXPECT_EQ("true", BoxedValue(new bool(true)).ToString());
+}
+
+TEST(PmBoxedValueTest, DoubleToString) {
+  EXPECT_EQ("1.501", BoxedValue(new double(1.501)).ToString());
+}
+
+TEST(PmBoxedValueTest, TimeToString) {
+  // Tue Apr 29 22:30:55 UTC 2014 is 1398810655 seconds since the Unix Epoch.
+  EXPECT_EQ("4/29/2014 22:30:55 GMT",
+            BoxedValue(new Time(Time::FromTimeT(1398810655))).ToString());
+}
+
+TEST(PmBoxedValueTest, TimeDeltaToString) {
+  // 12345 seconds is 3 hours, 25 minutes and 45 seconds.
+  EXPECT_EQ("3h25m45s",
+            BoxedValue(new TimeDelta(TimeDelta::FromSeconds(12345)))
+            .ToString());
+}
+
+TEST(PmBoxedValueTest, ConnectionTypeToString) {
+  EXPECT_EQ("Ethernet",
+            BoxedValue(new ConnectionType(ConnectionType::kEthernet))
+            .ToString());
+  EXPECT_EQ("Wifi",
+            BoxedValue(new ConnectionType(ConnectionType::kWifi)).ToString());
+  EXPECT_EQ("Wimax",
+            BoxedValue(new ConnectionType(ConnectionType::kWimax)).ToString());
+  EXPECT_EQ("Bluetooth",
+            BoxedValue(new ConnectionType(ConnectionType::kBluetooth))
+            .ToString());
+  EXPECT_EQ("Cellular",
+            BoxedValue(new ConnectionType(ConnectionType::kCellular))
+            .ToString());
+  EXPECT_EQ("Unknown",
+            BoxedValue(new ConnectionType(ConnectionType::kUnknown))
+            .ToString());
+}
+
+TEST(PmBoxedValueTest, ConnectionTetheringToString) {
+  EXPECT_EQ("Not Detected",
+            BoxedValue(new ConnectionTethering(
+                ConnectionTethering::kNotDetected)).ToString());
+  EXPECT_EQ("Suspected",
+            BoxedValue(new ConnectionTethering(ConnectionTethering::kSuspected))
+            .ToString());
+  EXPECT_EQ("Confirmed",
+            BoxedValue(new ConnectionTethering(ConnectionTethering::kConfirmed))
+            .ToString());
+  EXPECT_EQ("Unknown",
+            BoxedValue(new ConnectionTethering(ConnectionTethering::kUnknown))
+            .ToString());
+}
+
+TEST(PmBoxedValueTest, SetConnectionTypeToString) {
+  set<ConnectionType>* set1 = new set<ConnectionType>;
+  set1->insert(ConnectionType::kWimax);
+  set1->insert(ConnectionType::kEthernet);
+  EXPECT_EQ("Ethernet,Wimax", BoxedValue(set1).ToString());
+
+  set<ConnectionType>* set2 = new set<ConnectionType>;
+  set2->insert(ConnectionType::kWifi);
+  EXPECT_EQ("Wifi", BoxedValue(set2).ToString());
+}
+
+TEST(PmBoxedValueTest, StageToString) {
+  EXPECT_EQ("Idle",
+            BoxedValue(new Stage(Stage::kIdle)).ToString());
+  EXPECT_EQ("Checking For Update",
+            BoxedValue(new Stage(Stage::kCheckingForUpdate)).ToString());
+  EXPECT_EQ("Update Available",
+            BoxedValue(new Stage(Stage::kUpdateAvailable)).ToString());
+  EXPECT_EQ("Downloading",
+            BoxedValue(new Stage(Stage::kDownloading)).ToString());
+  EXPECT_EQ("Verifying",
+            BoxedValue(new Stage(Stage::kVerifying)).ToString());
+  EXPECT_EQ("Finalizing",
+            BoxedValue(new Stage(Stage::kFinalizing)).ToString());
+  EXPECT_EQ("Updated, Need Reboot",
+            BoxedValue(new Stage(Stage::kUpdatedNeedReboot)).ToString());
+  EXPECT_EQ("Reporting Error Event",
+            BoxedValue(new Stage(Stage::kReportingErrorEvent)).ToString());
+  EXPECT_EQ("Attempting Rollback",
+            BoxedValue(new Stage(Stage::kAttemptingRollback)).ToString());
+}
+
+TEST(PmBoxedValueTest, DeleterMarkerToString) {
+  bool marker = false;
+  BoxedValue value = BoxedValue(new DeleterMarker(&marker));
+  EXPECT_EQ("DeleterMarker:false", value.ToString());
+  marker = true;
+  EXPECT_EQ("DeleterMarker:true", value.ToString());
+}
+
 }  // namespace chromeos_policy_manager