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

Side by Side Diff: remoting/host/host_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 2013 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 "remoting/host/host_status_sender.h"
6
7 #include "base/strings/string_number_conversions.h"
8 #include "remoting/base/constants.h"
9 #include "remoting/base/rsa_key_pair.h"
10 #include "remoting/base/test_rsa_key_pair.h"
11 #include "remoting/jingle_glue/mock_objects.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h"
15
16 using buzz::QName;
17 using buzz::XmlElement;
18
19 using testing::DoAll;
20 using testing::NotNull;
21 using testing::Return;
22 using testing::SaveArg;
23
24 namespace remoting {
25
26 namespace {
27
28 const char kTestBotJid[] = "remotingunittest@bot.talk.google.com";
29 const char kHostId[] = "0";
30 const char kTestJid[] = "user@gmail.com/chromoting123";
31 const char kStanzaId[] = "123";
32
33 // kInvalidHostConfigurationExitCode (see host_exit_codes.h)
34 const uint32 kTestExitCode = 100;
35 const char kTestExitCodeString[] = "INVALID_HOST_CONFIGURATION";
36
37 } // namespace
38
39 class HostStatusSenderTest
40 : public testing::Test {
41 protected:
42 virtual void SetUp() OVERRIDE {
43 key_pair_ = RsaKeyPair::FromString(kTestRsaKeyPair);
44 ASSERT_TRUE(key_pair_.get());
45
46 host_status_sender_.reset(new HostStatusSender(
47 kHostId, &signal_strategy_, key_pair_, kTestBotJid));
48 }
49
50 virtual void TearDown() OVERRIDE {
51 host_status_sender_.reset();
52 }
53
54 void ValidateHostStatusStanza(XmlElement* stanza,
55 HostStatusSender::HostStatus status);
56
57 void ValidateSignature(
58 XmlElement* signature, HostStatusSender::HostStatus status);
59
60 MockSignalStrategy signal_strategy_;
61 scoped_refptr<RsaKeyPair> key_pair_;
62 scoped_ptr<HostStatusSender> host_status_sender_;
63 };
64
65 TEST_F(HostStatusSenderTest, SendOnlineStatus) {
66 XmlElement* sent_iq = NULL;
67 EXPECT_CALL(signal_strategy_, GetState())
68 .WillOnce(Return(SignalStrategy::DISCONNECTED))
69 .WillRepeatedly(Return(SignalStrategy::CONNECTED));
70 EXPECT_CALL(signal_strategy_, GetLocalJid())
71 .WillRepeatedly(Return(kTestJid));
72 EXPECT_CALL(signal_strategy_, GetNextId())
73 .WillOnce(Return(kStanzaId));
74 EXPECT_CALL(signal_strategy_, SendStanzaPtr(NotNull()))
75 .WillOnce(DoAll(SaveArg<0>(&sent_iq), Return(true)));
76
77 // Call SendOnlineStatus twice. The first call should be a
78 // no-op because |signal_strategy_| is diconnected.
79 // So we expect SendStanza to be called only once.
80 host_status_sender_->SendOnlineStatus();
81
82 host_status_sender_->OnSignalStrategyStateChange(
83 SignalStrategy::CONNECTED);
84 host_status_sender_->SendOnlineStatus();
85
86 scoped_ptr<XmlElement> stanza(sent_iq);
87
88 ASSERT_TRUE(stanza != NULL);
89 LOG(INFO) << stanza->Str();
90
91 ValidateHostStatusStanza(stanza.get(), HostStatusSender::ONLINE);
92 }
93
94 TEST_F(HostStatusSenderTest, SendOfflineStatus) {
95 XmlElement* sent_iq = NULL;
96 EXPECT_CALL(signal_strategy_, GetState())
97 .WillOnce(Return(SignalStrategy::DISCONNECTED))
98 .WillRepeatedly(Return(SignalStrategy::CONNECTED));
99 EXPECT_CALL(signal_strategy_, GetLocalJid())
100 .WillRepeatedly(Return(kTestJid));
101 EXPECT_CALL(signal_strategy_, GetNextId())
102 .WillOnce(Return(kStanzaId));
103 EXPECT_CALL(signal_strategy_, SendStanzaPtr(NotNull()))
104 .WillOnce(DoAll(SaveArg<0>(&sent_iq), Return(true)));
105
106 // Call SendOfflineStatus twice. The first call should be a
107 // no-op because |signal_strategy_| is diconnected.
108 // So we expect SendStanza to be called only once.
109 host_status_sender_->SendOfflineStatus(kTestExitCode);
110
111 host_status_sender_->OnSignalStrategyStateChange(
112 SignalStrategy::CONNECTED);
113 host_status_sender_->SendOfflineStatus(kTestExitCode);
114
115 scoped_ptr<XmlElement> stanza(sent_iq);
116
117 ASSERT_TRUE(stanza != NULL);
118 LOG(INFO) << stanza->Str();
119
120 ValidateHostStatusStanza(stanza.get(), HostStatusSender::OFFLINE);
121 }
122
123 // Validate a host status stanza.
124 void HostStatusSenderTest::ValidateHostStatusStanza(
125 XmlElement* stanza, HostStatusSender::HostStatus status) {
126 EXPECT_EQ(stanza->Attr(QName(std::string(), "to")),
127 std::string(kTestBotJid));
128 EXPECT_EQ(stanza->Attr(QName(std::string(), "type")), "set");
129
130 XmlElement* host_status_stanza =
131 stanza->FirstNamed(QName(kChromotingXmlNamespace, "host-status"));
132 ASSERT_TRUE(host_status_stanza != NULL);
133
134 if (status == HostStatusSender::ONLINE) {
135 EXPECT_EQ("ONLINE",
136 host_status_stanza->Attr(
137 QName(kChromotingXmlNamespace, "status")));
138 EXPECT_FALSE(host_status_stanza->HasAttr(
139 QName(kChromotingXmlNamespace, "exit-code")));
140 } else {
141 EXPECT_EQ("OFFLINE",
142 host_status_stanza->Attr(
143 QName(kChromotingXmlNamespace, "status")));
144 EXPECT_EQ(kTestExitCodeString,
145 host_status_stanza->Attr(
146 QName(kChromotingXmlNamespace, "exit-code")));
147 }
148
149 EXPECT_EQ(std::string(kHostId),
150 host_status_stanza->Attr(
151 QName(kChromotingXmlNamespace, "hostid")));
152
153 QName signature_tag(kChromotingXmlNamespace, "signature");
154 XmlElement* signature = host_status_stanza->FirstNamed(signature_tag);
155 ASSERT_TRUE(signature != NULL);
156 EXPECT_TRUE(host_status_stanza->NextNamed(signature_tag) == NULL);
157
158 ValidateSignature(signature, status);
159 }
160
161 // Validate the signature.
162 void HostStatusSenderTest::ValidateSignature(
163 XmlElement* signature, HostStatusSender::HostStatus status) {
164
165 EXPECT_TRUE(signature->HasAttr(
166 QName(kChromotingXmlNamespace, "time")));
167
168 std::string time_str =
169 signature->Attr(QName(kChromotingXmlNamespace, "time"));
170
171 int64 time;
172 ASSERT_TRUE(base::StringToInt64(time_str, &time));
173
174 std::string message;
175 message += kTestJid;
176 message += " ";
177 message += time_str;
178 message += " ";
179
180 if (status == HostStatusSender::OFFLINE) {
181 message += "OFFLINE";
182 message += " ";
183 message += kTestExitCodeString;
184 } else {
185 message += "ONLINE";
186 }
187
188 scoped_refptr<RsaKeyPair> key_pair = RsaKeyPair::FromString(kTestRsaKeyPair);
189 ASSERT_TRUE(key_pair.get());
190
191 std::string expected_signature =
192 key_pair->SignMessage(message);
193 EXPECT_EQ(expected_signature, signature->BodyText());
194
195 int64 now = static_cast<int64>(base::Time::Now().ToDoubleT());
196 LOG(INFO) << "SendHostStatus took " << now - time << " seconds.";
197 }
198
199 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698