1 // Copyright (c) 2012 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.
5 #include "jingle/notifier/communicator/single_login_attempt.h"
9 #include "base/compiler_specific.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop/message_loop.h"
12 #include "jingle/notifier/base/const_communicator.h"
13 #include "jingle/notifier/base/fake_base_task.h"
14 #include "jingle/notifier/communicator/login_settings.h"
15 #include "net/dns/mock_host_resolver.h"
16 #include "net/url_request/url_request_test_util.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h"
19 #include "webrtc/libjingle/xmpp/constants.h"
20 #include "webrtc/libjingle/xmpp/xmppengine.h"
23 class XmppTaskParentInterface
;
31 IDLE
, CONNECTED
, REDIRECTED
, CREDENTIALS_REJECTED
, SETTINGS_EXHAUSTED
34 class FakeDelegate
: public SingleLoginAttempt::Delegate
{
36 FakeDelegate() : state_(IDLE
) {}
39 base::WeakPtr
<buzz::XmppTaskParentInterface
> base_task
) override
{
41 base_task_
= base_task
;
44 void OnRedirect(const ServerInformation
& redirect_server
) override
{
46 redirect_server_
= redirect_server
;
49 void OnCredentialsRejected() override
{ state_
= CREDENTIALS_REJECTED
; }
51 void OnSettingsExhausted() override
{ state_
= SETTINGS_EXHAUSTED
; }
53 DelegateState
state() const { return state_
; }
55 base::WeakPtr
<buzz::XmppTaskParentInterface
> base_task() const {
59 const ServerInformation
& redirect_server() const {
60 return redirect_server_
;
65 base::WeakPtr
<buzz::XmppTaskParentInterface
> base_task_
;
66 ServerInformation redirect_server_
;
69 class MyTestURLRequestContext
: public net::TestURLRequestContext
{
71 MyTestURLRequestContext() : TestURLRequestContext(true) {
72 context_storage_
.set_host_resolver(
73 scoped_ptr
<net::HostResolver
>(new net::HangingHostResolver()));
76 ~MyTestURLRequestContext() override
{}
79 class SingleLoginAttemptTest
: public ::testing::Test
{
81 SingleLoginAttemptTest()
83 buzz::XmppClientSettings(),
84 new net::TestURLRequestContextGetter(
85 base::MessageLoopProxy::current(),
86 scoped_ptr
<net::TestURLRequestContext
>(
87 new MyTestURLRequestContext())),
91 net::HostPortPair("example.com", 100), SUPPORTS_SSLTCP
)),
92 false /* try_ssltcp_first */,
94 attempt_(new SingleLoginAttempt(login_settings_
, &fake_delegate_
)) {}
96 void TearDown() override
{ message_loop_
.RunUntilIdle(); }
98 void FireRedirect(buzz::XmlElement
* redirect_error
) {
99 attempt_
->OnError(buzz::XmppEngine::ERROR_STREAM
, 0, redirect_error
);
102 ~SingleLoginAttemptTest() override
{
104 message_loop_
.RunUntilIdle();
108 base::MessageLoop message_loop_
;
109 const LoginSettings login_settings_
;
112 scoped_ptr
<SingleLoginAttempt
> attempt_
;
113 FakeDelegate fake_delegate_
;
114 FakeBaseTask fake_base_task_
;
117 // Fire OnConnect and make sure the base task gets passed to the
118 // delegate properly.
119 TEST_F(SingleLoginAttemptTest
, Basic
) {
120 attempt_
->OnConnect(fake_base_task_
.AsWeakPtr());
121 EXPECT_EQ(CONNECTED
, fake_delegate_
.state());
122 EXPECT_EQ(fake_base_task_
.AsWeakPtr().get(),
123 fake_delegate_
.base_task().get());
126 // Fire OnErrors and make sure the delegate gets the
127 // OnSettingsExhausted() event.
128 TEST_F(SingleLoginAttemptTest
, Error
) {
129 for (int i
= 0; i
< 2; ++i
) {
130 EXPECT_EQ(IDLE
, fake_delegate_
.state());
131 attempt_
->OnError(buzz::XmppEngine::ERROR_NONE
, 0, NULL
);
133 EXPECT_EQ(SETTINGS_EXHAUSTED
, fake_delegate_
.state());
136 // Fire OnErrors but replace the last one with OnConnect, and make
137 // sure the delegate still gets the OnConnect message.
138 TEST_F(SingleLoginAttemptTest
, ErrorThenSuccess
) {
139 attempt_
->OnError(buzz::XmppEngine::ERROR_NONE
, 0, NULL
);
140 attempt_
->OnConnect(fake_base_task_
.AsWeakPtr());
141 EXPECT_EQ(CONNECTED
, fake_delegate_
.state());
142 EXPECT_EQ(fake_base_task_
.AsWeakPtr().get(),
143 fake_delegate_
.base_task().get());
146 buzz::XmlElement
* MakeRedirectError(const std::string
& redirect_server
) {
147 buzz::XmlElement
* stream_error
=
148 new buzz::XmlElement(buzz::QN_STREAM_ERROR
, true);
149 stream_error
->AddElement(
150 new buzz::XmlElement(buzz::QN_XSTREAM_SEE_OTHER_HOST
, true));
151 buzz::XmlElement
* text
=
152 new buzz::XmlElement(buzz::QN_XSTREAM_TEXT
, true);
153 stream_error
->AddElement(text
);
154 text
->SetBodyText(redirect_server
);
158 // Fire a redirect and make sure the delegate gets the proper redirect
160 TEST_F(SingleLoginAttemptTest
, Redirect
) {
161 const ServerInformation
redirect_server(
162 net::HostPortPair("example.com", 1000),
165 scoped_ptr
<buzz::XmlElement
> redirect_error(
166 MakeRedirectError(redirect_server
.server
.ToString()));
167 FireRedirect(redirect_error
.get());
169 EXPECT_EQ(REDIRECTED
, fake_delegate_
.state());
170 EXPECT_TRUE(fake_delegate_
.redirect_server().Equals(redirect_server
));
173 // Fire a redirect with the host only and make sure the delegate gets
174 // the proper redirect server info with the default XMPP port.
175 TEST_F(SingleLoginAttemptTest
, RedirectHostOnly
) {
176 const ServerInformation
redirect_server(
177 net::HostPortPair("example.com", kDefaultXmppPort
),
180 scoped_ptr
<buzz::XmlElement
> redirect_error(
181 MakeRedirectError(redirect_server
.server
.host()));
182 FireRedirect(redirect_error
.get());
184 EXPECT_EQ(REDIRECTED
, fake_delegate_
.state());
185 EXPECT_TRUE(fake_delegate_
.redirect_server().Equals(redirect_server
));
188 // Fire a redirect with a zero port and make sure the delegate gets
189 // the proper redirect server info with the default XMPP port.
190 TEST_F(SingleLoginAttemptTest
, RedirectZeroPort
) {
191 const ServerInformation
redirect_server(
192 net::HostPortPair("example.com", kDefaultXmppPort
),
195 scoped_ptr
<buzz::XmlElement
> redirect_error(
196 MakeRedirectError(redirect_server
.server
.host() + ":0"));
197 FireRedirect(redirect_error
.get());
199 EXPECT_EQ(REDIRECTED
, fake_delegate_
.state());
200 EXPECT_TRUE(fake_delegate_
.redirect_server().Equals(redirect_server
));
203 // Fire a redirect with an invalid port and make sure the delegate
204 // gets the proper redirect server info with the default XMPP port.
205 TEST_F(SingleLoginAttemptTest
, RedirectInvalidPort
) {
206 const ServerInformation
redirect_server(
207 net::HostPortPair("example.com", kDefaultXmppPort
),
210 scoped_ptr
<buzz::XmlElement
> redirect_error(
211 MakeRedirectError(redirect_server
.server
.host() + ":invalidport"));
212 FireRedirect(redirect_error
.get());
214 EXPECT_EQ(REDIRECTED
, fake_delegate_
.state());
215 EXPECT_TRUE(fake_delegate_
.redirect_server().Equals(redirect_server
));
218 // Fire an empty redirect and make sure the delegate does not get a
220 TEST_F(SingleLoginAttemptTest
, RedirectEmpty
) {
221 scoped_ptr
<buzz::XmlElement
> redirect_error(MakeRedirectError(std::string()));
222 FireRedirect(redirect_error
.get());
223 EXPECT_EQ(IDLE
, fake_delegate_
.state());
226 // Fire a redirect with a missing text element and make sure the
227 // delegate does not get a redirect.
228 TEST_F(SingleLoginAttemptTest
, RedirectMissingText
) {
229 scoped_ptr
<buzz::XmlElement
> redirect_error(MakeRedirectError(std::string()));
230 redirect_error
->RemoveChildAfter(redirect_error
->FirstChild());
231 FireRedirect(redirect_error
.get());
232 EXPECT_EQ(IDLE
, fake_delegate_
.state());
235 // Fire a redirect with a missing see-other-host element and make sure
236 // the delegate does not get a redirect.
237 TEST_F(SingleLoginAttemptTest
, RedirectMissingSeeOtherHost
) {
238 scoped_ptr
<buzz::XmlElement
> redirect_error(MakeRedirectError(std::string()));
239 redirect_error
->RemoveChildAfter(NULL
);
240 FireRedirect(redirect_error
.get());
241 EXPECT_EQ(IDLE
, fake_delegate_
.state());
244 // Fire 'Unauthorized' errors and make sure the delegate gets the
245 // OnCredentialsRejected() event.
246 TEST_F(SingleLoginAttemptTest
, CredentialsRejected
) {
247 attempt_
->OnError(buzz::XmppEngine::ERROR_UNAUTHORIZED
, 0, NULL
);
248 EXPECT_EQ(CREDENTIALS_REJECTED
, fake_delegate_
.state());
253 } // namespace notifier