1 // Copyright (c) 2012 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 "base/memory/scoped_ptr.h"
6 #include "base/strings/stringize_macros.h"
7 #include "remoting/host/server_log_entry.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h"
12 using buzz::XmlElement
;
16 class ServerLogEntryTest
: public testing::Test
{
18 // Verifies a logging stanza.
19 // |keyValuePairs| lists the keys that must have specified values, and |keys|
20 // lists the keys that must be present, but may have arbitrary values.
21 // There must be no other keys.
22 static bool VerifyStanza(
23 const std::map
<std::string
, std::string
>& key_value_pairs
,
24 const std::set
<std::string
> keys
,
25 const XmlElement
* elem
,
28 for (const XmlAttr
* attr
= elem
->FirstAttr(); attr
!= NULL
;
29 attr
= attr
->NextAttr(), attrCount
++) {
30 if (attr
->Name().Namespace().length() != 0) {
31 *error
= "attribute has non-empty namespace " +
32 attr
->Name().Namespace();
35 const std::string
& key
= attr
->Name().LocalPart();
36 const std::string
& value
= attr
->Value();
37 std::map
<std::string
, std::string
>::const_iterator iter
=
38 key_value_pairs
.find(key
);
39 if (iter
== key_value_pairs
.end()) {
40 if (keys
.find(key
) == keys
.end()) {
41 *error
= "unexpected attribute " + key
;
45 if (iter
->second
!= value
) {
46 *error
= "attribute " + key
+ " has value " + iter
->second
+
47 ": expected " + value
;
52 int attr_count_expected
= key_value_pairs
.size() + keys
.size();
53 if (attrCount
!= attr_count_expected
) {
55 s
<< "stanza has " << attrCount
<< " keys: expected "
56 << attr_count_expected
;
64 TEST_F(ServerLogEntryTest
, MakeForSessionStateChange
) {
65 scoped_ptr
<ServerLogEntry
> entry(
66 ServerLogEntry::MakeForSessionStateChange(true));
67 scoped_ptr
<XmlElement
> stanza
= entry
->ToStanza();
69 std::map
<std::string
, std::string
> key_value_pairs
;
70 key_value_pairs
["role"] = "host";
71 key_value_pairs
["event-name"] = "session-state";
72 key_value_pairs
["session-state"] = "connected";
73 std::set
<std::string
> keys
;
74 ASSERT_TRUE(VerifyStanza(key_value_pairs
, keys
, stanza
.get(), &error
))
78 TEST_F(ServerLogEntryTest
, MakeForHeartbeat
) {
79 scoped_ptr
<ServerLogEntry
> entry(ServerLogEntry::MakeForHeartbeat());
80 scoped_ptr
<XmlElement
> stanza
= entry
->ToStanza();
82 std::map
<std::string
, std::string
> key_value_pairs
;
83 key_value_pairs
["role"] = "host";
84 key_value_pairs
["event-name"] = "heartbeat";
85 std::set
<std::string
> keys
;
86 ASSERT_TRUE(VerifyStanza(key_value_pairs
, keys
, stanza
.get(), &error
))
90 TEST_F(ServerLogEntryTest
, AddHostFields
) {
91 scoped_ptr
<ServerLogEntry
> entry(
92 ServerLogEntry::MakeForSessionStateChange(true));
93 entry
->AddHostFields();
94 scoped_ptr
<XmlElement
> stanza
= entry
->ToStanza();
96 std::map
<std::string
, std::string
> key_value_pairs
;
97 key_value_pairs
["role"] = "host";
98 key_value_pairs
["event-name"] = "session-state";
99 key_value_pairs
["session-state"] = "connected";
100 std::set
<std::string
> keys
;
103 key_value_pairs
["os-name"] = "Windows";
104 keys
.insert("os-version");
105 #elif defined(OS_MACOSX)
106 key_value_pairs
["os-name"] = "Mac";
107 keys
.insert("os-version");
108 #elif defined(OS_CHROMEOS)
109 key_value_pairs
["os-name"] = "ChromeOS";
110 keys
.insert("os-version");
111 #elif defined(OS_LINUX)
112 key_value_pairs
["os-name"] = "Linux";
114 key_value_pairs
["host-version"] = STRINGIZE(VERSION
);
115 ASSERT_TRUE(VerifyStanza(key_value_pairs
, keys
, stanza
.get(), &error
)) <<
119 TEST_F(ServerLogEntryTest
, AddModeField1
) {
120 scoped_ptr
<ServerLogEntry
> entry(
121 ServerLogEntry::MakeForSessionStateChange(true));
122 entry
->AddModeField(ServerLogEntry::IT2ME
);
123 scoped_ptr
<XmlElement
> stanza
= entry
->ToStanza();
125 std::map
<std::string
, std::string
> key_value_pairs
;
126 key_value_pairs
["role"] = "host";
127 key_value_pairs
["event-name"] = "session-state";
128 key_value_pairs
["session-state"] = "connected";
129 key_value_pairs
["mode"] = "it2me";
130 std::set
<std::string
> keys
;
131 ASSERT_TRUE(VerifyStanza(key_value_pairs
, keys
, stanza
.get(), &error
)) <<
135 TEST_F(ServerLogEntryTest
, AddModeField2
) {
136 scoped_ptr
<ServerLogEntry
> entry(
137 ServerLogEntry::MakeForSessionStateChange(true));
138 entry
->AddModeField(ServerLogEntry::ME2ME
);
139 scoped_ptr
<XmlElement
> stanza
= entry
->ToStanza();
141 std::map
<std::string
, std::string
> key_value_pairs
;
142 key_value_pairs
["role"] = "host";
143 key_value_pairs
["event-name"] = "session-state";
144 key_value_pairs
["session-state"] = "connected";
145 key_value_pairs
["mode"] = "me2me";
146 std::set
<std::string
> keys
;
147 ASSERT_TRUE(VerifyStanza(key_value_pairs
, keys
, stanza
.get(), &error
)) <<
151 } // namespace remoting