OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/message_loop.h" | 5 #include "base/message_loop.h" |
6 #include "base/message_loop_proxy.h" | 6 #include "base/message_loop_proxy.h" |
7 #include "base/ref_counted.h" | 7 #include "base/ref_counted.h" |
8 #include "base/string_number_conversions.h" | 8 #include "base/string_number_conversions.h" |
9 #include "remoting/base/constants.h" | 9 #include "remoting/base/constants.h" |
10 #include "remoting/host/heartbeat_sender.h" | 10 #include "remoting/host/heartbeat_sender.h" |
11 #include "remoting/host/host_key_pair.h" | 11 #include "remoting/host/host_key_pair.h" |
12 #include "remoting/host/in_memory_host_config.h" | 12 #include "remoting/host/in_memory_host_config.h" |
13 #include "remoting/host/test_key_pair.h" | 13 #include "remoting/host/test_key_pair.h" |
14 #include "remoting/jingle_glue/iq_request.h" | 14 #include "remoting/jingle_glue/iq_request.h" |
15 #include "remoting/jingle_glue/jingle_client.h" | 15 #include "remoting/jingle_glue/jingle_client.h" |
16 #include "remoting/jingle_glue/jingle_thread.h" | 16 #include "remoting/jingle_glue/jingle_thread.h" |
17 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
19 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" | 19 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" |
20 #include "third_party/libjingle/source/talk/xmpp/constants.h" | 20 #include "third_party/libjingle/source/talk/xmpp/constants.h" |
21 | 21 |
| 22 using buzz::QName; |
| 23 using buzz::XmlElement; |
| 24 |
22 using testing::_; | 25 using testing::_; |
23 using testing::DeleteArg; | 26 using testing::DeleteArg; |
24 using testing::DoAll; | 27 using testing::DoAll; |
25 using testing::NotNull; | 28 using testing::NotNull; |
26 using testing::Return; | 29 using testing::Return; |
27 | 30 |
28 namespace remoting { | 31 namespace remoting { |
29 | 32 |
30 namespace { | 33 namespace { |
31 const char kHostId[] = "0"; | 34 const char kHostId[] = "0"; |
32 const char kTestJid[] = "user@gmail.com/chromoting123"; | 35 const char kTestJid[] = "user@gmail.com/chromoting123"; |
33 const int64 kTestTime = 123123123; | 36 const int64 kTestTime = 123123123; |
34 } // namespace | 37 } // namespace |
35 | 38 |
36 class MockJingleClient : public JingleClient { | 39 class MockJingleClient : public JingleClient { |
37 public: | 40 public: |
38 explicit MockJingleClient(JingleThread* thread) : JingleClient(thread) { } | 41 explicit MockJingleClient(JingleThread* thread) : JingleClient(thread) { } |
39 MOCK_METHOD0(CreateIqRequest, IqRequest*()); | 42 MOCK_METHOD0(CreateIqRequest, IqRequest*()); |
40 }; | 43 }; |
41 | 44 |
42 class MockIqRequest : public IqRequest { | 45 class MockIqRequest : public IqRequest { |
43 public: | 46 public: |
44 explicit MockIqRequest(JingleClient* jingle_client) | 47 explicit MockIqRequest(JingleClient* jingle_client) |
45 : IqRequest(jingle_client) { | 48 : IqRequest(jingle_client) { |
46 } | 49 } |
47 MOCK_METHOD3(SendIq, void(const std::string& type, | 50 MOCK_METHOD3(SendIq, void(const std::string& type, |
48 const std::string& addressee, | 51 const std::string& addressee, |
49 buzz::XmlElement* iq_body)); | 52 XmlElement* iq_body)); |
50 }; | 53 }; |
51 | 54 |
52 class HeartbeatSenderTest : public testing::Test { | 55 class HeartbeatSenderTest : public testing::Test { |
53 protected: | 56 protected: |
54 class TestConfigUpdater : | 57 class TestConfigUpdater : |
55 public base::RefCountedThreadSafe<TestConfigUpdater> { | 58 public base::RefCountedThreadSafe<TestConfigUpdater> { |
56 public: | 59 public: |
57 void DoUpdate(scoped_refptr<InMemoryHostConfig> target) { | 60 void DoUpdate(scoped_refptr<InMemoryHostConfig> target) { |
58 target->SetString(kHostIdConfigPath, kHostId); | 61 target->SetString(kHostIdConfigPath, kHostId); |
59 target->SetString(kPrivateKeyConfigPath, kTestHostKeyPair); | 62 target->SetString(kPrivateKeyConfigPath, kTestHostKeyPair); |
(...skipping 12 matching lines...) Expand all Loading... |
72 jingle_client_ = new MockJingleClient(&jingle_thread_); | 75 jingle_client_ = new MockJingleClient(&jingle_thread_); |
73 jingle_client_->full_jid_ = kTestJid; | 76 jingle_client_->full_jid_ = kTestJid; |
74 } | 77 } |
75 | 78 |
76 JingleThread jingle_thread_; | 79 JingleThread jingle_thread_; |
77 scoped_refptr<MockJingleClient> jingle_client_; | 80 scoped_refptr<MockJingleClient> jingle_client_; |
78 MessageLoop message_loop_; | 81 MessageLoop message_loop_; |
79 scoped_refptr<InMemoryHostConfig> config_; | 82 scoped_refptr<InMemoryHostConfig> config_; |
80 }; | 83 }; |
81 | 84 |
| 85 // Call Start() followed by Stop(), and makes sure an Iq stanza is |
| 86 // being send. |
82 TEST_F(HeartbeatSenderTest, DoSendStanza) { | 87 TEST_F(HeartbeatSenderTest, DoSendStanza) { |
83 // This test calls Start() followed by Stop(), and makes sure an Iq | |
84 // stanza is being send. | |
85 | |
86 // |iq_request| is freed by HeartbeatSender. | 88 // |iq_request| is freed by HeartbeatSender. |
87 MockIqRequest* iq_request = new MockIqRequest(jingle_client_); | 89 MockIqRequest* iq_request = new MockIqRequest(jingle_client_); |
88 | 90 |
89 scoped_refptr<HeartbeatSender> heartbeat_sender(new HeartbeatSender()); | 91 scoped_refptr<HeartbeatSender> heartbeat_sender(new HeartbeatSender()); |
90 ASSERT_TRUE(heartbeat_sender->Init(config_, jingle_client_)); | 92 ASSERT_TRUE(heartbeat_sender->Init(config_, jingle_client_)); |
91 | 93 |
92 EXPECT_CALL(*jingle_client_, CreateIqRequest()) | 94 EXPECT_CALL(*jingle_client_, CreateIqRequest()) |
93 .WillOnce(Return(iq_request)); | 95 .WillOnce(Return(iq_request)); |
94 | 96 |
95 EXPECT_CALL(*iq_request, SendIq(buzz::STR_SET, kChromotingBotJid, NotNull())) | 97 EXPECT_CALL(*iq_request, SendIq(buzz::STR_SET, kChromotingBotJid, NotNull())) |
96 .WillOnce(DoAll(DeleteArg<2>(), Return())); | 98 .WillOnce(DoAll(DeleteArg<2>(), Return())); |
97 | 99 |
98 heartbeat_sender->Start(); | 100 heartbeat_sender->Start(); |
99 message_loop_.RunAllPending(); | 101 message_loop_.RunAllPending(); |
100 | 102 |
101 heartbeat_sender->Stop(); | 103 heartbeat_sender->Stop(); |
102 message_loop_.RunAllPending(); | 104 message_loop_.RunAllPending(); |
103 } | 105 } |
104 | 106 |
| 107 // Validate format of the heartbeat stanza. |
105 TEST_F(HeartbeatSenderTest, CreateHeartbeatMessage) { | 108 TEST_F(HeartbeatSenderTest, CreateHeartbeatMessage) { |
106 // This test validates format of the heartbeat stanza. | |
107 | |
108 scoped_refptr<HeartbeatSender> heartbeat_sender(new HeartbeatSender()); | 109 scoped_refptr<HeartbeatSender> heartbeat_sender(new HeartbeatSender()); |
109 ASSERT_TRUE(heartbeat_sender->Init(config_, jingle_client_)); | 110 ASSERT_TRUE(heartbeat_sender->Init(config_, jingle_client_)); |
110 | 111 |
111 int64 start_time = static_cast<int64>(base::Time::Now().ToDoubleT()); | 112 int64 start_time = static_cast<int64>(base::Time::Now().ToDoubleT()); |
112 | 113 |
113 scoped_ptr<buzz::XmlElement> stanza( | 114 scoped_ptr<XmlElement> stanza( |
114 heartbeat_sender->CreateHeartbeatMessage()); | 115 heartbeat_sender->CreateHeartbeatMessage()); |
115 ASSERT_TRUE(stanza.get() != NULL); | 116 ASSERT_TRUE(stanza.get() != NULL); |
116 | 117 |
117 EXPECT_TRUE(buzz::QName(kChromotingXmlNamespace, "heartbeat") == | 118 EXPECT_TRUE(QName(kChromotingXmlNamespace, "heartbeat") == |
118 stanza->Name()); | 119 stanza->Name()); |
119 EXPECT_EQ(std::string(kHostId), | 120 EXPECT_EQ(std::string(kHostId), |
120 stanza->Attr(buzz::QName(kChromotingXmlNamespace, "hostid"))); | 121 stanza->Attr(QName(kChromotingXmlNamespace, "hostid"))); |
121 | 122 |
122 buzz::QName signature_tag(kChromotingXmlNamespace, "signature"); | 123 QName signature_tag(kChromotingXmlNamespace, "signature"); |
123 buzz::XmlElement* signature = stanza->FirstNamed(signature_tag); | 124 XmlElement* signature = stanza->FirstNamed(signature_tag); |
124 ASSERT_TRUE(signature != NULL); | 125 ASSERT_TRUE(signature != NULL); |
125 EXPECT_TRUE(stanza->NextNamed(signature_tag) == NULL); | 126 EXPECT_TRUE(stanza->NextNamed(signature_tag) == NULL); |
126 | 127 |
127 std::string time_str = | 128 std::string time_str = |
128 signature->Attr(buzz::QName(kChromotingXmlNamespace, "time")); | 129 signature->Attr(QName(kChromotingXmlNamespace, "time")); |
129 int64 time; | 130 int64 time; |
130 EXPECT_TRUE(base::StringToInt64(time_str, &time)); | 131 EXPECT_TRUE(base::StringToInt64(time_str, &time)); |
131 int64 now = static_cast<int64>(base::Time::Now().ToDoubleT()); | 132 int64 now = static_cast<int64>(base::Time::Now().ToDoubleT()); |
132 EXPECT_LE(start_time, time); | 133 EXPECT_LE(start_time, time); |
133 EXPECT_GE(now, time); | 134 EXPECT_GE(now, time); |
134 | 135 |
135 HostKeyPair key_pair; | 136 HostKeyPair key_pair; |
136 key_pair.LoadFromString(kTestHostKeyPair); | 137 key_pair.LoadFromString(kTestHostKeyPair); |
137 std::string expected_signature = | 138 std::string expected_signature = |
138 key_pair.GetSignature(std::string(kTestJid) + ' ' + time_str); | 139 key_pair.GetSignature(std::string(kTestJid) + ' ' + time_str); |
139 EXPECT_EQ(expected_signature, signature->BodyText()); | 140 EXPECT_EQ(expected_signature, signature->BodyText()); |
140 } | 141 } |
141 | 142 |
| 143 // Verify that ProcessResponse parses set-interval result. |
| 144 TEST_F(HeartbeatSenderTest, ProcessResponse) { |
| 145 XmlElement* response = new XmlElement(QName("", "iq")); |
| 146 response->AddAttr(QName("", "type"), "result"); |
| 147 |
| 148 XmlElement* result = new XmlElement( |
| 149 QName(kChromotingXmlNamespace, "heartbeat-result")); |
| 150 response->AddElement(result); |
| 151 |
| 152 XmlElement* set_interval = new XmlElement( |
| 153 QName(kChromotingXmlNamespace, "set-interval")); |
| 154 result->AddElement(set_interval); |
| 155 |
| 156 const int kTestInterval = 123; |
| 157 set_interval->AddText(base::IntToString(kTestInterval)); |
| 158 |
| 159 scoped_refptr<HeartbeatSender> heartbeat_sender(new HeartbeatSender()); |
| 160 heartbeat_sender->ProcessResponse(response); |
| 161 |
| 162 EXPECT_EQ(kTestInterval * 1000, heartbeat_sender->interval_ms_); |
| 163 } |
| 164 |
142 } // namespace remoting | 165 } // namespace remoting |
OLD | NEW |