diff options
author | sergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-08-04 23:04:05 +0000 |
---|---|---|
committer | sergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-08-04 23:04:05 +0000 |
commit | cdf8c57cbf1d997346e650fb5d0b301f7abc4dd4 (patch) | |
tree | 81a7bfd30b29dfaea9cd2644daf06f39d11b17a0 /remoting/host/heartbeat_sender_unittest.cc | |
parent | c0035fff490b71ff952b97b6a3d22b1041689979 (diff) | |
download | chromium_src-cdf8c57cbf1d997346e650fb5d0b301f7abc4dd4.zip chromium_src-cdf8c57cbf1d997346e650fb5d0b301f7abc4dd4.tar.gz chromium_src-cdf8c57cbf1d997346e650fb5d0b301f7abc4dd4.tar.bz2 |
Added HostKeyPair class, signatures for heartbeat messages.
BUG=None
TEST=unittests
Review URL: http://codereview.chromium.org/3087003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@54991 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'remoting/host/heartbeat_sender_unittest.cc')
-rw-r--r-- | remoting/host/heartbeat_sender_unittest.cc | 148 |
1 files changed, 148 insertions, 0 deletions
diff --git a/remoting/host/heartbeat_sender_unittest.cc b/remoting/host/heartbeat_sender_unittest.cc new file mode 100644 index 0000000..6ac2037 --- /dev/null +++ b/remoting/host/heartbeat_sender_unittest.cc @@ -0,0 +1,148 @@ +// 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_number_conversions.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/json_host_config.h" +#include "remoting/host/test_key_pair.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::DeleteArg; +using testing::DoAll; +using testing::NotNull; +using testing::Return; + +namespace remoting { + +namespace { +const char kHostId[] = "0"; +const char kTestJid[] = "user@gmail.com/chromoting123"; +const int64 kTestTime = 123123123; +} // 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_)); + + jingle_thread_.message_loop_ = &message_loop_; + + jingle_client_ = new MockJingleClient(&jingle_thread_); + jingle_client_->full_jid_ = kTestJid; + } + + JingleThread jingle_thread_; + scoped_refptr<MockJingleClient> jingle_client_; + MessageLoop message_loop_; + ScopedTempDir test_dir_; + scoped_refptr<JsonHostConfig> config_; +}; + +TEST_F(HeartbeatSenderTest, DoSendStanza) { + // This test calls Start() followed by Stop(), and makes sure an Iq + // stanza is being send. + + // |iq_request| is freed by HeartbeatSender. + 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)); + + EXPECT_CALL(*iq_request, SendIq(buzz::STR_SET, kChromotingBotJid, NotNull())) + .WillOnce(DoAll(DeleteArg<2>(), Return())); + + heartbeat_sender->Start(); + message_loop_.RunAllPending(); + + heartbeat_sender->Stop(); + message_loop_.RunAllPending(); +} + +TEST_F(HeartbeatSenderTest, CreateHeartbeatMessage) { + // This test validates format of the heartbeat stanza. + + scoped_refptr<HeartbeatSender> heartbeat_sender = new HeartbeatSender(); + ASSERT_TRUE(heartbeat_sender->Init(config_, jingle_client_)); + + int64 start_time = static_cast<int64>(base::Time::Now().ToDoubleT()); + + scoped_ptr<buzz::XmlElement> stanza( + heartbeat_sender->CreateHeartbeatMessage()); + ASSERT_TRUE(stanza.get() != NULL); + + 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 = 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(base::StringToInt64(time_str, &time)); + int64 now = static_cast<int64>(base::Time::Now().ToDoubleT()); + EXPECT_LE(start_time, time); + EXPECT_GE(now, time); + + HostKeyPair key_pair; + key_pair.LoadFromString(kTestHostKeyPair); + std::string expected_signature = + key_pair.GetSignature(std::string(kTestJid) + ' ' + time_str); + EXPECT_EQ(expected_signature, signature->BodyText()); +} + +} // namespace remoting |