buffet: Handle XMPP authentication failures

For the nightly tests there is a fake GCD server used, and as a result
the XMPP client can't authenticate with the XMPP server (since the
access_token is also fake). This simply closes the XMPP connection if we
see a read fail, so that we don't get spammed with 0 size reads.

TEST=manual and FEATURES=test emerge-whirlwind buffet
BUG=brillo:338

Change-Id: Ie8db114fcc197f816f3cb56673d6f393fa05c8e5
Reviewed-on: https://chromium-review.googlesource.com/251587
Reviewed-by: David Zeuthen <zeuthen@chromium.org>
Reviewed-by: Alex Vakulenko <avakulenko@chromium.org>
Commit-Queue: Nathan Bullock <nathanbullock@google.com>
Tested-by: Nathan Bullock <nathanbullock@google.com>
diff --git a/buffet/device_registration_info.cc b/buffet/device_registration_info.cc
index 5851f04..3e8fd00 100644
--- a/buffet/device_registration_info.cc
+++ b/buffet/device_registration_info.cc
@@ -373,6 +373,17 @@
   xmpp_client_->StartStream();
 }
 
+void DeviceRegistrationInfo::OnFileCanReadWithoutBlocking(int fd) {
+  if (xmpp_client_ && xmpp_client_->GetFileDescriptor() == fd) {
+    if (!xmpp_client_->Read()) {
+      // Authentication failed or the socket was closed.
+      if (!fd_watcher_.StopWatchingFileDescriptor()) {
+        LOG(WARNING) << "Failed to stop the watcher";
+      }
+    }
+  }
+}
+
 std::unique_ptr<base::DictionaryValue>
 DeviceRegistrationInfo::BuildDeviceResource(chromeos::ErrorPtr* error) {
   std::unique_ptr<base::DictionaryValue> commands =