Disable SourceDir.ResolveRelativeFile and SourceDir.ResolveRelativeDir on win
[chromium-blink-merge.git] / jingle / notifier / communicator / single_login_attempt_unittest.cc
blobd791f4a7fe6d1bff29e7b6c54ed3060e71a7f26c
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"
7 #include <cstddef>
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"
22 namespace buzz {
23 class XmppTaskParentInterface;
24 } // namespace buzz
26 namespace notifier {
28 namespace {
30 enum DelegateState {
31 IDLE, CONNECTED, REDIRECTED, CREDENTIALS_REJECTED, SETTINGS_EXHAUSTED
34 class FakeDelegate : public SingleLoginAttempt::Delegate {
35 public:
36 FakeDelegate() : state_(IDLE) {}
38 void OnConnect(
39 base::WeakPtr<buzz::XmppTaskParentInterface> base_task) override {
40 state_ = CONNECTED;
41 base_task_ = base_task;
44 void OnRedirect(const ServerInformation& redirect_server) override {
45 state_ = REDIRECTED;
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 {
56 return base_task_;
59 const ServerInformation& redirect_server() const {
60 return redirect_server_;
63 private:
64 DelegateState state_;
65 base::WeakPtr<buzz::XmppTaskParentInterface> base_task_;
66 ServerInformation redirect_server_;
69 class MyTestURLRequestContext : public net::TestURLRequestContext {
70 public:
71 MyTestURLRequestContext() : TestURLRequestContext(true) {
72 context_storage_.set_host_resolver(
73 scoped_ptr<net::HostResolver>(new net::HangingHostResolver()));
74 Init();
76 ~MyTestURLRequestContext() override {}
79 class SingleLoginAttemptTest : public ::testing::Test {
80 protected:
81 SingleLoginAttemptTest()
82 : login_settings_(
83 buzz::XmppClientSettings(),
84 new net::TestURLRequestContextGetter(
85 base::MessageLoopProxy::current(),
86 scoped_ptr<net::TestURLRequestContext>(
87 new MyTestURLRequestContext())),
88 ServerList(
90 ServerInformation(
91 net::HostPortPair("example.com", 100), SUPPORTS_SSLTCP)),
92 false /* try_ssltcp_first */,
93 "auth_mechanism"),
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 {
103 attempt_.reset();
104 message_loop_.RunUntilIdle();
107 private:
108 base::MessageLoop message_loop_;
109 const LoginSettings login_settings_;
111 protected:
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);
155 return stream_error;
158 // Fire a redirect and make sure the delegate gets the proper redirect
159 // server info.
160 TEST_F(SingleLoginAttemptTest, Redirect) {
161 const ServerInformation redirect_server(
162 net::HostPortPair("example.com", 1000),
163 SUPPORTS_SSLTCP);
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),
178 SUPPORTS_SSLTCP);
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),
193 SUPPORTS_SSLTCP);
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),
208 SUPPORTS_SSLTCP);
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
219 // redirect.
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());
251 } // namespace
253 } // namespace notifier