OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/http/http_auth_handler_mock.h" | 5 #include "net/http/http_auth_handler_mock.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
12 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
13 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
14 #include "net/http/http_auth_challenge_tokenizer.h" | 14 #include "net/http/http_auth_challenge_tokenizer.h" |
15 #include "net/http/http_request_info.h" | 15 #include "net/http/http_request_info.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
17 | 18 |
18 namespace net { | 19 namespace net { |
19 | 20 |
| 21 void PrintTo(const HttpAuthHandlerMock::State& state, ::std::ostream* os) { |
| 22 switch (state) { |
| 23 case HttpAuthHandlerMock::State::WAIT_FOR_INIT: |
| 24 *os << "WAIT_FOR_INIT"; |
| 25 break; |
| 26 case HttpAuthHandlerMock::State::WAIT_FOR_CHALLENGE: |
| 27 *os << "WAIT_FOR_CHALLENGE"; |
| 28 break; |
| 29 case HttpAuthHandlerMock::State::WAIT_FOR_GENERATE_AUTH_TOKEN: |
| 30 *os << "WAIT_FOR_GENERATE_AUTH_TOKEN"; |
| 31 break; |
| 32 case HttpAuthHandlerMock::State::TOKEN_PENDING: |
| 33 *os << "TOKEN_PENDING"; |
| 34 break; |
| 35 case HttpAuthHandlerMock::State::DONE: |
| 36 *os << "DONE"; |
| 37 break; |
| 38 } |
| 39 } |
| 40 |
20 HttpAuthHandlerMock::HttpAuthHandlerMock() | 41 HttpAuthHandlerMock::HttpAuthHandlerMock() |
21 : resolve_(RESOLVE_INIT), | 42 : state_(State::WAIT_FOR_INIT), |
22 generate_async_(false), | 43 resolve_(RESOLVE_INIT), |
23 generate_rv_(OK), | 44 generate_async_(false), |
24 auth_token_(NULL), | 45 generate_rv_(OK), |
25 first_round_(true), | 46 auth_token_(NULL), |
26 connection_based_(false), | 47 first_round_(true), |
27 allows_default_credentials_(false), | 48 connection_based_(false), |
28 allows_explicit_credentials_(true), | 49 allows_default_credentials_(false), |
29 weak_factory_(this) { | 50 allows_explicit_credentials_(true), |
30 } | 51 weak_factory_(this) {} |
31 | 52 |
32 HttpAuthHandlerMock::~HttpAuthHandlerMock() { | 53 HttpAuthHandlerMock::~HttpAuthHandlerMock() { |
33 } | 54 } |
34 | 55 |
35 void HttpAuthHandlerMock::SetResolveExpectation(Resolve resolve) { | 56 void HttpAuthHandlerMock::SetResolveExpectation(Resolve resolve) { |
36 EXPECT_EQ(RESOLVE_INIT, resolve_); | 57 EXPECT_EQ(RESOLVE_INIT, resolve_); |
37 resolve_ = resolve; | 58 resolve_ = resolve; |
38 } | 59 } |
39 | 60 |
40 bool HttpAuthHandlerMock::NeedsCanonicalName() { | 61 bool HttpAuthHandlerMock::NeedsCanonicalName() { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 return rv; | 95 return rv; |
75 } | 96 } |
76 | 97 |
77 void HttpAuthHandlerMock::SetGenerateExpectation(bool async, int rv) { | 98 void HttpAuthHandlerMock::SetGenerateExpectation(bool async, int rv) { |
78 generate_async_ = async; | 99 generate_async_ = async; |
79 generate_rv_ = rv; | 100 generate_rv_ = rv; |
80 } | 101 } |
81 | 102 |
82 HttpAuth::AuthorizationResult HttpAuthHandlerMock::HandleAnotherChallenge( | 103 HttpAuth::AuthorizationResult HttpAuthHandlerMock::HandleAnotherChallenge( |
83 HttpAuthChallengeTokenizer* challenge) { | 104 HttpAuthChallengeTokenizer* challenge) { |
| 105 EXPECT_THAT(state_, ::testing::AnyOf(State::WAIT_FOR_CHALLENGE, |
| 106 State::WAIT_FOR_GENERATE_AUTH_TOKEN)); |
84 // If we receive an empty challenge for a connection based scheme, or a second | 107 // If we receive an empty challenge for a connection based scheme, or a second |
85 // challenge for a non connection based scheme, assume it's a rejection. | 108 // challenge for a non connection based scheme, assume it's a rejection. |
86 if (!is_connection_based() || challenge->base64_param().empty()) | 109 if (!is_connection_based() || challenge->base64_param().empty()) { |
| 110 state_ = State::DONE; |
87 return HttpAuth::AUTHORIZATION_RESULT_REJECT; | 111 return HttpAuth::AUTHORIZATION_RESULT_REJECT; |
88 if (!base::LowerCaseEqualsASCII(challenge->scheme(), "mock")) | 112 } |
| 113 |
| 114 if (!base::LowerCaseEqualsASCII(challenge->scheme(), "mock")) { |
| 115 state_ = State::DONE; |
89 return HttpAuth::AUTHORIZATION_RESULT_INVALID; | 116 return HttpAuth::AUTHORIZATION_RESULT_INVALID; |
| 117 } |
| 118 |
| 119 state_ = State::WAIT_FOR_GENERATE_AUTH_TOKEN; |
90 return HttpAuth::AUTHORIZATION_RESULT_ACCEPT; | 120 return HttpAuth::AUTHORIZATION_RESULT_ACCEPT; |
91 } | 121 } |
92 | 122 |
93 bool HttpAuthHandlerMock::NeedsIdentity() { | 123 bool HttpAuthHandlerMock::NeedsIdentity() { |
94 return first_round_; | 124 return first_round_; |
95 } | 125 } |
96 | 126 |
97 bool HttpAuthHandlerMock::AllowsDefaultCredentials() { | 127 bool HttpAuthHandlerMock::AllowsDefaultCredentials() { |
98 return allows_default_credentials_; | 128 return allows_default_credentials_; |
99 } | 129 } |
100 | 130 |
101 bool HttpAuthHandlerMock::AllowsExplicitCredentials() { | 131 bool HttpAuthHandlerMock::AllowsExplicitCredentials() { |
102 return allows_explicit_credentials_; | 132 return allows_explicit_credentials_; |
103 } | 133 } |
104 | 134 |
105 bool HttpAuthHandlerMock::Init(HttpAuthChallengeTokenizer* challenge, | 135 bool HttpAuthHandlerMock::Init(HttpAuthChallengeTokenizer* challenge, |
106 const SSLInfo& ssl_info) { | 136 const SSLInfo& ssl_info) { |
| 137 EXPECT_EQ(State::WAIT_FOR_INIT, state_); |
| 138 state_ = State::WAIT_FOR_GENERATE_AUTH_TOKEN; |
107 auth_scheme_ = HttpAuth::AUTH_SCHEME_MOCK; | 139 auth_scheme_ = HttpAuth::AUTH_SCHEME_MOCK; |
108 score_ = 1; | 140 score_ = 1; |
109 properties_ = connection_based_ ? IS_CONNECTION_BASED : 0; | 141 properties_ = connection_based_ ? IS_CONNECTION_BASED : 0; |
110 return true; | 142 return true; |
111 } | 143 } |
112 | 144 |
113 int HttpAuthHandlerMock::GenerateAuthTokenImpl( | 145 int HttpAuthHandlerMock::GenerateAuthTokenImpl( |
114 const AuthCredentials* credentials, | 146 const AuthCredentials* credentials, |
115 const HttpRequestInfo* request, | 147 const HttpRequestInfo* request, |
116 const CompletionCallback& callback, | 148 const CompletionCallback& callback, |
117 std::string* auth_token) { | 149 std::string* auth_token) { |
| 150 EXPECT_EQ(State::WAIT_FOR_GENERATE_AUTH_TOKEN, state_); |
118 first_round_ = false; | 151 first_round_ = false; |
119 request_url_ = request->url; | 152 request_url_ = request->url; |
120 if (generate_async_) { | 153 if (generate_async_) { |
121 EXPECT_TRUE(callback_.is_null()); | 154 EXPECT_TRUE(callback_.is_null()); |
122 EXPECT_TRUE(auth_token_ == NULL); | 155 EXPECT_TRUE(auth_token_ == NULL); |
123 callback_ = callback; | 156 callback_ = callback; |
124 auth_token_ = auth_token; | 157 auth_token_ = auth_token; |
125 base::ThreadTaskRunnerHandle::Get()->PostTask( | 158 base::ThreadTaskRunnerHandle::Get()->PostTask( |
126 FROM_HERE, base::Bind(&HttpAuthHandlerMock::OnGenerateAuthToken, | 159 FROM_HERE, base::Bind(&HttpAuthHandlerMock::OnGenerateAuthToken, |
127 weak_factory_.GetWeakPtr())); | 160 weak_factory_.GetWeakPtr())); |
| 161 state_ = State::TOKEN_PENDING; |
128 return ERR_IO_PENDING; | 162 return ERR_IO_PENDING; |
129 } else { | 163 } else { |
130 if (generate_rv_ == OK) | 164 if (generate_rv_ == OK) { |
131 *auth_token = "auth_token"; | 165 *auth_token = "auth_token"; |
| 166 state_ = is_connection_based() ? State::WAIT_FOR_CHALLENGE |
| 167 : State::WAIT_FOR_GENERATE_AUTH_TOKEN; |
| 168 } else { |
| 169 state_ = State::DONE; |
| 170 } |
132 return generate_rv_; | 171 return generate_rv_; |
133 } | 172 } |
134 } | 173 } |
135 | 174 |
136 void HttpAuthHandlerMock::OnResolveCanonicalName() { | 175 void HttpAuthHandlerMock::OnResolveCanonicalName() { |
137 EXPECT_EQ(RESOLVE_ASYNC, resolve_); | 176 EXPECT_EQ(RESOLVE_ASYNC, resolve_); |
138 EXPECT_TRUE(!callback_.is_null()); | 177 EXPECT_TRUE(!callback_.is_null()); |
139 resolve_ = RESOLVE_TESTED; | 178 resolve_ = RESOLVE_TESTED; |
140 CompletionCallback callback = callback_; | 179 CompletionCallback callback = callback_; |
141 callback_.Reset(); | 180 callback_.Reset(); |
142 callback.Run(OK); | 181 callback.Run(OK); |
143 } | 182 } |
144 | 183 |
145 void HttpAuthHandlerMock::OnGenerateAuthToken() { | 184 void HttpAuthHandlerMock::OnGenerateAuthToken() { |
146 EXPECT_TRUE(generate_async_); | 185 EXPECT_TRUE(generate_async_); |
147 EXPECT_TRUE(!callback_.is_null()); | 186 EXPECT_TRUE(!callback_.is_null()); |
148 if (generate_rv_ == OK) | 187 EXPECT_EQ(State::TOKEN_PENDING, state_); |
| 188 if (generate_rv_ == OK) { |
149 *auth_token_ = "auth_token"; | 189 *auth_token_ = "auth_token"; |
| 190 state_ = is_connection_based() ? State::WAIT_FOR_CHALLENGE |
| 191 : State::WAIT_FOR_GENERATE_AUTH_TOKEN; |
| 192 } else { |
| 193 state_ = State::DONE; |
| 194 } |
150 auth_token_ = NULL; | 195 auth_token_ = NULL; |
151 CompletionCallback callback = callback_; | 196 CompletionCallback callback = callback_; |
152 callback_.Reset(); | 197 callback_.Reset(); |
153 callback.Run(generate_rv_); | 198 callback.Run(generate_rv_); |
154 } | 199 } |
155 | 200 |
156 HttpAuthHandlerMock::Factory::Factory() | 201 HttpAuthHandlerMock::Factory::Factory() |
157 : do_init_from_challenge_(false) { | 202 : do_init_from_challenge_(false) { |
158 // TODO(cbentzel): Default do_init_from_challenge_ to true. | 203 // TODO(cbentzel): Default do_init_from_challenge_ to true. |
159 } | 204 } |
(...skipping 23 matching lines...) Expand all Loading... |
183 handlers.erase(handlers.begin()); | 228 handlers.erase(handlers.begin()); |
184 if (do_init_from_challenge_ && | 229 if (do_init_from_challenge_ && |
185 !tmp_handler->InitFromChallenge(challenge, target, ssl_info, origin, | 230 !tmp_handler->InitFromChallenge(challenge, target, ssl_info, origin, |
186 net_log)) | 231 net_log)) |
187 return ERR_INVALID_RESPONSE; | 232 return ERR_INVALID_RESPONSE; |
188 handler->swap(tmp_handler); | 233 handler->swap(tmp_handler); |
189 return OK; | 234 return OK; |
190 } | 235 } |
191 | 236 |
192 } // namespace net | 237 } // namespace net |
OLD | NEW |