1 // Copyright 2011 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef NET_HTTP_HTTP_AUTH_HANDLER_MOCK_H_ 6 #define NET_HTTP_HTTP_AUTH_HANDLER_MOCK_H_ 7 8 #include <memory> 9 #include <ostream> 10 #include <string> 11 #include <vector> 12 13 #include "base/memory/raw_ptr.h" 14 #include "base/memory/weak_ptr.h" 15 #include "net/base/completion_once_callback.h" 16 #include "net/base/net_errors.h" 17 #include "net/http/http_auth_handler.h" 18 #include "net/http/http_auth_handler_factory.h" 19 #include "url/gurl.h" 20 21 namespace url { 22 class SchemeHostPort; 23 } 24 25 namespace net { 26 27 // MockAuthHandler is used in tests to reliably trigger edge cases. 28 class HttpAuthHandlerMock : public HttpAuthHandler { 29 public: 30 enum class State { 31 WAIT_FOR_INIT, 32 WAIT_FOR_CHALLENGE, 33 WAIT_FOR_GENERATE_AUTH_TOKEN, 34 TOKEN_PENDING, 35 DONE 36 }; 37 38 // The Factory class returns handlers in the order they were added via 39 // AddMockHandler. 40 class Factory : public HttpAuthHandlerFactory { 41 public: 42 Factory(); 43 ~Factory() override; 44 45 void AddMockHandler(std::unique_ptr<HttpAuthHandler> handler, 46 HttpAuth::Target target); 47 set_do_init_from_challenge(bool do_init_from_challenge)48 void set_do_init_from_challenge(bool do_init_from_challenge) { 49 do_init_from_challenge_ = do_init_from_challenge; 50 } 51 52 // HttpAuthHandlerFactory: 53 int CreateAuthHandler( 54 HttpAuthChallengeTokenizer* challenge, 55 HttpAuth::Target target, 56 const SSLInfo& ssl_info, 57 const NetworkAnonymizationKey& network_anonymization_key, 58 const url::SchemeHostPort& scheme_host_port, 59 CreateReason reason, 60 int nonce_count, 61 const NetLogWithSource& net_log, 62 HostResolver* host_resolver, 63 std::unique_ptr<HttpAuthHandler>* handler) override; 64 65 private: 66 std::vector<std::unique_ptr<HttpAuthHandler>> 67 handlers_[HttpAuth::AUTH_NUM_TARGETS]; 68 bool do_init_from_challenge_ = false; 69 }; 70 71 HttpAuthHandlerMock(); 72 73 ~HttpAuthHandlerMock() override; 74 75 76 void SetGenerateExpectation(bool async, int rv); 77 set_connection_based(bool connection_based)78 void set_connection_based(bool connection_based) { 79 connection_based_ = connection_based; 80 } 81 set_allows_default_credentials(bool allows_default_credentials)82 void set_allows_default_credentials(bool allows_default_credentials) { 83 allows_default_credentials_ = allows_default_credentials; 84 } 85 set_allows_explicit_credentials(bool allows_explicit_credentials)86 void set_allows_explicit_credentials(bool allows_explicit_credentials) { 87 allows_explicit_credentials_ = allows_explicit_credentials; 88 } 89 request_url()90 const GURL& request_url() const { 91 return request_url_; 92 } 93 state()94 State state() const { return state_; } 95 96 protected: 97 // HttpAuthHandler 98 bool NeedsIdentity() override; 99 bool AllowsDefaultCredentials() override; 100 bool AllowsExplicitCredentials() override; 101 bool Init(HttpAuthChallengeTokenizer* challenge, 102 const SSLInfo& ssl_info, 103 const NetworkAnonymizationKey& network_anonymization_key) override; 104 int GenerateAuthTokenImpl(const AuthCredentials* credentials, 105 const HttpRequestInfo* request, 106 CompletionOnceCallback callback, 107 std::string* auth_token) override; 108 HttpAuth::AuthorizationResult HandleAnotherChallengeImpl( 109 HttpAuthChallengeTokenizer* challenge) override; 110 111 private: 112 void OnGenerateAuthToken(); 113 114 State state_ = State::WAIT_FOR_INIT; 115 CompletionOnceCallback callback_; 116 bool generate_async_ = false; 117 int generate_rv_ = OK; 118 raw_ptr<std::string> auth_token_ = nullptr; 119 bool first_round_ = true; 120 bool connection_based_ = false; 121 bool allows_default_credentials_ = false; 122 bool allows_explicit_credentials_ = true; 123 GURL request_url_; 124 base::WeakPtrFactory<HttpAuthHandlerMock> weak_factory_{this}; 125 }; 126 127 void PrintTo(const HttpAuthHandlerMock::State& state, ::std::ostream* os); 128 129 } // namespace net 130 131 #endif // NET_HTTP_HTTP_AUTH_HANDLER_MOCK_H_ 132