OLD | NEW |
---|---|
(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/test_key_pair.h" | |
awong
2010/08/02 19:53:49
alphabetical order
Sergey Ulanov
2010/08/03 02:10:39
Done.
| |
15 #include "remoting/host/json_host_config.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::DoAll; | |
26 using testing::Return; | |
27 using testing::SaveArg; | |
28 | |
29 namespace remoting { | |
30 | |
31 namespace { | |
32 const char kHostId[] = "0"; | |
33 const char kTestJid[] = "user@gmail.com/chromoting123"; | |
34 } // namespace | |
35 | |
36 class MockJingleClient : public JingleClient { | |
37 public: | |
38 explicit MockJingleClient(JingleThread* thread) : JingleClient(thread) { } | |
39 MOCK_METHOD0(CreateIqRequest, IqRequest*()); | |
40 }; | |
41 | |
42 class MockIqRequest : public IqRequest { | |
43 public: | |
44 explicit MockIqRequest(JingleClient* jingle_client) | |
45 : IqRequest(jingle_client) { | |
46 } | |
47 MOCK_METHOD3(SendIq, void(const std::string& type, | |
48 const std::string& addressee, | |
49 buzz::XmlElement* iq_body)); | |
50 }; | |
51 | |
52 class HeartbeatSenderTest : public testing::Test { | |
53 protected: | |
54 class TestConfigUpdater : | |
55 public base::RefCountedThreadSafe<TestConfigUpdater> { | |
56 public: | |
57 void DoUpdate(scoped_refptr<JsonHostConfig> target) { | |
58 target->SetString(kHostIdConfigPath, kHostId); | |
59 target->SetString(kPrivateKeyConfigPath, kTestHostKeyPair); | |
60 } | |
61 }; | |
62 | |
63 virtual void SetUp() { | |
64 ASSERT_TRUE(test_dir_.CreateUniqueTempDir()); | |
65 FilePath config_path = test_dir_.path().AppendASCII("test_config.json"); | |
66 config_ = new JsonHostConfig( | |
67 config_path, base::MessageLoopProxy::CreateForCurrentThread()); | |
68 scoped_refptr<TestConfigUpdater> config_updater(new TestConfigUpdater()); | |
69 config_->Update( | |
70 NewRunnableMethod(config_updater.get(), &TestConfigUpdater::DoUpdate, | |
71 config_)); | |
72 } | |
73 | |
74 MessageLoop message_loop_; | |
75 ScopedTempDir test_dir_; | |
76 scoped_refptr<JsonHostConfig> config_; | |
77 }; | |
78 | |
79 TEST_F(HeartbeatSenderTest, DoSendStanza) { | |
80 // 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
| |
81 // send stanzas. | |
82 | |
83 JingleThread jingle_thread; | |
84 jingle_thread.message_loop_ = &message_loop_; | |
85 | |
86 scoped_refptr<MockJingleClient> jingle_client = | |
87 new MockJingleClient(&jingle_thread); | |
88 jingle_client->full_jid_ = kTestJid; | |
89 | |
90 // 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.
| |
91 MockIqRequest* iq_request = new MockIqRequest(jingle_client); | |
92 | |
93 scoped_refptr<HeartbeatSender> heartbeat_sender = new HeartbeatSender(); | |
94 ASSERT_TRUE(heartbeat_sender->Init(config_, jingle_client)); | |
95 | |
96 EXPECT_CALL(*jingle_client, CreateIqRequest()) | |
97 .WillOnce(Return(iq_request)); | |
98 | |
99 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
| |
100 // TODO(sergeyu): validate the stanza here. | |
101 EXPECT_CALL(*iq_request, SendIq(buzz::STR_SET, kChromotingBotJid, _)) | |
102 .WillOnce(DoAll(SaveArg<2>(&stanza), Return())); | |
103 | |
104 heartbeat_sender->Start(); | |
105 message_loop_.RunAllPending(); | |
106 | |
107 heartbeat_sender->Stop(); | |
108 message_loop_.RunAllPending(); | |
109 | |
110 EXPECT_TRUE(buzz::QName(kChromotingXmlNamespace, "heartbeat") == | |
111 stanza->Name()); | |
112 EXPECT_EQ(std::string(kHostId), | |
113 stanza->Attr(buzz::QName(kChromotingXmlNamespace, "hostid"))); | |
114 | |
115 buzz::QName signature_tag(kChromotingXmlNamespace, "signature"); | |
116 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.
| |
117 stanza->FirstNamed(signature_tag); | |
118 ASSERT_TRUE(signature != NULL); | |
119 EXPECT_TRUE(stanza->NextNamed(signature_tag) == NULL); | |
120 | |
121 std::string time_str = | |
122 signature->Attr(buzz::QName(kChromotingXmlNamespace, "time")); | |
123 int64 time; | |
124 EXPECT_TRUE(StringToInt64(time_str, &time)); | |
125 int64 now = static_cast<int64>(base::Time::Now().ToDoubleT()); | |
126 | |
127 // Allow for 10000 seconds difference in case the test is slow, | |
128 // 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.
| |
129 EXPECT_LE(now, time + 10000); | |
130 EXPECT_GE(now, time); | |
131 | |
132 scoped_refptr<HostKeyPair> key_pair = new HostKeyPair(); | |
133 key_pair->LoadFromString(kTestHostKeyPair); | |
134 std::string expected_signature = | |
135 key_pair->GetSignature(std::string(kTestJid) + ' ' + time_str); | |
136 EXPECT_EQ(expected_signature, signature->BodyText()); | |
137 | |
138 delete stanza; | |
139 } | |
140 | |
141 } // namespace remoting | |
OLD | NEW |