Chromium Code Reviews| Index: remoting/host/heartbeat_sender_unittest.cc |
| diff --git a/remoting/host/heartbeat_sender_unittest.cc b/remoting/host/heartbeat_sender_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2ac3af456d0f80e54c311100bf4da325da1efed9 |
| --- /dev/null |
| +++ b/remoting/host/heartbeat_sender_unittest.cc |
| @@ -0,0 +1,141 @@ |
| +// Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/message_loop.h" |
| +#include "base/message_loop_proxy.h" |
| +#include "base/ref_counted.h" |
| +#include "base/scoped_temp_dir.h" |
| +#include "base/string_util.h" |
| +#include "media/base/data_buffer.h" |
| +#include "remoting/base/constants.h" |
| +#include "remoting/host/heartbeat_sender.h" |
| +#include "remoting/host/host_key_pair.h" |
| +#include "remoting/host/test_key_pair.h" |
|
awong
2010/08/02 19:53:49
alphabetical order
Sergey Ulanov
2010/08/03 02:10:39
Done.
|
| +#include "remoting/host/json_host_config.h" |
| +#include "remoting/jingle_glue/iq_request.h" |
| +#include "remoting/jingle_glue/jingle_client.h" |
| +#include "remoting/jingle_glue/jingle_thread.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "third_party/libjingle/source/talk/xmllite/xmlelement.h" |
| +#include "third_party/libjingle/source/talk/xmpp/constants.h" |
| + |
| +using testing::_; |
| +using testing::DoAll; |
| +using testing::Return; |
| +using testing::SaveArg; |
| + |
| +namespace remoting { |
| + |
| +namespace { |
| +const char kHostId[] = "0"; |
| +const char kTestJid[] = "user@gmail.com/chromoting123"; |
| +} // namespace |
| + |
| +class MockJingleClient : public JingleClient { |
| + public: |
| + explicit MockJingleClient(JingleThread* thread) : JingleClient(thread) { } |
| + MOCK_METHOD0(CreateIqRequest, IqRequest*()); |
| +}; |
| + |
| +class MockIqRequest : public IqRequest { |
| + public: |
| + explicit MockIqRequest(JingleClient* jingle_client) |
| + : IqRequest(jingle_client) { |
| + } |
| + MOCK_METHOD3(SendIq, void(const std::string& type, |
| + const std::string& addressee, |
| + buzz::XmlElement* iq_body)); |
| +}; |
| + |
| +class HeartbeatSenderTest : public testing::Test { |
| + protected: |
| + class TestConfigUpdater : |
| + public base::RefCountedThreadSafe<TestConfigUpdater> { |
| + public: |
| + void DoUpdate(scoped_refptr<JsonHostConfig> target) { |
| + target->SetString(kHostIdConfigPath, kHostId); |
| + target->SetString(kPrivateKeyConfigPath, kTestHostKeyPair); |
| + } |
| + }; |
| + |
| + virtual void SetUp() { |
| + ASSERT_TRUE(test_dir_.CreateUniqueTempDir()); |
| + FilePath config_path = test_dir_.path().AppendASCII("test_config.json"); |
| + config_ = new JsonHostConfig( |
| + config_path, base::MessageLoopProxy::CreateForCurrentThread()); |
| + scoped_refptr<TestConfigUpdater> config_updater(new TestConfigUpdater()); |
| + config_->Update( |
| + NewRunnableMethod(config_updater.get(), &TestConfigUpdater::DoUpdate, |
| + config_)); |
| + } |
| + |
| + MessageLoop message_loop_; |
| + ScopedTempDir test_dir_; |
| + scoped_refptr<JsonHostConfig> config_; |
| +}; |
| + |
| +TEST_F(HeartbeatSenderTest, DoSendStanza) { |
| + // This test creates a HeartbeatSender object and then makes sure it does |
|
awong
2010/08/02 19:53:49
Can you add a little more description on what high
Sergey Ulanov
2010/08/03 02:10:39
Refactored the message generation to a separate me
|
| + // send stanzas. |
| + |
| + JingleThread jingle_thread; |
| + jingle_thread.message_loop_ = &message_loop_; |
| + |
| + scoped_refptr<MockJingleClient> jingle_client = |
| + new MockJingleClient(&jingle_thread); |
| + jingle_client->full_jid_ = kTestJid; |
| + |
| + // iq_request is freed by HeartbeatSender. |
|
awong
2010/08/02 19:53:49
iq_request -> |iq_request|
Sergey Ulanov
2010/08/03 02:10:39
Done.
|
| + MockIqRequest* iq_request = new MockIqRequest(jingle_client); |
| + |
| + scoped_refptr<HeartbeatSender> heartbeat_sender = new HeartbeatSender(); |
| + ASSERT_TRUE(heartbeat_sender->Init(config_, jingle_client)); |
| + |
| + EXPECT_CALL(*jingle_client, CreateIqRequest()) |
| + .WillOnce(Return(iq_request)); |
| + |
| + buzz::XmlElement* stanza; |
|
awong
2010/08/02 19:53:49
Throw this in a scoped_ptr, you have an ASSERT_TR
Sergey Ulanov
2010/08/03 02:10:39
Can I use SaveArg() with scoped_ptr?
awong
2010/08/03 19:21:02
I don't know....I would have expcted somethin glik
Sergey Ulanov
2010/08/04 01:41:12
This wouldn't work: stanza.get() would return NULL
|
| + // TODO(sergeyu): validate the stanza here. |
| + EXPECT_CALL(*iq_request, SendIq(buzz::STR_SET, kChromotingBotJid, _)) |
| + .WillOnce(DoAll(SaveArg<2>(&stanza), Return())); |
| + |
| + heartbeat_sender->Start(); |
| + message_loop_.RunAllPending(); |
| + |
| + heartbeat_sender->Stop(); |
| + message_loop_.RunAllPending(); |
| + |
| + EXPECT_TRUE(buzz::QName(kChromotingXmlNamespace, "heartbeat") == |
| + stanza->Name()); |
| + EXPECT_EQ(std::string(kHostId), |
| + stanza->Attr(buzz::QName(kChromotingXmlNamespace, "hostid"))); |
| + |
| + buzz::QName signature_tag(kChromotingXmlNamespace, "signature"); |
| + buzz::XmlElement* signature = |
|
awong
2010/08/02 19:53:49
Does this not fit on one line?
Sergey Ulanov
2010/08/03 02:10:39
Done.
|
| + stanza->FirstNamed(signature_tag); |
| + ASSERT_TRUE(signature != NULL); |
| + EXPECT_TRUE(stanza->NextNamed(signature_tag) == NULL); |
| + |
| + std::string time_str = |
| + signature->Attr(buzz::QName(kChromotingXmlNamespace, "time")); |
| + int64 time; |
| + EXPECT_TRUE(StringToInt64(time_str, &time)); |
| + int64 now = static_cast<int64>(base::Time::Now().ToDoubleT()); |
| + |
| + // Allow for 10000 seconds difference in case the test is slow, |
| + // for example under valgrind. |
|
awong
2010/08/02 19:53:49
Can we inject a clock? Add a GetTimestamp() metho
Sergey Ulanov
2010/08/03 02:10:39
Added start_time that clocks the time before the s
awong
2010/08/03 19:21:02
That works too.
|
| + EXPECT_LE(now, time + 10000); |
| + EXPECT_GE(now, time); |
| + |
| + scoped_refptr<HostKeyPair> key_pair = new HostKeyPair(); |
| + key_pair->LoadFromString(kTestHostKeyPair); |
| + std::string expected_signature = |
| + key_pair->GetSignature(std::string(kTestJid) + ' ' + time_str); |
| + EXPECT_EQ(expected_signature, signature->BodyText()); |
| + |
| + delete stanza; |
| +} |
| + |
| +} // namespace remoting |