Home | History | Annotate | Download | only in host
      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.
      4 
      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"
     10 
     11 using buzz::XmlAttr;
     12 using buzz::XmlElement;
     13 
     14 namespace remoting {
     15 
     16 class ServerLogEntryTest : public testing::Test {
     17  protected:
     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,
     26       std::string* error) {
     27     int attrCount = 0;
     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();
     33         return false;
     34       }
     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;
     42           return false;
     43         }
     44       } else {
     45         if (iter->second != value) {
     46           *error = "attribute " + key + " has value " + iter->second +
     47               ": expected " + value;
     48           return false;
     49         }
     50       }
     51     }
     52     int attr_count_expected = key_value_pairs.size() + keys.size();
     53     if (attrCount != attr_count_expected) {
     54       std::stringstream s;
     55       s << "stanza has " << attrCount << " keys: expected "
     56         << attr_count_expected;
     57       *error = s.str();
     58       return false;
     59     }
     60     return true;
     61   }
     62 };
     63 
     64 TEST_F(ServerLogEntryTest, MakeForSessionStateChange) {
     65   scoped_ptr<ServerLogEntry> entry(
     66       ServerLogEntry::MakeForSessionStateChange(true));
     67   scoped_ptr<XmlElement> stanza = entry->ToStanza();
     68   std::string error;
     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))
     75       << error;
     76 }
     77 
     78 TEST_F(ServerLogEntryTest, MakeForHeartbeat) {
     79   scoped_ptr<ServerLogEntry> entry(ServerLogEntry::MakeForHeartbeat());
     80   scoped_ptr<XmlElement> stanza = entry->ToStanza();
     81   std::string error;
     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))
     87       << error;
     88 }
     89 
     90 TEST_F(ServerLogEntryTest, AddHostFields) {
     91   scoped_ptr<ServerLogEntry> entry(
     92       ServerLogEntry::MakeForSessionStateChange(true));
     93   entry->AddHostFields();
     94   scoped_ptr<XmlElement> stanza = entry->ToStanza();
     95   std::string error;
     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;
    101   keys.insert("cpu");
    102 #if defined(OS_WIN)
    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";
    113 #endif
    114   key_value_pairs["host-version"] = STRINGIZE(VERSION);
    115   ASSERT_TRUE(VerifyStanza(key_value_pairs, keys, stanza.get(), &error)) <<
    116       error;
    117 }
    118 
    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();
    124   std::string error;
    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)) <<
    132       error;
    133 }
    134 
    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();
    140   std::string error;
    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)) <<
    148       error;
    149 }
    150 
    151 }  // namespace remoting
    152