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.
5 // A set of unit tests for TokenValidatorFactoryImpl
9 #include "base/json/json_writer.h"
10 #include "base/values.h"
11 #include "net/http/http_status_code.h"
12 #include "net/url_request/test_url_fetcher_factory.h"
13 #include "net/url_request/url_request_status.h"
14 #include "net/url_request/url_request_test_util.h"
15 #include "remoting/base/rsa_key_pair.h"
16 #include "remoting/base/test_rsa_key_pair.h"
17 #include "remoting/host/token_validator_factory_impl.h"
18 #include "testing/gtest/include/gtest/gtest.h"
23 const char kTokenUrl
[] = "https://example.com/token";
24 const char kTokenValidationUrl
[] = "https://example.com/validate";
25 const char kLocalJid
[] = "user@example.com/local";
26 const char kRemoteJid
[] = "user@example.com/remote";
27 const char kToken
[] = "xyz123456";
28 const char kSharedSecret
[] = "abcdefgh";
30 // Bad scope: no nonce element.
31 const char kBadScope
[] =
32 "client:user@example.com/local host:user@example.com/remote";
38 class TokenValidatorFactoryImplTest
: public testing::Test
{
40 TokenValidatorFactoryImplTest() : message_loop_(base::MessageLoop::TYPE_IO
) {}
42 void SuccessCallback(const std::string
& shared_secret
) {
43 EXPECT_FALSE(shared_secret
.empty());
47 void FailureCallback(const std::string
& shared_secret
) {
48 EXPECT_TRUE(shared_secret
.empty());
52 void DeleteOnFailureCallback(const std::string
& shared_secret
) {
53 EXPECT_TRUE(shared_secret
.empty());
54 token_validator_
.reset();
59 virtual void SetUp() OVERRIDE
{
60 key_pair_
= RsaKeyPair::FromString(kTestRsaKeyPair
);
61 request_context_getter_
= new net::TestURLRequestContextGetter(
62 message_loop_
.message_loop_proxy());
63 token_validator_factory_
.reset(new TokenValidatorFactoryImpl(
64 GURL(kTokenUrl
), GURL(kTokenValidationUrl
), key_pair_
,
65 request_context_getter_
));
68 static std::string
CreateResponse(const std::string
& scope
) {
69 base::DictionaryValue response_dict
;
70 response_dict
.SetString("access_token", kSharedSecret
);
71 response_dict
.SetString("token_type", "shared_secret");
72 response_dict
.SetString("scope", scope
);
74 base::JSONWriter::Write(&response_dict
, &response
);
78 static std::string
CreateErrorResponse(const std::string
& error
) {
79 base::DictionaryValue response_dict
;
80 response_dict
.SetString("error", error
);
82 base::JSONWriter::Write(&response_dict
, &response
);
86 base::MessageLoop message_loop_
;
87 scoped_refptr
<RsaKeyPair
> key_pair_
;
88 scoped_refptr
<net::URLRequestContextGetter
> request_context_getter_
;
89 scoped_ptr
<TokenValidatorFactoryImpl
> token_validator_factory_
;
90 scoped_ptr
<protocol::ThirdPartyHostAuthenticator::TokenValidator
>
94 TEST_F(TokenValidatorFactoryImplTest
, Success
) {
95 net::FakeURLFetcherFactory
factory(NULL
);
96 token_validator_
= token_validator_factory_
->CreateTokenValidator(
97 kLocalJid
, kRemoteJid
);
98 factory
.SetFakeResponse(
99 GURL(kTokenValidationUrl
),
100 CreateResponse(token_validator_
->token_scope()),
101 net::HTTP_OK
, net::URLRequestStatus::SUCCESS
);
102 token_validator_
->ValidateThirdPartyToken(
103 kToken
, base::Bind(&TokenValidatorFactoryImplTest::SuccessCallback
,
104 base::Unretained(this)));
108 TEST_F(TokenValidatorFactoryImplTest
, BadToken
) {
109 net::FakeURLFetcherFactory
factory(NULL
);
110 token_validator_
= token_validator_factory_
->CreateTokenValidator(
111 kLocalJid
, kRemoteJid
);
112 factory
.SetFakeResponse(GURL(kTokenValidationUrl
), std::string(),
113 net::HTTP_INTERNAL_SERVER_ERROR
,
114 net::URLRequestStatus::FAILED
);
115 token_validator_
->ValidateThirdPartyToken(
116 kToken
, base::Bind(&TokenValidatorFactoryImplTest::FailureCallback
,
117 base::Unretained(this)));
121 TEST_F(TokenValidatorFactoryImplTest
, BadScope
) {
122 net::FakeURLFetcherFactory
factory(NULL
);
123 token_validator_
= token_validator_factory_
->CreateTokenValidator(
124 kLocalJid
, kRemoteJid
);
125 factory
.SetFakeResponse(
126 GURL(kTokenValidationUrl
), CreateResponse(kBadScope
), net::HTTP_OK
,
127 net::URLRequestStatus::SUCCESS
);
128 token_validator_
->ValidateThirdPartyToken(
129 kToken
, base::Bind(&TokenValidatorFactoryImplTest::FailureCallback
,
130 base::Unretained(this)));
134 TEST_F(TokenValidatorFactoryImplTest
, DeleteOnFailure
) {
135 net::FakeURLFetcherFactory
factory(NULL
);
136 token_validator_
= token_validator_factory_
->CreateTokenValidator(
137 kLocalJid
, kRemoteJid
);
138 factory
.SetFakeResponse(GURL(kTokenValidationUrl
),
140 net::HTTP_INTERNAL_SERVER_ERROR
,
141 net::URLRequestStatus::FAILED
);
142 token_validator_
->ValidateThirdPartyToken(
144 &TokenValidatorFactoryImplTest::DeleteOnFailureCallback
,
145 base::Unretained(this)));
149 } // namespace remoting