dbus_send: correctly parse new dbus message header

dbus-1.10.12 has changed the message header a bit. Noticeably, it now
includes an optional "time=" field, and a mandatory "serial=" field.
Also, "dest=" is changed to "destination="

https://cgit.freedesktop.org/dbus/dbus/tree/tools/dbus-print-message.c?id=dbus-1.10.12#n545
https://cgit.freedesktop.org/dbus/dbus/tree/tools/dbus-print-message.c?id=dbus-1.10.12#n572

For easier transition, the regex for older header format is kept. That
way this CL can go in before the dbus upgrade, and we could delete it
easily when that happens.

BUG=chromium:704365, b:34841890
TEST=test_that -b x86-generic ... peerd_DiscoverServices peerd_AdvertiseServices

Change-Id: I032706f681537dbe880d358b8c832a86c8ce3a35
Reviewed-on: https://chromium-review.googlesource.com/465546
Commit-Ready: Daniel Wang <wonderfly@google.com>
Tested-by: Daniel Wang <wonderfly@google.com>
Reviewed-by: Mike Frysinger <vapier@chromium.org>
diff --git a/client/common_lib/cros/dbus_send.py b/client/common_lib/cros/dbus_send.py
index d20312b..915c321 100644
--- a/client/common_lib/cros/dbus_send.py
+++ b/client/common_lib/cros/dbus_send.py
@@ -85,7 +85,8 @@
     localhost ~ # dbus-send --system --dest=org.chromium.flimflam \
             --print-reply --reply-timeout=2000 / \
             org.chromium.flimflam.Manager.GetProperties
-    method return sender=:1.12 -> dest=:1.37 reply_serial=2
+    method return time=1490931987.170070 sender=:1.12 -> destination=:1.37 \
+        serial=6 reply_serial=2
        array [
           dict entry(
              string "ActiveProfile"
@@ -106,9 +107,19 @@
     # The first line contains meta-information about the response
     header = lines[0]
     lines = lines[1:]
-    dbus_address_pattern = '[:\d\\.]+'
-    match = re.match('method return sender=(%s) -> dest=(%s) reply_serial=\d+' %
+    dbus_address_pattern = r'[:\d\\.]+'
+    # The header may or may not have a time= field.
+    match = re.match(r'method return (time=[\d\\.]+ )?sender=(%s) -> '
+                     r'destination=(%s) serial=\d+ reply_serial=\d+' %
                      (dbus_address_pattern, dbus_address_pattern), header)
+
+    # For backward compatibility, match old dbus-send output too.
+    # TODO: drop this when dbus is upgraded to 1.10.12+.
+    if match is None:
+      match = re.match(r'method return sender=(%s) -> dest=(%s) '
+                       r'reply_serial=\d+' %
+                       (dbus_address_pattern, dbus_address_pattern), header)
+
     if match is None:
         raise error.TestError('Could not parse dbus-send header: %s' % header)