buffet: Fix constant retries in XMPP on authorization failure

When XMPP channel is rejected due to invalid credentials, we did not
close the channel stream properly after server had sent "</stream:stream>
element which lead to numerous stream tear-down requests from the server.

Once we receive a stream closed request from the server, we must close
the actual stream/socket immediately.

BUG=brillo:1215
TEST=`FEATURES=test emerge-link buffet`
     Test on the device.

Change-Id: I924e72cd3e647bedf6e3138b55aed146259d52f1
Reviewed-on: https://chromium-review.googlesource.com/281392
Tested-by: Alex Vakulenko <avakulenko@chromium.org>
Trybot-Ready: Alex Vakulenko <avakulenko@chromium.org>
Reviewed-by: Vitaly Buka <vitalybuka@chromium.org>
Commit-Queue: Alex Vakulenko <avakulenko@chromium.org>
diff --git a/buffet/notification/xmpp_channel.cc b/buffet/notification/xmpp_channel.cc
index fbe2f95..bf9e0b9 100644
--- a/buffet/notification/xmpp_channel.cc
+++ b/buffet/notification/xmpp_channel.cc
@@ -121,6 +121,7 @@
 
 void XmppChannel::OnStreamEnd(const std::string& node_name) {
   VLOG(2) << "XMPP stream ended: " << node_name;
+  Stop();
   if (IsConnected()) {
     // If we had a fully-established connection, restart it now.
     // However, if the connection has never been established yet (e.g.
@@ -344,7 +345,7 @@
 }
 
 void XmppChannel::WaitForMessage() {
-  if (read_pending_)
+  if (read_pending_ || !stream_)
     return;
 
   chromeos::ErrorPtr error;
diff --git a/buffet/notification/xmpp_channel_unittest.cc b/buffet/notification/xmpp_channel_unittest.cc
index d16e81e..80a9af8 100644
--- a/buffet/notification/xmpp_channel_unittest.cc
+++ b/buffet/notification/xmpp_channel_unittest.cc
@@ -41,7 +41,7 @@
     "<success xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\"/>";
 constexpr char kAuthenticationFailedResponse[] =
     "<failure xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\"><not-authorized/>"
-    "</failure></stream:stream>";
+    "</failure>";
 constexpr char kRestartStreamResponse[] =
     "<stream:features><bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\"/>"
     "<session xmlns=\"urn:ietf:params:xml:ns:xmpp-session\"/>"