More relaxed machine info parsing.

It turns out that dump_vpd_log output may contain comment lines. In
order to handle garbled output more gracefully, ignore lines that
don't contain valid key-value pairs.

BUG=chromium:371776
TEST=unit tests.

Change-Id: I4d82777ee9e305734729df51df609ebe48bdb0df
Reviewed-on: https://chromium-review.googlesource.com/199061
Reviewed-by: Chris Masone <cmasone@chromium.org>
Commit-Queue: Mattias Nissler <mnissler@chromium.org>
Tested-by: Mattias Nissler <mnissler@chromium.org>
diff --git a/server_backed_state_key_generator.cc b/server_backed_state_key_generator.cc
index e452957..c79edfe 100644
--- a/server_backed_state_key_generator.cc
+++ b/server_backed_state_key_generator.cc
@@ -73,20 +73,27 @@
     const std::string& data,
     std::map<std::string, std::string>* params) {
   params->clear();
+
+  // Parse the name-value pairs list. The return value of
+  // SplitStringIntoKeyValuePairs is deliberately ignored in order to handle
+  // comment lines (those start with a #) emitted by dump_vpd_log.
   base::StringPairs pairs;
-  if (!base::SplitStringIntoKeyValuePairs(data, '=', '\n', &pairs))
-    return false;
+  base::SplitStringIntoKeyValuePairs(data, '=', '\n', &pairs);
 
   for (base::StringPairs::const_iterator pair(pairs.begin());
        pair != pairs.end();
        ++pair) {
     std::string name;
     base::TrimString(pair->first, kTrimChars, &name);
+    if (name.empty())
+      continue;
+
     std::string value;
     base::TrimString(pair->second, kTrimChars, &value);
     (*params)[name] = value;
   }
-  return true;
+
+  return !params->empty();
 }
 
 bool ServerBackedStateKeyGenerator::InitMachineInfo(
diff --git a/server_backed_state_key_generator_unittest.cc b/server_backed_state_key_generator_unittest.cc
index 5e03a2f..0af928c 100644
--- a/server_backed_state_key_generator_unittest.cc
+++ b/server_backed_state_key_generator_unittest.cc
@@ -159,6 +159,7 @@
   std::map<std::string, std::string> params;
   EXPECT_TRUE(ServerBackedStateKeyGenerator::ParseMachineInfo(
       "\"serial_number\"=\"fake-machine-serial-number\"\n"
+      "# This is a comment.\n"
       "root_disk_serial_number=fake-disk-serial-number\n",
       &params));
   EXPECT_EQ(2, params.size());