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/callback_helpers.h" |
8 #include "base/location.h" | 9 #include "base/location.h" |
9 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
10 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
11 #include "base/thread_task_runner_handle.h" | 12 #include "base/thread_task_runner_handle.h" |
12 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
13 #include "net/http/http_auth_challenge_tokenizer.h" | 14 #include "net/http/http_auth_challenge_tokenizer.h" |
14 #include "net/http/http_request_info.h" | 15 #include "net/http/http_request_info.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
16 | 17 |
17 namespace net { | 18 namespace net { |
18 | 19 |
19 HttpAuthHandlerMock::HttpAuthHandlerMock() | 20 HttpAuthHandlerMock::HttpAuthHandlerMock() |
20 : HttpAuthHandler("mock"), weak_factory_(this) {} | 21 : HttpAuthHandler("mock"), weak_factory_(this) {} |
21 | 22 |
22 HttpAuthHandlerMock::~HttpAuthHandlerMock() {} | 23 HttpAuthHandlerMock::~HttpAuthHandlerMock() {} |
23 | 24 |
| 25 void HttpAuthHandlerMock::SetInitExpectation(bool async, int rv) { |
| 26 init_async_ = async; |
| 27 init_rv_ = rv; |
| 28 } |
| 29 |
24 void HttpAuthHandlerMock::SetGenerateExpectation(bool async, int rv) { | 30 void HttpAuthHandlerMock::SetGenerateExpectation(bool async, int rv) { |
25 generate_async_ = async; | 31 generate_async_ = async; |
26 generate_rv_ = rv; | 32 generate_rv_ = rv; |
27 } | 33 } |
28 | 34 |
29 HttpAuth::AuthorizationResult HttpAuthHandlerMock::HandleAnotherChallenge( | 35 HttpAuth::AuthorizationResult HttpAuthHandlerMock::HandleAnotherChallenge( |
30 const HttpAuthChallengeTokenizer& challenge) { | 36 const HttpAuthChallengeTokenizer& challenge) { |
31 EXPECT_TRUE(challenge.SchemeIs(auth_scheme_)); | 37 EXPECT_TRUE(challenge.SchemeIs(auth_scheme_)) |
| 38 << "Auth scheme of challenge doesn't match scheme of AuthHandler."; |
| 39 EXPECT_TRUE(initialized_) << __FUNCTION__ << " called without calling Init."; |
| 40 EXPECT_TRUE(initialization_succeeded) << __FUNCTION__ |
| 41 << " called after a failed Init."; |
32 // If we receive a second challenge for a regular scheme, assume it's a | 42 // If we receive a second challenge for a regular scheme, assume it's a |
33 // rejection. Receiving an empty second challenge when expecting multiple | 43 // rejection. Receiving an empty second challenge when expecting multiple |
34 // rounds is also considered a rejection. | 44 // rounds is also considered a rejection. |
35 if (!expect_multiple_challenges_ || challenge.base64_param().empty()) | 45 base::StringPiece challenge_params(challenge.params_begin(), |
| 46 challenge.params_end()); |
| 47 if (!expect_multiple_challenges_ || challenge_params.empty()) |
36 return HttpAuth::AUTHORIZATION_RESULT_REJECT; | 48 return HttpAuth::AUTHORIZATION_RESULT_REJECT; |
37 if (!challenge.SchemeIs(auth_scheme_)) | |
38 return HttpAuth::AUTHORIZATION_RESULT_INVALID; | |
39 auth_token_ = auth_scheme_; | 49 auth_token_ = auth_scheme_; |
40 auth_token_.append(" continuation,"); | 50 auth_token_.append(" continuation,"); |
41 auth_token_.append(challenge.base64_param()); | 51 auth_token_.append(challenge.params_begin(), challenge.params_end()); |
42 return HttpAuth::AUTHORIZATION_RESULT_ACCEPT; | 52 return HttpAuth::AUTHORIZATION_RESULT_ACCEPT; |
43 } | 53 } |
44 | 54 |
45 bool HttpAuthHandlerMock::NeedsIdentity() { | 55 bool HttpAuthHandlerMock::NeedsIdentity() { |
| 56 EXPECT_TRUE(initialized_) << __FUNCTION__ << " called without calling Init."; |
| 57 EXPECT_TRUE(initialization_succeeded) << __FUNCTION__ |
| 58 << " called after a failed Init."; |
46 return first_round_; | 59 return first_round_; |
47 } | 60 } |
48 | 61 |
49 bool HttpAuthHandlerMock::AllowsDefaultCredentials() { | 62 bool HttpAuthHandlerMock::AllowsDefaultCredentials() { |
| 63 EXPECT_TRUE(initialized_) << __FUNCTION__ << " called without calling Init."; |
| 64 EXPECT_TRUE(initialization_succeeded) << __FUNCTION__ |
| 65 << " called after a failed Init."; |
50 return allows_default_credentials_; | 66 return allows_default_credentials_; |
51 } | 67 } |
52 | 68 |
53 bool HttpAuthHandlerMock::AllowsExplicitCredentials() { | 69 bool HttpAuthHandlerMock::AllowsExplicitCredentials() { |
| 70 EXPECT_TRUE(initialized_) << __FUNCTION__ << " called without calling Init."; |
| 71 EXPECT_TRUE(initialization_succeeded) << __FUNCTION__ |
| 72 << " called after a failed Init."; |
54 return allows_explicit_credentials_; | 73 return allows_explicit_credentials_; |
55 } | 74 } |
56 | 75 |
57 int HttpAuthHandlerMock::Init(const HttpAuthChallengeTokenizer& challenge) { | 76 int HttpAuthHandlerMock::InitializeFromChallengeInternal( |
| 77 const HttpAuthChallengeTokenizer& challenge, |
| 78 const HttpResponseInfo& response_with_challenge, |
| 79 const CompletionCallback& callback) { |
| 80 EXPECT_FALSE(initialized_) << "Init already called once."; |
58 EXPECT_TRUE(challenge.SchemeIs(auth_scheme_)) | 81 EXPECT_TRUE(challenge.SchemeIs(auth_scheme_)) |
59 << "Mismatched scheme for challenge: " << challenge.challenge_text(); | 82 << "Mismatched scheme for challenge: " << challenge.challenge_text(); |
60 EXPECT_TRUE(HttpAuth::IsValidNormalizedScheme(auth_scheme_)) | 83 EXPECT_TRUE(HttpAuth::IsValidNormalizedScheme(auth_scheme_)) |
61 << "Invalid expected auth scheme."; | 84 << "Invalid expected auth scheme."; |
| 85 |
| 86 base::StringPiece challenge_params(challenge.params_begin(), |
| 87 challenge.params_end()); |
| 88 initialized_ = true; |
| 89 |
62 auth_token_ = auth_scheme_ + " auth_token"; | 90 auth_token_ = auth_scheme_ + " auth_token"; |
63 if (challenge.params_end() != challenge.params_begin()) { | 91 if (!challenge_params.empty()) { |
64 auth_token_ += ","; | 92 auth_token_ += ","; |
65 auth_token_.append(challenge.params_begin(), challenge.params_end()); | 93 auth_token_.append(challenge.params_begin(), challenge.params_end()); |
66 } | 94 } |
| 95 |
| 96 if (init_async_) { |
| 97 callback_ = callback; |
| 98 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 99 FROM_HERE, base::Bind(&HttpAuthHandlerMock::OnInitializeComplete, |
| 100 weak_factory_.GetWeakPtr())); |
| 101 return ERR_IO_PENDING; |
| 102 } |
| 103 |
| 104 initialization_succeeded = (init_rv_ == OK); |
| 105 return init_rv_; |
| 106 } |
| 107 |
| 108 void HttpAuthHandlerMock::OnInitializeComplete() { |
| 109 ASSERT_FALSE(callback_.is_null()); |
| 110 initialization_succeeded = (init_rv_ == OK); |
| 111 base::ResetAndReturn(&callback_).Run(init_rv_); |
| 112 } |
| 113 |
| 114 int HttpAuthHandlerMock::InitializeFromCacheEntryInternal( |
| 115 HttpAuthCache::Entry* cache_entry) { |
| 116 EXPECT_FALSE(initialized_) << "Init already called once."; |
| 117 EXPECT_EQ(auth_scheme_, cache_entry->scheme()) |
| 118 << "Mismatched scheme for challenge: " << cache_entry->auth_challenge(); |
| 119 EXPECT_TRUE(HttpAuth::IsValidNormalizedScheme(auth_scheme_)) |
| 120 << "Invalid expected auth scheme."; |
| 121 |
| 122 HttpAuthChallengeTokenizer challenge(cache_entry->auth_challenge().begin(), |
| 123 cache_entry->auth_challenge().end()); |
| 124 EXPECT_TRUE(challenge.SchemeIs(auth_scheme_)) |
| 125 << "Mismatched scheme for challenge: " << challenge.challenge_text(); |
| 126 |
| 127 auth_token_ = auth_scheme_ + " preemptive_auth_token"; |
| 128 if (challenge.params_begin() != challenge.params_end()) { |
| 129 auth_token_ += ","; |
| 130 auth_token_.append(challenge.params_begin(), challenge.params_end()); |
| 131 } |
| 132 |
| 133 initialized_ = true; |
| 134 initialization_succeeded = true; |
67 return OK; | 135 return OK; |
68 } | 136 } |
69 | 137 |
70 int HttpAuthHandlerMock::GenerateAuthTokenImpl( | 138 int HttpAuthHandlerMock::GenerateAuthTokenImpl( |
71 const AuthCredentials* credentials, | 139 const AuthCredentials* credentials, |
72 const HttpRequestInfo& request, | 140 const HttpRequestInfo& request, |
73 const CompletionCallback& callback, | 141 const CompletionCallback& callback, |
74 std::string* auth_token) { | 142 std::string* auth_token) { |
| 143 EXPECT_TRUE(initialized_) << __FUNCTION__ << " called without calling Init."; |
| 144 EXPECT_TRUE(initialization_succeeded) << __FUNCTION__ |
| 145 << " called after a failed Init."; |
75 first_round_ = false; | 146 first_round_ = false; |
76 request_url_ = request.url; | 147 request_url_ = request.url; |
77 | 148 |
78 if (!credentials || credentials->Empty()) { | 149 if (credentials) { |
| 150 EXPECT_TRUE(AllowsExplicitCredentials()) << "Explicit credentials can only " |
| 151 "be specified if the handler " |
| 152 "supports it."; |
| 153 } else { |
79 EXPECT_TRUE(AllowsDefaultCredentials()) << "Credentials must be specified " | 154 EXPECT_TRUE(AllowsDefaultCredentials()) << "Credentials must be specified " |
80 "if the handler doesn't support " | 155 "if the handler doesn't support " |
81 "default credentials."; | 156 "default credentials."; |
82 } else { | |
83 EXPECT_TRUE(AllowsExplicitCredentials()) << "Explicit credentials can only " | |
84 "be specified if the handler " | |
85 "supports it."; | |
86 } | 157 } |
87 | 158 |
88 if (generate_async_) { | 159 if (generate_async_) { |
89 EXPECT_TRUE(callback_.is_null()) << "GenerateAuthTokenImpl() called while " | 160 EXPECT_TRUE(callback_.is_null()) << "GenerateAuthTokenImpl() called while " |
90 "a pending request is in progress"; | 161 "a pending request is in progress"; |
91 EXPECT_EQ(nullptr, generate_auth_token_buffer_) | 162 EXPECT_EQ(nullptr, generate_auth_token_buffer_) |
92 << "GenerateAuthTokenImpl() called before previous token was retrieved"; | 163 << "GenerateAuthTokenImpl() called before previous token was retrieved"; |
93 callback_ = callback; | 164 callback_ = callback; |
94 generate_auth_token_buffer_ = auth_token; | 165 generate_auth_token_buffer_ = auth_token; |
95 base::ThreadTaskRunnerHandle::Get()->PostTask( | 166 base::ThreadTaskRunnerHandle::Get()->PostTask( |
96 FROM_HERE, base::Bind(&HttpAuthHandlerMock::OnGenerateAuthToken, | 167 FROM_HERE, base::Bind(&HttpAuthHandlerMock::OnGenerateAuthTokenComplete, |
97 weak_factory_.GetWeakPtr())); | 168 weak_factory_.GetWeakPtr())); |
98 return ERR_IO_PENDING; | 169 return ERR_IO_PENDING; |
99 } else { | 170 } else { |
100 if (generate_rv_ == OK) | 171 if (generate_rv_ == OK) |
101 *auth_token = auth_token_; | 172 *auth_token = auth_token_; |
102 return generate_rv_; | 173 return generate_rv_; |
103 } | 174 } |
104 } | 175 } |
105 | 176 |
106 void HttpAuthHandlerMock::OnGenerateAuthToken() { | 177 void HttpAuthHandlerMock::OnGenerateAuthTokenComplete() { |
107 EXPECT_TRUE(generate_async_); | 178 EXPECT_TRUE(generate_async_); |
108 EXPECT_TRUE(!callback_.is_null()); | 179 EXPECT_TRUE(!callback_.is_null()); |
109 if (generate_rv_ == OK) | 180 if (generate_rv_ == OK) |
110 *generate_auth_token_buffer_ = auth_token_; | 181 *generate_auth_token_buffer_ = auth_token_; |
111 generate_auth_token_buffer_ = nullptr; | 182 generate_auth_token_buffer_ = nullptr; |
112 CompletionCallback callback = callback_; | 183 base::ResetAndReturn(&callback_).Run(generate_rv_); |
113 callback_.Reset(); | |
114 callback.Run(generate_rv_); | |
115 } | 184 } |
116 | 185 |
117 HttpAuthHandlerMock::Factory::Factory() {} | 186 HttpAuthHandlerMock::Factory::Factory() {} |
118 | 187 |
119 HttpAuthHandlerMock::Factory::~Factory() { | 188 HttpAuthHandlerMock::Factory::~Factory() { |
120 } | 189 } |
121 | 190 |
122 void HttpAuthHandlerMock::Factory::AddMockHandler( | 191 void HttpAuthHandlerMock::Factory::AddMockHandler( |
123 scoped_ptr<HttpAuthHandler> handler, | 192 scoped_ptr<HttpAuthHandler> handler, |
124 HttpAuthHandlerCreateReason reason) { | 193 HttpAuthHandlerCreateReason reason) { |
(...skipping 17 matching lines...) Expand all Loading... |
142 } | 211 } |
143 | 212 |
144 scoped_ptr<HttpAuthHandler> | 213 scoped_ptr<HttpAuthHandler> |
145 HttpAuthHandlerMock::Factory::CreateAuthHandlerForScheme( | 214 HttpAuthHandlerMock::Factory::CreateAuthHandlerForScheme( |
146 const std::string& scheme) { | 215 const std::string& scheme) { |
147 scoped_ptr<HttpAuthHandler> handler = | 216 scoped_ptr<HttpAuthHandler> handler = |
148 GetNextAuthHandler(&challenge_handlers_); | 217 GetNextAuthHandler(&challenge_handlers_); |
149 EXPECT_TRUE(!handler || handler->auth_scheme() == scheme) | 218 EXPECT_TRUE(!handler || handler->auth_scheme() == scheme) |
150 << "Next auth handler is for scheme " << handler->auth_scheme() | 219 << "Next auth handler is for scheme " << handler->auth_scheme() |
151 << " while request scheme is " << scheme; | 220 << " while request scheme is " << scheme; |
152 return handler.Pass(); | 221 return handler; |
153 } | 222 } |
154 | 223 |
155 scoped_ptr<HttpAuthHandler> | 224 scoped_ptr<HttpAuthHandler> |
156 HttpAuthHandlerMock::Factory::CreateAndInitPreemptiveAuthHandler( | 225 HttpAuthHandlerMock::Factory::CreateAndInitPreemptiveAuthHandler( |
157 HttpAuthCache::Entry* cache_entry, | 226 HttpAuthCache::Entry* cache_entry, |
158 const HttpAuthChallengeTokenizer& tokenizer, | |
159 HttpAuth::Target target, | 227 HttpAuth::Target target, |
160 const BoundNetLog& net_log) { | 228 const BoundNetLog& net_log) { |
161 scoped_ptr<HttpAuthHandler> handler = | 229 scoped_ptr<HttpAuthHandler> handler = |
162 GetNextAuthHandler(&preemptive_handlers_); | 230 GetNextAuthHandler(&preemptive_handlers_); |
163 if (handler) { | 231 if (handler) |
164 handler->HandleInitialChallenge(tokenizer, target, cache_entry->origin(), | 232 handler->InitializeFromCacheEntry(cache_entry, target, net_log); |
165 net_log); | |
166 } | |
167 return handler; | 233 return handler; |
168 } | 234 } |
169 | 235 |
170 } // namespace net | 236 } // namespace net |
OLD | NEW |