server_log_entry_unittest.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #include "remoting/signaling/server_log_entry_unittest.h"
  5. #include <sstream>
  6. #include "testing/gtest/include/gtest/gtest.h"
  7. #include "third_party/libjingle_xmpp/xmllite/xmlelement.h"
  8. using jingle_xmpp::QName;
  9. using jingle_xmpp::XmlAttr;
  10. using jingle_xmpp::XmlElement;
  11. namespace remoting {
  12. const char kJabberClientNamespace[] = "jabber:client";
  13. const char kChromotingNamespace[] = "google:remoting";
  14. XmlElement* GetLogElementFromStanza(XmlElement* stanza) {
  15. if (stanza->Name() != QName(kJabberClientNamespace, "iq")) {
  16. ADD_FAILURE() << "Expected element 'iq'";
  17. return nullptr;
  18. }
  19. XmlElement* log_element = stanza->FirstChild()->AsElement();
  20. if (log_element->Name() != QName(kChromotingNamespace, "log")) {
  21. ADD_FAILURE() << "Expected element 'log'";
  22. return nullptr;
  23. }
  24. if (log_element->NextChild()) {
  25. ADD_FAILURE() << "Expected only 1 child of 'iq'";
  26. return nullptr;
  27. }
  28. return log_element;
  29. }
  30. XmlElement* GetSingleLogEntryFromStanza(XmlElement* stanza) {
  31. XmlElement* log_element = GetLogElementFromStanza(stanza);
  32. if (!log_element) {
  33. // Test failure already recorded, so just return nullptr here.
  34. return nullptr;
  35. }
  36. XmlElement* entry = log_element->FirstChild()->AsElement();
  37. if (entry->Name() != QName(kChromotingNamespace, "entry")) {
  38. ADD_FAILURE() << "Expected element 'entry'";
  39. return nullptr;
  40. }
  41. if (entry->NextChild()) {
  42. ADD_FAILURE() << "Expected only 1 child of 'log'";
  43. return nullptr;
  44. }
  45. return entry;
  46. }
  47. bool VerifyStanza(
  48. const std::map<std::string, std::string>& key_value_pairs,
  49. const std::set<std::string> keys,
  50. const XmlElement* elem,
  51. std::string* error) {
  52. int attrCount = 0;
  53. for (const XmlAttr* attr = elem->FirstAttr(); attr != nullptr;
  54. attr = attr->NextAttr(), attrCount++) {
  55. if (attr->Name().Namespace().length() != 0) {
  56. *error = "attribute has non-empty namespace " +
  57. attr->Name().Namespace();
  58. return false;
  59. }
  60. const std::string& key = attr->Name().LocalPart();
  61. const std::string& value = attr->Value();
  62. auto iter = key_value_pairs.find(key);
  63. if (iter == key_value_pairs.end()) {
  64. if (keys.find(key) == keys.end()) {
  65. *error = "unexpected attribute " + key;
  66. return false;
  67. }
  68. } else {
  69. if (iter->second != value) {
  70. *error = "attribute " + key + " has value " + iter->second +
  71. ": expected " + value;
  72. return false;
  73. }
  74. }
  75. }
  76. int attr_count_expected = key_value_pairs.size() + keys.size();
  77. if (attrCount != attr_count_expected) {
  78. std::stringstream s;
  79. s << "stanza has " << attrCount << " keys: expected "
  80. << attr_count_expected;
  81. *error = s.str();
  82. return false;
  83. }
  84. return true;
  85. }
  86. } // namespace remoting