Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / remoting / host / token_validator_factory_impl_unittest.cc
blob2b5e0aac7f65370308251cfd5b24fad06ced9f2f
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
3 // found in the LICENSE file.
4 //
5 // A set of unit tests for TokenValidatorFactoryImpl
7 #include <string>
9 #include "base/json/json_writer.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/values.h"
12 #include "net/http/http_status_code.h"
13 #include "net/url_request/url_request_job_factory.h"
14 #include "net/url_request/url_request_job_factory_impl.h"
15 #include "net/url_request/url_request_status.h"
16 #include "net/url_request/url_request_test_job.h"
17 #include "net/url_request/url_request_test_util.h"
18 #include "remoting/base/rsa_key_pair.h"
19 #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"
22 #include "url/gurl.h"
24 namespace {
26 const char kTokenUrl[] = "https://example.com/token";
27 const char kTokenValidationUrl[] = "https://example.com/validate";
28 const char kTokenValidationCertIssuer[] = "";
29 const char kLocalJid[] = "user@example.com/local";
30 const char kRemoteJid[] = "user@example.com/remote";
31 const char kToken[] = "xyz123456";
32 const char kSharedSecret[] = "abcdefgh";
34 // Bad scope: no nonce element.
35 const char kBadScope[] =
36 "client:user@example.com/local host:user@example.com/remote";
38 class FakeProtocolHandler : public net::URLRequestJobFactory::ProtocolHandler {
39 public:
40 FakeProtocolHandler(const std::string& headers, const std::string& response)
41 : headers_(headers),
42 response_(response) {
44 net::URLRequestJob* MaybeCreateJob(
45 net::URLRequest* request,
46 net::NetworkDelegate* network_delegate) const override {
47 return new net::URLRequestTestJob(
48 request, network_delegate, headers_, response_, true);
51 private:
52 std::string headers_;
53 std::string response_;
56 class SetResponseURLRequestContext: public net::TestURLRequestContext {
57 public:
58 void SetResponse(const std::string& headers, const std::string& response) {
59 scoped_ptr<net::URLRequestJobFactoryImpl> factory =
60 make_scoped_ptr(new net::URLRequestJobFactoryImpl());
61 factory->SetProtocolHandler(
62 "https", make_scoped_ptr(new FakeProtocolHandler(headers, response)));
63 context_storage_.set_job_factory(factory.Pass());
67 } // namespace
69 namespace remoting {
71 class TokenValidatorFactoryImplTest : public testing::Test {
72 public:
73 TokenValidatorFactoryImplTest() : message_loop_(base::MessageLoop::TYPE_IO) {}
75 void SuccessCallback(const std::string& shared_secret) {
76 EXPECT_FALSE(shared_secret.empty());
77 message_loop_.Quit();
80 void FailureCallback(const std::string& shared_secret) {
81 EXPECT_TRUE(shared_secret.empty());
82 message_loop_.Quit();
85 void DeleteOnFailureCallback(const std::string& shared_secret) {
86 EXPECT_TRUE(shared_secret.empty());
87 token_validator_.reset();
88 message_loop_.Quit();
91 protected:
92 void SetUp() override {
93 key_pair_ = RsaKeyPair::FromString(kTestRsaKeyPair);
94 scoped_ptr<net::TestURLRequestContext> context(
95 new SetResponseURLRequestContext());
96 request_context_getter_ = new net::TestURLRequestContextGetter(
97 message_loop_.task_runner(), context.Pass());
98 ThirdPartyAuthConfig config;
99 config.token_url = GURL(kTokenUrl);
100 config.token_validation_url = GURL(kTokenValidationUrl);
101 config.token_validation_cert_issuer = kTokenValidationCertIssuer;
102 token_validator_factory_.reset(new TokenValidatorFactoryImpl(
103 config, key_pair_, request_context_getter_));
106 static std::string CreateResponse(const std::string& scope) {
107 base::DictionaryValue response_dict;
108 response_dict.SetString("access_token", kSharedSecret);
109 response_dict.SetString("token_type", "shared_secret");
110 response_dict.SetString("scope", scope);
111 std::string response;
112 base::JSONWriter::Write(response_dict, &response);
113 return response;
116 static std::string CreateErrorResponse(const std::string& error) {
117 base::DictionaryValue response_dict;
118 response_dict.SetString("error", error);
119 std::string response;
120 base::JSONWriter::Write(response_dict, &response);
121 return response;
125 void SetResponse(const std::string& headers, const std::string& response) {
126 SetResponseURLRequestContext* context =
127 static_cast<SetResponseURLRequestContext*>(
128 request_context_getter_->GetURLRequestContext());
129 context->SetResponse(headers, response);
132 base::MessageLoop message_loop_;
133 scoped_refptr<RsaKeyPair> key_pair_;
134 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
135 scoped_ptr<TokenValidatorFactoryImpl> token_validator_factory_;
136 scoped_ptr<protocol::TokenValidator> token_validator_;
139 TEST_F(TokenValidatorFactoryImplTest, Success) {
140 token_validator_ = token_validator_factory_->CreateTokenValidator(
141 kLocalJid, kRemoteJid);
143 SetResponse(net::URLRequestTestJob::test_headers(),
144 CreateResponse(token_validator_->token_scope()));
146 token_validator_->ValidateThirdPartyToken(
147 kToken, base::Bind(&TokenValidatorFactoryImplTest::SuccessCallback,
148 base::Unretained(this)));
149 message_loop_.Run();
152 TEST_F(TokenValidatorFactoryImplTest, BadToken) {
153 token_validator_ = token_validator_factory_->CreateTokenValidator(
154 kLocalJid, kRemoteJid);
156 SetResponse(net::URLRequestTestJob::test_error_headers(), std::string());
158 token_validator_->ValidateThirdPartyToken(
159 kToken, base::Bind(&TokenValidatorFactoryImplTest::FailureCallback,
160 base::Unretained(this)));
161 message_loop_.Run();
164 TEST_F(TokenValidatorFactoryImplTest, BadScope) {
165 token_validator_ = token_validator_factory_->CreateTokenValidator(
166 kLocalJid, kRemoteJid);
168 SetResponse(net::URLRequestTestJob::test_headers(),
169 CreateResponse(kBadScope));
171 token_validator_->ValidateThirdPartyToken(
172 kToken, base::Bind(&TokenValidatorFactoryImplTest::FailureCallback,
173 base::Unretained(this)));
174 message_loop_.Run();
177 TEST_F(TokenValidatorFactoryImplTest, DeleteOnFailure) {
178 token_validator_ = token_validator_factory_->CreateTokenValidator(
179 kLocalJid, kRemoteJid);
181 SetResponse(net::URLRequestTestJob::test_error_headers(), std::string());
183 token_validator_->ValidateThirdPartyToken(
184 kToken, base::Bind(
185 &TokenValidatorFactoryImplTest::DeleteOnFailureCallback,
186 base::Unretained(this)));
187 message_loop_.Run();
190 } // namespace remoting