Index: remoting/host/offline_status_sender_unittest.cc |
diff --git a/remoting/host/offline_status_sender_unittest.cc b/remoting/host/offline_status_sender_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..61c784dac2997866b6ee25d4638d05b99dd44406 |
--- /dev/null |
+++ b/remoting/host/offline_status_sender_unittest.cc |
@@ -0,0 +1,119 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
rmsousa
2013/07/02 02:09:24
Nit: remove (c), change to 2013 in all new files
weitao
2013/07/03 19:23:31
Done.
|
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "remoting/host/offline_status_sender.h" |
+ |
+#include "remoting/base/constants.h" |
+#include "remoting/base/rsa_key_pair.h" |
+#include "remoting/base/test_rsa_key_pair.h" |
+#include "remoting/jingle_glue/mock_objects.h" |
+#include "testing/gmock/include/gmock/gmock.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "third_party/libjingle/source/talk/xmllite/xmlelement.h" |
+ |
+using buzz::QName; |
+using buzz::XmlElement; |
+ |
+using testing::DoAll; |
+using testing::NotNull; |
+using testing::Return; |
+using testing::SaveArg; |
+ |
+namespace remoting { |
+ |
+namespace { |
+ |
+const char kTestBotJid[] = "remotingunittest@bot.talk.google.com"; |
+const char kHostId[] = "0"; |
+const char kTestJid[] = "user@gmail.com/chromoting123"; |
+const char kStanzaId[] = "123"; |
+const uint32 kTestOfflineStatus = 100; |
+const char kTestOfflineStatusString[] = "100"; |
+const uint32 kTestErrorCode = 5; |
+const char kTestErrorCodeString[] = "5"; |
+ |
+} // namespace |
+ |
+class OfflineStatusSenderTest |
+ : public testing::Test { |
+ protected: |
+ virtual void SetUp() OVERRIDE { |
+ key_pair_ = RsaKeyPair::FromString(kTestRsaKeyPair); |
+ ASSERT_TRUE(key_pair_.get()); |
+ |
+ offline_status_sender_.reset(new OfflineStatusSender( |
+ kHostId, &signal_strategy_, key_pair_, kTestBotJid)); |
+ } |
+ |
+ virtual void TearDown() OVERRIDE { |
+ offline_status_sender_.reset(); |
+ } |
+ |
+ void ValidateOfflineStatusStanza(XmlElement* stanza); |
+ |
+ MockSignalStrategy signal_strategy_; |
+ scoped_refptr<RsaKeyPair> key_pair_; |
+ scoped_ptr<OfflineStatusSender> offline_status_sender_; |
+}; |
+ |
+TEST_F(OfflineStatusSenderTest, SendOfflineStatus) { |
+ XmlElement* sent_iq = NULL; |
+ EXPECT_CALL(signal_strategy_, GetState()) |
+ .WillOnce(Return(SignalStrategy::DISCONNECTED)) |
+ .WillRepeatedly(Return(SignalStrategy::CONNECTED)); |
+ EXPECT_CALL(signal_strategy_, GetLocalJid()) |
+ .WillRepeatedly(Return(kTestJid)); |
+ EXPECT_CALL(signal_strategy_, GetNextId()) |
+ .WillOnce(Return(kStanzaId)); |
+ EXPECT_CALL(signal_strategy_, SendStanzaPtr(NotNull())) |
+ .WillOnce(DoAll(SaveArg<0>(&sent_iq), Return(true))); |
+ |
+ // Call SendOfflineStatus twice. The first call should be a |
+ // no-op because |signal_strategy_| is diconnected. |
+ // So we expect SendStanza to be called only once. |
+ offline_status_sender_->SendOfflineStatus(kTestOfflineStatus); |
+ |
+ offline_status_sender_->OnSignalStrategyStateChange( |
+ SignalStrategy::CONNECTED); |
+ offline_status_sender_->SendOfflineStatus(kTestOfflineStatus, kTestErrorCode); |
+ |
+ scoped_ptr<XmlElement> stanza(sent_iq); |
+ |
+ ASSERT_TRUE(stanza != NULL); |
+ LOG(INFO) << stanza->Str(); |
+ |
+ ValidateOfflineStatusStanza(stanza.get()); |
+} |
+ |
+// Validate an offline status stanza. |
+void OfflineStatusSenderTest::ValidateOfflineStatusStanza(XmlElement* stanza) { |
+ EXPECT_EQ(stanza->Attr(QName(std::string(), "to")), |
+ std::string(kTestBotJid)); |
+ EXPECT_EQ(stanza->Attr(QName(std::string(), "type")), "set"); |
+ XmlElement* offline_status_stanza = |
+ stanza->FirstNamed(QName(kChromotingXmlNamespace, "offline-status")); |
+ ASSERT_TRUE(offline_status_stanza != NULL); |
+ EXPECT_EQ(kTestOfflineStatusString, |
+ offline_status_stanza->Attr( |
+ QName(kChromotingXmlNamespace, "status"))); |
+ EXPECT_EQ(kTestErrorCodeString, |
+ offline_status_stanza->Attr( |
+ QName(kChromotingXmlNamespace, "errorcode"))); |
+ EXPECT_EQ(std::string(kHostId), |
+ offline_status_stanza->Attr( |
+ QName(kChromotingXmlNamespace, "hostid"))); |
+ |
+ QName signature_tag(kChromotingXmlNamespace, "signature"); |
+ XmlElement* signature = offline_status_stanza->FirstNamed(signature_tag); |
+ ASSERT_TRUE(signature != NULL); |
+ EXPECT_TRUE(offline_status_stanza->NextNamed(signature_tag) == NULL); |
+ |
+ scoped_refptr<RsaKeyPair> key_pair = RsaKeyPair::FromString(kTestRsaKeyPair); |
+ ASSERT_TRUE(key_pair.get()); |
+ std::string expected_signature = |
+ key_pair->SignMessage(std::string(kTestJid)); |
+ EXPECT_EQ(expected_signature, signature->BodyText()); |
+} |
+ |
+} // namespace remoting |