Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(267)

Side by Side Diff: remoting/host/offline_status_sender_unittest.cc

Issue 18075003: Host offline status reporting. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // 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.
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 "remoting/host/offline_status_sender.h"
6
7 #include "remoting/base/constants.h"
8 #include "remoting/base/rsa_key_pair.h"
9 #include "remoting/base/test_rsa_key_pair.h"
10 #include "remoting/jingle_glue/mock_objects.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h"
14
15 using buzz::QName;
16 using buzz::XmlElement;
17
18 using testing::DoAll;
19 using testing::NotNull;
20 using testing::Return;
21 using testing::SaveArg;
22
23 namespace remoting {
24
25 namespace {
26
27 const char kTestBotJid[] = "remotingunittest@bot.talk.google.com";
28 const char kHostId[] = "0";
29 const char kTestJid[] = "user@gmail.com/chromoting123";
30 const char kStanzaId[] = "123";
31 const uint32 kTestOfflineStatus = 100;
32 const char kTestOfflineStatusString[] = "100";
33 const uint32 kTestErrorCode = 5;
34 const char kTestErrorCodeString[] = "5";
35
36 } // namespace
37
38 class OfflineStatusSenderTest
39 : public testing::Test {
40 protected:
41 virtual void SetUp() OVERRIDE {
42 key_pair_ = RsaKeyPair::FromString(kTestRsaKeyPair);
43 ASSERT_TRUE(key_pair_.get());
44
45 offline_status_sender_.reset(new OfflineStatusSender(
46 kHostId, &signal_strategy_, key_pair_, kTestBotJid));
47 }
48
49 virtual void TearDown() OVERRIDE {
50 offline_status_sender_.reset();
51 }
52
53 void ValidateOfflineStatusStanza(XmlElement* stanza);
54
55 MockSignalStrategy signal_strategy_;
56 scoped_refptr<RsaKeyPair> key_pair_;
57 scoped_ptr<OfflineStatusSender> offline_status_sender_;
58 };
59
60 TEST_F(OfflineStatusSenderTest, SendOfflineStatus) {
61 XmlElement* sent_iq = NULL;
62 EXPECT_CALL(signal_strategy_, GetState())
63 .WillOnce(Return(SignalStrategy::DISCONNECTED))
64 .WillRepeatedly(Return(SignalStrategy::CONNECTED));
65 EXPECT_CALL(signal_strategy_, GetLocalJid())
66 .WillRepeatedly(Return(kTestJid));
67 EXPECT_CALL(signal_strategy_, GetNextId())
68 .WillOnce(Return(kStanzaId));
69 EXPECT_CALL(signal_strategy_, SendStanzaPtr(NotNull()))
70 .WillOnce(DoAll(SaveArg<0>(&sent_iq), Return(true)));
71
72 // Call SendOfflineStatus twice. The first call should be a
73 // no-op because |signal_strategy_| is diconnected.
74 // So we expect SendStanza to be called only once.
75 offline_status_sender_->SendOfflineStatus(kTestOfflineStatus);
76
77 offline_status_sender_->OnSignalStrategyStateChange(
78 SignalStrategy::CONNECTED);
79 offline_status_sender_->SendOfflineStatus(kTestOfflineStatus, kTestErrorCode);
80
81 scoped_ptr<XmlElement> stanza(sent_iq);
82
83 ASSERT_TRUE(stanza != NULL);
84 LOG(INFO) << stanza->Str();
85
86 ValidateOfflineStatusStanza(stanza.get());
87 }
88
89 // Validate an offline status stanza.
90 void OfflineStatusSenderTest::ValidateOfflineStatusStanza(XmlElement* stanza) {
91 EXPECT_EQ(stanza->Attr(QName(std::string(), "to")),
92 std::string(kTestBotJid));
93 EXPECT_EQ(stanza->Attr(QName(std::string(), "type")), "set");
94 XmlElement* offline_status_stanza =
95 stanza->FirstNamed(QName(kChromotingXmlNamespace, "offline-status"));
96 ASSERT_TRUE(offline_status_stanza != NULL);
97 EXPECT_EQ(kTestOfflineStatusString,
98 offline_status_stanza->Attr(
99 QName(kChromotingXmlNamespace, "status")));
100 EXPECT_EQ(kTestErrorCodeString,
101 offline_status_stanza->Attr(
102 QName(kChromotingXmlNamespace, "errorcode")));
103 EXPECT_EQ(std::string(kHostId),
104 offline_status_stanza->Attr(
105 QName(kChromotingXmlNamespace, "hostid")));
106
107 QName signature_tag(kChromotingXmlNamespace, "signature");
108 XmlElement* signature = offline_status_stanza->FirstNamed(signature_tag);
109 ASSERT_TRUE(signature != NULL);
110 EXPECT_TRUE(offline_status_stanza->NextNamed(signature_tag) == NULL);
111
112 scoped_refptr<RsaKeyPair> key_pair = RsaKeyPair::FromString(kTestRsaKeyPair);
113 ASSERT_TRUE(key_pair.get());
114 std::string expected_signature =
115 key_pair->SignMessage(std::string(kTestJid));
116 EXPECT_EQ(expected_signature, signature->BodyText());
117 }
118
119 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698