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

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

Issue 3087003: Added HostKeyPair class, signatures for heartbeat messages. (Closed)
Patch Set: - Created 10 years, 4 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
« no previous file with comments | « remoting/host/heartbeat_sender.cc ('k') | remoting/host/host_key_pair.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "base/message_loop.h"
6 #include "base/message_loop_proxy.h"
7 #include "base/ref_counted.h"
8 #include "base/scoped_temp_dir.h"
9 #include "base/string_util.h"
10 #include "media/base/data_buffer.h"
11 #include "remoting/base/constants.h"
12 #include "remoting/host/heartbeat_sender.h"
13 #include "remoting/host/host_key_pair.h"
14 #include "remoting/host/json_host_config.h"
15 #include "remoting/host/test_key_pair.h"
16 #include "remoting/jingle_glue/iq_request.h"
17 #include "remoting/jingle_glue/jingle_client.h"
18 #include "remoting/jingle_glue/jingle_thread.h"
19 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h"
22 #include "third_party/libjingle/source/talk/xmpp/constants.h"
23
24 using testing::_;
25 using testing::DeleteArg;
26 using testing::DoAll;
27 using testing::NotNull;
28 using testing::Return;
29
30 namespace remoting {
31
32 namespace {
33 const char kHostId[] = "0";
34 const char kTestJid[] = "user@gmail.com/chromoting123";
35 const int64 kTestTime = 123123123;
36 } // namespace
37
38 class MockJingleClient : public JingleClient {
39 public:
40 explicit MockJingleClient(JingleThread* thread) : JingleClient(thread) { }
41 MOCK_METHOD0(CreateIqRequest, IqRequest*());
42 };
43
44 class MockIqRequest : public IqRequest {
45 public:
46 explicit MockIqRequest(JingleClient* jingle_client)
47 : IqRequest(jingle_client) {
48 }
49 MOCK_METHOD3(SendIq, void(const std::string& type,
50 const std::string& addressee,
51 buzz::XmlElement* iq_body));
52 };
53
54 class HeartbeatSenderTest : public testing::Test {
55 protected:
56 class TestConfigUpdater :
57 public base::RefCountedThreadSafe<TestConfigUpdater> {
58 public:
59 void DoUpdate(scoped_refptr<JsonHostConfig> target) {
60 target->SetString(kHostIdConfigPath, kHostId);
61 target->SetString(kPrivateKeyConfigPath, kTestHostKeyPair);
62 }
63 };
64
65 virtual void SetUp() {
66 ASSERT_TRUE(test_dir_.CreateUniqueTempDir());
67 FilePath config_path = test_dir_.path().AppendASCII("test_config.json");
68 config_ = new JsonHostConfig(
69 config_path, base::MessageLoopProxy::CreateForCurrentThread());
70 scoped_refptr<TestConfigUpdater> config_updater(new TestConfigUpdater());
71 config_->Update(
72 NewRunnableMethod(config_updater.get(), &TestConfigUpdater::DoUpdate,
73 config_));
74
75 jingle_thread_.message_loop_ = &message_loop_;
76
77 jingle_client_ = new MockJingleClient(&jingle_thread_);
78 jingle_client_->full_jid_ = kTestJid;
79 }
80
81 JingleThread jingle_thread_;
82 scoped_refptr<MockJingleClient> jingle_client_;
83 MessageLoop message_loop_;
84 ScopedTempDir test_dir_;
85 scoped_refptr<JsonHostConfig> config_;
86 };
87
88 TEST_F(HeartbeatSenderTest, DoSendStanza) {
89 // This test calls Start() followed by Stop(), and makes sure an Iq
90 // stanza is being send.
91
92 // |iq_request| is freed by HeartbeatSender.
93 MockIqRequest* iq_request = new MockIqRequest(jingle_client_);
94
95 scoped_refptr<HeartbeatSender> heartbeat_sender = new HeartbeatSender();
96 ASSERT_TRUE(heartbeat_sender->Init(config_, jingle_client_));
97
98 EXPECT_CALL(*jingle_client_, CreateIqRequest())
99 .WillOnce(Return(iq_request));
100
101 EXPECT_CALL(*iq_request, SendIq(buzz::STR_SET, kChromotingBotJid, NotNull()))
102 .WillOnce(DoAll(DeleteArg<2>(), Return()));
103
104 heartbeat_sender->Start();
105 message_loop_.RunAllPending();
106
107 heartbeat_sender->Stop();
108 message_loop_.RunAllPending();
109 }
110
111 TEST_F(HeartbeatSenderTest, CreateHeartbeatMessage) {
112 // This test validates format of the heartbeat stanza.
113
114 scoped_refptr<HeartbeatSender> heartbeat_sender = new HeartbeatSender();
115 ASSERT_TRUE(heartbeat_sender->Init(config_, jingle_client_));
116
117 int64 start_time = static_cast<int64>(base::Time::Now().ToDoubleT());
118
119 scoped_ptr<buzz::XmlElement> stanza(
120 heartbeat_sender->CreateHeartbeatMessage());
121 ASSERT_TRUE(stanza.get() != NULL);
122
123 EXPECT_TRUE(buzz::QName(kChromotingXmlNamespace, "heartbeat") ==
124 stanza->Name());
125 EXPECT_EQ(std::string(kHostId),
126 stanza->Attr(buzz::QName(kChromotingXmlNamespace, "hostid")));
127
128 buzz::QName signature_tag(kChromotingXmlNamespace, "signature");
129 buzz::XmlElement* signature = stanza->FirstNamed(signature_tag);
130 ASSERT_TRUE(signature != NULL);
131 EXPECT_TRUE(stanza->NextNamed(signature_tag) == NULL);
132
133 std::string time_str =
134 signature->Attr(buzz::QName(kChromotingXmlNamespace, "time"));
135 int64 time;
136 EXPECT_TRUE(StringToInt64(time_str, &time));
137 int64 now = static_cast<int64>(base::Time::Now().ToDoubleT());
138 EXPECT_LE(start_time, time);
139 EXPECT_GE(now, time);
140
141 HostKeyPair key_pair;
142 key_pair.LoadFromString(kTestHostKeyPair);
143 std::string expected_signature =
144 key_pair.GetSignature(std::string(kTestJid) + ' ' + time_str);
145 EXPECT_EQ(expected_signature, signature->BodyText());
146 }
147
148 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/host/heartbeat_sender.cc ('k') | remoting/host/host_key_pair.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698