OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 // A set of unit tests for TokenValidatorFactoryImpl | 5 // A set of unit tests for TokenValidatorFactoryImpl |
6 | 6 |
7 #include "remoting/host/token_validator_factory_impl.h" | 7 #include "remoting/host/token_validator_factory_impl.h" |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #include "base/json/json_writer.h" | 12 #include "base/json/json_writer.h" |
13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
15 #include "base/values.h" | 15 #include "base/values.h" |
| 16 #include "net/base/net_errors.h" |
16 #include "net/http/http_status_code.h" | 17 #include "net/http/http_status_code.h" |
| 18 #include "net/test/url_request/url_request_failed_job.h" |
17 #include "net/url_request/url_request_job_factory.h" | 19 #include "net/url_request/url_request_job_factory.h" |
18 #include "net/url_request/url_request_job_factory_impl.h" | 20 #include "net/url_request/url_request_job_factory_impl.h" |
19 #include "net/url_request/url_request_status.h" | 21 #include "net/url_request/url_request_status.h" |
20 #include "net/url_request/url_request_test_job.h" | 22 #include "net/url_request/url_request_test_job.h" |
21 #include "net/url_request/url_request_test_util.h" | 23 #include "net/url_request/url_request_test_util.h" |
22 #include "remoting/base/rsa_key_pair.h" | 24 #include "remoting/base/rsa_key_pair.h" |
23 #include "remoting/base/test_rsa_key_pair.h" | 25 #include "remoting/base/test_rsa_key_pair.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
25 #include "url/gurl.h" | 27 #include "url/gurl.h" |
26 | 28 |
(...skipping 10 matching lines...) Expand all Loading... |
37 // Bad scope: no nonce element. | 39 // Bad scope: no nonce element. |
38 const char kBadScope[] = | 40 const char kBadScope[] = |
39 "client:user@example.com/local host:user@example.com/remote"; | 41 "client:user@example.com/local host:user@example.com/remote"; |
40 | 42 |
41 class FakeProtocolHandler : public net::URLRequestJobFactory::ProtocolHandler { | 43 class FakeProtocolHandler : public net::URLRequestJobFactory::ProtocolHandler { |
42 public: | 44 public: |
43 FakeProtocolHandler(const std::string& headers, const std::string& response) | 45 FakeProtocolHandler(const std::string& headers, const std::string& response) |
44 : headers_(headers), | 46 : headers_(headers), |
45 response_(response) { | 47 response_(response) { |
46 } | 48 } |
| 49 |
| 50 ~FakeProtocolHandler() override {} |
| 51 |
47 net::URLRequestJob* MaybeCreateJob( | 52 net::URLRequestJob* MaybeCreateJob( |
48 net::URLRequest* request, | 53 net::URLRequest* request, |
49 net::NetworkDelegate* network_delegate) const override { | 54 net::NetworkDelegate* network_delegate) const override { |
50 return new net::URLRequestTestJob( | 55 return new net::URLRequestTestJob( |
51 request, network_delegate, headers_, response_, true); | 56 request, network_delegate, headers_, response_, true); |
52 } | 57 } |
53 | 58 |
54 private: | 59 private: |
55 std::string headers_; | 60 std::string headers_; |
56 std::string response_; | 61 std::string response_; |
57 }; | 62 }; |
58 | 63 |
| 64 // Creates URLRequestJobs that fail at the specified phase. |
| 65 class FakeFailingProtocolHandler |
| 66 : public net::URLRequestJobFactory::ProtocolHandler { |
| 67 public: |
| 68 FakeFailingProtocolHandler( |
| 69 net::URLRequestFailedJob::FailurePhase failure_phase, |
| 70 net::Error net_error) |
| 71 : failure_phase_(failure_phase), net_error_(net_error) {} |
| 72 |
| 73 ~FakeFailingProtocolHandler() override {} |
| 74 |
| 75 net::URLRequestJob* MaybeCreateJob( |
| 76 net::URLRequest* request, |
| 77 net::NetworkDelegate* network_delegate) const override { |
| 78 return new net::URLRequestFailedJob(request, network_delegate, |
| 79 failure_phase_, net_error_); |
| 80 } |
| 81 |
| 82 private: |
| 83 const net::URLRequestFailedJob::FailurePhase failure_phase_; |
| 84 const net::Error net_error_; |
| 85 }; |
| 86 |
59 class SetResponseURLRequestContext: public net::TestURLRequestContext { | 87 class SetResponseURLRequestContext: public net::TestURLRequestContext { |
60 public: | 88 public: |
61 void SetResponse(const std::string& headers, const std::string& response) { | 89 void SetResponse(const std::string& headers, const std::string& response) { |
62 std::unique_ptr<net::URLRequestJobFactoryImpl> factory = | 90 std::unique_ptr<net::URLRequestJobFactoryImpl> factory = |
63 base::MakeUnique<net::URLRequestJobFactoryImpl>(); | 91 base::MakeUnique<net::URLRequestJobFactoryImpl>(); |
64 factory->SetProtocolHandler( | 92 factory->SetProtocolHandler( |
65 "https", base::MakeUnique<FakeProtocolHandler>(headers, response)); | 93 "https", base::MakeUnique<FakeProtocolHandler>(headers, response)); |
66 context_storage_.set_job_factory(std::move(factory)); | 94 context_storage_.set_job_factory(std::move(factory)); |
67 } | 95 } |
| 96 |
| 97 void SetErrorResponse(net::URLRequestFailedJob::FailurePhase failure_phase, |
| 98 net::Error net_error) { |
| 99 std::unique_ptr<net::URLRequestJobFactoryImpl> factory = |
| 100 base::MakeUnique<net::URLRequestJobFactoryImpl>(); |
| 101 factory->SetProtocolHandler( |
| 102 "https", |
| 103 base::MakeUnique<FakeFailingProtocolHandler>(failure_phase, net_error)); |
| 104 context_storage_.set_job_factory(std::move(factory)); |
| 105 } |
68 }; | 106 }; |
69 | 107 |
70 } // namespace | 108 } // namespace |
71 | 109 |
72 namespace remoting { | 110 namespace remoting { |
73 | 111 |
74 class TokenValidatorFactoryImplTest : public testing::Test { | 112 class TokenValidatorFactoryImplTest : public testing::Test { |
75 public: | 113 public: |
76 TokenValidatorFactoryImplTest() : message_loop_(base::MessageLoop::TYPE_IO) {} | 114 TokenValidatorFactoryImplTest() : message_loop_(base::MessageLoop::TYPE_IO) {} |
77 | 115 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 } | 162 } |
125 | 163 |
126 | 164 |
127 void SetResponse(const std::string& headers, const std::string& response) { | 165 void SetResponse(const std::string& headers, const std::string& response) { |
128 SetResponseURLRequestContext* context = | 166 SetResponseURLRequestContext* context = |
129 static_cast<SetResponseURLRequestContext*>( | 167 static_cast<SetResponseURLRequestContext*>( |
130 request_context_getter_->GetURLRequestContext()); | 168 request_context_getter_->GetURLRequestContext()); |
131 context->SetResponse(headers, response); | 169 context->SetResponse(headers, response); |
132 } | 170 } |
133 | 171 |
| 172 void SetErrorResponse(net::URLRequestFailedJob::FailurePhase failure_phase, |
| 173 net::Error net_error) { |
| 174 SetResponseURLRequestContext* context = |
| 175 static_cast<SetResponseURLRequestContext*>( |
| 176 request_context_getter_->GetURLRequestContext()); |
| 177 context->SetErrorResponse(failure_phase, net_error); |
| 178 } |
| 179 |
134 base::MessageLoop message_loop_; | 180 base::MessageLoop message_loop_; |
135 scoped_refptr<RsaKeyPair> key_pair_; | 181 scoped_refptr<RsaKeyPair> key_pair_; |
136 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | 182 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
137 scoped_refptr<TokenValidatorFactoryImpl> token_validator_factory_; | 183 scoped_refptr<TokenValidatorFactoryImpl> token_validator_factory_; |
138 std::unique_ptr<protocol::TokenValidator> token_validator_; | 184 std::unique_ptr<protocol::TokenValidator> token_validator_; |
139 }; | 185 }; |
140 | 186 |
141 TEST_F(TokenValidatorFactoryImplTest, Success) { | 187 TEST_F(TokenValidatorFactoryImplTest, Success) { |
142 token_validator_ = token_validator_factory_->CreateTokenValidator( | 188 token_validator_ = token_validator_factory_->CreateTokenValidator( |
143 kLocalJid, kRemoteJid); | 189 kLocalJid, kRemoteJid); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 | 228 |
183 SetResponse(net::URLRequestTestJob::test_error_headers(), std::string()); | 229 SetResponse(net::URLRequestTestJob::test_error_headers(), std::string()); |
184 | 230 |
185 token_validator_->ValidateThirdPartyToken( | 231 token_validator_->ValidateThirdPartyToken( |
186 kToken, base::Bind( | 232 kToken, base::Bind( |
187 &TokenValidatorFactoryImplTest::DeleteOnFailureCallback, | 233 &TokenValidatorFactoryImplTest::DeleteOnFailureCallback, |
188 base::Unretained(this))); | 234 base::Unretained(this))); |
189 base::RunLoop().Run(); | 235 base::RunLoop().Run(); |
190 } | 236 } |
191 | 237 |
| 238 TEST_F(TokenValidatorFactoryImplTest, DeleteOnStartError) { |
| 239 token_validator_ = |
| 240 token_validator_factory_->CreateTokenValidator(kLocalJid, kRemoteJid); |
| 241 |
| 242 SetErrorResponse(net::URLRequestFailedJob::START, net::ERR_FAILED); |
| 243 |
| 244 token_validator_->ValidateThirdPartyToken( |
| 245 kToken, |
| 246 base::Bind(&TokenValidatorFactoryImplTest::DeleteOnFailureCallback, |
| 247 base::Unretained(this))); |
| 248 base::RunLoop().Run(); |
| 249 } |
| 250 |
| 251 TEST_F(TokenValidatorFactoryImplTest, DeleteOnSyncReadError) { |
| 252 token_validator_ = |
| 253 token_validator_factory_->CreateTokenValidator(kLocalJid, kRemoteJid); |
| 254 |
| 255 SetErrorResponse(net::URLRequestFailedJob::READ_SYNC, net::ERR_FAILED); |
| 256 |
| 257 token_validator_->ValidateThirdPartyToken( |
| 258 kToken, |
| 259 base::Bind(&TokenValidatorFactoryImplTest::DeleteOnFailureCallback, |
| 260 base::Unretained(this))); |
| 261 base::RunLoop().Run(); |
| 262 } |
| 263 |
| 264 TEST_F(TokenValidatorFactoryImplTest, DeleteOnAsyncReadError) { |
| 265 token_validator_ = |
| 266 token_validator_factory_->CreateTokenValidator(kLocalJid, kRemoteJid); |
| 267 |
| 268 SetErrorResponse(net::URLRequestFailedJob::READ_ASYNC, net::ERR_FAILED); |
| 269 |
| 270 token_validator_->ValidateThirdPartyToken( |
| 271 kToken, |
| 272 base::Bind(&TokenValidatorFactoryImplTest::DeleteOnFailureCallback, |
| 273 base::Unretained(this))); |
| 274 base::RunLoop().Run(); |
| 275 } |
| 276 |
192 } // namespace remoting | 277 } // namespace remoting |
OLD | NEW |