| 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" |
| 8 |
| 9 #include <memory> |
| 7 #include <string> | 10 #include <string> |
| 8 | 11 |
| 9 #include "base/json/json_writer.h" | 12 #include "base/json/json_writer.h" |
| 10 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/ptr_util.h" |
| 11 #include "base/values.h" | 14 #include "base/values.h" |
| 12 #include "net/http/http_status_code.h" | 15 #include "net/http/http_status_code.h" |
| 13 #include "net/url_request/url_request_job_factory.h" | 16 #include "net/url_request/url_request_job_factory.h" |
| 14 #include "net/url_request/url_request_job_factory_impl.h" | 17 #include "net/url_request/url_request_job_factory_impl.h" |
| 15 #include "net/url_request/url_request_status.h" | 18 #include "net/url_request/url_request_status.h" |
| 16 #include "net/url_request/url_request_test_job.h" | 19 #include "net/url_request/url_request_test_job.h" |
| 17 #include "net/url_request/url_request_test_util.h" | 20 #include "net/url_request/url_request_test_util.h" |
| 18 #include "remoting/base/rsa_key_pair.h" | 21 #include "remoting/base/rsa_key_pair.h" |
| 19 #include "remoting/base/test_rsa_key_pair.h" | 22 #include "remoting/base/test_rsa_key_pair.h" |
| 20 #include "remoting/host/token_validator_factory_impl.h" | |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 22 #include "url/gurl.h" | 24 #include "url/gurl.h" |
| 23 | 25 |
| 24 namespace { | 26 namespace { |
| 25 | 27 |
| 26 const char kTokenUrl[] = "https://example.com/token"; | 28 const char kTokenUrl[] = "https://example.com/token"; |
| 27 const char kTokenValidationUrl[] = "https://example.com/validate"; | 29 const char kTokenValidationUrl[] = "https://example.com/validate"; |
| 28 const char kTokenValidationCertIssuer[] = ""; | 30 const char kTokenValidationCertIssuer[] = ""; |
| 29 const char kLocalJid[] = "user@example.com/local"; | 31 const char kLocalJid[] = "user@example.com/local"; |
| 30 const char kRemoteJid[] = "user@example.com/remote"; | 32 const char kRemoteJid[] = "user@example.com/remote"; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 49 } | 51 } |
| 50 | 52 |
| 51 private: | 53 private: |
| 52 std::string headers_; | 54 std::string headers_; |
| 53 std::string response_; | 55 std::string response_; |
| 54 }; | 56 }; |
| 55 | 57 |
| 56 class SetResponseURLRequestContext: public net::TestURLRequestContext { | 58 class SetResponseURLRequestContext: public net::TestURLRequestContext { |
| 57 public: | 59 public: |
| 58 void SetResponse(const std::string& headers, const std::string& response) { | 60 void SetResponse(const std::string& headers, const std::string& response) { |
| 59 scoped_ptr<net::URLRequestJobFactoryImpl> factory = | 61 std::unique_ptr<net::URLRequestJobFactoryImpl> factory = |
| 60 make_scoped_ptr(new net::URLRequestJobFactoryImpl()); | 62 base::WrapUnique(new net::URLRequestJobFactoryImpl()); |
| 61 factory->SetProtocolHandler( | 63 factory->SetProtocolHandler( |
| 62 "https", make_scoped_ptr(new FakeProtocolHandler(headers, response))); | 64 "https", base::WrapUnique(new FakeProtocolHandler(headers, response))); |
| 63 context_storage_.set_job_factory(std::move(factory)); | 65 context_storage_.set_job_factory(std::move(factory)); |
| 64 } | 66 } |
| 65 }; | 67 }; |
| 66 | 68 |
| 67 } // namespace | 69 } // namespace |
| 68 | 70 |
| 69 namespace remoting { | 71 namespace remoting { |
| 70 | 72 |
| 71 class TokenValidatorFactoryImplTest : public testing::Test { | 73 class TokenValidatorFactoryImplTest : public testing::Test { |
| 72 public: | 74 public: |
| (...skipping 13 matching lines...) Expand all Loading... |
| 86 EXPECT_TRUE(shared_secret.empty()); | 88 EXPECT_TRUE(shared_secret.empty()); |
| 87 token_validator_.reset(); | 89 token_validator_.reset(); |
| 88 message_loop_.QuitWhenIdle(); | 90 message_loop_.QuitWhenIdle(); |
| 89 } | 91 } |
| 90 | 92 |
| 91 protected: | 93 protected: |
| 92 void SetUp() override { | 94 void SetUp() override { |
| 93 key_pair_ = RsaKeyPair::FromString(kTestRsaKeyPair); | 95 key_pair_ = RsaKeyPair::FromString(kTestRsaKeyPair); |
| 94 request_context_getter_ = new net::TestURLRequestContextGetter( | 96 request_context_getter_ = new net::TestURLRequestContextGetter( |
| 95 message_loop_.task_runner(), | 97 message_loop_.task_runner(), |
| 96 make_scoped_ptr(new SetResponseURLRequestContext())); | 98 base::WrapUnique(new SetResponseURLRequestContext())); |
| 97 ThirdPartyAuthConfig config; | 99 ThirdPartyAuthConfig config; |
| 98 config.token_url = GURL(kTokenUrl); | 100 config.token_url = GURL(kTokenUrl); |
| 99 config.token_validation_url = GURL(kTokenValidationUrl); | 101 config.token_validation_url = GURL(kTokenValidationUrl); |
| 100 config.token_validation_cert_issuer = kTokenValidationCertIssuer; | 102 config.token_validation_cert_issuer = kTokenValidationCertIssuer; |
| 101 token_validator_factory_ = new TokenValidatorFactoryImpl( | 103 token_validator_factory_ = new TokenValidatorFactoryImpl( |
| 102 config, key_pair_, request_context_getter_); | 104 config, key_pair_, request_context_getter_); |
| 103 } | 105 } |
| 104 | 106 |
| 105 static std::string CreateResponse(const std::string& scope) { | 107 static std::string CreateResponse(const std::string& scope) { |
| 106 base::DictionaryValue response_dict; | 108 base::DictionaryValue response_dict; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 125 SetResponseURLRequestContext* context = | 127 SetResponseURLRequestContext* context = |
| 126 static_cast<SetResponseURLRequestContext*>( | 128 static_cast<SetResponseURLRequestContext*>( |
| 127 request_context_getter_->GetURLRequestContext()); | 129 request_context_getter_->GetURLRequestContext()); |
| 128 context->SetResponse(headers, response); | 130 context->SetResponse(headers, response); |
| 129 } | 131 } |
| 130 | 132 |
| 131 base::MessageLoop message_loop_; | 133 base::MessageLoop message_loop_; |
| 132 scoped_refptr<RsaKeyPair> key_pair_; | 134 scoped_refptr<RsaKeyPair> key_pair_; |
| 133 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | 135 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
| 134 scoped_refptr<TokenValidatorFactoryImpl> token_validator_factory_; | 136 scoped_refptr<TokenValidatorFactoryImpl> token_validator_factory_; |
| 135 scoped_ptr<protocol::TokenValidator> token_validator_; | 137 std::unique_ptr<protocol::TokenValidator> token_validator_; |
| 136 }; | 138 }; |
| 137 | 139 |
| 138 TEST_F(TokenValidatorFactoryImplTest, Success) { | 140 TEST_F(TokenValidatorFactoryImplTest, Success) { |
| 139 token_validator_ = token_validator_factory_->CreateTokenValidator( | 141 token_validator_ = token_validator_factory_->CreateTokenValidator( |
| 140 kLocalJid, kRemoteJid); | 142 kLocalJid, kRemoteJid); |
| 141 | 143 |
| 142 SetResponse(net::URLRequestTestJob::test_headers(), | 144 SetResponse(net::URLRequestTestJob::test_headers(), |
| 143 CreateResponse(token_validator_->token_scope())); | 145 CreateResponse(token_validator_->token_scope())); |
| 144 | 146 |
| 145 token_validator_->ValidateThirdPartyToken( | 147 token_validator_->ValidateThirdPartyToken( |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 SetResponse(net::URLRequestTestJob::test_error_headers(), std::string()); | 182 SetResponse(net::URLRequestTestJob::test_error_headers(), std::string()); |
| 181 | 183 |
| 182 token_validator_->ValidateThirdPartyToken( | 184 token_validator_->ValidateThirdPartyToken( |
| 183 kToken, base::Bind( | 185 kToken, base::Bind( |
| 184 &TokenValidatorFactoryImplTest::DeleteOnFailureCallback, | 186 &TokenValidatorFactoryImplTest::DeleteOnFailureCallback, |
| 185 base::Unretained(this))); | 187 base::Unretained(this))); |
| 186 message_loop_.Run(); | 188 message_loop_.Run(); |
| 187 } | 189 } |
| 188 | 190 |
| 189 } // namespace remoting | 191 } // namespace remoting |
| OLD | NEW |