Home | History | Annotate | Download | only in signaling
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "remoting/signaling/server_log_entry_unittest.h"
      6 
      7 #include <sstream>
      8 
      9 #include "testing/gtest/include/gtest/gtest.h"
     10 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h"
     11 
     12 using buzz::QName;
     13 using buzz::XmlAttr;
     14 using buzz::XmlElement;
     15 
     16 namespace remoting {
     17 
     18 const char kJabberClientNamespace[] = "jabber:client";
     19 const char kChromotingNamespace[] = "google:remoting";
     20 
     21 XmlElement* GetLogElementFromStanza(XmlElement* stanza) {
     22   if (stanza->Name() != QName(kJabberClientNamespace, "iq")) {
     23     ADD_FAILURE() << "Expected element 'iq'";
     24     return NULL;
     25   }
     26   XmlElement* log_element = stanza->FirstChild()->AsElement();
     27   if (log_element->Name() != QName(kChromotingNamespace, "log")) {
     28     ADD_FAILURE() << "Expected element 'log'";
     29     return NULL;
     30   }
     31   if (log_element->NextChild()) {
     32     ADD_FAILURE() << "Expected only 1 child of 'iq'";
     33     return NULL;
     34   }
     35   return log_element;
     36 }
     37 
     38 XmlElement* GetSingleLogEntryFromStanza(XmlElement* stanza) {
     39   XmlElement* log_element = GetLogElementFromStanza(stanza);
     40   if (!log_element) {
     41     // Test failure already recorded, so just return NULL here.
     42     return NULL;
     43   }
     44   XmlElement* entry = log_element->FirstChild()->AsElement();
     45   if (entry->Name() != QName(kChromotingNamespace, "entry")) {
     46     ADD_FAILURE() << "Expected element 'entry'";
     47     return NULL;
     48   }
     49   if (entry->NextChild()) {
     50     ADD_FAILURE() << "Expected only 1 child of 'log'";
     51     return NULL;
     52   }
     53   return entry;
     54 }
     55 
     56 bool VerifyStanza(
     57     const std::map<std::string, std::string>& key_value_pairs,
     58     const std::set<std::string> keys,
     59     const XmlElement* elem,
     60     std::string* error) {
     61   int attrCount = 0;
     62   for (const XmlAttr* attr = elem->FirstAttr(); attr != NULL;
     63        attr = attr->NextAttr(), attrCount++) {
     64     if (attr->Name().Namespace().length() != 0) {
     65       *error = "attribute has non-empty namespace " +
     66           attr->Name().Namespace();
     67       return false;
     68     }
     69     const std::string& key = attr->Name().LocalPart();
     70     const std::string& value = attr->Value();
     71     std::map<std::string, std::string>::const_iterator iter =
     72         key_value_pairs.find(key);
     73     if (iter == key_value_pairs.end()) {
     74       if (keys.find(key) == keys.end()) {
     75         *error = "unexpected attribute " + key;
     76         return false;
     77       }
     78     } else {
     79       if (iter->second != value) {
     80         *error = "attribute " + key + " has value " + iter->second +
     81             ": expected " + value;
     82         return false;
     83       }
     84     }
     85   }
     86   int attr_count_expected = key_value_pairs.size() + keys.size();
     87   if (attrCount != attr_count_expected) {
     88     std::stringstream s;
     89     s << "stanza has " << attrCount << " keys: expected "
     90       << attr_count_expected;
     91     *error = s.str();
     92     return false;
     93   }
     94   return true;
     95 }
     96 
     97 }  // namespace remoting
     98