Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / remoting / signaling / server_log_entry_unittest.cc
blobac3506d3746f557e110df58019266d231603f31c
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.
5 #include "remoting/signaling/server_log_entry_unittest.h"
7 #include <sstream>
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h"
12 using buzz::QName;
13 using buzz::XmlAttr;
14 using buzz::XmlElement;
16 namespace remoting {
18 const char kJabberClientNamespace[] = "jabber:client";
19 const char kChromotingNamespace[] = "google:remoting";
21 XmlElement* GetLogElementFromStanza(XmlElement* stanza) {
22 if (stanza->Name() != QName(kJabberClientNamespace, "iq")) {
23 ADD_FAILURE() << "Expected element 'iq'";
24 return nullptr;
26 XmlElement* log_element = stanza->FirstChild()->AsElement();
27 if (log_element->Name() != QName(kChromotingNamespace, "log")) {
28 ADD_FAILURE() << "Expected element 'log'";
29 return nullptr;
31 if (log_element->NextChild()) {
32 ADD_FAILURE() << "Expected only 1 child of 'iq'";
33 return nullptr;
35 return log_element;
38 XmlElement* GetSingleLogEntryFromStanza(XmlElement* stanza) {
39 XmlElement* log_element = GetLogElementFromStanza(stanza);
40 if (!log_element) {
41 // Test failure already recorded, so just return nullptr here.
42 return nullptr;
44 XmlElement* entry = log_element->FirstChild()->AsElement();
45 if (entry->Name() != QName(kChromotingNamespace, "entry")) {
46 ADD_FAILURE() << "Expected element 'entry'";
47 return nullptr;
49 if (entry->NextChild()) {
50 ADD_FAILURE() << "Expected only 1 child of 'log'";
51 return nullptr;
53 return entry;
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 != nullptr;
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;
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;
78 } else {
79 if (iter->second != value) {
80 *error = "attribute " + key + " has value " + iter->second +
81 ": expected " + value;
82 return false;
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;
94 return true;
97 } // namespace remoting