http_auth_handler_mock.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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
  3. // found in the LICENSE file.
  4. #include "net/http/http_auth_handler_mock.h"
  5. #include <utility>
  6. #include "base/bind.h"
  7. #include "base/location.h"
  8. #include "base/memory/ptr_util.h"
  9. #include "base/strings/string_util.h"
  10. #include "base/task/single_thread_task_runner.h"
  11. #include "base/threading/thread_task_runner_handle.h"
  12. #include "net/base/net_errors.h"
  13. #include "net/dns/host_resolver.h"
  14. #include "net/http/http_auth_challenge_tokenizer.h"
  15. #include "net/http/http_request_info.h"
  16. #include "testing/gmock/include/gmock/gmock.h"
  17. #include "testing/gtest/include/gtest/gtest.h"
  18. namespace net {
  19. void PrintTo(const HttpAuthHandlerMock::State& state, ::std::ostream* os) {
  20. switch (state) {
  21. case HttpAuthHandlerMock::State::WAIT_FOR_INIT:
  22. *os << "WAIT_FOR_INIT";
  23. break;
  24. case HttpAuthHandlerMock::State::WAIT_FOR_CHALLENGE:
  25. *os << "WAIT_FOR_CHALLENGE";
  26. break;
  27. case HttpAuthHandlerMock::State::WAIT_FOR_GENERATE_AUTH_TOKEN:
  28. *os << "WAIT_FOR_GENERATE_AUTH_TOKEN";
  29. break;
  30. case HttpAuthHandlerMock::State::TOKEN_PENDING:
  31. *os << "TOKEN_PENDING";
  32. break;
  33. case HttpAuthHandlerMock::State::DONE:
  34. *os << "DONE";
  35. break;
  36. }
  37. }
  38. HttpAuthHandlerMock::HttpAuthHandlerMock() = default;
  39. HttpAuthHandlerMock::~HttpAuthHandlerMock() = default;
  40. void HttpAuthHandlerMock::SetGenerateExpectation(bool async, int rv) {
  41. generate_async_ = async;
  42. generate_rv_ = rv;
  43. }
  44. bool HttpAuthHandlerMock::NeedsIdentity() {
  45. return first_round_;
  46. }
  47. bool HttpAuthHandlerMock::AllowsDefaultCredentials() {
  48. return allows_default_credentials_;
  49. }
  50. bool HttpAuthHandlerMock::AllowsExplicitCredentials() {
  51. return allows_explicit_credentials_;
  52. }
  53. bool HttpAuthHandlerMock::Init(
  54. HttpAuthChallengeTokenizer* challenge,
  55. const SSLInfo& ssl_info,
  56. const NetworkIsolationKey& network_isolation_key) {
  57. EXPECT_EQ(State::WAIT_FOR_INIT, state_);
  58. state_ = State::WAIT_FOR_GENERATE_AUTH_TOKEN;
  59. auth_scheme_ = HttpAuth::AUTH_SCHEME_MOCK;
  60. score_ = 1;
  61. properties_ = connection_based_ ? IS_CONNECTION_BASED : 0;
  62. return true;
  63. }
  64. int HttpAuthHandlerMock::GenerateAuthTokenImpl(
  65. const AuthCredentials* credentials,
  66. const HttpRequestInfo* request,
  67. CompletionOnceCallback callback,
  68. std::string* auth_token) {
  69. EXPECT_EQ(State::WAIT_FOR_GENERATE_AUTH_TOKEN, state_);
  70. first_round_ = false;
  71. request_url_ = request->url;
  72. if (generate_async_) {
  73. EXPECT_TRUE(callback_.is_null());
  74. EXPECT_TRUE(auth_token_ == nullptr);
  75. callback_ = std::move(callback);
  76. auth_token_ = auth_token;
  77. base::ThreadTaskRunnerHandle::Get()->PostTask(
  78. FROM_HERE, base::BindOnce(&HttpAuthHandlerMock::OnGenerateAuthToken,
  79. weak_factory_.GetWeakPtr()));
  80. state_ = State::TOKEN_PENDING;
  81. return ERR_IO_PENDING;
  82. } else {
  83. if (generate_rv_ == OK) {
  84. *auth_token = "auth_token";
  85. state_ = is_connection_based() ? State::WAIT_FOR_CHALLENGE
  86. : State::WAIT_FOR_GENERATE_AUTH_TOKEN;
  87. } else {
  88. state_ = State::DONE;
  89. }
  90. return generate_rv_;
  91. }
  92. }
  93. HttpAuth::AuthorizationResult HttpAuthHandlerMock::HandleAnotherChallengeImpl(
  94. HttpAuthChallengeTokenizer* challenge) {
  95. EXPECT_THAT(state_, ::testing::AnyOf(State::WAIT_FOR_CHALLENGE,
  96. State::WAIT_FOR_GENERATE_AUTH_TOKEN));
  97. // If we receive an empty challenge for a connection based scheme, or a second
  98. // challenge for a non connection based scheme, assume it's a rejection.
  99. if (!is_connection_based() || challenge->base64_param().empty()) {
  100. state_ = State::DONE;
  101. return HttpAuth::AUTHORIZATION_RESULT_REJECT;
  102. }
  103. if (challenge->auth_scheme() != "mock") {
  104. state_ = State::DONE;
  105. return HttpAuth::AUTHORIZATION_RESULT_INVALID;
  106. }
  107. state_ = State::WAIT_FOR_GENERATE_AUTH_TOKEN;
  108. return HttpAuth::AUTHORIZATION_RESULT_ACCEPT;
  109. }
  110. void HttpAuthHandlerMock::OnGenerateAuthToken() {
  111. EXPECT_TRUE(generate_async_);
  112. EXPECT_TRUE(!callback_.is_null());
  113. EXPECT_EQ(State::TOKEN_PENDING, state_);
  114. if (generate_rv_ == OK) {
  115. *auth_token_ = "auth_token";
  116. state_ = is_connection_based() ? State::WAIT_FOR_CHALLENGE
  117. : State::WAIT_FOR_GENERATE_AUTH_TOKEN;
  118. } else {
  119. state_ = State::DONE;
  120. }
  121. auth_token_ = nullptr;
  122. std::move(callback_).Run(generate_rv_);
  123. }
  124. HttpAuthHandlerMock::Factory::Factory() {
  125. // TODO(cbentzel): Default do_init_from_challenge_ to true.
  126. }
  127. HttpAuthHandlerMock::Factory::~Factory() = default;
  128. void HttpAuthHandlerMock::Factory::AddMockHandler(
  129. std::unique_ptr<HttpAuthHandler> handler,
  130. HttpAuth::Target target) {
  131. handlers_[target].push_back(std::move(handler));
  132. }
  133. int HttpAuthHandlerMock::Factory::CreateAuthHandler(
  134. HttpAuthChallengeTokenizer* challenge,
  135. HttpAuth::Target target,
  136. const SSLInfo& ssl_info,
  137. const NetworkIsolationKey& network_isolation_key,
  138. const url::SchemeHostPort& scheme_host_port,
  139. CreateReason reason,
  140. int nonce_count,
  141. const NetLogWithSource& net_log,
  142. HostResolver* host_resolver,
  143. std::unique_ptr<HttpAuthHandler>* handler) {
  144. if (handlers_[target].empty())
  145. return ERR_UNEXPECTED;
  146. std::unique_ptr<HttpAuthHandler> tmp_handler =
  147. std::move(handlers_[target][0]);
  148. std::vector<std::unique_ptr<HttpAuthHandler>>& handlers = handlers_[target];
  149. handlers.erase(handlers.begin());
  150. if (do_init_from_challenge_ &&
  151. !tmp_handler->InitFromChallenge(challenge, target, ssl_info,
  152. network_isolation_key, scheme_host_port,
  153. net_log)) {
  154. return ERR_INVALID_RESPONSE;
  155. }
  156. handler->swap(tmp_handler);
  157. return OK;
  158. }
  159. } // namespace net